@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,1807 @@
|
|
|
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 name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// dist/parser/index.js
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
SQLParseError: () => SQLParseError,
|
|
23
|
+
SQL_PREDICATE_JSON_SCHEMA: () => SQL_PREDICATE_JSON_SCHEMA,
|
|
24
|
+
areSQLParametersEqual: () => areSQLParametersEqual,
|
|
25
|
+
bindSQLPredicate: () => bindSQLPredicate,
|
|
26
|
+
getSQLParameterNames: () => getSQLParameterNames,
|
|
27
|
+
isSQLPredicate: () => isSQLPredicate,
|
|
28
|
+
isSQLPredicateParameter: () => isSQLPredicateParameter,
|
|
29
|
+
parseSQLExpression: () => parseSQLExpression,
|
|
30
|
+
parseSQLPredicate: () => parseSQLPredicate,
|
|
31
|
+
parseSQLQuery: () => parseSQLQuery,
|
|
32
|
+
parseSQLSelect: () => parseSQLSelect,
|
|
33
|
+
parseSQLSelectCompound: () => parseSQLSelectCompound,
|
|
34
|
+
parseSQLSelectStatement: () => parseSQLSelectStatement,
|
|
35
|
+
prepareSQLParameters: () => prepareSQLParameters,
|
|
36
|
+
validateSQLPredicate: () => validateSQLPredicate
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
|
|
40
|
+
// dist/expressions/scalar-type.js
|
|
41
|
+
var COLUMNAR_SCALAR_TYPES = Object.freeze([
|
|
42
|
+
"int8",
|
|
43
|
+
"int16",
|
|
44
|
+
"int32",
|
|
45
|
+
"int64",
|
|
46
|
+
"uint8",
|
|
47
|
+
"uint16",
|
|
48
|
+
"uint32",
|
|
49
|
+
"uint64",
|
|
50
|
+
"float16",
|
|
51
|
+
"float32",
|
|
52
|
+
"float64",
|
|
53
|
+
"bool",
|
|
54
|
+
"utf8"
|
|
55
|
+
]);
|
|
56
|
+
function isColumnarScalarType(value) {
|
|
57
|
+
return typeof value === "string" && COLUMNAR_SCALAR_TYPES.includes(value);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// dist/expressions/columnar-predicate.js
|
|
61
|
+
function isColumnarPredicateParameter(value) {
|
|
62
|
+
const parameterValue = value;
|
|
63
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && Object.keys(value).length === 1 && Object.prototype.hasOwnProperty.call(value, "parameter") && typeof parameterValue.parameter === "string" && parameterValue.parameter.length > 0;
|
|
64
|
+
}
|
|
65
|
+
function isColumnarPredicateValue(value) {
|
|
66
|
+
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;
|
|
67
|
+
}
|
|
68
|
+
function bindColumnarPredicateParameters(predicate, parameters) {
|
|
69
|
+
switch (predicate.op) {
|
|
70
|
+
case "and":
|
|
71
|
+
case "or":
|
|
72
|
+
return {
|
|
73
|
+
op: predicate.op,
|
|
74
|
+
args: predicate.args.map((child) => bindColumnarPredicateParameters(child, parameters))
|
|
75
|
+
};
|
|
76
|
+
case "not":
|
|
77
|
+
return {
|
|
78
|
+
op: "not",
|
|
79
|
+
args: [bindColumnarPredicateParameters(predicate.args[0], parameters)]
|
|
80
|
+
};
|
|
81
|
+
case "isNull":
|
|
82
|
+
return { op: "isNull", args: [copyColumnarPredicateProperty(predicate.args[0])] };
|
|
83
|
+
case "in":
|
|
84
|
+
return {
|
|
85
|
+
op: "in",
|
|
86
|
+
args: [
|
|
87
|
+
copyColumnarPredicateProperty(predicate.args[0]),
|
|
88
|
+
predicate.args[1].map((value) => bindColumnarPredicateValue(value, parameters))
|
|
89
|
+
]
|
|
90
|
+
};
|
|
91
|
+
default:
|
|
92
|
+
return {
|
|
93
|
+
op: predicate.op,
|
|
94
|
+
args: [
|
|
95
|
+
copyColumnarPredicateProperty(predicate.args[0]),
|
|
96
|
+
bindColumnarPredicateValue(predicate.args[1], parameters)
|
|
97
|
+
]
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function bindColumnarPredicateValue(value, parameters) {
|
|
102
|
+
if (!isColumnarPredicateParameter(value))
|
|
103
|
+
return value;
|
|
104
|
+
if (!Object.prototype.hasOwnProperty.call(parameters, value.parameter)) {
|
|
105
|
+
throw new Error(`Columnar predicate parameter ":${value.parameter}" requires a value`);
|
|
106
|
+
}
|
|
107
|
+
const parameterValue = parameters[value.parameter];
|
|
108
|
+
if (isColumnarPredicateParameter(parameterValue)) {
|
|
109
|
+
throw new Error(`Columnar predicate parameter ":${value.parameter}" cannot reference another parameter`);
|
|
110
|
+
}
|
|
111
|
+
if (!isColumnarPredicateValue(parameterValue)) {
|
|
112
|
+
throw new Error(`Columnar predicate parameter ":${value.parameter}" has an unsupported value`);
|
|
113
|
+
}
|
|
114
|
+
return copyColumnarPredicateValue(parameterValue);
|
|
115
|
+
}
|
|
116
|
+
function copyColumnarPredicateProperty(property) {
|
|
117
|
+
return {
|
|
118
|
+
...property,
|
|
119
|
+
...property.casts ? { casts: [...property.casts] } : {},
|
|
120
|
+
property: typeof property.property === "string" ? property.property : [...property.property]
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function copyColumnarPredicateValue(value) {
|
|
124
|
+
if (value instanceof Uint8Array)
|
|
125
|
+
return value.slice();
|
|
126
|
+
if (value instanceof Date)
|
|
127
|
+
return new Date(value.getTime());
|
|
128
|
+
return value;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// dist/parser/sql-parameters.js
|
|
132
|
+
function validateSQLParameterOptions(options) {
|
|
133
|
+
if (options.parameters !== void 0 && options.parseParameter !== void 0) {
|
|
134
|
+
throw new Error("SQL parameters and parseParameter are mutually exclusive");
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function bindSQLParameter(name, options) {
|
|
138
|
+
if (options.parseParameter)
|
|
139
|
+
return options.parseParameter(name);
|
|
140
|
+
if (isSQLParameterSnapshot(options.parameters))
|
|
141
|
+
return snapshotParameter(options.parameters, name);
|
|
142
|
+
if (!Object.prototype.hasOwnProperty.call(options.parameters ?? {}, name)) {
|
|
143
|
+
throw new Error(`SQL parameter ":${name}" requires a value`);
|
|
144
|
+
}
|
|
145
|
+
const value = options.parameters[name];
|
|
146
|
+
if (isNumericArray(value) && !(value instanceof Uint8Array))
|
|
147
|
+
return typedArrayLiteral(value, name);
|
|
148
|
+
if (value && typeof value === "object" && "type" in value) {
|
|
149
|
+
if (value.type === "array" && isNumericArray(value.value))
|
|
150
|
+
return typedArrayLiteral(value.value, name);
|
|
151
|
+
if ((value.type === "utf8" || value.type === "binary") && value.value instanceof Uint8Array) {
|
|
152
|
+
const data = new Uint8Array(value.value);
|
|
153
|
+
if (value.type === "binary")
|
|
154
|
+
return literal(data);
|
|
155
|
+
try {
|
|
156
|
+
return {
|
|
157
|
+
type: "Literal",
|
|
158
|
+
value: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(data),
|
|
159
|
+
utf8: data
|
|
160
|
+
};
|
|
161
|
+
} catch {
|
|
162
|
+
throw new Error(`SQL parameter ":${name}" requires valid UTF-8 bytes`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
throw new Error(`SQL parameter ":${name}" has an unsupported binary format`);
|
|
166
|
+
}
|
|
167
|
+
if (Array.isArray(value)) {
|
|
168
|
+
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)))))
|
|
169
|
+
throw new Error(`SQL parameter ":${name}" has an unsupported array value`);
|
|
170
|
+
return arrayLiteral(value, name);
|
|
171
|
+
}
|
|
172
|
+
if (value !== null && !isColumnarPredicateValue(value)) {
|
|
173
|
+
throw new Error(`SQL parameter ":${name}" has an unsupported value`);
|
|
174
|
+
}
|
|
175
|
+
return literal(value instanceof Date ? new Date(value.getTime()) : value instanceof Uint8Array ? new Uint8Array(value) : value);
|
|
176
|
+
}
|
|
177
|
+
function arrayLiteral(values, name) {
|
|
178
|
+
return {
|
|
179
|
+
type: "ArrayExpression",
|
|
180
|
+
elements: Array.from(values, (value) => Array.isArray(value) ? arrayLiteral(value, name) : isNumericArray(value) ? typedArrayLiteral(value, name) : literal(value))
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
function literal(value) {
|
|
184
|
+
if (value instanceof Uint8Array || value instanceof Date)
|
|
185
|
+
return { type: "Literal", value };
|
|
186
|
+
return {
|
|
187
|
+
type: "Literal",
|
|
188
|
+
value,
|
|
189
|
+
raw: typeof value === "bigint" ? String(value) : JSON.stringify(value)
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function isNumericArray(value) {
|
|
193
|
+
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;
|
|
194
|
+
}
|
|
195
|
+
function typedArrayLiteral(value, name) {
|
|
196
|
+
if (value.length > 4096 || !value.every(Number.isFinite)) {
|
|
197
|
+
throw new Error(`SQL parameter ":${name}" has an unsupported numeric array`);
|
|
198
|
+
}
|
|
199
|
+
const typedValues = value instanceof Uint8Array ? new Uint8Array(value) : value.slice();
|
|
200
|
+
return {
|
|
201
|
+
type: "ArrayExpression",
|
|
202
|
+
elements: Array.from(typedValues, (item) => literal(item)),
|
|
203
|
+
typedValues
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// dist/parser/parameter-snapshots.js
|
|
208
|
+
var snapshots = /* @__PURE__ */ new WeakMap();
|
|
209
|
+
function prepareSQLParameters(parameters, options = {}) {
|
|
210
|
+
const previous = options.previous && storage(options.previous);
|
|
211
|
+
const entries = /* @__PURE__ */ new Map();
|
|
212
|
+
for (const name of Object.keys(parameters)) {
|
|
213
|
+
const expression = bindSQLParameter(name, { parameters });
|
|
214
|
+
encodeStrings(expression);
|
|
215
|
+
const old = previous == null ? void 0 : previous.get(name);
|
|
216
|
+
entries.set(name, old && equalValue(old, expression) ? old : expression);
|
|
217
|
+
}
|
|
218
|
+
if (options.previous && (previous == null ? void 0 : previous.size) === entries.size && [...entries].every(([name, value]) => previous.get(name) === value))
|
|
219
|
+
return options.previous;
|
|
220
|
+
const snapshot = Object.freeze({ kind: "sql-parameter-snapshot" });
|
|
221
|
+
snapshots.set(snapshot, entries);
|
|
222
|
+
return snapshot;
|
|
223
|
+
}
|
|
224
|
+
function areSQLParametersEqual(a, b, options = {}) {
|
|
225
|
+
const left = storage(a);
|
|
226
|
+
const right = storage(b);
|
|
227
|
+
if (a === b)
|
|
228
|
+
return true;
|
|
229
|
+
if (!options.names && left.size !== right.size)
|
|
230
|
+
return false;
|
|
231
|
+
return (options.names ?? [...left.keys()]).every((name) => left.has(name) === right.has(name) && equalValue(left.get(name), right.get(name)));
|
|
232
|
+
}
|
|
233
|
+
function isSQLParameterSnapshot(value) {
|
|
234
|
+
return Boolean(value && typeof value === "object" && snapshots.has(value));
|
|
235
|
+
}
|
|
236
|
+
function snapshotParameter(snapshot, name) {
|
|
237
|
+
const expression = storage(snapshot).get(name);
|
|
238
|
+
if (!expression)
|
|
239
|
+
throw new Error(`SQL parameter ":${name}" requires a value`);
|
|
240
|
+
return structuredClone(expression);
|
|
241
|
+
}
|
|
242
|
+
function storage(snapshot) {
|
|
243
|
+
const values = snapshots.get(snapshot);
|
|
244
|
+
if (!values)
|
|
245
|
+
throw new Error("Invalid SQL parameter snapshot");
|
|
246
|
+
return values;
|
|
247
|
+
}
|
|
248
|
+
function equalValue(a, b) {
|
|
249
|
+
if (Object.is(a, b))
|
|
250
|
+
return true;
|
|
251
|
+
if (!a || !b || typeof a !== "object" || typeof b !== "object" || a.constructor !== b.constructor)
|
|
252
|
+
return false;
|
|
253
|
+
if (a instanceof Date && b instanceof Date)
|
|
254
|
+
return a.getTime() === b.getTime();
|
|
255
|
+
const left = Object.keys(a);
|
|
256
|
+
const right = Object.keys(b);
|
|
257
|
+
return left.length === right.length && left.every((key) => Object.prototype.hasOwnProperty.call(b, key) && equalValue(a[key], b[key]));
|
|
258
|
+
}
|
|
259
|
+
function getSQLParameterNames(sql) {
|
|
260
|
+
const names = /* @__PURE__ */ new Set();
|
|
261
|
+
const tokens = /'(?:''|[^'])*'|"(?:""|[^"])*"|--[^\r\n]*|\/\*[\s\S]*?\*\/|::|:([A-Za-z_][A-Za-z0-9_]*)/g;
|
|
262
|
+
for (const match of sql.matchAll(tokens))
|
|
263
|
+
if (match[1])
|
|
264
|
+
names.add(match[1]);
|
|
265
|
+
return Object.freeze([...names]);
|
|
266
|
+
}
|
|
267
|
+
function encodeStrings(expression) {
|
|
268
|
+
if (expression.type === "Literal" && typeof expression.value === "string" && !expression.utf8) {
|
|
269
|
+
expression.utf8 = new TextEncoder().encode(expression.value);
|
|
270
|
+
} else if (expression.type === "ArrayExpression") {
|
|
271
|
+
for (const element of expression.elements)
|
|
272
|
+
encodeStrings(element);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// dist/diagnostic-log.js
|
|
277
|
+
function reportDiagnosticError(log, error) {
|
|
278
|
+
if (log) {
|
|
279
|
+
try {
|
|
280
|
+
log.error(error instanceof Error ? error.message : String(error), error)();
|
|
281
|
+
} catch {
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// dist/parser/sql-parse-error.js
|
|
287
|
+
var SQLParseError = class extends SyntaxError {
|
|
288
|
+
constructor(reason, source, offset) {
|
|
289
|
+
var _a;
|
|
290
|
+
const lines = source.slice(0, offset).split(/\r\n|\r|\n/);
|
|
291
|
+
const line = lines.length;
|
|
292
|
+
const column = lines[line - 1].length + 1;
|
|
293
|
+
const token = ((_a = source.slice(offset, offset + 32).match(/^:?\w+|^\S/u)) == null ? void 0 : _a[0]) ?? null;
|
|
294
|
+
super(`${reason} (line ${line}, column ${column}${token ? `, near ${JSON.stringify(token)}` : ""})`);
|
|
295
|
+
this.reason = reason;
|
|
296
|
+
this.name = "SQLParseError";
|
|
297
|
+
this.offset = offset;
|
|
298
|
+
this.line = line;
|
|
299
|
+
this.column = column;
|
|
300
|
+
this.token = token;
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
// dist/parser/parse-sql-expression.js
|
|
305
|
+
var SQL_RESERVED_WORDS = /* @__PURE__ */ new Set([
|
|
306
|
+
"SELECT",
|
|
307
|
+
"ARRAY",
|
|
308
|
+
"FROM",
|
|
309
|
+
"WHERE",
|
|
310
|
+
"LIMIT",
|
|
311
|
+
"OFFSET",
|
|
312
|
+
"AS",
|
|
313
|
+
"TRUE",
|
|
314
|
+
"FALSE",
|
|
315
|
+
"NULL",
|
|
316
|
+
"ALL",
|
|
317
|
+
"AND",
|
|
318
|
+
"OR",
|
|
319
|
+
"NOT",
|
|
320
|
+
"IN",
|
|
321
|
+
"IS",
|
|
322
|
+
"DISTINCT",
|
|
323
|
+
"ORDER",
|
|
324
|
+
"GROUP",
|
|
325
|
+
"HAVING",
|
|
326
|
+
"JOIN",
|
|
327
|
+
"UNION"
|
|
328
|
+
]);
|
|
329
|
+
function sqlCastType(name, source = name, offset = 0) {
|
|
330
|
+
const normalized = name.toLowerCase().replace(/\s+/g, " ").trim();
|
|
331
|
+
if (/^(?:u?int(?:8|16|32|64)|float(?:16|32|64))$/.test(normalized)) {
|
|
332
|
+
return normalized;
|
|
333
|
+
}
|
|
334
|
+
switch (normalized) {
|
|
335
|
+
case "smallint":
|
|
336
|
+
return "int16";
|
|
337
|
+
case "int":
|
|
338
|
+
case "integer":
|
|
339
|
+
return "int32";
|
|
340
|
+
case "bigint":
|
|
341
|
+
return "int64";
|
|
342
|
+
case "real":
|
|
343
|
+
return "float32";
|
|
344
|
+
case "double precision":
|
|
345
|
+
return "float64";
|
|
346
|
+
case "boolean":
|
|
347
|
+
return "bool";
|
|
348
|
+
case "utf8":
|
|
349
|
+
case "varchar":
|
|
350
|
+
case "character varying":
|
|
351
|
+
return "utf8";
|
|
352
|
+
default:
|
|
353
|
+
throw new SQLParseError(`Unsupported SQL CAST type: ${name}`, source, offset);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function parseSQLExpression(source, options = {}) {
|
|
357
|
+
const reader = new SQLExpressionReader(source, 0, options);
|
|
358
|
+
const expression = reader.expression();
|
|
359
|
+
reader.space();
|
|
360
|
+
if (reader.position !== source.length)
|
|
361
|
+
throw reader.error("Unexpected token after SQL expression");
|
|
362
|
+
return expression;
|
|
363
|
+
}
|
|
364
|
+
var SQLExpressionReader = class {
|
|
365
|
+
constructor(source, start = 0, parameterOptions = {}) {
|
|
366
|
+
this.source = source;
|
|
367
|
+
this.parameterOptions = parameterOptions;
|
|
368
|
+
this.tokens = 0;
|
|
369
|
+
this.depths = /* @__PURE__ */ new WeakMap();
|
|
370
|
+
validateSQLParameterOptions(parameterOptions);
|
|
371
|
+
this.position = start;
|
|
372
|
+
if (start === 0 && (typeof source !== "string" || !source.trim() || source.length > 65536))
|
|
373
|
+
throw new SQLParseError("SQL expression requires non-empty input of at most 65536 characters", "", 0);
|
|
374
|
+
}
|
|
375
|
+
/** Creates an error at the current expression cursor. */
|
|
376
|
+
error(message, offset = this.position) {
|
|
377
|
+
return new SQLParseError(message, this.source, offset);
|
|
378
|
+
}
|
|
379
|
+
space() {
|
|
380
|
+
while (this.position < this.source.length && /\s/.test(this.source[this.position]))
|
|
381
|
+
this.position++;
|
|
382
|
+
if (this.source.startsWith("--", this.position) || this.source.startsWith("/*", this.position))
|
|
383
|
+
throw this.error("SQL comments are not supported.");
|
|
384
|
+
}
|
|
385
|
+
word(word) {
|
|
386
|
+
this.space();
|
|
387
|
+
if (this.source.slice(this.position, this.position + word.length).toUpperCase() !== word || /[A-Za-z0-9_]/.test(this.source[this.position + word.length] || ""))
|
|
388
|
+
return false;
|
|
389
|
+
this.position += word.length;
|
|
390
|
+
this.token();
|
|
391
|
+
return true;
|
|
392
|
+
}
|
|
393
|
+
take(symbol) {
|
|
394
|
+
this.space();
|
|
395
|
+
if (!this.source.startsWith(symbol, this.position))
|
|
396
|
+
return false;
|
|
397
|
+
this.position += symbol.length;
|
|
398
|
+
this.token();
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
symbol(symbol) {
|
|
402
|
+
if (!this.take(symbol))
|
|
403
|
+
throw this.error(`SQL expression expected ${symbol}`);
|
|
404
|
+
}
|
|
405
|
+
node(value, children = []) {
|
|
406
|
+
const depth = children.length ? 1 + Math.max(...children.map((child) => this.depths.get(child) || 0)) : 0;
|
|
407
|
+
if (depth > 64)
|
|
408
|
+
throw this.error("SQL expression exceeds nesting limit");
|
|
409
|
+
this.depths.set(value, depth);
|
|
410
|
+
return value;
|
|
411
|
+
}
|
|
412
|
+
expression(depth = 0, minimum = 0) {
|
|
413
|
+
if (depth > 64)
|
|
414
|
+
throw this.error("SQL expression exceeds nesting limit");
|
|
415
|
+
let left;
|
|
416
|
+
if (this.word("NOT")) {
|
|
417
|
+
const argument = this.expression(depth + 1, 3);
|
|
418
|
+
left = this.node({ type: "UnaryExpression", operator: "!", argument }, [argument]);
|
|
419
|
+
} else if (/^\s*[+-](?:\d|\.\d)/.test(this.source.slice(this.position))) {
|
|
420
|
+
left = this.primary(depth);
|
|
421
|
+
} else if (this.take("+") || this.take("-")) {
|
|
422
|
+
const operator = this.source[this.position - 1];
|
|
423
|
+
const argument = this.expression(depth + 1, 6);
|
|
424
|
+
left = this.node({ type: "UnaryExpression", operator, argument }, [argument]);
|
|
425
|
+
} else
|
|
426
|
+
left = this.primary(depth);
|
|
427
|
+
while (true) {
|
|
428
|
+
const start = this.position;
|
|
429
|
+
if (this.take("[")) {
|
|
430
|
+
const index = this.expression(depth + 1);
|
|
431
|
+
this.symbol("]");
|
|
432
|
+
left = this.call("LIST_EXTRACT", [left, index], true);
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
if (this.take(".")) {
|
|
436
|
+
left = this.memberAccess(left, this.readIdentifier(true));
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (this.take("::")) {
|
|
440
|
+
if (minimum > 7) {
|
|
441
|
+
this.position = start;
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
left = this.cast(left);
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
if (minimum <= 3 && this.word("IS")) {
|
|
448
|
+
const negate = this.word("NOT");
|
|
449
|
+
if (!this.word("NULL"))
|
|
450
|
+
throw this.error("SQL IS requires NULL or NOT NULL");
|
|
451
|
+
left = this.call(negate ? "isNotNull" : "isNull", [left]);
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
if (minimum <= 3) {
|
|
455
|
+
const negate = this.word("NOT");
|
|
456
|
+
if (this.word("IN")) {
|
|
457
|
+
this.symbol("(");
|
|
458
|
+
const values = [];
|
|
459
|
+
do {
|
|
460
|
+
const item = this.expression(depth + 1);
|
|
461
|
+
if (item.type === "ArrayExpression") {
|
|
462
|
+
for (const element of item.elements) {
|
|
463
|
+
if (element.type !== "Literal")
|
|
464
|
+
throw this.error("SQL IN requires scalar values");
|
|
465
|
+
values.push(element);
|
|
466
|
+
}
|
|
467
|
+
} else
|
|
468
|
+
values.push(item);
|
|
469
|
+
if (values.length > 4096)
|
|
470
|
+
throw this.error("SQL IN exceeds 4096 values");
|
|
471
|
+
} while (this.take(","));
|
|
472
|
+
this.symbol(")");
|
|
473
|
+
left = values.length ? this.call("in", [left, ...values]) : this.node({ type: "Literal", value: false, raw: "false" });
|
|
474
|
+
if (negate)
|
|
475
|
+
left = this.node({ type: "UnaryExpression", operator: "!", argument: left }, [left]);
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
const insensitive = this.word("ILIKE");
|
|
479
|
+
if (insensitive || this.word("LIKE")) {
|
|
480
|
+
const pattern = this.expression(depth + 1, 4);
|
|
481
|
+
const args = [left, pattern];
|
|
482
|
+
if (this.word("ESCAPE"))
|
|
483
|
+
args.push(this.expression(depth + 1, 4));
|
|
484
|
+
left = this.call(insensitive ? "ilike" : "like", args);
|
|
485
|
+
if (negate)
|
|
486
|
+
left = this.node({ type: "UnaryExpression", operator: "!", argument: left }, [left]);
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
this.position = start;
|
|
490
|
+
}
|
|
491
|
+
this.space();
|
|
492
|
+
const match = /^(OR\b|AND\b|\|\||<>|!=|<=|>=|=|<|>|\+|-|\*|\/)/i.exec(this.source.slice(this.position));
|
|
493
|
+
if (!match)
|
|
494
|
+
break;
|
|
495
|
+
const token = match[0].toUpperCase();
|
|
496
|
+
const precedence = token === "OR" ? 1 : token === "AND" ? 2 : ["=", "<>", "!=", "<", ">", "<=", ">="].includes(token) ? 3 : token === "+" || token === "-" || token === "||" ? 4 : 5;
|
|
497
|
+
if (precedence < minimum)
|
|
498
|
+
break;
|
|
499
|
+
this.position += match[0].length;
|
|
500
|
+
this.token();
|
|
501
|
+
const right = this.expression(depth + 1, precedence + 1);
|
|
502
|
+
if (token === "||") {
|
|
503
|
+
left = this.call("concatStrict", [left, right]);
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
const operator = token === "OR" ? "||" : token === "AND" ? "&&" : token === "=" ? "==" : token === "<>" ? "!=" : token;
|
|
507
|
+
left = this.node({ type: "BinaryExpression", operator, left, right }, [left, right]);
|
|
508
|
+
}
|
|
509
|
+
return left;
|
|
510
|
+
}
|
|
511
|
+
call(name, args, sqlFunction = false) {
|
|
512
|
+
return this.node({
|
|
513
|
+
type: "CallExpression",
|
|
514
|
+
callee: { type: "Identifier", name },
|
|
515
|
+
arguments: args,
|
|
516
|
+
...sqlFunction ? { sqlFunction: true } : {}
|
|
517
|
+
}, args);
|
|
518
|
+
}
|
|
519
|
+
cast(argument) {
|
|
520
|
+
this.space();
|
|
521
|
+
const start = this.position;
|
|
522
|
+
const match = /^[A-Za-z][A-Za-z0-9]*(?:\s+(?:PRECISION|VARYING)\b)?/i.exec(this.source.slice(this.position));
|
|
523
|
+
if (!match)
|
|
524
|
+
throw this.error("SQL cast requires a target type");
|
|
525
|
+
this.position += match[0].length;
|
|
526
|
+
this.token();
|
|
527
|
+
if (this.take("[")) {
|
|
528
|
+
this.space();
|
|
529
|
+
const width = /^\d+/.exec(this.source.slice(this.position));
|
|
530
|
+
if (!width)
|
|
531
|
+
throw this.error("SQL ARRAY cast requires a fixed length");
|
|
532
|
+
this.position += width[0].length;
|
|
533
|
+
this.token();
|
|
534
|
+
this.symbol("]");
|
|
535
|
+
const array = argument.type === "ArrayExpression" ? { type: "CallExpression", callee: { name: "array" }, arguments: argument.elements } : argument;
|
|
536
|
+
if (array.type !== "CallExpression" || !/^array(?:_(?:float(?:16|32|64)|uint8))?$/.test(array.callee.name) || Number(width[0]) !== array.arguments.length)
|
|
537
|
+
throw this.error("ARRAY cast length must match the number of components");
|
|
538
|
+
const type = sqlCastType(match[0], this.source, start);
|
|
539
|
+
if (!["float16", "float32", "float64", "uint8"].includes(type))
|
|
540
|
+
throw this.error("Unsupported ARRAY cast type");
|
|
541
|
+
if (array.callee.name !== "array" && (type === "uint8" || array.callee.name === "array_uint8"))
|
|
542
|
+
throw this.error("Chained UINT8 array casts are not supported");
|
|
543
|
+
return this.call(`array_${type}`, array.arguments);
|
|
544
|
+
}
|
|
545
|
+
const name = match[0].toLowerCase();
|
|
546
|
+
let target;
|
|
547
|
+
if (name === "timestamp")
|
|
548
|
+
target = this.timestampCastType();
|
|
549
|
+
else if (name === "date" || name === "geometry")
|
|
550
|
+
target = name;
|
|
551
|
+
else if (name === "dictionary")
|
|
552
|
+
target = this.dictionaryCastType();
|
|
553
|
+
else if (/^(?:binary|varbinary|blob|fixedsizebinary)$/.test(name))
|
|
554
|
+
target = this.binaryCastType(name);
|
|
555
|
+
else
|
|
556
|
+
target = sqlCastType(name, this.source, start);
|
|
557
|
+
return this.call("cast", [
|
|
558
|
+
argument,
|
|
559
|
+
this.node({ type: "Literal", value: target, raw: JSON.stringify(target) })
|
|
560
|
+
]);
|
|
561
|
+
}
|
|
562
|
+
/** Parses the optional SQL time zone qualifier. */
|
|
563
|
+
timestampCastType() {
|
|
564
|
+
const zoned = this.word("WITH");
|
|
565
|
+
if (!zoned && !this.word("WITHOUT"))
|
|
566
|
+
return "timestamp";
|
|
567
|
+
if (!this.word("TIME") || !this.word("ZONE"))
|
|
568
|
+
throw this.error("Expected TIME ZONE");
|
|
569
|
+
return zoned ? "timestamp_utc" : "timestamp";
|
|
570
|
+
}
|
|
571
|
+
/** Parses the supported Arrow dictionary layout. */
|
|
572
|
+
dictionaryCastType() {
|
|
573
|
+
this.symbol("(");
|
|
574
|
+
if (!this.word("UTF8"))
|
|
575
|
+
throw this.error("Expected UTF8 dictionary values");
|
|
576
|
+
this.symbol(",");
|
|
577
|
+
if (!this.word("INT32"))
|
|
578
|
+
throw this.error("Expected INT32 dictionary indices");
|
|
579
|
+
this.symbol(")");
|
|
580
|
+
return "dictionary_utf8_int32";
|
|
581
|
+
}
|
|
582
|
+
/** Parses binary aliases and optional positive byte widths. */
|
|
583
|
+
binaryCastType(name) {
|
|
584
|
+
var _a;
|
|
585
|
+
if (name === "blob")
|
|
586
|
+
return "binary";
|
|
587
|
+
if (!this.take("(")) {
|
|
588
|
+
if (name === "fixedsizebinary")
|
|
589
|
+
throw this.error("FIXEDSIZEBINARY requires a byte width");
|
|
590
|
+
return "binary";
|
|
591
|
+
}
|
|
592
|
+
this.space();
|
|
593
|
+
const width = ((_a = /^\d+/.exec(this.source.slice(this.position))) == null ? void 0 : _a[0]) ?? "";
|
|
594
|
+
const byteWidth = Number(width);
|
|
595
|
+
if (byteWidth < 1 || byteWidth > 2147483647)
|
|
596
|
+
throw this.error("Binary CAST requires a positive 32-bit byte width");
|
|
597
|
+
this.position += width.length;
|
|
598
|
+
this.token();
|
|
599
|
+
this.symbol(")");
|
|
600
|
+
return `${name === "varbinary" ? "varbinary" : "fixedsizebinary"}:${byteWidth}`;
|
|
601
|
+
}
|
|
602
|
+
primary(depth) {
|
|
603
|
+
if (depth > 64)
|
|
604
|
+
throw this.error("SQL expression exceeds nesting limit");
|
|
605
|
+
if (this.take("(")) {
|
|
606
|
+
const value = this.expression(depth + 1);
|
|
607
|
+
this.symbol(")");
|
|
608
|
+
return value;
|
|
609
|
+
}
|
|
610
|
+
if (this.word("CAST")) {
|
|
611
|
+
this.symbol("(");
|
|
612
|
+
const argument = this.expression(depth + 1);
|
|
613
|
+
if (!this.word("AS"))
|
|
614
|
+
throw this.error("SQL CAST requires AS");
|
|
615
|
+
const result = this.cast(argument);
|
|
616
|
+
this.symbol(")");
|
|
617
|
+
return result;
|
|
618
|
+
}
|
|
619
|
+
if (this.word("ARRAY")) {
|
|
620
|
+
this.symbol("[");
|
|
621
|
+
const args = [];
|
|
622
|
+
do {
|
|
623
|
+
args.push(this.expression(depth + 1));
|
|
624
|
+
} while (this.take(","));
|
|
625
|
+
this.symbol("]");
|
|
626
|
+
return this.call("array", args);
|
|
627
|
+
}
|
|
628
|
+
if (this.word("CASE")) {
|
|
629
|
+
const clauses = [];
|
|
630
|
+
while (this.word("WHEN")) {
|
|
631
|
+
const test = this.expression(depth + 1);
|
|
632
|
+
if (!this.word("THEN"))
|
|
633
|
+
throw this.error("SQL CASE requires THEN");
|
|
634
|
+
clauses.push({ test, consequent: this.expression(depth + 1) });
|
|
635
|
+
}
|
|
636
|
+
if (!clauses.length)
|
|
637
|
+
throw this.error("SQL CASE requires WHEN");
|
|
638
|
+
let alternate = this.word("ELSE") ? this.expression(depth + 1) : this.node({ type: "Literal", value: null, raw: "null" });
|
|
639
|
+
if (!this.word("END"))
|
|
640
|
+
throw this.error("SQL CASE requires END");
|
|
641
|
+
for (let index = clauses.length - 1; index >= 0; index--) {
|
|
642
|
+
const { test, consequent } = clauses[index];
|
|
643
|
+
alternate = this.node({ type: "ConditionalExpression", test, consequent, alternate }, [
|
|
644
|
+
test,
|
|
645
|
+
consequent,
|
|
646
|
+
alternate
|
|
647
|
+
]);
|
|
648
|
+
}
|
|
649
|
+
return alternate;
|
|
650
|
+
}
|
|
651
|
+
this.space();
|
|
652
|
+
const start = this.position;
|
|
653
|
+
const extended = this.extendedPrimary();
|
|
654
|
+
if (extended)
|
|
655
|
+
return extended;
|
|
656
|
+
const quote = this.source[this.position];
|
|
657
|
+
if (quote === '"' || quote === "'") {
|
|
658
|
+
const value = this.quoted(quote);
|
|
659
|
+
return quote === '"' ? this.identifier(value, depth, false) : this.node({ type: "Literal", value, raw: JSON.stringify(value) });
|
|
660
|
+
}
|
|
661
|
+
const number = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/.exec(this.source.slice(this.position));
|
|
662
|
+
if (number) {
|
|
663
|
+
this.position += number[0].length;
|
|
664
|
+
this.token();
|
|
665
|
+
const value = Number(number[0]);
|
|
666
|
+
if (!Number.isFinite(value) || /^[+-]?\d+$/.test(number[0]) && !Number.isSafeInteger(value))
|
|
667
|
+
throw this.error("SQL numeric literal must be finite and exactly representable; use a quoted BIGINT value", start);
|
|
668
|
+
return this.node({ type: "Literal", value, raw: number[0] });
|
|
669
|
+
}
|
|
670
|
+
for (const [keyword, value] of [
|
|
671
|
+
["NULL", null],
|
|
672
|
+
["TRUE", true],
|
|
673
|
+
["FALSE", false]
|
|
674
|
+
])
|
|
675
|
+
if (this.word(keyword))
|
|
676
|
+
return this.node({ type: "Literal", value, raw: keyword.toLowerCase() });
|
|
677
|
+
return this.identifier(this.readIdentifier(), depth, true);
|
|
678
|
+
}
|
|
679
|
+
/** Reads an identifier in projections, source references, and field access. */
|
|
680
|
+
readIdentifier(member = false) {
|
|
681
|
+
var _a;
|
|
682
|
+
this.space();
|
|
683
|
+
if (this.source[this.position] === '"')
|
|
684
|
+
return this.quoted('"');
|
|
685
|
+
const identifier = (_a = /^[A-Za-z_][A-Za-z0-9_]*/.exec(this.source.slice(this.position))) == null ? void 0 : _a[0];
|
|
686
|
+
if (!identifier || !member && (SQL_RESERVED_WORDS.has(identifier.toUpperCase()) || /^(WHEN|THEN|ELSE|END)$/i.test(identifier)))
|
|
687
|
+
throw this.error("SQL expression requires a column, literal or function");
|
|
688
|
+
this.position += identifier.length;
|
|
689
|
+
this.token();
|
|
690
|
+
return identifier;
|
|
691
|
+
}
|
|
692
|
+
/** Decodes a quoted token, including doubled quote escapes. */
|
|
693
|
+
quoted(quote) {
|
|
694
|
+
const start = this.position++;
|
|
695
|
+
this.token();
|
|
696
|
+
let value = "";
|
|
697
|
+
while (this.position < this.source.length) {
|
|
698
|
+
const character = this.source[this.position++];
|
|
699
|
+
if (character !== quote)
|
|
700
|
+
value += character;
|
|
701
|
+
else if (this.source[this.position] === quote) {
|
|
702
|
+
value += quote;
|
|
703
|
+
this.position++;
|
|
704
|
+
} else {
|
|
705
|
+
if (quote === '"' && !value)
|
|
706
|
+
throw this.error("SQL identifiers must not be empty", start);
|
|
707
|
+
return value;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
throw this.error("Unterminated SQL expression quote", start);
|
|
711
|
+
}
|
|
712
|
+
identifier(name, depth, fold) {
|
|
713
|
+
if (!this.take("("))
|
|
714
|
+
return this.node({ type: "Identifier", name });
|
|
715
|
+
const args = [];
|
|
716
|
+
const wildcard = fold && this.wildcardArgument(name);
|
|
717
|
+
if (wildcard) {
|
|
718
|
+
this.symbol(")");
|
|
719
|
+
return this.call(name, [wildcard], true);
|
|
720
|
+
}
|
|
721
|
+
if (!this.take(")")) {
|
|
722
|
+
do {
|
|
723
|
+
args.push(this.expression(depth + 1));
|
|
724
|
+
} while (this.take(","));
|
|
725
|
+
this.symbol(")");
|
|
726
|
+
}
|
|
727
|
+
return this.call(name, args, fold);
|
|
728
|
+
}
|
|
729
|
+
/** Reads named value placeholders; quotes and identifiers use separate productions. */
|
|
730
|
+
extendedPrimary() {
|
|
731
|
+
if (this.source[this.position] !== ":")
|
|
732
|
+
return void 0;
|
|
733
|
+
const start = this.position;
|
|
734
|
+
const parameter = /^:([A-Za-z_][A-Za-z0-9_]*)/.exec(this.source.slice(start));
|
|
735
|
+
if (!parameter)
|
|
736
|
+
throw this.error("SQL named parameters require a name");
|
|
737
|
+
this.position += parameter[0].length;
|
|
738
|
+
this.token();
|
|
739
|
+
try {
|
|
740
|
+
return this.node(bindSQLParameter(parameter[1], this.parameterOptions));
|
|
741
|
+
} catch (error) {
|
|
742
|
+
throw this.error(error instanceof Error ? error.message : String(error), start);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
/** Lowers field access to a portable expression call for Arrow binding. */
|
|
746
|
+
memberAccess(object, name) {
|
|
747
|
+
const key = this.node({ type: "Literal", value: name, raw: JSON.stringify(name) });
|
|
748
|
+
return this.call("STRUCT_EXTRACT", [object, key], true);
|
|
749
|
+
}
|
|
750
|
+
wildcardArgument(_name) {
|
|
751
|
+
return void 0;
|
|
752
|
+
}
|
|
753
|
+
token() {
|
|
754
|
+
if (++this.tokens > 4096)
|
|
755
|
+
throw this.error("SQL expression exceeds token limit");
|
|
756
|
+
}
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
// dist/parser/sql-predicate-types.js
|
|
760
|
+
function isSQLPredicateParameter(value) {
|
|
761
|
+
return isColumnarPredicateParameter(value);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// dist/parser/sql-predicate-schema.js
|
|
765
|
+
var MAXIMUM_PREDICATE_DEPTH = 64;
|
|
766
|
+
var MAXIMUM_PREDICATE_NODES = 4096;
|
|
767
|
+
var SQL_PREDICATE_JSON_SCHEMA = {
|
|
768
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
769
|
+
$id: "https://math.gl/schemas/sql-predicate.schema.json",
|
|
770
|
+
title: "math.gl SQL predicate",
|
|
771
|
+
oneOf: [
|
|
772
|
+
{ $ref: "#/$defs/comparison" },
|
|
773
|
+
{ $ref: "#/$defs/in" },
|
|
774
|
+
{ $ref: "#/$defs/isNull" },
|
|
775
|
+
{ $ref: "#/$defs/logical" },
|
|
776
|
+
{ $ref: "#/$defs/not" }
|
|
777
|
+
],
|
|
778
|
+
$defs: {
|
|
779
|
+
property: {
|
|
780
|
+
type: "object",
|
|
781
|
+
required: ["property"],
|
|
782
|
+
properties: {
|
|
783
|
+
property: { type: "string", minLength: 1 },
|
|
784
|
+
quoted: { type: "boolean" },
|
|
785
|
+
casts: {
|
|
786
|
+
type: "array",
|
|
787
|
+
minItems: 1,
|
|
788
|
+
maxItems: 64,
|
|
789
|
+
items: { enum: COLUMNAR_SCALAR_TYPES }
|
|
790
|
+
}
|
|
791
|
+
},
|
|
792
|
+
additionalProperties: false
|
|
793
|
+
},
|
|
794
|
+
value: {
|
|
795
|
+
oneOf: [
|
|
796
|
+
{ type: ["boolean", "number", "string"] },
|
|
797
|
+
{
|
|
798
|
+
type: "object",
|
|
799
|
+
required: ["parameter"],
|
|
800
|
+
properties: { parameter: { type: "string", minLength: 1 } },
|
|
801
|
+
additionalProperties: false
|
|
802
|
+
}
|
|
803
|
+
]
|
|
804
|
+
},
|
|
805
|
+
comparison: {
|
|
806
|
+
type: "object",
|
|
807
|
+
required: ["op", "args"],
|
|
808
|
+
properties: {
|
|
809
|
+
op: { enum: ["=", "<>", "<", "<=", ">", ">="] },
|
|
810
|
+
args: {
|
|
811
|
+
type: "array",
|
|
812
|
+
prefixItems: [{ $ref: "#/$defs/property" }, { $ref: "#/$defs/value" }],
|
|
813
|
+
minItems: 2,
|
|
814
|
+
maxItems: 2
|
|
815
|
+
}
|
|
816
|
+
},
|
|
817
|
+
additionalProperties: false
|
|
818
|
+
},
|
|
819
|
+
in: {
|
|
820
|
+
type: "object",
|
|
821
|
+
required: ["op", "args"],
|
|
822
|
+
properties: {
|
|
823
|
+
op: { const: "in" },
|
|
824
|
+
args: {
|
|
825
|
+
type: "array",
|
|
826
|
+
prefixItems: [
|
|
827
|
+
{ $ref: "#/$defs/property" },
|
|
828
|
+
{ type: "array", items: { $ref: "#/$defs/value" }, minItems: 1 }
|
|
829
|
+
],
|
|
830
|
+
minItems: 2,
|
|
831
|
+
maxItems: 2
|
|
832
|
+
}
|
|
833
|
+
},
|
|
834
|
+
additionalProperties: false
|
|
835
|
+
},
|
|
836
|
+
isNull: {
|
|
837
|
+
type: "object",
|
|
838
|
+
required: ["op", "args"],
|
|
839
|
+
properties: {
|
|
840
|
+
op: { const: "isNull" },
|
|
841
|
+
args: {
|
|
842
|
+
type: "array",
|
|
843
|
+
prefixItems: [{ $ref: "#/$defs/property" }],
|
|
844
|
+
minItems: 1,
|
|
845
|
+
maxItems: 1
|
|
846
|
+
}
|
|
847
|
+
},
|
|
848
|
+
additionalProperties: false
|
|
849
|
+
},
|
|
850
|
+
logical: {
|
|
851
|
+
type: "object",
|
|
852
|
+
required: ["op", "args"],
|
|
853
|
+
properties: {
|
|
854
|
+
op: { enum: ["and", "or"] },
|
|
855
|
+
args: { type: "array", items: { $ref: "#" }, minItems: 2 }
|
|
856
|
+
},
|
|
857
|
+
additionalProperties: false
|
|
858
|
+
},
|
|
859
|
+
not: {
|
|
860
|
+
type: "object",
|
|
861
|
+
required: ["op", "args"],
|
|
862
|
+
properties: {
|
|
863
|
+
op: { const: "not" },
|
|
864
|
+
args: {
|
|
865
|
+
type: "array",
|
|
866
|
+
prefixItems: [{ $ref: "#" }],
|
|
867
|
+
minItems: 1,
|
|
868
|
+
maxItems: 1
|
|
869
|
+
}
|
|
870
|
+
},
|
|
871
|
+
additionalProperties: false
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
};
|
|
875
|
+
function isSQLPredicate(value) {
|
|
876
|
+
try {
|
|
877
|
+
validateSQLPredicate(value);
|
|
878
|
+
return true;
|
|
879
|
+
} catch {
|
|
880
|
+
return false;
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
function validateSQLPredicate(value) {
|
|
884
|
+
const state = { nodes: 0 };
|
|
885
|
+
validatePredicateNode(value, "$", 0, state);
|
|
886
|
+
}
|
|
887
|
+
function validatePredicateNode(value, path, depth, state) {
|
|
888
|
+
if (depth > MAXIMUM_PREDICATE_DEPTH) {
|
|
889
|
+
throw new Error(`SQL predicate ${path} exceeds the maximum nesting depth`);
|
|
890
|
+
}
|
|
891
|
+
state.nodes++;
|
|
892
|
+
if (state.nodes > MAXIMUM_PREDICATE_NODES) {
|
|
893
|
+
throw new Error("SQL predicate exceeds the maximum node count");
|
|
894
|
+
}
|
|
895
|
+
if (!isRecord(value) || !hasExactKeys(value, ["op", "args"])) {
|
|
896
|
+
throw new Error(`SQL predicate ${path} must contain only op and args`);
|
|
897
|
+
}
|
|
898
|
+
if (typeof value["op"] !== "string" || !Array.isArray(value["args"])) {
|
|
899
|
+
throw new Error(`SQL predicate ${path} requires string op and array args`);
|
|
900
|
+
}
|
|
901
|
+
switch (value["op"]) {
|
|
902
|
+
case "and":
|
|
903
|
+
case "or":
|
|
904
|
+
if (value["args"].length < 2) {
|
|
905
|
+
throw new Error(`SQL predicate ${path}.${value["op"]} requires at least two arguments`);
|
|
906
|
+
}
|
|
907
|
+
for (let index = 0; index < value["args"].length; index++) {
|
|
908
|
+
validatePredicateNode(value["args"][index], `${path}.args[${index}]`, depth + 1, state);
|
|
909
|
+
}
|
|
910
|
+
return;
|
|
911
|
+
case "not":
|
|
912
|
+
if (value["args"].length !== 1) {
|
|
913
|
+
throw new Error(`SQL predicate ${path}.not requires exactly one argument`);
|
|
914
|
+
}
|
|
915
|
+
validatePredicateNode(value["args"][0], `${path}.args[0]`, depth + 1, state);
|
|
916
|
+
return;
|
|
917
|
+
case "isNull":
|
|
918
|
+
if (value["args"].length !== 1) {
|
|
919
|
+
throw new Error(`SQL predicate ${path}.isNull requires exactly one argument`);
|
|
920
|
+
}
|
|
921
|
+
validatePredicateProperty(value["args"][0], `${path}.args[0]`);
|
|
922
|
+
return;
|
|
923
|
+
case "in":
|
|
924
|
+
if (value["args"].length !== 2 || !Array.isArray(value["args"][1]) || value["args"][1].length === 0) {
|
|
925
|
+
throw new Error(`SQL predicate ${path}.in requires a property and non-empty value list`);
|
|
926
|
+
}
|
|
927
|
+
validatePredicateProperty(value["args"][0], `${path}.args[0]`);
|
|
928
|
+
for (let index = 0; index < value["args"][1].length; index++) {
|
|
929
|
+
validatePredicateValue(value["args"][1][index], `${path}.args[1][${index}]`);
|
|
930
|
+
}
|
|
931
|
+
return;
|
|
932
|
+
case "=":
|
|
933
|
+
case "<>":
|
|
934
|
+
case "<":
|
|
935
|
+
case "<=":
|
|
936
|
+
case ">":
|
|
937
|
+
case ">=":
|
|
938
|
+
if (value["args"].length !== 2) {
|
|
939
|
+
throw new Error(`SQL predicate ${path}.${value["op"]} requires exactly two arguments`);
|
|
940
|
+
}
|
|
941
|
+
validatePredicateProperty(value["args"][0], `${path}.args[0]`);
|
|
942
|
+
validatePredicateValue(value["args"][1], `${path}.args[1]`);
|
|
943
|
+
return;
|
|
944
|
+
default:
|
|
945
|
+
throw new Error(`SQL predicate ${path} has unsupported operator ${JSON.stringify(value["op"])}`);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
function validatePredicateProperty(value, path) {
|
|
949
|
+
if (!isRecord(value) || !hasAllowedKeys(value, ["property", "quoted", "casts"]) || typeof value["property"] !== "string" || value["property"].length === 0 || value["quoted"] !== void 0 && typeof value["quoted"] !== "boolean") {
|
|
950
|
+
throw new Error(`SQL predicate ${path} requires a non-empty property reference`);
|
|
951
|
+
}
|
|
952
|
+
const casts = value["casts"];
|
|
953
|
+
if (casts !== void 0 && (!Array.isArray(casts) || !casts.length || casts.length > 64 || Array.from(casts).some((type) => !isColumnarScalarType(type))))
|
|
954
|
+
throw new Error(`SQL predicate ${path} has invalid casts`);
|
|
955
|
+
}
|
|
956
|
+
function validatePredicateValue(value, path) {
|
|
957
|
+
if (isSQLPredicateParameter(value)) {
|
|
958
|
+
return;
|
|
959
|
+
}
|
|
960
|
+
if (typeof value === "boolean" || typeof value === "bigint" || typeof value === "string" || value instanceof Uint8Array) {
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
if (value instanceof Date && Number.isFinite(value.getTime())) {
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
throw new Error(`SQL predicate ${path} contains an unsupported scalar value`);
|
|
970
|
+
}
|
|
971
|
+
function isRecord(value) {
|
|
972
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
973
|
+
}
|
|
974
|
+
function hasExactKeys(value, expectedKeys) {
|
|
975
|
+
const keys = Object.keys(value);
|
|
976
|
+
return keys.length === expectedKeys.length && expectedKeys.every((key) => keys.includes(key));
|
|
977
|
+
}
|
|
978
|
+
function hasAllowedKeys(value, allowedKeys) {
|
|
979
|
+
return Object.keys(value).every((key) => allowedKeys.includes(key));
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
// dist/parser/parse-sql-predicate.js
|
|
983
|
+
var MAXIMUM_SQL_LENGTH = 65536;
|
|
984
|
+
var MAXIMUM_SQL_TOKENS = 4096;
|
|
985
|
+
var MAXIMUM_EXPRESSION_DEPTH = 64;
|
|
986
|
+
function parseSQLPredicate(source, options = {}) {
|
|
987
|
+
const parser = new SQLPredicateParser(source, options.parameters ?? {}, options.preserveParameters === true);
|
|
988
|
+
const predicate = parser.parse();
|
|
989
|
+
validateSQLPredicate(predicate);
|
|
990
|
+
return predicate;
|
|
991
|
+
}
|
|
992
|
+
var SQLPredicateParser = class {
|
|
993
|
+
constructor(source, parameters, preserveParameters) {
|
|
994
|
+
this.source = source;
|
|
995
|
+
this.position = 0;
|
|
996
|
+
this.tokens = tokenizeSQLPredicate(source);
|
|
997
|
+
this.parameters = parameters;
|
|
998
|
+
this.preserveParameters = preserveParameters;
|
|
999
|
+
}
|
|
1000
|
+
/** Reports the current token, or a token already consumed by the production. */
|
|
1001
|
+
error(reason, token = this.peek()) {
|
|
1002
|
+
return new SQLParseError(reason, this.source, (token == null ? void 0 : token.offset) ?? this.source.length);
|
|
1003
|
+
}
|
|
1004
|
+
/** Parses the complete token stream. */
|
|
1005
|
+
parse() {
|
|
1006
|
+
const predicate = this.parseOr(0);
|
|
1007
|
+
this.takeSymbol(";");
|
|
1008
|
+
const token = this.peek();
|
|
1009
|
+
if (token) {
|
|
1010
|
+
throw this.error(`SQL predicate contains unexpected token ${JSON.stringify(token.value)}`);
|
|
1011
|
+
}
|
|
1012
|
+
return predicate;
|
|
1013
|
+
}
|
|
1014
|
+
/** Parses left-associative disjunction. */
|
|
1015
|
+
parseOr(depth) {
|
|
1016
|
+
let predicate = this.parseAnd(depth);
|
|
1017
|
+
while (this.takeKeyword("OR")) {
|
|
1018
|
+
predicate = combineLogicalPredicate("or", predicate, this.parseAnd(depth));
|
|
1019
|
+
}
|
|
1020
|
+
return predicate;
|
|
1021
|
+
}
|
|
1022
|
+
/** Parses left-associative conjunction. */
|
|
1023
|
+
parseAnd(depth) {
|
|
1024
|
+
let predicate = this.parseNot(depth);
|
|
1025
|
+
while (this.takeKeyword("AND")) {
|
|
1026
|
+
predicate = combineLogicalPredicate("and", predicate, this.parseNot(depth));
|
|
1027
|
+
}
|
|
1028
|
+
return predicate;
|
|
1029
|
+
}
|
|
1030
|
+
/** Parses boolean negation and parenthesized predicates. */
|
|
1031
|
+
parseNot(depth) {
|
|
1032
|
+
this.assertDepth(depth);
|
|
1033
|
+
if (this.takeKeyword("NOT")) {
|
|
1034
|
+
return { op: "not", args: [this.parseNot(depth + 1)] };
|
|
1035
|
+
}
|
|
1036
|
+
if (this.takeSymbol("(")) {
|
|
1037
|
+
const predicate = this.parseOr(depth + 1);
|
|
1038
|
+
this.expectSymbol(")");
|
|
1039
|
+
return predicate;
|
|
1040
|
+
}
|
|
1041
|
+
return this.parseLeaf(depth);
|
|
1042
|
+
}
|
|
1043
|
+
/** Parses one comparison, membership, or null predicate. */
|
|
1044
|
+
parseLeaf(depth) {
|
|
1045
|
+
var _a, _b;
|
|
1046
|
+
const property = this.parseProperty(depth);
|
|
1047
|
+
if (this.takeKeyword("IS")) {
|
|
1048
|
+
const negated = this.takeKeyword("NOT");
|
|
1049
|
+
this.expectKeyword("NULL");
|
|
1050
|
+
const predicate = { op: "isNull", args: [property] };
|
|
1051
|
+
return negated ? { op: "not", args: [predicate] } : predicate;
|
|
1052
|
+
}
|
|
1053
|
+
const negatedMembership = this.takeKeyword("NOT");
|
|
1054
|
+
if (negatedMembership || this.peekKeyword("IN")) {
|
|
1055
|
+
this.expectKeyword("IN");
|
|
1056
|
+
this.expectSymbol("(");
|
|
1057
|
+
if (((_a = this.peek()) == null ? void 0 : _a.kind) === "symbol" && ((_b = this.peek()) == null ? void 0 : _b.value) === ")") {
|
|
1058
|
+
throw this.error("SQL predicate IN requires at least one scalar value");
|
|
1059
|
+
}
|
|
1060
|
+
const values = [this.parseValue()];
|
|
1061
|
+
while (this.takeSymbol(",")) {
|
|
1062
|
+
values.push(this.parseValue());
|
|
1063
|
+
}
|
|
1064
|
+
this.expectSymbol(")");
|
|
1065
|
+
const predicate = { op: "in", args: [property, values] };
|
|
1066
|
+
return negatedMembership ? { op: "not", args: [predicate] } : predicate;
|
|
1067
|
+
}
|
|
1068
|
+
const token = this.take();
|
|
1069
|
+
if ((token == null ? void 0 : token.kind) !== "symbol" || !["=", "!=", "<>", "<", "<=", ">", ">="].includes(token.value)) {
|
|
1070
|
+
throw this.error("SQL predicate expected a comparison, IN, or IS NULL operator", token);
|
|
1071
|
+
}
|
|
1072
|
+
const operator = token.value === "!=" ? "<>" : token.value;
|
|
1073
|
+
return {
|
|
1074
|
+
op: operator,
|
|
1075
|
+
args: [property, this.parseValue()]
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
/** Parses an identifier into a property reference. */
|
|
1079
|
+
parseProperty(depth) {
|
|
1080
|
+
let property = this.parsePropertyPrimary(depth);
|
|
1081
|
+
while (this.takeSymbol("::")) {
|
|
1082
|
+
const token = this.take();
|
|
1083
|
+
if ((token == null ? void 0 : token.kind) !== "identifier" || token.quoted)
|
|
1084
|
+
throw this.error("SQL cast requires a target type", token);
|
|
1085
|
+
let name = token.value;
|
|
1086
|
+
if (name.toUpperCase() === "DOUBLE") {
|
|
1087
|
+
this.expectKeyword("PRECISION");
|
|
1088
|
+
name += " PRECISION";
|
|
1089
|
+
}
|
|
1090
|
+
if (name.toUpperCase() === "CHARACTER") {
|
|
1091
|
+
this.expectKeyword("VARYING");
|
|
1092
|
+
name += " VARYING";
|
|
1093
|
+
}
|
|
1094
|
+
property = {
|
|
1095
|
+
...property,
|
|
1096
|
+
casts: [...property.casts ?? [], sqlCastType(name, this.source, token.offset)]
|
|
1097
|
+
};
|
|
1098
|
+
this.assertDepth(depth + property.casts.length);
|
|
1099
|
+
}
|
|
1100
|
+
return property;
|
|
1101
|
+
}
|
|
1102
|
+
parsePropertyPrimary(depth) {
|
|
1103
|
+
this.assertDepth(depth);
|
|
1104
|
+
if (this.takeKeyword("CAST")) {
|
|
1105
|
+
this.expectSymbol("(");
|
|
1106
|
+
const property = this.parseProperty(depth + 1);
|
|
1107
|
+
this.expectKeyword("AS");
|
|
1108
|
+
const target = this.take();
|
|
1109
|
+
if ((target == null ? void 0 : target.kind) !== "identifier" || target.quoted)
|
|
1110
|
+
throw this.error("SQL CAST requires a target type", target);
|
|
1111
|
+
let name = target.value;
|
|
1112
|
+
if (name.toUpperCase() === "DOUBLE") {
|
|
1113
|
+
this.expectKeyword("PRECISION");
|
|
1114
|
+
name += " PRECISION";
|
|
1115
|
+
}
|
|
1116
|
+
if (name.toUpperCase() === "CHARACTER") {
|
|
1117
|
+
this.expectKeyword("VARYING");
|
|
1118
|
+
name += " VARYING";
|
|
1119
|
+
}
|
|
1120
|
+
const type = sqlCastType(name, this.source, target.offset);
|
|
1121
|
+
this.expectSymbol(")");
|
|
1122
|
+
return { ...property, casts: [...property.casts ?? [], type] };
|
|
1123
|
+
}
|
|
1124
|
+
const token = this.take();
|
|
1125
|
+
if ((token == null ? void 0 : token.kind) !== "identifier") {
|
|
1126
|
+
throw this.error("SQL predicate expected a column identifier", token);
|
|
1127
|
+
}
|
|
1128
|
+
return token.quoted ? { property: token.value, quoted: true } : { property: token.value };
|
|
1129
|
+
}
|
|
1130
|
+
/** Parses one scalar literal or named parameter. */
|
|
1131
|
+
parseValue() {
|
|
1132
|
+
const negative = this.takeSymbol("-");
|
|
1133
|
+
const token = this.take();
|
|
1134
|
+
if (!token) {
|
|
1135
|
+
throw this.error("SQL predicate expected a scalar value");
|
|
1136
|
+
}
|
|
1137
|
+
if (negative && token.kind !== "number") {
|
|
1138
|
+
throw this.error("SQL predicate only permits unary minus on numeric literals", token);
|
|
1139
|
+
}
|
|
1140
|
+
if (token.kind === "number") {
|
|
1141
|
+
return parseNumericLiteral(negative ? `-${token.value}` : token.value, this.source, token.offset);
|
|
1142
|
+
}
|
|
1143
|
+
if (negative) {
|
|
1144
|
+
throw this.error("SQL predicate expected a numeric literal after unary minus");
|
|
1145
|
+
}
|
|
1146
|
+
if (token.kind === "string") {
|
|
1147
|
+
return token.value;
|
|
1148
|
+
}
|
|
1149
|
+
if (token.kind === "parameter") {
|
|
1150
|
+
if (this.preserveParameters) {
|
|
1151
|
+
return { parameter: token.value };
|
|
1152
|
+
}
|
|
1153
|
+
if (!Object.prototype.hasOwnProperty.call(this.parameters, token.value)) {
|
|
1154
|
+
throw this.error(`SQL predicate parameter ":${token.value}" requires a value`, token);
|
|
1155
|
+
}
|
|
1156
|
+
const value = this.parameters[token.value];
|
|
1157
|
+
validateParameterValue(value, token.value);
|
|
1158
|
+
if (value instanceof Uint8Array)
|
|
1159
|
+
return value.slice();
|
|
1160
|
+
if (value instanceof Date)
|
|
1161
|
+
return new Date(value.getTime());
|
|
1162
|
+
return value;
|
|
1163
|
+
}
|
|
1164
|
+
if (token.kind === "identifier" && !token.quoted && /^(TRUE|FALSE)$/i.test(token.value)) {
|
|
1165
|
+
return /^TRUE$/i.test(token.value);
|
|
1166
|
+
}
|
|
1167
|
+
if (token.kind === "identifier" && !token.quoted && /^NULL$/i.test(token.value)) {
|
|
1168
|
+
throw this.error("SQL predicate comparisons with NULL must use IS NULL or IS NOT NULL", token);
|
|
1169
|
+
}
|
|
1170
|
+
throw this.error(`SQL predicate does not support scalar token ${JSON.stringify(token.value)}`, token);
|
|
1171
|
+
}
|
|
1172
|
+
/** Rejects excessively nested inputs before recursion can exhaust the stack. */
|
|
1173
|
+
assertDepth(depth) {
|
|
1174
|
+
if (depth > MAXIMUM_EXPRESSION_DEPTH) {
|
|
1175
|
+
throw this.error("SQL predicate nesting exceeds its safe limit");
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
/** Consumes one required keyword. */
|
|
1179
|
+
expectKeyword(keyword) {
|
|
1180
|
+
if (!this.takeKeyword(keyword)) {
|
|
1181
|
+
throw this.error(`SQL predicate expected ${keyword}`);
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
/** Returns whether the next token is a keyword without consuming it. */
|
|
1185
|
+
peekKeyword(keyword) {
|
|
1186
|
+
const token = this.peek();
|
|
1187
|
+
return (token == null ? void 0 : token.kind) === "identifier" && !token.quoted && token.value.toUpperCase() === keyword;
|
|
1188
|
+
}
|
|
1189
|
+
/** Consumes one optional keyword. */
|
|
1190
|
+
takeKeyword(keyword) {
|
|
1191
|
+
if (!this.peekKeyword(keyword)) {
|
|
1192
|
+
return false;
|
|
1193
|
+
}
|
|
1194
|
+
this.position++;
|
|
1195
|
+
return true;
|
|
1196
|
+
}
|
|
1197
|
+
/** Consumes one required symbol. */
|
|
1198
|
+
expectSymbol(symbol) {
|
|
1199
|
+
if (!this.takeSymbol(symbol)) {
|
|
1200
|
+
throw this.error(`SQL predicate expected ${JSON.stringify(symbol)}`);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
/** Consumes one optional symbol. */
|
|
1204
|
+
takeSymbol(symbol) {
|
|
1205
|
+
const token = this.peek();
|
|
1206
|
+
if ((token == null ? void 0 : token.kind) !== "symbol" || token.value !== symbol) {
|
|
1207
|
+
return false;
|
|
1208
|
+
}
|
|
1209
|
+
this.position++;
|
|
1210
|
+
return true;
|
|
1211
|
+
}
|
|
1212
|
+
/** Consumes and returns the next token. */
|
|
1213
|
+
take() {
|
|
1214
|
+
const token = this.tokens[this.position];
|
|
1215
|
+
if (token) {
|
|
1216
|
+
this.position++;
|
|
1217
|
+
}
|
|
1218
|
+
return token;
|
|
1219
|
+
}
|
|
1220
|
+
/** Returns the next token without consuming it. */
|
|
1221
|
+
peek() {
|
|
1222
|
+
return this.tokens[this.position];
|
|
1223
|
+
}
|
|
1224
|
+
};
|
|
1225
|
+
function combineLogicalPredicate(operator, left, right) {
|
|
1226
|
+
return left.op === operator ? { op: operator, args: [...left.args, right] } : { op: operator, args: [left, right] };
|
|
1227
|
+
}
|
|
1228
|
+
function parseNumericLiteral(source, sql, offset) {
|
|
1229
|
+
if (/^-?\d+$/.test(source)) {
|
|
1230
|
+
const bigint = BigInt(source);
|
|
1231
|
+
if (bigint > BigInt(Number.MAX_SAFE_INTEGER) || bigint < BigInt(Number.MIN_SAFE_INTEGER)) {
|
|
1232
|
+
return bigint;
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
const number = Number(source);
|
|
1236
|
+
if (!Number.isFinite(number)) {
|
|
1237
|
+
throw new SQLParseError("SQL predicate numeric literals must be finite", sql, offset);
|
|
1238
|
+
}
|
|
1239
|
+
return number;
|
|
1240
|
+
}
|
|
1241
|
+
function validateParameterValue(value, name) {
|
|
1242
|
+
if (!isColumnarPredicateValue(value)) {
|
|
1243
|
+
throw new Error(`SQL predicate parameter ":${name}" has an unsupported value`);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
function tokenizeSQLPredicate(source) {
|
|
1247
|
+
if (typeof source !== "string" || source.trim().length === 0 || source.length > MAXIMUM_SQL_LENGTH) {
|
|
1248
|
+
throw new SQLParseError("SQL predicate requires a non-empty, safely bounded expression", "", 0);
|
|
1249
|
+
}
|
|
1250
|
+
const tokens = [];
|
|
1251
|
+
let position = 0;
|
|
1252
|
+
while (position < source.length) {
|
|
1253
|
+
const character = source[position];
|
|
1254
|
+
if (/\s/.test(character)) {
|
|
1255
|
+
position++;
|
|
1256
|
+
continue;
|
|
1257
|
+
}
|
|
1258
|
+
if (character === "'") {
|
|
1259
|
+
const result = readQuotedToken(source, position, "'", "'");
|
|
1260
|
+
tokens.push({ offset: position, kind: "string", value: result.value });
|
|
1261
|
+
position = result.position;
|
|
1262
|
+
} else if (character === '"') {
|
|
1263
|
+
const result = readQuotedToken(source, position, '"', '"');
|
|
1264
|
+
tokens.push({ offset: position, kind: "identifier", value: result.value, quoted: true });
|
|
1265
|
+
position = result.position;
|
|
1266
|
+
} else if (source.startsWith("::", position)) {
|
|
1267
|
+
tokens.push({ offset: position, kind: "symbol", value: "::" });
|
|
1268
|
+
position += 2;
|
|
1269
|
+
} else if (character === ":") {
|
|
1270
|
+
const match = /^[A-Za-z_][A-Za-z0-9_]*/.exec(source.slice(position + 1));
|
|
1271
|
+
if (!match) {
|
|
1272
|
+
throw new SQLParseError("SQL predicate contains an invalid parameter", source, position);
|
|
1273
|
+
}
|
|
1274
|
+
tokens.push({ offset: position, kind: "parameter", value: match[0] });
|
|
1275
|
+
position += match[0].length + 1;
|
|
1276
|
+
} else {
|
|
1277
|
+
const numberMatch = /^(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/.exec(source.slice(position));
|
|
1278
|
+
const identifierMatch = /^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*/.exec(source.slice(position));
|
|
1279
|
+
if (numberMatch) {
|
|
1280
|
+
tokens.push({ offset: position, kind: "number", value: numberMatch[0] });
|
|
1281
|
+
position += numberMatch[0].length;
|
|
1282
|
+
} else if (identifierMatch) {
|
|
1283
|
+
tokens.push({ offset: position, kind: "identifier", value: identifierMatch[0] });
|
|
1284
|
+
position += identifierMatch[0].length;
|
|
1285
|
+
} else {
|
|
1286
|
+
const twoCharacters = source.slice(position, position + 2);
|
|
1287
|
+
if (["<=", ">=", "<>", "!="].includes(twoCharacters)) {
|
|
1288
|
+
tokens.push({ offset: position, kind: "symbol", value: twoCharacters });
|
|
1289
|
+
position += 2;
|
|
1290
|
+
} else if ("(),;-=<>".includes(character)) {
|
|
1291
|
+
tokens.push({ offset: position, kind: "symbol", value: character });
|
|
1292
|
+
position++;
|
|
1293
|
+
} else {
|
|
1294
|
+
throw new SQLParseError("SQL predicate contains an unsupported token", source, position);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
if (tokens.length > MAXIMUM_SQL_TOKENS) {
|
|
1299
|
+
throw new SQLParseError("SQL predicate exceeds its safe token limit", source, position);
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
return tokens;
|
|
1303
|
+
}
|
|
1304
|
+
function readQuotedToken(source, start, quote, escapedQuote) {
|
|
1305
|
+
let value = "";
|
|
1306
|
+
let position = start + 1;
|
|
1307
|
+
while (position < source.length) {
|
|
1308
|
+
if (source[position] !== quote) {
|
|
1309
|
+
value += source[position++];
|
|
1310
|
+
continue;
|
|
1311
|
+
}
|
|
1312
|
+
if (source[position + 1] === escapedQuote) {
|
|
1313
|
+
value += quote;
|
|
1314
|
+
position += 2;
|
|
1315
|
+
continue;
|
|
1316
|
+
}
|
|
1317
|
+
return { value, position: position + 1 };
|
|
1318
|
+
}
|
|
1319
|
+
throw new SQLParseError("SQL predicate contains an unterminated quoted value", source, start);
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
// dist/parser/parse-sql-select.js
|
|
1323
|
+
var MAXIMUM_SQL_LENGTH2 = 65536;
|
|
1324
|
+
var MAXIMUM_SELECT_TOKENS = 4096;
|
|
1325
|
+
function parseSQLSelect(sql, options = {}) {
|
|
1326
|
+
const reader = new SQLQueryReader(sql, options);
|
|
1327
|
+
reader.expectKeyword("SELECT");
|
|
1328
|
+
let select;
|
|
1329
|
+
if (!reader.takeSymbol("*")) {
|
|
1330
|
+
select = [];
|
|
1331
|
+
do {
|
|
1332
|
+
const expression = reader.selectionExpression();
|
|
1333
|
+
const alias = reader.takeKeyword("AS") ? reader.identifier() : void 0;
|
|
1334
|
+
if (expression.type === "Identifier")
|
|
1335
|
+
select.push(alias ? { column: expression.name, as: alias } : expression.name);
|
|
1336
|
+
else if (expression.type === "CallExpression" && /^array(?:_(?:float(?:16|32|64)|uint8))?$/.test(expression.callee.name) && expression.arguments.every((argument) => argument.type === "Identifier")) {
|
|
1337
|
+
if (!alias)
|
|
1338
|
+
throw reader.error("SQL ARRAY requires AS");
|
|
1339
|
+
const type = expression.callee.name.slice(6);
|
|
1340
|
+
select.push({
|
|
1341
|
+
array: expression.arguments.map((argument) => argument.name),
|
|
1342
|
+
as: alias,
|
|
1343
|
+
...type ? { type } : {}
|
|
1344
|
+
});
|
|
1345
|
+
} else
|
|
1346
|
+
select.push({ expression, ...alias ? { as: alias } : {} });
|
|
1347
|
+
} while (reader.takeSymbol(","));
|
|
1348
|
+
}
|
|
1349
|
+
let from;
|
|
1350
|
+
if (reader.takeKeyword("FROM")) {
|
|
1351
|
+
const source = reader.identifier();
|
|
1352
|
+
from = reader.takeSymbol(".") ? { source, table: reader.identifier() } : source;
|
|
1353
|
+
if (!source.trim() || typeof from === "object" && !from.table.trim())
|
|
1354
|
+
throw reader.error("SQL FROM requires non-empty names.");
|
|
1355
|
+
}
|
|
1356
|
+
let where;
|
|
1357
|
+
if (reader.takeKeyword("WHERE")) {
|
|
1358
|
+
if (typeof options.parseWhere !== "function")
|
|
1359
|
+
throw reader.error("parseSQLSelect requires parseWhere for WHERE; use parseSQLQuery for built-in predicate support.");
|
|
1360
|
+
where = reader.parseWhere(options.parseWhere);
|
|
1361
|
+
if (!where || typeof where !== "object" || Array.isArray(where))
|
|
1362
|
+
throw reader.error("SQL WHERE parser must return a predicate.");
|
|
1363
|
+
}
|
|
1364
|
+
const pagination = reader.pagination();
|
|
1365
|
+
reader.end();
|
|
1366
|
+
return {
|
|
1367
|
+
...select ? { select } : {},
|
|
1368
|
+
...from === void 0 ? {} : { from },
|
|
1369
|
+
...where ? { where } : {},
|
|
1370
|
+
...pagination
|
|
1371
|
+
};
|
|
1372
|
+
}
|
|
1373
|
+
var SQLQueryReader = class {
|
|
1374
|
+
/** Rejects empty or oversized statements before scanning. */
|
|
1375
|
+
constructor(sql, parameterOptions = {}) {
|
|
1376
|
+
this.parameterOptions = parameterOptions;
|
|
1377
|
+
this.position = 0;
|
|
1378
|
+
this.tokens = 0;
|
|
1379
|
+
validateSQLParameterOptions(parameterOptions);
|
|
1380
|
+
if (typeof sql !== "string" || !sql.trim() || sql.length > MAXIMUM_SQL_LENGTH2)
|
|
1381
|
+
throw new SQLParseError("SQL query requires a non-empty statement of at most 65536 characters.", "", 0);
|
|
1382
|
+
this.sql = sql;
|
|
1383
|
+
}
|
|
1384
|
+
/** Reads a bounded scalar or fixed-array projection. */
|
|
1385
|
+
selectionExpression() {
|
|
1386
|
+
this.skipWhitespace();
|
|
1387
|
+
this.countToken();
|
|
1388
|
+
const reader = this.expressionReader();
|
|
1389
|
+
const expression = reader.expression();
|
|
1390
|
+
this.position = reader.position;
|
|
1391
|
+
return expression;
|
|
1392
|
+
}
|
|
1393
|
+
/** Consumes one mandatory keyword. */
|
|
1394
|
+
expectKeyword(keyword) {
|
|
1395
|
+
if (!this.takeKeyword(keyword))
|
|
1396
|
+
throw this.error(`SQL query expected ${keyword}.`);
|
|
1397
|
+
}
|
|
1398
|
+
/** Consumes an unquoted keyword without folding identifier names. */
|
|
1399
|
+
takeKeyword(keyword) {
|
|
1400
|
+
this.skipWhitespace();
|
|
1401
|
+
const match = /^[A-Za-z_][A-Za-z0-9_]*/.exec(this.sql.slice(this.position));
|
|
1402
|
+
if ((match == null ? void 0 : match[0].toUpperCase()) !== keyword)
|
|
1403
|
+
return false;
|
|
1404
|
+
this.position += match[0].length;
|
|
1405
|
+
this.countToken();
|
|
1406
|
+
return true;
|
|
1407
|
+
}
|
|
1408
|
+
/** Consumes a punctuation token. */
|
|
1409
|
+
takeSymbol(symbol) {
|
|
1410
|
+
this.skipWhitespace();
|
|
1411
|
+
if (this.sql[this.position] !== symbol)
|
|
1412
|
+
return false;
|
|
1413
|
+
this.position++;
|
|
1414
|
+
this.countToken();
|
|
1415
|
+
return true;
|
|
1416
|
+
}
|
|
1417
|
+
/** Reads one literal identifier, including doubled double-quote escapes. */
|
|
1418
|
+
identifier() {
|
|
1419
|
+
this.countToken();
|
|
1420
|
+
const reader = this.expressionReader();
|
|
1421
|
+
const identifier = reader.readIdentifier();
|
|
1422
|
+
this.position = reader.position;
|
|
1423
|
+
return identifier;
|
|
1424
|
+
}
|
|
1425
|
+
/** Parses a WHERE fragment and translates its cursor back to the full statement. */
|
|
1426
|
+
parseWhere(parse) {
|
|
1427
|
+
this.skipWhitespace();
|
|
1428
|
+
const start = this.position;
|
|
1429
|
+
const source = this.whereExpression();
|
|
1430
|
+
try {
|
|
1431
|
+
return parse(source);
|
|
1432
|
+
} catch (error) {
|
|
1433
|
+
if (error instanceof SQLParseError)
|
|
1434
|
+
throw new SQLParseError(error.reason, this.sql, start + error.offset);
|
|
1435
|
+
throw error;
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
/** Creates an error at the current statement cursor. */
|
|
1439
|
+
error(message, offset = this.position) {
|
|
1440
|
+
return new SQLParseError(message, this.sql, offset);
|
|
1441
|
+
}
|
|
1442
|
+
/** Finds a top-level pagination clause or terminator without interpreting quoted predicate contents. */
|
|
1443
|
+
whereExpression() {
|
|
1444
|
+
this.skipWhitespace();
|
|
1445
|
+
const start = this.position;
|
|
1446
|
+
let quote = "";
|
|
1447
|
+
let quoteStart = start;
|
|
1448
|
+
let depth = 0;
|
|
1449
|
+
let groupingDepth = 0;
|
|
1450
|
+
const membershipParens = [];
|
|
1451
|
+
while (this.position < this.sql.length) {
|
|
1452
|
+
const character = this.sql[this.position];
|
|
1453
|
+
if (quote) {
|
|
1454
|
+
if (character === quote) {
|
|
1455
|
+
if (this.sql[this.position + 1] === quote) {
|
|
1456
|
+
this.position += 2;
|
|
1457
|
+
continue;
|
|
1458
|
+
}
|
|
1459
|
+
quote = "";
|
|
1460
|
+
}
|
|
1461
|
+
} else if (character === '"' || character === "'") {
|
|
1462
|
+
quote = character;
|
|
1463
|
+
quoteStart = this.position;
|
|
1464
|
+
} else if (this.sql.startsWith("--", this.position) || this.sql.startsWith("/*", this.position))
|
|
1465
|
+
throw this.error("SQL comments are not supported.");
|
|
1466
|
+
else if (character === "(") {
|
|
1467
|
+
const membership = /\bIN\s*$/i.test(this.sql.slice(start, this.position));
|
|
1468
|
+
membershipParens.push(membership);
|
|
1469
|
+
depth++;
|
|
1470
|
+
if (!membership && ++groupingDepth > 64)
|
|
1471
|
+
throw this.error("SQL query exceeds its predicate nesting limit.");
|
|
1472
|
+
} else if (character === ")") {
|
|
1473
|
+
if (--depth < 0)
|
|
1474
|
+
throw this.error("SQL query contains unbalanced parentheses.");
|
|
1475
|
+
if (!membershipParens.pop())
|
|
1476
|
+
groupingDepth--;
|
|
1477
|
+
} else if (depth === 0 && (character === ";" || this.atPaginationClause()))
|
|
1478
|
+
break;
|
|
1479
|
+
this.position++;
|
|
1480
|
+
}
|
|
1481
|
+
if (quote || depth)
|
|
1482
|
+
throw this.error("SQL query contains an unterminated predicate.", quote ? quoteStart : this.position);
|
|
1483
|
+
const expression = this.sql.slice(start, this.position).trim();
|
|
1484
|
+
if (!expression)
|
|
1485
|
+
throw this.error("SQL WHERE requires a predicate.");
|
|
1486
|
+
return expression;
|
|
1487
|
+
}
|
|
1488
|
+
/** Checks for an unquoted pagination keyword at the current predicate boundary. */
|
|
1489
|
+
atPaginationClause() {
|
|
1490
|
+
return !/[A-Za-z0-9_.:]/.test(this.sql[this.position - 1] || "") && /^(?:LIMIT|OFFSET)(?![A-Za-z0-9_.:])/i.test(this.sql.slice(this.position, this.position + 7));
|
|
1491
|
+
}
|
|
1492
|
+
/** Reads optional LIMIT and OFFSET in either order. */
|
|
1493
|
+
pagination() {
|
|
1494
|
+
let limit;
|
|
1495
|
+
let offset;
|
|
1496
|
+
while (true) {
|
|
1497
|
+
if (this.takeKeyword("LIMIT")) {
|
|
1498
|
+
if (limit !== void 0)
|
|
1499
|
+
throw this.error("Duplicate SQL LIMIT clause.");
|
|
1500
|
+
limit = this.rowCount("LIMIT");
|
|
1501
|
+
} else if (this.takeKeyword("OFFSET")) {
|
|
1502
|
+
if (offset !== void 0)
|
|
1503
|
+
throw this.error("Duplicate SQL OFFSET clause.");
|
|
1504
|
+
offset = this.rowCount("OFFSET");
|
|
1505
|
+
if (!this.takeKeyword("ROWS"))
|
|
1506
|
+
this.takeKeyword("ROW");
|
|
1507
|
+
} else
|
|
1508
|
+
break;
|
|
1509
|
+
}
|
|
1510
|
+
return {
|
|
1511
|
+
...offset === void 0 ? {} : { offset },
|
|
1512
|
+
...limit === void 0 ? {} : { limit }
|
|
1513
|
+
};
|
|
1514
|
+
}
|
|
1515
|
+
/** Reads the non-negative safe integer for a pagination clause. */
|
|
1516
|
+
rowCount(clause) {
|
|
1517
|
+
this.skipWhitespace();
|
|
1518
|
+
const match = /^\d+(?![A-Za-z0-9_.])/.exec(this.sql.slice(this.position));
|
|
1519
|
+
const value = Number(match == null ? void 0 : match[0]);
|
|
1520
|
+
if (!match || !Number.isSafeInteger(value))
|
|
1521
|
+
throw this.error(`SQL ${clause} requires a non-negative safe integer.`);
|
|
1522
|
+
this.position += match[0].length;
|
|
1523
|
+
return value;
|
|
1524
|
+
}
|
|
1525
|
+
/** Requires one complete statement with at most one trailing semicolon. */
|
|
1526
|
+
end() {
|
|
1527
|
+
this.takeSymbol(";");
|
|
1528
|
+
this.skipWhitespace();
|
|
1529
|
+
if (this.position !== this.sql.length)
|
|
1530
|
+
throw this.error("Unsupported SQL query syntax.");
|
|
1531
|
+
}
|
|
1532
|
+
/** Skips whitespace without altering quoted identifiers. */
|
|
1533
|
+
skipWhitespace() {
|
|
1534
|
+
while (this.position < this.sql.length && /\s/.test(this.sql[this.position]))
|
|
1535
|
+
this.position++;
|
|
1536
|
+
}
|
|
1537
|
+
expressionReader() {
|
|
1538
|
+
return new SQLExpressionReader(this.sql, this.position, this.parameterOptions);
|
|
1539
|
+
}
|
|
1540
|
+
/** Bounds large projection lists independently of predicate parsing. */
|
|
1541
|
+
countToken() {
|
|
1542
|
+
if (++this.tokens > MAXIMUM_SELECT_TOKENS)
|
|
1543
|
+
throw this.error("SQL query exceeds its structural token limit.");
|
|
1544
|
+
}
|
|
1545
|
+
};
|
|
1546
|
+
|
|
1547
|
+
// dist/parser/parse-sql-query.js
|
|
1548
|
+
function parseSQLQuery(sql, options = {}) {
|
|
1549
|
+
try {
|
|
1550
|
+
return parseQuery(sql, options);
|
|
1551
|
+
} catch (error) {
|
|
1552
|
+
reportDiagnosticError(options == null ? void 0 : options.log, error);
|
|
1553
|
+
throw error;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
function parseQuery(sql, options) {
|
|
1557
|
+
const reader = new SQLQueryReader(sql, options);
|
|
1558
|
+
if (reader.takeKeyword("SHOW")) {
|
|
1559
|
+
let request;
|
|
1560
|
+
if (reader.takeKeyword("SOURCES"))
|
|
1561
|
+
request = { command: "showSources" };
|
|
1562
|
+
else {
|
|
1563
|
+
reader.expectKeyword("TABLES");
|
|
1564
|
+
request = reader.takeKeyword("FROM") ? { command: "showTables", from: reader.identifier() } : { command: "showTables" };
|
|
1565
|
+
}
|
|
1566
|
+
reader.end();
|
|
1567
|
+
return request;
|
|
1568
|
+
}
|
|
1569
|
+
if (reader.takeKeyword("DESCRIBE")) {
|
|
1570
|
+
const source = reader.identifier();
|
|
1571
|
+
const from = reader.takeSymbol(".") ? { source, table: reader.identifier() } : source;
|
|
1572
|
+
reader.end();
|
|
1573
|
+
return { command: "describe", from };
|
|
1574
|
+
}
|
|
1575
|
+
return parseSQLSelect(sql, {
|
|
1576
|
+
...options,
|
|
1577
|
+
parseWhere: (expression) => {
|
|
1578
|
+
try {
|
|
1579
|
+
if (options.parseParameter || isSQLParameterSnapshot(options.parameters))
|
|
1580
|
+
throw new Error("Custom parameter AST requires expression parsing");
|
|
1581
|
+
const predicate = parseSQLPredicate(expression, {
|
|
1582
|
+
parameters: options.parameters
|
|
1583
|
+
});
|
|
1584
|
+
validateColumnReferences(predicate);
|
|
1585
|
+
return predicate;
|
|
1586
|
+
} catch {
|
|
1587
|
+
return { expression: parseSQLExpression(expression, options) };
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
});
|
|
1591
|
+
}
|
|
1592
|
+
function validateColumnReferences(predicate) {
|
|
1593
|
+
if (predicate.op === "and" || predicate.op === "or") {
|
|
1594
|
+
predicate.args.forEach(validateColumnReferences);
|
|
1595
|
+
} else if (predicate.op === "not")
|
|
1596
|
+
validateColumnReferences(predicate.args[0]);
|
|
1597
|
+
else {
|
|
1598
|
+
const values = predicate.op === "in" ? predicate.args[1] : predicate.op === "isNull" ? [] : [predicate.args[1]];
|
|
1599
|
+
if (values.some((value) => value instanceof Date || value instanceof Uint8Array))
|
|
1600
|
+
throw new Error("Date and binary values require expression evaluation");
|
|
1601
|
+
const property = predicate.args[0];
|
|
1602
|
+
if (!property.quoted && property.property.includes("."))
|
|
1603
|
+
throw new Error("Nested fields require expression evaluation; quote literal column names containing dots.");
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
|
|
1607
|
+
// dist/parser/bind-sql-predicate.js
|
|
1608
|
+
function bindSQLPredicate(predicate, parameters) {
|
|
1609
|
+
validateSQLPredicate(predicate);
|
|
1610
|
+
return bindColumnarPredicateParameters(predicate, parameters);
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
// dist/parser/parse-sql-statement.js
|
|
1614
|
+
function parseSQLSelectStatement(sql, options = {}) {
|
|
1615
|
+
const reader = new RelationalQueryReader(sql, options);
|
|
1616
|
+
const statement = { ...readSelect(reader), ...readTail(reader) };
|
|
1617
|
+
reader.end();
|
|
1618
|
+
return statement;
|
|
1619
|
+
}
|
|
1620
|
+
function parseSQLSelectCompound(sql, options = {}) {
|
|
1621
|
+
const reader = new RelationalQueryReader(sql, options);
|
|
1622
|
+
const inputs = [readSelect(reader)];
|
|
1623
|
+
while (reader.takeKeyword("UNION")) {
|
|
1624
|
+
reader.expectKeyword("ALL");
|
|
1625
|
+
inputs.push(readSelect(reader));
|
|
1626
|
+
}
|
|
1627
|
+
const tail = readTail(reader);
|
|
1628
|
+
reader.end();
|
|
1629
|
+
return { inputs, ...tail };
|
|
1630
|
+
}
|
|
1631
|
+
function readSelect(reader) {
|
|
1632
|
+
reader.expectKeyword("SELECT");
|
|
1633
|
+
const select = reader.takeSymbol("*") ? void 0 : readList(reader, () => {
|
|
1634
|
+
const expression = reader.selectionExpression();
|
|
1635
|
+
const alias = reader.takeKeyword("AS") ? reader.identifier() : void 0;
|
|
1636
|
+
return { expression, ...alias === void 0 ? {} : { alias } };
|
|
1637
|
+
});
|
|
1638
|
+
reader.expectKeyword("FROM");
|
|
1639
|
+
const from = readTable(reader);
|
|
1640
|
+
const joins = [];
|
|
1641
|
+
for (let type = readJoinType(reader); type; type = readJoinType(reader)) {
|
|
1642
|
+
if (reader.takeKeyword("UNNEST")) {
|
|
1643
|
+
reader.expectSymbol("(");
|
|
1644
|
+
const expression = reader.selectionExpression();
|
|
1645
|
+
reader.expectSymbol(")");
|
|
1646
|
+
const ordinality = reader.takeKeyword("WITH");
|
|
1647
|
+
if (ordinality)
|
|
1648
|
+
reader.expectKeyword("ORDINALITY");
|
|
1649
|
+
reader.expectKeyword("AS");
|
|
1650
|
+
const alias = reader.identifier();
|
|
1651
|
+
reader.expectSymbol("(");
|
|
1652
|
+
const columns = readList(reader, () => reader.identifier());
|
|
1653
|
+
reader.expectSymbol(")");
|
|
1654
|
+
if (type === "left") {
|
|
1655
|
+
reader.expectKeyword("ON");
|
|
1656
|
+
reader.expectKeyword("TRUE");
|
|
1657
|
+
} else if (type !== "cross")
|
|
1658
|
+
throw reader.error("UNNEST requires CROSS JOIN or LEFT JOIN");
|
|
1659
|
+
joins.push({
|
|
1660
|
+
type,
|
|
1661
|
+
table: { name: alias, alias },
|
|
1662
|
+
on: { type: "Literal", value: true },
|
|
1663
|
+
unnest: { expression, columns, ordinality }
|
|
1664
|
+
});
|
|
1665
|
+
continue;
|
|
1666
|
+
}
|
|
1667
|
+
if (type === "cross")
|
|
1668
|
+
throw reader.error("CROSS JOIN requires UNNEST");
|
|
1669
|
+
const table = readTable(reader);
|
|
1670
|
+
reader.expectKeyword("ON");
|
|
1671
|
+
joins.push({ type, table, on: reader.selectionExpression() });
|
|
1672
|
+
}
|
|
1673
|
+
const where = reader.takeKeyword("WHERE") ? reader.selectionExpression() : void 0;
|
|
1674
|
+
const groupBy = readBy(reader, "GROUP", () => reader.selectionExpression());
|
|
1675
|
+
return {
|
|
1676
|
+
from,
|
|
1677
|
+
joins,
|
|
1678
|
+
...select ? { select } : {},
|
|
1679
|
+
...where ? { where } : {},
|
|
1680
|
+
...groupBy ? { groupBy } : {}
|
|
1681
|
+
};
|
|
1682
|
+
}
|
|
1683
|
+
function readTail(reader) {
|
|
1684
|
+
const orderBy = readBy(reader, "ORDER", () => readSort(reader));
|
|
1685
|
+
return { ...orderBy ? { orderBy } : {}, ...reader.pagination() };
|
|
1686
|
+
}
|
|
1687
|
+
function readBy(reader, clause, read) {
|
|
1688
|
+
if (!reader.takeKeyword(clause))
|
|
1689
|
+
return void 0;
|
|
1690
|
+
reader.expectKeyword("BY");
|
|
1691
|
+
return readList(reader, read);
|
|
1692
|
+
}
|
|
1693
|
+
function readTable(reader) {
|
|
1694
|
+
const name = reader.tableName();
|
|
1695
|
+
const alias = reader.takeKeyword("AS") ? reader.identifier() : reader.optionalAlias();
|
|
1696
|
+
return { name, ...alias === void 0 ? {} : { alias } };
|
|
1697
|
+
}
|
|
1698
|
+
function readJoinType(reader) {
|
|
1699
|
+
if (reader.takeKeyword("CROSS")) {
|
|
1700
|
+
reader.expectKeyword("JOIN");
|
|
1701
|
+
return "cross";
|
|
1702
|
+
}
|
|
1703
|
+
if (reader.takeKeyword("JOIN"))
|
|
1704
|
+
return "inner";
|
|
1705
|
+
let type;
|
|
1706
|
+
if (reader.takeKeyword("INNER"))
|
|
1707
|
+
type = "inner";
|
|
1708
|
+
else if (reader.takeKeyword("LEFT")) {
|
|
1709
|
+
reader.takeKeyword("OUTER");
|
|
1710
|
+
type = "left";
|
|
1711
|
+
} else if (reader.takeKeyword("SEMI"))
|
|
1712
|
+
type = "semi";
|
|
1713
|
+
else if (reader.takeKeyword("ANTI"))
|
|
1714
|
+
type = "anti";
|
|
1715
|
+
else
|
|
1716
|
+
return void 0;
|
|
1717
|
+
reader.expectKeyword("JOIN");
|
|
1718
|
+
return type;
|
|
1719
|
+
}
|
|
1720
|
+
function readSort(reader) {
|
|
1721
|
+
const expression = reader.selectionExpression();
|
|
1722
|
+
const direction = reader.takeKeyword("DESC") ? "desc" : "asc";
|
|
1723
|
+
if (direction === "asc")
|
|
1724
|
+
reader.takeKeyword("ASC");
|
|
1725
|
+
if (!reader.takeKeyword("NULLS"))
|
|
1726
|
+
return { expression, direction };
|
|
1727
|
+
if (reader.takeKeyword("FIRST"))
|
|
1728
|
+
return { expression, direction, nulls: "first" };
|
|
1729
|
+
reader.expectKeyword("LAST");
|
|
1730
|
+
return { expression, direction, nulls: "last" };
|
|
1731
|
+
}
|
|
1732
|
+
function readList(reader, readItem) {
|
|
1733
|
+
const items = [readItem()];
|
|
1734
|
+
while (reader.takeSymbol(","))
|
|
1735
|
+
items.push(readItem());
|
|
1736
|
+
return items;
|
|
1737
|
+
}
|
|
1738
|
+
var RelationalQueryReader = class extends SQLQueryReader {
|
|
1739
|
+
/** Requires punctuation with a positioned diagnostic. */
|
|
1740
|
+
expectSymbol(symbol) {
|
|
1741
|
+
if (!this.takeSymbol(symbol))
|
|
1742
|
+
throw this.error(`Expected ${symbol}`);
|
|
1743
|
+
}
|
|
1744
|
+
constructor(sql, options) {
|
|
1745
|
+
super(sql, options);
|
|
1746
|
+
this.options = options;
|
|
1747
|
+
}
|
|
1748
|
+
tableName() {
|
|
1749
|
+
this.skipWhitespace();
|
|
1750
|
+
if (/^LATERAL(?![A-Za-z0-9_])/i.test(this.sql.slice(this.position))) {
|
|
1751
|
+
throw this.error("SQL LATERAL table expressions are not supported.");
|
|
1752
|
+
}
|
|
1753
|
+
return this.identifier();
|
|
1754
|
+
}
|
|
1755
|
+
/** Reads an implicit alias unless the next token starts a clause. */
|
|
1756
|
+
optionalAlias() {
|
|
1757
|
+
var _a;
|
|
1758
|
+
this.skipWhitespace();
|
|
1759
|
+
if (this.sql[this.position] === '"')
|
|
1760
|
+
return this.identifier();
|
|
1761
|
+
const name = (_a = /^[A-Za-z_][A-Za-z0-9_]*/.exec(this.sql.slice(this.position))) == null ? void 0 : _a[0];
|
|
1762
|
+
if (name && !SQL_RESERVED_WORDS.has(name.toUpperCase()) && !/^(INNER|LEFT|RIGHT|FULL|CROSS|OUTER|SEMI|ANTI|NATURAL|LATERAL|ON)$/i.test(name))
|
|
1763
|
+
return this.identifier();
|
|
1764
|
+
return void 0;
|
|
1765
|
+
}
|
|
1766
|
+
expressionReader() {
|
|
1767
|
+
return new RelationalExpressionReader(this.sql, this.position, this.options);
|
|
1768
|
+
}
|
|
1769
|
+
skipWhitespace() {
|
|
1770
|
+
this.position = skipLineComments(this.sql, this.position);
|
|
1771
|
+
}
|
|
1772
|
+
};
|
|
1773
|
+
var RelationalExpressionReader = class extends SQLExpressionReader {
|
|
1774
|
+
constructor(source, position, options) {
|
|
1775
|
+
super(source, position, options);
|
|
1776
|
+
}
|
|
1777
|
+
space() {
|
|
1778
|
+
this.position = skipLineComments(this.source, this.position);
|
|
1779
|
+
super.space();
|
|
1780
|
+
}
|
|
1781
|
+
/** Preserves dot access until the planner can resolve table aliases. */
|
|
1782
|
+
memberAccess(object, name) {
|
|
1783
|
+
const property = this.node({ type: "Identifier", name });
|
|
1784
|
+
return this.node({ type: "MemberExpression", computed: false, object, property }, [
|
|
1785
|
+
object,
|
|
1786
|
+
property
|
|
1787
|
+
]);
|
|
1788
|
+
}
|
|
1789
|
+
wildcardArgument(name) {
|
|
1790
|
+
if (name.toUpperCase() !== "COUNT" || !this.take("*"))
|
|
1791
|
+
return void 0;
|
|
1792
|
+
return this.node({ type: "Identifier", name: "*", sqlWildcard: true });
|
|
1793
|
+
}
|
|
1794
|
+
};
|
|
1795
|
+
function skipLineComments(source, position) {
|
|
1796
|
+
while (position < source.length) {
|
|
1797
|
+
if (/\s/.test(source[position]))
|
|
1798
|
+
position++;
|
|
1799
|
+
else if (source.startsWith("--", position)) {
|
|
1800
|
+
while (position < source.length && !/[\r\n]/.test(source[position]))
|
|
1801
|
+
position++;
|
|
1802
|
+
} else
|
|
1803
|
+
break;
|
|
1804
|
+
}
|
|
1805
|
+
return position;
|
|
1806
|
+
}
|
|
1807
|
+
//# sourceMappingURL=index.cjs.map
|