avro · API

avro.schema

Avro schema parsing and printing.

The parsed schema is an arena of SchemaNodes with integer child indices. That is not just a Mojo workaround for self-referential structs: it is also exactly what Avro's named type references want — a record that refers to itself ({"type": "record", "name": "Node", "fields": [{"name": "next", "type": ["null", "Node"]}]}) is one node index appearing twice.

Everything the JSON carried that Avro does not define is preserved as a raw property (field-id, element-id, key-id, value-id — Apache Iceberg writes all four), so to_json() round-trips and consumers can read them back.

Structs

Props

struct Props

Ordered extra JSON attributes; values are kept as raw JSON text.

Implements Copyable, Defaultable, Movable, Sized

Fields
keys: List[String]
vals: List[String]

__init__

def __init__(out self)

__len__

def __len__(self) -> Int

set

def set(mut self, var key: String, var raw_json: String)

get

def get(self, key: StringSpan) -> String

Raw JSON text for key, or "" when absent.

has

def has(self, key: StringSpan) -> Bool

get_int

def get_int(self, key: StringSpan, default: Int = Int(-1)) -> Int

Integer property value, or default when absent or not a number.

Field

struct Field

One record field.

Implements Copyable, Movable

Fields
name: String
doc: String
type_index: Int: Index of the field's type node in the owning Schema's arena.
has_default: Bool
default_json: String: Raw JSON text of the field's default, when it has one.
order: String
aliases: List[String]
props: Props

make

def make(var name: String, type_index: Int) -> Self

field_id

def field_id(self) -> Int

Iceberg's field-id attribute, or -1 when the field has none.

SchemaNode

struct SchemaNode

Implements Copyable, Movable

Fields
kind: Int
name: String: Fullname (namespace-qualified) for record/enum/fixed; "" otherwise.
namespace: String
doc: String
aliases: List[String]
logical_type: String
precision: Int
scale: Int
size: Int: Byte width for fixed.
symbols: List[String]
default_symbol: String
fields: List[Field]
children: List[Int]: Array items / map values (one entry), or union branches.
props: Props

of

def of(kind: Int) -> Self

short_name

def short_name(self) -> String

Schema

struct Schema

A parsed Avro schema: an arena of nodes plus the root node's index.

Implements Copyable, Movable, Writable

Fields
nodes: List[SchemaNode]
root: Int
names: Dict[String, Int]: Fullname -> node index, for every named type the schema defines.

__init__

def __init__(out self)

kind

def kind(self, i: Int) -> Int

root_kind

def root_kind(self) -> Int

name

def name(self, i: Int) -> String

logical_type

def logical_type(self, i: Int) -> String

size

def size(self, i: Int) -> Int

symbols

def symbols(self, i: Int) -> List[String]

num_fields

def num_fields(self, i: Int) -> Int

field

def field(self, i: Int, k: Int) -> Field

field_index

def field_index(self, i: Int, name: StringSpan) -> Int

Position of the named field in record node i, or -1.

field_type

def field_type(self, i: Int, k: Int) -> Int

items

def items(self, i: Int) -> Int

Element node index of an array.

values

def values(self, i: Int) -> Int

Value node index of a map.

num_branches

def num_branches(self, i: Int) -> Int

branch

def branch(self, i: Int, k: Int) -> Int

props

def props(self, i: Int) -> Props

is_optional

def is_optional(self, i: Int) -> Bool

True for a two-branch union with a null branch — Iceberg's spelling of an optional field.

optional_branch

def optional_branch(self, i: Int) -> Int

For an optional union, the node index of the non-null branch; -1 otherwise.

null_branch_index

def null_branch_index(self, i: Int) -> Int

Branch position of the null member of a union, or -1.

is_logical_map

def is_logical_map(self, i: Int) -> Bool

An array of two-field records tagged "logicalType": "map" — how Avro (and Iceberg) spells a map with non-string keys.

element_id

def element_id(self, i: Int) -> Int

key_id

def key_id(self, i: Int) -> Int

value_id

def value_id(self, i: Int) -> Int

to_json

def to_json(self) -> String

to_json_at

def to_json_at(self, i: Int) -> String

write_to

def write_to(self, mut writer: T)

parsing_canonical_form

def parsing_canonical_form(self) -> String

fingerprint

def fingerprint(self) -> UInt64

The 64-bit Rabin fingerprint (CRC-64-AVRO) of the parsing canonical form — Avro's own single-object-encoding schema id.

Functions

kind_name

def kind_name(kind: Int) -> String

primitive_kind

def primitive_kind(name: StringSpan) -> Int

Kind for a primitive type name, or -1 if name is not primitive.

is_primitive

def is_primitive(kind: Int) -> Bool

is_named

def is_named(kind: Int) -> Bool

crc64_avro

def crc64_avro(data: Span[UInt8]) -> UInt64

Avro's CRC-64-AVRO Rabin fingerprint.

parse_schema

def parse_schema(text: StringSpan) -> Schema

Parse an Avro schema from its JSON text.

Aliases

comptime NULL = Int(0)
comptime BOOLEAN = Int(1)
comptime INT = Int(2)
comptime LONG = Int(3)
comptime FLOAT = Int(4)
comptime DOUBLE = Int(5)
comptime BYTES = Int(6)
comptime STRING = Int(7)
comptime RECORD = Int(8)
comptime ENUM = Int(9)
comptime ARRAY = Int(10)
comptime MAP = Int(11)
comptime UNION = Int(12)
comptime FIXED = Int(13)