@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,500 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
bindSQLParameter,
|
|
7
|
+
validateSQLParameterOptions,
|
|
8
|
+
type SQLParameterOptions
|
|
9
|
+
} from './sql-parameters';
|
|
10
|
+
import {SQLParseError} from './sql-parse-error';
|
|
11
|
+
import type {Expression} from '../expressions';
|
|
12
|
+
import type {ColumnarScalarType} from '../expressions/scalar-type';
|
|
13
|
+
|
|
14
|
+
export const SQL_RESERVED_WORDS = new Set([
|
|
15
|
+
'SELECT',
|
|
16
|
+
'ARRAY',
|
|
17
|
+
'FROM',
|
|
18
|
+
'WHERE',
|
|
19
|
+
'LIMIT',
|
|
20
|
+
'OFFSET',
|
|
21
|
+
'AS',
|
|
22
|
+
'TRUE',
|
|
23
|
+
'FALSE',
|
|
24
|
+
'NULL',
|
|
25
|
+
'ALL',
|
|
26
|
+
'AND',
|
|
27
|
+
'OR',
|
|
28
|
+
'NOT',
|
|
29
|
+
'IN',
|
|
30
|
+
'IS',
|
|
31
|
+
'DISTINCT',
|
|
32
|
+
'ORDER',
|
|
33
|
+
'GROUP',
|
|
34
|
+
'HAVING',
|
|
35
|
+
'JOIN',
|
|
36
|
+
'UNION'
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
/** Resolves standard SQL names and explicit numeric widths. */
|
|
40
|
+
export function sqlCastType(name: string, source = name, offset = 0): ColumnarScalarType {
|
|
41
|
+
const normalized = name.toLowerCase().replace(/\s+/g, ' ').trim();
|
|
42
|
+
if (/^(?:u?int(?:8|16|32|64)|float(?:16|32|64))$/.test(normalized)) {
|
|
43
|
+
return normalized as ColumnarScalarType;
|
|
44
|
+
}
|
|
45
|
+
switch (normalized) {
|
|
46
|
+
case 'smallint':
|
|
47
|
+
return 'int16';
|
|
48
|
+
case 'int':
|
|
49
|
+
case 'integer':
|
|
50
|
+
return 'int32';
|
|
51
|
+
case 'bigint':
|
|
52
|
+
return 'int64';
|
|
53
|
+
case 'real':
|
|
54
|
+
return 'float32';
|
|
55
|
+
case 'double precision':
|
|
56
|
+
return 'float64';
|
|
57
|
+
case 'boolean':
|
|
58
|
+
return 'bool';
|
|
59
|
+
case 'utf8':
|
|
60
|
+
case 'varchar':
|
|
61
|
+
case 'character varying':
|
|
62
|
+
return 'utf8';
|
|
63
|
+
default:
|
|
64
|
+
throw new SQLParseError(`Unsupported SQL CAST type: ${name}`, source, offset);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Parses bounded SQL expressions into the shared expression AST. */
|
|
69
|
+
export function parseSQLExpression(source: string, options: SQLParameterOptions = {}): Expression {
|
|
70
|
+
const reader = new SQLExpressionReader(source, 0, options);
|
|
71
|
+
const expression = reader.expression();
|
|
72
|
+
reader.space();
|
|
73
|
+
if (reader.position !== source.length)
|
|
74
|
+
throw reader.error('Unexpected token after SQL expression');
|
|
75
|
+
return expression;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Bounded expression reader shared by SELECT and standalone expressions. */
|
|
79
|
+
export class SQLExpressionReader {
|
|
80
|
+
position: number;
|
|
81
|
+
private tokens = 0;
|
|
82
|
+
private readonly depths = new WeakMap<object, number>();
|
|
83
|
+
constructor(
|
|
84
|
+
readonly source: string,
|
|
85
|
+
start = 0,
|
|
86
|
+
/** Binding configuration shared by standalone and structural SQL parsers. */
|
|
87
|
+
protected readonly parameterOptions: SQLParameterOptions = {}
|
|
88
|
+
) {
|
|
89
|
+
validateSQLParameterOptions(parameterOptions);
|
|
90
|
+
this.position = start;
|
|
91
|
+
if (start === 0 && (typeof source !== 'string' || !source.trim() || source.length > 65536))
|
|
92
|
+
throw new SQLParseError(
|
|
93
|
+
'SQL expression requires non-empty input of at most 65536 characters',
|
|
94
|
+
'',
|
|
95
|
+
0
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
/** Creates an error at the current expression cursor. */
|
|
99
|
+
error(message: string, offset = this.position): SQLParseError {
|
|
100
|
+
return new SQLParseError(message, this.source, offset);
|
|
101
|
+
}
|
|
102
|
+
space(): void {
|
|
103
|
+
while (this.position < this.source.length && /\s/.test(this.source[this.position]!))
|
|
104
|
+
this.position++;
|
|
105
|
+
if (this.source.startsWith('--', this.position) || this.source.startsWith('/*', this.position))
|
|
106
|
+
throw this.error('SQL comments are not supported.');
|
|
107
|
+
}
|
|
108
|
+
word(word: string): boolean {
|
|
109
|
+
this.space();
|
|
110
|
+
if (
|
|
111
|
+
this.source.slice(this.position, this.position + word.length).toUpperCase() !== word ||
|
|
112
|
+
/[A-Za-z0-9_]/.test(this.source[this.position + word.length] || '')
|
|
113
|
+
)
|
|
114
|
+
return false;
|
|
115
|
+
this.position += word.length;
|
|
116
|
+
this.token();
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
protected take(symbol: string): boolean {
|
|
120
|
+
this.space();
|
|
121
|
+
if (!this.source.startsWith(symbol, this.position)) return false;
|
|
122
|
+
this.position += symbol.length;
|
|
123
|
+
this.token();
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
symbol(symbol: string): void {
|
|
127
|
+
if (!this.take(symbol)) throw this.error(`SQL expression expected ${symbol}`);
|
|
128
|
+
}
|
|
129
|
+
protected node(value: object, children: Expression[] = []): Expression {
|
|
130
|
+
const depth = children.length
|
|
131
|
+
? 1 + Math.max(...children.map(child => this.depths.get(child) || 0))
|
|
132
|
+
: 0;
|
|
133
|
+
if (depth > 64) throw this.error('SQL expression exceeds nesting limit');
|
|
134
|
+
this.depths.set(value, depth);
|
|
135
|
+
return value as Expression;
|
|
136
|
+
}
|
|
137
|
+
expression(depth = 0, minimum = 0): Expression {
|
|
138
|
+
if (depth > 64) throw this.error('SQL expression exceeds nesting limit');
|
|
139
|
+
let left: Expression;
|
|
140
|
+
if (this.word('NOT')) {
|
|
141
|
+
const argument = this.expression(depth + 1, 3);
|
|
142
|
+
left = this.node({type: 'UnaryExpression', operator: '!', argument}, [argument]);
|
|
143
|
+
} else if (/^\s*[+-](?:\d|\.\d)/.test(this.source.slice(this.position))) {
|
|
144
|
+
left = this.primary(depth);
|
|
145
|
+
} else if (this.take('+') || this.take('-')) {
|
|
146
|
+
const operator = this.source[this.position - 1]!;
|
|
147
|
+
const argument = this.expression(depth + 1, 6);
|
|
148
|
+
left = this.node({type: 'UnaryExpression', operator, argument}, [argument]);
|
|
149
|
+
} else left = this.primary(depth);
|
|
150
|
+
while (true) {
|
|
151
|
+
const start = this.position;
|
|
152
|
+
if (this.take('[')) {
|
|
153
|
+
const index = this.expression(depth + 1);
|
|
154
|
+
this.symbol(']');
|
|
155
|
+
left = this.call('LIST_EXTRACT', [left, index], true);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (this.take('.')) {
|
|
159
|
+
left = this.memberAccess(left, this.readIdentifier(true));
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (this.take('::')) {
|
|
163
|
+
if (minimum > 7) {
|
|
164
|
+
this.position = start;
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
left = this.cast(left);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (minimum <= 3 && this.word('IS')) {
|
|
171
|
+
const negate = this.word('NOT');
|
|
172
|
+
if (!this.word('NULL')) throw this.error('SQL IS requires NULL or NOT NULL');
|
|
173
|
+
left = this.call(negate ? 'isNotNull' : 'isNull', [left]);
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (minimum <= 3) {
|
|
177
|
+
const negate = this.word('NOT');
|
|
178
|
+
if (this.word('IN')) {
|
|
179
|
+
this.symbol('(');
|
|
180
|
+
const values: Expression[] = [];
|
|
181
|
+
do {
|
|
182
|
+
const item = this.expression(depth + 1);
|
|
183
|
+
if (item.type === 'ArrayExpression') {
|
|
184
|
+
for (const element of item.elements) {
|
|
185
|
+
if (element.type !== 'Literal') throw this.error('SQL IN requires scalar values');
|
|
186
|
+
values.push(element as Expression);
|
|
187
|
+
}
|
|
188
|
+
} else values.push(item);
|
|
189
|
+
if (values.length > 4096) throw this.error('SQL IN exceeds 4096 values');
|
|
190
|
+
} while (this.take(','));
|
|
191
|
+
this.symbol(')');
|
|
192
|
+
left = values.length
|
|
193
|
+
? this.call('in', [left, ...values])
|
|
194
|
+
: this.node({type: 'Literal', value: false, raw: 'false'});
|
|
195
|
+
if (negate)
|
|
196
|
+
left = this.node({type: 'UnaryExpression', operator: '!', argument: left}, [left]);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
const insensitive = this.word('ILIKE');
|
|
200
|
+
if (insensitive || this.word('LIKE')) {
|
|
201
|
+
const pattern = this.expression(depth + 1, 4);
|
|
202
|
+
const args = [left, pattern];
|
|
203
|
+
if (this.word('ESCAPE')) args.push(this.expression(depth + 1, 4));
|
|
204
|
+
left = this.call(insensitive ? 'ilike' : 'like', args);
|
|
205
|
+
if (negate)
|
|
206
|
+
left = this.node({type: 'UnaryExpression', operator: '!', argument: left}, [left]);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
this.position = start;
|
|
210
|
+
}
|
|
211
|
+
this.space();
|
|
212
|
+
const match = /^(OR\b|AND\b|\|\||<>|!=|<=|>=|=|<|>|\+|-|\*|\/)/i.exec(
|
|
213
|
+
this.source.slice(this.position)
|
|
214
|
+
);
|
|
215
|
+
if (!match) break;
|
|
216
|
+
const token = match[0].toUpperCase();
|
|
217
|
+
const precedence =
|
|
218
|
+
token === 'OR'
|
|
219
|
+
? 1
|
|
220
|
+
: token === 'AND'
|
|
221
|
+
? 2
|
|
222
|
+
: ['=', '<>', '!=', '<', '>', '<=', '>='].includes(token)
|
|
223
|
+
? 3
|
|
224
|
+
: token === '+' || token === '-' || token === '||'
|
|
225
|
+
? 4
|
|
226
|
+
: 5;
|
|
227
|
+
if (precedence < minimum) break;
|
|
228
|
+
this.position += match[0].length;
|
|
229
|
+
this.token();
|
|
230
|
+
const right = this.expression(depth + 1, precedence + 1);
|
|
231
|
+
if (token === '||') {
|
|
232
|
+
left = this.call('concatStrict', [left, right]);
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
const operator =
|
|
236
|
+
token === 'OR'
|
|
237
|
+
? '||'
|
|
238
|
+
: token === 'AND'
|
|
239
|
+
? '&&'
|
|
240
|
+
: token === '='
|
|
241
|
+
? '=='
|
|
242
|
+
: token === '<>'
|
|
243
|
+
? '!='
|
|
244
|
+
: token;
|
|
245
|
+
left = this.node({type: 'BinaryExpression', operator, left, right}, [left, right]);
|
|
246
|
+
}
|
|
247
|
+
return left;
|
|
248
|
+
}
|
|
249
|
+
private call(name: string, args: Expression[], sqlFunction = false): Expression {
|
|
250
|
+
return this.node(
|
|
251
|
+
{
|
|
252
|
+
type: 'CallExpression',
|
|
253
|
+
callee: {type: 'Identifier', name},
|
|
254
|
+
arguments: args,
|
|
255
|
+
...(sqlFunction ? {sqlFunction: true} : {})
|
|
256
|
+
},
|
|
257
|
+
args
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
private cast(argument: Expression): Expression {
|
|
261
|
+
this.space();
|
|
262
|
+
const start = this.position;
|
|
263
|
+
const match = /^[A-Za-z][A-Za-z0-9]*(?:\s+(?:PRECISION|VARYING)\b)?/i.exec(
|
|
264
|
+
this.source.slice(this.position)
|
|
265
|
+
);
|
|
266
|
+
if (!match) throw this.error('SQL cast requires a target type');
|
|
267
|
+
this.position += match[0].length;
|
|
268
|
+
this.token();
|
|
269
|
+
if (this.take('[')) {
|
|
270
|
+
this.space();
|
|
271
|
+
const width = /^\d+/.exec(this.source.slice(this.position));
|
|
272
|
+
if (!width) throw this.error('SQL ARRAY cast requires a fixed length');
|
|
273
|
+
this.position += width[0].length;
|
|
274
|
+
this.token();
|
|
275
|
+
this.symbol(']');
|
|
276
|
+
const array =
|
|
277
|
+
argument.type === 'ArrayExpression'
|
|
278
|
+
? {type: 'CallExpression', callee: {name: 'array'}, arguments: argument.elements}
|
|
279
|
+
: (argument as any);
|
|
280
|
+
if (
|
|
281
|
+
array.type !== 'CallExpression' ||
|
|
282
|
+
!/^array(?:_(?:float(?:16|32|64)|uint8))?$/.test(array.callee.name) ||
|
|
283
|
+
Number(width[0]) !== array.arguments.length
|
|
284
|
+
)
|
|
285
|
+
throw this.error('ARRAY cast length must match the number of components');
|
|
286
|
+
const type = sqlCastType(match[0], this.source, start);
|
|
287
|
+
if (!['float16', 'float32', 'float64', 'uint8'].includes(type))
|
|
288
|
+
throw this.error('Unsupported ARRAY cast type');
|
|
289
|
+
if (
|
|
290
|
+
array.callee.name !== 'array' &&
|
|
291
|
+
(type === 'uint8' || array.callee.name === 'array_uint8')
|
|
292
|
+
)
|
|
293
|
+
throw this.error('Chained UINT8 array casts are not supported');
|
|
294
|
+
return this.call(`array_${type}`, array.arguments);
|
|
295
|
+
}
|
|
296
|
+
const name = match[0].toLowerCase();
|
|
297
|
+
let target: string;
|
|
298
|
+
if (name === 'timestamp') target = this.timestampCastType();
|
|
299
|
+
else if (name === 'date' || name === 'geometry') target = name;
|
|
300
|
+
else if (name === 'dictionary') target = this.dictionaryCastType();
|
|
301
|
+
else if (/^(?:binary|varbinary|blob|fixedsizebinary)$/.test(name))
|
|
302
|
+
target = this.binaryCastType(name);
|
|
303
|
+
else target = sqlCastType(name, this.source, start);
|
|
304
|
+
return this.call('cast', [
|
|
305
|
+
argument,
|
|
306
|
+
this.node({type: 'Literal', value: target, raw: JSON.stringify(target)})
|
|
307
|
+
]);
|
|
308
|
+
}
|
|
309
|
+
/** Parses the optional SQL time zone qualifier. */
|
|
310
|
+
private timestampCastType(): string {
|
|
311
|
+
const zoned = this.word('WITH');
|
|
312
|
+
if (!zoned && !this.word('WITHOUT')) return 'timestamp';
|
|
313
|
+
if (!this.word('TIME') || !this.word('ZONE')) throw this.error('Expected TIME ZONE');
|
|
314
|
+
return zoned ? 'timestamp_utc' : 'timestamp';
|
|
315
|
+
}
|
|
316
|
+
/** Parses the supported Arrow dictionary layout. */
|
|
317
|
+
private dictionaryCastType(): string {
|
|
318
|
+
this.symbol('(');
|
|
319
|
+
if (!this.word('UTF8')) throw this.error('Expected UTF8 dictionary values');
|
|
320
|
+
this.symbol(',');
|
|
321
|
+
if (!this.word('INT32')) throw this.error('Expected INT32 dictionary indices');
|
|
322
|
+
this.symbol(')');
|
|
323
|
+
return 'dictionary_utf8_int32';
|
|
324
|
+
}
|
|
325
|
+
/** Parses binary aliases and optional positive byte widths. */
|
|
326
|
+
private binaryCastType(name: string): string {
|
|
327
|
+
if (name === 'blob') return 'binary';
|
|
328
|
+
if (!this.take('(')) {
|
|
329
|
+
if (name === 'fixedsizebinary') throw this.error('FIXEDSIZEBINARY requires a byte width');
|
|
330
|
+
return 'binary';
|
|
331
|
+
}
|
|
332
|
+
this.space();
|
|
333
|
+
const width = /^\d+/.exec(this.source.slice(this.position))?.[0] ?? '';
|
|
334
|
+
const byteWidth = Number(width);
|
|
335
|
+
if (byteWidth < 1 || byteWidth > 0x7fffffff)
|
|
336
|
+
throw this.error('Binary CAST requires a positive 32-bit byte width');
|
|
337
|
+
this.position += width.length;
|
|
338
|
+
this.token();
|
|
339
|
+
this.symbol(')');
|
|
340
|
+
return `${name === 'varbinary' ? 'varbinary' : 'fixedsizebinary'}:${byteWidth}`;
|
|
341
|
+
}
|
|
342
|
+
private primary(depth: number): Expression {
|
|
343
|
+
if (depth > 64) throw this.error('SQL expression exceeds nesting limit');
|
|
344
|
+
if (this.take('(')) {
|
|
345
|
+
const value = this.expression(depth + 1);
|
|
346
|
+
this.symbol(')');
|
|
347
|
+
return value;
|
|
348
|
+
}
|
|
349
|
+
if (this.word('CAST')) {
|
|
350
|
+
this.symbol('(');
|
|
351
|
+
const argument = this.expression(depth + 1);
|
|
352
|
+
if (!this.word('AS')) throw this.error('SQL CAST requires AS');
|
|
353
|
+
const result = this.cast(argument);
|
|
354
|
+
this.symbol(')');
|
|
355
|
+
return result;
|
|
356
|
+
}
|
|
357
|
+
if (this.word('ARRAY')) {
|
|
358
|
+
this.symbol('[');
|
|
359
|
+
const args: Expression[] = [];
|
|
360
|
+
do {
|
|
361
|
+
args.push(this.expression(depth + 1));
|
|
362
|
+
} while (this.take(','));
|
|
363
|
+
this.symbol(']');
|
|
364
|
+
return this.call('array', args);
|
|
365
|
+
}
|
|
366
|
+
if (this.word('CASE')) {
|
|
367
|
+
const clauses: {test: Expression; consequent: Expression}[] = [];
|
|
368
|
+
while (this.word('WHEN')) {
|
|
369
|
+
const test = this.expression(depth + 1);
|
|
370
|
+
if (!this.word('THEN')) throw this.error('SQL CASE requires THEN');
|
|
371
|
+
clauses.push({test, consequent: this.expression(depth + 1)});
|
|
372
|
+
}
|
|
373
|
+
if (!clauses.length) throw this.error('SQL CASE requires WHEN');
|
|
374
|
+
let alternate = this.word('ELSE')
|
|
375
|
+
? this.expression(depth + 1)
|
|
376
|
+
: this.node({type: 'Literal', value: null, raw: 'null'});
|
|
377
|
+
if (!this.word('END')) throw this.error('SQL CASE requires END');
|
|
378
|
+
for (let index = clauses.length - 1; index >= 0; index--) {
|
|
379
|
+
const {test, consequent} = clauses[index]!;
|
|
380
|
+
alternate = this.node({type: 'ConditionalExpression', test, consequent, alternate}, [
|
|
381
|
+
test,
|
|
382
|
+
consequent,
|
|
383
|
+
alternate
|
|
384
|
+
]);
|
|
385
|
+
}
|
|
386
|
+
return alternate;
|
|
387
|
+
}
|
|
388
|
+
this.space();
|
|
389
|
+
const start = this.position;
|
|
390
|
+
const extended = this.extendedPrimary();
|
|
391
|
+
if (extended) return extended;
|
|
392
|
+
const quote = this.source[this.position];
|
|
393
|
+
if (quote === '"' || quote === "'") {
|
|
394
|
+
const value = this.quoted(quote);
|
|
395
|
+
return quote === '"'
|
|
396
|
+
? this.identifier(value, depth, false)
|
|
397
|
+
: this.node({type: 'Literal', value, raw: JSON.stringify(value)});
|
|
398
|
+
}
|
|
399
|
+
const number = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/.exec(
|
|
400
|
+
this.source.slice(this.position)
|
|
401
|
+
);
|
|
402
|
+
if (number) {
|
|
403
|
+
this.position += number[0].length;
|
|
404
|
+
this.token();
|
|
405
|
+
const value = Number(number[0]);
|
|
406
|
+
if (!Number.isFinite(value) || (/^[+-]?\d+$/.test(number[0]) && !Number.isSafeInteger(value)))
|
|
407
|
+
throw this.error(
|
|
408
|
+
'SQL numeric literal must be finite and exactly representable; use a quoted BIGINT value',
|
|
409
|
+
start
|
|
410
|
+
);
|
|
411
|
+
return this.node({type: 'Literal', value, raw: number[0]});
|
|
412
|
+
}
|
|
413
|
+
for (const [keyword, value] of [
|
|
414
|
+
['NULL', null],
|
|
415
|
+
['TRUE', true],
|
|
416
|
+
['FALSE', false]
|
|
417
|
+
] as const)
|
|
418
|
+
if (this.word(keyword))
|
|
419
|
+
return this.node({type: 'Literal', value, raw: keyword.toLowerCase()});
|
|
420
|
+
return this.identifier(this.readIdentifier(), depth, true);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** Reads an identifier in projections, source references, and field access. */
|
|
424
|
+
readIdentifier(member = false): string {
|
|
425
|
+
this.space();
|
|
426
|
+
if (this.source[this.position] === '"') return this.quoted('"');
|
|
427
|
+
const identifier = /^[A-Za-z_][A-Za-z0-9_]*/.exec(this.source.slice(this.position))?.[0];
|
|
428
|
+
if (
|
|
429
|
+
!identifier ||
|
|
430
|
+
(!member &&
|
|
431
|
+
(SQL_RESERVED_WORDS.has(identifier.toUpperCase()) ||
|
|
432
|
+
/^(WHEN|THEN|ELSE|END)$/i.test(identifier)))
|
|
433
|
+
)
|
|
434
|
+
throw this.error('SQL expression requires a column, literal or function');
|
|
435
|
+
this.position += identifier.length;
|
|
436
|
+
this.token();
|
|
437
|
+
return identifier;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/** Decodes a quoted token, including doubled quote escapes. */
|
|
441
|
+
private quoted(quote: string): string {
|
|
442
|
+
const start = this.position++;
|
|
443
|
+
this.token();
|
|
444
|
+
let value = '';
|
|
445
|
+
while (this.position < this.source.length) {
|
|
446
|
+
const character = this.source[this.position++]!;
|
|
447
|
+
if (character !== quote) value += character;
|
|
448
|
+
else if (this.source[this.position] === quote) {
|
|
449
|
+
value += quote;
|
|
450
|
+
this.position++;
|
|
451
|
+
} else {
|
|
452
|
+
if (quote === '"' && !value) throw this.error('SQL identifiers must not be empty', start);
|
|
453
|
+
return value;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
throw this.error('Unterminated SQL expression quote', start);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
private identifier(name: string, depth: number, fold: boolean): Expression {
|
|
460
|
+
if (!this.take('(')) return this.node({type: 'Identifier', name});
|
|
461
|
+
const args: Expression[] = [];
|
|
462
|
+
const wildcard = fold && this.wildcardArgument(name);
|
|
463
|
+
if (wildcard) {
|
|
464
|
+
this.symbol(')');
|
|
465
|
+
return this.call(name, [wildcard], true);
|
|
466
|
+
}
|
|
467
|
+
if (!this.take(')')) {
|
|
468
|
+
do {
|
|
469
|
+
args.push(this.expression(depth + 1));
|
|
470
|
+
} while (this.take(','));
|
|
471
|
+
this.symbol(')');
|
|
472
|
+
}
|
|
473
|
+
return this.call(name, args, fold);
|
|
474
|
+
}
|
|
475
|
+
/** Reads named value placeholders; quotes and identifiers use separate productions. */
|
|
476
|
+
protected extendedPrimary(): Expression | undefined {
|
|
477
|
+
if (this.source[this.position] !== ':') return undefined;
|
|
478
|
+
const start = this.position;
|
|
479
|
+
const parameter = /^:([A-Za-z_][A-Za-z0-9_]*)/.exec(this.source.slice(start));
|
|
480
|
+
if (!parameter) throw this.error('SQL named parameters require a name');
|
|
481
|
+
this.position += parameter[0].length;
|
|
482
|
+
this.token();
|
|
483
|
+
try {
|
|
484
|
+
return this.node(bindSQLParameter(parameter[1]!, this.parameterOptions));
|
|
485
|
+
} catch (error) {
|
|
486
|
+
throw this.error(error instanceof Error ? error.message : String(error), start);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
/** Lowers field access to a portable expression call for Arrow binding. */
|
|
490
|
+
protected memberAccess(object: Expression, name: string): Expression {
|
|
491
|
+
const key = this.node({type: 'Literal', value: name, raw: JSON.stringify(name)});
|
|
492
|
+
return this.call('STRUCT_EXTRACT', [object, key], true);
|
|
493
|
+
}
|
|
494
|
+
protected wildcardArgument(_name: string): Expression | undefined {
|
|
495
|
+
return undefined;
|
|
496
|
+
}
|
|
497
|
+
protected token(): void {
|
|
498
|
+
if (++this.tokens > 4096) throw this.error('SQL expression exceeds token limit');
|
|
499
|
+
}
|
|
500
|
+
}
|