Composite, Range, Network & Bit Types

User-defined composite types, enums, typed multi-dimensional arrays, ranges, hstore, xml, inet/cidr/macaddr, bit strings, and exclusion constraints.

2 min read SpydQL & Data Modeling

Beyond standard integers, floating-point values, decimal(p, s), text, timestamp, json, vectors, and geospatial primitives documented in Schemas & Data Types, SpydDB stores rich structured and domain-specific column types natively in rows, B+ tree indexes, and PostgreSQL Wire / gRPC responses.


1. User-Defined Composite Types (CREATE TYPE)#

Declare reusable record types inside the default schema or a named schema, compose them hierarchically, and index or query nested fields directly:

sql
create schema sales;

create type pair as {
  a: u64,
  b: text
};

create type sales.pair as {
  a: u64,
  b: text
};

-- Nested composite type + multidimensional typed array
create type order_box as {
  item: pair,
  grid: u64[][]
};

create collection shipments {
  id: u64 primary,
  box: order_box
};

shipments | insert {
  id: 1,
  box: {
    item: { a: 7, b: "fragile" },
    grid: [[10, 20], [30]]
  }
};

-- Filter directly using nested paths and 1-based array indexing
shipments
| where box.item.a == 7 and box.grid[2][1] == 30
| pick id, box.item.b;

Composite Guardrails & Dependencies#

  • Empty composites ({}), duplicate field names, built-in type names (such as text), and references to undefined member types are rejected when created.
  • drop type <name> fails closed if another composite type or live collection still depends on it. Drop dependent objects first, or use drop type if exists <name>.

2. Enumerated Types (enum(...))#

Column-level enums validate every inserted or updated string at write time, sort by declaration order (not alphabetically), and support B+ tree secondary indexes:

sql
create collection tickets {
  id: u64 primary,
  priority: enum("low", "medium", "high", "urgent"),
  index by_priority on priority
};

tickets | insert { id: 1, priority: "urgent" };
tickets | insert { id: 2, priority: "low" };

-- Rejects any value outside the declared enum set
-- Sorts in declaration order: id 2 ("low") then id 1 ("urgent")
tickets | sort priority | pick id, priority;

3. Typed Arrays (T[])#

Typed arrays enforce homogeneous element types, allow null elements, support nesting (u64[][]), and can be unnested into relational rows:

sql
create collection articles {
  id: u64 primary,
  tags: text[],
  scores: u64[]
};

articles | insert {
  id: 1,
  tags: ["rust", "storage"],
  scores: [95, 88, null]
};

-- Mixed element types like [1] in text[] or wrong nesting depth fail on write
articles
| where array_length(tags) == 2
    and array_contains(tags, "rust")
    and tags[1] == "rust"
| pick id;

-- Flatten arrays into one row per non-null element
articles | unnest tags as tag | sort tag | pick id, tag;

Important — 1-based vs. 0-based indexing: Typed SQL/SpydQL arrays (text[], u64[]) use 1-based indexing—tags[1] returns the first element, while tags[0] and out-of-range indexes return null. Untyped json array members follow JSON convention and use 0-based indexing (doc.items[0]).


4. Range Types (int4range, int8range, daterange, tsrange)#

Store validated [lower, upper) bounds and test containment or overlaps cleanly:

sql
create collection bookings {
  id: u64 primary,
  window_span: int8range,
  active_dates: daterange
};

bookings | insert {
  id: 1,
  window_span: int8range(100, 200),
  active_dates: daterange(date("2026-01-01"), date("2026-02-01"))
};

-- Lower bound > upper bound (e.g. int8range(10, 1)) is rejected at write time
bookings
| where range_contains(window_span, 150)
    and range_overlaps(window_span, int8range(180, 220))
| extend lower(window_span) as lo, upper(window_span) as hi
| pick id, lo, hi;

See the Built-in Functions Reference for range_contains, range_overlaps, lower, upper, and is_empty.


5. Network, Bit String, HSTORE & XML Types#

TypeSyntax / PurposeValidation & Indexing
inetIPv4 or IPv6 host/interface address (192.0.2.10)Parsed at write time; sortable & B+ tree indexable
cidrIPv4 or IPv6 network block (192.0.2.0/24)Canonical network validation; sortable
macaddrIEEE-802 6-byte MAC address (08:00:2b:01:02:03)Validated & binary-comparable
bit(n) / varbitFixed-width or varying bit strings ("10101010")Enforces exact bit length for bit(n)
hstoreFlat string key-to-(string or null) mapshstore_get, hstore_has_key, hstore_keys, hstore_values
xmlWell-formed XML documentsParsed on insert/update; invalid XML fails closed
sql
create collection network_inventory {
  id: u64 primary,
  ip: inet,
  net: cidr,
  mac: macaddr,
  flags: bit(8),
  doc: xml,
  attrs: hstore,
  index by_ip on ip
};

network_inventory | where ip == "192.0.2.10" | sort ip | pick id;

6. Exclusion Constraints (EXCLUDE)#

Enforce custom conflict predicates across rows in the same collection—such as guaranteeing unique values or non-overlapping keys during ACID inserts and updates:

sql
create collection resource_locks {
  id: u64 primary,
  code: text,
  exclude (code with =)
};

Writes that violate an exclusion predicate fail with a constraint violation inside the active transaction without leaving partial index state behind.