@loaders.gl/sql 5.0.0-alpha.9 → 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.
Files changed (107) hide show
  1. package/LICENSE +2 -30
  2. package/README.md +21 -12
  3. package/THIRD-PARTY-NOTICES.md +17 -0
  4. package/dist/arrow-table-source.d.ts +9 -9
  5. package/dist/arrow-table-source.d.ts.map +1 -1
  6. package/dist/arrow-table-source.js +11 -7
  7. package/dist/arrow-table-source.js.map +1 -1
  8. package/dist/compile-table-query.d.ts +3 -3
  9. package/dist/compile-table-query.d.ts.map +1 -1
  10. package/dist/compile-table-query.js +2 -2
  11. package/dist/compile-table-query.js.map +1 -1
  12. package/dist/duckdb-sql-source.d.ts +36 -32
  13. package/dist/duckdb-sql-source.d.ts.map +1 -1
  14. package/dist/duckdb-sql-source.js +61 -17
  15. package/dist/duckdb-sql-source.js.map +1 -1
  16. package/dist/duckdb-table-query.d.ts +9 -0
  17. package/dist/duckdb-table-query.d.ts.map +1 -0
  18. package/dist/duckdb-table-query.js +242 -0
  19. package/dist/duckdb-table-query.js.map +1 -0
  20. package/dist/index.cjs +1843 -1473
  21. package/dist/index.cjs.map +3 -3
  22. package/dist/index.d.ts +12 -12
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +7 -7
  25. package/dist/index.js.map +1 -1
  26. package/dist/snowflake-sql-source.d.ts +18 -31
  27. package/dist/snowflake-sql-source.d.ts.map +1 -1
  28. package/dist/snowflake-sql-source.js +2 -2
  29. package/dist/snowflake-sql-source.js.map +1 -1
  30. package/dist/sql-source.d.ts +24 -11
  31. package/dist/sql-source.d.ts.map +1 -1
  32. package/dist/sql-source.js +35 -3
  33. package/dist/sql-source.js.map +1 -1
  34. package/dist/sql-types.d.ts +15 -3
  35. package/dist/sql-types.d.ts.map +1 -1
  36. package/dist/sql-utils.d.ts +2 -3
  37. package/dist/sql-utils.d.ts.map +1 -1
  38. package/dist/sql-utils.js +1 -34
  39. package/dist/sql-utils.js.map +1 -1
  40. package/package.json +8 -28
  41. package/src/arrow-table-source.ts +17 -16
  42. package/src/compile-table-query.ts +4 -4
  43. package/src/duckdb-sql-source.ts +81 -20
  44. package/src/duckdb-table-query.ts +277 -0
  45. package/src/index.ts +19 -11
  46. package/src/snowflake-sql-source.ts +3 -3
  47. package/src/sql-source.ts +46 -7
  48. package/src/sql-types.ts +16 -3
  49. package/src/sql-utils.ts +2 -40
  50. package/dist/arrow-query.cjs +0 -1404
  51. package/dist/arrow-query.cjs.map +0 -6
  52. package/dist/arrow-query.d.ts +0 -5
  53. package/dist/arrow-query.d.ts.map +0 -1
  54. package/dist/arrow-query.js +0 -6
  55. package/dist/arrow-query.js.map +0 -1
  56. package/dist/bind-sql-predicate.d.ts +0 -4
  57. package/dist/bind-sql-predicate.d.ts.map +0 -1
  58. package/dist/bind-sql-predicate.js +0 -11
  59. package/dist/bind-sql-predicate.js.map +0 -1
  60. package/dist/parse-sql-predicate.d.ts +0 -10
  61. package/dist/parse-sql-predicate.d.ts.map +0 -1
  62. package/dist/parse-sql-predicate.js +0 -322
  63. package/dist/parse-sql-predicate.js.map +0 -1
  64. package/dist/query-arrow-table.d.ts +0 -83
  65. package/dist/query-arrow-table.d.ts.map +0 -1
  66. package/dist/query-arrow-table.js +0 -663
  67. package/dist/query-arrow-table.js.map +0 -1
  68. package/dist/sql-predicate-schema.d.ts +0 -156
  69. package/dist/sql-predicate-schema.d.ts.map +0 -1
  70. package/dist/sql-predicate-schema.js +0 -226
  71. package/dist/sql-predicate-schema.js.map +0 -1
  72. package/dist/sql-predicate-types.d.ts +0 -45
  73. package/dist/sql-predicate-types.d.ts.map +0 -1
  74. package/dist/sql-predicate-types.js +0 -9
  75. package/dist/sql-predicate-types.js.map +0 -1
  76. package/dist/sql-predicate-zod.cjs +0 -54
  77. package/dist/sql-predicate-zod.cjs.map +0 -6
  78. package/dist/sql-predicate-zod.d.ts +0 -11
  79. package/dist/sql-predicate-zod.d.ts.map +0 -1
  80. package/dist/sql-predicate-zod.js +0 -45
  81. package/dist/sql-predicate-zod.js.map +0 -1
  82. package/dist/sql-predicate.cjs +0 -607
  83. package/dist/sql-predicate.cjs.map +0 -6
  84. package/dist/sql-predicate.d.ts +0 -6
  85. package/dist/sql-predicate.d.ts.map +0 -1
  86. package/dist/sql-predicate.js +0 -8
  87. package/dist/sql-predicate.js.map +0 -1
  88. package/dist/table-query-capabilities.d.ts +0 -6
  89. package/dist/table-query-capabilities.d.ts.map +0 -1
  90. package/dist/table-query-capabilities.js +0 -26
  91. package/dist/table-query-capabilities.js.map +0 -1
  92. package/dist/table-query.cjs +0 -286
  93. package/dist/table-query.cjs.map +0 -6
  94. package/dist/table-query.d.ts +0 -29
  95. package/dist/table-query.d.ts.map +0 -1
  96. package/dist/table-query.js +0 -30
  97. package/dist/table-query.js.map +0 -1
  98. package/src/arrow-query.ts +0 -17
  99. package/src/bind-sql-predicate.ts +0 -21
  100. package/src/parse-sql-predicate.ts +0 -381
  101. package/src/query-arrow-table.ts +0 -846
  102. package/src/sql-predicate-schema.ts +0 -251
  103. package/src/sql-predicate-types.ts +0 -87
  104. package/src/sql-predicate-zod.ts +0 -52
  105. package/src/sql-predicate.ts +0 -28
  106. package/src/table-query-capabilities.ts +0 -29
  107. package/src/table-query.ts +0 -78
package/dist/index.cjs CHANGED
@@ -55,8 +55,8 @@ __export(index_exports, {
55
55
  });
56
56
  module.exports = __toCommonJS(index_exports);
57
57
 
58
- // ../loader-utils/src/lib/log-utils/log.ts
59
- var import_log = require("@probe.gl/log");
58
+ // ../core/src/loader-utils/lib/log-utils/log.ts
59
+ var import_log = require("probe.gl/log");
60
60
  var VERSION = typeof __VERSION__ !== "undefined" ? __VERSION__ : "latest";
61
61
  var version = VERSION[0] >= "0" && VERSION[0] <= "9" ? `v${VERSION}` : "";
