@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,298 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
Data,
|
|
7
|
+
DataType,
|
|
8
|
+
makeData,
|
|
9
|
+
type RecordBatch,
|
|
10
|
+
Schema,
|
|
11
|
+
Struct,
|
|
12
|
+
Table,
|
|
13
|
+
type Field
|
|
14
|
+
} from 'apache-arrow';
|
|
15
|
+
import {
|
|
16
|
+
getRecordBatchMetadata,
|
|
17
|
+
createRecordBatchWithMetadata,
|
|
18
|
+
validateArrowTableSchema,
|
|
19
|
+
takeArrowTableRows
|
|
20
|
+
} from '@math.gl/arrow';
|
|
21
|
+
import type {
|
|
22
|
+
ColumnarPredicateParameterValues,
|
|
23
|
+
ParameterizedColumnarPredicate
|
|
24
|
+
} from '../expressions/index';
|
|
25
|
+
import type {TableQuery} from '../sql/query-types';
|
|
26
|
+
import {compileArrowArray, compileArrowExpression} from './compiler';
|
|
27
|
+
import {compileArrowPredicate} from './predicate';
|
|
28
|
+
import type {ArrowAggregate} from './aggregates';
|
|
29
|
+
import {
|
|
30
|
+
groupArrowTable,
|
|
31
|
+
joinArrowTables,
|
|
32
|
+
orderArrowTable,
|
|
33
|
+
unionArrowTables,
|
|
34
|
+
type ArrowQueryJoin,
|
|
35
|
+
type ArrowQueryOrder
|
|
36
|
+
} from './relational-query';
|
|
37
|
+
import type {ArrowExpressionOptions, CompiledArrowExpression} from './types';
|
|
38
|
+
|
|
39
|
+
/** A linear Arrow query that can execute incrementally over batches. */
|
|
40
|
+
export type ArrowBatchQuery = TableQuery<ParameterizedColumnarPredicate> &
|
|
41
|
+
Omit<ArrowExpressionOptions, 'inputSchema'> & {
|
|
42
|
+
/** Named values for a parameterized predicate. */
|
|
43
|
+
parameters?: ColumnarPredicateParameterValues;
|
|
44
|
+
/** Checked between batches and during expression evaluation. */
|
|
45
|
+
signal?: AbortSignal;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** Options specific to streaming Arrow queries. */
|
|
49
|
+
export type ArrowStreamingQuery = ArrowBatchQuery & {
|
|
50
|
+
/** Emit a schema-bearing empty batch when an input batch produces no rows. */
|
|
51
|
+
emitEmptyBatches?: boolean;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** An in-memory SELECT with optional relational operators. */
|
|
55
|
+
export type ArrowTableQuery = ArrowBatchQuery & {
|
|
56
|
+
/** Append schema-compatible tables before filtering. */
|
|
57
|
+
union?: readonly Table[];
|
|
58
|
+
/** Join a second table before filtering. */
|
|
59
|
+
join?: ArrowQueryJoin;
|
|
60
|
+
/** Scalar source columns that define groups. */
|
|
61
|
+
groupBy?: readonly string[];
|
|
62
|
+
/** Aggregate reductions indexed by output name. */
|
|
63
|
+
aggregates?: Readonly<Record<string, ArrowAggregate>>;
|
|
64
|
+
/** Stable ordering applied after grouping and before pagination. */
|
|
65
|
+
orderBy?: readonly ArrowQueryOrder[];
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** A bound output field and either a source column index or a computed expression. */
|
|
69
|
+
type Projection = {field: Field} & ({index: number} | {expression: CompiledArrowExpression});
|
|
70
|
+
|
|
71
|
+
/** Applies WHERE, projection, and pagination directly to a native Apache Arrow table. */
|
|
72
|
+
export function queryArrowTable(source: Table, query: ArrowTableQuery = {}): Table {
|
|
73
|
+
const {select, where, offset = 0, limit, functions, parameters, signal} = query;
|
|
74
|
+
signal?.throwIfAborted();
|
|
75
|
+
validatePagination(offset, limit);
|
|
76
|
+
|
|
77
|
+
const {union, join, groupBy, aggregates, orderBy} = query;
|
|
78
|
+
if (union?.length) source = unionArrowTables(source, union);
|
|
79
|
+
if (join) source = joinArrowTables(source, join, signal);
|
|
80
|
+
|
|
81
|
+
if (groupBy?.length || Object.keys(aggregates ?? {}).length || orderBy?.length) {
|
|
82
|
+
source = queryArrowTable(source, {where, functions, parameters, signal});
|
|
83
|
+
if (groupBy?.length || Object.keys(aggregates ?? {}).length) {
|
|
84
|
+
source = groupArrowTable(source, groupBy ?? [], aggregates ?? {}, {
|
|
85
|
+
inputSchema: source.schema,
|
|
86
|
+
functions,
|
|
87
|
+
signal
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
if (orderBy?.length) {
|
|
91
|
+
source = orderArrowTable(source, orderBy, offset, limit, signal);
|
|
92
|
+
return queryArrowTable(source, {select, functions, signal});
|
|
93
|
+
}
|
|
94
|
+
return queryArrowTable(source, {select, offset, limit, functions, signal});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const plan = compileArrowQuery(source.schema, query);
|
|
98
|
+
const result: RecordBatch[] = [];
|
|
99
|
+
for (const batch of source.batches) {
|
|
100
|
+
if (plan.done()) break;
|
|
101
|
+
for (const output of plan.execute(batch)) result.push(output);
|
|
102
|
+
}
|
|
103
|
+
return new Table(plan.schema, result);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Streams a linear Arrow query with global pagination and source backpressure. */
|
|
107
|
+
export async function* queryArrowTableInBatches(
|
|
108
|
+
input: AsyncIterable<RecordBatch> | Iterable<RecordBatch>,
|
|
109
|
+
inputSchema: Schema,
|
|
110
|
+
query: ArrowStreamingQuery = {}
|
|
111
|
+
): AsyncIterable<RecordBatch> {
|
|
112
|
+
query.signal?.throwIfAborted();
|
|
113
|
+
validatePagination(query.offset ?? 0, query.limit);
|
|
114
|
+
const plan = compileArrowQuery(inputSchema, query);
|
|
115
|
+
if (plan.done()) return;
|
|
116
|
+
for await (const batch of input) {
|
|
117
|
+
query.signal?.throwIfAborted();
|
|
118
|
+
if (batch.schema !== inputSchema) {
|
|
119
|
+
validateArrowTableSchema(new Table(batch.schema, []), inputSchema, {
|
|
120
|
+
rejectExtraFields: true,
|
|
121
|
+
schemaName: 'query input schema'
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
yield* plan.execute(batch, query.emitEmptyBatches);
|
|
125
|
+
if (plan.done()) return;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Validates pagination before the query reads or transforms any rows. */
|
|
130
|
+
function validatePagination(offset: number, limit?: number): void {
|
|
131
|
+
if (!Number.isSafeInteger(offset) || offset < 0) throw new Error('Invalid Arrow query offset');
|
|
132
|
+
if (limit !== undefined && (!Number.isSafeInteger(limit) || limit < 0))
|
|
133
|
+
throw new Error('Invalid Arrow query limit');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Binds predicates and projection to a schema once per execution. */
|
|
137
|
+
function compileArrowQuery(inputSchema: Schema, query: ArrowBatchQuery) {
|
|
138
|
+
const {select, where, offset = 0, limit, functions, parameters, signal} = query;
|
|
139
|
+
const options = {inputSchema, functions};
|
|
140
|
+
const predicate = where
|
|
141
|
+
? 'expression' in where
|
|
142
|
+
? compileArrowExpression(where.expression, options)
|
|
143
|
+
: compileArrowPredicate(where, {...options, parameters})
|
|
144
|
+
: undefined;
|
|
145
|
+
if (predicate && !DataType.isBool(predicate.outputField.type))
|
|
146
|
+
throw new Error('Arrow query WHERE requires a Boolean expression');
|
|
147
|
+
|
|
148
|
+
const names = new Set<string>();
|
|
149
|
+
for (const selection of select ?? []) {
|
|
150
|
+
const name =
|
|
151
|
+
typeof selection === 'string'
|
|
152
|
+
? selection
|
|
153
|
+
: (selection.as ?? ('column' in selection ? selection.column : undefined));
|
|
154
|
+
if (name === undefined) continue;
|
|
155
|
+
if (names.has(name) || (typeof selection !== 'string' && selection.as === ''))
|
|
156
|
+
throw new Error(`Invalid or duplicate Arrow query output name: ${name}`);
|
|
157
|
+
names.add(name);
|
|
158
|
+
}
|
|
159
|
+
const projection: Projection[] = select
|
|
160
|
+
? select.map((selection, ordinal) => {
|
|
161
|
+
if (typeof selection !== 'string' && ('array' in selection || 'expression' in selection)) {
|
|
162
|
+
const expression =
|
|
163
|
+
'array' in selection
|
|
164
|
+
? compileArrowArray(selection.array, {...options, type: selection.type})
|
|
165
|
+
: compileArrowExpression(selection.expression, options);
|
|
166
|
+
let name = selection.as;
|
|
167
|
+
if (name === undefined) {
|
|
168
|
+
name = `expression_${ordinal + 1}`;
|
|
169
|
+
while (names.has(name)) name += '_';
|
|
170
|
+
names.add(name);
|
|
171
|
+
}
|
|
172
|
+
return {expression, field: expression.outputField.clone({name})};
|
|
173
|
+
}
|
|
174
|
+
const column = typeof selection === 'string' ? selection : selection.column;
|
|
175
|
+
const index = inputSchema.fields.findIndex(field => field.name === column);
|
|
176
|
+
if (index < 0) throw new Error(`Arrow query column not found: ${column}`);
|
|
177
|
+
const field = inputSchema.fields[index]!;
|
|
178
|
+
return {
|
|
179
|
+
index,
|
|
180
|
+
field: typeof selection === 'string' ? field : field.clone({name: selection.as ?? column})
|
|
181
|
+
};
|
|
182
|
+
})
|
|
183
|
+
: inputSchema.fields.map((field, index) => ({field, index}));
|
|
184
|
+
|
|
185
|
+
const schema = select
|
|
186
|
+
? new Schema(
|
|
187
|
+
projection.map(({field}) => field),
|
|
188
|
+
inputSchema.metadata,
|
|
189
|
+
undefined,
|
|
190
|
+
inputSchema.metadataVersion
|
|
191
|
+
)
|
|
192
|
+
: inputSchema;
|
|
193
|
+
let skip = offset;
|
|
194
|
+
let remaining = limit ?? Infinity;
|
|
195
|
+
/** Evaluates one batch using the remaining pagination budget. */
|
|
196
|
+
function* execute(batch: RecordBatch, emitEmpty = false): Iterable<RecordBatch> {
|
|
197
|
+
signal?.throwIfAborted();
|
|
198
|
+
const metadata = getRecordBatchMetadata(batch);
|
|
199
|
+
const mask = predicate?.evaluate(batch, {signal});
|
|
200
|
+
let selected: RecordBatch | undefined;
|
|
201
|
+
if (mask) {
|
|
202
|
+
const indexes: number[] = [];
|
|
203
|
+
for (let row = 0; row < batch.numRows && indexes.length < remaining; row++) {
|
|
204
|
+
if ((row & 1023) === 0) signal?.throwIfAborted();
|
|
205
|
+
if (mask.get(row) !== true) continue;
|
|
206
|
+
if (skip > 0) skip--;
|
|
207
|
+
else indexes.push(row);
|
|
208
|
+
}
|
|
209
|
+
if (indexes.length === batch.numRows) selected = batch;
|
|
210
|
+
else if (
|
|
211
|
+
indexes.length &&
|
|
212
|
+
indexes[indexes.length - 1]! - indexes[0]! + 1 === indexes.length
|
|
213
|
+
) {
|
|
214
|
+
selected = createRecordBatchWithMetadata(
|
|
215
|
+
batch.schema,
|
|
216
|
+
batch.data.slice(indexes[0]!, indexes.length),
|
|
217
|
+
metadata
|
|
218
|
+
);
|
|
219
|
+
} else if (indexes.length) {
|
|
220
|
+
const gathered = takeArrowTableRows(
|
|
221
|
+
new Table(inputSchema, [batch]),
|
|
222
|
+
Int32Array.from(indexes)
|
|
223
|
+
);
|
|
224
|
+
selected = makeQueryBatch(
|
|
225
|
+
inputSchema,
|
|
226
|
+
gathered.batches[0]!.data.children,
|
|
227
|
+
indexes.length,
|
|
228
|
+
metadata
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
} else {
|
|
232
|
+
const start = Math.min(skip, batch.numRows);
|
|
233
|
+
skip -= start;
|
|
234
|
+
const end = start + Math.min(batch.numRows - start, remaining);
|
|
235
|
+
if (end > start)
|
|
236
|
+
selected =
|
|
237
|
+
start === 0 && end === batch.numRows
|
|
238
|
+
? batch
|
|
239
|
+
: createRecordBatchWithMetadata(
|
|
240
|
+
batch.schema,
|
|
241
|
+
batch.data.slice(start, end - start),
|
|
242
|
+
metadata
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
if (!selected?.numRows) {
|
|
246
|
+
if (emitEmpty) {
|
|
247
|
+
const children = schema.fields.map(field => makeData({type: field.type, nullCount: 0}));
|
|
248
|
+
yield makeQueryBatch(schema, children, 0, metadata);
|
|
249
|
+
}
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
remaining -= selected.numRows;
|
|
253
|
+
if (!select) {
|
|
254
|
+
yield selected;
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const columns = projection.map(item =>
|
|
259
|
+
'index' in item
|
|
260
|
+
? selected.data.children[item.index]!
|
|
261
|
+
: item.expression.evaluate(selected, {signal})
|
|
262
|
+
);
|
|
263
|
+
// Computed vectors can introduce chunk boundaries inside the selected source batch.
|
|
264
|
+
const boundaries = new Set([selected.numRows]);
|
|
265
|
+
for (const column of columns) {
|
|
266
|
+
if ('children' in column) continue;
|
|
267
|
+
let end = 0;
|
|
268
|
+
for (const chunk of column.data) {
|
|
269
|
+
end += chunk.length;
|
|
270
|
+
if (end > 0) boundaries.add(end);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
let start = 0;
|
|
274
|
+
for (const end of [...boundaries].sort((left, right) => left - right)) {
|
|
275
|
+
const children = columns.map(column =>
|
|
276
|
+
'children' in column
|
|
277
|
+
? start === 0 && end === column.length
|
|
278
|
+
? column
|
|
279
|
+
: column.slice(start, end - start)
|
|
280
|
+
: column.slice(start, end).data[0]!
|
|
281
|
+
);
|
|
282
|
+
yield makeQueryBatch(schema, children, end - start, metadata);
|
|
283
|
+
start = end;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return {schema, done: () => remaining === 0, execute};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Creates a batch while preserving its row count even for an empty projection. */
|
|
290
|
+
function makeQueryBatch(
|
|
291
|
+
schema: Schema,
|
|
292
|
+
children: Data[],
|
|
293
|
+
length: number,
|
|
294
|
+
metadata: Map<string, string> | undefined
|
|
295
|
+
): RecordBatch {
|
|
296
|
+
const data = new Data(new Struct(schema.fields), 0, length, 0, undefined, children);
|
|
297
|
+
return createRecordBatchWithMetadata(schema, data, metadata);
|
|
298
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {initializeTaskWorker} from '@math.gl/tasks';
|
|
6
|
+
import {createArrowQueryTask} from './query-worker';
|
|
7
|
+
|
|
8
|
+
void initializeTaskWorker(createArrowQueryTask());
|
|
@@ -0,0 +1,190 @@
|
|
|
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 {dehydrateArrowTable, hydrateArrowTable, type DehydratedArrowTable} from '@math.gl/arrow';
|
|
7
|
+
import {TaskRunner, type Task, type TaskWorker} from '@math.gl/tasks';
|
|
8
|
+
import {queryArrowTable, type ArrowTableQuery} from './query-arrow-table';
|
|
9
|
+
import type {ArrowTableFunction} from './types';
|
|
10
|
+
|
|
11
|
+
/** Query options accepted by a worker session. Register functions inside the worker. */
|
|
12
|
+
export type ArrowWorkerQuery = Omit<ArrowTableQuery, 'functions'>;
|
|
13
|
+
|
|
14
|
+
type QueryPayload = Omit<ArrowWorkerQuery, 'signal' | 'union' | 'join'> & {
|
|
15
|
+
union?: DehydratedArrowTable[];
|
|
16
|
+
join?: Omit<NonNullable<ArrowTableQuery['join']>, 'table'> & {table: DehydratedArrowTable};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type QueryResult = {table: DehydratedArrowTable} | {error: {name: string; message: string}};
|
|
20
|
+
|
|
21
|
+
type QueryTask = {
|
|
22
|
+
initialization: {table: DehydratedArrowTable};
|
|
23
|
+
input: {operation: 'query'; query: QueryPayload} | {operation: 'get' | 'take'};
|
|
24
|
+
output: QueryResult;
|
|
25
|
+
state: Table;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** Configuration for a retained Arrow query worker. */
|
|
29
|
+
export type ArrowQueryWorkerOptions = {
|
|
30
|
+
/** Module worker URL or browser worker factory. */
|
|
31
|
+
worker: Pick<TaskWorker, 'url' | 'create'>;
|
|
32
|
+
/** Defaults to copy. Transfer relinquishes every source buffer and its aliases. */
|
|
33
|
+
inputOwnership?: 'copy' | 'transfer';
|
|
34
|
+
/** Cancels worker initialization. */
|
|
35
|
+
signal?: AbortSignal;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** Runs queries against a source table retained in a dedicated worker. */
|
|
39
|
+
export type ArrowQueryWorkerSession = {
|
|
40
|
+
/** Executes a query. Its union and join inputs are copied for this call. */
|
|
41
|
+
query(query?: ArrowWorkerQuery): Promise<Table>;
|
|
42
|
+
/** Returns a snapshot and keeps the worker available. */
|
|
43
|
+
getTable(): Promise<Table>;
|
|
44
|
+
/** Transfers the source back and closes the session. */
|
|
45
|
+
takeTable(): Promise<Table>;
|
|
46
|
+
/** Drains accepted operations and releases the worker. */
|
|
47
|
+
close(): Promise<void>;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** Defines the shared task protocol. Custom function declarations stay in the worker. */
|
|
51
|
+
export function createArrowQueryTask(
|
|
52
|
+
options: {functions?: readonly ArrowTableFunction[]; worker?: TaskWorker} = {}
|
|
53
|
+
): Task<QueryTask> {
|
|
54
|
+
return {
|
|
55
|
+
id: 'math.gl-arrow-query',
|
|
56
|
+
worker: options.worker,
|
|
57
|
+
initialize: ({table}) => hydrateArrowTable(table),
|
|
58
|
+
process(input, _options, {state, signal}) {
|
|
59
|
+
if (input.operation === 'take') return {table: dehydrateArrowTable(state, {copy: 'none'})};
|
|
60
|
+
let result = state;
|
|
61
|
+
try {
|
|
62
|
+
if (input.operation === 'query') {
|
|
63
|
+
const {union, join, ...query} = input.query;
|
|
64
|
+
result = queryArrowTable(state, {
|
|
65
|
+
...query,
|
|
66
|
+
union: union?.map(hydrateArrowTable),
|
|
67
|
+
join: join && {...join, table: hydrateArrowTable(join.table)},
|
|
68
|
+
functions: options.functions,
|
|
69
|
+
signal
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return {table: dehydrateArrowTable(result, {retain: [state]})};
|
|
73
|
+
} catch (error) {
|
|
74
|
+
// A query error is a response, so the task runner keeps the pinned worker alive.
|
|
75
|
+
return {
|
|
76
|
+
error: {
|
|
77
|
+
name: error instanceof Error ? error.name : 'Error',
|
|
78
|
+
message: error instanceof Error ? error.message : String(error)
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Sends one table to a worker and retains it across queries. */
|
|
87
|
+
export async function createArrowQueryWorker(
|
|
88
|
+
source: Table,
|
|
89
|
+
{worker, inputOwnership, signal}: ArrowQueryWorkerOptions
|
|
90
|
+
): Promise<ArrowQueryWorkerSession> {
|
|
91
|
+
signal?.throwIfAborted();
|
|
92
|
+
const runner = new TaskRunner();
|
|
93
|
+
try {
|
|
94
|
+
const task = await runner.initializeTask(
|
|
95
|
+
createArrowQueryTask({worker: {...worker, capabilities: {atomic: true, streaming: false}}}),
|
|
96
|
+
{table: dehydrateArrowTable(source, {copy: 'none'})},
|
|
97
|
+
{execution: 'worker', inputOwnership, signal}
|
|
98
|
+
);
|
|
99
|
+
let closing = false;
|
|
100
|
+
function assertOpen(): void {
|
|
101
|
+
if (closing) throw new Error('Arrow query worker session is closed');
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
async query({signal: querySignal, union, join, ...query} = {}) {
|
|
105
|
+
assertOpen();
|
|
106
|
+
querySignal?.throwIfAborted();
|
|
107
|
+
const result = await runQueryWithSignal(
|
|
108
|
+
() =>
|
|
109
|
+
task.runTask({
|
|
110
|
+
operation: 'query',
|
|
111
|
+
query: {
|
|
112
|
+
...query,
|
|
113
|
+
union: union?.map(table => dehydrateArrowTable(table, {copy: 'none'})),
|
|
114
|
+
join: join && {...join, table: dehydrateArrowTable(join.table, {copy: 'none'})}
|
|
115
|
+
}
|
|
116
|
+
}),
|
|
117
|
+
querySignal
|
|
118
|
+
);
|
|
119
|
+
return hydrateQueryResult(result);
|
|
120
|
+
},
|
|
121
|
+
async getTable() {
|
|
122
|
+
assertOpen();
|
|
123
|
+
return hydrateQueryResult(await task.runTask({operation: 'get'}));
|
|
124
|
+
},
|
|
125
|
+
async takeTable() {
|
|
126
|
+
assertOpen();
|
|
127
|
+
closing = true;
|
|
128
|
+
try {
|
|
129
|
+
return hydrateQueryResult(await task.runTask({operation: 'take'}));
|
|
130
|
+
} finally {
|
|
131
|
+
await runner.close();
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
close() {
|
|
135
|
+
closing = true;
|
|
136
|
+
return runner.close();
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
} catch (error) {
|
|
140
|
+
await runner.close();
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Executes one query on a worker and releases the worker after it settles. */
|
|
146
|
+
export async function queryArrowTableInWorker(
|
|
147
|
+
source: Table,
|
|
148
|
+
query: ArrowWorkerQuery,
|
|
149
|
+
options: ArrowQueryWorkerOptions
|
|
150
|
+
): Promise<Table> {
|
|
151
|
+
const signal = query.signal ?? options.signal;
|
|
152
|
+
const session = await createArrowQueryWorker(source, {...options, signal});
|
|
153
|
+
try {
|
|
154
|
+
return await session.query({...query, signal});
|
|
155
|
+
} finally {
|
|
156
|
+
// Cancellation stops the caller's wait. Drain the worker independently.
|
|
157
|
+
if (signal?.aborted) void session.close().catch(() => {});
|
|
158
|
+
else await session.close();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Restores a result or propagates a query error without invalidating the session. */
|
|
163
|
+
function hydrateQueryResult(result: QueryResult): Table {
|
|
164
|
+
if ('table' in result) return hydrateArrowTable(result.table);
|
|
165
|
+
const error = new Error(result.error.message);
|
|
166
|
+
error.name = result.error.name;
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Cancels the caller's wait while leaving accepted work queued on the retained worker. */
|
|
171
|
+
async function runQueryWithSignal<T>(run: () => Promise<T>, signal?: AbortSignal): Promise<T> {
|
|
172
|
+
if (!signal) return run();
|
|
173
|
+
signal.throwIfAborted();
|
|
174
|
+
const pending = run();
|
|
175
|
+
return new Promise<T>((resolve, reject) => {
|
|
176
|
+
const abort = () => reject(signal.reason);
|
|
177
|
+
signal.addEventListener('abort', abort, {once: true});
|
|
178
|
+
if (signal.aborted) abort();
|
|
179
|
+
void pending.then(
|
|
180
|
+
result => {
|
|
181
|
+
signal.removeEventListener('abort', abort);
|
|
182
|
+
resolve(result);
|
|
183
|
+
},
|
|
184
|
+
error => {
|
|
185
|
+
signal.removeEventListener('abort', abort);
|
|
186
|
+
reject(error);
|
|
187
|
+
}
|
|
188
|
+
);
|
|
189
|
+
});
|
|
190
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {physicalKey} from './signature';
|
|
6
|
+
import type {ArrowTableFunction} from './types';
|
|
7
|
+
|
|
8
|
+
/** Builds one private, immutable declaration lookup per compilation. */
|
|
9
|
+
export function createFunctionLookup(
|
|
10
|
+
declarations: readonly ArrowTableFunction[] = []
|
|
11
|
+
): ReadonlyMap<string, ArrowTableFunction> {
|
|
12
|
+
const functions = new Map<string, ArrowTableFunction>();
|
|
13
|
+
for (const declaration of declarations) {
|
|
14
|
+
const name = declaration?.name;
|
|
15
|
+
if (typeof name !== 'string' || !/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name))
|
|
16
|
+
throw new Error('Invalid Arrow function name');
|
|
17
|
+
if (
|
|
18
|
+
[
|
|
19
|
+
'TRY',
|
|
20
|
+
'COALESCE',
|
|
21
|
+
'array',
|
|
22
|
+
'array_float16',
|
|
23
|
+
'array_float32',
|
|
24
|
+
'array_float64',
|
|
25
|
+
'array_uint8',
|
|
26
|
+
'PACK_UNORM8',
|
|
27
|
+
'LIST_EXTRACT',
|
|
28
|
+
'STRUCT_EXTRACT',
|
|
29
|
+
'cast',
|
|
30
|
+
'in',
|
|
31
|
+
'isNull',
|
|
32
|
+
'isNotNull',
|
|
33
|
+
'parseNumber',
|
|
34
|
+
'parseBigInt',
|
|
35
|
+
'parseBoolean',
|
|
36
|
+
'toFloat64',
|
|
37
|
+
'toInt32',
|
|
38
|
+
'toInt64'
|
|
39
|
+
].includes(name)
|
|
40
|
+
)
|
|
41
|
+
throw new Error(`Reserved Arrow function: ${name}`);
|
|
42
|
+
if (!Array.isArray(declaration.prototypes) || !declaration.prototypes.length)
|
|
43
|
+
throw new Error('Arrow functions require one or more prototypes');
|
|
44
|
+
const prototypes = [...(functions.get(name)?.prototypes ?? [])];
|
|
45
|
+
for (const prototype of declaration.prototypes) {
|
|
46
|
+
if (
|
|
47
|
+
!prototype ||
|
|
48
|
+
typeof prototype !== 'object' ||
|
|
49
|
+
!Array.isArray(prototype.arguments) ||
|
|
50
|
+
!prototype.result ||
|
|
51
|
+
typeof prototype.evaluate !== 'function'
|
|
52
|
+
)
|
|
53
|
+
throw new Error('Arrow prototypes require argument fields, a result field and evaluate');
|
|
54
|
+
if (
|
|
55
|
+
prototype.nullHandling !== undefined &&
|
|
56
|
+
!['propagate', 'call'].includes(prototype.nullHandling)
|
|
57
|
+
)
|
|
58
|
+
throw new Error('Unsupported null handling');
|
|
59
|
+
if (prototype.evaluateScalar !== undefined && typeof prototype.evaluateScalar !== 'function')
|
|
60
|
+
throw new Error('Arrow function evaluateScalar must be a function');
|
|
61
|
+
const key = JSON.stringify(
|
|
62
|
+
prototype.arguments.map(field => [
|
|
63
|
+
physicalKey(field.type),
|
|
64
|
+
[...field.metadata].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
65
|
+
])
|
|
66
|
+
);
|
|
67
|
+
if (
|
|
68
|
+
prototypes.some(
|
|
69
|
+
existing =>
|
|
70
|
+
JSON.stringify(
|
|
71
|
+
existing.arguments.map(field => [
|
|
72
|
+
physicalKey(field.type),
|
|
73
|
+
[...field.metadata].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
74
|
+
])
|
|
75
|
+
) === key
|
|
76
|
+
)
|
|
77
|
+
)
|
|
78
|
+
throw new Error(`Duplicate Arrow function signature: ${name}`);
|
|
79
|
+
prototypes.push(
|
|
80
|
+
Object.freeze({...prototype, arguments: Object.freeze([...prototype.arguments])})
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
functions.set(name, Object.freeze({name, prototypes: Object.freeze(prototypes)}));
|
|
84
|
+
}
|
|
85
|
+
return functions;
|
|
86
|
+
}
|