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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/dist/cluster-source.cjs +45 -1
  2. package/dist/cluster-source.cjs.map +1 -1
  3. package/dist/dist.min.js +2 -2
  4. package/dist/federated-table-scan-source.cjs +272 -201
  5. package/dist/federated-table-scan-source.cjs.map +3 -3
  6. package/dist/index.cjs +1 -1
  7. package/dist/lib/init.js +1 -1
  8. package/dist/loader-utils/index.cjs +233 -12
  9. package/dist/loader-utils/index.cjs.map +2 -2
  10. package/dist/loader-utils/index.d.ts +8 -4
  11. package/dist/loader-utils/index.d.ts.map +1 -1
  12. package/dist/loader-utils/index.js +3 -1
  13. package/dist/loader-utils/index.js.map +1 -1
  14. package/dist/loader-utils/json-loader.js +1 -1
  15. package/dist/loader-utils/lib/files/http-file-transport.d.ts +4 -0
  16. package/dist/loader-utils/lib/files/http-file-transport.d.ts.map +1 -1
  17. package/dist/loader-utils/lib/files/http-file-transport.js +45 -1
  18. package/dist/loader-utils/lib/files/http-file-transport.js.map +1 -1
  19. package/dist/loader-utils/lib/files/http-file-types.d.ts +2 -0
  20. package/dist/loader-utils/lib/files/http-file-types.d.ts.map +1 -1
  21. package/dist/loader-utils/lib/files/read-range-response.d.ts +3 -0
  22. package/dist/loader-utils/lib/files/read-range-response.d.ts.map +1 -0
  23. package/dist/loader-utils/lib/files/read-range-response.js +62 -0
  24. package/dist/loader-utils/lib/files/read-range-response.js.map +1 -0
  25. package/dist/loader-utils/lib/log-utils/log.js +1 -1
  26. package/dist/loader-utils/lib/scan-utils/logical-table-query.d.ts +38 -0
  27. package/dist/loader-utils/lib/scan-utils/logical-table-query.d.ts.map +1 -0
  28. package/dist/loader-utils/lib/scan-utils/logical-table-query.js +96 -0
  29. package/dist/loader-utils/lib/scan-utils/logical-table-query.js.map +1 -0
  30. package/dist/loader-utils/lib/scan-utils/{experimental-scan-options.d.ts → query-options.d.ts} +5 -8
  31. package/dist/loader-utils/lib/scan-utils/query-options.d.ts.map +1 -0
  32. package/dist/loader-utils/lib/scan-utils/{experimental-scan-options.js → query-options.js} +1 -1
  33. package/dist/loader-utils/lib/scan-utils/query-options.js.map +1 -0
  34. package/dist/loader-utils/lib/scan-utils/scan-query-metadata.d.ts +12 -17
  35. package/dist/loader-utils/lib/scan-utils/scan-query-metadata.d.ts.map +1 -1
  36. package/dist/loader-utils/lib/scan-utils/scan-query-metadata.js +5 -5
  37. package/dist/loader-utils/lib/scan-utils/scan-query-metadata.js.map +1 -1
  38. package/dist/loader-utils/lib/scan-utils/table-query.d.ts +33 -3
  39. package/dist/loader-utils/lib/scan-utils/table-query.d.ts.map +1 -1
  40. package/dist/loader-utils/lib/scan-utils/table-query.js.map +1 -1
  41. package/dist/loader-utils/lib/scan-utils/table-scan-batch.d.ts +2 -2
  42. package/dist/loader-utils/lib/scan-utils/table-scan-batch.d.ts.map +1 -1
  43. package/dist/loader-utils/lib/scan-utils/table-scan-batch.js +11 -3
  44. package/dist/loader-utils/lib/scan-utils/table-scan-batch.js.map +1 -1
  45. package/dist/loader-utils/lib/sources/data-source.d.ts +4 -0
  46. package/dist/loader-utils/lib/sources/data-source.d.ts.map +1 -1
  47. package/dist/loader-utils/lib/sources/data-source.js +50 -1
  48. package/dist/loader-utils/lib/sources/data-source.js.map +1 -1
  49. package/dist/loader-utils/lib/sources/table-source-collection.d.ts +30 -0
  50. package/dist/loader-utils/lib/sources/table-source-collection.d.ts.map +1 -0
  51. package/dist/loader-utils/lib/sources/table-source-collection.js +5 -0
  52. package/dist/loader-utils/lib/sources/table-source-collection.js.map +1 -0
  53. package/dist/loader-utils/loader-types.d.ts +5 -5
  54. package/dist/loader-utils/loader-types.d.ts.map +1 -1
  55. package/dist/null-loader.js +1 -1
  56. package/dist/null-worker-node.js +1 -1
  57. package/dist/null-worker.js +1 -1
  58. package/dist/scan/arrow-execution/filter-arrow-table.cjs +254 -0
  59. package/dist/scan/arrow-execution/filter-arrow-table.cjs.map +7 -0
  60. package/dist/scan/arrow-execution/filter-arrow-table.d.ts +16 -0
  61. package/dist/scan/arrow-execution/filter-arrow-table.d.ts.map +1 -0
  62. package/dist/scan/arrow-execution/filter-arrow-table.js +222 -0
  63. package/dist/scan/arrow-execution/filter-arrow-table.js.map +1 -0
  64. package/dist/scan/arrow-execution/project-arrow-table.cjs +70 -0
  65. package/dist/scan/arrow-execution/project-arrow-table.cjs.map +7 -0
  66. package/dist/scan/arrow-execution/project-arrow-table.d.ts +8 -0
  67. package/dist/scan/arrow-execution/project-arrow-table.d.ts.map +1 -0
  68. package/dist/scan/arrow-execution/project-arrow-table.js +39 -0
  69. package/dist/scan/arrow-execution/project-arrow-table.js.map +1 -0
  70. package/dist/scan/arrow-execution.cjs +34 -0
  71. package/dist/scan/arrow-execution.cjs.map +7 -0
  72. package/dist/scan/arrow-execution.d.ts +4 -0
  73. package/dist/scan/arrow-execution.d.ts.map +1 -0
  74. package/dist/scan/arrow-execution.js +6 -0
  75. package/dist/scan/arrow-execution.js.map +1 -0
  76. package/dist/scan/arrow-query.cjs.map +1 -1
  77. package/dist/scan/arrow-query.d.ts +1 -1
  78. package/dist/scan/arrow-query.d.ts.map +1 -1
  79. package/dist/scan/catalog-query-source.cjs +122 -0
  80. package/dist/scan/catalog-query-source.cjs.map +7 -0
  81. package/dist/scan/catalog-query-source.d.ts +6 -0
  82. package/dist/scan/catalog-query-source.d.ts.map +1 -0
  83. package/dist/scan/catalog-query-source.js +115 -0
  84. package/dist/scan/catalog-query-source.js.map +1 -0
  85. package/dist/scan/federated-table-scan-source.cjs +4 -4
  86. package/dist/scan/federated-table-scan-source.cjs.map +2 -2
  87. package/dist/scan/federated-table-scan-source.d.ts +1 -1
  88. package/dist/scan/federated-table-scan-source.d.ts.map +1 -1
  89. package/dist/scan/federated-table-scan-source.js +4 -4
  90. package/dist/scan/federated-table-scan-source.js.map +1 -1
  91. package/dist/scan/index.cjs +188 -4
  92. package/dist/scan/index.cjs.map +4 -4
  93. package/dist/scan/index.d.ts +9 -2
  94. package/dist/scan/index.d.ts.map +1 -1
  95. package/dist/scan/index.js +4 -0
  96. package/dist/scan/index.js.map +1 -1
  97. package/dist/scan/parse-sql-predicate.cjs +3 -3
  98. package/dist/scan/parse-sql-predicate.cjs.map +2 -2
  99. package/dist/scan/parse-sql-predicate.d.ts +5 -2
  100. package/dist/scan/parse-sql-predicate.d.ts.map +1 -1
  101. package/dist/scan/parse-sql-predicate.js +3 -10
  102. package/dist/scan/parse-sql-predicate.js.map +1 -1
  103. package/dist/scan/parse-sql-query.cjs +70 -0
  104. package/dist/scan/parse-sql-query.cjs.map +7 -0
  105. package/dist/scan/parse-sql-query.d.ts +5 -0
  106. package/dist/scan/parse-sql-query.d.ts.map +1 -0
  107. package/dist/scan/parse-sql-query.js +49 -0
  108. package/dist/scan/parse-sql-query.js.map +1 -0
  109. package/dist/scan/parse-sql-select.cjs +215 -0
  110. package/dist/scan/parse-sql-select.cjs.map +7 -0
  111. package/dist/scan/parse-sql-select.d.ts +38 -0
  112. package/dist/scan/parse-sql-select.d.ts.map +1 -0
  113. package/dist/scan/parse-sql-select.js +208 -0
  114. package/dist/scan/parse-sql-select.js.map +1 -0
  115. package/dist/scan/query-arrow-table-in-batches.cjs +65 -0
  116. package/dist/scan/query-arrow-table-in-batches.cjs.map +7 -0
  117. package/dist/scan/query-arrow-table-in-batches.d.ts +8 -0
  118. package/dist/scan/query-arrow-table-in-batches.d.ts.map +1 -0
  119. package/dist/scan/query-arrow-table-in-batches.js +44 -0
  120. package/dist/scan/query-arrow-table-in-batches.js.map +1 -0
  121. package/dist/scan/query-arrow-table.cjs +52 -179
  122. package/dist/scan/query-arrow-table.cjs.map +3 -3
  123. package/dist/scan/query-arrow-table.d.ts +4 -37
  124. package/dist/scan/query-arrow-table.d.ts.map +1 -1
  125. package/dist/scan/query-arrow-table.js +37 -188
  126. package/dist/scan/query-arrow-table.js.map +1 -1
  127. package/dist/scan/query.cjs +257 -0
  128. package/dist/scan/query.cjs.map +7 -0
  129. package/dist/scan/query.d.ts +44 -0
  130. package/dist/scan/query.d.ts.map +1 -0
  131. package/dist/scan/query.js +246 -0
  132. package/dist/scan/query.js.map +1 -0
  133. package/dist/scan/resolve-query-source.cjs +59 -0
  134. package/dist/scan/resolve-query-source.cjs.map +7 -0
  135. package/dist/scan/resolve-query-source.d.ts +6 -0
  136. package/dist/scan/resolve-query-source.d.ts.map +1 -0
  137. package/dist/scan/resolve-query-source.js +37 -0
  138. package/dist/scan/resolve-query-source.js.map +1 -0
  139. package/dist/scan/sql-catalog.cjs +110 -0
  140. package/dist/scan/sql-catalog.cjs.map +7 -0
  141. package/dist/scan/sql-catalog.d.ts +47 -0
  142. package/dist/scan/sql-catalog.d.ts.map +1 -0
  143. package/dist/scan/sql-catalog.js +97 -0
  144. package/dist/scan/sql-catalog.js.map +1 -0
  145. package/dist/scan/table-query.cjs.map +2 -2
  146. package/dist/scan/table-query.d.ts +4 -1
  147. package/dist/scan/table-query.d.ts.map +1 -1
  148. package/dist/scan/table-query.js.map +1 -1
  149. package/dist/vector-table-scan-source.cjs +281 -193
  150. package/dist/vector-table-scan-source.cjs.map +3 -3
  151. package/dist/worker-utils/index.cjs +2 -1
  152. package/dist/worker-utils/index.cjs.map +1 -1
  153. package/dist/worker-utils/lib/env-utils/version.js +1 -1
  154. package/dist/worker-utils/lib/library-utils/library-utils.js +1 -1
  155. package/dist/worker-utils/lib/library-utils/library-utils.js.map +1 -1
  156. package/package.json +17 -7
  157. package/src/loader-utils/index.ts +30 -6
  158. package/src/loader-utils/lib/files/http-file-transport.ts +65 -1
  159. package/src/loader-utils/lib/files/http-file-types.ts +2 -0
  160. package/src/loader-utils/lib/files/read-range-response.ts +66 -0
  161. package/src/loader-utils/lib/scan-utils/logical-table-query.ts +164 -0
  162. package/src/loader-utils/lib/scan-utils/query-options.ts +44 -0
  163. package/src/loader-utils/lib/scan-utils/scan-query-metadata.ts +17 -26
  164. package/src/loader-utils/lib/scan-utils/table-query.ts +49 -8
  165. package/src/loader-utils/lib/scan-utils/table-scan-batch.ts +25 -9
  166. package/src/loader-utils/lib/sources/data-source.ts +54 -1
  167. package/src/loader-utils/lib/sources/table-source-collection.ts +38 -0
  168. package/src/loader-utils/loader-types.ts +5 -5
  169. package/src/scan/arrow-execution/filter-arrow-table.ts +293 -0
  170. package/src/scan/arrow-execution/project-arrow-table.ts +53 -0
  171. package/src/scan/arrow-execution.ts +7 -0
  172. package/src/scan/arrow-query.ts +3 -0
  173. package/src/scan/catalog-query-source.ts +132 -0
  174. package/src/scan/federated-table-scan-source.ts +19 -19
  175. package/src/scan/index.ts +28 -4
  176. package/src/scan/parse-sql-predicate.ts +9 -3
  177. package/src/scan/parse-sql-query.ts +53 -0
  178. package/src/scan/parse-sql-select.ts +220 -0
  179. package/src/scan/query-arrow-table-in-batches.ts +58 -0
  180. package/src/scan/query-arrow-table.ts +60 -237
  181. package/src/scan/query.ts +339 -0
  182. package/src/scan/resolve-query-source.ts +47 -0
  183. package/src/scan/sql-catalog.ts +167 -0
  184. package/src/scan/table-query.ts +6 -1
  185. package/src/worker-utils/lib/library-utils/library-utils.ts +1 -1
  186. package/dist/loader-utils/lib/scan-utils/experimental-scan-options.d.ts.map +0 -1
  187. package/dist/loader-utils/lib/scan-utils/experimental-scan-options.js.map +0 -1
  188. package/src/loader-utils/lib/scan-utils/experimental-scan-options.ts +0 -46
