# iceberg.types

> A module of iceberg's API.

Iceberg data types — primitives, nested types, and their JSON forms.

Iceberg types are recursive (a struct holds fields whose types may be structs).
Mojo has no easy recursive value type, so a whole type tree lives in a
`TypeStore` arena: every node is an entry in `TypeStore.nodes` and children are
referred to by `Int` index. A "type" in this module is therefore always a
`(store, index)` pair; `TypeStore` owns the memory.

All primitives from format versions 1, 2 and 3 are covered, including the v3
additions `unknown`, `timestamp_ns`, `timestamptz_ns`, `variant`,
`geometry(C)` and `geography(C, A)`. A primitive the reader does not recognise
is kept as `UNKNOWN_PRIM` with its original spelling in `TypeNode.raw`, so
forward-compatible metadata (the spec requires readers to tolerate it) still
parses instead of blowing up.

## Structs

### `NestedField`

```mojo
struct NestedField
```

A field of a struct type: id, name, requiredness and a type index.

Implements: `Copyable`, `Movable`, `Writable`

Fields:

- `id: Int`
- `name: String`
- `required: Bool`
- `type: Int`: Index into the owning `TypeStore`.
- `doc: String`
- `has_doc: Bool`
- `initial_default: String`: Raw JSON text of `initial-default`, or "" when absent.
- `has_initial_default: Bool`
- `write_default: String`
- `has_write_default: Bool`

#### `simple`

```mojo
def simple(id: Int, var name: String, required: Bool, type: Int) -> Self
```

#### `write_to`

```mojo
def write_to(self, mut writer: T)
```

### `TypeNode`

```mojo
struct TypeNode
```

One node of a type tree. Which fields matter depends on `kind`/`prim`.

Implements: `Copyable`, `Movable`

Fields:

- `kind: UInt8`
- `prim: UInt8`
- `precision: Int`: `decimal(P, S)`.
- `scale: Int`
- `length: Int`: `fixed[L]`.
- `crs: String`: `geometry(C)` / `geography(C, A)`.
- `algorithm: String`
- `raw: String`: Original spelling, for `P_UNRECOGNIZED` and for exact re-serialization.
- `fields: List[NestedField]`: `struct`.
- `element: Int`: `list`: element type index.
- `element_id: Int`
- `element_required: Bool`
- `key: Int`: `map`: key type index.
- `key_id: Int`
- `value: Int`
- `value_id: Int`
- `value_required: Bool`

#### `prim_`

```mojo
def prim_(kind: UInt8) -> Self
```

### `TypeStore`

```mojo
struct TypeStore
```

Arena of type nodes. Indices returned by the builders point in here.

Implements: `Copyable`, `Movable`

Fields:

- `nodes: List[TypeNode]`

#### `__init__`

```mojo
def __init__(out self)
```

#### `add`

```mojo
def add(mut self, var n: TypeNode) -> Int
```

#### `primitive`

```mojo
def primitive(mut self, kind: UInt8) -> Int
```

#### `decimal`

```mojo
def decimal(mut self, precision: Int, scale: Int) -> Int
```

#### `fixed`

```mojo
def fixed(mut self, length: Int) -> Int
```

#### `struct_`

```mojo
def struct_(mut self, var fields: List[NestedField]) -> Int
```

#### `list_`

```mojo
def list_(mut self, element_id: Int, element: Int, required: Bool) -> Int
```

#### `map_`

```mojo
def map_(mut self, key_id: Int, key: Int, value_id: Int, value: Int, value_required: Bool) -> Int
```

#### `kind`

```mojo
def kind(self, i: Int) -> UInt8
```

#### `prim`

```mojo
def prim(self, i: Int) -> UInt8
```

#### `is_primitive`

```mojo
def is_primitive(self, i: Int) -> Bool
```

#### `is_nested`

```mojo
def is_nested(self, i: Int) -> Bool
```

#### `type_name`

```mojo
def type_name(self, i: Int) -> String
```

The Iceberg JSON spelling of a primitive; the kind for nested types.

