@math.gl/sql 0.0.0-stage → 5.0.0-beta.12
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 +1538 -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 +3321 -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 +201 -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 +223 -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 +66 -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/unnest-arrow-column.d.ts +22 -0
- package/dist/arrow/unnest-arrow-column.d.ts.map +1 -0
- package/dist/arrow/unnest-arrow-column.js +112 -0
- package/dist/arrow/unnest-arrow-column.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 +4074 -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-functions/index.cjs +382 -0
- package/dist/color-functions/index.cjs.map +6 -0
- package/dist/color-functions/index.d.ts +26 -0
- package/dist/color-functions/index.d.ts.map +1 -0
- package/dist/color-functions/index.js +306 -0
- package/dist/color-functions/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 +114 -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 +105 -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 +5294 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/math-functions/index.cjs +444 -0
- package/dist/math-functions/index.cjs.map +6 -0
- package/dist/math-functions/index.d.ts +44 -0
- package/dist/math-functions/index.d.ts.map +1 -0
- package/dist/math-functions/index.js +182 -0
- package/dist/math-functions/index.js.map +1 -0
- package/dist/parser/bind-sql-predicate.d.ts +4 -0
- package/dist/parser/bind-sql-predicate.d.ts.map +1 -0
- package/dist/parser/bind-sql-predicate.js +11 -0
- package/dist/parser/bind-sql-predicate.js.map +1 -0
- package/dist/parser/index.cjs +1807 -0
- package/dist/parser/index.cjs.map +6 -0
- package/dist/parser/index.d.ts +12 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +11 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/parameter-snapshots.d.ts +21 -0
- package/dist/parser/parameter-snapshots.d.ts.map +1 -0
- package/dist/parser/parameter-snapshots.js +85 -0
- package/dist/parser/parameter-snapshots.js.map +1 -0
- package/dist/parser/parse-sql-expression.d.ts +50 -0
- package/dist/parser/parse-sql-expression.d.ts.map +1 -0
- package/dist/parser/parse-sql-expression.js +501 -0
- package/dist/parser/parse-sql-expression.js.map +1 -0
- package/dist/parser/parse-sql-predicate.d.ts +13 -0
- package/dist/parser/parse-sql-predicate.d.ts.map +1 -0
- package/dist/parser/parse-sql-predicate.js +365 -0
- package/dist/parser/parse-sql-predicate.js.map +1 -0
- package/dist/parser/parse-sql-query.d.ts +9 -0
- package/dist/parser/parse-sql-query.d.ts.map +1 -0
- package/dist/parser/parse-sql-query.js +81 -0
- package/dist/parser/parse-sql-query.js.map +1 -0
- package/dist/parser/parse-sql-select.d.ts +57 -0
- package/dist/parser/parse-sql-select.d.ts.map +1 -0
- package/dist/parser/parse-sql-select.js +247 -0
- package/dist/parser/parse-sql-select.js.map +1 -0
- package/dist/parser/parse-sql-statement.d.ts +54 -0
- package/dist/parser/parse-sql-statement.d.ts.map +1 -0
- package/dist/parser/parse-sql-statement.js +207 -0
- package/dist/parser/parse-sql-statement.js.map +1 -0
- package/dist/parser/query-types.d.ts +56 -0
- package/dist/parser/query-types.d.ts.map +1 -0
- package/dist/parser/query-types.js +5 -0
- package/dist/parser/query-types.js.map +1 -0
- package/dist/parser/sql-parameters.d.ts +29 -0
- package/dist/parser/sql-parameters.d.ts.map +1 -0
- package/dist/parser/sql-parameters.js +110 -0
- package/dist/parser/sql-parameters.js.map +1 -0
- package/dist/parser/sql-parse-error.d.ts +13 -0
- package/dist/parser/sql-parse-error.d.ts.map +1 -0
- package/dist/parser/sql-parse-error.js +20 -0
- package/dist/parser/sql-parse-error.js.map +1 -0
- package/dist/parser/sql-predicate-schema.d.ts +164 -0
- package/dist/parser/sql-predicate-schema.d.ts.map +1 -0
- package/dist/parser/sql-predicate-schema.js +249 -0
- package/dist/parser/sql-predicate-schema.js.map +1 -0
- package/dist/parser/sql-predicate-types.d.ts +48 -0
- package/dist/parser/sql-predicate-types.d.ts.map +1 -0
- package/dist/parser/sql-predicate-types.js +9 -0
- package/dist/parser/sql-predicate-types.js.map +1 -0
- package/dist/parser/sql-predicate.d.ts +6 -0
- package/dist/parser/sql-predicate.d.ts.map +1 -0
- package/dist/parser/sql-predicate.js +8 -0
- package/dist/parser/sql-predicate.js.map +1 -0
- package/dist/scales/arrow-domains.d.ts +22 -0
- package/dist/scales/arrow-domains.d.ts.map +1 -0
- package/dist/scales/arrow-domains.js +166 -0
- package/dist/scales/arrow-domains.js.map +1 -0
- package/dist/scales/arrow-scales.d.ts +44 -0
- package/dist/scales/arrow-scales.d.ts.map +1 -0
- package/dist/scales/arrow-scales.js +255 -0
- package/dist/scales/arrow-scales.js.map +1 -0
- package/dist/scales/compile-scale.d.ts +2 -0
- package/dist/scales/compile-scale.d.ts.map +1 -0
- package/dist/scales/compile-scale.js +5 -0
- package/dist/scales/compile-scale.js.map +1 -0
- package/dist/scales/conformance.cjs +180 -0
- package/dist/scales/conformance.cjs.map +6 -0
- package/dist/scales/conformance.d.ts +26 -0
- package/dist/scales/conformance.d.ts.map +1 -0
- package/dist/scales/conformance.js +164 -0
- package/dist/scales/conformance.js.map +1 -0
- package/dist/scales/hdr-colors.d.ts +2 -0
- package/dist/scales/hdr-colors.d.ts.map +1 -0
- package/dist/scales/hdr-colors.js +5 -0
- package/dist/scales/hdr-colors.js.map +1 -0
- package/dist/scales/index.cjs +4334 -0
- package/dist/scales/index.cjs.map +6 -0
- package/dist/scales/index.d.ts +10 -0
- package/dist/scales/index.d.ts.map +1 -0
- package/dist/scales/index.js +11 -0
- package/dist/scales/index.js.map +1 -0
- package/dist/scales/palettes.d.ts +2 -0
- package/dist/scales/palettes.d.ts.map +1 -0
- package/dist/scales/palettes.js +5 -0
- package/dist/scales/palettes.js.map +1 -0
- package/dist/scales/parse-sql-scale.d.ts +27 -0
- package/dist/scales/parse-sql-scale.d.ts.map +1 -0
- package/dist/scales/parse-sql-scale.js +257 -0
- package/dist/scales/parse-sql-scale.js.map +1 -0
- package/dist/scales/quantile-sketch.d.ts +2 -0
- package/dist/scales/quantile-sketch.d.ts.map +1 -0
- package/dist/scales/quantile-sketch.js +5 -0
- package/dist/scales/quantile-sketch.js.map +1 -0
- package/dist/scales/scale-types.d.ts +2 -0
- package/dist/scales/scale-types.d.ts.map +1 -0
- package/dist/scales/scale-types.js +5 -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 +115 -0
- package/dist/scales/streaming-domains.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 +1778 -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 +221 -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 +255 -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 +194 -0
- package/src/arrow/registry.ts +87 -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/unnest-arrow-column.ts +145 -0
- package/src/arrow/worker-entry.ts +10 -0
- package/src/arrow/worker.ts +143 -0
- package/src/color-functions/index.ts +405 -0
- package/src/diagnostic-log.ts +20 -0
- package/src/expressions/ast.ts +143 -0
- package/src/expressions/columnar-predicate.ts +293 -0
- package/src/expressions/evaluation-error.ts +29 -0
- package/src/expressions/expression-eval.ts +489 -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/runtime.ts +949 -0
- package/src/expressions/vendor/jsep/types.ts +191 -0
- package/src/index.ts +54 -0
- package/src/math-functions/index.ts +235 -0
- package/src/parser/bind-sql-predicate.ts +21 -0
- package/src/parser/index.ts +43 -0
- package/src/parser/parameter-snapshots.ts +103 -0
- package/src/parser/parse-sql-expression.ts +500 -0
- package/src/parser/parse-sql-predicate.ts +454 -0
- package/src/parser/parse-sql-query.ts +90 -0
- package/src/parser/parse-sql-select.ts +272 -0
- package/src/parser/parse-sql-statement.ts +260 -0
- package/src/parser/query-types.ts +71 -0
- package/src/parser/sql-parameters.ts +174 -0
- package/src/parser/sql-parse-error.ts +33 -0
- package/src/parser/sql-predicate-schema.ts +279 -0
- package/src/parser/sql-predicate-types.ts +91 -0
- package/src/parser/sql-predicate.ts +28 -0
- package/src/scales/arrow-domains.ts +190 -0
- package/src/scales/arrow-scales.ts +363 -0
- package/src/scales/compile-scale.ts +5 -0
- package/src/scales/conformance.ts +186 -0
- package/src/scales/hdr-colors.ts +5 -0
- package/src/scales/index.ts +37 -0
- package/src/scales/palettes.ts +5 -0
- package/src/scales/parse-sql-scale.ts +300 -0
- package/src/scales/quantile-sketch.ts +5 -0
- package/src/scales/scale-types.ts +5 -0
- package/src/scales/streaming-domains.ts +139 -0
|
@@ -0,0 +1,4334 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name2 in all)
|
|
7
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// dist/scales/index.js
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
ScaleQuantileSketch: () => quantile_sketch_exports.ScaleQuantileSketch,
|
|
24
|
+
bindArrowScale: () => bindArrowScale,
|
|
25
|
+
compileArrowScale: () => compileArrowScale,
|
|
26
|
+
compileArrowScaleExpression: () => compileArrowScaleExpression,
|
|
27
|
+
compileArrowScales: () => compileArrowScales,
|
|
28
|
+
compileResolvedScale: () => compile_scale_exports.compileResolvedScale,
|
|
29
|
+
convertScaleColor: () => hdr_colors_exports.convertScaleColor,
|
|
30
|
+
createResolvedScale: () => scale_types_exports.createResolvedScale,
|
|
31
|
+
createStreamingDomainAccumulator: () => createStreamingDomainAccumulator,
|
|
32
|
+
decodeSRGB: () => hdr_colors_exports.decodeSRGB,
|
|
33
|
+
encodeSRGB: () => hdr_colors_exports.encodeSRGB,
|
|
34
|
+
listScalePalettes: () => palettes_exports.listScalePalettes,
|
|
35
|
+
packFloat16: () => hdr_colors_exports.packFloat16,
|
|
36
|
+
packRGBA16F: () => hdr_colors_exports.packRGBA16F,
|
|
37
|
+
parseSQLScaleAST: () => parseSQLScaleAST,
|
|
38
|
+
parseSQLScaleExpression: () => parseSQLScaleExpression,
|
|
39
|
+
resolvePalette: () => palettes_exports.resolvePalette,
|
|
40
|
+
unpackFloat16: () => hdr_colors_exports.unpackFloat16,
|
|
41
|
+
validateScaleSpec: () => scale_types_exports.validateScaleSpec
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(index_exports);
|
|
44
|
+
|
|
45
|
+
// dist/scales/scale-types.js
|
|
46
|
+
var scale_types_exports = {};
|
|
47
|
+
__reExport(scale_types_exports, require("@math.gl/core/color"));
|
|
48
|
+
|
|
49
|
+
// dist/parser/parameter-snapshots.js
|
|
50
|
+
var snapshots = /* @__PURE__ */ new WeakMap();
|
|
51
|
+
function isSQLParameterSnapshot(value) {
|
|
52
|
+
return Boolean(value && typeof value === "object" && snapshots.has(value));
|
|
53
|
+
}
|
|
54
|
+
function snapshotParameter(snapshot, name2) {
|
|
55
|
+
const expression = storage(snapshot).get(name2);
|
|
56
|
+
if (!expression)
|
|
57
|
+
throw new Error(`SQL parameter ":${name2}" requires a value`);
|
|
58
|
+
return structuredClone(expression);
|
|
59
|
+
}
|
|
60
|
+
function storage(snapshot) {
|
|
61
|
+
const values = snapshots.get(snapshot);
|
|
62
|
+
if (!values)
|
|
63
|
+
throw new Error("Invalid SQL parameter snapshot");
|
|
64
|
+
return values;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// dist/expressions/scalar-type.js
|
|
68
|
+
var COLUMNAR_SCALAR_TYPES = Object.freeze([
|
|
69
|
+
"int8",
|
|
70
|
+
"int16",
|
|
71
|
+
"int32",
|
|
72
|
+
"int64",
|
|
73
|
+
"uint8",
|
|
74
|
+
"uint16",
|
|
75
|
+
"uint32",
|
|
76
|
+
"uint64",
|
|
77
|
+
"float16",
|
|
78
|
+
"float32",
|
|
79
|
+
"float64",
|
|
80
|
+
"bool",
|
|
81
|
+
"utf8"
|
|
82
|
+
]);
|
|
83
|
+
function isColumnarScalarType(value) {
|
|
84
|
+
return typeof value === "string" && COLUMNAR_SCALAR_TYPES.includes(value);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// dist/expressions/columnar-predicate.js
|
|
88
|
+
function isColumnarPredicateValue(value) {
|
|
89
|
+
return typeof value === "boolean" || typeof value === "bigint" || typeof value === "string" || typeof value === "number" && Number.isFinite(value) || value instanceof Date && Number.isFinite(value.getTime()) || value instanceof Uint8Array;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// dist/parser/sql-parameters.js
|
|
93
|
+
function validateSQLParameterOptions(options) {
|
|
94
|
+
if (options.parameters !== void 0 && options.parseParameter !== void 0) {
|
|
95
|
+
throw new Error("SQL parameters and parseParameter are mutually exclusive");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function bindSQLParameter(name2, options) {
|
|
99
|
+
if (options.parseParameter)
|
|
100
|
+
return options.parseParameter(name2);
|
|
101
|
+
if (isSQLParameterSnapshot(options.parameters))
|
|
102
|
+
return snapshotParameter(options.parameters, name2);
|
|
103
|
+
if (!Object.prototype.hasOwnProperty.call(options.parameters ?? {}, name2)) {
|
|
104
|
+
throw new Error(`SQL parameter ":${name2}" requires a value`);
|
|
105
|
+
}
|
|
106
|
+
const value = options.parameters[name2];
|
|
107
|
+
if (isNumericArray(value) && !(value instanceof Uint8Array))
|
|
108
|
+
return typedArrayLiteral(value, name2);
|
|
109
|
+
if (value && typeof value === "object" && "type" in value) {
|
|
110
|
+
if (value.type === "array" && isNumericArray(value.value))
|
|
111
|
+
return typedArrayLiteral(value.value, name2);
|
|
112
|
+
if ((value.type === "utf8" || value.type === "binary") && value.value instanceof Uint8Array) {
|
|
113
|
+
const data = new Uint8Array(value.value);
|
|
114
|
+
if (value.type === "binary")
|
|
115
|
+
return literal(data);
|
|
116
|
+
try {
|
|
117
|
+
return {
|
|
118
|
+
type: "Literal",
|
|
119
|
+
value: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(data),
|
|
120
|
+
utf8: data
|
|
121
|
+
};
|
|
122
|
+
} catch {
|
|
123
|
+
throw new Error(`SQL parameter ":${name2}" requires valid UTF-8 bytes`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
throw new Error(`SQL parameter ":${name2}" has an unsupported binary format`);
|
|
127
|
+
}
|
|
128
|
+
if (Array.isArray(value)) {
|
|
129
|
+
if (value.length > 4096 || !(Array.from(value).every((item) => typeof item === "number" && Number.isFinite(item)) || Array.from(value).every((item) => typeof item === "string") || Array.from(value).every((item) => (Array.isArray(item) || isNumericArray(item)) && item.length <= 4096 && Array.from(item).every((component) => typeof component === "number" && Number.isFinite(component)))))
|
|
130
|
+
throw new Error(`SQL parameter ":${name2}" has an unsupported array value`);
|
|
131
|
+
return arrayLiteral(value, name2);
|
|
132
|
+
}
|
|
133
|
+
if (value !== null && !isColumnarPredicateValue(value)) {
|
|
134
|
+
throw new Error(`SQL parameter ":${name2}" has an unsupported value`);
|
|
135
|
+
}
|
|
136
|
+
return literal(value instanceof Date ? new Date(value.getTime()) : value instanceof Uint8Array ? new Uint8Array(value) : value);
|
|
137
|
+
}
|
|
138
|
+
function arrayLiteral(values, name2) {
|
|
139
|
+
return {
|
|
140
|
+
type: "ArrayExpression",
|
|
141
|
+
elements: Array.from(values, (value) => Array.isArray(value) ? arrayLiteral(value, name2) : isNumericArray(value) ? typedArrayLiteral(value, name2) : literal(value))
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function literal(value) {
|
|
145
|
+
if (value instanceof Uint8Array || value instanceof Date)
|
|
146
|
+
return { type: "Literal", value };
|
|
147
|
+
return {
|
|
148
|
+
type: "Literal",
|
|
149
|
+
value,
|
|
150
|
+
raw: typeof value === "bigint" ? String(value) : JSON.stringify(value)
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function isNumericArray(value) {
|
|
154
|
+
return value instanceof Float32Array || value instanceof Float64Array || value instanceof Int8Array || value instanceof Uint8Array || value instanceof Uint8ClampedArray || value instanceof Int16Array || value instanceof Uint16Array || value instanceof Int32Array || value instanceof Uint32Array;
|
|
155
|
+
}
|
|
156
|
+
function typedArrayLiteral(value, name2) {
|
|
157
|
+
if (value.length > 4096 || !value.every(Number.isFinite)) {
|
|
158
|
+
throw new Error(`SQL parameter ":${name2}" has an unsupported numeric array`);
|
|
159
|
+
}
|
|
160
|
+
const typedValues = value instanceof Uint8Array ? new Uint8Array(value) : value.slice();
|
|
161
|
+
return {
|
|
162
|
+
type: "ArrayExpression",
|
|
163
|
+
elements: Array.from(typedValues, (item) => literal(item)),
|
|
164
|
+
typedValues
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// dist/parser/sql-parse-error.js
|
|
169
|
+
var SQLParseError = class extends SyntaxError {
|
|
170
|
+
constructor(reason, source, offset) {
|
|
171
|
+
var _a2;
|
|
172
|
+
const lines = source.slice(0, offset).split(/\r\n|\r|\n/);
|
|
173
|
+
const line = lines.length;
|
|
174
|
+
const column = lines[line - 1].length + 1;
|
|
175
|
+
const token = ((_a2 = source.slice(offset, offset + 32).match(/^:?\w+|^\S/u)) == null ? void 0 : _a2[0]) ?? null;
|
|
176
|
+
super(`${reason} (line ${line}, column ${column}${token ? `, near ${JSON.stringify(token)}` : ""})`);
|
|
177
|
+
this.reason = reason;
|
|
178
|
+
this.name = "SQLParseError";
|
|
179
|
+
this.offset = offset;
|
|
180
|
+
this.line = line;
|
|
181
|
+
this.column = column;
|
|
182
|
+
this.token = token;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// dist/parser/parse-sql-expression.js
|
|
187
|
+
var SQL_RESERVED_WORDS = /* @__PURE__ */ new Set([
|
|
188
|
+
"SELECT",
|
|
189
|
+
"ARRAY",
|
|
190
|
+
"FROM",
|
|
191
|
+
"WHERE",
|
|
192
|
+
"LIMIT",
|
|
193
|
+
"OFFSET",
|
|
194
|
+
"AS",
|
|
195
|
+
"TRUE",
|
|
196
|
+
"FALSE",
|
|
197
|
+
"NULL",
|
|
198
|
+
"ALL",
|
|
199
|
+
"AND",
|
|
200
|
+
"OR",
|
|
201
|
+
"NOT",
|
|
202
|
+
"IN",
|
|
203
|
+
"IS",
|
|
204
|
+
"DISTINCT",
|
|
205
|
+
"ORDER",
|
|
206
|
+
"GROUP",
|
|
207
|
+
"HAVING",
|
|
208
|
+
"JOIN",
|
|
209
|
+
"UNION"
|
|
210
|
+
]);
|
|
211
|
+
function sqlCastType(name2, source = name2, offset = 0) {
|
|
212
|
+
const normalized = name2.toLowerCase().replace(/\s+/g, " ").trim();
|
|
213
|
+
if (/^(?:u?int(?:8|16|32|64)|float(?:16|32|64))$/.test(normalized)) {
|
|
214
|
+
return normalized;
|
|
215
|
+
}
|
|
216
|
+
switch (normalized) {
|
|
217
|
+
case "smallint":
|
|
218
|
+
return "int16";
|
|
219
|
+
case "int":
|
|
220
|
+
case "integer":
|
|
221
|
+
return "int32";
|
|
222
|
+
case "bigint":
|
|
223
|
+
return "int64";
|
|
224
|
+
case "real":
|
|
225
|
+
return "float32";
|
|
226
|
+
case "double precision":
|
|
227
|
+
return "float64";
|
|
228
|
+
case "boolean":
|
|
229
|
+
return "bool";
|
|
230
|
+
case "utf8":
|
|
231
|
+
case "varchar":
|
|
232
|
+
case "character varying":
|
|
233
|
+
return "utf8";
|
|
234
|
+
default:
|
|
235
|
+
throw new SQLParseError(`Unsupported SQL CAST type: ${name2}`, source, offset);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function parseSQLExpression(source, options = {}) {
|
|
239
|
+
const reader = new SQLExpressionReader(source, 0, options);
|
|
240
|
+
const expression = reader.expression();
|
|
241
|
+
reader.space();
|
|
242
|
+
if (reader.position !== source.length)
|
|
243
|
+
throw reader.error("Unexpected token after SQL expression");
|
|
244
|
+
return expression;
|
|
245
|
+
}
|
|
246
|
+
var SQLExpressionReader = class {
|
|
247
|
+
constructor(source, start = 0, parameterOptions = {}) {
|
|
248
|
+
this.source = source;
|
|
249
|
+
this.parameterOptions = parameterOptions;
|
|
250
|
+
this.tokens = 0;
|
|
251
|
+
this.depths = /* @__PURE__ */ new WeakMap();
|
|
252
|
+
validateSQLParameterOptions(parameterOptions);
|
|
253
|
+
this.position = start;
|
|
254
|
+
if (start === 0 && (typeof source !== "string" || !source.trim() || source.length > 65536))
|
|
255
|
+
throw new SQLParseError("SQL expression requires non-empty input of at most 65536 characters", "", 0);
|
|
256
|
+
}
|
|
257
|
+
/** Creates an error at the current expression cursor. */
|
|
258
|
+
error(message, offset = this.position) {
|
|
259
|
+
return new SQLParseError(message, this.source, offset);
|
|
260
|
+
}
|
|
261
|
+
space() {
|
|
262
|
+
while (this.position < this.source.length && /\s/.test(this.source[this.position]))
|
|
263
|
+
this.position++;
|
|
264
|
+
if (this.source.startsWith("--", this.position) || this.source.startsWith("/*", this.position))
|
|
265
|
+
throw this.error("SQL comments are not supported.");
|
|
266
|
+
}
|
|
267
|
+
word(word) {
|
|
268
|
+
this.space();
|
|
269
|
+
if (this.source.slice(this.position, this.position + word.length).toUpperCase() !== word || /[A-Za-z0-9_]/.test(this.source[this.position + word.length] || ""))
|
|
270
|
+
return false;
|
|
271
|
+
this.position += word.length;
|
|
272
|
+
this.token();
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
take(symbol) {
|
|
276
|
+
this.space();
|
|
277
|
+
if (!this.source.startsWith(symbol, this.position))
|
|
278
|
+
return false;
|
|
279
|
+
this.position += symbol.length;
|
|
280
|
+
this.token();
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
symbol(symbol) {
|
|
284
|
+
if (!this.take(symbol))
|
|
285
|
+
throw this.error(`SQL expression expected ${symbol}`);
|
|
286
|
+
}
|
|
287
|
+
node(value, children = []) {
|
|
288
|
+
const depth = children.length ? 1 + Math.max(...children.map((child) => this.depths.get(child) || 0)) : 0;
|
|
289
|
+
if (depth > 64)
|
|
290
|
+
throw this.error("SQL expression exceeds nesting limit");
|
|
291
|
+
this.depths.set(value, depth);
|
|
292
|
+
return value;
|
|
293
|
+
}
|
|
294
|
+
expression(depth = 0, minimum = 0) {
|
|
295
|
+
if (depth > 64)
|
|
296
|
+
throw this.error("SQL expression exceeds nesting limit");
|
|
297
|
+
let left;
|
|
298
|
+
if (this.word("NOT")) {
|
|
299
|
+
const argument = this.expression(depth + 1, 3);
|
|
300
|
+
left = this.node({ type: "UnaryExpression", operator: "!", argument }, [argument]);
|
|
301
|
+
} else if (/^\s*[+-](?:\d|\.\d)/.test(this.source.slice(this.position))) {
|
|
302
|
+
left = this.primary(depth);
|
|
303
|
+
} else if (this.take("+") || this.take("-")) {
|
|
304
|
+
const operator = this.source[this.position - 1];
|
|
305
|
+
const argument = this.expression(depth + 1, 6);
|
|
306
|
+
left = this.node({ type: "UnaryExpression", operator, argument }, [argument]);
|
|
307
|
+
} else
|
|
308
|
+
left = this.primary(depth);
|
|
309
|
+
while (true) {
|
|
310
|
+
const start = this.position;
|
|
311
|
+
if (this.take("[")) {
|
|
312
|
+
const index = this.expression(depth + 1);
|
|
313
|
+
this.symbol("]");
|
|
314
|
+
left = this.call("LIST_EXTRACT", [left, index], true);
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
if (this.take(".")) {
|
|
318
|
+
left = this.memberAccess(left, this.readIdentifier(true));
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (this.take("::")) {
|
|
322
|
+
if (minimum > 7) {
|
|
323
|
+
this.position = start;
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
left = this.cast(left);
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
if (minimum <= 3 && this.word("IS")) {
|
|
330
|
+
const negate = this.word("NOT");
|
|
331
|
+
if (!this.word("NULL"))
|
|
332
|
+
throw this.error("SQL IS requires NULL or NOT NULL");
|
|
333
|
+
left = this.call(negate ? "isNotNull" : "isNull", [left]);
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
if (minimum <= 3) {
|
|
337
|
+
const negate = this.word("NOT");
|
|
338
|
+
if (this.word("IN")) {
|
|
339
|
+
this.symbol("(");
|
|
340
|
+
const values = [];
|
|
341
|
+
do {
|
|
342
|
+
const item = this.expression(depth + 1);
|
|
343
|
+
if (item.type === "ArrayExpression") {
|
|
344
|
+
for (const element of item.elements) {
|
|
345
|
+
if (element.type !== "Literal")
|
|
346
|
+
throw this.error("SQL IN requires scalar values");
|
|
347
|
+
values.push(element);
|
|
348
|
+
}
|
|
349
|
+
} else
|
|
350
|
+
values.push(item);
|
|
351
|
+
if (values.length > 4096)
|
|
352
|
+
throw this.error("SQL IN exceeds 4096 values");
|
|
353
|
+
} while (this.take(","));
|
|
354
|
+
this.symbol(")");
|
|
355
|
+
left = values.length ? this.call("in", [left, ...values]) : this.node({ type: "Literal", value: false, raw: "false" });
|
|
356
|
+
if (negate)
|
|
357
|
+
left = this.node({ type: "UnaryExpression", operator: "!", argument: left }, [left]);
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
const insensitive = this.word("ILIKE");
|
|
361
|
+
if (insensitive || this.word("LIKE")) {
|
|
362
|
+
const pattern = this.expression(depth + 1, 4);
|
|
363
|
+
const args = [left, pattern];
|
|
364
|
+
if (this.word("ESCAPE"))
|
|
365
|
+
args.push(this.expression(depth + 1, 4));
|
|
366
|
+
left = this.call(insensitive ? "ilike" : "like", args);
|
|
367
|
+
if (negate)
|
|
368
|
+
left = this.node({ type: "UnaryExpression", operator: "!", argument: left }, [left]);
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
this.position = start;
|
|
372
|
+
}
|
|
373
|
+
this.space();
|
|
374
|
+
const match = /^(OR\b|AND\b|\|\||<>|!=|<=|>=|=|<|>|\+|-|\*|\/)/i.exec(this.source.slice(this.position));
|
|
375
|
+
if (!match)
|
|
376
|
+
break;
|
|
377
|
+
const token = match[0].toUpperCase();
|
|
378
|
+
const precedence = token === "OR" ? 1 : token === "AND" ? 2 : ["=", "<>", "!=", "<", ">", "<=", ">="].includes(token) ? 3 : token === "+" || token === "-" || token === "||" ? 4 : 5;
|
|
379
|
+
if (precedence < minimum)
|
|
380
|
+
break;
|
|
381
|
+
this.position += match[0].length;
|
|
382
|
+
this.token();
|
|
383
|
+
const right = this.expression(depth + 1, precedence + 1);
|
|
384
|
+
if (token === "||") {
|
|
385
|
+
left = this.call("concatStrict", [left, right]);
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
const operator = token === "OR" ? "||" : token === "AND" ? "&&" : token === "=" ? "==" : token === "<>" ? "!=" : token;
|
|
389
|
+
left = this.node({ type: "BinaryExpression", operator, left, right }, [left, right]);
|
|
390
|
+
}
|
|
391
|
+
return left;
|
|
392
|
+
}
|
|
393
|
+
call(name2, args, sqlFunction = false) {
|
|
394
|
+
return this.node({
|
|
395
|
+
type: "CallExpression",
|
|
396
|
+
callee: { type: "Identifier", name: name2 },
|
|
397
|
+
arguments: args,
|
|
398
|
+
...sqlFunction ? { sqlFunction: true } : {}
|
|
399
|
+
}, args);
|
|
400
|
+
}
|
|
401
|
+
cast(argument) {
|
|
402
|
+
this.space();
|
|
403
|
+
const start = this.position;
|
|
404
|
+
const match = /^[A-Za-z][A-Za-z0-9]*(?:\s+(?:PRECISION|VARYING)\b)?/i.exec(this.source.slice(this.position));
|
|
405
|
+
if (!match)
|
|
406
|
+
throw this.error("SQL cast requires a target type");
|
|
407
|
+
this.position += match[0].length;
|
|
408
|
+
this.token();
|
|
409
|
+
if (this.take("[")) {
|
|
410
|
+
this.space();
|
|
411
|
+
const width = /^\d+/.exec(this.source.slice(this.position));
|
|
412
|
+
if (!width)
|
|
413
|
+
throw this.error("SQL ARRAY cast requires a fixed length");
|
|
414
|
+
this.position += width[0].length;
|
|
415
|
+
this.token();
|
|
416
|
+
this.symbol("]");
|
|
417
|
+
const array = argument.type === "ArrayExpression" ? { type: "CallExpression", callee: { name: "array" }, arguments: argument.elements } : argument;
|
|
418
|
+
if (array.type !== "CallExpression" || !/^array(?:_(?:float(?:16|32|64)|uint8))?$/.test(array.callee.name) || Number(width[0]) !== array.arguments.length)
|
|
419
|
+
throw this.error("ARRAY cast length must match the number of components");
|
|
420
|
+
const type = sqlCastType(match[0], this.source, start);
|
|
421
|
+
if (!["float16", "float32", "float64", "uint8"].includes(type))
|
|
422
|
+
throw this.error("Unsupported ARRAY cast type");
|
|
423
|
+
if (array.callee.name !== "array" && (type === "uint8" || array.callee.name === "array_uint8"))
|
|
424
|
+
throw this.error("Chained UINT8 array casts are not supported");
|
|
425
|
+
return this.call(`array_${type}`, array.arguments);
|
|
426
|
+
}
|
|
427
|
+
const name2 = match[0].toLowerCase();
|
|
428
|
+
let target;
|
|
429
|
+
if (name2 === "timestamp")
|
|
430
|
+
target = this.timestampCastType();
|
|
431
|
+
else if (name2 === "date" || name2 === "geometry")
|
|
432
|
+
target = name2;
|
|
433
|
+
else if (name2 === "dictionary")
|
|
434
|
+
target = this.dictionaryCastType();
|
|
435
|
+
else if (/^(?:binary|varbinary|blob|fixedsizebinary)$/.test(name2))
|
|
436
|
+
target = this.binaryCastType(name2);
|
|
437
|
+
else
|
|
438
|
+
target = sqlCastType(name2, this.source, start);
|
|
439
|
+
return this.call("cast", [
|
|
440
|
+
argument,
|
|
441
|
+
this.node({ type: "Literal", value: target, raw: JSON.stringify(target) })
|
|
442
|
+
]);
|
|
443
|
+
}
|
|
444
|
+
/** Parses the optional SQL time zone qualifier. */
|
|
445
|
+
timestampCastType() {
|
|
446
|
+
const zoned = this.word("WITH");
|
|
447
|
+
if (!zoned && !this.word("WITHOUT"))
|
|
448
|
+
return "timestamp";
|
|
449
|
+
if (!this.word("TIME") || !this.word("ZONE"))
|
|
450
|
+
throw this.error("Expected TIME ZONE");
|
|
451
|
+
return zoned ? "timestamp_utc" : "timestamp";
|
|
452
|
+
}
|
|
453
|
+
/** Parses the supported Arrow dictionary layout. */
|
|
454
|
+
dictionaryCastType() {
|
|
455
|
+
this.symbol("(");
|
|
456
|
+
if (!this.word("UTF8"))
|
|
457
|
+
throw this.error("Expected UTF8 dictionary values");
|
|
458
|
+
this.symbol(",");
|
|
459
|
+
if (!this.word("INT32"))
|
|
460
|
+
throw this.error("Expected INT32 dictionary indices");
|
|
461
|
+
this.symbol(")");
|
|
462
|
+
return "dictionary_utf8_int32";
|
|
463
|
+
}
|
|
464
|
+
/** Parses binary aliases and optional positive byte widths. */
|
|
465
|
+
binaryCastType(name2) {
|
|
466
|
+
var _a2;
|
|
467
|
+
if (name2 === "blob")
|
|
468
|
+
return "binary";
|
|
469
|
+
if (!this.take("(")) {
|
|
470
|
+
if (name2 === "fixedsizebinary")
|
|
471
|
+
throw this.error("FIXEDSIZEBINARY requires a byte width");
|
|
472
|
+
return "binary";
|
|
473
|
+
}
|
|
474
|
+
this.space();
|
|
475
|
+
const width = ((_a2 = /^\d+/.exec(this.source.slice(this.position))) == null ? void 0 : _a2[0]) ?? "";
|
|
476
|
+
const byteWidth = Number(width);
|
|
477
|
+
if (byteWidth < 1 || byteWidth > 2147483647)
|
|
478
|
+
throw this.error("Binary CAST requires a positive 32-bit byte width");
|
|
479
|
+
this.position += width.length;
|
|
480
|
+
this.token();
|
|
481
|
+
this.symbol(")");
|
|
482
|
+
return `${name2 === "varbinary" ? "varbinary" : "fixedsizebinary"}:${byteWidth}`;
|
|
483
|
+
}
|
|
484
|
+
primary(depth) {
|
|
485
|
+
if (depth > 64)
|
|
486
|
+
throw this.error("SQL expression exceeds nesting limit");
|
|
487
|
+
if (this.take("(")) {
|
|
488
|
+
const value = this.expression(depth + 1);
|
|
489
|
+
this.symbol(")");
|
|
490
|
+
return value;
|
|
491
|
+
}
|
|
492
|
+
if (this.word("CAST")) {
|
|
493
|
+
this.symbol("(");
|
|
494
|
+
const argument = this.expression(depth + 1);
|
|
495
|
+
if (!this.word("AS"))
|
|
496
|
+
throw this.error("SQL CAST requires AS");
|
|
497
|
+
const result = this.cast(argument);
|
|
498
|
+
this.symbol(")");
|
|
499
|
+
return result;
|
|
500
|
+
}
|
|
501
|
+
if (this.word("ARRAY")) {
|
|
502
|
+
this.symbol("[");
|
|
503
|
+
const args = [];
|
|
504
|
+
do {
|
|
505
|
+
args.push(this.expression(depth + 1));
|
|
506
|
+
} while (this.take(","));
|
|
507
|
+
this.symbol("]");
|
|
508
|
+
return this.call("array", args);
|
|
509
|
+
}
|
|
510
|
+
if (this.word("CASE")) {
|
|
511
|
+
const clauses = [];
|
|
512
|
+
while (this.word("WHEN")) {
|
|
513
|
+
const test = this.expression(depth + 1);
|
|
514
|
+
if (!this.word("THEN"))
|
|
515
|
+
throw this.error("SQL CASE requires THEN");
|
|
516
|
+
clauses.push({ test, consequent: this.expression(depth + 1) });
|
|
517
|
+
}
|
|
518
|
+
if (!clauses.length)
|
|
519
|
+
throw this.error("SQL CASE requires WHEN");
|
|
520
|
+
let alternate = this.word("ELSE") ? this.expression(depth + 1) : this.node({ type: "Literal", value: null, raw: "null" });
|
|
521
|
+
if (!this.word("END"))
|
|
522
|
+
throw this.error("SQL CASE requires END");
|
|
523
|
+
for (let index = clauses.length - 1; index >= 0; index--) {
|
|
524
|
+
const { test, consequent } = clauses[index];
|
|
525
|
+
alternate = this.node({ type: "ConditionalExpression", test, consequent, alternate }, [
|
|
526
|
+
test,
|
|
527
|
+
consequent,
|
|
528
|
+
alternate
|
|
529
|
+
]);
|
|
530
|
+
}
|
|
531
|
+
return alternate;
|
|
532
|
+
}
|
|
533
|
+
this.space();
|
|
534
|
+
const start = this.position;
|
|
535
|
+
const extended = this.extendedPrimary();
|
|
536
|
+
if (extended)
|
|
537
|
+
return extended;
|
|
538
|
+
const quote = this.source[this.position];
|
|
539
|
+
if (quote === '"' || quote === "'") {
|
|
540
|
+
const value = this.quoted(quote);
|
|
541
|
+
return quote === '"' ? this.identifier(value, depth, false) : this.node({ type: "Literal", value, raw: JSON.stringify(value) });
|
|
542
|
+
}
|
|
543
|
+
const number2 = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/.exec(this.source.slice(this.position));
|
|
544
|
+
if (number2) {
|
|
545
|
+
this.position += number2[0].length;
|
|
546
|
+
this.token();
|
|
547
|
+
const value = Number(number2[0]);
|
|
548
|
+
if (!Number.isFinite(value) || /^[+-]?\d+$/.test(number2[0]) && !Number.isSafeInteger(value))
|
|
549
|
+
throw this.error("SQL numeric literal must be finite and exactly representable; use a quoted BIGINT value", start);
|
|
550
|
+
return this.node({ type: "Literal", value, raw: number2[0] });
|
|
551
|
+
}
|
|
552
|
+
for (const [keyword, value] of [
|
|
553
|
+
["NULL", null],
|
|
554
|
+
["TRUE", true],
|
|
555
|
+
["FALSE", false]
|
|
556
|
+
])
|
|
557
|
+
if (this.word(keyword))
|
|
558
|
+
return this.node({ type: "Literal", value, raw: keyword.toLowerCase() });
|
|
559
|
+
return this.identifier(this.readIdentifier(), depth, true);
|
|
560
|
+
}
|
|
561
|
+
/** Reads an identifier in projections, source references, and field access. */
|
|
562
|
+
readIdentifier(member = false) {
|
|
563
|
+
var _a2;
|
|
564
|
+
this.space();
|
|
565
|
+
if (this.source[this.position] === '"')
|
|
566
|
+
return this.quoted('"');
|
|
567
|
+
const identifier = (_a2 = /^[A-Za-z_][A-Za-z0-9_]*/.exec(this.source.slice(this.position))) == null ? void 0 : _a2[0];
|
|
568
|
+
if (!identifier || !member && (SQL_RESERVED_WORDS.has(identifier.toUpperCase()) || /^(WHEN|THEN|ELSE|END)$/i.test(identifier)))
|
|
569
|
+
throw this.error("SQL expression requires a column, literal or function");
|
|
570
|
+
this.position += identifier.length;
|
|
571
|
+
this.token();
|
|
572
|
+
return identifier;
|
|
573
|
+
}
|
|
574
|
+
/** Decodes a quoted token, including doubled quote escapes. */
|
|
575
|
+
quoted(quote) {
|
|
576
|
+
const start = this.position++;
|
|
577
|
+
this.token();
|
|
578
|
+
let value = "";
|
|
579
|
+
while (this.position < this.source.length) {
|
|
580
|
+
const character = this.source[this.position++];
|
|
581
|
+
if (character !== quote)
|
|
582
|
+
value += character;
|
|
583
|
+
else if (this.source[this.position] === quote) {
|
|
584
|
+
value += quote;
|
|
585
|
+
this.position++;
|
|
586
|
+
} else {
|
|
587
|
+
if (quote === '"' && !value)
|
|
588
|
+
throw this.error("SQL identifiers must not be empty", start);
|
|
589
|
+
return value;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
throw this.error("Unterminated SQL expression quote", start);
|
|
593
|
+
}
|
|
594
|
+
identifier(name2, depth, fold2) {
|
|
595
|
+
if (!this.take("("))
|
|
596
|
+
return this.node({ type: "Identifier", name: name2 });
|
|
597
|
+
const args = [];
|
|
598
|
+
const wildcard = fold2 && this.wildcardArgument(name2);
|
|
599
|
+
if (wildcard) {
|
|
600
|
+
this.symbol(")");
|
|
601
|
+
return this.call(name2, [wildcard], true);
|
|
602
|
+
}
|
|
603
|
+
if (!this.take(")")) {
|
|
604
|
+
do {
|
|
605
|
+
args.push(this.expression(depth + 1));
|
|
606
|
+
} while (this.take(","));
|
|
607
|
+
this.symbol(")");
|
|
608
|
+
}
|
|
609
|
+
return this.call(name2, args, fold2);
|
|
610
|
+
}
|
|
611
|
+
/** Reads named value placeholders; quotes and identifiers use separate productions. */
|
|
612
|
+
extendedPrimary() {
|
|
613
|
+
if (this.source[this.position] !== ":")
|
|
614
|
+
return void 0;
|
|
615
|
+
const start = this.position;
|
|
616
|
+
const parameter = /^:([A-Za-z_][A-Za-z0-9_]*)/.exec(this.source.slice(start));
|
|
617
|
+
if (!parameter)
|
|
618
|
+
throw this.error("SQL named parameters require a name");
|
|
619
|
+
this.position += parameter[0].length;
|
|
620
|
+
this.token();
|
|
621
|
+
try {
|
|
622
|
+
return this.node(bindSQLParameter(parameter[1], this.parameterOptions));
|
|
623
|
+
} catch (error) {
|
|
624
|
+
throw this.error(error instanceof Error ? error.message : String(error), start);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
/** Lowers field access to a portable expression call for Arrow binding. */
|
|
628
|
+
memberAccess(object, name2) {
|
|
629
|
+
const key = this.node({ type: "Literal", value: name2, raw: JSON.stringify(name2) });
|
|
630
|
+
return this.call("STRUCT_EXTRACT", [object, key], true);
|
|
631
|
+
}
|
|
632
|
+
wildcardArgument(_name) {
|
|
633
|
+
return void 0;
|
|
634
|
+
}
|
|
635
|
+
token() {
|
|
636
|
+
if (++this.tokens > 4096)
|
|
637
|
+
throw this.error("SQL expression exceeds token limit");
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
// dist/scales/parse-sql-scale.js
|
|
642
|
+
function call(node, expected) {
|
|
643
|
+
var _a2;
|
|
644
|
+
const candidate = node;
|
|
645
|
+
if (candidate.type !== "CallExpression" || ((_a2 = candidate.callee) == null ? void 0 : _a2.type) !== "Identifier") {
|
|
646
|
+
throw new Error(expected ? `${expected} expects a function call` : "Expected a scale function");
|
|
647
|
+
}
|
|
648
|
+
if (expected && candidate.callee.name.toUpperCase() !== expected) {
|
|
649
|
+
throw new Error(`Expected ${expected}`);
|
|
650
|
+
}
|
|
651
|
+
return candidate;
|
|
652
|
+
}
|
|
653
|
+
function name(node) {
|
|
654
|
+
const candidate = node;
|
|
655
|
+
if (candidate.type !== "Identifier" || !candidate.name) {
|
|
656
|
+
throw new Error("Data-derived scale domain requires one column identifier");
|
|
657
|
+
}
|
|
658
|
+
return candidate.name;
|
|
659
|
+
}
|
|
660
|
+
function quantileDomain(value) {
|
|
661
|
+
return value.type === "Identifier" ? { type: "extent", column: name(value) } : { type: "expression-extent", expression: value };
|
|
662
|
+
}
|
|
663
|
+
function literal2(node) {
|
|
664
|
+
const value = node;
|
|
665
|
+
if (value.type !== "Literal" || typeof value.value !== "string" && typeof value.value !== "number" || typeof value.value === "number" && !Number.isFinite(value.value)) {
|
|
666
|
+
throw new Error("Scale argument requires a literal number or quoted string");
|
|
667
|
+
}
|
|
668
|
+
return value.value;
|
|
669
|
+
}
|
|
670
|
+
function number(node) {
|
|
671
|
+
const unary = node;
|
|
672
|
+
if (unary.type === "UnaryExpression" && (unary.operator === "-" || unary.operator === "+") && unary.argument) {
|
|
673
|
+
const n = number(unary.argument);
|
|
674
|
+
return unary.operator === "-" ? -n : n;
|
|
675
|
+
}
|
|
676
|
+
const value = literal2(node);
|
|
677
|
+
if (typeof value !== "number")
|
|
678
|
+
throw new Error("Expected numeric scale argument");
|
|
679
|
+
return value;
|
|
680
|
+
}
|
|
681
|
+
function color(hex) {
|
|
682
|
+
if (!/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(hex)) {
|
|
683
|
+
throw new Error("Explicit SQL color stops require #RRGGBB or #RRGGBBAA");
|
|
684
|
+
}
|
|
685
|
+
const bytes2 = hex.slice(1).match(/../g);
|
|
686
|
+
return [0, 1, 2, 3].map((index) => parseInt(bytes2[index] ?? "ff", 16) / 255);
|
|
687
|
+
}
|
|
688
|
+
function parseDomain(node) {
|
|
689
|
+
const fn = call(node);
|
|
690
|
+
const functionName = fn.callee.name.toUpperCase();
|
|
691
|
+
const args = arrayArguments(fn.arguments);
|
|
692
|
+
switch (functionName) {
|
|
693
|
+
case "DOMAIN":
|
|
694
|
+
if (args.length === 1 && args[0].type === "Identifier")
|
|
695
|
+
return { type: "extent", column: name(args[0]) };
|
|
696
|
+
if (args.length >= 2)
|
|
697
|
+
return { type: "fixed", values: args.map(number) };
|
|
698
|
+
break;
|
|
699
|
+
case "DOMAIN_ZERO":
|
|
700
|
+
if (args.length === 1)
|
|
701
|
+
return { type: "extent", column: name(args[0]), includeZero: true };
|
|
702
|
+
break;
|
|
703
|
+
case "DOMAIN_SYMMETRIC":
|
|
704
|
+
if (args.length === 2)
|
|
705
|
+
return { type: "symmetric", column: name(args[0]), pivot: number(args[1]) };
|
|
706
|
+
break;
|
|
707
|
+
case "DOMAIN_QUANTILE":
|
|
708
|
+
if (args.length === 3) {
|
|
709
|
+
return {
|
|
710
|
+
type: "percentile",
|
|
711
|
+
column: name(args[0]),
|
|
712
|
+
lower: number(args[1]),
|
|
713
|
+
upper: number(args[2])
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
break;
|
|
717
|
+
}
|
|
718
|
+
throw new Error(`Invalid ${functionName} domain arguments`);
|
|
719
|
+
}
|
|
720
|
+
function parseCategories(node) {
|
|
721
|
+
const values = arrayArguments(call(node, "CATEGORIES").arguments).map((argument) => argument.type === "Literal" ? literal2(argument) : number(argument));
|
|
722
|
+
if (!values.length || new Set(values).size !== values.length) {
|
|
723
|
+
throw new Error("CATEGORIES requires at least one category and no duplicates");
|
|
724
|
+
}
|
|
725
|
+
return { type: "fixed", values };
|
|
726
|
+
}
|
|
727
|
+
function arrayArguments(args) {
|
|
728
|
+
return args.length === 1 && args[0].type === "ArrayExpression" ? args[0].elements : args;
|
|
729
|
+
}
|
|
730
|
+
function colorStop(node) {
|
|
731
|
+
if (node.type !== "ArrayExpression") {
|
|
732
|
+
const value = literal2(node);
|
|
733
|
+
if (typeof value !== "string")
|
|
734
|
+
throw new Error("Palette colors require hex strings or RGB/RGBA tuples");
|
|
735
|
+
return color(value);
|
|
736
|
+
}
|
|
737
|
+
if (node.elements.length !== 3 && node.elements.length !== 4) {
|
|
738
|
+
throw new Error("Palette colors require RGB or RGBA tuples");
|
|
739
|
+
}
|
|
740
|
+
const components = node.elements.map(number);
|
|
741
|
+
return [components[0], components[1], components[2], components[3] ?? 1];
|
|
742
|
+
}
|
|
743
|
+
function parseRange(node) {
|
|
744
|
+
var _a2;
|
|
745
|
+
const fn = call(node);
|
|
746
|
+
const args = arrayArguments(fn.arguments);
|
|
747
|
+
if (fn.callee.name.toUpperCase() === "PALETTE" && fn.arguments.length === 1 && ((_a2 = fn.arguments[0]) == null ? void 0 : _a2.type) === "ArrayExpression") {
|
|
748
|
+
return { type: "colors", values: args.map(colorStop) };
|
|
749
|
+
}
|
|
750
|
+
switch (fn.callee.name.toUpperCase()) {
|
|
751
|
+
case "RANGE":
|
|
752
|
+
if (args.length >= 2)
|
|
753
|
+
return { type: "numeric", values: args.map(number) };
|
|
754
|
+
break;
|
|
755
|
+
case "PALETTE": {
|
|
756
|
+
if (args.length === 1) {
|
|
757
|
+
const stop = literal2(args[0]);
|
|
758
|
+
if (typeof stop === "string" && stop.startsWith("#")) {
|
|
759
|
+
return { type: "colors", values: [color(stop)] };
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
if (args.length === 1 || args.length === 2 && typeof literal2(args[0]) === "string" && typeof literal2(args[1]) === "number") {
|
|
763
|
+
const paletteName = literal2(args[0]);
|
|
764
|
+
if (typeof paletteName !== "string")
|
|
765
|
+
throw new Error("Palette name must be a string");
|
|
766
|
+
const count = args.length === 2 ? number(args[1]) : void 0;
|
|
767
|
+
return { type: "palette", name: paletteName, ...count === void 0 ? {} : { count } };
|
|
768
|
+
}
|
|
769
|
+
if (args.length >= 2 && args.every((arg) => typeof literal2(arg) === "string")) {
|
|
770
|
+
return { type: "colors", values: args.map((arg) => color(literal2(arg))) };
|
|
771
|
+
}
|
|
772
|
+
break;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
throw new Error("Expected RANGE(numbers...) or PALETTE(name[, count] | color stops...)");
|
|
776
|
+
}
|
|
777
|
+
function parseSQLScaleExpression(sql, options = {}) {
|
|
778
|
+
return parseSQLScaleAST(parseSQLExpression(sql, options));
|
|
779
|
+
}
|
|
780
|
+
function parseSQLScaleAST(expression) {
|
|
781
|
+
const root = call(expression);
|
|
782
|
+
const fn = root.callee.name.toUpperCase();
|
|
783
|
+
const args = root.arguments;
|
|
784
|
+
const names = {
|
|
785
|
+
SCALE_LINEAR: "linear",
|
|
786
|
+
SCALE_SQRT: "sqrt",
|
|
787
|
+
SCALE_LOG: "log",
|
|
788
|
+
SCALE_COLOR: "sequential",
|
|
789
|
+
SCALE_DIVERGING: "diverging",
|
|
790
|
+
SCALE_ORDINAL: "ordinal",
|
|
791
|
+
SCALE_QUANTILE: "quantile",
|
|
792
|
+
SCALE_QUANTIZE: "quantize",
|
|
793
|
+
SCALE_THRESHOLD: "threshold"
|
|
794
|
+
};
|
|
795
|
+
const type = names[fn];
|
|
796
|
+
if (!type)
|
|
797
|
+
throw new Error(`Unsupported SQL scale function: ${fn}`);
|
|
798
|
+
const inferredOrdinal = fn === "SCALE_ORDINAL" && args.length === 2;
|
|
799
|
+
if (!inferredOrdinal && args.length !== 3) {
|
|
800
|
+
throw new Error(`${fn} expects ${fn === "SCALE_ORDINAL" ? "two or three" : "three"} arguments`);
|
|
801
|
+
}
|
|
802
|
+
const value = args[0];
|
|
803
|
+
const domain = inferredOrdinal ? { type: "categories", column: name(value) } : fn === "SCALE_ORDINAL" ? parseCategories(args[1]) : fn === "SCALE_QUANTILE" ? quantileDomain(value) : fn === "SCALE_QUANTIZE" ? { type: "extent", column: name(value) } : fn === "SCALE_THRESHOLD" ? { type: "fixed", values: call(args[1], "ARRAY").arguments.map(number) } : parseDomain(args[1]);
|
|
804
|
+
let range = parseRange(args[inferredOrdinal ? 1 : 2]);
|
|
805
|
+
if (fn === "SCALE_QUANTILE" || fn === "SCALE_QUANTIZE") {
|
|
806
|
+
const count = number(args[1]);
|
|
807
|
+
if (!Number.isSafeInteger(count) || count < 2) {
|
|
808
|
+
throw new Error(`${fn} class count must be an integer >= 2`);
|
|
809
|
+
}
|
|
810
|
+
if (range.type === "palette") {
|
|
811
|
+
range = { ...range, count };
|
|
812
|
+
} else if (fn !== "SCALE_QUANTILE" || range.type !== "colors") {
|
|
813
|
+
throw new Error(`${fn} requires a named palette`);
|
|
814
|
+
} else if (range.values.length !== count) {
|
|
815
|
+
throw new Error("SCALE_QUANTILE requires one color per class");
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
const colors = [
|
|
819
|
+
"sequential",
|
|
820
|
+
"diverging",
|
|
821
|
+
"ordinal",
|
|
822
|
+
"quantile",
|
|
823
|
+
"quantize",
|
|
824
|
+
"threshold"
|
|
825
|
+
].includes(type);
|
|
826
|
+
if (colors && range.type === "numeric")
|
|
827
|
+
throw new Error(`${fn} requires a color range`);
|
|
828
|
+
if (!colors && range.type !== "numeric")
|
|
829
|
+
throw new Error(`${fn} requires a numeric RANGE`);
|
|
830
|
+
if (type === "threshold" && domain.type === "fixed") {
|
|
831
|
+
const breaks = domain.values;
|
|
832
|
+
if (!breaks.length || breaks.some((value2, index) => index > 0 && value2 <= breaks[index - 1])) {
|
|
833
|
+
throw new Error("SCALE_THRESHOLD breaks must be nonempty and strictly increasing");
|
|
834
|
+
}
|
|
835
|
+
const count = breaks.length + 1;
|
|
836
|
+
if (range.type === "palette" && range.count === void 0)
|
|
837
|
+
range = { ...range, count };
|
|
838
|
+
if (range.type === "colors" && range.values.length !== count || range.type === "palette" && range.count !== count) {
|
|
839
|
+
throw new Error("SCALE_THRESHOLD requires one more color than breaks");
|
|
840
|
+
}
|
|
841
|
+
} else if (domain.type === "fixed" && range.type === "colors" && domain.values.length !== range.values.length) {
|
|
842
|
+
throw new Error("Explicit domain and color stops must have equal lengths");
|
|
843
|
+
}
|
|
844
|
+
const scale = (0, scale_types_exports.validateScaleSpec)({ type, domain, range });
|
|
845
|
+
return { value, scale };
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
// dist/scales/compile-scale.js
|
|
849
|
+
var compile_scale_exports = {};
|
|
850
|
+
__reExport(compile_scale_exports, require("@math.gl/core/color"));
|
|
851
|
+
|
|
852
|
+
// dist/scales/palettes.js
|
|
853
|
+
var palettes_exports = {};
|
|
854
|
+
__reExport(palettes_exports, require("@math.gl/core/color"));
|
|
855
|
+
|
|
856
|
+
// dist/scales/quantile-sketch.js
|
|
857
|
+
var quantile_sketch_exports = {};
|
|
858
|
+
__reExport(quantile_sketch_exports, require("@math.gl/core/color"));
|
|
859
|
+
|
|
860
|
+
// dist/scales/hdr-colors.js
|
|
861
|
+
var hdr_colors_exports = {};
|
|
862
|
+
__reExport(hdr_colors_exports, require("@math.gl/core/color"));
|
|
863
|
+
|
|
864
|
+
// dist/scales/arrow-scales.js
|
|
865
|
+
var import_apache_arrow7 = require("apache-arrow");
|
|
866
|
+
|
|
867
|
+
// dist/arrow/compiler.js
|
|
868
|
+
var import_apache_arrow6 = require("apache-arrow");
|
|
869
|
+
var import_utf82 = require("@math.gl/core/utf8");
|
|
870
|
+
|
|
871
|
+
// dist/diagnostic-log.js
|
|
872
|
+
function reportDiagnosticError(log, error) {
|
|
873
|
+
if (log) {
|
|
874
|
+
try {
|
|
875
|
+
log.error(error instanceof Error ? error.message : String(error), error)();
|
|
876
|
+
} catch {
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
// dist/expressions/vendor/jsep/runtime.js
|
|
882
|
+
var Hooks = class {
|
|
883
|
+
/**
|
|
884
|
+
* @callback HookCallback
|
|
885
|
+
* @this {*|Jsep} this
|
|
886
|
+
* @param {Jsep} env
|
|
887
|
+
* @returns: void
|
|
888
|
+
*/
|
|
889
|
+
/**
|
|
890
|
+
* Adds the given callback to the list of callbacks for the given hook.
|
|
891
|
+
*
|
|
892
|
+
* The callback will be invoked when the hook it is registered for is run.
|
|
893
|
+
*
|
|
894
|
+
* One callback function can be registered to multiple hooks and the same hook multiple times.
|
|
895
|
+
*
|
|
896
|
+
* @param {string|object} name The name of the hook, or an object of callbacks keyed by name
|
|
897
|
+
* @param {HookCallback|boolean} callback The callback function which is given environment variables.
|
|
898
|
+
* @param {?boolean} [first=false] Will add the hook to the top of the list (defaults to the bottom)
|
|
899
|
+
* @public
|
|
900
|
+
*/
|
|
901
|
+
add(name2, callback, first) {
|
|
902
|
+
if (typeof arguments[0] != "string") {
|
|
903
|
+
for (let name22 in arguments[0]) {
|
|
904
|
+
this.add(name22, arguments[0][name22], arguments[1]);
|
|
905
|
+
}
|
|
906
|
+
} else {
|
|
907
|
+
(Array.isArray(name2) ? name2 : [name2]).forEach(function(name22) {
|
|
908
|
+
this[name22] = this[name22] || [];
|
|
909
|
+
if (callback) {
|
|
910
|
+
this[name22][first ? "unshift" : "push"](callback);
|
|
911
|
+
}
|
|
912
|
+
}, this);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* Runs a hook invoking all registered callbacks with the given environment variables.
|
|
917
|
+
*
|
|
918
|
+
* Callbacks will be invoked synchronously and in the order in which they were registered.
|
|
919
|
+
*
|
|
920
|
+
* @param {string} name The name of the hook.
|
|
921
|
+
* @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered.
|
|
922
|
+
* @public
|
|
923
|
+
*/
|
|
924
|
+
run(name2, env) {
|
|
925
|
+
this[name2] = this[name2] || [];
|
|
926
|
+
this[name2].forEach(function(callback) {
|
|
927
|
+
callback.call(env && env.context ? env.context : env, env);
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
};
|
|
931
|
+
var Plugins = class {
|
|
932
|
+
constructor(jsep22) {
|
|
933
|
+
this.jsep = jsep22;
|
|
934
|
+
this.registered = {};
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* @callback PluginSetup
|
|
938
|
+
* @this {Jsep} jsep
|
|
939
|
+
* @returns: void
|
|
940
|
+
*/
|
|
941
|
+
/**
|
|
942
|
+
* Adds the given plugin(s) to the registry
|
|
943
|
+
*
|
|
944
|
+
* @param {object} plugins
|
|
945
|
+
* @param {string} plugins.name The name of the plugin
|
|
946
|
+
* @param {PluginSetup} plugins.init The init function
|
|
947
|
+
* @public
|
|
948
|
+
*/
|
|
949
|
+
register(...plugins) {
|
|
950
|
+
plugins.forEach((plugin) => {
|
|
951
|
+
if (typeof plugin !== "object" || !plugin.name || !plugin.init) {
|
|
952
|
+
throw new Error("Invalid JSEP plugin format");
|
|
953
|
+
}
|
|
954
|
+
if (this.registered[plugin.name]) {
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
plugin.init(this.jsep);
|
|
958
|
+
this.registered[plugin.name] = plugin;
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
};
|
|
962
|
+
var Jsep = class _Jsep {
|
|
963
|
+
/**
|
|
964
|
+
* @returns {string}
|
|
965
|
+
*/
|
|
966
|
+
static get version() {
|
|
967
|
+
return "1.4.0";
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* @returns {string}
|
|
971
|
+
*/
|
|
972
|
+
static toString() {
|
|
973
|
+
return "JavaScript Expression Parser (JSEP) v" + _Jsep.version;
|
|
974
|
+
}
|
|
975
|
+
// ==================== CONFIG ================================
|
|
976
|
+
/**
|
|
977
|
+
* @method addUnaryOp
|
|
978
|
+
* @param {string} op_name The name of the unary op to add
|
|
979
|
+
* @returns {Jsep}
|
|
980
|
+
*/
|
|
981
|
+
static addUnaryOp(op_name) {
|
|
982
|
+
_Jsep.max_unop_len = Math.max(op_name.length, _Jsep.max_unop_len);
|
|
983
|
+
_Jsep.unary_ops[op_name] = 1;
|
|
984
|
+
return _Jsep;
|
|
985
|
+
}
|
|
986
|
+
/**
|
|
987
|
+
* @method jsep.addBinaryOp
|
|
988
|
+
* @param {string} op_name The name of the binary op to add
|
|
989
|
+
* @param {number} precedence The precedence of the binary op (can be a float). Higher number = higher precedence
|
|
990
|
+
* @param {boolean} [isRightAssociative=false] whether operator is right-associative
|
|
991
|
+
* @returns {Jsep}
|
|
992
|
+
*/
|
|
993
|
+
static addBinaryOp(op_name, precedence, isRightAssociative) {
|
|
994
|
+
_Jsep.max_binop_len = Math.max(op_name.length, _Jsep.max_binop_len);
|
|
995
|
+
_Jsep.binary_ops[op_name] = precedence;
|
|
996
|
+
if (isRightAssociative) {
|
|
997
|
+
_Jsep.right_associative.add(op_name);
|
|
998
|
+
} else {
|
|
999
|
+
_Jsep.right_associative.delete(op_name);
|
|
1000
|
+
}
|
|
1001
|
+
return _Jsep;
|
|
1002
|
+
}
|
|
1003
|
+
/**
|
|
1004
|
+
* @method addIdentifierChar
|
|
1005
|
+
* @param {string} char The additional character to treat as a valid part of an identifier
|
|
1006
|
+
* @returns {Jsep}
|
|
1007
|
+
*/
|
|
1008
|
+
static addIdentifierChar(char) {
|
|
1009
|
+
_Jsep.additional_identifier_chars.add(char);
|
|
1010
|
+
return _Jsep;
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* @method addLiteral
|
|
1014
|
+
* @param {string} literal_name The name of the literal to add
|
|
1015
|
+
* @param {*} literal_value The value of the literal
|
|
1016
|
+
* @returns {Jsep}
|
|
1017
|
+
*/
|
|
1018
|
+
static addLiteral(literal_name, literal_value) {
|
|
1019
|
+
_Jsep.literals[literal_name] = literal_value;
|
|
1020
|
+
return _Jsep;
|
|
1021
|
+
}
|
|
1022
|
+
/**
|
|
1023
|
+
* @method removeUnaryOp
|
|
1024
|
+
* @param {string} op_name The name of the unary op to remove
|
|
1025
|
+
* @returns {Jsep}
|
|
1026
|
+
*/
|
|
1027
|
+
static removeUnaryOp(op_name) {
|
|
1028
|
+
delete _Jsep.unary_ops[op_name];
|
|
1029
|
+
if (op_name.length === _Jsep.max_unop_len) {
|
|
1030
|
+
_Jsep.max_unop_len = _Jsep.getMaxKeyLen(_Jsep.unary_ops);
|
|
1031
|
+
}
|
|
1032
|
+
return _Jsep;
|
|
1033
|
+
}
|
|
1034
|
+
/**
|
|
1035
|
+
* @method removeAllUnaryOps
|
|
1036
|
+
* @returns {Jsep}
|
|
1037
|
+
*/
|
|
1038
|
+
static removeAllUnaryOps() {
|
|
1039
|
+
_Jsep.unary_ops = {};
|
|
1040
|
+
_Jsep.max_unop_len = 0;
|
|
1041
|
+
return _Jsep;
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
1044
|
+
* @method removeIdentifierChar
|
|
1045
|
+
* @param {string} char The additional character to stop treating as a valid part of an identifier
|
|
1046
|
+
* @returns {Jsep}
|
|
1047
|
+
*/
|
|
1048
|
+
static removeIdentifierChar(char) {
|
|
1049
|
+
_Jsep.additional_identifier_chars.delete(char);
|
|
1050
|
+
return _Jsep;
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* @method removeBinaryOp
|
|
1054
|
+
* @param {string} op_name The name of the binary op to remove
|
|
1055
|
+
* @returns {Jsep}
|
|
1056
|
+
*/
|
|
1057
|
+
static removeBinaryOp(op_name) {
|
|
1058
|
+
delete _Jsep.binary_ops[op_name];
|
|
1059
|
+
if (op_name.length === _Jsep.max_binop_len) {
|
|
1060
|
+
_Jsep.max_binop_len = _Jsep.getMaxKeyLen(_Jsep.binary_ops);
|
|
1061
|
+
}
|
|
1062
|
+
_Jsep.right_associative.delete(op_name);
|
|
1063
|
+
return _Jsep;
|
|
1064
|
+
}
|
|
1065
|
+
/**
|
|
1066
|
+
* @method removeAllBinaryOps
|
|
1067
|
+
* @returns {Jsep}
|
|
1068
|
+
*/
|
|
1069
|
+
static removeAllBinaryOps() {
|
|
1070
|
+
_Jsep.binary_ops = {};
|
|
1071
|
+
_Jsep.max_binop_len = 0;
|
|
1072
|
+
return _Jsep;
|
|
1073
|
+
}
|
|
1074
|
+
/**
|
|
1075
|
+
* @method removeLiteral
|
|
1076
|
+
* @param {string} literal_name The name of the literal to remove
|
|
1077
|
+
* @returns {Jsep}
|
|
1078
|
+
*/
|
|
1079
|
+
static removeLiteral(literal_name) {
|
|
1080
|
+
delete _Jsep.literals[literal_name];
|
|
1081
|
+
return _Jsep;
|
|
1082
|
+
}
|
|
1083
|
+
/**
|
|
1084
|
+
* @method removeAllLiterals
|
|
1085
|
+
* @returns {Jsep}
|
|
1086
|
+
*/
|
|
1087
|
+
static removeAllLiterals() {
|
|
1088
|
+
_Jsep.literals = {};
|
|
1089
|
+
return _Jsep;
|
|
1090
|
+
}
|
|
1091
|
+
// ==================== END CONFIG ============================
|
|
1092
|
+
/**
|
|
1093
|
+
* @returns {string}
|
|
1094
|
+
*/
|
|
1095
|
+
get char() {
|
|
1096
|
+
return this.expr.charAt(this.index);
|
|
1097
|
+
}
|
|
1098
|
+
/**
|
|
1099
|
+
* @returns {number}
|
|
1100
|
+
*/
|
|
1101
|
+
get code() {
|
|
1102
|
+
return this.expr.charCodeAt(this.index);
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* @param {string} expr a string with the passed in express
|
|
1106
|
+
* @returns Jsep
|
|
1107
|
+
*/
|
|
1108
|
+
constructor(expr) {
|
|
1109
|
+
this.expr = expr;
|
|
1110
|
+
this.index = 0;
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* static top-level parser
|
|
1114
|
+
* @returns {jsep.Expression}
|
|
1115
|
+
*/
|
|
1116
|
+
static parse(expr) {
|
|
1117
|
+
return new _Jsep(expr).parse();
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* Get the longest key length of any object
|
|
1121
|
+
* @param {object} obj
|
|
1122
|
+
* @returns {number}
|
|
1123
|
+
*/
|
|
1124
|
+
static getMaxKeyLen(obj) {
|
|
1125
|
+
return Math.max(0, ...Object.keys(obj).map((k) => k.length));
|
|
1126
|
+
}
|
|
1127
|
+
/**
|
|
1128
|
+
* `ch` is a character code in the next three functions
|
|
1129
|
+
* @param {number} ch
|
|
1130
|
+
* @returns {boolean}
|
|
1131
|
+
*/
|
|
1132
|
+
static isDecimalDigit(ch) {
|
|
1133
|
+
return ch >= 48 && ch <= 57;
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* Returns the precedence of a binary operator or `0` if it isn't a binary operator. Can be float.
|
|
1137
|
+
* @param {string} op_val
|
|
1138
|
+
* @returns {number}
|
|
1139
|
+
*/
|
|
1140
|
+
static binaryPrecedence(op_val) {
|
|
1141
|
+
return _Jsep.binary_ops[op_val] || 0;
|
|
1142
|
+
}
|
|
1143
|
+
/**
|
|
1144
|
+
* Looks for start of identifier
|
|
1145
|
+
* @param {number} ch
|
|
1146
|
+
* @returns {boolean}
|
|
1147
|
+
*/
|
|
1148
|
+
static isIdentifierStart(ch) {
|
|
1149
|
+
return ch >= 65 && ch <= 90 || // A...Z
|
|
1150
|
+
ch >= 97 && ch <= 122 || // a...z
|
|
1151
|
+
ch >= 128 && !_Jsep.binary_ops[String.fromCharCode(ch)] || // any non-ASCII that is not an operator
|
|
1152
|
+
_Jsep.additional_identifier_chars.has(String.fromCharCode(ch));
|
|
1153
|
+
}
|
|
1154
|
+
/**
|
|
1155
|
+
* @param {number} ch
|
|
1156
|
+
* @returns {boolean}
|
|
1157
|
+
*/
|
|
1158
|
+
static isIdentifierPart(ch) {
|
|
1159
|
+
return _Jsep.isIdentifierStart(ch) || _Jsep.isDecimalDigit(ch);
|
|
1160
|
+
}
|
|
1161
|
+
/**
|
|
1162
|
+
* throw error at index of the expression
|
|
1163
|
+
* @param {string} message
|
|
1164
|
+
* @throws
|
|
1165
|
+
*/
|
|
1166
|
+
throwError(message) {
|
|
1167
|
+
const error = new Error(message + " at character " + this.index);
|
|
1168
|
+
error.index = this.index;
|
|
1169
|
+
error.description = message;
|
|
1170
|
+
throw error;
|
|
1171
|
+
}
|
|
1172
|
+
/**
|
|
1173
|
+
* Run a given hook
|
|
1174
|
+
* @param {string} name
|
|
1175
|
+
* @param {jsep.Expression|false} [node]
|
|
1176
|
+
* @returns {?jsep.Expression}
|
|
1177
|
+
*/
|
|
1178
|
+
runHook(name2, node) {
|
|
1179
|
+
if (_Jsep.hooks[name2]) {
|
|
1180
|
+
const env = { context: this, node };
|
|
1181
|
+
_Jsep.hooks.run(name2, env);
|
|
1182
|
+
return env.node;
|
|
1183
|
+
}
|
|
1184
|
+
return node;
|
|
1185
|
+
}
|
|
1186
|
+
/**
|
|
1187
|
+
* Runs a given hook until one returns a node
|
|
1188
|
+
* @param {string} name
|
|
1189
|
+
* @returns {?jsep.Expression}
|
|
1190
|
+
*/
|
|
1191
|
+
searchHook(name2) {
|
|
1192
|
+
if (_Jsep.hooks[name2]) {
|
|
1193
|
+
const env = { context: this };
|
|
1194
|
+
_Jsep.hooks[name2].find(function(callback) {
|
|
1195
|
+
callback.call(env.context, env);
|
|
1196
|
+
return env.node;
|
|
1197
|
+
});
|
|
1198
|
+
return env.node;
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* Push `index` up to the next non-space character
|
|
1203
|
+
*/
|
|
1204
|
+
gobbleSpaces() {
|
|
1205
|
+
let ch = this.code;
|
|
1206
|
+
while (ch === _Jsep.SPACE_CODE || ch === _Jsep.TAB_CODE || ch === _Jsep.LF_CODE || ch === _Jsep.CR_CODE) {
|
|
1207
|
+
ch = this.expr.charCodeAt(++this.index);
|
|
1208
|
+
}
|
|
1209
|
+
this.runHook("gobble-spaces");
|
|
1210
|
+
}
|
|
1211
|
+
/**
|
|
1212
|
+
* Top-level method to parse all expressions and returns compound or single node
|
|
1213
|
+
* @returns {jsep.Expression}
|
|
1214
|
+
*/
|
|
1215
|
+
parse() {
|
|
1216
|
+
this.runHook("before-all");
|
|
1217
|
+
const nodes = this.gobbleExpressions();
|
|
1218
|
+
const node = nodes.length === 1 ? nodes[0] : {
|
|
1219
|
+
type: _Jsep.COMPOUND,
|
|
1220
|
+
body: nodes
|
|
1221
|
+
};
|
|
1222
|
+
return this.runHook("after-all", node);
|
|
1223
|
+
}
|
|
1224
|
+
/**
|
|
1225
|
+
* top-level parser (but can be reused within as well)
|
|
1226
|
+
* @param {number} [untilICode]
|
|
1227
|
+
* @returns {jsep.Expression[]}
|
|
1228
|
+
*/
|
|
1229
|
+
gobbleExpressions(untilICode) {
|
|
1230
|
+
let nodes = [], ch_i, node;
|
|
1231
|
+
while (this.index < this.expr.length) {
|
|
1232
|
+
ch_i = this.code;
|
|
1233
|
+
if (ch_i === _Jsep.SEMCOL_CODE || ch_i === _Jsep.COMMA_CODE) {
|
|
1234
|
+
this.index++;
|
|
1235
|
+
} else {
|
|
1236
|
+
if (node = this.gobbleExpression()) {
|
|
1237
|
+
nodes.push(node);
|
|
1238
|
+
} else if (this.index < this.expr.length) {
|
|
1239
|
+
if (ch_i === untilICode) {
|
|
1240
|
+
break;
|
|
1241
|
+
}
|
|
1242
|
+
this.throwError('Unexpected "' + this.char + '"');
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
return nodes;
|
|
1247
|
+
}
|
|
1248
|
+
/**
|
|
1249
|
+
* The main parsing function.
|
|
1250
|
+
* @returns {?jsep.Expression}
|
|
1251
|
+
*/
|
|
1252
|
+
gobbleExpression() {
|
|
1253
|
+
const node = this.searchHook("gobble-expression") || this.gobbleBinaryExpression();
|
|
1254
|
+
this.gobbleSpaces();
|
|
1255
|
+
return this.runHook("after-expression", node);
|
|
1256
|
+
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Search for the operation portion of the string (e.g. `+`, `===`)
|
|
1259
|
+
* Start by taking the longest possible binary operations (3 characters: `===`, `!==`, `>>>`)
|
|
1260
|
+
* and move down from 3 to 2 to 1 character until a matching binary operation is found
|
|
1261
|
+
* then, return that binary operation
|
|
1262
|
+
* @returns {string|boolean}
|
|
1263
|
+
*/
|
|
1264
|
+
gobbleBinaryOp() {
|
|
1265
|
+
this.gobbleSpaces();
|
|
1266
|
+
let to_check = this.expr.substr(this.index, _Jsep.max_binop_len);
|
|
1267
|
+
let tc_len = to_check.length;
|
|
1268
|
+
while (tc_len > 0) {
|
|
1269
|
+
if (_Jsep.binary_ops.hasOwnProperty(to_check) && (!_Jsep.isIdentifierStart(this.code) || this.index + to_check.length < this.expr.length && !_Jsep.isIdentifierPart(this.expr.charCodeAt(this.index + to_check.length)))) {
|
|
1270
|
+
this.index += tc_len;
|
|
1271
|
+
return to_check;
|
|
1272
|
+
}
|
|
1273
|
+
to_check = to_check.substr(0, --tc_len);
|
|
1274
|
+
}
|
|
1275
|
+
return false;
|
|
1276
|
+
}
|
|
1277
|
+
/**
|
|
1278
|
+
* This function is responsible for gobbling an individual expression,
|
|
1279
|
+
* e.g. `1`, `1+2`, `a+(b*2)-Math.sqrt(2)`
|
|
1280
|
+
* @returns {?jsep.BinaryExpression}
|
|
1281
|
+
*/
|
|
1282
|
+
gobbleBinaryExpression() {
|
|
1283
|
+
let node, biop, prec, stack, biop_info, left, right, i, cur_biop;
|
|
1284
|
+
left = this.gobbleToken();
|
|
1285
|
+
if (!left) {
|
|
1286
|
+
return left;
|
|
1287
|
+
}
|
|
1288
|
+
biop = this.gobbleBinaryOp();
|
|
1289
|
+
if (!biop) {
|
|
1290
|
+
return left;
|
|
1291
|
+
}
|
|
1292
|
+
biop_info = { value: biop, prec: _Jsep.binaryPrecedence(biop), right_a: _Jsep.right_associative.has(biop) };
|
|
1293
|
+
right = this.gobbleToken();
|
|
1294
|
+
if (!right) {
|
|
1295
|
+
this.throwError("Expected expression after " + biop);
|
|
1296
|
+
}
|
|
1297
|
+
stack = [left, biop_info, right];
|
|
1298
|
+
while (biop = this.gobbleBinaryOp()) {
|
|
1299
|
+
prec = _Jsep.binaryPrecedence(biop);
|
|
1300
|
+
if (prec === 0) {
|
|
1301
|
+
this.index -= biop.length;
|
|
1302
|
+
break;
|
|
1303
|
+
}
|
|
1304
|
+
biop_info = { value: biop, prec, right_a: _Jsep.right_associative.has(biop) };
|
|
1305
|
+
cur_biop = biop;
|
|
1306
|
+
const comparePrev = (prev) => biop_info.right_a && prev.right_a ? prec > prev.prec : prec <= prev.prec;
|
|
1307
|
+
while (stack.length > 2 && comparePrev(stack[stack.length - 2])) {
|
|
1308
|
+
right = stack.pop();
|
|
1309
|
+
biop = stack.pop().value;
|
|
1310
|
+
left = stack.pop();
|
|
1311
|
+
node = {
|
|
1312
|
+
type: _Jsep.BINARY_EXP,
|
|
1313
|
+
operator: biop,
|
|
1314
|
+
left,
|
|
1315
|
+
right
|
|
1316
|
+
};
|
|
1317
|
+
stack.push(node);
|
|
1318
|
+
}
|
|
1319
|
+
node = this.gobbleToken();
|
|
1320
|
+
if (!node) {
|
|
1321
|
+
this.throwError("Expected expression after " + cur_biop);
|
|
1322
|
+
}
|
|
1323
|
+
stack.push(biop_info, node);
|
|
1324
|
+
}
|
|
1325
|
+
i = stack.length - 1;
|
|
1326
|
+
node = stack[i];
|
|
1327
|
+
while (i > 1) {
|
|
1328
|
+
node = {
|
|
1329
|
+
type: _Jsep.BINARY_EXP,
|
|
1330
|
+
operator: stack[i - 1].value,
|
|
1331
|
+
left: stack[i - 2],
|
|
1332
|
+
right: node
|
|
1333
|
+
};
|
|
1334
|
+
i -= 2;
|
|
1335
|
+
}
|
|
1336
|
+
return node;
|
|
1337
|
+
}
|
|
1338
|
+
/**
|
|
1339
|
+
* An individual part of a binary expression:
|
|
1340
|
+
* e.g. `foo.bar(baz)`, `1`, `"abc"`, `(a % 2)` (because it's in parenthesis)
|
|
1341
|
+
* @returns {boolean|jsep.Expression}
|
|
1342
|
+
*/
|
|
1343
|
+
gobbleToken() {
|
|
1344
|
+
let ch, to_check, tc_len, node;
|
|
1345
|
+
this.gobbleSpaces();
|
|
1346
|
+
node = this.searchHook("gobble-token");
|
|
1347
|
+
if (node) {
|
|
1348
|
+
return this.runHook("after-token", node);
|
|
1349
|
+
}
|
|
1350
|
+
ch = this.code;
|
|
1351
|
+
if (_Jsep.isDecimalDigit(ch) || ch === _Jsep.PERIOD_CODE) {
|
|
1352
|
+
return this.gobbleNumericLiteral();
|
|
1353
|
+
}
|
|
1354
|
+
if (ch === _Jsep.SQUOTE_CODE || ch === _Jsep.DQUOTE_CODE) {
|
|
1355
|
+
node = this.gobbleStringLiteral();
|
|
1356
|
+
} else if (ch === _Jsep.OBRACK_CODE) {
|
|
1357
|
+
node = this.gobbleArray();
|
|
1358
|
+
} else {
|
|
1359
|
+
to_check = this.expr.substr(this.index, _Jsep.max_unop_len);
|
|
1360
|
+
tc_len = to_check.length;
|
|
1361
|
+
while (tc_len > 0) {
|
|
1362
|
+
if (_Jsep.unary_ops.hasOwnProperty(to_check) && (!_Jsep.isIdentifierStart(this.code) || this.index + to_check.length < this.expr.length && !_Jsep.isIdentifierPart(this.expr.charCodeAt(this.index + to_check.length)))) {
|
|
1363
|
+
this.index += tc_len;
|
|
1364
|
+
const argument = this.gobbleToken();
|
|
1365
|
+
if (!argument) {
|
|
1366
|
+
this.throwError("missing unaryOp argument");
|
|
1367
|
+
}
|
|
1368
|
+
return this.runHook("after-token", {
|
|
1369
|
+
type: _Jsep.UNARY_EXP,
|
|
1370
|
+
operator: to_check,
|
|
1371
|
+
argument,
|
|
1372
|
+
prefix: true
|
|
1373
|
+
});
|
|
1374
|
+
}
|
|
1375
|
+
to_check = to_check.substr(0, --tc_len);
|
|
1376
|
+
}
|
|
1377
|
+
if (_Jsep.isIdentifierStart(ch)) {
|
|
1378
|
+
node = this.gobbleIdentifier();
|
|
1379
|
+
if (_Jsep.literals.hasOwnProperty(node.name)) {
|
|
1380
|
+
node = {
|
|
1381
|
+
type: _Jsep.LITERAL,
|
|
1382
|
+
value: _Jsep.literals[node.name],
|
|
1383
|
+
raw: node.name
|
|
1384
|
+
};
|
|
1385
|
+
} else if (node.name === _Jsep.this_str) {
|
|
1386
|
+
node = { type: _Jsep.THIS_EXP };
|
|
1387
|
+
}
|
|
1388
|
+
} else if (ch === _Jsep.OPAREN_CODE) {
|
|
1389
|
+
node = this.gobbleGroup();
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
if (!node) {
|
|
1393
|
+
return this.runHook("after-token", false);
|
|
1394
|
+
}
|
|
1395
|
+
node = this.gobbleTokenProperty(node);
|
|
1396
|
+
return this.runHook("after-token", node);
|
|
1397
|
+
}
|
|
1398
|
+
/**
|
|
1399
|
+
* Gobble properties of of identifiers/strings/arrays/groups.
|
|
1400
|
+
* e.g. `foo`, `bar.baz`, `foo['bar'].baz`
|
|
1401
|
+
* It also gobbles function calls:
|
|
1402
|
+
* e.g. `Math.acos(obj.angle)`
|
|
1403
|
+
* @param {jsep.Expression} node
|
|
1404
|
+
* @returns {jsep.Expression}
|
|
1405
|
+
*/
|
|
1406
|
+
gobbleTokenProperty(node) {
|
|
1407
|
+
this.gobbleSpaces();
|
|
1408
|
+
let ch = this.code;
|
|
1409
|
+
while (ch === _Jsep.PERIOD_CODE || ch === _Jsep.OBRACK_CODE || ch === _Jsep.OPAREN_CODE || ch === _Jsep.QUMARK_CODE) {
|
|
1410
|
+
let optional;
|
|
1411
|
+
if (ch === _Jsep.QUMARK_CODE) {
|
|
1412
|
+
if (this.expr.charCodeAt(this.index + 1) !== _Jsep.PERIOD_CODE) {
|
|
1413
|
+
break;
|
|
1414
|
+
}
|
|
1415
|
+
optional = true;
|
|
1416
|
+
this.index += 2;
|
|
1417
|
+
this.gobbleSpaces();
|
|
1418
|
+
ch = this.code;
|
|
1419
|
+
}
|
|
1420
|
+
this.index++;
|
|
1421
|
+
if (ch === _Jsep.OBRACK_CODE) {
|
|
1422
|
+
node = {
|
|
1423
|
+
type: _Jsep.MEMBER_EXP,
|
|
1424
|
+
computed: true,
|
|
1425
|
+
object: node,
|
|
1426
|
+
property: this.gobbleExpression()
|
|
1427
|
+
};
|
|
1428
|
+
if (!node.property) {
|
|
1429
|
+
this.throwError('Unexpected "' + this.char + '"');
|
|
1430
|
+
}
|
|
1431
|
+
this.gobbleSpaces();
|
|
1432
|
+
ch = this.code;
|
|
1433
|
+
if (ch !== _Jsep.CBRACK_CODE) {
|
|
1434
|
+
this.throwError("Unclosed [");
|
|
1435
|
+
}
|
|
1436
|
+
this.index++;
|
|
1437
|
+
} else if (ch === _Jsep.OPAREN_CODE) {
|
|
1438
|
+
node = {
|
|
1439
|
+
type: _Jsep.CALL_EXP,
|
|
1440
|
+
"arguments": this.gobbleArguments(_Jsep.CPAREN_CODE),
|
|
1441
|
+
callee: node
|
|
1442
|
+
};
|
|
1443
|
+
} else if (ch === _Jsep.PERIOD_CODE || optional) {
|
|
1444
|
+
if (optional) {
|
|
1445
|
+
this.index--;
|
|
1446
|
+
}
|
|
1447
|
+
this.gobbleSpaces();
|
|
1448
|
+
node = {
|
|
1449
|
+
type: _Jsep.MEMBER_EXP,
|
|
1450
|
+
computed: false,
|
|
1451
|
+
object: node,
|
|
1452
|
+
property: this.gobbleIdentifier()
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1455
|
+
if (optional) {
|
|
1456
|
+
node.optional = true;
|
|
1457
|
+
}
|
|
1458
|
+
this.gobbleSpaces();
|
|
1459
|
+
ch = this.code;
|
|
1460
|
+
}
|
|
1461
|
+
return node;
|
|
1462
|
+
}
|
|
1463
|
+
/**
|
|
1464
|
+
* Parse simple numeric literals: `12`, `3.4`, `.5`. Do this by using a string to
|
|
1465
|
+
* keep track of everything in the numeric literal and then calling `parseFloat` on that string
|
|
1466
|
+
* @returns {jsep.Literal}
|
|
1467
|
+
*/
|
|
1468
|
+
gobbleNumericLiteral() {
|
|
1469
|
+
let number2 = "", ch, chCode;
|
|
1470
|
+
while (_Jsep.isDecimalDigit(this.code)) {
|
|
1471
|
+
number2 += this.expr.charAt(this.index++);
|
|
1472
|
+
}
|
|
1473
|
+
if (this.code === _Jsep.PERIOD_CODE) {
|
|
1474
|
+
number2 += this.expr.charAt(this.index++);
|
|
1475
|
+
while (_Jsep.isDecimalDigit(this.code)) {
|
|
1476
|
+
number2 += this.expr.charAt(this.index++);
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
ch = this.char;
|
|
1480
|
+
if (ch === "e" || ch === "E") {
|
|
1481
|
+
number2 += this.expr.charAt(this.index++);
|
|
1482
|
+
ch = this.char;
|
|
1483
|
+
if (ch === "+" || ch === "-") {
|
|
1484
|
+
number2 += this.expr.charAt(this.index++);
|
|
1485
|
+
}
|
|
1486
|
+
while (_Jsep.isDecimalDigit(this.code)) {
|
|
1487
|
+
number2 += this.expr.charAt(this.index++);
|
|
1488
|
+
}
|
|
1489
|
+
if (!_Jsep.isDecimalDigit(this.expr.charCodeAt(this.index - 1))) {
|
|
1490
|
+
this.throwError("Expected exponent (" + number2 + this.char + ")");
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
chCode = this.code;
|
|
1494
|
+
if (_Jsep.isIdentifierStart(chCode)) {
|
|
1495
|
+
this.throwError("Variable names cannot start with a number (" + number2 + this.char + ")");
|
|
1496
|
+
} else if (chCode === _Jsep.PERIOD_CODE || number2.length === 1 && number2.charCodeAt(0) === _Jsep.PERIOD_CODE) {
|
|
1497
|
+
this.throwError("Unexpected period");
|
|
1498
|
+
}
|
|
1499
|
+
return {
|
|
1500
|
+
type: _Jsep.LITERAL,
|
|
1501
|
+
value: parseFloat(number2),
|
|
1502
|
+
raw: number2
|
|
1503
|
+
};
|
|
1504
|
+
}
|
|
1505
|
+
/**
|
|
1506
|
+
* Parses a string literal, staring with single or double quotes with basic support for escape codes
|
|
1507
|
+
* e.g. `"hello world"`, `'this is\nJSEP'`
|
|
1508
|
+
* @returns {jsep.Literal}
|
|
1509
|
+
*/
|
|
1510
|
+
gobbleStringLiteral() {
|
|
1511
|
+
let str = "";
|
|
1512
|
+
const startIndex = this.index;
|
|
1513
|
+
const quote = this.expr.charAt(this.index++);
|
|
1514
|
+
let closed = false;
|
|
1515
|
+
while (this.index < this.expr.length) {
|
|
1516
|
+
let ch = this.expr.charAt(this.index++);
|
|
1517
|
+
if (ch === quote) {
|
|
1518
|
+
closed = true;
|
|
1519
|
+
break;
|
|
1520
|
+
} else if (ch === "\\") {
|
|
1521
|
+
ch = this.expr.charAt(this.index++);
|
|
1522
|
+
switch (ch) {
|
|
1523
|
+
case "n":
|
|
1524
|
+
str += "\n";
|
|
1525
|
+
break;
|
|
1526
|
+
case "r":
|
|
1527
|
+
str += "\r";
|
|
1528
|
+
break;
|
|
1529
|
+
case "t":
|
|
1530
|
+
str += " ";
|
|
1531
|
+
break;
|
|
1532
|
+
case "b":
|
|
1533
|
+
str += "\b";
|
|
1534
|
+
break;
|
|
1535
|
+
case "f":
|
|
1536
|
+
str += "\f";
|
|
1537
|
+
break;
|
|
1538
|
+
case "v":
|
|
1539
|
+
str += "\v";
|
|
1540
|
+
break;
|
|
1541
|
+
default:
|
|
1542
|
+
str += ch;
|
|
1543
|
+
}
|
|
1544
|
+
} else {
|
|
1545
|
+
str += ch;
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
if (!closed) {
|
|
1549
|
+
this.throwError('Unclosed quote after "' + str + '"');
|
|
1550
|
+
}
|
|
1551
|
+
return {
|
|
1552
|
+
type: _Jsep.LITERAL,
|
|
1553
|
+
value: str,
|
|
1554
|
+
raw: this.expr.substring(startIndex, this.index)
|
|
1555
|
+
};
|
|
1556
|
+
}
|
|
1557
|
+
/**
|
|
1558
|
+
* Gobbles only identifiers
|
|
1559
|
+
* e.g.: `foo`, `_value`, `$x1`
|
|
1560
|
+
* Also, this function checks if that identifier is a literal:
|
|
1561
|
+
* (e.g. `true`, `false`, `null`) or `this`
|
|
1562
|
+
* @returns {jsep.Identifier}
|
|
1563
|
+
*/
|
|
1564
|
+
gobbleIdentifier() {
|
|
1565
|
+
let ch = this.code, start = this.index;
|
|
1566
|
+
if (_Jsep.isIdentifierStart(ch)) {
|
|
1567
|
+
this.index++;
|
|
1568
|
+
} else {
|
|
1569
|
+
this.throwError("Unexpected " + this.char);
|
|
1570
|
+
}
|
|
1571
|
+
while (this.index < this.expr.length) {
|
|
1572
|
+
ch = this.code;
|
|
1573
|
+
if (_Jsep.isIdentifierPart(ch)) {
|
|
1574
|
+
this.index++;
|
|
1575
|
+
} else {
|
|
1576
|
+
break;
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
return {
|
|
1580
|
+
type: _Jsep.IDENTIFIER,
|
|
1581
|
+
name: this.expr.slice(start, this.index)
|
|
1582
|
+
};
|
|
1583
|
+
}
|
|
1584
|
+
/**
|
|
1585
|
+
* Gobbles a list of arguments within the context of a function call
|
|
1586
|
+
* or array literal. This function also assumes that the opening character
|
|
1587
|
+
* `(` or `[` has already been gobbled, and gobbles expressions and commas
|
|
1588
|
+
* until the terminator character `)` or `]` is encountered.
|
|
1589
|
+
* e.g. `foo(bar, baz)`, `my_func()`, or `[bar, baz]`
|
|
1590
|
+
* @param {number} termination
|
|
1591
|
+
* @returns {jsep.Expression[]}
|
|
1592
|
+
*/
|
|
1593
|
+
gobbleArguments(termination) {
|
|
1594
|
+
const args = [];
|
|
1595
|
+
let closed = false;
|
|
1596
|
+
let separator_count = 0;
|
|
1597
|
+
while (this.index < this.expr.length) {
|
|
1598
|
+
this.gobbleSpaces();
|
|
1599
|
+
let ch_i = this.code;
|
|
1600
|
+
if (ch_i === termination) {
|
|
1601
|
+
closed = true;
|
|
1602
|
+
this.index++;
|
|
1603
|
+
if (termination === _Jsep.CPAREN_CODE && separator_count && separator_count >= args.length) {
|
|
1604
|
+
this.throwError("Unexpected token " + String.fromCharCode(termination));
|
|
1605
|
+
}
|
|
1606
|
+
break;
|
|
1607
|
+
} else if (ch_i === _Jsep.COMMA_CODE) {
|
|
1608
|
+
this.index++;
|
|
1609
|
+
separator_count++;
|
|
1610
|
+
if (separator_count !== args.length) {
|
|
1611
|
+
if (termination === _Jsep.CPAREN_CODE) {
|
|
1612
|
+
this.throwError("Unexpected token ,");
|
|
1613
|
+
} else if (termination === _Jsep.CBRACK_CODE) {
|
|
1614
|
+
for (let arg = args.length; arg < separator_count; arg++) {
|
|
1615
|
+
args.push(null);
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
} else if (args.length !== separator_count && separator_count !== 0) {
|
|
1620
|
+
this.throwError("Expected comma");
|
|
1621
|
+
} else {
|
|
1622
|
+
const node = this.gobbleExpression();
|
|
1623
|
+
if (!node || node.type === _Jsep.COMPOUND) {
|
|
1624
|
+
this.throwError("Expected comma");
|
|
1625
|
+
}
|
|
1626
|
+
args.push(node);
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
if (!closed) {
|
|
1630
|
+
this.throwError("Expected " + String.fromCharCode(termination));
|
|
1631
|
+
}
|
|
1632
|
+
return args;
|
|
1633
|
+
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Responsible for parsing a group of things within parentheses `()`
|
|
1636
|
+
* that have no identifier in front (so not a function call)
|
|
1637
|
+
* This function assumes that it needs to gobble the opening parenthesis
|
|
1638
|
+
* and then tries to gobble everything within that parenthesis, assuming
|
|
1639
|
+
* that the next thing it should see is the close parenthesis. If not,
|
|
1640
|
+
* then the expression probably doesn't have a `)`
|
|
1641
|
+
* @returns {boolean|jsep.Expression}
|
|
1642
|
+
*/
|
|
1643
|
+
gobbleGroup() {
|
|
1644
|
+
this.index++;
|
|
1645
|
+
let nodes = this.gobbleExpressions(_Jsep.CPAREN_CODE);
|
|
1646
|
+
if (this.code === _Jsep.CPAREN_CODE) {
|
|
1647
|
+
this.index++;
|
|
1648
|
+
if (nodes.length === 1) {
|
|
1649
|
+
return nodes[0];
|
|
1650
|
+
} else if (!nodes.length) {
|
|
1651
|
+
return false;
|
|
1652
|
+
} else {
|
|
1653
|
+
return {
|
|
1654
|
+
type: _Jsep.SEQUENCE_EXP,
|
|
1655
|
+
expressions: nodes
|
|
1656
|
+
};
|
|
1657
|
+
}
|
|
1658
|
+
} else {
|
|
1659
|
+
this.throwError("Unclosed (");
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
/**
|
|
1663
|
+
* Responsible for parsing Array literals `[1, 2, 3]`
|
|
1664
|
+
* This function assumes that it needs to gobble the opening bracket
|
|
1665
|
+
* and then tries to gobble the expressions as arguments.
|
|
1666
|
+
* @returns {jsep.ArrayExpression}
|
|
1667
|
+
*/
|
|
1668
|
+
gobbleArray() {
|
|
1669
|
+
this.index++;
|
|
1670
|
+
return {
|
|
1671
|
+
type: _Jsep.ARRAY_EXP,
|
|
1672
|
+
elements: this.gobbleArguments(_Jsep.CBRACK_CODE)
|
|
1673
|
+
};
|
|
1674
|
+
}
|
|
1675
|
+
};
|
|
1676
|
+
var hooks = new Hooks();
|
|
1677
|
+
Object.assign(Jsep, {
|
|
1678
|
+
hooks,
|
|
1679
|
+
plugins: new Plugins(Jsep),
|
|
1680
|
+
// Node Types
|
|
1681
|
+
// ----------
|
|
1682
|
+
// This is the full set of types that any JSEP node can be.
|
|
1683
|
+
// Store them here to save space when minified
|
|
1684
|
+
COMPOUND: "Compound",
|
|
1685
|
+
SEQUENCE_EXP: "SequenceExpression",
|
|
1686
|
+
IDENTIFIER: "Identifier",
|
|
1687
|
+
MEMBER_EXP: "MemberExpression",
|
|
1688
|
+
LITERAL: "Literal",
|
|
1689
|
+
THIS_EXP: "ThisExpression",
|
|
1690
|
+
CALL_EXP: "CallExpression",
|
|
1691
|
+
UNARY_EXP: "UnaryExpression",
|
|
1692
|
+
BINARY_EXP: "BinaryExpression",
|
|
1693
|
+
ARRAY_EXP: "ArrayExpression",
|
|
1694
|
+
TAB_CODE: 9,
|
|
1695
|
+
LF_CODE: 10,
|
|
1696
|
+
CR_CODE: 13,
|
|
1697
|
+
SPACE_CODE: 32,
|
|
1698
|
+
PERIOD_CODE: 46,
|
|
1699
|
+
// '.'
|
|
1700
|
+
COMMA_CODE: 44,
|
|
1701
|
+
// ','
|
|
1702
|
+
SQUOTE_CODE: 39,
|
|
1703
|
+
// single quote
|
|
1704
|
+
DQUOTE_CODE: 34,
|
|
1705
|
+
// double quotes
|
|
1706
|
+
OPAREN_CODE: 40,
|
|
1707
|
+
// (
|
|
1708
|
+
CPAREN_CODE: 41,
|
|
1709
|
+
// )
|
|
1710
|
+
OBRACK_CODE: 91,
|
|
1711
|
+
// [
|
|
1712
|
+
CBRACK_CODE: 93,
|
|
1713
|
+
// ]
|
|
1714
|
+
QUMARK_CODE: 63,
|
|
1715
|
+
// ?
|
|
1716
|
+
SEMCOL_CODE: 59,
|
|
1717
|
+
// ;
|
|
1718
|
+
COLON_CODE: 58,
|
|
1719
|
+
// :
|
|
1720
|
+
// Operations
|
|
1721
|
+
// ----------
|
|
1722
|
+
// Use a quickly-accessible map to store all of the unary operators
|
|
1723
|
+
// Values are set to `1` (it really doesn't matter)
|
|
1724
|
+
unary_ops: {
|
|
1725
|
+
"-": 1,
|
|
1726
|
+
"!": 1,
|
|
1727
|
+
"~": 1,
|
|
1728
|
+
"+": 1
|
|
1729
|
+
},
|
|
1730
|
+
// Also use a map for the binary operations but set their values to their
|
|
1731
|
+
// binary precedence for quick reference (higher number = higher precedence)
|
|
1732
|
+
// see [Order of operations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)
|
|
1733
|
+
binary_ops: {
|
|
1734
|
+
"||": 1,
|
|
1735
|
+
"??": 1,
|
|
1736
|
+
"&&": 2,
|
|
1737
|
+
"|": 3,
|
|
1738
|
+
"^": 4,
|
|
1739
|
+
"&": 5,
|
|
1740
|
+
"==": 6,
|
|
1741
|
+
"!=": 6,
|
|
1742
|
+
"===": 6,
|
|
1743
|
+
"!==": 6,
|
|
1744
|
+
"<": 7,
|
|
1745
|
+
">": 7,
|
|
1746
|
+
"<=": 7,
|
|
1747
|
+
">=": 7,
|
|
1748
|
+
"<<": 8,
|
|
1749
|
+
">>": 8,
|
|
1750
|
+
">>>": 8,
|
|
1751
|
+
"+": 9,
|
|
1752
|
+
"-": 9,
|
|
1753
|
+
"*": 10,
|
|
1754
|
+
"/": 10,
|
|
1755
|
+
"%": 10,
|
|
1756
|
+
"**": 11
|
|
1757
|
+
},
|
|
1758
|
+
// sets specific binary_ops as right-associative
|
|
1759
|
+
right_associative: /* @__PURE__ */ new Set(["**"]),
|
|
1760
|
+
// Additional valid identifier chars, apart from a-z, A-Z and 0-9 (except on the starting char)
|
|
1761
|
+
additional_identifier_chars: /* @__PURE__ */ new Set(["$", "_"]),
|
|
1762
|
+
// Literals
|
|
1763
|
+
// ----------
|
|
1764
|
+
// Store the values to return for the various literals we may encounter
|
|
1765
|
+
literals: {
|
|
1766
|
+
"true": true,
|
|
1767
|
+
"false": false,
|
|
1768
|
+
"null": null
|
|
1769
|
+
},
|
|
1770
|
+
// Except for `this`, which is special. This could be changed to something like `'self'` as well
|
|
1771
|
+
this_str: "this"
|
|
1772
|
+
});
|
|
1773
|
+
Jsep.max_unop_len = Jsep.getMaxKeyLen(Jsep.unary_ops);
|
|
1774
|
+
Jsep.max_binop_len = Jsep.getMaxKeyLen(Jsep.binary_ops);
|
|
1775
|
+
var jsep = (expr) => new Jsep(expr).parse();
|
|
1776
|
+
var stdClassProps = Object.getOwnPropertyNames(class Test {
|
|
1777
|
+
});
|
|
1778
|
+
Object.getOwnPropertyNames(Jsep).filter((prop) => !stdClassProps.includes(prop) && jsep[prop] === void 0).forEach((m) => {
|
|
1779
|
+
jsep[m] = Jsep[m];
|
|
1780
|
+
});
|
|
1781
|
+
jsep.Jsep = Jsep;
|
|
1782
|
+
var CONDITIONAL_EXP = "ConditionalExpression";
|
|
1783
|
+
var ternary = {
|
|
1784
|
+
name: "ternary",
|
|
1785
|
+
init(jsep22) {
|
|
1786
|
+
jsep22.hooks.add("after-expression", function gobbleTernary(env) {
|
|
1787
|
+
if (env.node && this.code === jsep22.QUMARK_CODE) {
|
|
1788
|
+
this.index++;
|
|
1789
|
+
const test = env.node;
|
|
1790
|
+
const consequent = this.gobbleExpression();
|
|
1791
|
+
if (!consequent) {
|
|
1792
|
+
this.throwError("Expected expression");
|
|
1793
|
+
}
|
|
1794
|
+
this.gobbleSpaces();
|
|
1795
|
+
if (this.code === jsep22.COLON_CODE) {
|
|
1796
|
+
this.index++;
|
|
1797
|
+
const alternate = this.gobbleExpression();
|
|
1798
|
+
if (!alternate) {
|
|
1799
|
+
this.throwError("Expected expression");
|
|
1800
|
+
}
|
|
1801
|
+
env.node = {
|
|
1802
|
+
type: CONDITIONAL_EXP,
|
|
1803
|
+
test,
|
|
1804
|
+
consequent,
|
|
1805
|
+
alternate
|
|
1806
|
+
};
|
|
1807
|
+
if (test.operator && jsep22.binary_ops[test.operator] <= 0.9) {
|
|
1808
|
+
let newTest = test;
|
|
1809
|
+
while (newTest.right.operator && jsep22.binary_ops[newTest.right.operator] <= 0.9) {
|
|
1810
|
+
newTest = newTest.right;
|
|
1811
|
+
}
|
|
1812
|
+
env.node.test = newTest.right;
|
|
1813
|
+
newTest.right = env.node;
|
|
1814
|
+
env.node = test;
|
|
1815
|
+
}
|
|
1816
|
+
} else {
|
|
1817
|
+
this.throwError("Expected :");
|
|
1818
|
+
}
|
|
1819
|
+
}
|
|
1820
|
+
});
|
|
1821
|
+
}
|
|
1822
|
+
};
|
|
1823
|
+
jsep.plugins.register(ternary);
|
|
1824
|
+
var runtime_default = jsep;
|
|
1825
|
+
|
|
1826
|
+
// dist/expressions/expression-eval.js
|
|
1827
|
+
var jsep2 = runtime_default;
|
|
1828
|
+
function parse(expression, options) {
|
|
1829
|
+
try {
|
|
1830
|
+
return typeof expression === "string" ? jsep2(expression) : expression;
|
|
1831
|
+
} catch (error) {
|
|
1832
|
+
reportDiagnosticError(options == null ? void 0 : options.log, error);
|
|
1833
|
+
throw error;
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
|
|
1837
|
+
// dist/expressions/ast.js
|
|
1838
|
+
var MAXIMUM_EXPRESSION_NODES = 16384;
|
|
1839
|
+
var MAXIMUM_EXPRESSION_DEPTH = 192;
|
|
1840
|
+
function validateExpressionAST(value, options = {}) {
|
|
1841
|
+
visitExpressionAST(value, () => {
|
|
1842
|
+
}, options);
|
|
1843
|
+
}
|
|
1844
|
+
function visitExpressionAST(value, visit, options) {
|
|
1845
|
+
var _a2;
|
|
1846
|
+
const maxNodes = options.maxNodes ?? MAXIMUM_EXPRESSION_NODES, maxDepth = options.maxDepth ?? MAXIMUM_EXPRESSION_DEPTH;
|
|
1847
|
+
if (!Number.isSafeInteger(maxNodes) || maxNodes < 1 || maxNodes > MAXIMUM_EXPRESSION_NODES || !Number.isSafeInteger(maxDepth) || maxDepth < 0 || maxDepth > MAXIMUM_EXPRESSION_DEPTH)
|
|
1848
|
+
throw new Error(`Expression AST limits must not exceed ${MAXIMUM_EXPRESSION_NODES} nodes and ${MAXIMUM_EXPRESSION_DEPTH} levels`);
|
|
1849
|
+
const stack = [{ node: value, depth: 0 }];
|
|
1850
|
+
const ancestors = /* @__PURE__ */ new Set();
|
|
1851
|
+
let count = 0;
|
|
1852
|
+
while (stack.length) {
|
|
1853
|
+
const { node: input, depth, exit } = stack.pop();
|
|
1854
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
1855
|
+
throw new Error("Invalid expression AST node");
|
|
1856
|
+
if (exit) {
|
|
1857
|
+
ancestors.delete(input);
|
|
1858
|
+
continue;
|
|
1859
|
+
}
|
|
1860
|
+
if (++count > maxNodes || depth > maxDepth)
|
|
1861
|
+
throw new Error("Expression AST exceeds node or depth limit");
|
|
1862
|
+
if (ancestors.has(input))
|
|
1863
|
+
throw new Error("Expression AST contains a cycle");
|
|
1864
|
+
ancestors.add(input);
|
|
1865
|
+
stack.push({ node: input, depth, exit: true });
|
|
1866
|
+
const node = input;
|
|
1867
|
+
const children = [];
|
|
1868
|
+
switch (node.type) {
|
|
1869
|
+
case "Identifier":
|
|
1870
|
+
if (typeof node.name !== "string" || !node.name.length || node.name.length > 65536)
|
|
1871
|
+
throw new Error("Invalid expression identifier");
|
|
1872
|
+
break;
|
|
1873
|
+
case "Literal":
|
|
1874
|
+
if (node.value !== null && !["number", "bigint", "boolean", "string"].includes(typeof node.value) && !(node.value instanceof Uint8Array) && !(node.value instanceof Date && Number.isFinite(node.value.getTime())))
|
|
1875
|
+
throw new Error("Unsupported expression literal");
|
|
1876
|
+
if (node.raw !== void 0 && typeof node.raw !== "string")
|
|
1877
|
+
throw new Error("Invalid literal annotation");
|
|
1878
|
+
break;
|
|
1879
|
+
case "UnaryExpression":
|
|
1880
|
+
if (typeof node.operator !== "string" || !node.operator.length || node.operator.length > 32)
|
|
1881
|
+
throw new Error("Invalid expression operator");
|
|
1882
|
+
if (node.prefix !== void 0 && typeof node.prefix !== "boolean")
|
|
1883
|
+
throw new Error("Invalid unary annotation");
|
|
1884
|
+
children.push(node.argument);
|
|
1885
|
+
break;
|
|
1886
|
+
case "LogicalExpression":
|
|
1887
|
+
if (node.operator !== "&&" && node.operator !== "||")
|
|
1888
|
+
throw new Error("Invalid logical operator");
|
|
1889
|
+
children.push(node.left, node.right);
|
|
1890
|
+
break;
|
|
1891
|
+
case "BinaryExpression":
|
|
1892
|
+
if (typeof node.operator !== "string" || !node.operator.length || node.operator.length > 32)
|
|
1893
|
+
throw new Error("Invalid expression operator");
|
|
1894
|
+
children.push(node.left, node.right);
|
|
1895
|
+
break;
|
|
1896
|
+
case "ConditionalExpression":
|
|
1897
|
+
children.push(node.test, node.consequent, node.alternate);
|
|
1898
|
+
break;
|
|
1899
|
+
case "ArrayExpression":
|
|
1900
|
+
if (!Array.isArray(node.elements))
|
|
1901
|
+
throw new Error("Invalid expression array");
|
|
1902
|
+
if (node.elements.length + count > maxNodes)
|
|
1903
|
+
throw new Error("Expression AST exceeds node limit");
|
|
1904
|
+
for (const element of node.elements) {
|
|
1905
|
+
if (element == null)
|
|
1906
|
+
throw new Error("Expression array contains holes");
|
|
1907
|
+
children.push(element);
|
|
1908
|
+
}
|
|
1909
|
+
break;
|
|
1910
|
+
case "CallExpression":
|
|
1911
|
+
if (((_a2 = node.callee) == null ? void 0 : _a2.type) !== "Identifier" || typeof node.callee.name !== "string" || !node.callee.name.length || node.callee.name.length > 65536 || !Array.isArray(node.arguments) || node.sqlFunction !== void 0 && node.sqlFunction !== true)
|
|
1912
|
+
throw new Error("Expressions require named function calls");
|
|
1913
|
+
if (node.arguments.length + count > maxNodes)
|
|
1914
|
+
throw new Error("Expression AST exceeds node limit");
|
|
1915
|
+
children.push(...node.arguments);
|
|
1916
|
+
break;
|
|
1917
|
+
default:
|
|
1918
|
+
throw new Error(`Unsupported portable expression AST node: ${node.type}`);
|
|
1919
|
+
}
|
|
1920
|
+
if (children.length + count + stack.length - ancestors.size > maxNodes)
|
|
1921
|
+
throw new Error("Expression AST exceeds node limit");
|
|
1922
|
+
visit(node);
|
|
1923
|
+
for (let index = children.length - 1; index >= 0; index--)
|
|
1924
|
+
stack.push({ node: children[index], depth: depth + 1 });
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1928
|
+
// dist/expressions/evaluation-error.js
|
|
1929
|
+
var _a;
|
|
1930
|
+
var brand = /* @__PURE__ */ Symbol.for("@math.gl/core/ExpressionEvaluationError");
|
|
1931
|
+
var codes = ["domain", "conversion", "overflow", "divisionByZero"];
|
|
1932
|
+
var ExpressionEvaluationError = class extends Error {
|
|
1933
|
+
constructor(code, message) {
|
|
1934
|
+
super(message);
|
|
1935
|
+
this.code = code;
|
|
1936
|
+
this.name = "ExpressionEvaluationError";
|
|
1937
|
+
this[_a] = true;
|
|
1938
|
+
if (!codes.includes(code))
|
|
1939
|
+
throw new TypeError("Unknown Arrow evaluation error code");
|
|
1940
|
+
}
|
|
1941
|
+
};
|
|
1942
|
+
_a = brand;
|
|
1943
|
+
function isExpressionEvaluationError(error) {
|
|
1944
|
+
return typeof error === "object" && error !== null && error[brand] === true && codes.includes(error.code);
|
|
1945
|
+
}
|
|
1946
|
+
|
|
1947
|
+
// dist/arrow/buffers.js
|
|
1948
|
+
var import_apache_arrow = require("apache-arrow");
|
|
1949
|
+
|
|
1950
|
+
// dist/arrow/float16.js
|
|
1951
|
+
var import_core = require("@math.gl/core");
|
|
1952
|
+
function requireFloat16() {
|
|
1953
|
+
if (!(0, import_core.isFloat16ArrayConstructor)((0, import_core.getFloat16ArrayConstructor)()))
|
|
1954
|
+
throw new Error("Arrow Float16 expressions require native Float16Array support");
|
|
1955
|
+
}
|
|
1956
|
+
function float16Values(values) {
|
|
1957
|
+
if ((0, import_core.isFloat16Array)(values))
|
|
1958
|
+
return values;
|
|
1959
|
+
requireFloat16();
|
|
1960
|
+
const Constructor = (0, import_core.getFloat16ArrayConstructor)();
|
|
1961
|
+
return new Constructor(values.buffer, values.byteOffset, values.length);
|
|
1962
|
+
}
|
|
1963
|
+
function roundFloat16() {
|
|
1964
|
+
const cell = float16Values(new Uint16Array(1));
|
|
1965
|
+
return (value) => {
|
|
1966
|
+
cell[0] = value;
|
|
1967
|
+
return cell[0];
|
|
1968
|
+
};
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
// dist/arrow/buffers.js
|
|
1972
|
+
function valueType(type) {
|
|
1973
|
+
return import_apache_arrow.DataType.isDictionary(type) ? valueType(type.dictionary) : type;
|
|
1974
|
+
}
|
|
1975
|
+
function typeKey(type) {
|
|
1976
|
+
type = valueType(type);
|
|
1977
|
+
if (import_apache_arrow.DataType.isInt(type))
|
|
1978
|
+
return `${type.isSigned ? "i" : "u"}${type.bitWidth}`;
|
|
1979
|
+
if (import_apache_arrow.DataType.isFloat(type)) {
|
|
1980
|
+
if (type.precision === import_apache_arrow.Precision.HALF)
|
|
1981
|
+
requireFloat16();
|
|
1982
|
+
return `f${type.precision}`;
|
|
1983
|
+
}
|
|
1984
|
+
if (import_apache_arrow.DataType.isBool(type))
|
|
1985
|
+
return "bool";
|
|
1986
|
+
if (import_apache_arrow.DataType.isUtf8(type))
|
|
1987
|
+
return "utf8";
|
|
1988
|
+
if (import_apache_arrow.DataType.isDate(type) && type.unit === import_apache_arrow.DateUnit.DAY)
|
|
1989
|
+
return "date32";
|
|
1990
|
+
if (import_apache_arrow.DataType.isTimestamp(type) && type.unit === import_apache_arrow.TimeUnit.MICROSECOND)
|
|
1991
|
+
return `timestamp_us:${type.timezone ?? ""}`;
|
|
1992
|
+
if (import_apache_arrow.DataType.isBinary(type))
|
|
1993
|
+
return "binary";
|
|
1994
|
+
if (import_apache_arrow.DataType.isFixedSizeBinary(type))
|
|
1995
|
+
return `fixedsizebinary:${type.byteWidth}`;
|
|
1996
|
+
if (import_apache_arrow.DataType.isNull(type))
|
|
1997
|
+
return "null";
|
|
1998
|
+
throw new Error(`Unsupported Arrow expression type: ${type}`);
|
|
1999
|
+
}
|
|
2000
|
+
var ColumnReader = class _ColumnReader {
|
|
2001
|
+
constructor(vector) {
|
|
2002
|
+
this.vector = vector;
|
|
2003
|
+
this.ends = [];
|
|
2004
|
+
this.location = [null, 0];
|
|
2005
|
+
this.range = [null, 0, 0];
|
|
2006
|
+
this.half = /* @__PURE__ */ new Map();
|
|
2007
|
+
this.dictionaries = /* @__PURE__ */ new Map();
|
|
2008
|
+
this.decoder = new TextDecoder();
|
|
2009
|
+
let end = 0;
|
|
2010
|
+
for (const data of vector.data) {
|
|
2011
|
+
end += data.length;
|
|
2012
|
+
this.ends.push(end);
|
|
2013
|
+
if (import_apache_arrow.DataType.isFloat(data.type) && data.type.precision === import_apache_arrow.Precision.HALF)
|
|
2014
|
+
this.half.set(data, float16Values(data.values));
|
|
2015
|
+
if (import_apache_arrow.DataType.isDictionary(data.type))
|
|
2016
|
+
this.dictionaries.set(data, new _ColumnReader(data.dictionary));
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
/** Locates a chunk without constructing row objects. */
|
|
2020
|
+
chunk(row) {
|
|
2021
|
+
let index = 0;
|
|
2022
|
+
while (row >= this.ends[index])
|
|
2023
|
+
index++;
|
|
2024
|
+
this.location[0] = this.vector.data[index];
|
|
2025
|
+
this.location[1] = row - (index ? this.ends[index - 1] : 0);
|
|
2026
|
+
return this.location;
|
|
2027
|
+
}
|
|
2028
|
+
/** Tests validity with the physical bit offset. */
|
|
2029
|
+
isValid(row) {
|
|
2030
|
+
const [data, index] = this.chunk(row);
|
|
2031
|
+
if (import_apache_arrow.DataType.isNull(data.type))
|
|
2032
|
+
return false;
|
|
2033
|
+
const bit = data.offset + index;
|
|
2034
|
+
if (data.nullCount && !(data.nullBitmap[bit >> 3] & 1 << (bit & 7)))
|
|
2035
|
+
return false;
|
|
2036
|
+
return !import_apache_arrow.DataType.isDictionary(data.type) || this.dictionaries.get(data).isValid(Number(data.values[index]));
|
|
2037
|
+
}
|
|
2038
|
+
/** Reads a primitive; UTF-8 is decoded only for scalar callbacks or output materialization. */
|
|
2039
|
+
get(row) {
|
|
2040
|
+
if (!this.isValid(row))
|
|
2041
|
+
return null;
|
|
2042
|
+
const [data, index] = this.chunk(row);
|
|
2043
|
+
if (import_apache_arrow.DataType.isDictionary(data.type))
|
|
2044
|
+
return this.dictionaries.get(data).get(Number(data.values[index]));
|
|
2045
|
+
if (import_apache_arrow.DataType.isUtf8(data.type))
|
|
2046
|
+
return this.decoder.decode(data.values.subarray(data.valueOffsets[index], data.valueOffsets[index + 1]));
|
|
2047
|
+
if (import_apache_arrow.DataType.isBinary(data.type))
|
|
2048
|
+
return data.values.subarray(data.valueOffsets[index], data.valueOffsets[index + 1]);
|
|
2049
|
+
if (import_apache_arrow.DataType.isFixedSizeBinary(data.type))
|
|
2050
|
+
return data.values.subarray(data.stride * index, data.stride * (index + 1));
|
|
2051
|
+
if (import_apache_arrow.DataType.isBool(data.type)) {
|
|
2052
|
+
const bit = data.offset + index;
|
|
2053
|
+
return Boolean(data.values[bit >> 3] & 1 << (bit & 7));
|
|
2054
|
+
}
|
|
2055
|
+
return this.half.has(data) ? this.half.get(data)[index] : data.values[index];
|
|
2056
|
+
}
|
|
2057
|
+
/** Resolves UTF-8 buffers, including dictionary values, without decoding. */
|
|
2058
|
+
utf8(row) {
|
|
2059
|
+
const [data, index] = this.chunk(row);
|
|
2060
|
+
if (import_apache_arrow.DataType.isDictionary(data.type))
|
|
2061
|
+
return this.dictionaries.get(data).utf8(Number(data.values[index]));
|
|
2062
|
+
this.range[0] = data.values;
|
|
2063
|
+
this.range[1] = Number(data.valueOffsets[index]);
|
|
2064
|
+
this.range[2] = Number(data.valueOffsets[index + 1]);
|
|
2065
|
+
return this.range;
|
|
2066
|
+
}
|
|
2067
|
+
};
|
|
2068
|
+
function createValueValidator(inputType) {
|
|
2069
|
+
const type = valueType(inputType), key = typeKey(type);
|
|
2070
|
+
if (import_apache_arrow.DataType.isInt(type)) {
|
|
2071
|
+
if (type.bitWidth === 64) {
|
|
2072
|
+
const bits = BigInt(type.bitWidth), min2 = type.isSigned ? -(1n << bits - 1n) : 0n;
|
|
2073
|
+
const max2 = type.isSigned ? (1n << bits - 1n) - 1n : (1n << bits) - 1n;
|
|
2074
|
+
return (value) => {
|
|
2075
|
+
if (value === null)
|
|
2076
|
+
return;
|
|
2077
|
+
if (typeof value !== "bigint")
|
|
2078
|
+
throw new Error("64-bit integers require bigint values");
|
|
2079
|
+
if (value < min2 || value > max2)
|
|
2080
|
+
throw new ExpressionEvaluationError("overflow", "Integer expression overflow");
|
|
2081
|
+
};
|
|
2082
|
+
}
|
|
2083
|
+
const min = type.isSigned ? -(2 ** (type.bitWidth - 1)) : 0;
|
|
2084
|
+
const max = type.isSigned ? 2 ** (type.bitWidth - 1) - 1 : 2 ** type.bitWidth - 1;
|
|
2085
|
+
return (value) => {
|
|
2086
|
+
if (value === null)
|
|
2087
|
+
return;
|
|
2088
|
+
if (typeof value !== "number")
|
|
2089
|
+
throw new TypeError("Integer expressions require numeric values");
|
|
2090
|
+
if (!Number.isInteger(value))
|
|
2091
|
+
throw new ExpressionEvaluationError("conversion", "Integer expression requires integral numbers");
|
|
2092
|
+
if (value < min || value > max)
|
|
2093
|
+
throw new ExpressionEvaluationError("overflow", "Integer expression overflow");
|
|
2094
|
+
};
|
|
2095
|
+
}
|
|
2096
|
+
if (key === "date32") {
|
|
2097
|
+
return (value) => {
|
|
2098
|
+
if (value === null)
|
|
2099
|
+
return;
|
|
2100
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < -2147483648 || value > 2147483647)
|
|
2101
|
+
throw new ExpressionEvaluationError("overflow", "Invalid Date32 epoch day");
|
|
2102
|
+
};
|
|
2103
|
+
}
|
|
2104
|
+
if (key.startsWith("timestamp_us:")) {
|
|
2105
|
+
return (value) => {
|
|
2106
|
+
if (value === null)
|
|
2107
|
+
return;
|
|
2108
|
+
if (typeof value !== "bigint" || value !== BigInt.asIntN(64, value))
|
|
2109
|
+
throw new ExpressionEvaluationError("overflow", "Invalid timestamp epoch microseconds");
|
|
2110
|
+
};
|
|
2111
|
+
}
|
|
2112
|
+
if (import_apache_arrow.DataType.isBinary(type) || import_apache_arrow.DataType.isFixedSizeBinary(type)) {
|
|
2113
|
+
return (value) => {
|
|
2114
|
+
if (value === null)
|
|
2115
|
+
return;
|
|
2116
|
+
if (!(value instanceof Uint8Array) || import_apache_arrow.DataType.isFixedSizeBinary(type) && value.byteLength !== type.byteWidth)
|
|
2117
|
+
throw new Error(`Invalid value for ${type}`);
|
|
2118
|
+
};
|
|
2119
|
+
}
|
|
2120
|
+
const expected = import_apache_arrow.DataType.isFloat(type) ? "number" : key === "bool" ? "boolean" : "string";
|
|
2121
|
+
return (value) => {
|
|
2122
|
+
if (value !== null && (key === "null" || typeof value !== expected))
|
|
2123
|
+
throw new Error(`Invalid value for ${type}`);
|
|
2124
|
+
};
|
|
2125
|
+
}
|
|
2126
|
+
function validateValue(value, type) {
|
|
2127
|
+
createValueValidator(type)(value);
|
|
2128
|
+
}
|
|
2129
|
+
|
|
2130
|
+
// dist/arrow/cast.js
|
|
2131
|
+
var import_apache_arrow3 = require("apache-arrow");
|
|
2132
|
+
var import_utf8 = require("@math.gl/core/utf8");
|
|
2133
|
+
|
|
2134
|
+
// dist/arrow/temporal-cast.js
|
|
2135
|
+
var import_apache_arrow2 = require("apache-arrow");
|
|
2136
|
+
var DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
2137
|
+
var TIMESTAMP = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,6}))?(Z|[+-]\d{2}:\d{2})?$/;
|
|
2138
|
+
function bindTemporalCast(source, target) {
|
|
2139
|
+
const dateOnly = target === "date";
|
|
2140
|
+
const zoned = target === "timestamp_utc";
|
|
2141
|
+
const type = dateOnly ? new import_apache_arrow2.DateDay() : new import_apache_arrow2.TimestampMicrosecond(zoned ? "UTC" : void 0);
|
|
2142
|
+
const sourceKey = typeKey(source);
|
|
2143
|
+
if (sourceKey !== "utf8" && sourceKey !== "null" && sourceKey !== typeKey(type))
|
|
2144
|
+
throw new Error("Temporal CAST requires UTF8 or an input of the requested Arrow type");
|
|
2145
|
+
return {
|
|
2146
|
+
type,
|
|
2147
|
+
textInput: false,
|
|
2148
|
+
evaluate(value) {
|
|
2149
|
+
return typeof value === "string" ? parseTemporalValue(value, dateOnly, zoned) : value;
|
|
2150
|
+
}
|
|
2151
|
+
};
|
|
2152
|
+
}
|
|
2153
|
+
function parseTemporalValue(text, dateOnly, zoned) {
|
|
2154
|
+
const match = (dateOnly ? DATE : TIMESTAMP).exec(text.trim());
|
|
2155
|
+
const invalid = () => new ExpressionEvaluationError("conversion", "Invalid text for temporal CAST");
|
|
2156
|
+
if (!match)
|
|
2157
|
+
throw invalid();
|
|
2158
|
+
const [, yearText, monthText, dayText, hourText, minuteText, secondText, fraction, zone] = match;
|
|
2159
|
+
const year = Number(yearText);
|
|
2160
|
+
const month = Number(monthText);
|
|
2161
|
+
const day = Number(dayText);
|
|
2162
|
+
const calendar = /* @__PURE__ */ new Date(0);
|
|
2163
|
+
calendar.setUTCFullYear(year, month - 1, day);
|
|
2164
|
+
if (calendar.getUTCFullYear() !== year || calendar.getUTCMonth() !== month - 1 || calendar.getUTCDate() !== day)
|
|
2165
|
+
throw invalid();
|
|
2166
|
+
if (dateOnly)
|
|
2167
|
+
return calendar.getTime() / 864e5;
|
|
2168
|
+
const hour = Number(hourText);
|
|
2169
|
+
const minute = Number(minuteText);
|
|
2170
|
+
const second = Number(secondText);
|
|
2171
|
+
if (hour > 23 || minute > 59 || second > 59 || zoned !== Boolean(zone))
|
|
2172
|
+
throw invalid();
|
|
2173
|
+
let offsetMinutes = 0;
|
|
2174
|
+
if (zone && zone !== "Z") {
|
|
2175
|
+
const offsetHour = Number(zone.slice(1, 3));
|
|
2176
|
+
const offsetMinute = Number(zone.slice(4));
|
|
2177
|
+
if (offsetHour > 23 || offsetMinute > 59)
|
|
2178
|
+
throw invalid();
|
|
2179
|
+
offsetMinutes = (zone.startsWith("-") ? -1 : 1) * (offsetHour * 60 + offsetMinute);
|
|
2180
|
+
}
|
|
2181
|
+
const seconds = hour * 3600 + minute * 60 + second - offsetMinutes * 60;
|
|
2182
|
+
return BigInt(calendar.getTime() + seconds * 1e3) * 1000n + BigInt((fraction ?? "").padEnd(6, "0"));
|
|
2183
|
+
}
|
|
2184
|
+
|
|
2185
|
+
// dist/arrow/cast.js
|
|
2186
|
+
var CAST_TYPES = {
|
|
2187
|
+
int8: import_apache_arrow3.Int8,
|
|
2188
|
+
int16: import_apache_arrow3.Int16,
|
|
2189
|
+
int32: import_apache_arrow3.Int32,
|
|
2190
|
+
int64: import_apache_arrow3.Int64,
|
|
2191
|
+
uint8: import_apache_arrow3.Uint8,
|
|
2192
|
+
uint16: import_apache_arrow3.Uint16,
|
|
2193
|
+
uint32: import_apache_arrow3.Uint32,
|
|
2194
|
+
uint64: import_apache_arrow3.Uint64,
|
|
2195
|
+
float16: import_apache_arrow3.Float16,
|
|
2196
|
+
float32: import_apache_arrow3.Float32,
|
|
2197
|
+
float64: import_apache_arrow3.Float64,
|
|
2198
|
+
bool: import_apache_arrow3.Bool,
|
|
2199
|
+
utf8: import_apache_arrow3.Utf8
|
|
2200
|
+
};
|
|
2201
|
+
function bindCast(source, target) {
|
|
2202
|
+
if (target === "date" || target === "timestamp" || target === "timestamp_utc")
|
|
2203
|
+
return bindTemporalCast(source, target);
|
|
2204
|
+
const binaryTarget = typeof target === "string" && /^(varbinary|fixedsizebinary):([1-9]\d*)$/.exec(target);
|
|
2205
|
+
if (target === "binary" || binaryTarget) {
|
|
2206
|
+
const byteWidth = binaryTarget ? Number(binaryTarget[2]) : void 0;
|
|
2207
|
+
if (byteWidth !== void 0 && byteWidth > 2147483647)
|
|
2208
|
+
throw new Error("Binary CAST requires a positive 32-bit byte width");
|
|
2209
|
+
const type2 = binaryTarget && binaryTarget[1] === "fixedsizebinary" ? new import_apache_arrow3.FixedSizeBinary(byteWidth) : new import_apache_arrow3.Binary();
|
|
2210
|
+
const sourceKey2 = typeKey(source);
|
|
2211
|
+
const textInput2 = sourceKey2 === "utf8";
|
|
2212
|
+
if (!textInput2 && sourceKey2 !== "null" && sourceKey2 !== "binary" && !sourceKey2.startsWith("fixedsizebinary:"))
|
|
2213
|
+
throw new Error("Binary CAST requires binary or UTF8 input");
|
|
2214
|
+
return {
|
|
2215
|
+
type: type2,
|
|
2216
|
+
textInput: textInput2,
|
|
2217
|
+
evaluate(value, range) {
|
|
2218
|
+
const bytes2 = range ? range[0].subarray(range[1], range[2]) : value;
|
|
2219
|
+
if (bytes2 === null || byteWidth === void 0)
|
|
2220
|
+
return bytes2;
|
|
2221
|
+
if (bytes2.byteLength > byteWidth)
|
|
2222
|
+
throw new ExpressionEvaluationError("overflow", `Binary CAST exceeds ${byteWidth} bytes`);
|
|
2223
|
+
if (import_apache_arrow3.DataType.isFixedSizeBinary(type2) && bytes2.byteLength < byteWidth) {
|
|
2224
|
+
const padded = new Uint8Array(byteWidth);
|
|
2225
|
+
padded.set(bytes2);
|
|
2226
|
+
return padded;
|
|
2227
|
+
}
|
|
2228
|
+
return bytes2;
|
|
2229
|
+
}
|
|
2230
|
+
};
|
|
2231
|
+
}
|
|
2232
|
+
const dictionary = target === "dictionary_utf8_int32";
|
|
2233
|
+
if (dictionary)
|
|
2234
|
+
target = "utf8";
|
|
2235
|
+
if (!isColumnarScalarType(target))
|
|
2236
|
+
throw new Error("Unsupported scalar CAST target");
|
|
2237
|
+
const type = new CAST_TYPES[target]();
|
|
2238
|
+
const integer = import_apache_arrow3.DataType.isInt(type);
|
|
2239
|
+
const wideInteger = integer && type.bitWidth === 64;
|
|
2240
|
+
const sourceKey = typeKey(source);
|
|
2241
|
+
const textInput = sourceKey === "utf8";
|
|
2242
|
+
if (sourceKey === "date32" || sourceKey.startsWith("timestamp_us:"))
|
|
2243
|
+
throw new Error("Temporal CAST requires a matching temporal target");
|
|
2244
|
+
if (sourceKey === "binary" || sourceKey.startsWith("fixedsizebinary:"))
|
|
2245
|
+
throw new Error("CAST from binary requires a binary target");
|
|
2246
|
+
if (sourceKey !== "null" && (sourceKey === "bool" && !["bool", "utf8"].includes(target) || target === "bool" && !["bool", "utf8"].includes(sourceKey)))
|
|
2247
|
+
throw new Error("CAST between Boolean and numeric values is not supported");
|
|
2248
|
+
const validate = createValueValidator(type);
|
|
2249
|
+
const halfRound = target === "float16" ? roundFloat16() : void 0;
|
|
2250
|
+
const encoder2 = new TextEncoder();
|
|
2251
|
+
return {
|
|
2252
|
+
type: dictionary ? new import_apache_arrow3.Dictionary(new import_apache_arrow3.Utf8(), new import_apache_arrow3.Int32()) : type,
|
|
2253
|
+
textInput,
|
|
2254
|
+
evaluate(value, range) {
|
|
2255
|
+
if (value === null && !range)
|
|
2256
|
+
return null;
|
|
2257
|
+
let result;
|
|
2258
|
+
if (target === "utf8") {
|
|
2259
|
+
if (typeof value === "boolean")
|
|
2260
|
+
return value ? "TRUE" : "FALSE";
|
|
2261
|
+
return String(value);
|
|
2262
|
+
}
|
|
2263
|
+
if (textInput) {
|
|
2264
|
+
const input = range ?? (() => {
|
|
2265
|
+
const bytes3 = encoder2.encode(value);
|
|
2266
|
+
return [bytes3, 0, bytes3.length];
|
|
2267
|
+
})();
|
|
2268
|
+
const [bytes2, start, end] = input;
|
|
2269
|
+
result = target === "bool" ? (0, import_utf8.parseUTF8Boolean)(bytes2, start, end) ?? null : integer ? (0, import_utf8.parseUTF8BigInt)(bytes2, start, end) ?? null : (0, import_utf8.parseUTF8Number)(bytes2, start, end) ?? null;
|
|
2270
|
+
if (result === null)
|
|
2271
|
+
throw new ExpressionEvaluationError("conversion", `Invalid text for CAST to ${target}`);
|
|
2272
|
+
if (integer && !wideInteger)
|
|
2273
|
+
result = Number(result);
|
|
2274
|
+
} else if (target === "bool")
|
|
2275
|
+
result = value;
|
|
2276
|
+
else if (wideInteger) {
|
|
2277
|
+
if (typeof value === "number" && !Number.isFinite(value))
|
|
2278
|
+
throw new ExpressionEvaluationError("conversion", "Non-finite integer CAST");
|
|
2279
|
+
result = typeof value === "bigint" ? value : BigInt(Math.trunc(value));
|
|
2280
|
+
} else if (integer)
|
|
2281
|
+
result = Math.trunc(Number(value));
|
|
2282
|
+
else
|
|
2283
|
+
result = Number(value);
|
|
2284
|
+
if (halfRound)
|
|
2285
|
+
result = halfRound(result);
|
|
2286
|
+
if (target === "float32")
|
|
2287
|
+
result = Math.fround(result);
|
|
2288
|
+
if ((target === "float16" || target === "float32" || target === "float64") && !Number.isFinite(result))
|
|
2289
|
+
throw new ExpressionEvaluationError("overflow", "Floating CAST overflow or non-finite value");
|
|
2290
|
+
validate(result);
|
|
2291
|
+
return result;
|
|
2292
|
+
}
|
|
2293
|
+
};
|
|
2294
|
+
}
|
|
2295
|
+
|
|
2296
|
+
// dist/arrow/like.js
|
|
2297
|
+
function compileUTF8Like(pattern, insensitive, escape) {
|
|
2298
|
+
if (escape !== void 0 && [...escape].length !== 1) {
|
|
2299
|
+
throw new Error("LIKE ESCAPE requires one character");
|
|
2300
|
+
}
|
|
2301
|
+
const tokens = [];
|
|
2302
|
+
let escaped = false;
|
|
2303
|
+
for (const character of pattern) {
|
|
2304
|
+
if (!escaped && character === escape) {
|
|
2305
|
+
escaped = true;
|
|
2306
|
+
continue;
|
|
2307
|
+
}
|
|
2308
|
+
tokens.push(!escaped && character === "%" ? -1 : !escaped && character === "_" ? -2 : fold(character.codePointAt(0), insensitive));
|
|
2309
|
+
escaped = false;
|
|
2310
|
+
}
|
|
2311
|
+
if (escaped)
|
|
2312
|
+
throw new Error("LIKE pattern ends with ESCAPE");
|
|
2313
|
+
return (bytes2, start, end) => {
|
|
2314
|
+
let offset = start;
|
|
2315
|
+
let token = 0;
|
|
2316
|
+
let wildcard = -1;
|
|
2317
|
+
let retry = start;
|
|
2318
|
+
while (offset < end) {
|
|
2319
|
+
if (tokens[token] === -1) {
|
|
2320
|
+
wildcard = token++;
|
|
2321
|
+
retry = offset;
|
|
2322
|
+
continue;
|
|
2323
|
+
}
|
|
2324
|
+
const [point, next] = readCodePoint(bytes2, offset);
|
|
2325
|
+
if (tokens[token] === -2 || tokens[token] === fold(point, insensitive)) {
|
|
2326
|
+
token++;
|
|
2327
|
+
offset = next;
|
|
2328
|
+
} else if (wildcard >= 0) {
|
|
2329
|
+
retry = readCodePoint(bytes2, retry)[1];
|
|
2330
|
+
offset = retry;
|
|
2331
|
+
token = wildcard + 1;
|
|
2332
|
+
} else {
|
|
2333
|
+
return false;
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
while (tokens[token] === -1)
|
|
2337
|
+
token++;
|
|
2338
|
+
return token === tokens.length;
|
|
2339
|
+
};
|
|
2340
|
+
}
|
|
2341
|
+
function fold(point, insensitive) {
|
|
2342
|
+
if (!insensitive)
|
|
2343
|
+
return point;
|
|
2344
|
+
if (point < 128)
|
|
2345
|
+
return point >= 65 && point <= 90 ? point + 32 : point;
|
|
2346
|
+
const folded = String.fromCodePoint(point).toLowerCase();
|
|
2347
|
+
return folded.length === 1 && folded.charCodeAt(0) < 128 ? folded.charCodeAt(0) : folded;
|
|
2348
|
+
}
|
|
2349
|
+
function readCodePoint(bytes2, offset) {
|
|
2350
|
+
const first = bytes2[offset++];
|
|
2351
|
+
if (first < 128)
|
|
2352
|
+
return [first, offset];
|
|
2353
|
+
const count = first < 224 ? 1 : first < 240 ? 2 : 3;
|
|
2354
|
+
let point = first & (count === 1 ? 31 : count === 2 ? 15 : 7);
|
|
2355
|
+
for (let index = 0; index < count; index++)
|
|
2356
|
+
point = point << 6 | bytes2[offset++] & 63;
|
|
2357
|
+
return [point, offset];
|
|
2358
|
+
}
|
|
2359
|
+
|
|
2360
|
+
// dist/arrow/signature.js
|
|
2361
|
+
var import_apache_arrow4 = require("apache-arrow");
|
|
2362
|
+
function physicalKey(type) {
|
|
2363
|
+
var _a2;
|
|
2364
|
+
try {
|
|
2365
|
+
return import_apache_arrow4.DataType.isDictionary(type) ? `dictionary:${typeKey(type.indices)}:${typeKey(type.dictionary)}:${type.isOrdered}` : typeKey(type);
|
|
2366
|
+
} catch {
|
|
2367
|
+
const properties = [
|
|
2368
|
+
"bitWidth",
|
|
2369
|
+
"isSigned",
|
|
2370
|
+
"precision",
|
|
2371
|
+
"scale",
|
|
2372
|
+
"unit",
|
|
2373
|
+
"timezone",
|
|
2374
|
+
"byteWidth",
|
|
2375
|
+
"listSize",
|
|
2376
|
+
"keysSorted",
|
|
2377
|
+
"isOrdered",
|
|
2378
|
+
"mode"
|
|
2379
|
+
].map((key) => type[key]);
|
|
2380
|
+
return JSON.stringify([
|
|
2381
|
+
type.typeId,
|
|
2382
|
+
properties,
|
|
2383
|
+
import_apache_arrow4.DataType.isUnion(type) ? Array.from(type.typeIds) : null,
|
|
2384
|
+
import_apache_arrow4.DataType.isDictionary(type) ? [physicalKey(type.indices), physicalKey(type.dictionary), type.isOrdered] : null,
|
|
2385
|
+
(_a2 = type.children) == null ? void 0 : _a2.map((field) => [
|
|
2386
|
+
field.name,
|
|
2387
|
+
physicalKey(field.type),
|
|
2388
|
+
field.nullable,
|
|
2389
|
+
extensionKey(field)
|
|
2390
|
+
])
|
|
2391
|
+
]);
|
|
2392
|
+
}
|
|
2393
|
+
}
|
|
2394
|
+
function extensionKey(field) {
|
|
2395
|
+
return JSON.stringify([
|
|
2396
|
+
field.metadata.get("ARROW:extension:name"),
|
|
2397
|
+
field.metadata.get("ARROW:extension:metadata")
|
|
2398
|
+
]);
|
|
2399
|
+
}
|
|
2400
|
+
|
|
2401
|
+
// dist/arrow/registry.js
|
|
2402
|
+
function createFunctionLookup(declarations = []) {
|
|
2403
|
+
var _a2;
|
|
2404
|
+
const functions = /* @__PURE__ */ new Map();
|
|
2405
|
+
for (const declaration of declarations) {
|
|
2406
|
+
const name2 = declaration == null ? void 0 : declaration.name;
|
|
2407
|
+
if (typeof name2 !== "string" || !/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name2))
|
|
2408
|
+
throw new Error("Invalid Arrow function name");
|
|
2409
|
+
if ([
|
|
2410
|
+
"TRY",
|
|
2411
|
+
"COALESCE",
|
|
2412
|
+
"array",
|
|
2413
|
+
"array_float16",
|
|
2414
|
+
"array_float32",
|
|
2415
|
+
"array_float64",
|
|
2416
|
+
"array_uint8",
|
|
2417
|
+
"PACK_UNORM8",
|
|
2418
|
+
"EPOCH_MS",
|
|
2419
|
+
"LIST_EXTRACT",
|
|
2420
|
+
"STRUCT_EXTRACT",
|
|
2421
|
+
"cast",
|
|
2422
|
+
"in",
|
|
2423
|
+
"isNull",
|
|
2424
|
+
"isNotNull",
|
|
2425
|
+
"parseNumber",
|
|
2426
|
+
"parseBigInt",
|
|
2427
|
+
"parseBoolean",
|
|
2428
|
+
"toFloat64",
|
|
2429
|
+
"toInt32",
|
|
2430
|
+
"toInt64"
|
|
2431
|
+
].includes(name2))
|
|
2432
|
+
throw new Error(`Reserved Arrow function: ${name2}`);
|
|
2433
|
+
if (!Array.isArray(declaration.prototypes) || !declaration.prototypes.length)
|
|
2434
|
+
throw new Error("Arrow functions require one or more prototypes");
|
|
2435
|
+
const prototypes = [...((_a2 = functions.get(name2)) == null ? void 0 : _a2.prototypes) ?? []];
|
|
2436
|
+
for (const prototype of declaration.prototypes) {
|
|
2437
|
+
if (!prototype || typeof prototype !== "object" || !Array.isArray(prototype.arguments) || !prototype.result || typeof prototype.evaluate !== "function")
|
|
2438
|
+
throw new Error("Arrow prototypes require argument fields, a result field and evaluate");
|
|
2439
|
+
if (prototype.nullHandling !== void 0 && !["propagate", "call"].includes(prototype.nullHandling))
|
|
2440
|
+
throw new Error("Unsupported null handling");
|
|
2441
|
+
if (prototype.evaluateScalar !== void 0 && typeof prototype.evaluateScalar !== "function")
|
|
2442
|
+
throw new Error("Arrow function evaluateScalar must be a function");
|
|
2443
|
+
const key = JSON.stringify(prototype.arguments.map((field) => [
|
|
2444
|
+
physicalKey(field.type),
|
|
2445
|
+
[...field.metadata].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)
|
|
2446
|
+
]));
|
|
2447
|
+
if (prototypes.some((existing) => JSON.stringify(existing.arguments.map((field) => [
|
|
2448
|
+
physicalKey(field.type),
|
|
2449
|
+
[...field.metadata].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)
|
|
2450
|
+
])) === key))
|
|
2451
|
+
throw new Error(`Duplicate Arrow function signature: ${name2}`);
|
|
2452
|
+
prototypes.push(Object.freeze({ ...prototype, arguments: Object.freeze([...prototype.arguments]) }));
|
|
2453
|
+
}
|
|
2454
|
+
functions.set(name2, Object.freeze({ name: name2, prototypes: Object.freeze(prototypes) }));
|
|
2455
|
+
}
|
|
2456
|
+
return functions;
|
|
2457
|
+
}
|
|
2458
|
+
|
|
2459
|
+
// dist/arrow/list-values.js
|
|
2460
|
+
var import_apache_arrow5 = require("apache-arrow");
|
|
2461
|
+
function isTupleType(type) {
|
|
2462
|
+
if (!import_apache_arrow5.DataType.isFixedSizeList(type))
|
|
2463
|
+
return false;
|
|
2464
|
+
const child = type.children[0];
|
|
2465
|
+
if (!Number.isSafeInteger(type.listSize) || type.listSize <= 0 || !child || child.nullable)
|
|
2466
|
+
return false;
|
|
2467
|
+
if (!import_apache_arrow5.DataType.isInt(child.type) && !import_apache_arrow5.DataType.isFloat(child.type))
|
|
2468
|
+
return false;
|
|
2469
|
+
typeKey(child.type);
|
|
2470
|
+
return true;
|
|
2471
|
+
}
|
|
2472
|
+
function nullTupleVector(type, length) {
|
|
2473
|
+
const childType = type.children[0].type;
|
|
2474
|
+
const child = (0, import_apache_arrow5.makeData)({
|
|
2475
|
+
type: childType,
|
|
2476
|
+
length: length * type.listSize,
|
|
2477
|
+
data: new childType.ArrayType(length * type.listSize)
|
|
2478
|
+
});
|
|
2479
|
+
return (0, import_apache_arrow5.makeVector)((0, import_apache_arrow5.makeData)({
|
|
2480
|
+
type,
|
|
2481
|
+
length,
|
|
2482
|
+
child,
|
|
2483
|
+
nullCount: length,
|
|
2484
|
+
nullBitmap: new Uint8Array(Math.ceil(length / 8))
|
|
2485
|
+
}));
|
|
2486
|
+
}
|
|
2487
|
+
function maskTupleVector(vector, active) {
|
|
2488
|
+
if (!active.some((value) => !value))
|
|
2489
|
+
return vector;
|
|
2490
|
+
let start = 0;
|
|
2491
|
+
const chunks = vector.data.map((data) => {
|
|
2492
|
+
const bitmap = new Uint8Array(Math.ceil((data.offset + data.length) / 8));
|
|
2493
|
+
let nullCount = 0;
|
|
2494
|
+
for (let row = 0; row < data.length; row++) {
|
|
2495
|
+
if (active[start + row] && data.getValid(row)) {
|
|
2496
|
+
const bit = data.offset + row;
|
|
2497
|
+
bitmap[bit >> 3] |= 1 << (bit & 7);
|
|
2498
|
+
} else
|
|
2499
|
+
nullCount++;
|
|
2500
|
+
}
|
|
2501
|
+
start += data.length;
|
|
2502
|
+
const buffers = data.buffers;
|
|
2503
|
+
buffers[2] = bitmap;
|
|
2504
|
+
return data.clone(data.type, data.offset, data.length, nullCount, buffers);
|
|
2505
|
+
});
|
|
2506
|
+
return new import_apache_arrow5.Vector(chunks);
|
|
2507
|
+
}
|
|
2508
|
+
function validateTupleVector(vector, field, active) {
|
|
2509
|
+
const type = field.type;
|
|
2510
|
+
let start = 0;
|
|
2511
|
+
for (const data of vector.data) {
|
|
2512
|
+
const child = data.children[0];
|
|
2513
|
+
if (!child || child.length < data.length * type.listSize)
|
|
2514
|
+
throw new Error("Invalid fixed-size list child length");
|
|
2515
|
+
for (let row = 0; row < data.length; row++) {
|
|
2516
|
+
if (!active[start + row])
|
|
2517
|
+
continue;
|
|
2518
|
+
if (!data.getValid(row)) {
|
|
2519
|
+
if (!field.nullable)
|
|
2520
|
+
throw new Error(`Function ${field.name} returned null for a non-nullable result`);
|
|
2521
|
+
continue;
|
|
2522
|
+
}
|
|
2523
|
+
for (let component = 0; component < type.listSize; component++) {
|
|
2524
|
+
if (!child.getValid(row * type.listSize + component))
|
|
2525
|
+
throw new Error("Fixed-size list result contains a null non-nullable component");
|
|
2526
|
+
}
|
|
2527
|
+
}
|
|
2528
|
+
start += data.length;
|
|
2529
|
+
}
|
|
2530
|
+
}
|
|
2531
|
+
function selectTupleVectors(type, yes, no, selected, active) {
|
|
2532
|
+
const result = nullTupleVector(type, active.length);
|
|
2533
|
+
const data = result.data[0];
|
|
2534
|
+
const child = data.children[0];
|
|
2535
|
+
const values = import_apache_arrow5.DataType.isFloat(child.type) && child.type.precision === import_apache_arrow5.Precision.HALF ? float16Values(child.values) : child.values;
|
|
2536
|
+
const a = new ColumnReader(yes.getChildAt(0));
|
|
2537
|
+
const b = new ColumnReader(no.getChildAt(0));
|
|
2538
|
+
const aValid = new ColumnReader(yes);
|
|
2539
|
+
const bValid = new ColumnReader(no);
|
|
2540
|
+
for (let row = 0; row < active.length; row++) {
|
|
2541
|
+
if (!active[row])
|
|
2542
|
+
continue;
|
|
2543
|
+
const reader = selected[row] ? a : b;
|
|
2544
|
+
if (!(selected[row] ? aValid : bValid).isValid(row))
|
|
2545
|
+
continue;
|
|
2546
|
+
data.setValid(row, true);
|
|
2547
|
+
for (let component = 0; component < type.listSize; component++) {
|
|
2548
|
+
const index = row * type.listSize + component;
|
|
2549
|
+
values[index] = reader.get(index);
|
|
2550
|
+
}
|
|
2551
|
+
}
|
|
2552
|
+
return result;
|
|
2553
|
+
}
|
|
2554
|
+
function compileArrayLiteral(ast, type) {
|
|
2555
|
+
if (!import_apache_arrow5.DataType.isList(type) && !import_apache_arrow5.DataType.isFixedSizeList(type))
|
|
2556
|
+
throw new Error("Array literals require a declared List or FixedSizeList argument");
|
|
2557
|
+
const listType = type;
|
|
2558
|
+
const child = listType.children[0];
|
|
2559
|
+
if (import_apache_arrow5.DataType.isDictionary(child.type))
|
|
2560
|
+
throw new Error("Array literal elements require primitive types");
|
|
2561
|
+
typeKey(child.type);
|
|
2562
|
+
if (ast.elements.length > 4096)
|
|
2563
|
+
throw new Error("Array literal exceeds 4096 elements");
|
|
2564
|
+
if (import_apache_arrow5.DataType.isFixedSizeList(type) && ast.elements.length !== type.listSize)
|
|
2565
|
+
throw new Error("Array literal length must match its FixedSizeList type");
|
|
2566
|
+
if (import_apache_arrow5.DataType.isFixedSizeList(type) && !isTupleType(type))
|
|
2567
|
+
throw new Error("Fixed-size array literals require non-nullable numeric elements");
|
|
2568
|
+
const round = import_apache_arrow5.DataType.isFloat(child.type) ? child.type.precision === import_apache_arrow5.Precision.HALF ? roundFloat16() : child.type.precision === import_apache_arrow5.Precision.SINGLE ? Math.fround : void 0 : void 0;
|
|
2569
|
+
const values = Array.from(ast.elements, (element) => {
|
|
2570
|
+
var _a2;
|
|
2571
|
+
let value;
|
|
2572
|
+
if ((element == null ? void 0 : element.type) === "Literal")
|
|
2573
|
+
value = element.value;
|
|
2574
|
+
else if ((element == null ? void 0 : element.type) === "UnaryExpression" && ["+", "-"].includes(element.operator) && ((_a2 = element.argument) == null ? void 0 : _a2.type) === "Literal" && typeof element.argument.value === "number")
|
|
2575
|
+
value = element.operator === "-" ? -element.argument.value : element.argument.value;
|
|
2576
|
+
else
|
|
2577
|
+
throw new Error("Array elements must be scalar literals");
|
|
2578
|
+
if (value === null && !child.nullable)
|
|
2579
|
+
throw new Error("Array literal contains a null non-nullable element");
|
|
2580
|
+
validateValue(value, child.type);
|
|
2581
|
+
if (round && typeof value === "number") {
|
|
2582
|
+
const rounded = round(value);
|
|
2583
|
+
if (Number.isFinite(value) && !Number.isFinite(rounded))
|
|
2584
|
+
throw new Error(`Numeric literal overflow for ${child.type}`);
|
|
2585
|
+
return rounded;
|
|
2586
|
+
}
|
|
2587
|
+
return value;
|
|
2588
|
+
});
|
|
2589
|
+
const typed = ast.typedValues;
|
|
2590
|
+
const matchingStorage = typed && typed.constructor === child.type.ArrayType && typed.length === values.length && values.every((value, index) => Object.is(value, typed[index]));
|
|
2591
|
+
const literal3 = matchingStorage ? new import_apache_arrow5.Vector([
|
|
2592
|
+
import_apache_arrow5.DataType.isList(listType) ? (0, import_apache_arrow5.makeData)({
|
|
2593
|
+
type: listType,
|
|
2594
|
+
length: 1,
|
|
2595
|
+
valueOffsets: Int32Array.of(0, values.length),
|
|
2596
|
+
child: (0, import_apache_arrow5.makeData)({ type: child.type, length: values.length, data: typed.slice() })
|
|
2597
|
+
}) : (0, import_apache_arrow5.makeData)({
|
|
2598
|
+
type: listType,
|
|
2599
|
+
length: 1,
|
|
2600
|
+
child: (0, import_apache_arrow5.makeData)({ type: child.type, length: values.length, data: typed.slice() })
|
|
2601
|
+
})
|
|
2602
|
+
]) : (0, import_apache_arrow5.vectorFromArray)([values], listType);
|
|
2603
|
+
const dictionaryType = new import_apache_arrow5.Dictionary(listType, new import_apache_arrow5.Uint8());
|
|
2604
|
+
let cached;
|
|
2605
|
+
return (length) => {
|
|
2606
|
+
if (!import_apache_arrow5.DataType.isFixedSizeList(listType) && (cached == null ? void 0 : cached.length) === length)
|
|
2607
|
+
return cached;
|
|
2608
|
+
if (import_apache_arrow5.DataType.isFixedSizeList(listType)) {
|
|
2609
|
+
const result = nullTupleVector(listType, length);
|
|
2610
|
+
const data = result.data[0];
|
|
2611
|
+
const childData = data.children[0];
|
|
2612
|
+
const components = literal3.data[0].children[0].values.subarray(0, listType.listSize);
|
|
2613
|
+
for (let row = 0; row < length; row++) {
|
|
2614
|
+
data.setValid(row, true);
|
|
2615
|
+
childData.values.set(components, row * listType.listSize);
|
|
2616
|
+
}
|
|
2617
|
+
cached = result;
|
|
2618
|
+
} else {
|
|
2619
|
+
cached = (0, import_apache_arrow5.makeVector)((0, import_apache_arrow5.makeData)({ type: dictionaryType, length, data: new Uint8Array(length), dictionary: literal3 }));
|
|
2620
|
+
}
|
|
2621
|
+
return cached;
|
|
2622
|
+
};
|
|
2623
|
+
}
|
|
2624
|
+
|
|
2625
|
+
// dist/arrow/compiler.js
|
|
2626
|
+
var encoder = new TextEncoder();
|
|
2627
|
+
var bool = new import_apache_arrow6.Bool();
|
|
2628
|
+
function compileArrowExpression(expression, options) {
|
|
2629
|
+
const fields = [...options.inputSchema.fields];
|
|
2630
|
+
if (new Set(fields.map((field) => field.name)).size !== fields.length)
|
|
2631
|
+
throw new Error("Expression input field names must be unique");
|
|
2632
|
+
const ast = parse(expression);
|
|
2633
|
+
validateExpressionAST(ast);
|
|
2634
|
+
const root = bind(ast, {
|
|
2635
|
+
...options,
|
|
2636
|
+
functionLookup: createFunctionLookup(options.functions)
|
|
2637
|
+
});
|
|
2638
|
+
const keys = fields.map((field) => ({
|
|
2639
|
+
name: field.name,
|
|
2640
|
+
type: physicalKey(field.type),
|
|
2641
|
+
nullable: field.nullable,
|
|
2642
|
+
extension: extensionKey(field)
|
|
2643
|
+
}));
|
|
2644
|
+
const validateSchema = (schema) => {
|
|
2645
|
+
if (schema.fields.length !== keys.length || schema.fields.some((field, index) => {
|
|
2646
|
+
const expected = keys[index];
|
|
2647
|
+
return field.name !== expected.name || physicalKey(field.type) !== expected.type || field.nullable !== expected.nullable || extensionKey(field) !== expected.extension;
|
|
2648
|
+
}))
|
|
2649
|
+
throw new Error("Arrow expression input schema mismatch");
|
|
2650
|
+
};
|
|
2651
|
+
const validateMask = (mask, length) => {
|
|
2652
|
+
if (mask && (mask.length !== length || mask.some((value) => value !== 0 && value !== 1)))
|
|
2653
|
+
throw new Error("activeRows must contain one 0 or 1 per input row");
|
|
2654
|
+
};
|
|
2655
|
+
const evaluate = (batch, evaluation = {}) => {
|
|
2656
|
+
var _a2, _b;
|
|
2657
|
+
(_a2 = evaluation.signal) == null ? void 0 : _a2.throwIfAborted();
|
|
2658
|
+
validateSchema(batch.schema);
|
|
2659
|
+
validateMask(evaluation.activeRows, batch.numRows);
|
|
2660
|
+
const active = evaluation.activeRows ? evaluation.activeRows.slice() : new Uint8Array(batch.numRows).fill(1);
|
|
2661
|
+
const prepared = root.prepare(batch, active, {});
|
|
2662
|
+
const result = isTupleType(root.field.type) ? maskTupleVector(prepared.vector, active) : isNestedType(root.field.type) ? nestedVector(prepared.vector, root.field.type, active) : prepared.vector && (root.constant || !import_apache_arrow6.DataType.isDictionary(prepared.vector.type) && !active.some((value) => !value)) ? prepared.vector : output(root.field, active, prepared.get);
|
|
2663
|
+
(_b = evaluation.signal) == null ? void 0 : _b.throwIfAborted();
|
|
2664
|
+
return result;
|
|
2665
|
+
};
|
|
2666
|
+
return Object.freeze({
|
|
2667
|
+
outputField: root.field,
|
|
2668
|
+
evaluate,
|
|
2669
|
+
evaluateTable(arrowTable, evaluation = {}) {
|
|
2670
|
+
var _a2;
|
|
2671
|
+
(_a2 = evaluation.signal) == null ? void 0 : _a2.throwIfAborted();
|
|
2672
|
+
validateSchema(arrowTable.schema);
|
|
2673
|
+
validateMask(evaluation.activeRows, arrowTable.numRows);
|
|
2674
|
+
let offset = 0;
|
|
2675
|
+
const chunks = arrowTable.batches.flatMap((batch) => {
|
|
2676
|
+
var _a3;
|
|
2677
|
+
const activeRows = (_a3 = evaluation.activeRows) == null ? void 0 : _a3.subarray(offset, offset + batch.numRows);
|
|
2678
|
+
offset += batch.numRows;
|
|
2679
|
+
return evaluate(batch, { ...evaluation, activeRows }).data;
|
|
2680
|
+
});
|
|
2681
|
+
return chunks.length ? new import_apache_arrow6.Vector(chunks) : isTupleType(root.field.type) ? nullTupleVector(root.field.type, 0) : isNestedType(root.field.type) ? (0, import_apache_arrow6.vectorFromArray)([], root.field.type) : output(root.field, new Uint8Array(0), () => null);
|
|
2682
|
+
}
|
|
2683
|
+
});
|
|
2684
|
+
}
|
|
2685
|
+
function output(field, active, evaluate) {
|
|
2686
|
+
const type = valueType(field.type);
|
|
2687
|
+
const validate = createValueValidator(type);
|
|
2688
|
+
if (import_apache_arrow6.DataType.isUtf8(type) || import_apache_arrow6.DataType.isBinary(type) || import_apache_arrow6.DataType.isFixedSizeBinary(type)) {
|
|
2689
|
+
const values = new Array(active.length).fill(null);
|
|
2690
|
+
for (let row = 0; row < active.length; row++)
|
|
2691
|
+
if (active[row]) {
|
|
2692
|
+
const value = evaluate(row);
|
|
2693
|
+
if (value === null && !field.nullable)
|
|
2694
|
+
throw new Error(`Non-nullable expression ${field.name} returned null`);
|
|
2695
|
+
validate(value);
|
|
2696
|
+
values[row] = value;
|
|
2697
|
+
}
|
|
2698
|
+
return (0, import_apache_arrow6.vectorFromArray)(values, field.type);
|
|
2699
|
+
}
|
|
2700
|
+
if (import_apache_arrow6.DataType.isNull(type)) {
|
|
2701
|
+
for (let row = 0; row < active.length; row++)
|
|
2702
|
+
if (active[row])
|
|
2703
|
+
validate(evaluate(row));
|
|
2704
|
+
return (0, import_apache_arrow6.makeVector)((0, import_apache_arrow6.makeData)({ type, length: active.length }));
|
|
2705
|
+
}
|
|
2706
|
+
const isBoolean = import_apache_arrow6.DataType.isBool(type);
|
|
2707
|
+
const data = isBoolean ? new Uint8Array(Math.ceil(active.length / 8)) : new type.ArrayType(active.length);
|
|
2708
|
+
const numericData = import_apache_arrow6.DataType.isFloat(type) && type.precision === import_apache_arrow6.Precision.HALF ? float16Values(data) : data;
|
|
2709
|
+
const validity = new Uint8Array(Math.ceil(active.length / 8));
|
|
2710
|
+
let nullCount = 0;
|
|
2711
|
+
for (let row = 0; row < active.length; row++) {
|
|
2712
|
+
if (!active[row]) {
|
|
2713
|
+
nullCount++;
|
|
2714
|
+
continue;
|
|
2715
|
+
}
|
|
2716
|
+
const value = evaluate(row);
|
|
2717
|
+
if (value === null) {
|
|
2718
|
+
if (!field.nullable)
|
|
2719
|
+
throw new Error(`Non-nullable expression ${field.name} returned null`);
|
|
2720
|
+
nullCount++;
|
|
2721
|
+
continue;
|
|
2722
|
+
}
|
|
2723
|
+
validate(value);
|
|
2724
|
+
validity[row >> 3] |= 1 << (row & 7);
|
|
2725
|
+
if (isBoolean) {
|
|
2726
|
+
if (value)
|
|
2727
|
+
data[row >> 3] |= 1 << (row & 7);
|
|
2728
|
+
} else
|
|
2729
|
+
numericData[row] = value;
|
|
2730
|
+
}
|
|
2731
|
+
return (0, import_apache_arrow6.makeVector)((0, import_apache_arrow6.makeData)({
|
|
2732
|
+
type,
|
|
2733
|
+
data,
|
|
2734
|
+
length: active.length,
|
|
2735
|
+
nullBitmap: validity,
|
|
2736
|
+
nullCount
|
|
2737
|
+
}));
|
|
2738
|
+
}
|
|
2739
|
+
function sourceReader(vector) {
|
|
2740
|
+
const data = vector.data[0];
|
|
2741
|
+
if (vector.data.length === 1 && (import_apache_arrow6.DataType.isInt(data.type) || import_apache_arrow6.DataType.isFloat(data.type) && data.type.precision !== import_apache_arrow6.Precision.HALF)) {
|
|
2742
|
+
const values = data.values, bitmap = data.nullBitmap, offset = data.offset;
|
|
2743
|
+
const valid = data.nullCount ? (row) => Boolean(bitmap[offset + row >> 3] & 1 << (offset + row & 7)) : () => true;
|
|
2744
|
+
return {
|
|
2745
|
+
vector,
|
|
2746
|
+
get: (row) => valid(row) ? values[row] : null,
|
|
2747
|
+
isNull: (row) => !valid(row)
|
|
2748
|
+
};
|
|
2749
|
+
}
|
|
2750
|
+
const reader = new ColumnReader(vector);
|
|
2751
|
+
return {
|
|
2752
|
+
vector,
|
|
2753
|
+
get: (row) => reader.get(row),
|
|
2754
|
+
isNull: (row) => !reader.isValid(row),
|
|
2755
|
+
bytes: typeKey(vector.type) === "utf8" ? (row) => reader.isValid(row) ? reader.utf8(row) : null : void 0
|
|
2756
|
+
};
|
|
2757
|
+
}
|
|
2758
|
+
function vectorReader(vector) {
|
|
2759
|
+
try {
|
|
2760
|
+
typeKey(vector.type);
|
|
2761
|
+
return sourceReader(vector);
|
|
2762
|
+
} catch {
|
|
2763
|
+
}
|
|
2764
|
+
const reader = new ColumnReader(vector);
|
|
2765
|
+
return {
|
|
2766
|
+
vector,
|
|
2767
|
+
isNull: (row) => !reader.isValid(row),
|
|
2768
|
+
get: () => {
|
|
2769
|
+
throw new Error("Nested values require a vector kernel");
|
|
2770
|
+
}
|
|
2771
|
+
};
|
|
2772
|
+
}
|
|
2773
|
+
function bytes(value, row) {
|
|
2774
|
+
if (value.bytes)
|
|
2775
|
+
return value.bytes(row);
|
|
2776
|
+
const text = value.get(row);
|
|
2777
|
+
if (text === null)
|
|
2778
|
+
return null;
|
|
2779
|
+
const encoded = encoder.encode(text);
|
|
2780
|
+
return [encoded, 0, encoded.length];
|
|
2781
|
+
}
|
|
2782
|
+
function bind(ast, options, expected, vectorInput = false) {
|
|
2783
|
+
const node = bindNode(ast, options, expected, vectorInput);
|
|
2784
|
+
return {
|
|
2785
|
+
...node,
|
|
2786
|
+
prepare(batch, active, context) {
|
|
2787
|
+
const mask = context.failed ? active.map((value, row) => value && !context.failed[row] ? 1 : 0) : active;
|
|
2788
|
+
const prepared = node.prepare(batch, mask, context);
|
|
2789
|
+
if (!context.failed)
|
|
2790
|
+
return prepared;
|
|
2791
|
+
const guard = (row, read) => {
|
|
2792
|
+
if (!mask[row] || context.failed[row])
|
|
2793
|
+
return null;
|
|
2794
|
+
try {
|
|
2795
|
+
const value = read();
|
|
2796
|
+
return context.failed[row] ? null : value;
|
|
2797
|
+
} catch (error) {
|
|
2798
|
+
if (!isExpressionEvaluationError(error))
|
|
2799
|
+
throw error;
|
|
2800
|
+
context.failed[row] = 1;
|
|
2801
|
+
return null;
|
|
2802
|
+
}
|
|
2803
|
+
};
|
|
2804
|
+
return {
|
|
2805
|
+
...prepared,
|
|
2806
|
+
get: (row) => guard(row, () => prepared.get(row)),
|
|
2807
|
+
bytes: prepared.bytes ? (row) => guard(row, () => prepared.bytes(row)) : void 0,
|
|
2808
|
+
isNull: (row) => guard(row, () => prepared.isNull ? prepared.isNull(row) : prepared.get(row) === null) ?? true
|
|
2809
|
+
};
|
|
2810
|
+
}
|
|
2811
|
+
};
|
|
2812
|
+
}
|
|
2813
|
+
function prepareInput(node, batch, active, context, force) {
|
|
2814
|
+
const prepared = node.prepare(batch, active, context);
|
|
2815
|
+
if (!context.failed || !force || prepared.vector)
|
|
2816
|
+
return prepared;
|
|
2817
|
+
const mask = active.map((value, row) => value && !context.failed[row] ? 1 : 0);
|
|
2818
|
+
const field = new import_apache_arrow6.Field(node.field.name, node.field.type, true, node.field.metadata);
|
|
2819
|
+
return sourceReader(output(field, mask, prepared.get));
|
|
2820
|
+
}
|
|
2821
|
+
function bindNode(ast, options, expected, vectorInput = false) {
|
|
2822
|
+
if (isSignedLiteral(ast)) {
|
|
2823
|
+
const value = ast.operator === "-" ? -ast.argument.value : ast.argument.value;
|
|
2824
|
+
return bind({ type: "Literal", value }, options, expected, vectorInput);
|
|
2825
|
+
}
|
|
2826
|
+
if (ast.type === "ArrayExpression") {
|
|
2827
|
+
if (!expected)
|
|
2828
|
+
throw new Error("Array literals require a declared list type");
|
|
2829
|
+
const materialize = compileArrayLiteral(ast, expected);
|
|
2830
|
+
return {
|
|
2831
|
+
field: new import_apache_arrow6.Field("literal", expected, false),
|
|
2832
|
+
hasKernel: false,
|
|
2833
|
+
prepare: (_batch, active) => vectorReader(materialize(active.length))
|
|
2834
|
+
};
|
|
2835
|
+
}
|
|
2836
|
+
if (ast.type === "Identifier") {
|
|
2837
|
+
const index = options.inputSchema.fields.findIndex((field) => field.name === ast.name);
|
|
2838
|
+
if (index < 0)
|
|
2839
|
+
throw new Error(`Unknown Arrow expression field: ${ast.name}`);
|
|
2840
|
+
const source = options.inputSchema.fields[index];
|
|
2841
|
+
const tuple = isTupleType(source.type);
|
|
2842
|
+
if (!vectorInput && !tuple)
|
|
2843
|
+
typeKey(source.type);
|
|
2844
|
+
return {
|
|
2845
|
+
field: new import_apache_arrow6.Field(source.name, vectorInput ? source.type : valueType(source.type), source.nullable, source.metadata),
|
|
2846
|
+
hasKernel: false,
|
|
2847
|
+
prepare: (batch) => {
|
|
2848
|
+
const vector = batch.getChildAt(index);
|
|
2849
|
+
return vectorInput || tuple ? vectorReader(vector) : sourceReader(vector);
|
|
2850
|
+
}
|
|
2851
|
+
};
|
|
2852
|
+
}
|
|
2853
|
+
if (ast.type === "Literal") {
|
|
2854
|
+
if (vectorInput && expected && import_apache_arrow6.DataType.isDictionary(expected))
|
|
2855
|
+
throw new Error("Dictionary function arguments require a dictionary-encoded source column");
|
|
2856
|
+
const date = ast.value instanceof Date ? ast.value : void 0;
|
|
2857
|
+
let value = date ? expected && import_apache_arrow6.DataType.isDate(expected) ? Math.floor(date.getTime() / 864e5) : BigInt(date.getTime()) * 1000n : ast.value;
|
|
2858
|
+
if (value instanceof Uint8Array)
|
|
2859
|
+
value = new Uint8Array(value);
|
|
2860
|
+
if (expected && import_apache_arrow6.DataType.isInt(expected) && expected.bitWidth === 64 && typeof value === "number" && Number.isSafeInteger(value))
|
|
2861
|
+
value = BigInt(value);
|
|
2862
|
+
const type = expected ?? (date ? new import_apache_arrow6.TimestampMicrosecond("UTC") : value instanceof Uint8Array ? new import_apache_arrow6.Binary() : value === null ? new import_apache_arrow6.Null() : typeof value === "boolean" ? bool : typeof value === "number" ? new import_apache_arrow6.Float64() : typeof value === "bigint" ? new import_apache_arrow6.Int64() : new import_apache_arrow6.Utf8());
|
|
2863
|
+
if (value === null && isTupleType(type))
|
|
2864
|
+
return {
|
|
2865
|
+
field: new import_apache_arrow6.Field("literal", type, true),
|
|
2866
|
+
hasKernel: false,
|
|
2867
|
+
prepare: (_batch, active) => vectorReader(nullTupleVector(type, active.length))
|
|
2868
|
+
};
|
|
2869
|
+
if (value === null && import_apache_arrow6.DataType.isList(type)) {
|
|
2870
|
+
const dictionary = (0, import_apache_arrow6.vectorFromArray)([null], type);
|
|
2871
|
+
return {
|
|
2872
|
+
field: new import_apache_arrow6.Field("literal", type, true),
|
|
2873
|
+
hasKernel: false,
|
|
2874
|
+
prepare: (_batch, active) => vectorReader((0, import_apache_arrow6.makeVector)((0, import_apache_arrow6.makeData)({
|
|
2875
|
+
type: new import_apache_arrow6.Dictionary(type, new import_apache_arrow6.Int32()),
|
|
2876
|
+
length: active.length,
|
|
2877
|
+
data: new Int32Array(active.length),
|
|
2878
|
+
dictionary
|
|
2879
|
+
})))
|
|
2880
|
+
};
|
|
2881
|
+
}
|
|
2882
|
+
if (value === null && isNestedType(type))
|
|
2883
|
+
return {
|
|
2884
|
+
field: new import_apache_arrow6.Field("literal", type, true),
|
|
2885
|
+
hasKernel: false,
|
|
2886
|
+
prepare: (_batch, active) => vectorReader((0, import_apache_arrow6.vectorFromArray)(Array(active.length).fill(null), type))
|
|
2887
|
+
};
|
|
2888
|
+
validateValue(value, type);
|
|
2889
|
+
const rounded = roundToType(type)(value);
|
|
2890
|
+
if (typeof value === "number" && Number.isFinite(value) && !Number.isFinite(rounded))
|
|
2891
|
+
throw new Error(`Numeric literal overflow for ${type}`);
|
|
2892
|
+
const encoded = typeof value === "string" ? ast.utf8 instanceof Uint8Array && new TextDecoder("utf-8", { ignoreBOM: true }).decode(ast.utf8) === value ? new Uint8Array(ast.utf8) : encoder.encode(value) : void 0;
|
|
2893
|
+
const range = encoded ? [encoded, 0, encoded.length] : void 0;
|
|
2894
|
+
const constant = {
|
|
2895
|
+
get: () => rounded,
|
|
2896
|
+
isNull: () => value === null,
|
|
2897
|
+
bytes: range ? () => range : void 0
|
|
2898
|
+
};
|
|
2899
|
+
return {
|
|
2900
|
+
field: new import_apache_arrow6.Field("literal", type, value === null),
|
|
2901
|
+
hasKernel: false,
|
|
2902
|
+
constant,
|
|
2903
|
+
prepare: (_batch, active) => {
|
|
2904
|
+
if (!encoded)
|
|
2905
|
+
return constant;
|
|
2906
|
+
let vector;
|
|
2907
|
+
return {
|
|
2908
|
+
...constant,
|
|
2909
|
+
// Comparisons only read bytes. Materialize physical UTF-8 only when a
|
|
2910
|
+
// projection or vector function actually requests a batch-sized vector.
|
|
2911
|
+
get vector() {
|
|
2912
|
+
if (!vector)
|
|
2913
|
+
vector = repeatUTF8(encoded, active);
|
|
2914
|
+
return vector;
|
|
2915
|
+
}
|
|
2916
|
+
};
|
|
2917
|
+
}
|
|
2918
|
+
};
|
|
2919
|
+
}
|
|
2920
|
+
if (ast.type === "CallExpression")
|
|
2921
|
+
return bindCall(ast, options, expected);
|
|
2922
|
+
if (ast.type === "ConditionalExpression") {
|
|
2923
|
+
const condition = bind(ast.test, options, bool);
|
|
2924
|
+
requireType(condition, "bool");
|
|
2925
|
+
let yes, no;
|
|
2926
|
+
if (isContextualLiteral(ast.consequent) && !isContextualLiteral(ast.alternate)) {
|
|
2927
|
+
no = bind(ast.alternate, options, expected);
|
|
2928
|
+
yes = bind(ast.consequent, options, no.field.type);
|
|
2929
|
+
} else {
|
|
2930
|
+
yes = bind(ast.consequent, options, expected);
|
|
2931
|
+
no = bind(ast.alternate, options, isContextualLiteral(ast.alternate) && !import_apache_arrow6.DataType.isNull(yes.field.type) ? yes.field.type : expected);
|
|
2932
|
+
if (isContextualLiteral(ast.consequent) && !import_apache_arrow6.DataType.isNull(no.field.type))
|
|
2933
|
+
yes = bind(ast.consequent, options, no.field.type);
|
|
2934
|
+
}
|
|
2935
|
+
const tuple = isTupleType(yes.field.type);
|
|
2936
|
+
const list = import_apache_arrow6.DataType.isList(yes.field.type);
|
|
2937
|
+
const nested = isNestedType(yes.field.type);
|
|
2938
|
+
if (nested) {
|
|
2939
|
+
if (physicalKey(yes.field.type) !== physicalKey(no.field.type))
|
|
2940
|
+
throw new Error("Arrow expression operand types must match");
|
|
2941
|
+
if (!isContextualLiteral(ast.consequent) && !isContextualLiteral(ast.alternate) && extensionKey(yes.field) !== extensionKey(no.field))
|
|
2942
|
+
throw new Error("Arrow expression branch extension metadata must match");
|
|
2943
|
+
} else
|
|
2944
|
+
sameType(yes, no);
|
|
2945
|
+
const metadata = isContextualLiteral(ast.consequent) ? no.field.metadata : yes.field.metadata;
|
|
2946
|
+
const field = new import_apache_arrow6.Field("conditional", yes.field.type, yes.field.nullable || no.field.nullable, metadata);
|
|
2947
|
+
return {
|
|
2948
|
+
field,
|
|
2949
|
+
hasKernel: nested || condition.hasKernel || yes.hasKernel || no.hasKernel,
|
|
2950
|
+
prepare(batch, active, context) {
|
|
2951
|
+
const test = condition.prepare(batch, active, context);
|
|
2952
|
+
const selected = nested || yes.hasKernel || no.hasKernel ? active.map((value, row) => value && test.get(row) === true ? 1 : 0) : void 0;
|
|
2953
|
+
const a = yes.prepare(batch, selected ?? active, context);
|
|
2954
|
+
const b = no.prepare(batch, selected ? active.map((value, row) => value && !selected[row] ? 1 : 0) : active, context);
|
|
2955
|
+
if (list && import_apache_arrow6.DataType.isDictionary(a.vector.type) && import_apache_arrow6.DataType.isDictionary(b.vector.type))
|
|
2956
|
+
return vectorReader(selectListVectors(field.type, a.vector, b.vector, selected, active));
|
|
2957
|
+
if (tuple) {
|
|
2958
|
+
const validActive = context.failed ? active.map((value, row) => value && !context.failed[row] ? 1 : 0) : active;
|
|
2959
|
+
const yesMask = validActive.map((value, row) => value && selected[row] ? 1 : 0);
|
|
2960
|
+
const noMask = validActive.map((value, row) => value && !selected[row] ? 1 : 0);
|
|
2961
|
+
validateTupleVector(a.vector, yes.field, yesMask);
|
|
2962
|
+
validateTupleVector(b.vector, no.field, noMask);
|
|
2963
|
+
return vectorReader(selectTupleVectors(field.type, a.vector, b.vector, yesMask, validActive));
|
|
2964
|
+
}
|
|
2965
|
+
if (nested) {
|
|
2966
|
+
const values = Array.from(active, (valid, row) => {
|
|
2967
|
+
var _a2;
|
|
2968
|
+
return valid && !((_a2 = context.failed) == null ? void 0 : _a2[row]) ? nestedValue((selected[row] ? a : b).vector.get(row), field.type) : null;
|
|
2969
|
+
});
|
|
2970
|
+
return vectorReader((0, import_apache_arrow6.vectorFromArray)(values, field.type));
|
|
2971
|
+
}
|
|
2972
|
+
const choose = (row) => (selected ? selected[row] : test.get(row) === true) ? a : b;
|
|
2973
|
+
return {
|
|
2974
|
+
get: (row) => choose(row).get(row),
|
|
2975
|
+
isNull: (row) => {
|
|
2976
|
+
const branch = choose(row);
|
|
2977
|
+
return branch.isNull ? branch.isNull(row) : branch.get(row) === null;
|
|
2978
|
+
},
|
|
2979
|
+
bytes: typeKey(field.type) === "utf8" ? (row) => bytes(choose(row), row) : void 0
|
|
2980
|
+
};
|
|
2981
|
+
}
|
|
2982
|
+
};
|
|
2983
|
+
}
|
|
2984
|
+
if (ast.type === "UnaryExpression") {
|
|
2985
|
+
const argument = bind(ast.argument, options, ast.operator === "!" ? bool : void 0);
|
|
2986
|
+
if (ast.operator === "!")
|
|
2987
|
+
requireType(argument, "bool");
|
|
2988
|
+
else if (ast.operator === "+" || ast.operator === "-")
|
|
2989
|
+
requireNumeric(argument);
|
|
2990
|
+
else
|
|
2991
|
+
throw new Error(`Unsupported unary operator: ${ast.operator}`);
|
|
2992
|
+
const validate = createValueValidator(argument.field.type);
|
|
2993
|
+
const round = roundToType(argument.field.type);
|
|
2994
|
+
return {
|
|
2995
|
+
field: new import_apache_arrow6.Field("unary", argument.field.type, argument.field.nullable),
|
|
2996
|
+
hasKernel: argument.hasKernel,
|
|
2997
|
+
prepare(batch, active, context) {
|
|
2998
|
+
const child = argument.prepare(batch, active, context);
|
|
2999
|
+
return {
|
|
3000
|
+
get: (row) => {
|
|
3001
|
+
const value = child.get(row);
|
|
3002
|
+
if (value === null)
|
|
3003
|
+
return null;
|
|
3004
|
+
const result = ast.operator === "!" ? !value : ast.operator === "+" ? value : -value;
|
|
3005
|
+
validate(result);
|
|
3006
|
+
return round(result);
|
|
3007
|
+
}
|
|
3008
|
+
};
|
|
3009
|
+
}
|
|
3010
|
+
};
|
|
3011
|
+
}
|
|
3012
|
+
if (ast.type === "BinaryExpression" || ast.type === "LogicalExpression")
|
|
3013
|
+
return bindBinary(ast, options);
|
|
3014
|
+
throw new Error(`Unsupported Arrow expression syntax: ${ast.type}`);
|
|
3015
|
+
}
|
|
3016
|
+
function sameType(a, b) {
|
|
3017
|
+
if (typeKey(a.field.type) !== typeKey(b.field.type))
|
|
3018
|
+
throw new Error("Arrow expression operand types must match");
|
|
3019
|
+
}
|
|
3020
|
+
function requireType(node, key) {
|
|
3021
|
+
if (typeKey(node.field.type) !== key)
|
|
3022
|
+
throw new Error(`Arrow expression requires ${key}`);
|
|
3023
|
+
}
|
|
3024
|
+
function requireNumeric(node) {
|
|
3025
|
+
if (!import_apache_arrow6.DataType.isInt(node.field.type) && !import_apache_arrow6.DataType.isFloat(node.field.type))
|
|
3026
|
+
throw new Error("Arrow expression requires numeric operands");
|
|
3027
|
+
}
|
|
3028
|
+
function bindBinary(ast, options) {
|
|
3029
|
+
const op = ast.operator, logical = op === "&&" || op === "||";
|
|
3030
|
+
let left = bind(ast.left, options, logical ? bool : void 0), right = bind(ast.right, options, logical ? bool : void 0);
|
|
3031
|
+
if (!logical) {
|
|
3032
|
+
if (isContextualLiteral(ast.left) && !import_apache_arrow6.DataType.isNull(right.field.type))
|
|
3033
|
+
left = bind(ast.left, options, right.field.type);
|
|
3034
|
+
if (isContextualLiteral(ast.right))
|
|
3035
|
+
right = bind(ast.right, options, left.field.type);
|
|
3036
|
+
}
|
|
3037
|
+
sameType(left, right);
|
|
3038
|
+
const comparison = ["==", "===", "!=", "!==", "<", "<=", ">", ">="].includes(op);
|
|
3039
|
+
if (logical)
|
|
3040
|
+
requireType(left, "bool");
|
|
3041
|
+
else if (!comparison) {
|
|
3042
|
+
if (!["+", "-", "*", "/", "%"].includes(op))
|
|
3043
|
+
throw new Error(`Unsupported binary operator: ${op}`);
|
|
3044
|
+
requireNumeric(left);
|
|
3045
|
+
}
|
|
3046
|
+
const type = left.field.type, utf8 = typeKey(type) === "utf8", binary = import_apache_arrow6.DataType.isBinary(type) || import_apache_arrow6.DataType.isFixedSizeBinary(type), integer = import_apache_arrow6.DataType.isInt(type);
|
|
3047
|
+
const validate = createValueValidator(type);
|
|
3048
|
+
const round = roundToType(type);
|
|
3049
|
+
const field = new import_apache_arrow6.Field("expression", logical || comparison ? bool : type, left.field.nullable || right.field.nullable);
|
|
3050
|
+
return {
|
|
3051
|
+
field,
|
|
3052
|
+
hasKernel: left.hasKernel || right.hasKernel,
|
|
3053
|
+
prepare(batch, active, context) {
|
|
3054
|
+
const a = prepareInput(left, batch, active, context, left.hasKernel || right.hasKernel);
|
|
3055
|
+
let truth;
|
|
3056
|
+
let mask = active;
|
|
3057
|
+
if (logical && right.hasKernel) {
|
|
3058
|
+
truth = new Uint8Array(active.length);
|
|
3059
|
+
mask = active.map((value, row) => {
|
|
3060
|
+
if (!value)
|
|
3061
|
+
return 0;
|
|
3062
|
+
const x = a.get(row);
|
|
3063
|
+
truth[row] = x === null ? 2 : x ? 1 : 0;
|
|
3064
|
+
return (op === "&&" ? x !== false : x !== true) ? 1 : 0;
|
|
3065
|
+
});
|
|
3066
|
+
}
|
|
3067
|
+
const b = right.prepare(batch, mask, context);
|
|
3068
|
+
return {
|
|
3069
|
+
get: (row) => {
|
|
3070
|
+
if (logical) {
|
|
3071
|
+
const x2 = truth ? truth[row] === 2 ? null : truth[row] === 1 : a.get(row);
|
|
3072
|
+
if (op === "&&" && x2 === false)
|
|
3073
|
+
return false;
|
|
3074
|
+
if (op === "||" && x2 === true)
|
|
3075
|
+
return true;
|
|
3076
|
+
const y2 = b.get(row);
|
|
3077
|
+
return op === "&&" ? y2 === false ? false : x2 === null || y2 === null ? null : true : y2 === true ? true : x2 === null || y2 === null ? null : false;
|
|
3078
|
+
}
|
|
3079
|
+
if (utf8) {
|
|
3080
|
+
const x2 = bytes(a, row), y2 = bytes(b, row);
|
|
3081
|
+
if (!x2 || !y2)
|
|
3082
|
+
return null;
|
|
3083
|
+
return compare(op, (0, import_utf82.compareUTF8)(x2[0], x2[1], x2[2], y2[0], y2[1], y2[2]), 0);
|
|
3084
|
+
}
|
|
3085
|
+
const x = a.get(row), y = b.get(row);
|
|
3086
|
+
if (x === null || y === null)
|
|
3087
|
+
return null;
|
|
3088
|
+
if (binary)
|
|
3089
|
+
return compare(op, (0, import_utf82.compareUTF8)(x, 0, x.length, y, 0, y.length), 0);
|
|
3090
|
+
if (comparison)
|
|
3091
|
+
return compare(op, x, y);
|
|
3092
|
+
if ((op === "/" || op === "%") && (y === 0 || y === 0n))
|
|
3093
|
+
throw new ExpressionEvaluationError("divisionByZero", "Division by zero");
|
|
3094
|
+
const result = op === "+" ? x + y : op === "-" ? x - y : op === "*" ? x * y : op === "/" ? integer && typeof x === "number" ? Math.trunc(x / y) : x / y : x % y;
|
|
3095
|
+
validate(result);
|
|
3096
|
+
const rounded = round(result);
|
|
3097
|
+
if (!integer && Number.isFinite(x) && Number.isFinite(y) && !Number.isFinite(rounded))
|
|
3098
|
+
throw new ExpressionEvaluationError("overflow", "Floating expression overflow");
|
|
3099
|
+
return rounded;
|
|
3100
|
+
}
|
|
3101
|
+
};
|
|
3102
|
+
}
|
|
3103
|
+
};
|
|
3104
|
+
}
|
|
3105
|
+
function compare(op, a, b) {
|
|
3106
|
+
switch (op) {
|
|
3107
|
+
case "==":
|
|
3108
|
+
case "===":
|
|
3109
|
+
return a === b;
|
|
3110
|
+
case "!=":
|
|
3111
|
+
case "!==":
|
|
3112
|
+
return a !== b;
|
|
3113
|
+
case "<":
|
|
3114
|
+
return a < b;
|
|
3115
|
+
case "<=":
|
|
3116
|
+
return a <= b;
|
|
3117
|
+
case ">":
|
|
3118
|
+
return a > b;
|
|
3119
|
+
case ">=":
|
|
3120
|
+
return a >= b;
|
|
3121
|
+
default:
|
|
3122
|
+
throw new Error("Invalid comparison");
|
|
3123
|
+
}
|
|
3124
|
+
}
|
|
3125
|
+
function selectListVectors(type, left, right, selected, active) {
|
|
3126
|
+
if (!import_apache_arrow6.DataType.isDictionary(left.type) || !import_apache_arrow6.DataType.isDictionary(right.type))
|
|
3127
|
+
throw new Error("Computed List arguments require array literal branches");
|
|
3128
|
+
const a = left.data[0], b = right.data[0];
|
|
3129
|
+
const leftDictionary = a.dictionary, rightDictionary = b.dictionary;
|
|
3130
|
+
const indices = new Int32Array(active.length);
|
|
3131
|
+
const valid = new Uint8Array(active.length);
|
|
3132
|
+
for (let row = 0; row < active.length; row++) {
|
|
3133
|
+
if (!active[row])
|
|
3134
|
+
continue;
|
|
3135
|
+
if (selected[row]) {
|
|
3136
|
+
indices[row] = Number(a.values[row]);
|
|
3137
|
+
valid[row] = a.getValid(row) ? 1 : 0;
|
|
3138
|
+
} else {
|
|
3139
|
+
indices[row] = leftDictionary.length + Number(b.values[row]);
|
|
3140
|
+
valid[row] = b.getValid(row) ? 1 : 0;
|
|
3141
|
+
}
|
|
3142
|
+
}
|
|
3143
|
+
return maskTupleVector((0, import_apache_arrow6.makeVector)((0, import_apache_arrow6.makeData)({
|
|
3144
|
+
type: new import_apache_arrow6.Dictionary(type, new import_apache_arrow6.Int32()),
|
|
3145
|
+
length: active.length,
|
|
3146
|
+
data: indices,
|
|
3147
|
+
dictionary: leftDictionary.concat(rightDictionary)
|
|
3148
|
+
})), valid);
|
|
3149
|
+
}
|
|
3150
|
+
function bindRecovery(name2, expressions, options, expected) {
|
|
3151
|
+
if (expected && import_apache_arrow6.DataType.isDictionary(expected))
|
|
3152
|
+
throw new Error("Dictionary function arguments require a dictionary-encoded source column");
|
|
3153
|
+
if (name2 === "TRY" ? expressions.length < 1 || expressions.length > 2 : !expressions.length)
|
|
3154
|
+
throw new Error(`${name2} has an invalid argument count`);
|
|
3155
|
+
const indices = expressions.map((_, index) => index);
|
|
3156
|
+
const ordered = [
|
|
3157
|
+
...indices.filter((index) => !isContextualLiteral(expressions[index])),
|
|
3158
|
+
...indices.filter((index) => isContextualLiteral(expressions[index]))
|
|
3159
|
+
];
|
|
3160
|
+
let anchor = 0;
|
|
3161
|
+
let candidate;
|
|
3162
|
+
for (const index of ordered) {
|
|
3163
|
+
const expression = expressions[index];
|
|
3164
|
+
if (!expected && isContextualLiteral(expression) && containsContextualArray(expression))
|
|
3165
|
+
continue;
|
|
3166
|
+
const node = bind(expression, options, expected);
|
|
3167
|
+
if (!candidate || !import_apache_arrow6.DataType.isNull(node.field.type)) {
|
|
3168
|
+
anchor = index;
|
|
3169
|
+
candidate = node;
|
|
3170
|
+
}
|
|
3171
|
+
if (!import_apache_arrow6.DataType.isNull(node.field.type))
|
|
3172
|
+
break;
|
|
3173
|
+
}
|
|
3174
|
+
const first = candidate ?? bind(expressions[anchor], options, expected);
|
|
3175
|
+
const nodes = expressions.map((expression, index) => index === anchor ? first : bind(expression, options, first.field.type));
|
|
3176
|
+
for (const [index, node] of nodes.entries()) {
|
|
3177
|
+
if (physicalKey(node.field.type) !== physicalKey(first.field.type) || !isContextualLiteral(expressions[index]) && extensionKey(node.field) !== extensionKey(first.field))
|
|
3178
|
+
throw new Error(`${name2} arguments must have matching types and metadata`);
|
|
3179
|
+
}
|
|
3180
|
+
const field = new import_apache_arrow6.Field(name2.toLowerCase(), first.field.type, true, first.field.metadata);
|
|
3181
|
+
const tuple = isTupleType(field.type);
|
|
3182
|
+
const list = Boolean(expected && import_apache_arrow6.DataType.isList(expected) && import_apache_arrow6.DataType.isList(field.type));
|
|
3183
|
+
if (!tuple && !list)
|
|
3184
|
+
typeKey(field.type);
|
|
3185
|
+
return {
|
|
3186
|
+
field,
|
|
3187
|
+
hasKernel: true,
|
|
3188
|
+
prepare(batch, active, context) {
|
|
3189
|
+
const failed = new Uint8Array(active.length);
|
|
3190
|
+
const childContext = name2 === "TRY" ? { failed } : context;
|
|
3191
|
+
const child = nodes[0].prepare(batch, active, childContext);
|
|
3192
|
+
const nullable = (node) => new import_apache_arrow6.Field(node.field.name, node.field.type, true, node.field.metadata);
|
|
3193
|
+
const materialize = (node, prepared, mask) => tuple || list ? maskTupleVector(prepared.vector, mask) : output(nullable(node), mask, prepared.get);
|
|
3194
|
+
const original = materialize(nodes[0], child, active);
|
|
3195
|
+
const reader = tuple ? void 0 : new ColumnReader(original);
|
|
3196
|
+
let result = original;
|
|
3197
|
+
let pending = active.map((value, row) => value && (name2 === "TRY" ? failed[row] : tuple ? !original.isValid(row) : !reader.isValid(row)) ? 1 : 0);
|
|
3198
|
+
if ((tuple || list) && name2 === "TRY")
|
|
3199
|
+
result = maskTupleVector(result, active.map((value, row) => value && !failed[row] ? 1 : 0));
|
|
3200
|
+
for (let index = 1; index < nodes.length && pending.some(Boolean); index++) {
|
|
3201
|
+
const node = nodes[index];
|
|
3202
|
+
const fallback = node.prepare(batch, pending, context);
|
|
3203
|
+
const vector = materialize(node, fallback, pending);
|
|
3204
|
+
const keep = active.map((value, row) => value && !pending[row] ? 1 : 0);
|
|
3205
|
+
if (tuple)
|
|
3206
|
+
result = selectTupleVectors(field.type, result, vector, keep, active);
|
|
3207
|
+
else if (list)
|
|
3208
|
+
result = selectListVectors(field.type, result, vector, keep, active);
|
|
3209
|
+
else {
|
|
3210
|
+
const a = new ColumnReader(result), b = new ColumnReader(vector);
|
|
3211
|
+
result = output(field, active, (row) => pending[row] ? b.get(row) : a.get(row));
|
|
3212
|
+
}
|
|
3213
|
+
const next = tuple ? void 0 : new ColumnReader(vector);
|
|
3214
|
+
pending = pending.map((value, row) => value && (tuple ? !vector.isValid(row) : !next.isValid(row)) ? 1 : 0);
|
|
3215
|
+
}
|
|
3216
|
+
return vectorReader(result);
|
|
3217
|
+
}
|
|
3218
|
+
};
|
|
3219
|
+
}
|
|
3220
|
+
function bindGeometryCast(argument) {
|
|
3221
|
+
const source = valueType(argument.field.type);
|
|
3222
|
+
const metadata = new Map(argument.field.metadata);
|
|
3223
|
+
const extension = metadata.get("ARROW:extension:name");
|
|
3224
|
+
if (!import_apache_arrow6.DataType.isBinary(source) && !import_apache_arrow6.DataType.isNull(source) || extension && extension !== "geoarrow.wkb")
|
|
3225
|
+
throw new Error("GEOMETRY CAST requires Binary WKB input");
|
|
3226
|
+
metadata.set("ARROW:extension:name", "geoarrow.wkb");
|
|
3227
|
+
return {
|
|
3228
|
+
...argument,
|
|
3229
|
+
field: new import_apache_arrow6.Field("cast", new import_apache_arrow6.Binary(), argument.field.nullable, metadata),
|
|
3230
|
+
prepare(batch, active, context) {
|
|
3231
|
+
const child = argument.prepare(batch, active, context);
|
|
3232
|
+
return { get: (row) => child.get(row) };
|
|
3233
|
+
}
|
|
3234
|
+
};
|
|
3235
|
+
}
|
|
3236
|
+
function bindMembership(arguments_, options) {
|
|
3237
|
+
if (arguments_.length < 2 || arguments_.length > 4097 || arguments_.slice(1).some((value) => value.type !== "Literal" && !isSignedLiteral(value)))
|
|
3238
|
+
throw new Error("IN requires a value and 1 to 4096 scalar literals");
|
|
3239
|
+
const argument = bind(arguments_[0], options);
|
|
3240
|
+
const utf8 = typeKey(argument.field.type) === "utf8";
|
|
3241
|
+
const binary = import_apache_arrow6.DataType.isBinary(argument.field.type) || import_apache_arrow6.DataType.isFixedSizeBinary(argument.field.type);
|
|
3242
|
+
const literals = arguments_.slice(1).map((value) => bind(value, options, argument.field.type));
|
|
3243
|
+
const prepared = literals.map((value) => value.constant);
|
|
3244
|
+
const values = new Set(prepared.map((value) => value.get(0)));
|
|
3245
|
+
const ranges = utf8 ? prepared.map((value) => bytes(value, 0)).filter((range) => range !== null) : binary ? prepared.flatMap((value) => {
|
|
3246
|
+
const data = value.get(0);
|
|
3247
|
+
return data === null ? [] : [[data, 0, data.length]];
|
|
3248
|
+
}) : [];
|
|
3249
|
+
ranges.sort((a, b) => (0, import_utf82.compareUTF8)(a[0], a[1], a[2], b[0], b[1], b[2]));
|
|
3250
|
+
return {
|
|
3251
|
+
field: new import_apache_arrow6.Field("in", bool, argument.field.nullable || literals.some((value) => value.field.nullable)),
|
|
3252
|
+
hasKernel: argument.hasKernel,
|
|
3253
|
+
prepare(batch, active, context) {
|
|
3254
|
+
const source = argument.prepare(batch, active, context);
|
|
3255
|
+
return {
|
|
3256
|
+
get(row) {
|
|
3257
|
+
if (utf8 || binary) {
|
|
3258
|
+
const data = binary ? source.get(row) : null;
|
|
3259
|
+
const value = utf8 ? bytes(source, row) : data === null ? null : [data, 0, data.length];
|
|
3260
|
+
if (!value)
|
|
3261
|
+
return null;
|
|
3262
|
+
if (containsByteRange(ranges, value))
|
|
3263
|
+
return true;
|
|
3264
|
+
} else {
|
|
3265
|
+
const value = source.get(row);
|
|
3266
|
+
if (value === null)
|
|
3267
|
+
return null;
|
|
3268
|
+
if (!Number.isNaN(value) && values.has(value))
|
|
3269
|
+
return true;
|
|
3270
|
+
}
|
|
3271
|
+
return values.has(null) ? null : false;
|
|
3272
|
+
}
|
|
3273
|
+
};
|
|
3274
|
+
}
|
|
3275
|
+
};
|
|
3276
|
+
}
|
|
3277
|
+
function repeatUTF8(encoded, active) {
|
|
3278
|
+
const count = active.reduce((sum, value) => sum + value, 0);
|
|
3279
|
+
const data = new Uint8Array(count * encoded.length);
|
|
3280
|
+
const offsets = new Int32Array(active.length + 1);
|
|
3281
|
+
const validity = new Uint8Array(Math.ceil(active.length / 8));
|
|
3282
|
+
let position = 0;
|
|
3283
|
+
for (let row = 0; row < active.length; row++) {
|
|
3284
|
+
if (active[row]) {
|
|
3285
|
+
data.set(encoded, position);
|
|
3286
|
+
position += encoded.length;
|
|
3287
|
+
validity[row >> 3] |= 1 << (row & 7);
|
|
3288
|
+
}
|
|
3289
|
+
offsets[row + 1] = position;
|
|
3290
|
+
}
|
|
3291
|
+
return (0, import_apache_arrow6.makeVector)((0, import_apache_arrow6.makeData)({
|
|
3292
|
+
type: new import_apache_arrow6.Utf8(),
|
|
3293
|
+
length: active.length,
|
|
3294
|
+
data,
|
|
3295
|
+
valueOffsets: offsets,
|
|
3296
|
+
nullBitmap: validity,
|
|
3297
|
+
nullCount: active.length - count
|
|
3298
|
+
}));
|
|
3299
|
+
}
|
|
3300
|
+
function containsByteRange(ranges, value) {
|
|
3301
|
+
let low = 0;
|
|
3302
|
+
let high = ranges.length - 1;
|
|
3303
|
+
while (low <= high) {
|
|
3304
|
+
const middle = low + high >>> 1;
|
|
3305
|
+
const candidate = ranges[middle];
|
|
3306
|
+
const order = (0, import_utf82.compareUTF8)(value[0], value[1], value[2], candidate[0], candidate[1], candidate[2]);
|
|
3307
|
+
if (order === 0)
|
|
3308
|
+
return true;
|
|
3309
|
+
if (order < 0)
|
|
3310
|
+
high = middle - 1;
|
|
3311
|
+
else
|
|
3312
|
+
low = middle + 1;
|
|
3313
|
+
}
|
|
3314
|
+
return false;
|
|
3315
|
+
}
|
|
3316
|
+
function bindLike(args, options, insensitive) {
|
|
3317
|
+
if (args.length < 2 || args.length > 3)
|
|
3318
|
+
throw new Error("LIKE requires two or three arguments");
|
|
3319
|
+
const value = bind(args[0], options, new import_apache_arrow6.Utf8());
|
|
3320
|
+
const pattern = bind(args[1], options, new import_apache_arrow6.Utf8());
|
|
3321
|
+
requireType(value, "utf8");
|
|
3322
|
+
requireType(pattern, "utf8");
|
|
3323
|
+
const escape = args[2];
|
|
3324
|
+
if (escape && (escape.type !== "Literal" || typeof escape.value !== "string")) {
|
|
3325
|
+
throw new Error("LIKE ESCAPE requires a literal character");
|
|
3326
|
+
}
|
|
3327
|
+
const compile2 = (text) => compileUTF8Like(text, insensitive, escape == null ? void 0 : escape.value);
|
|
3328
|
+
const literal3 = args[1].type === "Literal" && typeof args[1].value === "string";
|
|
3329
|
+
const matcher = literal3 ? compile2(args[1].value) : void 0;
|
|
3330
|
+
return {
|
|
3331
|
+
field: new import_apache_arrow6.Field("like", bool, value.field.nullable || pattern.field.nullable),
|
|
3332
|
+
hasKernel: value.hasKernel || pattern.hasKernel,
|
|
3333
|
+
prepare(batch, active, context) {
|
|
3334
|
+
const source = value.prepare(batch, active, context);
|
|
3335
|
+
const patterns = pattern.prepare(batch, active, context);
|
|
3336
|
+
return {
|
|
3337
|
+
get(row) {
|
|
3338
|
+
const range = bytes(source, row);
|
|
3339
|
+
const text = patterns.get(row);
|
|
3340
|
+
if (!range || text === null)
|
|
3341
|
+
return null;
|
|
3342
|
+
return (matcher || compile2(text))(...range);
|
|
3343
|
+
}
|
|
3344
|
+
};
|
|
3345
|
+
}
|
|
3346
|
+
};
|
|
3347
|
+
}
|
|
3348
|
+
function convertByte(value, normalized) {
|
|
3349
|
+
if (!Number.isFinite(value))
|
|
3350
|
+
throw new ExpressionEvaluationError("conversion", "Byte conversion requires a finite number");
|
|
3351
|
+
if (normalized) {
|
|
3352
|
+
if (value < 0 || value > 1)
|
|
3353
|
+
throw new ExpressionEvaluationError("domain", "PACK_UNORM8 requires values in [0, 1]");
|
|
3354
|
+
return Math.round(value * 255);
|
|
3355
|
+
}
|
|
3356
|
+
const integer = Math.trunc(value);
|
|
3357
|
+
if (integer < 0 || integer > 255)
|
|
3358
|
+
throw new ExpressionEvaluationError("overflow", "UINT8 cast overflow");
|
|
3359
|
+
return integer;
|
|
3360
|
+
}
|
|
3361
|
+
function bindPackUnorm8(arguments_, options) {
|
|
3362
|
+
if (arguments_.length !== 1)
|
|
3363
|
+
throw new Error("PACK_UNORM8 requires one fixed-size numeric array");
|
|
3364
|
+
const argument = bind(arguments_[0], options, void 0, true);
|
|
3365
|
+
const inputType = argument.field.type;
|
|
3366
|
+
if (!import_apache_arrow6.DataType.isFixedSizeList(inputType) || !inputType.listSize || inputType.listSize > 4096)
|
|
3367
|
+
throw new Error("PACK_UNORM8 requires a fixed-size numeric array of 1 to 4096 components");
|
|
3368
|
+
const width = inputType.listSize;
|
|
3369
|
+
const child = inputType.children[0];
|
|
3370
|
+
if (!import_apache_arrow6.DataType.isInt(valueType(child.type)) && !import_apache_arrow6.DataType.isFloat(valueType(child.type)))
|
|
3371
|
+
throw new Error("PACK_UNORM8 requires numeric components");
|
|
3372
|
+
const type = new import_apache_arrow6.FixedSizeList(width, new import_apache_arrow6.Field("value", new import_apache_arrow6.Uint8(), false));
|
|
3373
|
+
const field = new import_apache_arrow6.Field("pack_unorm8", type, argument.field.nullable || child.nullable);
|
|
3374
|
+
return {
|
|
3375
|
+
field,
|
|
3376
|
+
hasKernel: true,
|
|
3377
|
+
prepare(batch, active, context) {
|
|
3378
|
+
var _a2;
|
|
3379
|
+
const input = argument.prepare(batch, active, context).vector;
|
|
3380
|
+
const parent = new ColumnReader(input);
|
|
3381
|
+
const components = new ColumnReader(input.getChildAt(0));
|
|
3382
|
+
const result = nullTupleVector(type, active.length);
|
|
3383
|
+
const data = result.data[0];
|
|
3384
|
+
const bytes2 = data.children[0].values;
|
|
3385
|
+
const rowValues = new Float64Array(width);
|
|
3386
|
+
for (let row = 0; row < active.length; row++) {
|
|
3387
|
+
if (!active[row] || ((_a2 = context.failed) == null ? void 0 : _a2[row]) || !parent.isValid(row))
|
|
3388
|
+
continue;
|
|
3389
|
+
const start = row * width;
|
|
3390
|
+
let valid = true;
|
|
3391
|
+
for (let component = 0; component < width; component++) {
|
|
3392
|
+
const value = components.get(start + component);
|
|
3393
|
+
if (value === null) {
|
|
3394
|
+
valid = false;
|
|
3395
|
+
break;
|
|
3396
|
+
}
|
|
3397
|
+
rowValues[component] = Number(value);
|
|
3398
|
+
}
|
|
3399
|
+
if (!valid)
|
|
3400
|
+
continue;
|
|
3401
|
+
try {
|
|
3402
|
+
for (let component = 0; component < width; component++)
|
|
3403
|
+
bytes2[start + component] = convertByte(rowValues[component], true);
|
|
3404
|
+
} catch (error) {
|
|
3405
|
+
if (!context.failed || !isExpressionEvaluationError(error))
|
|
3406
|
+
throw error;
|
|
3407
|
+
context.failed[row] = 1;
|
|
3408
|
+
bytes2.fill(0, start, start + width);
|
|
3409
|
+
continue;
|
|
3410
|
+
}
|
|
3411
|
+
data.setValid(row, true);
|
|
3412
|
+
}
|
|
3413
|
+
return vectorReader(result);
|
|
3414
|
+
}
|
|
3415
|
+
};
|
|
3416
|
+
}
|
|
3417
|
+
function isNestedType(type) {
|
|
3418
|
+
return import_apache_arrow6.DataType.isStruct(type) || import_apache_arrow6.DataType.isList(type) || import_apache_arrow6.DataType.isFixedSizeList(type);
|
|
3419
|
+
}
|
|
3420
|
+
function nestedValue(value, type) {
|
|
3421
|
+
if (value == null)
|
|
3422
|
+
return null;
|
|
3423
|
+
if (import_apache_arrow6.DataType.isList(type) || import_apache_arrow6.DataType.isFixedSizeList(type))
|
|
3424
|
+
return Array.from(value, (item) => nestedValue(item, type.valueType));
|
|
3425
|
+
if (import_apache_arrow6.DataType.isStruct(type))
|
|
3426
|
+
return Object.fromEntries(type.children.map((field) => [field.name, nestedValue(value[field.name], field.type)]));
|
|
3427
|
+
return value;
|
|
3428
|
+
}
|
|
3429
|
+
function nestedVector(vector, type, active) {
|
|
3430
|
+
if (!active.includes(0))
|
|
3431
|
+
return vector;
|
|
3432
|
+
return (0, import_apache_arrow6.vectorFromArray)(Array.from(active, (valid, row) => valid ? nestedValue(vector.get(row), type) : null), type);
|
|
3433
|
+
}
|
|
3434
|
+
function bindNestedExtract(name2, args, options) {
|
|
3435
|
+
if (args.length !== 2)
|
|
3436
|
+
throw new Error(`${name2} requires two arguments`);
|
|
3437
|
+
const input = bind(args[0], options, void 0, true);
|
|
3438
|
+
const inputType = input.field.type;
|
|
3439
|
+
const selector = args[1];
|
|
3440
|
+
let child;
|
|
3441
|
+
let index;
|
|
3442
|
+
if (name2 === "LIST_EXTRACT") {
|
|
3443
|
+
if (!import_apache_arrow6.DataType.isList(inputType) && !import_apache_arrow6.DataType.isFixedSizeList(inputType))
|
|
3444
|
+
throw new Error("LIST_EXTRACT requires an Arrow List or FixedSizeList");
|
|
3445
|
+
child = inputType.children[0];
|
|
3446
|
+
index = bind(selector, options);
|
|
3447
|
+
if (!import_apache_arrow6.DataType.isInt(index.field.type) && !import_apache_arrow6.DataType.isFloat(index.field.type) && !import_apache_arrow6.DataType.isNull(index.field.type))
|
|
3448
|
+
throw new Error("LIST_EXTRACT requires an integer index");
|
|
3449
|
+
} else {
|
|
3450
|
+
if (!import_apache_arrow6.DataType.isStruct(inputType))
|
|
3451
|
+
throw new Error("STRUCT_EXTRACT requires an Arrow Struct");
|
|
3452
|
+
if (selector.type !== "Literal" || typeof selector.value !== "string")
|
|
3453
|
+
throw new Error("STRUCT_EXTRACT requires a literal field name");
|
|
3454
|
+
const field2 = inputType.children.find((candidate) => candidate.name === selector.value);
|
|
3455
|
+
if (!field2)
|
|
3456
|
+
throw new Error(`Unknown Arrow struct field: ${selector.value}`);
|
|
3457
|
+
child = field2;
|
|
3458
|
+
}
|
|
3459
|
+
const field = new import_apache_arrow6.Field(child.name, valueType(child.type), true, child.metadata);
|
|
3460
|
+
return {
|
|
3461
|
+
field,
|
|
3462
|
+
hasKernel: true,
|
|
3463
|
+
prepare(batch, active, context) {
|
|
3464
|
+
const inputValues = input.prepare(batch, active, context).vector;
|
|
3465
|
+
const positions = index == null ? void 0 : index.prepare(batch, active, context);
|
|
3466
|
+
const childValues = index ? void 0 : inputValues.getChild(child.name);
|
|
3467
|
+
const values = Array.from(active, (valid, row) => {
|
|
3468
|
+
if (!valid || !inputValues.isValid(row))
|
|
3469
|
+
return null;
|
|
3470
|
+
if (!positions)
|
|
3471
|
+
return nestedValue(childValues.get(row), field.type);
|
|
3472
|
+
const position = positions.get(row);
|
|
3473
|
+
if (position === null)
|
|
3474
|
+
return null;
|
|
3475
|
+
const offset = Number(position);
|
|
3476
|
+
if (!Number.isSafeInteger(offset))
|
|
3477
|
+
return null;
|
|
3478
|
+
const list = inputValues.get(row);
|
|
3479
|
+
const element = offset < 0 ? list.length + offset : offset - 1;
|
|
3480
|
+
return element >= 0 && element < list.length ? nestedValue(list.get(element), field.type) : null;
|
|
3481
|
+
});
|
|
3482
|
+
return vectorReader((0, import_apache_arrow6.vectorFromArray)(values, field.type));
|
|
3483
|
+
}
|
|
3484
|
+
};
|
|
3485
|
+
}
|
|
3486
|
+
function bindCall(ast, options, expected) {
|
|
3487
|
+
var _a2, _b;
|
|
3488
|
+
if (ast.callee.type !== "Identifier")
|
|
3489
|
+
throw new Error("Only registered named function calls are supported");
|
|
3490
|
+
let name2 = ast.callee.name;
|
|
3491
|
+
const recovery = recoveryName(ast);
|
|
3492
|
+
if (recovery)
|
|
3493
|
+
return bindRecovery(recovery, ast.arguments, options, expected);
|
|
3494
|
+
if (ast.sqlFunction) {
|
|
3495
|
+
const matches = [
|
|
3496
|
+
...options.functionLookup.keys(),
|
|
3497
|
+
"array",
|
|
3498
|
+
"array_float16",
|
|
3499
|
+
"array_float32",
|
|
3500
|
+
"array_float64",
|
|
3501
|
+
"array_uint8",
|
|
3502
|
+
"PACK_UNORM8",
|
|
3503
|
+
"EPOCH_MS",
|
|
3504
|
+
"LIST_EXTRACT",
|
|
3505
|
+
"STRUCT_EXTRACT",
|
|
3506
|
+
"cast",
|
|
3507
|
+
"like",
|
|
3508
|
+
"ilike",
|
|
3509
|
+
"concat",
|
|
3510
|
+
"concatStrict",
|
|
3511
|
+
"upper",
|
|
3512
|
+
"lower",
|
|
3513
|
+
"capitalize",
|
|
3514
|
+
"isNull",
|
|
3515
|
+
"isNotNull",
|
|
3516
|
+
"parseNumber",
|
|
3517
|
+
"parseBigInt",
|
|
3518
|
+
"parseBoolean",
|
|
3519
|
+
"toFloat64",
|
|
3520
|
+
"toInt32",
|
|
3521
|
+
"toInt64"
|
|
3522
|
+
].filter((key) => key.toUpperCase() === name2.toUpperCase());
|
|
3523
|
+
if (matches.length > 1)
|
|
3524
|
+
throw new Error(`Ambiguous SQL function: ${name2}`);
|
|
3525
|
+
name2 = matches[0] ?? name2;
|
|
3526
|
+
}
|
|
3527
|
+
if (name2 === "LIST_EXTRACT" || name2 === "STRUCT_EXTRACT")
|
|
3528
|
+
return bindNestedExtract(name2, ast.arguments, options);
|
|
3529
|
+
if (name2 === "PACK_UNORM8")
|
|
3530
|
+
return bindPackUnorm8(ast.arguments, options);
|
|
3531
|
+
if (["array", "array_float16", "array_float32", "array_float64", "array_uint8"].includes(name2)) {
|
|
3532
|
+
if (!ast.arguments.length || ast.arguments.length > 4096)
|
|
3533
|
+
throw new Error("ARRAY requires 1 to 4096 numeric expressions");
|
|
3534
|
+
const arguments_2 = ast.arguments.map((value) => bind(value, options));
|
|
3535
|
+
if (arguments_2.some((value) => !import_apache_arrow6.DataType.isFloat(valueType(value.field.type)) && !import_apache_arrow6.DataType.isInt(valueType(value.field.type))))
|
|
3536
|
+
throw new Error("ARRAY requires numeric expressions");
|
|
3537
|
+
const component = name2 === "array_uint8" ? new import_apache_arrow6.Uint8() : name2 === "array_float16" ? new import_apache_arrow6.Float16() : name2 === "array_float32" ? new import_apache_arrow6.Float32() : name2 === "array_float64" || arguments_2.every((value) => import_apache_arrow6.DataType.isInt(valueType(value.field.type))) || arguments_2.some((value) => typeKey(value.field.type) === "f2") ? new import_apache_arrow6.Float64() : arguments_2.some((value) => typeKey(value.field.type) === "f1") ? new import_apache_arrow6.Float32() : new import_apache_arrow6.Float16();
|
|
3538
|
+
const type = new import_apache_arrow6.FixedSizeList(arguments_2.length, new import_apache_arrow6.Field("value", component, false));
|
|
3539
|
+
const field2 = new import_apache_arrow6.Field("array", type, arguments_2.some((value) => value.field.nullable));
|
|
3540
|
+
return {
|
|
3541
|
+
field: field2,
|
|
3542
|
+
hasKernel: true,
|
|
3543
|
+
prepare(batch, active, context) {
|
|
3544
|
+
const values = arguments_2.map((value) => prepareInput(value, batch, active, context, arguments_2.some((argument) => argument.hasKernel)));
|
|
3545
|
+
const vector = nullTupleVector(type, active.length), data = vector.data[0], output2 = import_apache_arrow6.DataType.isFloat(component) && component.precision === import_apache_arrow6.Precision.HALF ? float16Values(data.children[0].values) : data.children[0].values;
|
|
3546
|
+
const rowValues = new Array(values.length);
|
|
3547
|
+
for (let row = 0; row < active.length; row++) {
|
|
3548
|
+
if (!active[row])
|
|
3549
|
+
continue;
|
|
3550
|
+
for (let column = 0; column < values.length; column++)
|
|
3551
|
+
rowValues[column] = values[column].get(row);
|
|
3552
|
+
if (rowValues.some((value) => value === null))
|
|
3553
|
+
continue;
|
|
3554
|
+
try {
|
|
3555
|
+
for (let column = 0; column < values.length; column++) {
|
|
3556
|
+
const index = row * values.length + column;
|
|
3557
|
+
const value = Number(rowValues[column]);
|
|
3558
|
+
output2[index] = name2 === "array_uint8" ? convertByte(value, false) : value;
|
|
3559
|
+
if (Number.isFinite(value) && !Number.isFinite(output2[index]))
|
|
3560
|
+
throw new ExpressionEvaluationError("overflow", "ARRAY cast overflow");
|
|
3561
|
+
}
|
|
3562
|
+
} catch (error) {
|
|
3563
|
+
if (!context.failed || !isExpressionEvaluationError(error))
|
|
3564
|
+
throw error;
|
|
3565
|
+
context.failed[row] = 1;
|
|
3566
|
+
output2.fill(0, row * values.length, (row + 1) * values.length);
|
|
3567
|
+
continue;
|
|
3568
|
+
}
|
|
3569
|
+
data.setValid(row, true);
|
|
3570
|
+
}
|
|
3571
|
+
return vectorReader(vector);
|
|
3572
|
+
}
|
|
3573
|
+
};
|
|
3574
|
+
}
|
|
3575
|
+
if (name2 === "like" || name2 === "ilike")
|
|
3576
|
+
return bindLike(ast.arguments, options, name2 === "ilike");
|
|
3577
|
+
if (name2 === "upper" || name2 === "lower" || name2 === "capitalize") {
|
|
3578
|
+
if (ast.arguments.length !== 1)
|
|
3579
|
+
throw new Error("Case conversion requires one argument");
|
|
3580
|
+
const arg = bind(ast.arguments[0], options, new import_apache_arrow6.Utf8());
|
|
3581
|
+
requireType(arg, "utf8");
|
|
3582
|
+
return {
|
|
3583
|
+
field: new import_apache_arrow6.Field(name2, new import_apache_arrow6.Utf8(), arg.field.nullable),
|
|
3584
|
+
hasKernel: arg.hasKernel,
|
|
3585
|
+
prepare(batch, active, context) {
|
|
3586
|
+
const input = arg.prepare(batch, active, context);
|
|
3587
|
+
return {
|
|
3588
|
+
get(row) {
|
|
3589
|
+
const value = input.get(row);
|
|
3590
|
+
if (!value)
|
|
3591
|
+
return value;
|
|
3592
|
+
if (name2 === "upper")
|
|
3593
|
+
return value.toUpperCase();
|
|
3594
|
+
if (name2 === "lower")
|
|
3595
|
+
return value.toLowerCase();
|
|
3596
|
+
const first = String.fromCodePoint(value.codePointAt(0));
|
|
3597
|
+
return first.toUpperCase() + value.slice(first.length);
|
|
3598
|
+
}
|
|
3599
|
+
};
|
|
3600
|
+
}
|
|
3601
|
+
};
|
|
3602
|
+
}
|
|
3603
|
+
if (name2 === "concat" || name2 === "concatStrict") {
|
|
3604
|
+
if (!ast.arguments.length)
|
|
3605
|
+
throw new Error("CONCAT requires at least one argument");
|
|
3606
|
+
const args = ast.arguments.map((arg) => bind(arg, options, new import_apache_arrow6.Utf8()));
|
|
3607
|
+
for (const arg of args)
|
|
3608
|
+
requireType(arg, "utf8");
|
|
3609
|
+
const strict = name2 === "concatStrict";
|
|
3610
|
+
return {
|
|
3611
|
+
field: new import_apache_arrow6.Field("concat", new import_apache_arrow6.Utf8(), strict && args.some((arg) => arg.field.nullable)),
|
|
3612
|
+
hasKernel: args.some((arg) => arg.hasKernel),
|
|
3613
|
+
prepare(batch, active, context) {
|
|
3614
|
+
const inputs = args.map((arg) => arg.prepare(batch, active, context));
|
|
3615
|
+
return {
|
|
3616
|
+
get(row) {
|
|
3617
|
+
let result2 = "";
|
|
3618
|
+
for (const input of inputs) {
|
|
3619
|
+
const value = input.get(row);
|
|
3620
|
+
if (value === null && strict)
|
|
3621
|
+
return null;
|
|
3622
|
+
if (value !== null)
|
|
3623
|
+
result2 += value;
|
|
3624
|
+
}
|
|
3625
|
+
return result2;
|
|
3626
|
+
}
|
|
3627
|
+
};
|
|
3628
|
+
}
|
|
3629
|
+
};
|
|
3630
|
+
}
|
|
3631
|
+
if (name2 === "in")
|
|
3632
|
+
return bindMembership(ast.arguments, options);
|
|
3633
|
+
if (name2 === "EPOCH_MS") {
|
|
3634
|
+
if (ast.arguments.length !== 1)
|
|
3635
|
+
throw new Error("EPOCH_MS requires one argument");
|
|
3636
|
+
const argument = bind(ast.arguments[0], options);
|
|
3637
|
+
const key = typeKey(argument.field.type);
|
|
3638
|
+
if (key !== "date32" && !key.startsWith("timestamp_us:")) {
|
|
3639
|
+
throw new Error("EPOCH_MS requires DATE or microsecond TIMESTAMP");
|
|
3640
|
+
}
|
|
3641
|
+
return {
|
|
3642
|
+
field: new import_apache_arrow6.Field("epoch_ms", new import_apache_arrow6.Float64(), argument.field.nullable),
|
|
3643
|
+
hasKernel: argument.hasKernel,
|
|
3644
|
+
prepare(batch, active, context) {
|
|
3645
|
+
const input = argument.prepare(batch, active, context);
|
|
3646
|
+
return {
|
|
3647
|
+
get(row) {
|
|
3648
|
+
const value = input.get(row);
|
|
3649
|
+
if (value === null)
|
|
3650
|
+
return null;
|
|
3651
|
+
return key === "date32" ? Number(value) * 864e5 : Number(value / 1000n) + Number(value % 1000n) / 1e3;
|
|
3652
|
+
}
|
|
3653
|
+
};
|
|
3654
|
+
}
|
|
3655
|
+
};
|
|
3656
|
+
}
|
|
3657
|
+
if (name2 === "cast") {
|
|
3658
|
+
if (ast.arguments.length !== 2 || ((_a2 = ast.arguments[1]) == null ? void 0 : _a2.type) !== "Literal")
|
|
3659
|
+
throw new Error("cast requires a value and a literal scalar target");
|
|
3660
|
+
const argument = bind(ast.arguments[0], options);
|
|
3661
|
+
const target = ast.arguments[1].value;
|
|
3662
|
+
if (target === "geometry")
|
|
3663
|
+
return bindGeometryCast(argument);
|
|
3664
|
+
const cast = bindCast(argument.field.type, target);
|
|
3665
|
+
return {
|
|
3666
|
+
field: new import_apache_arrow6.Field("cast", cast.type, argument.field.nullable),
|
|
3667
|
+
hasKernel: argument.hasKernel,
|
|
3668
|
+
prepare(batch, active, context) {
|
|
3669
|
+
const child = argument.prepare(batch, active, context);
|
|
3670
|
+
return {
|
|
3671
|
+
get: (row) => cast.textInput && !import_apache_arrow6.DataType.isUtf8(valueType(cast.type)) ? cast.evaluate(null, bytes(child, row)) : cast.evaluate(child.get(row))
|
|
3672
|
+
};
|
|
3673
|
+
}
|
|
3674
|
+
};
|
|
3675
|
+
}
|
|
3676
|
+
const conversion = ["toFloat64", "toInt32", "toInt64"].includes(name2);
|
|
3677
|
+
const nullTest = name2 === "isNull" || name2 === "isNotNull";
|
|
3678
|
+
if (conversion || nullTest || ["parseNumber", "parseBigInt", "parseBoolean"].includes(name2)) {
|
|
3679
|
+
if (ast.arguments.length !== 1)
|
|
3680
|
+
throw new Error(`${name2} requires one argument`);
|
|
3681
|
+
const argument = bind(ast.arguments[0], options);
|
|
3682
|
+
if (conversion)
|
|
3683
|
+
requireNumeric(argument);
|
|
3684
|
+
else if (!nullTest)
|
|
3685
|
+
requireType(argument, "utf8");
|
|
3686
|
+
const type = name2 === "parseNumber" || name2 === "toFloat64" ? new import_apache_arrow6.Float64() : name2 === "toInt32" ? new import_apache_arrow6.Int32() : name2 === "parseBigInt" || name2 === "toInt64" ? new import_apache_arrow6.Int64() : bool;
|
|
3687
|
+
const validate2 = createValueValidator(type);
|
|
3688
|
+
const round2 = roundToType(type);
|
|
3689
|
+
const field2 = new import_apache_arrow6.Field(name2, type, conversion ? argument.field.nullable : !nullTest);
|
|
3690
|
+
return {
|
|
3691
|
+
field: field2,
|
|
3692
|
+
hasKernel: argument.hasKernel,
|
|
3693
|
+
prepare(batch, active, context) {
|
|
3694
|
+
const child = argument.prepare(batch, active, context);
|
|
3695
|
+
return {
|
|
3696
|
+
get: (row) => {
|
|
3697
|
+
if (nullTest) {
|
|
3698
|
+
const missing = child.isNull ? child.isNull(row) : child.get(row) === null;
|
|
3699
|
+
return name2 === "isNull" ? missing : !missing;
|
|
3700
|
+
}
|
|
3701
|
+
if (conversion) {
|
|
3702
|
+
const value = child.get(row);
|
|
3703
|
+
if (value === null)
|
|
3704
|
+
return null;
|
|
3705
|
+
if (name2 === "toInt64" && typeof value === "number" && !Number.isInteger(value))
|
|
3706
|
+
throw new ExpressionEvaluationError("conversion", "Int64 conversion requires a finite integer");
|
|
3707
|
+
const result3 = name2 === "toInt64" ? BigInt(value) : Number(value);
|
|
3708
|
+
validate2(result3);
|
|
3709
|
+
return result3;
|
|
3710
|
+
}
|
|
3711
|
+
const range = bytes(child, row);
|
|
3712
|
+
if (!range)
|
|
3713
|
+
return null;
|
|
3714
|
+
const [buffer, start, end] = range;
|
|
3715
|
+
const result2 = (name2 === "parseNumber" ? (0, import_utf82.parseUTF8Number)(buffer, start, end) : name2 === "parseBigInt" ? (0, import_utf82.parseUTF8BigInt)(buffer, start, end) : (0, import_utf82.parseUTF8Boolean)(buffer, start, end)) ?? null;
|
|
3716
|
+
validate2(result2);
|
|
3717
|
+
return round2(result2);
|
|
3718
|
+
}
|
|
3719
|
+
};
|
|
3720
|
+
}
|
|
3721
|
+
};
|
|
3722
|
+
}
|
|
3723
|
+
const group = options.functionLookup.get(name2);
|
|
3724
|
+
if (!group)
|
|
3725
|
+
throw new Error(`Unknown Arrow function: ${name2}`);
|
|
3726
|
+
const inferred = ast.arguments.map((argument) => needsArgumentContext(argument) ? void 0 : bind(argument, options, void 0, true));
|
|
3727
|
+
const candidates = [];
|
|
3728
|
+
const allLiteralArguments = ast.arguments.every(isContextualLiteral);
|
|
3729
|
+
let contextualError;
|
|
3730
|
+
for (const prototype of group.prototypes) {
|
|
3731
|
+
if (ast.arguments.length !== prototype.arguments.length)
|
|
3732
|
+
continue;
|
|
3733
|
+
let cost = 0;
|
|
3734
|
+
const arguments_2 = [];
|
|
3735
|
+
let compatible = true;
|
|
3736
|
+
for (let index = 0; index < ast.arguments.length; index++) {
|
|
3737
|
+
const argument = ast.arguments[index], field2 = prototype.arguments[index], source = inferred[index];
|
|
3738
|
+
let bound;
|
|
3739
|
+
if (isContextualLiteral(argument) || !source) {
|
|
3740
|
+
try {
|
|
3741
|
+
bound = bind(argument, options, field2.type, !prototype.evaluateScalar);
|
|
3742
|
+
} catch (error) {
|
|
3743
|
+
contextualError = error;
|
|
3744
|
+
compatible = false;
|
|
3745
|
+
break;
|
|
3746
|
+
}
|
|
3747
|
+
if (source && allLiteralArguments && physicalKey(valueType(source.field.type)) !== physicalKey(field2.type))
|
|
3748
|
+
cost += 4;
|
|
3749
|
+
} else {
|
|
3750
|
+
bound = source;
|
|
3751
|
+
}
|
|
3752
|
+
const sourceType = valueType(bound.field.type);
|
|
3753
|
+
if (import_apache_arrow6.DataType.isDictionary(bound.field.type) && !import_apache_arrow6.DataType.isDictionary(field2.type))
|
|
3754
|
+
cost += 1;
|
|
3755
|
+
if (physicalKey(import_apache_arrow6.DataType.isDictionary(field2.type) ? bound.field.type : sourceType) !== physicalKey(field2.type)) {
|
|
3756
|
+
if (import_apache_arrow6.DataType.isFloat(sourceType) && import_apache_arrow6.DataType.isFloat(field2.type) && sourceType.precision < field2.type.precision) {
|
|
3757
|
+
cost += field2.type.precision - sourceType.precision;
|
|
3758
|
+
const child = bound, round2 = roundToType(field2.type);
|
|
3759
|
+
bound = {
|
|
3760
|
+
field: new import_apache_arrow6.Field(child.field.name, field2.type, child.field.nullable, child.field.metadata),
|
|
3761
|
+
hasKernel: child.hasKernel,
|
|
3762
|
+
prepare(batch, active, context) {
|
|
3763
|
+
const prepared = child.prepare(batch, active, context);
|
|
3764
|
+
return { get: (row) => round2(prepared.get(row)) };
|
|
3765
|
+
}
|
|
3766
|
+
};
|
|
3767
|
+
} else {
|
|
3768
|
+
compatible = false;
|
|
3769
|
+
break;
|
|
3770
|
+
}
|
|
3771
|
+
}
|
|
3772
|
+
if ([...field2.metadata].some(([key, value]) => bound.field.metadata.get(key) !== value) || prototype.nullHandling === "call" && bound.field.nullable && !field2.nullable) {
|
|
3773
|
+
compatible = false;
|
|
3774
|
+
break;
|
|
3775
|
+
}
|
|
3776
|
+
arguments_2.push(bound);
|
|
3777
|
+
}
|
|
3778
|
+
if (compatible)
|
|
3779
|
+
candidates.push({ prototype, arguments: arguments_2, cost });
|
|
3780
|
+
}
|
|
3781
|
+
const untypedNumeric = (value) => value.type === "Literal" && (value.value === null || typeof value.value === "number") || isSignedLiteral(value) || Boolean(recoveryName(value) && value.arguments.every(untypedNumeric)) || value.type === "ConditionalExpression" && untypedNumeric(value.consequent) && untypedNumeric(value.alternate);
|
|
3782
|
+
const allFloat64 = (prototype) => prototype.arguments.every((field2) => import_apache_arrow6.DataType.isFloat(field2.type) && field2.type.precision === import_apache_arrow6.Precision.DOUBLE);
|
|
3783
|
+
if (ast.arguments.every(untypedNumeric) && candidates.every((candidate) => candidate.prototype.arguments.every((field2) => import_apache_arrow6.DataType.isInt(field2.type) || import_apache_arrow6.DataType.isFloat(field2.type))) && candidates.some((candidate) => allFloat64(candidate.prototype))) {
|
|
3784
|
+
for (const candidate of candidates)
|
|
3785
|
+
if (!allFloat64(candidate.prototype))
|
|
3786
|
+
candidate.cost++;
|
|
3787
|
+
}
|
|
3788
|
+
candidates.sort((a, b) => a.cost - b.cost);
|
|
3789
|
+
if (!candidates.length && group.prototypes.length === 1 && contextualError)
|
|
3790
|
+
throw contextualError;
|
|
3791
|
+
if (!candidates.length)
|
|
3792
|
+
throw new Error(`Argument type mismatch or wrong argument count for ${name2}`);
|
|
3793
|
+
if (((_b = candidates[1]) == null ? void 0 : _b.cost) === candidates[0].cost)
|
|
3794
|
+
throw new Error(`Ambiguous Arrow function call: ${name2}`);
|
|
3795
|
+
const declaration = candidates[0].prototype;
|
|
3796
|
+
const arguments_ = candidates[0].arguments;
|
|
3797
|
+
const evaluateVector = declaration.evaluateScalar ? void 0 : declaration.evaluate;
|
|
3798
|
+
const result = declaration.result;
|
|
3799
|
+
const tuple = isTupleType(result.type);
|
|
3800
|
+
if (tuple && declaration.evaluateScalar)
|
|
3801
|
+
throw new Error("Fixed-size list results require a vector-only function");
|
|
3802
|
+
if (declaration.evaluateScalar)
|
|
3803
|
+
for (const argument of arguments_)
|
|
3804
|
+
typeKey(argument.field.type);
|
|
3805
|
+
if (!tuple)
|
|
3806
|
+
typeKey(result.type);
|
|
3807
|
+
const validate = tuple ? void 0 : createValueValidator(result.type);
|
|
3808
|
+
const round = tuple ? void 0 : roundToType(result.type);
|
|
3809
|
+
const field = new import_apache_arrow6.Field(result.name, valueType(result.type), result.nullable || declaration.nullHandling !== "call" && arguments_.some((argument) => argument.field.nullable), result.metadata);
|
|
3810
|
+
return {
|
|
3811
|
+
field,
|
|
3812
|
+
hasKernel: Boolean(evaluateVector) || arguments_.some((argument) => argument.hasKernel),
|
|
3813
|
+
prepare(batch, active, context) {
|
|
3814
|
+
const children = arguments_.map((argument) => prepareInput(argument, batch, active, context, Boolean(evaluateVector) || arguments_.some((value) => value.hasKernel)));
|
|
3815
|
+
let kernel;
|
|
3816
|
+
let mask = active;
|
|
3817
|
+
if (evaluateVector && active.some(Boolean)) {
|
|
3818
|
+
const vectors = children.map((child, index) => child.vector ?? output(arguments_[index].field, context.failed ? active.map((value, row) => value && !context.failed[row] ? 1 : 0) : active, child.get));
|
|
3819
|
+
const readers = vectors.map((vector) => new ColumnReader(vector));
|
|
3820
|
+
mask = active.map((value, row) => {
|
|
3821
|
+
var _a3;
|
|
3822
|
+
return value && !((_a3 = context.failed) == null ? void 0 : _a3[row]) && (declaration.nullHandling === "call" || readers.every((reader) => reader.isValid(row))) ? 1 : 0;
|
|
3823
|
+
});
|
|
3824
|
+
if (mask.some(Boolean)) {
|
|
3825
|
+
const vector = evaluateVector(vectors, {
|
|
3826
|
+
activeRows: mask,
|
|
3827
|
+
length: active.length,
|
|
3828
|
+
reportError(row, error) {
|
|
3829
|
+
if (!Number.isInteger(row) || row < 0 || row >= active.length || !mask[row])
|
|
3830
|
+
throw new Error("Kernel reported an inactive or invalid row");
|
|
3831
|
+
if (!context.failed || !isExpressionEvaluationError(error))
|
|
3832
|
+
throw error;
|
|
3833
|
+
context.failed[row] = 1;
|
|
3834
|
+
mask[row] = 0;
|
|
3835
|
+
}
|
|
3836
|
+
});
|
|
3837
|
+
if (vector.length !== active.length || (tuple ? physicalKey(vector.type) !== physicalKey(result.type) : typeKey(vector.type) !== typeKey(result.type)))
|
|
3838
|
+
throw new Error(`Invalid kernel result for ${name2}`);
|
|
3839
|
+
if (tuple) {
|
|
3840
|
+
validateTupleVector(vector, result, mask);
|
|
3841
|
+
return vectorReader(maskTupleVector(vector, mask));
|
|
3842
|
+
}
|
|
3843
|
+
kernel = sourceReader(vector);
|
|
3844
|
+
}
|
|
3845
|
+
}
|
|
3846
|
+
if (isTupleType(result.type))
|
|
3847
|
+
return vectorReader(nullTupleVector(result.type, active.length));
|
|
3848
|
+
return {
|
|
3849
|
+
get: (row) => {
|
|
3850
|
+
var _a3;
|
|
3851
|
+
let value;
|
|
3852
|
+
if (evaluateVector) {
|
|
3853
|
+
if (!mask[row] || !kernel)
|
|
3854
|
+
return null;
|
|
3855
|
+
value = kernel.get(row);
|
|
3856
|
+
} else {
|
|
3857
|
+
const args = children.map((child) => child.get(row));
|
|
3858
|
+
if ((_a3 = context.failed) == null ? void 0 : _a3[row])
|
|
3859
|
+
return null;
|
|
3860
|
+
if (declaration.nullHandling !== "call" && args.some((value2) => value2 === null))
|
|
3861
|
+
return null;
|
|
3862
|
+
value = declaration.evaluateScalar(...args);
|
|
3863
|
+
}
|
|
3864
|
+
if (value === null && !result.nullable)
|
|
3865
|
+
throw new Error(`Function ${name2} returned null for a non-nullable result`);
|
|
3866
|
+
validate(value);
|
|
3867
|
+
return round(value);
|
|
3868
|
+
}
|
|
3869
|
+
};
|
|
3870
|
+
}
|
|
3871
|
+
};
|
|
3872
|
+
}
|
|
3873
|
+
function roundToType(type) {
|
|
3874
|
+
if (typeKey(type) === "f0") {
|
|
3875
|
+
const round = roundFloat16();
|
|
3876
|
+
return (value) => value === null ? null : round(value);
|
|
3877
|
+
}
|
|
3878
|
+
return typeKey(type) === "f1" ? (value) => value === null ? null : Math.fround(value) : (value) => value;
|
|
3879
|
+
}
|
|
3880
|
+
function isSignedLiteral(ast) {
|
|
3881
|
+
return ast.type === "UnaryExpression" && (ast.operator === "+" || ast.operator === "-") && ast.argument.type === "Literal" && typeof ast.argument.value === "number";
|
|
3882
|
+
}
|
|
3883
|
+
function recoveryName(ast) {
|
|
3884
|
+
if (ast.type !== "CallExpression" || ast.callee.type !== "Identifier")
|
|
3885
|
+
return void 0;
|
|
3886
|
+
const name2 = ast.sqlFunction ? ast.callee.name.toUpperCase() : ast.callee.name;
|
|
3887
|
+
return name2 === "TRY" || name2 === "COALESCE" ? name2 : void 0;
|
|
3888
|
+
}
|
|
3889
|
+
function containsContextualArray(ast) {
|
|
3890
|
+
return ast.type === "ArrayExpression" || ast.type === "ConditionalExpression" && (containsContextualArray(ast.consequent) || containsContextualArray(ast.alternate)) || Boolean(recoveryName(ast) && ast.arguments.some(containsContextualArray));
|
|
3891
|
+
}
|
|
3892
|
+
function isContextualLiteral(ast) {
|
|
3893
|
+
return ast.type === "Literal" || ast.type === "ArrayExpression" || isSignedLiteral(ast) || Boolean(recoveryName(ast) && ast.arguments.every(isContextualLiteral)) || ast.type === "ConditionalExpression" && isContextualLiteral(ast.consequent) && isContextualLiteral(ast.alternate);
|
|
3894
|
+
}
|
|
3895
|
+
function needsArgumentContext(ast) {
|
|
3896
|
+
return ast.type === "ArrayExpression" || ast.type === "Literal" && ast.value === null || Boolean(recoveryName(ast) && ast.arguments.some(containsContextualArray)) || ast.type === "ConditionalExpression" && (needsArgumentContext(ast.consequent) || needsArgumentContext(ast.alternate));
|
|
3897
|
+
}
|
|
3898
|
+
|
|
3899
|
+
// dist/scales/arrow-domains.js
|
|
3900
|
+
function arrowScaleValue(value) {
|
|
3901
|
+
if (typeof value !== "bigint")
|
|
3902
|
+
return value;
|
|
3903
|
+
const numeric = Number(value);
|
|
3904
|
+
if (!Number.isSafeInteger(numeric))
|
|
3905
|
+
throw new RangeError("Scale integer exceeds the safe numeric range");
|
|
3906
|
+
return numeric;
|
|
3907
|
+
}
|
|
3908
|
+
function quantileSorted(sorted, probability) {
|
|
3909
|
+
if (!sorted.length || !Number.isFinite(probability) || probability < 0 || probability > 1) {
|
|
3910
|
+
throw new Error("Quantile requires nonempty finite samples and a probability in [0, 1]");
|
|
3911
|
+
}
|
|
3912
|
+
const index = (sorted.length - 1) * probability;
|
|
3913
|
+
const lower = Math.floor(index);
|
|
3914
|
+
const fraction = index - lower;
|
|
3915
|
+
return sorted[lower] + fraction * (sorted[Math.ceil(index)] - sorted[lower]);
|
|
3916
|
+
}
|
|
3917
|
+
function resolveArrowDomains(table, domains, functions) {
|
|
3918
|
+
const columns = /* @__PURE__ */ new Map();
|
|
3919
|
+
const needsSamples = /* @__PURE__ */ new Set();
|
|
3920
|
+
const needsCategories = /* @__PURE__ */ new Set();
|
|
3921
|
+
for (const domain of domains) {
|
|
3922
|
+
if (domain.type === "expression-extent")
|
|
3923
|
+
continue;
|
|
3924
|
+
if (domain.type === "percentile")
|
|
3925
|
+
needsSamples.add(domain.column);
|
|
3926
|
+
if (domain.type === "categories")
|
|
3927
|
+
needsCategories.add(domain.column);
|
|
3928
|
+
if (domain.type !== "fixed" && !table.getChild(domain.column)) {
|
|
3929
|
+
throw new Error(`Unknown scale domain column: ${domain.column}`);
|
|
3930
|
+
}
|
|
3931
|
+
}
|
|
3932
|
+
for (const domain of domains) {
|
|
3933
|
+
if (domain.type === "fixed" || domain.type === "expression-extent" || columns.has(domain.column))
|
|
3934
|
+
continue;
|
|
3935
|
+
const vector = table.getChild(domain.column);
|
|
3936
|
+
const state = {
|
|
3937
|
+
values: [],
|
|
3938
|
+
categories: /* @__PURE__ */ new Set(),
|
|
3939
|
+
min: Infinity,
|
|
3940
|
+
max: -Infinity
|
|
3941
|
+
};
|
|
3942
|
+
const samples = needsSamples.has(domain.column);
|
|
3943
|
+
for (let row = 0; row < vector.length; row++) {
|
|
3944
|
+
const value = arrowScaleValue(vector.get(row));
|
|
3945
|
+
if (value === null || value === void 0)
|
|
3946
|
+
continue;
|
|
3947
|
+
if (typeof value === "string") {
|
|
3948
|
+
if (needsCategories.has(domain.column))
|
|
3949
|
+
state.categories.add(value);
|
|
3950
|
+
continue;
|
|
3951
|
+
}
|
|
3952
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
3953
|
+
throw new Error(`Unsupported scale domain value in ${domain.column} at row ${row}`);
|
|
3954
|
+
}
|
|
3955
|
+
if (needsCategories.has(domain.column))
|
|
3956
|
+
state.categories.add(value);
|
|
3957
|
+
if (value < state.min)
|
|
3958
|
+
state.min = value;
|
|
3959
|
+
if (value > state.max)
|
|
3960
|
+
state.max = value;
|
|
3961
|
+
if (samples)
|
|
3962
|
+
state.values.push(value);
|
|
3963
|
+
}
|
|
3964
|
+
if (samples)
|
|
3965
|
+
state.values.sort((a, b) => a - b);
|
|
3966
|
+
columns.set(domain.column, state);
|
|
3967
|
+
}
|
|
3968
|
+
return domains.map((domain) => {
|
|
3969
|
+
if (domain.type === "fixed")
|
|
3970
|
+
return [...domain.values];
|
|
3971
|
+
if (domain.type === "expression-extent") {
|
|
3972
|
+
return resolveArrowExpressionStatistics(table, domain.expression, void 0, functions).domain;
|
|
3973
|
+
}
|
|
3974
|
+
const data = columns.get(domain.column);
|
|
3975
|
+
if (domain.type === "categories") {
|
|
3976
|
+
const values = [...data.categories];
|
|
3977
|
+
if (domain.order !== "first-seen")
|
|
3978
|
+
values.sort((a, b) => typeof a === "number" && typeof b === "number" ? a - b : a < b ? -1 : a > b ? 1 : 0);
|
|
3979
|
+
if (!values.length)
|
|
3980
|
+
throw new Error(`Empty categorical scale domain: ${domain.column}`);
|
|
3981
|
+
return values;
|
|
3982
|
+
}
|
|
3983
|
+
if (!Number.isFinite(data.min) || !Number.isFinite(data.max)) {
|
|
3984
|
+
throw new Error(`Empty numeric scale domain: ${domain.column}`);
|
|
3985
|
+
}
|
|
3986
|
+
if (domain.type === "percentile") {
|
|
3987
|
+
return [quantileSorted(data.values, domain.lower), quantileSorted(data.values, domain.upper)];
|
|
3988
|
+
}
|
|
3989
|
+
if (domain.type === "symmetric") {
|
|
3990
|
+
const radius = Math.max(Math.abs(data.min - domain.pivot), Math.abs(data.max - domain.pivot));
|
|
3991
|
+
return [domain.pivot - radius, domain.pivot + radius];
|
|
3992
|
+
}
|
|
3993
|
+
if (domain.includeZero)
|
|
3994
|
+
return [Math.min(0, data.min), Math.max(0, data.max)];
|
|
3995
|
+
return [data.min, data.max];
|
|
3996
|
+
});
|
|
3997
|
+
}
|
|
3998
|
+
function resolveArrowExpressionStatistics(table, expression, classes, functions) {
|
|
3999
|
+
const compiled = compileArrowExpression(expression, {
|
|
4000
|
+
inputSchema: table.schema,
|
|
4001
|
+
functions
|
|
4002
|
+
});
|
|
4003
|
+
const values = readNumericSamples(compiled.evaluateTable(table), "Quantile expression");
|
|
4004
|
+
if (!values.length)
|
|
4005
|
+
throw new Error("Empty numeric scale expression domain");
|
|
4006
|
+
if (classes === void 0) {
|
|
4007
|
+
let min = Infinity;
|
|
4008
|
+
let max = -Infinity;
|
|
4009
|
+
for (const value of values) {
|
|
4010
|
+
min = Math.min(min, value);
|
|
4011
|
+
max = Math.max(max, value);
|
|
4012
|
+
}
|
|
4013
|
+
return { domain: [min, max] };
|
|
4014
|
+
}
|
|
4015
|
+
values.sort((a, b) => a - b);
|
|
4016
|
+
return {
|
|
4017
|
+
domain: [values[0], values[values.length - 1]],
|
|
4018
|
+
quantileBreaks: computeQuantileBreaks(values, classes)
|
|
4019
|
+
};
|
|
4020
|
+
}
|
|
4021
|
+
function resolveArrowQuantiles(table, column, classes) {
|
|
4022
|
+
const vector = table.getChild(column);
|
|
4023
|
+
if (!vector)
|
|
4024
|
+
throw new Error(`Unknown quantile column: ${column}`);
|
|
4025
|
+
const values = readNumericSamples(vector, `Quantile column ${column}`);
|
|
4026
|
+
values.sort((a, b) => a - b);
|
|
4027
|
+
return computeQuantileBreaks(values, classes);
|
|
4028
|
+
}
|
|
4029
|
+
function readNumericSamples(vector, label) {
|
|
4030
|
+
const values = [];
|
|
4031
|
+
for (let row = 0; row < vector.length; row++) {
|
|
4032
|
+
const value = arrowScaleValue(vector.get(row));
|
|
4033
|
+
if (value === null || value === void 0)
|
|
4034
|
+
continue;
|
|
4035
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
4036
|
+
throw new Error(`${label} must be finite numeric`);
|
|
4037
|
+
}
|
|
4038
|
+
values.push(value);
|
|
4039
|
+
}
|
|
4040
|
+
return values;
|
|
4041
|
+
}
|
|
4042
|
+
function computeQuantileBreaks(values, classes) {
|
|
4043
|
+
if (!Number.isSafeInteger(classes) || classes < 2) {
|
|
4044
|
+
throw new Error("Quantile classes must be >= 2");
|
|
4045
|
+
}
|
|
4046
|
+
return Array.from({ length: classes - 1 }, (_, i) => quantileSorted(values, (i + 1) / classes));
|
|
4047
|
+
}
|
|
4048
|
+
|
|
4049
|
+
// dist/scales/streaming-domains.js
|
|
4050
|
+
function createStreamingDomainAccumulator(domains, options = {}) {
|
|
4051
|
+
const columns = /* @__PURE__ */ new Map();
|
|
4052
|
+
const quantileColumns = new Set(options.quantileColumns ?? []);
|
|
4053
|
+
const streamingDomains = domains.map((domain) => {
|
|
4054
|
+
if (domain.type === "expression-extent") {
|
|
4055
|
+
throw new Error("Streaming scale domains require computed values in a projected column");
|
|
4056
|
+
}
|
|
4057
|
+
return domain;
|
|
4058
|
+
});
|
|
4059
|
+
for (const domain of streamingDomains) {
|
|
4060
|
+
if (domain.type === "fixed")
|
|
4061
|
+
continue;
|
|
4062
|
+
const previous = columns.get(domain.column);
|
|
4063
|
+
if (previous) {
|
|
4064
|
+
if ((domain.type === "percentile" || quantileColumns.has(domain.column)) && !previous.sketch) {
|
|
4065
|
+
previous.sketch = new quantile_sketch_exports.ScaleQuantileSketch(options.quantileCapacity);
|
|
4066
|
+
}
|
|
4067
|
+
if (domain.type === "categories")
|
|
4068
|
+
previous.categories ?? (previous.categories = /* @__PURE__ */ new Set());
|
|
4069
|
+
continue;
|
|
4070
|
+
}
|
|
4071
|
+
columns.set(domain.column, {
|
|
4072
|
+
min: Infinity,
|
|
4073
|
+
max: -Infinity,
|
|
4074
|
+
seen: 0,
|
|
4075
|
+
...domain.type === "categories" ? { categories: /* @__PURE__ */ new Set() } : {},
|
|
4076
|
+
...domain.type === "percentile" || quantileColumns.has(domain.column) ? { sketch: new quantile_sketch_exports.ScaleQuantileSketch(options.quantileCapacity) } : {}
|
|
4077
|
+
});
|
|
4078
|
+
}
|
|
4079
|
+
for (const name2 of quantileColumns) {
|
|
4080
|
+
if (!columns.has(name2))
|
|
4081
|
+
columns.set(name2, {
|
|
4082
|
+
min: Infinity,
|
|
4083
|
+
max: -Infinity,
|
|
4084
|
+
seen: 0,
|
|
4085
|
+
sketch: new quantile_sketch_exports.ScaleQuantileSketch(options.quantileCapacity)
|
|
4086
|
+
});
|
|
4087
|
+
}
|
|
4088
|
+
return {
|
|
4089
|
+
update(batch) {
|
|
4090
|
+
var _a2, _b, _c;
|
|
4091
|
+
for (const [name2, state] of columns) {
|
|
4092
|
+
const vector = batch.getChild(name2);
|
|
4093
|
+
if (!vector)
|
|
4094
|
+
throw new Error(`Missing scale column: ${name2}`);
|
|
4095
|
+
for (let row = 0; row < vector.length; row++) {
|
|
4096
|
+
const value = arrowScaleValue(vector.get(row));
|
|
4097
|
+
if (value === null || value === void 0)
|
|
4098
|
+
continue;
|
|
4099
|
+
if (typeof value === "string") {
|
|
4100
|
+
(_a2 = state.categories) == null ? void 0 : _a2.add(value);
|
|
4101
|
+
} else if (typeof value === "number" && Number.isFinite(value)) {
|
|
4102
|
+
(_b = state.categories) == null ? void 0 : _b.add(value);
|
|
4103
|
+
state.seen++;
|
|
4104
|
+
state.min = Math.min(state.min, value);
|
|
4105
|
+
state.max = Math.max(state.max, value);
|
|
4106
|
+
(_c = state.sketch) == null ? void 0 : _c.add(value);
|
|
4107
|
+
} else {
|
|
4108
|
+
throw new Error(`Scale column ${name2} has unsupported value at row ${row}`);
|
|
4109
|
+
}
|
|
4110
|
+
}
|
|
4111
|
+
}
|
|
4112
|
+
},
|
|
4113
|
+
result() {
|
|
4114
|
+
return streamingDomains.map((domain) => {
|
|
4115
|
+
if (domain.type === "fixed")
|
|
4116
|
+
return [...domain.values];
|
|
4117
|
+
const state = columns.get(domain.column);
|
|
4118
|
+
if (domain.type === "categories") {
|
|
4119
|
+
const labels = [...state.categories];
|
|
4120
|
+
return domain.order === "first-seen" ? labels : labels.sort((a, b) => typeof a === "number" && typeof b === "number" ? a - b : a < b ? -1 : a > b ? 1 : 0);
|
|
4121
|
+
}
|
|
4122
|
+
if (!state.seen)
|
|
4123
|
+
return [];
|
|
4124
|
+
if (domain.type === "percentile") {
|
|
4125
|
+
return [state.sketch.quantile(domain.lower), state.sketch.quantile(domain.upper)];
|
|
4126
|
+
}
|
|
4127
|
+
if (domain.type === "symmetric") {
|
|
4128
|
+
const radius = Math.max(Math.abs(state.min - domain.pivot), Math.abs(state.max - domain.pivot));
|
|
4129
|
+
return [domain.pivot - radius, domain.pivot + radius];
|
|
4130
|
+
}
|
|
4131
|
+
if (domain.includeZero)
|
|
4132
|
+
return [Math.min(state.min, 0), Math.max(state.max, 0)];
|
|
4133
|
+
return [state.min, state.max];
|
|
4134
|
+
});
|
|
4135
|
+
},
|
|
4136
|
+
quantileBreaks(column, classes) {
|
|
4137
|
+
var _a2;
|
|
4138
|
+
if (!Number.isSafeInteger(classes) || classes < 2) {
|
|
4139
|
+
throw new Error("Scale class count must be at least two");
|
|
4140
|
+
}
|
|
4141
|
+
const sketch = (_a2 = columns.get(column)) == null ? void 0 : _a2.sketch;
|
|
4142
|
+
if (!sketch)
|
|
4143
|
+
throw new Error(`Column ${column} has no quantile sketch`);
|
|
4144
|
+
if (!sketch.length)
|
|
4145
|
+
return [];
|
|
4146
|
+
return Array.from({ length: classes - 1 }, (_, i) => sketch.quantile((i + 1) / classes));
|
|
4147
|
+
}
|
|
4148
|
+
};
|
|
4149
|
+
}
|
|
4150
|
+
|
|
4151
|
+
// dist/scales/arrow-scales.js
|
|
4152
|
+
function asColor(value) {
|
|
4153
|
+
if (value === null)
|
|
4154
|
+
return null;
|
|
4155
|
+
if (typeof value === "number")
|
|
4156
|
+
throw new Error("Expected a color-valued scale");
|
|
4157
|
+
return value;
|
|
4158
|
+
}
|
|
4159
|
+
function toVector(rows, spec) {
|
|
4160
|
+
var _a2;
|
|
4161
|
+
const numeric = spec.range.type === "numeric";
|
|
4162
|
+
if (numeric) {
|
|
4163
|
+
return (0, import_apache_arrow7.vectorFromArray)(rows, new import_apache_arrow7.Float64());
|
|
4164
|
+
}
|
|
4165
|
+
const format = ((_a2 = spec.encoding) == null ? void 0 : _a2.format) ?? "rgba8unorm";
|
|
4166
|
+
const type = format === "rgba8unorm" ? new import_apache_arrow7.Uint8() : format === "rgba16unorm" ? new import_apache_arrow7.Uint16() : format === "rgba16float" ? new import_apache_arrow7.Float16() : new import_apache_arrow7.Float32();
|
|
4167
|
+
const factor = format === "rgba8unorm" ? 255 : format === "rgba16unorm" ? 65535 : void 0;
|
|
4168
|
+
const vector = nullTupleVector(new import_apache_arrow7.FixedSizeList(4, new import_apache_arrow7.Field("item", type, false)), rows.length);
|
|
4169
|
+
const channels = vector.getChildAt(0);
|
|
4170
|
+
for (let row = 0; row < rows.length; row++) {
|
|
4171
|
+
const color2 = asColor(rows[row]);
|
|
4172
|
+
if (!color2)
|
|
4173
|
+
continue;
|
|
4174
|
+
for (let component = 0; component < 4; component++) {
|
|
4175
|
+
const channel = color2[component];
|
|
4176
|
+
if (!Number.isFinite(channel) || format === "rgba16float" && Math.abs(channel) >= 65520 || format === "rgba32float" && !Number.isFinite(Math.fround(channel)))
|
|
4177
|
+
throw new Error("Scale color cannot be represented in the requested format");
|
|
4178
|
+
channels.set(row * 4 + component, factor === void 0 ? channel : Math.round(Math.max(0, Math.min(1, channel)) * factor));
|
|
4179
|
+
}
|
|
4180
|
+
vector.data[0].setValid(row, true);
|
|
4181
|
+
}
|
|
4182
|
+
return vector;
|
|
4183
|
+
}
|
|
4184
|
+
function getClassCount(spec) {
|
|
4185
|
+
if (spec.range.type === "palette")
|
|
4186
|
+
return spec.range.count;
|
|
4187
|
+
return spec.type === "quantile" && spec.range.type === "colors" ? spec.range.values.length : void 0;
|
|
4188
|
+
}
|
|
4189
|
+
function compileArrowScale(table, parsed, resolvedDomain, functions) {
|
|
4190
|
+
const classCount = getClassCount(parsed.scale);
|
|
4191
|
+
if (parsed.scale.type === "quantile" && (!classCount || parsed.scale.domain.type === "fixed")) {
|
|
4192
|
+
throw new Error("Quantile scale requires a class count and a data-derived domain");
|
|
4193
|
+
}
|
|
4194
|
+
if (parsed.scale.domain.type === "expression-extent") {
|
|
4195
|
+
const resolution2 = resolveArrowExpressionStatistics(table, parsed.scale.domain.expression, parsed.scale.type === "quantile" ? classCount : void 0, functions);
|
|
4196
|
+
return bindArrowScale(table.schema, parsed, { ...resolution2, domain: resolvedDomain ?? resolution2.domain }, functions);
|
|
4197
|
+
}
|
|
4198
|
+
const resolution = {
|
|
4199
|
+
domain: resolvedDomain ?? resolveArrowDomains(table, [parsed.scale.domain])[0],
|
|
4200
|
+
...parsed.scale.type === "quantile" && parsed.scale.domain.type !== "fixed" ? { quantileBreaks: resolveArrowQuantiles(table, parsed.scale.domain.column, classCount) } : {}
|
|
4201
|
+
};
|
|
4202
|
+
return bindArrowScale(table.schema, parsed, resolution, functions);
|
|
4203
|
+
}
|
|
4204
|
+
function bindArrowScale(inputSchema, parsed, { domain, quantileBreaks }, functions) {
|
|
4205
|
+
let thresholds;
|
|
4206
|
+
const kind = parsed.scale.type;
|
|
4207
|
+
const classCount = getClassCount(parsed.scale);
|
|
4208
|
+
if (kind === "quantile") {
|
|
4209
|
+
if (classCount === void 0 || !Number.isSafeInteger(classCount) || classCount < 2 || (quantileBreaks == null ? void 0 : quantileBreaks.length) !== classCount - 1) {
|
|
4210
|
+
throw new Error("Quantile scale requires one resolved break per class boundary");
|
|
4211
|
+
}
|
|
4212
|
+
if (quantileBreaks.some((value, index) => !Number.isFinite(value) || index > 0 && value < quantileBreaks[index - 1])) {
|
|
4213
|
+
throw new Error("Quantile scale breaks must be finite and nondecreasing");
|
|
4214
|
+
}
|
|
4215
|
+
thresholds = quantileBreaks;
|
|
4216
|
+
} else if (kind === "quantize") {
|
|
4217
|
+
if (!classCount || domain.length !== 2 || typeof domain[0] !== "number" || typeof domain[1] !== "number") {
|
|
4218
|
+
throw new Error("Quantize scale requires a numeric extent and a class count");
|
|
4219
|
+
}
|
|
4220
|
+
thresholds = Array.from({ length: classCount - 1 }, (_, i) => domain[0] + (domain[1] - domain[0]) * (i + 1) / classCount);
|
|
4221
|
+
} else if (kind === "threshold") {
|
|
4222
|
+
if (domain.some((value) => typeof value !== "number")) {
|
|
4223
|
+
throw new Error("Thresholds require numeric domain values");
|
|
4224
|
+
}
|
|
4225
|
+
thresholds = domain;
|
|
4226
|
+
}
|
|
4227
|
+
const scale = (0, compile_scale_exports.compileResolvedScale)(parsed.scale, domain, thresholds);
|
|
4228
|
+
const compiled = compileArrowExpression(parsed.value, {
|
|
4229
|
+
inputSchema,
|
|
4230
|
+
functions
|
|
4231
|
+
});
|
|
4232
|
+
const evaluateVector = (input) => {
|
|
4233
|
+
const result = Array.from({ length: input.length }, (_, i) => {
|
|
4234
|
+
const value = arrowScaleValue(input.get(i));
|
|
4235
|
+
if (value === null || value === void 0)
|
|
4236
|
+
return scale.sample(null);
|
|
4237
|
+
if (typeof value !== "number" && typeof value !== "string") {
|
|
4238
|
+
throw new Error("Scale expression must produce scalar numeric or categorical values");
|
|
4239
|
+
}
|
|
4240
|
+
return scale.sample(value);
|
|
4241
|
+
});
|
|
4242
|
+
return toVector(result, parsed.scale);
|
|
4243
|
+
};
|
|
4244
|
+
return {
|
|
4245
|
+
scale,
|
|
4246
|
+
evaluate: (input) => evaluateVector(compiled.evaluateTable(input)),
|
|
4247
|
+
evaluateBatch: (batch) => evaluateVector(compiled.evaluate(batch))
|
|
4248
|
+
};
|
|
4249
|
+
}
|
|
4250
|
+
function compileArrowScales(table, expressions, functions) {
|
|
4251
|
+
const entries = Object.entries(expressions);
|
|
4252
|
+
const columnEntries = entries.filter(([, parsed]) => parsed.scale.domain.type !== "expression-extent");
|
|
4253
|
+
const domains = resolveArrowDomains(table, columnEntries.map(([, parsed]) => parsed.scale.domain));
|
|
4254
|
+
const domainsByKey = new Map(columnEntries.map(([key], index) => [key, domains[index]]));
|
|
4255
|
+
return Object.fromEntries(entries.map(([key, parsed]) => [
|
|
4256
|
+
key,
|
|
4257
|
+
compileArrowScale(table, parsed, domainsByKey.get(key), functions)
|
|
4258
|
+
]));
|
|
4259
|
+
}
|
|
4260
|
+
function compileArrowScaleExpression(table, expression, functions = []) {
|
|
4261
|
+
const ast = typeof expression === "string" ? parseSQLExpression(expression) : expression;
|
|
4262
|
+
const names = new Set(functions.map((fn) => fn.name.toUpperCase()));
|
|
4263
|
+
const scales = [];
|
|
4264
|
+
const declarations = [...functions];
|
|
4265
|
+
function reserve(node) {
|
|
4266
|
+
if (!node || typeof node !== "object")
|
|
4267
|
+
return;
|
|
4268
|
+
if ("type" in node && node.type === "Identifier" && "name" in node)
|
|
4269
|
+
names.add(String(node.name).toUpperCase());
|
|
4270
|
+
for (const value of Object.values(node))
|
|
4271
|
+
reserve(value);
|
|
4272
|
+
}
|
|
4273
|
+
reserve(ast);
|
|
4274
|
+
let nextName = 0;
|
|
4275
|
+
function visit(node) {
|
|
4276
|
+
var _a2;
|
|
4277
|
+
if (!node || typeof node !== "object")
|
|
4278
|
+
return node;
|
|
4279
|
+
if (Array.isArray(node))
|
|
4280
|
+
return node.map(visit);
|
|
4281
|
+
if (node.type === "CallExpression" && ((_a2 = node.callee) == null ? void 0 : _a2.type) === "Identifier" && /^SCALE_(LINEAR|SQRT|LOG|COLOR|DIVERGING|ORDINAL|QUANTILE|QUANTIZE|THRESHOLD)$/i.test(node.callee.name)) {
|
|
4282
|
+
const scaleIndex = scales.length;
|
|
4283
|
+
scales.length++;
|
|
4284
|
+
const valueExpression = visit(node.arguments[0]);
|
|
4285
|
+
const parsed = parseSQLScaleAST({
|
|
4286
|
+
...node,
|
|
4287
|
+
arguments: [valueExpression, ...node.arguments.slice(1)]
|
|
4288
|
+
});
|
|
4289
|
+
const { scale } = compileArrowScale(table, parsed, void 0, declarations);
|
|
4290
|
+
scales[scaleIndex] = scale;
|
|
4291
|
+
const input = compileArrowExpression(parsed.value, {
|
|
4292
|
+
inputSchema: table.schema,
|
|
4293
|
+
functions: declarations
|
|
4294
|
+
});
|
|
4295
|
+
let name2;
|
|
4296
|
+
do {
|
|
4297
|
+
name2 = "__scale_" + nextName++;
|
|
4298
|
+
} while (names.has(name2.toUpperCase()));
|
|
4299
|
+
names.add(name2.toUpperCase());
|
|
4300
|
+
declarations.push({
|
|
4301
|
+
name: name2,
|
|
4302
|
+
prototypes: [
|
|
4303
|
+
{
|
|
4304
|
+
arguments: [input.outputField],
|
|
4305
|
+
result: new import_apache_arrow7.Field(name2, toVector([], parsed.scale).type, true),
|
|
4306
|
+
nullHandling: "call",
|
|
4307
|
+
evaluate: ([values], { activeRows, length }) => toVector(Array.from({ length }, (_, row) => {
|
|
4308
|
+
if (!activeRows[row])
|
|
4309
|
+
return null;
|
|
4310
|
+
const value = arrowScaleValue(values.get(row));
|
|
4311
|
+
if (value !== null && value !== void 0 && typeof value !== "number" && typeof value !== "string") {
|
|
4312
|
+
throw new Error("Scale input must be numeric or categorical");
|
|
4313
|
+
}
|
|
4314
|
+
return scale.sample(value ?? null);
|
|
4315
|
+
}), parsed.scale)
|
|
4316
|
+
}
|
|
4317
|
+
]
|
|
4318
|
+
});
|
|
4319
|
+
return {
|
|
4320
|
+
type: "CallExpression",
|
|
4321
|
+
callee: { type: "Identifier", name: name2 },
|
|
4322
|
+
arguments: [parsed.value]
|
|
4323
|
+
};
|
|
4324
|
+
}
|
|
4325
|
+
return Object.fromEntries(Object.entries(node).map(([key, value]) => [key, visit(value)]));
|
|
4326
|
+
}
|
|
4327
|
+
const rewritten = visit(ast);
|
|
4328
|
+
const compiled = compileArrowExpression(rewritten, {
|
|
4329
|
+
inputSchema: table.schema,
|
|
4330
|
+
functions: declarations
|
|
4331
|
+
});
|
|
4332
|
+
return { ...compiled, scales };
|
|
4333
|
+
}
|
|
4334
|
+
//# sourceMappingURL=index.cjs.map
|