Skip to main content

@math.gl/sql/arrow

@math.gl/sql/arrow evaluates schema-bound expressions directly over Arrow batches. Use it for WHERE predicates, derived columns and typed application functions. It reuses the parser from @math.gl/sql/expressions. For SQL text use the parser; for complete queries use the query engine.

import {compileArrowExpression, compileArrowPredicate} from '@math.gl/sql/arrow';

const predicate = compileArrowPredicate(
{op: '>=', args: [{property: 'population'}, {parameter: 'minimum'}]},
{inputSchema: batch.schema, parameters: {minimum: 100000}}
);
const matches = predicate.evaluate(batch); // Bool Vector: retain only true rows

const expression = compileArrowExpression('population * 2', {inputSchema: batch.schema});
const result = expression.evaluate(batch);
console.log(expression.outputField, result);

Compile once and reuse across batches with the same ordered field names, types, encodings and nullability. GeoArrow extension encoding/CRS metadata must match; other metadata differences do not invalidate a binding. Evaluation preserves row count; empty batches return empty vectors. Output field names can be replaced by an application's SELECT AS alias. WHERE column names and SELECT alias resolution belong to the caller's query planner.

Nested columns​

Use LIST_EXTRACT(start, 3) or start[3] to read the third element of an Arrow List or FixedSizeList column. Indexes start at 1, and negative indexes count from the end. Zero, non-integer or out-of-range indexes, and null inputs return null.

Use STRUCT_EXTRACT(endpoint, 'longitude') or endpoint.longitude to read a named field from an Arrow Struct column. The field must exist in the Arrow schema and the explicit function takes its name as a string literal. A null parent struct yields null. Each operation retains the selected element or field type.

SELECT start[3] AS altitude, endpoint.longitude AS longitude
WHERE start[3] > 0

Functions​

compileArrowExpression(expression, options)​

Returns a reusable evaluator with outputField: Field and evaluate(recordBatch, {activeRows?}): Vector.

ParameterDescription
expressionExpression text or the AST accepted by @math.gl/sql/expressions.
options.inputSchemaRequired Arrow schema used to bind field names and types.
options.functionsOptional array of named ArrowTableFunction declarations.
activeRowsOptional Uint8Array, one 0 or 1 per input row. Zero skips evaluation.

compileArrowFunction(name, arguments, options)​

Compiles a direct function call. Each argument is expression text or an AST; options and the returned evaluator are the same as compileArrowExpression. This also supports built-in function names.

compileArrowPredicate(predicate, options)​

Compiles a portable ParameterizedColumnarPredicate. Options additionally accept parameters, a map of parameter names to scalar values. Missing parameters and unknown columns fail at compilation. The returned evaluator produces a Bool vector using the null semantics described below.

Named table functions​

Pass declarations through functions; the compiler builds a private name lookup once per compilation. Names must be valid identifiers; built-in names are reserved. Argument signatures within each name must be unique. No global registry is modified. Treat declared fields and their types as immutable. Compilation snapshots the declarations and their argument arrays, so later changes to the supplied function list do not change existing evaluators.

import {Field, Float64, vectorFromArray} from 'apache-arrow';
import {compileArrowFunction, type ArrowTableFunction} from '@math.gl/sql/arrow';

const discount: ArrowTableFunction = {
name: 'discount',
prototypes: [{arguments: [new Field('price', new Float64(), true)],
result: new Field('discounted_price', new Float64(), true),
nullHandling: 'propagate',
evaluate([prices], {length, activeRows}) {
return vectorFromArray(Array.from({length}, (_, row) =>
activeRows[row] && prices!.isValid(row) ? prices!.get(row) * 0.9 : null
), new Float64());
},
evaluateScalar: price => Number(price) * 0.9
}]
};

const compiled = compileArrowFunction('discount', ['price'], {
inputSchema: batch.schema,
functions: [discount]
});
const prices = compiled.evaluate(batch);
// The same declaration can be used inside 'discount(price) > 100'.

ArrowTableFunctionType is the type of evaluate: it accepts an ordered readonly Vector[] and {activeRows: Uint8Array, length: number}, and returns a Vector of the same length. evaluate is required on every prototype and can be called directly as function.prototypes[index].evaluate(...). Direct callers must supply correctly typed vectors and a mask, and honor the declaration's null policy; compilation provides these checks and null propagation when invoking functions inside expressions.

Arguments use exact declared logical types. Numeric literals are checked against their operand's type; other conversions must be explicit. nullHandling: 'propagate' skips rows with null arguments and makes the expression result nullable. With nullHandling: 'call', nullable arguments must be declared nullable and the implementation receives null values. Returning null for a non-nullable declared result is an error.

