@loaders.gl/core 5.0.0-rc.2 → 5.0.0
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/arrow-geometry/geometry-codecs.cjs +51 -50
- package/dist/arrow-geometry/geometry-codecs.cjs.map +3 -3
- package/dist/arrow-geometry/geometry-codecs.d.ts +1 -1
- package/dist/arrow-geometry/geometry-codecs.d.ts.map +1 -1
- package/dist/arrow-geometry/geometry-codecs.js +27 -60
- package/dist/arrow-geometry/geometry-codecs.js.map +1 -1
- package/dist/arrow-geometry/triangulate-wkb.cjs +33 -118
- package/dist/arrow-geometry/triangulate-wkb.cjs.map +2 -2
- package/dist/arrow-geometry/triangulate-wkb.d.ts.map +1 -1
- package/dist/arrow-geometry/triangulate-wkb.js +39 -142
- package/dist/arrow-geometry/triangulate-wkb.js.map +1 -1
- package/dist/cluster-source.cjs +124 -370
- package/dist/cluster-source.cjs.map +3 -3
- package/dist/constants/index.cjs +2 -2
- package/dist/constants/index.d.ts +4 -4
- package/dist/constants/index.d.ts.map +1 -1
- package/dist/constants/index.js +2 -2
- package/dist/constants/index.js.map +1 -1
- package/dist/dist.dev.js +2 -2
- package/dist/dist.min.js +2 -2
- package/dist/federated-table-scan-source.cjs +166 -564
- package/dist/federated-table-scan-source.cjs.map +3 -3
- package/dist/geoarrow/geoarrow-converter/convert-geoarrow-geometry.cjs +15 -0
- package/dist/geoarrow/geoarrow-converter/convert-geoarrow-geometry.cjs.map +2 -2
- package/dist/geoarrow/geoarrow-converter/convert-geoarrow-geometry.js +19 -1
- package/dist/geoarrow/geoarrow-converter/convert-geoarrow-geometry.js.map +1 -1
- package/dist/geoarrow/get-geoarrow-vertex-count.cjs +6 -27
- package/dist/geoarrow/get-geoarrow-vertex-count.cjs.map +2 -2
- package/dist/geoarrow/get-geoarrow-vertex-count.d.ts.map +1 -1
- package/dist/geoarrow/get-geoarrow-vertex-count.js +9 -24
- package/dist/geoarrow/get-geoarrow-vertex-count.js.map +1 -1
- package/dist/geoarrow/lib/geometry-converters/wkb/helpers/parse-wkb-header.cjs +3 -5
- package/dist/geoarrow/lib/geometry-converters/wkb/helpers/parse-wkb-header.cjs.map +2 -2
- package/dist/geoarrow/lib/geometry-converters/wkb/helpers/parse-wkb-header.d.ts +2 -24
- package/dist/geoarrow/lib/geometry-converters/wkb/helpers/parse-wkb-header.d.ts.map +1 -1
- package/dist/geoarrow/lib/geometry-converters/wkb/helpers/parse-wkb-header.js +6 -28
- package/dist/geoarrow/lib/geometry-converters/wkb/helpers/parse-wkb-header.js.map +1 -1
- package/dist/geoarrow/lib/kernels/decode-wkt-native.cjs +20 -226
- package/dist/geoarrow/lib/kernels/decode-wkt-native.cjs.map +2 -2
- package/dist/geoarrow/lib/kernels/decode-wkt-native.d.ts +1 -2
- package/dist/geoarrow/lib/kernels/decode-wkt-native.d.ts.map +1 -1
- package/dist/geoarrow/lib/kernels/decode-wkt-native.js +23 -264
- package/dist/geoarrow/lib/kernels/decode-wkt-native.js.map +1 -1
- package/dist/index.cjs +4 -4
- package/dist/index.cjs.map +1 -1
- package/dist/lib/init.js +1 -1
- package/dist/lib/init.js.map +1 -1
- package/dist/loader-utils/index.cjs +31 -217
- package/dist/loader-utils/index.cjs.map +2 -2
- package/dist/loader-utils/json-loader.js +1 -1
- package/dist/loader-utils/json-loader.js.map +1 -1
- package/dist/loader-utils/lib/log-utils/log.js +1 -1
- package/dist/loader-utils/lib/log-utils/log.js.map +1 -1
- package/dist/loader-utils/lib/scan-utils/columnar-predicate.d.ts +4 -52
- package/dist/loader-utils/lib/scan-utils/columnar-predicate.d.ts.map +1 -1
- package/dist/loader-utils/lib/scan-utils/columnar-predicate.js +16 -236
- package/dist/loader-utils/lib/scan-utils/columnar-predicate.js.map +1 -1
- package/dist/loader-utils/lib/scan-utils/relational-query.d.ts +2 -0
- package/dist/loader-utils/lib/scan-utils/relational-query.d.ts.map +1 -1
- package/dist/loader-utils/lib/scan-utils/relational-query.js.map +1 -1
- package/dist/null-loader.js +1 -1
- package/dist/null-loader.js.map +1 -1
- package/dist/null-worker-node.js +1 -1
- package/dist/null-worker.js +1 -1
- package/dist/scan/arrow-execution/expressions.cjs +9 -78
- package/dist/scan/arrow-execution/expressions.cjs.map +3 -3
- package/dist/scan/arrow-execution/expressions.d.ts +2 -8
- package/dist/scan/arrow-execution/expressions.d.ts.map +1 -1
- package/dist/scan/arrow-execution/expressions.js +2 -55
- package/dist/scan/arrow-execution/expressions.js.map +1 -1
- package/dist/scan/arrow-execution/filter-arrow-table.cjs +41 -179
- package/dist/scan/arrow-execution/filter-arrow-table.cjs.map +3 -3
- package/dist/scan/arrow-execution/filter-arrow-table.d.ts +1 -5
- package/dist/scan/arrow-execution/filter-arrow-table.d.ts.map +1 -1
- package/dist/scan/arrow-execution/filter-arrow-table.js +44 -190
- package/dist/scan/arrow-execution/filter-arrow-table.js.map +1 -1
- package/dist/scan/arrow-execution.cjs +2 -2
- package/dist/scan/arrow-execution.cjs.map +2 -2
- package/dist/scan/arrow-execution.d.ts +1 -1
- package/dist/scan/arrow-execution.d.ts.map +1 -1
- package/dist/scan/arrow-execution.js +1 -1
- package/dist/scan/arrow-execution.js.map +1 -1
- package/dist/scan/arrow-query.cjs +3 -3
- package/dist/scan/arrow-query.cjs.map +2 -2
- package/dist/scan/arrow-query.d.ts +2 -2
- package/dist/scan/arrow-query.d.ts.map +1 -1
- package/dist/scan/arrow-query.js +2 -2
- package/dist/scan/arrow-query.js.map +1 -1
- package/dist/scan/federated-table-scan-source.cjs +3 -3
- package/dist/scan/federated-table-scan-source.cjs.map +2 -2
- package/dist/scan/federated-table-scan-source.js +1 -1
- package/dist/scan/federated-table-scan-source.js.map +1 -1
- package/dist/scan/index.cjs +9 -49
- package/dist/scan/index.cjs.map +3 -3
- package/dist/scan/index.d.ts +2 -2
- package/dist/scan/index.d.ts.map +1 -1
- package/dist/scan/index.js +2 -2
- package/dist/scan/index.js.map +1 -1
- package/dist/scan/plan-arrow-table-query.cjs +271 -0
- package/dist/scan/plan-arrow-table-query.cjs.map +7 -0
- package/dist/scan/plan-arrow-table-query.d.ts +50 -0
- package/dist/scan/plan-arrow-table-query.d.ts.map +1 -0
- package/dist/scan/plan-arrow-table-query.js +257 -0
- package/dist/scan/plan-arrow-table-query.js.map +1 -0
- package/dist/scan/query-arrow-table-in-batches.cjs +2 -39
- package/dist/scan/query-arrow-table-in-batches.cjs.map +2 -2
- package/dist/scan/query-arrow-table-in-batches.d.ts +1 -1
- package/dist/scan/query-arrow-table-in-batches.d.ts.map +1 -1
- package/dist/scan/query-arrow-table-in-batches.js +3 -26
- package/dist/scan/query-arrow-table-in-batches.js.map +1 -1
- package/dist/scan/query-arrow-table.cjs +7 -443
- package/dist/scan/query-arrow-table.cjs.map +3 -3
- package/dist/scan/query-arrow-table.d.ts +6 -54
- package/dist/scan/query-arrow-table.d.ts.map +1 -1
- package/dist/scan/query-arrow-table.js +6 -475
- package/dist/scan/query-arrow-table.js.map +1 -1
- package/dist/scan/query-pushdown.cjs +3 -4
- package/dist/scan/query-pushdown.cjs.map +2 -2
- package/dist/scan/query-pushdown.js +4 -5
- package/dist/scan/query-pushdown.js.map +1 -1
- package/dist/scan/query.cjs +10 -9
- package/dist/scan/query.cjs.map +2 -2
- package/dist/scan/query.d.ts.map +1 -1
- package/dist/scan/query.js +14 -5
- package/dist/scan/query.js.map +1 -1
- package/dist/scan/scan-engine.cjs +14 -7
- package/dist/scan/scan-engine.cjs.map +2 -2
- package/dist/scan/scan-engine.d.ts +1 -1
- package/dist/scan/scan-engine.d.ts.map +1 -1
- package/dist/scan/scan-engine.js +4 -9
- package/dist/scan/scan-engine.js.map +1 -1
- package/dist/scan/vector-table-scan-source.cjs +14 -3
- package/dist/scan/vector-table-scan-source.cjs.map +2 -2
- package/dist/scan/vector-table-scan-source.d.ts +1 -1
- package/dist/scan/vector-table-scan-source.d.ts.map +1 -1
- package/dist/scan/vector-table-scan-source.js +2 -1
- package/dist/scan/vector-table-scan-source.js.map +1 -1
- package/dist/schema-utils/index.cjs +11 -47
- package/dist/schema-utils/index.cjs.map +1 -1
- package/dist/schema-utils/lib/arrow/arrow-metadata.d.ts +2 -4
- package/dist/schema-utils/lib/arrow/arrow-metadata.d.ts.map +1 -1
- package/dist/schema-utils/lib/arrow/arrow-metadata.js +2 -43
- package/dist/schema-utils/lib/arrow/arrow-metadata.js.map +1 -1
- package/dist/vector-table-scan-source.cjs +854 -1314
- package/dist/vector-table-scan-source.cjs.map +3 -3
- package/dist/wkt/twkb/index.cjs +68 -5
- package/dist/wkt/twkb/index.cjs.map +3 -3
- package/dist/wkt/twkb/index.d.ts +10 -2
- package/dist/wkt/twkb/index.d.ts.map +1 -1
- package/dist/wkt/twkb/index.js +49 -2
- package/dist/wkt/twkb/index.js.map +1 -1
- package/dist/worker-utils/index.cjs +4 -4
- 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/env-utils/version.js.map +1 -1
- package/package.json +7 -7
- package/src/arrow-geometry/geometry-codecs.ts +35 -78
- package/src/arrow-geometry/triangulate-wkb.ts +37 -169
- package/src/constants/index.ts +2 -2
- package/src/geoarrow/geoarrow-converter/convert-geoarrow-geometry.ts +22 -1
- package/src/geoarrow/get-geoarrow-vertex-count.ts +7 -32
- package/src/geoarrow/lib/geometry-converters/wkb/helpers/parse-wkb-header.ts +6 -30
- package/src/geoarrow/lib/kernels/decode-wkt-native.ts +23 -269
- package/src/loader-utils/lib/scan-utils/columnar-predicate.ts +46 -363
- package/src/loader-utils/lib/scan-utils/relational-query.ts +7 -1
- package/src/scan/arrow-execution/expressions.ts +2 -86
- package/src/scan/arrow-execution/filter-arrow-table.ts +54 -238
- package/src/scan/arrow-execution.ts +1 -1
- package/src/scan/arrow-query.ts +2 -2
- package/src/scan/federated-table-scan-source.ts +1 -1
- package/src/scan/index.ts +2 -2
- package/src/scan/plan-arrow-table-query.ts +367 -0
- package/src/scan/query-arrow-table-in-batches.ts +4 -33
- package/src/scan/query-arrow-table.ts +10 -626
- package/src/scan/query-pushdown.ts +3 -4
- package/src/scan/query.ts +16 -10
- package/src/scan/scan-engine.ts +4 -11
- package/src/scan/vector-table-scan-source.ts +2 -1
- package/src/schema-utils/lib/arrow/arrow-metadata.ts +2 -50
- package/src/wkt/twkb/index.ts +63 -2
- package/dist/scan/arrow-execution/portable-predicate-compatibility.cjs +0 -41
- package/dist/scan/arrow-execution/portable-predicate-compatibility.cjs.map +0 -7
- package/dist/scan/arrow-execution/portable-predicate-compatibility.d.ts +0 -5
- package/dist/scan/arrow-execution/portable-predicate-compatibility.d.ts.map +0 -1
- package/dist/scan/arrow-execution/portable-predicate-compatibility.js +0 -23
- package/dist/scan/arrow-execution/portable-predicate-compatibility.js.map +0 -1
- package/dist/scan/arrow-execution/project-arrow-table.cjs +0 -70
- package/dist/scan/arrow-execution/project-arrow-table.cjs.map +0 -7
- package/dist/scan/arrow-execution/project-arrow-table.d.ts +0 -8
- package/dist/scan/arrow-execution/project-arrow-table.d.ts.map +0 -1
- package/dist/scan/arrow-execution/project-arrow-table.js +0 -39
- package/dist/scan/arrow-execution/project-arrow-table.js.map +0 -1
- package/dist/scan/query-arrow-table-math.cjs +0 -41
- package/dist/scan/query-arrow-table-math.cjs.map +0 -7
- package/dist/scan/query-arrow-table-math.d.ts +0 -5
- package/dist/scan/query-arrow-table-math.d.ts.map +0 -1
- package/dist/scan/query-arrow-table-math.js +0 -14
- package/dist/scan/query-arrow-table-math.js.map +0 -1
- package/dist/wkt/twkb/convert-geometry-to-twkb.cjs +0 -211
- package/dist/wkt/twkb/convert-geometry-to-twkb.cjs.map +0 -7
- package/dist/wkt/twkb/convert-geometry-to-twkb.d.ts +0 -6
- package/dist/wkt/twkb/convert-geometry-to-twkb.d.ts.map +0 -1
- package/dist/wkt/twkb/convert-geometry-to-twkb.js +0 -204
- package/dist/wkt/twkb/convert-geometry-to-twkb.js.map +0 -1
- package/dist/wkt/twkb/convert-twkb-to-geometry.cjs +0 -251
- package/dist/wkt/twkb/convert-twkb-to-geometry.cjs.map +0 -7
- package/dist/wkt/twkb/convert-twkb-to-geometry.d.ts +0 -10
- package/dist/wkt/twkb/convert-twkb-to-geometry.d.ts.map +0 -1
- package/dist/wkt/twkb/convert-twkb-to-geometry.js +0 -261
- package/dist/wkt/twkb/convert-twkb-to-geometry.js.map +0 -1
- package/dist/wkt/twkb/twkb-io.cjs +0 -102
- package/dist/wkt/twkb/twkb-io.cjs.map +0 -7
- package/dist/wkt/twkb/twkb-io.d.ts +0 -33
- package/dist/wkt/twkb/twkb-io.d.ts.map +0 -1
- package/dist/wkt/twkb/twkb-io.js +0 -78
- package/dist/wkt/twkb/twkb-io.js.map +0 -1
- package/dist/wkt/twkb/twkb-types.cjs +0 -40
- package/dist/wkt/twkb/twkb-types.cjs.map +0 -7
- package/dist/wkt/twkb/twkb-types.d.ts +0 -11
- package/dist/wkt/twkb/twkb-types.d.ts.map +0 -1
- package/dist/wkt/twkb/twkb-types.js +0 -15
- package/dist/wkt/twkb/twkb-types.js.map +0 -1
- package/src/scan/arrow-execution/portable-predicate-compatibility.ts +0 -27
- package/src/scan/arrow-execution/project-arrow-table.ts +0 -53
- package/src/scan/query-arrow-table-math.ts +0 -23
- package/src/wkt/twkb/convert-geometry-to-twkb.ts +0 -317
- package/src/wkt/twkb/convert-twkb-to-geometry.ts +0 -373
- package/src/wkt/twkb/twkb-io.ts +0 -85
- package/src/wkt/twkb/twkb-types.ts +0 -14
|
@@ -0,0 +1,367 @@
|
|
|
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 {ArrowBatchQuery, ArrowTableQuery} from '@math.gl/sql/query';
|
|
7
|
+
import {filterArrowTable, projectArrowTable} from './arrow-execution';
|
|
8
|
+
import type {SQLPredicate} from './sql-predicate-types';
|
|
9
|
+
import type {TableSelection} from '@loaders.gl/core/loader-utils';
|
|
10
|
+
|
|
11
|
+
import {planTableQuery, type TableQueryOptions} from './table-query';
|
|
12
|
+
import type {
|
|
13
|
+
RelationalAggregate,
|
|
14
|
+
RelationalExpression,
|
|
15
|
+
RelationalOrderKey
|
|
16
|
+
} from '@loaders.gl/core/loader-utils';
|
|
17
|
+
import {
|
|
18
|
+
explainTableQuery as explainColumnarTableQuery,
|
|
19
|
+
planRelationalQuery
|
|
20
|
+
} from '@loaders.gl/core/loader-utils';
|
|
21
|
+
import {ARROW_TABLE_QUERY_CAPABILITIES} from './table-query-capabilities';
|
|
22
|
+
import {validateSQLPredicate} from './sql-predicate-schema';
|
|
23
|
+
export {ARROW_TABLE_QUERY_CAPABILITIES};
|
|
24
|
+
|
|
25
|
+
/** A named child relation using the SQL module's property-oriented predicate AST. */
|
|
26
|
+
type ArrowChildQuery = Readonly<{
|
|
27
|
+
/** Logical source name resolved through the query's table map. */
|
|
28
|
+
source: string;
|
|
29
|
+
/** Optional query applied to the child relation before combining it. */
|
|
30
|
+
query?: TableQueryOptions;
|
|
31
|
+
}>;
|
|
32
|
+
|
|
33
|
+
/** Options for the lightweight in-memory Arrow query executor. */
|
|
34
|
+
export type ArrowQueryOptions = TableQueryOptions &
|
|
35
|
+
Readonly<{
|
|
36
|
+
/** Output source fields and aliases, in selection order. Cannot be combined with columns. */
|
|
37
|
+
select?: readonly TableSelection[];
|
|
38
|
+
/** Cancels the query before or during predicate evaluation. */
|
|
39
|
+
signal?: AbortSignal;
|
|
40
|
+
/** Computed columns evaluated before ordering and projection. */
|
|
41
|
+
expressions?: readonly RelationalExpression[];
|
|
42
|
+
/** Stable ordering applied before the global limit. */
|
|
43
|
+
orderBy?: readonly RelationalOrderKey[];
|
|
44
|
+
/** Grouping keys and aggregate definitions for relational output. */
|
|
45
|
+
groupBy?: readonly string[];
|
|
46
|
+
/** Aggregate definitions evaluated after filtering and expressions. */
|
|
47
|
+
aggregates?: readonly RelationalAggregate[];
|
|
48
|
+
/** Additional Arrow tables concatenated in source order before relational operators. */
|
|
49
|
+
union?: readonly ArrowChildQuery[];
|
|
50
|
+
/** Optional equi-join against a table supplied in `tables`. */
|
|
51
|
+
join?: Readonly<{
|
|
52
|
+
child: ArrowChildQuery;
|
|
53
|
+
left: string;
|
|
54
|
+
right: string;
|
|
55
|
+
/** Keep unmatched root rows with null child fields. Defaults to inner. */
|
|
56
|
+
type?: 'inner' | 'left';
|
|
57
|
+
}>;
|
|
58
|
+
/** Named in-memory child tables used by union and join operators. */
|
|
59
|
+
tables?: Readonly<Record<string, arrow.Table>>;
|
|
60
|
+
}>;
|
|
61
|
+
|
|
62
|
+
/** Explains an Arrow table query without materializing or scanning table rows. */
|
|
63
|
+
export function explainArrowTableQuery(
|
|
64
|
+
sourceTable: arrow.Table,
|
|
65
|
+
options: ArrowQueryOptions = {}
|
|
66
|
+
): import('./table-query').TableQueryExplain {
|
|
67
|
+
if (options.select !== undefined) {
|
|
68
|
+
if (options.columns !== undefined || hasRelationalOperators(options) || options.join) {
|
|
69
|
+
throw new Error('Arrow select cannot be combined with columns or relational operators.');
|
|
70
|
+
}
|
|
71
|
+
const projected = projectArrowTable(sourceTable, options.select);
|
|
72
|
+
const columns = [
|
|
73
|
+
...new Set(
|
|
74
|
+
options.select.map(selection =>
|
|
75
|
+
typeof selection === 'string' ? selection : selection.column
|
|
76
|
+
)
|
|
77
|
+
)
|
|
78
|
+
];
|
|
79
|
+
const {select, ...remainingOptions} = options;
|
|
80
|
+
const explanation = explainArrowTableQuery(sourceTable, {...remainingOptions, columns});
|
|
81
|
+
const outputColumns = Object.freeze(projected.schema.fields.map(field => field.name));
|
|
82
|
+
return Object.freeze({
|
|
83
|
+
...explanation,
|
|
84
|
+
outputColumns,
|
|
85
|
+
plan: Object.freeze(
|
|
86
|
+
explanation.plan.map(step =>
|
|
87
|
+
step.kind === 'project' ? Object.freeze({...step, columns: outputColumns, select}) : step
|
|
88
|
+
)
|
|
89
|
+
)
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (options.predicate) {
|
|
93
|
+
validateSQLPredicate(options.predicate);
|
|
94
|
+
}
|
|
95
|
+
return explainColumnarTableQuery(
|
|
96
|
+
sourceTable.schema.fields.map(field => field.name),
|
|
97
|
+
options,
|
|
98
|
+
ARROW_TABLE_QUERY_CAPABILITIES
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Executes a validated linear query after portable predicates have been applied. */
|
|
103
|
+
type ExecuteLinearArrowQuery = (
|
|
104
|
+
table: arrow.Table,
|
|
105
|
+
options: Readonly<{
|
|
106
|
+
select?: readonly TableSelection[];
|
|
107
|
+
where?: SQLPredicate;
|
|
108
|
+
limit?: number;
|
|
109
|
+
signal?: AbortSignal;
|
|
110
|
+
}>
|
|
111
|
+
) => arrow.Table;
|
|
112
|
+
|
|
113
|
+
/** Projects and limits Arrow rows for the lightweight scan sources. */
|
|
114
|
+
const executePortableArrowQuery: ExecuteLinearArrowQuery = (
|
|
115
|
+
table,
|
|
116
|
+
{select, where, limit, signal}
|
|
117
|
+
) => {
|
|
118
|
+
if (where) table = filterArrowTable(table, where, {limit, signal});
|
|
119
|
+
const projected = select ? projectArrowTable(table, select) : table;
|
|
120
|
+
return projected.numRows === 0 || limit === undefined ? projected : projected.slice(0, limit);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Executes a small, Arrow-native query over one in-memory table.
|
|
125
|
+
*
|
|
126
|
+
* Linear queries delegate to math.gl. Named source combinations retain loaders.gl planning.
|
|
127
|
+
*/
|
|
128
|
+
export function queryArrowTable(
|
|
129
|
+
sourceTable: arrow.Table,
|
|
130
|
+
options: ArrowQueryOptions = {},
|
|
131
|
+
execute?: (table: arrow.Table, query: ArrowTableQuery) => arrow.Table
|
|
132
|
+
): arrow.Table {
|
|
133
|
+
throwIfAborted(options.signal);
|
|
134
|
+
if (options.select !== undefined) {
|
|
135
|
+
if (options.columns !== undefined || hasRelationalOperators(options) || options.join) {
|
|
136
|
+
throw new Error('Arrow select cannot be combined with columns or relational operators.');
|
|
137
|
+
}
|
|
138
|
+
return (execute ?? executePortableArrowQuery)(sourceTable, {
|
|
139
|
+
where: options.predicate,
|
|
140
|
+
select: options.select,
|
|
141
|
+
limit: options.limit,
|
|
142
|
+
signal: options.signal
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (options.join || hasRelationalOperators(options)) {
|
|
147
|
+
if (options.join) {
|
|
148
|
+
validateArrowJoinQuery(sourceTable, options);
|
|
149
|
+
if (hasRelationalOperators(options)) {
|
|
150
|
+
throw new Error('Arrow joins cannot yet be combined with other relational operators.');
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (!execute) throw new Error('Relational queries require the SQL executor.');
|
|
154
|
+
return options.join
|
|
155
|
+
? queryArrowJoin(sourceTable, options, execute)
|
|
156
|
+
: queryArrowTableRelational(sourceTable, options, execute);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const plan = planTableQuery(
|
|
160
|
+
sourceTable.schema.fields.map(field => field.name),
|
|
161
|
+
options
|
|
162
|
+
);
|
|
163
|
+
const projection = plan.find(step => step.kind === 'project');
|
|
164
|
+
return (execute ?? executePortableArrowQuery)(sourceTable, {
|
|
165
|
+
where: options.predicate,
|
|
166
|
+
select: projection?.columns,
|
|
167
|
+
limit: options.limit,
|
|
168
|
+
signal: options.signal
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Resolves named inputs and delegates relational execution to math.gl. */
|
|
173
|
+
function queryArrowTableRelational(
|
|
174
|
+
sourceTable: arrow.Table,
|
|
175
|
+
options: ArrowQueryOptions,
|
|
176
|
+
execute: (table: arrow.Table, query: ArrowTableQuery) => arrow.Table
|
|
177
|
+
): arrow.Table {
|
|
178
|
+
const sourceColumns = sourceTable.schema.fields.map(field => field.name);
|
|
179
|
+
const grouped = options.groupBy?.length || options.aggregates?.length;
|
|
180
|
+
const outputColumns =
|
|
181
|
+
options.columns ??
|
|
182
|
+
(grouped
|
|
183
|
+
? [...(options.groupBy ?? []), ...(options.aggregates ?? []).map(aggregate => aggregate.name)]
|
|
184
|
+
: sourceColumns);
|
|
185
|
+
const plan = planRelationalQuery(sourceColumns, {...options, columns: outputColumns} as never);
|
|
186
|
+
const scan = plan.tablePlan.find(step => step.kind === 'scan')!;
|
|
187
|
+
let table = execute(sourceTable, {
|
|
188
|
+
where: options.predicate,
|
|
189
|
+
select: scan.columns,
|
|
190
|
+
signal: options.signal
|
|
191
|
+
});
|
|
192
|
+
if (options.union?.length) {
|
|
193
|
+
const childPlan = planRelationalQuery(sourceColumns, {
|
|
194
|
+
...options,
|
|
195
|
+
columns: outputColumns,
|
|
196
|
+
predicate: undefined
|
|
197
|
+
} as never);
|
|
198
|
+
const columns = childPlan.tablePlan.find(step => step.kind === 'scan')!.columns;
|
|
199
|
+
const union = options.union.map(child => {
|
|
200
|
+
const input = options.tables?.[child.source];
|
|
201
|
+
if (!input) throw new Error('Arrow union source not found: ' + child.source);
|
|
202
|
+
return queryArrowTable(input, {...child.query, columns, signal: options.signal}, execute);
|
|
203
|
+
});
|
|
204
|
+
table = execute(projectArrowTable(table, columns), {union, signal: options.signal});
|
|
205
|
+
}
|
|
206
|
+
for (const {name, expression} of options.expressions ?? []) {
|
|
207
|
+
const select: ArrowBatchQuery['select'] = [
|
|
208
|
+
...table.schema.fields.map(field => field.name),
|
|
209
|
+
expression.op === 'column'
|
|
210
|
+
? {column: expression.column, as: name}
|
|
211
|
+
: {expression: toArrowExpression(expression), as: name}
|
|
212
|
+
];
|
|
213
|
+
table = execute(table, {select, signal: options.signal});
|
|
214
|
+
}
|
|
215
|
+
return execute(table, {
|
|
216
|
+
select: outputColumns,
|
|
217
|
+
groupBy: options.groupBy,
|
|
218
|
+
aggregates: Object.fromEntries(
|
|
219
|
+
(options.aggregates ?? []).map(aggregate => {
|
|
220
|
+
const expression =
|
|
221
|
+
aggregate.column === undefined ? undefined : identifier(aggregate.column);
|
|
222
|
+
if (aggregate.function === 'count')
|
|
223
|
+
return [aggregate.name, {operation: 'COUNT', expression}];
|
|
224
|
+
const operations = {sum: 'SUM', min: 'MIN', max: 'MAX', avg: 'AVG'} as const;
|
|
225
|
+
return [
|
|
226
|
+
aggregate.name,
|
|
227
|
+
{operation: operations[aggregate.function], expression: expression!}
|
|
228
|
+
];
|
|
229
|
+
})
|
|
230
|
+
),
|
|
231
|
+
orderBy: options.orderBy,
|
|
232
|
+
limit: options.limit,
|
|
233
|
+
signal: options.signal
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** References an Arrow field by name without parsing SQL. */
|
|
238
|
+
function identifier(name: string): {type: 'Identifier'; name: string} {
|
|
239
|
+
return {type: 'Identifier', name};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Translates portable arithmetic to the upstream expression AST. */
|
|
243
|
+
function toArrowExpression(
|
|
244
|
+
expression: Exclude<RelationalExpression['expression'], {op: 'column'}>
|
|
245
|
+
) {
|
|
246
|
+
if (expression.op === 'literal') return literal(expression.value);
|
|
247
|
+
const operators = {add: '+', subtract: '-', multiply: '*', divide: '/'} as const;
|
|
248
|
+
const binary = {
|
|
249
|
+
type: 'BinaryExpression' as const,
|
|
250
|
+
operator: operators[expression.op],
|
|
251
|
+
left: identifier(expression.left),
|
|
252
|
+
right: identifier(expression.right)
|
|
253
|
+
};
|
|
254
|
+
if (expression.op !== 'divide') return binary;
|
|
255
|
+
return {
|
|
256
|
+
type: 'ConditionalExpression' as const,
|
|
257
|
+
test: {...binary, operator: '===', left: binary.right, right: literal(0)},
|
|
258
|
+
consequent: literal(null),
|
|
259
|
+
alternate: binary
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Creates a scalar AST literal for the Arrow compiler. */
|
|
264
|
+
function literal(value: boolean | number | string | null) {
|
|
265
|
+
return {type: 'Literal' as const, value};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Returns whether the query uses any in-memory relational operator. */
|
|
269
|
+
function hasRelationalOperators(options: ArrowQueryOptions): boolean {
|
|
270
|
+
return Boolean(
|
|
271
|
+
options.expressions?.length ||
|
|
272
|
+
options.orderBy?.length ||
|
|
273
|
+
options.groupBy?.length ||
|
|
274
|
+
options.aggregates?.length ||
|
|
275
|
+
options.union?.length
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Resolves child inputs and delegates the residual equijoin to math.gl. */
|
|
280
|
+
function queryArrowJoin(
|
|
281
|
+
sourceTable: arrow.Table,
|
|
282
|
+
options: ArrowQueryOptions,
|
|
283
|
+
executeRelational: (table: arrow.Table, query: ArrowTableQuery) => arrow.Table
|
|
284
|
+
): arrow.Table {
|
|
285
|
+
const join = options.join!;
|
|
286
|
+
const childTable = options.tables?.[join.child.source];
|
|
287
|
+
if (!childTable) throw new Error(`Arrow join source not found: ${join.child.source}`);
|
|
288
|
+
const left = executeRelational(sourceTable, {
|
|
289
|
+
where: options.predicate,
|
|
290
|
+
signal: options.signal
|
|
291
|
+
});
|
|
292
|
+
const right = executeRelational(childTable, {
|
|
293
|
+
where: join.child.query?.predicate,
|
|
294
|
+
select: join.child.query?.columns,
|
|
295
|
+
limit: join.child.query?.limit,
|
|
296
|
+
signal: options.signal
|
|
297
|
+
});
|
|
298
|
+
return executeRelational(left, {
|
|
299
|
+
join: {
|
|
300
|
+
table: right,
|
|
301
|
+
type: join.type,
|
|
302
|
+
left: join.left,
|
|
303
|
+
right: join.right,
|
|
304
|
+
rightPrefix: `${join.child.source}.`
|
|
305
|
+
},
|
|
306
|
+
select: options.columns,
|
|
307
|
+
limit: options.limit,
|
|
308
|
+
signal: options.signal
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Validates root and child projections before dispatching to the join executor. */
|
|
313
|
+
function validateArrowJoinQuery(sourceTable: arrow.Table, options: ArrowQueryOptions): void {
|
|
314
|
+
const join = options.join;
|
|
315
|
+
if (!join) return;
|
|
316
|
+
const sourceColumnNames = sourceTable.schema.fields.map(field => field.name);
|
|
317
|
+
const childTable = options.tables?.[join.child.source];
|
|
318
|
+
if (!childTable) throw new Error(`Arrow join source not found: ${join.child.source}`);
|
|
319
|
+
const childColumnNames = childTable.schema.fields.map(field => field.name);
|
|
320
|
+
if (!sourceColumnNames.includes(join.left)) {
|
|
321
|
+
throw new Error(`Arrow join column not found: ${join.left}`);
|
|
322
|
+
}
|
|
323
|
+
if (!childColumnNames.includes(join.right)) {
|
|
324
|
+
throw new Error(`Arrow join column not found: ${join.right}`);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const childPrefix = `${join.child.source}.`;
|
|
328
|
+
const rootColumns = options.columns?.filter(column => !column.startsWith(childPrefix));
|
|
329
|
+
planTableQuery(sourceColumnNames, {
|
|
330
|
+
predicate: options.predicate,
|
|
331
|
+
columns: rootColumns?.length ? rootColumns : undefined,
|
|
332
|
+
limit: options.limit,
|
|
333
|
+
signal: options.signal
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
const childQuery = join.child.query || {};
|
|
337
|
+
planTableQuery(childColumnNames, childQuery);
|
|
338
|
+
const childOutputColumns = childQuery.columns || childColumnNames;
|
|
339
|
+
if (!childOutputColumns.includes(join.right)) {
|
|
340
|
+
throw new Error(`Arrow join child projection must include: ${join.right}`);
|
|
341
|
+
}
|
|
342
|
+
const projectedChildColumns =
|
|
343
|
+
options.columns?.filter(column => column.startsWith(childPrefix)) || [];
|
|
344
|
+
const seenChildColumns = new Set<string>();
|
|
345
|
+
for (const projectedColumn of projectedChildColumns) {
|
|
346
|
+
const childColumn = projectedColumn.slice(childPrefix.length);
|
|
347
|
+
if (!childColumnNames.includes(childColumn)) {
|
|
348
|
+
throw new Error(`Arrow join column not found: ${projectedColumn}`);
|
|
349
|
+
}
|
|
350
|
+
if (seenChildColumns.has(childColumn)) {
|
|
351
|
+
throw new Error(`Arrow query column was selected more than once: ${projectedColumn}`);
|
|
352
|
+
}
|
|
353
|
+
seenChildColumns.add(childColumn);
|
|
354
|
+
if (!childOutputColumns.includes(childColumn)) {
|
|
355
|
+
throw new Error(`Arrow join child projection does not include: ${childColumn}`);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** Throws a standard cancellation error if a caller has aborted the query. */
|
|
361
|
+
function throwIfAborted(signal: AbortSignal | undefined): void {
|
|
362
|
+
if (signal?.aborted) {
|
|
363
|
+
const error = new Error('Arrow query was aborted.');
|
|
364
|
+
error.name = 'AbortError';
|
|
365
|
+
throw error;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
@@ -2,12 +2,9 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
import * as arrow from 'apache-arrow';
|
|
6
|
-
import {createRecordBatchWithMetadata, getRecordBatchMetadata} from '@math.gl/core/arrow';
|
|
5
|
+
import type * as arrow from 'apache-arrow';
|
|
7
6
|
import {queryArrowTableInBatches as executeArrowQueryInBatches} from '@math.gl/sql/query';
|
|
8
7
|
import {validateTableQueryLimit} from '@loaders.gl/core/loader-utils';
|
|
9
|
-
import {filterArrowTable} from './arrow-execution/filter-arrow-table';
|
|
10
|
-
import {needsPortablePredicateCompatibility} from './arrow-execution/portable-predicate-compatibility';
|
|
11
8
|
import type {ArrowQueryOptions} from './query-arrow-table';
|
|
12
9
|
|
|
13
10
|
/**
|
|
@@ -38,45 +35,19 @@ export async function* queryArrowTableInBatches(
|
|
|
38
35
|
try {
|
|
39
36
|
const first = await iterator.next();
|
|
40
37
|
if (first.done) return;
|
|
41
|
-
|
|
42
|
-
const compatiblePredicate =
|
|
43
|
-
predicate && needsPortablePredicateCompatibility(first.value.schema, predicate)
|
|
44
|
-
? predicate
|
|
45
|
-
: undefined;
|
|
46
|
-
/** Replays the first batch and adapts predicate shapes RC.2 cannot bind. */
|
|
38
|
+
/** Replays the first batch after reading its schema. */
|
|
47
39
|
async function* readBatches(): AsyncIterable<arrow.RecordBatch> {
|
|
48
|
-
let remaining = options.limit;
|
|
49
40
|
for (
|
|
50
41
|
let next: IteratorResult<arrow.RecordBatch> = first;
|
|
51
42
|
!next.done;
|
|
52
43
|
next = await iterator.next()
|
|
53
44
|
) {
|
|
54
|
-
|
|
55
|
-
if (!compatiblePredicate) {
|
|
56
|
-
yield batch;
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
const filtered = filterArrowTable(
|
|
60
|
-
new arrow.Table(batch.schema, [batch]),
|
|
61
|
-
compatiblePredicate,
|
|
62
|
-
{
|
|
63
|
-
limit: remaining,
|
|
64
|
-
signal: options.signal
|
|
65
|
-
}
|
|
66
|
-
);
|
|
67
|
-
if (filtered.numRows) yield* filtered.batches;
|
|
68
|
-
else
|
|
69
|
-
yield createRecordBatchWithMetadata(
|
|
70
|
-
batch.schema,
|
|
71
|
-
new arrow.RecordBatch(batch.schema, undefined).data,
|
|
72
|
-
getRecordBatchMetadata(batch)
|
|
73
|
-
);
|
|
74
|
-
if (remaining !== undefined) remaining -= filtered.numRows;
|
|
45
|
+
yield next.value;
|
|
75
46
|
}
|
|
76
47
|
}
|
|
77
48
|
yield* executeArrowQueryInBatches(readBatches(), first.value.schema, {
|
|
78
49
|
select: options.select ?? options.columns,
|
|
79
|
-
where:
|
|
50
|
+
where: options.predicate,
|
|
80
51
|
limit: options.limit,
|
|
81
52
|
signal: options.signal,
|
|
82
53
|
emitEmptyBatches: true
|