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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) 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 +279 -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/expressions.cjs +110 -0
  59. package/dist/scan/arrow-execution/expressions.cjs.map +7 -0
  60. package/dist/scan/arrow-execution/expressions.d.ts +9 -0
  61. package/dist/scan/arrow-execution/expressions.d.ts.map +1 -0
  62. package/dist/scan/arrow-execution/expressions.js +60 -0
  63. package/dist/scan/arrow-execution/expressions.js.map +1 -0
  64. package/dist/scan/arrow-execution/filter-arrow-table.cjs +266 -0
  65. package/dist/scan/arrow-execution/filter-arrow-table.cjs.map +7 -0
  66. package/dist/scan/arrow-execution/filter-arrow-table.d.ts +18 -0
  67. package/dist/scan/arrow-execution/filter-arrow-table.d.ts.map +1 -0
  68. package/dist/scan/arrow-execution/filter-arrow-table.js +230 -0
  69. package/dist/scan/arrow-execution/filter-arrow-table.js.map +1 -0
  70. package/dist/scan/arrow-execution/project-arrow-table.cjs +70 -0
  71. package/dist/scan/arrow-execution/project-arrow-table.cjs.map +7 -0
  72. package/dist/scan/arrow-execution/project-arrow-table.d.ts +8 -0
  73. package/dist/scan/arrow-execution/project-arrow-table.d.ts.map +1 -0
  74. package/dist/scan/arrow-execution/project-arrow-table.js +39 -0
  75. package/dist/scan/arrow-execution/project-arrow-table.js.map +1 -0
  76. package/dist/scan/arrow-execution.cjs +34 -0
  77. package/dist/scan/arrow-execution.cjs.map +7 -0
  78. package/dist/scan/arrow-execution.d.ts +4 -0
  79. package/dist/scan/arrow-execution.d.ts.map +1 -0
  80. package/dist/scan/arrow-execution.js +6 -0
  81. package/dist/scan/arrow-execution.js.map +1 -0
  82. package/dist/scan/arrow-query.cjs.map +1 -1
  83. package/dist/scan/arrow-query.d.ts +1 -1
  84. package/dist/scan/arrow-query.d.ts.map +1 -1
  85. package/dist/scan/catalog-query-source.cjs +122 -0
  86. package/dist/scan/catalog-query-source.cjs.map +7 -0
  87. package/dist/scan/catalog-query-source.d.ts +6 -0
  88. package/dist/scan/catalog-query-source.d.ts.map +1 -0
  89. package/dist/scan/catalog-query-source.js +115 -0
  90. package/dist/scan/catalog-query-source.js.map +1 -0
  91. package/dist/scan/federated-table-scan-source.cjs +4 -4
  92. package/dist/scan/federated-table-scan-source.cjs.map +2 -2
  93. package/dist/scan/federated-table-scan-source.d.ts +1 -1
  94. package/dist/scan/federated-table-scan-source.d.ts.map +1 -1
  95. package/dist/scan/federated-table-scan-source.js +4 -4
  96. package/dist/scan/federated-table-scan-source.js.map +1 -1
  97. package/dist/scan/index.cjs +188 -4
  98. package/dist/scan/index.cjs.map +4 -4
  99. package/dist/scan/index.d.ts +9 -2
  100. package/dist/scan/index.d.ts.map +1 -1
  101. package/dist/scan/index.js +4 -0
  102. package/dist/scan/index.js.map +1 -1
  103. package/dist/scan/parse-sql-predicate.cjs +3 -294
  104. package/dist/scan/parse-sql-predicate.cjs.map +3 -3
  105. package/dist/scan/parse-sql-predicate.d.ts +6 -9
  106. package/dist/scan/parse-sql-predicate.d.ts.map +1 -1
  107. package/dist/scan/parse-sql-predicate.js +5 -314
  108. package/dist/scan/parse-sql-predicate.js.map +1 -1
  109. package/dist/scan/query-arrow-table-in-batches.cjs +65 -0
  110. package/dist/scan/query-arrow-table-in-batches.cjs.map +7 -0
  111. package/dist/scan/query-arrow-table-in-batches.d.ts +8 -0
  112. package/dist/scan/query-arrow-table-in-batches.d.ts.map +1 -0
  113. package/dist/scan/query-arrow-table-in-batches.js +44 -0
  114. package/dist/scan/query-arrow-table-in-batches.js.map +1 -0
  115. package/dist/scan/query-arrow-table.cjs +52 -179
  116. package/dist/scan/query-arrow-table.cjs.map +3 -3
  117. package/dist/scan/query-arrow-table.d.ts +4 -37
  118. package/dist/scan/query-arrow-table.d.ts.map +1 -1
  119. package/dist/scan/query-arrow-table.js +37 -188
  120. package/dist/scan/query-arrow-table.js.map +1 -1
  121. package/dist/scan/query.cjs +344 -0
  122. package/dist/scan/query.cjs.map +7 -0
  123. package/dist/scan/query.d.ts +42 -0
  124. package/dist/scan/query.d.ts.map +1 -0
  125. package/dist/scan/query.js +367 -0
  126. package/dist/scan/query.js.map +1 -0
  127. package/dist/scan/resolve-query-source.cjs +59 -0
  128. package/dist/scan/resolve-query-source.cjs.map +7 -0
  129. package/dist/scan/resolve-query-source.d.ts +6 -0
  130. package/dist/scan/resolve-query-source.d.ts.map +1 -0
  131. package/dist/scan/resolve-query-source.js +37 -0
  132. package/dist/scan/resolve-query-source.js.map +1 -0
  133. package/dist/scan/sql-catalog.cjs +110 -0
  134. package/dist/scan/sql-catalog.cjs.map +7 -0
  135. package/dist/scan/sql-catalog.d.ts +47 -0
  136. package/dist/scan/sql-catalog.d.ts.map +1 -0
  137. package/dist/scan/sql-catalog.js +97 -0
  138. package/dist/scan/sql-catalog.js.map +1 -0
  139. package/dist/scan/table-query.cjs.map +2 -2
  140. package/dist/scan/table-query.d.ts +4 -1
  141. package/dist/scan/table-query.d.ts.map +1 -1
  142. package/dist/scan/table-query.js.map +1 -1
  143. package/dist/vector-table-scan-source.cjs +288 -193
  144. package/dist/vector-table-scan-source.cjs.map +3 -3
  145. package/dist/worker-utils/index.cjs +2 -1
  146. package/dist/worker-utils/index.cjs.map +1 -1
  147. package/dist/worker-utils/lib/env-utils/version.js +1 -1
  148. package/dist/worker-utils/lib/library-utils/library-utils.js +1 -1
  149. package/dist/worker-utils/lib/library-utils/library-utils.js.map +1 -1
  150. package/package.json +17 -7
  151. package/src/loader-utils/index.ts +30 -6
  152. package/src/loader-utils/lib/files/http-file-transport.ts +65 -1
  153. package/src/loader-utils/lib/files/http-file-types.ts +2 -0
  154. package/src/loader-utils/lib/files/read-range-response.ts +66 -0
  155. package/src/loader-utils/lib/scan-utils/logical-table-query.ts +164 -0
  156. package/src/loader-utils/lib/scan-utils/query-options.ts +44 -0
  157. package/src/loader-utils/lib/scan-utils/scan-query-metadata.ts +17 -26
  158. package/src/loader-utils/lib/scan-utils/table-query.ts +49 -8
  159. package/src/loader-utils/lib/scan-utils/table-scan-batch.ts +25 -9
  160. package/src/loader-utils/lib/sources/data-source.ts +54 -1
  161. package/src/loader-utils/lib/sources/table-source-collection.ts +38 -0
  162. package/src/loader-utils/loader-types.ts +5 -5
  163. package/src/scan/arrow-execution/expressions.ts +86 -0
  164. package/src/scan/arrow-execution/filter-arrow-table.ts +309 -0
  165. package/src/scan/arrow-execution/project-arrow-table.ts +53 -0
  166. package/src/scan/arrow-execution.ts +7 -0
  167. package/src/scan/arrow-query.ts +3 -0
  168. package/src/scan/catalog-query-source.ts +132 -0
  169. package/src/scan/federated-table-scan-source.ts +19 -19
  170. package/src/scan/index.ts +28 -4
  171. package/src/scan/parse-sql-predicate.ts +14 -367
  172. package/src/scan/query-arrow-table-in-batches.ts +58 -0
  173. package/src/scan/query-arrow-table.ts +60 -237
  174. package/src/scan/query.ts +480 -0
  175. package/src/scan/resolve-query-source.ts +47 -0
  176. package/src/scan/sql-catalog.ts +167 -0
  177. package/src/scan/table-query.ts +6 -1
  178. package/src/worker-utils/lib/library-utils/library-utils.ts +1 -1
  179. package/dist/loader-utils/lib/scan-utils/experimental-scan-options.d.ts.map +0 -1
  180. package/dist/loader-utils/lib/scan-utils/experimental-scan-options.js.map +0 -1
  181. 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,86 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import * as arrow from 'apache-arrow';
