@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,272 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {validateSQLParameterOptions, type SQLParameterOptions} from './sql-parameters';
|
|
6
|
+
import {SQLParseError} from './sql-parse-error';
|
|
7
|
+
import type {Expression} from '../expressions';
|
|
8
|
+
import {SQLExpressionReader} from './parse-sql-expression';
|
|
9
|
+
import type {ColumnarPredicate} from '../expressions/columnar-predicate';
|
|
10
|
+
import type {TableQuery, TableSelection, TableReference} from './query-types';
|
|
11
|
+
|
|
12
|
+
const MAXIMUM_SQL_LENGTH = 65536;
|
|
13
|
+
const MAXIMUM_SELECT_TOKENS = 4096;
|
|
14
|
+
|
|
15
|
+
/** Optional WHERE support for the independently tree-shakeable structural parser. */
|
|
16
|
+
export type SQLSelectParserOptions = SQLParameterOptions &
|
|
17
|
+
Readonly<{
|
|
18
|
+
/** Converts a WHERE expression to the portable predicate vocabulary. Omission rejects WHERE. */
|
|
19
|
+
parseWhere?: (expression: string) => ColumnarPredicate | Readonly<{expression: Expression}>;
|
|
20
|
+
}>;
|
|
21
|
+
|
|
22
|
+
/** Parses SELECT columns/AS aliases, optional FROM, LIMIT and OFFSET; WHERE requires a supplied parser. */
|
|
23
|
+
export function parseSQLSelect(sql: string, options: SQLSelectParserOptions = {}): TableQuery {
|
|
24
|
+
const reader = new SQLQueryReader(sql, options);
|
|
25
|
+
reader.expectKeyword('SELECT');
|
|
26
|
+
let select: TableSelection[] | undefined;
|
|
27
|
+
if (!reader.takeSymbol('*')) {
|
|
28
|
+
select = [];
|
|
29
|
+
do {
|
|
30
|
+
const expression = reader.selectionExpression() as any;
|
|
31
|
+
const alias = reader.takeKeyword('AS') ? reader.identifier() : undefined;
|
|
32
|
+
if (expression.type === 'Identifier')
|
|
33
|
+
select.push(alias ? {column: expression.name, as: alias} : expression.name);
|
|
34
|
+
else if (
|
|
35
|
+
expression.type === 'CallExpression' &&
|
|
36
|
+
/^array(?:_(?:float(?:16|32|64)|uint8))?$/.test(expression.callee.name) &&
|
|
37
|
+
expression.arguments.every((argument: any) => argument.type === 'Identifier')
|
|
38
|
+
) {
|
|
39
|
+
if (!alias) throw reader.error('SQL ARRAY requires AS');
|
|
40
|
+
const type = expression.callee.name.slice(6);
|
|
41
|
+
select.push({
|
|
42
|
+
array: expression.arguments.map((argument: any) => argument.name),
|
|
43
|
+
as: alias,
|
|
44
|
+
...(type ? {type} : {})
|
|
45
|
+
});
|
|
46
|
+
} else select.push({expression, ...(alias ? {as: alias} : {})});
|
|
47
|
+
} while (reader.takeSymbol(','));
|
|
48
|
+
}
|
|
49
|
+
let from: TableReference | undefined;
|
|
50
|
+
if (reader.takeKeyword('FROM')) {
|
|
51
|
+
const source = reader.identifier();
|
|
52
|
+
from = reader.takeSymbol('.') ? {source, table: reader.identifier()} : source;
|
|
53
|
+
if (!source.trim() || (typeof from === 'object' && !from.table.trim()))
|
|
54
|
+
throw reader.error('SQL FROM requires non-empty names.');
|
|
55
|
+
}
|
|
56
|
+
let where: ColumnarPredicate | Readonly<{expression: Expression}> | undefined;
|
|
57
|
+
if (reader.takeKeyword('WHERE')) {
|
|
58
|
+
if (typeof options.parseWhere !== 'function')
|
|
59
|
+
throw reader.error(
|
|
60
|
+
'parseSQLSelect requires parseWhere for WHERE; use parseSQLQuery for built-in predicate support.'
|
|
61
|
+
);
|
|
62
|
+
where = reader.parseWhere(options.parseWhere);
|
|
63
|
+
if (!where || typeof where !== 'object' || Array.isArray(where))
|
|
64
|
+
throw reader.error('SQL WHERE parser must return a predicate.');
|
|
65
|
+
}
|
|
66
|
+
const pagination = reader.pagination();
|
|
67
|
+
reader.end();
|
|
68
|
+
return {
|
|
69
|
+
...(select ? {select} : {}),
|
|
70
|
+
...(from === undefined ? {} : {from}),
|
|
71
|
+
...(where ? {where} : {}),
|
|
72
|
+
...pagination
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Small bounded scanner shared by structural and predicate-enabled SELECT parsing. */
|
|
77
|
+
export class SQLQueryReader {
|
|
78
|
+
/** Original SQL, retained for exact WHERE substring extraction. */
|
|
79
|
+
protected readonly sql: string;
|
|
80
|
+
/** Current character offset. */
|
|
81
|
+
protected position = 0;
|
|
82
|
+
/** Number of consumed structural tokens. */
|
|
83
|
+
private tokens = 0;
|
|
84
|
+
|
|
85
|
+
/** Rejects empty or oversized statements before scanning. */
|
|
86
|
+
constructor(
|
|
87
|
+
sql: string,
|
|
88
|
+
protected readonly parameterOptions: SQLParameterOptions = {}
|
|
89
|
+
) {
|
|
90
|
+
validateSQLParameterOptions(parameterOptions);
|
|
91
|
+
if (typeof sql !== 'string' || !sql.trim() || sql.length > MAXIMUM_SQL_LENGTH)
|
|
92
|
+
throw new SQLParseError(
|
|
93
|
+
'SQL query requires a non-empty statement of at most 65536 characters.',
|
|
94
|
+
'',
|
|
95
|
+
0
|
|
96
|
+
);
|
|
97
|
+
this.sql = sql;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Reads a bounded scalar or fixed-array projection. */
|
|
101
|
+
selectionExpression(): Expression {
|
|
102
|
+
this.skipWhitespace();
|
|
103
|
+
this.countToken();
|
|
104
|
+
const reader = this.expressionReader();
|
|
105
|
+
const expression = reader.expression();
|
|
106
|
+
this.position = reader.position;
|
|
107
|
+
return expression;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Consumes one mandatory keyword. */
|
|
111
|
+
expectKeyword(keyword: string): void {
|
|
112
|
+
if (!this.takeKeyword(keyword)) throw this.error(`SQL query expected ${keyword}.`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Consumes an unquoted keyword without folding identifier names. */
|
|
116
|
+
takeKeyword(keyword: string): boolean {
|
|
117
|
+
this.skipWhitespace();
|
|
118
|
+
const match = /^[A-Za-z_][A-Za-z0-9_]*/.exec(this.sql.slice(this.position));
|
|
119
|
+
if (match?.[0].toUpperCase() !== keyword) return false;
|
|
120
|
+
this.position += match[0].length;
|
|
121
|
+
this.countToken();
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Consumes a punctuation token. */
|
|
126
|
+
takeSymbol(symbol: string): boolean {
|
|
127
|
+
this.skipWhitespace();
|
|
128
|
+
if (this.sql[this.position] !== symbol) return false;
|
|
129
|
+
this.position++;
|
|
130
|
+
this.countToken();
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Reads one literal identifier, including doubled double-quote escapes. */
|
|
135
|
+
identifier(): string {
|
|
136
|
+
this.countToken();
|
|
137
|
+
const reader = this.expressionReader();
|
|
138
|
+
const identifier = reader.readIdentifier();
|
|
139
|
+
this.position = reader.position;
|
|
140
|
+
return identifier;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Parses a WHERE fragment and translates its cursor back to the full statement. */
|
|
144
|
+
parseWhere(parse: NonNullable<SQLSelectParserOptions['parseWhere']>): ReturnType<typeof parse> {
|
|
145
|
+
this.skipWhitespace();
|
|
146
|
+
const start = this.position;
|
|
147
|
+
const source = this.whereExpression();
|
|
148
|
+
try {
|
|
149
|
+
return parse(source);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
if (error instanceof SQLParseError)
|
|
152
|
+
throw new SQLParseError(error.reason, this.sql, start + error.offset);
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Creates an error at the current statement cursor. */
|
|
158
|
+
error(message: string, offset = this.position): SQLParseError {
|
|
159
|
+
return new SQLParseError(message, this.sql, offset);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Finds a top-level pagination clause or terminator without interpreting quoted predicate contents. */
|
|
163
|
+
whereExpression(): string {
|
|
164
|
+
this.skipWhitespace();
|
|
165
|
+
const start = this.position;
|
|
166
|
+
let quote = '';
|
|
167
|
+
let quoteStart = start;
|
|
168
|
+
let depth = 0;
|
|
169
|
+
let groupingDepth = 0;
|
|
170
|
+
const membershipParens: boolean[] = [];
|
|
171
|
+
while (this.position < this.sql.length) {
|
|
172
|
+
const character = this.sql[this.position];
|
|
173
|
+
if (quote) {
|
|
174
|
+
if (character === quote) {
|
|
175
|
+
if (this.sql[this.position + 1] === quote) {
|
|
176
|
+
this.position += 2;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
quote = '';
|
|
180
|
+
}
|
|
181
|
+
} else if (character === '"' || character === "'") {
|
|
182
|
+
quote = character;
|
|
183
|
+
quoteStart = this.position;
|
|
184
|
+
} else if (
|
|
185
|
+
this.sql.startsWith('--', this.position) ||
|
|
186
|
+
this.sql.startsWith('/*', this.position)
|
|
187
|
+
)
|
|
188
|
+
throw this.error('SQL comments are not supported.');
|
|
189
|
+
else if (character === '(') {
|
|
190
|
+
// IN list delimiters do not add a predicate grouping level.
|
|
191
|
+
const membership = /\bIN\s*$/i.test(this.sql.slice(start, this.position));
|
|
192
|
+
membershipParens.push(membership);
|
|
193
|
+
depth++;
|
|
194
|
+
if (!membership && ++groupingDepth > 64)
|
|
195
|
+
throw this.error('SQL query exceeds its predicate nesting limit.');
|
|
196
|
+
} else if (character === ')') {
|
|
197
|
+
if (--depth < 0) throw this.error('SQL query contains unbalanced parentheses.');
|
|
198
|
+
if (!membershipParens.pop()) groupingDepth--;
|
|
199
|
+
} else if (depth === 0 && (character === ';' || this.atPaginationClause())) break;
|
|
200
|
+
this.position++;
|
|
201
|
+
}
|
|
202
|
+
if (quote || depth)
|
|
203
|
+
throw this.error(
|
|
204
|
+
'SQL query contains an unterminated predicate.',
|
|
205
|
+
quote ? quoteStart : this.position
|
|
206
|
+
);
|
|
207
|
+
const expression = this.sql.slice(start, this.position).trim();
|
|
208
|
+
if (!expression) throw this.error('SQL WHERE requires a predicate.');
|
|
209
|
+
return expression;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Checks for an unquoted pagination keyword at the current predicate boundary. */
|
|
213
|
+
private atPaginationClause(): boolean {
|
|
214
|
+
return (
|
|
215
|
+
!/[A-Za-z0-9_.:]/.test(this.sql[this.position - 1] || '') &&
|
|
216
|
+
/^(?:LIMIT|OFFSET)(?![A-Za-z0-9_.:])/i.test(this.sql.slice(this.position, this.position + 7))
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Reads optional LIMIT and OFFSET in either order. */
|
|
221
|
+
pagination(): Pick<TableQuery, 'limit' | 'offset'> {
|
|
222
|
+
let limit: number | undefined;
|
|
223
|
+
let offset: number | undefined;
|
|
224
|
+
while (true) {
|
|
225
|
+
if (this.takeKeyword('LIMIT')) {
|
|
226
|
+
if (limit !== undefined) throw this.error('Duplicate SQL LIMIT clause.');
|
|
227
|
+
limit = this.rowCount('LIMIT');
|
|
228
|
+
} else if (this.takeKeyword('OFFSET')) {
|
|
229
|
+
if (offset !== undefined) throw this.error('Duplicate SQL OFFSET clause.');
|
|
230
|
+
offset = this.rowCount('OFFSET');
|
|
231
|
+
if (!this.takeKeyword('ROWS')) this.takeKeyword('ROW');
|
|
232
|
+
} else break;
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
...(offset === undefined ? {} : {offset}),
|
|
236
|
+
...(limit === undefined ? {} : {limit})
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Reads the non-negative safe integer for a pagination clause. */
|
|
241
|
+
private rowCount(clause: 'LIMIT' | 'OFFSET'): number {
|
|
242
|
+
this.skipWhitespace();
|
|
243
|
+
const match = /^\d+(?![A-Za-z0-9_.])/.exec(this.sql.slice(this.position));
|
|
244
|
+
const value = Number(match?.[0]);
|
|
245
|
+
if (!match || !Number.isSafeInteger(value))
|
|
246
|
+
throw this.error(`SQL ${clause} requires a non-negative safe integer.`);
|
|
247
|
+
this.position += match[0].length;
|
|
248
|
+
return value;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Requires one complete statement with at most one trailing semicolon. */
|
|
252
|
+
end(): void {
|
|
253
|
+
this.takeSymbol(';');
|
|
254
|
+
this.skipWhitespace();
|
|
255
|
+
if (this.position !== this.sql.length) throw this.error('Unsupported SQL query syntax.');
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** Skips whitespace without altering quoted identifiers. */
|
|
259
|
+
protected skipWhitespace(): void {
|
|
260
|
+
while (this.position < this.sql.length && /\s/.test(this.sql[this.position])) this.position++;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
protected expressionReader(): SQLExpressionReader {
|
|
264
|
+
return new SQLExpressionReader(this.sql, this.position, this.parameterOptions);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Bounds large projection lists independently of predicate parsing. */
|
|
268
|
+
private countToken(): void {
|
|
269
|
+
if (++this.tokens > MAXIMUM_SELECT_TOKENS)
|
|
270
|
+
throw this.error('SQL query exceeds its structural token limit.');
|
|
271
|
+
}
|
|
272
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {SQLParameterOptions} from './sql-parameters';
|
|
6
|
+
import type {Expression} from '../expressions';
|
|
7
|
+
import {SQLExpressionReader, SQL_RESERVED_WORDS} from './parse-sql-expression';
|
|
8
|
+
import {SQLQueryReader} from './parse-sql-select';
|
|
9
|
+
|
|
10
|
+
/** A named input with an optional alias in a relational SELECT. */
|
|
11
|
+
export type SQLTableBinding = Readonly<{name: string; alias?: string}>;
|
|
12
|
+
|
|
13
|
+
/** A join whose condition is interpreted by the query planner. */
|
|
14
|
+
export type SQLJoin = Readonly<{
|
|
15
|
+
type: 'inner' | 'left' | 'semi' | 'anti' | 'cross';
|
|
16
|
+
/** Experimental lateral collection expansion. */
|
|
17
|
+
unnest?: Readonly<{expression: Expression; columns: readonly string[]; ordinality: boolean}>;
|
|
18
|
+
table: SQLTableBinding;
|
|
19
|
+
on: Expression;
|
|
20
|
+
}>;
|
|
21
|
+
|
|
22
|
+
/** A sort expression and its explicitly requested null placement, if supplied. */
|
|
23
|
+
export type SQLSort = Readonly<{
|
|
24
|
+
expression: Expression;
|
|
25
|
+
direction: 'asc' | 'desc';
|
|
26
|
+
nulls?: 'first' | 'last';
|
|
27
|
+
}>;
|
|
28
|
+
|
|
29
|
+
/** Relational SELECT syntax for callers that implement their own query planner. */
|
|
30
|
+
export type SQLSelectStatement = Readonly<{
|
|
31
|
+
/** Omission represents SELECT *. */
|
|
32
|
+
select?: readonly Readonly<{expression: Expression; alias?: string}>[];
|
|
33
|
+
from: SQLTableBinding;
|
|
34
|
+
joins: readonly SQLJoin[];
|
|
35
|
+
where?: Expression;
|
|
36
|
+
groupBy?: readonly Expression[];
|
|
37
|
+
orderBy?: readonly SQLSort[];
|
|
38
|
+
limit?: number;
|
|
39
|
+
offset?: number;
|
|
40
|
+
}>;
|
|
41
|
+
|
|
42
|
+
/** Options for interpreting named parameters in a relational SELECT. */
|
|
43
|
+
export type SQLSelectStatementOptions = SQLParameterOptions;
|
|
44
|
+
|
|
45
|
+
/** Parses a SELECT with a required FROM, without assuming an execution engine. */
|
|
46
|
+
export function parseSQLSelectStatement(
|
|
47
|
+
sql: string,
|
|
48
|
+
options: SQLSelectStatementOptions = {}
|
|
49
|
+
): SQLSelectStatement {
|
|
50
|
+
const reader = new RelationalQueryReader(sql, options);
|
|
51
|
+
const statement = {...readSelect(reader), ...readTail(reader)};
|
|
52
|
+
reader.end();
|
|
53
|
+
return statement;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** SELECT inputs followed by ordering and pagination for the combined result. */
|
|
57
|
+
export type SQLSelectCompound = {
|
|
58
|
+
inputs: readonly SQLSelectStatement[];
|
|
59
|
+
orderBy?: readonly SQLSort[];
|
|
60
|
+
limit?: number;
|
|
61
|
+
offset?: number;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** Parses SELECT or a sequence of UNION ALL inputs. */
|
|
65
|
+
export function parseSQLSelectCompound(
|
|
66
|
+
sql: string,
|
|
67
|
+
options: SQLSelectStatementOptions = {}
|
|
68
|
+
): SQLSelectCompound {
|
|
69
|
+
const reader = new RelationalQueryReader(sql, options);
|
|
70
|
+
const inputs = [readSelect(reader)];
|
|
71
|
+
while (reader.takeKeyword('UNION')) {
|
|
72
|
+
reader.expectKeyword('ALL');
|
|
73
|
+
inputs.push(readSelect(reader));
|
|
74
|
+
}
|
|
75
|
+
const tail = readTail(reader);
|
|
76
|
+
reader.end();
|
|
77
|
+
return {inputs, ...tail};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function readSelect(reader: RelationalQueryReader): SQLSelectStatement {
|
|
81
|
+
reader.expectKeyword('SELECT');
|
|
82
|
+
const select = reader.takeSymbol('*')
|
|
83
|
+
? undefined
|
|
84
|
+
: readList(reader, () => {
|
|
85
|
+
const expression = reader.selectionExpression();
|
|
86
|
+
const alias = reader.takeKeyword('AS') ? reader.identifier() : undefined;
|
|
87
|
+
return {expression, ...(alias === undefined ? {} : {alias})};
|
|
88
|
+
});
|
|
89
|
+
reader.expectKeyword('FROM');
|
|
90
|
+
const from = readTable(reader);
|
|
91
|
+
const joins: SQLJoin[] = [];
|
|
92
|
+
for (let type = readJoinType(reader); type; type = readJoinType(reader)) {
|
|
93
|
+
if (reader.takeKeyword('UNNEST')) {
|
|
94
|
+
reader.expectSymbol('(');
|
|
95
|
+
const expression = reader.selectionExpression();
|
|
96
|
+
reader.expectSymbol(')');
|
|
97
|
+
const ordinality = reader.takeKeyword('WITH');
|
|
98
|
+
if (ordinality) reader.expectKeyword('ORDINALITY');
|
|
99
|
+
reader.expectKeyword('AS');
|
|
100
|
+
const alias = reader.identifier();
|
|
101
|
+
reader.expectSymbol('(');
|
|
102
|
+
const columns = readList(reader, () => reader.identifier());
|
|
103
|
+
reader.expectSymbol(')');
|
|
104
|
+
if (type === 'left') {
|
|
105
|
+
reader.expectKeyword('ON');
|
|
106
|
+
reader.expectKeyword('TRUE');
|
|
107
|
+
} else if (type !== 'cross') throw reader.error('UNNEST requires CROSS JOIN or LEFT JOIN');
|
|
108
|
+
joins.push({
|
|
109
|
+
type,
|
|
110
|
+
table: {name: alias, alias},
|
|
111
|
+
on: {type: 'Literal', value: true},
|
|
112
|
+
unnest: {expression, columns, ordinality}
|
|
113
|
+
});
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (type === 'cross') throw reader.error('CROSS JOIN requires UNNEST');
|
|
117
|
+
const table = readTable(reader);
|
|
118
|
+
reader.expectKeyword('ON');
|
|
119
|
+
joins.push({type, table, on: reader.selectionExpression()});
|
|
120
|
+
}
|
|
121
|
+
const where = reader.takeKeyword('WHERE') ? reader.selectionExpression() : undefined;
|
|
122
|
+
const groupBy = readBy(reader, 'GROUP', () => reader.selectionExpression());
|
|
123
|
+
return {
|
|
124
|
+
from,
|
|
125
|
+
joins,
|
|
126
|
+
...(select ? {select} : {}),
|
|
127
|
+
...(where ? {where} : {}),
|
|
128
|
+
...(groupBy ? {groupBy} : {})
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function readTail(reader: RelationalQueryReader) {
|
|
133
|
+
const orderBy = readBy(reader, 'ORDER', () => readSort(reader));
|
|
134
|
+
return {...(orderBy ? {orderBy} : {}), ...reader.pagination()};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function readBy<T>(reader: RelationalQueryReader, clause: string, read: () => T): T[] | undefined {
|
|
138
|
+
if (!reader.takeKeyword(clause)) return undefined;
|
|
139
|
+
reader.expectKeyword('BY');
|
|
140
|
+
return readList(reader, read);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function readTable(reader: RelationalQueryReader): SQLTableBinding {
|
|
144
|
+
const name = reader.tableName();
|
|
145
|
+
const alias = reader.takeKeyword('AS') ? reader.identifier() : reader.optionalAlias();
|
|
146
|
+
return {name, ...(alias === undefined ? {} : {alias})};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function readJoinType(reader: SQLQueryReader): SQLJoin['type'] | undefined {
|
|
150
|
+
if (reader.takeKeyword('CROSS')) {
|
|
151
|
+
reader.expectKeyword('JOIN');
|
|
152
|
+
return 'cross';
|
|
153
|
+
}
|
|
154
|
+
if (reader.takeKeyword('JOIN')) return 'inner';
|
|
155
|
+
let type: SQLJoin['type'];
|
|
156
|
+
if (reader.takeKeyword('INNER')) type = 'inner';
|
|
157
|
+
else if (reader.takeKeyword('LEFT')) {
|
|
158
|
+
reader.takeKeyword('OUTER');
|
|
159
|
+
type = 'left';
|
|
160
|
+
} else if (reader.takeKeyword('SEMI')) type = 'semi';
|
|
161
|
+
else if (reader.takeKeyword('ANTI')) type = 'anti';
|
|
162
|
+
else return undefined;
|
|
163
|
+
reader.expectKeyword('JOIN');
|
|
164
|
+
return type;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function readSort(reader: SQLQueryReader): SQLSort {
|
|
168
|
+
const expression = reader.selectionExpression();
|
|
169
|
+
const direction = reader.takeKeyword('DESC') ? 'desc' : 'asc';
|
|
170
|
+
if (direction === 'asc') reader.takeKeyword('ASC');
|
|
171
|
+
if (!reader.takeKeyword('NULLS')) return {expression, direction};
|
|
172
|
+
if (reader.takeKeyword('FIRST')) return {expression, direction, nulls: 'first'};
|
|
173
|
+
reader.expectKeyword('LAST');
|
|
174
|
+
return {expression, direction, nulls: 'last'};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function readList<T>(reader: SQLQueryReader, readItem: () => T): T[] {
|
|
178
|
+
const items = [readItem()];
|
|
179
|
+
while (reader.takeSymbol(',')) items.push(readItem());
|
|
180
|
+
return items;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
class RelationalQueryReader extends SQLQueryReader {
|
|
184
|
+
/** Requires punctuation with a positioned diagnostic. */
|
|
185
|
+
expectSymbol(symbol: string): void {
|
|
186
|
+
if (!this.takeSymbol(symbol)) throw this.error(`Expected ${symbol}`);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
constructor(
|
|
190
|
+
sql: string,
|
|
191
|
+
private readonly options: SQLSelectStatementOptions
|
|
192
|
+
) {
|
|
193
|
+
super(sql, options);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
tableName(): string {
|
|
197
|
+
this.skipWhitespace();
|
|
198
|
+
if (/^LATERAL(?![A-Za-z0-9_])/i.test(this.sql.slice(this.position))) {
|
|
199
|
+
throw this.error('SQL LATERAL table expressions are not supported.');
|
|
200
|
+
}
|
|
201
|
+
return this.identifier();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Reads an implicit alias unless the next token starts a clause. */
|
|
205
|
+
optionalAlias(): string | undefined {
|
|
206
|
+
this.skipWhitespace();
|
|
207
|
+
if (this.sql[this.position] === '"') return this.identifier();
|
|
208
|
+
const name = /^[A-Za-z_][A-Za-z0-9_]*/.exec(this.sql.slice(this.position))?.[0];
|
|
209
|
+
if (
|
|
210
|
+
name &&
|
|
211
|
+
!SQL_RESERVED_WORDS.has(name.toUpperCase()) &&
|
|
212
|
+
!/^(INNER|LEFT|RIGHT|FULL|CROSS|OUTER|SEMI|ANTI|NATURAL|LATERAL|ON)$/i.test(name)
|
|
213
|
+
)
|
|
214
|
+
return this.identifier();
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
protected override expressionReader(): SQLExpressionReader {
|
|
219
|
+
return new RelationalExpressionReader(this.sql, this.position, this.options);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
protected override skipWhitespace(): void {
|
|
223
|
+
this.position = skipLineComments(this.sql, this.position);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
class RelationalExpressionReader extends SQLExpressionReader {
|
|
228
|
+
constructor(source: string, position: number, options: SQLSelectStatementOptions) {
|
|
229
|
+
super(source, position, options);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
override space(): void {
|
|
233
|
+
this.position = skipLineComments(this.source, this.position);
|
|
234
|
+
super.space();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Preserves dot access until the planner can resolve table aliases. */
|
|
238
|
+
protected override memberAccess(object: Expression, name: string): Expression {
|
|
239
|
+
const property = this.node({type: 'Identifier', name});
|
|
240
|
+
return this.node({type: 'MemberExpression', computed: false, object, property}, [
|
|
241
|
+
object,
|
|
242
|
+
property
|
|
243
|
+
]);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
protected override wildcardArgument(name: string): Expression | undefined {
|
|
247
|
+
if (name.toUpperCase() !== 'COUNT' || !this.take('*')) return undefined;
|
|
248
|
+
return this.node({type: 'Identifier', name: '*', sqlWildcard: true});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function skipLineComments(source: string, position: number): number {
|
|
253
|
+
while (position < source.length) {
|
|
254
|
+
if (/\s/.test(source[position]!)) position++;
|
|
255
|
+
else if (source.startsWith('--', position)) {
|
|
256
|
+
while (position < source.length && !/[\r\n]/.test(source[position]!)) position++;
|
|
257
|
+
} else break;
|
|
258
|
+
}
|
|
259
|
+
return position;
|
|
260
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {Expression} from '../expressions';
|
|
6
|
+
import type {ColumnarPredicate, ColumnarPredicateProperty} from '../expressions/columnar-predicate';
|
|
7
|
+
|
|
8
|
+
/** One source column selected for logical output, optionally under a new name. */
|
|
9
|
+
export type TableSelection =
|
|
10
|
+
| Readonly<{
|
|
11
|
+
array: readonly string[];
|
|
12
|
+
as: string;
|
|
13
|
+
type?: 'float16' | 'float32' | 'float64' | 'uint8';
|
|
14
|
+
}>
|
|
15
|
+
| Readonly<{expression: Expression; as?: string}>
|
|
16
|
+
| string
|
|
17
|
+
| Readonly<{
|
|
18
|
+
/** Source column name, resolved before aliases are applied. */
|
|
19
|
+
column: string;
|
|
20
|
+
/** Output field name. Defaults to the source column name. */
|
|
21
|
+
as?: string;
|
|
22
|
+
}>;
|
|
23
|
+
|
|
24
|
+
/** Named source, or a named table exposed by a source. Strings are literal names, not dotted paths. */
|
|
25
|
+
export type TableReference =
|
|
26
|
+
| string
|
|
27
|
+
| Readonly<{
|
|
28
|
+
/** Name of a registered data source. */
|
|
29
|
+
source: string;
|
|
30
|
+
/** Name of a table exposed by that source. */
|
|
31
|
+
table: string;
|
|
32
|
+
}>;
|
|
33
|
+
|
|
34
|
+
/** Logical query description, independent of cancellation and execution observers. */
|
|
35
|
+
export type TableQuery<
|
|
36
|
+
PredicateT extends ColumnarPredicate<unknown, ColumnarPredicateProperty> = ColumnarPredicate
|
|
37
|
+
> = Readonly<{
|
|
38
|
+
/** Optional named source or qualified table selected before planning. */
|
|
39
|
+
from?: TableReference;
|
|
40
|
+
/** Output columns in selection order. Omission selects all source columns. */
|
|
41
|
+
select?: readonly TableSelection[];
|
|
42
|
+
/** Predicate evaluated against source columns before output aliases are applied. */
|
|
43
|
+
where?: PredicateT | Readonly<{expression: Expression}>;
|
|
44
|
+
/** Number of filtered rows skipped before applying the limit. */
|
|
45
|
+
offset?: number;
|
|
46
|
+
/** Maximum number of rows retained after filtering and offset. */
|
|
47
|
+
limit?: number;
|
|
48
|
+
}>;
|
|
49
|
+
|
|
50
|
+
/** Portable logical metadata commands, independent of SQL parsing and execution controls. */
|
|
51
|
+
export type SQLCatalogQuery = Readonly<
|
|
52
|
+
| {
|
|
53
|
+
/** Lists locally registered source names. */
|
|
54
|
+
command: 'showSources';
|
|
55
|
+
}
|
|
56
|
+
| {
|
|
57
|
+
/** Lists discoverable tables in registration order. */
|
|
58
|
+
command: 'showTables';
|
|
59
|
+
/** Optional exact source name; omission discovers every registered source. */
|
|
60
|
+
from?: string;
|
|
61
|
+
}
|
|
62
|
+
| {
|
|
63
|
+
/** Describes the fields of one table. */
|
|
64
|
+
command: 'describe';
|
|
65
|
+
/** Literal source name, or separate source and loaded table names. */
|
|
66
|
+
from: TableReference;
|
|
67
|
+
}
|
|
68
|
+
>;
|
|
69
|
+
|
|
70
|
+
/** Logical table selection or catalog request, independent of execution. */
|
|
71
|
+
export type SQLQuery = TableQuery | SQLCatalogQuery;
|