62
62
  function createLog() {
@@ -69,12 +69,42 @@ 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
- // ../loader-utils/src/lib/javascript-utils/is-type.ts
103
+ // ../core/src/loader-utils/lib/javascript-utils/is-type.ts
74
104
  var isObject = (value) => value !== null && typeof value === "object";
75
105
  var isPureObject = (value) => isObject(value) && value.constructor === {}.constructor;
76
106
 
77
- // ../loader-utils/src/lib/option-utils/merge-options.ts
107
+ // ../core/src/loader-utils/lib/option-utils/merge-options.ts
78
108
  function mergeOptions(baseOptions, newOptions) {
79
109
  return mergeOptionsRecursively(baseOptions || {}, newOptions);
80
110
  }
@@ -97,7 +127,7 @@ function mergeOptionsRecursively(baseOptions, newOptions, level = 0) {
97
127
  return options;
98
128
  }
99
129
 
100
- // ../loader-utils/src/lib/request-utils/authentication.ts
130
+ // ../core/src/loader-utils/lib/request-utils/authentication.ts
101
131
  function resolveCredentials(credentials, authentications = []) {
102
132
  if (credentials.every(isAuthentication)) return credentials;
103
133
  return credentials.map((credential, index) => {
@@ -134,7 +164,7 @@ function isAuthentication(credential) {
134
164
  return (credential.type === "header" || credential.type === "query-parameter") && typeof credential.name === "string" && (typeof credential.token === "string" || typeof credential.token === "function") && Array.isArray(credential.refreshStatusCodes);
135
165
  }
136
166
 
137
- // ../loader-utils/src/lib/request-utils/request-credentials.ts
167
+ // ../core/src/loader-utils/lib/request-utils/request-credentials.ts
138
168
  var AUTHENTICATED_FETCH = /* @__PURE__ */ Symbol("loaders.gl.authenticated-fetch");
139
169
  var refreshPromises = /* @__PURE__ */ new WeakMap();
140
170
  function createAuthenticatedFetch(options) {
@@ -166,26 +196,28 @@ function createAuthenticatedFetch(options) {
166
196
  );
167
197
  for (const credential of requestCredentials) normalizeRequestOrigins(credential.origins);
168
198
  const authenticatedFetch = async (url, requestOptions = {}) => {
169
- var _a, _b, _c, _d;
199
+ var _a, _b, _c, _d, _e, _f, _g;
170
200
  requestOptions = mergeFetchOptions(fetchOptions, requestOptions);
171
- if ((_a = requestOptions.signal) == null ? void 0 : _a.aborted) throw createAbortError();
201
+ (_a = requestOptions.signal) == null ? void 0 : _a.throwIfAborted();
172
202
  if (!credentials.length) return baseFetch(url, requestOptions);
173
203
  const resolvedURL = parseHTTPURL(url);
174
204
  const authorization = resolvedURL ? await authorizeRequest(resolvedURL, requestOptions, tokenCredentials) : { url, options: requestOptions, credentials: [] };
175
205
  const signedRequest = await authenticateRequest(authorization, requestCredentials, "request");
176
206
  const response = await baseFetch(signedRequest.url, signedRequest.options);
177
- const refreshableCredentials = authorization.credentials.filter(
178
- ({ credential }) => typeof credential.token === "function" && credential.refreshStatusCodes.includes(response.status)
179
- );
180
- const shouldRetrySignature = signedRequest.credentials.some(
207
+ const refreshableCredentials = [];
208
+ for (const resolvedCredential of authorization.credentials) {
209
+ const { credential } = resolvedCredential;
210
+ if (typeof credential.token === "function" && (isReplayableRequest(requestOptions) || ((_b = credential.canReplayRequest) == null ? void 0 : _b.call(credential, url, requestOptions))) && (isReplayableRequest(signedRequest.options) || ((_c = credential.canReplayRequest) == null ? void 0 : _c.call(credential, signedRequest.url, signedRequest.options))) && (credential.refreshStatusCodes.includes(response.status) || await ((_d = credential.shouldRefresh) == null ? void 0 : _d.call(credential, response))))
211
+ refreshableCredentials.push(resolvedCredential);
212
+ }
213
+ const shouldRetrySignature = isReplayableRequest(requestOptions) && isReplayableRequest(signedRequest.options) && signedRequest.credentials.some(
181
214
  (credential) => {
182
215
  var _a2;
183
216
  return (_a2 = credential.refreshStatusCodes) == null ? void 0 : _a2.includes(response.status);
184
217
  }
185
218
  );
186
- if (!refreshableCredentials.length && !shouldRetrySignature || !isReplayableRequest(requestOptions) || !isReplayableRequest(signedRequest.options))
187
- return response;
188
- if ((_b = requestOptions.signal) == null ? void 0 : _b.aborted) throw createAbortError();
219
+ if (!refreshableCredentials.length && !shouldRetrySignature) return response;
220
+ (_e = requestOptions.signal) == null ? void 0 : _e.throwIfAborted();
189
221
  const refreshedTokens = /* @__PURE__ */ new Map();
190
222
  await Promise.all(
191
223
  refreshableCredentials.map(async ({ credential }) => {
@@ -194,9 +226,9 @@ function createAuthenticatedFetch(options) {
194
226
  })
195
227
  );
196
228
  if (!refreshedTokens.size && !shouldRetrySignature) return response;
197
- await ((_c = response.body) == null ? void 0 : _c.cancel().catch(() => {
229
+ await ((_f = response.body) == null ? void 0 : _f.cancel().catch(() => {
198
230
  }));
199
- if ((_d = requestOptions.signal) == null ? void 0 : _d.aborted) throw createAbortError();
231
+ (_g = requestOptions.signal) == null ? void 0 : _g.throwIfAborted();
200
232
  const replayAuthorization = resolvedURL ? await authorizeRequest(
201
233
  resolvedURL,
202
234
  requestOptions,
@@ -224,16 +256,17 @@ function createAuthenticatedFetch(options) {
224
256
  }
225
257
  function getAuthenticatedFetch(options = {}) {
226
258
  var _a, _b, _c;
227
- const fetchOption = options.fetch ?? ((_a = options.core) == null ? void 0 : _a.fetch);
259
+ const fetchOption = (_a = options.core) == null ? void 0 : _a.fetch;
228
260
  let fetchFunction;
229
261
  if (typeof fetchOption === "function") {
230
262
  fetchFunction = fetchOption;
231
263
  } else {
232
264
  fetchFunction = (url, requestOptions) => fetch(url, requestOptions);
233
265
  }
266
+ const fetchOptions = typeof fetchOption === "object" && fetchOption ? fetchOption : void 0;
234
267
  return createAuthenticatedFetch({
235
268
  fetch: fetchFunction,
236
- fetchOptions: typeof fetchOption === "object" && fetchOption ? fetchOption : void 0,
269
+ fetchOptions: options.signal ? { ...fetchOptions, signal: options.signal } : fetchOptions,
237
270
  credentials: ((_b = options.core) == null ? void 0 : _b.credentials) || [],
238
271
  authentications: (_c = options.core) == null ? void 0 : _c.authentications
239
272
  });
@@ -253,7 +286,7 @@ async function authenticateRequest(request, credentials, reason, response) {
253
286
  for (const credential of credentials) {
254
287
  const origin = getRequestOrigin(request.url);
255
288
  if (!origin || !normalizeRequestOrigins(credential.origins).includes(origin)) continue;
256
- if (signal == null ? void 0 : signal.aborted) throw createAbortError();
289
+ signal == null ? void 0 : signal.throwIfAborted();
257
290
  const authenticatedRequest = await credential.authenticate({
258
291
  url: request.url,
259
292
  options: { ...request.options, headers: new Headers(request.options.headers) },
@@ -269,7 +302,7 @@ async function authenticateRequest(request, credentials, reason, response) {
269
302
  };
270
303
  appliedCredentials.push(credential);
271
304
  }
272
- if (signal == null ? void 0 : signal.aborted) throw createAbortError();
305
+ signal == null ? void 0 : signal.throwIfAborted();
273
306
  return { ...request, credentials: appliedCredentials };
274
307
  }
275
308
  function getRequestOrigin(url) {
@@ -359,11 +392,8 @@ function isReplayableRequest(options) {
359
392
  if (!["GET", "HEAD", "OPTIONS", "PUT", "DELETE"].includes(method)) return false;
360
393
  return !(options.body && typeof ReadableStream !== "undefined" && options.body instanceof ReadableStream);
361
394
  }
362
- function createAbortError() {
363
- return new DOMException("The operation was aborted.", "AbortError");
364
- }
365
395
 
366
- // ../loader-utils/src/lib/path-utils/file-aliases.ts
396
+ // ../core/src/loader-utils/lib/path-utils/file-aliases.ts
367
397
  var pathPrefix = "";
368
398
  var fileAliases = {};
369
399
  function resolvePath(filename) {
@@ -379,7 +409,7 @@ function resolvePath(filename) {
379
409
  return filename;
380
410
  }
381
411
 
382
- // ../loader-utils/src/lib/sources/data-source.ts
412
+ // ../core/src/loader-utils/lib/sources/data-source.ts
383
413
  var UNAVAILABLE_CORE_API = {
384
414
  fetchFile: unavailableCoreApiMethod("fetchFile"),
385
415
  parse: unavailableCoreApiMethod("parse"),
@@ -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,12 +532,50 @@ 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
- // ../loader-utils/src/lib/scan-utils/columnar-predicate.ts
578
+ // ../core/src/loader-utils/lib/scan-utils/columnar-predicate.ts
515
579
  function getColumnarPredicateColumns(predicate) {
516
580
  return [...new Set(getColumnarPredicatePaths(predicate).map((path) => path[0]))];
517
581
  }
@@ -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
- // ../loader-utils/src/lib/scan-utils/scan-query-metadata.ts
626
- var import_crs = require("@math.gl/crs");
752
+ // ../core/src/loader-utils/lib/scan-utils/scan-query-metadata.ts
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,8 +835,15 @@ 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
- // ../loader-utils/src/lib/scan-utils/table-query.ts
846
+ // ../core/src/loader-utils/lib/scan-utils/table-query.ts
704
847
  function planTableQuery(sourceColumnNames, options = {}) {
705
848
  validateTableQueryOptions(sourceColumnNames, options);
706
849
  const outputColumnNames = options.columns ? [...options.columns] : [...sourceColumnNames];
@@ -744,13 +887,174 @@ 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
- // ../loader-utils/src/lib/scan-utils/table-scan-batch.ts
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
- // ../loader-utils/src/lib/scan-utils/relational-query.ts
1057
+ // ../core/src/loader-utils/lib/scan-utils/relational-query.ts
754
1058
  function planRelationalQuery(sourceColumnNames, options = {}) {
755
1059
  var _a, _b, _c, _d, _e, _f;
756
1060
  const available = new Set(sourceColumnNames);
@@ -840,7 +1144,7 @@ function validateRelationalColumnReference(columnName, availableColumns) {
840
1144
  }
841
1145
  }
842
1146
 
843
- // ../loader-utils/src/lib/scan-utils/table-query-explain.ts
1147
+ // ../core/src/loader-utils/lib/scan-utils/table-query-explain.ts
844
1148
  function explainTableQuery(sourceColumnNames, options, capabilities) {
845
1149
  const plan = planTableQuery(sourceColumnNames, options);
846
1150
  const outputColumns = options.columns ? [...options.columns] : [...sourceColumnNames];
@@ -864,12 +1168,12 @@ function createOperatorExplanation(enabled, support) {
864
1168
  return Object.freeze({ enabled, support });
865
1169
  }
866
1170
 
867
- // dist/sql-predicate-types.js
1171
+ // ../core/src/scan/sql-predicate-types.ts
868
1172
  function isSQLPredicateParameter(value) {
869
1173
  return isColumnarPredicateParameter(value);
870
1174
  }
871
1175
 
872
- // dist/sql-predicate-schema.js
1176
+ // ../core/src/scan/sql-predicate-schema.ts
873
1177
  var MAXIMUM_PREDICATE_DEPTH = 64;
874
1178
  var MAXIMUM_PREDICATE_NODES = 4096;
875
1179
  var SQL_PREDICATE_JSON_SCHEMA = {
@@ -1022,7 +1326,9 @@ function validatePredicateNode(value, path, depth, state) {
1022
1326
  throw new Error(`SQL predicate ${path}.in requires a property and non-empty value list`);
1023
1327
  }
1024
1328
  validatePredicateProperty(value.args[0], `${path}.args[0]`);
1025
- value.args[1].forEach((item, index) => validatePredicateValue(item, `${path}.args[1][${index}]`));
1329
+ value.args[1].forEach(
1330
+ (item, index) => validatePredicateValue(item, `${path}.args[1][${index}]`)
1331
+ );
1026
1332
  return;
1027
1333
  case "=":
1028
1334
  case "<>":
@@ -1284,115 +1590,389 @@ function validateSQLIdentifier(identifier) {
1284
1590
  }
1285
1591
  }
1286
1592
 
1287
- // dist/sql-utils.js
1288
- var arrow5 = __toESM(require("apache-arrow"), 1);
1593
+ // ../core/src/scan/rows-to-arrow.ts
1594
+ var arrow = __toESM(require("apache-arrow"), 1);
1595
+ function convertRowsToArrowTable(rows) {
1596
+ return arrow.tableFromJSON(rows);
1597
+ }
1289
1598
 
1290
- // ../schema-utils/src/lib/schema/data-type.ts
1291
- var import_types = require("@math.gl/types");
1292
- function getDataTypeFromValue(value, defaultNumberType = "float32") {
1293
- if (value instanceof Date) {
1294
- return "date-millisecond";
1295
- }
1296
- if (typeof value === "number") {
1297
- 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";
1298
1627
  }
1299
- if (typeof value === "boolean") {
1300
- return "bool";
1628
+ if (normalizedType.includes("INT") || normalizedType === "INTEGER" || normalizedType === "SMALLINT" || normalizedType === "TINYINT") {
1629
+ return "int32";
1301
1630
  }
1302
- if (typeof value === "string") {
1303
- return "utf8";
1631
+ if (normalizedType.includes("DOUBLE") || normalizedType.includes("FLOAT8") || normalizedType.includes("REAL")) {
1632
+ return "float64";
1304
1633
  }
1305
- if (value === null || value === "undefined") {
1306
- return "null";
1634
+ if (normalizedType.includes("FLOAT") || normalizedType.includes("DECIMAL") || normalizedType.includes("NUMERIC")) {
1635
+ return "float64";
1307
1636
  }
1308
- return "null";
1309
- }
1310
- function getDataTypeFromArray(array) {
1311
- let type = getDataTypeFromTypedArray(array);
1312
- if (type !== "null") {
1313
- return { type, nullable: false };
1637
+ if (normalizedType.includes("BOOL")) {
1638
+ return "bool";
1314
1639
  }
1315
- if (array.length > 0) {
1316
- type = getDataTypeFromValue(array[0]);
1317
- return { type, nullable: true };
1640
+ if (normalizedType.includes("DATE")) {
1641
+ return "date-day";
1318
1642
  }
1319
- return { type: "null", nullable: true };
1320
- }
1321
- function getDataTypeFromTypedArray(array) {
1322
- if (typeof Float16Array !== "undefined" && array instanceof Float16Array) {
1323
- return "float16";
1643
+ if (normalizedType.includes("TIME") || normalizedType.includes("TIMESTAMP")) {
1644
+ return "timestamp-millisecond";
1324
1645
  }
1325
- switch (array.constructor) {
1326
- case Int8Array:
1327
- return "int8";
1328
- case Uint8Array:
1329
- case Uint8ClampedArray:
1330
- return "uint8";
1331
- case Int16Array:
1332
- return "int16";
1333
- case Uint16Array:
1334
- return "uint16";
1335
- case Int32Array:
1336
- return "int32";
1337
- case Uint32Array:
1338
- return "uint32";
1339
- case BigInt64Array:
1340
- return "int64";
1341
- case BigUint64Array:
1342
- return "uint64";
1343
- case Float32Array:
1344
- return "float32";
1345
- case Float64Array:
1346
- return "float64";
1347
- default:
1348
- return "null";
1646
+ if (normalizedType.includes("BLOB") || normalizedType.includes("BINARY")) {
1647
+ return "binary";
1349
1648
  }
1649
+ return "utf8";
1350
1650
  }
1351
- function getArrayTypeFromDataType(type, nullable) {
1352
- if (!nullable) {
1353
- switch (type) {
1354
- case "int8":
1355
- return Int8Array;
1356
- case "uint8":
1357
- return Uint8Array;
1358
- case "int16":
1359
- return Int16Array;
1360
- case "uint16":
1361
- return Uint16Array;
1362
- case "int32":
1363
- return Int32Array;
1364
- case "uint32":
1365
- return Uint32Array;
1366
- case "float32":
1367
- return Float32Array;
1368
- case "float64":
1369
- return Float64Array;
1370
- case "float16":
1371
- return (0, import_types.getFloat16ArrayConstructor)();
1372
- default:
1373
- break;
1374
- }
1375
- }
1376
- 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(".");
1377
1654
  }
1378
1655
 
1379
- // ../schema-utils/src/lib/schema/convert-arrow-schema.ts
1380
- var arrow = __toESM(require("apache-arrow"), 1);
1381
- function convertArrowToSchema(arrowSchema) {
1382
- return serializeArrowSchema(arrowSchema);
1383
- }
1384
- function convertSchemaToArrow(schema, options) {
1385
- 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);
1386
1682
  }
1387
- function serializeArrowSchema(arrowSchema) {
1388
- return {
1389
- fields: arrowSchema.fields.map((arrowField) => serializeArrowField(arrowField)),
1390
- metadata: serializeArrowMetadata(arrowSchema.metadata)
1391
- };
1683
+ function getSQLAdapterFactory(type) {
1684
+ return SQL_ADAPTER_FACTORIES.get(type);
1392
1685
  }
1393
- function deserializeArrowSchema(schema, options) {
1394
- return new arrow.Schema(
1395
- schema.fields.map((field) => deserializeArrowField(field, options)),
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)),
1396
1976
  deserializeArrowMetadata(schema.metadata)
1397
1977
  );
1398
1978
  }
@@ -1411,7 +1991,7 @@ function serializeArrowField(field) {
1411
1991
  };
1412
1992
  }
1413
1993
  function deserializeArrowField(field, options) {
1414
- return new arrow.Field(
1994
+ return new arrow2.Field(
1415
1995
  field.name,
1416
1996
  deserializeArrowType(field.type, options),
1417
1997
  field.nullable,
@@ -1433,57 +2013,57 @@ function serializeArrowType(arrowType) {
1433
2013
  };
1434
2014
  }
1435
2015
  switch (arrowType.constructor) {
1436
- case arrow.Null:
2016
+ case arrow2.Null:
1437
2017
  return "null";
1438
- case arrow.Binary:
2018
+ case arrow2.Binary:
1439
2019
  return "binary";
1440
- case arrow.FixedSizeBinary:
2020
+ case arrow2.FixedSizeBinary:
1441
2021
  return {
1442
2022
  type: "fixed-size-binary",
1443
2023
  byteWidth: arrowType.byteWidth
1444
2024
  };
1445
- case arrow.Bool:
2025
+ case arrow2.Bool:
1446
2026
  return "bool";
1447
- case arrow.Int:
2027
+ case arrow2.Int:
1448
2028
  const intType = arrowType;
1449
2029
  return `${intType.isSigned ? "" : "u"}int${intType.bitWidth}`;
1450
- case arrow.Int8:
2030
+ case arrow2.Int8:
1451
2031
  return "int8";
1452
- case arrow.Int16:
2032
+ case arrow2.Int16:
1453
2033
  return "int16";
1454
- case arrow.Int32:
2034
+ case arrow2.Int32:
1455
2035
  return "int32";
1456
- case arrow.Int64:
2036
+ case arrow2.Int64:
1457
2037
  return "int64";
1458
- case arrow.Uint8:
2038
+ case arrow2.Uint8:
1459
2039
  return "uint8";
1460
- case arrow.Uint16:
2040
+ case arrow2.Uint16:
1461
2041
  return "uint16";
1462
- case arrow.Uint32:
2042
+ case arrow2.Uint32:
1463
2043
  return "uint32";
1464
- case arrow.Uint64:
2044
+ case arrow2.Uint64:
1465
2045
  return "uint64";
1466
- case arrow.Float:
2046
+ case arrow2.Float:
1467
2047
  const precision = arrowType.precision;
1468
2048
  switch (precision) {
1469
- case arrow.Precision.HALF:
2049
+ case arrow2.Precision.HALF:
1470
2050
  return "float16";
1471
- case arrow.Precision.SINGLE:
2051
+ case arrow2.Precision.SINGLE:
1472
2052
  return "float32";
1473
- case arrow.Precision.DOUBLE:
2053
+ case arrow2.Precision.DOUBLE:
1474
2054
  return "float64";
1475
2055
  default:
1476
2056
  return "float16";
1477
2057
  }
1478
- case arrow.Float16:
2058
+ case arrow2.Float16:
1479
2059
  return "float16";
1480
- case arrow.Float32:
2060
+ case arrow2.Float32:
1481
2061
  return "float32";
1482
- case arrow.Float64:
2062
+ case arrow2.Float64:
1483
2063
  return "float64";
1484
- case arrow.Utf8:
2064
+ case arrow2.Utf8:
1485
2065
  return "utf8";
1486
- case arrow.Decimal:
2066
+ case arrow2.Decimal:
1487
2067
  const decimal = arrowType;
1488
2068
  return {
1489
2069
  type: "decimal",
@@ -1491,108 +2071,108 @@ function serializeArrowType(arrowType) {
1491
2071
  precision: decimal.precision,
1492
2072
  scale: decimal.scale
1493
2073
  };
1494
- case arrow.Date_:
2074
+ case arrow2.Date_:
1495
2075
  const dateUnit = arrowType.unit;
1496
- return dateUnit === arrow.DateUnit.DAY ? "date-day" : "date-millisecond";
1497
- case arrow.DateDay:
2076
+ return dateUnit === arrow2.DateUnit.DAY ? "date-day" : "date-millisecond";
2077
+ case arrow2.DateDay:
1498
2078
  return "date-day";
1499
- case arrow.DateMillisecond:
2079
+ case arrow2.DateMillisecond:
1500
2080
  return "date-millisecond";
1501
- case arrow.Time:
2081
+ case arrow2.Time:
1502
2082
  const timeUnit = arrowType.unit;
1503
2083
  switch (timeUnit) {
1504
- case arrow.TimeUnit.SECOND:
2084
+ case arrow2.TimeUnit.SECOND:
1505
2085
  return "time-second";
1506
- case arrow.TimeUnit.MILLISECOND:
2086
+ case arrow2.TimeUnit.MILLISECOND:
1507
2087
  return "time-millisecond";
1508
- case arrow.TimeUnit.MICROSECOND:
2088
+ case arrow2.TimeUnit.MICROSECOND:
1509
2089
  return "time-microsecond";
1510
- case arrow.TimeUnit.NANOSECOND:
2090
+ case arrow2.TimeUnit.NANOSECOND:
1511
2091
  return "time-nanosecond";
1512
2092
  default:
1513
2093
  return "time-second";
1514
2094
  }
1515
- case arrow.TimeMillisecond:
2095
+ case arrow2.TimeMillisecond:
1516
2096
  return "time-millisecond";
1517
- case arrow.TimeSecond:
2097
+ case arrow2.TimeSecond:
1518
2098
  return "time-second";
1519
- case arrow.TimeMicrosecond:
2099
+ case arrow2.TimeMicrosecond:
1520
2100
  return "time-microsecond";
1521
- case arrow.TimeNanosecond:
2101
+ case arrow2.TimeNanosecond:
1522
2102
  return "time-nanosecond";
1523
- case arrow.Timestamp:
2103
+ case arrow2.Timestamp:
1524
2104
  const timeStampUnit = arrowType.unit;
1525
2105
  switch (timeStampUnit) {
1526
- case arrow.TimeUnit.SECOND:
2106
+ case arrow2.TimeUnit.SECOND:
1527
2107
  return "timestamp-second";
1528
- case arrow.TimeUnit.MILLISECOND:
2108
+ case arrow2.TimeUnit.MILLISECOND:
1529
2109
  return "timestamp-millisecond";
1530
- case arrow.TimeUnit.MICROSECOND:
2110
+ case arrow2.TimeUnit.MICROSECOND:
1531
2111
  return "timestamp-microsecond";
1532
- case arrow.TimeUnit.NANOSECOND:
2112
+ case arrow2.TimeUnit.NANOSECOND:
1533
2113
  return "timestamp-nanosecond";
1534
2114
  default:
1535
2115
  return "timestamp-second";
1536
2116
  }
1537
- case arrow.TimestampSecond:
2117
+ case arrow2.TimestampSecond:
1538
2118
  return "timestamp-second";
1539
- case arrow.TimestampMillisecond:
2119
+ case arrow2.TimestampMillisecond:
1540
2120
  return "timestamp-millisecond";
1541
- case arrow.TimestampMicrosecond:
2121
+ case arrow2.TimestampMicrosecond:
1542
2122
  return "timestamp-microsecond";
1543
- case arrow.TimestampNanosecond:
2123
+ case arrow2.TimestampNanosecond:
1544
2124
  return "timestamp-nanosecond";
1545
- case arrow.Interval:
2125
+ case arrow2.Interval:
1546
2126
  const intervalUnit = arrowType.unit;
1547
2127
  switch (intervalUnit) {
1548
- case arrow.IntervalUnit.DAY_TIME:
2128
+ case arrow2.IntervalUnit.DAY_TIME:
1549
2129
  return "interval-daytime";
1550
- case arrow.IntervalUnit.YEAR_MONTH:
2130
+ case arrow2.IntervalUnit.YEAR_MONTH:
1551
2131
  return "interval-yearmonth";
1552
2132
  default:
1553
2133
  return "interval-daytime";
1554
2134
  }
1555
- case arrow.IntervalDayTime:
2135
+ case arrow2.IntervalDayTime:
1556
2136
  return "interval-daytime";
1557
- case arrow.IntervalYearMonth:
2137
+ case arrow2.IntervalYearMonth:
1558
2138
  return "interval-yearmonth";
1559
- case arrow.Map_:
2139
+ case arrow2.Map_:
1560
2140
  const mapType = arrowType;
1561
2141
  return {
1562
2142
  type: "map",
1563
2143
  keysSorted: mapType.keysSorted,
1564
2144
  children: mapType.children.map((arrowField) => serializeArrowField(arrowField))
1565
2145
  };
1566
- case arrow.List:
2146
+ case arrow2.List:
1567
2147
  const listType = arrowType;
1568
2148
  const listField = listType.valueField;
1569
2149
  return {
1570
2150
  type: "list",
1571
2151
  children: [serializeArrowField(listField)]
1572
2152
  };
1573
- case arrow.FixedSizeList:
2153
+ case arrow2.FixedSizeList:
1574
2154
  const fixedSizeList = arrowType;
1575
2155
  return {
1576
2156
  type: "fixed-size-list",
1577
2157
  listSize: fixedSizeList.listSize,
1578
2158
  children: [serializeArrowField(fixedSizeList.children[0])]
1579
2159
  };
1580
- case arrow.Struct:
2160
+ case arrow2.Struct:
1581
2161
  const structType = arrowType;
1582
2162
  return {
1583
2163
  type: "struct",
1584
2164
  children: structType.children.map((arrowField) => serializeArrowField(arrowField))
1585
2165
  };
1586
- case arrow.SparseUnion:
1587
- case arrow.DenseUnion:
2166
+ case arrow2.SparseUnion:
2167
+ case arrow2.DenseUnion:
1588
2168
  const unionType = arrowType;
1589
2169
  return {
1590
- type: unionType instanceof arrow.DenseUnion ? "dense-union" : "sparse-union",
2170
+ type: unionType instanceof arrow2.DenseUnion ? "dense-union" : "sparse-union",
1591
2171
  typeIds: unionType.typeIds,
1592
2172
  children: unionType.children.map((arrowField) => serializeArrowField(arrowField)),
1593
2173
  typeIdToChildIndex: { ...unionType.typeIdToChildIndex }
1594
2174
  };
1595
- case arrow.Dictionary:
2175
+ case arrow2.Dictionary:
1596
2176
  const dictionaryType = arrowType;
1597
2177
  return {
1598
2178
  type: "dictionary",
@@ -1609,16 +2189,16 @@ function deserializeArrowType(dataType, options) {
1609
2189
  if (typeof dataType === "object") {
1610
2190
  switch (dataType.type) {
1611
2191
  case "decimal":
1612
- return new arrow.Decimal(dataType.scale, dataType.precision, dataType.bitWidth);
2192
+ return new arrow2.Decimal(dataType.scale, dataType.precision, dataType.bitWidth);
1613
2193
  case "map": {
1614
2194
  const children = dataType.children.map(
1615
2195
  (arrowField) => deserializeArrowField(arrowField, options)
1616
2196
  );
1617
- return new arrow.Map_(children[0], dataType.keysSorted);
2197
+ return new arrow2.Map_(children[0], dataType.keysSorted);
1618
2198
  }
1619
2199
  case "list": {
1620
2200
  const field = deserializeArrowField(dataType.children[0], options);
1621
- return new arrow.List(field);
2201
+ return new arrow2.List(field);
1622
2202
  }
1623
2203
  case "large-list": {
1624
2204
  const field = deserializeArrowField(dataType.children[0], options);
@@ -1632,25 +2212,25 @@ function deserializeArrowType(dataType, options) {
1632
2212
  }
1633
2213
  case "fixed-size-list": {
1634
2214
  const child = deserializeArrowField(dataType.children[0], options);
1635
- return new arrow.FixedSizeList(dataType.listSize, child);
2215
+ return new arrow2.FixedSizeList(dataType.listSize, child);
1636
2216
  }
1637
2217
  case "fixed-size-binary":
1638
- return new arrow.FixedSizeBinary(dataType.byteWidth);
2218
+ return new arrow2.FixedSizeBinary(dataType.byteWidth);
1639
2219
  case "struct": {
1640
2220
  const children = dataType.children.map(
1641
2221
  (arrowField) => deserializeArrowField(arrowField, options)
1642
2222
  );
1643
- return new arrow.Struct(children);
2223
+ return new arrow2.Struct(children);
1644
2224
  }
1645
2225
  case "sparse-union":
1646
2226
  case "dense-union": {
1647
2227
  const children = dataType.children.map(
1648
2228
  (arrowField) => deserializeArrowField(arrowField, options)
1649
2229
  );
1650
- 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);
1651
2231
  }
1652
2232
  case "dictionary": {
1653
- return new arrow.Dictionary(
2233
+ return new arrow2.Dictionary(
1654
2234
  deserializeArrowType(dataType.dictionary, options),
1655
2235
  deserializeArrowDictionaryKeyType(dataType.indices),
1656
2236
  dataType.id,
@@ -1663,76 +2243,76 @@ function deserializeArrowType(dataType, options) {
1663
2243
  }
1664
2244
  switch (dataType) {
1665
2245
  case "null":
1666
- return new arrow.Null();
2246
+ return new arrow2.Null();
1667
2247
  case "binary":
1668
- return makeVariableWidthArrowType("BinaryView", new arrow.Binary(), options);
2248
+ return makeVariableWidthArrowType("BinaryView", new arrow2.Binary(), options);
1669
2249
  case "binary-view":
1670
2250
  return makeRequiredArrowViewType("BinaryView");
1671
2251
  case "bool":
1672
- return new arrow.Bool();
2252
+ return new arrow2.Bool();
1673
2253
  case "int8":
1674
- return new arrow.Int8();
2254
+ return new arrow2.Int8();
1675
2255
  case "int16":
1676
- return new arrow.Int16();
2256
+ return new arrow2.Int16();
1677
2257
  case "int32":
1678
- return new arrow.Int32();
2258
+ return new arrow2.Int32();
1679
2259
  case "int64":
1680
- return new arrow.Int64();
2260
+ return new arrow2.Int64();
1681
2261
  case "uint8":
1682
- return new arrow.Uint8();
2262
+ return new arrow2.Uint8();
1683
2263
  case "uint16":
1684
- return new arrow.Uint16();
2264
+ return new arrow2.Uint16();
1685
2265
  case "uint32":
1686
- return new arrow.Uint32();
2266
+ return new arrow2.Uint32();
1687
2267
  case "uint64":
1688
- return new arrow.Uint64();
2268
+ return new arrow2.Uint64();
1689
2269
  case "float16":
1690
- return new arrow.Float16();
2270
+ return new arrow2.Float16();
1691
2271
  case "float32":
1692
- return new arrow.Float32();
2272
+ return new arrow2.Float32();
1693
2273
  case "float64":
1694
- return new arrow.Float64();
2274
+ return new arrow2.Float64();
1695
2275
  case "utf8":
1696
- return makeVariableWidthArrowType("Utf8View", new arrow.Utf8(), options);
2276
+ return makeVariableWidthArrowType("Utf8View", new arrow2.Utf8(), options);
1697
2277
  case "utf8-view":
1698
2278
  return makeRequiredArrowViewType("Utf8View");
1699
2279
  case "date-day":
1700
- return new arrow.DateDay();
2280
+ return new arrow2.DateDay();
1701
2281
  case "date-millisecond":
1702
- return new arrow.DateMillisecond();
2282
+ return new arrow2.DateMillisecond();
1703
2283
  case "time-second":
1704
- return new arrow.TimeSecond();
2284
+ return new arrow2.TimeSecond();
1705
2285
  case "time-millisecond":
1706
- return new arrow.TimeMillisecond();
2286
+ return new arrow2.TimeMillisecond();
1707
2287
  case "time-microsecond":
1708
- return new arrow.TimeMicrosecond();
2288
+ return new arrow2.TimeMicrosecond();
1709
2289
  case "time-nanosecond":
1710
- return new arrow.TimeNanosecond();
2290
+ return new arrow2.TimeNanosecond();
1711
2291
  case "timestamp-second":
1712
- return new arrow.TimestampSecond();
2292
+ return new arrow2.TimestampSecond();
1713
2293
  case "timestamp-millisecond":
1714
- return new arrow.TimestampMillisecond();
2294
+ return new arrow2.TimestampMillisecond();
1715
2295
  case "timestamp-microsecond":
1716
- return new arrow.TimestampMicrosecond();
2296
+ return new arrow2.TimestampMicrosecond();
1717
2297
  case "timestamp-nanosecond":
1718
- return new arrow.TimestampNanosecond();
2298
+ return new arrow2.TimestampNanosecond();
1719
2299
  case "interval-daytime":
1720
- return new arrow.IntervalDayTime();
2300
+ return new arrow2.IntervalDayTime();
1721
2301
  case "interval-yearmonth":
1722
- return new arrow.IntervalYearMonth();
2302
+ return new arrow2.IntervalYearMonth();
1723
2303
  default:
1724
2304
  throw new Error("array type not supported");
1725
2305
  }
1726
2306
  }
1727
2307
  function getOptionalArrowConstructor(typeName) {
1728
- return arrow[typeName];
2308
+ return arrow2[typeName];
1729
2309
  }
1730
2310
  function isArrowViewType(arrowType, viewTypeName) {
1731
2311
  const constructor = getOptionalArrowConstructor(viewTypeName);
1732
2312
  if (constructor && arrowType instanceof constructor) {
1733
2313
  return true;
1734
2314
  }
1735
- const guards = arrow.DataType;
2315
+ const guards = arrow2.DataType;
1736
2316
  const guard = viewTypeName === "BinaryView" ? guards.isBinaryView : guards.isUtf8View;
1737
2317
  return Boolean(guard == null ? void 0 : guard(arrowType));
1738
2318
  }
@@ -1766,7 +2346,7 @@ function makeRequiredArrowViewType(viewTypeName) {
1766
2346
  return new constructor();
1767
2347
  }
1768
2348
  function serializeArrowDictionaryKeyType(arrowType) {
1769
- if (arrowType instanceof arrow.Int) {
2349
+ if (arrowType instanceof arrow2.Int) {
1770
2350
  const prefix = arrowType.isSigned ? "int" : "uint";
1771
2351
  const keyType = `${prefix}${arrowType.bitWidth}`;
1772
2352
  if (isSchemaDictionaryKeyType(keyType)) {
@@ -1774,17 +2354,17 @@ function serializeArrowDictionaryKeyType(arrowType) {
1774
2354
  }
1775
2355
  }
1776
2356
  switch (arrowType.constructor) {
1777
- case arrow.Int8:
2357
+ case arrow2.Int8:
1778
2358
  return "int8";
1779
- case arrow.Int16:
2359
+ case arrow2.Int16:
1780
2360
  return "int16";
1781
- case arrow.Int32:
2361
+ case arrow2.Int32:
1782
2362
  return "int32";
1783
- case arrow.Uint8:
2363
+ case arrow2.Uint8:
1784
2364
  return "uint8";
1785
- case arrow.Uint16:
2365
+ case arrow2.Uint16:
1786
2366
  return "uint16";
1787
- case arrow.Uint32:
2367
+ case arrow2.Uint32:
1788
2368
  return "uint32";
1789
2369
  default:
1790
2370
  throw new Error(`arrow dictionary index type not supported: ${arrowType.constructor.name}`);
@@ -1796,146 +2376,28 @@ function isSchemaDictionaryKeyType(keyType) {
1796
2376
  function deserializeArrowDictionaryKeyType(keyType) {
1797
2377
  switch (keyType) {
1798
2378
  case "int8":
1799
- return new arrow.Int8();
2379
+ return new arrow2.Int8();
1800
2380
  case "int16":
1801
- return new arrow.Int16();
2381
+ return new arrow2.Int16();
1802
2382
  case "int32":
1803
- return new arrow.Int32();
2383
+ return new arrow2.Int32();
1804
2384
  case "uint8":
1805
- return new arrow.Uint8();
2385
+ return new arrow2.Uint8();
1806
2386
  case "uint16":
1807
- return new arrow.Uint16();
2387
+ return new arrow2.Uint16();
1808
2388
  case "uint32":
1809
- return new arrow.Uint32();
2389
+ return new arrow2.Uint32();
1810
2390
  default:
1811
2391
  throw new Error(`schema dictionary index type not supported: ${keyType}`);
1812
2392
  }
1813
2393
  }
1814
2394
 
1815
- // ../schema-utils/src/lib/schema/deduce-table-schema.ts
1816
- function deduceTableSchema(table) {
1817
- switch (table.shape) {
1818
- case "array-row-table":
1819
- case "object-row-table":
1820
- return deduceSchemaFromRows(table.data);
1821
- case "geojson-table":
1822
- return deduceSchemaFromGeoJSON(table.features);
1823
- case "columnar-table":
1824
- return deduceSchemaFromColumns(table.data);
1825
- case "arrow-table":
1826
- default:
1827
- throw new Error("Deduce schema");
1828
- }
1829
- }
1830
- function deduceSchemaFromColumns(columnarTable) {
1831
- const fields = [];
1832
- for (const [columnName, column] of Object.entries(columnarTable)) {
1833
- const field = deduceFieldFromColumn(column, columnName);
1834
- fields.push(field);
1835
- }
1836
- return { fields, metadata: {} };
1837
- }
1838
- function deduceSchemaFromRows(rowTable) {
1839
- if (!rowTable.length) {
1840
- throw new Error("deduce from empty table");
1841
- }
1842
- const fields = [];
1843
- const row0 = rowTable[0];
1844
- for (const [columnName, value] of Object.entries(row0)) {
1845
- fields.push(deduceFieldFromValue(value, columnName));
1846
- }
1847
- return { fields, metadata: {} };
1848
- }
1849
- function deduceSchemaFromGeoJSON(features) {
1850
- if (!features.length) {
1851
- throw new Error("deduce from empty table");
1852
- }
1853
- const fields = [];
1854
- const row0 = features[0].properties || {};
1855
- for (const [columnName, value] of Object.entries(row0)) {
1856
- fields.push(deduceFieldFromValue(value, columnName));
1857
- }
1858
- return { fields, metadata: {} };
1859
- }
1860
- function deduceFieldFromColumn(column, name) {
1861
- if (ArrayBuffer.isView(column)) {
1862
- const type = getDataTypeFromArray(column);
1863
- return {
1864
- name,
1865
- type: type.type || "null",
1866
- nullable: type.nullable
1867
- // metadata: {}
1868
- };
1869
- }
1870
- if (Array.isArray(column) && column.length > 0) {
1871
- const value = column[0];
1872
- const type = getDataTypeFromValue(value);
1873
- return {
1874
- name,
1875
- type,
1876
- nullable: true
1877
- // metadata: {},
1878
- };
1879
- }
1880
- throw new Error("empty table");
1881
- }
1882
- function deduceFieldFromValue(value, name) {
1883
- const type = getDataTypeFromValue(value);
1884
- return {
1885
- name,
1886
- type,
1887
- nullable: true
1888
- // metadata: {}
1889
- };
1890
- }
1891
-
1892
- // ../schema-utils/src/lib/table/tables/make-table.ts
1893
- var arrow2 = __toESM(require("apache-arrow"), 1);
1894
- function makeTableFromData(data) {
1895
- let table;
1896
- switch (getTableShapeFromData(data)) {
1897
- case "array-row-table":
1898
- table = { shape: "array-row-table", data };
1899
- break;
1900
- case "object-row-table":
1901
- table = { shape: "object-row-table", data };
1902
- break;
1903
- case "columnar-table":
1904
- table = { shape: "columnar-table", data };
1905
- break;
1906
- case "arrow-table":
1907
- table = { shape: "arrow-table", data };
1908
- break;
1909
- default:
1910
- throw new Error("table");
1911
- }
1912
- const schema = deduceTableSchema(table);
1913
- return { ...table, schema };
1914
- }
1915
- function getTableShapeFromData(data) {
1916
- if (data instanceof arrow2.Table) {
1917
- return "arrow-table";
1918
- }
1919
- if (Array.isArray(data)) {
1920
- if (data.length === 0) {
1921
- throw new Error("cannot deduce type of empty table");
1922
- }
1923
- const firstRow = data[0];
1924
- if (Array.isArray(firstRow)) {
1925
- return "array-row-table";
1926
- }
1927
- if (firstRow && typeof firstRow === "object") {
1928
- return "object-row-table";
1929
- }
1930
- }
1931
- if (data && typeof data === "object") {
1932
- return "columnar-table";
1933
- }
1934
- throw new Error("invalid table");
1935
- }
2395
+ // ../core/src/schema-utils/lib/table/tables/convert-arrow-table.ts
2396
+ var arrow4 = __toESM(require("apache-arrow"), 1);
1936
2397
 
1937
- // ../schema-utils/src/lib/table/tables/table-accessors.ts
2398
+ // ../core/src/schema-utils/lib/table/tables/table-accessors.ts
1938
2399
  function getTableLength(table) {
2400
+ if (!("shape" in table)) return table.numRows;
1939
2401
  switch (table.shape) {
1940
2402
  case "array-row-table":
1941
2403
  case "object-row-table":
@@ -1943,8 +2405,7 @@ function getTableLength(table) {
1943
2405
  case "geojson-table":
1944
2406
  return table.features.length;
1945
2407
  case "arrow-table":
1946
- const arrowTable = table.data;
1947
- return arrowTable.numRows;
2408
+ return getTableLength(table.data);
1948
2409
  case "columnar-table":
1949
2410
  for (const column of Object.values(table.data)) {
1950
2411
  return column.length || 0;
@@ -1955,6 +2416,7 @@ function getTableLength(table) {
1955
2416
  }
1956
2417
  }
1957
2418
  function getTableNumCols(table) {
2419
+ if (!("shape" in table)) return table.numCols;
1958
2420
  if (table.schema) {
1959
2421
  return table.schema.fields.length;
1960
2422
  }
@@ -1971,14 +2433,14 @@ function getTableNumCols(table) {
1971
2433
  case "columnar-table":
1972
2434
  return Object.keys(table.data).length;
1973
2435
  case "arrow-table":
1974
- const arrowTable = table.data;
1975
- return arrowTable.numCols;
2436
+ return getTableNumCols(table.data);
1976
2437
  default:
1977
2438
  throw new Error("table");
1978
2439
  }
1979
2440
  }
1980
2441
  function getTableCell(table, rowIndex, columnName) {
1981
2442
  var _a;
2443
+ if (!("shape" in table)) return (_a = table.getChild(columnName)) == null ? void 0 : _a.get(rowIndex);
1982
2444
  switch (table.shape) {
1983
2445
  case "array-row-table":
1984
2446
  const columnIndex = getTableColumnIndex(table, columnName);
@@ -1991,17 +2453,14 @@ function getTableCell(table, rowIndex, columnName) {
1991
2453
  const column = table.data[columnName];
1992
2454
  return column[rowIndex];
1993
2455
  case "arrow-table":
1994
- const arrowTable = table.data;
1995
- const arrowColumnIndex = arrowTable.schema.fields.findIndex(
1996
- (field) => field.name === columnName
1997
- );
1998
- return (_a = arrowTable.getChildAt(arrowColumnIndex)) == null ? void 0 : _a.get(rowIndex);
2456
+ return getTableCell(table.data, rowIndex, columnName);
1999
2457
  default:
2000
2458
  throw new Error("todo");
2001
2459
  }
2002
2460
  }
2003
2461
  function getTableCellAt(table, rowIndex, columnIndex) {
2004
2462
  var _a;
2463
+ if (!("shape" in table)) return (_a = table.getChildAt(columnIndex)) == null ? void 0 : _a.get(rowIndex);
2005
2464
  switch (table.shape) {
2006
2465
  case "array-row-table":
2007
2466
  return table.data[rowIndex][columnIndex];
@@ -2016,8 +2475,7 @@ function getTableCellAt(table, rowIndex, columnIndex) {
2016
2475
  const column = table.data[columnName3];
2017
2476
  return column[rowIndex];
2018
2477
  case "arrow-table":
2019
- const arrowTable = table.data;
2020
- return (_a = arrowTable.getChildAt(columnIndex)) == null ? void 0 : _a.get(rowIndex);
2478
+ return getTableCellAt(table.data, rowIndex, columnIndex);
2021
2479
  default:
2022
2480
  throw new Error("todo");
2023
2481
  }
@@ -2036,121 +2494,281 @@ function getTableColumnName(table, columnIndex) {
2036
2494
  if (!columnName) {
2037
2495
  throw new Error(`${columnIndex}`);
2038
2496
  }
2039
- return columnName;
2497
+ return columnName;
2498
+ }
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
+ }
2506
+ switch (table.shape) {
2507
+ case "object-row-table":
2508
+ return copy ? Object.fromEntries(Object.entries(table.data[rowIndex])) : table.data[rowIndex];
2509
+ case "array-row-table":
2510
+ if (table.schema) {
2511
+ const objectRow = target || {};
2512
+ for (let i = 0; i < table.schema.fields.length; i++) {
2513
+ objectRow[table.schema.fields[i].name] = table.data[rowIndex][i];
2514
+ }
2515
+ return objectRow;
2516
+ }
2517
+ throw new Error("no schema");
2518
+ case "geojson-table":
2519
+ if (table.schema) {
2520
+ const objectRow = target || {};
2521
+ for (let i = 0; i < table.schema.fields.length; i++) {
2522
+ objectRow[table.schema.fields[i].name] = table.features[rowIndex][table.schema.fields[i].name];
2523
+ }
2524
+ return objectRow;
2525
+ }
2526
+ throw new Error("no schema");
2527
+ case "columnar-table":
2528
+ if (table.schema) {
2529
+ const objectRow = target || {};
2530
+ for (let i = 0; i < table.schema.fields.length; i++) {
2531
+ objectRow[table.schema.fields[i].name] = table.data[table.schema.fields[i].name][rowIndex];
2532
+ }
2533
+ return objectRow;
2534
+ } else {
2535
+ const objectRow = target || {};
2536
+ for (const [name, column] of Object.entries(table.data)) {
2537
+ objectRow[name] = column[rowIndex];
2538
+ }
2539
+ return objectRow;
2540
+ }
2541
+ case "arrow-table":
2542
+ return getTableRowAsObject(table.data, rowIndex, target, copy);
2543
+ default:
2544
+ throw new Error("shape");
2545
+ }
2546
+ }
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
+ }
2556
+ switch (table.shape) {
2557
+ case "array-row-table":
2558
+ return copy ? Array.from(table.data[rowIndex]) : table.data[rowIndex];
2559
+ case "object-row-table":
2560
+ if (table.schema) {
2561
+ const arrayRow = target || [];
2562
+ for (let i = 0; i < table.schema.fields.length; i++) {
2563
+ arrayRow[i] = table.data[rowIndex][table.schema.fields[i].name];
2564
+ }
2565
+ return arrayRow;
2566
+ }
2567
+ return Object.values(table.data[rowIndex]);
2568
+ case "geojson-table":
2569
+ if (table.schema) {
2570
+ const arrayRow = target || [];
2571
+ for (let i = 0; i < table.schema.fields.length; i++) {
2572
+ arrayRow[i] = table.features[rowIndex][table.schema.fields[i].name];
2573
+ }
2574
+ return arrayRow;
2575
+ }
2576
+ return Object.values(table.features[rowIndex]);
2577
+ case "columnar-table":
2578
+ if (table.schema) {
2579
+ const arrayRow = target || [];
2580
+ for (let i = 0; i < table.schema.fields.length; i++) {
2581
+ arrayRow[i] = table.data[table.schema.fields[i].name][rowIndex];
2582
+ }
2583
+ return arrayRow;
2584
+ } else {
2585
+ const arrayRow = target || [];
2586
+ let i = 0;
2587
+ for (const column of Object.values(table.data)) {
2588
+ arrayRow[i] = column[rowIndex];
2589
+ i++;
2590
+ }
2591
+ return arrayRow;
2592
+ }
2593
+ case "arrow-table":
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":
2611
+ default:
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
+ };
2665
+ }
2666
+ throw new Error("empty table");
2040
2667
  }
2041
- function getTableRowAsObject(table, rowIndex, target, copy) {
2042
- switch (table.shape) {
2668
+ function deduceFieldFromValue(value, name) {
2669
+ const type = getDataTypeFromValue(value);
2670
+ return {
2671
+ name,
2672
+ type,
2673
+ nullable: true
2674
+ // metadata: {}
2675
+ };
2676
+ }
2677
+
2678
+ // ../core/src/schema-utils/lib/table/tables/table-column.ts
2679
+ function makeColumnFromField(field, length) {
2680
+ const ArrayType = getArrayTypeFromDataType(field.type, field.nullable);
2681
+ return new ArrayType(length);
2682
+ }
2683
+
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) {
2043
2689
  case "object-row-table":
2044
- return copy ? Object.fromEntries(Object.entries(table.data[rowIndex])) : table.data[rowIndex];
2690
+ return convertToObjectRowTable(table);
2045
2691
  case "array-row-table":
2046
- if (table.schema) {
2047
- const objectRow2 = target || {};
2048
- for (let i = 0; i < table.schema.fields.length; i++) {
2049
- objectRow2[table.schema.fields[i].name] = table.data[rowIndex][i];
2050
- }
2051
- return objectRow2;
2052
- }
2053
- throw new Error("no schema");
2054
- case "geojson-table":
2055
- if (table.schema) {
2056
- const objectRow2 = target || {};
2057
- for (let i = 0; i < table.schema.fields.length; i++) {
2058
- objectRow2[table.schema.fields[i].name] = table.features[rowIndex][table.schema.fields[i].name];
2059
- }
2060
- return objectRow2;
2061
- }
2062
- throw new Error("no schema");
2692
+ return convertToArrayRowTable(table);
2063
2693
  case "columnar-table":
2064
- if (table.schema) {
2065
- const objectRow2 = target || {};
2066
- for (let i = 0; i < table.schema.fields.length; i++) {
2067
- objectRow2[table.schema.fields[i].name] = table.data[table.schema.fields[i].name][rowIndex];
2068
- }
2069
- return objectRow2;
2070
- } else {
2071
- const objectRow2 = target || {};
2072
- for (const [name, column] of Object.entries(table.data)) {
2073
- objectRow2[name] = column[rowIndex];
2074
- }
2075
- return objectRow2;
2076
- }
2077
- case "arrow-table":
2078
- const arrowTable = table.data;
2079
- const objectRow = target || {};
2080
- const row = arrowTable.get(rowIndex);
2081
- const schema = arrowTable.schema;
2082
- for (let i = 0; i < schema.fields.length; i++) {
2083
- objectRow[schema.fields[i].name] = row == null ? void 0 : row[schema.fields[i].name];
2084
- }
2085
- return objectRow;
2694
+ return convertToColumnarTable(table);
2086
2695
  default:
2087
- throw new Error("shape");
2696
+ throw new Error(shape);
2088
2697
  }
2089
2698
  }
2090
- function getTableRowAsArray(table, rowIndex, target, copy) {
2091
- switch (table.shape) {
2092
- case "array-row-table":
2093
- return copy ? Array.from(table.data[rowIndex]) : table.data[rowIndex];
2094
- case "object-row-table":
2095
- if (table.schema) {
2096
- const arrayRow2 = target || [];
2097
- for (let i = 0; i < table.schema.fields.length; i++) {
2098
- arrayRow2[i] = table.data[rowIndex][table.schema.fields[i].name];
2099
- }
2100
- return arrayRow2;
2101
- }
2102
- return Object.values(table.data[rowIndex]);
2103
- case "geojson-table":
2104
- if (table.schema) {
2105
- const arrayRow2 = target || [];
2106
- for (let i = 0; i < table.schema.fields.length; i++) {
2107
- arrayRow2[i] = table.features[rowIndex][table.schema.fields[i].name];
2108
- }
2109
- return arrayRow2;
2110
- }
2111
- return Object.values(table.features[rowIndex]);
2112
- case "columnar-table":
2113
- if (table.schema) {
2114
- const arrayRow2 = target || [];
2115
- for (let i = 0; i < table.schema.fields.length; i++) {
2116
- arrayRow2[i] = table.data[table.schema.fields[i].name][rowIndex];
2117
- }
2118
- return arrayRow2;
2119
- } else {
2120
- const arrayRow2 = target || [];
2121
- let i = 0;
2122
- for (const column of Object.values(table.data)) {
2123
- arrayRow2[i] = column[rowIndex];
2124
- i++;
2125
- }
2126
- return arrayRow2;
2127
- }
2128
- case "arrow-table":
2129
- const arrowTable = table.data;
2130
- const arrayRow = target || [];
2131
- const row = arrowTable.get(rowIndex);
2132
- const schema = arrowTable.schema;
2133
- for (let i = 0; i < schema.fields.length; i++) {
2134
- arrayRow[i] = row == null ? void 0 : row[schema.fields[i].name];
2135
- }
2136
- return arrayRow;
2137
- default:
2138
- throw new Error("shape");
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
+ }
2139
2713
  }
2714
+ return {
2715
+ shape: "columnar-table",
2716
+ schema,
2717
+ data: columns
2718
+ };
2140
2719
  }
2141
-
2142
- // ../schema-utils/src/lib/table/tables/table-column.ts
2143
- function makeColumnFromField(field, length) {
2144
- const ArrayType = getArrayTypeFromDataType(field.type, field.nullable);
2145
- return new ArrayType(length);
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
+ };
2146
2749
  }
2147
2750
 
2148
- // ../schema-utils/src/lib/table/tables/convert-arrow-table.ts
2149
- var arrow4 = __toESM(require("apache-arrow"), 1);
2150
-
2151
- // ../schema-utils/src/lib/table/batches/make-arrow-batch-iterator.ts
2751
+ // ../core/src/schema-utils/lib/table/batches/make-arrow-batch-iterator.ts
2152
2752
  var arrow3 = __toESM(require("apache-arrow"), 1);
2153
- 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
+ }
2154
2772
  const arrowSchema = convertSchemaToArrow(table.schema);
2155
2773
  const length = getTableLength(table);
2156
2774
  const numColumns = getTableNumCols(table);
@@ -2186,8 +2804,9 @@ function* makeArrowRecordBatchIterator(table, options) {
2186
2804
  builders.map((builder) => builder.finish());
2187
2805
  }
2188
2806
 
2189
- // ../schema-utils/src/lib/table/tables/convert-arrow-table.ts
2807
+ // ../core/src/schema-utils/lib/table/tables/convert-arrow-table.ts
2190
2808
  function convertTableToArrow(table, options) {
2809
+ if (!("shape" in table)) return table;
2191
2810
  switch (table.shape) {
2192
2811
  case "arrow-table":
2193
2812
  return table.data;
@@ -2196,14 +2815,16 @@ function convertTableToArrow(table, options) {
2196
2815
  // return convertColumnarTableToArrow(table, options);
2197
2816
  // fall through
2198
2817
  default:
2199
- const arrowBatchIterator = makeArrowRecordBatchIterator(table, options);
2200
- 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
+ ]);
2201
2822
  }
2202
2823
  }
2203
2824
  function convertArrowToTable(arrowTable, shape) {
2204
2825
  switch (shape) {
2205
- case "arrow-table":
2206
- return convertArrowToArrowTable(arrowTable);
2826
+ case "arrow":
2827
+ return arrowTable;
2207
2828
  case "array-row-table":
2208
2829
  return convertArrowToArrayRowTable(arrowTable);
2209
2830
  case "object-row-table":
@@ -2216,13 +2837,6 @@ function convertArrowToTable(arrowTable, shape) {
2216
2837
  throw new Error(shape);
2217
2838
  }
2218
2839
  }
2219
- function convertArrowToArrowTable(arrowTable) {
2220
- return {
2221
- shape: "arrow-table",
2222
- schema: convertArrowToSchema(arrowTable.schema),
2223
- data: arrowTable
2224
- };
2225
- }
2226
2840
  function convertArrowToArrayRowTable(arrowTable) {
2227
2841
  const columnarTable = convertArrowToColumnarTable(arrowTable);
2228
2842
  return convertTable(columnarTable, "array-row-table");
@@ -2261,361 +2875,267 @@ function convertArrowToGeoJSONTable(arrowTable) {
2261
2875
  };
2262
2876
  }
2263
2877
 
2264
- // ../schema-utils/src/lib/table/tables/convert-table.ts
2265
- function convertTable(table, shape) {
2266
- switch (shape) {
2267
- case "object-row-table":
2268
- return convertToObjectRowTable(table);
2269
- case "array-row-table":
2270
- return convertToArrayRowTable(table);
2271
- case "columnar-table":
2272
- return convertToColumnarTable(table);
2273
- case "arrow-table":
2274
- return convertToArrowTable(table);
2275
- default:
2276
- throw new Error(shape);
2277
- }
2278
- }
2279
- function convertToColumnarTable(table) {
2280
- const schema = table.schema || deduceTableSchema(table);
2281
- const fields = schema.fields;
2282
- if (table.shape === "columnar-table") {
2283
- return { ...table, schema };
2284
- }
2285
- const length = getTableLength(table);
2286
- const columns = {};
2287
- for (const field of fields) {
2288
- const column = makeColumnFromField(field, length);
2289
- columns[field.name] = column;
2290
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
2291
- column[rowIndex] = getTableCell(table, rowIndex, field.name);
2292
- }
2293
- }
2294
- return {
2295
- shape: "columnar-table",
2296
- schema,
2297
- data: columns
2298
- };
2299
- }
2300
- function convertToArrayRowTable(table) {
2301
- if (table.shape === "array-row-table") {
2302
- return table;
2303
- }
2304
- const length = getTableLength(table);
2305
- const data = new Array(length);
2306
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
2307
- data[rowIndex] = getTableRowAsArray(table, rowIndex);
2308
- }
2309
- return {
2310
- shape: "array-row-table",
2311
- schema: table.schema,
2312
- data
2313
- };
2314
- }
2315
- function convertToObjectRowTable(table) {
2316
- if (table.shape === "object-row-table") {
2317
- return table;
2318
- }
2319
- const length = getTableLength(table);
2320
- const data = new Array(length);
2321
- for (let rowIndex = 0; rowIndex < length; rowIndex++) {
2322
- data[rowIndex] = getTableRowAsObject(table, rowIndex);
2323
- }
2324
- return {
2325
- shape: "object-row-table",
2326
- schema: table.schema,
2327
- data
2328
- };
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));
2329
2882
  }
2330
- function convertToArrowTable(table) {
2331
- const schema = table.schema || deduceTableSchema(table);
2332
- const arrowTable = convertTableToArrow({ ...table, schema });
2333
- return {
2334
- shape: "arrow-table",
2335
- schema,
2336
- data: arrowTable
2337
- };
2883
+ function makeArrowMetadataBatch(schema) {
2884
+ return new arrow5.RecordBatch(snapshotArrowSchema(schema), void 0);
2338
2885
  }
2339
2886
 
2340
- // ../schema-utils/src/deprecated.ts
2887
+ // ../core/src/schema-utils/deprecated.ts
2341
2888
  var convertArrowToTable2 = ((...args) => convertArrowToTable(
2342
2889
  ...args
2343
2890
  ));
2344
2891
 
2345
- // dist/sql-utils.js
2346
- function isNodeRuntime() {
2347
- var _a;
2348
- return typeof process !== "undefined" && Boolean((_a = process.versions) == null ? void 0 : _a.node);
2349
- }
2350
- function escapeSqlString(value) {
2351
- return value.replace(/'/g, "''");
2352
- }
2353
- function quoteSqlIdentifier(identifier) {
2354
- return `"${identifier.replace(/"/g, '""')}"`;
2355
- }
2356
- function convertRowsToArrowTable(rows) {
2357
- const table = rows.length > 0 ? makeTableFromData(rows) : { shape: "object-row-table", schema: { fields: [], metadata: {} }, data: rows };
2358
- const schema = deducePrimitiveAwareSchema(table);
2359
- if (table.data.length === 0 || schema.fields.length === 0) {
2360
- return {
2361
- 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(
2362
2915
  schema,
2363
- data: new arrow5.Table(new arrow5.Schema([]))
2364
- };
2365
- }
2366
- return {
2367
- shape: "arrow-table",
2368
- schema,
2369
- data: arrow5.tableFromJSON(table.data)
2370
- };
2371
- }
2372
- function convertSQLColumnsToSchema(columns) {
2373
- return {
2374
- metadata: {},
2375
- fields: columns.sort((left, right) => (left.ordinalPosition || 0) - (right.ordinalPosition || 0)).map((column) => ({
2376
- name: column.columnName,
2377
- type: mapSQLTypeToDataType(column.sqlType),
2378
- nullable: column.nullable,
2379
- metadata: {
2380
- sqlType: column.sqlType
2381
- }
2382
- }))
2383
- };
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);
2384
2925
  }
2385
- function mapSQLTypeToDataType(sqlType) {
2386
- const normalizedType = sqlType.toUpperCase();
2387
- if (normalizedType.includes("BIGINT") || normalizedType === "HUGEINT" || normalizedType === "LONG") {
2388
- return "int64";
2926
+
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
+ );
2941
+ }
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
+ );
2389
2976
  }
2390
- if (normalizedType.includes("INT") || normalizedType === "INTEGER" || normalizedType === "SMALLINT" || normalizedType === "TINYINT") {
2391
- 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;
2392
2987
  }
2393
- if (normalizedType.includes("DOUBLE") || normalizedType.includes("FLOAT8") || normalizedType.includes("REAL")) {
2394
- 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);
2395
2994
  }
2396
- if (normalizedType.includes("FLOAT") || normalizedType.includes("DECIMAL") || normalizedType.includes("NUMERIC")) {
2397
- return "float64";
2995
+ if (isNotPredicate(predicate)) {
2996
+ const value2 = evaluatePredicate(predicate.args[0], columns, rowIndex);
2997
+ return value2 === null ? null : !value2;
2398
2998
  }
2399
- if (normalizedType.includes("BOOL")) {
2400
- return "bool";
2999
+ const value = getColumnValue(columns, predicate.args[0].property, rowIndex);
3000
+ if (isNullPredicate(predicate)) {
3001
+ return value === null || value === void 0;
2401
3002
  }
2402
- if (normalizedType.includes("DATE")) {
2403
- return "date-day";
3003
+ if (isInPredicate(predicate)) {
3004
+ return evaluateIn(value, predicate.args[1]);
2404
3005
  }
2405
- if (normalizedType.includes("TIME") || normalizedType.includes("TIMESTAMP")) {
2406
- 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}".`);
2407
3013
  }
2408
- if (normalizedType.includes("BLOB") || normalizedType.includes("BINARY")) {
2409
- 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];
2410
3018
  }
2411
- return "utf8";
2412
- }
2413
- function deducePrimitiveAwareSchema(table) {
2414
- return {
2415
- ...table.schema,
2416
- fields: table.schema.fields.map((field) => {
2417
- if (field.type !== "null") {
2418
- return field;
2419
- }
2420
- const columnValues = table.data.map((row) => row[field.name]);
2421
- const inferredType = getDataTypeFromArray(columnValues);
2422
- return { ...field, type: inferredType.type, nullable: inferredType.nullable };
2423
- })
2424
- };
2425
- }
2426
- function getQualifiedTableName(options) {
2427
- const identifiers = [options.catalogName, options.schemaName, options.tableName].filter((value) => Boolean(value));
2428
- return identifiers.map((identifier) => quoteSqlIdentifier(identifier)).join(".");
2429
- }
2430
-
2431
- // dist/table-query-capabilities.js
2432
- var ARROW_TABLE_QUERY_CAPABILITIES = Object.freeze({
2433
- projection: "residual",
2434
- predicate: "residual",
2435
- limit: "residual",
2436
- streaming: false,
2437
- cancellation: true,
2438
- expressions: "residual",
2439
- orderBy: "residual",
2440
- aggregates: "residual"
2441
- });
2442
- var SQL_DATA_SOURCE_TABLE_QUERY_CAPABILITIES = Object.freeze({
2443
- projection: "pushdown",
2444
- predicate: "pushdown",
2445
- limit: "pushdown",
2446
- streaming: false,
2447
- cancellation: false,
2448
- expressions: "pushdown",
2449
- orderBy: "pushdown",
2450
- aggregates: "pushdown"
2451
- });
2452
-
2453
- // dist/sql-source.js
2454
- var SQL_ADAPTER_FACTORIES = /* @__PURE__ */ new Map();
2455
- function registerSQLAdapter(type, factory) {
2456
- SQL_ADAPTER_FACTORIES.set(type, factory);
3019
+ return value;
2457
3020
  }
2458
- function getSQLAdapterFactory(type) {
2459
- 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;
2460
3026
  }
2461
- var SQL_SOURCE_DEFAULT_OPTIONS = {
2462
- sql: {
2463
- resultFormat: "auto",
2464
- adapterOptions: {},
2465
- browserAdapterFactory: void 0,
2466
- nodeAdapterFactory: void 0
2467
- },
2468
- duckdb: {
2469
- accessMode: "auto",
2470
- bundles: void 0,
2471
- databasePath: void 0,
2472
- remoteUrl: void 0,
2473
- workerUrl: void 0
2474
- },
2475
- snowflake: {
2476
- account: void 0,
2477
- database: void 0,
2478
- schema: void 0,
2479
- token: void 0,
2480
- warehouse: void 0,
2481
- role: void 0,
2482
- authenticator: void 0
3027
+ function evaluateOr(values) {
3028
+ if (values.includes(true)) {
3029
+ return true;
2483
3030
  }
2484
- };
2485
- var SQLDataSource = class extends DataSource {
2486
- /** Optimization and execution capabilities of portable table-bound queries. */
2487
- tableQueryCapabilities = SQL_DATA_SOURCE_TABLE_QUERY_CAPABILITIES;
2488
- sourceType;
2489
- adapterFactory;
2490
- adapterPromise = null;
2491
- metadataPromise = null;
2492
- constructor(data, options, sourceType, defaultOptions = SQL_SOURCE_DEFAULT_OPTIONS, adapterFactory, coreApi) {
2493
- super(data, options, defaultOptions, coreApi);
2494
- this.sourceType = sourceType;
2495
- 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;
2496
3036
  }
2497
- /** Returns cached high-level metadata for the SQL source. */
2498
- async getMetadata() {
2499
- if (!this.metadataPromise) {
2500
- this.metadataPromise = this.loadMetadata();
3037
+ for (const candidate of candidates) {
3038
+ if (compareValues(value, candidate) === 0) {
3039
+ return true;
2501
3040
  }
2502
- return this.metadataPromise;
2503
3041
  }
2504
- /** Lists available catalogs from the connected database. */
2505
- async listCatalogs() {
2506
- const adapter = await this.getAdapter();
2507
- return await adapter.listCatalogs();
3042
+ return false;
3043
+ }
3044
+ function evaluateComparison(value, predicate) {
3045
+ if (value === null || value === void 0) {
3046
+ return null;
2508
3047
  }
2509
- /** Lists available schemas from the connected database. */
2510
- async listSchemas(catalogName) {
2511
- const adapter = await this.getAdapter();
2512
- 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;
2513
3062
  }
2514
- /** Lists available tables from the connected database. */
2515
- async listTables(options) {
2516
- const adapter = await this.getAdapter();
2517
- 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
+ );
2518
3069
  }
2519
- /** Returns the schema for a specific SQL table. */
2520
- async getTableSchema(options) {
2521
- const adapter = await this.getAdapter();
2522
- return await adapter.getTableSchema(options);
3070
+ if (left instanceof Date && right instanceof Date) {
3071
+ return compareArrowQueryNumbers(left.getTime(), right.getTime());
2523
3072
  }
2524
- /** Executes a SQL query and returns object rows. */
2525
- async queryRows(query, options = {}) {
2526
- const adapter = await this.getAdapter();
2527
- const compiledQuery = this.compileQuery(query, options);
2528
- return await adapter.executeRows(compiledQuery.sql, compiledQuery.options);
3073
+ if (typeof left === "number" && right instanceof Date) {
3074
+ return compareArrowQueryNumbers(left, right.getTime());
2529
3075
  }
2530
- /** Executes a SQL query and returns a loaders.gl Arrow table. */
2531
- async queryArrow(query, options = {}) {
2532
- const adapter = await this.getAdapter();
2533
- const compiledQuery = this.compileQuery(query, options);
2534
- if (adapter.executeArrow) {
2535
- return await adapter.executeArrow(compiledQuery.sql, compiledQuery.options);
2536
- }
2537
- const rows = await adapter.executeRows(compiledQuery.sql, compiledQuery.options);
2538
- return convertRowsToArrowTable(rows);
3076
+ if (left instanceof Uint8Array && right instanceof Uint8Array) {
3077
+ return compareBinary(left, right);
2539
3078
  }
2540
- /** Closes any underlying adapter connection. */
2541
- async close() {
2542
- if (!this.adapterPromise) {
2543
- return;
2544
- }
2545
- const adapter = await this.adapterPromise;
2546
- await adapter.close();
2547
- this.adapterPromise = null;
2548
- this.metadataPromise = null;
3079
+ if (typeof left === "bigint" && typeof right === "bigint") {
3080
+ return left === right ? 0 : left < right ? -1 : 1;
2549
3081
  }
2550
- async getAdapter() {
2551
- if (!this.adapterPromise) {
2552
- this.adapterPromise = this.loadAdapter();
2553
- }
2554
- 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;
2555
3087
  }
2556
- /** Converts a portable query object to SQL while leaving raw SQL text unchanged. */
2557
- compileQuery(query, options) {
2558
- if (typeof query === "string") {
2559
- return { sql: query, options };
2560
- }
2561
- const namedParameters = options.parameters && !Array.isArray(options.parameters) ? options.parameters : void 0;
2562
- const compiledQuery = compileSQLTableQuery(query, {
2563
- dialect: getSQLTableQueryDialect(this.sourceType),
2564
- parameters: namedParameters
2565
- });
2566
- return {
2567
- sql: compiledQuery.sql,
2568
- options: { signal: options.signal, parameters: compiledQuery.parameters }
2569
- };
3088
+ if (typeof left === "number" && typeof right === "number") {
3089
+ return compareArrowQueryNumbers(left, right);
2570
3090
  }
2571
- async loadAdapter() {
2572
- var _a, _b;
2573
- try {
2574
- const runtimeFactory = isNodeRuntime() ? (_a = this.options.sql) == null ? void 0 : _a.nodeAdapterFactory : (_b = this.options.sql) == null ? void 0 : _b.browserAdapterFactory;
2575
- const factory = runtimeFactory || this.adapterFactory;
2576
- if (!factory) {
2577
- throw new Error(`No SQL adapter is registered for source type "${this.sourceType}".`);
2578
- }
2579
- const adapter = await factory({
2580
- coreApi: this.coreApi,
2581
- options: this.options,
2582
- sourceType: this.sourceType,
2583
- url: this.url
2584
- });
2585
- await adapter.connect();
2586
- return adapter;
2587
- } catch (error) {
2588
- 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;
2589
3112
  }
2590
3113
  }
2591
- async loadMetadata() {
2592
- const adapter = await this.getAdapter();
2593
- const [catalogs, schemas, tables] = await Promise.all([
2594
- adapter.listCatalogs(),
2595
- adapter.listSchemas(),
2596
- adapter.listTables()
2597
- ]);
2598
- return {
2599
- type: this.sourceType,
2600
- capabilities: adapter.capabilities,
2601
- catalogs,
2602
- schemas,
2603
- tables
2604
- };
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";
2605
3119
  }
2606
- };
2607
- function getSQLTableQueryDialect(sourceType) {
2608
- if (sourceType === "duckdb-sql")
2609
- return "duckdb";
2610
- if (sourceType === "snowflake-sql")
2611
- return "snowflake";
2612
- 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";
2613
3136
  }
2614
3137
 
2615
- // dist/query-arrow-table.js
2616
- var arrow6 = __toESM(require("apache-arrow"), 1);
2617
-
2618
- // dist/table-query.js
3138
+ // ../core/src/scan/table-query.ts
2619
3139
  function planTableQuery2(sourceColumnNames, options = {}) {
2620
3140
  if (options.predicate) {
2621
3141
  validateSQLPredicate(options.predicate);
@@ -2626,22 +3146,71 @@ function explainTableQuery2(sourceColumnNames, options = {}) {
2626
3146
  if (options.predicate) {
2627
3147
  validateSQLPredicate(options.predicate);
2628
3148
  }
2629
- return explainTableQuery(sourceColumnNames, options, SQL_DATA_SOURCE_TABLE_QUERY_CAPABILITIES);
3149
+ return explainTableQuery(
3150
+ sourceColumnNames,
3151
+ options,
3152
+ SQL_DATA_SOURCE_TABLE_QUERY_CAPABILITIES
3153
+ );
2630
3154
  }
2631
3155
  function getSQLPredicateColumnNames(predicate) {
2632
3156
  return getColumnarPredicateColumns(predicate);
2633
3157
  }
2634
3158
 
2635
- // dist/query-arrow-table.js
3159
+ // ../core/src/scan/query-arrow-table.ts
2636
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
+ }
2637
3186
  if (options.predicate) {
2638
3187
  validateSQLPredicate(options.predicate);
2639
3188
  }
2640
- return explainTableQuery(sourceTable.data.schema.fields.map((field) => field.name), options, ARROW_TABLE_QUERY_CAPABILITIES);
3189
+ return explainTableQuery(
3190
+ sourceTable.schema.fields.map((field) => field.name),
3191
+ options,
3192
+ ARROW_TABLE_QUERY_CAPABILITIES
3193
+ );
2641
3194
  }
2642
3195
  function queryArrowTable(sourceTable, options = {}) {
2643
3196
  var _a, _b;
2644
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
+ }
2645
3214
  if (options.join) {
2646
3215
  validateArrowJoinQuery(sourceTable, options);
2647
3216
  }
@@ -2654,63 +3223,46 @@ function queryArrowTable(sourceTable, options = {}) {
2654
3223
  if (hasRelationalOperators(options)) {
2655
3224
  return queryArrowTableRelational(sourceTable, options);
2656
3225
  }
2657
- const sourceData = sourceTable.data;
2658
- const plan = planTableQuery2(sourceData.schema.fields.map((field) => field.name), options);
3226
+ const sourceData = sourceTable;
3227
+ const plan = planTableQuery2(
3228
+ sourceData.schema.fields.map((field) => field.name),
3229
+ options
3230
+ );
2659
3231
  const scannedData = sourceData.select([...getScanStep(plan).columns]);
2660
- const projectedData = scannedData.select([...getProjectStep(plan).columns]);
2661
3232
  const predicate = (_a = getFilterStep(plan)) == null ? void 0 : _a.predicate;
2662
3233
  const limit = ((_b = getLimitStep(plan)) == null ? void 0 : _b.limit) ?? Number.POSITIVE_INFINITY;
2663
3234
  if (!predicate) {
2664
- return wrapArrowTable(projectedData.slice(0, limit));
2665
- }
2666
- const predicateColumns = getPredicateColumns(scannedData, getSQLPredicateColumnNames(predicate));
2667
- const matchingRowIndices = [];
2668
- for (let rowIndex = 0; rowIndex < scannedData.numRows && matchingRowIndices.length < limit; rowIndex++) {
2669
- throwIfAborted(options.signal);
2670
- if (evaluatePredicate(predicate, predicateColumns, rowIndex) === true) {
2671
- matchingRowIndices.push(rowIndex);
2672
- }
2673
- }
2674
- if (matchingRowIndices.length === 0) {
2675
- return wrapArrowTable(projectedData.slice(0, 0));
2676
- }
2677
- const outputColumns = {};
2678
- for (const field of projectedData.schema.fields) {
2679
- const sourceColumn = projectedData.getChild(field.name);
2680
- if (!sourceColumn) {
2681
- throw new Error(`Arrow query could not read column "${field.name}".`);
2682
- }
2683
- const values = matchingRowIndices.map((rowIndex) => sourceColumn.get(rowIndex));
2684
- outputColumns[field.name] = arrow6.vectorFromArray(values, sourceColumn.type);
3235
+ const projectedData = projectArrowTable(scannedData, getProjectStep(plan).columns);
3236
+ return projectedData.numRows === 0 ? projectedData : projectedData.slice(0, limit);
2685
3237
  }
2686
- 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);
2687
3240
  }
2688
3241
  function queryArrowTableRelational(sourceTable, options) {
2689
3242
  var _a, _b, _c, _d, _e, _f;
2690
- const sourceData = sourceTable.data;
3243
+ const sourceData = sourceTable;
2691
3244
  const sourceColumnNames = sourceData.schema.fields.map((field) => field.name);
2692
3245
  const relationalPlan = planRelationalQuery(sourceColumnNames, options);
2693
3246
  const scanStep = relationalPlan.tablePlan.find((step) => step.kind === "scan");
2694
3247
  if (!scanStep || scanStep.kind !== "scan") {
2695
3248
  throw new Error("Arrow relational query plan is missing a scan step.");
2696
3249
  }
2697
- const scannedTable = wrapArrowTable(sourceData.select([...scanStep.columns]));
3250
+ const scannedTable = sourceData.select([...scanStep.columns]);
2698
3251
  const filteredTable = queryArrowTable(scannedTable, {
2699
3252
  predicate: options.predicate,
2700
3253
  columns: scanStep.columns,
2701
3254
  signal: options.signal
2702
3255
  });
2703
- let rows = arrowTableToRows(filteredTable.data);
3256
+ let rows = arrowTableToRows(filteredTable);
2704
3257
  for (const child of options.union || []) {
2705
3258
  const childTable = (_a = options.tables) == null ? void 0 : _a[child.source];
2706
- if (!childTable)
2707
- throw new Error(`Arrow union source not found: ${child.source}`);
3259
+ if (!childTable) throw new Error(`Arrow union source not found: ${child.source}`);
2708
3260
  const childResult = queryArrowTable(childTable, {
2709
3261
  ...child.query || {},
2710
3262
  columns: getUnionChildColumns(sourceColumnNames, options),
2711
3263
  signal: options.signal
2712
3264
  });
2713
- rows = rows.concat(arrowTableToRows(childResult.data));
3265
+ rows = rows.concat(arrowTableToRows(childResult));
2714
3266
  }
2715
3267
  for (const expression of options.expressions || []) {
2716
3268
  for (const row of rows) {
@@ -2744,11 +3296,12 @@ function getUnionChildColumns(sourceColumnNames, options) {
2744
3296
  const expressionNames = new Set((options.expressions || []).map((expression) => expression.name));
2745
3297
  const aggregateNames = new Set((options.aggregates || []).map((aggregate) => aggregate.name));
2746
3298
  const outputColumns = options.columns || (((_a = options.groupBy) == null ? void 0 : _a.length) || ((_b = options.aggregates) == null ? void 0 : _b.length) ? [...options.groupBy || [], ...(options.aggregates || []).map((aggregate) => aggregate.name)] : sourceColumnNames);
2747
- const requiredColumns = new Set(outputColumns.filter((column) => !expressionNames.has(column) && !aggregateNames.has(column)));
3299
+ const requiredColumns = new Set(
3300
+ outputColumns.filter((column) => !expressionNames.has(column) && !aggregateNames.has(column))
3301
+ );
2748
3302
  for (const expression of options.expressions || []) {
2749
3303
  const definition = expression.expression;
2750
- if (definition.op === "column")
2751
- requiredColumns.add(definition.column);
3304
+ if (definition.op === "column") requiredColumns.add(definition.column);
2752
3305
  if ("left" in definition) {
2753
3306
  requiredColumns.add(definition.left);
2754
3307
  requiredColumns.add(definition.right);
@@ -2759,30 +3312,30 @@ function getUnionChildColumns(sourceColumnNames, options) {
2759
3312
  requiredColumns.add(key.column);
2760
3313
  }
2761
3314
  }
2762
- for (const column of options.groupBy || [])
2763
- requiredColumns.add(column);
3315
+ for (const column of options.groupBy || []) requiredColumns.add(column);
2764
3316
  for (const aggregate of options.aggregates || []) {
2765
- if (aggregate.column)
2766
- requiredColumns.add(aggregate.column);
3317
+ if (aggregate.column) requiredColumns.add(aggregate.column);
2767
3318
  }
2768
3319
  return sourceColumnNames.filter((column) => requiredColumns.has(column));
2769
3320
  }
2770
3321
  function hasRelationalOperators(options) {
2771
3322
  var _a, _b, _c, _d, _e;
2772
- return Boolean(((_a = options.expressions) == null ? void 0 : _a.length) || ((_b = options.orderBy) == null ? void 0 : _b.length) || ((_c = options.groupBy) == null ? void 0 : _c.length) || ((_d = options.aggregates) == null ? void 0 : _d.length) || ((_e = options.union) == null ? void 0 : _e.length) || options.join);
3323
+ return Boolean(
3324
+ ((_a = options.expressions) == null ? void 0 : _a.length) || ((_b = options.orderBy) == null ? void 0 : _b.length) || ((_c = options.groupBy) == null ? void 0 : _c.length) || ((_d = options.aggregates) == null ? void 0 : _d.length) || ((_e = options.union) == null ? void 0 : _e.length) || options.join
3325
+ );
2773
3326
  }
2774
3327
  function hasRelationalOperatorsExceptJoin(options) {
2775
3328
  var _a, _b, _c, _d, _e;
2776
- return Boolean(((_a = options.expressions) == null ? void 0 : _a.length) || ((_b = options.orderBy) == null ? void 0 : _b.length) || ((_c = options.groupBy) == null ? void 0 : _c.length) || ((_d = options.aggregates) == null ? void 0 : _d.length) || ((_e = options.union) == null ? void 0 : _e.length));
3329
+ return Boolean(
3330
+ ((_a = options.expressions) == null ? void 0 : _a.length) || ((_b = options.orderBy) == null ? void 0 : _b.length) || ((_c = options.groupBy) == null ? void 0 : _c.length) || ((_d = options.aggregates) == null ? void 0 : _d.length) || ((_e = options.union) == null ? void 0 : _e.length)
3331
+ );
2777
3332
  }
2778
3333
  function queryArrowJoin(sourceTable, options) {
2779
3334
  var _a;
2780
3335
  const join = options.join;
2781
- if (!join)
2782
- throw new Error("Arrow join query is missing a join definition.");
3336
+ if (!join) throw new Error("Arrow join query is missing a join definition.");
2783
3337
  const childTable = (_a = options.tables) == null ? void 0 : _a[join.child.source];
2784
- if (!childTable)
2785
- throw new Error(`Arrow join source not found: ${join.child.source}`);
3338
+ if (!childTable) throw new Error(`Arrow join source not found: ${join.child.source}`);
2786
3339
  const leftResult = queryArrowTable(sourceTable, {
2787
3340
  predicate: options.predicate,
2788
3341
  columns: void 0,
@@ -2792,12 +3345,12 @@ function queryArrowJoin(sourceTable, options) {
2792
3345
  ...join.child.query || {},
2793
3346
  signal: options.signal
2794
3347
  });
2795
- const leftRows = arrowTableToRows(leftResult.data);
2796
- const rightRows = arrowTableToRows(rightResult.data);
3348
+ const leftRows = arrowTableToRows(leftResult);
3349
+ const rightRows = arrowTableToRows(rightResult);
2797
3350
  const rightPrefix = `${join.child.source}.`;
2798
3351
  const defaultOutputColumns = [
2799
- ...sourceTable.data.schema.fields.map((field) => field.name),
2800
- ...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}`)
2801
3354
  ];
2802
3355
  const rows = [];
2803
3356
  for (const leftRow of leftRows) {
@@ -2805,29 +3358,43 @@ function queryArrowJoin(sourceTable, options) {
2805
3358
  if (leftRow[join.left] !== null && leftRow[join.left] !== void 0 && rightRow[join.right] !== null && rightRow[join.right] !== void 0 && compareSortValues(leftRow[join.left], rightRow[join.right]) === 0) {
2806
3359
  rows.push({
2807
3360
  ...leftRow,
2808
- ...Object.fromEntries(Object.entries(rightRow).map(([key, value]) => [`${rightPrefix}${key}`, value]))
3361
+ ...Object.fromEntries(
3362
+ Object.entries(rightRow).map(([key, value]) => [`${rightPrefix}${key}`, value])
3363
+ )
2809
3364
  });
2810
3365
  }
2811
3366
  }
2812
3367
  }
2813
3368
  const outputColumns = options.columns || defaultOutputColumns;
2814
- const outputRows = rows.map((row) => Object.fromEntries(outputColumns.map((column) => [column, row[column]])));
3369
+ const outputRows = rows.map(
3370
+ (row) => Object.fromEntries(outputColumns.map((column) => [column, row[column]]))
3371
+ );
2815
3372
  const limitedRows = outputRows.slice(0, options.limit);
2816
- return limitedRows.length ? convertRowsToArrowTable(limitedRows) : createEmptyRelationalResult(sourceTable.data, outputColumns, options, new Map(rightResult.data.schema.fields.map((field) => [
2817
- `${rightPrefix}${field.name}`,
2818
- new arrow6.Field(`${rightPrefix}${field.name}`, field.type, field.nullable, field.metadata)
2819
- ])));
3373
+ return limitedRows.length ? convertRowsToArrowTable(limitedRows) : createEmptyRelationalResult(
3374
+ sourceTable,
3375
+ outputColumns,
3376
+ options,
3377
+ new Map(
3378
+ rightResult.schema.fields.map((field) => [
3379
+ `${rightPrefix}${field.name}`,
3380
+ new arrow8.Field(
3381
+ `${rightPrefix}${field.name}`,
3382
+ field.type,
3383
+ field.nullable,
3384
+ field.metadata
3385
+ )
3386
+ ])
3387
+ )
3388
+ );
2820
3389
  }
2821
3390
  function validateArrowJoinQuery(sourceTable, options) {
2822
3391
  var _a, _b, _c;
2823
3392
  const join = options.join;
2824
- if (!join)
2825
- return;
2826
- const sourceColumnNames = sourceTable.data.schema.fields.map((field) => field.name);
3393
+ if (!join) return;
3394
+ const sourceColumnNames = sourceTable.schema.fields.map((field) => field.name);
2827
3395
  const childTable = (_a = options.tables) == null ? void 0 : _a[join.child.source];
2828
- if (!childTable)
2829
- throw new Error(`Arrow join source not found: ${join.child.source}`);
2830
- const childColumnNames = childTable.data.schema.fields.map((field) => field.name);
3396
+ if (!childTable) throw new Error(`Arrow join source not found: ${join.child.source}`);
3397
+ const childColumnNames = childTable.schema.fields.map((field) => field.name);
2831
3398
  if (!sourceColumnNames.includes(join.left)) {
2832
3399
  throw new Error(`Arrow join column not found: ${join.left}`);
2833
3400
  }
@@ -2872,8 +3439,7 @@ function arrowTableToRows(data) {
2872
3439
  return Array.from({ length: data.numRows }, (_, rowIndex) => {
2873
3440
  const row = {};
2874
3441
  for (const column of columns) {
2875
- if (!column.vector)
2876
- throw new Error(`Arrow relational query could not read ${column.name}`);
3442
+ if (!column.vector) throw new Error(`Arrow relational query could not read ${column.name}`);
2877
3443
  row[column.name] = column.vector.get(rowIndex);
2878
3444
  }
2879
3445
  return row;
@@ -2881,14 +3447,11 @@ function arrowTableToRows(data) {
2881
3447
  }
2882
3448
  function evaluateRelationalExpression(expression, row) {
2883
3449
  const definition = expression.expression;
2884
- if (definition.op === "literal")
2885
- return definition.value;
2886
- if (definition.op === "column")
2887
- return row[definition.column];
3450
+ if (definition.op === "literal") return definition.value;
3451
+ if (definition.op === "column") return row[definition.column];
2888
3452
  const left = row[definition.left];
2889
3453
  const right = row[definition.right];
2890
- if (left === null || left === void 0 || right === null || right === void 0)
2891
- return null;
3454
+ if (left === null || left === void 0 || right === null || right === void 0) return null;
2892
3455
  if (typeof left !== "number" || typeof right !== "number") {
2893
3456
  throw new Error(`Arrow relational expression ${expression.name} requires numeric operands.`);
2894
3457
  }
@@ -2900,282 +3463,124 @@ function evaluateRelationalExpression(expression, row) {
2900
3463
  case "multiply":
2901
3464
  return left * right;
2902
3465
  case "divide":
2903
- return right === 0 ? null : left / right;
2904
- }
2905
- }
2906
- function compareRows(left, right, orderBy) {
2907
- for (const key of orderBy) {
2908
- const leftValue = left[key.column];
2909
- const rightValue = right[key.column];
2910
- const nulls = key.nulls || "last";
2911
- if (leftValue === null || leftValue === void 0 || rightValue === null || rightValue === void 0) {
2912
- if (leftValue === rightValue)
2913
- continue;
2914
- const nullResult = leftValue === null || leftValue === void 0 ? -1 : 1;
2915
- return nulls === "first" ? nullResult : -nullResult;
2916
- }
2917
- const result = compareSortValues(leftValue, rightValue);
2918
- if (result)
2919
- return (key.direction === "desc" ? -1 : 1) * result;
2920
- }
2921
- return 0;
2922
- }
2923
- function compareSortValues(left, right) {
2924
- if (left instanceof Date && right instanceof Date)
2925
- return compareNumbers(left.getTime(), right.getTime());
2926
- if (typeof left === "bigint" && typeof right === "bigint")
2927
- return left === right ? 0 : left < right ? -1 : 1;
2928
- if (typeof left === "number" && typeof right === "number")
2929
- return compareNumbers(left, right);
2930
- if (typeof left === "string" && typeof right === "string")
2931
- return left === right ? 0 : left < right ? -1 : 1;
2932
- if (typeof left === "boolean" && typeof right === "boolean")
2933
- return Number(left) - Number(right);
2934
- throw new Error(`Arrow relational order cannot compare ${getValueType(left)} with ${getValueType(right)}.`);
2935
- }
2936
- function aggregateRows(rows, groupBy, aggregates) {
2937
- const groups = /* @__PURE__ */ new Map();
2938
- for (const row of rows) {
2939
- const key = serializeGroupKey(groupBy.map((column) => row[column]));
2940
- const group = groups.get(key) || [];
2941
- group.push(row);
2942
- groups.set(key, group);
2943
- }
2944
- if (!groupBy.length && !rows.length)
2945
- groups.set("[]", []);
2946
- return [...groups.values()].map((group) => {
2947
- var _a;
2948
- const output = {};
2949
- for (const column of groupBy)
2950
- output[column] = ((_a = group[0]) == null ? void 0 : _a[column]) ?? null;
2951
- for (const aggregate of aggregates)
2952
- output[aggregate.name] = evaluateAggregate(aggregate, group);
2953
- return output;
2954
- });
2955
- }
2956
- function serializeGroupKey(values) {
2957
- return values.map((value) => {
2958
- if (value === null || value === void 0)
2959
- return "null:";
2960
- if (value instanceof Date)
2961
- return `date:${value.getTime()}`;
2962
- if (value instanceof Uint8Array)
2963
- return `bytes:${Array.from(value).join(".")}`;
2964
- return `${typeof value}:${String(value)}`;
2965
- }).join("");
2966
- }
2967
- function createEmptyRelationalResult(sourceData, outputColumns, options, additionalFields) {
2968
- const sourceFields = new Map(sourceData.schema.fields.map((field) => [field.name, field]));
2969
- const expressions = new Map((options.expressions || []).map((expression) => [expression.name, expression]));
2970
- const aggregates = new Map((options.aggregates || []).map((aggregate) => [aggregate.name, aggregate]));
2971
- const fields = outputColumns.map((columnName) => {
2972
- const sourceField = sourceFields.get(columnName);
2973
- if (sourceField)
2974
- return sourceField;
2975
- const additionalField = additionalFields == null ? void 0 : additionalFields.get(columnName);
2976
- if (additionalField)
2977
- return additionalField;
2978
- const expression = expressions.get(columnName);
2979
- if (expression)
2980
- return new arrow6.Field(columnName, getExpressionType(expression, sourceFields), true);
2981
- const aggregate = aggregates.get(columnName);
2982
- if (aggregate)
2983
- return new arrow6.Field(columnName, getAggregateType(aggregate, sourceFields), true);
2984
- return new arrow6.Field(columnName, new arrow6.Utf8(), true);
2985
- });
2986
- const columns = {};
2987
- for (const field of fields)
2988
- columns[field.name] = arrow6.vectorFromArray([], field.type);
2989
- return wrapArrowTable(new arrow6.Table(new arrow6.Schema(fields), columns));
2990
- }
2991
- function getExpressionType(expression, sourceFields) {
2992
- var _a;
2993
- if (expression.expression.op === "literal") {
2994
- const value = expression.expression.value;
2995
- if (typeof value === "boolean")
2996
- return new arrow6.Bool();
2997
- if (typeof value === "number")
2998
- return new arrow6.Float64();
2999
- return new arrow6.Utf8();
3000
- }
3001
- if (expression.expression.op === "column") {
3002
- return ((_a = sourceFields.get(expression.expression.column)) == null ? void 0 : _a.type) || new arrow6.Utf8();
3003
- }
3004
- return new arrow6.Float64();
3005
- }
3006
- function getAggregateType(aggregate, sourceFields) {
3007
- var _a;
3008
- if (aggregate.function === "count")
3009
- return new arrow6.Int32();
3010
- return aggregate.column ? ((_a = sourceFields.get(aggregate.column)) == null ? void 0 : _a.type) || new arrow6.Float64() : new arrow6.Float64();
3011
- }
3012
- function evaluateAggregate(aggregate, rows) {
3013
- if (aggregate.function === "count")
3014
- return aggregate.column ? rows.filter((row) => row[aggregate.column] != null).length : rows.length;
3015
- const values = rows.map((row) => row[aggregate.column]).filter((value) => value != null);
3016
- if (!values.length)
3017
- return null;
3018
- if (!values.every((value) => typeof value === "number"))
3019
- throw new Error(`Arrow aggregate ${aggregate.function} requires numeric values.`);
3020
- const numericValues = values;
3021
- switch (aggregate.function) {
3022
- case "sum":
3023
- return numericValues.reduce((sum, value) => sum + value, 0);
3024
- case "avg":
3025
- return numericValues.reduce((sum, value) => sum + value, 0) / numericValues.length;
3026
- case "min":
3027
- return Math.min(...numericValues);
3028
- case "max":
3029
- return Math.max(...numericValues);
3030
- }
3031
- }
3032
- function getPredicateColumns(sourceTable, columnNames) {
3033
- const columns = {};
3034
- for (const columnName of columnNames) {
3035
- const column = sourceTable.getChild(columnName);
3036
- if (!column) {
3037
- throw new Error(`Arrow query column not found: ${columnName}`);
3038
- }
3039
- columns[columnName] = column;
3040
- }
3041
- return columns;
3042
- }
3043
- function evaluatePredicate(predicate, columns, rowIndex) {
3044
- if (isLogicalPredicate(predicate)) {
3045
- const values = predicate.args.map((child) => evaluatePredicate(child, columns, rowIndex));
3046
- return predicate.op === "and" ? evaluateAnd(values) : evaluateOr(values);
3047
- }
3048
- if (isNotPredicate(predicate)) {
3049
- const value2 = evaluatePredicate(predicate.args[0], columns, rowIndex);
3050
- return value2 === null ? null : !value2;
3051
- }
3052
- const value = getColumnValue(columns, predicate.args[0].property, rowIndex);
3053
- if (isNullPredicate(predicate)) {
3054
- return value === null;
3055
- }
3056
- if (isInPredicate(predicate)) {
3057
- return evaluateIn(value, predicate.args[1]);
3058
- }
3059
- return evaluateComparison(value, predicate);
3060
- }
3061
- function getColumnValue(columns, columnName, rowIndex) {
3062
- const column = columns[columnName];
3063
- if (!column) {
3064
- throw new Error(`Arrow query could not read predicate column "${columnName}".`);
3065
- }
3066
- return column.get(rowIndex);
3067
- }
3068
- function evaluateAnd(values) {
3069
- if (values.includes(false)) {
3070
- return false;
3071
- }
3072
- return values.includes(null) ? null : true;
3073
- }
3074
- function evaluateOr(values) {
3075
- if (values.includes(true)) {
3076
- return true;
3077
- }
3078
- return values.includes(null) ? null : false;
3079
- }
3080
- function evaluateIn(value, candidates) {
3081
- if (value === null || value === void 0) {
3082
- return null;
3083
- }
3084
- for (const candidate of candidates) {
3085
- if (compareValues(value, candidate) === 0) {
3086
- return true;
3087
- }
3088
- }
3089
- return false;
3090
- }
3091
- function evaluateComparison(value, predicate) {
3092
- if (value === null || value === void 0) {
3093
- return null;
3094
- }
3095
- const comparison = compareValues(value, predicate.args[1]);
3096
- switch (predicate.op) {
3097
- case "=":
3098
- return comparison === 0;
3099
- case "<>":
3100
- return comparison !== 0;
3101
- case "<":
3102
- return comparison < 0;
3103
- case "<=":
3104
- return comparison <= 0;
3105
- case ">":
3106
- return comparison > 0;
3107
- case ">=":
3108
- return comparison >= 0;
3109
- }
3110
- }
3111
- function compareValues(left, right) {
3112
- if (isSQLPredicateParameter(right)) {
3113
- throw new Error(`Arrow query parameter ":${right.parameter}" must be bound before query execution.`);
3114
- }
3115
- if (left instanceof Date && right instanceof Date) {
3116
- return compareNumbers(left.getTime(), right.getTime());
3117
- }
3118
- if (typeof left === "number" && right instanceof Date) {
3119
- return compareNumbers(left, right.getTime());
3466
+ return right === 0 ? null : left / right;
3120
3467
  }
3121
- if (left instanceof Uint8Array && right instanceof Uint8Array) {
3122
- return compareBinary(left, right);
3468
+ }
3469
+ function compareRows(left, right, orderBy) {
3470
+ for (const key of orderBy) {
3471
+ const leftValue = left[key.column];
3472
+ const rightValue = right[key.column];
3473
+ const nulls = key.nulls || "last";
3474
+ if (leftValue === null || leftValue === void 0 || rightValue === null || rightValue === void 0) {
3475
+ if (leftValue === rightValue) continue;
3476
+ const nullResult = leftValue === null || leftValue === void 0 ? -1 : 1;
3477
+ return nulls === "first" ? nullResult : -nullResult;
3478
+ }
3479
+ const result = compareSortValues(leftValue, rightValue);
3480
+ if (result) return (key.direction === "desc" ? -1 : 1) * result;
3123
3481
  }
3124
- 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")
3125
3488
  return left === right ? 0 : left < right ? -1 : 1;
3126
- }
3127
- if (typeof left === "bigint" && typeof right === "number" && Number.isSafeInteger(right)) {
3128
- return compareValues(left, BigInt(right));
3129
- }
3130
- if (typeof left === "number" && typeof right === "bigint" && Number.isSafeInteger(left)) {
3131
- return compareValues(BigInt(left), right);
3132
- }
3133
- if (typeof left === "number" && typeof right === "number") {
3134
- return compareNumbers(left, right);
3135
- }
3136
- 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")
3137
3491
  return left === right ? 0 : left < right ? -1 : 1;
3138
- }
3139
- if (typeof left === "boolean" && typeof right === "boolean") {
3140
- return Number(left) - Number(right);
3141
- }
3142
- throw new Error(`Arrow query cannot compare ${getValueType(left)} with ${getValueType(right)} values.`);
3492
+ if (typeof left === "boolean" && typeof right === "boolean") return Number(left) - Number(right);
3493
+ throw new Error(
3494
+ `Arrow relational order cannot compare ${getArrowQueryValueType(left)} with ${getArrowQueryValueType(right)}.`
3495
+ );
3143
3496
  }
3144
- function compareNumbers(left, right) {
3145
- if (!Number.isFinite(left) || !Number.isFinite(right)) {
3146
- 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);
3147
3504
  }
3148
- 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
+ });
3149
3514
  }
3150
- function compareBinary(left, right) {
3151
- const commonLength = Math.min(left.length, right.length);
3152
- for (let index = 0; index < commonLength; index++) {
3153
- if (left[index] !== right[index]) {
3154
- return left[index] < right[index] ? -1 : 1;
3155
- }
3156
- }
3157
- 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("");
3158
3522
  }
3159
- function getValueType(value) {
3160
- if (value instanceof Date) {
3161
- 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();
3162
3555
  }
3163
- if (value instanceof Uint8Array) {
3164
- return "Uint8Array";
3556
+ if (expression.expression.op === "column") {
3557
+ return ((_a = sourceFields.get(expression.expression.column)) == null ? void 0 : _a.type) || new arrow8.Utf8();
3165
3558
  }
3166
- return typeof value;
3167
- }
3168
- function isLogicalPredicate(predicate) {
3169
- return predicate.op === "and" || predicate.op === "or";
3170
- }
3171
- function isNotPredicate(predicate) {
3172
- return predicate.op === "not";
3559
+ return new arrow8.Float64();
3173
3560
  }
3174
- function isNullPredicate(predicate) {
3175
- 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();
3176
3565
  }
3177
- function isInPredicate(predicate) {
3178
- 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
+ }
3179
3584
  }
3180
3585
  function getScanStep(plan) {
3181
3586
  const step = plan.find((candidate) => candidate.kind === "scan");
@@ -3188,7 +3593,9 @@ function getFilterStep(plan) {
3188
3593
  return plan.find((candidate) => candidate.kind === "filter");
3189
3594
  }
3190
3595
  function getProjectStep(plan) {
3191
- const step = plan.find((candidate) => candidate.kind === "project");
3596
+ const step = plan.find(
3597
+ (candidate) => candidate.kind === "project"
3598
+ );
3192
3599
  if (!step) {
3193
3600
  throw new Error("Arrow query plan is missing a project step.");
3194
3601
  }
@@ -3204,13 +3611,6 @@ function throwIfAborted(signal) {
3204
3611
  throw error;
3205
3612
  }
3206
3613
  }
3207
- function wrapArrowTable(data) {
3208
- return {
3209
- shape: "arrow-table",
3210
- schema: convertArrowToSchema(data.schema),
3211
- data
3212
- };
3213
- }
3214
3614
 
3215
3615
  // dist/arrow-table-source.js
3216
3616
  var ArrowTableSource = class extends DataSource {
@@ -3221,7 +3621,7 @@ var ArrowTableSource = class extends DataSource {
3221
3621
  /** Discovers query-visible fields and capabilities without evaluating any rows. */
3222
3622
  async getQueryMetadata(options = {}) {
3223
3623
  throwIfAborted2(options.signal);
3224
- const schema = convertArrowToSchema(this.data.data.schema);
3624
+ const schema = convertArrowToSchema(this.data.schema);
3225
3625
  return createScanQueryMetadata({
3226
3626
  sourceType: "arrow-table",
3227
3627
  queryType: "table",
@@ -3229,7 +3629,7 @@ var ArrowTableSource = class extends DataSource {
3229
3629
  schema,
3230
3630
  capabilities: { table: ARROW_TABLE_QUERY_CAPABILITIES },
3231
3631
  columnRoles: getArrowColumnRoles(schema.fields.map((field) => field.name)),
3232
- statistics: { rowCount: this.data.data.numRows }
3632
+ statistics: { rowCount: this.data.numRows }
3233
3633
  });
3234
3634
  }
3235
3635
  /** Executes a portable query and returns an Arrow table result. */
@@ -3242,8 +3642,11 @@ var ArrowTableSource = class extends DataSource {
3242
3642
  }
3243
3643
  /** Executes a query as one bounded Arrow batch. */
3244
3644
  async *read(options = {}) {
3245
- const result = this.query(options);
3246
- 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);
3247
3650
  }
3248
3651
  };
3249
3652
  function getArrowColumnRoles(columnNames) {
@@ -3269,7 +3672,219 @@ function throwIfAborted2(signal) {
3269
3672
  }
3270
3673
 
3271
3674
  // dist/duckdb-sql-source.js
3272
- var VERSION2 = true ? "5.0.0-alpha.9" : "latest";
3675
+ var import_apache_arrow2 = require("apache-arrow");
3676
+
3677
+ // dist/duckdb-table-query.js
3678
+ var import_apache_arrow = require("apache-arrow");
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";
3273
3888
  var DuckDBSQLSource = {
3274
3889
  dataType: null,
3275
3890
  batchType: null,
@@ -3308,9 +3923,41 @@ var DuckDBSQLSource = {
3308
3923
  },
3309
3924
  defaultOptions: SQL_SOURCE_DEFAULT_OPTIONS,
3310
3925
  testURL: (url) => /^duckdb:\/\//i.test(url),
3311
- createDataSource: (data, options, coreApi) => new DuckDBSQLDataSource(data, options, coreApi)
3926
+ createDataSourceSync: (data, options, coreApi) => new DuckDBSQLDataSource(data, options, coreApi)
3312
3927
  };
3313
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
+ }
3949
+ /** Uploads an Arrow table through IPC. Supported by the browser DuckDB-Wasm adapter. */
3950
+ async insertArrowTable(table, options) {
3951
+ await this.insertArrowIPC((0, import_apache_arrow2.tableToIPC)(table, "stream"), options);
3952
+ }
3953
+ /** Uploads a complete Arrow IPC stream to a browser DuckDB-Wasm table. */
3954
+ async insertArrowIPC(bytes, options) {
3955
+ const adapter = await this.getAdapter();
3956
+ if (!adapter.insertArrowIPC) {
3957
+ throw new Error("This DuckDB adapter does not support Arrow IPC uploads.");
3958
+ }
3959
+ await adapter.insertArrowIPC(bytes, options);
3960
+ }
3314
3961
  constructor(data, options, coreApi) {
3315
3962
  super(data, options, DuckDBSQLSource.type, SQL_SOURCE_DEFAULT_OPTIONS, createDuckDBAdapter, coreApi);
3316
3963
  }
@@ -3450,7 +4097,7 @@ async function createDuckDBBrowserAdapter(context) {
3450
4097
  const connection = await database.connect();
3451
4098
  const executeRows = async (sqlText, options = {}) => {
3452
4099
  const result = await executeDuckDBBrowserQuery(connection, sqlText, options.parameters);
3453
- return convertArrowToTable2(result, "object-row-table").data;
4100
+ return convertArrowToTable2((0, import_apache_arrow2.tableFromIPC)(result), "object-row-table").data;
3454
4101
  };
3455
4102
  return {
3456
4103
  capabilities: {
@@ -3459,6 +4106,13 @@ async function createDuckDBBrowserAdapter(context) {
3459
4106
  runtime: "browser",
3460
4107
  isDynamic: true
3461
4108
  },
4109
+ async insertArrowIPC(bytes, options) {
4110
+ await connection.insertArrowFromIPCStream(new Uint8Array(bytes), {
4111
+ name: options.tableName,
4112
+ schema: options.schemaName,
4113
+ create: options.create ?? true
4114
+ });
4115
+ },
3462
4116
  async connect() {
3463
4117
  },
3464
4118
  async close() {
@@ -3535,21 +4189,23 @@ async function createDuckDBBrowserAdapter(context) {
3535
4189
  },
3536
4190
  async executeArrow(sqlText, options = {}) {
3537
4191
  const result = await executeDuckDBBrowserQuery(connection, sqlText, options.parameters);
3538
- return convertArrowToTable2(result, "arrow-table");
4192
+ return (0, import_apache_arrow2.tableFromIPC)(result);
3539
4193
  }
3540
4194
  };
3541
4195
  }
3542
4196
  async function executeDuckDBBrowserQuery(connection, sqlText, parameters) {
3543
- if (!parameters) {
3544
- return await connection.query(sqlText);
3545
- }
3546
- const values = Array.isArray(parameters) ? parameters : Object.values(parameters);
3547
- const statement = await connection.prepare(sqlText);
3548
- try {
3549
- return await statement.query(...values);
3550
- } finally {
3551
- await statement.close();
3552
- }
4197
+ return await connection.useUnsafe(async (bindings, connectionId) => {
4198
+ if (!parameters) {
4199
+ return await bindings.runQuery(connectionId, sqlText);
4200
+ }
4201
+ const values = Array.isArray(parameters) ? parameters : Object.values(parameters);
4202
+ const statementId = await bindings.createPrepared(connectionId, sqlText);
4203
+ try {
4204
+ return await bindings.runPrepared(connectionId, statementId, values);
4205
+ } finally {
4206
+ await bindings.closePrepared(connectionId, statementId);
4207
+ }
4208
+ });
3553
4209
  }
3554
4210
  function getDuckDBDatabasePath(url, options) {
3555
4211
  var _a;
@@ -3565,7 +4221,7 @@ function getDuckDBDatabasePath(url, options) {
3565
4221
  }
3566
4222
 
3567
4223
  // dist/snowflake-sql-source.js
3568
- var VERSION3 = true ? "5.0.0-alpha.9" : "latest";
4224
+ var VERSION3 = true ? "5.0.0-beta.10" : "latest";
3569
4225
  var SnowflakeSQLSource = {
3570
4226
  dataType: null,
3571
4227
  batchType: null,
@@ -3604,7 +4260,7 @@ var SnowflakeSQLSource = {
3604
4260
  },
3605
4261
  defaultOptions: SQL_SOURCE_DEFAULT_OPTIONS,
3606
4262
  testURL: (url) => /^(sql\+snowflake|snowflake):\/\//i.test(url),
3607
- createDataSource: (data, options, coreApi) => new SnowflakeSQLDataSource(data, options, coreApi)
4263
+ createDataSourceSync: (data, options, coreApi) => new SnowflakeSQLDataSource(data, options, coreApi)
3608
4264
  };
3609
4265
  var SnowflakeSQLDataSource = class extends SQLDataSource {
3610
4266
  constructor(data, options, coreApi) {
@@ -3770,301 +4426,15 @@ function getSnowflakeBaseUrl(url, options) {
3770
4426
  return `https://${account}.snowflakecomputing.com`;
3771
4427
  }
3772
4428
 
3773
- // dist/parse-sql-predicate.js
3774
- var MAXIMUM_SQL_LENGTH = 65536;
3775
- var MAXIMUM_SQL_TOKENS = 4096;
3776
- var MAXIMUM_EXPRESSION_DEPTH = 64;
3777
- function parseSQLPredicate(source, options = {}) {
3778
- const parser = new SQLPredicateParser(source, options.parameters ?? {}, options.preserveParameters === true);
3779
- const predicate = parser.parse();
4429
+ // ../core/src/scan/parse-sql-predicate.ts
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);
3780
4433
  validateSQLPredicate(predicate);
3781
4434
  return predicate;
3782
4435
  }
3783
- var SQLPredicateParser = class {
3784
- /** Token stream parsed by this parser instance. */
3785
- tokens;
3786
- /** Named scalar parameters available to the expression. */
3787
- parameters;
3788
- /** Whether named parameters remain references in the parsed AST. */
3789
- preserveParameters;
3790
- /** Current index in the token stream. */
3791
- position = 0;
3792
- constructor(source, parameters, preserveParameters) {
3793
- this.tokens = tokenizeSQLPredicate(source);
3794
- this.parameters = parameters;
3795
- this.preserveParameters = preserveParameters;
3796
- }
3797
- /** Parses the complete token stream. */
3798
- parse() {
3799
- const predicate = this.parseOr(0);
3800
- this.takeSymbol(";");
3801
- const token = this.peek();
3802
- if (token) {
3803
- throw new Error(`SQL predicate contains unexpected token ${JSON.stringify(token.value)}`);
3804
- }
3805
- return predicate;
3806
- }
3807
- /** Parses left-associative disjunction. */
3808
- parseOr(depth) {
3809
- let predicate = this.parseAnd(depth + 1);
3810
- while (this.takeKeyword("OR")) {
3811
- predicate = combineLogicalPredicate("or", predicate, this.parseAnd(depth + 1));
3812
- }
3813
- return predicate;
3814
- }
3815
- /** Parses left-associative conjunction. */
3816
- parseAnd(depth) {
3817
- let predicate = this.parseNot(depth + 1);
3818
- while (this.takeKeyword("AND")) {
3819
- predicate = combineLogicalPredicate("and", predicate, this.parseNot(depth + 1));
3820
- }
3821
- return predicate;
3822
- }
3823
- /** Parses boolean negation and parenthesized predicates. */
3824
- parseNot(depth) {
3825
- this.assertDepth(depth);
3826
- if (this.takeKeyword("NOT")) {
3827
- return { op: "not", args: [this.parseNot(depth + 1)] };
3828
- }
3829
- if (this.takeSymbol("(")) {
3830
- const predicate = this.parseOr(depth + 1);
3831
- this.expectSymbol(")");
3832
- return predicate;
3833
- }
3834
- return this.parseLeaf();
3835
- }
3836
- /** Parses one comparison, membership, or null predicate. */
3837
- parseLeaf() {
3838
- var _a, _b;
3839
- const property = this.parseProperty();
3840
- if (this.takeKeyword("IS")) {
3841
- const negated = this.takeKeyword("NOT");
3842
- this.expectKeyword("NULL");
3843
- const predicate = { op: "isNull", args: [property] };
3844
- return negated ? { op: "not", args: [predicate] } : predicate;
3845
- }
3846
- const negatedMembership = this.takeKeyword("NOT");
3847
- if (negatedMembership || this.peekKeyword("IN")) {
3848
- this.expectKeyword("IN");
3849
- this.expectSymbol("(");
3850
- if (((_a = this.peek()) == null ? void 0 : _a.kind) === "symbol" && ((_b = this.peek()) == null ? void 0 : _b.value) === ")") {
3851
- throw new Error("SQL predicate IN requires at least one scalar value");
3852
- }
3853
- const values = [this.parseValue()];
3854
- while (this.takeSymbol(",")) {
3855
- values.push(this.parseValue());
3856
- }
3857
- this.expectSymbol(")");
3858
- const predicate = { op: "in", args: [property, values] };
3859
- return negatedMembership ? { op: "not", args: [predicate] } : predicate;
3860
- }
3861
- const token = this.take();
3862
- if ((token == null ? void 0 : token.kind) !== "symbol" || !["=", "!=", "<>", "<", "<=", ">", ">="].includes(token.value)) {
3863
- throw new Error("SQL predicate expected a comparison, IN, or IS NULL operator");
3864
- }
3865
- const operator = token.value === "!=" ? "<>" : token.value;
3866
- return {
3867
- op: operator,
3868
- args: [property, this.parseValue()]
3869
- };
3870
- }
3871
- /** Parses an identifier into a property reference. */
3872
- parseProperty() {
3873
- const token = this.take();
3874
- if ((token == null ? void 0 : token.kind) !== "identifier") {
3875
- throw new Error("SQL predicate expected a column identifier");
3876
- }
3877
- return token.quoted ? { property: token.value, quoted: true } : { property: token.value };
3878
- }
3879
- /** Parses one scalar literal or named parameter. */
3880
- parseValue() {
3881
- const negative = this.takeSymbol("-");
3882
- const token = this.take();
3883
- if (!token) {
3884
- throw new Error("SQL predicate expected a scalar value");
3885
- }
3886
- if (negative && token.kind !== "number") {
3887
- throw new Error("SQL predicate only permits unary minus on numeric literals");
3888
- }
3889
- if (token.kind === "number") {
3890
- return parseNumericLiteral(negative ? `-${token.value}` : token.value);
3891
- }
3892
- if (negative) {
3893
- throw new Error("SQL predicate expected a numeric literal after unary minus");
3894
- }
3895
- if (token.kind === "string") {
3896
- return token.value;
3897
- }
3898
- if (token.kind === "parameter") {
3899
- if (this.preserveParameters) {
3900
- return { parameter: token.value };
3901
- }
3902
- if (!Object.hasOwn(this.parameters, token.value)) {
3903
- throw new Error(`SQL predicate parameter ":${token.value}" requires a value`);
3904
- }
3905
- const value = this.parameters[token.value];
3906
- validateParameterValue(value, token.value);
3907
- return value;
3908
- }
3909
- if (token.kind === "identifier" && /^(TRUE|FALSE)$/i.test(token.value)) {
3910
- return /^TRUE$/i.test(token.value);
3911
- }
3912
- if (token.kind === "identifier" && /^NULL$/i.test(token.value)) {
3913
- throw new Error("SQL predicate comparisons with NULL must use IS NULL or IS NOT NULL");
3914
- }
3915
- throw new Error(`SQL predicate does not support scalar token ${JSON.stringify(token.value)}`);
3916
- }
3917
- /** Rejects excessively nested inputs before recursion can exhaust the stack. */
3918
- assertDepth(depth) {
3919
- if (depth > MAXIMUM_EXPRESSION_DEPTH) {
3920
- throw new Error("SQL predicate nesting exceeds its safe limit");
3921
- }
3922
- }
3923
- /** Consumes one required keyword. */
3924
- expectKeyword(keyword) {
3925
- if (!this.takeKeyword(keyword)) {
3926
- throw new Error(`SQL predicate expected ${keyword}`);
3927
- }
3928
- }
3929
- /** Returns whether the next token is a keyword without consuming it. */
3930
- peekKeyword(keyword) {
3931
- const token = this.peek();
3932
- return (token == null ? void 0 : token.kind) === "identifier" && token.value.toUpperCase() === keyword;
3933
- }
3934
- /** Consumes one optional keyword. */
3935
- takeKeyword(keyword) {
3936
- if (!this.peekKeyword(keyword)) {
3937
- return false;
3938
- }
3939
- this.position++;
3940
- return true;
3941
- }
3942
- /** Consumes one required symbol. */
3943
- expectSymbol(symbol) {
3944
- if (!this.takeSymbol(symbol)) {
3945
- throw new Error(`SQL predicate expected ${JSON.stringify(symbol)}`);
3946
- }
3947
- }
3948
- /** Consumes one optional symbol. */
3949
- takeSymbol(symbol) {
3950
- const token = this.peek();
3951
- if ((token == null ? void 0 : token.kind) !== "symbol" || token.value !== symbol) {
3952
- return false;
3953
- }
3954
- this.position++;
3955
- return true;
3956
- }
3957
- /** Consumes and returns the next token. */
3958
- take() {
3959
- const token = this.tokens[this.position];
3960
- if (token) {
3961
- this.position++;
3962
- }
3963
- return token;
3964
- }
3965
- /** Returns the next token without consuming it. */
3966
- peek() {
3967
- return this.tokens[this.position];
3968
- }
3969
- };
3970
- function combineLogicalPredicate(operator, left, right) {
3971
- return left.op === operator ? { op: operator, args: [...left.args, right] } : { op: operator, args: [left, right] };
3972
- }
3973
- function parseNumericLiteral(source) {
3974
- if (/^-?\d+$/.test(source)) {
3975
- const bigint = BigInt(source);
3976
- if (bigint > BigInt(Number.MAX_SAFE_INTEGER) || bigint < BigInt(Number.MIN_SAFE_INTEGER)) {
3977
- return bigint;
3978
- }
3979
- }
3980
- const number = Number(source);
3981
- if (!Number.isFinite(number)) {
3982
- throw new Error("SQL predicate numeric literals must be finite");
3983
- }
3984
- return number;
3985
- }
3986
- function validateParameterValue(value, name) {
3987
- const probe = { op: "=", args: [{ property: "_" }, value] };
3988
- try {
3989
- validateSQLPredicate(probe);
3990
- } catch {
3991
- throw new Error(`SQL predicate parameter ":${name}" has an unsupported value`);
3992
- }
3993
- }
3994
- function tokenizeSQLPredicate(source) {
3995
- if (typeof source !== "string" || source.trim().length === 0 || source.length > MAXIMUM_SQL_LENGTH) {
3996
- throw new Error("SQL predicate requires a non-empty, safely bounded expression");
3997
- }
3998
- const tokens = [];
3999
- let position = 0;
4000
- while (position < source.length) {
4001
- const character = source[position];
4002
- if (/\s/.test(character)) {
4003
- position++;
4004
- continue;
4005
- }
4006
- if (character === "'") {
4007
- const result = readQuotedToken(source, position, "'", "'");
4008
- tokens.push({ kind: "string", value: result.value });
4009
- position = result.position;
4010
- } else if (character === '"') {
4011
- const result = readQuotedToken(source, position, '"', '"');
4012
- tokens.push({ kind: "identifier", value: result.value, quoted: true });
4013
- position = result.position;
4014
- } else if (character === ":") {
4015
- const match = /^[A-Za-z_][A-Za-z0-9_]*/.exec(source.slice(position + 1));
4016
- if (!match) {
4017
- throw new Error(`SQL predicate contains an invalid parameter at character ${position}`);
4018
- }
4019
- tokens.push({ kind: "parameter", value: match[0] });
4020
- position += match[0].length + 1;
4021
- } else {
4022
- const numberMatch = /^(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/.exec(source.slice(position));
4023
- const identifierMatch = /^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*/.exec(source.slice(position));
4024
- if (numberMatch) {
4025
- tokens.push({ kind: "number", value: numberMatch[0] });
4026
- position += numberMatch[0].length;
4027
- } else if (identifierMatch) {
4028
- tokens.push({ kind: "identifier", value: identifierMatch[0] });
4029
- position += identifierMatch[0].length;
4030
- } else {
4031
- const twoCharacters = source.slice(position, position + 2);
4032
- if (["<=", ">=", "<>", "!="].includes(twoCharacters)) {
4033
- tokens.push({ kind: "symbol", value: twoCharacters });
4034
- position += 2;
4035
- } else if ("(),;-=<>".includes(character)) {
4036
- tokens.push({ kind: "symbol", value: character });
4037
- position++;
4038
- } else {
4039
- throw new Error(`SQL predicate contains an unsupported token at character ${position}`);
4040
- }
4041
- }
4042
- }
4043
- if (tokens.length > MAXIMUM_SQL_TOKENS) {
4044
- throw new Error("SQL predicate exceeds its safe token limit");
4045
- }
4046
- }
4047
- return tokens;
4048
- }
4049
- function readQuotedToken(source, start, quote, escapedQuote) {
4050
- let value = "";
4051
- let position = start + 1;
4052
- while (position < source.length) {
4053
- if (source[position] !== quote) {
4054
- value += source[position++];
4055
- continue;
4056
- }
4057
- if (source[position + 1] === escapedQuote) {
4058
- value += quote;
4059
- position += 2;
4060
- continue;
4061
- }
4062
- return { value, position: position + 1 };
4063
- }
4064
- throw new Error(`SQL predicate contains an unterminated quoted value at character ${start}`);
4065
- }
4066
4436
 
4067
- // dist/bind-sql-predicate.js
4437
+ // ../core/src/scan/bind-sql-predicate.ts
4068
4438
  function bindSQLPredicate(predicate, parameters) {
4069
4439
  validateSQLPredicate(predicate);
4070
4440
  return bindColumnarPredicateParameters(predicate, parameters);