Minimal FlatBuffer binary reader for Arrow IPC metadata.
Implements the subset of the FlatBuffer binary format needed to parse
Arrow IPC Message, Schema, RecordBatch, and related tables.
No external FlatBuffer library is required.
FlatBuffer Binary Layout
A FlatBuffer binary starts with a 4-byte root offset pointing to the root table. Tables consist of a vtable (field offset directory) and inline data. Strings, vectors, and child tables are referenced by relative offsets.
This module exposes low-level readers that operate on {binary, position}
pairs, following the FlatBuffer spec exactly.
References
- FlatBuffer encoding: https://flatbuffers.dev/flatbuffers_internals.html
- Arrow IPC format: https://arrow.apache.org/docs/format/Columnar.html
Summary
Functions
Reads a field offset from the vtable for a given field index.
Reads a boolean value (stored as a byte) at the given position.
Reads a scalar int16 value at the given position.
Reads a scalar int32 value at the given position.
Reads a scalar int64 value at the given position.
Reads a FlatBuffer offset (uoffset32) and returns the absolute target position.
Reads a FlatBuffer string at the given indirect offset position.
Reads an unsigned byte (uint8) at the given position.
Reads a vector header at an indirect offset position and returns
{element_start_pos, count}.
Reads a table position from a vector of table offsets.
Reads the vtable for a table at the given position.
Returns the absolute position of the root table in the buffer.
Functions
@spec field_pos( binary(), non_neg_integer(), non_neg_integer(), non_neg_integer(), non_neg_integer() ) :: non_neg_integer() | nil
Reads a field offset from the vtable for a given field index.
Returns the absolute position of the field data, or nil if the field
is not present (offset is 0 or the vtable is too small).
Parameters
buf— the complete FlatBuffer binarytable_pos— absolute position of the tablevtable_pos— absolute position of the vtablevtable_size— total size of the vtable in bytesfield_index— zero-based field index
Returns
Absolute position of the field data, or nil.
@spec read_bool(binary(), non_neg_integer()) :: boolean()
Reads a boolean value (stored as a byte) at the given position.
@spec read_int16(binary(), non_neg_integer()) :: integer()
Reads a scalar int16 value at the given position.
@spec read_int32(binary(), non_neg_integer()) :: integer()
Reads a scalar int32 value at the given position.
@spec read_int64(binary(), non_neg_integer()) :: integer()
Reads a scalar int64 value at the given position.
@spec read_offset(binary(), non_neg_integer()) :: non_neg_integer()
Reads a FlatBuffer offset (uoffset32) and returns the absolute target position.
Parameters
buf— the complete FlatBuffer binaryoffset_pos— position of the offset value
Returns
The absolute position of the referenced object.
@spec read_string(binary(), non_neg_integer()) :: binary()
Reads a FlatBuffer string at the given indirect offset position.
The position contains a relative offset (uint32) to the string data. The string data starts with a uint32 length followed by UTF-8 bytes.
Parameters
buf— the complete FlatBuffer binaryoffset_pos— position of the offset (uoffset32) to the string
Returns
The string as a binary.
@spec read_uint8(binary(), non_neg_integer()) :: non_neg_integer()
Reads an unsigned byte (uint8) at the given position.
@spec read_vector_header(binary(), non_neg_integer()) :: {non_neg_integer(), non_neg_integer()}
Reads a vector header at an indirect offset position and returns
{element_start_pos, count}.
The offset position contains a uoffset32 to the vector. The vector starts with a uint32 element count, followed by the elements.
Parameters
buf— the complete FlatBuffer binaryoffset_pos— position of the offset (uoffset32) to the vector
Returns
{element_start_pos, count} where element_start_pos is the absolute
position of the first element and count is the number of elements.
@spec read_vector_table(binary(), non_neg_integer(), non_neg_integer()) :: non_neg_integer()
Reads a table position from a vector of table offsets.
In FlatBuffers, a vector of tables stores uoffset32 values. Each offset is relative to the position of that offset within the vector.
Parameters
buf— the complete FlatBuffer binaryelement_start— absolute position of the first element in the vectorindex— zero-based index of the element to read
Returns
The absolute position of the table at the given index.
@spec read_vtable(binary(), non_neg_integer()) :: {non_neg_integer(), non_neg_integer()}
Reads the vtable for a table at the given position.
Returns {vtable_pos, vtable_size} where vtable_pos is the absolute
position of the vtable and vtable_size is its total size in bytes.
Parameters
buf— the complete FlatBuffer binarytable_pos— absolute position of the table
Returns
{vtable_pos, vtable_size} tuple.
@spec root_table_pos(binary()) :: non_neg_integer()
Returns the absolute position of the root table in the buffer.
The first 4 bytes of a FlatBuffer are a little-endian uint32 offset from position 0 to the root table.
Parameters
buf— the complete FlatBuffer binary
Returns
The absolute byte position of the root table.