@loaders.gl/sql 5.0.0-beta.5 → 5.0.0-beta.6

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/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,256 @@ 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
+ var _a, _b, _c;
2727
+ validateColumnarPredicate(predicate, new Set(source.schema.fields.map((field) => field.name)));
2728
+ let remaining = options.limit ?? Infinity;
2729
+ if (remaining !== Infinity && (!Number.isSafeInteger(remaining) || remaining < 0)) {
2730
+ throw new Error("Arrow filter limit must be a non-negative safe integer.");
2731
+ }
2732
+ (_a = options.signal) == null ? void 0 : _a.throwIfAborted();
2733
+ const batches = [];
2734
+ for (const batch of source.batches) {
2735
+ if (remaining === 0) break;
2736
+ (_b = options.signal) == null ? void 0 : _b.throwIfAborted();
2737
+ const data = new arrow7.Table(source.schema, [batch]);
2738
+ const columns = getPredicateColumns(data, getColumnarPredicateColumns(predicate));
2739
+ const indices = [];
2740
+ for (let rowIndex = 0; rowIndex < batch.numRows && indices.length < remaining; rowIndex++) {
2741
+ (_c = options.signal) == null ? void 0 : _c.throwIfAborted();
2742
+ if (evaluatePredicate(predicate, columns, rowIndex) === true)
2743
+ indices.push(rowIndex);
2744
+ }
2745
+ remaining -= indices.length;
2746
+ if (indices.length === 0) continue;
2747
+ if (indices.length === batch.numRows) {
2748
+ batches.push(batch);
2749
+ continue;
2750
+ }
2751
+ if (indices[indices.length - 1] - indices[0] + 1 === indices.length) {
2752
+ batches.push(batch.slice(indices[0], indices[indices.length - 1] + 1));
2753
+ continue;
2754
+ }
2755
+ const vectors = Object.fromEntries(
2756
+ source.schema.fields.map((field) => {
2757
+ const vector = data.getChild(field.name);
2758
+ return [
2759
+ field.name,
2760
+ arrow7.vectorFromArray(
2761
+ indices.map((index) => vector.get(index)),
2762
+ field.type
2763
+ )
2764
+ ];
2765
+ })
2766
+ );
2767
+ const gathered = new arrow7.Table(source.schema, vectors);
2768
+ batches.push(
2769
+ ...gathered.batches.map(
2770
+ (output) => new arrow7.RecordBatch(source.schema, output.data, batch.metadata)
2771
+ )
2772
+ );
2773
+ }
2774
+ return new arrow7.Table(source.schema, batches);
2775
+ }
2776
+ function getPredicateColumns(sourceTable, columnNames) {
2777
+ const columns = {};
2778
+ for (const columnName of columnNames) {
2779
+ const column = sourceTable.getChild(columnName);
2780
+ if (!column) {
2781
+ throw new Error(`Arrow query column not found: ${columnName}`);
2782
+ }
2783
+ columns[columnName] = column;
2784
+ }
2785
+ return columns;
2786
+ }
2787
+ function evaluatePredicate(predicate, columns, rowIndex) {
2788
+ if (isLogicalPredicate(predicate)) {
2789
+ const values = predicate.args.map((child) => evaluatePredicate(child, columns, rowIndex));
2790
+ return predicate.op === "and" ? evaluateAnd(values) : evaluateOr(values);
2791
+ }
2792
+ if (isNotPredicate(predicate)) {
2793
+ const value2 = evaluatePredicate(predicate.args[0], columns, rowIndex);
2794
+ return value2 === null ? null : !value2;
2795
+ }
2796
+ const value = getColumnValue(columns, predicate.args[0].property, rowIndex);
2797
+ if (isNullPredicate(predicate)) {
2798
+ return value === null || value === void 0;
2799
+ }
2800
+ if (isInPredicate(predicate)) {
2801
+ return evaluateIn(value, predicate.args[1]);
2802
+ }
2803
+ return evaluateComparison(value, predicate);
2804
+ }
2805
+ function getColumnValue(columns, columnName, rowIndex) {
2806
+ const path = typeof columnName === "string" ? [columnName] : columnName;
2807
+ const column = columns[path[0]];
2808
+ if (!column) {
2809
+ throw new Error(`Arrow query could not read predicate column "${columnName}".`);
2810
+ }
2811
+ let value = column.get(rowIndex);
2812
+ for (const property of path.slice(1)) {
2813
+ if (value === null || value === void 0) return null;
2814
+ value = value[property];
2815
+ }
2816
+ return value;
2817
+ }
2818
+ function evaluateAnd(values) {
2819
+ if (values.includes(false)) {
2820
+ return false;
2821
+ }
2822
+ return values.includes(null) ? null : true;
2823
+ }
2824
+ function evaluateOr(values) {
2825
+ if (values.includes(true)) {
2826
+ return true;
2827
+ }
2828
+ return values.includes(null) ? null : false;
2829
+ }
2830
+ function evaluateIn(value, candidates) {
2831
+ if (value === null || value === void 0) {
2832
+ return null;
2833
+ }
2834
+ for (const candidate of candidates) {
2835
+ if (compareValues(value, candidate) === 0) {
2836
+ return true;
2837
+ }
2838
+ }
2839
+ return false;
2840
+ }
2841
+ function evaluateComparison(value, predicate) {
2842
+ if (value === null || value === void 0) {
2843
+ return null;
2844
+ }
2845
+ const comparison = compareValues(value, predicate.args[1]);
2846
+ switch (predicate.op) {
2847
+ case "=":
2848
+ return comparison === 0;
2849
+ case "<>":
2850
+ return comparison !== 0;
2851
+ case "<":
2852
+ return comparison < 0;
2853
+ case "<=":
2854
+ return comparison <= 0;
2855
+ case ">":
2856
+ return comparison > 0;
2857
+ case ">=":
2858
+ return comparison >= 0;
2859
+ }
2860
+ }
2861
+ function compareValues(left, right) {
2862
+ if (isColumnarPredicateParameter(right)) {
2863
+ throw new Error(
2864
+ `Arrow query parameter ":${right.parameter}" must be bound before query execution.`
2865
+ );
2866
+ }
2867
+ if (left instanceof Date && right instanceof Date) {
2868
+ return compareArrowQueryNumbers(left.getTime(), right.getTime());
2869
+ }
2870
+ if (typeof left === "number" && right instanceof Date) {
2871
+ return compareArrowQueryNumbers(left, right.getTime());
2872
+ }
2873
+ if (left instanceof Uint8Array && right instanceof Uint8Array) {
2874
+ return compareBinary(left, right);
2875
+ }
2876
+ if (typeof left === "bigint" && typeof right === "bigint") {
2877
+ return left === right ? 0 : left < right ? -1 : 1;
2878
+ }
2879
+ if (typeof left === "bigint" && typeof right === "number" || typeof left === "number" && typeof right === "bigint") {
2880
+ const numericValue = typeof left === "number" ? left : right;
2881
+ if (!Number.isFinite(numericValue))
2882
+ throw new Error("Arrow query cannot compare non-finite numbers.");
2883
+ return left < right ? -1 : left > right ? 1 : 0;
2884
+ }
2885
+ if (typeof left === "number" && typeof right === "number") {
2886
+ return compareArrowQueryNumbers(left, right);
2887
+ }
2888
+ if (typeof left === "string" && typeof right === "string") {
2889
+ return left === right ? 0 : left < right ? -1 : 1;
2890
+ }
2891
+ if (typeof left === "boolean" && typeof right === "boolean") {
2892
+ return Number(left) - Number(right);
2893
+ }
2894
+ throw new Error(
2895
+ `Arrow query cannot compare ${getArrowQueryValueType(left)} with ${getArrowQueryValueType(right)} values.`
2896
+ );
2897
+ }
2898
+ function compareArrowQueryNumbers(left, right) {
2899
+ if (!Number.isFinite(left) || !Number.isFinite(right)) {
2900
+ throw new Error("Arrow query cannot compare non-finite numbers.");
2901
+ }
2902
+ return left === right ? 0 : left < right ? -1 : 1;
2903
+ }
2904
+ function compareBinary(left, right) {
2905
+ const commonLength = Math.min(left.length, right.length);
2906
+ for (let index = 0; index < commonLength; index++) {
2907
+ if (left[index] !== right[index]) {
2908
+ return left[index] < right[index] ? -1 : 1;
2909
+ }
2910
+ }
2911
+ return left.length === right.length ? 0 : left.length < right.length ? -1 : 1;
2912
+ }
2913
+ function getArrowQueryValueType(value) {
2914
+ if (value instanceof Date) {
2915
+ return "Date";
2916
+ }
2917
+ if (value instanceof Uint8Array) {
2918
+ return "Uint8Array";
2919
+ }
2920
+ return typeof value;
2921
+ }
2922
+ function isLogicalPredicate(predicate) {
2923
+ return predicate.op === "and" || predicate.op === "or";
2924
+ }
2925
+ function isNotPredicate(predicate) {
2926
+ return predicate.op === "not";
2927
+ }
2928
+ function isNullPredicate(predicate) {
2929
+ return predicate.op === "isNull";
2930
+ }
2931
+ function isInPredicate(predicate) {
2932
+ return predicate.op === "in";
2933
+ }
2621
2934
 
