@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.
- package/dist/cluster-source.cjs +45 -1
- package/dist/cluster-source.cjs.map +1 -1
- package/dist/dist.min.js +2 -2
- package/dist/federated-table-scan-source.cjs +272 -201
- package/dist/federated-table-scan-source.cjs.map +3 -3
- package/dist/index.cjs +1 -1
- package/dist/lib/init.js +1 -1
- package/dist/loader-utils/index.cjs +233 -12
- package/dist/loader-utils/index.cjs.map +2 -2
- package/dist/loader-utils/index.d.ts +8 -4
- package/dist/loader-utils/index.d.ts.map +1 -1
- package/dist/loader-utils/index.js +3 -1
- package/dist/loader-utils/index.js.map +1 -1
- package/dist/loader-utils/json-loader.js +1 -1
- package/dist/loader-utils/lib/files/http-file-transport.d.ts +4 -0
- package/dist/loader-utils/lib/files/http-file-transport.d.ts.map +1 -1
- package/dist/loader-utils/lib/files/http-file-transport.js +45 -1
- package/dist/loader-utils/lib/files/http-file-transport.js.map +1 -1
- package/dist/loader-utils/lib/files/http-file-types.d.ts +2 -0
- package/dist/loader-utils/lib/files/http-file-types.d.ts.map +1 -1
- package/dist/loader-utils/lib/files/read-range-response.d.ts +3 -0
- package/dist/loader-utils/lib/files/read-range-response.d.ts.map +1 -0
- package/dist/loader-utils/lib/files/read-range-response.js +62 -0
- package/dist/loader-utils/lib/files/read-range-response.js.map +1 -0
- package/dist/loader-utils/lib/log-utils/log.js +1 -1
- package/dist/loader-utils/lib/scan-utils/logical-table-query.d.ts +38 -0
- package/dist/loader-utils/lib/scan-utils/logical-table-query.d.ts.map +1 -0
- package/dist/loader-utils/lib/scan-utils/logical-table-query.js +96 -0
- package/dist/loader-utils/lib/scan-utils/logical-table-query.js.map +1 -0
- package/dist/loader-utils/lib/scan-utils/{experimental-scan-options.d.ts → query-options.d.ts} +5 -8
- package/dist/loader-utils/lib/scan-utils/query-options.d.ts.map +1 -0
- package/dist/loader-utils/lib/scan-utils/{experimental-scan-options.js → query-options.js} +1 -1
- package/dist/loader-utils/lib/scan-utils/query-options.js.map +1 -0
- package/dist/loader-utils/lib/scan-utils/scan-query-metadata.d.ts +12 -17
- package/dist/loader-utils/lib/scan-utils/scan-query-metadata.d.ts.map +1 -1
- package/dist/loader-utils/lib/scan-utils/scan-query-metadata.js +5 -5
- package/dist/loader-utils/lib/scan-utils/scan-query-metadata.js.map +1 -1
- package/dist/loader-utils/lib/scan-utils/table-query.d.ts +33 -3
- package/dist/loader-utils/lib/scan-utils/table-query.d.ts.map +1 -1
- package/dist/loader-utils/lib/scan-utils/table-query.js.map +1 -1
- package/dist/loader-utils/lib/scan-utils/table-scan-batch.d.ts +2 -2
- package/dist/loader-utils/lib/scan-utils/table-scan-batch.d.ts.map +1 -1
- package/dist/loader-utils/lib/scan-utils/table-scan-batch.js +11 -3
- package/dist/loader-utils/lib/scan-utils/table-scan-batch.js.map +1 -1
- package/dist/loader-utils/lib/sources/data-source.d.ts +4 -0
- package/dist/loader-utils/lib/sources/data-source.d.ts.map +1 -1
- package/dist/loader-utils/lib/sources/data-source.js +50 -1
- package/dist/loader-utils/lib/sources/data-source.js.map +1 -1
- package/dist/loader-utils/lib/sources/table-source-collection.d.ts +30 -0
- package/dist/loader-utils/lib/sources/table-source-collection.d.ts.map +1 -0
- package/dist/loader-utils/lib/sources/table-source-collection.js +5 -0
- package/dist/loader-utils/lib/sources/table-source-collection.js.map +1 -0
- package/dist/loader-utils/loader-types.d.ts +5 -5
- package/dist/loader-utils/loader-types.d.ts.map +1 -1
- package/dist/null-loader.js +1 -1
- package/dist/null-worker-node.js +1 -1
- package/dist/null-worker.js +1 -1
- package/dist/scan/arrow-execution/filter-arrow-table.cjs +254 -0
- package/dist/scan/arrow-execution/filter-arrow-table.cjs.map +7 -0
- package/dist/scan/arrow-execution/filter-arrow-table.d.ts +16 -0
- package/dist/scan/arrow-execution/filter-arrow-table.d.ts.map +1 -0
- package/dist/scan/arrow-execution/filter-arrow-table.js +222 -0
- package/dist/scan/arrow-execution/filter-arrow-table.js.map +1 -0
- package/dist/scan/arrow-execution/project-arrow-table.cjs +70 -0
- package/dist/scan/arrow-execution/project-arrow-table.cjs.map +7 -0
- package/dist/scan/arrow-execution/project-arrow-table.d.ts +8 -0
- package/dist/scan/arrow-execution/project-arrow-table.d.ts.map +1 -0
- package/dist/scan/arrow-execution/project-arrow-table.js +39 -0
- package/dist/scan/arrow-execution/project-arrow-table.js.map +1 -0
- package/dist/scan/arrow-execution.cjs +34 -0
- package/dist/scan/arrow-execution.cjs.map +7 -0
- package/dist/scan/arrow-execution.d.ts +4 -0
- package/dist/scan/arrow-execution.d.ts.map +1 -0
- package/dist/scan/arrow-execution.js +6 -0
- package/dist/scan/arrow-execution.js.map +1 -0
- package/dist/scan/arrow-query.cjs.map +1 -1
- package/dist/scan/arrow-query.d.ts +1 -1
- package/dist/scan/arrow-query.d.ts.map +1 -1
- package/dist/scan/catalog-query-source.cjs +122 -0
- package/dist/scan/catalog-query-source.cjs.map +7 -0
- package/dist/scan/catalog-query-source.d.ts +6 -0
- package/dist/scan/catalog-query-source.d.ts.map +1 -0
- package/dist/scan/catalog-query-source.js +115 -0
- package/dist/scan/catalog-query-source.js.map +1 -0
- package/dist/scan/federated-table-scan-source.cjs +4 -4
- package/dist/scan/federated-table-scan-source.cjs.map +2 -2
- package/dist/scan/federated-table-scan-source.d.ts +1 -1
- package/dist/scan/federated-table-scan-source.d.ts.map +1 -1
- package/dist/scan/federated-table-scan-source.js +4 -4
- package/dist/scan/federated-table-scan-source.js.map +1 -1
- package/dist/scan/index.cjs +188 -4
- package/dist/scan/index.cjs.map +4 -4
- package/dist/scan/index.d.ts +9 -2
- package/dist/scan/index.d.ts.map +1 -1
- package/dist/scan/index.js +4 -0
- package/dist/scan/index.js.map +1 -1
- package/dist/scan/parse-sql-predicate.cjs +3 -3
- package/dist/scan/parse-sql-predicate.cjs.map +2 -2
- package/dist/scan/parse-sql-predicate.d.ts +5 -2
- package/dist/scan/parse-sql-predicate.d.ts.map +1 -1
- package/dist/scan/parse-sql-predicate.js +3 -10
- package/dist/scan/parse-sql-predicate.js.map +1 -1
- package/dist/scan/parse-sql-query.cjs +70 -0
- package/dist/scan/parse-sql-query.cjs.map +7 -0
- package/dist/scan/parse-sql-query.d.ts +5 -0
- package/dist/scan/parse-sql-query.d.ts.map +1 -0
- package/dist/scan/parse-sql-query.js +49 -0
- package/dist/scan/parse-sql-query.js.map +1 -0
- package/dist/scan/parse-sql-select.cjs +215 -0
- package/dist/scan/parse-sql-select.cjs.map +7 -0
- package/dist/scan/parse-sql-select.d.ts +38 -0
- package/dist/scan/parse-sql-select.d.ts.map +1 -0
- package/dist/scan/parse-sql-select.js +208 -0
- package/dist/scan/parse-sql-select.js.map +1 -0
- package/dist/scan/query-arrow-table-in-batches.cjs +65 -0
- package/dist/scan/query-arrow-table-in-batches.cjs.map +7 -0
- package/dist/scan/query-arrow-table-in-batches.d.ts +8 -0
- package/dist/scan/query-arrow-table-in-batches.d.ts.map +1 -0
- package/dist/scan/query-arrow-table-in-batches.js +44 -0
- package/dist/scan/query-arrow-table-in-batches.js.map +1 -0
- package/dist/scan/query-arrow-table.cjs +52 -179
- package/dist/scan/query-arrow-table.cjs.map +3 -3
- package/dist/scan/query-arrow-table.d.ts +4 -37
- package/dist/scan/query-arrow-table.d.ts.map +1 -1
- package/dist/scan/query-arrow-table.js +37 -188
- package/dist/scan/query-arrow-table.js.map +1 -1
- package/dist/scan/query.cjs +257 -0
- package/dist/scan/query.cjs.map +7 -0
- package/dist/scan/query.d.ts +44 -0
- package/dist/scan/query.d.ts.map +1 -0
- package/dist/scan/query.js +246 -0
- package/dist/scan/query.js.map +1 -0
- package/dist/scan/resolve-query-source.cjs +59 -0
- package/dist/scan/resolve-query-source.cjs.map +7 -0
- package/dist/scan/resolve-query-source.d.ts +6 -0
- package/dist/scan/resolve-query-source.d.ts.map +1 -0
- package/dist/scan/resolve-query-source.js +37 -0
- package/dist/scan/resolve-query-source.js.map +1 -0
- package/dist/scan/sql-catalog.cjs +110 -0
- package/dist/scan/sql-catalog.cjs.map +7 -0
- package/dist/scan/sql-catalog.d.ts +47 -0
- package/dist/scan/sql-catalog.d.ts.map +1 -0
- package/dist/scan/sql-catalog.js +97 -0
- package/dist/scan/sql-catalog.js.map +1 -0
- package/dist/scan/table-query.cjs.map +2 -2
- package/dist/scan/table-query.d.ts +4 -1
- package/dist/scan/table-query.d.ts.map +1 -1
- package/dist/scan/table-query.js.map +1 -1
- package/dist/vector-table-scan-source.cjs +281 -193
- package/dist/vector-table-scan-source.cjs.map +3 -3
- package/dist/worker-utils/index.cjs +2 -1
- package/dist/worker-utils/index.cjs.map +1 -1
- package/dist/worker-utils/lib/env-utils/version.js +1 -1
- package/dist/worker-utils/lib/library-utils/library-utils.js +1 -1
- package/dist/worker-utils/lib/library-utils/library-utils.js.map +1 -1
- package/package.json +17 -7
- package/src/loader-utils/index.ts +30 -6
- package/src/loader-utils/lib/files/http-file-transport.ts +65 -1
- package/src/loader-utils/lib/files/http-file-types.ts +2 -0
- package/src/loader-utils/lib/files/read-range-response.ts +66 -0
- package/src/loader-utils/lib/scan-utils/logical-table-query.ts +164 -0
- package/src/loader-utils/lib/scan-utils/query-options.ts +44 -0
- package/src/loader-utils/lib/scan-utils/scan-query-metadata.ts +17 -26
- package/src/loader-utils/lib/scan-utils/table-query.ts +49 -8
- package/src/loader-utils/lib/scan-utils/table-scan-batch.ts +25 -9
- package/src/loader-utils/lib/sources/data-source.ts +54 -1
- package/src/loader-utils/lib/sources/table-source-collection.ts +38 -0
- package/src/loader-utils/loader-types.ts +5 -5
- package/src/scan/arrow-execution/filter-arrow-table.ts +293 -0
- package/src/scan/arrow-execution/project-arrow-table.ts +53 -0
- package/src/scan/arrow-execution.ts +7 -0
- package/src/scan/arrow-query.ts +3 -0
- package/src/scan/catalog-query-source.ts +132 -0
- package/src/scan/federated-table-scan-source.ts +19 -19
- package/src/scan/index.ts +28 -4
- package/src/scan/parse-sql-predicate.ts +9 -3
- package/src/scan/parse-sql-query.ts +53 -0
- package/src/scan/parse-sql-select.ts +220 -0
- package/src/scan/query-arrow-table-in-batches.ts +58 -0
- package/src/scan/query-arrow-table.ts +60 -237
- package/src/scan/query.ts +339 -0
- package/src/scan/resolve-query-source.ts +47 -0
- package/src/scan/sql-catalog.ts +167 -0
- package/src/scan/table-query.ts +6 -1
- package/src/worker-utils/lib/library-utils/library-utils.ts +1 -1
- package/dist/loader-utils/lib/scan-utils/experimental-scan-options.d.ts.map +0 -1
- package/dist/loader-utils/lib/scan-utils/experimental-scan-options.js.map +0 -1
- package/src/loader-utils/lib/scan-utils/experimental-scan-options.ts +0 -46
|
@@ -5,20 +5,15 @@
|
|
|
5
5
|
import * as arrow from 'apache-arrow';
|
|
6
6
|
import type {ArrowTable} from '@loaders.gl/core/schema';
|
|
7
7
|
import {convertArrowToSchema} from '@loaders.gl/core/schema-utils';
|
|
8
|
+
import {projectArrowTable, filterArrowTable} from './arrow-execution';
|
|
9
|
+
import {
|
|
10
|
+
compareArrowQueryNumbers as compareNumbers,
|
|
11
|
+
getArrowQueryValueType as getValueType
|
|
12
|
+
} from './arrow-execution/filter-arrow-table';
|
|
13
|
+
import type {TableSelection} from '@loaders.gl/core/loader-utils';
|
|
8
14
|
import {convertRowsToArrowTable} from './rows-to-arrow';
|
|
9
15
|
|
|
10
16
|
import {
|
|
11
|
-
isSQLPredicateParameter,
|
|
12
|
-
type SQLComparisonPredicate,
|
|
13
|
-
type SQLInPredicate,
|
|
14
|
-
type SQLLogicalPredicate,
|
|
15
|
-
type SQLNotPredicate,
|
|
16
|
-
type SQLNullPredicate,
|
|
17
|
-
type SQLPredicate,
|
|
18
|
-
type SQLPredicateValue
|
|
19
|
-
} from './sql-predicate-types';
|
|
20
|
-
import {
|
|
21
|
-
getSQLPredicateColumnNames,
|
|
22
17
|
planTableQuery,
|
|
23
18
|
type TableQueryFilterStep,
|
|
24
19
|
type TableQueryLimitStep,
|
|
@@ -51,6 +46,8 @@ type ArrowChildQuery = Readonly<{
|
|
|
51
46
|
/** Options for the lightweight in-memory Arrow query executor. */
|
|
52
47
|
export type ArrowQueryOptions = TableQueryOptions &
|
|
53
48
|
Readonly<{
|
|
49
|
+
/** Output source fields and aliases, in selection order. Cannot be combined with columns. */
|
|
50
|
+
select?: readonly TableSelection[];
|
|
54
51
|
/** Cancels the query before or during predicate evaluation. */
|
|
55
52
|
signal?: AbortSignal;
|
|
56
53
|
/** Computed columns evaluated before ordering and projection. */
|
|
@@ -70,7 +67,35 @@ export type ArrowQueryOptions = TableQueryOptions &
|
|
|
70
67
|
}>;
|
|
71
68
|
|
|
72
69
|
/** Explains an Arrow table query without materializing or scanning table rows. */
|
|
73
|
-
export function explainArrowTableQuery(
|
|
70
|
+
export function explainArrowTableQuery(
|
|
71
|
+
sourceTable: ArrowTable,
|
|
72
|
+
options: ArrowQueryOptions = {}
|
|
73
|
+
): import('./table-query').TableQueryExplain {
|
|
74
|
+
if (options.select !== undefined) {
|
|
75
|
+
if (options.columns !== undefined || hasRelationalOperators(options) || options.join) {
|
|
76
|
+
throw new Error('Arrow select cannot be combined with columns or relational operators.');
|
|
77
|
+
}
|
|
78
|
+
const projected = projectArrowTable(sourceTable.data, options.select);
|
|
79
|
+
const columns = [
|
|
80
|
+
...new Set(
|
|
81
|
+
options.select.map(selection =>
|
|
82
|
+
typeof selection === 'string' ? selection : selection.column
|
|
83
|
+
)
|
|
84
|
+
)
|
|
85
|
+
];
|
|
86
|
+
const {select, ...remainingOptions} = options;
|
|
87
|
+
const explanation = explainArrowTableQuery(sourceTable, {...remainingOptions, columns});
|
|
88
|
+
const outputColumns = Object.freeze(projected.schema.fields.map(field => field.name));
|
|
89
|
+
return Object.freeze({
|
|
90
|
+
...explanation,
|
|
91
|
+
outputColumns,
|
|
92
|
+
plan: Object.freeze(
|
|
93
|
+
explanation.plan.map(step =>
|
|
94
|
+
step.kind === 'project' ? Object.freeze({...step, columns: outputColumns, select}) : step
|
|
95
|
+
)
|
|
96
|
+
)
|
|
97
|
+
});
|
|
98
|
+
}
|
|
74
99
|
if (options.predicate) {
|
|
75
100
|
validateSQLPredicate(options.predicate);
|
|
76
101
|
}
|
|
@@ -81,8 +106,6 @@ export function explainArrowTableQuery(sourceTable: ArrowTable, options: ArrowQu
|
|
|
81
106
|
);
|
|
82
107
|
}
|
|
83
108
|
|
|
84
|
-
type SQLTruthValue = boolean | null;
|
|
85
|
-
|
|
86
109
|
/**
|
|
87
110
|
* Executes a small, Arrow-native query over one in-memory table.
|
|
88
111
|
*
|
|
@@ -96,6 +119,23 @@ export function queryArrowTable(
|
|
|
96
119
|
options: ArrowQueryOptions = {}
|
|
97
120
|
): ArrowTable {
|
|
98
121
|
throwIfAborted(options.signal);
|
|
122
|
+
if (options.select !== undefined) {
|
|
123
|
+
if (options.columns !== undefined || hasRelationalOperators(options) || options.join) {
|
|
124
|
+
throw new Error('Arrow select cannot be combined with columns or relational operators.');
|
|
125
|
+
}
|
|
126
|
+
// Validate aliases before evaluating any predicate, then apply them after filtering.
|
|
127
|
+
projectArrowTable(sourceTable.data, options.select);
|
|
128
|
+
const columns = [
|
|
129
|
+
...new Set(
|
|
130
|
+
options.select.map(selection =>
|
|
131
|
+
typeof selection === 'string' ? selection : selection.column
|
|
132
|
+
)
|
|
133
|
+
)
|
|
134
|
+
];
|
|
135
|
+
const {select, ...remainingOptions} = options;
|
|
136
|
+
const result = queryArrowTable(sourceTable, {...remainingOptions, columns});
|
|
137
|
+
return wrapArrowTable(projectArrowTable(result.data, select));
|
|
138
|
+
}
|
|
99
139
|
|
|
100
140
|
if (options.join) {
|
|
101
141
|
validateArrowJoinQuery(sourceTable, options);
|
|
@@ -118,40 +158,17 @@ export function queryArrowTable(
|
|
|
118
158
|
options
|
|
119
159
|
);
|
|
120
160
|
const scannedData = sourceData.select([...getScanStep(plan).columns]);
|
|
121
|
-
const projectedData = scannedData.select([...getProjectStep(plan).columns]);
|
|
122
161
|
const predicate = getFilterStep(plan)?.predicate;
|
|
123
162
|
const limit = getLimitStep(plan)?.limit ?? Number.POSITIVE_INFINITY;
|
|
124
163
|
if (!predicate) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const matchingRowIndices: number[] = [];
|
|
130
|
-
for (
|
|
131
|
-
let rowIndex = 0;
|
|
132
|
-
rowIndex < scannedData.numRows && matchingRowIndices.length < limit;
|
|
133
|
-
rowIndex++
|
|
134
|
-
) {
|
|
135
|
-
throwIfAborted(options.signal);
|
|
136
|
-
if (evaluatePredicate(predicate, predicateColumns, rowIndex) === true) {
|
|
137
|
-
matchingRowIndices.push(rowIndex);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (matchingRowIndices.length === 0) {
|
|
142
|
-
return wrapArrowTable(projectedData.slice(0, 0));
|
|
164
|
+
const projectedData = projectArrowTable(scannedData, getProjectStep(plan).columns);
|
|
165
|
+
return wrapArrowTable(
|
|
166
|
+
projectedData.numRows === 0 ? projectedData : projectedData.slice(0, limit)
|
|
167
|
+
);
|
|
143
168
|
}
|
|
144
169
|
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
const sourceColumn = projectedData.getChild(field.name);
|
|
148
|
-
if (!sourceColumn) {
|
|
149
|
-
throw new Error(`Arrow query could not read column "${field.name}".`);
|
|
150
|
-
}
|
|
151
|
-
const values = matchingRowIndices.map(rowIndex => sourceColumn.get(rowIndex));
|
|
152
|
-
outputColumns[field.name] = arrow.vectorFromArray(values, sourceColumn.type);
|
|
153
|
-
}
|
|
154
|
-
return wrapArrowTable(new arrow.Table(projectedData.schema, outputColumns));
|
|
170
|
+
const filteredData = filterArrowTable(scannedData, predicate, {limit, signal: options.signal});
|
|
171
|
+
return wrapArrowTable(projectArrowTable(filteredData, getProjectStep(plan).columns));
|
|
155
172
|
}
|
|
156
173
|
|
|
157
174
|
/** Executes the relational subset that can be evaluated against one in-memory Arrow table. */
|
|
@@ -603,200 +620,6 @@ function evaluateAggregate(
|
|
|
603
620
|
}
|
|
604
621
|
}
|
|
605
622
|
|
|
606
|
-
/** Creates a predicate-column lookup so values are not resolved for every source row. */
|
|
607
|
-
function getPredicateColumns(
|
|
608
|
-
sourceTable: arrow.Table,
|
|
609
|
-
columnNames: readonly string[]
|
|
610
|
-
): Readonly<Record<string, arrow.Vector>> {
|
|
611
|
-
const columns: Record<string, arrow.Vector> = {};
|
|
612
|
-
for (const columnName of columnNames) {
|
|
613
|
-
const column = sourceTable.getChild(columnName);
|
|
614
|
-
if (!column) {
|
|
615
|
-
throw new Error(`Arrow query column not found: ${columnName}`);
|
|
616
|
-
}
|
|
617
|
-
columns[columnName] = column;
|
|
618
|
-
}
|
|
619
|
-
return columns;
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
/** Evaluates a predicate against one Arrow table row using SQL three-valued Boolean logic. */
|
|
623
|
-
function evaluatePredicate(
|
|
624
|
-
predicate: SQLPredicate,
|
|
625
|
-
columns: Readonly<Record<string, arrow.Vector>>,
|
|
626
|
-
rowIndex: number
|
|
627
|
-
): SQLTruthValue {
|
|
628
|
-
if (isLogicalPredicate(predicate)) {
|
|
629
|
-
const values = predicate.args.map(child => evaluatePredicate(child, columns, rowIndex));
|
|
630
|
-
return predicate.op === 'and' ? evaluateAnd(values) : evaluateOr(values);
|
|
631
|
-
}
|
|
632
|
-
if (isNotPredicate(predicate)) {
|
|
633
|
-
const value = evaluatePredicate(predicate.args[0], columns, rowIndex);
|
|
634
|
-
return value === null ? null : !value;
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
const value = getColumnValue(columns, predicate.args[0].property, rowIndex);
|
|
638
|
-
if (isNullPredicate(predicate)) {
|
|
639
|
-
return value === null;
|
|
640
|
-
}
|
|
641
|
-
if (isInPredicate(predicate)) {
|
|
642
|
-
return evaluateIn(value, predicate.args[1]);
|
|
643
|
-
}
|
|
644
|
-
return evaluateComparison(value, predicate);
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
/** Returns the value of one predicate column or raises an internal invariant error. */
|
|
648
|
-
function getColumnValue(
|
|
649
|
-
columns: Readonly<Record<string, arrow.Vector>>,
|
|
650
|
-
columnName: string,
|
|
651
|
-
rowIndex: number
|
|
652
|
-
): unknown {
|
|
653
|
-
const column = columns[columnName];
|
|
654
|
-
if (!column) {
|
|
655
|
-
throw new Error(`Arrow query could not read predicate column "${columnName}".`);
|
|
656
|
-
}
|
|
657
|
-
return column.get(rowIndex);
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
/** Evaluates SQL AND with null propagation. */
|
|
661
|
-
function evaluateAnd(values: readonly SQLTruthValue[]): SQLTruthValue {
|
|
662
|
-
if (values.includes(false)) {
|
|
663
|
-
return false;
|
|
664
|
-
}
|
|
665
|
-
return values.includes(null) ? null : true;
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
/** Evaluates SQL OR with null propagation. */
|
|
669
|
-
function evaluateOr(values: readonly SQLTruthValue[]): SQLTruthValue {
|
|
670
|
-
if (values.includes(true)) {
|
|
671
|
-
return true;
|
|
672
|
-
}
|
|
673
|
-
return values.includes(null) ? null : false;
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
/** Evaluates a membership expression with SQL null propagation. */
|
|
677
|
-
function evaluateIn(value: unknown, candidates: readonly SQLPredicateValue[]): SQLTruthValue {
|
|
678
|
-
if (value === null || value === undefined) {
|
|
679
|
-
return null;
|
|
680
|
-
}
|
|
681
|
-
for (const candidate of candidates) {
|
|
682
|
-
if (compareValues(value, candidate) === 0) {
|
|
683
|
-
return true;
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
return false;
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
/** Evaluates a binary SQL comparison with SQL null propagation. */
|
|
690
|
-
function evaluateComparison(value: unknown, predicate: SQLComparisonPredicate): SQLTruthValue {
|
|
691
|
-
if (value === null || value === undefined) {
|
|
692
|
-
return null;
|
|
693
|
-
}
|
|
694
|
-
const comparison = compareValues(value, predicate.args[1]);
|
|
695
|
-
switch (predicate.op) {
|
|
696
|
-
case '=':
|
|
697
|
-
return comparison === 0;
|
|
698
|
-
case '<>':
|
|
699
|
-
return comparison !== 0;
|
|
700
|
-
case '<':
|
|
701
|
-
return comparison < 0;
|
|
702
|
-
case '<=':
|
|
703
|
-
return comparison <= 0;
|
|
704
|
-
case '>':
|
|
705
|
-
return comparison > 0;
|
|
706
|
-
case '>=':
|
|
707
|
-
return comparison >= 0;
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
/** Compares supported predicate values and rejects comparisons without an unambiguous ordering. */
|
|
712
|
-
function compareValues(left: unknown, right: SQLPredicateValue): number {
|
|
713
|
-
if (isSQLPredicateParameter(right)) {
|
|
714
|
-
throw new Error(
|
|
715
|
-
`Arrow query parameter ":${right.parameter}" must be bound before query execution.`
|
|
716
|
-
);
|
|
717
|
-
}
|
|
718
|
-
if (left instanceof Date && right instanceof Date) {
|
|
719
|
-
return compareNumbers(left.getTime(), right.getTime());
|
|
720
|
-
}
|
|
721
|
-
if (typeof left === 'number' && right instanceof Date) {
|
|
722
|
-
return compareNumbers(left, right.getTime());
|
|
723
|
-
}
|
|
724
|
-
if (left instanceof Uint8Array && right instanceof Uint8Array) {
|
|
725
|
-
return compareBinary(left, right);
|
|
726
|
-
}
|
|
727
|
-
if (typeof left === 'bigint' && typeof right === 'bigint') {
|
|
728
|
-
return left === right ? 0 : left < right ? -1 : 1;
|
|
729
|
-
}
|
|
730
|
-
if (typeof left === 'bigint' && typeof right === 'number' && Number.isSafeInteger(right)) {
|
|
731
|
-
return compareValues(left, BigInt(right));
|
|
732
|
-
}
|
|
733
|
-
if (typeof left === 'number' && typeof right === 'bigint' && Number.isSafeInteger(left)) {
|
|
734
|
-
return compareValues(BigInt(left), right);
|
|
735
|
-
}
|
|
736
|
-
if (typeof left === 'number' && typeof right === 'number') {
|
|
737
|
-
return compareNumbers(left, right);
|
|
738
|
-
}
|
|
739
|
-
if (typeof left === 'string' && typeof right === 'string') {
|
|
740
|
-
return left === right ? 0 : left < right ? -1 : 1;
|
|
741
|
-
}
|
|
742
|
-
if (typeof left === 'boolean' && typeof right === 'boolean') {
|
|
743
|
-
return Number(left) - Number(right);
|
|
744
|
-
}
|
|
745
|
-
throw new Error(
|
|
746
|
-
`Arrow query cannot compare ${getValueType(left)} with ${getValueType(right)} values.`
|
|
747
|
-
);
|
|
748
|
-
}
|
|
749
|
-
|
|
750
|
-
/** Compares two finite number values. */
|
|
751
|
-
function compareNumbers(left: number, right: number): number {
|
|
752
|
-
if (!Number.isFinite(left) || !Number.isFinite(right)) {
|
|
753
|
-
throw new Error('Arrow query cannot compare non-finite numbers.');
|
|
754
|
-
}
|
|
755
|
-
return left === right ? 0 : left < right ? -1 : 1;
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
/** Compares binary values lexicographically. */
|
|
759
|
-
function compareBinary(left: Uint8Array, right: Uint8Array): number {
|
|
760
|
-
const commonLength = Math.min(left.length, right.length);
|
|
761
|
-
for (let index = 0; index < commonLength; index++) {
|
|
762
|
-
if (left[index] !== right[index]) {
|
|
763
|
-
return left[index] < right[index] ? -1 : 1;
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
return left.length === right.length ? 0 : left.length < right.length ? -1 : 1;
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
/** Returns a human-readable type name for predicate diagnostics. */
|
|
770
|
-
function getValueType(value: unknown): string {
|
|
771
|
-
if (value instanceof Date) {
|
|
772
|
-
return 'Date';
|
|
773
|
-
}
|
|
774
|
-
if (value instanceof Uint8Array) {
|
|
775
|
-
return 'Uint8Array';
|
|
776
|
-
}
|
|
777
|
-
return typeof value;
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
/** Returns whether the predicate combines two or more child predicates. */
|
|
781
|
-
function isLogicalPredicate(predicate: SQLPredicate): predicate is SQLLogicalPredicate {
|
|
782
|
-
return predicate.op === 'and' || predicate.op === 'or';
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
/** Returns whether the predicate negates one child predicate. */
|
|
786
|
-
function isNotPredicate(predicate: SQLPredicate): predicate is SQLNotPredicate {
|
|
787
|
-
return predicate.op === 'not';
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
/** Returns whether the predicate tests a column for null values. */
|
|
791
|
-
function isNullPredicate(predicate: SQLPredicate): predicate is SQLNullPredicate {
|
|
792
|
-
return predicate.op === 'isNull';
|
|
793
|
-
}
|
|
794
|
-
|
|
795
|
-
/** Returns whether the predicate tests a column against several values. */
|
|
796
|
-
function isInPredicate(predicate: SQLPredicate): predicate is SQLInPredicate {
|
|
797
|
-
return predicate.op === 'in';
|
|
798
|
-
}
|
|
799
|
-
|
|
800
623
|
/** Returns the mandatory source scan from a normalized table-query plan. */
|
|
801
624
|
function getScanStep(plan: readonly TableQueryPlanStep[]): TableQueryScanStep {
|
|
802
625
|
const step = plan.find((candidate): candidate is TableQueryScanStep => candidate.kind === 'scan');
|
|
@@ -0,0 +1,339 @@
|
|
|
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 {ArrowTable, ArrowTableBatch, TableBatch, Schema} from '@loaders.gl/core/schema';
|
|
7
|
+
import {
|
|
8
|
+
normalizeTableQuery,
|
|
9
|
+
executeTableScanBatches,
|
|
10
|
+
planTableQuery,
|
|
11
|
+
emitQueryComplete,
|
|
12
|
+
type TableQuery,
|
|
13
|
+
type TableQueryExecutionOptions,
|
|
14
|
+
type TableQueryExecutionSummary,
|
|
15
|
+
type TableScanSource,
|
|
16
|
+
type TableScanReadOptions,
|
|
17
|
+
type LoaderOptions,
|
|
18
|
+
type DataSourceOptions,
|
|
19
|
+
type SourceLoader,
|
|
20
|
+
resolveLoaderAuthenticationOptions,
|
|
21
|
+
isSourceLoader
|
|
22
|
+
} from '@loaders.gl/core/loader-utils';
|
|
23
|
+
import {
|
|
24
|
+
convertArrowToSchema,
|
|
25
|
+
convertSchemaToArrow,
|
|
26
|
+
convertTableToArrow
|
|
27
|
+
} from '@loaders.gl/core/schema-utils';
|
|
28
|
+
import {projectArrowTable, filterArrowTable} from './arrow-execution';
|
|
29
|
+
import {createCatalogQuerySource} from './catalog-query-source';
|
|
30
|
+
import type {SQLCatalogQuery} from './sql-catalog';
|
|
31
|
+
import {resolveQuerySource} from './resolve-query-source';
|
|
32
|
+
import type {DataSource} from '@loaders.gl/core/loader-utils';
|
|
33
|
+
|
|
34
|
+
/** Logical table or metadata request. */
|
|
35
|
+
export type Query = TableQuery | SQLCatalogQuery;
|
|
36
|
+
|
|
37
|
+
/** Injected SQL parser; core validates and executes the returned logical query. */
|
|
38
|
+
export type SQLQueryParser = (sql: string) => Query | Promise<Query>;
|
|
39
|
+
|
|
40
|
+
/** Input and execution controls for one public asynchronous table query. */
|
|
41
|
+
export type QueryOptions = TableQueryExecutionOptions &
|
|
42
|
+
LoaderOptions &
|
|
43
|
+
Readonly<{
|
|
44
|
+
/** Source construction options, including an explicit core.name. */
|
|
45
|
+
core?: DataSourceOptions['core'];
|
|
46
|
+
/** Named caller-owned data sources used by FROM and metadata commands. */
|
|
47
|
+
sources?: readonly DataSource<unknown, DataSourceOptions>[];
|
|
48
|
+
/** Parser or lazy parser promise, required only for SQL string input. */
|
|
49
|
+
sqlParser?: SQLQueryParser | Promise<SQLQueryParser>;
|
|
50
|
+
}> &
|
|
51
|
+
Readonly<
|
|
52
|
+
| {
|
|
53
|
+
/** Already loaded Arrow table or caller-owned table scan source. */
|
|
54
|
+
source: ArrowTable | TableScanSource;
|
|
55
|
+
/** URL loader selection is only used with URL sources. */
|
|
56
|
+
loaders?: never;
|
|
57
|
+
}
|
|
58
|
+
| {
|
|
59
|
+
/** FROM chooses a source from the named registry when no default source is supplied. */
|
|
60
|
+
source?: undefined;
|
|
61
|
+
/** Named sources available for FROM resolution. */
|
|
62
|
+
sources: readonly DataSource<unknown, DataSourceOptions>[];
|
|
63
|
+
/** URL loader selection requires a URL source. */
|
|
64
|
+
loaders?: never;
|
|
65
|
+
}
|
|
66
|
+
| {
|
|
67
|
+
/** URL opened lazily with the explicitly supplied source loaders. */
|
|
68
|
+
source: string;
|
|
69
|
+
/** URL-capable source loaders; registered loaders are never selected implicitly. */
|
|
70
|
+
loaders: SourceLoader | SourceLoader[];
|
|
71
|
+
}
|
|
72
|
+
>;
|
|
73
|
+
|
|
74
|
+
/** Collects the same ordered Arrow batch pipeline exposed by queryInBatches. */
|
|
75
|
+
export async function query(
|
|
76
|
+
tableQuery: Query | string,
|
|
77
|
+
options: QueryOptions
|
|
78
|
+
): Promise<ArrowTable> {
|
|
79
|
+
let schema: arrow.Schema | undefined;
|
|
80
|
+
const batches: arrow.RecordBatch[] = [];
|
|
81
|
+
for await (const batch of queryInBatches(tableQuery, options)) {
|
|
82
|
+
schema = batch.data.schema;
|
|
83
|
+
batches.push(...batch.data.batches);
|
|
84
|
+
}
|
|
85
|
+
const data = new arrow.Table(schema!, batches);
|
|
86
|
+
return {shape: 'arrow-table', schema: convertArrowToSchema(data.schema), data};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Executes one logical query with bounded batch buffering and cooperative cancellation. */
|
|
90
|
+
export async function* queryInBatches(
|
|
91
|
+
tableQuery: Query | string,
|
|
92
|
+
options: QueryOptions
|
|
93
|
+
): AsyncIterable<ArrowTableBatch> {
|
|
94
|
+
const startedAt = Date.now();
|
|
95
|
+
let status: TableQueryExecutionSummary['status'] = 'early-terminated';
|
|
96
|
+
let rowsRead = 0;
|
|
97
|
+
let rowsReturned = 0;
|
|
98
|
+
let batchesRead = 0;
|
|
99
|
+
let sourcesRead = 0;
|
|
100
|
+
let error: unknown;
|
|
101
|
+
let ownedSource: {close?: () => void | Promise<void>} | undefined;
|
|
102
|
+
let earlyTerminationReason: TableQueryExecutionSummary['earlyTerminationReason'];
|
|
103
|
+
try {
|
|
104
|
+
options.signal?.throwIfAborted();
|
|
105
|
+
let parsedQuery: Query;
|
|
106
|
+
if (typeof tableQuery === 'string') {
|
|
107
|
+
const parser = await options.sqlParser;
|
|
108
|
+
options.signal?.throwIfAborted();
|
|
109
|
+
if (typeof parser !== 'function')
|
|
110
|
+
throw new Error('SQL queries require a sqlParser function or promise.');
|
|
111
|
+
parsedQuery = await parser(tableQuery);
|
|
112
|
+
options.signal?.throwIfAborted();
|
|
113
|
+
} else parsedQuery = tableQuery;
|
|
114
|
+
if (!parsedQuery || typeof parsedQuery !== 'object' || Array.isArray(parsedQuery))
|
|
115
|
+
throw new Error('Table query must be an object.');
|
|
116
|
+
let logicalQuery: TableQuery;
|
|
117
|
+
let source: unknown;
|
|
118
|
+
if ('command' in parsedQuery) {
|
|
119
|
+
logicalQuery = {};
|
|
120
|
+
source = createCatalogQuerySource(parsedQuery, options.sources);
|
|
121
|
+
} else {
|
|
122
|
+
logicalQuery = parsedQuery;
|
|
123
|
+
source =
|
|
124
|
+
logicalQuery.from === undefined
|
|
125
|
+
? options.source
|
|
126
|
+
: await resolveQuerySource(logicalQuery.from, options.sources, options.signal);
|
|
127
|
+
}
|
|
128
|
+
if (logicalQuery.from === undefined && typeof source === 'string') {
|
|
129
|
+
const opened = await openQuerySource(options);
|
|
130
|
+
if (
|
|
131
|
+
opened &&
|
|
132
|
+
typeof opened === 'object' &&
|
|
133
|
+
'close' in opened &&
|
|
134
|
+
typeof opened.close === 'function'
|
|
135
|
+
)
|
|
136
|
+
ownedSource = opened as {close: () => void | Promise<void>};
|
|
137
|
+
source = opened;
|
|
138
|
+
}
|
|
139
|
+
options.signal?.throwIfAborted();
|
|
140
|
+
if (!source || typeof source !== 'object')
|
|
141
|
+
throw new Error('Query source must be an Arrow table or table scan source.');
|
|
142
|
+
const inMemory = 'shape' in source && source.shape === 'arrow-table';
|
|
143
|
+
if (
|
|
144
|
+
!inMemory &&
|
|
145
|
+
(!('read' in source) ||
|
|
146
|
+
typeof source.read !== 'function' ||
|
|
147
|
+
!('getQueryMetadata' in source) ||
|
|
148
|
+
typeof source.getQueryMetadata !== 'function')
|
|
149
|
+
) {
|
|
150
|
+
throw new Error('Query source must be an Arrow table or table scan source.');
|
|
151
|
+
}
|
|
152
|
+
const metadata = inMemory
|
|
153
|
+
? undefined
|
|
154
|
+
: await (source as TableScanSource).getQueryMetadata({signal: options.signal});
|
|
155
|
+
options.signal?.throwIfAborted();
|
|
156
|
+
if (
|
|
157
|
+
metadata &&
|
|
158
|
+
(metadata.queryType !== 'table' ||
|
|
159
|
+
metadata.execution.status !== 'supported' ||
|
|
160
|
+
metadata.execution.method !== 'read')
|
|
161
|
+
) {
|
|
162
|
+
throw new Error('Query source must support table reads.');
|
|
163
|
+
}
|
|
164
|
+
const sourceSchema = inMemory
|
|
165
|
+
? (source as ArrowTable).data.schema
|
|
166
|
+
: convertSchemaToArrow(metadata!.schema);
|
|
167
|
+
const names = sourceSchema.fields.map(field => field.name);
|
|
168
|
+
const normalized = normalizeTableQuery(names, logicalQuery);
|
|
169
|
+
const outputColumns = [...new Set(normalized.select.map(selection => selection.column))];
|
|
170
|
+
const plan = planTableQuery(names, {columns: outputColumns, predicate: normalized.where});
|
|
171
|
+
const requiredColumns = plan[0].kind === 'scan' ? plan[0].columns : names;
|
|
172
|
+
const capabilities = metadata?.capabilities.table;
|
|
173
|
+
// read() must perform exact filtering when it advertises pushdown+residual, not only pruning.
|
|
174
|
+
const delegatePredicate = Boolean(
|
|
175
|
+
normalized.where &&
|
|
176
|
+
capabilities &&
|
|
177
|
+
['pushdown', 'pushdown+residual'].includes(capabilities.predicate)
|
|
178
|
+
);
|
|
179
|
+
const residual = delegatePredicate ? undefined : normalized.where;
|
|
180
|
+
const readOptions: TableScanReadOptions = {
|
|
181
|
+
signal: options.signal,
|
|
182
|
+
...(capabilities && capabilities.projection !== 'unsupported'
|
|
183
|
+
? {columns: requiredColumns}
|
|
184
|
+
: {}),
|
|
185
|
+
...(delegatePredicate ? {predicate: normalized.where} : {}),
|
|
186
|
+
...(!residual &&
|
|
187
|
+
capabilities &&
|
|
188
|
+
['pushdown', 'pushdown+residual'].includes(capabilities.limit)
|
|
189
|
+
? {limit: normalized.limit}
|
|
190
|
+
: {})
|
|
191
|
+
};
|
|
192
|
+
const readSchema = convertArrowToSchema(
|
|
193
|
+
readOptions.columns ? sourceSchema.select([...readOptions.columns]) : sourceSchema
|
|
194
|
+
);
|
|
195
|
+
const empty = projectArrowTable(new arrow.Table(sourceSchema), normalized.select);
|
|
196
|
+
const comparisonSchema = convertSchemaToArrow(convertArrowToSchema(empty.schema));
|
|
197
|
+
let emitted = false;
|
|
198
|
+
/** Adapts physical batches without pulling ahead of the query consumer. */
|
|
199
|
+
async function* inputs(): AsyncIterable<ArrowTableBatch> {
|
|
200
|
+
sourcesRead = 1;
|
|
201
|
+
const batches: AsyncIterable<unknown> = inMemory
|
|
202
|
+
? memoryBatches((source as ArrowTable).data)
|
|
203
|
+
: (source as TableScanSource).read(readOptions);
|
|
204
|
+
for await (const value of batches) {
|
|
205
|
+
options.signal?.throwIfAborted();
|
|
206
|
+
const batch = normalizeBatch(value, readSchema);
|
|
207
|
+
batchesRead++;
|
|
208
|
+
rowsRead += batch.length;
|
|
209
|
+
yield batch;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
for await (const value of executeTableScanBatches(
|
|
213
|
+
() => inputs(),
|
|
214
|
+
{
|
|
215
|
+
predicate: residual,
|
|
216
|
+
limit: normalized.limit,
|
|
217
|
+
signal: options.signal
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
filter: (batch, predicate, signal) => {
|
|
221
|
+
const input = batch as ArrowTableBatch;
|
|
222
|
+
const data = predicate ? filterArrowTable(input.data, predicate, {signal}) : input.data;
|
|
223
|
+
return {...input, ...wrapBatch(data)};
|
|
224
|
+
},
|
|
225
|
+
project: batch => {
|
|
226
|
+
const input = batch as ArrowTableBatch;
|
|
227
|
+
return {...input, ...wrapBatch(projectArrowTable(input.data, normalized.select))};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
)) {
|
|
231
|
+
const batch = value as ArrowTableBatch;
|
|
232
|
+
if (!arrow.util.compareSchemas(comparisonSchema, convertSchemaToArrow(batch.schema!)))
|
|
233
|
+
throw new Error('Query output schema does not match source metadata.');
|
|
234
|
+
emitted = true;
|
|
235
|
+
rowsReturned += batch.length;
|
|
236
|
+
yield batch;
|
|
237
|
+
}
|
|
238
|
+
// An empty schema-bearing batch makes collected and streaming empty results identical.
|
|
239
|
+
if (!emitted) yield wrapBatch(empty);
|
|
240
|
+
if (normalized.limit !== undefined && rowsReturned >= normalized.limit)
|
|
241
|
+
earlyTerminationReason = 'limit';
|
|
242
|
+
status = earlyTerminationReason ? 'early-terminated' : 'completed';
|
|
243
|
+
} catch (failure) {
|
|
244
|
+
error = failure;
|
|
245
|
+
status = options.signal?.aborted ? 'cancelled' : 'failed';
|
|
246
|
+
throw failure;
|
|
247
|
+
} finally {
|
|
248
|
+
if (status === 'early-terminated' && !earlyTerminationReason)
|
|
249
|
+
earlyTerminationReason = 'consumer-return';
|
|
250
|
+
try {
|
|
251
|
+
await ownedSource?.close?.();
|
|
252
|
+
} catch (failure) {
|
|
253
|
+
if (error === undefined) {
|
|
254
|
+
error = failure;
|
|
255
|
+
status = options.signal?.aborted ? 'cancelled' : 'failed';
|
|
256
|
+
earlyTerminationReason = undefined;
|
|
257
|
+
throw failure;
|
|
258
|
+
}
|
|
259
|
+
} finally {
|
|
260
|
+
emitQueryComplete(
|
|
261
|
+
options.onQueryComplete,
|
|
262
|
+
Object.freeze({
|
|
263
|
+
status,
|
|
264
|
+
sourcesPlanned: 1,
|
|
265
|
+
sourcesRead,
|
|
266
|
+
batchesRead,
|
|
267
|
+
rowsRead,
|
|
268
|
+
rowsReturned,
|
|
269
|
+
durationMilliseconds: Date.now() - startedAt,
|
|
270
|
+
...(earlyTerminationReason ? {earlyTerminationReason} : {}),
|
|
271
|
+
...(error === undefined ? {} : {error})
|
|
272
|
+
})
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Emits original Arrow RecordBatches without copying their buffers. */
|
|
279
|
+
async function* memoryBatches(data: arrow.Table): AsyncIterable<ArrowTableBatch> {
|
|
280
|
+
for (const batch of data.batches) yield wrapBatch(new arrow.Table(data.schema, [batch]));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Converts supported native table batches to Arrow while preserving their declared schema. */
|
|
284
|
+
function normalizeBatch(value: unknown, fallbackSchema: Schema): ArrowTableBatch {
|
|
285
|
+
if (!value || typeof value !== 'object' || !('shape' in value))
|
|
286
|
+
throw new Error('Query source returned an unsupported batch.');
|
|
287
|
+
const input = value as TableBatch;
|
|
288
|
+
const batch = {...input, schema: input.schema ?? fallbackSchema} as TableBatch;
|
|
289
|
+
if (
|
|
290
|
+
!['arrow-table', 'object-row-table', 'array-row-table', 'columnar-table'].includes(batch.shape)
|
|
291
|
+
)
|
|
292
|
+
throw new Error(`Unsupported query batch shape: ${batch.shape}`);
|
|
293
|
+
const data =
|
|
294
|
+
batch.length === 0 && batch.schema
|
|
295
|
+
? new arrow.Table(convertSchemaToArrow(batch.schema))
|
|
296
|
+
: convertTableToArrow(batch);
|
|
297
|
+
return {...batch, ...wrapBatch(data)} as ArrowTableBatch;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Wraps Arrow data in the standard batch envelope. */
|
|
301
|
+
function wrapBatch(data: arrow.Table): ArrowTableBatch {
|
|
302
|
+
return {
|
|
303
|
+
shape: 'arrow-table',
|
|
304
|
+
data,
|
|
305
|
+
schema: convertArrowToSchema(data.schema),
|
|
306
|
+
batchType: 'data',
|
|
307
|
+
length: data.numRows
|
|
308
|
+
} as ArrowTableBatch;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Opens only explicit URL source loaders; the public query owns the returned source. */
|
|
312
|
+
async function openQuerySource(options: QueryOptions): Promise<unknown> {
|
|
313
|
+
const candidates = Array.isArray(options.loaders) ? options.loaders : [options.loaders];
|
|
314
|
+
if (!candidates.length || candidates.some(loader => !isSourceLoader(loader) || !loader.fromUrl))
|
|
315
|
+
throw new Error('Query source must specify explicit URL-capable source loaders for a URL.');
|
|
316
|
+
const {source, sources, loaders, sqlParser, onQueryComplete, ...sourceOptions} = options;
|
|
317
|
+
const url = source as string;
|
|
318
|
+
const selected =
|
|
319
|
+
candidates.length === 1 ? candidates[0]! : candidates.find(loader => loader!.testURL(url));
|
|
320
|
+
if (!selected) throw new Error('No explicit source loader matches the query URL.');
|
|
321
|
+
const implementation = selected.preload ? await selected.preload(url, sourceOptions) : selected;
|
|
322
|
+
options.signal?.throwIfAborted();
|
|
323
|
+
if (!isSourceLoader(implementation) || !implementation.fromUrl)
|
|
324
|
+
throw new Error('Query source preload did not return a URL-capable source loader.');
|
|
325
|
+
const authenticatedOptions = await resolveLoaderAuthenticationOptions(
|
|
326
|
+
selected.getAuthentications ? selected : implementation,
|
|
327
|
+
url,
|
|
328
|
+
{...sourceOptions, core: {...sourceOptions.core, ignoreRegisteredLoaders: true}}
|
|
329
|
+
);
|
|
330
|
+
options.signal?.throwIfAborted();
|
|
331
|
+
return implementation.createDataSource(url, authenticatedOptions as DataSourceOptions);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export {parseSQLQuery} from './parse-sql-query';
|
|
335
|
+
export {parseSQLSelect} from './parse-sql-select';
|
|
336
|
+
export type {SQLSelectParserOptions} from './parse-sql-select';
|
|
337
|
+
|
|
338
|
+
export {createSQLCatalog} from './sql-catalog';
|
|
339
|
+
export type {SQLCatalog, SQLCatalogQuery, SQLCatalogSource, SQLCatalogTable} from './sql-catalog';
|