Vector evaluation and scalar fusion​

The vector implementation receives full-length Arrow vectors, potentially dictionary encoded, and a byte mask containing 0 or 1 per row. It must evaluate only active rows, preserve row count, and return the declared logical result type. Inactive output values are ignored. Inputs belong to the caller and must not be mutated. The evaluator never calls a function when no rows are active.

An optional evaluateScalar(...values) provides equivalent primitive row semantics. When present, the expression compiler uses it to fuse the function into its output loop, avoiding intermediate vectors. Without it, the compiler calls evaluate at the vector boundary. It never runs both implementations or retries a failure through the other implementation. Both implementations must agree on values and errors. Conditional branches and null propagation skip scalar callbacks too.

The example uses vectorFromArray for readability; vector kernels can write Arrow buffers directly. Geometry functions can supply only evaluate, with exact nested argument fields and GeoArrow extension metadata. Results support primitives and fixed-size numeric tuples. A geometry result can use Binary with the geoarrow.wkb extension. The spatial function guide shows how to use those results in another expression.

RGBA and other numeric tuple results​

A vector-only function can return a FixedSizeList with a positive fixed width and a non-nullable numeric child. For RGBA, declare four Float32 channels or four Uint8 channels:

const rgba = new FixedSizeList(4, new Field('channel', new Float32(), false));
const result = new Field('color', rgba, true);

The parent validity bitmap represents a missing color; transparent RGBA is a valid value. The evaluator preserves the declared field metadata and child buffers, validates active result rows, and masks inactive or null-propagated rows through parent validity. Tuple functions cannot supply evaluateScalar. Tuple values can be projected, passed to another vector function, tested with isNull, or selected with condition ? color(value) : [0, 0, 0, 1]. Arithmetic and comparisons on whole tuples are unsupported. Conditional selection allocates one numeric child buffer and copies selected components, without per-row JavaScript arrays.

Typed array arguments​

Array literals take their type from a function's declared List or FixedSizeList argument. For example, List<Float64> accepts numeric bins and List<Utf8> accepts category names:

// Application-supplied functions declare these arguments:
const bins = new Field('bins', new List(new Field('item', new Float64(), false)), false);
const categories = new Field('categories', new List(new Field('item', new Utf8(), false)), false);
// Expressions: bucket(value, [10, 20, 50]) and category(kind, ["road", "rail", "water"])

The compiler validates every element against the exact declared type. Empty variable-length lists are supported; fixed-size literals must match their width. Null elements require a nullable child. Literals are flat and limited to 4096 elements: dynamic values, nested arrays, holes and spreads are rejected. Standalone arrays have no inferred type. A conditional array branch can inherit its type from the other tuple branch or a declared function argument.

Variable-length constants are broadcast as dictionary-encoded vectors containing one list value, so a large bin/category list is not copied for every row. Kernels must handle dictionary inputs (as Arrow Vector.get does) and treat inputs as read-only. Fixed tuples use ordinary child buffers. The same declarations and literals work with evaluateTable and worker-local functions. These are evaluator primitives; palette and color-scale functions are supplied separately.

Supported expressions​

  • Top-level fields, numeric/string/Boolean/null literals, parentheses and named function calls.
  • Arithmetic +, -, *, /, %; unary + and -; comparisons ==, ===, !=, !==, <, <=, >, >=.
  • Boolean &&, ||, ! and the conditional condition ? yes : no.
  • isNull, isNotNull, parseNumber, parseBigInt, parseBoolean.
  • Explicit numeric conversions toFloat64, toInt32, toInt64.

Referenced inputs support signed/unsigned integers, Float32/Float64, Bool and Utf8, including dictionary encodings. Computed outputs use ordinary vectors. Int64 and Uint64 values use bigint. Integer arithmetic checks bounds rather than wrapping; integer division truncates toward zero and division/remainder by zero throws. Checked integer conversions reject fractional or out-of-range results. Explicit conversion from bigint to Float64 can round. Float32 intermediates round to Float32 at each operation. Floating arithmetic and comparisons retain IEEE behavior, including Infinity and NaN for zero divisors.

UTF-8 comparison uses unsigned byte order without locale collation. String parsing operates on Arrow byte ranges and uses the existing UTF-8 helpers' decimal/Boolean grammar. Invalid text becomes null; parsed integers outside Int64 range throw. Custom scalar functions decode only the active values they consume. Binary kernels and input readers honor validity bitmaps and slice offsets.

