@loaders.gl/sql 5.0.0-beta.5 → 5.0.0-beta.7
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/dist/duckdb-sql-source.d.ts +2 -17
- package/dist/duckdb-sql-source.d.ts.map +1 -1
- package/dist/duckdb-sql-source.js +1 -1
- package/dist/index.cjs +392 -489
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/snowflake-sql-source.d.ts +2 -17
- package/dist/snowflake-sql-source.d.ts.map +1 -1
- package/dist/snowflake-sql-source.js +1 -1
- package/dist/sql-source.d.ts +9 -1
- package/dist/sql-source.d.ts.map +1 -1
- package/dist/sql-source.js +27 -0
- package/dist/sql-source.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +3 -0
- package/src/sql-source.ts +33 -1
package/dist/index.cjs
CHANGED
|
@@ -402,6 +402,8 @@ var DataSource = class _DataSource {
|
|
|
402
402
|
options;
|
|
403
403
|
data;
|
|
404
404
|
url;
|
|
405
|
+
/** Stable query-visible name, resolved without fetching source data. */
|
|
406
|
+
name;
|
|
405
407
|
/** Shared parser options derived from the source options, excluding source-only core controls. */
|
|
406
408
|
loadOptions;
|
|
407
409
|
/** A resolved fetch function extracted from the shared core options. */
|
|
@@ -418,6 +420,7 @@ var DataSource = class _DataSource {
|
|
|
418
420
|
);
|
|
419
421
|
this.data = data;
|
|
420
422
|
this.url = typeof data === "string" ? resolvePath(data) : "";
|
|
423
|
+
this.name = getDataSourceName(data, this.options);
|
|
421
424
|
const loadOptions = resolveAuthenticationOptions(getSourceLoaderOptions(this.options));
|
|
422
425
|
this.loadOptions = loadOptions;
|
|
423
426
|
const fetch2 = getFetchFunction(loadOptions);
|
|
@@ -426,6 +429,9 @@ var DataSource = class _DataSource {
|
|
|
426
429
|
this.fetch = fetch2;
|
|
427
430
|
}
|
|
428
431
|
setProps(options) {
|
|
432
|
+
var _a;
|
|
433
|
+
if (((_a = options.core) == null ? void 0 : _a.name) !== void 0 && options.core.name !== this.name)
|
|
434
|
+
throw new Error("Data source names are fixed at creation.");
|
|
429
435
|
const mergedOptions = mergeOptions(this.options, options);
|
|
430
436
|
const loadOptions = resolveAuthenticationOptions(getSourceLoaderOptions(mergedOptions));
|
|
431
437
|
this.options = mergedOptions;
|
|
@@ -475,7 +481,7 @@ function getSourceLoaderOptions(options) {
|
|
|
475
481
|
if (options == null ? void 0 : options.core) {
|
|
476
482
|
loadOptions.core = { ...options.core };
|
|
477
483
|
const parserCore = loadOptions.core;
|
|
478
|
-
for (const key of ["type", "attributions", "loaders", "onError", "loadOptions"]) {
|
|
484
|
+
for (const key of ["type", "name", "attributions", "loaders", "onError", "loadOptions"]) {
|
|
479
485
|
delete parserCore[key];
|
|
480
486
|
}
|
|
481
487
|
if (Object.keys(parserCore).length === 0) {
|
|
@@ -510,6 +516,44 @@ function unavailableCoreApiMethod(methodName) {
|
|
|
510
516
|
);
|
|
511
517
|
};
|
|
512
518
|
}
|
|
519
|
+
function getDataSourceName(data, options) {
|
|
520
|
+
var _a, _b, _c;
|
|
521
|
+
const explicitName = (_a = options.core) == null ? void 0 : _a.name;
|
|
522
|
+
if (explicitName !== void 0) {
|
|
523
|
+
if (typeof explicitName !== "string" || !explicitName.trim())
|
|
524
|
+
throw new Error("Data source name must be a non-empty string.");
|
|
525
|
+
return explicitName;
|
|
526
|
+
}
|
|
527
|
+
if (data && typeof data === "object") {
|
|
528
|
+
const named = data;
|
|
529
|
+
if (typeof named.name === "string" && named.name.trim())
|
|
530
|
+
return data instanceof Blob ? removeFileExtension(named.name) : named.name;
|
|
531
|
+
const metadata = (_b = named.schema) == null ? void 0 : _b.metadata;
|
|
532
|
+
const embeddedName = metadata instanceof Map ? metadata.get("name") : metadata == null ? void 0 : metadata.name;
|
|
533
|
+
if (typeof embeddedName === "string" && embeddedName.trim()) return embeddedName;
|
|
534
|
+
}
|
|
535
|
+
if (typeof data === "string") {
|
|
536
|
+
try {
|
|
537
|
+
const url = new URL(data.replace(/\\/g, "/"), "https://source.invalid/");
|
|
538
|
+
if (!["data:", "blob:"].includes(url.protocol)) {
|
|
539
|
+
let basename = url.pathname.split("/").filter(Boolean).pop();
|
|
540
|
+
if (basename) {
|
|
541
|
+
try {
|
|
542
|
+
basename = decodeURIComponent(basename);
|
|
543
|
+
} catch {
|
|
544
|
+
}
|
|
545
|
+
return removeFileExtension(basename);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
} catch {
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
return ((_c = options.core) == null ? void 0 : _c.type) && options.core.type !== "auto" ? options.core.type : "source";
|
|
552
|
+
}
|
|
553
|
+
function removeFileExtension(name) {
|
|
554
|
+
const extension = name.lastIndexOf(".");
|
|
555
|
+
return extension > 0 ? name.slice(0, extension) : name;
|
|
556
|
+
}
|
|
513
557
|
|
|
514
558
|
// ../core/src/loader-utils/lib/scan-utils/columnar-predicate.ts
|
|
515
559
|
function getColumnarPredicateColumns(predicate) {
|
|
@@ -2520,6 +2564,28 @@ var SQLDataSource = class extends DataSource {
|
|
|
2520
2564
|
const adapter = await this.getAdapter();
|
|
2521
2565
|
return await adapter.listTables(options);
|
|
2522
2566
|
}
|
|
2567
|
+
/** Lists stable native table identifiers without fetching any column schemas or data rows. */
|
|
2568
|
+
async getTablesMetadata(options = {}) {
|
|
2569
|
+
var _a, _b;
|
|
2570
|
+
(_a = options.signal) == null ? void 0 : _a.throwIfAborted();
|
|
2571
|
+
const tables = await this.listTables();
|
|
2572
|
+
(_b = options.signal) == null ? void 0 : _b.throwIfAborted();
|
|
2573
|
+
return tables.map((table) => ({ name: getTableMetadataName(table), queryable: false }));
|
|
2574
|
+
}
|
|
2575
|
+
/** Discovers one native table schema; ambiguous unqualified names are rejected. */
|
|
2576
|
+
async getTableMetadata(name, options = {}) {
|
|
2577
|
+
var _a, _b, _c;
|
|
2578
|
+
(_a = options.signal) == null ? void 0 : _a.throwIfAborted();
|
|
2579
|
+
const tables = await this.listTables();
|
|
2580
|
+
(_b = options.signal) == null ? void 0 : _b.throwIfAborted();
|
|
2581
|
+
const exact = tables.filter((table) => getTableMetadataName(table) === name);
|
|
2582
|
+
const matches = exact.length ? exact : tables.filter((table) => table.tableName === name);
|
|
2583
|
+
if (matches.length !== 1)
|
|
2584
|
+
throw new Error(`Unknown or ambiguous SQL table: ${name}`);
|
|
2585
|
+
const schema = await this.getTableSchema(matches[0]);
|
|
2586
|
+
(_c = options.signal) == null ? void 0 : _c.throwIfAborted();
|
|
2587
|
+
return { name: getTableMetadataName(matches[0]), schema, queryable: false };
|
|
2588
|
+
}
|
|
2523
2589
|
/** Returns the schema for a specific SQL table. */
|
|
2524
2590
|
async getTableSchema(options) {
|
|
2525
2591
|
const adapter = await this.getAdapter();
|
|
@@ -2615,9 +2681,266 @@ function getSQLTableQueryDialect(sourceType) {
|
|
|
2615
2681
|
return "snowflake";
|
|
2616
2682
|
throw new Error(`Portable table queries are not supported for SQL source type "${sourceType}".`);
|
|
2617
2683
|
}
|
|
2684
|
+
function getTableMetadataName(table) {
|
|
2685
|
+
return [table.catalogName, table.schemaName, table.tableName].filter((name) => typeof name === "string" && name.length > 0).map((name) => `"${name.replaceAll('"', '""')}"`).join(".");
|
|
2686
|
+
}
|
|
2618
2687
|
|
|
2619
2688
|
// ../core/src/scan/query-arrow-table.ts
|
|
2689
|
+
var arrow8 = __toESM(require("apache-arrow"), 1);
|
|
2690
|
+
|
|
2691
|
+
// ../core/src/scan/arrow-execution/project-arrow-table.ts
|
|
2620
2692
|
var arrow6 = __toESM(require("apache-arrow"), 1);
|
|
2693
|
+
function projectArrowTable(source, selections) {
|
|
2694
|
+
const outputNames = /* @__PURE__ */ new Set();
|
|
2695
|
+
const indices = [];
|
|
2696
|
+
const fields = selections.map((selection) => {
|
|
2697
|
+
const column = typeof selection === "string" ? selection : selection.column;
|
|
2698
|
+
const name = typeof selection === "string" ? selection : selection.as === void 0 ? column : selection.as;
|
|
2699
|
+
const index = source.schema.fields.findIndex((field) => field.name === column);
|
|
2700
|
+
if (index < 0) throw new Error(`Arrow projection column not found: ${column}`);
|
|
2701
|
+
if (typeof name !== "string" || typeof selection !== "string" && selection.as !== void 0 && !name.length || outputNames.has(name)) {
|
|
2702
|
+
throw new Error(`Invalid or duplicate Arrow projection output name: ${name}`);
|
|
2703
|
+
}
|
|
2704
|
+
outputNames.add(name);
|
|
2705
|
+
indices.push(index);
|
|
2706
|
+
return source.schema.fields[index].clone({ name });
|
|
2707
|
+
});
|
|
2708
|
+
const schema = new arrow6.Schema(fields, source.schema.metadata);
|
|
2709
|
+
const batches = source.batches.map(
|
|
2710
|
+
(batch) => new arrow6.RecordBatch(
|
|
2711
|
+
schema,
|
|
2712
|
+
arrow6.makeData({
|
|
2713
|
+
type: new arrow6.Struct(fields),
|
|
2714
|
+
length: batch.numRows,
|
|
2715
|
+
children: indices.map((index) => batch.data.children[index])
|
|
2716
|
+
}),
|
|
2717
|
+
batch.metadata
|
|
2718
|
+
)
|
|
2719
|
+
);
|
|
2720
|
+
return new arrow6.Table(schema, batches);
|
|
2721
|
+
}
|
|
2722
|
+
|
|
2723
|
+
// ../core/src/scan/arrow-execution/filter-arrow-table.ts
|
|
2724
|
+
var arrow7 = __toESM(require("apache-arrow"), 1);
|
|
2725
|
+
function filterArrowTable(source, predicate, options = {}) {
|
|
2726
|
+
validateColumnarPredicate(predicate, new Set(source.schema.fields.map((field) => field.name)));
|
|
2727
|
+
const columnNames = getColumnarPredicateColumns(predicate);
|
|
2728
|
+
return filterArrowRows(
|
|
2729
|
+
source,
|
|
2730
|
+
(batch) => {
|
|
2731
|
+
const columns = getPredicateColumns(new arrow7.Table(source.schema, [batch]), columnNames);
|
|
2732
|
+
return (rowIndex) => evaluatePredicate(predicate, columns, rowIndex) === true;
|
|
2733
|
+
},
|
|
2734
|
+
options
|
|
2735
|
+
);
|
|
2736
|
+
}
|
|
2737
|
+
function filterArrowRows(source, selectBatch, options = {}) {
|
|
2738
|
+
var _a, _b, _c;
|
|
2739
|
+
let remaining = options.limit ?? Infinity;
|
|
2740
|
+
if (remaining !== Infinity && (!Number.isSafeInteger(remaining) || remaining < 0)) {
|
|
2741
|
+
throw new Error("Arrow filter limit must be a non-negative safe integer.");
|
|
2742
|
+
}
|
|
2743
|
+
(_a = options.signal) == null ? void 0 : _a.throwIfAborted();
|
|
2744
|
+
const batches = [];
|
|
2745
|
+
for (const batch of source.batches) {
|
|
2746
|
+
if (remaining === 0) break;
|
|
2747
|
+
(_b = options.signal) == null ? void 0 : _b.throwIfAborted();
|
|
2748
|
+
const data = new arrow7.Table(source.schema, [batch]);
|
|
2749
|
+
const selectRow = selectBatch(batch);
|
|
2750
|
+
const indices = [];
|
|
2751
|
+
for (let rowIndex = 0; rowIndex < batch.numRows && indices.length < remaining; rowIndex++) {
|
|
2752
|
+
(_c = options.signal) == null ? void 0 : _c.throwIfAborted();
|
|
2753
|
+
if (selectRow(rowIndex)) indices.push(rowIndex);
|
|
2754
|
+
}
|
|
2755
|
+
remaining -= indices.length;
|
|
2756
|
+
if (indices.length === 0) continue;
|
|
2757
|
+
if (indices.length === batch.numRows) {
|
|
2758
|
+
batches.push(batch);
|
|
2759
|
+
continue;
|
|
2760
|
+
}
|
|
2761
|
+
if (indices[indices.length - 1] - indices[0] + 1 === indices.length) {
|
|
2762
|
+
batches.push(batch.slice(indices[0], indices[indices.length - 1] + 1));
|
|
2763
|
+
continue;
|
|
2764
|
+
}
|
|
2765
|
+
const vectors = Object.fromEntries(
|
|
2766
|
+
source.schema.fields.map((field) => {
|
|
2767
|
+
const vector = data.getChild(field.name);
|
|
2768
|
+
return [
|
|
2769
|
+
field.name,
|
|
2770
|
+
arrow7.vectorFromArray(
|
|
2771
|
+
indices.map((index) => vector.get(index)),
|
|
2772
|
+
field.type
|
|
2773
|
+
)
|
|
2774
|
+
];
|
|
2775
|
+
})
|
|
2776
|
+
);
|
|
2777
|
+
const gathered = new arrow7.Table(source.schema, vectors);
|
|
2778
|
+
batches.push(
|
|
2779
|
+
...gathered.batches.map(
|
|
2780
|
+
(output) => new arrow7.RecordBatch(source.schema, output.data, batch.metadata)
|
|
2781
|
+
)
|
|
2782
|
+
);
|
|
2783
|
+
}
|
|
2784
|
+
return new arrow7.Table(source.schema, batches);
|
|
2785
|
+
}
|
|
2786
|
+
function getPredicateColumns(sourceTable, columnNames) {
|
|
2787
|
+
const columns = {};
|
|
2788
|
+
for (const columnName of columnNames) {
|
|
2789
|
+
const column = sourceTable.getChild(columnName);
|
|
2790
|
+
if (!column) {
|
|
2791
|
+
throw new Error(`Arrow query column not found: ${columnName}`);
|
|
2792
|
+
}
|
|
2793
|
+
columns[columnName] = column;
|
|
2794
|
+
}
|
|
2795
|
+
return columns;
|
|
2796
|
+
}
|
|
2797
|
+
function evaluatePredicate(predicate, columns, rowIndex) {
|
|
2798
|
+
if (isLogicalPredicate(predicate)) {
|
|
2799
|
+
const values = predicate.args.map((child) => evaluatePredicate(child, columns, rowIndex));
|
|
2800
|
+
return predicate.op === "and" ? evaluateAnd(values) : evaluateOr(values);
|
|
2801
|
+
}
|
|
2802
|
+
if (isNotPredicate(predicate)) {
|
|
2803
|
+
const value2 = evaluatePredicate(predicate.args[0], columns, rowIndex);
|
|
2804
|
+
return value2 === null ? null : !value2;
|
|
2805
|
+
}
|
|
2806
|
+
const value = getColumnValue(columns, predicate.args[0].property, rowIndex);
|
|
2807
|
+
if (isNullPredicate(predicate)) {
|
|
2808
|
+
return value === null || value === void 0;
|
|
2809
|
+
}
|
|
2810
|
+
if (isInPredicate(predicate)) {
|
|
2811
|
+
return evaluateIn(value, predicate.args[1]);
|
|
2812
|
+
}
|
|
2813
|
+
return evaluateComparison(value, predicate);
|
|
2814
|
+
}
|
|
2815
|
+
function getColumnValue(columns, columnName, rowIndex) {
|
|
2816
|
+
const path = typeof columnName === "string" ? [columnName] : columnName;
|
|
2817
|
+
const column = columns[path[0]];
|
|
2818
|
+
if (!column) {
|
|
2819
|
+
throw new Error(`Arrow query could not read predicate column "${columnName}".`);
|
|
2820
|
+
}
|
|
2821
|
+
let value = column.get(rowIndex);
|
|
2822
|
+
for (const property of path.slice(1)) {
|
|
2823
|
+
if (value === null || value === void 0) return null;
|
|
2824
|
+
value = value[property];
|
|
2825
|
+
}
|
|
2826
|
+
return value;
|
|
2827
|
+
}
|
|
2828
|
+
function evaluateAnd(values) {
|
|
2829
|
+
if (values.includes(false)) {
|
|
2830
|
+
return false;
|
|
2831
|
+
}
|
|
2832
|
+
return values.includes(null) ? null : true;
|
|
2833
|
+
}
|
|
2834
|
+
function evaluateOr(values) {
|
|
2835
|
+
if (values.includes(true)) {
|
|
2836
|
+
return true;
|
|
2837
|
+
}
|
|
2838
|
+
return values.includes(null) ? null : false;
|
|
2839
|
+
}
|
|
2840
|
+
function evaluateIn(value, candidates) {
|
|
2841
|
+
if (value === null || value === void 0) {
|
|
2842
|
+
return null;
|
|
2843
|
+
}
|
|
2844
|
+
for (const candidate of candidates) {
|
|
2845
|
+
if (compareValues(value, candidate) === 0) {
|
|
2846
|
+
return true;
|
|
2847
|
+
}
|
|
2848
|
+
}
|
|
2849
|
+
return false;
|
|
2850
|
+
}
|
|
2851
|
+
function evaluateComparison(value, predicate) {
|
|
2852
|
+
if (value === null || value === void 0) {
|
|
2853
|
+
return null;
|
|
2854
|
+
}
|
|
2855
|
+
const comparison = compareValues(value, predicate.args[1]);
|
|
2856
|
+
switch (predicate.op) {
|
|
2857
|
+
case "=":
|
|
2858
|
+
return comparison === 0;
|
|
2859
|
+
case "<>":
|
|
2860
|
+
return comparison !== 0;
|
|
2861
|
+
case "<":
|
|
2862
|
+
return comparison < 0;
|
|
2863
|
+
case "<=":
|
|
2864
|
+
return comparison <= 0;
|
|
2865
|
+
case ">":
|
|
2866
|
+
return comparison > 0;
|
|
2867
|
+
case ">=":
|
|
2868
|
+
return comparison >= 0;
|
|
2869
|
+
}
|
|
2870
|
+
}
|
|
2871
|
+
function compareValues(left, right) {
|
|
2872
|
+
if (isColumnarPredicateParameter(right)) {
|
|
2873
|
+
throw new Error(
|
|
2874
|
+
`Arrow query parameter ":${right.parameter}" must be bound before query execution.`
|
|
2875
|
+
);
|
|
2876
|
+
}
|
|
2877
|
+
if (left instanceof Date && right instanceof Date) {
|
|
2878
|
+
return compareArrowQueryNumbers(left.getTime(), right.getTime());
|
|
2879
|
+
}
|
|
2880
|
+
if (typeof left === "number" && right instanceof Date) {
|
|
2881
|
+
return compareArrowQueryNumbers(left, right.getTime());
|
|
2882
|
+
}
|
|
2883
|
+
if (left instanceof Uint8Array && right instanceof Uint8Array) {
|
|
2884
|
+
return compareBinary(left, right);
|
|
2885
|
+
}
|
|
2886
|
+
if (typeof left === "bigint" && typeof right === "bigint") {
|
|
2887
|
+
return left === right ? 0 : left < right ? -1 : 1;
|
|
2888
|
+
}
|
|
2889
|
+
if (typeof left === "bigint" && typeof right === "number" || typeof left === "number" && typeof right === "bigint") {
|
|
2890
|
+
const numericValue = typeof left === "number" ? left : right;
|
|
2891
|
+
if (!Number.isFinite(numericValue))
|
|
2892
|
+
throw new Error("Arrow query cannot compare non-finite numbers.");
|
|
2893
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
2894
|
+
}
|
|
2895
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
2896
|
+
return compareArrowQueryNumbers(left, right);
|
|
2897
|
+
}
|
|
2898
|
+
if (typeof left === "string" && typeof right === "string") {
|
|
2899
|
+
return left === right ? 0 : left < right ? -1 : 1;
|
|
2900
|
+
}
|
|
2901
|
+
if (typeof left === "boolean" && typeof right === "boolean") {
|
|
2902
|
+
return Number(left) - Number(right);
|
|
2903
|
+
}
|
|
2904
|
+
throw new Error(
|
|
2905
|
+
`Arrow query cannot compare ${getArrowQueryValueType(left)} with ${getArrowQueryValueType(right)} values.`
|
|
2906
|
+
);
|
|
2907
|
+
}
|
|
2908
|
+
function compareArrowQueryNumbers(left, right) {
|
|
2909
|
+
if (!Number.isFinite(left) || !Number.isFinite(right)) {
|
|
2910
|
+
throw new Error("Arrow query cannot compare non-finite numbers.");
|
|
2911
|
+
}
|
|
2912
|
+
return left === right ? 0 : left < right ? -1 : 1;
|
|
2913
|
+
}
|
|
2914
|
+
function compareBinary(left, right) {
|
|
2915
|
+
const commonLength = Math.min(left.length, right.length);
|
|
2916
|
+
for (let index = 0; index < commonLength; index++) {
|
|
2917
|
+
if (left[index] !== right[index]) {
|
|
2918
|
+
return left[index] < right[index] ? -1 : 1;
|
|
2919
|
+
}
|
|
2920
|
+
}
|
|
2921
|
+
return left.length === right.length ? 0 : left.length < right.length ? -1 : 1;
|
|
2922
|
+
}
|
|
2923
|
+
function getArrowQueryValueType(value) {
|
|
2924
|
+
if (value instanceof Date) {
|
|
2925
|
+
return "Date";
|
|
2926
|
+
}
|
|
2927
|
+
if (value instanceof Uint8Array) {
|
|
2928
|
+
return "Uint8Array";
|
|
2929
|
+
}
|
|
2930
|
+
return typeof value;
|
|
2931
|
+
}
|
|
2932
|
+
function isLogicalPredicate(predicate) {
|
|
2933
|
+
return predicate.op === "and" || predicate.op === "or";
|
|
2934
|
+
}
|
|
2935
|
+
function isNotPredicate(predicate) {
|
|
2936
|
+
return predicate.op === "not";
|
|
2937
|
+
}
|
|
2938
|
+
function isNullPredicate(predicate) {
|
|
2939
|
+
return predicate.op === "isNull";
|
|
2940
|
+
}
|
|
2941
|
+
function isInPredicate(predicate) {
|
|
2942
|
+
return predicate.op === "in";
|
|
2943
|
+
}
|
|
2621
2944
|
|
|
2622
2945
|
// ../core/src/scan/table-query.ts
|
|
2623
2946
|
function planTableQuery2(sourceColumnNames, options = {}) {
|
|
@@ -2642,6 +2965,31 @@ function getSQLPredicateColumnNames(predicate) {
|
|
|
2642
2965
|
|
|
2643
2966
|
// ../core/src/scan/query-arrow-table.ts
|
|
2644
2967
|
function explainArrowTableQuery(sourceTable, options = {}) {
|
|
2968
|
+
if (options.select !== void 0) {
|
|
2969
|
+
if (options.columns !== void 0 || hasRelationalOperators(options) || options.join) {
|
|
2970
|
+
throw new Error("Arrow select cannot be combined with columns or relational operators.");
|
|
2971
|
+
}
|
|
2972
|
+
const projected = projectArrowTable(sourceTable.data, options.select);
|
|
2973
|
+
const columns = [
|
|
2974
|
+
...new Set(
|
|
2975
|
+
options.select.map(
|
|
2976
|
+
(selection) => typeof selection === "string" ? selection : selection.column
|
|
2977
|
+
)
|
|
2978
|
+
)
|
|
2979
|
+
];
|
|
2980
|
+
const { select, ...remainingOptions } = options;
|
|
2981
|
+
const explanation = explainArrowTableQuery(sourceTable, { ...remainingOptions, columns });
|
|
2982
|
+
const outputColumns = Object.freeze(projected.schema.fields.map((field) => field.name));
|
|
2983
|
+
return Object.freeze({
|
|
2984
|
+
...explanation,
|
|
2985
|
+
outputColumns,
|
|
2986
|
+
plan: Object.freeze(
|
|
2987
|
+
explanation.plan.map(
|
|
2988
|
+
(step) => step.kind === "project" ? Object.freeze({ ...step, columns: outputColumns, select }) : step
|
|
2989
|
+
)
|
|
2990
|
+
)
|
|
2991
|
+
});
|
|
2992
|
+
}
|
|
2645
2993
|
if (options.predicate) {
|
|
2646
2994
|
validateSQLPredicate(options.predicate);
|
|
2647
2995
|
}
|
|
@@ -2654,6 +3002,22 @@ function explainArrowTableQuery(sourceTable, options = {}) {
|
|
|
2654
3002
|
function queryArrowTable(sourceTable, options = {}) {
|
|
2655
3003
|
var _a, _b;
|
|
2656
3004
|
throwIfAborted(options.signal);
|
|
3005
|
+
if (options.select !== void 0) {
|
|
3006
|
+
if (options.columns !== void 0 || hasRelationalOperators(options) || options.join) {
|
|
3007
|
+
throw new Error("Arrow select cannot be combined with columns or relational operators.");
|
|
3008
|
+
}
|
|
3009
|
+
projectArrowTable(sourceTable.data, options.select);
|
|
3010
|
+
const columns = [
|
|
3011
|
+
...new Set(
|
|
3012
|
+
options.select.map(
|
|
3013
|
+
(selection) => typeof selection === "string" ? selection : selection.column
|
|
3014
|
+
)
|
|
3015
|
+
)
|
|
3016
|
+
];
|
|
3017
|
+
const { select, ...remainingOptions } = options;
|
|
3018
|
+
const result = queryArrowTable(sourceTable, { ...remainingOptions, columns });
|
|
3019
|
+
return wrapArrowTable(projectArrowTable(result.data, select));
|
|
3020
|
+
}
|
|
2657
3021
|
if (options.join) {
|
|
2658
3022
|
validateArrowJoinQuery(sourceTable, options);
|
|
2659
3023
|
}
|
|
@@ -2672,33 +3036,16 @@ function queryArrowTable(sourceTable, options = {}) {
|
|
|
2672
3036
|
options
|
|
2673
3037
|
);
|
|
2674
3038
|
const scannedData = sourceData.select([...getScanStep(plan).columns]);
|
|
2675
|
-
const projectedData = scannedData.select([...getProjectStep(plan).columns]);
|
|
2676
3039
|
const predicate = (_a = getFilterStep(plan)) == null ? void 0 : _a.predicate;
|
|
2677
3040
|
const limit = ((_b = getLimitStep(plan)) == null ? void 0 : _b.limit) ?? Number.POSITIVE_INFINITY;
|
|
2678
3041
|
if (!predicate) {
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
for (let rowIndex = 0; rowIndex < scannedData.numRows && matchingRowIndices.length < limit; rowIndex++) {
|
|
2684
|
-
throwIfAborted(options.signal);
|
|
2685
|
-
if (evaluatePredicate(predicate, predicateColumns, rowIndex) === true) {
|
|
2686
|
-
matchingRowIndices.push(rowIndex);
|
|
2687
|
-
}
|
|
2688
|
-
}
|
|
2689
|
-
if (matchingRowIndices.length === 0) {
|
|
2690
|
-
return wrapArrowTable(projectedData.slice(0, 0));
|
|
2691
|
-
}
|
|
2692
|
-
const outputColumns = {};
|
|
2693
|
-
for (const field of projectedData.schema.fields) {
|
|
2694
|
-
const sourceColumn = projectedData.getChild(field.name);
|
|
2695
|
-
if (!sourceColumn) {
|
|
2696
|
-
throw new Error(`Arrow query could not read column "${field.name}".`);
|
|
2697
|
-
}
|
|
2698
|
-
const values = matchingRowIndices.map((rowIndex) => sourceColumn.get(rowIndex));
|
|
2699
|
-
outputColumns[field.name] = arrow6.vectorFromArray(values, sourceColumn.type);
|
|
3042
|
+
const projectedData = projectArrowTable(scannedData, getProjectStep(plan).columns);
|
|
3043
|
+
return wrapArrowTable(
|
|
3044
|
+
projectedData.numRows === 0 ? projectedData : projectedData.slice(0, limit)
|
|
3045
|
+
);
|
|
2700
3046
|
}
|
|
2701
|
-
|
|
3047
|
+
const filteredData = filterArrowTable(scannedData, predicate, { limit, signal: options.signal });
|
|
3048
|
+
return wrapArrowTable(projectArrowTable(filteredData, getProjectStep(plan).columns));
|
|
2702
3049
|
}
|
|
2703
3050
|
function queryArrowTableRelational(sourceTable, options) {
|
|
2704
3051
|
var _a, _b, _c, _d, _e, _f;
|
|
@@ -2839,7 +3186,7 @@ function queryArrowJoin(sourceTable, options) {
|
|
|
2839
3186
|
new Map(
|
|
2840
3187
|
rightResult.data.schema.fields.map((field) => [
|
|
2841
3188
|
`${rightPrefix}${field.name}`,
|
|
2842
|
-
new
|
|
3189
|
+
new arrow8.Field(
|
|
2843
3190
|
`${rightPrefix}${field.name}`,
|
|
2844
3191
|
field.type,
|
|
2845
3192
|
field.nullable,
|
|
@@ -2945,15 +3292,15 @@ function compareRows(left, right, orderBy) {
|
|
|
2945
3292
|
}
|
|
2946
3293
|
function compareSortValues(left, right) {
|
|
2947
3294
|
if (left instanceof Date && right instanceof Date)
|
|
2948
|
-
return
|
|
3295
|
+
return compareArrowQueryNumbers(left.getTime(), right.getTime());
|
|
2949
3296
|
if (typeof left === "bigint" && typeof right === "bigint")
|
|
2950
3297
|
return left === right ? 0 : left < right ? -1 : 1;
|
|
2951
|
-
if (typeof left === "number" && typeof right === "number") return
|
|
3298
|
+
if (typeof left === "number" && typeof right === "number") return compareArrowQueryNumbers(left, right);
|
|
2952
3299
|
if (typeof left === "string" && typeof right === "string")
|
|
2953
3300
|
return left === right ? 0 : left < right ? -1 : 1;
|
|
2954
3301
|
if (typeof left === "boolean" && typeof right === "boolean") return Number(left) - Number(right);
|
|
2955
3302
|
throw new Error(
|
|
2956
|
-
`Arrow relational order cannot compare ${
|
|
3303
|
+
`Arrow relational order cannot compare ${getArrowQueryValueType(left)} with ${getArrowQueryValueType(right)}.`
|
|
2957
3304
|
);
|
|
2958
3305
|
}
|
|
2959
3306
|
function aggregateRows(rows, groupBy, aggregates) {
|
|
@@ -2997,33 +3344,33 @@ function createEmptyRelationalResult(sourceData, outputColumns, options, additio
|
|
|
2997
3344
|
if (additionalField) return additionalField;
|
|
2998
3345
|
const expression = expressions.get(columnName);
|
|
2999
3346
|
if (expression)
|
|
3000
|
-
return new
|
|
3347
|
+
return new arrow8.Field(columnName, getExpressionType(expression, sourceFields), true);
|
|
3001
3348
|
const aggregate = aggregates.get(columnName);
|
|
3002
3349
|
if (aggregate)
|
|
3003
|
-
return new
|
|
3004
|
-
return new
|
|
3350
|
+
return new arrow8.Field(columnName, getAggregateType(aggregate, sourceFields), true);
|
|
3351
|
+
return new arrow8.Field(columnName, new arrow8.Utf8(), true);
|
|
3005
3352
|
});
|
|
3006
3353
|
const columns = {};
|
|
3007
|
-
for (const field of fields) columns[field.name] =
|
|
3008
|
-
return wrapArrowTable(new
|
|
3354
|
+
for (const field of fields) columns[field.name] = arrow8.vectorFromArray([], field.type);
|
|
3355
|
+
return wrapArrowTable(new arrow8.Table(new arrow8.Schema(fields), columns));
|
|
3009
3356
|
}
|
|
3010
3357
|
function getExpressionType(expression, sourceFields) {
|
|
3011
3358
|
var _a;
|
|
3012
3359
|
if (expression.expression.op === "literal") {
|
|
3013
3360
|
const value = expression.expression.value;
|
|
3014
|
-
if (typeof value === "boolean") return new
|
|
3015
|
-
if (typeof value === "number") return new
|
|
3016
|
-
return new
|
|
3361
|
+
if (typeof value === "boolean") return new arrow8.Bool();
|
|
3362
|
+
if (typeof value === "number") return new arrow8.Float64();
|
|
3363
|
+
return new arrow8.Utf8();
|
|
3017
3364
|
}
|
|
3018
3365
|
if (expression.expression.op === "column") {
|
|
3019
|
-
return ((_a = sourceFields.get(expression.expression.column)) == null ? void 0 : _a.type) || new
|
|
3366
|
+
return ((_a = sourceFields.get(expression.expression.column)) == null ? void 0 : _a.type) || new arrow8.Utf8();
|
|
3020
3367
|
}
|
|
3021
|
-
return new
|
|
3368
|
+
return new arrow8.Float64();
|
|
3022
3369
|
}
|
|
3023
3370
|
function getAggregateType(aggregate, sourceFields) {
|
|
3024
3371
|
var _a;
|
|
3025
|
-
if (aggregate.function === "count") return new
|
|
3026
|
-
return aggregate.column ? ((_a = sourceFields.get(aggregate.column)) == null ? void 0 : _a.type) || new
|
|
3372
|
+
if (aggregate.function === "count") return new arrow8.Int32();
|
|
3373
|
+
return aggregate.column ? ((_a = sourceFields.get(aggregate.column)) == null ? void 0 : _a.type) || new arrow8.Float64() : new arrow8.Float64();
|
|
3027
3374
|
}
|
|
3028
3375
|
function evaluateAggregate(aggregate, rows) {
|
|
3029
3376
|
if (aggregate.function === "count")
|
|
@@ -3044,158 +3391,6 @@ function evaluateAggregate(aggregate, rows) {
|
|
|
3044
3391
|
return Math.max(...numericValues);
|
|
3045
3392
|
}
|
|
3046
3393
|
}
|
|
3047
|
-
function getPredicateColumns(sourceTable, columnNames) {
|
|
3048
|
-
const columns = {};
|
|
3049
|
-
for (const columnName of columnNames) {
|
|
3050
|
-
const column = sourceTable.getChild(columnName);
|
|
3051
|
-
if (!column) {
|
|
3052
|
-
throw new Error(`Arrow query column not found: ${columnName}`);
|
|
3053
|
-
}
|
|
3054
|
-
columns[columnName] = column;
|
|
3055
|
-
}
|
|
3056
|
-
return columns;
|
|
3057
|
-
}
|
|
3058
|
-
function evaluatePredicate(predicate, columns, rowIndex) {
|
|
3059
|
-
if (isLogicalPredicate(predicate)) {
|
|
3060
|
-
const values = predicate.args.map((child) => evaluatePredicate(child, columns, rowIndex));
|
|
3061
|
-
return predicate.op === "and" ? evaluateAnd(values) : evaluateOr(values);
|
|
3062
|
-
}
|
|
3063
|
-
if (isNotPredicate(predicate)) {
|
|
3064
|
-
const value2 = evaluatePredicate(predicate.args[0], columns, rowIndex);
|
|
3065
|
-
return value2 === null ? null : !value2;
|
|
3066
|
-
}
|
|
3067
|
-
const value = getColumnValue(columns, predicate.args[0].property, rowIndex);
|
|
3068
|
-
if (isNullPredicate(predicate)) {
|
|
3069
|
-
return value === null;
|
|
3070
|
-
}
|
|
3071
|
-
if (isInPredicate(predicate)) {
|
|
3072
|
-
return evaluateIn(value, predicate.args[1]);
|
|
3073
|
-
}
|
|
3074
|
-
return evaluateComparison(value, predicate);
|
|
3075
|
-
}
|
|
3076
|
-
function getColumnValue(columns, columnName, rowIndex) {
|
|
3077
|
-
const column = columns[columnName];
|
|
3078
|
-
if (!column) {
|
|
3079
|
-
throw new Error(`Arrow query could not read predicate column "${columnName}".`);
|
|
3080
|
-
}
|
|
3081
|
-
return column.get(rowIndex);
|
|
3082
|
-
}
|
|
3083
|
-
function evaluateAnd(values) {
|
|
3084
|
-
if (values.includes(false)) {
|
|
3085
|
-
return false;
|
|
3086
|
-
}
|
|
3087
|
-
return values.includes(null) ? null : true;
|
|
3088
|
-
}
|
|
3089
|
-
function evaluateOr(values) {
|
|
3090
|
-
if (values.includes(true)) {
|
|
3091
|
-
return true;
|
|
3092
|
-
}
|
|
3093
|
-
return values.includes(null) ? null : false;
|
|
3094
|
-
}
|
|
3095
|
-
function evaluateIn(value, candidates) {
|
|
3096
|
-
if (value === null || value === void 0) {
|
|
3097
|
-
return null;
|
|
3098
|
-
}
|
|
3099
|
-
for (const candidate of candidates) {
|
|
3100
|
-
if (compareValues(value, candidate) === 0) {
|
|
3101
|
-
return true;
|
|
3102
|
-
}
|
|
3103
|
-
}
|
|
3104
|
-
return false;
|
|
3105
|
-
}
|
|
3106
|
-
function evaluateComparison(value, predicate) {
|
|
3107
|
-
if (value === null || value === void 0) {
|
|
3108
|
-
return null;
|
|
3109
|
-
}
|
|
3110
|
-
const comparison = compareValues(value, predicate.args[1]);
|
|
3111
|
-
switch (predicate.op) {
|
|
3112
|
-
case "=":
|
|
3113
|
-
return comparison === 0;
|
|
3114
|
-
case "<>":
|
|
3115
|
-
return comparison !== 0;
|
|
3116
|
-
case "<":
|
|
3117
|
-
return comparison < 0;
|
|
3118
|
-
case "<=":
|
|
3119
|
-
return comparison <= 0;
|
|
3120
|
-
case ">":
|
|
3121
|
-
return comparison > 0;
|
|
3122
|
-
case ">=":
|
|
3123
|
-
return comparison >= 0;
|
|
3124
|
-
}
|
|
3125
|
-
}
|
|
3126
|
-
function compareValues(left, right) {
|
|
3127
|
-
if (isSQLPredicateParameter(right)) {
|
|
3128
|
-
throw new Error(
|
|
3129
|
-
`Arrow query parameter ":${right.parameter}" must be bound before query execution.`
|
|
3130
|
-
);
|
|
3131
|
-
}
|
|
3132
|
-
if (left instanceof Date && right instanceof Date) {
|
|
3133
|
-
return compareNumbers(left.getTime(), right.getTime());
|
|
3134
|
-
}
|
|
3135
|
-
if (typeof left === "number" && right instanceof Date) {
|
|
3136
|
-
return compareNumbers(left, right.getTime());
|
|
3137
|
-
}
|
|
3138
|
-
if (left instanceof Uint8Array && right instanceof Uint8Array) {
|
|
3139
|
-
return compareBinary(left, right);
|
|
3140
|
-
}
|
|
3141
|
-
if (typeof left === "bigint" && typeof right === "bigint") {
|
|
3142
|
-
return left === right ? 0 : left < right ? -1 : 1;
|
|
3143
|
-
}
|
|
3144
|
-
if (typeof left === "bigint" && typeof right === "number" && Number.isSafeInteger(right)) {
|
|
3145
|
-
return compareValues(left, BigInt(right));
|
|
3146
|
-
}
|
|
3147
|
-
if (typeof left === "number" && typeof right === "bigint" && Number.isSafeInteger(left)) {
|
|
3148
|
-
return compareValues(BigInt(left), right);
|
|
3149
|
-
}
|
|
3150
|
-
if (typeof left === "number" && typeof right === "number") {
|
|
3151
|
-
return compareNumbers(left, right);
|
|
3152
|
-
}
|
|
3153
|
-
if (typeof left === "string" && typeof right === "string") {
|
|
3154
|
-
return left === right ? 0 : left < right ? -1 : 1;
|
|
3155
|
-
}
|
|
3156
|
-
if (typeof left === "boolean" && typeof right === "boolean") {
|
|
3157
|
-
return Number(left) - Number(right);
|
|
3158
|
-
}
|
|
3159
|
-
throw new Error(
|
|
3160
|
-
`Arrow query cannot compare ${getValueType(left)} with ${getValueType(right)} values.`
|
|
3161
|
-
);
|
|
3162
|
-
}
|
|
3163
|
-
function compareNumbers(left, right) {
|
|
3164
|
-
if (!Number.isFinite(left) || !Number.isFinite(right)) {
|
|
3165
|
-
throw new Error("Arrow query cannot compare non-finite numbers.");
|
|
3166
|
-
}
|
|
3167
|
-
return left === right ? 0 : left < right ? -1 : 1;
|
|
3168
|
-
}
|
|
3169
|
-
function compareBinary(left, right) {
|
|
3170
|
-
const commonLength = Math.min(left.length, right.length);
|
|
3171
|
-
for (let index = 0; index < commonLength; index++) {
|
|
3172
|
-
if (left[index] !== right[index]) {
|
|
3173
|
-
return left[index] < right[index] ? -1 : 1;
|
|
3174
|
-
}
|
|
3175
|
-
}
|
|
3176
|
-
return left.length === right.length ? 0 : left.length < right.length ? -1 : 1;
|
|
3177
|
-
}
|
|
3178
|
-
function getValueType(value) {
|
|
3179
|
-
if (value instanceof Date) {
|
|
3180
|
-
return "Date";
|
|
3181
|
-
}
|
|
3182
|
-
if (value instanceof Uint8Array) {
|
|
3183
|
-
return "Uint8Array";
|
|
3184
|
-
}
|
|
3185
|
-
return typeof value;
|
|
3186
|
-
}
|
|
3187
|
-
function isLogicalPredicate(predicate) {
|
|
3188
|
-
return predicate.op === "and" || predicate.op === "or";
|
|
3189
|
-
}
|
|
3190
|
-
function isNotPredicate(predicate) {
|
|
3191
|
-
return predicate.op === "not";
|
|
3192
|
-
}
|
|
3193
|
-
function isNullPredicate(predicate) {
|
|
3194
|
-
return predicate.op === "isNull";
|
|
3195
|
-
}
|
|
3196
|
-
function isInPredicate(predicate) {
|
|
3197
|
-
return predicate.op === "in";
|
|
3198
|
-
}
|
|
3199
3394
|
function getScanStep(plan) {
|
|
3200
3395
|
const step = plan.find((candidate) => candidate.kind === "scan");
|
|
3201
3396
|
if (!step) {
|
|
@@ -3291,7 +3486,7 @@ function throwIfAborted2(signal) {
|
|
|
3291
3486
|
|
|
3292
3487
|
// dist/duckdb-sql-source.js
|
|
3293
3488
|
var import_apache_arrow = require("apache-arrow");
|
|
3294
|
-
var VERSION2 = true ? "5.0.0-beta.
|
|
3489
|
+
var VERSION2 = true ? "5.0.0-beta.7" : "latest";
|
|
3295
3490
|
var DuckDBSQLSource = {
|
|
3296
3491
|
dataType: null,
|
|
3297
3492
|
batchType: null,
|
|
@@ -3608,7 +3803,7 @@ function getDuckDBDatabasePath(url, options) {
|
|
|
3608
3803
|
}
|
|
3609
3804
|
|
|
3610
3805
|
// dist/snowflake-sql-source.js
|
|
3611
|
-
var VERSION3 = true ? "5.0.0-beta.
|
|
3806
|
+
var VERSION3 = true ? "5.0.0-beta.7" : "latest";
|
|
3612
3807
|
var SnowflakeSQLSource = {
|
|
3613
3808
|
dataType: null,
|
|
3614
3809
|
batchType: null,
|
|
@@ -3814,304 +4009,12 @@ function getSnowflakeBaseUrl(url, options) {
|
|
|
3814
4009
|
}
|
|
3815
4010
|
|
|
3816
4011
|
// ../core/src/scan/parse-sql-predicate.ts
|
|
3817
|
-
var
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
function parseSQLPredicate(source, options = {}) {
|
|
3821
|
-
const parser = new SQLPredicateParser(
|
|
3822
|
-
source,
|
|
3823
|
-
options.parameters ?? {},
|
|
3824
|
-
options.preserveParameters === true
|
|
3825
|
-
);
|
|
3826
|
-
const predicate = parser.parse();
|
|
4012
|
+
var import_sql = require("@math.gl/core/sql");
|
|
4013
|
+
function parseSQLPredicate(source, options) {
|
|
4014
|
+
const predicate = options ? (0, import_sql.parseSQLPredicate)(source, options) : (0, import_sql.parseSQLPredicate)(source);
|
|
3827
4015
|
validateSQLPredicate(predicate);
|
|
3828
4016
|
return predicate;
|
|
3829
4017
|
}
|
|
3830
|
-
var SQLPredicateParser = class {
|
|
3831
|
-
/** Token stream parsed by this parser instance. */
|
|
3832
|
-
tokens;
|
|
3833
|
-
/** Named scalar parameters available to the expression. */
|
|
3834
|
-
parameters;
|
|
3835
|
-
/** Whether named parameters remain references in the parsed AST. */
|
|
3836
|
-
preserveParameters;
|
|
3837
|
-
/** Current index in the token stream. */
|
|
3838
|
-
position = 0;
|
|
3839
|
-
constructor(source, parameters, preserveParameters) {
|
|
3840
|
-
this.tokens = tokenizeSQLPredicate(source);
|
|
3841
|
-
this.parameters = parameters;
|
|
3842
|
-
this.preserveParameters = preserveParameters;
|
|
3843
|
-
}
|
|
3844
|
-
/** Parses the complete token stream. */
|
|
3845
|
-
parse() {
|
|
3846
|
-
const predicate = this.parseOr(0);
|
|
3847
|
-
this.takeSymbol(";");
|
|
3848
|
-
const token = this.peek();
|
|
3849
|
-
if (token) {
|
|
3850
|
-
throw new Error(`SQL predicate contains unexpected token ${JSON.stringify(token.value)}`);
|
|
3851
|
-
}
|
|
3852
|
-
return predicate;
|
|
3853
|
-
}
|
|
3854
|
-
/** Parses left-associative disjunction. */
|
|
3855
|
-
parseOr(depth) {
|
|
3856
|
-
let predicate = this.parseAnd(depth + 1);
|
|
3857
|
-
while (this.takeKeyword("OR")) {
|
|
3858
|
-
predicate = combineLogicalPredicate("or", predicate, this.parseAnd(depth + 1));
|
|
3859
|
-
}
|
|
3860
|
-
return predicate;
|
|
3861
|
-
}
|
|
3862
|
-
/** Parses left-associative conjunction. */
|
|
3863
|
-
parseAnd(depth) {
|
|
3864
|
-
let predicate = this.parseNot(depth + 1);
|
|
3865
|
-
while (this.takeKeyword("AND")) {
|
|
3866
|
-
predicate = combineLogicalPredicate("and", predicate, this.parseNot(depth + 1));
|
|
3867
|
-
}
|
|
3868
|
-
return predicate;
|
|
3869
|
-
}
|
|
3870
|
-
/** Parses boolean negation and parenthesized predicates. */
|
|
3871
|
-
parseNot(depth) {
|
|
3872
|
-
this.assertDepth(depth);
|
|
3873
|
-
if (this.takeKeyword("NOT")) {
|
|
3874
|
-
return { op: "not", args: [this.parseNot(depth + 1)] };
|
|
3875
|
-
}
|
|
3876
|
-
if (this.takeSymbol("(")) {
|
|
3877
|
-
const predicate = this.parseOr(depth + 1);
|
|
3878
|
-
this.expectSymbol(")");
|
|
3879
|
-
return predicate;
|
|
3880
|
-
}
|
|
3881
|
-
return this.parseLeaf();
|
|
3882
|
-
}
|
|
3883
|
-
/** Parses one comparison, membership, or null predicate. */
|
|
3884
|
-
parseLeaf() {
|
|
3885
|
-
var _a, _b;
|
|
3886
|
-
const property = this.parseProperty();
|
|
3887
|
-
if (this.takeKeyword("IS")) {
|
|
3888
|
-
const negated = this.takeKeyword("NOT");
|
|
3889
|
-
this.expectKeyword("NULL");
|
|
3890
|
-
const predicate = { op: "isNull", args: [property] };
|
|
3891
|
-
return negated ? { op: "not", args: [predicate] } : predicate;
|
|
3892
|
-
}
|
|
3893
|
-
const negatedMembership = this.takeKeyword("NOT");
|
|
3894
|
-
if (negatedMembership || this.peekKeyword("IN")) {
|
|
3895
|
-
this.expectKeyword("IN");
|
|
3896
|
-
this.expectSymbol("(");
|
|
3897
|
-
if (((_a = this.peek()) == null ? void 0 : _a.kind) === "symbol" && ((_b = this.peek()) == null ? void 0 : _b.value) === ")") {
|
|
3898
|
-
throw new Error("SQL predicate IN requires at least one scalar value");
|
|
3899
|
-
}
|
|
3900
|
-
const values = [this.parseValue()];
|
|
3901
|
-
while (this.takeSymbol(",")) {
|
|
3902
|
-
values.push(this.parseValue());
|
|
3903
|
-
}
|
|
3904
|
-
this.expectSymbol(")");
|
|
3905
|
-
const predicate = { op: "in", args: [property, values] };
|
|
3906
|
-
return negatedMembership ? { op: "not", args: [predicate] } : predicate;
|
|
3907
|
-
}
|
|
3908
|
-
const token = this.take();
|
|
3909
|
-
if ((token == null ? void 0 : token.kind) !== "symbol" || !["=", "!=", "<>", "<", "<=", ">", ">="].includes(token.value)) {
|
|
3910
|
-
throw new Error("SQL predicate expected a comparison, IN, or IS NULL operator");
|
|
3911
|
-
}
|
|
3912
|
-
const operator = token.value === "!=" ? "<>" : token.value;
|
|
3913
|
-
return {
|
|
3914
|
-
op: operator,
|
|
3915
|
-
args: [property, this.parseValue()]
|
|
3916
|
-
};
|
|
3917
|
-
}
|
|
3918
|
-
/** Parses an identifier into a property reference. */
|
|
3919
|
-
parseProperty() {
|
|
3920
|
-
const token = this.take();
|
|
3921
|
-
if ((token == null ? void 0 : token.kind) !== "identifier") {
|
|
3922
|
-
throw new Error("SQL predicate expected a column identifier");
|
|
3923
|
-
}
|
|
3924
|
-
return token.quoted ? { property: token.value, quoted: true } : { property: token.value };
|
|
3925
|
-
}
|
|
3926
|
-
/** Parses one scalar literal or named parameter. */
|
|
3927
|
-
parseValue() {
|
|
3928
|
-
const negative = this.takeSymbol("-");
|
|
3929
|
-
const token = this.take();
|
|
3930
|
-
if (!token) {
|
|
3931
|
-
throw new Error("SQL predicate expected a scalar value");
|
|
3932
|
-
}
|
|
3933
|
-
if (negative && token.kind !== "number") {
|
|
3934
|
-
throw new Error("SQL predicate only permits unary minus on numeric literals");
|
|
3935
|
-
}
|
|
3936
|
-
if (token.kind === "number") {
|
|
3937
|
-
return parseNumericLiteral(negative ? `-${token.value}` : token.value);
|
|
3938
|
-
}
|
|
3939
|
-
if (negative) {
|
|
3940
|
-
throw new Error("SQL predicate expected a numeric literal after unary minus");
|
|
3941
|
-
}
|
|
3942
|
-
if (token.kind === "string") {
|
|
3943
|
-
return token.value;
|
|
3944
|
-
}
|
|
3945
|
-
if (token.kind === "parameter") {
|
|
3946
|
-
if (this.preserveParameters) {
|
|
3947
|
-
return { parameter: token.value };
|
|
3948
|
-
}
|
|
3949
|
-
if (!Object.hasOwn(this.parameters, token.value)) {
|
|
3950
|
-
throw new Error(`SQL predicate parameter ":${token.value}" requires a value`);
|
|
3951
|
-
}
|
|
3952
|
-
const value = this.parameters[token.value];
|
|
3953
|
-
validateParameterValue(value, token.value);
|
|
3954
|
-
return value;
|
|
3955
|
-
}
|
|
3956
|
-
if (token.kind === "identifier" && /^(TRUE|FALSE)$/i.test(token.value)) {
|
|
3957
|
-
return /^TRUE$/i.test(token.value);
|
|
3958
|
-
}
|
|
3959
|
-
if (token.kind === "identifier" && /^NULL$/i.test(token.value)) {
|
|
3960
|
-
throw new Error("SQL predicate comparisons with NULL must use IS NULL or IS NOT NULL");
|
|
3961
|
-
}
|
|
3962
|
-
throw new Error(`SQL predicate does not support scalar token ${JSON.stringify(token.value)}`);
|
|
3963
|
-
}
|
|
3964
|
-
/** Rejects excessively nested inputs before recursion can exhaust the stack. */
|
|
3965
|
-
assertDepth(depth) {
|
|
3966
|
-
if (depth > MAXIMUM_EXPRESSION_DEPTH) {
|
|
3967
|
-
throw new Error("SQL predicate nesting exceeds its safe limit");
|
|
3968
|
-
}
|
|
3969
|
-
}
|
|
3970
|
-
/** Consumes one required keyword. */
|
|
3971
|
-
expectKeyword(keyword) {
|
|
3972
|
-
if (!this.takeKeyword(keyword)) {
|
|
3973
|
-
throw new Error(`SQL predicate expected ${keyword}`);
|
|
3974
|
-
}
|
|
3975
|
-
}
|
|
3976
|
-
/** Returns whether the next token is a keyword without consuming it. */
|
|
3977
|
-
peekKeyword(keyword) {
|
|
3978
|
-
const token = this.peek();
|
|
3979
|
-
return (token == null ? void 0 : token.kind) === "identifier" && token.value.toUpperCase() === keyword;
|
|
3980
|
-
}
|
|
3981
|
-
/** Consumes one optional keyword. */
|
|
3982
|
-
takeKeyword(keyword) {
|
|
3983
|
-
if (!this.peekKeyword(keyword)) {
|
|
3984
|
-
return false;
|
|
3985
|
-
}
|
|
3986
|
-
this.position++;
|
|
3987
|
-
return true;
|
|
3988
|
-
}
|
|
3989
|
-
/** Consumes one required symbol. */
|
|
3990
|
-
expectSymbol(symbol) {
|
|
3991
|
-
if (!this.takeSymbol(symbol)) {
|
|
3992
|
-
throw new Error(`SQL predicate expected ${JSON.stringify(symbol)}`);
|
|
3993
|
-
}
|
|
3994
|
-
}
|
|
3995
|
-
/** Consumes one optional symbol. */
|
|
3996
|
-
takeSymbol(symbol) {
|
|
3997
|
-
const token = this.peek();
|
|
3998
|
-
if ((token == null ? void 0 : token.kind) !== "symbol" || token.value !== symbol) {
|
|
3999
|
-
return false;
|
|
4000
|
-
}
|
|
4001
|
-
this.position++;
|
|
4002
|
-
return true;
|
|
4003
|
-
}
|
|
4004
|
-
/** Consumes and returns the next token. */
|
|
4005
|
-
take() {
|
|
4006
|
-
const token = this.tokens[this.position];
|
|
4007
|
-
if (token) {
|
|
4008
|
-
this.position++;
|
|
4009
|
-
}
|
|
4010
|
-
return token;
|
|
4011
|
-
}
|
|
4012
|
-
/** Returns the next token without consuming it. */
|
|
4013
|
-
peek() {
|
|
4014
|
-
return this.tokens[this.position];
|
|
4015
|
-
}
|
|
4016
|
-
};
|
|
4017
|
-
function combineLogicalPredicate(operator, left, right) {
|
|
4018
|
-
return left.op === operator ? { op: operator, args: [...left.args, right] } : { op: operator, args: [left, right] };
|
|
4019
|
-
}
|
|
4020
|
-
function parseNumericLiteral(source) {
|
|
4021
|
-
if (/^-?\d+$/.test(source)) {
|
|
4022
|
-
const bigint = BigInt(source);
|
|
4023
|
-
if (bigint > BigInt(Number.MAX_SAFE_INTEGER) || bigint < BigInt(Number.MIN_SAFE_INTEGER)) {
|
|
4024
|
-
return bigint;
|
|
4025
|
-
}
|
|
4026
|
-
}
|
|
4027
|
-
const number = Number(source);
|
|
4028
|
-
if (!Number.isFinite(number)) {
|
|
4029
|
-
throw new Error("SQL predicate numeric literals must be finite");
|
|
4030
|
-
}
|
|
4031
|
-
return number;
|
|
4032
|
-
}
|
|
4033
|
-
function validateParameterValue(value, name) {
|
|
4034
|
-
const probe = { op: "=", args: [{ property: "_" }, value] };
|
|
4035
|
-
try {
|
|
4036
|
-
validateSQLPredicate(probe);
|
|
4037
|
-
} catch {
|
|
4038
|
-
throw new Error(`SQL predicate parameter ":${name}" has an unsupported value`);
|
|
4039
|
-
}
|
|
4040
|
-
}
|
|
4041
|
-
function tokenizeSQLPredicate(source) {
|
|
4042
|
-
if (typeof source !== "string" || source.trim().length === 0 || source.length > MAXIMUM_SQL_LENGTH) {
|
|
4043
|
-
throw new Error("SQL predicate requires a non-empty, safely bounded expression");
|
|
4044
|
-
}
|
|
4045
|
-
const tokens = [];
|
|
4046
|
-
let position = 0;
|
|
4047
|
-
while (position < source.length) {
|
|
4048
|
-
const character = source[position];
|
|
4049
|
-
if (/\s/.test(character)) {
|
|
4050
|
-
position++;
|
|
4051
|
-
continue;
|
|
4052
|
-
}
|
|
4053
|
-
if (character === "'") {
|
|
4054
|
-
const result = readQuotedToken(source, position, "'", "'");
|
|
4055
|
-
tokens.push({ kind: "string", value: result.value });
|
|
4056
|
-
position = result.position;
|
|
4057
|
-
} else if (character === '"') {
|
|
4058
|
-
const result = readQuotedToken(source, position, '"', '"');
|
|
4059
|
-
tokens.push({ kind: "identifier", value: result.value, quoted: true });
|
|
4060
|
-
position = result.position;
|
|
4061
|
-
} else if (character === ":") {
|
|
4062
|
-
const match = /^[A-Za-z_][A-Za-z0-9_]*/.exec(source.slice(position + 1));
|
|
4063
|
-
if (!match) {
|
|
4064
|
-
throw new Error(`SQL predicate contains an invalid parameter at character ${position}`);
|
|
4065
|
-
}
|
|
4066
|
-
tokens.push({ kind: "parameter", value: match[0] });
|
|
4067
|
-
position += match[0].length + 1;
|
|
4068
|
-
} else {
|
|
4069
|
-
const numberMatch = /^(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/.exec(source.slice(position));
|
|
4070
|
-
const identifierMatch = /^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*/.exec(
|
|
4071
|
-
source.slice(position)
|
|
4072
|
-
);
|
|
4073
|
-
if (numberMatch) {
|
|
4074
|
-
tokens.push({ kind: "number", value: numberMatch[0] });
|
|
4075
|
-
position += numberMatch[0].length;
|
|
4076
|
-
} else if (identifierMatch) {
|
|
4077
|
-
tokens.push({ kind: "identifier", value: identifierMatch[0] });
|
|
4078
|
-
position += identifierMatch[0].length;
|
|
4079
|
-
} else {
|
|
4080
|
-
const twoCharacters = source.slice(position, position + 2);
|
|
4081
|
-
if (["<=", ">=", "<>", "!="].includes(twoCharacters)) {
|
|
4082
|
-
tokens.push({ kind: "symbol", value: twoCharacters });
|
|
4083
|
-
position += 2;
|
|
4084
|
-
} else if ("(),;-=<>".includes(character)) {
|
|
4085
|
-
tokens.push({ kind: "symbol", value: character });
|
|
4086
|
-
position++;
|
|
4087
|
-
} else {
|
|
4088
|
-
throw new Error(`SQL predicate contains an unsupported token at character ${position}`);
|
|
4089
|
-
}
|
|
4090
|
-
}
|
|
4091
|
-
}
|
|
4092
|
-
if (tokens.length > MAXIMUM_SQL_TOKENS) {
|
|
4093
|
-
throw new Error("SQL predicate exceeds its safe token limit");
|
|
4094
|
-
}
|
|
4095
|
-
}
|
|
4096
|
-
return tokens;
|
|
4097
|
-
}
|
|
4098
|
-
function readQuotedToken(source, start, quote, escapedQuote) {
|
|
4099
|
-
let value = "";
|
|
4100
|
-
let position = start + 1;
|
|
4101
|
-
while (position < source.length) {
|
|
4102
|
-
if (source[position] !== quote) {
|
|
4103
|
-
value += source[position++];
|
|
4104
|
-
continue;
|
|
4105
|
-
}
|
|
4106
|
-
if (source[position + 1] === escapedQuote) {
|
|
4107
|
-
value += quote;
|
|
4108
|
-
position += 2;
|
|
4109
|
-
continue;
|
|
4110
|
-
}
|
|
4111
|
-
return { value, position: position + 1 };
|
|
4112
|
-
}
|
|
4113
|
-
throw new Error(`SQL predicate contains an unterminated quoted value at character ${start}`);
|
|
4114
|
-
}
|
|
4115
4018
|
|
|
4116
4019
|
// ../core/src/scan/bind-sql-predicate.ts
|
|
4117
4020
|
function bindSQLPredicate(predicate, parameters) {
|