6
+ import {compileArrowArray, compileArrowExpression} from '@math.gl/arrow/expressions';
7
+ import type {TableSelection} from '@math.gl/core/sql';
8
+ import {getExpressionColumns} from '@math.gl/core/expressions';
9
+
10
+ export {compileArrowExpression, compileArrowPredicate} from '@math.gl/arrow/expressions';
11
+ export {getExpressionColumns} from '@math.gl/core/expressions';
12
+
13
+ /** Finds source columns and rejects ambiguous projection shapes before a scan. */
14
+ export function getProjectionColumns(selections: readonly TableSelection[]): string[] {
15
+ return selections.flatMap(selection => {
16
+ if (typeof selection === 'string') return [selection];
17
+ if (!selection || typeof selection !== 'object' || Array.isArray(selection))
18
+ throw new Error('Invalid SQL projection.');
19
+ const keys = Object.keys(selection);
20
+ if ('array' in selection && keys.every(key => ['array', 'as', 'type'].includes(key))) {
21
+ if (!Array.isArray(selection.array))
22
+ throw new Error('SQL ARRAY must specify source columns.');
23
+ return [...selection.array];
24
+ }
25
+ if ('expression' in selection && keys.every(key => ['expression', 'as'].includes(key)))
26
+ return getExpressionColumns(selection.expression);
27
+ if ('column' in selection && keys.every(key => ['column', 'as'].includes(key)))
28
+ return [selection.column];
29
+ throw new Error('Unsupported SQL projection field.');
30
+ });
31
+ }
32
+
33
+ /** Binds SQL projections once and reuses source buffers for uncomputed fields. */
34
+ export function compileArrowProjection(
35
+ inputSchema: arrow.Schema,
36
+ selections: readonly TableSelection[],
37
+ signal?: AbortSignal
38
+ ): (source: arrow.Table) => arrow.Table {
39
+ const names = new Set<string>();
40
+ const columns = selections.map((selection, index) => {
41
+ const computed =
42
+ typeof selection === 'string' || 'column' in selection
43
+ ? undefined
44
+ : 'array' in selection
45
+ ? compileArrowArray(selection.array, {inputSchema, type: selection.type})
46
+ : compileArrowExpression(selection.expression, {inputSchema});
47
+ const column =
48
+ typeof selection === 'string' ? selection : 'column' in selection ? selection.column : '';
49
+ const sourceIndex = inputSchema.fields.findIndex(field => field.name === column);
50
+ const field = computed?.outputField ?? inputSchema.fields[sourceIndex];
51
+ if (!field) throw new Error(`Arrow projection column not found: ${column}`);
52
+ const alias = typeof selection === 'string' ? undefined : selection.as;
53
+ const name = alias === undefined ? (computed ? `column${index + 1}` : column) : alias;
54
+ if (typeof name !== 'string' || !name.length || names.has(name))
55
+ throw new Error(`Invalid or duplicate Arrow projection output name: ${name}`);
56
+ names.add(name);
57
+ return {field: field.clone({name}), computed, sourceIndex};
58
+ });
59
+ const fields = columns.map(column => column.field);
60
+ const schema = new arrow.Schema(
61
+ fields,
62
+ inputSchema.metadata,
63
+ undefined,
64
+ inputSchema.metadataVersion
65
+ );
66
+ return source =>
67
+ new arrow.Table(
68
+ schema,
69
+ source.batches.map(
70
+ batch =>
71
+ new arrow.RecordBatch(
72
+ schema,
73
+ arrow.makeData({
74
+ type: new arrow.Struct(fields),
75
+ length: batch.numRows,
76
+ children: columns.map(({computed, sourceIndex}) =>
77
+ computed
78
+ ? computed.evaluate(batch, {signal}).data[0]
79
+ : batch.data.children[sourceIndex]
80
+ )
81
+ }),
82
+ batch.metadata
83
+ )
84
+ )
85
+ );
86
+ }
@@ -0,0 +1,309 @@
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
+ const columnNames = getColumnarPredicateColumns(predicate);
43
+ return filterArrowRows(
44
+ source,
45
+ batch => {
46
+ const columns = getPredicateColumns(new arrow.Table(source.schema, [batch]), columnNames);
47
+ return rowIndex => evaluatePredicate(predicate as SQLPredicate, columns, rowIndex) === true;
48
+ },
49
+ options
50
+ );
51
+ }
52
+
53
+ /** Retains matching rows while preserving Arrow schema and batch metadata. */
54
+ export function filterArrowRows(
55
+ source: arrow.Table,
56
+ selectBatch: (batch: arrow.RecordBatch) => (rowIndex: number) => boolean,
57
+ options: ArrowFilterOptions = {}
58
+ ): arrow.Table {
59
+ let remaining = options.limit ?? Infinity;
60
+ if (remaining !== Infinity && (!Number.isSafeInteger(remaining) || remaining < 0)) {
61
+ throw new Error('Arrow filter limit must be a non-negative safe integer.');
62
+ }
63
+ options.signal?.throwIfAborted();
64
+ const batches: arrow.RecordBatch[] = [];
65
+ for (const batch of source.batches) {
66
+ if (remaining === 0) break;
67
+ options.signal?.throwIfAborted();
68
+ const data = new arrow.Table(source.schema, [batch]);
69
+ const selectRow = selectBatch(batch);
70
+ const indices: number[] = [];
71
+ for (let rowIndex = 0; rowIndex < batch.numRows && indices.length < remaining; rowIndex++) {
72
+ options.signal?.throwIfAborted();
73
+ if (selectRow(rowIndex)) indices.push(rowIndex);
74
+ }
75
+ remaining -= indices.length;
76
+ if (indices.length === 0) continue;
77
+ if (indices.length === batch.numRows) {
78
+ batches.push(batch);
79
+ continue;
80
+ }
81
+ if (indices[indices.length - 1] - indices[0] + 1 === indices.length) {
82
+ batches.push(batch.slice(indices[0], indices[indices.length - 1] + 1));
83
+ continue;
84
+ }
85
+ const vectors = Object.fromEntries(
86
+ source.schema.fields.map(field => {
87
+ const vector = data.getChild(field.name)!;
88
+ return [
89
+ field.name,
90
+ arrow.vectorFromArray(
91
+ indices.map(index => vector.get(index)),
92
+ field.type
93
+ )
94
+ ];
95
+ })
96
+ );
97
+ const gathered = new arrow.Table(source.schema, vectors);
98
+ batches.push(
99
+ ...gathered.batches.map(
100
+ output => new arrow.RecordBatch(source.schema, output.data, batch.metadata)
101
+ )
102
+ );
103
+ }
104
+ return new arrow.Table(source.schema, batches);
105
+ }
106
+
107
+ /** Creates a predicate-column lookup so values are not resolved for every source row. */
108
+ function getPredicateColumns(
109
+ sourceTable: arrow.Table,
110
+ columnNames: readonly string[]
111
+ ): Readonly<Record<string, arrow.Vector>> {
112
+ const columns: Record<string, arrow.Vector> = {};
113
+ for (const columnName of columnNames) {
114
+ const column = sourceTable.getChild(columnName);
115
+ if (!column) {
116
+ throw new Error(`Arrow query column not found: ${columnName}`);
117
+ }
118
+ columns[columnName] = column;
119
+ }
120
+ return columns;
121
+ }
122
+
123
+ /** Evaluates a predicate against one Arrow table row using SQL three-valued Boolean logic. */
124
+ function evaluatePredicate(
125
+ predicate: SQLPredicate,
126
+ columns: Readonly<Record<string, arrow.Vector>>,
127
+ rowIndex: number
128
+ ): SQLTruthValue {
129
+ if (isLogicalPredicate(predicate)) {
130
+ const values = predicate.args.map(child => evaluatePredicate(child, columns, rowIndex));
131
+ return predicate.op === 'and' ? evaluateAnd(values) : evaluateOr(values);
132
+ }
133
+ if (isNotPredicate(predicate)) {
134
+ const value = evaluatePredicate(predicate.args[0], columns, rowIndex);
135
+ return value === null ? null : !value;
136
+ }
137
+
138
+ const value = getColumnValue(columns, predicate.args[0].property, rowIndex);
139
+ if (isNullPredicate(predicate)) {
140
+ return value === null || value === undefined;
141
+ }
142
+ if (isInPredicate(predicate)) {
143
+ return evaluateIn(value, predicate.args[1]);
144
+ }
145
+ return evaluateComparison(value, predicate);
146
+ }
147
+
148
+ /** Returns the value of one predicate column or raises an internal invariant error. */
149
+ function getColumnValue(
150
+ columns: Readonly<Record<string, arrow.Vector>>,
151
+ columnName: string | readonly string[],
152
+ rowIndex: number
153
+ ): unknown {
154
+ const path = typeof columnName === 'string' ? [columnName] : columnName;
155
+ const column = columns[path[0]];
156
+ if (!column) {
157
+ throw new Error(`Arrow query could not read predicate column "${columnName}".`);
158
+ }
159
+ let value: unknown = column.get(rowIndex);
160
+ for (const property of path.slice(1)) {
161
+ if (value === null || value === undefined) return null;
162
+ value = (value as Record<string, unknown>)[property];
163
+ }
164
+ return value;
165
+ }
166
+
167
+ /** Evaluates SQL AND with null propagation. */
168
+ function evaluateAnd(values: readonly SQLTruthValue[]): SQLTruthValue {
169
+ if (values.includes(false)) {
170
+ return false;
171
+ }
172
+ return values.includes(null) ? null : true;
173
+ }
174
+
175
+ /** Evaluates SQL OR with null propagation. */
176
+ function evaluateOr(values: readonly SQLTruthValue[]): SQLTruthValue {
177
+ if (values.includes(true)) {
178
+ return true;
179
+ }
180
+ return values.includes(null) ? null : false;
181
+ }
182
+
183
+ /** Evaluates a membership expression with SQL null propagation. */
184
+ function evaluateIn(value: unknown, candidates: readonly SQLPredicateValue[]): SQLTruthValue {
185
+ if (value === null || value === undefined) {
186
+ return null;
187
+ }
188
+ for (const candidate of candidates) {
189
+ if (compareValues(value, candidate) === 0) {
190
+ return true;
191
+ }
192
+ }
193
+ return false;
194
+ }
195
+
196
+ /** Evaluates a binary SQL comparison with SQL null propagation. */
197
+ function evaluateComparison(value: unknown, predicate: SQLComparisonPredicate): SQLTruthValue {
198
+ if (value === null || value === undefined) {
199
+ return null;
200
+ }
201
+ const comparison = compareValues(value, predicate.args[1]);
202
+ switch (predicate.op) {
203
+ case '=':
204
+ return comparison === 0;
205
+ case '<>':
206
+ return comparison !== 0;
207
+ case '<':
208
+ return comparison < 0;
209
+ case '<=':
210
+ return comparison <= 0;
211
+ case '>':
212
+ return comparison > 0;
213
+ case '>=':
214
+ return comparison >= 0;
215
+ }
216
+ }
217
+
218
+ /** Compares supported predicate values and rejects comparisons without an unambiguous ordering. */
219
+ function compareValues(left: unknown, right: SQLPredicateValue): number {
220
+ if (isSQLPredicateParameter(right)) {
221
+ throw new Error(
222
+ `Arrow query parameter ":${right.parameter}" must be bound before query execution.`
223
+ );
224
+ }
225
+ if (left instanceof Date && right instanceof Date) {
226
+ return compareArrowQueryNumbers(left.getTime(), right.getTime());
227
+ }
228
+ if (typeof left === 'number' && right instanceof Date) {
229
+ return compareArrowQueryNumbers(left, right.getTime());
230
+ }
231
+ if (left instanceof Uint8Array && right instanceof Uint8Array) {
232
+ return compareBinary(left, right);
233
+ }
234
+ if (typeof left === 'bigint' && typeof right === 'bigint') {
235
+ return left === right ? 0 : left < right ? -1 : 1;
236
+ }
237
+ if (
238
+ (typeof left === 'bigint' && typeof right === 'number') ||
239
+ (typeof left === 'number' && typeof right === 'bigint')
240
+ ) {
241
+ const numericValue = typeof left === 'number' ? left : (right as number);
242
+ if (!Number.isFinite(numericValue))
243
+ throw new Error('Arrow query cannot compare non-finite numbers.');
244
+ // Native relational comparison preserves bigint precision and fractional number bounds.
245
+ return left < right ? -1 : left > right ? 1 : 0;
246
+ }
247
+ if (typeof left === 'number' && typeof right === 'number') {
248
+ return compareArrowQueryNumbers(left, right);
249
+ }
250
+ if (typeof left === 'string' && typeof right === 'string') {
251
+ return left === right ? 0 : left < right ? -1 : 1;
252
+ }
253
+ if (typeof left === 'boolean' && typeof right === 'boolean') {
254
+ return Number(left) - Number(right);
255
+ }
256
+ throw new Error(
257
+ `Arrow query cannot compare ${getArrowQueryValueType(left)} with ${getArrowQueryValueType(right)} values.`
258
+ );
259
+ }
260
+
261
+ /** Compares two finite number values. */
262
+ export function compareArrowQueryNumbers(left: number, right: number): number {
263
+ if (!Number.isFinite(left) || !Number.isFinite(right)) {
264
+ throw new Error('Arrow query cannot compare non-finite numbers.');
265
+ }
266
+ return left === right ? 0 : left < right ? -1 : 1;
267
+ }
268
+
269
+ /** Compares binary values lexicographically. */
270
+ function compareBinary(left: Uint8Array, right: Uint8Array): number {
271
+ const commonLength = Math.min(left.length, right.length);
272
+ for (let index = 0; index < commonLength; index++) {
273
+ if (left[index] !== right[index]) {
274
+ return left[index] < right[index] ? -1 : 1;
275
+ }
276
+ }
277
+ return left.length === right.length ? 0 : left.length < right.length ? -1 : 1;
278
+ }
279
+
280
+ /** Returns a human-readable type name for predicate diagnostics. */
281
+ export function getArrowQueryValueType(value: unknown): string {
282
+ if (value instanceof Date) {
283
+ return 'Date';
284
+ }
285
+ if (value instanceof Uint8Array) {
286
+ return 'Uint8Array';
287
+ }
288
+ return typeof value;
289
+ }
290
+
291
+ /** Returns whether the predicate combines two or more child predicates. */
292
+ function isLogicalPredicate(predicate: SQLPredicate): predicate is SQLLogicalPredicate {
293
+ return predicate.op === 'and' || predicate.op === 'or';
294
+ }
295
+
296
+ /** Returns whether the predicate negates one child predicate. */
297
+ function isNotPredicate(predicate: SQLPredicate): predicate is SQLNotPredicate {
298
+ return predicate.op === 'not';
299
+ }
300
+
301
+ /** Returns whether the predicate tests a column for null values. */
302
+ function isNullPredicate(predicate: SQLPredicate): predicate is SQLNullPredicate {
303
+ return predicate.op === 'isNull';
304
+ }
305
+
306
+ /** Returns whether the predicate tests a column against several values. */
307
+ function isInPredicate(predicate: SQLPredicate): predicate is SQLInPredicate {
308
+ return predicate.op === 'in';
309
+ }
@@ -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
+ }