Batch helpers
On this page
Building, reading, projecting and serializing record batches.
function adoptArrowJsShape
Section titled âfunction adoptArrowJsShapeâexport function adoptArrowJsShape<T>(batch: T): TDescription
No-op on this backend: arrow-js RecordBatch/Vector already expose the
full API worker code uses, and arrow-js is a peer dependency, so there is
only ever one copy. Present so both backends satisfy one facade interface â
see impl-flechette/compat.ts for what this does there.
const backend
Section titled âconst backendâconst backend: VgiBackendInfo = { name: âarrow-jsâ }function batchFromColumns
Section titled âfunction batchFromColumnsâexport function batchFromColumns(columns: Record<string, any[]>,schema: Schema | VgiSchema,repr: "rich" | "raw" = "rich",): RecordBatchDescription
Build a RecordBatch from column arrays (values are RICH). Each column is converted rich -> canonical via the codec, then canonical -> arrow-js column data via the canonical writer.
function batchFromRows
Section titled âfunction batchFromRowsâexport function batchFromRows(rows: Record<string, any>[],schema: Schema | VgiSchema,): RecordBatchDescription
Build a RecordBatch from row objects (values are RICH).
function batchToScalarDict
Section titled âfunction batchToScalarDictâexport function batchToScalarDict(batch: RecordBatch | VgiBatch | null): Record<string, any>Description
Extract single-row batch to a scalar dict, in the RICH representation. Routes through the canonical reader + codec (same as iterRows) so a temporal/decimal setting is represented identically to column data and across backends, and so Dictionary-encoded columns (DuckDB sends these for enum-shaped fields like SchemaObjectType) are decoded â readCanonicalValue handles dictionary decode.
function batchToSecretDict
Section titled âfunction batchToSecretDictâexport function batchToSecretDict(batch: RecordBatch | VgiBatch | null): Record<string, Record<string, any>>Description
Extract single-row batch to a secret dict (column per secret, each value is a struct). Handles both named secrets (column name = secret type) and scoped secrets (column name = âsecret_Nâ with secret_type in field metadata).
function columnFromArray
Section titled âfunction columnFromArrayâexport function columnFromArray(values: any[], type: VgiDataType): VgiColumnDataDescription
Build an opaque column-data handle from a JS array. arrow-js wraps
vectorFromArray and exposes its first Data node. Unlike the public
build path, this is a low-level pass-through used by callers that already
hold backend-native values, so it does NOT run the codec.
interface ColumnStatistics
Section titled âinterface ColumnStatisticsâexport interface ColumnStatisticsFields
columnNamestringarrowTypeVgiDataTypeminanymaxanyhasNullbooleanhasNotNullbooleandistinctCountbigint | number | nullcontainsUnicodeboolean | nullmaxStringLengthbigint | number | null
function decodeDictValue
Section titled âfunction decodeDictValueâexport function decodeDictValue(value: any, index = 0): anyDescription
If value looks like a Dictionary-encoded Arrow scalar (Vector.get on a
dict column on the apache-arrow fork returns the underlying Data, not the
decoded string), pull out the decoded value at row index. Returns
value unchanged when it isnât dict-shaped.
Used at handler call sites where the incoming params came from the RPC
layerâs row extractor (which doesnât auto-decode dictionaries) and the
handler needs the plain string.
function deserializeBatch
Section titled âfunction deserializeBatchâexport function deserializeBatch(bytes: Uint8Array): RecordBatchDescription
Deserialize a RecordBatch from Arrow IPC bytes.
function deserializeSchema
Section titled âfunction deserializeSchemaâexport function deserializeSchema(bytes: Uint8Array): SchemaDescription
Deserialize an Arrow Schema from IPC bytes. Note: In Bun, reader.schema is always undefined, so we must read from the batch.
Returns arrow-jsâs Schema (which structurally satisfies VgiSchema).
function emptyBatch
Section titled âfunction emptyBatchâexport function emptyBatch(schema: Schema | VgiSchema): RecordBatchDescription
Create an empty (0-row) batch with the given schema. Accepts arrow-js
Schema or facade VgiSchema (cast at the boundary).
function filterBatch
Section titled âfunction filterBatchâexport function filterBatch(batch: RecordBatch | VgiBatch,mask: Uint8Array,): RecordBatchDescription
Filter a RecordBatch using a Uint8Array mask (0=exclude, nonzero=include). Returns a new batch containing only the rows where mask[i] is nonzero.
Rows are read in canonical form then mapped back to RICH so the rebuild goes
through the same codec/canonical path as every other column build â lossless
and identical across backends (never the lossy/raw Vector.get).
function iterRows
Section titled âfunction iterRowsâexport function* iterRows(batch: RecordBatch | VgiBatch,repr: "rich" | "raw" = "rich",): Generator<Record<string, any>>Description
Iterate rows of a RecordBatch as plain objects, in the RICH representation
(Date for date32/date64; canonical otherwise). Reads via the per-backend
canonical reader (lossless, backend-agnostic) then maps canonical -> rich
through the codec â symmetric with the build path. Accepts arrow-js
RecordBatch or facade VgiBatch.
function projectBatch
Section titled âfunction projectBatchâexport function projectBatch(projectionIds: number[] | null,batch: RecordBatch | VgiBatch,): RecordBatchDescription
Project a RecordBatch by column indices.
function projectSchema
Section titled âfunction projectSchemaâexport function projectSchema(projectionIds: number[] | null,schema: Schema | VgiSchema,): SchemaDescription
Project a schema by column indices, preserving only selected fields.
function readCanonicalValue
Section titled âfunction readCanonicalValueâexport function readCanonicalValue(type: VgiDataType,column: unknown,index: number,): unknownDescription
Read a single CANONICAL value at index from an arrow-js column (Vector).
Reads the underlying typed-array storage for scalars (so it is lossless and
never depends on Vector.get()âs lossy/divergent coercions â e.g. arrow-js
Vector.get() returns a JS number for timestamp[us], losing precision), and
recurses for composites.
function safeNumber
Section titled âfunction safeNumberâexport function safeNumber(value: any): numberDescription
Narrow a bigint to a number, refusing to do it lossily.
Arrow int64/uint64 values arrive as bigint. Several bind-time argument paths
want a plain number, and used to get one from a bare Number(value) â which
silently rounds above 2^53, so an id or a nanosecond timestamp passed as a
function argument came back subtly wrong with nothing logged anywhere.
Throwing is the lesser evil: a caller that genuinely wants a lossy narrowing
can still write Number(v) themselves, but nobody has to discover the loss
from mismatched output weeks later.
function serializeBatch
Section titled âfunction serializeBatchâexport function serializeBatch(batch: RecordBatch | VgiBatch): Uint8ArrayDescription
Serialize a RecordBatch to Arrow IPC bytes. Accepts arrow-js RecordBatch
or facade VgiBatch.
function serializeColumnStatistics
Section titled âfunction serializeColumnStatisticsâexport function serializeColumnStatistics(stats: ColumnStatistics[],_cacheMaxAgeSeconds?: number | null,): Uint8ArrayDescription
Serialize column statistics to IPC bytes per vgi-pythonâs wire format.
Returns an empty-stats batch when stats is empty (matching Python).
function serializeSchema
Section titled âfunction serializeSchemaâexport function serializeSchema(schema: Schema | VgiSchema): Uint8ArrayDescription
Serialize a Schema to Arrow IPC bytes. Accepts arrow-js Schema or
facade VgiSchema (the latter is satisfied structurally by arrow-js
Schema instances at runtime).
function toUint8Array
Section titled âfunction toUint8Arrayâexport function toUint8Array(val: any): Uint8ArrayDescription
Convert any binary-ish value to a Uint8Array. Returns empty array for null/undefined.