@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
|
@@ -2,633 +2,17 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
5
|
+
import type {Table} from 'apache-arrow';
|
|
6
|
+
import {queryArrowTable as executeArrowQuery} from '@math.gl/sql/query';
|
|
7
7
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from './
|
|
11
|
-
import type {SQLPredicate} from './sql-predicate-types';
|
|
12
|
-
import type {TableSelection} from '@loaders.gl/core/loader-utils';
|
|
13
|
-
import {convertRowsToArrowTable} from './rows-to-arrow';
|
|
8
|
+
queryArrowTable as planAndQueryArrowTable,
|
|
9
|
+
type ArrowQueryOptions
|
|
10
|
+
} from './plan-arrow-table-query';
|
|
14
11
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
RelationalAggregate,
|
|
18
|
-
RelationalExpression,
|
|
19
|
-
RelationalOrderKey
|
|
20
|
-
} from '@loaders.gl/core/loader-utils';
|
|
21
|
-
import {
|
|
22
|
-
explainTableQuery as explainColumnarTableQuery,
|
|
23
|
-
planRelationalQuery
|
|
24
|
-
} from '@loaders.gl/core/loader-utils';
|
|
25
|
-
import {ARROW_TABLE_QUERY_CAPABILITIES} from './table-query-capabilities';
|
|
26
|
-
import {validateSQLPredicate} from './sql-predicate-schema';
|
|
27
|
-
export {ARROW_TABLE_QUERY_CAPABILITIES};
|
|
28
|
-
|
|
29
|
-
/** A named child relation using the SQL module's property-oriented predicate AST. */
|
|
30
|
-
type ArrowChildQuery = Readonly<{
|
|
31
|
-
/** Logical source name resolved through the query's table map. */
|
|
32
|
-
source: string;
|
|
33
|
-
/** Optional query applied to the child relation before combining it. */
|
|
34
|
-
query?: TableQueryOptions;
|
|
35
|
-
}>;
|
|
36
|
-
|
|
37
|
-
/** Options for the lightweight in-memory Arrow query executor. */
|
|
38
|
-
export type ArrowQueryOptions = TableQueryOptions &
|
|
39
|
-
Readonly<{
|
|
40
|
-
/** Output source fields and aliases, in selection order. Cannot be combined with columns. */
|
|
41
|
-
select?: readonly TableSelection[];
|
|
42
|
-
/** Cancels the query before or during predicate evaluation. */
|
|
43
|
-
signal?: AbortSignal;
|
|
44
|
-
/** Computed columns evaluated before ordering and projection. */
|
|
45
|
-
expressions?: readonly RelationalExpression[];
|
|
46
|
-
/** Stable ordering applied before the global limit. */
|
|
47
|
-
orderBy?: readonly RelationalOrderKey[];
|
|
48
|
-
/** Grouping keys and aggregate definitions for relational output. */
|
|
49
|
-
groupBy?: readonly string[];
|
|
50
|
-
/** Aggregate definitions evaluated after filtering and expressions. */
|
|
51
|
-
aggregates?: readonly RelationalAggregate[];
|
|
52
|
-
/** Additional Arrow tables concatenated in source order before relational operators. */
|
|
53
|
-
union?: readonly ArrowChildQuery[];
|
|
54
|
-
/** Optional equi-join against a table supplied in `tables`. */
|
|
55
|
-
join?: Readonly<{child: ArrowChildQuery; left: string; right: string}>;
|
|
56
|
-
/** Named in-memory child tables used by union and join operators. */
|
|
57
|
-
tables?: Readonly<Record<string, arrow.Table>>;
|
|
58
|
-
}>;
|
|
59
|
-
|
|
60
|
-
/** Explains an Arrow table query without materializing or scanning table rows. */
|
|
61
|
-
export function explainArrowTableQuery(
|
|
62
|
-
sourceTable: arrow.Table,
|
|
63
|
-
options: ArrowQueryOptions = {}
|
|
64
|
-
): import('./table-query').TableQueryExplain {
|
|
65
|
-
if (options.select !== undefined) {
|
|
66
|
-
if (options.columns !== undefined || hasRelationalOperators(options) || options.join) {
|
|
67
|
-
throw new Error('Arrow select cannot be combined with columns or relational operators.');
|
|
68
|
-
}
|
|
69
|
-
const projected = projectArrowTable(sourceTable, options.select);
|
|
70
|
-
const columns = [
|
|
71
|
-
...new Set(
|
|
72
|
-
options.select.map(selection =>
|
|
73
|
-
typeof selection === 'string' ? selection : selection.column
|
|
74
|
-
)
|
|
75
|
-
)
|
|
76
|
-
];
|
|
77
|
-
const {select, ...remainingOptions} = options;
|
|
78
|
-
const explanation = explainArrowTableQuery(sourceTable, {...remainingOptions, columns});
|
|
79
|
-
const outputColumns = Object.freeze(projected.schema.fields.map(field => field.name));
|
|
80
|
-
return Object.freeze({
|
|
81
|
-
...explanation,
|
|
82
|
-
outputColumns,
|
|
83
|
-
plan: Object.freeze(
|
|
84
|
-
explanation.plan.map(step =>
|
|
85
|
-
step.kind === 'project' ? Object.freeze({...step, columns: outputColumns, select}) : step
|
|
86
|
-
)
|
|
87
|
-
)
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
if (options.predicate) {
|
|
91
|
-
validateSQLPredicate(options.predicate);
|
|
92
|
-
}
|
|
93
|
-
return explainColumnarTableQuery(
|
|
94
|
-
sourceTable.schema.fields.map(field => field.name),
|
|
95
|
-
options,
|
|
96
|
-
ARROW_TABLE_QUERY_CAPABILITIES
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/** Executes a validated linear query after portable predicates have been applied. */
|
|
101
|
-
type ExecuteLinearArrowQuery = (
|
|
102
|
-
table: arrow.Table,
|
|
103
|
-
options: Readonly<{
|
|
104
|
-
select?: readonly string[];
|
|
105
|
-
where?: SQLPredicate;
|
|
106
|
-
limit?: number;
|
|
107
|
-
signal?: AbortSignal;
|
|
108
|
-
}>
|
|
109
|
-
) => arrow.Table;
|
|
110
|
-
|
|
111
|
-
/** Projects and limits Arrow rows for the lightweight scan sources. */
|
|
112
|
-
const executePortableArrowQuery: ExecuteLinearArrowQuery = (
|
|
113
|
-
table,
|
|
114
|
-
{select, where, limit, signal}
|
|
115
|
-
) => {
|
|
116
|
-
if (where) table = filterArrowTable(table, where, {limit, signal});
|
|
117
|
-
const projected = select ? projectArrowTable(table, select) : table;
|
|
118
|
-
return projected.numRows === 0 || limit === undefined ? projected : projected.slice(0, limit);
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Executes a small, Arrow-native query over one in-memory table.
|
|
123
|
-
*
|
|
124
|
-
* Linear queries delegate to math.gl. Named source combinations retain loaders.gl planning.
|
|
125
|
-
*/
|
|
126
|
-
export function queryArrowTable(
|
|
127
|
-
sourceTable: arrow.Table,
|
|
128
|
-
options: ArrowQueryOptions = {},
|
|
129
|
-
executeLinear: ExecuteLinearArrowQuery = executePortableArrowQuery
|
|
130
|
-
): arrow.Table {
|
|
131
|
-
throwIfAborted(options.signal);
|
|
132
|
-
if (options.select !== undefined) {
|
|
133
|
-
if (options.columns !== undefined || hasRelationalOperators(options) || options.join) {
|
|
134
|
-
throw new Error('Arrow select cannot be combined with columns or relational operators.');
|
|
135
|
-
}
|
|
136
|
-
// Validate aliases before evaluating any predicate, then apply them after filtering.
|
|
137
|
-
projectArrowTable(sourceTable, options.select);
|
|
138
|
-
const columns = [
|
|
139
|
-
...new Set(
|
|
140
|
-
options.select.map(selection =>
|
|
141
|
-
typeof selection === 'string' ? selection : selection.column
|
|
142
|
-
)
|
|
143
|
-
)
|
|
144
|
-
];
|
|
145
|
-
const {select, ...remainingOptions} = options;
|
|
146
|
-
const result = queryArrowTable(sourceTable, {...remainingOptions, columns}, executeLinear);
|
|
147
|
-
return projectArrowTable(result, select);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (options.join) {
|
|
151
|
-
validateArrowJoinQuery(sourceTable, options);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (options.join && hasRelationalOperatorsExceptJoin(options)) {
|
|
155
|
-
throw new Error('Arrow joins cannot yet be combined with other relational operators.');
|
|
156
|
-
}
|
|
157
|
-
if (options.join && !hasRelationalOperatorsExceptJoin(options)) {
|
|
158
|
-
return queryArrowJoin(sourceTable, options);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (hasRelationalOperators(options)) {
|
|
162
|
-
return queryArrowTableRelational(sourceTable, options);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const plan = planTableQuery(
|
|
166
|
-
sourceTable.schema.fields.map(field => field.name),
|
|
167
|
-
options
|
|
168
|
-
);
|
|
169
|
-
const projection = plan.find(step => step.kind === 'project');
|
|
170
|
-
return executeLinear(sourceTable, {
|
|
171
|
-
where: options.predicate,
|
|
172
|
-
select: projection?.columns,
|
|
173
|
-
limit: options.limit,
|
|
174
|
-
signal: options.signal
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** Executes the relational subset that can be evaluated against one in-memory Arrow table. */
|
|
179
|
-
function queryArrowTableRelational(
|
|
180
|
-
sourceTable: arrow.Table,
|
|
181
|
-
options: ArrowQueryOptions
|
|
182
|
-
): arrow.Table {
|
|
183
|
-
const sourceData = sourceTable;
|
|
184
|
-
const sourceColumnNames = sourceData.schema.fields.map(field => field.name);
|
|
185
|
-
// The shared planner is predicate-generic; this executor supplies the narrower SQL AST.
|
|
186
|
-
const relationalPlan = planRelationalQuery(sourceColumnNames, options as never);
|
|
187
|
-
const scanStep = relationalPlan.tablePlan.find(step => step.kind === 'scan');
|
|
188
|
-
if (!scanStep || scanStep.kind !== 'scan') {
|
|
189
|
-
throw new Error('Arrow relational query plan is missing a scan step.');
|
|
190
|
-
}
|
|
191
|
-
const scannedTable = sourceData.select([...scanStep.columns]);
|
|
192
|
-
const filteredTable = queryArrowTable(scannedTable, {
|
|
193
|
-
predicate: options.predicate,
|
|
194
|
-
columns: scanStep.columns,
|
|
195
|
-
signal: options.signal
|
|
196
|
-
});
|
|
197
|
-
let rows = arrowTableToRows(filteredTable);
|
|
198
|
-
|
|
199
|
-
for (const child of options.union || []) {
|
|
200
|
-
const childTable = options.tables?.[child.source];
|
|
201
|
-
if (!childTable) throw new Error(`Arrow union source not found: ${child.source}`);
|
|
202
|
-
const childResult = queryArrowTable(childTable, {
|
|
203
|
-
...(child.query || {}),
|
|
204
|
-
columns: getUnionChildColumns(sourceColumnNames, options),
|
|
205
|
-
signal: options.signal
|
|
206
|
-
});
|
|
207
|
-
rows = rows.concat(arrowTableToRows(childResult));
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
for (const expression of options.expressions || []) {
|
|
211
|
-
for (const row of rows) {
|
|
212
|
-
row[expression.name] = evaluateRelationalExpression(expression, row);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
if (options.groupBy?.length || options.aggregates?.length) {
|
|
217
|
-
rows = aggregateRows(rows, options.groupBy || [], options.aggregates || []);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
if (options.orderBy?.length) {
|
|
221
|
-
const orderBy = options.orderBy;
|
|
222
|
-
rows = rows
|
|
223
|
-
.map((row, index) => ({row, index}))
|
|
224
|
-
.sort((left, right) => compareRows(left.row, right.row, orderBy) || left.index - right.index)
|
|
225
|
-
.map(entry => entry.row);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const outputColumns =
|
|
229
|
-
options.columns ||
|
|
230
|
-
(options.groupBy?.length || options.aggregates?.length
|
|
231
|
-
? [...(options.groupBy || []), ...(options.aggregates || []).map(aggregate => aggregate.name)]
|
|
232
|
-
: sourceColumnNames);
|
|
233
|
-
const outputRows = rows.map(row => {
|
|
234
|
-
const output: Record<string, unknown> = {};
|
|
235
|
-
for (const columnName of outputColumns) {
|
|
236
|
-
if (!(columnName in row)) {
|
|
237
|
-
throw new Error(`Arrow relational query column not found: ${columnName}`);
|
|
238
|
-
}
|
|
239
|
-
output[columnName] = row[columnName];
|
|
240
|
-
}
|
|
241
|
-
return output;
|
|
242
|
-
});
|
|
243
|
-
const limit = options.limit === undefined ? outputRows.length : options.limit;
|
|
244
|
-
const limitedRows = outputRows.slice(0, limit);
|
|
245
|
-
return limitedRows.length
|
|
246
|
-
? convertRowsToArrowTable(limitedRows)
|
|
247
|
-
: createEmptyRelationalResult(sourceData, outputColumns, options);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/** Derives the source columns a UNION child needs without inheriting base-only predicate columns. */
|
|
251
|
-
function getUnionChildColumns(
|
|
252
|
-
sourceColumnNames: readonly string[],
|
|
253
|
-
options: ArrowQueryOptions
|
|
254
|
-
): string[] {
|
|
255
|
-
const expressionNames = new Set((options.expressions || []).map(expression => expression.name));
|
|
256
|
-
const aggregateNames = new Set((options.aggregates || []).map(aggregate => aggregate.name));
|
|
257
|
-
const outputColumns =
|
|
258
|
-
options.columns ||
|
|
259
|
-
(options.groupBy?.length || options.aggregates?.length
|
|
260
|
-
? [...(options.groupBy || []), ...(options.aggregates || []).map(aggregate => aggregate.name)]
|
|
261
|
-
: sourceColumnNames);
|
|
262
|
-
const requiredColumns = new Set(
|
|
263
|
-
outputColumns.filter(column => !expressionNames.has(column) && !aggregateNames.has(column))
|
|
264
|
-
);
|
|
265
|
-
|
|
266
|
-
for (const expression of options.expressions || []) {
|
|
267
|
-
const definition = expression.expression;
|
|
268
|
-
if (definition.op === 'column') requiredColumns.add(definition.column);
|
|
269
|
-
if ('left' in definition) {
|
|
270
|
-
requiredColumns.add(definition.left);
|
|
271
|
-
requiredColumns.add(definition.right);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
for (const key of options.orderBy || []) {
|
|
275
|
-
if (!expressionNames.has(key.column) && !aggregateNames.has(key.column)) {
|
|
276
|
-
requiredColumns.add(key.column);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
for (const column of options.groupBy || []) requiredColumns.add(column);
|
|
280
|
-
for (const aggregate of options.aggregates || []) {
|
|
281
|
-
if (aggregate.column) requiredColumns.add(aggregate.column);
|
|
282
|
-
}
|
|
283
|
-
return sourceColumnNames.filter(column => requiredColumns.has(column));
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/** Returns whether the query uses any in-memory relational operator. */
|
|
287
|
-
function hasRelationalOperators(options: ArrowQueryOptions): boolean {
|
|
288
|
-
return Boolean(
|
|
289
|
-
options.expressions?.length ||
|
|
290
|
-
options.orderBy?.length ||
|
|
291
|
-
options.groupBy?.length ||
|
|
292
|
-
options.aggregates?.length ||
|
|
293
|
-
options.union?.length ||
|
|
294
|
-
options.join
|
|
295
|
-
);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/** Returns whether a join is the only relational operator requested. */
|
|
299
|
-
function hasRelationalOperatorsExceptJoin(options: ArrowQueryOptions): boolean {
|
|
300
|
-
return Boolean(
|
|
301
|
-
options.expressions?.length ||
|
|
302
|
-
options.orderBy?.length ||
|
|
303
|
-
options.groupBy?.length ||
|
|
304
|
-
options.aggregates?.length ||
|
|
305
|
-
options.union?.length
|
|
306
|
-
);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/** Executes a bounded nested-loop equi-join for small in-memory Arrow proof-of-concept tables. */
|
|
310
|
-
function queryArrowJoin(sourceTable: arrow.Table, options: ArrowQueryOptions): arrow.Table {
|
|
311
|
-
const join = options.join;
|
|
312
|
-
if (!join) throw new Error('Arrow join query is missing a join definition.');
|
|
313
|
-
const childTable = options.tables?.[join.child.source];
|
|
314
|
-
if (!childTable) throw new Error(`Arrow join source not found: ${join.child.source}`);
|
|
315
|
-
const leftResult = queryArrowTable(sourceTable, {
|
|
316
|
-
predicate: options.predicate,
|
|
317
|
-
columns: undefined,
|
|
318
|
-
signal: options.signal
|
|
319
|
-
});
|
|
320
|
-
const rightResult = queryArrowTable(childTable, {
|
|
321
|
-
...(join.child.query || {}),
|
|
322
|
-
signal: options.signal
|
|
323
|
-
});
|
|
324
|
-
const leftRows = arrowTableToRows(leftResult);
|
|
325
|
-
const rightRows = arrowTableToRows(rightResult);
|
|
326
|
-
const rightPrefix = `${join.child.source}.`;
|
|
327
|
-
const defaultOutputColumns = [
|
|
328
|
-
...sourceTable.schema.fields.map(field => field.name),
|
|
329
|
-
...rightResult.schema.fields.map(field => `${rightPrefix}${field.name}`)
|
|
330
|
-
];
|
|
331
|
-
const rows: Record<string, unknown>[] = [];
|
|
332
|
-
for (const leftRow of leftRows) {
|
|
333
|
-
for (const rightRow of rightRows) {
|
|
334
|
-
if (
|
|
335
|
-
leftRow[join.left] !== null &&
|
|
336
|
-
leftRow[join.left] !== undefined &&
|
|
337
|
-
rightRow[join.right] !== null &&
|
|
338
|
-
rightRow[join.right] !== undefined &&
|
|
339
|
-
compareSortValues(leftRow[join.left], rightRow[join.right]) === 0
|
|
340
|
-
) {
|
|
341
|
-
rows.push({
|
|
342
|
-
...leftRow,
|
|
343
|
-
...Object.fromEntries(
|
|
344
|
-
Object.entries(rightRow).map(([key, value]) => [`${rightPrefix}${key}`, value])
|
|
345
|
-
)
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
const outputColumns = options.columns || defaultOutputColumns;
|
|
351
|
-
const outputRows = rows.map(row =>
|
|
352
|
-
Object.fromEntries(outputColumns.map(column => [column, row[column]]))
|
|
353
|
-
);
|
|
354
|
-
const limitedRows = outputRows.slice(0, options.limit);
|
|
355
|
-
return limitedRows.length
|
|
356
|
-
? convertRowsToArrowTable(limitedRows)
|
|
357
|
-
: createEmptyRelationalResult(
|
|
358
|
-
sourceTable,
|
|
359
|
-
outputColumns,
|
|
360
|
-
options,
|
|
361
|
-
new Map(
|
|
362
|
-
rightResult.schema.fields.map(field => [
|
|
363
|
-
`${rightPrefix}${field.name}`,
|
|
364
|
-
new arrow.Field(
|
|
365
|
-
`${rightPrefix}${field.name}`,
|
|
366
|
-
field.type,
|
|
367
|
-
field.nullable,
|
|
368
|
-
field.metadata
|
|
369
|
-
)
|
|
370
|
-
])
|
|
371
|
-
)
|
|
372
|
-
);
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/** Validates root and child projections before dispatching to the join executor. */
|
|
376
|
-
function validateArrowJoinQuery(sourceTable: arrow.Table, options: ArrowQueryOptions): void {
|
|
377
|
-
const join = options.join;
|
|
378
|
-
if (!join) return;
|
|
379
|
-
const sourceColumnNames = sourceTable.schema.fields.map(field => field.name);
|
|
380
|
-
const childTable = options.tables?.[join.child.source];
|
|
381
|
-
if (!childTable) throw new Error(`Arrow join source not found: ${join.child.source}`);
|
|
382
|
-
const childColumnNames = childTable.schema.fields.map(field => field.name);
|
|
383
|
-
if (!sourceColumnNames.includes(join.left)) {
|
|
384
|
-
throw new Error(`Arrow join column not found: ${join.left}`);
|
|
385
|
-
}
|
|
386
|
-
if (!childColumnNames.includes(join.right)) {
|
|
387
|
-
throw new Error(`Arrow join column not found: ${join.right}`);
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
const childPrefix = `${join.child.source}.`;
|
|
391
|
-
const rootColumns = options.columns?.filter(column => !column.startsWith(childPrefix));
|
|
392
|
-
planTableQuery(sourceColumnNames, {
|
|
393
|
-
predicate: options.predicate,
|
|
394
|
-
columns: rootColumns?.length ? rootColumns : undefined,
|
|
395
|
-
limit: options.limit,
|
|
396
|
-
signal: options.signal
|
|
397
|
-
});
|
|
398
|
-
|
|
399
|
-
const childQuery = join.child.query || {};
|
|
400
|
-
planTableQuery(childColumnNames, childQuery);
|
|
401
|
-
const childOutputColumns = childQuery.columns || childColumnNames;
|
|
402
|
-
if (!childOutputColumns.includes(join.right)) {
|
|
403
|
-
throw new Error(`Arrow join child projection must include: ${join.right}`);
|
|
404
|
-
}
|
|
405
|
-
const projectedChildColumns =
|
|
406
|
-
options.columns?.filter(column => column.startsWith(childPrefix)) || [];
|
|
407
|
-
const seenChildColumns = new Set<string>();
|
|
408
|
-
for (const projectedColumn of projectedChildColumns) {
|
|
409
|
-
const childColumn = projectedColumn.slice(childPrefix.length);
|
|
410
|
-
if (!childColumnNames.includes(childColumn)) {
|
|
411
|
-
throw new Error(`Arrow join column not found: ${projectedColumn}`);
|
|
412
|
-
}
|
|
413
|
-
if (seenChildColumns.has(childColumn)) {
|
|
414
|
-
throw new Error(`Arrow query column was selected more than once: ${projectedColumn}`);
|
|
415
|
-
}
|
|
416
|
-
seenChildColumns.add(childColumn);
|
|
417
|
-
if (!childOutputColumns.includes(childColumn)) {
|
|
418
|
-
throw new Error(`Arrow join child projection does not include: ${childColumn}`);
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
/** Materializes one Arrow table into row objects for the relational proof of concept. */
|
|
424
|
-
function arrowTableToRows(data: arrow.Table): Record<string, unknown>[] {
|
|
425
|
-
const columns = data.schema.fields.map(field => ({
|
|
426
|
-
name: field.name,
|
|
427
|
-
vector: data.getChild(field.name)
|
|
428
|
-
}));
|
|
429
|
-
return Array.from({length: data.numRows}, (_, rowIndex) => {
|
|
430
|
-
const row: Record<string, unknown> = {};
|
|
431
|
-
for (const column of columns) {
|
|
432
|
-
if (!column.vector) throw new Error(`Arrow relational query could not read ${column.name}`);
|
|
433
|
-
row[column.name] = column.vector.get(rowIndex);
|
|
434
|
-
}
|
|
435
|
-
return row;
|
|
436
|
-
});
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
/** Evaluates one portable scalar expression with SQL-style null propagation. */
|
|
440
|
-
function evaluateRelationalExpression(
|
|
441
|
-
expression: RelationalExpression,
|
|
442
|
-
row: Readonly<Record<string, unknown>>
|
|
443
|
-
): unknown {
|
|
444
|
-
const definition = expression.expression;
|
|
445
|
-
if (definition.op === 'literal') return definition.value;
|
|
446
|
-
if (definition.op === 'column') return row[definition.column];
|
|
447
|
-
const left = row[definition.left];
|
|
448
|
-
const right = row[definition.right];
|
|
449
|
-
if (left === null || left === undefined || right === null || right === undefined) return null;
|
|
450
|
-
if (typeof left !== 'number' || typeof right !== 'number') {
|
|
451
|
-
throw new Error(`Arrow relational expression ${expression.name} requires numeric operands.`);
|
|
452
|
-
}
|
|
453
|
-
switch (definition.op) {
|
|
454
|
-
case 'add':
|
|
455
|
-
return left + right;
|
|
456
|
-
case 'subtract':
|
|
457
|
-
return left - right;
|
|
458
|
-
case 'multiply':
|
|
459
|
-
return left * right;
|
|
460
|
-
case 'divide':
|
|
461
|
-
return right === 0 ? null : left / right;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
/** Compares two rows using stable, multi-key relational ordering. */
|
|
466
|
-
function compareRows(
|
|
467
|
-
left: Readonly<Record<string, unknown>>,
|
|
468
|
-
right: Readonly<Record<string, unknown>>,
|
|
469
|
-
orderBy: readonly RelationalOrderKey[]
|
|
470
|
-
): number {
|
|
471
|
-
for (const key of orderBy) {
|
|
472
|
-
const leftValue = left[key.column];
|
|
473
|
-
const rightValue = right[key.column];
|
|
474
|
-
const nulls = key.nulls || 'last';
|
|
475
|
-
if (
|
|
476
|
-
leftValue === null ||
|
|
477
|
-
leftValue === undefined ||
|
|
478
|
-
rightValue === null ||
|
|
479
|
-
rightValue === undefined
|
|
480
|
-
) {
|
|
481
|
-
if (leftValue === rightValue) continue;
|
|
482
|
-
const nullResult = leftValue === null || leftValue === undefined ? -1 : 1;
|
|
483
|
-
return nulls === 'first' ? nullResult : -nullResult;
|
|
484
|
-
}
|
|
485
|
-
const result = compareSortValues(leftValue, rightValue);
|
|
486
|
-
if (result) return (key.direction === 'desc' ? -1 : 1) * result;
|
|
487
|
-
}
|
|
488
|
-
return 0;
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
/** Compares two non-null values supported by portable ordering. */
|
|
492
|
-
function compareSortValues(left: unknown, right: unknown): number {
|
|
493
|
-
if (left instanceof Date && right instanceof Date)
|
|
494
|
-
return compareNumbers(left.getTime(), right.getTime());
|
|
495
|
-
if (typeof left === 'bigint' && typeof right === 'bigint')
|
|
496
|
-
return left === right ? 0 : left < right ? -1 : 1;
|
|
497
|
-
if (typeof left === 'number' && typeof right === 'number') return compareNumbers(left, right);
|
|
498
|
-
if (typeof left === 'string' && typeof right === 'string')
|
|
499
|
-
return left === right ? 0 : left < right ? -1 : 1;
|
|
500
|
-
if (typeof left === 'boolean' && typeof right === 'boolean') return Number(left) - Number(right);
|
|
501
|
-
throw new Error(
|
|
502
|
-
`Arrow relational order cannot compare ${getValueType(left)} with ${getValueType(right)}.`
|
|
503
|
-
);
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
/** Groups rows and computes the requested aggregate output columns. */
|
|
507
|
-
function aggregateRows(
|
|
508
|
-
rows: readonly Record<string, unknown>[],
|
|
509
|
-
groupBy: readonly string[],
|
|
510
|
-
aggregates: readonly RelationalAggregate[]
|
|
511
|
-
): Record<string, unknown>[] {
|
|
512
|
-
const groups = new Map<string, Record<string, unknown>[]>();
|
|
513
|
-
for (const row of rows) {
|
|
514
|
-
const key = serializeGroupKey(groupBy.map(column => row[column]));
|
|
515
|
-
const group = groups.get(key) || [];
|
|
516
|
-
group.push(row);
|
|
517
|
-
groups.set(key, group);
|
|
518
|
-
}
|
|
519
|
-
if (!groupBy.length && !rows.length) groups.set('[]', []);
|
|
520
|
-
return [...groups.values()].map(group => {
|
|
521
|
-
const output: Record<string, unknown> = {};
|
|
522
|
-
for (const column of groupBy) output[column] = group[0]?.[column] ?? null;
|
|
523
|
-
for (const aggregate of aggregates)
|
|
524
|
-
output[aggregate.name] = evaluateAggregate(aggregate, group);
|
|
525
|
-
return output;
|
|
526
|
-
});
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
/** Produces a collision-resistant group key for Arrow scalar values, including bigint values. */
|
|
530
|
-
function serializeGroupKey(values: readonly unknown[]): string {
|
|
531
|
-
return values
|
|
532
|
-
.map(value => {
|
|
533
|
-
if (value === null || value === undefined) return 'null:';
|
|
534
|
-
if (value instanceof Date) return `date:${value.getTime()}`;
|
|
535
|
-
if (value instanceof Uint8Array) return `bytes:${Array.from(value).join('.')}`;
|
|
536
|
-
return `${typeof value}:${String(value)}`;
|
|
537
|
-
})
|
|
538
|
-
.join('\u001f');
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
/** Creates a zero-row Arrow result while preserving source and computed output fields. */
|
|
542
|
-
function createEmptyRelationalResult(
|
|
543
|
-
sourceData: arrow.Table,
|
|
544
|
-
outputColumns: readonly string[],
|
|
545
|
-
options: ArrowQueryOptions,
|
|
546
|
-
additionalFields?: ReadonlyMap<string, arrow.Field>
|
|
547
|
-
): arrow.Table {
|
|
548
|
-
const sourceFields = new Map(sourceData.schema.fields.map(field => [field.name, field]));
|
|
549
|
-
const expressions = new Map(
|
|
550
|
-
(options.expressions || []).map(expression => [expression.name, expression])
|
|
551
|
-
);
|
|
552
|
-
const aggregates = new Map(
|
|
553
|
-
(options.aggregates || []).map(aggregate => [aggregate.name, aggregate])
|
|
554
|
-
);
|
|
555
|
-
const fields = outputColumns.map(columnName => {
|
|
556
|
-
const sourceField = sourceFields.get(columnName);
|
|
557
|
-
if (sourceField) return sourceField;
|
|
558
|
-
const additionalField = additionalFields?.get(columnName);
|
|
559
|
-
if (additionalField) return additionalField;
|
|
560
|
-
const expression = expressions.get(columnName);
|
|
561
|
-
if (expression)
|
|
562
|
-
return new arrow.Field(columnName, getExpressionType(expression, sourceFields), true);
|
|
563
|
-
const aggregate = aggregates.get(columnName);
|
|
564
|
-
if (aggregate)
|
|
565
|
-
return new arrow.Field(columnName, getAggregateType(aggregate, sourceFields), true);
|
|
566
|
-
return new arrow.Field(columnName, new arrow.Utf8(), true);
|
|
567
|
-
});
|
|
568
|
-
const columns: Record<string, arrow.Vector> = {};
|
|
569
|
-
for (const field of fields) columns[field.name] = arrow.vectorFromArray([], field.type);
|
|
570
|
-
return new arrow.Table(new arrow.Schema(fields), columns);
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
/** Infers the Arrow type for an expression in an empty relational result. */
|
|
574
|
-
function getExpressionType(
|
|
575
|
-
expression: RelationalExpression,
|
|
576
|
-
sourceFields: ReadonlyMap<string, arrow.Field>
|
|
577
|
-
): arrow.DataType {
|
|
578
|
-
if (expression.expression.op === 'literal') {
|
|
579
|
-
const value = expression.expression.value;
|
|
580
|
-
if (typeof value === 'boolean') return new arrow.Bool();
|
|
581
|
-
if (typeof value === 'number') return new arrow.Float64();
|
|
582
|
-
return new arrow.Utf8();
|
|
583
|
-
}
|
|
584
|
-
if (expression.expression.op === 'column') {
|
|
585
|
-
return sourceFields.get(expression.expression.column)?.type || new arrow.Utf8();
|
|
586
|
-
}
|
|
587
|
-
return new arrow.Float64();
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
/** Infers the Arrow type for an aggregate in an empty relational result. */
|
|
591
|
-
function getAggregateType(
|
|
592
|
-
aggregate: RelationalAggregate,
|
|
593
|
-
sourceFields: ReadonlyMap<string, arrow.Field>
|
|
594
|
-
): arrow.DataType {
|
|
595
|
-
if (aggregate.function === 'count') return new arrow.Int32();
|
|
596
|
-
return aggregate.column
|
|
597
|
-
? sourceFields.get(aggregate.column)?.type || new arrow.Float64()
|
|
598
|
-
: new arrow.Float64();
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
/** Computes one aggregate over a group while ignoring null input values. */
|
|
602
|
-
function evaluateAggregate(
|
|
603
|
-
aggregate: RelationalAggregate,
|
|
604
|
-
rows: readonly Record<string, unknown>[]
|
|
605
|
-
): unknown {
|
|
606
|
-
if (aggregate.function === 'count')
|
|
607
|
-
return aggregate.column
|
|
608
|
-
? rows.filter(row => row[aggregate.column!] != null).length
|
|
609
|
-
: rows.length;
|
|
610
|
-
const values = rows.map(row => row[aggregate.column!]).filter(value => value != null);
|
|
611
|
-
if (!values.length) return null;
|
|
612
|
-
if (!values.every(value => typeof value === 'number'))
|
|
613
|
-
throw new Error(`Arrow aggregate ${aggregate.function} requires numeric values.`);
|
|
614
|
-
const numericValues = values as number[];
|
|
615
|
-
switch (aggregate.function) {
|
|
616
|
-
case 'sum':
|
|
617
|
-
return numericValues.reduce((sum, value) => sum + value, 0);
|
|
618
|
-
case 'avg':
|
|
619
|
-
return numericValues.reduce((sum, value) => sum + value, 0) / numericValues.length;
|
|
620
|
-
case 'min':
|
|
621
|
-
return Math.min(...numericValues);
|
|
622
|
-
case 'max':
|
|
623
|
-
return Math.max(...numericValues);
|
|
624
|
-
}
|
|
625
|
-
}
|
|
12
|
+
export {ARROW_TABLE_QUERY_CAPABILITIES, explainArrowTableQuery} from './plan-arrow-table-query';
|
|
13
|
+
export type {ArrowQueryOptions} from './plan-arrow-table-query';
|
|
626
14
|
|
|
627
|
-
/**
|
|
628
|
-
function
|
|
629
|
-
|
|
630
|
-
const error = new Error('Arrow query was aborted.');
|
|
631
|
-
error.name = 'AbortError';
|
|
632
|
-
throw error;
|
|
633
|
-
}
|
|
15
|
+
/** Executes a validated Arrow query with math.gl as the linear executor. */
|
|
16
|
+
export function queryArrowTable(source: Table, options?: ArrowQueryOptions): Table {
|
|
17
|
+
return planAndQueryArrowTable(source, options, executeArrowQuery);
|
|
634
18
|
}
|
|
@@ -95,7 +95,7 @@ async function planPartialProjection(
|
|
|
95
95
|
source.supportsQueryProjectionPushdown!(selection)
|
|
96
96
|
);
|
|
97
97
|
if (!accepted.some(Boolean) || accepted.every(Boolean)) return null;
|
|
98
|
-
const {
|
|
98
|
+
const {prepareArrowProjection, getProjectionColumns} = await import(
|
|
99
99
|
'./arrow-execution/expressions'
|
|
100
100
|
);
|
|
101
101
|
const nativeSelections: NonNullable<typeof query.select>[number][] = [];
|
|
@@ -115,9 +115,8 @@ async function planPartialProjection(
|
|
|
115
115
|
});
|
|
116
116
|
const request = {...query, select: [...dependencies, ...nativeSelections]};
|
|
117
117
|
if (!canPushdownQuery(request, capabilities, true)) return null;
|
|
118
|
-
const
|
|
119
|
-
const
|
|
120
|
-
const project = compileArrowProjection(intermediate.schema, residual, signal);
|
|
118
|
+
const intermediate = prepareArrowProjection(inputSchema, request.select, {signal});
|
|
119
|
+
const {projectTable: project} = prepareArrowProjection(intermediate.schema, residual, {signal});
|
|
121
120
|
const plan = await source.planQueryPushdown!(request, {signal});
|
|
122
121
|
const checked = validatePushdownPlan(
|
|
123
122
|
plan,
|