arrow

Apache Arrow record batch transform streams. Convert rows to and from Arrow RecordBatch objects and detect a schema from sampled data.

Install

npm install @datastream/arrow apache-arrow

apache-arrow is a peer dependency.

arrowDetectSchemaStream PassThrough

Samples the first rows of the stream and infers an Arrow Schema. Rows pass through unchanged. Accepts either arrays (columns become column0, column1, …) or objects (columns become object keys).

Options

OptionTypeDefaultDescription
sampleSizenumber100Number of rows to buffer before sealing the schema
resultKeystring"arrowDetectSchema"Key in pipeline result

Result

{ schema: Schema | null, fields: string[] | null }

Type inference

ValueArrow type
booleanBool
Integer in signed 32-bit rangeInt32
Integer outside 32-bit range / non-integer numberFloat64
DateTimestampMillisecond
Anything elseUtf8

Integers outside the signed 32-bit range widen to Float64 (exact up to 2^53) instead of silently wrapping inside an Int32 builder.

Example

import { pipeline, createReadableStream } from '@datastream/core'
import { arrowDetectSchemaStream } from '@datastream/arrow'

const detect = arrowDetectSchemaStream()

const result = await pipeline([
  createReadableStream([
    { id: 1, name: 'Alice' },
    { id: 2, name: 'Bob' },
  ]),
  detect,
])

console.log(result.arrowDetectSchema.fields) // ['id', 'name']

arrowBatchFromArrayStream Transform

Builds Arrow RecordBatch objects from incoming array rows. Each row is an array of column values in schema field order.

Options

OptionTypeDefaultDescription
schemaSchema \| () => SchemaArrow schema, or a lazy function returning one (required)
batchSizenumber10000Rows per emitted RecordBatch

arrowBatchFromObjectStream Transform

Builds Arrow RecordBatch objects from incoming object rows, mapping each schema field name to the matching object key.

Options

OptionTypeDefaultDescription
schemaSchema \| () => SchemaArrow schema, or a lazy function returning one (required)
batchSizenumber10000Rows per emitted RecordBatch

Example

import { pipeline, createReadableStream } from '@datastream/core'
import { arrowDetectSchemaStream, arrowBatchFromObjectStream } from '@datastream/arrow'

const detect = arrowDetectSchemaStream()

await pipeline([
  createReadableStream(rows),
  detect,
  arrowBatchFromObjectStream({
    schema: () => detect.result().value.schema,
  }),
  // ... e.g. duckdbArrowInsertStream
])

arrowToArrayStream Transform

Expands each incoming RecordBatch into array rows (one array of column values per row).

arrowToObjectStream Transform

Expands each incoming RecordBatch into object rows, keyed by the batch schema’s field names.

Example

import { pipeline } from '@datastream/core'
import { arrowToObjectStream } from '@datastream/arrow'

await pipeline([
  recordBatchReadableStream,
  arrowToObjectStream(),
  // ... each chunk is { id, name, ... }
])