#### `equal`

```mojo
def equal(self, a: Int, b: Int) -> Bool
```

Structural equality of two types in this store.

#### `parse_type`

```mojo
def parse_type(mut self, doc: Json, i: Int) -> Int
```

Parse an Iceberg type from JSON — a string, or a nested-type object.

#### `parse_field`

```mojo
def parse_field(mut self, doc: Json, i: Int) -> NestedField
```

#### `parse_primitive`

```mojo
def parse_primitive(mut self, s: String) -> Int
```

Parse a primitive spelling. Unknown spellings are kept, not rejected.

#### `type_json`

```mojo
def type_json(self, i: Int) -> String
```

Serialize a type back to its Iceberg JSON form.

#### `field_json`

```mojo
def field_json(self, f: NestedField) -> String
```

## Functions

### `primitive_name`

```mojo
def primitive_name(kind: UInt8) -> String
```

### `is_integer_like`

```mojo
def is_integer_like(kind: UInt8) -> Bool
```

True for primitives whose value is carried as a signed 64-bit integer.

### `is_time_like`

```mojo
def is_time_like(kind: UInt8) -> Bool
```

## Aliases

### `TK_PRIMITIVE`

```mojo
comptime TK_PRIMITIVE = UInt8(0)
```

### `TK_STRUCT`

```mojo
comptime TK_STRUCT = UInt8(1)
```

### `TK_LIST`

```mojo
comptime TK_LIST = UInt8(2)
```

### `TK_MAP`

```mojo
comptime TK_MAP = UInt8(3)
```

### `P_BOOLEAN`

```mojo
comptime P_BOOLEAN = UInt8(0)
```

### `P_INT`

```mojo
comptime P_INT = UInt8(1)
```

### `P_LONG`

```mojo
comptime P_LONG = UInt8(2)
```

### `P_FLOAT`

```mojo
comptime P_FLOAT = UInt8(3)
```

### `P_DOUBLE`

```mojo
comptime P_DOUBLE = UInt8(4)
```

### `P_DATE`

```mojo
comptime P_DATE = UInt8(5)
```

### `P_TIME`

```mojo
comptime P_TIME = UInt8(6)
```

### `P_TIMESTAMP`

```mojo
comptime P_TIMESTAMP = UInt8(7)
```

### `P_TIMESTAMPTZ`

```mojo
comptime P_TIMESTAMPTZ = UInt8(8)
```

### `P_TIMESTAMP_NS`

```mojo
comptime P_TIMESTAMP_NS = UInt8(9)
```

### `P_TIMESTAMPTZ_NS`

```mojo
comptime P_TIMESTAMPTZ_NS = UInt8(10)
```

### `P_STRING`

```mojo
comptime P_STRING = UInt8(11)
```

### `P_UUID`

```mojo
comptime P_UUID = UInt8(12)
```

### `P_FIXED`

```mojo
comptime P_FIXED = UInt8(13)
```

### `P_BINARY`

```mojo
comptime P_BINARY = UInt8(14)
```

### `P_DECIMAL`

```mojo
comptime P_DECIMAL = UInt8(15)
```

### `P_UNKNOWN`

```mojo
comptime P_UNKNOWN = UInt8(16)
```

### `P_VARIANT`

```mojo
comptime P_VARIANT = UInt8(17)
```

### `P_GEOMETRY`

```mojo
comptime P_GEOMETRY = UInt8(18)
```

### `P_GEOGRAPHY`

```mojo
comptime P_GEOGRAPHY = UInt8(19)
```

### `P_UNRECOGNIZED`

```mojo
comptime P_UNRECOGNIZED = UInt8(255)
```

A primitive spelling this build does not know — kept verbatim, never fatal.

### `DEFAULT_CRS`

```mojo
comptime DEFAULT_CRS = String("OGC:CRS84")
```

### `DEFAULT_EDGE_ALGORITHM`

```mojo
comptime DEFAULT_EDGE_ALGORITHM = String("spherical")
```
