@loaders.gl/sql 5.0.0-beta.1 → 5.0.0-beta.10

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
@@ -69,6 +69,36 @@ function createLog() {
69
69
  return log2;
70
70
  }
71
71
  var log = createLog();
72
+ var NullLog = class {
73
+ /** Discards debug messages. */
74
+ log() {
75
+ return ignoreMessage;
76
+ }
77
+ /** Discards informational messages. */
78
+ info() {
79
+ return ignoreMessage;
80
+ }
81
+ /** Discards messages marked to be emitted once. */
82
+ once() {
83
+ return ignoreMessage;
84
+ }
85
+ /** Discards warnings. */
86
+ warn() {
87
+ return ignoreMessage;
88
+ }
89
+ /** Discards errors. */
90
+ error() {
91
+ return ignoreMessage;
92
+ }
93
+ };
94
+ function ignoreMessage() {
95
+ }
96
+ var nullLog = new NullLog();
97
+ function getLoaderLog(options) {
98
+ var _a;
99
+ const logger = (_a = options == null ? void 0 : options.core) == null ? void 0 : _a.log;
100
+ return logger === null ? nullLog : logger || log;
101
+ }
72
102
 
73
103
  // ../core/src/loader-utils/lib/javascript-utils/is-type.ts
74
104
  var isObject = (value) => value !== null && typeof value === "object";
@@ -226,7 +256,7 @@ function createAuthenticatedFetch(options) {
226
256
  }