2622
2935
  // ../core/src/scan/table-query.ts
2623
2936
  function planTableQuery2(sourceColumnNames, options = {}) {
@@ -2642,6 +2955,31 @@ function getSQLPredicateColumnNames(predicate) {
2642
2955
 
2643
2956
  // ../core/src/scan/query-arrow-table.ts
2644
2957
  function explainArrowTableQuery(sourceTable, options = {}) {
2958
+ if (options.select !== void 0) {
2959
+ if (options.columns !== void 0 || hasRelationalOperators(options) || options.join) {
2960
+ throw new Error("Arrow select cannot be combined with columns or relational operators.");
2961
+ }
2962
+ const projected = projectArrowTable(sourceTable.data, options.select);
2963
+ const columns = [
2964
+ ...new Set(
2965
+ options.select.map(
2966
+ (selection) => typeof selection === "string" ? selection : selection.column
2967
+ )
2968
+ )
2969
+ ];
2970
+ const { select, ...remainingOptions } = options;
2971
+ const explanation = explainArrowTableQuery(sourceTable, { ...remainingOptions, columns });
2972
+ const outputColumns = Object.freeze(projected.schema.fields.map((field) => field.name));
2973
+ return Object.freeze({
2974
+ ...explanation,
2975
+ outputColumns,
2976
+ plan: Object.freeze(
2977
+ explanation.plan.map(
2978
+ (step) => step.kind === "project" ? Object.freeze({ ...step, columns: outputColumns, select }) : step
2979
+ )
2980
+ )
2981
+ });
2982
+ }
2645
2983
  if (options.predicate) {
2646
2984
  validateSQLPredicate(options.predicate);
2647
2985
  }
@@ -2654,6 +2992,22 @@ function explainArrowTableQuery(sourceTable, options = {}) {
2654
2992
  function queryArrowTable(sourceTable, options = {}) {
2655
2993
  var _a, _b;
2656
2994
  throwIfAborted(options.signal);
2995
+ if (options.select !== void 0) {
2996
+ if (options.columns !== void 0 || hasRelationalOperators(options) || options.join) {
2997
+ throw new Error("Arrow select cannot be combined with columns or relational operators.");
2998
+ }
2999
+ projectArrowTable(sourceTable.data, options.select);
3000
+ const columns = [
3001
+ ...new Set(
3002
+ options.select.map(
3003
+ (selection) => typeof selection === "string" ? selection : selection.column
3004
+ )
3005
+ )
3006
+ ];
3007
+ const { select, ...remainingOptions } = options;
3008
+ const result = queryArrowTable(sourceTable, { ...remainingOptions, columns });
3009
+ return wrapArrowTable(projectArrowTable(result.data, select));
3010
+ }
2657
3011
  if (options.join) {
2658
3012
  validateArrowJoinQuery(sourceTable, options);
2659
3013
  }
@@ -2672,33 +3026,16 @@ function queryArrowTable(sourceTable, options = {}) {
2672
3026
  options
2673
3027
  );
2674
3028
  const scannedData = sourceData.select([...getScanStep(plan).columns]);
2675
- const projectedData = scannedData.select([...getProjectStep(plan).columns]);
2676
3029
  const predicate = (_a = getFilterStep(plan)) == null ? void 0 : _a.predicate;
2677
3030
  const limit = ((_b = getLimitStep(plan)) == null ? void 0 : _b.limit) ?? Number.POSITIVE_INFINITY;
2678
3031
  if (!predicate) {
2679
- return wrapArrowTable(projectedData.slice(0, limit));
2680
- }
2681
- const predicateColumns = getPredicateColumns(scannedData, getSQLPredicateColumnNames(predicate));
2682
- const matchingRowIndices = [];
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);
3032
+ const projectedData = projectArrowTable(scannedData, getProjectStep(plan).columns);
3033
+ return wrapArrowTable(
3034
+ projectedData.numRows === 0 ? projectedData : projectedData.slice(0, limit)
3035
+ );
2700
3036
  }
2701
- return wrapArrowTable(new arrow6.Table(projectedData.schema, outputColumns));
3037
+ const filteredData = filterArrowTable(scannedData, predicate, { limit, signal: options.signal });
3038
+ return wrapArrowTable(projectArrowTable(filteredData, getProjectStep(plan).columns));
2702
3039
  }
2703
3040
  function queryArrowTableRelational(sourceTable, options) {
2704
3041
  var _a, _b, _c, _d, _e, _f;
@@ -2839,7 +3176,7 @@ function queryArrowJoin(sourceTable, options) {
2839
3176
  new Map(
2840
3177
  rightResult.data.schema.fields.map((field) => [
2841
3178
  `${rightPrefix}${field.name}`,
2842
- new arrow6.Field(
3179
+ new arrow8.Field(
2843
3180
  `${rightPrefix}${field.name}`,
2844
3181
  field.type,
2845
3182
  field.nullable,
@@ -2945,15 +3282,15 @@ function compareRows(left, right, orderBy) {
2945
3282
  }
2946
3283
  function compareSortValues(left, right) {
2947
3284
  if (left instanceof Date && right instanceof Date)
2948
- return compareNumbers(left.getTime(), right.getTime());
3285
+ return compareArrowQueryNumbers(left.getTime(), right.getTime());
2949
3286
  if (typeof left === "bigint" && typeof right === "bigint")
2950
3287
  return left === right ? 0 : left < right ? -1 : 1;
2951
- if (typeof left === "number" && typeof right === "number") return compareNumbers(left, right);
3288
+ if (typeof left === "number" && typeof right === "number") return compareArrowQueryNumbers(left, right);
2952
3289
  if (typeof left === "string" && typeof right === "string")
2953
3290
  return left === right ? 0 : left < right ? -1 : 1;
2954
3291
  if (typeof left === "boolean" && typeof right === "boolean") return Number(left) - Number(right);
2955
3292
  throw new Error(
2956
- `Arrow relational order cannot compare ${getValueType(left)} with ${getValueType(right)}.`
3293
+ `Arrow relational order cannot compare ${getArrowQueryValueType(left)} with ${getArrowQueryValueType(right)}.`
2957
3294
  );
2958
3295
  }
2959
3296
  function aggregateRows(rows, groupBy, aggregates) {
@@ -2997,33 +3334,33 @@ function createEmptyRelationalResult(sourceData, outputColumns, options, additio
2997
3334
  if (additionalField) return additionalField;
2998
3335
  const expression = expressions.get(columnName);
2999
3336
  if (expression)
3000
- return new arrow6.Field(columnName, getExpressionType(expression, sourceFields), true);
3337
+ return new arrow8.Field(columnName, getExpressionType(expression, sourceFields), true);
3001
3338
  const aggregate = aggregates.get(columnName);
3002
3339
  if (aggregate)
3003
- return new arrow6.Field(columnName, getAggregateType(aggregate, sourceFields), true);
3004
- return new arrow6.Field(columnName, new arrow6.Utf8(), true);
3340
+ return new arrow8.Field(columnName, getAggregateType(aggregate, sourceFields), true);
3341
+ return new arrow8.Field(columnName, new arrow8.Utf8(), true);
3005
3342
  });
3006
3343
  const columns = {};
3007
- for (const field of fields) columns[field.name] = arrow6.vectorFromArray([], field.type);
3008
- return wrapArrowTable(new arrow6.Table(new arrow6.Schema(fields), columns));
3344
+ for (const field of fields) columns[field.name] = arrow8.vectorFromArray([], field.type);
3345
+ return wrapArrowTable(new arrow8.Table(new arrow8.Schema(fields), columns));
3009
3346
  }
3010
3347
  function getExpressionType(expression, sourceFields) {
3011
3348
  var _a;
3012
3349
  if (expression.expression.op === "literal") {
3013
3350
  const value = expression.expression.value;
3014
- if (typeof value === "boolean") return new arrow6.Bool();
3015
- if (typeof value === "number") return new arrow6.Float64();
3016
- return new arrow6.Utf8();
3351
+ if (typeof value === "boolean") return new arrow8.Bool();
3352
+ if (typeof value === "number") return new arrow8.Float64();
3353
+ return new arrow8.Utf8();
3017
3354
  }
3018
3355
  if (expression.expression.op === "column") {
3019
- return ((_a = sourceFields.get(expression.expression.column)) == null ? void 0 : _a.type) || new arrow6.Utf8();
3356
+ return ((_a = sourceFields.get(expression.expression.column)) == null ? void 0 : _a.type) || new arrow8.Utf8();
3020
3357
  }
3021
- return new arrow6.Float64();
3358
+ return new arrow8.Float64();
3022
3359
  }
3023
3360
  function getAggregateType(aggregate, sourceFields) {
3024
3361
  var _a;
3025
- if (aggregate.function === "count") return new arrow6.Int32();
3026
- return aggregate.column ? ((_a = sourceFields.get(aggregate.column)) == null ? void 0 : _a.type) || new arrow6.Float64() : new arrow6.Float64();
3362
+ if (aggregate.function === "count") return new arrow8.Int32();
3363
+ return aggregate.column ? ((_a = sourceFields.get(aggregate.column)) == null ? void 0 : _a.type) || new arrow8.Float64() : new arrow8.Float64();
3027
3364
  }
3028
3365
  function evaluateAggregate(aggregate, rows) {
3029
3366
  if (aggregate.function === "count")
@@ -3044,158 +3381,6 @@ function evaluateAggregate(aggregate, rows) {
3044
3381
  return Math.max(...numericValues);
3045
3382
  }
3046
3383
  }
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
3384
  function getScanStep(plan) {
3200
3385
  const step = plan.find((candidate) => candidate.kind === "scan");
3201
3386
  if (!step) {
@@ -3291,7 +3476,7 @@ function throwIfAborted2(signal) {
3291
3476
 
3292
3477
  // dist/duckdb-sql-source.js
3293
3478
  var import_apache_arrow = require("apache-arrow");
3294
- var VERSION2 = true ? "5.0.0-beta.5" : "latest";
3479
+ var VERSION2 = true ? "5.0.0-beta.6" : "latest";
3295
3480
  var DuckDBSQLSource = {
3296
3481
  dataType: null,
3297
3482
  batchType: null,
@@ -3608,7 +3793,7 @@ function getDuckDBDatabasePath(url, options) {
3608
3793
  }
3609
3794
 
3610
3795
  // dist/snowflake-sql-source.js
3611
- var VERSION3 = true ? "5.0.0-beta.5" : "latest";
3796
+ var VERSION3 = true ? "5.0.0-beta.6" : "latest";
3612
3797
  var SnowflakeSQLSource = {
3613
3798
  dataType: null,
3614
3799
  batchType: null,
@@ -3953,10 +4138,10 @@ var SQLPredicateParser = class {
3953
4138
  validateParameterValue(value, token.value);
3954
4139
  return value;
3955
4140
  }
3956
- if (token.kind === "identifier" && /^(TRUE|FALSE)$/i.test(token.value)) {
4141
+ if (token.kind === "identifier" && !token.quoted && /^(TRUE|FALSE)$/i.test(token.value)) {
3957
4142
  return /^TRUE$/i.test(token.value);
3958
4143
  }
3959
- if (token.kind === "identifier" && /^NULL$/i.test(token.value)) {
4144
+ if (token.kind === "identifier" && !token.quoted && /^NULL$/i.test(token.value)) {
3960
4145
  throw new Error("SQL predicate comparisons with NULL must use IS NULL or IS NOT NULL");
3961
4146
  }
3962
4147
  throw new Error(`SQL predicate does not support scalar token ${JSON.stringify(token.value)}`);
@@ -3976,7 +4161,7 @@ var SQLPredicateParser = class {
3976
4161
  /** Returns whether the next token is a keyword without consuming it. */
3977
4162
  peekKeyword(keyword) {
3978
4163
  const token = this.peek();
3979
- return (token == null ? void 0 : token.kind) === "identifier" && token.value.toUpperCase() === keyword;
4164
+ return (token == null ? void 0 : token.kind) === "identifier" && !token.quoted && token.value.toUpperCase() === keyword;
3980
4165
  }
3981
4166
  /** Consumes one optional keyword. */
3982
4167
  takeKeyword(keyword) {