@@ -8,7 +8,7 @@ import {DataType, FetchLike, TransformBatches} from './types';
8
8
  import {ReadableFile} from './lib/files/file';
9
9
  import type {CoreAPI} from './lib/sources/data-source';
10
10
  import type {Credential, AuthenticationConstructor} from './lib/request-utils/authentication';
11
- import type {ExperimentalScanOptions} from './lib/scan-utils/experimental-scan-options';
11
+ import type {QueryOptions} from './lib/scan-utils/query-options';
12
12
 
13
13
  /** Query parameters appended to URL-backed loader requests. */
14
14
  export type SearchParams = Record<string, string | number | boolean>;
@@ -19,8 +19,8 @@ export type SearchParams = Record<string, string | number | boolean>;
19
19
  * Core Loader Options
20
20
  */
21
21
  export type StrictLoaderOptions = Pick<WorkerOptions, 'signal' | 'onProgress'> & {
22
- /** Experimental common scan request. Format loaders implement the fields they support. */
23
- _scan?: ExperimentalScanOptions;
22
+ /** Common query request. Format loaders implement the fields they support. */
23
+ _scan?: QueryOptions;
24
24
  /** Query parameters appended to the initial URL request. */
25
25
  searchParams?: SearchParams;
26
26
  core?: {
@@ -110,8 +110,8 @@ export type StrictLoaderOptions = Pick<WorkerOptions, 'signal' | 'onProgress'> &
110
110
  * Core Loader Options
111
111
  */
112
112
  export type LoaderOptions = Pick<WorkerOptions, 'signal' | 'onProgress'> & {
113
- /** Experimental common scan request. Format loaders implement the fields they support. */
114
- _scan?: ExperimentalScanOptions;
113
+ /** Common query request. Format loaders implement the fields they support. */
114
+ _scan?: QueryOptions;
115
115
  /** Query parameters appended to the initial URL request. */
116
116
  searchParams?: SearchParams;
117
117
  core?: StrictLoaderOptions['core'];
@@ -0,0 +1,293 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import * as arrow from 'apache-arrow';
6
+ import {
7
+ getColumnarPredicateColumns,
8
+ validateColumnarPredicate,
9
+ isColumnarPredicateParameter as isSQLPredicateParameter,
10
+ type ColumnarPredicate,
11
+ type ColumnarPredicateInputValue as SQLPredicateValue,
12
+ type ColumnarComparisonPredicate,
13
+ type ColumnarLogicalPredicate,
14
+ type ColumnarNotPredicate,
15
+ type ColumnarNullPredicate,
16
+ type ColumnarInPredicate
17
+ } from '@loaders.gl/core/loader-utils';
18
+
19
+ type SQLPredicate = ColumnarPredicate<SQLPredicateValue>;
20
+ type SQLComparisonPredicate = ColumnarComparisonPredicate<SQLPredicateValue>;
21
+ type SQLLogicalPredicate = ColumnarLogicalPredicate<SQLPredicateValue>;
22
+ type SQLNotPredicate = ColumnarNotPredicate<SQLPredicateValue>;
23
+ type SQLNullPredicate = ColumnarNullPredicate;
24
+ type SQLInPredicate = ColumnarInPredicate<SQLPredicateValue>;
25
+ type SQLTruthValue = boolean | null;
26
+
27
+ /** Controls residual filtering of one Arrow input. */
28
+ export type ArrowFilterOptions = Readonly<{
29
+ /** Stops after retaining this many rows, in source order. */
30
+ limit?: number;
31
+ /** Cooperatively cancels predicate evaluation. */
32
+ signal?: AbortSignal;
33
+ }>;
34
+
35
+ /** Filters record batches independently while preserving source schema and batch metadata. */
36
+ export function filterArrowTable(
37
+ source: arrow.Table,
38
+ predicate: ColumnarPredicate<unknown>,
39
+ options: ArrowFilterOptions = {}
40
+ ): arrow.Table {
41
+ validateColumnarPredicate(predicate, new Set(source.schema.fields.map(field => field.name)));
42
+ let remaining = options.limit ?? Infinity;
43
+ if (remaining !== Infinity && (!Number.isSafeInteger(remaining) || remaining < 0)) {
44
+ throw new Error('Arrow filter limit must be a non-negative safe integer.');
45
+ }
46
+ options.signal?.throwIfAborted();
47
+ const batches: arrow.RecordBatch[] = [];
48
+ for (const batch of source.batches) {
49
+ if (remaining === 0) break;
50
+ options.signal?.throwIfAborted();
51
+ const data = new arrow.Table(source.schema, [batch]);
52
+ const columns = getPredicateColumns(data, getColumnarPredicateColumns(predicate));
53
+ const indices: number[] = [];
54
+ for (let rowIndex = 0; rowIndex < batch.numRows && indices.length < remaining; rowIndex++) {
55
+ options.signal?.throwIfAborted();
56
+ if (evaluatePredicate(predicate as SQLPredicate, columns, rowIndex) === true)
57
+ indices.push(rowIndex);
58
+ }
59
+ remaining -= indices.length;
60
+ if (indices.length === 0) continue;
61
+ if (indices.length === batch.numRows) {
62
+ batches.push(batch);
63
+ continue;
64
+ }
65
+ if (indices[indices.length - 1] - indices[0] + 1 === indices.length) {
66
+ batches.push(batch.slice(indices[0], indices[indices.length - 1] + 1));
67
+ continue;
68
+ }
69
+ const vectors = Object.fromEntries(
70
+ source.schema.fields.map(field => {
71
+ const vector = data.getChild(field.name)!;
72
+ return [
73
+ field.name,
74
+ arrow.vectorFromArray(
75
+ indices.map(index => vector.get(index)),
76
+ field.type
77
+ )
78
+ ];
79
+ })
80
+ );
81
+ const gathered = new arrow.Table(source.schema, vectors);
82
+ batches.push(
83
+ ...gathered.batches.map(
84
+ output => new arrow.RecordBatch(source.schema, output.data, batch.metadata)
85
+ )
86
+ );
87
+ }
88
+ return new arrow.Table(source.schema, batches);
89
+ }
90
+
91
+ /** Creates a predicate-column lookup so values are not resolved for every source row. */
92
+ function getPredicateColumns(
93
+ sourceTable: arrow.Table,
94
+ columnNames: readonly string[]
95
+ ): Readonly<Record<string, arrow.Vector>> {
96
+ const columns: Record<string, arrow.Vector> = {};
97
+ for (const columnName of columnNames) {
98
+ const column = sourceTable.getChild(columnName);
99
+ if (!column) {
100
+ throw new Error(`Arrow query column not found: ${columnName}`);
101
+ }
102
+ columns[columnName] = column;
103
+ }
104
+ return columns;
105
+ }
106
+
107
+ /** Evaluates a predicate against one Arrow table row using SQL three-valued Boolean logic. */
108
+ function evaluatePredicate(
109
+ predicate: SQLPredicate,
110
+ columns: Readonly<Record<string, arrow.Vector>>,
111
+ rowIndex: number
112
+ ): SQLTruthValue {
113
+ if (isLogicalPredicate(predicate)) {
114
+ const values = predicate.args.map(child => evaluatePredicate(child, columns, rowIndex));
115
+ return predicate.op === 'and' ? evaluateAnd(values) : evaluateOr(values);
116
+ }
117
+ if (isNotPredicate(predicate)) {
118
+ const value = evaluatePredicate(predicate.args[0], columns, rowIndex);
119
+ return value === null ? null : !value;
120
+ }
121
+
122
+ const value = getColumnValue(columns, predicate.args[0].property, rowIndex);
123
+ if (isNullPredicate(predicate)) {
124
+ return value === null || value === undefined;
125
+ }
126
+ if (isInPredicate(predicate)) {
127
+ return evaluateIn(value, predicate.args[1]);
128
+ }
129
+ return evaluateComparison(value, predicate);
130
+ }
131
+
132
+ /** Returns the value of one predicate column or raises an internal invariant error. */
133
+ function getColumnValue(
134
+ columns: Readonly<Record<string, arrow.Vector>>,
135
+ columnName: string | readonly string[],
136
+ rowIndex: number
137
+ ): unknown {
138
+ const path = typeof columnName === 'string' ? [columnName] : columnName;
139
+ const column = columns[path[0]];
140
+ if (!column) {
141
+ throw new Error(`Arrow query could not read predicate column "${columnName}".`);
142
+ }
143
+ let value: unknown = column.get(rowIndex);
144
+ for (const property of path.slice(1)) {
145
+ if (value === null || value === undefined) return null;
146
+ value = (value as Record<string, unknown>)[property];
147
+ }
148
+ return value;
149
+ }
150
+
151
+ /** Evaluates SQL AND with null propagation. */
152
+ function evaluateAnd(values: readonly SQLTruthValue[]): SQLTruthValue {
153
+ if (values.includes(false)) {
154
+ return false;
155
+ }
156
+ return values.includes(null) ? null : true;
157
+ }
158
+
159
+ /** Evaluates SQL OR with null propagation. */
160
+ function evaluateOr(values: readonly SQLTruthValue[]): SQLTruthValue {
161
+ if (values.includes(true)) {
162
+ return true;
163
+ }
164
+ return values.includes(null) ? null : false;
165
+ }
166
+
167
+ /** Evaluates a membership expression with SQL null propagation. */
168
+ function evaluateIn(value: unknown, candidates: readonly SQLPredicateValue[]): SQLTruthValue {
169
+ if (value === null || value === undefined) {
170
+ return null;
171
+ }
172
+ for (const candidate of candidates) {
173
+ if (compareValues(value, candidate) === 0) {
174
+ return true;
175
+ }
176
+ }
177
+ return false;
178
+ }
179
+
180
+ /** Evaluates a binary SQL comparison with SQL null propagation. */
181
+ function evaluateComparison(value: unknown, predicate: SQLComparisonPredicate): SQLTruthValue {
182
+ if (value === null || value === undefined) {
183
+ return null;
184
+ }
185
+ const comparison = compareValues(value, predicate.args[1]);
186
+ switch (predicate.op) {
187
+ case '=':
188
+ return comparison === 0;
189
+ case '<>':
190
+ return comparison !== 0;
191
+ case '<':
192
+ return comparison < 0;
193
+ case '<=':
194
+ return comparison <= 0;
195
+ case '>':
196
+ return comparison > 0;
197
+ case '>=':
198
+ return comparison >= 0;
199
+ }
200
+ }
201
+
202
+ /** Compares supported predicate values and rejects comparisons without an unambiguous ordering. */
203
+ function compareValues(left: unknown, right: SQLPredicateValue): number {
204
+ if (isSQLPredicateParameter(right)) {
205
+ throw new Error(
206
+ `Arrow query parameter ":${right.parameter}" must be bound before query execution.`
207
+ );
208
+ }
209
+ if (left instanceof Date && right instanceof Date) {
210
+ return compareArrowQueryNumbers(left.getTime(), right.getTime());
211
+ }
212
+ if (typeof left === 'number' && right instanceof Date) {
213
+ return compareArrowQueryNumbers(left, right.getTime());
214
+ }
215
+ if (left instanceof Uint8Array && right instanceof Uint8Array) {
216
+ return compareBinary(left, right);
217
+ }
218
+ if (typeof left === 'bigint' && typeof right === 'bigint') {
219
+ return left === right ? 0 : left < right ? -1 : 1;
220
+ }
221
+ if (
222
+ (typeof left === 'bigint' && typeof right === 'number') ||
223
+ (typeof left === 'number' && typeof right === 'bigint')
224
+ ) {
225
+ const numericValue = typeof left === 'number' ? left : (right as number);
226
+ if (!Number.isFinite(numericValue))
227
+ throw new Error('Arrow query cannot compare non-finite numbers.');
228
+ // Native relational comparison preserves bigint precision and fractional number bounds.
229
+ return left < right ? -1 : left > right ? 1 : 0;
230
+ }
231
+ if (typeof left === 'number' && typeof right === 'number') {
232
+ return compareArrowQueryNumbers(left, right);
233
+ }
234
+ if (typeof left === 'string' && typeof right === 'string') {
235
+ return left === right ? 0 : left < right ? -1 : 1;
236
+ }
237
+ if (typeof left === 'boolean' && typeof right === 'boolean') {
238
+ return Number(left) - Number(right);
239
+ }
240
+ throw new Error(
241
+ `Arrow query cannot compare ${getArrowQueryValueType(left)} with ${getArrowQueryValueType(right)} values.`
242
+ );
243
+ }
244
+
245
+ /** Compares two finite number values. */
246
+ export function compareArrowQueryNumbers(left: number, right: number): number {
247
+ if (!Number.isFinite(left) || !Number.isFinite(right)) {
248
+ throw new Error('Arrow query cannot compare non-finite numbers.');
249
+ }
250
+ return left === right ? 0 : left < right ? -1 : 1;
251
+ }
252
+
253
+ /** Compares binary values lexicographically. */
254
+ function compareBinary(left: Uint8Array, right: Uint8Array): number {
255
+ const commonLength = Math.min(left.length, right.length);
256
+ for (let index = 0; index < commonLength; index++) {
257
+ if (left[index] !== right[index]) {
258
+ return left[index] < right[index] ? -1 : 1;
259
+ }
260
+ }
261
+ return left.length === right.length ? 0 : left.length < right.length ? -1 : 1;
262
+ }
263
+
264
+ /** Returns a human-readable type name for predicate diagnostics. */
265
+ export function getArrowQueryValueType(value: unknown): string {
266
+ if (value instanceof Date) {
267
+ return 'Date';
268
+ }
269
+ if (value instanceof Uint8Array) {
270
+ return 'Uint8Array';
271
+ }
272
+ return typeof value;
273
+ }
274
+
275
+ /** Returns whether the predicate combines two or more child predicates. */
276
+ function isLogicalPredicate(predicate: SQLPredicate): predicate is SQLLogicalPredicate {
277
+ return predicate.op === 'and' || predicate.op === 'or';
278
+ }
279
+
280
+ /** Returns whether the predicate negates one child predicate. */
281
+ function isNotPredicate(predicate: SQLPredicate): predicate is SQLNotPredicate {
282
+ return predicate.op === 'not';
283
+ }
284
+
285
+ /** Returns whether the predicate tests a column for null values. */
286
+ function isNullPredicate(predicate: SQLPredicate): predicate is SQLNullPredicate {
287
+ return predicate.op === 'isNull';
288
+ }
289
+
290
+ /** Returns whether the predicate tests a column against several values. */
291
+ function isInPredicate(predicate: SQLPredicate): predicate is SQLInPredicate {
292
+ return predicate.op === 'in';
293
+ }
@@ -0,0 +1,53 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import * as arrow from 'apache-arrow';
6
+ import type {TableSelection} from '@loaders.gl/core/loader-utils';
7
+
8
+ /**
9
+ * Projects and renames Arrow fields without copying column data buffers.
10
+ * New schema and batch wrappers preserve source row order, batch boundaries, and metadata.
11
+ */
12
+ export function projectArrowTable(
13
+ source: arrow.Table,
14
+ selections: readonly TableSelection[]
15
+ ): arrow.Table {
16
+ const outputNames = new Set<string>();
17
+ const indices: number[] = [];
18
+ const fields = selections.map(selection => {
19
+ const column = typeof selection === 'string' ? selection : selection.column;
20
+ const name =
21
+ typeof selection === 'string'
22
+ ? selection
23
+ : selection.as === undefined
24
+ ? column
25
+ : selection.as;
26
+ const index = source.schema.fields.findIndex(field => field.name === column);
27
+ if (index < 0) throw new Error(`Arrow projection column not found: ${column}`);
28
+ if (
29
+ typeof name !== 'string' ||
30
+ (typeof selection !== 'string' && selection.as !== undefined && !name.length) ||
31
+ outputNames.has(name)
32
+ ) {
33
+ throw new Error(`Invalid or duplicate Arrow projection output name: ${name}`);
34
+ }
35
+ outputNames.add(name);
36
+ indices.push(index);
37
+ return source.schema.fields[index].clone({name});
38
+ });
39
+ const schema = new arrow.Schema(fields, source.schema.metadata);
40
+ const batches = source.batches.map(
41
+ batch =>
42
+ new arrow.RecordBatch(
43
+ schema,
44
+ arrow.makeData({
45
+ type: new arrow.Struct(fields),
46
+ length: batch.numRows,
47
+ children: indices.map(index => batch.data.children[index])
48
+ }),
49
+ batch.metadata
50
+ )
51
+ );
52
+ return new arrow.Table(schema, batches);
53
+ }
@@ -0,0 +1,7 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ export {projectArrowTable} from './arrow-execution/project-arrow-table';
6
+ export {filterArrowTable} from './arrow-execution/filter-arrow-table';
7
+ export type {ArrowFilterOptions} from './arrow-execution/filter-arrow-table';
@@ -8,6 +8,9 @@ export {getSQLPredicateColumnNames, planTableQuery} from './table-query';
8
8
  export type {
9
9
  TableQueryFilterStep,
10
10
  TableQueryLimitStep,
11
+ TableQuery,
12
+ TableSelection,
13
+ TableQueryExecutionOptions,
11
14
  TableQueryOptions,
12
15
  TableQueryPlan,
13
16
  TableQueryPlanStep,
@@ -0,0 +1,132 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {Schema, ObjectRowTableBatch} from '@loaders.gl/core/schema';
6
+ import type {
7
+ DataSource,
8
+ DataSourceOptions,
9
+ TableReference,
10
+ TableScanSource
11
+ } from '@loaders.gl/core/loader-utils';
12
+ import {createScanQueryMetadata} from '@loaders.gl/core/loader-utils';
13
+ import {convertSchemaToArrow} from '@loaders.gl/core/schema-utils';
14
+ import {createSQLCatalog, type SQLCatalogQuery} from './sql-catalog';
15
+
16
+ /** Turns a logical metadata command into an ordinary schema-bearing table source. */
17
+ export function createCatalogQuerySource(
18
+ request: SQLCatalogQuery,
19
+ sources: readonly DataSource<unknown, DataSourceOptions>[] | undefined
20
+ ): TableScanSource<ObjectRowTableBatch> {
21
+ const allowed = request.command === 'showSources' ? ['command'] : ['command', 'from'];
22
+ if (
23
+ !['showSources', 'showTables', 'describe'].includes(request.command) ||
24
+ Object.keys(request).some(key => !allowed.includes(key))
25
+ )
26
+ throw new Error('Unsupported catalog query.');
27
+ if (
28
+ request.command === 'showTables' &&
29
+ request.from !== undefined &&
30
+ (typeof request.from !== 'string' || !request.from.trim())
31
+ )
32
+ throw new Error('SHOW TABLES requires a non-empty source name.');
33
+ if (request.command === 'describe' && request.from === undefined)
34
+ throw new Error('DESCRIBE requires a table reference.');
35
+ const catalog = createSQLCatalog(sources!);
36
+ const fields =
37
+ request.command === 'showSources'
38
+ ? ['source_name']
39
+ : request.command === 'showTables'
40
+ ? ['source_name', 'table_name', 'table_reference', 'queryable']
41
+ : [
42
+ 'source_name',
43
+ 'table_name',
44
+ 'column_name',
45
+ 'ordinal_position',
46
+ 'data_type',
47
+ 'is_nullable',
48
+ 'metadata'
49
+ ];
50
+ const schema: Schema = {
51
+ fields: fields.map(name => ({
52
+ name,
53
+ type:
54
+ name === 'ordinal_position'
55
+ ? 'int32'
56
+ : name === 'queryable' || name === 'is_nullable'
57
+ ? 'bool'
58
+ : 'utf8',
59
+ nullable: false
60
+ })),
61
+ metadata: {catalog: request.command}
62
+ };
63
+ return {
64
+ async getQueryMetadata(options = {}) {
65
+ options.signal?.throwIfAborted();
66
+ return createScanQueryMetadata({
67
+ sourceType: 'catalog',
68
+ queryType: 'table',
69
+ schema,
70
+ execution: {status: 'supported', method: 'read'},
71
+ capabilities: {
72
+ table: {
73
+ projection: 'unsupported',
74
+ predicate: 'unsupported',
75
+ limit: 'unsupported',
76
+ streaming: true,
77
+ cancellation: true
78
+ }
79
+ }
80
+ });
81
+ },
82
+ async *read(options = {}) {
83
+ const metadataOptions = {signal: options.signal};
84
+ options.signal?.throwIfAborted();
85
+ if (request.command === 'showSources') {
86
+ const data = catalog.getSources().map(source => ({source_name: source.name}));
87
+ if (data.length)
88
+ yield {shape: 'object-row-table', schema, data, length: data.length, batchType: 'data'};
89
+ } else if (request.command === 'showTables') {
90
+ const names =
91
+ request.from === undefined
92
+ ? catalog.getSources().map(source => source.name)
93
+ : [request.from];
94
+ for (const name of names) {
95
+ const tables = await catalog.getTables(name, metadataOptions);
96
+ const data = tables.map(table => ({
97
+ source_name: table.sourceName,
98
+ table_name: table.name,
99
+ table_reference: formatReference(table.reference),
100
+ queryable: table.queryable
101
+ }));
102
+ if (data.length)
103
+ yield {shape: 'object-row-table', schema, data, length: data.length, batchType: 'data'};
104
+ }
105
+ } else {
106
+ const tableSchema = await catalog.getSchema(request.from, metadataOptions);
107
+ const arrowSchema = convertSchemaToArrow(tableSchema);
108
+ const sourceName = typeof request.from === 'string' ? request.from : request.from.source;
109
+ const tableName = typeof request.from === 'string' ? request.from : request.from.table;
110
+ const data = tableSchema.fields.map((field, index) => ({
111
+ source_name: sourceName,
112
+ table_name: tableName,
113
+ column_name: field.name,
114
+ ordinal_position: index + 1,
115
+ data_type: arrowSchema.fields[index].type.toString(),
116
+ is_nullable: arrowSchema.fields[index].nullable,
117
+ metadata: JSON.stringify(field.metadata || {})
118
+ }));
119
+ if (data.length)
120
+ yield {shape: 'object-row-table', schema, data, length: data.length, batchType: 'data'};
121
+ }
122
+ }
123
+ };
124
+ }
125
+
126
+ /** Renders actual references as safely quoted SQL for schema explorers. */
127
+ function formatReference(reference: TableReference): string {
128
+ const quote = (name: string) => `"${name.replaceAll('"', '""')}"`;
129
+ return typeof reference === 'string'
130
+ ? quote(reference)
131
+ : `${quote(reference.source)}.${quote(reference.table)}`;
132
+ }
@@ -5,7 +5,7 @@
5
5
  import * as arrow from 'apache-arrow';
6
6
  import {
7
7
  createScanQueryMetadata,
8
- emitScanExecutionTelemetry,
8
+ emitQueryComplete,
9
9
  explainTableQuery,
10
10
  getColumnarPredicateColumns,
11
11
  planTableQuery,
@@ -13,9 +13,9 @@ import {
13
13
  type ManageableDataSource,
14
14
  type ScanQueryMetadata,
15
15
  type ScanQueryMetadataOptions,
16
- type ScanExecutionTelemetry,
17
- type ScanExecutionTelemetryStatus,
18
- type ScanSourceExecutionTelemetry,
16
+ type TableQueryExecutionSummary,
17
+ type TableQueryExecutionStatus,
18
+ type TableQuerySourceExecutionSummary,
19
19
  type TableQueryCapabilities,
20
20
  type TableQueryExplain,
21
21
  type TableScanReadOptions,
@@ -45,7 +45,7 @@ export type FederatedTableSourceEntry = Readonly<{
45
45
  /** Stable id resolved through the shared {@link DataSourceManager}. */
46
46
  dataSourceId: string;
47
47
  /** Source-local predicate, projection, and limit applied before schema reconciliation. */
48
- query?: Omit<TableScanReadOptions<SQLPredicate>, 'signal' | 'onTelemetry'>;
48
+ query?: Omit<TableScanReadOptions<SQLPredicate>, 'signal' | 'onQueryComplete'>;
49
49
  /** Source-column to federated-column renames applied before compatibility checks. */
50
50
  columnMapping?: Readonly<Record<string, string>>;
51
51
  }>;
@@ -145,8 +145,8 @@ type FederatedTablePlan = Readonly<{
145
145
  sources: readonly ResolvedFederatedSource[];
146
146
  }>;
147
147
 
148
- type MutableScanExecutionTelemetry = {
149
- status: ScanExecutionTelemetryStatus;
148
+ type MutableTableQueryExecutionSummary = {
149
+ status: TableQueryExecutionStatus;
150
150
  sourcesPlanned: number;
151
151
  sourcesRead: number;
152
152
  batchesRead: number;
@@ -159,8 +159,8 @@ type MutableScanExecutionTelemetry = {
159
159
  tasksOpened: number;
160
160
  rowsPruned: number;
161
161
  durationMilliseconds: number;
162
- earlyTerminationReason?: ScanExecutionTelemetry['earlyTerminationReason'];
163
- sources: ScanSourceExecutionTelemetry[];
162
+ earlyTerminationReason?: TableQueryExecutionSummary['earlyTerminationReason'];
163
+ sources: TableQuerySourceExecutionSummary[];
164
164
  error?: unknown;
165
165
  };
166
166
 
@@ -269,7 +269,7 @@ export class FederatedTableScanSource
269
269
  ): AsyncIterableIterator<FederatedTableBatch> {
270
270
  const consumerId = this.createConsumerId('read');
271
271
  const startedAt = Date.now();
272
- const telemetry: MutableScanExecutionTelemetry = {
272
+ const telemetry: MutableTableQueryExecutionSummary = {
273
273
  status: 'early-terminated',
274
274
  sourcesPlanned: this.sources.length,
275
275
  sourcesRead: 0,
@@ -304,7 +304,7 @@ export class FederatedTableScanSource
304
304
  return;
305
305
  }
306
306
  const sourceStartedAt = Date.now();
307
- let childTelemetry: ScanExecutionTelemetry | undefined;
307
+ let childTelemetry: TableQueryExecutionSummary | undefined;
308
308
  let sourceCompleted = false;
309
309
  let sourceError: unknown;
310
310
  let sourceBatchesDecoded = 0;
@@ -429,13 +429,13 @@ export class FederatedTableScanSource
429
429
  if (telemetry.status === 'early-terminated' && !telemetry.earlyTerminationReason) {
430
430
  telemetry.earlyTerminationReason = 'consumer-return';
431
431
  }
432
- emitScanExecutionTelemetry(
433
- options.onTelemetry,
432
+ emitQueryComplete(
433
+ options.onQueryComplete,
434
434
  Object.freeze({
435
435
  ...telemetry,
436
436
  batchesDecoded: telemetry.batchesRead,
437
437
  sources: Object.freeze([...telemetry.sources])
438
- }) satisfies ScanExecutionTelemetry
438
+ }) satisfies TableQueryExecutionSummary
439
439
  );
440
440
  }
441
441
  }
@@ -661,7 +661,7 @@ function createSourceReadOptions(
661
661
  requiredOutputColumns: readonly string[],
662
662
  remaining: number,
663
663
  options: TableScanReadOptions<SQLPredicate>,
664
- onTelemetry: (telemetry: ScanExecutionTelemetry) => void
664
+ onQueryComplete: (telemetry: TableQueryExecutionSummary) => void
665
665
  ): TableScanReadOptions<SQLPredicate> {
666
666
  const sourceResidualPredicate = getSourceResidualPredicate(source);
667
667
  const requiredSourceColumns = requiredOutputColumns
@@ -684,7 +684,7 @@ function createSourceReadOptions(
684
684
  predicate: sourceResidualPredicate ? undefined : source.entry.query?.predicate,
685
685
  limit: sourceResidualPredicate ? undefined : Number.isFinite(limit) ? limit : undefined,
686
686
  signal: options.signal,
687
- onTelemetry
687
+ onQueryComplete
688
688
  };
689
689
  }
690
690
 
@@ -731,7 +731,7 @@ function createCanonicalArrowTable(
731
731
  function createSourceTelemetry(
732
732
  options: Readonly<{
733
733
  resolvedSource: ResolvedFederatedSource;
734
- childTelemetry?: ScanExecutionTelemetry;
734
+ childTelemetry?: TableQueryExecutionSummary;
735
735
  sourceStartedAt: number;
736
736
  sourceCompleted: boolean;
737
737
  sourceError?: unknown;
@@ -742,9 +742,9 @@ function createSourceTelemetry(
742
742
  rowsRetained: number;
743
743
  rowsReturned: number;
744
744
  }>
745
- ): ScanSourceExecutionTelemetry {
745
+ ): TableQuerySourceExecutionSummary {
746
746
  const {resolvedSource, childTelemetry} = options;
747
- const status: ScanExecutionTelemetryStatus =
747
+ const status: TableQueryExecutionStatus =
748
748
  childTelemetry?.status ||
749
749
  (options.signal?.aborted
750
750
  ? 'cancelled'