227
257
  function getAuthenticatedFetch(options = {}) {
228
258
  var _a, _b, _c;
229
- const fetchOption = options.fetch ?? ((_a = options.core) == null ? void 0 : _a.fetch);
259
+ const fetchOption = (_a = options.core) == null ? void 0 : _a.fetch;
230
260
  let fetchFunction;
231
261
  if (typeof fetchOption === "function") {
232
262
  fetchFunction = fetchOption;
@@ -402,6 +432,8 @@ var DataSource = class _DataSource {
402
432
  options;
403
433
  data;
404
434
  url;
435
+ /** Stable query-visible name, resolved without fetching source data. */
436
+ name;
405
437
  /** Shared parser options derived from the source options, excluding source-only core controls. */
406
438
  loadOptions;
407
439
  /** A resolved fetch function extracted from the shared core options. */
@@ -418,6 +450,7 @@ var DataSource = class _DataSource {
418
450
  );
419
451
  this.data = data;
420
452
  this.url = typeof data === "string" ? resolvePath(data) : "";
453
+ this.name = getDataSourceName(data, this.options);
421
454
  const loadOptions = resolveAuthenticationOptions(getSourceLoaderOptions(this.options));
422
455
  this.loadOptions = loadOptions;
423
456
  const fetch2 = getFetchFunction(loadOptions);
@@ -426,6 +459,9 @@ var DataSource = class _DataSource {
426
459
  this.fetch = fetch2;
427
460
  }
428
461
  setProps(options) {
462
+ var _a;
463
+ if (((_a = options.core) == null ? void 0 : _a.name) !== void 0 && options.core.name !== this.name)
464
+ throw new Error("Data source names are fixed at creation.");
429
465
  const mergedOptions = mergeOptions(this.options, options);
430
466
  const loadOptions = resolveAuthenticationOptions(getSourceLoaderOptions(mergedOptions));
431
467
  this.options = mergedOptions;
@@ -456,7 +492,7 @@ var DataSource = class _DataSource {
456
492
  if (callback) {
457
493
  callback(normalizedError, this);
458
494
  } else {
459
- log.warn(`${this.constructor.name}: ${normalizedError.message}`)();
495
+ getLoaderLog(this.options).warn(`${this.constructor.name}: ${normalizedError.message}`)();
460
496
  }
461
497
  return normalizedError;
462
498
  }
@@ -475,23 +511,13 @@ function getSourceLoaderOptions(options) {
475
511
  if (options == null ? void 0 : options.core) {
476
512
  loadOptions.core = { ...options.core };
477
513
  const parserCore = loadOptions.core;
478
- for (const key of ["type", "attributions", "loaders", "onError", "loadOptions"]) {
514
+ for (const key of ["type", "name", "attributions", "loaders", "onError", "loadOptions"]) {
479
515
  delete parserCore[key];
480
516
  }
481
517
  if (Object.keys(parserCore).length === 0) {
482
518
  delete loadOptions.core;
483
519
  }
484
520
  }
485
- const topLevelBaseUri = typeof loadOptions.baseUri === "string" ? loadOptions.baseUri : void 0;
486
- const topLevelBaseUrl = typeof loadOptions.baseUrl === "string" ? loadOptions.baseUrl : void 0;
487
- if (topLevelBaseUri !== void 0 || topLevelBaseUrl !== void 0) {
488
- loadOptions.core ||= {};
489
- if (loadOptions.core.baseUrl === void 0) {
490
- loadOptions.core.baseUrl = topLevelBaseUrl ?? topLevelBaseUri;
491
- }
492
- delete loadOptions.baseUri;
493
- delete loadOptions.baseUrl;
494
- }
495
521
  return loadOptions;
496
522
  }
497
523
  function normalizeError(error, message) {
@@ -506,10 +532,48 @@ function normalizeError(error, message) {
506
532
  function unavailableCoreApiMethod(methodName) {
507
533
  return () => {
508
534
  throw new Error(
509
- `CoreAPI.${methodName} is unavailable. Use @loaders.gl/core.createDataSource().`
535
+ `CoreAPI.${methodName} is unavailable. Use @loaders.gl/core.createDataSourceSync().`
510
536
  );
511
537
  };
512
538
  }
539
+ function getDataSourceName(data, options) {
540
+ var _a, _b, _c;
541
+ const explicitName = (_a = options.core) == null ? void 0 : _a.name;
542
+ if (explicitName !== void 0) {
543
+ if (typeof explicitName !== "string" || !explicitName.trim())
544
+ throw new Error("Data source name must be a non-empty string.");
545
+ return explicitName;
546
+ }
547
+ if (data && typeof data === "object") {
548
+ const named = data;
549
+ if (typeof named.name === "string" && named.name.trim())
550
+ return data instanceof Blob ? removeFileExtension(named.name) : named.name;
551
+ const metadata = (_b = named.schema) == null ? void 0 : _b.metadata;
552
+ const embeddedName = metadata instanceof Map ? metadata.get("name") : metadata == null ? void 0 : metadata.name;
553
+ if (typeof embeddedName === "string" && embeddedName.trim()) return embeddedName;
554
+ }
555
+ if (typeof data === "string") {
556
+ try {
557
+ const url = new URL(data.replace(/\\/g, "/"), "https://source.invalid/");
558
+ if (!["data:", "blob:"].includes(url.protocol)) {
559
+ let basename = url.pathname.split("/").filter(Boolean).pop();
560
+ if (basename) {
561
+ try {
562
+ basename = decodeURIComponent(basename);
563
+ } catch {
564
+ }
565
+ return removeFileExtension(basename);
566
+ }
567
+ }
568
+ } catch {
569
+ }
570
+ }
571
+ return ((_c = options.core) == null ? void 0 : _c.type) && options.core.type !== "auto" ? options.core.type : "source";
572
+ }
573
+ function removeFileExtension(name) {
574
+ const extension = name.lastIndexOf(".");
575
+ return extension > 0 ? name.slice(0, extension) : name;
576
+ }
513
577
 
514
578
  // ../core/src/loader-utils/lib/scan-utils/columnar-predicate.ts
515
579
  function getColumnarPredicateColumns(predicate) {
@@ -588,6 +652,34 @@ function bindColumnarPredicateParameters(predicate, parameters) {
588
652
  };
589
653
  }
590
654
  }
655
+ function filterColumnarRowIndices(predicate, columns, rowCount) {
656
+ if (!predicate) return Array.from({ length: rowCount }, (_, index) => index);
657
+ const result = [];
658
+ for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
659
+ if (evaluateColumnarPredicate(predicate, columns, rowIndex) === true) result.push(rowIndex);
660
+ }
661
+ return result;
662
+ }
663
+ function evaluateColumnarPredicate(predicate, columns, rowIndex) {
664
+ if (predicate.op === "and" || predicate.op === "or") {
665
+ const results = predicate.args.map(
666
+ (child) => evaluateColumnarPredicate(child, columns, rowIndex)
667
+ );
668
+ return predicate.op === "and" ? results.includes(false) ? false : results.includes(null) ? null : true : results.includes(true) ? true : results.includes(null) ? null : false;
669
+ }
670
+ if (predicate.op === "not") {
671
+ const value2 = evaluateColumnarPredicate(predicate.args[0], columns, rowIndex);
672
+ return value2 === null ? null : !value2;
673
+ }
674
+ const leaf = predicate;
675
+ const value = getColumnarValue(columns, getColumnarPredicatePath(leaf.args[0]), rowIndex);
676
+ if (leaf.op === "isNull") return value === null || value === void 0;
677
+ if (value === null || value === void 0) return null;
678
+ if (leaf.op === "in")
679
+ return leaf.args[1].some((candidate) => compareColumnarValues(value, candidate) === 0);
680
+ const comparison = compareColumnarValues(value, leaf.args[1]);
681
+ return leaf.op === "=" ? comparison === 0 : leaf.op === "<>" ? comparison !== 0 : leaf.op === "<" ? comparison < 0 : leaf.op === "<=" ? comparison <= 0 : leaf.op === ">" ? comparison > 0 : comparison >= 0;
682
+ }
591
683
  function visitColumnarPredicate(predicate, visitor) {
592
684
  visitor(predicate);
593
685
  if (predicate.op === "and" || predicate.op === "or")
@@ -616,14 +708,56 @@ function copyColumnarPredicateProperty(property) {
616
708
  property: typeof property.property === "string" ? property.property : [...property.property]
617
709
  };
618
710
  }
711
+ function getColumnarValue(columns, path, rowIndex) {
712
+ var _a;
713
+ let value = (_a = columns[path[0]]) == null ? void 0 : _a[rowIndex];
714
+ for (let index = 1; index < path.length && value != null; index++) value = value[path[index]];
715
+ return value;
716
+ }
619
717
  function copyColumnarPredicateValue(value) {
620
718
  if (value instanceof Uint8Array) return value.slice();
621
719
  if (value instanceof Date) return new Date(value.getTime());
622
720
  return value;
623
721
  }
722
+ function compareColumnarValues(left, right) {
723
+ if (left instanceof Uint8Array && right instanceof Uint8Array) return compareBytes(left, right);
724
+ if (left instanceof Uint8Array || right instanceof Uint8Array)
725
+ throw new Error("Columnar binary predicates require binary values on both sides");
726
+ if (left instanceof Date || right instanceof Date) {
727
+ if (!(left instanceof Date) || !(right instanceof Date))
728
+ throw new Error("Columnar date predicates require date values on both sides");
729
+ return compareNumbers(left.getTime(), right.getTime());
730
+ }
731
+ const leftType = typeof left;
732
+ const rightType = typeof right;
733
+ const numeric = (leftType === "number" || leftType === "bigint") && (rightType === "number" || rightType === "bigint");
734
+ if (!numeric && leftType !== rightType)
735
+ throw new Error(`Columnar predicate cannot compare ${leftType} with ${rightType}`);
736
+ if (!numeric && leftType !== "string" && leftType !== "boolean")
737
+ throw new Error(`Columnar predicate does not support ${leftType} values`);
738
+ return left < right ? -1 : left > right ? 1 : 0;
739
+ }
740
+ function compareNumbers(left, right) {
741
+ if (!Number.isFinite(left) || !Number.isFinite(right))
742
+ throw new Error("Non-finite values cannot be ordered by a columnar predicate");
743
+ return left < right ? -1 : left > right ? 1 : 0;
744
+ }
745
+ function compareBytes(left, right) {
746
+ const length = Math.min(left.length, right.length);
747
+ for (let index = 0; index < length; index++)
748
+ if (left[index] !== right[index]) return left[index] < right[index] ? -1 : 1;
749
+ return left.length - right.length;
750
+ }
624
751
 
625
752
  // ../core/src/loader-utils/lib/scan-utils/scan-query-metadata.ts
626
753
  var import_crs = require("@math.gl/geospatial/crs");
754
+ function emitQueryComplete(callback, summary) {
755
+ if (!callback) return;
756
+ try {
757
+ callback(summary);
758
+ } catch {
759
+ }
760
+ }
627
761
  function createScanQueryMetadata(options) {
628
762
  validateScanExecutionSupport(options.queryType, options.execution);
629
763
  const schema = Object.freeze({
@@ -648,6 +782,7 @@ function createScanQueryMetadata(options) {
648
782
  });
649
783
  }
650
784
  );
785
+ const queryCapabilities = Object.freeze({ ...options.capabilities });
651
786
  return Object.freeze({
652
787
  sourceType: options.sourceType,
653
788
  queryType: options.queryType,
@@ -656,7 +791,8 @@ function createScanQueryMetadata(options) {
656
791
  description: options.description,
657
792
  schema,
658
793
  columns: Object.freeze(columns),
659
- capabilities: Object.freeze({ ...options.capabilities }),
794
+ capabilities: queryCapabilities,
795
+ queryCapabilities,
660
796
  spatial: options.spatial ? Object.freeze({
661
797
  ...options.spatial,
662
798
  bounds: options.spatial.bounds ? Object.freeze({
@@ -699,6 +835,13 @@ function getNonEmptyMetadataValue(metadata, key) {
699
835
  const value = metadata == null ? void 0 : metadata[key];
700
836
  return value || void 0;
701
837
  }
838
+ function createQueryMetadata(options) {
839
+ const { queryCapabilities, ...metadataOptions } = options;
840
+ return createScanQueryMetadata({
841
+ ...metadataOptions,
842
+ capabilities: queryCapabilities
843
+ });
844
+ }
702
845
 
703
846
  // ../core/src/loader-utils/lib/scan-utils/table-query.ts
704
847
  function planTableQuery(sourceColumnNames, options = {}) {
@@ -744,11 +887,172 @@ function validateTableQueryLimit(limit) {
744
887
  throw new Error("Table query limit must be a non-negative safe integer.");
745
888
  }
746
889
  }
890
+ function validateTableQueryOffset(offset) {
891
+ if (offset !== void 0 && (!Number.isSafeInteger(offset) || offset < 0)) {
892
+ throw new Error("Table query offset must be a non-negative safe integer.");
893
+ }
894
+ }
747
895
 
748
896
  // ../core/src/loader-utils/lib/scan-utils/table-scan-batch.ts
749
897
  function makeTableScanBatch(table) {
750
898
  return { ...table, batchType: "data", length: table.data.numRows };
751
899
  }
900
+ async function* executeTableScanBatches(readBatches, options = {}, operators = {}) {
901
+ var _a, _b, _c, _d, _e;
902
+ validateTableQueryLimit(options.limit);
903
+ validateTableQueryOffset(options.offset);
904
+ let skip = options.offset ?? 0;
905
+ const startedAt = Date.now();
906
+ let sourcesRead = 0;
907
+ let batchesRead = 0;
908
+ let rowsRead = 0;
909
+ let rowsTested = 0;
910
+ let rowsRetained = 0;
911
+ let rowsReturned = 0;
912
+ let bytesFetched = 0;
913
+ let status = "early-terminated";
914
+ let earlyTerminationReason;
915
+ let executionError;
916
+ let remaining = options.limit === void 0 ? Number.POSITIVE_INFINITY : Math.max(0, options.limit);
917
+ try {
918
+ (_a = options.signal) == null ? void 0 : _a.throwIfAborted();
919
+ if (remaining <= 0) {
920
+ earlyTerminationReason = "limit";
921
+ return;
922
+ }
923
+ sourcesRead = 1;
924
+ for await (const batch of readBatches(
925
+ options.signal,
926
+ (byteLength) => bytesFetched += byteLength
927
+ )) {
928
+ if (remaining <= 0) {
929
+ earlyTerminationReason = "limit";
930
+ return;
931
+ }
932
+ (_b = options.signal) == null ? void 0 : _b.throwIfAborted();
933
+ batchesRead++;
934
+ rowsRead += batch.length;
935
+ const filteredBatch = await (operators.filter || filterTableBatch)(
936
+ batch,
937
+ options.predicate,
938
+ options.signal
939
+ );
940
+ (_c = options.signal) == null ? void 0 : _c.throwIfAborted();
941
+ if (options.predicate) {
942
+ rowsTested += batch.length;
943
+ rowsRetained += filteredBatch.length;
944
+ }
945
+ if (skip >= filteredBatch.length && (skip > 0 || options.skipEmptyBatches)) {
946
+ skip -= filteredBatch.length;
947
+ continue;
948
+ }
949
+ const end = Math.min(filteredBatch.length, skip + remaining);
950
+ const window = skip || end < filteredBatch.length ? sliceTableBatch(filteredBatch, skip, end) : filteredBatch;
951
+ skip = 0;
952
+ const output = (operators.project || projectTableBatch)(window, options.columns);
953
+ remaining -= window.length;
954
+ rowsReturned += output.length;
955
+ if (remaining === 0) earlyTerminationReason = "limit";
956
+ yield output;
957
+ if (remaining === 0) return;
958
+ (_d = options.signal) == null ? void 0 : _d.throwIfAborted();
959
+ }
960
+ status = "completed";
961
+ } catch (error) {
962
+ status = ((_e = options.signal) == null ? void 0 : _e.aborted) ? "cancelled" : "failed";
963
+ executionError = error;
964
+ throw error;
965
+ } finally {
966
+ if (status === "early-terminated" && !earlyTerminationReason) {
967
+ earlyTerminationReason = "consumer-return";
968
+ }
969
+ emitQueryComplete(
970
+ options.onQueryComplete,
971
+ Object.freeze({
972
+ status,
973
+ sourcesPlanned: 1,
974
+ sourcesRead,
975
+ batchesRead,
976
+ batchesDecoded: batchesRead,
977
+ rowsRead,
978
+ rowsTested: rowsTested || void 0,
979
+ rowsRetained: rowsRetained || void 0,
980
+ rowsReturned,
981
+ bytesRead: bytesFetched,
982
+ bytesFetched,
983
+ filesOpened: sourcesRead,
984
+ tasksOpened: sourcesRead,
985
+ durationMilliseconds: Date.now() - startedAt,
986
+ earlyTerminationReason,
987
+ ...executionError === void 0 ? {} : { error: executionError }
988
+ })
989
+ );
990
+ }
991
+ }
992
+ function sliceTableBatch(batch, start, end) {
993
+ const length = end - start;
994
+ if (batch.shape === "object-row-table" || batch.shape === "array-row-table") {
995
+ return { ...batch, data: batch.data.slice(start, end), length };
996
+ }
997
+ if (batch.shape === "columnar-table") {
998
+ return {
999
+ ...batch,
1000
+ data: Object.fromEntries(
1001
+ Object.entries(batch.data).map(([name, values]) => [
1002
+ name,
1003
+ Array.from(values).slice(start, end)
1004
+ ])
1005
+ ),
1006
+ length
1007
+ };
1008
+ }
1009
+ if (batch.shape === "arrow-table") return { ...batch, data: batch.data.slice(start, end), length };
1010
+ return { ...batch, features: batch.features.slice(start, end), length };
1011
+ }
1012
+ function projectTableBatch(batch, columns) {
1013
+ if (columns === void 0) return batch;
1014
+ const schema = projectTableSchema(batch, columns);
1015
+ if (batch.shape === "object-row-table") {
1016
+ return {
1017
+ ...batch,
1018
+ schema,
1019
+ data: batch.data.map((row) => Object.fromEntries(columns.map((column) => [column, row[column]])))
1020
+ };
1021
+ }
1022
+ if (batch.shape === "columnar-table") {
1023
+ return {
1024
+ ...batch,
1025
+ schema,
1026
+ data: Object.fromEntries(columns.map((column) => [column, batch.data[column]]))
1027
+ };
1028
+ }
1029
+ if (batch.shape === "arrow-table")
1030
+ return { ...batch, schema, data: batch.data.select([...columns]) };
1031
+ return batch;
1032
+ }
1033
+ function projectTableSchema(batch, columns) {
1034
+ if (!batch.schema) return void 0;
1035
+ const fieldsByName = new Map(batch.schema.fields.map((field) => [field.name, field]));
1036
+ return {
1037
+ ...batch.schema,
1038
+ fields: columns.flatMap((column) => {
1039
+ const field = fieldsByName.get(column);
1040
+ return field ? [field] : [];
1041
+ })
1042
+ };
1043
+ }
1044
+ function filterTableBatch(batch, predicate) {
1045
+ var _a;
1046
+ if (!predicate || batch.shape !== "object-row-table") return batch;
1047
+ const rows = batch.data;
1048
+ const columnNames = new Set(((_a = batch.schema) == null ? void 0 : _a.fields.map((field) => field.name)) || []);
1049
+ validateColumnarPredicate(predicate, columnNames);
1050
+ const columns = Object.fromEntries(
1051
+ [...columnNames].map((name) => [name, rows.map((row) => row[name])])
1052
+ );
1053
+ const rowIndices = filterColumnarRowIndices(predicate, columns, rows.length);
1054
+ return { ...batch, data: rowIndices.map((rowIndex) => rows[rowIndex]), length: rowIndices.length };
1055
+ }
752
1056
 
753
1057
  // ../core/src/loader-utils/lib/scan-utils/relational-query.ts
754
1058
  function planRelationalQuery(sourceColumnNames, options = {}) {
@@ -1287,116 +1591,390 @@ function validateSQLIdentifier(identifier) {
1287
1591
  }
1288
1592
 
1289
1593
  // ../core/src/scan/rows-to-arrow.ts
1290
- var arrow5 = __toESM(require("apache-arrow"), 1);
1594
+ var arrow = __toESM(require("apache-arrow"), 1);
1595
+ function convertRowsToArrowTable(rows) {
1596
+ return arrow.tableFromJSON(rows);
1597
+ }
1291
1598
 
1292
- // ../core/src/schema-utils/lib/schema/data-type.ts
1293
- var import_core = require("@math.gl/core");
1294
- function getDataTypeFromValue(value, defaultNumberType = "float32") {
1295
- if (value instanceof Date) {
1296
- return "date-millisecond";
1297
- }
1298
- if (typeof value === "number") {
1299
- return defaultNumberType;
1599
+ // dist/sql-utils.js
1600
+ function isNodeRuntime() {
1601
+ var _a;
1602
+ return typeof process !== "undefined" && Boolean((_a = process.versions) == null ? void 0 : _a.node);
1603
+ }
1604
+ function escapeSqlString(value) {
1605
+ return value.replace(/'/g, "''");
1606
+ }
1607
+ function quoteSqlIdentifier(identifier) {
1608
+ return `"${identifier.replace(/"/g, '""')}"`;
1609
+ }
1610
+ function convertSQLColumnsToSchema(columns) {
1611
+ return {
1612
+ metadata: {},
1613
+ fields: columns.sort((left, right) => (left.ordinalPosition || 0) - (right.ordinalPosition || 0)).map((column) => ({
1614
+ name: column.columnName,
1615
+ type: mapSQLTypeToDataType(column.sqlType),
1616
+ nullable: column.nullable,
1617
+ metadata: {
1618
+ sqlType: column.sqlType
1619
+ }
1620
+ }))
1621
+ };
1622
+ }
1623
+ function mapSQLTypeToDataType(sqlType) {
1624
+ const normalizedType = sqlType.toUpperCase();
1625
+ if (normalizedType.includes("BIGINT") || normalizedType === "HUGEINT" || normalizedType === "LONG") {
1626
+ return "int64";
1300
1627
  }
1301
- if (typeof value === "boolean") {
1302
- return "bool";
1628
+ if (normalizedType.includes("INT") || normalizedType === "INTEGER" || normalizedType === "SMALLINT" || normalizedType === "TINYINT") {
1629
+ return "int32";
1303
1630
  }
1304
- if (typeof value === "string") {
1305
- return "utf8";
1631
+ if (normalizedType.includes("DOUBLE") || normalizedType.includes("FLOAT8") || normalizedType.includes("REAL")) {
1632
+ return "float64";
1306
1633
  }
1307
- if (value === null || value === "undefined") {
1308
- return "null";
1634
+ if (normalizedType.includes("FLOAT") || normalizedType.includes("DECIMAL") || normalizedType.includes("NUMERIC")) {
1635
+ return "float64";
1309
1636
  }
1310
- return "null";
1311
- }
1312
- function getDataTypeFromArray(array) {
1313
- let type = getDataTypeFromTypedArray(array);
1314
- if (type !== "null") {
1315
- return { type, nullable: false };
1637
+ if (normalizedType.includes("BOOL")) {
1638
+ return "bool";
1316
1639
  }
1317
- if (array.length > 0) {
1318
- type = getDataTypeFromValue(array[0]);
1319
- return { type, nullable: true };
1640
+ if (normalizedType.includes("DATE")) {
1641
+ return "date-day";
1320
1642
  }
1321
- return { type: "null", nullable: true };
1322
- }
1323
- function getDataTypeFromTypedArray(array) {
1324
- if (typeof Float16Array !== "undefined" && array instanceof Float16Array) {
1325
- return "float16";
1643
+ if (normalizedType.includes("TIME") || normalizedType.includes("TIMESTAMP")) {
1644
+ return "timestamp-millisecond";
1326
1645
  }
1327
- switch (array.constructor) {
1328
- case Int8Array:
1329
- return "int8";
1330
- case Uint8Array:
1331
- case Uint8ClampedArray:
1332
- return "uint8";
1333
- case Int16Array:
1334
- return "int16";
1335
- case Uint16Array:
1336
- return "uint16";
1337
- case Int32Array:
1338
- return "int32";
1339
- case Uint32Array:
1340
- return "uint32";
1341
- case BigInt64Array:
1342
- return "int64";
1343
- case BigUint64Array:
1344
- return "uint64";
1345
- case Float32Array:
1346
- return "float32";
1347
- case Float64Array:
1348
- return "float64";
1349
- default:
1350
- return "null";
1646
+ if (normalizedType.includes("BLOB") || normalizedType.includes("BINARY")) {
1647
+ return "binary";
1351
1648
  }
1649
+ return "utf8";
1352
1650
  }
1353
- function getArrayTypeFromDataType(type, nullable) {
1354
- if (!nullable) {
1355
- switch (type) {
1356
- case "int8":
1357
- return Int8Array;
1358
- case "uint8":
1359
- return Uint8Array;
1360
- case "int16":
1361
- return Int16Array;
1362
- case "uint16":
1363
- return Uint16Array;
1364
- case "int32":
1365
- return Int32Array;
1366
- case "uint32":
1367
- return Uint32Array;
1368
- case "float32":
1369
- return Float32Array;
1370
- case "float64":
1371
- return Float64Array;
1372
- case "float16":
1373
- return (0, import_core.getFloat16ArrayConstructor)();
1374
- default:
1375
- break;
1376
- }
1377
- }
1378
- return Array;
1651
+ function getQualifiedTableName(options) {
1652
+ const identifiers = [options.catalogName, options.schemaName, options.tableName].filter((value) => Boolean(value));
1653
+ return identifiers.map((identifier) => quoteSqlIdentifier(identifier)).join(".");
1379
1654
  }
1380
1655
 
1381
- // ../core/src/schema-utils/lib/schema/convert-arrow-schema.ts
1382
- var arrow = __toESM(require("apache-arrow"), 1);
1383
- function convertArrowToSchema(arrowSchema) {
1384
- return serializeArrowSchema(arrowSchema);
1385
- }
1386
- function convertSchemaToArrow(schema, options) {
1387
- return deserializeArrowSchema(schema, options);
1656
+ // ../core/src/scan/table-query-capabilities.ts
1657
+ var ARROW_TABLE_QUERY_CAPABILITIES = Object.freeze({
1658
+ projection: "residual",
1659
+ predicate: "residual",
1660
+ limit: "residual",
1661
+ streaming: false,
1662
+ cancellation: true,
1663
+ expressions: "residual",
1664
+ orderBy: "residual",
1665
+ aggregates: "residual"
1666
+ });
1667
+ var SQL_DATA_SOURCE_TABLE_QUERY_CAPABILITIES = Object.freeze({
1668
+ projection: "pushdown",
1669
+ predicate: "pushdown",
1670
+ limit: "pushdown",
1671
+ streaming: false,
1672
+ cancellation: false,
1673
+ expressions: "pushdown",
1674
+ orderBy: "pushdown",
1675
+ aggregates: "pushdown"
1676
+ });
1677
+
1678
+ // dist/sql-source.js
1679
+ var SQL_ADAPTER_FACTORIES = /* @__PURE__ */ new Map();
1680
+ function registerSQLAdapter(type, factory) {
1681
+ SQL_ADAPTER_FACTORIES.set(type, factory);
1388
1682
  }
1389
- function serializeArrowSchema(arrowSchema) {
1390
- return {
1391
- fields: arrowSchema.fields.map((arrowField) => serializeArrowField(arrowField)),
1392
- metadata: serializeArrowMetadata(arrowSchema.metadata)
1393
- };
1683
+ function getSQLAdapterFactory(type) {
1684
+ return SQL_ADAPTER_FACTORIES.get(type);
1394
1685
  }
1395
- function deserializeArrowSchema(schema, options) {
1396
- return new arrow.Schema(
1397
- schema.fields.map((field) => deserializeArrowField(field, options)),
1398
- deserializeArrowMetadata(schema.metadata)
1399
- );
1686
+ var SQL_SOURCE_DEFAULT_OPTIONS = {
1687
+ sql: {
1688
+ resultFormat: "auto",
1689
+ adapterOptions: {},
1690
+ browserAdapterFactory: void 0,
1691
+ nodeAdapterFactory: void 0
1692
+ },
1693
+ duckdb: {
1694
+ accessMode: "auto",
1695
+ bundles: void 0,
1696
+ databasePath: void 0,
1697
+ remoteUrl: void 0,
1698
+ workerUrl: void 0
1699
+ },
1700
+ snowflake: {
1701
+ account: void 0,
1702
+ database: void 0,
1703
+ schema: void 0,
1704
+ token: void 0,
1705
+ warehouse: void 0,
1706
+ role: void 0,
1707
+ authenticator: void 0
1708
+ }
1709
+ };
1710
+ var SQLDataSource = class extends DataSource {
1711
+ /** Optimization and execution capabilities of portable table-bound queries. */
1712
+ tableQueryCapabilities = SQL_DATA_SOURCE_TABLE_QUERY_CAPABILITIES;
1713
+ sourceType;
1714
+ adapterFactory;
1715
+ adapterPromise = null;
1716
+ metadataPromise = null;
1717
+ constructor(data, options, sourceType, defaultOptions = SQL_SOURCE_DEFAULT_OPTIONS, adapterFactory, coreApi) {
1718
+ super(data, options, defaultOptions, coreApi);
1719
+ this.sourceType = sourceType;
1720
+ this.adapterFactory = adapterFactory || getSQLAdapterFactory(sourceType);
1721
+ }
1722
+ /** Returns cached high-level metadata for the SQL source. */
1723
+ async getMetadata() {
1724
+ if (!this.metadataPromise) {
1725
+ this.metadataPromise = this.loadMetadata();
1726
+ }
1727
+ return this.metadataPromise;
1728
+ }
1729
+ /** Lists available catalogs from the connected database. */
1730
+ async listCatalogs() {
1731
+ const adapter = await this.getAdapter();
1732
+ return await adapter.listCatalogs();
1733
+ }
1734
+ /** Lists available schemas from the connected database. */
1735
+ async listSchemas(catalogName) {
1736
+ const adapter = await this.getAdapter();
1737
+ return await adapter.listSchemas(catalogName);
1738
+ }
1739
+ /** Lists available tables from the connected database. */
1740
+ async listTables(options) {
1741
+ const adapter = await this.getAdapter();
1742
+ return await adapter.listTables(options);
1743
+ }
1744
+ /** Lists stable native table identifiers without fetching any column schemas or data rows. */
1745
+ async getTablesMetadata(options = {}) {
1746
+ var _a, _b;
1747
+ (_a = options.signal) == null ? void 0 : _a.throwIfAborted();
1748
+ const tables = await this.listTables();
1749
+ (_b = options.signal) == null ? void 0 : _b.throwIfAborted();
1750
+ return tables.map((table) => ({ name: getTableMetadataName(table), queryable: false }));
1751
+ }
1752
+ /** Discovers one native table schema; ambiguous unqualified names are rejected. */
1753
+ async getTableMetadata(name, options = {}) {
1754
+ var _a;
1755
+ const table = await this.resolveTable(name, options.signal);
1756
+ const schema = await this.getTableSchema(table);
1757
+ (_a = options.signal) == null ? void 0 : _a.throwIfAborted();
1758
+ return { name: getTableMetadataName(table), schema, queryable: false };
1759
+ }
1760
+ /** Resolves a qualified or unique unqualified table name. */
1761
+ async resolveTable(name, signal) {
1762
+ signal == null ? void 0 : signal.throwIfAborted();
1763
+ const tables = await this.listTables();
1764
+ signal == null ? void 0 : signal.throwIfAborted();
1765
+ const exact = tables.filter((table) => getTableMetadataName(table) === name);
1766
+ const matches = exact.length ? exact : tables.filter((table) => table.tableName === name);
1767
+ if (matches.length !== 1)
1768
+ throw new Error(`Unknown or ambiguous SQL table: ${name}`);
1769
+ return matches[0];
1770
+ }
1771
+ /** Returns the schema for a specific SQL table. */
1772
+ async getTableSchema(options) {
1773
+ const adapter = await this.getAdapter();
1774
+ return await adapter.getTableSchema(options);
1775
+ }
1776
+ /** Executes a SQL query and returns object rows. */
1777
+ async queryRows(query, options = {}) {
1778
+ const adapter = await this.getAdapter();
1779
+ const compiledQuery = this.compileQuery(query, options);
1780
+ return await adapter.executeRows(compiledQuery.sql, compiledQuery.options);
1781
+ }
1782
+ /** Executes a SQL query and returns a native Arrow table. */
1783
+ async queryArrow(query, options = {}) {
1784
+ const adapter = await this.getAdapter();
1785
+ const compiledQuery = this.compileQuery(query, options);
1786
+ if (adapter.executeArrow) {
1787
+ return await adapter.executeArrow(compiledQuery.sql, compiledQuery.options);
1788
+ }
1789
+ const rows = await adapter.executeRows(compiledQuery.sql, compiledQuery.options);
1790
+ return convertRowsToArrowTable(rows);
1791
+ }
1792
+ /** Closes any underlying adapter connection. */
1793
+ async close() {
1794
+ if (!this.adapterPromise) {
1795
+ return;
1796
+ }
1797
+ const adapter = await this.adapterPromise;
1798
+ await adapter.close();
1799
+ this.adapterPromise = null;
1800
+ this.metadataPromise = null;
1801
+ }
1802
+ async getAdapter() {
1803
+ if (!this.adapterPromise) {
1804
+ this.adapterPromise = this.loadAdapter();
1805
+ }
1806
+ return await this.adapterPromise;
1807
+ }
1808
+ /** Converts a portable query object to SQL while leaving raw SQL text unchanged. */
1809
+ compileQuery(query, options) {
1810
+ if (typeof query === "string") {
1811
+ return { sql: query, options };
1812
+ }
1813
+ const namedParameters = options.parameters && !Array.isArray(options.parameters) ? options.parameters : void 0;
1814
+ const compiledQuery = compileSQLTableQuery(query, {
1815
+ dialect: getSQLTableQueryDialect(this.sourceType),
1816
+ parameters: namedParameters
1817
+ });
1818
+ return {
1819
+ sql: compiledQuery.sql,
1820
+ options: { signal: options.signal, parameters: compiledQuery.parameters }
1821
+ };
1822
+ }
1823
+ async loadAdapter() {
1824
+ var _a, _b;
1825
+ try {
1826
+ const runtimeFactory = isNodeRuntime() ? (_a = this.options.sql) == null ? void 0 : _a.nodeAdapterFactory : (_b = this.options.sql) == null ? void 0 : _b.browserAdapterFactory;
1827
+ const factory = runtimeFactory || this.adapterFactory;
1828
+ if (!factory) {
1829
+ throw new Error(`No SQL adapter is registered for source type "${this.sourceType}".`);
1830
+ }
1831
+ const adapter = await factory({
1832
+ coreApi: this.coreApi,
1833
+ options: this.options,
1834
+ sourceType: this.sourceType,
1835
+ url: this.url
1836
+ });
1837
+ await adapter.connect();
1838
+ return adapter;
1839
+ } catch (error) {
1840
+ throw this.reportError(error, `Failed to create SQL adapter for ${this.sourceType} from ${this.url}`);
1841
+ }
1842
+ }
1843
+ async loadMetadata() {
1844
+ const adapter = await this.getAdapter();
1845
+ const [catalogs, schemas, tables] = await Promise.all([
1846
+ adapter.listCatalogs(),
1847
+ adapter.listSchemas(),
1848
+ adapter.listTables()
1849
+ ]);
1850
+ return {
1851
+ type: this.sourceType,
1852
+ capabilities: adapter.capabilities,
1853
+ catalogs,
1854
+ schemas,
1855
+ tables
1856
+ };
1857
+ }
1858
+ };
1859
+ function getSQLTableQueryDialect(sourceType) {
1860
+ if (sourceType === "duckdb-sql")
1861
+ return "duckdb";
1862
+ if (sourceType === "snowflake-sql")
1863
+ return "snowflake";
1864
+ throw new Error(`Portable table queries are not supported for SQL source type "${sourceType}".`);
1865
+ }
1866
+ function getTableMetadataName(table) {
1867
+ return [table.catalogName, table.schemaName, table.tableName].filter((name) => typeof name === "string" && name.length > 0).map((name) => `"${name.replaceAll('"', '""')}"`).join(".");
1868
+ }
1869
+
1870
+ // ../core/src/schema-utils/lib/schema/data-type.ts
1871
+ var import_core = require("@math.gl/core");
1872
+ function getDataTypeFromValue(value, defaultNumberType = "float32") {
1873
+ if (value instanceof Date) {
1874
+ return "date-millisecond";
1875
+ }
1876
+ if (typeof value === "number") {
1877
+ return defaultNumberType;
1878
+ }
1879
+ if (typeof value === "boolean") {
1880
+ return "bool";
1881
+ }
1882
+ if (typeof value === "string") {
1883
+ return "utf8";
1884
+ }
1885
+ if (value === null || value === "undefined") {
1886
+ return "null";
1887
+ }
1888
+ return "null";
1889
+ }
1890
+ function getDataTypeFromArray(array) {
1891
+ let type = getDataTypeFromTypedArray(array);
1892
+ if (type !== "null") {
1893
+ return { type, nullable: false };
1894
+ }
1895
+ if (array.length > 0) {
1896
+ type = getDataTypeFromValue(array[0]);
1897
+ return { type, nullable: true };
1898
+ }
1899
+ return { type: "null", nullable: true };
1900
+ }
1901
+ function getDataTypeFromTypedArray(array) {
1902
+ if (typeof Float16Array !== "undefined" && array instanceof Float16Array) {
1903
+ return "float16";
1904
+ }
1905
+ switch (array.constructor) {
1906
+ case Int8Array:
1907
+ return "int8";
1908
+ case Uint8Array:
1909
+ case Uint8ClampedArray:
1910
+ return "uint8";
1911
+ case Int16Array:
1912
+ return "int16";
1913
+ case Uint16Array:
1914
+ return "uint16";
1915
+ case Int32Array:
1916
+ return "int32";
1917
+ case Uint32Array:
1918
+ return "uint32";
1919
+ case BigInt64Array:
1920
+ return "int64";
1921
+ case BigUint64Array:
1922
+ return "uint64";
1923
+ case Float32Array:
1924
+ return "float32";
1925
+ case Float64Array:
1926
+ return "float64";
1927
+ default:
1928
+ return "null";
1929
+ }
1930
+ }
1931
+ function getArrayTypeFromDataType(type, nullable) {
1932
+ if (!nullable) {
1933
+ switch (type) {
1934
+ case "int8":
1935
+ return Int8Array;
1936
+ case "uint8":
1937
+ return Uint8Array;
1938
+ case "int16":
1939
+ return Int16Array;
1940
+ case "uint16":
1941
+ return Uint16Array;
1942
+ case "int32":
1943
+ return Int32Array;
1944
+ case "uint32":
1945
+ return Uint32Array;
1946
+ case "float32":
1947
+ return Float32Array;
1948
+ case "float64":
1949
+ return Float64Array;
1950
+ case "float16":
1951
+ return (0, import_core.getFloat16ArrayConstructor)();
1952
+ default:
1953
+ break;
1954
+ }
1955
+ }
1956
+ return Array;
1957
+ }
1958
+
1959
+ // ../core/src/schema-utils/lib/schema/convert-arrow-schema.ts
1960
+ var arrow2 = __toESM(require("apache-arrow"), 1);
1961
+ function convertArrowToSchema(arrowSchema) {
1962
+ return serializeArrowSchema(arrowSchema);
1963
+ }
1964
+ function convertSchemaToArrow(schema, options) {
1965
+ return deserializeArrowSchema(schema, options);
1966
+ }
1967
+ function serializeArrowSchema(arrowSchema) {
1968
+ return {
1969
+ fields: arrowSchema.fields.map((arrowField) => serializeArrowField(arrowField)),
1970
+ metadata: serializeArrowMetadata(arrowSchema.metadata)
1971
+ };
1972
+ }
1973
+ function deserializeArrowSchema(schema, options) {
1974
+ return new arrow2.Schema(
1975
+ schema.fields.map((field) => deserializeArrowField(field, options)),
1976
+ deserializeArrowMetadata(schema.metadata)
1977
+ );
1400
1978
  }
1401
1979
  function serializeArrowMetadata(arrowMetadata) {
1402
1980
  return Object.fromEntries(arrowMetadata);
@@ -1413,7 +1991,7 @@ function serializeArrowField(field) {
1413
1991
  };
1414
1992
  }
1415
1993
  function deserializeArrowField(field, options) {
1416
- return new arrow.Field(
1994
+ return new arrow2.Field(
1417
1995
  field.name,
1418
1996
  deserializeArrowType(field.type, options),
1419
1997
  field.nullable,
@@ -1435,57 +2013,57 @@ function serializeArrowType(arrowType) {
1435
2013
  };
1436
2014
  }
1437
2015
  switch (arrowType.constructor) {
1438
- case arrow.Null:
2016
+ case arrow2.Null:
1439
2017
  return "null";
1440
- case arrow.Binary:
2018
+ case arrow2.Binary:
1441
2019
  return "binary";
1442
- case arrow.FixedSizeBinary:
2020
+ case arrow2.FixedSizeBinary:
1443
2021
  return {
1444
2022
  type: "fixed-size-binary",
1445
2023
  byteWidth: arrowType.byteWidth
1446
2024
  };
1447
- case arrow.Bool:
2025
+ case arrow2.Bool:
1448
2026
  return "bool";
1449
- case arrow.Int:
2027
+ case arrow2.Int:
1450
2028
  const intType = arrowType;
1451
2029
  return `${intType.isSigned ? "" : "u"}int${intType.bitWidth}`;
1452
- case arrow.Int8:
2030
+ case arrow2.Int8:
1453
2031
  return "int8";
1454
- case arrow.Int16:
2032
+ case arrow2.Int16:
1455
2033
  return "int16";
1456
- case arrow.Int32:
2034
+ case arrow2.Int32:
1457
2035
  return "int32";
1458
- case arrow.Int64:
2036
+ case arrow2.Int64:
1459
2037
  return "int64";
1460
- case arrow.Uint8:
2038
+ case arrow2.Uint8:
1461
2039
  return "uint8";
1462
- case arrow.Uint16:
2040
+ case arrow2.Uint16:
1463
2041
  return "uint16";
1464
- case arrow.Uint32:
2042
+ case arrow2.Uint32:
1465
2043
  return "uint32";
1466
- case arrow.Uint64:
2044
+ case arrow2.Uint64:
1467
2045
  return "uint64";
1468
- case arrow.Float:
2046
+ case arrow2.Float:
1469
2047
  const precision = arrowType.precision;
1470
2048
  switch (precision) {
1471
- case arrow.Precision.HALF:
2049
+ case arrow2.Precision.HALF:
1472
2050
  return "float16";
1473
- case arrow.Precision.SINGLE:
2051
+ case arrow2.Precision.SINGLE:
1474
2052
  return "float32";
1475
- case arrow.Precision.DOUBLE:
2053
+ case arrow2.Precision.DOUBLE:
1476
2054
  return "float64";
1477
2055
  default:
1478
2056
  return "float16";
1479
2057
  }
1480
- case arrow.Float16:
2058
+ case arrow2.Float16:
1481
2059
  return "float16";
1482
- case arrow.Float32:
2060
+ case arrow2.Float32:
1483
2061
  return "float32";
1484
- case arrow.Float64:
2062
+ case arrow2.Float64:
1485
2063
  return "float64";
1486
- case arrow.Utf8:
2064
+ case arrow2.Utf8:
1487
2065
  return "utf8";
1488
- case arrow.Decimal:
2066
+ case arrow2.Decimal:
1489
2067
  const decimal = arrowType;
1490
2068
  return {
1491
2069
  type: "decimal",
@@ -1493,108 +2071,108 @@ function serializeArrowType(arrowType) {
1493
2071
  precision: decimal.precision,
1494
2072
  scale: decimal.scale
1495
2073
  };
1496
- case arrow.Date_:
2074
+ case arrow2.Date_:
1497
2075
  const dateUnit = arrowType.unit;
1498
- return dateUnit === arrow.DateUnit.DAY ? "date-day" : "date-millisecond";
1499
- case arrow.DateDay:
2076
+ return dateUnit === arrow2.DateUnit.DAY ? "date-day" : "date-millisecond";
2077
+ case arrow2.DateDay:
1500
2078
  return "date-day";
1501
- case arrow.DateMillisecond:
2079
+ case arrow2.DateMillisecond:
1502
2080
  return "date-millisecond";
1503
- case arrow.Time:
2081
+ case arrow2.Time:
1504
2082
  const timeUnit = arrowType.unit;
1505
2083
  switch (timeUnit) {
1506
- case arrow.TimeUnit.SECOND:
2084
+ case arrow2.TimeUnit.SECOND:
1507
2085
  return "time-second";
1508
- case arrow.TimeUnit.MILLISECOND:
2086
+ case arrow2.TimeUnit.MILLISECOND:
1509
2087
  return "time-millisecond";
1510
- case arrow.TimeUnit.MICROSECOND:
2088
+ case arrow2.TimeUnit.MICROSECOND:
1511
2089
  return "time-microsecond";
1512
- case arrow.TimeUnit.NANOSECOND:
2090
+ case arrow2.TimeUnit.NANOSECOND:
1513
2091
  return "time-nanosecond";
1514
2092
  default:
1515
2093
  return "time-second";
1516
2094
  }
1517
- case arrow.TimeMillisecond:
2095
+ case arrow2.TimeMillisecond:
1518
2096
  return "time-millisecond";
1519
- case arrow.TimeSecond:
2097
+ case arrow2.TimeSecond:
1520
2098
  return "time-second";
1521
- case arrow.TimeMicrosecond:
2099
+ case arrow2.TimeMicrosecond:
1522
2100
  return "time-microsecond";
1523
- case arrow.TimeNanosecond:
2101
+ case arrow2.TimeNanosecond:
1524
2102
  return "time-nanosecond";
1525
- case arrow.Timestamp:
2103
+ case arrow2.Timestamp:
1526
2104
  const timeStampUnit = arrowType.unit;
1527
2105
  switch (timeStampUnit) {
1528
- case arrow.TimeUnit.SECOND:
2106
+ case arrow2.TimeUnit.SECOND:
1529
2107
  return "timestamp-second";
1530
- case arrow.TimeUnit.MILLISECOND:
2108
+ case arrow2.TimeUnit.MILLISECOND:
1531
2109
  return "timestamp-millisecond";
1532
- case arrow.TimeUnit.MICROSECOND:
2110
+ case arrow2.TimeUnit.MICROSECOND:
1533
2111
  return "timestamp-microsecond";
1534
- case arrow.TimeUnit.NANOSECOND:
2112
+ case arrow2.TimeUnit.NANOSECOND:
1535
2113
  return "timestamp-nanosecond";
1536
2114
  default:
1537
2115
  return "timestamp-second";
1538
2116
  }
1539
- case arrow.TimestampSecond:
2117
+ case arrow2.TimestampSecond:
1540
2118
  return "timestamp-second";
1541
- case arrow.TimestampMillisecond:
2119
+ case arrow2.TimestampMillisecond:
1542
2120
  return "timestamp-millisecond";
1543
- case arrow.TimestampMicrosecond:
2121
+ case arrow2.TimestampMicrosecond:
1544
2122
  return "timestamp-microsecond";
1545
- case arrow.TimestampNanosecond:
2123
+ case arrow2.TimestampNanosecond:
1546
2124
  return "timestamp-nanosecond";
1547
- case arrow.Interval:
2125
+ case arrow2.Interval:
1548
2126
  const intervalUnit = arrowType.unit;
1549
2127
  switch (intervalUnit) {
1550
- case arrow.IntervalUnit.DAY_TIME:
2128
+ case arrow2.IntervalUnit.DAY_TIME:
1551
2129
  return "interval-daytime";
1552
- case arrow.IntervalUnit.YEAR_MONTH:
2130
+ case arrow2.IntervalUnit.YEAR_MONTH:
1553
2131
  return "interval-yearmonth";
1554
2132
  default:
1555
2133
  return "interval-daytime";
1556
2134
  }
1557
- case arrow.IntervalDayTime:
2135
+ case arrow2.IntervalDayTime:
1558
2136
  return "interval-daytime";
1559
- case arrow.IntervalYearMonth:
2137
+ case arrow2.IntervalYearMonth:
1560
2138
  return "interval-yearmonth";
1561
- case arrow.Map_:
2139
+ case arrow2.Map_:
1562
2140
  const mapType = arrowType;
1563
2141
  return {
1564
2142
  type: "map",
1565
2143
  keysSorted: mapType.keysSorted,
1566
2144
  children: mapType.children.map((arrowField) => serializeArrowField(arrowField))
1567
2145
  };
1568
- case arrow.List:
2146
+ case arrow2.List:
1569
2147
  const listType = arrowType;
1570
2148
  const listField = listType.valueField;
1571
2149
  return {
1572
2150
  type: "list",
1573
2151
  children: [serializeArrowField(listField)]
1574
2152
  };
1575
- case arrow.FixedSizeList:
2153
+ case arrow2.FixedSizeList:
1576
2154
  const fixedSizeList = arrowType;
1577
2155
  return {
1578
2156
  type: "fixed-size-list",
1579
2157
  listSize: fixedSizeList.listSize,
1580
2158
  children: [serializeArrowField(fixedSizeList.children[0])]
1581
2159
  };
1582
- case arrow.Struct:
2160
+ case arrow2.Struct:
1583
2161
  const structType = arrowType;
1584
2162
  return {
1585
2163
  type: "struct",
1586
2164
  children: structType.children.map((arrowField) => serializeArrowField(arrowField))
1587
2165
  };
1588
- case arrow.SparseUnion:
1589
- case arrow.DenseUnion:
2166
+ case arrow2.SparseUnion:
2167
+ case arrow2.DenseUnion:
1590
2168
  const unionType = arrowType;
1591
2169
  return {
1592
- type: unionType instanceof arrow.DenseUnion ? "dense-union" : "sparse-union",
2170
+ type: unionType instanceof arrow2.DenseUnion ? "dense-union" : "sparse-union",
1593
2171
  typeIds: unionType.typeIds,
1594
2172
  children: unionType.children.map((arrowField) => serializeArrowField(arrowField)),
1595
2173
  typeIdToChildIndex: { ...unionType.typeIdToChildIndex }
1596
2174
  };
1597
- case arrow.Dictionary:
2175
+ case arrow2.Dictionary:
1598
2176
  const dictionaryType = arrowType;
1599
2177
  return {
1600
2178
  type: "dictionary",
@@ -1611,16 +2189,16 @@ function deserializeArrowType(dataType, options) {
1611
2189
  if (typeof dataType === "object") {
1612
2190
  switch (dataType.type) {
1613
2191
  case "decimal":
1614
- return new arrow.Decimal(dataType.scale, dataType.precision, dataType.bitWidth);
2192
+ return new arrow2.Decimal(dataType.scale, dataType.precision, dataType.bitWidth);
1615
2193
  case "map": {
1616
2194
  const children = dataType.children.map(
1617
2195
  (arrowField) => deserializeArrowField(arrowField, options)
1618
2196
  );
1619
- return new arrow.Map_(children[0], dataType.keysSorted);
2197
+ return new arrow2.Map_(children[0], dataType.keysSorted);
1620
2198
  }
1621
2199
  case "list": {
1622
2200
  const field = deserializeArrowField(dataType.children[0], options);
1623
- return new arrow.List(field);
2201
+ return new arrow2.List(field);
1624
2202
  }
1625
2203
  case "large-list": {
1626
2204
  const field = deserializeArrowField(dataType.children[0], options);
@@ -1634,25 +2212,25 @@ function deserializeArrowType(dataType, options) {
1634
2212
  }
1635
2213
  case "fixed-size-list": {
1636
2214
  const child = deserializeArrowField(dataType.children[0], options);
1637
- return new arrow.FixedSizeList(dataType.listSize, child);
2215
+ return new arrow2.FixedSizeList(dataType.listSize, child);
1638
2216
  }
1639
2217
  case "fixed-size-binary":
1640
- return new arrow.FixedSizeBinary(dataType.byteWidth);
2218
+ return new arrow2.FixedSizeBinary(dataType.byteWidth);
1641
2219
  case "struct": {
1642
2220
  const children = dataType.children.map(
1643
2221
  (arrowField) => deserializeArrowField(arrowField, options)
1644
2222
  );
1645
- return new arrow.Struct(children);
2223
+ return new arrow2.Struct(children);
1646
2224
  }
1647
2225
  case "sparse-union":
1648
2226
  case "dense-union": {
1649
2227
  const children = dataType.children.map(
1650
2228
  (arrowField) => deserializeArrowField(arrowField, options)
1651
2229
  );
1652
- return dataType.type === "dense-union" ? new arrow.DenseUnion(dataType.typeIds, children) : new arrow.SparseUnion(dataType.typeIds, children);
2230
+ return dataType.type === "dense-union" ? new arrow2.DenseUnion(dataType.typeIds, children) : new arrow2.SparseUnion(dataType.typeIds, children);
1653
2231
  }
1654
2232
  case "dictionary": {
1655
- return new arrow.Dictionary(
2233
+ return new arrow2.Dictionary(
1656
2234
  deserializeArrowType(dataType.dictionary, options),
1657
2235
  deserializeArrowDictionaryKeyType(dataType.indices),
1658
2236
  dataType.id,
@@ -1665,76 +2243,76 @@ function deserializeArrowType(dataType, options) {
1665
2243
  }
1666
2244
  switch (dataType) {
1667
2245
  case "null":
1668
- return new arrow.Null();
2246
+ return new arrow2.Null();
1669
2247
  case "binary":
1670
- return makeVariableWidthArrowType("BinaryView", new arrow.Binary(), options);
2248
+ return makeVariableWidthArrowType("BinaryView", new arrow2.Binary(), options);
1671
2249
  case "binary-view":
1672
2250
  return makeRequiredArrowViewType("BinaryView");
1673
2251
  case "bool":
1674
- return new arrow.Bool();
2252
+ return new arrow2.Bool();
1675
2253
  case "int8":
1676
- return new arrow.Int8();
2254
+ return new arrow2.Int8();
1677
2255
  case "int16":
1678
- return new arrow.Int16();
2256
+ return new arrow2.Int16();
1679
2257
  case "int32":
1680
- return new arrow.Int32();
2258
+ return new arrow2.Int32();
1681
2259
  case "int64":
1682
- return new arrow.Int64();
2260
+ return new arrow2.Int64();
1683
2261
  case "uint8":
1684
- return new arrow.Uint8();
2262
+ return new arrow2.Uint8();
1685
2263
  case "uint16":
1686
- return new arrow.Uint16();
2264
+ return new arrow2.Uint16();
1687
2265
  case "uint32":
1688
- return new arrow.Uint32();
2266
+ return new arrow2.Uint32();
1689
2267
  case "uint64":
1690
- return new arrow.Uint64();
2268
+ return new arrow2.Uint64();
1691
2269
  case "float16":
1692
- return new arrow.Float16();
2270
+ return new arrow2.Float16();
1693
2271
  case "float32":
1694
- return new arrow.Float32();
2272
+ return new arrow2.Float32();
1695
2273
  case "float64":
1696
- return new arrow.Float64();
2274
+ return new arrow2.Float64();
1697
2275
  case "utf8":
1698
- return makeVariableWidthArrowType("Utf8View", new arrow.Utf8(), options);
2276
+ return makeVariableWidthArrowType("Utf8View", new arrow2.Utf8(), options);
1699
2277
  case "utf8-view":
1700
2278
  return makeRequiredArrowViewType("Utf8View");
1701
2279
  case "date-day":
1702
- return new arrow.DateDay();
2280
+ return new arrow2.DateDay();
1703
2281
  case "date-millisecond":
1704
- return new arrow.DateMillisecond();
2282
+ return new arrow2.DateMillisecond();
1705
2283
  case "time-second":
1706
- return new arrow.TimeSecond();
2284
+ return new arrow2.TimeSecond();
1707
2285
  case "time-millisecond":
1708
- return new arrow.TimeMillisecond();
2286
+ return new arrow2.TimeMillisecond();
1709
2287
  case "time-microsecond":
1710
- return new arrow.TimeMicrosecond();
2288
+ return new arrow2.TimeMicrosecond();
1711
2289
  case "time-nanosecond":
1712
- return new arrow.TimeNanosecond();
2290
+ return new arrow2.TimeNanosecond();
1713
2291
  case "timestamp-second":
1714
- return new arrow.TimestampSecond();
2292
+ return new arrow2.TimestampSecond();
1715
2293
  case "timestamp-millisecond":
1716
- return new arrow.TimestampMillisecond();
2294
+ return new arrow2.TimestampMillisecond();
1717
2295
  case "timestamp-microsecond":
1718
- return new arrow.TimestampMicrosecond();
2296
+ return new arrow2.TimestampMicrosecond();
1719
2297
  case "timestamp-nanosecond":
1720
- return new arrow.TimestampNanosecond();
2298
+ return new arrow2.TimestampNanosecond();
1721
2299
  case "interval-daytime":
1722
- return new arrow.IntervalDayTime();
2300
+ return new arrow2.IntervalDayTime();
1723
2301
  case "interval-yearmonth":
1724
- return new arrow.IntervalYearMonth();
2302
+ return new arrow2.IntervalYearMonth();
1725
2303
  default:
1726
2304
  throw new Error("array type not supported");
1727
2305
  }
1728
2306
  }
1729
2307
  function getOptionalArrowConstructor(typeName) {
1730
- return arrow[typeName];
2308
+ return arrow2[typeName];
1731
2309
  }
1732
2310
  function isArrowViewType(arrowType, viewTypeName) {
1733
2311
  const constructor = getOptionalArrowConstructor(viewTypeName);
1734
2312
  if (constructor && arrowType instanceof constructor) {
1735
2313
  return true;
1736
2314
  }
1737
- const guards = arrow.DataType;
2315
+ const guards = arrow2.DataType;
1738
2316
  const guard = viewTypeName === "BinaryView" ? guards.isBinaryView : guards.isUtf8View;
1739
2317
  return Boolean(guard == null ? void 0 : guard(arrowType));
1740
2318
  }
@@ -1768,7 +2346,7 @@ function makeRequiredArrowViewType(viewTypeName) {
1768
2346
  return new constructor();
1769
2347
  }
1770
2348
  function serializeArrowDictionaryKeyType(arrowType) {
1771
- if (arrowType instanceof arrow.Int) {
2349
+ if (arrowType instanceof arrow2.Int) {
1772
2350
  const prefix = arrowType.isSigned ? "int" : "uint";
1773
2351
  const keyType = `${prefix}${arrowType.bitWidth}`;
1774
2352
  if (isSchemaDictionaryKeyType(keyType)) {
@@ -1776,17 +2354,17 @@ function serializeArrowDictionaryKeyType(arrowType) {
1776
2354
  }
1777
2355
  }
1778
2356
  switch (arrowType.constructor) {
1779
- case arrow.Int8:
2357
+ case arrow2.Int8:
1780
2358
  return "int8";
1781
- case arrow.Int16:
2359
+ case arrow2.Int16:
1782
2360
  return "int16";
1783
- case arrow.Int32:
2361
+ case arrow2.Int32:
1784
2362
  return "int32";
1785
- case arrow.Uint8:
2363
+ case arrow2.Uint8:
1786
2364
  return "uint8";
1787
- case arrow.Uint16:
2365
+ case arrow2.Uint16:
1788
2366
  return "uint16";
1789
- case arrow.Uint32:
2367
+ case arrow2.Uint32:
1790
2368
  return "uint32";
1791
2369
  default:
1792
2370
  throw new Error(`arrow dictionary index type not supported: ${arrowType.constructor.name}`);
@@ -1798,146 +2376,28 @@ function isSchemaDictionaryKeyType(keyType) {
1798
2376
  function deserializeArrowDictionaryKeyType(keyType) {
1799
2377
  switch (keyType) {
1800
2378
  case "int8":
1801
- return new arrow.Int8();
2379
+ return new arrow2.Int8();
1802
2380
  case "int16":
1803
- return new arrow.Int16();
2381
+ return new arrow2.Int16();
1804
2382
  case "int32":
1805
- return new arrow.Int32();
2383
+ return new arrow2.Int32();
1806
2384
  case "uint8":
1807
- return new arrow.Uint8();
2385
+ return new arrow2.Uint8();
1808
2386
  case "uint16":
1809
- return new arrow.Uint16();
2387
+ return new arrow2.Uint16();
1810
2388
  case "uint32":
1811
- return new arrow.Uint32();
2389
+ return new arrow2.Uint32();
1812
2390
  default:
1813
2391
  throw new Error(`schema dictionary index type not supported: ${keyType}`);
1814
2392
  }
1815
2393
  }
1816
2394
 
1817
- // ../core/src/schema-utils/lib/schema/deduce-table-schema.ts
1818
- function deduceTableSchema(table) {
1819
- switch (table.shape) {
1820
- case "array-row-table":
1821
- case "object-row-table":
1822
- return deduceSchemaFromRows(table.data);
1823
- case "geojson-table":
1824
- return deduceSchemaFromGeoJSON(table.features);
1825
- case "columnar-table":
1826
- return deduceSchemaFromColumns(table.data);
1827
- case "arrow-table":
1828
- default:
1829
- throw new Error("Deduce schema");
1830
- }
1831
- }
1832
- function deduceSchemaFromColumns(columnarTable) {
1833
- const fields = [];
1834
- for (const [columnName, column] of Object.entries(columnarTable)) {
1835
- const field = deduceFieldFromColumn(column, columnName);
1836
- fields.push(field);
1837
- }
1838
- return { fields, metadata: {} };
1839
- }
1840
- function deduceSchemaFromRows(rowTable) {
1841
- if (!rowTable.length) {
1842
- throw new Error("deduce from empty table");
1843
- }
1844
- const fields = [];
1845
- const row0 = rowTable[0];
1846
- for (const [columnName, value] of Object.entries(row0)) {
1847
- fields.push(deduceFieldFromValue(value, columnName));
1848
- }
1849
- return { fields, metadata: {} };
1850
- }
1851
- function deduceSchemaFromGeoJSON(features) {
1852
- if (!features.length) {
1853
- throw new Error("deduce from empty table");
1854
- }
1855
- const fields = [];
1856
- const row0 = features[0].properties || {};
1857
- for (const [columnName, value] of Object.entries(row0)) {
1858
- fields.push(deduceFieldFromValue(value, columnName));
1859
- }
1860
- return { fields, metadata: {} };
1861
- }
1862
- function deduceFieldFromColumn(column, name) {
1863
- if (ArrayBuffer.isView(column)) {
1864
- const type = getDataTypeFromArray(column);
1865
- return {
1866
- name,
1867
- type: type.type || "null",
1868
- nullable: type.nullable
1869
- // metadata: {}
1870
- };
1871
- }
1872
- if (Array.isArray(column) && column.length > 0) {
1873
- const value = column[0];
1874
- const type = getDataTypeFromValue(value);
1875
- return {
1876
- name,
1877
- type,
1878
- nullable: true
1879
- // metadata: {},
1880
- };
1881
- }
1882
- throw new Error("empty table");
1883
- }
1884
- function deduceFieldFromValue(value, name) {
1885
- const type = getDataTypeFromValue(value);
1886
- return {
1887
- name,
1888
- type,
1889
- nullable: true
1890
- // metadata: {}
1891
- };
1892
- }
1893
-
1894
- // ../core/src/schema-utils/lib/table/tables/make-table.ts
1895
- var arrow2 = __toESM(require("apache-arrow"), 1);
1896
- function makeTableFromData(data) {
1897
- let table;
1898
- switch (getTableShapeFromData(data)) {
1899
- case "array-row-table":
1900
- table = { shape: "array-row-table", data };
1901
- break;
1902
- case "object-row-table":
1903
- table = { shape: "object-row-table", data };
1904
- break;
1905
- case "columnar-table":
1906
- table = { shape: "columnar-table", data };
1907
- break;
1908
- case "arrow-table":
1909
- table = { shape: "arrow-table", data };
1910
- break;
1911
- default:
1912
- throw new Error("table");
1913
- }
1914
- const schema = deduceTableSchema(table);
1915
- return { ...table, schema };
1916
- }
1917
- function getTableShapeFromData(data) {
1918
- if (data instanceof arrow2.Table) {
1919
- return "arrow-table";
1920
- }
1921
- if (Array.isArray(data)) {
1922
- if (data.length === 0) {
1923
- throw new Error("cannot deduce type of empty table");
1924
- }
1925
- const firstRow = data[0];
1926
- if (Array.isArray(firstRow)) {
1927
- return "array-row-table";
1928
- }
1929
- if (firstRow && typeof firstRow === "object") {
1930
- return "object-row-table";
1931
- }
1932
- }
1933
- if (data && typeof data === "object") {
1934
- return "columnar-table";
1935
- }
1936
- throw new Error("invalid table");
1937
- }
2395
+ // ../core/src/schema-utils/lib/table/tables/convert-arrow-table.ts
2396
+ var arrow4 = __toESM(require("apache-arrow"), 1);
1938
2397
 
1939
2398
  // ../core/src/schema-utils/lib/table/tables/table-accessors.ts
1940
2399
  function getTableLength(table) {
2400
+ if (!("shape" in table)) return table.numRows;
1941
2401
  switch (table.shape) {
1942
2402
  case "array-row-table":
1943
2403
  case "object-row-table":
@@ -1945,8 +2405,7 @@ function getTableLength(table) {
1945
2405
  case "geojson-table":
1946
2406
  return table.features.length;
1947
2407
  case "arrow-table":
1948
- const arrowTable = table.data;
1949
- return arrowTable.numRows;
2408
+ return getTableLength(table.data);
1950
2409
  case "columnar-table":
1951
2410
  for (const column of Object.values(table.data)) {
1952
2411
  return column.length || 0;
@@ -1957,6 +2416,7 @@ function getTableLength(table) {
1957
2416
  }
1958
2417
  }
1959
2418
  function getTableNumCols(table) {
2419
+ if (!("shape" in table)) return table.numCols;
1960
2420
  if (table.schema) {
1961
2421
  return table.schema.fields.length;
1962
2422
  }
@@ -1973,14 +2433,14 @@ function getTableNumCols(table) {
1973
2433
  case "columnar-table":
1974
2434
  return Object.keys(table.data).length;
1975
2435
  case "arrow-table":
1976
- const arrowTable = table.data;
1977
- return arrowTable.numCols;
2436
+ return getTableNumCols(table.data);
1978
2437
  default:
1979
2438
  throw new Error("table");
1980
2439
  }
1981
2440
  }
1982
2441
  function getTableCell(table, rowIndex, columnName) {
1983
2442
  var _a;
2443
+ if (!("shape" in table)) return (_a = table.getChild(columnName)) == null ? void 0 : _a.get(rowIndex);
1984
2444
  switch (table.shape) {
1985
2445
  case "array-row-table":
1986
2446
  const columnIndex = getTableColumnIndex(table, columnName);
@@ -1993,17 +2453,14 @@ function getTableCell(table, rowIndex, columnName) {
1993
2453
  const column = table.data[columnName];
1994
2454
  return column[rowIndex];
1995
2455
  case "arrow-table":
1996
- const arrowTable = table.data;
1997
- const arrowColumnIndex = arrowTable.schema.fields.findIndex(
1998
- (field) => field.name === columnName
1999
- );
2000
- return (_a = arrowTable.getChildAt(arrowColumnIndex)) == null ? void 0 : _a.get(rowIndex);
2456
+ return getTableCell(table.data, rowIndex, columnName);
2001
2457
  default:
2002
2458
  throw new Error("todo");
2003
2459
  }
2004
2460
  }
2005
2461
  function getTableCellAt(table, rowIndex, columnIndex) {
2006
2462
  var _a;
2463
+ if (!("shape" in table)) return (_a = table.getChildAt(columnIndex)) == null ? void 0 : _a.get(rowIndex);
2007
2464
  switch (table.shape) {
2008
2465
  case "array-row-table":
2009
2466
  return table.data[rowIndex][columnIndex];
@@ -2018,8 +2475,7 @@ function getTableCellAt(table, rowIndex, columnIndex) {
2018
2475
  const column = table.data[columnName3];
2019
2476
  return column[rowIndex];
2020
2477
  case "arrow-table":
2021
- const arrowTable = table.data;
2022
- return (_a = arrowTable.getChildAt(columnIndex)) == null ? void 0 : _a.get(rowIndex);
2478
+ return getTableCellAt(table.data, rowIndex, columnIndex);
2023
2479
  default:
2024
2480
  throw new Error("todo");
2025
2481
  }
@@ -2041,104 +2497,182 @@ function getTableColumnName(table, columnIndex) {
2041
2497
  return columnName;
2042
2498
  }
2043
2499
  function getTableRowAsObject(table, rowIndex, target, copy) {
2500
+ if (!("shape" in table)) {
2501
+ const objectRow = target || {};
2502
+ const row = table.get(rowIndex);
2503
+ for (const field of table.schema.fields) objectRow[field.name] = row == null ? void 0 : row[field.name];
2504
+ return objectRow;
2505
+ }
2044
2506
  switch (table.shape) {
2045
2507
  case "object-row-table":
2046
2508
  return copy ? Object.fromEntries(Object.entries(table.data[rowIndex])) : table.data[rowIndex];
2047
2509
  case "array-row-table":
2048
2510
  if (table.schema) {
2049
- const objectRow2 = target || {};
2511
+ const objectRow = target || {};
2050
2512
  for (let i = 0; i < table.schema.fields.length; i++) {
2051
- objectRow2[table.schema.fields[i].name] = table.data[rowIndex][i];
2513
+ objectRow[table.schema.fields[i].name] = table.data[rowIndex][i];
2052
2514
  }
2053
- return objectRow2;
2515
+ return objectRow;
2054
2516
  }
2055
2517
  throw new Error("no schema");
2056
2518
  case "geojson-table":
2057
2519
  if (table.schema) {
2058
- const objectRow2 = target || {};
2520
+ const objectRow = target || {};
2059
2521
  for (let i = 0; i < table.schema.fields.length; i++) {
2060
- objectRow2[table.schema.fields[i].name] = table.features[rowIndex][table.schema.fields[i].name];
2522
+ objectRow[table.schema.fields[i].name] = table.features[rowIndex][table.schema.fields[i].name];
2061
2523
  }
2062
- return objectRow2;
2524
+ return objectRow;
2063
2525
  }
2064
2526
  throw new Error("no schema");
2065
2527
  case "columnar-table":
2066
2528
  if (table.schema) {
2067
- const objectRow2 = target || {};
2529
+ const objectRow = target || {};
2068
2530
  for (let i = 0; i < table.schema.fields.length; i++) {
2069
- objectRow2[table.schema.fields[i].name] = table.data[table.schema.fields[i].name][rowIndex];
2531
+ objectRow[table.schema.fields[i].name] = table.data[table.schema.fields[i].name][rowIndex];
2070
2532
  }
2071
- return objectRow2;
2533
+ return objectRow;
2072
2534
  } else {
2073
- const objectRow2 = target || {};
2535
+ const objectRow = target || {};
2074
2536
  for (const [name, column] of Object.entries(table.data)) {
2075
- objectRow2[name] = column[rowIndex];
2537
+ objectRow[name] = column[rowIndex];
2076
2538
  }
2077
- return objectRow2;
2539
+ return objectRow;
2078
2540
  }
2079
2541
  case "arrow-table":
2080
- const arrowTable = table.data;
2081
- const objectRow = target || {};
2082
- const row = arrowTable.get(rowIndex);
2083
- const schema = arrowTable.schema;
2084
- for (let i = 0; i < schema.fields.length; i++) {
2085
- objectRow[schema.fields[i].name] = row == null ? void 0 : row[schema.fields[i].name];
2086
- }
2087
- return objectRow;
2542
+ return getTableRowAsObject(table.data, rowIndex, target, copy);
2088
2543
  default:
2089
2544
  throw new Error("shape");
2090
2545
  }
2091
2546
  }
2092
2547
  function getTableRowAsArray(table, rowIndex, target, copy) {
2548
+ if (!("shape" in table)) {
2549
+ const arrayRow = target || [];
2550
+ const row = table.get(rowIndex);
2551
+ table.schema.fields.forEach((field, columnIndex) => {
2552
+ arrayRow[columnIndex] = row == null ? void 0 : row[field.name];
2553
+ });
2554
+ return arrayRow;
2555
+ }
2093
2556
  switch (table.shape) {
2094
2557
  case "array-row-table":
2095
2558
  return copy ? Array.from(table.data[rowIndex]) : table.data[rowIndex];
2096
2559
  case "object-row-table":
2097
2560
  if (table.schema) {
2098
- const arrayRow2 = target || [];
2561
+ const arrayRow = target || [];
2099
2562
  for (let i = 0; i < table.schema.fields.length; i++) {
2100
- arrayRow2[i] = table.data[rowIndex][table.schema.fields[i].name];
2563
+ arrayRow[i] = table.data[rowIndex][table.schema.fields[i].name];
2101
2564
  }
2102
- return arrayRow2;
2565
+ return arrayRow;
2103
2566
  }
2104
2567
  return Object.values(table.data[rowIndex]);
2105
2568
  case "geojson-table":
2106
2569
  if (table.schema) {
2107
- const arrayRow2 = target || [];
2570
+ const arrayRow = target || [];
2108
2571
  for (let i = 0; i < table.schema.fields.length; i++) {
2109
- arrayRow2[i] = table.features[rowIndex][table.schema.fields[i].name];
2572
+ arrayRow[i] = table.features[rowIndex][table.schema.fields[i].name];
2110
2573
  }
2111
- return arrayRow2;
2574
+ return arrayRow;
2112
2575
  }
2113
2576
  return Object.values(table.features[rowIndex]);
2114
2577
  case "columnar-table":
2115
2578
  if (table.schema) {
2116
- const arrayRow2 = target || [];
2579
+ const arrayRow = target || [];
2117
2580
  for (let i = 0; i < table.schema.fields.length; i++) {
2118
- arrayRow2[i] = table.data[table.schema.fields[i].name][rowIndex];
2581
+ arrayRow[i] = table.data[table.schema.fields[i].name][rowIndex];
2119
2582
  }
2120
- return arrayRow2;
2583
+ return arrayRow;
2121
2584
  } else {
2122
- const arrayRow2 = target || [];
2585
+ const arrayRow = target || [];
2123
2586
  let i = 0;
2124
2587
  for (const column of Object.values(table.data)) {
2125
- arrayRow2[i] = column[rowIndex];
2588
+ arrayRow[i] = column[rowIndex];
2126
2589
  i++;
2127
2590
  }
2128
- return arrayRow2;
2591
+ return arrayRow;
2129
2592
  }
2130
2593
  case "arrow-table":
2131
- const arrowTable = table.data;
2132
- const arrayRow = target || [];
2133
- const row = arrowTable.get(rowIndex);
2134
- const schema = arrowTable.schema;
2135
- for (let i = 0; i < schema.fields.length; i++) {
2136
- arrayRow[i] = row == null ? void 0 : row[schema.fields[i].name];
2137
- }
2138
- return arrayRow;
2594
+ return getTableRowAsArray(table.data, rowIndex, target, copy);
2595
+ default:
2596
+ throw new Error("shape");
2597
+ }
2598
+ }
2599
+
2600
+ // ../core/src/schema-utils/lib/schema/deduce-table-schema.ts
2601
+ function deduceTableSchema(table) {
2602
+ switch (table.shape) {
2603
+ case "array-row-table":
2604
+ case "object-row-table":
2605
+ return deduceSchemaFromRows(table.data);
2606
+ case "geojson-table":
2607
+ return deduceSchemaFromGeoJSON(table.features);
2608
+ case "columnar-table":
2609
+ return deduceSchemaFromColumns(table.data);
2610
+ case "arrow-table":
2139
2611
  default:
2140
- throw new Error("shape");
2612
+ throw new Error("Deduce schema");
2613
+ }
2614
+ }
2615
+ function deduceSchemaFromColumns(columnarTable) {
2616
+ const fields = [];
2617
+ for (const [columnName, column] of Object.entries(columnarTable)) {
2618
+ const field = deduceFieldFromColumn(column, columnName);
2619
+ fields.push(field);
2620
+ }
2621
+ return { fields, metadata: {} };
2622
+ }
2623
+ function deduceSchemaFromRows(rowTable) {
2624
+ if (!rowTable.length) {
2625
+ throw new Error("deduce from empty table");
2626
+ }
2627
+ const fields = [];
2628
+ const row0 = rowTable[0];
2629
+ for (const [columnName, value] of Object.entries(row0)) {
2630
+ const name = Array.isArray(row0) ? `column${Number(columnName) + 1}` : columnName;
2631
+ fields.push(deduceFieldFromValue(value, name));
2632
+ }
2633
+ return { fields, metadata: {} };
2634
+ }
2635
+ function deduceSchemaFromGeoJSON(features) {
2636
+ if (!features.length) {
2637
+ throw new Error("deduce from empty table");
2638
+ }
2639
+ const fields = [];
2640
+ const row0 = features[0].properties || {};
2641
+ for (const [columnName, value] of Object.entries(row0)) {
2642
+ fields.push(deduceFieldFromValue(value, columnName));
2643
+ }
2644
+ return { fields, metadata: {} };
2645
+ }
2646
+ function deduceFieldFromColumn(column, name) {
2647
+ if (ArrayBuffer.isView(column)) {
2648
+ const type = getDataTypeFromArray(column);
2649
+ return {
2650
+ name,
2651
+ type: type.type || "null",
2652
+ nullable: type.nullable
2653
+ // metadata: {}
2654
+ };
2655
+ }
2656
+ if (Array.isArray(column) && column.length > 0) {
2657
+ const value = column[0];
2658
+ const type = getDataTypeFromValue(value);
2659
+ return {
2660
+ name,
2661
+ type,
2662
+ nullable: true
2663
+ // metadata: {},
2664
+ };
2141
2665
  }
2666
+ throw new Error("empty table");
2667
+ }
2668
+ function deduceFieldFromValue(value, name) {
2669
+ const type = getDataTypeFromValue(value);
2670
+ return {
2671
+ name,
2672
+ type,
2673
+ nullable: true
2674
+ // metadata: {}
2675
+ };
2142
2676
  }
2143
2677
 
2144
2678
  // ../core/src/schema-utils/lib/table/tables/table-column.ts
@@ -2147,12 +2681,94 @@ function makeColumnFromField(field, length) {
2147
2681
  return new ArrayType(length);
2148
2682
  }
2149
2683
 
2150
- // ../core/src/schema-utils/lib/table/tables/convert-arrow-table.ts
2151
- var arrow4 = __toESM(require("apache-arrow"), 1);
2684
+ // ../core/src/schema-utils/lib/table/tables/convert-table.ts
2685
+ function convertTable(table, shape) {
2686
+ if (shape === "arrow") return convertTableToArrow(table);
2687
+ if (!("shape" in table)) return convertArrowToTable(table, shape);
2688
+ switch (shape) {
2689
+ case "object-row-table":
2690
+ return convertToObjectRowTable(table);
2691
+ case "array-row-table":
2692
+ return convertToArrayRowTable(table);
2693
+ case "columnar-table":
2694
+ return convertToColumnarTable(table);
2695
+ default:
2696
+ throw new Error(shape);
2697
+ }
2698
+ }
2699
+ function convertToColumnarTable(table) {
2700
+ const schema = table.schema || deduceTableSchema(table);
2701
+ const fields = schema.fields;
2702
+ if (table.shape === "columnar-table") {
2703
+ return { ...table, schema };
2704
+ }
2705
+ const length = getTableLength(table);
2706
+ const columns = {};
2707
+ for (const field of fields) {
2708
+ const column = makeColumnFromField(field, length);
2709
+ columns[field.name] = column;
2710
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
2711
+ column[rowIndex] = getTableCell(table, rowIndex, field.name);
2712
+ }
2713
+ }
2714
+ return {
2715
+ shape: "columnar-table",
2716
+ schema,
2717
+ data: columns
2718
+ };
2719
+ }
2720
+ function convertToArrayRowTable(table) {
2721
+ if (table.shape === "array-row-table") {
2722
+ return table;
2723
+ }
2724
+ const length = getTableLength(table);
2725
+ const data = new Array(length);
2726
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
2727
+ data[rowIndex] = getTableRowAsArray(table, rowIndex);
2728
+ }
2729
+ return {
2730
+ shape: "array-row-table",
2731
+ schema: table.schema,
2732
+ data
2733
+ };
2734
+ }
2735
+ function convertToObjectRowTable(table) {
2736
+ if (table.shape === "object-row-table") {
2737
+ return table;
2738
+ }
2739
+ const length = getTableLength(table);
2740
+ const data = new Array(length);
2741
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
2742
+ data[rowIndex] = getTableRowAsObject(table, rowIndex);
2743
+ }
2744
+ return {
2745
+ shape: "object-row-table",
2746
+ schema: table.schema,
2747
+ data
2748
+ };
2749
+ }
2152
2750
 
2153
2751
  // ../core/src/schema-utils/lib/table/batches/make-arrow-batch-iterator.ts
2154
2752
  var arrow3 = __toESM(require("apache-arrow"), 1);
2155
- function* makeArrowRecordBatchIterator(table, options) {
2753
+ function* sliceNativeArrowBatches(table, batchSize) {
2754
+ if (batchSize !== void 0 && (!Number.isSafeInteger(batchSize) || batchSize < 1)) {
2755
+ throw new RangeError("batchSize must be a positive integer");
2756
+ }
2757
+ for (const batch of table.batches) {
2758
+ if (batchSize === void 0 || batch.numRows <= batchSize) {
2759
+ yield batch;
2760
+ } else {
2761
+ for (let offset = 0; offset < batch.numRows; offset += batchSize) {
2762
+ yield batch.slice(offset, offset + batchSize);
2763
+ }
2764
+ }
2765
+ }
2766
+ }
2767
+ function* makeWrappedTableRecordBatchIterator(table, options) {
2768
+ if (table.shape === "arrow-table") {
2769
+ yield* sliceNativeArrowBatches(table.data, options == null ? void 0 : options.batchSize);
2770
+ return;
2771
+ }
2156
2772
  const arrowSchema = convertSchemaToArrow(table.schema);
2157
2773
  const length = getTableLength(table);
2158
2774
  const numColumns = getTableNumCols(table);
@@ -2190,6 +2806,7 @@ function* makeArrowRecordBatchIterator(table, options) {
2190
2806
 
2191
2807
  // ../core/src/schema-utils/lib/table/tables/convert-arrow-table.ts
2192
2808
  function convertTableToArrow(table, options) {
2809
+ if (!("shape" in table)) return table;
2193
2810
  switch (table.shape) {
2194
2811
  case "arrow-table":
2195
2812
  return table.data;
@@ -2198,14 +2815,16 @@ function convertTableToArrow(table, options) {
2198
2815
  // return convertColumnarTableToArrow(table, options);
2199
2816
  // fall through
2200
2817
  default:
2201
- const arrowBatchIterator = makeArrowRecordBatchIterator(table, options);
2202
- return new arrow4.Table(arrowBatchIterator);
2818
+ const schema = table.schema ?? deduceTableSchema(table);
2819
+ return new arrow4.Table(convertSchemaToArrow(schema), [
2820
+ ...makeWrappedTableRecordBatchIterator({ ...table, schema }, options)
2821
+ ]);
2203
2822
  }
2204
2823
  }
2205
2824
  function convertArrowToTable(arrowTable, shape) {
2206
2825
  switch (shape) {
2207
- case "arrow-table":
2208
- return convertArrowToArrowTable(arrowTable);
2826
+ case "arrow":
2827
+ return arrowTable;
2209
2828
  case "array-row-table":
2210
2829
  return convertArrowToArrayRowTable(arrowTable);
2211
2830
  case "object-row-table":
@@ -2218,13 +2837,6 @@ function convertArrowToTable(arrowTable, shape) {
2218
2837
  throw new Error(shape);
2219
2838
  }
2220
2839
  }
2221
- function convertArrowToArrowTable(arrowTable) {
2222
- return {
2223
- shape: "arrow-table",
2224
- schema: convertArrowToSchema(arrowTable.schema),
2225
- data: arrowTable
2226
- };
2227
- }
2228
2840
  function convertArrowToArrayRowTable(arrowTable) {
2229
2841
  const columnarTable = convertArrowToColumnarTable(arrowTable);
2230
2842
  return convertTable(columnarTable, "array-row-table");
@@ -2263,80 +2875,13 @@ function convertArrowToGeoJSONTable(arrowTable) {
2263
2875
  };
2264
2876
  }
2265
2877
 
2266
- // ../core/src/schema-utils/lib/table/tables/convert-table.ts
2267
- function convertTable(table, shape) {
2268
- switch (shape) {
2269
- case "object-row-table":
2270
- return convertToObjectRowTable(table);
2271
- case "array-row-table":
2272
- return convertToArrayRowTable(table);
2273
- case "columnar-table":
2274
- return convertToColumnarTable(table);
2275
- case "arrow-table":
2276
- return convertToArrowTable(table);
2277
- default:
2278
- throw new Error(shape);
2279
- }
2280
- }
2281
- function convertToColumnarTable(table) {
2282
- const schema = table.schema || deduceTableSchema(table);
2283
- const fields = schema.fields;
2284
- if (table.shape === "columnar-table") {
2285
- return { ...table, schema };
2286
- }
2287
- const length = getTableLength(table);
2288
- const columns = {};
2289
- for (const field of fields) {
2290
- const column = makeColumnFromField(field, length);
2291
- columns[field.name] = column;
2292
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
2293
- column[rowIndex] = getTableCell(table, rowIndex, field.name);
2294
- }
2295
- }
2296
- return {
2297
- shape: "columnar-table",
2298
- schema,
2299
- data: columns
2300
- };
2301
- }
2302
- function convertToArrayRowTable(table) {
2303
- if (table.shape === "array-row-table") {
2304
- return table;
2305
- }
2306
- const length = getTableLength(table);
2307
- const data = new Array(length);
2308
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
2309
- data[rowIndex] = getTableRowAsArray(table, rowIndex);
2310
- }
2311
- return {
2312
- shape: "array-row-table",
2313
- schema: table.schema,
2314
- data
2315
- };
2316
- }
2317
- function convertToObjectRowTable(table) {
2318
- if (table.shape === "object-row-table") {
2319
- return table;
2320
- }
2321
- const length = getTableLength(table);
2322
- const data = new Array(length);
2323
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
2324
- data[rowIndex] = getTableRowAsObject(table, rowIndex);
2325
- }
2326
- return {
2327
- shape: "object-row-table",
2328
- schema: table.schema,
2329
- data
2330
- };
2878
+ // ../core/src/schema-utils/lib/arrow/arrow-metadata.ts
2879
+ var arrow5 = __toESM(require("apache-arrow"), 1);
2880
+ function snapshotArrowSchema(schema) {
2881
+ return deserializeArrowSchema(serializeArrowSchema(schema));
2331
2882
  }
2332
- function convertToArrowTable(table) {
2333
- const schema = table.schema || deduceTableSchema(table);
2334
- const arrowTable = convertTableToArrow({ ...table, schema });
2335
- return {
2336
- shape: "arrow-table",
2337
- schema,
2338
- data: arrowTable
2339
- };
2883
+ function makeArrowMetadataBatch(schema) {
2884
+ return new arrow5.RecordBatch(snapshotArrowSchema(schema), void 0);
2340
2885
  }
2341
2886
 
2342
2887
  // ../core/src/schema-utils/deprecated.ts
@@ -2344,280 +2889,251 @@ var convertArrowToTable2 = ((...args) => convertArrowToTable(
2344
2889
  ...args
2345
2890
  ));
2346
2891
 
2347
- // ../core/src/scan/rows-to-arrow.ts
2348
- function convertRowsToArrowTable(rows) {
2349
- const table = rows.length > 0 ? makeTableFromData(rows) : { shape: "object-row-table", schema: { fields: [], metadata: {} }, data: rows };
2350
- const schema = deducePrimitiveAwareSchema(table);
2351
- if (table.data.length === 0 || schema.fields.length === 0) {
2352
- return {
2353
- shape: "arrow-table",
2892
+ // ../core/src/scan/query-arrow-table.ts
2893
+ var arrow8 = __toESM(require("apache-arrow"), 1);
2894
+
2895
+ // ../core/src/scan/arrow-execution/project-arrow-table.ts
2896
+ var arrow6 = __toESM(require("apache-arrow"), 1);
2897
+ function projectArrowTable(source, selections) {
2898
+ const outputNames = /* @__PURE__ */ new Set();
2899
+ const indices = [];
2900
+ const fields = selections.map((selection) => {
2901
+ const column = typeof selection === "string" ? selection : selection.column;
2902
+ const name = typeof selection === "string" ? selection : selection.as === void 0 ? column : selection.as;
2903
+ const index = source.schema.fields.findIndex((field) => field.name === column);
2904
+ if (index < 0) throw new Error(`Arrow projection column not found: ${column}`);
2905
+ if (typeof name !== "string" || typeof selection !== "string" && selection.as !== void 0 && !name.length || outputNames.has(name)) {
2906
+ throw new Error(`Invalid or duplicate Arrow projection output name: ${name}`);
2907
+ }
2908
+ outputNames.add(name);
2909
+ indices.push(index);
2910
+ return source.schema.fields[index].clone({ name });
2911
+ });
2912
+ const schema = new arrow6.Schema(fields, source.schema.metadata);
2913
+ const batches = source.batches.map(
2914
+ (batch) => new arrow6.RecordBatch(
2354
2915
  schema,
2355
- data: new arrow5.Table(new arrow5.Schema([]))
2356
- };
2357
- }
2358
- return {
2359
- shape: "arrow-table",
2360
- schema,
2361
- data: arrow5.tableFromJSON(table.data)
2362
- };
2363
- }
2364
- function deducePrimitiveAwareSchema(table) {
2365
- return {
2366
- ...table.schema,
2367
- fields: table.schema.fields.map((field) => {
2368
- if (field.type !== "null") {
2369
- return field;
2370
- }
2371
- const columnValues = table.data.map((row) => row[field.name]);
2372
- const inferredType = getDataTypeFromArray(columnValues);
2373
- return { ...field, type: inferredType.type, nullable: inferredType.nullable };
2374
- })
2375
- };
2916
+ arrow6.makeData({
2917
+ type: new arrow6.Struct(fields),
2918
+ length: batch.numRows,
2919
+ children: indices.map((index) => batch.data.children[index])
2920
+ }),
2921
+ batch.metadata
2922
+ )
2923
+ );
2924
+ return new arrow6.Table(schema, batches);
2376
2925
  }
2377
2926
 
2378
- // dist/sql-utils.js
2379
- function isNodeRuntime() {
2380
- var _a;
2381
- return typeof process !== "undefined" && Boolean((_a = process.versions) == null ? void 0 : _a.node);
2382
- }
2383
- function escapeSqlString(value) {
2384
- return value.replace(/'/g, "''");
2385
- }
2386
- function quoteSqlIdentifier(identifier) {
2387
- return `"${identifier.replace(/"/g, '""')}"`;
2388
- }
2389
- function convertSQLColumnsToSchema(columns) {
2390
- return {
2391
- metadata: {},
2392
- fields: columns.sort((left, right) => (left.ordinalPosition || 0) - (right.ordinalPosition || 0)).map((column) => ({
2393
- name: column.columnName,
2394
- type: mapSQLTypeToDataType(column.sqlType),
2395
- nullable: column.nullable,
2396
- metadata: {
2397
- sqlType: column.sqlType
2398
- }
2399
- }))
2400
- };
2927
+ // ../core/src/scan/arrow-execution/filter-arrow-table.ts
2928
+ var arrow7 = __toESM(require("apache-arrow"), 1);
2929
+ var import_arrow = require("@math.gl/arrow");
2930
+ function filterArrowTable(source, predicate, options = {}) {
2931
+ validateColumnarPredicate(predicate, new Set(source.schema.fields.map((field) => field.name)));
2932
+ const columnNames = getColumnarPredicateColumns(predicate);
2933
+ return filterArrowRows(
2934
+ source,
2935
+ (batch) => {
2936
+ const columns = getPredicateColumns(new arrow7.Table(source.schema, [batch]), columnNames);
2937
+ return (rowIndex) => evaluatePredicate(predicate, columns, rowIndex) === true;
2938
+ },
2939
+ options
2940
+ );
2401
2941
  }
2402
- function mapSQLTypeToDataType(sqlType) {
2403
- const normalizedType = sqlType.toUpperCase();
2404
- if (normalizedType.includes("BIGINT") || normalizedType === "HUGEINT" || normalizedType === "LONG") {
2405
- return "int64";
2942
+ function filterArrowRows(source, selectBatch, options = {}) {
2943
+ var _a, _b, _c;
2944
+ let remaining = options.limit ?? Infinity;
2945
+ if (remaining !== Infinity && (!Number.isSafeInteger(remaining) || remaining < 0)) {
2946
+ throw new Error("Arrow filter limit must be a non-negative safe integer.");
2947
+ }
2948
+ (_a = options.signal) == null ? void 0 : _a.throwIfAborted();
2949
+ const batches = [];
2950
+ for (const batch of source.batches) {
2951
+ if (remaining === 0) break;
2952
+ (_b = options.signal) == null ? void 0 : _b.throwIfAborted();
2953
+ const data = new arrow7.Table(source.schema, [batch]);
2954
+ const selectRow = selectBatch(batch);
2955
+ const indices = [];
2956
+ for (let rowIndex = 0; rowIndex < batch.numRows && indices.length < remaining; rowIndex++) {
2957
+ (_c = options.signal) == null ? void 0 : _c.throwIfAborted();
2958
+ if (selectRow(rowIndex)) indices.push(rowIndex);
2959
+ }
2960
+ remaining -= indices.length;
2961
+ if (indices.length === 0) continue;
2962
+ if (indices.length === batch.numRows) {
2963
+ batches.push(batch);
2964
+ continue;
2965
+ }
2966
+ if (indices[indices.length - 1] - indices[0] + 1 === indices.length) {
2967
+ batches.push(batch.slice(indices[0], indices[indices.length - 1] + 1));
2968
+ continue;
2969
+ }
2970
+ const gathered = (0, import_arrow.takeArrowTableRows)(data, Int32Array.from(indices));
2971
+ batches.push(
2972
+ ...gathered.batches.map(
2973
+ (output) => new arrow7.RecordBatch(source.schema, output.data, batch.metadata)
2974
+ )
2975
+ );
2406
2976
  }
2407
- if (normalizedType.includes("INT") || normalizedType === "INTEGER" || normalizedType === "SMALLINT" || normalizedType === "TINYINT") {
2408
- return "int32";
2977
+ return new arrow7.Table(source.schema, batches);
2978
+ }
2979
+ function getPredicateColumns(sourceTable, columnNames) {
2980
+ const columns = {};
2981
+ for (const columnName of columnNames) {
2982
+ const column = sourceTable.getChild(columnName);
2983
+ if (!column) {
2984
+ throw new Error(`Arrow query column not found: ${columnName}`);
2985
+ }
2986
+ columns[columnName] = column;
2409
2987
  }
2410
- if (normalizedType.includes("DOUBLE") || normalizedType.includes("FLOAT8") || normalizedType.includes("REAL")) {
2411
- return "float64";
2988
+ return columns;
2989
+ }
2990
+ function evaluatePredicate(predicate, columns, rowIndex) {
2991
+ if (isLogicalPredicate(predicate)) {
2992
+ const values = predicate.args.map((child) => evaluatePredicate(child, columns, rowIndex));
2993
+ return predicate.op === "and" ? evaluateAnd(values) : evaluateOr(values);
2412
2994
  }
2413
- if (normalizedType.includes("FLOAT") || normalizedType.includes("DECIMAL") || normalizedType.includes("NUMERIC")) {
2414
- return "float64";
2995
+ if (isNotPredicate(predicate)) {
2996
+ const value2 = evaluatePredicate(predicate.args[0], columns, rowIndex);
2997
+ return value2 === null ? null : !value2;
2415
2998
  }
2416
- if (normalizedType.includes("BOOL")) {
2417
- return "bool";
2999
+ const value = getColumnValue(columns, predicate.args[0].property, rowIndex);
3000
+ if (isNullPredicate(predicate)) {
3001
+ return value === null || value === void 0;
2418
3002
  }
2419
- if (normalizedType.includes("DATE")) {
2420
- return "date-day";
3003
+ if (isInPredicate(predicate)) {
3004
+ return evaluateIn(value, predicate.args[1]);
2421
3005
  }
2422
- if (normalizedType.includes("TIME") || normalizedType.includes("TIMESTAMP")) {
2423
- return "timestamp-millisecond";
3006
+ return evaluateComparison(value, predicate);
3007
+ }
3008
+ function getColumnValue(columns, columnName, rowIndex) {
3009
+ const path = typeof columnName === "string" ? [columnName] : columnName;
3010
+ const column = columns[path[0]];
3011
+ if (!column) {
3012
+ throw new Error(`Arrow query could not read predicate column "${columnName}".`);
2424
3013
  }
2425
- if (normalizedType.includes("BLOB") || normalizedType.includes("BINARY")) {
2426
- return "binary";
3014
+ let value = column.get(rowIndex);
3015
+ for (const property of path.slice(1)) {
3016
+ if (value === null || value === void 0) return null;
3017
+ value = value[property];
2427
3018
  }
2428
- return "utf8";
2429
- }
2430
- function getQualifiedTableName(options) {
2431
- const identifiers = [options.catalogName, options.schemaName, options.tableName].filter((value) => Boolean(value));
2432
- return identifiers.map((identifier) => quoteSqlIdentifier(identifier)).join(".");
2433
- }
2434
-
2435
- // ../core/src/scan/table-query-capabilities.ts
2436
- var ARROW_TABLE_QUERY_CAPABILITIES = Object.freeze({
2437
- projection: "residual",
2438
- predicate: "residual",
2439
- limit: "residual",
2440
- streaming: false,
2441
- cancellation: true,
2442
- expressions: "residual",
2443
- orderBy: "residual",
2444
- aggregates: "residual"
2445
- });
2446
- var SQL_DATA_SOURCE_TABLE_QUERY_CAPABILITIES = Object.freeze({
2447
- projection: "pushdown",
2448
- predicate: "pushdown",
2449
- limit: "pushdown",
2450
- streaming: false,
2451
- cancellation: false,
2452
- expressions: "pushdown",
2453
- orderBy: "pushdown",
2454
- aggregates: "pushdown"
2455
- });
2456
-
2457
- // dist/sql-source.js
2458
- var SQL_ADAPTER_FACTORIES = /* @__PURE__ */ new Map();
2459
- function registerSQLAdapter(type, factory) {
2460
- SQL_ADAPTER_FACTORIES.set(type, factory);
3019
+ return value;
2461
3020
  }
2462
- function getSQLAdapterFactory(type) {
2463
- return SQL_ADAPTER_FACTORIES.get(type);
3021
+ function evaluateAnd(values) {
3022
+ if (values.includes(false)) {
3023
+ return false;
3024
+ }
3025
+ return values.includes(null) ? null : true;
2464
3026
  }
2465
- var SQL_SOURCE_DEFAULT_OPTIONS = {
2466
- sql: {
2467
- resultFormat: "auto",
2468
- adapterOptions: {},
2469
- browserAdapterFactory: void 0,
2470
- nodeAdapterFactory: void 0
2471
- },
2472
- duckdb: {
2473
- accessMode: "auto",
2474
- bundles: void 0,
2475
- databasePath: void 0,
2476
- remoteUrl: void 0,
2477
- workerUrl: void 0
2478
- },
2479
- snowflake: {
2480
- account: void 0,
2481
- database: void 0,
2482
- schema: void 0,
2483
- token: void 0,
2484
- warehouse: void 0,
2485
- role: void 0,
2486
- authenticator: void 0
3027
+ function evaluateOr(values) {
3028
+ if (values.includes(true)) {
3029
+ return true;
2487
3030
  }
2488
- };
2489
- var SQLDataSource = class extends DataSource {
2490
- /** Optimization and execution capabilities of portable table-bound queries. */
2491
- tableQueryCapabilities = SQL_DATA_SOURCE_TABLE_QUERY_CAPABILITIES;
2492
- sourceType;
2493
- adapterFactory;
2494
- adapterPromise = null;
2495
- metadataPromise = null;
2496
- constructor(data, options, sourceType, defaultOptions = SQL_SOURCE_DEFAULT_OPTIONS, adapterFactory, coreApi) {
2497
- super(data, options, defaultOptions, coreApi);
2498
- this.sourceType = sourceType;
2499
- this.adapterFactory = adapterFactory || getSQLAdapterFactory(sourceType);
3031
+ return values.includes(null) ? null : false;
3032
+ }
3033
+ function evaluateIn(value, candidates) {
3034
+ if (value === null || value === void 0) {
3035
+ return null;
2500
3036
  }
2501
- /** Returns cached high-level metadata for the SQL source. */
2502
- async getMetadata() {
2503
- if (!this.metadataPromise) {
2504
- this.metadataPromise = this.loadMetadata();
3037
+ for (const candidate of candidates) {
3038
+ if (compareValues(value, candidate) === 0) {
3039
+ return true;
2505
3040
  }
2506
- return this.metadataPromise;
2507
3041
  }
2508
- /** Lists available catalogs from the connected database. */
2509
- async listCatalogs() {
2510
- const adapter = await this.getAdapter();
2511
- return await adapter.listCatalogs();
3042
+ return false;
3043
+ }
3044
+ function evaluateComparison(value, predicate) {
3045
+ if (value === null || value === void 0) {
3046
+ return null;
2512
3047
  }
2513
- /** Lists available schemas from the connected database. */
2514
- async listSchemas(catalogName) {
2515
- const adapter = await this.getAdapter();
2516
- return await adapter.listSchemas(catalogName);
3048
+ const comparison = compareValues(value, predicate.args[1]);
3049
+ switch (predicate.op) {
3050
+ case "=":
3051
+ return comparison === 0;
3052
+ case "<>":
3053
+ return comparison !== 0;
3054
+ case "<":
3055
+ return comparison < 0;
3056
+ case "<=":
3057
+ return comparison <= 0;
3058
+ case ">":
3059
+ return comparison > 0;
3060
+ case ">=":
3061
+ return comparison >= 0;
2517
3062
  }
2518
- /** Lists available tables from the connected database. */
2519
- async listTables(options) {
2520
- const adapter = await this.getAdapter();
2521
- return await adapter.listTables(options);
3063
+ }
3064
+ function compareValues(left, right) {
3065
+ if (isColumnarPredicateParameter(right)) {
3066
+ throw new Error(
3067
+ `Arrow query parameter ":${right.parameter}" must be bound before query execution.`
3068
+ );
2522
3069
  }
2523
- /** Returns the schema for a specific SQL table. */
2524
- async getTableSchema(options) {
2525
- const adapter = await this.getAdapter();
2526
- return await adapter.getTableSchema(options);
3070
+ if (left instanceof Date && right instanceof Date) {
3071
+ return compareArrowQueryNumbers(left.getTime(), right.getTime());
2527
3072
  }
2528
- /** Executes a SQL query and returns object rows. */
2529
- async queryRows(query, options = {}) {
2530
- const adapter = await this.getAdapter();
2531
- const compiledQuery = this.compileQuery(query, options);
2532
- return await adapter.executeRows(compiledQuery.sql, compiledQuery.options);
3073
+ if (typeof left === "number" && right instanceof Date) {
3074
+ return compareArrowQueryNumbers(left, right.getTime());
2533
3075
  }
2534
- /** Executes a SQL query and returns a loaders.gl Arrow table. */
2535
- async queryArrow(query, options = {}) {
2536
- const adapter = await this.getAdapter();
2537
- const compiledQuery = this.compileQuery(query, options);
2538
- if (adapter.executeArrow) {
2539
- return await adapter.executeArrow(compiledQuery.sql, compiledQuery.options);
2540
- }
2541
- const rows = await adapter.executeRows(compiledQuery.sql, compiledQuery.options);
2542
- return convertRowsToArrowTable(rows);
3076
+ if (left instanceof Uint8Array && right instanceof Uint8Array) {
3077
+ return compareBinary(left, right);
2543
3078
  }
2544
- /** Closes any underlying adapter connection. */
2545
- async close() {
2546
- if (!this.adapterPromise) {
2547
- return;
2548
- }
2549
- const adapter = await this.adapterPromise;
2550
- await adapter.close();
2551
- this.adapterPromise = null;
2552
- this.metadataPromise = null;
3079
+ if (typeof left === "bigint" && typeof right === "bigint") {
3080
+ return left === right ? 0 : left < right ? -1 : 1;
2553
3081
  }
2554
- async getAdapter() {
2555
- if (!this.adapterPromise) {
2556
- this.adapterPromise = this.loadAdapter();
2557
- }
2558
- return await this.adapterPromise;
3082
+ if (typeof left === "bigint" && typeof right === "number" || typeof left === "number" && typeof right === "bigint") {
3083
+ const numericValue = typeof left === "number" ? left : right;
3084
+ if (!Number.isFinite(numericValue))
3085
+ throw new Error("Arrow query cannot compare non-finite numbers.");
3086
+ return left < right ? -1 : left > right ? 1 : 0;
2559
3087
  }
2560
- /** Converts a portable query object to SQL while leaving raw SQL text unchanged. */
2561
- compileQuery(query, options) {
2562
- if (typeof query === "string") {
2563
- return { sql: query, options };
2564
- }
2565
- const namedParameters = options.parameters && !Array.isArray(options.parameters) ? options.parameters : void 0;
2566
- const compiledQuery = compileSQLTableQuery(query, {
2567
- dialect: getSQLTableQueryDialect(this.sourceType),
2568
- parameters: namedParameters
2569
- });
2570
- return {
2571
- sql: compiledQuery.sql,
2572
- options: { signal: options.signal, parameters: compiledQuery.parameters }
2573
- };
3088
+ if (typeof left === "number" && typeof right === "number") {
3089
+ return compareArrowQueryNumbers(left, right);
2574
3090
  }
2575
- async loadAdapter() {
2576
- var _a, _b;
2577
- try {
2578
- const runtimeFactory = isNodeRuntime() ? (_a = this.options.sql) == null ? void 0 : _a.nodeAdapterFactory : (_b = this.options.sql) == null ? void 0 : _b.browserAdapterFactory;
2579
- const factory = runtimeFactory || this.adapterFactory;
2580
- if (!factory) {
2581
- throw new Error(`No SQL adapter is registered for source type "${this.sourceType}".`);
2582
- }
2583
- const adapter = await factory({
2584
- coreApi: this.coreApi,
2585
- options: this.options,
2586
- sourceType: this.sourceType,
2587
- url: this.url
2588
- });
2589
- await adapter.connect();
2590
- return adapter;
2591
- } catch (error) {
2592
- throw this.reportError(error, `Failed to create SQL adapter for ${this.sourceType} from ${this.url}`);
3091
+ if (typeof left === "string" && typeof right === "string") {
3092
+ return left === right ? 0 : left < right ? -1 : 1;
3093
+ }
3094
+ if (typeof left === "boolean" && typeof right === "boolean") {
3095
+ return Number(left) - Number(right);
3096
+ }
3097
+ throw new Error(
3098
+ `Arrow query cannot compare ${getArrowQueryValueType(left)} with ${getArrowQueryValueType(right)} values.`
3099
+ );
3100
+ }
3101
+ function compareArrowQueryNumbers(left, right) {
3102
+ if (!Number.isFinite(left) || !Number.isFinite(right)) {
3103
+ throw new Error("Arrow query cannot compare non-finite numbers.");
3104
+ }
3105
+ return left === right ? 0 : left < right ? -1 : 1;
3106
+ }
3107
+ function compareBinary(left, right) {
3108
+ const commonLength = Math.min(left.length, right.length);
3109
+ for (let index = 0; index < commonLength; index++) {
3110
+ if (left[index] !== right[index]) {
3111
+ return left[index] < right[index] ? -1 : 1;
2593
3112
  }
2594
3113
  }
2595
- async loadMetadata() {
2596
- const adapter = await this.getAdapter();
2597
- const [catalogs, schemas, tables] = await Promise.all([
2598
- adapter.listCatalogs(),
2599
- adapter.listSchemas(),
2600
- adapter.listTables()
2601
- ]);
2602
- return {
2603
- type: this.sourceType,
2604
- capabilities: adapter.capabilities,
2605
- catalogs,
2606
- schemas,
2607
- tables
2608
- };
3114
+ return left.length === right.length ? 0 : left.length < right.length ? -1 : 1;
3115
+ }
3116
+ function getArrowQueryValueType(value) {
3117
+ if (value instanceof Date) {
3118
+ return "Date";
2609
3119
  }
2610
- };
2611
- function getSQLTableQueryDialect(sourceType) {
2612
- if (sourceType === "duckdb-sql")
2613
- return "duckdb";
2614
- if (sourceType === "snowflake-sql")
2615
- return "snowflake";
2616
- throw new Error(`Portable table queries are not supported for SQL source type "${sourceType}".`);
3120
+ if (value instanceof Uint8Array) {
3121
+ return "Uint8Array";
3122
+ }
3123
+ return typeof value;
3124
+ }
3125
+ function isLogicalPredicate(predicate) {
3126
+ return predicate.op === "and" || predicate.op === "or";
3127
+ }
3128
+ function isNotPredicate(predicate) {
3129
+ return predicate.op === "not";
3130
+ }
3131
+ function isNullPredicate(predicate) {
3132
+ return predicate.op === "isNull";
3133
+ }
3134
+ function isInPredicate(predicate) {
3135
+ return predicate.op === "in";
2617
3136
  }
2618
-
2619
- // ../core/src/scan/query-arrow-table.ts
2620
- var arrow6 = __toESM(require("apache-arrow"), 1);
2621
3137
 
2622
3138
  // ../core/src/scan/table-query.ts
2623
3139
  function planTableQuery2(sourceColumnNames, options = {}) {
@@ -2642,11 +3158,36 @@ function getSQLPredicateColumnNames(predicate) {
2642
3158
 
2643
3159
  // ../core/src/scan/query-arrow-table.ts
2644
3160
  function explainArrowTableQuery(sourceTable, options = {}) {
3161
+ if (options.select !== void 0) {
3162
+ if (options.columns !== void 0 || hasRelationalOperators(options) || options.join) {
3163
+ throw new Error("Arrow select cannot be combined with columns or relational operators.");
3164
+ }
3165
+ const projected = projectArrowTable(sourceTable, options.select);
3166
+ const columns = [
3167
+ ...new Set(
3168
+ options.select.map(
3169
+ (selection) => typeof selection === "string" ? selection : selection.column
3170
+ )
3171
+ )
3172
+ ];
3173
+ const { select, ...remainingOptions } = options;
3174
+ const explanation = explainArrowTableQuery(sourceTable, { ...remainingOptions, columns });
3175
+ const outputColumns = Object.freeze(projected.schema.fields.map((field) => field.name));
3176
+ return Object.freeze({
3177
+ ...explanation,
3178
+ outputColumns,
3179
+ plan: Object.freeze(
3180
+ explanation.plan.map(
3181
+ (step) => step.kind === "project" ? Object.freeze({ ...step, columns: outputColumns, select }) : step
3182
+ )
3183
+ )
3184
+ });
3185
+ }
2645
3186
  if (options.predicate) {
2646
3187
  validateSQLPredicate(options.predicate);
2647
3188
  }
2648
3189
  return explainTableQuery(
2649
- sourceTable.data.schema.fields.map((field) => field.name),
3190
+ sourceTable.schema.fields.map((field) => field.name),
2650
3191
  options,
2651
3192
  ARROW_TABLE_QUERY_CAPABILITIES
2652
3193
  );
@@ -2654,6 +3195,22 @@ function explainArrowTableQuery(sourceTable, options = {}) {
2654
3195
  function queryArrowTable(sourceTable, options = {}) {
2655
3196
  var _a, _b;
2656
3197
  throwIfAborted(options.signal);
3198
+ if (options.select !== void 0) {
3199
+ if (options.columns !== void 0 || hasRelationalOperators(options) || options.join) {
3200
+ throw new Error("Arrow select cannot be combined with columns or relational operators.");
3201
+ }
3202
+ projectArrowTable(sourceTable, options.select);
3203
+ const columns = [
3204
+ ...new Set(
3205
+ options.select.map(
3206
+ (selection) => typeof selection === "string" ? selection : selection.column
3207
+ )
3208
+ )
3209
+ ];
3210
+ const { select, ...remainingOptions } = options;
3211
+ const result = queryArrowTable(sourceTable, { ...remainingOptions, columns });
3212
+ return projectArrowTable(result, select);
3213
+ }
2657
3214
  if (options.join) {
2658
3215
  validateArrowJoinQuery(sourceTable, options);
2659
3216
  }
@@ -2666,56 +3223,37 @@ function queryArrowTable(sourceTable, options = {}) {
2666
3223
  if (hasRelationalOperators(options)) {
2667
3224
  return queryArrowTableRelational(sourceTable, options);
2668
3225
  }
2669
- const sourceData = sourceTable.data;
3226
+ const sourceData = sourceTable;
2670
3227
  const plan = planTableQuery2(
2671
3228
  sourceData.schema.fields.map((field) => field.name),
2672
3229
  options
2673
3230
  );
2674
3231
  const scannedData = sourceData.select([...getScanStep(plan).columns]);
2675
- const projectedData = scannedData.select([...getProjectStep(plan).columns]);
2676
3232
  const predicate = (_a = getFilterStep(plan)) == null ? void 0 : _a.predicate;
2677
3233
  const limit = ((_b = getLimitStep(plan)) == null ? void 0 : _b.limit) ?? Number.POSITIVE_INFINITY;
2678
3234
  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));
3235
+ const projectedData = projectArrowTable(scannedData, getProjectStep(plan).columns);
3236
+ return projectedData.numRows === 0 ? projectedData : projectedData.slice(0, limit);
2691
3237
  }
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);
2700
- }
2701
- return wrapArrowTable(new arrow6.Table(projectedData.schema, outputColumns));
3238
+ const filteredData = filterArrowTable(scannedData, predicate, { limit, signal: options.signal });
3239
+ return projectArrowTable(filteredData, getProjectStep(plan).columns);
2702
3240
  }
2703
3241
  function queryArrowTableRelational(sourceTable, options) {
2704
3242
  var _a, _b, _c, _d, _e, _f;
2705
- const sourceData = sourceTable.data;
3243
+ const sourceData = sourceTable;
2706
3244
  const sourceColumnNames = sourceData.schema.fields.map((field) => field.name);
2707
3245
  const relationalPlan = planRelationalQuery(sourceColumnNames, options);
2708
3246
  const scanStep = relationalPlan.tablePlan.find((step) => step.kind === "scan");
2709
3247
  if (!scanStep || scanStep.kind !== "scan") {
2710
3248
  throw new Error("Arrow relational query plan is missing a scan step.");
2711
3249
  }
2712
- const scannedTable = wrapArrowTable(sourceData.select([...scanStep.columns]));
3250
+ const scannedTable = sourceData.select([...scanStep.columns]);
2713
3251
  const filteredTable = queryArrowTable(scannedTable, {
2714
3252
  predicate: options.predicate,
2715
3253
  columns: scanStep.columns,
2716
3254
  signal: options.signal
2717
3255
  });
2718
- let rows = arrowTableToRows(filteredTable.data);
3256
+ let rows = arrowTableToRows(filteredTable);
2719
3257
  for (const child of options.union || []) {
2720
3258
  const childTable = (_a = options.tables) == null ? void 0 : _a[child.source];
2721
3259
  if (!childTable) throw new Error(`Arrow union source not found: ${child.source}`);
@@ -2724,7 +3262,7 @@ function queryArrowTableRelational(sourceTable, options) {
2724
3262
  columns: getUnionChildColumns(sourceColumnNames, options),
2725
3263
  signal: options.signal
2726
3264
  });
2727
- rows = rows.concat(arrowTableToRows(childResult.data));
3265
+ rows = rows.concat(arrowTableToRows(childResult));
2728
3266
  }
2729
3267
  for (const expression of options.expressions || []) {
2730
3268
  for (const row of rows) {
@@ -2807,12 +3345,12 @@ function queryArrowJoin(sourceTable, options) {
2807
3345
  ...join.child.query || {},
2808
3346
  signal: options.signal
2809
3347
  });
2810
- const leftRows = arrowTableToRows(leftResult.data);
2811
- const rightRows = arrowTableToRows(rightResult.data);
3348
+ const leftRows = arrowTableToRows(leftResult);
3349
+ const rightRows = arrowTableToRows(rightResult);
2812
3350
  const rightPrefix = `${join.child.source}.`;
2813
3351
  const defaultOutputColumns = [
2814
- ...sourceTable.data.schema.fields.map((field) => field.name),
2815
- ...rightResult.data.schema.fields.map((field) => `${rightPrefix}${field.name}`)
3352
+ ...sourceTable.schema.fields.map((field) => field.name),
3353
+ ...rightResult.schema.fields.map((field) => `${rightPrefix}${field.name}`)
2816
3354
  ];
2817
3355
  const rows = [];
2818
3356
  for (const leftRow of leftRows) {
@@ -2833,13 +3371,13 @@ function queryArrowJoin(sourceTable, options) {
2833
3371
  );
2834
3372
  const limitedRows = outputRows.slice(0, options.limit);
2835
3373
  return limitedRows.length ? convertRowsToArrowTable(limitedRows) : createEmptyRelationalResult(
2836
- sourceTable.data,
3374
+ sourceTable,
2837
3375
  outputColumns,
2838
3376
  options,
2839
3377
  new Map(
2840
- rightResult.data.schema.fields.map((field) => [
3378
+ rightResult.schema.fields.map((field) => [
2841
3379
  `${rightPrefix}${field.name}`,
2842
- new arrow6.Field(
3380
+ new arrow8.Field(
2843
3381
  `${rightPrefix}${field.name}`,
2844
3382
  field.type,
2845
3383
  field.nullable,
@@ -2853,10 +3391,10 @@ function validateArrowJoinQuery(sourceTable, options) {
2853
3391
  var _a, _b, _c;
2854
3392
  const join = options.join;
2855
3393
  if (!join) return;
2856
- const sourceColumnNames = sourceTable.data.schema.fields.map((field) => field.name);
3394
+ const sourceColumnNames = sourceTable.schema.fields.map((field) => field.name);
2857
3395
  const childTable = (_a = options.tables) == null ? void 0 : _a[join.child.source];
2858
3396
  if (!childTable) throw new Error(`Arrow join source not found: ${join.child.source}`);
2859
- const childColumnNames = childTable.data.schema.fields.map((field) => field.name);
3397
+ const childColumnNames = childTable.schema.fields.map((field) => field.name);
2860
3398
  if (!sourceColumnNames.includes(join.left)) {
2861
3399
  throw new Error(`Arrow join column not found: ${join.left}`);
2862
3400
  }
@@ -2936,265 +3474,113 @@ function compareRows(left, right, orderBy) {
2936
3474
  if (leftValue === null || leftValue === void 0 || rightValue === null || rightValue === void 0) {
2937
3475
  if (leftValue === rightValue) continue;
2938
3476
  const nullResult = leftValue === null || leftValue === void 0 ? -1 : 1;
2939
- return nulls === "first" ? nullResult : -nullResult;
2940
- }
2941
- const result = compareSortValues(leftValue, rightValue);
2942
- if (result) return (key.direction === "desc" ? -1 : 1) * result;
2943
- }
2944
- return 0;
2945
- }
2946
- function compareSortValues(left, right) {
2947
- if (left instanceof Date && right instanceof Date)
2948
- return compareNumbers(left.getTime(), right.getTime());
2949
- if (typeof left === "bigint" && typeof right === "bigint")
2950
- return left === right ? 0 : left < right ? -1 : 1;
2951
- if (typeof left === "number" && typeof right === "number") return compareNumbers(left, right);
2952
- if (typeof left === "string" && typeof right === "string")
2953
- return left === right ? 0 : left < right ? -1 : 1;
2954
- if (typeof left === "boolean" && typeof right === "boolean") return Number(left) - Number(right);
2955
- throw new Error(
2956
- `Arrow relational order cannot compare ${getValueType(left)} with ${getValueType(right)}.`
2957
- );
2958
- }
2959
- function aggregateRows(rows, groupBy, aggregates) {
2960
- const groups = /* @__PURE__ */ new Map();
2961
- for (const row of rows) {
2962
- const key = serializeGroupKey(groupBy.map((column) => row[column]));
2963
- const group = groups.get(key) || [];
2964
- group.push(row);
2965
- groups.set(key, group);
2966
- }
2967
- if (!groupBy.length && !rows.length) groups.set("[]", []);
2968
- return [...groups.values()].map((group) => {
2969
- var _a;
2970
- const output = {};
2971
- for (const column of groupBy) output[column] = ((_a = group[0]) == null ? void 0 : _a[column]) ?? null;
2972
- for (const aggregate of aggregates)
2973
- output[aggregate.name] = evaluateAggregate(aggregate, group);
2974
- return output;
2975
- });
2976
- }
2977
- function serializeGroupKey(values) {
2978
- return values.map((value) => {
2979
- if (value === null || value === void 0) return "null:";
2980
- if (value instanceof Date) return `date:${value.getTime()}`;
2981
- if (value instanceof Uint8Array) return `bytes:${Array.from(value).join(".")}`;
2982
- return `${typeof value}:${String(value)}`;
2983
- }).join("");
2984
- }
2985
- function createEmptyRelationalResult(sourceData, outputColumns, options, additionalFields) {
2986
- const sourceFields = new Map(sourceData.schema.fields.map((field) => [field.name, field]));
2987
- const expressions = new Map(
2988
- (options.expressions || []).map((expression) => [expression.name, expression])
2989
- );
2990
- const aggregates = new Map(
2991
- (options.aggregates || []).map((aggregate) => [aggregate.name, aggregate])
2992
- );
2993
- const fields = outputColumns.map((columnName) => {
2994
- const sourceField = sourceFields.get(columnName);
2995
- if (sourceField) return sourceField;
2996
- const additionalField = additionalFields == null ? void 0 : additionalFields.get(columnName);
2997
- if (additionalField) return additionalField;
2998
- const expression = expressions.get(columnName);
2999
- if (expression)
3000
- return new arrow6.Field(columnName, getExpressionType(expression, sourceFields), true);
3001
- const aggregate = aggregates.get(columnName);
3002
- if (aggregate)
3003
- return new arrow6.Field(columnName, getAggregateType(aggregate, sourceFields), true);
3004
- return new arrow6.Field(columnName, new arrow6.Utf8(), true);
3005
- });
3006
- 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));
3009
- }
3010
- function getExpressionType(expression, sourceFields) {
3011
- var _a;
3012
- if (expression.expression.op === "literal") {
3013
- 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();
3017
- }
3018
- if (expression.expression.op === "column") {
3019
- return ((_a = sourceFields.get(expression.expression.column)) == null ? void 0 : _a.type) || new arrow6.Utf8();
3020
- }
3021
- return new arrow6.Float64();
3022
- }
3023
- function getAggregateType(aggregate, sourceFields) {
3024
- 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();
3027
- }
3028
- function evaluateAggregate(aggregate, rows) {
3029
- if (aggregate.function === "count")
3030
- return aggregate.column ? rows.filter((row) => row[aggregate.column] != null).length : rows.length;
3031
- const values = rows.map((row) => row[aggregate.column]).filter((value) => value != null);
3032
- if (!values.length) return null;
3033
- if (!values.every((value) => typeof value === "number"))
3034
- throw new Error(`Arrow aggregate ${aggregate.function} requires numeric values.`);
3035
- const numericValues = values;
3036
- switch (aggregate.function) {
3037
- case "sum":
3038
- return numericValues.reduce((sum, value) => sum + value, 0);
3039
- case "avg":
3040
- return numericValues.reduce((sum, value) => sum + value, 0) / numericValues.length;
3041
- case "min":
3042
- return Math.min(...numericValues);
3043
- case "max":
3044
- return Math.max(...numericValues);
3045
- }
3046
- }
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);
3477
+ return nulls === "first" ? nullResult : -nullResult;
3478
+ }
3479
+ const result = compareSortValues(leftValue, rightValue);
3480
+ if (result) return (key.direction === "desc" ? -1 : 1) * result;
3140
3481
  }
3141
- if (typeof left === "bigint" && typeof right === "bigint") {
3482
+ return 0;
3483
+ }
3484
+ function compareSortValues(left, right) {
3485
+ if (left instanceof Date && right instanceof Date)
3486
+ return compareArrowQueryNumbers(left.getTime(), right.getTime());
3487
+ if (typeof left === "bigint" && typeof right === "bigint")
3142
3488
  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") {
3489
+ if (typeof left === "number" && typeof right === "number") return compareArrowQueryNumbers(left, right);
3490
+ if (typeof left === "string" && typeof right === "string")
3154
3491
  return left === right ? 0 : left < right ? -1 : 1;
3155
- }
3156
- if (typeof left === "boolean" && typeof right === "boolean") {
3157
- return Number(left) - Number(right);
3158
- }
3492
+ if (typeof left === "boolean" && typeof right === "boolean") return Number(left) - Number(right);
3159
3493
  throw new Error(
3160
- `Arrow query cannot compare ${getValueType(left)} with ${getValueType(right)} values.`
3494
+ `Arrow relational order cannot compare ${getArrowQueryValueType(left)} with ${getArrowQueryValueType(right)}.`
3161
3495
  );
3162
3496
  }
3163
- function compareNumbers(left, right) {
3164
- if (!Number.isFinite(left) || !Number.isFinite(right)) {
3165
- throw new Error("Arrow query cannot compare non-finite numbers.");
3497
+ function aggregateRows(rows, groupBy, aggregates) {
3498
+ const groups = /* @__PURE__ */ new Map();
3499
+ for (const row of rows) {
3500
+ const key = serializeGroupKey(groupBy.map((column) => row[column]));
3501
+ const group = groups.get(key) || [];
3502
+ group.push(row);
3503
+ groups.set(key, group);
3166
3504
  }
3167
- return left === right ? 0 : left < right ? -1 : 1;
3505
+ if (!groupBy.length && !rows.length) groups.set("[]", []);
3506
+ return [...groups.values()].map((group) => {
3507
+ var _a;
3508
+ const output = {};
3509
+ for (const column of groupBy) output[column] = ((_a = group[0]) == null ? void 0 : _a[column]) ?? null;
3510
+ for (const aggregate of aggregates)
3511
+ output[aggregate.name] = evaluateAggregate(aggregate, group);
3512
+ return output;
3513
+ });
3168
3514
  }
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;
3515
+ function serializeGroupKey(values) {
3516
+ return values.map((value) => {
3517
+ if (value === null || value === void 0) return "null:";
3518
+ if (value instanceof Date) return `date:${value.getTime()}`;
3519
+ if (value instanceof Uint8Array) return `bytes:${Array.from(value).join(".")}`;
3520
+ return `${typeof value}:${String(value)}`;
3521
+ }).join("");
3177
3522
  }
3178
- function getValueType(value) {
3179
- if (value instanceof Date) {
3180
- return "Date";
3523
+ function createEmptyRelationalResult(sourceData, outputColumns, options, additionalFields) {
3524
+ const sourceFields = new Map(sourceData.schema.fields.map((field) => [field.name, field]));
3525
+ const expressions = new Map(
3526
+ (options.expressions || []).map((expression) => [expression.name, expression])
3527
+ );
3528
+ const aggregates = new Map(
3529
+ (options.aggregates || []).map((aggregate) => [aggregate.name, aggregate])
3530
+ );
3531
+ const fields = outputColumns.map((columnName) => {
3532
+ const sourceField = sourceFields.get(columnName);
3533
+ if (sourceField) return sourceField;
3534
+ const additionalField = additionalFields == null ? void 0 : additionalFields.get(columnName);
3535
+ if (additionalField) return additionalField;
3536
+ const expression = expressions.get(columnName);
3537
+ if (expression)
3538
+ return new arrow8.Field(columnName, getExpressionType(expression, sourceFields), true);
3539
+ const aggregate = aggregates.get(columnName);
3540
+ if (aggregate)
3541
+ return new arrow8.Field(columnName, getAggregateType(aggregate, sourceFields), true);
3542
+ return new arrow8.Field(columnName, new arrow8.Utf8(), true);
3543
+ });
3544
+ const columns = {};
3545
+ for (const field of fields) columns[field.name] = arrow8.vectorFromArray([], field.type);
3546
+ return new arrow8.Table(new arrow8.Schema(fields), columns);
3547
+ }
3548
+ function getExpressionType(expression, sourceFields) {
3549
+ var _a;
3550
+ if (expression.expression.op === "literal") {
3551
+ const value = expression.expression.value;
3552
+ if (typeof value === "boolean") return new arrow8.Bool();
3553
+ if (typeof value === "number") return new arrow8.Float64();
3554
+ return new arrow8.Utf8();
3181
3555
  }
3182
- if (value instanceof Uint8Array) {
3183
- return "Uint8Array";
3556
+ if (expression.expression.op === "column") {
3557
+ return ((_a = sourceFields.get(expression.expression.column)) == null ? void 0 : _a.type) || new arrow8.Utf8();
3184
3558
  }
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";
3559
+ return new arrow8.Float64();
3192
3560
  }
3193
- function isNullPredicate(predicate) {
3194
- return predicate.op === "isNull";
3561
+ function getAggregateType(aggregate, sourceFields) {
3562
+ var _a;
3563
+ if (aggregate.function === "count") return new arrow8.Int32();
3564
+ return aggregate.column ? ((_a = sourceFields.get(aggregate.column)) == null ? void 0 : _a.type) || new arrow8.Float64() : new arrow8.Float64();
3195
3565
  }
3196
- function isInPredicate(predicate) {
3197
- return predicate.op === "in";
3566
+ function evaluateAggregate(aggregate, rows) {
3567
+ if (aggregate.function === "count")
3568
+ return aggregate.column ? rows.filter((row) => row[aggregate.column] != null).length : rows.length;
3569
+ const values = rows.map((row) => row[aggregate.column]).filter((value) => value != null);
3570
+ if (!values.length) return null;
3571
+ if (!values.every((value) => typeof value === "number"))
3572
+ throw new Error(`Arrow aggregate ${aggregate.function} requires numeric values.`);
3573
+ const numericValues = values;
3574
+ switch (aggregate.function) {
3575
+ case "sum":
3576
+ return numericValues.reduce((sum, value) => sum + value, 0);
3577
+ case "avg":
3578
+ return numericValues.reduce((sum, value) => sum + value, 0) / numericValues.length;
3579
+ case "min":
3580
+ return Math.min(...numericValues);
3581
+ case "max":
3582
+ return Math.max(...numericValues);
3583
+ }
3198
3584
  }
3199
3585
  function getScanStep(plan) {
3200
3586
  const step = plan.find((candidate) => candidate.kind === "scan");
@@ -3225,13 +3611,6 @@ function throwIfAborted(signal) {
3225
3611
  throw error;
3226
3612
  }
3227
3613
  }
3228
- function wrapArrowTable(data) {
3229
- return {
3230
- shape: "arrow-table",
3231
- schema: convertArrowToSchema(data.schema),
3232
- data
3233
- };
3234
- }
3235
3614
 
3236
3615
  // dist/arrow-table-source.js
3237
3616
  var ArrowTableSource = class extends DataSource {
@@ -3242,7 +3621,7 @@ var ArrowTableSource = class extends DataSource {
3242
3621
  /** Discovers query-visible fields and capabilities without evaluating any rows. */
3243
3622
  async getQueryMetadata(options = {}) {
3244
3623
  throwIfAborted2(options.signal);
3245
- const schema = convertArrowToSchema(this.data.data.schema);
3624
+ const schema = convertArrowToSchema(this.data.schema);
3246
3625
  return createScanQueryMetadata({
3247
3626
  sourceType: "arrow-table",
3248
3627
  queryType: "table",
@@ -3250,7 +3629,7 @@ var ArrowTableSource = class extends DataSource {
3250
3629
  schema,
3251
3630
  capabilities: { table: ARROW_TABLE_QUERY_CAPABILITIES },
3252
3631
  columnRoles: getArrowColumnRoles(schema.fields.map((field) => field.name)),
3253
- statistics: { rowCount: this.data.data.numRows }
3632
+ statistics: { rowCount: this.data.numRows }
3254
3633
  });
3255
3634
  }
3256
3635
  /** Executes a portable query and returns an Arrow table result. */
@@ -3263,8 +3642,11 @@ var ArrowTableSource = class extends DataSource {
3263
3642
  }
3264
3643
  /** Executes a query as one bounded Arrow batch. */
3265
3644
  async *read(options = {}) {
3266
- const result = this.query(options);
3267
- yield makeTableScanBatch(result);
3645
+ const data = this.query(options);
3646
+ if (data.batches.length)
3647
+ yield* data.batches;
3648
+ else
3649
+ yield makeArrowMetadataBatch(data.schema);
3268
3650
  }
3269
3651
  };
3270
3652
  function getArrowColumnRoles(columnNames) {
@@ -3290,8 +3672,219 @@ function throwIfAborted2(signal) {
3290
3672
  }
3291
3673
 
3292
3674
  // dist/duckdb-sql-source.js
3675
+ var import_apache_arrow2 = require("apache-arrow");
3676
+
3677
+ // dist/duckdb-table-query.js
3293
3678
  var import_apache_arrow = require("apache-arrow");
3294
- var VERSION2 = true ? "5.0.0-beta.1" : "latest";
3679
+ var SCALAR_TYPES = {
3680
+ TINYINT: "int8",
3681
+ SMALLINT: "int16",
3682
+ INTEGER: "int32",
3683
+ BIGINT: "int64",
3684
+ FLOAT: "float32",
3685
+ REAL: "float32",
3686
+ DOUBLE: "float64",
3687
+ BOOLEAN: "bool",
3688
+ VARCHAR: "utf8"
3689
+ };
3690
+ var INTEGER_TYPES = { int8: "TINYINT", int16: "SMALLINT", int32: "INTEGER", int64: "BIGINT" };
3691
+ function getIntegerType(type) {
3692
+ return typeof type === "string" && Object.hasOwn(INTEGER_TYPES, type) ? INTEGER_TYPES[type] : void 0;
3693
+ }
3694
+ function isDuckDBQuerySchema(schema) {
3695
+ return schema.fields.length > 0 && schema.fields.every((field) => {
3696
+ var _a;
3697
+ return Object.hasOwn(SCALAR_TYPES, ((_a = field.metadata) == null ? void 0 : _a.sqlType) || "");
3698
+ });
3699
+ }
3700
+ function makeDuckDBTableQuerySource(source, table, sourceSchema) {
3701
+ if (!isDuckDBQuerySchema(sourceSchema))
3702
+ throw new Error("Unsupported DuckDB table schema for the common query API");
3703
+ const schema = {
3704
+ ...sourceSchema,
3705
+ fields: sourceSchema.fields.map((field) => {
3706
+ var _a, _b;
3707
+ const type = SCALAR_TYPES[((_a = field.metadata) == null ? void 0 : _a.sqlType) || ""];
3708
+ if (!type)
3709
+ throw new Error(`Unsupported DuckDB query field type: ${(_b = field.metadata) == null ? void 0 : _b.sqlType}`);
3710
+ return { ...field, type };
3711
+ })
3712
+ };
3713
+ const tableName = `${getQualifiedTableName(table)} AS query_input`;
3714
+ const ordered = table.tableType === "BASE TABLE" && !schema.fields.some((field) => field.name.toLowerCase() === "rowid");
3715
+ const order = ordered ? " ORDER BY query_input.rowid" : "";
3716
+ const metadata = createQueryMetadata({
3717
+ sourceType: "duckdb-table",
3718
+ queryType: "table",
3719
+ execution: { status: "supported", method: "read" },
3720
+ schema,
3721
+ queryCapabilities: {
3722
+ table: {
3723
+ projection: "pushdown",
3724
+ predicate: "pushdown+residual",
3725
+ cast: "pushdown",
3726
+ offset: ordered ? "pushdown" : "residual",
3727
+ limit: ordered ? "pushdown" : "residual",
3728
+ streaming: false,
3729
+ cancellation: false
3730
+ }
3731
+ }
3732
+ });
3733
+ async function* readStatement(sql, outputSchema, signal) {
3734
+ signal == null ? void 0 : signal.throwIfAborted();
3735
+ const rows = await source.queryRows(sql, { signal });
3736
+ signal == null ? void 0 : signal.throwIfAborted();
3737
+ const arrowSchema = convertSchemaToArrow(outputSchema);
3738
+ const vectors = arrowSchema.fields.map((field, index) => (0, import_apache_arrow.vectorFromArray)(rows.map((row) => {
3739
+ const value = row[field.name];
3740
+ if (value == null)
3741
+ return null;
3742
+ const type = outputSchema.fields[index].type;
3743
+ if (type === "int64")
3744
+ return BigInt(value);
3745
+ if (type === "float32" || type === "float64")
3746
+ return Number(value);
3747
+ return value;
3748
+ }), field.type));
3749
+ yield makeTableScanBatch({
3750
+ shape: "arrow-table",
3751
+ schema: outputSchema,
3752
+ data: new import_apache_arrow.Table(arrowSchema, [
3753
+ new import_apache_arrow.RecordBatch(arrowSchema, (0, import_apache_arrow.makeData)({
3754
+ type: new import_apache_arrow.Struct(arrowSchema.fields),
3755
+ length: rows.length,
3756
+ children: vectors.map((vector) => vector.data[0])
3757
+ }))
3758
+ ])
3759
+ });
3760
+ }
3761
+ function plan(query) {
3762
+ var _a;
3763
+ validateTableQueryLimit(query.limit);
3764
+ if (query.offset !== void 0 && (!Number.isSafeInteger(query.offset) || query.offset < 0)) {
3765
+ throw new Error("DuckDB query offset must be a non-negative safe integer.");
3766
+ }
3767
+ const selections = query.select ?? schema.fields.map((field) => field.name);
3768
+ const columns = [];
3769
+ const fields = [];
3770
+ for (const [index, selection] of selections.entries()) {
3771
+ const expression = typeof selection !== "string" && "expression" in selection ? selection.expression : void 0;
3772
+ if (typeof selection !== "string" && "array" in selection)
3773
+ return null;
3774
+ const cast = (expression == null ? void 0 : expression.type) === "CallExpression" && expression.callee.type === "Identifier" && expression.callee.name === "cast" ? expression.arguments : void 0;
3775
+ const column = typeof selection === "string" ? selection : "column" in selection ? selection.column : ((_a = cast == null ? void 0 : cast[0]) == null ? void 0 : _a.type) === "Identifier" ? cast[0].name : void 0;
3776
+ const field = schema.fields.find((candidate) => candidate.name === column);
3777
+ if (!field)
3778
+ return null;
3779
+ let sql2 = quoteSqlIdentifier(field.name);
3780
+ const name = typeof selection === "string" ? selection : selection.as ?? (expression ? `column${index + 1}` : field.name);
3781
+ if (expression) {
3782
+ if ((cast == null ? void 0 : cast.length) !== 2 || cast[1].type !== "Literal")
3783
+ return null;
3784
+ const target = getExactCast(field, cast[1].value);
3785
+ if (!target)
3786
+ return null;
3787
+ sql2 = `CAST(${sql2} AS ${target.sqlType})`;
3788
+ fields.push({ name, type: target.type, nullable: field.nullable });
3789
+ } else {
3790
+ fields.push({ ...field, name });
3791
+ }
3792
+ columns.push(`${sql2} AS ${quoteSqlIdentifier(name)}`);
3793
+ }
3794
+ if (new Set(fields.map((field) => field.name.toLowerCase())).size !== fields.length)
3795
+ return null;
3796
+ const predicate = query.where && compilePredicate(query.where, schema);
3797
+ if (query.where && !predicate)
3798
+ return null;
3799
+ if (!ordered && (query.offset !== void 0 || query.limit !== void 0))
3800
+ return null;
3801
+ const sql = `SELECT ${columns.join(", ")} FROM ${tableName}` + (predicate ? ` WHERE ${predicate}` : "") + order + (query.limit === void 0 ? "" : ` LIMIT ${query.limit}`) + (query.offset === void 0 ? "" : ` OFFSET ${query.offset}`);
3802
+ const outputSchema = { ...schema, fields };
3803
+ return {
3804
+ schema: outputSchema,
3805
+ /** Executes the planned SQL when the consumer starts reading. */
3806
+ async *read(options) {
3807
+ for await (const { data } of readStatement(sql, outputSchema, options == null ? void 0 : options.signal)) {
3808
+ yield* data.batches.length ? data.batches : [makeArrowMetadataBatch(data.schema)];
3809
+ }
3810
+ }
3811
+ };
3812
+ }
3813
+ return {
3814
+ /** Returns metadata without issuing another catalog query. */
3815
+ async getQueryMetadata() {
3816
+ return metadata;
3817
+ },
3818
+ /** Accepts only exact whole queries. */
3819
+ async planQueryPushdown(query, options) {
3820
+ var _a;
3821
+ (_a = options == null ? void 0 : options.signal) == null ? void 0 : _a.throwIfAborted();
3822
+ return plan(query);
3823
+ },
3824
+ /** Checks the exact CAST subset using the same compiler as whole-query execution. */
3825
+ supportsQueryProjectionPushdown(selection) {
3826
+ return Boolean(plan({ select: [selection] }));
3827
+ },
3828
+ /** Executes simple reads natively when possible and applies other operators in Arrow. */
3829
+ async *read(options = {}) {
3830
+ const { columns, predicate, limit, offset, signal } = options;
3831
+ const accepted = plan({ select: columns, where: predicate, limit, offset });
3832
+ if (accepted && limit !== 0) {
3833
+ yield* accepted.read({ signal });
3834
+ return;
3835
+ }
3836
+ for await (const batch of executeTableScanBatches((readSignal) => readStatement(`SELECT * FROM ${tableName}${order}`, schema, readSignal), options, {
3837
+ filter: (batch2, predicate2, filterSignal) => {
3838
+ if (!predicate2)
3839
+ return batch2;
3840
+ const arrowBatch = batch2;
3841
+ return makeTableScanBatch({
3842
+ ...arrowBatch,
3843
+ data: filterArrowTable(arrowBatch.data, predicate2, { signal: filterSignal })
3844
+ });
3845
+ }
3846
+ })) {
3847
+ const { data } = batch;
3848
+ yield* data.batches.length ? data.batches : [makeArrowMetadataBatch(data.schema)];
3849
+ }
3850
+ }
3851
+ };
3852
+ }
3853
+ function getExactCast(field, target) {
3854
+ if (target === "float64" && ["int8", "int16", "int32"].includes(String(field.type))) {
3855
+ return { type: target, sqlType: "DOUBLE" };
3856
+ }
3857
+ if (target !== "int16" && target !== "int32" && target !== "int64")
3858
+ return null;
3859
+ const sourceType = getIntegerType(field.type);
3860
+ if (!sourceType || Number(String(field.type).slice(3)) > Number(target.slice(3)))
3861
+ return null;
3862
+ return { type: target, sqlType: INTEGER_TYPES[target] };
3863
+ }
3864
+ function compilePredicate(predicate, schema) {
3865
+ if (!("op" in predicate) || !["=", "<>", "<", "<=", ">", ">="].includes(predicate.op))
3866
+ return null;
3867
+ const [property, value] = predicate.args;
3868
+ if (!property || typeof property !== "object" || !("property" in property))
3869
+ return null;
3870
+ const field = schema.fields.find((candidate) => candidate.name === property.property);
3871
+ if (!field || !getIntegerType(field.type))
3872
+ return null;
3873
+ if (typeof value !== "bigint" && (typeof value !== "number" || !Number.isSafeInteger(value)))
3874
+ return null;
3875
+ if (value < -(2n ** 63n) || value >= 2n ** 63n)
3876
+ return null;
3877
+ const column = quoteSqlIdentifier(field.name);
3878
+ if (!("casts" in property))
3879
+ return `${column} ${predicate.op} ${value}`;
3880
+ if (!Array.isArray(property.casts) || property.casts.length !== 1)
3881
+ return null;
3882
+ const cast = getExactCast(field, property.casts[0]);
3883
+ return cast ? `CAST(${column} AS ${cast.sqlType}) ${predicate.op} ${value}` : null;
3884
+ }
3885
+
3886
+ // dist/duckdb-sql-source.js
3887
+ var VERSION2 = true ? "5.0.0-beta.10" : "latest";
3295
3888
  var DuckDBSQLSource = {
3296
3889
  dataType: null,
3297
3890
  batchType: null,
@@ -3330,12 +3923,32 @@ var DuckDBSQLSource = {
3330
3923
  },
3331
3924
  defaultOptions: SQL_SOURCE_DEFAULT_OPTIONS,
3332
3925
  testURL: (url) => /^duckdb:\/\//i.test(url),
3333
- createDataSource: (data, options, coreApi) => new DuckDBSQLDataSource(data, options, coreApi)
3926
+ createDataSourceSync: (data, options, coreApi) => new DuckDBSQLDataSource(data, options, coreApi)
3334
3927
  };
3335
3928
  var DuckDBSQLDataSource = class extends SQLDataSource {
3929
+ /** Resolves a reusable common query handle for a named DuckDB table. */
3930
+ async getTable(name, options = {}) {
3931
+ var _a;
3932
+ const table = await this.resolveTable(name, options.signal);
3933
+ const schema = await this.getTableSchema(table);
3934
+ (_a = options.signal) == null ? void 0 : _a.throwIfAborted();
3935
+ return makeDuckDBTableQuerySource(this, table, schema);
3936
+ }
3937
+ /** Reports whether the table's schema can be represented exactly by the common reader. */
3938
+ async getTableMetadata(name, options = {}) {
3939
+ const metadata = await super.getTableMetadata(name, options);
3940
+ return {
3941
+ ...metadata,
3942
+ queryable: Boolean(metadata.schema && isDuckDBQuerySchema(metadata.schema))
3943
+ };
3944
+ }
3945
+ /** Lists table names without claiming compatibility before schema discovery. */
3946
+ async getTablesMetadata(options = {}) {
3947
+ return (await super.getTablesMetadata(options)).map(({ name }) => ({ name }));
3948
+ }
3336
3949
  /** Uploads an Arrow table through IPC. Supported by the browser DuckDB-Wasm adapter. */
3337
3950
  async insertArrowTable(table, options) {
3338
- await this.insertArrowIPC((0, import_apache_arrow.tableToIPC)(table.data, "stream"), options);
3951
+ await this.insertArrowIPC((0, import_apache_arrow2.tableToIPC)(table, "stream"), options);
3339
3952
  }
3340
3953
  /** Uploads a complete Arrow IPC stream to a browser DuckDB-Wasm table. */
3341
3954
  async insertArrowIPC(bytes, options) {
@@ -3484,7 +4097,7 @@ async function createDuckDBBrowserAdapter(context) {
3484
4097
  const connection = await database.connect();
3485
4098
  const executeRows = async (sqlText, options = {}) => {
3486
4099
  const result = await executeDuckDBBrowserQuery(connection, sqlText, options.parameters);
3487
- return convertArrowToTable2((0, import_apache_arrow.tableFromIPC)(result), "object-row-table").data;
4100
+ return convertArrowToTable2((0, import_apache_arrow2.tableFromIPC)(result), "object-row-table").data;
3488
4101
  };
3489
4102
  return {
3490
4103
  capabilities: {
@@ -3576,7 +4189,7 @@ async function createDuckDBBrowserAdapter(context) {
3576
4189
  },
3577
4190
  async executeArrow(sqlText, options = {}) {
3578
4191
  const result = await executeDuckDBBrowserQuery(connection, sqlText, options.parameters);
3579
- return convertArrowToTable2((0, import_apache_arrow.tableFromIPC)(result), "arrow-table");
4192
+ return (0, import_apache_arrow2.tableFromIPC)(result);
3580
4193
  }
3581
4194
  };
3582
4195
  }
@@ -3608,7 +4221,7 @@ function getDuckDBDatabasePath(url, options) {
3608
4221
  }
3609
4222
 
3610
4223
  // dist/snowflake-sql-source.js
3611
- var VERSION3 = true ? "5.0.0-beta.1" : "latest";
4224
+ var VERSION3 = true ? "5.0.0-beta.10" : "latest";
3612
4225
  var SnowflakeSQLSource = {
3613
4226
  dataType: null,
3614
4227
  batchType: null,
@@ -3647,7 +4260,7 @@ var SnowflakeSQLSource = {
3647
4260
  },
3648
4261
  defaultOptions: SQL_SOURCE_DEFAULT_OPTIONS,
3649
4262
  testURL: (url) => /^(sql\+snowflake|snowflake):\/\//i.test(url),
3650
- createDataSource: (data, options, coreApi) => new SnowflakeSQLDataSource(data, options, coreApi)
4263
+ createDataSourceSync: (data, options, coreApi) => new SnowflakeSQLDataSource(data, options, coreApi)
3651
4264
  };
3652
4265
  var SnowflakeSQLDataSource = class extends SQLDataSource {
3653
4266
  constructor(data, options, coreApi) {
@@ -3814,304 +4427,12 @@ function getSnowflakeBaseUrl(url, options) {
3814
4427
  }
3815
4428
 
3816
4429
  // ../core/src/scan/parse-sql-predicate.ts
3817
- var MAXIMUM_SQL_LENGTH = 65536;
3818
- var MAXIMUM_SQL_TOKENS = 4096;
3819
- var MAXIMUM_EXPRESSION_DEPTH = 64;
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();
4430
+ var import_sql = require("@math.gl/expressions/sql");
4431
+ function parseSQLPredicate(source, options) {
4432
+ const predicate = options ? (0, import_sql.parseSQLPredicate)(source, options) : (0, import_sql.parseSQLPredicate)(source);
3827
4433
  validateSQLPredicate(predicate);
3828
4434
  return predicate;
3829
4435
  }
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
4436
 
4116
4437
  // ../core/src/scan/bind-sql-predicate.ts
4117
4438
  function bindSQLPredicate(predicate, parameters) {