Nulls propagate through ordinary operators. Boolean logic is three-valued: false && null is false, true || null is true, and !null is null. A null conditional test selects the false branch. Inactive branches never invoke user functions. evaluate(batch, {activeRows}) can additionally skip selected rows; skipped positions are null even when outputField describes a non-nullable expression.

Portable predicates​

The format-neutral ColumnarPredicate vocabulary and its parameter-binding, validation, column-path and copy utilities are exported from @math.gl/sql/expressions, without an Arrow dependency. compileArrowPredicate accepts comparisons, in, isNull, and, or and not, plus named parameters. The Arrow executor accepts Date literals for Date32, DateMillisecond and all Arrow timestamp units, and byte-array literals for binary fields. Date32 compares UTC calendar days, so a non-midnight Date literal matches the stored day in comparisons and membership. DateMillisecond and timestamps compare instants. Use an array such as ['item', 'value'] to read a nested struct field. A missing nested member is null, and a string with dots remains one literal field name. Portable comparisons and membership preserve exact ordering and equality between bigint and number operands. Numeric comparisons reject NaN and infinity in portable predicates. Time-of-day and duration predicates compare numeric bounds in the column’s physical unit.

SQL parsing, source pushdown and result-row selection remain the query engine's responsibility. Only true predicate results select rows; false and null results do not. Computed SELECT functions should be evaluated after filtering, using the selected batch or an active-row mask.

Performance and boundaries​

Input readers use Arrow buffers without allocating JavaScript row objects. Primitive string kernels avoid decoding input strings. Built-in operations and scalar callbacks are fused into the output pass, and constants remain scalar. Temporary vectors are materialized at registered vector boundaries and for tuple conditionals. List constants retain one shared payload plus row indices. Kernel branches may require a mask prepass; working memory remains bounded by the current batch. Direct field projections can reuse input buffers; retain those outputs only as long as needed.

Run node modules/core/scripts/arrow/benchmark-expressions.mjs after building to compare numeric and UTF-8 workloads with the scalar evaluator from @math.gl/sql/expressions. The report separates throughput, row objects, string decodes and output buffer bytes; it does not claim to measure total allocator traffic.

This entry point requires Apache Arrow 17 or later. Import function libraries separately to register the functions your application uses.

Whole-table evaluation​

compiled.evaluateTable(arrowTable, {activeRows?, signal?}) evaluates the table's batches in order and returns one chunked Arrow vector. It reuses each batch result's buffers; it does not flatten or copy columns. Direct primitive projections reuse input buffers. Dictionary outputs use ordinary vectors. An empty table still validates its schema and produces an empty vector with the compiled output type.

A table mask contains one byte per row across all batches. Cancellation is checked before and after each batch; synchronous evaluation does not yield to the event loop. Use a worker for responsive UI cancellation. A single large batch or custom synchronous kernel cannot be interrupted mid-call.

Turf functions​

Use the spatial SQL function libraries to register PostGIS-aligned geometry functions or Turf functions for computed columns.

@math.gl/geospatial/turf-functions exports createTurfArrowFunction({name, arguments, result, evaluate}). Pass the exact GeoArrow argument fields and a nullable Float64 or Bool result. Import only the Turf functions you use; the adapter itself imports no Turf implementations.

import {Field, Float64} from 'apache-arrow';
import {area, distance} from '@math.gl/geospatial/turf';
import {createTurfArrowFunction} from '@math.gl/geospatial/turf-functions';
const functions = [
createTurfArrowFunction({
name: 'area',
arguments: [polygonField],
result: new Field('area', new Float64(), true),
evaluate: area
}),
createTurfArrowFunction({
name: 'distanceMeters',
arguments: [pointField, pointField],
result: new Field('distance', new Float64(), true),
evaluate: (a, b) => distance(a, b, {units: 'meters'})
})
];
// Compile 'area(geometry) > 1000' with {inputSchema, functions},
// or construct the same declarations inside the worker module.

The adapter combines active-row masks with GeoArrow validity without changing coordinate buffers. It supports row-aligned numeric and Boolean Turf results: area, length, distance, bearing, rhumb variants, angle and Boolean predicates. It retains each Turf function's units, geometry restrictions, CRS assumptions and null behavior. Geometry-producing functions, row-count-changing operations and flattened bbox results are excluded. Nested geometry arithmetic remains unsupported.

SQL CAST​

Use parseSQLExpression from @math.gl/sql/parser to compile standard SQL cast syntax:

import {parseSQLExpression} from '@math.gl/sql/parser';
const converted = compileArrowExpression(parseSQLExpression('CAST(population AS BIGINT)'), {
inputSchema: batch.schema
});

