Skip to main content

SQL expressions in Arrow

Parse SQL with @math.gl/sql/parser, then bind the returned AST to an Arrow schema with compileArrowExpression(). The compiled evaluator can process batches or an entire table in order. Parsing and execution are separate optional imports.

import {parseSQLExpression} from '@math.gl/sql/parser';
import {compileArrowExpression} from '@math.gl/sql/arrow';
import {SQRT} from '@math.gl/sql/math-functions';

const expression = parseSQLExpression('TRY(SQRT(value), 0)');
const compiled = compileArrowExpression(expression, {
inputSchema: arrowTable.schema,
functions: [SQRT]
});
const values = compiled.evaluateTable(arrowTable);

Syntax and supportโ€‹

OperationSQL expressionBasic expression syntaxArrow behavior
Arithmeticx + y, x - y, x * y, x / ySame operatorsExact integer arithmetic with overflow checks; typed floating arithmetic.
Exponent and remainderPOWER(x, y), MOD(x, y)Register the same named functions; % is also supported by ArrowSQL does not accept **, ^, or %.
Comparisons=, <>, !=, <, <=, >, >===, !=, ===, !==, <, <=, >, >=Same-type comparisons; null produces unknown. UTF-8 ordering is binary.
Boolean logicAND, OR, NOT&&, ||, !Three-valued logic with short-circuit evaluation.
Null testsx IS NULL, x IS NOT NULLisNull(x), isNotNull(x)Non-null Boolean result.
BranchesCASE WHEN condition THEN x ELSE y ENDcondition ? x : yOnly the selected branch runs; omitted ELSE produces null.
CastsCAST(x AS REAL), x::REALcast(x, 'float32')Explicit scalar conversion; invalid conversion or overflow throws.
Fixed arraysARRAY[x + 1, SIN(angle), 0]::REAL[3]array_float32(x + 1, SIN(angle), 0)Fixed width, numeric components, null tuple if any component is null.
Typed list argumentsโ€”f([1, 2, 3])List literals bind to the function signature. Variable-length result lists are deferred.
Error recoveryTRY(x) or TRY(x, fallback)Same names in the Arrow evaluatorClassified errors become null or a lazy fallback; genuine nulls remain null.
Null fallbackCOALESCE(x, fallback)Same name in the Arrow evaluatorEvaluates subsequent arguments only where previous results are null.

The basic parser accepts JavaScript-oriented syntax. The standalone JavaScript evaluator has its own semantics and does not automatically provide Arrow casts, null behavior, or TRY. SQL unquoted function calls are case-insensitive; column names and quoted calls retain exact case.

The SQL parser reference describes SELECT, WHERE, source references, LIMIT, and catalog commands. Joins, aggregation, sorting, window functions, and variable-length output arrays remain outside this SQL subset.

Numeric types and errorsโ€‹

See Arrow math functions for the complete function signatures and radians, rounding, and domain rules. Overloads prefer exact types; mixed floating arguments widen to the widest precision. Integer and floating columns require an explicit conversion when mixed. Integer transcendental calls return Float64. Int64/Uint64 integer operations preserve bigint precision where the operation returns an integer. Converting them to Float64 may lose precision.

Packed arrays infer the widest floating component or Float64 for integer-only inputs. A cast selects Float16, Float32, or Float64 and checks finite overflow. Invalid tuples have a cleared validity bit and zero-filled numeric padding. A renderer must consult the validity bit before using their coordinates or colors.

Domain, conversion, overflow, and division-by-zero errors are eligible for TRY. Float16 requires native Float16Array support in the executing thread. Subnormal results and underflow to zero are allowed. TRY(expression, fallback) leaves valid nulls alone; use COALESCE(TRY(expression), fallback) to replace both. Type errors, malformed expressions, unknown fields, cancellation, and unexpected callback failures remain fatal.

Worker executionโ€‹

Send the parsed AST as the expression initialization value using the whole-table worker API. Register functions in the worker module:

import {initializeTaskWorker} from '@math.gl/tasks';
import {createArrowExpressionTask} from '@math.gl/sql/worker';
import {SQRT} from '@math.gl/sql/math-functions';

await initializeTaskWorker(createArrowExpressionTask({functions: [SQRT]}));

On the host, initialize the task with {expression: parseSQLExpression('TRY(SQRT(value), 0)')}. The worker binds the schema and handles row errors before transporting the Arrow result. Functions and compiled closures stay in the worker. Keep input ownership and cancellation explicit.

Performance and allocationโ€‹

Run yarn build and yarn benchmark:arrow-expressions. Use --quick for a smoke run. The benchmark reuses four Arrow batches and compiled expressions. It compares a numeric composition with the existing JavaScript evaluator over predecoded rows, then measures TRY with 1 in 1,024 and 1 in 4 rows failing. It reports median and p95 execution time, rows per second, result chunks, and retained output buffer bytes. Each Arrow result is checked against an independent expected value for every row outside the timed section.

Scalar compositions are fused; vector kernels and TRY/COALESCE can materialize temporary vectors. TRY tracks failed rows per scope and skips subsequent work for those rows. A lazy fallback can still require full-length temporary buffers for the active batch. Frequent exceptions are more expensive than predictable domain checks such as CASE WHEN value >= 0 THEN SQRT(value) END. Retained output bytes do not include these temporary vectors, masks, exceptions, or GC traffic. Decoding input rows and worker transfer are excluded from the timed comparison.

One warm run on Apple M2 with Node 24.5.0 used 65,536 rows, four batches, three warmups, and seven interleaved samples. CPU time helps distinguish execution cost from other work on the host. These measurements describe this workload, not a performance guarantee.

WorkloadMedian wall timeMedian CPU timeRows/sRetained output
Arrow SIN(x) + COS(y) * 275.2 ms28.4 ms871,000520 KiB
JavaScript evaluator, predecoded rows437.4 ms127.4 ms150,000512 KiB
TRY null recovery, 64 failed rows54.3 ms17.7 ms1,208,000520 KiB
TRY fallback, 64 failed rows74.9 ms26.1 ms875,000520 KiB
TRY null recovery, 16,384 failed rows515.9 ms170.1 ms127,000520 KiB
TRY fallback, 16,384 failed rows515.8 ms174.9 ms127,000520 KiB

Arrow's retained buffers include 8 KiB of validity bits. The JavaScript baseline also retains 65,536 decoded row objects created before timing; their heap size is not measured here.