# Reylo Type Defs

> A format for types, which says what values a column, a parameter or a field can hold.

A format for types.

Reylo writes the type of every parameter and column it [analyzes](https://reylo.dev/docs/inference) in it. A type says which values a slot can hold. That's enough to pick a representation in any language.

## Types

Each type is an object with a `kind`.

Most other fields narrow the kind: a size, bounds, a pattern, a character set. A program reads `kind` and the fields it needs. It can ignore the rest, since an unknown field only narrows or describes the type. A few fields widen a type instead, each named under its kind below.

```jsonc
// A 32-bit integer from 0 to 150, which Postgres calls int4
{ "kind": "integer", "bits": 32, "min": "0", "max": "150", "label": "int4" }
```

Four rules hold for every type:

- **Values inside a type are strings**, in the kind's canonical text. So a 64-bit bound or a date survives any encoding. Counts, such as `bits`, and code points are integers.
- **A flag appears only when it's true.** Its absence means false.
- **A label is the database's name** for the type, where it has one. It never changes what the type means.
- **A comment** is the source's documentation of a named type. **A title** is a short name for a reader, as in JSON Schema. The label is the database's name for the type. Neither says anything of the values.

## Slots

Where a value stands, with whether it may be null.

A slot is a column, a parameter, a field, or an element, key or value of a collection. It holds a `type`, and `nullable` where the value may be null. Nullability is on the slot, never in the type, since an outer join makes any column nullable.

```jsonc
// Text that may be null
{ "type": { "kind": "text" }, "nullable": true }
```

## Kinds

Every type has one of these kinds:

| Kind | What it holds |
| --- | --- |
| [`boolean`](https://reylo.dev/docs/typedefs#boolean) | A truth value |
| [`integer`](https://reylo.dev/docs/typedefs#integer) | A whole number of a fixed width |
| [`float`](https://reylo.dev/docs/typedefs#float) | An IEEE 754 binary float |
| [`decimal`](https://reylo.dev/docs/typedefs#decimal) | An exact decimal |
| [`text`](https://reylo.dev/docs/typedefs#text) | Text |
| [`bytes`](https://reylo.dev/docs/typedefs#bytes) | Bytes |
| [`uuid`](https://reylo.dev/docs/typedefs#uuid) | A 128-bit identifier |
| [`enum`](https://reylo.dev/docs/typedefs#enum) | One of a list of values |
| [`date`](https://reylo.dev/docs/typedefs#date) | A calendar date |
| [`time`](https://reylo.dev/docs/typedefs#time) | A time of day |
| [`datetime`](https://reylo.dev/docs/typedefs#datetime) | A date and time as a wall clock reads it |
| [`instant`](https://reylo.dev/docs/typedefs#instant) | A point on the timeline |
| [`zoned_datetime`](https://reylo.dev/docs/typedefs#zoned_datetime) | A point on the timeline, with its time zone |
| [`duration`](https://reylo.dev/docs/typedefs#duration) | A length of time |
| [`array`](https://reylo.dev/docs/typedefs#array) | Elements in order |
| [`tuple`](https://reylo.dev/docs/typedefs#tuple) | Values in order, with no names |
| [`struct`](https://reylo.dev/docs/typedefs#struct) | Named fields |
| [`map`](https://reylo.dev/docs/typedefs#map) | Keys to values |
| [`table`](https://reylo.dev/docs/typedefs#table) | Rows of columns |
| [`json`](https://reylo.dev/docs/typedefs#json) | A JSON value |
| [`union`](https://reylo.dev/docs/typedefs#union) | One of several types |
| [`intersection`](https://reylo.dev/docs/typedefs#intersection) | Values of every member at once |
| [`tagged_union`](https://reylo.dev/docs/typedefs#tagged_union) | One of several alternatives, named by a tag |
| [`range`](https://reylo.dev/docs/typedefs#range) | A range of an ordered type |
| [`multirange`](https://reylo.dev/docs/typedefs#multirange) | Non-overlapping ranges |
| [`geometry`](https://reylo.dev/docs/typedefs#geometry) | An OGC simple feature |
| [`xml`](https://reylo.dev/docs/typedefs#xml) | XML |
| [`null`](https://reylo.dev/docs/typedefs#null) | Always null |
| [`opaque`](https://reylo.dev/docs/typedefs#opaque) | A type the format doesn't structure |
| [`unknown`](https://reylo.dev/docs/typedefs#unknown) | Nothing is known |

### `boolean`

A truth value.

A comparison's result is one on every database, though MySQL and SQLite deliver it as 1 or 0.

```jsonc
// Any truth value
{ "kind": "boolean" }
```

Holds: `true`, `false`

Databases:

- **Postgres:** `boolean`
- **ClickHouse:** `Bool`

### `integer`

A whole number of a fixed width.

Its range follows from `bits`, two's complement unless `unsigned`. A bound is always inclusive, since `x > 0` is `x >= 1`.

```jsonc
// A 32-bit integer
{ "kind": "integer", "bits": 32 }

// A 64-bit unsigned integer
{ "kind": "integer", "bits": 64, "unsigned": true }

// From 0 to 150, and a multiple of 5
{ "kind": "integer", "bits": 32, "min": "0", "max": "150", "multiple_of": "5" }
```

Holds: `-2147483648`, `0`, `2147483647`

Databases:

- **Postgres:** `smallint`, `integer`, `bigint`, `oid`
- **MySQL:** `tinyint`, `smallint`, `mediumint`, `int`, `bigint`, `year`, `boolean`
- **SQLite:** `integer`
- **ClickHouse:** `Int8`, `Int16`, `Int32`, `Int64`, `Int128`, `Int256`, `UInt8`, `UInt16`, `UInt32`, `UInt64`, `UInt128`, `UInt256`

### `float`

An [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754) binary float.

Its exponent and mantissa widths fix its range and precision. A program picks the smallest float it has that holds both. It holds the infinities and NaN only where `infinity` and `nan` say so.

```jsonc
// A double
{ "kind": "float", "exponent_bits": 11, "mantissa_bits": 52 }

// A double that also holds Infinity, -Infinity and NaN
{ "kind": "float", "exponent_bits": 11, "mantissa_bits": 52, "infinity": true, "nan": true }

// A single-precision float above 0
{ "kind": "float", "exponent_bits": 8, "mantissa_bits": 23, "exclusive_min": "0" }
```

Holds: `1.5`, `-0`, `Infinity`, `NaN`

Databases:

- **Postgres:** `real`, `double precision`
- **MySQL:** `float`, `double`
- **SQLite:** `real`
- **ClickHouse:** `Float32`, `Float64`, `BFloat16`

### `decimal`

An exact decimal.

Its `precision` counts its digits, and its scale the digits after the point. A decimal is a number, so `1.5` and `1.50` are one value.

```jsonc
// Up to 10 digits, 2 after the point
{ "kind": "decimal", "precision": 10, "min_scale": 2, "max_scale": 2 }

// Any decimal below 10^131072, NaN and the infinities too
{ "kind": "decimal", "min_scale": 0, "max_scale": 16383, "exclusive_min": "-1e131072", "exclusive_max": "1e131072", "infinity": true, "nan": true }
```

Holds: `1.50`, `99999999.99`, `NaN`

Databases:

- **Postgres:** `numeric`
- **MySQL:** `decimal`
- **ClickHouse:** `Decimal`

### `text`

With no `encoding`, it's any valid Unicode text. An `encoding` names its character set, from a [fixed list](https://reylo.dev/docs/typedefs#specification). With `ill_formed`, a value may be any sequence of that set's code units. A `code_points` field narrows the characters further. Its size counts code points unless its `unit` says otherwise. A `pattern` is an [I-Regexp](https://www.rfc-editor.org/rfc/rfc9485) every value matches whole. A `format` names what the text is meant to be, by JSON Schema's format names or a source's own. It says nothing of the values, so a rule every value keeps is a `pattern`.

```jsonc
// At most 255 code points
{ "kind": "text", "size": { "max": 255 } }

// Any character but NUL, as Postgres's text holds
{ "kind": "text", "code_points": [[1, 1114111]] }

// Text with an @ in it
{ "kind": "text", "pattern": "[^@]+@[^@]+" }

// Text meant to be an email address, by JSON Schema's format name
{ "kind": "text", "format": "email" }

// Text Latin-1 holds, as MySQL's latin1 stores it
{ "kind": "text", "encoding": "windows-1252" }

// At most 65,535 bytes of UTF-8
{ "kind": "text", "encoding": "UTF-8", "size": { "unit": "byte", "max": 65535 } }

// Bytes meant as UTF-8 that nothing checked, which may not be valid Unicode
{ "kind": "text", "encoding": "UTF-8", "ill_formed": true }
```

Holds: `"ada@example.com"`, `"happy"`, `""`

Databases:

- **Postgres:** `text`, `varchar`, `char`, `name`
- **MySQL:** `char`, `varchar`, `tinytext`, `text`, `mediumtext`, `longtext`
- **SQLite:** `text`
- **ClickHouse:** `String`, `FixedString`, `IPv4`, `IPv6`

### `bytes`

A value's length is stated where the type fixes one.

```jsonc
// Any number of bytes
{ "kind": "bytes" }

// Exactly 16 bytes
{ "kind": "bytes", "size": { "exact": 16 } }

// A PNG image
{ "kind": "bytes", "media_type": "image/png" }
```

Holds: `0x0102`, `0x`

Databases:

- **Postgres:** `bytea`, `macaddr`, `macaddr8`
- **MySQL:** `binary`, `varbinary`, `tinyblob`, `blob`, `mediumblob`, `longblob`
- **SQLite:** `blob`

### `uuid`

A 128-bit identifier.

It holds any 128 bits, written in [RFC 9562](https://www.rfc-editor.org/rfc/rfc9562)'s text form. Its `versions` say every value has one of those versions. A `uuidv7()` or `gen_random_uuid()` call in the statement sets it. So does a `CHECK` that holds `uuid_extract_version` to one version. A column with a `default uuidv7()` has none, since an insert may write any UUID.

```jsonc
// Any 128 bits
{ "kind": "uuid" }

// A version 7 UUID
{ "kind": "uuid", "versions": [7] }
```

Holds: `019a6b2c-3d4e-7f80-9a1b-2c3d4e5f6071`

Databases:

- **Postgres:** `uuid`
- **ClickHouse:** `UUID`

### `enum`

One of a list of values.

Its `type` is the type of every value. Each of its `values` is an object, whose `value` is written in that type's canonical text. One value makes a literal, which a constant the statement writes is: `select 'user' as kind` gives the enum of `"user"`.

```jsonc
// One of two strings
{ "kind": "enum", "type": { "kind": "text" }, "values": [{ "value": "happy" }, { "value": "sad" }] }

// The integer 1
{ "kind": "enum", "type": { "kind": "integer", "bits": 32 }, "values": [{ "value": "1" }] }

// The literal true
{ "kind": "enum", "type": { "kind": "boolean" }, "values": [{ "value": "true" }] }
```

Holds: `"happy"`, `1`

Databases:

- **Postgres:** an enum, a column a CHECK keeps to a list, a constant
- **MySQL:** `enum`, a constant
- **SQLite:** a constant
- **ClickHouse:** `Enum8`, `Enum16`

### `date`

A calendar date.

Its bounds are [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) text, with a sign and more than four digits where a year needs them. With no bounds, it runs from 0001-01-01 to 9999-12-31.

```jsonc
// Any date from 0001 to 9999
{ "kind": "date" }

// From 4714 BC to the year 5874897, and infinity and -infinity too
{ "kind": "date", "min": "-4713-11-24", "max": "+5874897-12-31", "infinity": true }
```

Holds: `2026-11-04`, `-infinity`

Databases:

- **Postgres:** `date`
- **MySQL:** `date`
- **ClickHouse:** `Date`, `Date32`

### `time`

A time of day.

Its `tick` is its smallest step, in seconds. It holds 24:00:00 only where its bounds say so, and keeps a UTC offset where `offset` says so.

```jsonc
// To the microsecond, up to 24:00:00
{ "kind": "time", "tick": "1/1000000", "max": "24:00:00" }

// To the second, with its UTC offset
{ "kind": "time", "tick": "1", "offset": true }
```

Holds: `09:30:00`, `24:00:00`

Databases:

- **Postgres:** `time`, `timetz`

### `datetime`

A date and time as a wall clock reads it.

With no zone, it can't be read as an instant.

```jsonc
// To the millisecond
{ "kind": "datetime", "tick": "1/1000" }
```

Holds: `2026-11-04T09:30:00.123`

Databases:

- **Postgres:** `timestamp`
- **MySQL:** `datetime`

### `instant`

A point on the timeline.

It's the moment alone, as [Temporal](https://tc39.es/proposal-temporal/docs/instant.html)'s `Instant` is, written in UTC however a session prints it. Its bounds are in UTC.

```jsonc
// To the microsecond, and infinity and -infinity too
{ "kind": "instant", "tick": "1/1000000", "infinity": true }
```

Holds: `2026-11-04T17:30:00.123456Z`

Databases:

- **Postgres:** `timestamptz`
- **MySQL:** `timestamp`
- **ClickHouse:** `DateTime`, `DateTime64`

### `zoned_datetime`

A point on the timeline, with the time zone it was written in.

Each value keeps its zone, a region or a UTC offset, as [Temporal](https://tc39.es/proposal-temporal/docs/zoneddatetime.html)'s `ZonedDateTime` does. It's written in [RFC 9557](https://www.rfc-editor.org/rfc/rfc9557)'s form. With `offset_only`, every zone is an offset.

```jsonc
// To the millisecond, in any zone
{ "kind": "zoned_datetime", "tick": "1/1000" }

// To 100 nanoseconds, with an offset, as SQL Server's datetimeoffset keeps it
{ "kind": "zoned_datetime", "tick": "1/10000000", "offset_only": true }
```

Holds: `2026-11-04T09:30:00-08:00[America/Los_Angeles]`, `2026-11-04T09:30:00-08:00[-08:00]`

### `duration`

A length of time.

Its parts are months, days and a time, each signed on its own. So 1 month and 30 days are two values. Its `max` is an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) duration of each part's largest magnitude.

```jsonc
// Months, days and a time to the microsecond
{ "kind": "duration", "max": "P2147483648M2147483648DT2562047788H54.775808S", "tick": "1/1000000" }

// A time alone, up to 838 hours, to the second
{ "kind": "duration", "max": "PT838H59M59S", "tick": "1" }
```

Holds: `P1M3D`, `PT1H30M`, `P-2D`

Databases:

- **Postgres:** `interval`
- **MySQL:** `time`

### `array`

Elements, in order.

Each element is a slot of its own, so it says whether it may be null. An array is flat: a two-dimensional one is an array of arrays. A [pgvector](https://github.com/pgvector/pgvector) vector is an array of floats. MySQL's `SET` is a `distinct` array, whose elements never repeat.

```jsonc
// Integers, each of which may be null
{ "kind": "array", "element": { "type": { "kind": "integer", "bits": 32 }, "nullable": true } }

// Exactly 3 floats
{ "kind": "array", "size": { "exact": 3 }, "element": { "type": { "kind": "float", "exponent_bits": 8, "mantissa_bits": 23 } } }

// Members that never repeat
{ "kind": "array", "distinct": true, "element": { "type": { "kind": "enum", "type": { "kind": "text" }, "values": [{ "value": "a" }, { "value": "b" }, { "value": "c" }] } } }
```

Holds: `[1, null, 3]`, `[]`

Databases:

- **Postgres:** any array, `bit`, `varbit`, `lseg`, `polygon`, `vector`, `halfvec`
- **MySQL:** `set`, `bit`
- **ClickHouse:** `Array`, `Nested`, `Map`

### `tuple`

Values in order, each of its own type, with no names.

Its `elements` are always there. Its `optional_elements` may be missing from the end, and its `rest` takes any number after them.

```jsonc
// An integer, then text that may be null
{ "kind": "tuple", "elements": [{ "type": { "kind": "integer", "bits": 32 } }, { "type": { "kind": "text" }, "nullable": true }] }

// Text, then any number of integers
{ "kind": "tuple", "elements": [{ "type": { "kind": "text" } }], "rest": { "type": { "kind": "integer", "bits": 32 } } }
```

Holds: `[1, "a"]`, `[2, null]`

Databases:

- **ClickHouse:** a Tuple without names, `Point`

### `struct`

Named fields, in order.

Each field is a slot with a `name`. It may be `optional`, its key left out, with a `default` the key takes when it is. A struct holds exactly its fields, unless `extra` lets it hold other keys too, each a value in its slot. A field may have a `title`.

```jsonc
// A point
{
  "kind": "struct",
  "fields": [
    { "name": "x", "type": { "kind": "float", "exponent_bits": 11, "mantissa_bits": 52 } },
    { "name": "y", "type": { "kind": "float", "exponent_bits": 11, "mantissa_bits": 52 } }
  ]
}

// An optional field, with its default and its comment
{
  "kind": "struct",
  "fields": [
    { "name": "id", "type": { "kind": "integer", "bits": 64 } },
    { "name": "role", "optional": true, "default": "member", "comment": "What the user may do", "type": { "kind": "enum", "type": { "kind": "text" }, "values": [{ "value": "admin" }, { "value": "member" }] } }
  ]
}

// An open object, whose other keys hold text
{
  "kind": "struct",
  "fields": [{ "name": "id", "type": { "kind": "integer", "bits": 64 } }],
  "extra": { "type": { "kind": "text" } }
}

// A title on the struct and on a field, beside the database's name for the type
{
  "kind": "struct",
  "title": "Mailing address",
  "label": "public.address",
  "fields": [{ "name": "zip", "title": "ZIP code", "type": { "kind": "text" } }]
}
```

Holds: `{ "x": 1, "y": 2 }`

Databases:

- **Postgres:** a composite type, `point`, `line`, `box`, `path`, `circle`
- **ClickHouse:** a Tuple with names

### `map`

Keys to values, each key once.

The key is a slot, as the value is, and the `size` counts the keys. A map whose keys may repeat is an array of `{ key, value }` structs.

```jsonc
// Text keys to text that may be null
{ "kind": "map", "key": { "type": { "kind": "text" } }, "value": { "type": { "kind": "text" }, "nullable": true } }

// At most 64 keys
{ "kind": "map", "key": { "type": { "kind": "text" } }, "value": { "type": { "kind": "integer", "bits": 32 } }, "size": { "max": 64 } }
```

Holds: `{ "color": "red", "size": null }`

Databases:

- **Postgres:** `hstore`

### `table`

Rows of columns.

It's what a statement returns. Its `cardinality` is `one`, `at_most_one`, `many` or `none`. Each column is a field, never optional. Where there may be many rows, `unique` lists the sets of columns no two rows share. The `order` field lists the columns the rows are sorted on.

```jsonc
// Rows, unique by id, newest first
{
  "kind": "table",
  "cardinality": "many",
  "columns": [
    { "name": "id", "type": { "kind": "integer", "bits": 64 } },
    { "name": "created_at", "type": { "kind": "instant", "tick": "1/1000000" } }
  ],
  "unique": [["id"]],
  "order": [{ "column": "created_at", "desc": true, "nulls": "first" }]
}

// One row, a count
{ "kind": "table", "cardinality": "one", "columns": [{ "name": "n", "type": { "kind": "integer", "bits": 64 } }] }
```

Holds: `[{ "id": 1 }, { "id": 2 }]`

Databases:

- **Postgres:** a statement's result
- **MySQL:** a statement's result
- **SQLite:** a statement's result
- **ClickHouse:** a statement's result

### `json`

A [JSON](https://www.rfc-editor.org/rfc/rfc8259) value.

Its `content` is what a statement built, in these same types, and its `schema` a [JSON Schema](https://json-schema.org) every value satisfies. With neither, it's any JSON.

```jsonc
// Any JSON value
{ "kind": "json" }

// An object of an id and a title
{
  "kind": "json",
  "content": {
    "type": {
      "kind": "struct",
      "fields": [
        { "name": "id", "type": { "kind": "integer", "bits": 64 } },
        { "name": "title", "type": { "kind": "text" } }
      ]
    }
  }
}

// What a JSON Schema admits
{ "kind": "json", "schema": { "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "required": ["x"] } }
```

Holds: `{ "id": 1, "title": "Hello" }`, `null`

Databases:

- **Postgres:** `json`, `jsonb`
- **MySQL:** `json`

### `union`

One of several types.

A union is flat, of two members or more, and lists its members in the order of the kinds. A union of structs that each give one field a different literal is a discriminated union. Each value says which member it is by that field.

```jsonc
// An integer or text
{ "kind": "union", "members": [{ "kind": "integer", "bits": 64 }, { "kind": "text" }] }

// A cat or a dog, which its type field names
{
  "kind": "union",
  "members": [
    { "kind": "struct", "fields": [{ "name": "type", "type": { "kind": "enum", "type": { "kind": "text" }, "values": [{ "value": "cat" }] } }, { "name": "meows", "type": { "kind": "boolean" } }] },
    { "kind": "struct", "fields": [{ "name": "type", "type": { "kind": "enum", "type": { "kind": "text" }, "values": [{ "value": "dog" }] } }, { "name": "barks", "type": { "kind": "boolean" } }] }
  ]
}
```

Holds: `12`, `"abc"`

Databases:

- **Postgres:** `inet`, `cidr`
- **SQLite:** any column outside a strict table
- **ClickHouse:** `Variant`

### `intersection`

Values of every member at once.

It's JSON Schema's `allOf` and TypeScript's `&`. An intersection that one struct can express is written as that struct.

```jsonc
// A cat or a dog, with an id beside whichever it is
{
  "kind": "intersection",
  "members": [
    {
      "kind": "union",
      "members": [
        { "kind": "struct", "fields": [{ "name": "meows", "type": { "kind": "boolean" } }], "extra": { "type": { "kind": "unknown" }, "nullable": true } },
        { "kind": "struct", "fields": [{ "name": "barks", "type": { "kind": "boolean" } }], "extra": { "type": { "kind": "unknown" }, "nullable": true } }
      ]
    },
    { "kind": "struct", "fields": [{ "name": "id", "type": { "kind": "integer", "bits": 64 } }], "extra": { "type": { "kind": "unknown" }, "nullable": true } }
  ]
}
```

Holds: `{ "id": 1, "meows": true }`

### `tagged_union`

One of several alternatives, each named by a tag.

Every value carries its tag beside the value, even where the value is null. Unlike a union's members, two alternatives may hold one type.

```jsonc
// A number or text, each tagged
{
  "kind": "tagged_union",
  "alternatives": [
    { "tag": "num", "type": { "kind": "integer", "bits": 32 }, "nullable": true },
    { "tag": "str", "type": { "kind": "text" }, "nullable": true }
  ]
}
```

Holds: `{ "tag": "num", "value": 1 }`

### `range`

A range of an ordered type.

It may be empty, and either end may be unbounded.

```jsonc
// A range of instants
{ "kind": "range", "subtype": { "kind": "instant", "tick": "1/1000000" } }
```

Holds: `[2026-11-04T09:00Z, 2026-11-04T10:00Z)`, `empty`

Databases:

- **Postgres:** `int4range`, `int8range`, `numrange`, `tsrange`, `tstzrange`, `daterange`

### `multirange`

Non-overlapping ranges, in order.

```jsonc
// Ranges of integers
{ "kind": "multirange", "subtype": { "kind": "integer", "bits": 32 } }
```

Holds: `{[1,3), [5,8)}`, `{}`

Databases:

- **Postgres:** `int4multirange`, `int8multirange`, `nummultirange`, `tsmultirange`, `tstzmultirange`, `datemultirange`

### `geometry`

An [OGC simple feature](https://www.ogc.org/standards/sfa/).

It may name its model, its spatial reference, its subtype and its coordinates.

```jsonc
// A point in WGS 84, with X and Y
{ "kind": "geometry", "srid": 4326, "subtype": "point", "coordinates": "xy" }

// Any shape on the sphere
{ "kind": "geometry", "model": "geodetic" }
```

Holds: `SRID=4326;POINT(-71.06 42.36)`

Databases:

- **Postgres:** `geometry`, `geography`
- **MySQL:** `geometry`, `point`, `linestring`, `polygon`, `multipoint`, `multilinestring`, `multipolygon`, `geometrycollection`

### `xml`

A document with one root, or with `fragment` several.

```jsonc
// A document
{ "kind": "xml" }

// A fragment, which may have several roots
{ "kind": "xml", "fragment": true }
```

Holds: `<a/>`, `<a/><b/>`

Databases:

- **Postgres:** `xml`

### `null`

Always null.

It stands in a nullable slot, for a value a statement always writes as null.

```jsonc
// The field a json_build_object('a', null) writes
{ "kind": "null" }
```

Holds: `null`

Databases:

- **Postgres:** a null the statement writes
- **MySQL:** a null the statement writes
- **ClickHouse:** a null the statement writes

### `opaque`

A type the format doesn't structure.

Its `label` is all the format says. A program that meets a kind it doesn't know reads it as this one.

```jsonc
// A Postgres extension's type, known by its name
{ "kind": "opaque", "label": "ltree" }
```

Holds: `top.science.physics`

Databases:

- **Postgres:** `money`, `tsvector`, `tsquery`, `pg_lsn`, an extension's type

### `unknown`

Nothing is known about the values.

A parameter nothing in the statement types is one, where [`allow_untyped_params`](https://reylo.dev/docs/configuration#allow_untyped_params) lets it through.

```jsonc
// Any value
{ "kind": "unknown" }
```

Databases:

- **SQLite:** any column in a strict table

## Specification

JSON Schema: https://reylo.dev/typedefs/preview.json

Every type and field, as TypeScript, generated from the format's JSON Schema:

```ts
export type Type =
  | BooleanType
  | IntegerType
  | FloatType
  | DecimalType
  | TextType
  | BytesType
  | UuidType
  | EnumType
  | DateType
  | TimeType
  | DateTimeType
  | InstantType
  | ZonedDateTimeType
  | DurationType
  | ArrayType
  | TupleType
  | StructType
  | MapType
  | TableType
  | JsonType
  | UnionType
  | IntersectionType
  | TaggedUnionType
  | RangeType
  | MultirangeType
  | GeometryType
  | XmlType
  | NullType
  | OpaqueType
  | UnknownType;

export interface Base {
  label?: string;
  comment?: string;
  title?: string;
}

export interface InclusivelyBounded {
  min?: string;
  max?: string;
}

export interface Bounded extends InclusivelyBounded {
  exclusive_min?: string;
  exclusive_max?: string;
}

export interface Size {
  exact?: number;
  min?: number;
  max?: number;
}

export interface Sized {
  size?: Size;
}

export interface BooleanType extends Base {
  kind: "boolean";
}

export interface IntegerType extends Base, InclusivelyBounded {
  kind: "integer";
  bits: number;
  unsigned?: true;
  multiple_of?: string;
}

export interface FloatType extends Base, Bounded {
  kind: "float";
  exponent_bits: number;
  mantissa_bits: number;
  infinity?: true;
  nan?: true;
}

export interface DecimalType extends Base, Bounded {
  kind: "decimal";
  precision?: number;
  min_scale?: number;
  max_scale?: number;
  infinity?: true;
  nan?: true;
  multiple_of?: string;
}

export interface TextType extends Base {
  kind: "text";
  encoding?: Charset;
  ill_formed?: true;
  code_points?: [number, number][];
  size?: TextSize;
  pattern?: string;
  media_type?: string;
  format?: string;
}

export interface TextSize extends Size {
  unit?: "code_unit" | "byte";
}

export type Charset =
  | "UTF-8"
  | "UTF-16"
  | "UTF-32"
  | "CESU-8"
  | "GB18030"
  | "US-ASCII"
  | "ISO-8859-1"
  | "ISO-8859-2"
  | "ISO-8859-3"
  | "ISO-8859-4"
  | "ISO-8859-5"
  | "ISO-8859-6"
  | "ISO-8859-7"
  | "ISO-8859-8"
  | "ISO-8859-9"
  | "ISO-8859-10"
  | "ISO-8859-13"
  | "ISO-8859-14"
  | "ISO-8859-15"
  | "ISO-8859-16"
  | "windows-874"
  | "windows-1250"
  | "windows-1251"
  | "windows-1252"
  | "windows-1253"
  | "windows-1254"
  | "windows-1255"
  | "windows-1256"
  | "windows-1257"
  | "windows-1258"
  | "IBM437"
  | "IBM850"
  | "IBM852"
  | "IBM866"
  | "KOI8-R"
  | "KOI8-U"
  | "macintosh"
  | "TIS-620"
  | "DEC-MCS"
  | "hp-roman8"
  | "SEN_850200_B"
  | "Shift_JIS"
  | "Windows-31J"
  | "EUC-JP"
  | "ISO-2022-JP"
  | "EUC-KR"
  | "GB2312"
  | "GBK"
  | "Big5"
  | "Big5-HKSCS";

export interface BytesType extends Base, Sized {
  kind: "bytes";
  media_type?: string;
}

export interface UuidType extends Base {
  kind: "uuid";
  versions?: number[];
}

export interface EnumType extends Base {
  kind: "enum";
  type: Type;
  values: EnumValue[];
}

export interface EnumValue {
  value: string;
  name?: string;
}

export interface DateType extends Base, Bounded {
  kind: "date";
  infinity?: true;
}

export interface TimeType extends Base, Bounded {
  kind: "time";
  tick: string;
  offset?: true;
}

export interface DateTimeType extends Base, Bounded {
  kind: "datetime";
  tick: string;
  infinity?: true;
}

export interface InstantType extends Base, Bounded {
  kind: "instant";
  tick: string;
  infinity?: true;
}

export interface ZonedDateTimeType extends Base, Bounded {
  kind: "zoned_datetime";
  tick: string;
  offset_only?: true;
  infinity?: true;
}

export interface DurationType extends Base {
  kind: "duration";
  max: string;
  tick?: string;
  infinity?: true;
}

export interface ArrayType extends Base, Sized {
  kind: "array";
  element: Slot;
  distinct?: true;
}

export interface TupleType extends Base {
  kind: "tuple";
  elements: Slot[];
  optional_elements?: Slot[];
  rest?: Slot;
}

export interface StructType extends Base {
  kind: "struct";
  fields: Field[];
  extra?: Slot;
}

export interface MapType extends Base, Sized {
  kind: "map";
  key: Slot;
  value: Slot;
}

export interface TableType extends Base {
  kind: "table";
  cardinality: "one" | "at_most_one" | "many" | "none";
  columns: Field[];
  unique?: string[][];
  order?: SortKey[];
}

export interface SortKey {
  column: string;
  desc?: true;
  nulls: "first" | "last";
}

export interface JsonType extends Base {
  kind: "json";
  content?: Slot;
  schema?: { $schema: string; [keyword: string]: unknown; };
}

export interface UnionType extends Base {
  kind: "union";
  members: Type[];
}

export interface IntersectionType extends Base {
  kind: "intersection";
  members: Type[];
}

export interface TaggedUnionType extends Base {
  kind: "tagged_union";
  alternatives: Alternative[];
}

export interface Alternative extends Slot {
  tag: string;
}

export interface RangeType extends Base {
  kind: "range";
  subtype: Type;
}

export interface MultirangeType extends Base {
  kind: "multirange";
  subtype: Type;
}

export interface GeometryType extends Base {
  kind: "geometry";
  model?: "planar" | "geodetic";
  srid?: number;
  subtype?: "point" | "linestring" | "polygon" | "multipoint" | "multilinestring" | "multipolygon" | "geometrycollection";
  coordinates?: "xy" | "xyz" | "xym" | "xyzm";
}

export type XmlType = XmlDocument | XmlFragment;

export interface XmlDocument extends Base {
  kind: "xml";
  fragment?: false;
  schemas?: string[];
}

export interface XmlFragment extends Base {
  kind: "xml";
  fragment: true;
}

export interface NullType {
  kind: "null";
}

export interface OpaqueType extends Base {
  kind: "opaque";
  label: string;
}

export interface UnknownType {
  kind: "unknown";
}

export interface StatementDescriptor {
  $schema: "https://reylo.dev/typedefs/preview.json";
  dialect: Dialect;
  sql: string;
  kind?: "select" | "insert" | "update" | "delete" | "merge" | "other";
  comment?: string;
  params: Param[];
  result: TableType;
  capabilities?: Capabilities;
  diagnostics: Diagnostic[];
}

export interface Diagnostic {
  message: string;
  start: number;
  end: number;
  code?: string;
}

export interface Slot {
  type: Type;
  nullable?: true;
}

export interface Field extends Slot {
  name: string;
  optional?: true;
  default?: string;
  comment?: string;
  title?: string;
}

export interface Param extends Slot {
  name: string;
  optional?: true;
  comment?: string;
}

export interface Dialect {
  name: string;
  version: string;
}

export interface Capabilities {
  tables: { [table: string]: Access; } | "unknown";
  ddl?: true;
  transaction?: true;
  session?: true;
  external?: true;
}

export type Access = "read" | "readlock" | "write";
```