Supported targets are SMALLINT (Int16), INTEGER/INT (Int32), BIGINT (Int64), REAL (Float32), DOUBLE PRECISION (Float64), BOOLEAN (Bool), and VARCHAR/CHARACTER VARYING (Utf8). Nulls remain null. Numeric-to-integer casts truncate toward zero and reject overflow; text-to-integer casts require integer text. Invalid text, non-finite numeric results and overflow throw only for active rows. Boolean casts accept Boolean/text inputs; Boolean-to-text yields TRUE or FALSE. Text parsing uses the UTF-8 primitives without decoding numeric/Boolean input strings. Bigint-to-floating conversion can lose precision. Float32 casts round to Float32.

The SQL parser emits the shared expression AST, equivalent to cast(value, "int64") in expression syntax. CAST can be nested, selected with an alias, or applied to a WHERE column before comparison. Precision/length modifiers, DECIMAL, date/time targets, casts on the right-hand predicate scalar are currently unsupported.

Postfix value::TYPE and chained casts are also supported with the same conversion rules.

Packed source columns​

compileArrowArray(columns, {inputSchema, type?}) packs existing floating-point columns into one fixed-size list vector. type may be float16, float32 or float64; omission preserves Float64 if present in any source component, otherwise Float32 or Float16, preserving the widest input precision. Unknown and non-floating columns are rejected at binding.

import {compileArrowArray} from '@math.gl/sql/arrow';
const position = compileArrowArray(['x', 'y', 'z'], {inputSchema: arrowTable.schema});
const positions = position.evaluateTable(arrowTable);

The optional SQL parser represents ARRAY[x,y,z] AS position as a portable array selection with an optional explicit precision. Pass its array and type to this helper. Evaluation interleaves each batch without row filtering, retaining source order and chunk boundaries. Any null component makes the parent list null; its numeric slots remain zeros. Masked rows follow the same rule. Consumers reading numeric buffers directly must honor parent validity. Empty results retain their fixed-size schema. Explicit Float32 conversion can lose precision, following Arrow floating-point conversion.

Float16​

Arrow Float16 inputs, scalar results and fixed-size numeric tuple results use native Float16Array views over Arrow's Uint16 binary16 storage. Expressions decode logical numbers, respect slice offsets and nulls, and round at every half-precision operation boundary. Output buffers remain Arrow-compatible Uint16 bit patterns; buffer transport and worker evaluation retain that representation. Numeric projections can reuse input storage.

parseSQLExpression('value::FLOAT16') and CAST(value AS FLOAT16) bind explicit half-precision casts. Values round using the runtime's binary16 conversion; finite values that overflow on explicit casts throw, while arithmetic retains IEEE infinity/NaN behavior. Subnormals and signed zero are preserved. Runtime native Float16Array support is required in every executing worker or thread; without it, Float16 operations fail with a capability error. Other expression types remain usable. The capability checks reuse the existing core Float16 helpers.

Packed arrays also accept ARRAY[x,y,z]::FLOAT16[3] or compileArrowArray(columns, {inputSchema, type: "float16"}). Native Float16Array is required for half-precision execution.

Function prototypes and numeric overloads​

A named ArrowTableFunction can supply fixed prototypes through prototypes. A prototype declares its argument fields, result field, null policy, vector implementation and optional fused scalar implementation. Multiple objects with the same name may contribute distinct prototypes. Duplicate argument-type/metadata signatures are rejected; changing only return type or nullability does not create an overload. There are no legacy single-signature aliases.

Compilation selects one prototype: exact signatures win, otherwise Float16 can widen to Float32 or Float64 and Float32 can widen to Float64. The least total widening wins; ties are ambiguous and fail binding. Integer/float and mixed integer-width conversions require explicit casts. Literals follow typed arguments; calls containing only numeric literals prefer their default Float64 type. Use typed null casts to disambiguate otherwise ambiguous null arguments. Kernels receive the chosen prototype's declared input types.

A declaration can also provide resolvePrototype(fields) for a schema-dependent signature. The compiler calls the resolver during binding when no fixed prototype matches. A function with only a resolver uses prototypes: []. The resolver receives the inferred argument fields in call order. An undefined field indicates an argument that needs type context, such as a bare NULL. Return one prototype for a supported signature, or undefined to indicate no match. Only one resolver can be registered per function name. The chosen prototype is reused across batches.

ExpressionEvaluationError exposes code values domain, conversion, overflow and divisionByZero. Numeric data errors use this class; unknown names, incompatible signatures, schema mismatches and implementation failures remain ordinary fatal errors. Division and modulo by zero throw for integer and floating Arrow arithmetic. Use TRY for row-level recovery, as described below.

TRY and COALESCE​

