@math.gl/sql 0.0.0-stage → 5.0.0-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +249 -0
- package/README.md +26 -2
- package/THIRD-PARTY-NOTICES.md +5 -0
- package/dist/arrow/aggregates.d.ts +46 -0
- package/dist/arrow/aggregates.d.ts.map +1 -0
- package/dist/arrow/aggregates.js +289 -0
- package/dist/arrow/aggregates.js.map +1 -0
- package/dist/arrow/buffers.d.ts +30 -0
- package/dist/arrow/buffers.d.ts.map +1 -0
- package/dist/arrow/buffers.js +177 -0
- package/dist/arrow/buffers.js.map +1 -0
- package/dist/arrow/cast.d.ts +19 -0
- package/dist/arrow/cast.d.ts.map +1 -0
- package/dist/arrow/cast.js +138 -0
- package/dist/arrow/cast.js.map +1 -0
- package/dist/arrow/compiler.d.ts +10 -0
- package/dist/arrow/compiler.d.ts.map +1 -0
- package/dist/arrow/compiler.js +1421 -0
- package/dist/arrow/compiler.js.map +1 -0
- package/dist/arrow/float16.d.ts +7 -0
- package/dist/arrow/float16.d.ts.map +1 -0
- package/dist/arrow/float16.js +26 -0
- package/dist/arrow/float16.js.map +1 -0
- package/dist/arrow/index.cjs +3220 -0
- package/dist/arrow/index.cjs.map +6 -0
- package/dist/arrow/index.d.ts +5 -0
- package/dist/arrow/index.d.ts.map +1 -0
- package/dist/arrow/index.js +7 -0
- package/dist/arrow/index.js.map +1 -0
- package/dist/arrow/like.d.ts +3 -0
- package/dist/arrow/like.d.ts.map +1 -0
- package/dist/arrow/like.js +75 -0
- package/dist/arrow/like.js.map +1 -0
- package/dist/arrow/list-values.d.ts +14 -0
- package/dist/arrow/list-values.d.ts.map +1 -0
- package/dist/arrow/list-values.js +180 -0
- package/dist/arrow/list-values.js.map +1 -0
- package/dist/arrow/predicate.d.ts +7 -0
- package/dist/arrow/predicate.d.ts.map +1 -0
- package/dist/arrow/predicate.js +10 -0
- package/dist/arrow/predicate.js.map +1 -0
- package/dist/arrow/query-arrow-aggregate-snapshots.d.ts +24 -0
- package/dist/arrow/query-arrow-aggregate-snapshots.d.ts.map +1 -0
- package/dist/arrow/query-arrow-aggregate-snapshots.js +64 -0
- package/dist/arrow/query-arrow-aggregate-snapshots.js.map +1 -0
- package/dist/arrow/query-arrow-sql.d.ts +8 -0
- package/dist/arrow/query-arrow-sql.d.ts.map +1 -0
- package/dist/arrow/query-arrow-sql.js +191 -0
- package/dist/arrow/query-arrow-sql.js.map +1 -0
- package/dist/arrow/query-arrow-table.d.ts +36 -0
- package/dist/arrow/query-arrow-table.d.ts.map +1 -0
- package/dist/arrow/query-arrow-table.js +208 -0
- package/dist/arrow/query-arrow-table.js.map +1 -0
- package/dist/arrow/query-worker-entry.d.ts +2 -0
- package/dist/arrow/query-worker-entry.d.ts.map +1 -0
- package/dist/arrow/query-worker-entry.js +7 -0
- package/dist/arrow/query-worker-entry.js.map +1 -0
- package/dist/arrow/query-worker.d.ts +65 -0
- package/dist/arrow/query-worker.d.ts.map +1 -0
- package/dist/arrow/query-worker.js +135 -0
- package/dist/arrow/query-worker.js.map +1 -0
- package/dist/arrow/registry.d.ts +4 -0
- package/dist/arrow/registry.d.ts.map +1 -0
- package/dist/arrow/registry.js +65 -0
- package/dist/arrow/registry.js.map +1 -0
- package/dist/arrow/relational-query.d.ts +36 -0
- package/dist/arrow/relational-query.d.ts.map +1 -0
- package/dist/arrow/relational-query.js +174 -0
- package/dist/arrow/relational-query.js.map +1 -0
- package/dist/arrow/replace-arrow-columns.d.ts +8 -0
- package/dist/arrow/replace-arrow-columns.d.ts.map +1 -0
- package/dist/arrow/replace-arrow-columns.js +63 -0
- package/dist/arrow/replace-arrow-columns.js.map +1 -0
- package/dist/arrow/signature.d.ts +6 -0
- package/dist/arrow/signature.d.ts.map +1 -0
- package/dist/arrow/signature.js +51 -0
- package/dist/arrow/signature.js.map +1 -0
- package/dist/arrow/temporal-cast.d.ts +9 -0
- package/dist/arrow/temporal-cast.d.ts.map +1 -0
- package/dist/arrow/temporal-cast.js +60 -0
- package/dist/arrow/temporal-cast.js.map +1 -0
- package/dist/arrow/types.d.ts +62 -0
- package/dist/arrow/types.d.ts.map +1 -0
- package/dist/arrow/types.js +5 -0
- package/dist/arrow/types.js.map +1 -0
- package/dist/arrow/worker-entry.d.ts +2 -0
- package/dist/arrow/worker-entry.d.ts.map +1 -0
- package/dist/arrow/worker-entry.js +9 -0
- package/dist/arrow/worker-entry.js.map +1 -0
- package/dist/arrow/worker.cjs +3973 -0
- package/dist/arrow/worker.cjs.map +6 -0
- package/dist/arrow/worker.d.ts +58 -0
- package/dist/arrow/worker.d.ts.map +1 -0
- package/dist/arrow/worker.js +84 -0
- package/dist/arrow/worker.js.map +1 -0
- package/dist/color/index.cjs +382 -0
- package/dist/color/index.cjs.map +6 -0
- package/dist/color/index.d.ts +26 -0
- package/dist/color/index.d.ts.map +1 -0
- package/dist/color/index.js +306 -0
- package/dist/color/index.js.map +1 -0
- package/dist/diagnostic-log.d.ts +8 -0
- package/dist/diagnostic-log.d.ts.map +1 -0
- package/dist/diagnostic-log.js +15 -0
- package/dist/diagnostic-log.js.map +1 -0
- package/dist/expressions/ast.d.ts +12 -0
- package/dist/expressions/ast.d.ts.map +1 -0
- package/dist/expressions/ast.js +112 -0
- package/dist/expressions/ast.js.map +1 -0
- package/dist/expressions/columnar-predicate.d.ts +56 -0
- package/dist/expressions/columnar-predicate.d.ts.map +1 -0
- package/dist/expressions/columnar-predicate.js +176 -0
- package/dist/expressions/columnar-predicate.js.map +1 -0
- package/dist/expressions/evaluation-error.d.ts +14 -0
- package/dist/expressions/evaluation-error.d.ts.map +1 -0
- package/dist/expressions/evaluation-error.js +26 -0
- package/dist/expressions/evaluation-error.js.map +1 -0
- package/dist/expressions/expression-eval.d.ts +96 -0
- package/dist/expressions/expression-eval.d.ts.map +1 -0
- package/dist/expressions/expression-eval.js +330 -0
- package/dist/expressions/expression-eval.js.map +1 -0
- package/dist/expressions/function-libraries.d.ts +63 -0
- package/dist/expressions/function-libraries.d.ts.map +1 -0
- package/dist/expressions/function-libraries.js +69 -0
- package/dist/expressions/function-libraries.js.map +1 -0
- package/dist/expressions/function-registry.d.ts +93 -0
- package/dist/expressions/function-registry.d.ts.map +1 -0
- package/dist/expressions/function-registry.js +139 -0
- package/dist/expressions/function-registry.js.map +1 -0
- package/dist/expressions/get.d.ts +9 -0
- package/dist/expressions/get.d.ts.map +1 -0
- package/dist/expressions/get.js +32 -0
- package/dist/expressions/get.js.map +1 -0
- package/dist/expressions/index.cjs +1888 -0
- package/dist/expressions/index.cjs.map +6 -0
- package/dist/expressions/index.d.ts +13 -0
- package/dist/expressions/index.d.ts.map +1 -0
- package/dist/expressions/index.js +14 -0
- package/dist/expressions/index.js.map +1 -0
- package/dist/expressions/numeric-functions.d.ts +277 -0
- package/dist/expressions/numeric-functions.d.ts.map +1 -0
- package/dist/expressions/numeric-functions.js +52 -0
- package/dist/expressions/numeric-functions.js.map +1 -0
- package/dist/expressions/parse-expression-string.d.ts +21 -0
- package/dist/expressions/parse-expression-string.d.ts.map +1 -0
- package/dist/expressions/parse-expression-string.js +61 -0
- package/dist/expressions/parse-expression-string.js.map +1 -0
- package/dist/expressions/predicate-expression.d.ts +7 -0
- package/dist/expressions/predicate-expression.d.ts.map +1 -0
- package/dist/expressions/predicate-expression.js +112 -0
- package/dist/expressions/predicate-expression.js.map +1 -0
- package/dist/expressions/scalar-type.d.ts +6 -0
- package/dist/expressions/scalar-type.d.ts.map +1 -0
- package/dist/expressions/scalar-type.js +23 -0
- package/dist/expressions/scalar-type.js.map +1 -0
- package/dist/expressions/vendor/jsep/runtime.d.ts +3 -0
- package/dist/expressions/vendor/jsep/runtime.d.ts.map +1 -0
- package/dist/expressions/vendor/jsep/runtime.js +972 -0
- package/dist/expressions/vendor/jsep/runtime.js.map +1 -0
- package/dist/expressions/vendor/jsep/types.d.ts +148 -0
- package/dist/expressions/vendor/jsep/types.d.ts.map +1 -0
- package/dist/expressions/vendor/jsep/types.js +6 -0
- package/dist/expressions/vendor/jsep/types.js.map +1 -0
- package/dist/index.cjs +4865 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/math/index.cjs +444 -0
- package/dist/math/index.cjs.map +6 -0
- package/dist/math/index.d.ts +44 -0
- package/dist/math/index.d.ts.map +1 -0
- package/dist/math/index.js +182 -0
- package/dist/math/index.js.map +1 -0
- package/dist/scales/arrow-domains.d.ts +15 -0
- package/dist/scales/arrow-domains.d.ts.map +1 -0
- package/dist/scales/arrow-domains.js +124 -0
- package/dist/scales/arrow-domains.js.map +1 -0
- package/dist/scales/arrow-scales.cjs +3837 -0
- package/dist/scales/arrow-scales.cjs.map +6 -0
- package/dist/scales/arrow-scales.d.ts +20 -0
- package/dist/scales/arrow-scales.d.ts.map +1 -0
- package/dist/scales/arrow-scales.js +109 -0
- package/dist/scales/arrow-scales.js.map +1 -0
- package/dist/scales/compile-scale.d.ts +7 -0
- package/dist/scales/compile-scale.d.ts.map +1 -0
- package/dist/scales/compile-scale.js +195 -0
- package/dist/scales/compile-scale.js.map +1 -0
- package/dist/scales/hdr-colors.d.ts +13 -0
- package/dist/scales/hdr-colors.d.ts.map +1 -0
- package/dist/scales/hdr-colors.js +115 -0
- package/dist/scales/hdr-colors.js.map +1 -0
- package/dist/scales/index.cjs +1214 -0
- package/dist/scales/index.cjs.map +6 -0
- package/dist/scales/index.d.ts +9 -0
- package/dist/scales/index.d.ts.map +1 -0
- package/dist/scales/index.js +10 -0
- package/dist/scales/index.js.map +1 -0
- package/dist/scales/palettes.d.ts +5 -0
- package/dist/scales/palettes.d.ts.map +1 -0
- package/dist/scales/palettes.js +85 -0
- package/dist/scales/palettes.js.map +1 -0
- package/dist/scales/parse-sql-scale.d.ts +14 -0
- package/dist/scales/parse-sql-scale.d.ts.map +1 -0
- package/dist/scales/parse-sql-scale.js +165 -0
- package/dist/scales/parse-sql-scale.js.map +1 -0
- package/dist/scales/quantile-sketch.d.ts +22 -0
- package/dist/scales/quantile-sketch.d.ts.map +1 -0
- package/dist/scales/quantile-sketch.js +108 -0
- package/dist/scales/quantile-sketch.js.map +1 -0
- package/dist/scales/scale-types.d.ts +92 -0
- package/dist/scales/scale-types.d.ts.map +1 -0
- package/dist/scales/scale-types.js +119 -0
- package/dist/scales/scale-types.js.map +1 -0
- package/dist/scales/streaming-domains.d.ts +18 -0
- package/dist/scales/streaming-domains.d.ts.map +1 -0
- package/dist/scales/streaming-domains.js +109 -0
- package/dist/scales/streaming-domains.js.map +1 -0
- package/dist/sql/bind-sql-predicate.d.ts +4 -0
- package/dist/sql/bind-sql-predicate.d.ts.map +1 -0
- package/dist/sql/bind-sql-predicate.js +11 -0
- package/dist/sql/bind-sql-predicate.js.map +1 -0
- package/dist/sql/index.cjs +1588 -0
- package/dist/sql/index.cjs.map +6 -0
- package/dist/sql/index.d.ts +10 -0
- package/dist/sql/index.d.ts.map +1 -0
- package/dist/sql/index.js +10 -0
- package/dist/sql/index.js.map +1 -0
- package/dist/sql/parse-sql-expression.d.ts +44 -0
- package/dist/sql/parse-sql-expression.d.ts.map +1 -0
- package/dist/sql/parse-sql-expression.js +455 -0
- package/dist/sql/parse-sql-expression.js.map +1 -0
- package/dist/sql/parse-sql-predicate.d.ts +13 -0
- package/dist/sql/parse-sql-predicate.d.ts.map +1 -0
- package/dist/sql/parse-sql-predicate.js +365 -0
- package/dist/sql/parse-sql-predicate.js.map +1 -0
- package/dist/sql/parse-sql-query.d.ts +8 -0
- package/dist/sql/parse-sql-query.d.ts.map +1 -0
- package/dist/sql/parse-sql-query.js +66 -0
- package/dist/sql/parse-sql-query.js.map +1 -0
- package/dist/sql/parse-sql-select.d.ts +55 -0
- package/dist/sql/parse-sql-select.d.ts.map +1 -0
- package/dist/sql/parse-sql-select.js +244 -0
- package/dist/sql/parse-sql-select.js.map +1 -0
- package/dist/sql/parse-sql-statement.d.ts +50 -0
- package/dist/sql/parse-sql-statement.d.ts.map +1 -0
- package/dist/sql/parse-sql-statement.js +182 -0
- package/dist/sql/parse-sql-statement.js.map +1 -0
- package/dist/sql/query-types.d.ts +56 -0
- package/dist/sql/query-types.d.ts.map +1 -0
- package/dist/sql/query-types.js +5 -0
- package/dist/sql/query-types.js.map +1 -0
- package/dist/sql/sql-parse-error.d.ts +13 -0
- package/dist/sql/sql-parse-error.d.ts.map +1 -0
- package/dist/sql/sql-parse-error.js +20 -0
- package/dist/sql/sql-parse-error.js.map +1 -0
- package/dist/sql/sql-predicate-schema.d.ts +164 -0
- package/dist/sql/sql-predicate-schema.d.ts.map +1 -0
- package/dist/sql/sql-predicate-schema.js +249 -0
- package/dist/sql/sql-predicate-schema.js.map +1 -0
- package/dist/sql/sql-predicate-types.d.ts +48 -0
- package/dist/sql/sql-predicate-types.d.ts.map +1 -0
- package/dist/sql/sql-predicate-types.js +9 -0
- package/dist/sql/sql-predicate-types.js.map +1 -0
- package/dist/sql/sql-predicate.d.ts +6 -0
- package/dist/sql/sql-predicate.d.ts.map +1 -0
- package/dist/sql/sql-predicate.js +8 -0
- package/dist/sql/sql-predicate.js.map +1 -0
- package/package.json +92 -4
- package/src/arrow/aggregates.ts +386 -0
- package/src/arrow/buffers.ts +177 -0
- package/src/arrow/cast.ts +160 -0
- package/src/arrow/compiler.ts +1659 -0
- package/src/arrow/float16.ts +25 -0
- package/src/arrow/index.ts +22 -0
- package/src/arrow/like.ts +72 -0
- package/src/arrow/list-values.ts +200 -0
- package/src/arrow/predicate.ts +19 -0
- package/src/arrow/query-arrow-aggregate-snapshots.ts +107 -0
- package/src/arrow/query-arrow-sql.ts +227 -0
- package/src/arrow/query-arrow-table.ts +298 -0
- package/src/arrow/query-worker-entry.ts +8 -0
- package/src/arrow/query-worker.ts +190 -0
- package/src/arrow/registry.ts +86 -0
- package/src/arrow/relational-query.ts +247 -0
- package/src/arrow/replace-arrow-columns.ts +71 -0
- package/src/arrow/signature.ts +50 -0
- package/src/arrow/temporal-cast.ts +66 -0
- package/src/arrow/types.ts +76 -0
- package/src/arrow/worker-entry.ts +10 -0
- package/src/arrow/worker.ts +139 -0
- package/src/color/index.ts +405 -0
- package/src/diagnostic-log.ts +20 -0
- package/src/expressions/ast.ts +141 -0
- package/src/expressions/columnar-predicate.ts +293 -0
- package/src/expressions/evaluation-error.ts +29 -0
- package/src/expressions/expression-eval.ts +480 -0
- package/src/expressions/function-libraries.ts +126 -0
- package/src/expressions/function-registry.ts +171 -0
- package/src/expressions/get.ts +37 -0
- package/src/expressions/index.ts +40 -0
- package/src/expressions/numeric-functions.ts +99 -0
- package/src/expressions/parse-expression-string.ts +80 -0
- package/src/expressions/predicate-expression.ts +128 -0
- package/src/expressions/scalar-type.ts +39 -0
- package/src/expressions/vendor/jsep/LICENSE.txt +23 -0
- package/src/expressions/vendor/jsep/README.md +22 -0
- package/src/expressions/vendor/jsep/runtime.ts +949 -0
- package/src/expressions/vendor/jsep/types.ts +191 -0
- package/src/index.ts +40 -0
- package/src/math/index.ts +235 -0
- package/src/scales/arrow-domains.ts +131 -0
- package/src/scales/arrow-scales.ts +156 -0
- package/src/scales/compile-scale.ts +217 -0
- package/src/scales/hdr-colors.ts +126 -0
- package/src/scales/index.ts +35 -0
- package/src/scales/palettes.ts +86 -0
- package/src/scales/parse-sql-scale.ts +195 -0
- package/src/scales/quantile-sketch.ts +106 -0
- package/src/scales/scale-types.ts +224 -0
- package/src/scales/streaming-domains.ts +133 -0
- package/src/sql/bind-sql-predicate.ts +21 -0
- package/src/sql/index.ts +28 -0
- package/src/sql/parse-sql-expression.ts +457 -0
- package/src/sql/parse-sql-predicate.ts +454 -0
- package/src/sql/parse-sql-query.ts +72 -0
- package/src/sql/parse-sql-select.ts +266 -0
- package/src/sql/parse-sql-statement.ts +242 -0
- package/src/sql/query-types.ts +71 -0
- package/src/sql/sql-parse-error.ts +33 -0
- package/src/sql/sql-predicate-schema.ts +279 -0
- package/src/sql/sql-predicate-types.ts +91 -0
- package/src/sql/sql-predicate.ts +28 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import {getFloat16ArrayConstructor, isFloat16Array, isFloat16ArrayConstructor} from '@math.gl/core';
|
|
5
|
+
|
|
6
|
+
/** Requires decoded native binary16 arithmetic rather than treating Uint16 storage as numbers. */
|
|
7
|
+
export function requireFloat16(): void {
|
|
8
|
+
if (!isFloat16ArrayConstructor(getFloat16ArrayConstructor()))
|
|
9
|
+
throw new Error('Arrow Float16 expressions require native Float16Array support');
|
|
10
|
+
}
|
|
11
|
+
/** Views Arrow Uint16 binary16 storage through the native decoded numeric array, without copying. */
|
|
12
|
+
export function float16Values(values: Uint16Array | Float16Array): Float16Array {
|
|
13
|
+
if (isFloat16Array(values)) return values;
|
|
14
|
+
requireFloat16();
|
|
15
|
+
const Constructor = getFloat16ArrayConstructor() as Float16ArrayConstructor;
|
|
16
|
+
return new Constructor(values.buffer, values.byteOffset, values.length) as Float16Array;
|
|
17
|
+
}
|
|
18
|
+
/** Creates one rounding cell per bound operation. */
|
|
19
|
+
export function roundFloat16(): (value: number) => number {
|
|
20
|
+
const cell = float16Values(new Uint16Array(1));
|
|
21
|
+
return value => {
|
|
22
|
+
cell[0] = value;
|
|
23
|
+
return cell[0]!;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
export {compileArrowArray, compileArrowExpression, compileArrowFunction} from './compiler';
|
|
6
|
+
export {compileArrowPredicate} from './predicate';
|
|
7
|
+
export type {
|
|
8
|
+
ArrowExpression,
|
|
9
|
+
ArrowExpressionOptions,
|
|
10
|
+
ArrowExpressionEvaluationOptions,
|
|
11
|
+
ArrowExpressionValue,
|
|
12
|
+
ArrowTableFunction,
|
|
13
|
+
ArrowTableFunctionPrototype,
|
|
14
|
+
ArrowTableFunctionType,
|
|
15
|
+
CompiledArrowExpression
|
|
16
|
+
} from './types';
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
ExpressionEvaluationError,
|
|
20
|
+
isExpressionEvaluationError,
|
|
21
|
+
type ExpressionEvaluationErrorCode
|
|
22
|
+
} from '../expressions/index';
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/** Compiles SQL wildcards once and matches Arrow UTF-8 ranges without decoding rows. */
|
|
6
|
+
export function compileUTF8Like(pattern: string, insensitive: boolean, escape?: string) {
|
|
7
|
+
if (escape !== undefined && [...escape].length !== 1) {
|
|
8
|
+
throw new Error('LIKE ESCAPE requires one character');
|
|
9
|
+
}
|
|
10
|
+
const tokens: (string | number)[] = [];
|
|
11
|
+
let escaped = false;
|
|
12
|
+
for (const character of pattern) {
|
|
13
|
+
if (!escaped && character === escape) {
|
|
14
|
+
escaped = true;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
tokens.push(
|
|
18
|
+
!escaped && character === '%'
|
|
19
|
+
? -1
|
|
20
|
+
: !escaped && character === '_'
|
|
21
|
+
? -2
|
|
22
|
+
: fold(character.codePointAt(0)!, insensitive)
|
|
23
|
+
);
|
|
24
|
+
escaped = false;
|
|
25
|
+
}
|
|
26
|
+
if (escaped) throw new Error('LIKE pattern ends with ESCAPE');
|
|
27
|
+
|
|
28
|
+
return (bytes: Uint8Array, start: number, end: number): boolean => {
|
|
29
|
+
let offset = start;
|
|
30
|
+
let token = 0;
|
|
31
|
+
let wildcard = -1;
|
|
32
|
+
let retry = start;
|
|
33
|
+
while (offset < end) {
|
|
34
|
+
if (tokens[token] === -1) {
|
|
35
|
+
wildcard = token++;
|
|
36
|
+
retry = offset;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const [point, next] = readCodePoint(bytes, offset);
|
|
40
|
+
if (tokens[token] === -2 || tokens[token] === fold(point, insensitive)) {
|
|
41
|
+
token++;
|
|
42
|
+
offset = next;
|
|
43
|
+
} else if (wildcard >= 0) {
|
|
44
|
+
retry = readCodePoint(bytes, retry)[1];
|
|
45
|
+
offset = retry;
|
|
46
|
+
token = wildcard + 1;
|
|
47
|
+
} else {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
while (tokens[token] === -1) token++;
|
|
52
|
+
return token === tokens.length;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Folds ASCII directly, using locale-independent Unicode lowercase for other characters. */
|
|
57
|
+
function fold(point: number, insensitive: boolean): number | string {
|
|
58
|
+
if (!insensitive) return point;
|
|
59
|
+
if (point < 128) return point >= 65 && point <= 90 ? point + 32 : point;
|
|
60
|
+
const folded = String.fromCodePoint(point).toLowerCase();
|
|
61
|
+
return folded.length === 1 && folded.charCodeAt(0) < 128 ? folded.charCodeAt(0) : folded;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Reads one code point from valid Arrow UTF-8 and returns its next byte offset. */
|
|
65
|
+
function readCodePoint(bytes: Uint8Array, offset: number): [number, number] {
|
|
66
|
+
const first = bytes[offset++]!;
|
|
67
|
+
if (first < 128) return [first, offset];
|
|
68
|
+
const count = first < 224 ? 1 : first < 240 ? 2 : 3;
|
|
69
|
+
let point = first & (count === 1 ? 31 : count === 2 ? 15 : 7);
|
|
70
|
+
for (let index = 0; index < count; index++) point = (point << 6) | (bytes[offset++]! & 63);
|
|
71
|
+
return [point, offset];
|
|
72
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
DataType,
|
|
7
|
+
Precision,
|
|
8
|
+
Dictionary,
|
|
9
|
+
Uint8,
|
|
10
|
+
Vector,
|
|
11
|
+
makeData,
|
|
12
|
+
makeVector,
|
|
13
|
+
vectorFromArray,
|
|
14
|
+
type Field,
|
|
15
|
+
type FixedSizeList,
|
|
16
|
+
type List
|
|
17
|
+
} from 'apache-arrow';
|
|
18
|
+
import {float16Values, roundFloat16} from './float16';
|
|
19
|
+
import {ColumnReader, typeKey, validateValue} from './buffers';
|
|
20
|
+
import type {ArrowExpressionValue} from './types';
|
|
21
|
+
|
|
22
|
+
/** Fixed numeric tuples are vector results; components of a valid tuple cannot be null. */
|
|
23
|
+
export function isTupleType(type: DataType): type is FixedSizeList {
|
|
24
|
+
if (!DataType.isFixedSizeList(type)) return false;
|
|
25
|
+
const child = type.children[0];
|
|
26
|
+
if (!Number.isSafeInteger(type.listSize) || type.listSize <= 0 || !child || child.nullable)
|
|
27
|
+
return false;
|
|
28
|
+
if (!DataType.isInt(child.type) && !DataType.isFloat(child.type)) return false;
|
|
29
|
+
typeKey(child.type);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Builds a correctly typed all-null result, including zero-row batches. */
|
|
34
|
+
export function nullTupleVector(type: FixedSizeList, length: number): Vector {
|
|
35
|
+
const childType = type.children[0]!.type;
|
|
36
|
+
const child = makeData({
|
|
37
|
+
type: childType,
|
|
38
|
+
length: length * type.listSize,
|
|
39
|
+
data: new childType.ArrayType(length * type.listSize)
|
|
40
|
+
} as any);
|
|
41
|
+
return makeVector(
|
|
42
|
+
makeData({
|
|
43
|
+
type,
|
|
44
|
+
length,
|
|
45
|
+
child,
|
|
46
|
+
nullCount: length,
|
|
47
|
+
nullBitmap: new Uint8Array(Math.ceil(length / 8))
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Masks parent validity while preserving the numeric child buffers and slice offsets. */
|
|
53
|
+
export function maskTupleVector(vector: Vector, active: Uint8Array): Vector {
|
|
54
|
+
if (!active.some(value => !value)) return vector;
|
|
55
|
+
let start = 0;
|
|
56
|
+
const chunks = vector.data.map(data => {
|
|
57
|
+
const bitmap = new Uint8Array(Math.ceil((data.offset + data.length) / 8));
|
|
58
|
+
let nullCount = 0;
|
|
59
|
+
for (let row = 0; row < data.length; row++) {
|
|
60
|
+
if (active[start + row] && data.getValid(row)) {
|
|
61
|
+
const bit = data.offset + row;
|
|
62
|
+
bitmap[bit >> 3] |= 1 << (bit & 7);
|
|
63
|
+
} else nullCount++;
|
|
64
|
+
}
|
|
65
|
+
start += data.length;
|
|
66
|
+
const buffers = data.buffers;
|
|
67
|
+
buffers[2] = bitmap;
|
|
68
|
+
return data.clone(data.type, data.offset, data.length, nullCount, buffers);
|
|
69
|
+
});
|
|
70
|
+
return new Vector(chunks);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Validates active tuple rows before exposing a kernel's buffers. */
|
|
74
|
+
export function validateTupleVector(vector: Vector, field: Field, active: Uint8Array): void {
|
|
75
|
+
const type = field.type as FixedSizeList;
|
|
76
|
+
let start = 0;
|
|
77
|
+
for (const data of vector.data) {
|
|
78
|
+
const child = data.children[0];
|
|
79
|
+
if (!child || child.length < data.length * type.listSize)
|
|
80
|
+
throw new Error('Invalid fixed-size list child length');
|
|
81
|
+
for (let row = 0; row < data.length; row++) {
|
|
82
|
+
if (!active[start + row]) continue;
|
|
83
|
+
if (!data.getValid(row)) {
|
|
84
|
+
if (!field.nullable)
|
|
85
|
+
throw new Error(`Function ${field.name} returned null for a non-nullable result`);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
for (let component = 0; component < type.listSize; component++) {
|
|
89
|
+
if (!child.getValid(row * type.listSize + component))
|
|
90
|
+
throw new Error('Fixed-size list result contains a null non-nullable component');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
start += data.length;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Selects branch tuples without decoding rows or constructing per-row arrays. */
|
|
98
|
+
export function selectTupleVectors(
|
|
99
|
+
type: FixedSizeList,
|
|
100
|
+
yes: Vector,
|
|
101
|
+
no: Vector,
|
|
102
|
+
selected: Uint8Array,
|
|
103
|
+
active: Uint8Array
|
|
104
|
+
): Vector {
|
|
105
|
+
const result = nullTupleVector(type, active.length);
|
|
106
|
+
const data = result.data[0]!;
|
|
107
|
+
const child = data.children[0]!;
|
|
108
|
+
const values =
|
|
109
|
+
DataType.isFloat(child.type) && child.type.precision === Precision.HALF
|
|
110
|
+
? float16Values(child.values as Uint16Array)
|
|
111
|
+
: child.values;
|
|
112
|
+
const a = new ColumnReader(yes.getChildAt(0)!);
|
|
113
|
+
const b = new ColumnReader(no.getChildAt(0)!);
|
|
114
|
+
const aValid = new ColumnReader(yes);
|
|
115
|
+
const bValid = new ColumnReader(no);
|
|
116
|
+
for (let row = 0; row < active.length; row++) {
|
|
117
|
+
if (!active[row]) continue;
|
|
118
|
+
const reader = selected[row] ? a : b;
|
|
119
|
+
if (!(selected[row] ? aValid : bValid).isValid(row)) continue;
|
|
120
|
+
data.setValid(row, true);
|
|
121
|
+
for (let component = 0; component < type.listSize; component++) {
|
|
122
|
+
const index = row * type.listSize + component;
|
|
123
|
+
values[index] = reader.get(index) as any;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Compiles a bounded flat array literal against its declared list element type. */
|
|
130
|
+
export function compileArrayLiteral(ast: any, type: DataType): (length: number) => Vector {
|
|
131
|
+
if (!DataType.isList(type) && !DataType.isFixedSizeList(type))
|
|
132
|
+
throw new Error('Array literals require a declared List or FixedSizeList argument');
|
|
133
|
+
const listType = type as List | FixedSizeList;
|
|
134
|
+
const child = listType.children[0]!;
|
|
135
|
+
if (DataType.isDictionary(child.type))
|
|
136
|
+
throw new Error('Array literal elements require primitive types');
|
|
137
|
+
typeKey(child.type);
|
|
138
|
+
if (ast.elements.length > 4096) throw new Error('Array literal exceeds 4096 elements');
|
|
139
|
+
if (DataType.isFixedSizeList(type) && ast.elements.length !== type.listSize)
|
|
140
|
+
throw new Error('Array literal length must match its FixedSizeList type');
|
|
141
|
+
if (DataType.isFixedSizeList(type) && !isTupleType(type))
|
|
142
|
+
throw new Error('Fixed-size array literals require non-nullable numeric elements');
|
|
143
|
+
const round = DataType.isFloat(child.type)
|
|
144
|
+
? child.type.precision === Precision.HALF
|
|
145
|
+
? roundFloat16()
|
|
146
|
+
: child.type.precision === Precision.SINGLE
|
|
147
|
+
? Math.fround
|
|
148
|
+
: undefined
|
|
149
|
+
: undefined;
|
|
150
|
+
const values = Array.from(ast.elements, (element: any): ArrowExpressionValue => {
|
|
151
|
+
let value: ArrowExpressionValue;
|
|
152
|
+
if (element?.type === 'Literal') value = element.value;
|
|
153
|
+
else if (
|
|
154
|
+
element?.type === 'UnaryExpression' &&
|
|
155
|
+
['+', '-'].includes(element.operator) &&
|
|
156
|
+
element.argument?.type === 'Literal' &&
|
|
157
|
+
typeof element.argument.value === 'number'
|
|
158
|
+
)
|
|
159
|
+
value = element.operator === '-' ? -element.argument.value : element.argument.value;
|
|
160
|
+
else throw new Error('Array elements must be scalar literals');
|
|
161
|
+
if (value === null && !child.nullable)
|
|
162
|
+
throw new Error('Array literal contains a null non-nullable element');
|
|
163
|
+
validateValue(value, child.type);
|
|
164
|
+
if (round && typeof value === 'number') {
|
|
165
|
+
const rounded = round(value);
|
|
166
|
+
if (Number.isFinite(value) && !Number.isFinite(rounded))
|
|
167
|
+
throw new Error(`Numeric literal overflow for ${child.type}`);
|
|
168
|
+
return rounded;
|
|
169
|
+
}
|
|
170
|
+
return value;
|
|
171
|
+
});
|
|
172
|
+
const literal = vectorFromArray([values], listType);
|
|
173
|
+
const dictionaryType = new Dictionary(listType, new Uint8());
|
|
174
|
+
let cached: Vector | undefined;
|
|
175
|
+
return length => {
|
|
176
|
+
if (!DataType.isFixedSizeList(listType) && cached?.length === length) return cached;
|
|
177
|
+
if (DataType.isFixedSizeList(listType)) {
|
|
178
|
+
// Fixed tuples can be expression results, so retain their ordinary Arrow representation.
|
|
179
|
+
const result = nullTupleVector(listType, length);
|
|
180
|
+
const data = result.data[0]!;
|
|
181
|
+
const childData = data.children[0]!;
|
|
182
|
+
const components =
|
|
183
|
+
DataType.isFloat(childData.type) && childData.type.precision === Precision.HALF
|
|
184
|
+
? float16Values(childData.values as Uint16Array)
|
|
185
|
+
: childData.values;
|
|
186
|
+
for (let row = 0; row < length; row++) {
|
|
187
|
+
data.setValid(row, true);
|
|
188
|
+
for (let component = 0; component < listType.listSize; component++)
|
|
189
|
+
components[row * listType.listSize + component] = values[component] as any;
|
|
190
|
+
}
|
|
191
|
+
cached = result;
|
|
192
|
+
} else {
|
|
193
|
+
// One dictionary value holds the complete list; never repeat it for every table row.
|
|
194
|
+
cached = makeVector(
|
|
195
|
+
makeData({type: dictionaryType, length, data: new Uint8Array(length), dictionary: literal})
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
return cached;
|
|
199
|
+
};
|
|
200
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
columnarPredicateToExpression,
|
|
7
|
+
type ParameterizedColumnarPredicate,
|
|
8
|
+
type ColumnarPredicateParameterValues
|
|
9
|
+
} from '../expressions/index';
|
|
10
|
+
import {compileArrowExpression} from './compiler';
|
|
11
|
+
import type {ArrowExpressionOptions, CompiledArrowExpression} from './types';
|
|
12
|
+
|
|
13
|
+
/** Compiles portable WHERE predicates with SQL three-valued Boolean results. */
|
|
14
|
+
export function compileArrowPredicate(
|
|
15
|
+
predicate: ParameterizedColumnarPredicate,
|
|
16
|
+
options: ArrowExpressionOptions & {parameters?: ColumnarPredicateParameterValues}
|
|
17
|
+
): CompiledArrowExpression {
|
|
18
|
+
return compileArrowExpression(columnarPredicateToExpression(predicate, options), options);
|
|
19
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {DataType, Schema, Table, vectorFromArray, type RecordBatch} from 'apache-arrow';
|
|
6
|
+
import {compileArrowAggregates, type ArrowAggregate} from './aggregates';
|
|
7
|
+
import {queryArrowTableInBatches, type ArrowBatchQuery} from './query-arrow-table';
|
|
8
|
+
import {getArrowAggregateFields} from './relational-query';
|
|
9
|
+
|
|
10
|
+
/** Configuration for incremental aggregation with optional filtering and one group column. */
|
|
11
|
+
export type ArrowAggregateSnapshotOptions = Omit<ArrowBatchQuery, 'from' | 'select'> & {
|
|
12
|
+
/** Names and definitions of the output aggregates. */
|
|
13
|
+
aggregates: Readonly<Record<string, ArrowAggregate>>;
|
|
14
|
+
/** Source column used as a group key. Omit it for scalar aggregates. */
|
|
15
|
+
groupBy?: string;
|
|
16
|
+
/** Emit after this many nonempty query batches. Defaults to one. */
|
|
17
|
+
emitEveryBatches?: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** A complete aggregate result at one point in the input stream. */
|
|
21
|
+
export type ArrowAggregateSnapshot = {
|
|
22
|
+
/** Independent result table. */
|
|
23
|
+
table: Table;
|
|
24
|
+
/** Number of rows admitted to the accumulator. */
|
|
25
|
+
rowsProcessed: number;
|
|
26
|
+
/** Whether the input query is complete. */
|
|
27
|
+
final: boolean;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Emits bounded-state aggregate snapshots, including a final snapshot for empty input. */
|
|
31
|
+
export async function* queryArrowAggregateSnapshots(
|
|
32
|
+
input: AsyncIterable<RecordBatch> | Iterable<RecordBatch>,
|
|
33
|
+
inputSchema: Schema,
|
|
34
|
+
options: ArrowAggregateSnapshotOptions
|
|
35
|
+
): AsyncIterable<ArrowAggregateSnapshot> {
|
|
36
|
+
const {aggregates, groupBy, emitEveryBatches = 1, ...query} = options;
|
|
37
|
+
query.signal?.throwIfAborted();
|
|
38
|
+
if (!Number.isSafeInteger(emitEveryBatches) || emitEveryBatches < 1) {
|
|
39
|
+
throw new Error('emitEveryBatches must be a positive safe integer');
|
|
40
|
+
}
|
|
41
|
+
if (!Object.keys(aggregates).length) throw new Error('Expected at least one Arrow aggregate');
|
|
42
|
+
const groupField =
|
|
43
|
+
groupBy === undefined ? undefined : inputSchema.fields.find(field => field.name === groupBy);
|
|
44
|
+
if (groupBy !== undefined && !groupField)
|
|
45
|
+
throw new Error(`Unknown Arrow group column: ${groupBy}`);
|
|
46
|
+
if (groupField && Object.prototype.hasOwnProperty.call(aggregates, groupField.name)) {
|
|
47
|
+
throw new Error('Duplicate Arrow grouping output name');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const compileOptions = {inputSchema, functions: query.functions};
|
|
51
|
+
const plan = compileArrowAggregates(aggregates, compileOptions);
|
|
52
|
+
const grouped = groupField
|
|
53
|
+
? plan.createGroupedAccumulator({type: 'Identifier', name: groupField.name})
|
|
54
|
+
: undefined;
|
|
55
|
+
const scalar = grouped ? undefined : plan.createAccumulator();
|
|
56
|
+
const aggregateFields = getArrowAggregateFields(aggregates, compileOptions);
|
|
57
|
+
const outputGroupField =
|
|
58
|
+
groupField && DataType.isDictionary(groupField.type)
|
|
59
|
+
? groupField.clone({type: groupField.type.dictionary})
|
|
60
|
+
: groupField;
|
|
61
|
+
const schema = new Schema(
|
|
62
|
+
outputGroupField ? [outputGroupField, ...aggregateFields] : aggregateFields,
|
|
63
|
+
inputSchema.metadata
|
|
64
|
+
);
|
|
65
|
+
let rowsProcessed = 0;
|
|
66
|
+
let batchesSinceEmission = 0;
|
|
67
|
+
|
|
68
|
+
/** Builds a snapshot from detached accumulator results. */
|
|
69
|
+
function snapshot(final: boolean): ArrowAggregateSnapshot {
|
|
70
|
+
const groups = grouped?.result();
|
|
71
|
+
const rows = groups ? [...groups.values()] : [scalar!.result()];
|
|
72
|
+
const columns = new Map(
|
|
73
|
+
aggregateFields.map(field => [
|
|
74
|
+
field.name,
|
|
75
|
+
vectorFromArray(
|
|
76
|
+
rows.map(row => row[field.name]),
|
|
77
|
+
field.type
|
|
78
|
+
)
|
|
79
|
+
])
|
|
80
|
+
);
|
|
81
|
+
if (outputGroupField && groups) {
|
|
82
|
+
columns.set(
|
|
83
|
+
outputGroupField.name,
|
|
84
|
+
vectorFromArray([...groups.keys()], outputGroupField.type)
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
table: new Table(
|
|
89
|
+
schema,
|
|
90
|
+
Object.fromEntries(schema.fields.map(field => [field.name, columns.get(field.name)!]))
|
|
91
|
+
),
|
|
92
|
+
rowsProcessed,
|
|
93
|
+
final
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
for await (const batch of queryArrowTableInBatches(input, inputSchema, query)) {
|
|
98
|
+
(grouped ?? scalar)!.update(batch, {signal: query.signal});
|
|
99
|
+
rowsProcessed += batch.numRows;
|
|
100
|
+
if (++batchesSinceEmission === emitEveryBatches) {
|
|
101
|
+
yield snapshot(false);
|
|
102
|
+
batchesSinceEmission = 0;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
query.signal?.throwIfAborted();
|
|
106
|
+
yield snapshot(true);
|
|
107
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {Table} from 'apache-arrow';
|
|
6
|
+
import type {Expression} from '../expressions/index';
|
|
7
|
+
import {
|
|
8
|
+
parseSQLSelectCompound,
|
|
9
|
+
type SQLSelectStatement,
|
|
10
|
+
type SQLSelectStatementOptions,
|
|
11
|
+
type SQLSort
|
|
12
|
+
} from '../sql/parse-sql-statement';
|
|
13
|
+
import {queryArrowTable, type ArrowTableQuery} from './query-arrow-table';
|
|
14
|
+
import type {ArrowAggregate} from './aggregates';
|
|
15
|
+
|
|
16
|
+
type Execution = Pick<ArrowTableQuery, 'signal' | 'functions'>;
|
|
17
|
+
|
|
18
|
+
/** Executes relational SQL over a caller-supplied catalog of in-memory Arrow tables. */
|
|
19
|
+
export function queryArrowSQL(
|
|
20
|
+
sql: string,
|
|
21
|
+
tables: ReadonlyMap<string, Table>,
|
|
22
|
+
options: Execution & SQLSelectStatementOptions = {}
|
|
23
|
+
): Table {
|
|
24
|
+
const {inputs, orderBy, offset, limit} = parseSQLSelectCompound(sql, options);
|
|
25
|
+
const execution = {signal: options.signal, functions: options.functions};
|
|
26
|
+
const [first, ...rest] = inputs.map(input => executeSelect(input, tables, execution)) as [
|
|
27
|
+
Table,
|
|
28
|
+
...Table[]
|
|
29
|
+
];
|
|
30
|
+
// Plan the final order against the projected names, including SELECT aliases.
|
|
31
|
+
return queryArrowTable(first, {
|
|
32
|
+
...execution,
|
|
33
|
+
union: rest.map(table => {
|
|
34
|
+
if (table.numCols !== first.numCols)
|
|
35
|
+
throw new Error('Arrow SQL UNION ALL requires the same number of columns in each input');
|
|
36
|
+
return queryArrowTable(table, {
|
|
37
|
+
select: table.schema.fields.map((field, index) => ({
|
|
38
|
+
column: field.name,
|
|
39
|
+
as: first.schema.fields[index]!.name
|
|
40
|
+
})),
|
|
41
|
+
signal: execution.signal
|
|
42
|
+
});
|
|
43
|
+
}),
|
|
44
|
+
orderBy: planOrder(orderBy),
|
|
45
|
+
offset,
|
|
46
|
+
limit
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function planOrder(orderBy?: readonly SQLSort[]) {
|
|
51
|
+
return orderBy?.map(({expression, direction, nulls}) => ({
|
|
52
|
+
column: requireColumn(expression),
|
|
53
|
+
direction,
|
|
54
|
+
nulls
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function executeSelect(
|
|
59
|
+
statement: SQLSelectStatement,
|
|
60
|
+
tables: ReadonlyMap<string, Table>,
|
|
61
|
+
execution: Execution
|
|
62
|
+
): Table {
|
|
63
|
+
const source = getTable(tables, statement.from.name);
|
|
64
|
+
const leftAlias = statement.from.alias ?? statement.from.name;
|
|
65
|
+
const [right] = statement.joins;
|
|
66
|
+
let join: ArrowTableQuery['join'];
|
|
67
|
+
let rightAlias: string | undefined;
|
|
68
|
+
let rightTable: Table | undefined;
|
|
69
|
+
if (right) {
|
|
70
|
+
if (statement.joins.length > 1 || right.type !== 'inner')
|
|
71
|
+
throw new Error('Arrow SQL supports one INNER JOIN per SELECT');
|
|
72
|
+
rightAlias = right.table.alias ?? right.table.name;
|
|
73
|
+
rightTable = getTable(tables, right.table.name);
|
|
74
|
+
if (leftAlias === rightAlias) throw new Error('Duplicate Arrow SQL table alias');
|
|
75
|
+
const {on} = right;
|
|
76
|
+
if (on.type !== 'BinaryExpression' || on.operator !== '==')
|
|
77
|
+
throw new Error('Arrow SQL JOIN requires equality between two columns');
|
|
78
|
+
const sides = [on.left, on.right].map(node => qualifiedColumn(node as Expression));
|
|
79
|
+
const left = sides.find(column => column?.qualifier === leftAlias);
|
|
80
|
+
const rightColumn = sides.find(column => column?.qualifier === rightAlias);
|
|
81
|
+
if (!left || !rightColumn) throw new Error('Qualify both Arrow SQL JOIN keys');
|
|
82
|
+
join = {
|
|
83
|
+
table: rightTable,
|
|
84
|
+
left: left.name,
|
|
85
|
+
right: rightColumn.name,
|
|
86
|
+
rightPrefix: rightAlias + '.'
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const bind = (node: Expression): Expression =>
|
|
91
|
+
bindColumns(node, column => {
|
|
92
|
+
if (column.qualifier === leftAlias) return column.name;
|
|
93
|
+
if (column.qualifier && column.qualifier === rightAlias)
|
|
94
|
+
return column.qualifier + '.' + column.name;
|
|
95
|
+
if (column.qualifier) {
|
|
96
|
+
if (
|
|
97
|
+
[source, rightTable].some(table =>
|
|
98
|
+
table?.schema.fields.some(field => field.name === column.qualifier)
|
|
99
|
+
)
|
|
100
|
+
)
|
|
101
|
+
return undefined;
|
|
102
|
+
throw new Error(`Unknown Arrow SQL table alias: ${column.qualifier}`);
|
|
103
|
+
}
|
|
104
|
+
const onRight = rightTable?.schema.fields.some(field => field.name === column.name);
|
|
105
|
+
if (!onRight) return column.name;
|
|
106
|
+
if (source.schema.fields.some(field => field.name === column.name))
|
|
107
|
+
throw new Error(`Ambiguous Arrow SQL column: ${column.name}`);
|
|
108
|
+
return rightAlias + '.' + column.name;
|
|
109
|
+
});
|
|
110
|
+
const groupBy = statement.groupBy?.map(expression => requireColumn(bind(expression)));
|
|
111
|
+
const aggregates = new Map<string, ArrowAggregate>();
|
|
112
|
+
const select = statement.select?.map(({expression, alias}, index) => {
|
|
113
|
+
const aggregate = planAggregate(expression, bind);
|
|
114
|
+
if (aggregate) {
|
|
115
|
+
const name = alias ?? `aggregate_${index + 1}`;
|
|
116
|
+
if (aggregates.has(name) || groupBy?.includes(name))
|
|
117
|
+
throw new Error(`Duplicate Arrow SQL aggregate name: ${name}`);
|
|
118
|
+
aggregates.set(name, aggregate);
|
|
119
|
+
return name;
|
|
120
|
+
}
|
|
121
|
+
const bound = bind(expression);
|
|
122
|
+
if (bound.type === 'Identifier') return {column: bound.name, as: alias};
|
|
123
|
+
return {expression: bound, as: alias};
|
|
124
|
+
});
|
|
125
|
+
return queryArrowTable(source, {
|
|
126
|
+
...execution,
|
|
127
|
+
join,
|
|
128
|
+
select,
|
|
129
|
+
where: statement.where && {expression: bind(statement.where)},
|
|
130
|
+
groupBy,
|
|
131
|
+
aggregates: Object.fromEntries(aggregates)
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function getTable(tables: ReadonlyMap<string, Table>, name: string): Table {
|
|
136
|
+
const table = tables.get(name);
|
|
137
|
+
if (!table) throw new Error(`Unknown Arrow SQL table: ${name}`);
|
|
138
|
+
return table;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
type Column = {name: string; qualifier?: string};
|
|
142
|
+
|
|
143
|
+
function qualifiedColumn(node: Expression): Column | undefined {
|
|
144
|
+
if (node.type === 'Identifier') return {name: node.name};
|
|
145
|
+
if (
|
|
146
|
+
node.type === 'MemberExpression' &&
|
|
147
|
+
!node.computed &&
|
|
148
|
+
node.object.type === 'Identifier' &&
|
|
149
|
+
node.property.type === 'Identifier'
|
|
150
|
+
)
|
|
151
|
+
return {name: String(node.property['name']), qualifier: String(node.object['name'])};
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function requireColumn(node: Expression): string {
|
|
156
|
+
if (node.type !== 'Identifier')
|
|
157
|
+
throw new Error('Arrow SQL requires a column name in this clause');
|
|
158
|
+
return node.name;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Resolves SQL column references while leaving function names and literals intact. */
|
|
162
|
+
function bindColumns(
|
|
163
|
+
node: Expression,
|
|
164
|
+
resolve: (column: Column) => string | undefined
|
|
165
|
+
): Expression {
|
|
166
|
+
const column = qualifiedColumn(node);
|
|
167
|
+
const name = column && resolve(column);
|
|
168
|
+
if (name !== undefined) return {type: 'Identifier', name};
|
|
169
|
+
const bind = (child: unknown) => bindColumns(child as Expression, resolve);
|
|
170
|
+
switch (node.type) {
|
|
171
|
+
case 'BinaryExpression':
|
|
172
|
+
case 'LogicalExpression':
|
|
173
|
+
return {...node, left: bind(node.left), right: bind(node.right)};
|
|
174
|
+
case 'UnaryExpression':
|
|
175
|
+
return {...node, argument: bind(node.argument)};
|
|
176
|
+
case 'CallExpression':
|
|
177
|
+
return {...node, arguments: node.arguments.map(bind)};
|
|
178
|
+
case 'MemberExpression': {
|
|
179
|
+
if (node.computed || node.property.type !== 'Identifier')
|
|
180
|
+
throw new Error('Arrow SQL struct access requires a field identifier');
|
|
181
|
+
const name = String(node.property['name']);
|
|
182
|
+
return {
|
|
183
|
+
type: 'CallExpression',
|
|
184
|
+
callee: {type: 'Identifier', name: 'STRUCT_EXTRACT'},
|
|
185
|
+
arguments: [bind(node.object), {type: 'Literal', value: name, raw: JSON.stringify(name)}]
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
case 'ConditionalExpression':
|
|
189
|
+
return {
|
|
190
|
+
...node,
|
|
191
|
+
test: bind(node.test),
|
|
192
|
+
consequent: bind(node.consequent),
|
|
193
|
+
alternate: bind(node.alternate)
|
|
194
|
+
};
|
|
195
|
+
default:
|
|
196
|
+
return node;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const aggregateOperations = new Set<ArrowAggregate['operation']>([
|
|
201
|
+
'COUNT',
|
|
202
|
+
'SUM',
|
|
203
|
+
'MIN',
|
|
204
|
+
'MAX',
|
|
205
|
+
'AVG',
|
|
206
|
+
'EVERY',
|
|
207
|
+
'BOOL_AND',
|
|
208
|
+
'BOOL_OR',
|
|
209
|
+
'BIT_AND',
|
|
210
|
+
'BIT_OR',
|
|
211
|
+
'BIT_XOR'
|
|
212
|
+
]);
|
|
213
|
+
|
|
214
|
+
function planAggregate(
|
|
215
|
+
node: Expression,
|
|
216
|
+
bind: (expression: Expression) => Expression
|
|
217
|
+
): ArrowAggregate | undefined {
|
|
218
|
+
if (node.type !== 'CallExpression' || node.callee.type !== 'Identifier' || !node.sqlFunction)
|
|
219
|
+
return undefined;
|
|
220
|
+
const operation = String(node.callee['name']).toUpperCase() as ArrowAggregate['operation'];
|
|
221
|
+
if (!aggregateOperations.has(operation)) return undefined;
|
|
222
|
+
if (node.arguments.length !== 1) throw new Error(`Arrow SQL ${operation} requires one argument`);
|
|
223
|
+
const argument = node.arguments[0] as Expression & {sqlWildcard?: boolean};
|
|
224
|
+
return operation === 'COUNT' && argument.sqlWildcard
|
|
225
|
+
? {operation}
|
|
226
|
+
: {operation, expression: bind(argument)};
|
|
227
|
+
}
|