TRY(expression) converts classified row-level domain, conversion, overflow, and division-by-zero errors to null. TRY(expression, fallback) evaluates the fallback only for failing rows; a genuine null result stays null. Use COALESCE(TRY(expression), fallback) to replace both.

import {parseSQLExpression} from '@math.gl/sql/parser';
import {SQRT} from '@math.gl/sql/math-functions';
const compiled = compileArrowExpression(parseSQLExpression('TRY(SQRT(value), 0)'), {
inputSchema: arrowTable.schema,
functions: [SQRT]
});

Fallback errors propagate unless separately wrapped in TRY. Nested recovery scopes are isolated. Syntax errors, unknown names, incompatible types, cancellation and unexpected callback/kernel exceptions remain fatal. TRY never retries a batch or invokes a callback twice. Recovery boundaries materialize an output vector; pure scalar operations outside them remain fused.

Vector implementations can call context.reportError?.(row, error) with a classified error and then skip the rest of that row. Outside TRY the reporter throws. Inside TRY it records the row failure and clears that row in the kernel's active mask. A kernel without a reporter must throw the error. A thrown batch exception cannot be associated with a row and remains fatal, even inside TRY.

See SQL expression conformance for syntax, numeric rules, worker registration, and the benchmark.

Byte arrays and normalized packing​

Use the conversion that matches the input units:

ExpressionInput valuesOutput
ARRAY[r, g, b, a]::UINT8[4]Byte values. Fractions truncate toward zero; values outside 0–255 after truncation overflow.FixedSizeList<Uint8>
PACK_UNORM8(ARRAY[r, g, b, a])Normalized values in [0, 1]. Multiplies by 255 and rounds to the nearest byte, with ties upward. Values outside the range raise a domain error.FixedSizeList<Uint8>

128 remains 128 in a value cast; 0.5 becomes 128 in normalized packing. Both forms support arbitrary fixed widths from 1 to 4,096. PACK_UNORM8 also accepts an existing fixed-size numeric column. Nonfinite inputs raise conversion errors. Null rows and null components produce a null byte tuple with zero padding. TRY can recover row conversion errors.

Arrow stores the bytes without normalized interpretation. A GPU consumer can bind a four-byte result as unorm8x4 to read channels as normalized floats. The existing COLOR_TO_RGBA8 helper remains the RGBA-specific, clamping conversion.

SQL string expressions​

Arrow SQL supports LIKE, ILIKE, NOT LIKE, and NOT ILIKE. Patterns match the entire value. % matches zero or more characters and _ matches one Unicode code point. ESCAPE '$' designates a literal single-character escape, for example name LIKE 'a$%b' ESCAPE '$'.

Literal patterns are compiled once. Matching reads Arrow UTF-8 byte ranges, including sliced and dictionary-encoded columns, without decoding whole rows. Dynamic patterns are supported and compiled per evaluated row. Null input or pattern produces null. ILIKE uses locale-independent, per-code-point Unicode lowercase matching, with a direct ASCII path.

ExpressionResult
'Airport' ILIKE 'air%'true
'Γ©πŸ˜€' LIKE '__'true
`'Air'
`'Air'
CONCAT('Air', NULL, 'port')'Airport'
UPPER('Γ©cole')'Γ‰COLE'
LOWER('Γ‰COLE')'Γ©cole'
CAPITALIZE('hello WORLD')'Hello WORLD'

Concatenation and case conversion accept text. Use an explicit SQL cast for numeric inputs. CAPITALIZE changes only the first Unicode code point. Case conversion uses JavaScript's locale-independent Unicode mappings. These functions preserve nulls, except CONCAT, which skips null arguments. Empty strings are preserved. Regular expressions, SIMILAR TO, and GLOB are outside this supported subset.

Epoch milliseconds​

EPOCH_MS(timestamp) converts an Arrow microsecond timestamp or DATE to a Float64 number of milliseconds since 1970-01-01. This is suitable for JavaScript Date values and time sliders. Nulls propagate; timestamps retain fractional milliseconds, including before the epoch. Large epochs have Float64 precision.

SELECT EPOCH_MS(CAST(observed_at AS TIMESTAMP WITH TIME ZONE)) AS time

Temporal casts accept ISO date/time text. Explicit offsets are normalized to UTC. Timezone-free timestamps use their stored epoch without applying the host timezone. Non-ISO source strings still need normalization by the loader or application.

Portable predicates can compare LargeUtf8 and LargeBinary directly through their Arrow offsets. Binary comparisons and membership use the complete byte sequence, including when a literal and a FixedSizeBinary column have different widths. Float16 predicates can read stored values on runtimes without native Float16Array; arithmetic that writes Float16 still requires native support.