@loaders.gl/core 5.0.0-beta.6 → 5.0.0-beta.8
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/geojson-table.cjs +26 -15
- package/dist/arrow-geometry/geojson-table.cjs.map +2 -2
- package/dist/arrow-geometry/geojson-table.d.ts +8 -1
- package/dist/arrow-geometry/geojson-table.d.ts.map +1 -1
- package/dist/arrow-geometry/geojson-table.js +18 -9
- package/dist/arrow-geometry/geojson-table.js.map +1 -1
- package/dist/cluster-source.cjs +78 -8
- package/dist/cluster-source.cjs.map +3 -3
- package/dist/dist.dev.js +354 -319
- package/dist/dist.min.js +3 -3
- package/dist/federated-table-scan-source.cjs +187 -179
- package/dist/federated-table-scan-source.cjs.map +2 -2
- package/dist/geoarrow/lib/feature-collection-converters/convert-geometry-column-to-binary-feature-collection.cjs +4 -8
- package/dist/geoarrow/lib/feature-collection-converters/convert-geometry-column-to-binary-feature-collection.cjs.map +2 -2
- package/dist/geoarrow/lib/feature-collection-converters/convert-geometry-column-to-binary-feature-collection.d.ts.map +1 -1
- package/dist/geoarrow/lib/feature-collection-converters/convert-geometry-column-to-binary-feature-collection.js +4 -8
- package/dist/geoarrow/lib/feature-collection-converters/convert-geometry-column-to-binary-feature-collection.js.map +1 -1
- package/dist/geoarrow/mesharrow/arrow-fixed-size-list-utils.cjs +3 -2
- package/dist/geoarrow/mesharrow/arrow-fixed-size-list-utils.cjs.map +2 -2
- package/dist/geoarrow/mesharrow/arrow-fixed-size-list-utils.d.ts.map +1 -1
- package/dist/geoarrow/mesharrow/arrow-fixed-size-list-utils.js +3 -2
- package/dist/geoarrow/mesharrow/arrow-fixed-size-list-utils.js.map +1 -1
- package/dist/geoarrow/metadata/geoarrow-metadata.cjs +44 -2
- package/dist/geoarrow/metadata/geoarrow-metadata.cjs.map +4 -4
- package/dist/geoarrow/metadata/geoarrow-metadata.d.ts.map +1 -1
- package/dist/geoarrow/metadata/geoarrow-metadata.js +3 -4
- package/dist/geoarrow/metadata/geoarrow-metadata.js.map +1 -1
- package/dist/index.cjs +351 -281
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/api/create-data-source.d.ts +14 -9
- package/dist/lib/api/create-data-source.d.ts.map +1 -1
- package/dist/lib/api/create-data-source.js +55 -45
- package/dist/lib/api/create-data-source.js.map +1 -1
- package/dist/lib/api/encode.d.ts +1 -1
- package/dist/lib/api/encode.d.ts.map +1 -1
- package/dist/lib/api/encode.js +11 -2
- package/dist/lib/api/encode.js.map +1 -1
- package/dist/lib/api/load-in-batches.d.ts +3 -2
- package/dist/lib/api/load-in-batches.d.ts.map +1 -1
- package/dist/lib/api/load-in-batches.js.map +1 -1
- package/dist/lib/api/load-loader.d.ts +3 -3
- package/dist/lib/api/load-loader.d.ts.map +1 -1
- package/dist/lib/api/load-loader.js +14 -18
- package/dist/lib/api/load-loader.js.map +1 -1
- package/dist/lib/api/load.d.ts +7 -3
- package/dist/lib/api/load.d.ts.map +1 -1
- package/dist/lib/api/load.js +11 -64
- package/dist/lib/api/load.js.map +1 -1
- package/dist/lib/api/parse-file.d.ts +5 -3
- package/dist/lib/api/parse-file.d.ts.map +1 -1
- package/dist/lib/api/parse-file.js +12 -7
- package/dist/lib/api/parse-file.js.map +1 -1
- package/dist/lib/api/parse-in-batches.d.ts +5 -3
- package/dist/lib/api/parse-in-batches.d.ts.map +1 -1
- package/dist/lib/api/parse-in-batches.js +14 -3
- package/dist/lib/api/parse-in-batches.js.map +1 -1
- package/dist/lib/api/parse-sync.d.ts +5 -3
- package/dist/lib/api/parse-sync.d.ts.map +1 -1
- package/dist/lib/api/parse-sync.js +8 -6
- package/dist/lib/api/parse-sync.js.map +1 -1
- package/dist/lib/api/parse.d.ts +5 -3
- package/dist/lib/api/parse.d.ts.map +1 -1
- package/dist/lib/api/parse.js +26 -15
- package/dist/lib/api/parse.js.map +1 -1
- package/dist/lib/api/select-loader.d.ts +4 -2
- package/dist/lib/api/select-loader.d.ts.map +1 -1
- package/dist/lib/api/select-loader.js +43 -40
- package/dist/lib/api/select-loader.js.map +1 -1
- package/dist/lib/api/select-source.d.ts.map +1 -1
- package/dist/lib/api/select-source.js +1 -1
- package/dist/lib/api/select-source.js.map +1 -1
- package/dist/lib/init.js +1 -1
- package/dist/lib/loader-utils/loggers.d.ts +2 -37
- package/dist/lib/loader-utils/loggers.d.ts.map +1 -1
- package/dist/lib/loader-utils/loggers.js +2 -63
- package/dist/lib/loader-utils/loggers.js.map +1 -1
- package/dist/lib/loader-utils/native-arrow.d.ts +11 -0
- package/dist/lib/loader-utils/native-arrow.d.ts.map +1 -0
- package/dist/lib/loader-utils/native-arrow.js +52 -0
- package/dist/lib/loader-utils/native-arrow.js.map +1 -0
- package/dist/lib/loader-utils/option-defaults.js +2 -2
- package/dist/lib/loader-utils/option-defaults.js.map +1 -1
- package/dist/lib/loader-utils/option-utils.d.ts.map +1 -1
- package/dist/lib/loader-utils/option-utils.js +21 -10
- package/dist/lib/loader-utils/option-utils.js.map +1 -1
- package/dist/loader-utils/index.cjs +143 -42
- package/dist/loader-utils/index.cjs.map +3 -3
- package/dist/loader-utils/index.d.ts +2 -2
- package/dist/loader-utils/index.d.ts.map +1 -1
- package/dist/loader-utils/index.js +1 -1
- package/dist/loader-utils/index.js.map +1 -1
- package/dist/loader-utils/json-loader.js +1 -1
- package/dist/loader-utils/lib/log-utils/log.d.ts +24 -1
- package/dist/loader-utils/lib/log-utils/log.d.ts.map +1 -1
- package/dist/loader-utils/lib/log-utils/log.js +32 -1
- package/dist/loader-utils/lib/log-utils/log.js.map +1 -1
- package/dist/loader-utils/lib/scan-utils/logical-table-query.d.ts +5 -0
- package/dist/loader-utils/lib/scan-utils/logical-table-query.d.ts.map +1 -1
- package/dist/loader-utils/lib/scan-utils/logical-table-query.js +13 -5
- package/dist/loader-utils/lib/scan-utils/logical-table-query.js.map +1 -1
- package/dist/loader-utils/lib/scan-utils/table-query.d.ts +5 -1
- package/dist/loader-utils/lib/scan-utils/table-query.d.ts.map +1 -1
- package/dist/loader-utils/lib/scan-utils/table-query.js +6 -0
- 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 +6 -1
- 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 +27 -20
- package/dist/loader-utils/lib/scan-utils/table-scan-batch.js.map +1 -1
- package/dist/loader-utils/lib/sources/data-source.js +3 -3
- package/dist/loader-utils/lib/sources/data-source.js.map +1 -1
- package/dist/loader-utils/lib/worker-loader-utils/parse-with-worker.d.ts.map +1 -1
- package/dist/loader-utils/lib/worker-loader-utils/parse-with-worker.js +15 -1
- package/dist/loader-utils/lib/worker-loader-utils/parse-with-worker.js.map +1 -1
- package/dist/loader-utils/loader-types.d.ts +102 -27
- package/dist/loader-utils/loader-types.d.ts.map +1 -1
- package/dist/loader-utils/loader-types.js.map +1 -1
- package/dist/loader-utils/source-types.d.ts +5 -5
- package/dist/loader-utils/source-types.d.ts.map +1 -1
- package/dist/loader-utils/source-types.js +5 -1
- package/dist/loader-utils/source-types.js.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/polyfills/index.cjs +3 -2
- package/dist/polyfills/index.d.ts.map +1 -1
- package/dist/polyfills/index.js +2 -2
- package/dist/polyfills/index.js.map +1 -1
- package/dist/scan/arrow-execution/expressions.cjs +110 -0
- package/dist/scan/arrow-execution/expressions.cjs.map +7 -0
- package/dist/scan/arrow-execution/expressions.d.ts +9 -0
- package/dist/scan/arrow-execution/expressions.d.ts.map +1 -0
- package/dist/scan/arrow-execution/expressions.js +60 -0
- package/dist/scan/arrow-execution/expressions.js.map +1 -0
- package/dist/scan/arrow-execution/filter-arrow-table.cjs +15 -3
- package/dist/scan/arrow-execution/filter-arrow-table.cjs.map +2 -2
- package/dist/scan/arrow-execution/filter-arrow-table.d.ts +2 -0
- package/dist/scan/arrow-execution/filter-arrow-table.d.ts.map +1 -1
- package/dist/scan/arrow-execution/filter-arrow-table.js +10 -2
- package/dist/scan/arrow-execution/filter-arrow-table.js.map +1 -1
- package/dist/scan/index.cjs +21 -6
- package/dist/scan/index.cjs.map +2 -2
- package/dist/scan/index.d.ts +2 -1
- package/dist/scan/index.d.ts.map +1 -1
- package/dist/scan/index.js +1 -1
- package/dist/scan/index.js.map +1 -1
- package/dist/scan/parse-sql-predicate.cjs +3 -294
- package/dist/scan/parse-sql-predicate.cjs.map +3 -3
- package/dist/scan/parse-sql-predicate.d.ts +1 -7
- package/dist/scan/parse-sql-predicate.d.ts.map +1 -1
- package/dist/scan/parse-sql-predicate.js +5 -307
- package/dist/scan/parse-sql-predicate.js.map +1 -1
- package/dist/scan/query-progressive.cjs +191 -0
- package/dist/scan/query-progressive.cjs.map +7 -0
- package/dist/scan/query-progressive.d.ts +39 -0
- package/dist/scan/query-progressive.d.ts.map +1 -0
- package/dist/scan/query-progressive.js +174 -0
- package/dist/scan/query-progressive.js.map +1 -0
- package/dist/scan/query.cjs +143 -22
- package/dist/scan/query.cjs.map +3 -3
- package/dist/scan/query.d.ts +27 -9
- package/dist/scan/query.d.ts.map +1 -1
- package/dist/scan/query.js +180 -18
- package/dist/scan/query.js.map +1 -1
- package/dist/schema-utils/index.cjs +262 -241
- package/dist/schema-utils/index.cjs.map +2 -2
- package/dist/schema-utils/index.d.ts +1 -0
- package/dist/schema-utils/index.d.ts.map +1 -1
- package/dist/schema-utils/index.js +1 -0
- package/dist/schema-utils/index.js.map +1 -1
- package/dist/schema-utils/lib/schema/deduce-table-schema.js +4 -2
- package/dist/schema-utils/lib/schema/deduce-table-schema.js.map +1 -1
- package/dist/schema-utils/lib/table/tables/convert-arrow-table-wrapper.d.ts +28 -0
- package/dist/schema-utils/lib/table/tables/convert-arrow-table-wrapper.d.ts.map +1 -0
- package/dist/schema-utils/lib/table/tables/convert-arrow-table-wrapper.js +26 -0
- package/dist/schema-utils/lib/table/tables/convert-arrow-table-wrapper.js.map +1 -0
- package/dist/schema-utils/lib/table/tables/row-utils.js +2 -2
- package/dist/schema-utils/lib/table/tables/row-utils.js.map +1 -1
- package/dist/vector-table-scan-source.cjs +12 -4
- package/dist/vector-table-scan-source.cjs.map +1 -1
- package/dist/worker-utils/index.cjs +49 -9
- package/dist/worker-utils/index.cjs.map +3 -3
- package/dist/worker-utils/lib/env-utils/version.js +1 -1
- package/dist/worker-utils/lib/library-utils/library-utils.d.ts.map +1 -1
- package/dist/worker-utils/lib/library-utils/library-utils.js +2 -1
- package/dist/worker-utils/lib/library-utils/library-utils.js.map +1 -1
- package/dist/worker-utils/lib/process-utils/child-process-proxy.d.ts.map +1 -1
- package/dist/worker-utils/lib/process-utils/child-process-proxy.js +8 -7
- package/dist/worker-utils/lib/process-utils/child-process-proxy.js.map +1 -1
- package/dist/worker-utils/lib/worker-api/get-worker-url.d.ts.map +1 -1
- package/dist/worker-utils/lib/worker-api/get-worker-url.js +5 -2
- package/dist/worker-utils/lib/worker-api/get-worker-url.js.map +1 -1
- package/dist/worker-utils/lib/worker-utils/get-loadable-worker-url.js +1 -6
- package/dist/worker-utils/lib/worker-utils/get-loadable-worker-url.js.map +1 -1
- package/package.json +8 -7
- package/src/arrow-geometry/geojson-table.ts +27 -17
- package/src/geoarrow/lib/feature-collection-converters/convert-geometry-column-to-binary-feature-collection.ts +4 -11
- package/src/geoarrow/mesharrow/arrow-fixed-size-list-utils.ts +3 -2
- package/src/geoarrow/metadata/geoarrow-metadata.ts +3 -4
- package/src/index.ts +1 -1
- package/src/lib/api/create-data-source.ts +136 -58
- package/src/lib/api/encode.ts +14 -3
- package/src/lib/api/load-in-batches.ts +15 -7
- package/src/lib/api/load-loader.ts +21 -20
- package/src/lib/api/load.ts +49 -121
- package/src/lib/api/parse-file.ts +42 -23
- package/src/lib/api/parse-in-batches.ts +49 -12
- package/src/lib/api/parse-sync.ts +37 -19
- package/src/lib/api/parse.ts +51 -30
- package/src/lib/api/select-loader.ts +56 -53
- package/src/lib/api/select-source.ts +7 -1
- package/src/lib/loader-utils/loggers.ts +2 -70
- package/src/lib/loader-utils/native-arrow.ts +70 -0
- package/src/lib/loader-utils/option-defaults.ts +2 -2
- package/src/lib/loader-utils/option-utils.ts +21 -10
- package/src/loader-utils/index.ts +2 -1
- package/src/loader-utils/lib/log-utils/log.ts +39 -1
- package/src/loader-utils/lib/scan-utils/logical-table-query.ts +18 -8
- package/src/loader-utils/lib/scan-utils/table-query.ts +10 -1
- package/src/loader-utils/lib/scan-utils/table-scan-batch.ts +36 -25
- package/src/loader-utils/lib/sources/data-source.ts +3 -3
- package/src/loader-utils/lib/worker-loader-utils/parse-with-worker.ts +16 -1
- package/src/loader-utils/loader-types.ts +168 -43
- package/src/loader-utils/source-types.ts +12 -6
- package/src/polyfills/index.ts +3 -3
- package/src/scan/arrow-execution/expressions.ts +86 -0
- package/src/scan/arrow-execution/filter-arrow-table.ts +19 -3
- package/src/scan/index.ts +7 -1
- package/src/scan/parse-sql-predicate.ts +8 -367
- package/src/scan/query-progressive.ts +231 -0
- package/src/scan/query.ts +247 -24
- package/src/schema-utils/index.ts +5 -0
- package/src/schema-utils/lib/schema/deduce-table-schema.ts +4 -2
- package/src/schema-utils/lib/table/tables/convert-arrow-table-wrapper.ts +58 -0
- package/src/schema-utils/lib/table/tables/row-utils.ts +2 -2
- package/src/worker-utils/lib/library-utils/library-utils.ts +2 -1
- package/src/worker-utils/lib/process-utils/child-process-proxy.ts +8 -7
- package/src/worker-utils/lib/worker-api/get-worker-url.ts +4 -3
- package/src/worker-utils/lib/worker-utils/get-loadable-worker-url.ts +1 -6
- package/dist/scan/parse-sql-query.cjs +0 -70
- package/dist/scan/parse-sql-query.cjs.map +0 -7
- package/dist/scan/parse-sql-query.d.ts +0 -5
- package/dist/scan/parse-sql-query.d.ts.map +0 -1
- package/dist/scan/parse-sql-query.js +0 -49
- package/dist/scan/parse-sql-query.js.map +0 -1
- package/dist/scan/parse-sql-select.cjs +0 -215
- package/dist/scan/parse-sql-select.cjs.map +0 -7
- package/dist/scan/parse-sql-select.d.ts +0 -38
- package/dist/scan/parse-sql-select.d.ts.map +0 -1
- package/dist/scan/parse-sql-select.js +0 -208
- package/dist/scan/parse-sql-select.js.map +0 -1
- package/src/scan/parse-sql-query.ts +0 -53
- package/src/scan/parse-sql-select.ts +0 -220
|
@@ -0,0 +1,231 @@
|
|
|
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} from '@loaders.gl/core/schema';
|
|
7
|
+
import {convertArrowToTable} from '@loaders.gl/core/schema-utils';
|
|
8
|
+
import type {TableQuery} from '@loaders.gl/core/loader-utils';
|
|
9
|
+
import {queryInBatches, type QueryOptions} from './query';
|
|
10
|
+
|
|
11
|
+
/** Supported incremental, bounded-state aggregate operations. */
|
|
12
|
+
export type ProgressiveAggregate = Readonly<{
|
|
13
|
+
function: 'count' | 'sum' | 'avg' | 'min' | 'max';
|
|
14
|
+
/** Optional only for COUNT(*); COUNT(column) ignores nulls. */
|
|
15
|
+
column?: string;
|
|
16
|
+
/** Result column name. */
|
|
17
|
+
as: string;
|
|
18
|
+
}>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Incremental aggregation over one table input.
|
|
22
|
+
*
|
|
23
|
+
* Pass GROUP BY as a structured request. Each snapshot replaces the previous result.
|
|
24
|
+
*/
|
|
25
|
+
export type ProgressiveAggregateQuery = Readonly<{
|
|
26
|
+
from?: TableQuery['from'];
|
|
27
|
+
where?: TableQuery['where'];
|
|
28
|
+
groupBy?: string;
|
|
29
|
+
aggregates: readonly ProgressiveAggregate[];
|
|
30
|
+
}>;
|
|
31
|
+
|
|
32
|
+
export type ProgressiveQueryOptions = QueryOptions &
|
|
33
|
+
Readonly<{
|
|
34
|
+
/** Emit a provisional complete table after this many nonempty input batches. Default: 1. */
|
|
35
|
+
emitEveryBatches?: number;
|
|
36
|
+
}>;
|
|
37
|
+
|
|
38
|
+
/** Each table is independent of later updates and replaces all previous snapshots. */
|
|
39
|
+
export type ProgressiveQuerySnapshot = Readonly<{
|
|
40
|
+
table: ArrowTable;
|
|
41
|
+
rowsProcessed: number;
|
|
42
|
+
final: boolean;
|
|
43
|
+
}>;
|
|
44
|
+
|
|
45
|
+
type AggregateState = {
|
|
46
|
+
count: number;
|
|
47
|
+
sum: number;
|
|
48
|
+
min: number | null;
|
|
49
|
+
max: number | null;
|
|
50
|
+
};
|
|
51
|
+
type GroupKey = string | number | boolean | null;
|
|
52
|
+
|
|
53
|
+
const MAX_SAFE_COUNT = Number.MAX_SAFE_INTEGER;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Consumes Arrow batches without retaining input rows. Group state is O(number of groups).
|
|
57
|
+
* Snapshot creation is O(number of groups × number of aggregates) and can be throttled
|
|
58
|
+
* with emitEveryBatches. Consumer early return closes the underlying source iterator.
|
|
59
|
+
*/
|
|
60
|
+
export async function* executeProgressiveQuery(
|
|
61
|
+
query: ProgressiveAggregateQuery,
|
|
62
|
+
options: ProgressiveQueryOptions
|
|
63
|
+
): AsyncIterable<ProgressiveQuerySnapshot> {
|
|
64
|
+
validateQuery(query, options);
|
|
65
|
+
const {emitEveryBatches = 1, ...queryOptions} = options;
|
|
66
|
+
const inputs = new Set<string>();
|
|
67
|
+
if (query.groupBy) inputs.add(query.groupBy);
|
|
68
|
+
for (const aggregate of query.aggregates) if (aggregate.column) inputs.add(aggregate.column);
|
|
69
|
+
const columns = [...inputs];
|
|
70
|
+
const groups = new Map<GroupKey, AggregateState[]>();
|
|
71
|
+
let sourceSchema: arrow.Schema | undefined;
|
|
72
|
+
let rowsProcessed = 0;
|
|
73
|
+
let batchesSinceEmission = 0;
|
|
74
|
+
if (query.groupBy === undefined) groups.set(null, makeAggregateStates(query.aggregates.length));
|
|
75
|
+
|
|
76
|
+
// A COUNT(*) request can have no input columns, but the query pipeline still needs
|
|
77
|
+
// a source column to preserve row counts. The absence of select means all source
|
|
78
|
+
// columns are read; no source data is retained beyond each batch.
|
|
79
|
+
const tableQuery: TableQuery = {
|
|
80
|
+
...(query.from === undefined ? {} : {from: query.from}),
|
|
81
|
+
...(query.where === undefined ? {} : {where: query.where}),
|
|
82
|
+
...(columns.length ? {select: columns} : {})
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
for await (const batch of queryInBatches(tableQuery, {...queryOptions, queryOutput: undefined})) {
|
|
86
|
+
options.signal?.throwIfAborted();
|
|
87
|
+
sourceSchema ??= batch.data.schema;
|
|
88
|
+
if (batch.length === 0) continue;
|
|
89
|
+
const keyVector = query.groupBy ? batch.data.getChild(query.groupBy) : undefined;
|
|
90
|
+
const aggregateVectors = query.aggregates.map(aggregate =>
|
|
91
|
+
aggregate.column ? batch.data.getChild(aggregate.column) : undefined
|
|
92
|
+
);
|
|
93
|
+
for (let row = 0; row < batch.length; row++) {
|
|
94
|
+
if ((row & 1023) === 0) options.signal?.throwIfAborted();
|
|
95
|
+
const key = keyVector ? normalizeKey(keyVector.get(row)) : null;
|
|
96
|
+
let group = groups.get(key);
|
|
97
|
+
if (!group) {
|
|
98
|
+
group = makeAggregateStates(query.aggregates.length);
|
|
99
|
+
groups.set(key, group);
|
|
100
|
+
}
|
|
101
|
+
for (let index = 0; index < query.aggregates.length; index++) {
|
|
102
|
+
const operation = query.aggregates[index];
|
|
103
|
+
const value = aggregateVectors[index]?.get(row);
|
|
104
|
+
if (operation.function === 'count') {
|
|
105
|
+
if (!operation.column || (value !== null && value !== undefined)) {
|
|
106
|
+
increaseCount(group[index]);
|
|
107
|
+
}
|
|
108
|
+
} else if (value !== null && value !== undefined) {
|
|
109
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`Progressive ${operation.function} requires finite numeric values in column ${operation.column}`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
updateNumeric(group[index], value);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
rowsProcessed += batch.length;
|
|
119
|
+
if (++batchesSinceEmission >= emitEveryBatches) {
|
|
120
|
+
yield {table: createSnapshot(groups, query, sourceSchema), rowsProcessed, final: false};
|
|
121
|
+
batchesSinceEmission = 0;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// The terminal snapshot is always emitted, including empty inputs and when the
|
|
126
|
+
// previous provisional snapshot already has the same values.
|
|
127
|
+
yield {
|
|
128
|
+
table: createSnapshot(groups, query, sourceSchema),
|
|
129
|
+
rowsProcessed,
|
|
130
|
+
final: true
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function validateQuery(query: ProgressiveAggregateQuery, options: ProgressiveQueryOptions): void {
|
|
135
|
+
if (!query || typeof query !== 'object' || Array.isArray(query)) {
|
|
136
|
+
throw new Error('Progressive query must be an object.');
|
|
137
|
+
}
|
|
138
|
+
if (Object.keys(query).some(key => !['from', 'where', 'groupBy', 'aggregates'].includes(key))) {
|
|
139
|
+
throw new Error('Unsupported progressive query field.');
|
|
140
|
+
}
|
|
141
|
+
if (query.groupBy !== undefined && (typeof query.groupBy !== 'string' || !query.groupBy)) {
|
|
142
|
+
throw new Error('Progressive groupBy must be a nonempty column name.');
|
|
143
|
+
}
|
|
144
|
+
if (!Array.isArray(query.aggregates) || !query.aggregates.length) {
|
|
145
|
+
throw new Error('Progressive query requires at least one aggregate.');
|
|
146
|
+
}
|
|
147
|
+
const names = new Set(query.groupBy ? [query.groupBy] : []);
|
|
148
|
+
for (const aggregate of query.aggregates) {
|
|
149
|
+
if (!aggregate || !['count', 'sum', 'avg', 'min', 'max'].includes(aggregate.function)) {
|
|
150
|
+
throw new Error('Unsupported progressive aggregate function.');
|
|
151
|
+
}
|
|
152
|
+
if (Object.keys(aggregate).some(key => !['function', 'column', 'as'].includes(key))) {
|
|
153
|
+
throw new Error('Unsupported progressive aggregate field.');
|
|
154
|
+
}
|
|
155
|
+
if (!aggregate.as || typeof aggregate.as !== 'string' || names.has(aggregate.as)) {
|
|
156
|
+
throw new Error('Progressive aggregate aliases must be unique nonempty output names.');
|
|
157
|
+
}
|
|
158
|
+
if (
|
|
159
|
+
aggregate.column !== undefined &&
|
|
160
|
+
(typeof aggregate.column !== 'string' || !aggregate.column)
|
|
161
|
+
) {
|
|
162
|
+
throw new Error('Progressive aggregate column must be nonempty.');
|
|
163
|
+
}
|
|
164
|
+
if (aggregate.function !== 'count' && !aggregate.column) {
|
|
165
|
+
throw new Error(`Progressive ${aggregate.function} requires an input column.`);
|
|
166
|
+
}
|
|
167
|
+
names.add(aggregate.as);
|
|
168
|
+
}
|
|
169
|
+
const interval = options.emitEveryBatches ?? 1;
|
|
170
|
+
if (!Number.isSafeInteger(interval) || interval <= 0) {
|
|
171
|
+
throw new Error('emitEveryBatches must be a positive safe integer.');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Initializes the independent aggregate state for one group. */
|
|
176
|
+
function makeAggregateStates(length: number): AggregateState[] {
|
|
177
|
+
return Array.from({length}, () => ({count: 0, sum: 0, min: null, max: null}));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function normalizeKey(value: unknown): GroupKey {
|
|
181
|
+
if (value === null || value === undefined) return null;
|
|
182
|
+
if (typeof value === 'string' || typeof value === 'boolean') return value;
|
|
183
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
184
|
+
throw new Error('Progressive GROUP BY supports finite numeric, string, boolean, and null keys.');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function increaseCount(state: AggregateState): void {
|
|
188
|
+
if (state.count >= MAX_SAFE_COUNT)
|
|
189
|
+
throw new Error('Progressive aggregate count exceeds safe integer range.');
|
|
190
|
+
state.count++;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function updateNumeric(state: AggregateState, value: number): void {
|
|
194
|
+
increaseCount(state);
|
|
195
|
+
state.sum += value;
|
|
196
|
+
state.min = state.min === null ? value : Math.min(state.min, value);
|
|
197
|
+
state.max = state.max === null ? value : Math.max(state.max, value);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function createSnapshot(
|
|
201
|
+
groups: Map<GroupKey, AggregateState[]>,
|
|
202
|
+
query: ProgressiveAggregateQuery,
|
|
203
|
+
sourceSchema: arrow.Schema | undefined
|
|
204
|
+
): ArrowTable {
|
|
205
|
+
const resultColumns: Record<string, arrow.Vector> = {};
|
|
206
|
+
if (query.groupBy) {
|
|
207
|
+
const field = sourceSchema?.fields.find(candidate => candidate.name === query.groupBy);
|
|
208
|
+
if (!field) throw new Error(`Progressive group column not found: ${query.groupBy}`);
|
|
209
|
+
resultColumns[query.groupBy] = arrow.vectorFromArray([...groups.keys()], field.type);
|
|
210
|
+
}
|
|
211
|
+
for (let index = 0; index < query.aggregates.length; index++) {
|
|
212
|
+
const aggregate = query.aggregates[index];
|
|
213
|
+
const values = [...groups.values()].map(group => {
|
|
214
|
+
const state = group[index];
|
|
215
|
+
switch (aggregate.function) {
|
|
216
|
+
case 'count':
|
|
217
|
+
return state.count;
|
|
218
|
+
case 'sum':
|
|
219
|
+
return state.count ? state.sum : null;
|
|
220
|
+
case 'avg':
|
|
221
|
+
return state.count ? state.sum / state.count : null;
|
|
222
|
+
case 'min':
|
|
223
|
+
return state.min;
|
|
224
|
+
case 'max':
|
|
225
|
+
return state.max;
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
resultColumns[aggregate.as] = arrow.vectorFromArray(values, new arrow.Float64());
|
|
229
|
+
}
|
|
230
|
+
return convertArrowToTable(new arrow.Table(resultColumns), 'arrow-table');
|
|
231
|
+
}
|
package/src/scan/query.ts
CHANGED
|
@@ -3,9 +3,16 @@
|
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
5
|
import * as arrow from 'apache-arrow';
|
|
6
|
+
import type {
|
|
7
|
+
ProgressiveAggregateQuery,
|
|
8
|
+
ProgressiveQueryOptions,
|
|
9
|
+
ProgressiveQuerySnapshot
|
|
10
|
+
} from './query-progressive';
|
|
11
|
+
import type {SQLQuery} from '@math.gl/expressions/sql';
|
|
6
12
|
import type {ArrowTable, ArrowTableBatch, TableBatch, Schema} from '@loaders.gl/core/schema';
|
|
7
13
|
import {
|
|
8
14
|
normalizeTableQuery,
|
|
15
|
+
getColumnarPredicateColumns,
|
|
9
16
|
executeTableScanBatches,
|
|
10
17
|
planTableQuery,
|
|
11
18
|
emitQueryComplete,
|
|
@@ -16,7 +23,7 @@ import {
|
|
|
16
23
|
type TableScanReadOptions,
|
|
17
24
|
type LoaderOptions,
|
|
18
25
|
type DataSourceOptions,
|
|
19
|
-
type
|
|
26
|
+
type Loader,
|
|
20
27
|
resolveLoaderAuthenticationOptions,
|
|
21
28
|
isSourceLoader
|
|
22
29
|
} from '@loaders.gl/core/loader-utils';
|
|
@@ -26,13 +33,14 @@ import {
|
|
|
26
33
|
convertTableToArrow
|
|
27
34
|
} from '@loaders.gl/core/schema-utils';
|
|
28
35
|
import {projectArrowTable, filterArrowTable} from './arrow-execution';
|
|
36
|
+
import {filterArrowRows} from './arrow-execution/filter-arrow-table';
|
|
29
37
|
import {createCatalogQuerySource} from './catalog-query-source';
|
|
30
38
|
import type {SQLCatalogQuery} from './sql-catalog';
|
|
31
39
|
import {resolveQuerySource} from './resolve-query-source';
|
|
32
40
|
import type {DataSource} from '@loaders.gl/core/loader-utils';
|
|
33
41
|
|
|
34
42
|
/** Logical table or metadata request. */
|
|
35
|
-
export type Query = TableQuery |
|
|
43
|
+
export type Query = TableQuery | SQLQuery;
|
|
36
44
|
|
|
37
45
|
/** Injected SQL parser; core validates and executes the returned logical query. */
|
|
38
46
|
export type SQLQueryParser = (sql: string) => Query | Promise<Query>;
|
|
@@ -47,6 +55,8 @@ export type QueryOptions = TableQueryExecutionOptions &
|
|
|
47
55
|
sources?: readonly DataSource<unknown, DataSourceOptions>[];
|
|
48
56
|
/** Parser or lazy parser promise, required only for SQL string input. */
|
|
49
57
|
sqlParser?: SQLQueryParser | Promise<SQLQueryParser>;
|
|
58
|
+
/** Append-only batches (default), or complete progressive replacement snapshots. */
|
|
59
|
+
queryOutput?: 'batches' | 'snapshots';
|
|
50
60
|
}> &
|
|
51
61
|
Readonly<
|
|
52
62
|
| {
|
|
@@ -67,7 +77,7 @@ export type QueryOptions = TableQueryExecutionOptions &
|
|
|
67
77
|
/** URL opened lazily with the explicitly supplied source loaders. */
|
|
68
78
|
source: string;
|
|
69
79
|
/** URL-capable source loaders; registered loaders are never selected implicitly. */
|
|
70
|
-
loaders:
|
|
80
|
+
loaders: Loader | Loader[];
|
|
71
81
|
}
|
|
72
82
|
>;
|
|
73
83
|
|
|
@@ -78,7 +88,7 @@ export async function query(
|
|
|
78
88
|
): Promise<ArrowTable> {
|
|
79
89
|
let schema: arrow.Schema | undefined;
|
|
80
90
|
const batches: arrow.RecordBatch[] = [];
|
|
81
|
-
for await (const batch of queryInBatches(tableQuery, options)) {
|
|
91
|
+
for await (const batch of queryInBatches(tableQuery, {...options, queryOutput: undefined})) {
|
|
82
92
|
schema = batch.data.schema;
|
|
83
93
|
batches.push(...batch.data.batches);
|
|
84
94
|
}
|
|
@@ -86,8 +96,71 @@ export async function query(
|
|
|
86
96
|
return {shape: 'arrow-table', schema: convertArrowToSchema(data.schema), data};
|
|
87
97
|
}
|
|
88
98
|
|
|
89
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* Without queryOutput, preserves the legacy loaders.gl ArrowTableBatch envelope.
|
|
101
|
+
* With queryOutput, yields pure Arrow RecordBatch objects. Each snapshot replaces
|
|
102
|
+
* all earlier snapshots; batches are append-only.
|
|
103
|
+
*/
|
|
104
|
+
export function queryInBatches(
|
|
105
|
+
tableQuery: ProgressiveAggregateQuery,
|
|
106
|
+
options: QueryOptions & {queryOutput: 'snapshots'; emitEveryBatches?: number}
|
|
107
|
+
): AsyncIterable<arrow.RecordBatch>;
|
|
108
|
+
export function queryInBatches(
|
|
109
|
+
tableQuery: Query | string,
|
|
110
|
+
options: QueryOptions & {queryOutput: 'batches'}
|
|
111
|
+
): AsyncIterable<arrow.RecordBatch>;
|
|
112
|
+
export function queryInBatches(
|
|
113
|
+
tableQuery: Query | string,
|
|
114
|
+
options: QueryOptions & {queryOutput?: undefined}
|
|
115
|
+
): AsyncIterable<ArrowTableBatch>;
|
|
116
|
+
export function queryInBatches(
|
|
117
|
+
tableQuery: Query | string | ProgressiveAggregateQuery,
|
|
118
|
+
options: QueryOptions
|
|
119
|
+
): AsyncIterable<ArrowTableBatch | arrow.RecordBatch>;
|
|
90
120
|
export async function* queryInBatches(
|
|
121
|
+
tableQuery: Query | string | ProgressiveAggregateQuery,
|
|
122
|
+
options: QueryOptions
|
|
123
|
+
): AsyncIterable<ArrowTableBatch | arrow.RecordBatch> {
|
|
124
|
+
if (options.queryOutput === 'snapshots') {
|
|
125
|
+
if (
|
|
126
|
+
!tableQuery ||
|
|
127
|
+
typeof tableQuery !== 'object' ||
|
|
128
|
+
!('aggregates' in tableQuery) ||
|
|
129
|
+
!Array.isArray(tableQuery.aggregates)
|
|
130
|
+
) {
|
|
131
|
+
throw new Error('Snapshot query output currently requires a structured aggregate query.');
|
|
132
|
+
}
|
|
133
|
+
for await (const snapshot of queryProgressive(tableQuery, options)) {
|
|
134
|
+
yield* snapshot.table.data.batches;
|
|
135
|
+
}
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (options.queryOutput !== undefined && options.queryOutput !== 'batches') {
|
|
139
|
+
throw new Error('Unsupported queryOutput; expected batches or snapshots.');
|
|
140
|
+
}
|
|
141
|
+
if (typeof tableQuery === 'object' && tableQuery && 'aggregates' in tableQuery) {
|
|
142
|
+
throw new Error('Aggregate queries require queryOutput: snapshots.');
|
|
143
|
+
}
|
|
144
|
+
for await (const batch of queryAppendBatches(tableQuery as Query | string, options)) {
|
|
145
|
+
if (options.queryOutput === 'batches') {
|
|
146
|
+
yield* batch.data.batches;
|
|
147
|
+
} else {
|
|
148
|
+
yield batch;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Streams complete aggregate snapshots and loads the grouped executor on demand. */
|
|
154
|
+
export async function* queryProgressive(
|
|
155
|
+
query: ProgressiveAggregateQuery,
|
|
156
|
+
options: ProgressiveQueryOptions
|
|
157
|
+
): AsyncIterable<ProgressiveQuerySnapshot> {
|
|
158
|
+
const {executeProgressiveQuery} = await import('./query-progressive');
|
|
159
|
+
yield* executeProgressiveQuery(query, options);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Executes one logical query with bounded batch buffering and cooperative cancellation. */
|
|
163
|
+
async function* queryAppendBatches(
|
|
91
164
|
tableQuery: Query | string,
|
|
92
165
|
options: QueryOptions
|
|
93
166
|
): AsyncIterable<ArrowTableBatch> {
|
|
@@ -113,7 +186,9 @@ export async function* queryInBatches(
|
|
|
113
186
|
} else parsedQuery = tableQuery;
|
|
114
187
|
if (!parsedQuery || typeof parsedQuery !== 'object' || Array.isArray(parsedQuery))
|
|
115
188
|
throw new Error('Table query must be an object.');
|
|
116
|
-
|
|
189
|
+
const castPredicate =
|
|
190
|
+
'command' in parsedQuery ? false : validatePredicateOperands(parsedQuery.where);
|
|
191
|
+
let logicalQuery: TableQuery | Exclude<SQLQuery, SQLCatalogQuery>;
|
|
117
192
|
let source: unknown;
|
|
118
193
|
if ('command' in parsedQuery) {
|
|
119
194
|
logicalQuery = {};
|
|
@@ -165,9 +240,41 @@ export async function* queryInBatches(
|
|
|
165
240
|
? (source as ArrowTable).data.schema
|
|
166
241
|
: convertSchemaToArrow(metadata!.schema);
|
|
167
242
|
const names = sourceSchema.fields.map(field => field.name);
|
|
168
|
-
const
|
|
169
|
-
const
|
|
170
|
-
|
|
243
|
+
const {select, where, ...controls} = logicalQuery;
|
|
244
|
+
const computedProjection =
|
|
245
|
+
Array.isArray(select) &&
|
|
246
|
+
select.some(
|
|
247
|
+
selection =>
|
|
248
|
+
selection &&
|
|
249
|
+
typeof selection === 'object' &&
|
|
250
|
+
('expression' in selection || 'array' in selection)
|
|
251
|
+
);
|
|
252
|
+
const expressionFilter =
|
|
253
|
+
where && typeof where === 'object' && 'expression' in where ? where : undefined;
|
|
254
|
+
const portablePredicate = expressionFilter ? undefined : (where as TableQuery['where']);
|
|
255
|
+
const normalized = normalizeTableQuery(names, {
|
|
256
|
+
...controls,
|
|
257
|
+
select: computedProjection ? undefined : (select as TableQuery['select']),
|
|
258
|
+
where: castPredicate ? undefined : portablePredicate
|
|
259
|
+
});
|
|
260
|
+
const expressionTools =
|
|
261
|
+
computedProjection || expressionFilter || castPredicate
|
|
262
|
+
? await import('./arrow-execution/expressions')
|
|
263
|
+
: undefined;
|
|
264
|
+
const outputColumns = computedProjection
|
|
265
|
+
? expressionTools!.getProjectionColumns(select!)
|
|
266
|
+
: normalized.select.map(selection => selection.column);
|
|
267
|
+
const expressionColumns = expressionFilter
|
|
268
|
+
? expressionTools!.getExpressionColumns(expressionFilter.expression)
|
|
269
|
+
: castPredicate && portablePredicate
|
|
270
|
+
? getColumnarPredicateColumns(portablePredicate)
|
|
271
|
+
: [];
|
|
272
|
+
const required = [...new Set([...outputColumns, ...expressionColumns])];
|
|
273
|
+
const plan = planTableQuery(names, {
|
|
274
|
+
// A literal-only projection still needs a physical column to retain row counts.
|
|
275
|
+
columns: required.length ? required : names.slice(0, 1),
|
|
276
|
+
predicate: normalized.where
|
|
277
|
+
});
|
|
171
278
|
const requiredColumns = plan[0].kind === 'scan' ? plan[0].columns : names;
|
|
172
279
|
const capabilities = metadata?.capabilities.table;
|
|
173
280
|
// read() must perform exact filtering when it advertises pushdown+residual, not only pruning.
|
|
@@ -177,6 +284,8 @@ export async function* queryInBatches(
|
|
|
177
284
|
['pushdown', 'pushdown+residual'].includes(capabilities.predicate)
|
|
178
285
|
);
|
|
179
286
|
const residual = delegatePredicate ? undefined : normalized.where;
|
|
287
|
+
const requestedRows =
|
|
288
|
+
normalized.limit === undefined ? undefined : (normalized.offset ?? 0) + normalized.limit;
|
|
180
289
|
const readOptions: TableScanReadOptions = {
|
|
181
290
|
signal: options.signal,
|
|
182
291
|
...(capabilities && capabilities.projection !== 'unsupported'
|
|
@@ -184,15 +293,31 @@ export async function* queryInBatches(
|
|
|
184
293
|
: {}),
|
|
185
294
|
...(delegatePredicate ? {predicate: normalized.where} : {}),
|
|
186
295
|
...(!residual &&
|
|
296
|
+
!expressionFilter &&
|
|
297
|
+
!castPredicate &&
|
|
298
|
+
requestedRows !== undefined &&
|
|
299
|
+
Number.isSafeInteger(requestedRows) &&
|
|
187
300
|
capabilities &&
|
|
188
301
|
['pushdown', 'pushdown+residual'].includes(capabilities.limit)
|
|
189
|
-
? {limit:
|
|
302
|
+
? {limit: requestedRows}
|
|
190
303
|
: {})
|
|
191
304
|
};
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const
|
|
305
|
+
const inputSchema = readOptions.columns
|
|
306
|
+
? sourceSchema.select([...readOptions.columns])
|
|
307
|
+
: sourceSchema;
|
|
308
|
+
const readSchema = convertArrowToSchema(inputSchema);
|
|
309
|
+
const comparisonInputSchema = expressionTools ? convertSchemaToArrow(readSchema) : undefined;
|
|
310
|
+
const compiledFilter = expressionFilter
|
|
311
|
+
? expressionTools!.compileArrowExpression(expressionFilter.expression, {inputSchema})
|
|
312
|
+
: castPredicate && portablePredicate
|
|
313
|
+
? expressionTools!.compileArrowPredicate(portablePredicate, {inputSchema})
|
|
314
|
+
: undefined;
|
|
315
|
+
if (compiledFilter && !arrow.DataType.isBool(compiledFilter.outputField.type))
|
|
316
|
+
throw new Error('SQL WHERE expression must return Boolean.');
|
|
317
|
+
const project = computedProjection
|
|
318
|
+
? expressionTools!.compileArrowProjection(inputSchema, select!, options.signal)
|
|
319
|
+
: (data: arrow.Table) => projectArrowTable(data, normalized.select);
|
|
320
|
+
const empty = project(new arrow.Table(inputSchema));
|
|
196
321
|
const comparisonSchema = convertSchemaToArrow(convertArrowToSchema(empty.schema));
|
|
197
322
|
let emitted = false;
|
|
198
323
|
/** Adapts physical batches without pulling ahead of the query consumer. */
|
|
@@ -203,7 +328,19 @@ export async function* queryInBatches(
|
|
|
203
328
|
: (source as TableScanSource).read(readOptions);
|
|
204
329
|
for await (const value of batches) {
|
|
205
330
|
options.signal?.throwIfAborted();
|
|
206
|
-
|
|
331
|
+
let batch = normalizeBatch(value, readSchema);
|
|
332
|
+
if (
|
|
333
|
+
comparisonInputSchema &&
|
|
334
|
+
!arrow.util.compareSchemas(comparisonInputSchema, convertSchemaToArrow(batch.schema!))
|
|
335
|
+
) {
|
|
336
|
+
// A source may return a wider or differently ordered projection than requested.
|
|
337
|
+
const data = batch.data.select(inputSchema.names);
|
|
338
|
+
batch = {...batch, ...wrapBatch(data)};
|
|
339
|
+
if (
|
|
340
|
+
!arrow.util.compareSchemas(comparisonInputSchema, convertSchemaToArrow(batch.schema!))
|
|
341
|
+
)
|
|
342
|
+
throw new Error('Query input schema does not match source metadata.');
|
|
343
|
+
}
|
|
207
344
|
batchesRead++;
|
|
208
345
|
rowsRead += batch.length;
|
|
209
346
|
yield batch;
|
|
@@ -213,22 +350,36 @@ export async function* queryInBatches(
|
|
|
213
350
|
() => inputs(),
|
|
214
351
|
{
|
|
215
352
|
predicate: residual,
|
|
353
|
+
offset: normalized.offset,
|
|
216
354
|
limit: normalized.limit,
|
|
355
|
+
skipEmptyBatches: true,
|
|
217
356
|
signal: options.signal
|
|
218
357
|
},
|
|
219
358
|
{
|
|
220
359
|
filter: (batch, predicate, signal) => {
|
|
221
360
|
const input = batch as ArrowTableBatch;
|
|
222
|
-
const data =
|
|
361
|
+
const data = compiledFilter
|
|
362
|
+
? filterArrowRows(
|
|
363
|
+
input.data,
|
|
364
|
+
batch => {
|
|
365
|
+
const matches = compiledFilter.evaluate(batch, {signal});
|
|
366
|
+
return index => matches.get(index) === true;
|
|
367
|
+
},
|
|
368
|
+
{signal}
|
|
369
|
+
)
|
|
370
|
+
: predicate
|
|
371
|
+
? filterArrowTable(input.data, predicate, {signal})
|
|
372
|
+
: input.data;
|
|
223
373
|
return {...input, ...wrapBatch(data)};
|
|
224
374
|
},
|
|
225
375
|
project: batch => {
|
|
226
376
|
const input = batch as ArrowTableBatch;
|
|
227
|
-
return {...input, ...wrapBatch(
|
|
377
|
+
return computedProjection ? input : {...input, ...wrapBatch(project(input.data))};
|
|
228
378
|
}
|
|
229
379
|
}
|
|
230
380
|
)) {
|
|
231
|
-
const
|
|
381
|
+
const input = value as ArrowTableBatch;
|
|
382
|
+
const batch = computedProjection ? {...input, ...wrapBatch(project(input.data))} : input;
|
|
232
383
|
if (!arrow.util.compareSchemas(comparisonSchema, convertSchemaToArrow(batch.schema!)))
|
|
233
384
|
throw new Error('Query output schema does not match source metadata.');
|
|
234
385
|
emitted = true;
|
|
@@ -311,9 +462,15 @@ function wrapBatch(data: arrow.Table): ArrowTableBatch {
|
|
|
311
462
|
/** Opens only explicit URL source loaders; the public query owns the returned source. */
|
|
312
463
|
async function openQuerySource(options: QueryOptions): Promise<unknown> {
|
|
313
464
|
const candidates = Array.isArray(options.loaders) ? options.loaders : [options.loaders];
|
|
314
|
-
if (
|
|
465
|
+
if (
|
|
466
|
+
!candidates.length ||
|
|
467
|
+
!candidates.every(isSourceLoader) ||
|
|
468
|
+
candidates.some(loader => !loader.fromUrl)
|
|
469
|
+
) {
|
|
315
470
|
throw new Error('Query source must specify explicit URL-capable source loaders for a URL.');
|
|
316
|
-
|
|
471
|
+
}
|
|
472
|
+
const {source, sources, loaders, sqlParser, onQueryComplete, queryOutput, ...sourceOptions} =
|
|
473
|
+
options;
|
|
317
474
|
const url = source as string;
|
|
318
475
|
const selected =
|
|
319
476
|
candidates.length === 1 ? candidates[0]! : candidates.find(loader => loader!.testURL(url));
|
|
@@ -328,12 +485,78 @@ async function openQuerySource(options: QueryOptions): Promise<unknown> {
|
|
|
328
485
|
{...sourceOptions, core: {...sourceOptions.core, ignoreRegisteredLoaders: true}}
|
|
329
486
|
);
|
|
330
487
|
options.signal?.throwIfAborted();
|
|
331
|
-
return implementation.
|
|
488
|
+
return implementation.createDataSourceSync(url, authenticatedOptions as DataSourceOptions);
|
|
332
489
|
}
|
|
333
490
|
|
|
334
|
-
export {parseSQLQuery} from '
|
|
335
|
-
export {parseSQLSelect} from './parse-sql-select';
|
|
336
|
-
export type {SQLSelectParserOptions} from './parse-sql-select';
|
|
491
|
+
export {parseSQLQuery, parseSQLSelect, type SQLSelectParserOptions} from '@math.gl/expressions/sql';
|
|
337
492
|
|
|
338
493
|
export {createSQLCatalog} from './sql-catalog';
|
|
339
494
|
export type {SQLCatalog, SQLCatalogQuery, SQLCatalogSource, SQLCatalogTable} from './sql-catalog';
|
|
495
|
+
|
|
496
|
+
/** Checks predicate tree bounds and detects casts that require Arrow execution. */
|
|
497
|
+
function validatePredicateOperands(where: unknown): boolean {
|
|
498
|
+
if (where && typeof where === 'object' && 'expression' in where) {
|
|
499
|
+
if (Object.keys(where).some(key => key !== 'expression'))
|
|
500
|
+
throw new Error('Unsupported computed WHERE field.');
|
|
501
|
+
return false;
|
|
502
|
+
}
|
|
503
|
+
let castPredicate = false;
|
|
504
|
+
const pending: {value: unknown; exit?: boolean}[] = where ? [{value: where}] : [];
|
|
505
|
+
const visited = new WeakSet<object>();
|
|
506
|
+
let nodes = 0;
|
|
507
|
+
while (pending.length) {
|
|
508
|
+
const frame = pending.pop()!;
|
|
509
|
+
const candidate = frame.value;
|
|
510
|
+
if (!candidate || typeof candidate !== 'object' || Array.isArray(candidate)) continue;
|
|
511
|
+
if (frame.exit) {
|
|
512
|
+
visited.delete(candidate);
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
if (visited.has(candidate) || ++nodes > 4096)
|
|
516
|
+
throw new Error('Query predicate must be an acyclic tree of at most 4096 nodes.');
|
|
517
|
+
visited.add(candidate);
|
|
518
|
+
if (isUnsupportedQueryExpression(candidate))
|
|
519
|
+
throw new Error('CAST and computed WHERE operands are not supported by this query executor.');
|
|
520
|
+
const predicate = candidate as {op?: unknown; args?: unknown};
|
|
521
|
+
if (!Array.isArray(predicate.args)) continue;
|
|
522
|
+
if (predicate.op === 'and' || predicate.op === 'or' || predicate.op === 'not') {
|
|
523
|
+
if (predicate.args.length > 4096 - nodes)
|
|
524
|
+
throw new Error('Query predicate exceeds 4096 nodes.');
|
|
525
|
+
pending.push({value: candidate, exit: true});
|
|
526
|
+
pending.push(...predicate.args.map(value => ({value})));
|
|
527
|
+
continue;
|
|
528
|
+
}
|
|
529
|
+
visited.delete(candidate);
|
|
530
|
+
for (const [index, operand] of predicate.args.entries()) {
|
|
531
|
+
if (
|
|
532
|
+
index === 0 &&
|
|
533
|
+
operand &&
|
|
534
|
+
typeof operand === 'object' &&
|
|
535
|
+
typeof operand.property === 'string' &&
|
|
536
|
+
'casts' in operand
|
|
537
|
+
) {
|
|
538
|
+
castPredicate = true;
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
if (
|
|
542
|
+
isUnsupportedQueryExpression(operand) ||
|
|
543
|
+
(Array.isArray(operand) && operand.some(isUnsupportedQueryExpression))
|
|
544
|
+
)
|
|
545
|
+
throw new Error(
|
|
546
|
+
'CAST and computed WHERE operands are not supported by this query executor.'
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return castPredicate;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/** Identifies newer portable operands and raw expression ASTs without interpreting scalar values. */
|
|
554
|
+
function isUnsupportedQueryExpression(value: unknown): boolean {
|
|
555
|
+
if (!value || typeof value !== 'object') return false;
|
|
556
|
+
if ('casts' in value || 'expression' in value) return true;
|
|
557
|
+
return (
|
|
558
|
+
'type' in value &&
|
|
559
|
+
typeof value.type === 'string' &&
|
|
560
|
+
/Expression$|^(Identifier|Literal|Compound)$/.test(value.type)
|
|
561
|
+
);
|
|
562
|
+
}
|
|
@@ -29,6 +29,11 @@ export type {
|
|
|
29
29
|
export {getArrowViewTypeSupport} from './lib/schema/convert-arrow-schema';
|
|
30
30
|
export {getDataTypeFromArray} from './lib/schema/data-type';
|
|
31
31
|
|
|
32
|
+
export {
|
|
33
|
+
convertArrowTable,
|
|
34
|
+
convertArrowTableBatch
|
|
35
|
+
} from './lib/table/tables/convert-arrow-table-wrapper';
|
|
36
|
+
|
|
32
37
|
// TABLE CATEGORY UTILS
|
|
33
38
|
|
|
34
39
|
export {deduceTableSchema} from './lib/schema/deduce-table-schema';
|
|
@@ -55,9 +55,11 @@ function deduceSchemaFromRows(rowTable: unknown[][] | {[key: string]: unknown}[]
|
|
|
55
55
|
const row0 = rowTable[0];
|
|
56
56
|
// TODO - fields can be nullable, false detection...
|
|
57
57
|
// Could look at additional rows if nulls in first row
|
|
58
|
-
//
|
|
58
|
+
// Positional rows use the same one-based generated names as headerless CSV.
|
|
59
|
+
// Object rows keep their original keys, including numeric-looking names.
|
|
59
60
|
for (const [columnName, value] of Object.entries(row0)) {
|
|
60
|
-
|
|
61
|
+
const name = Array.isArray(row0) ? `column${Number(columnName) + 1}` : columnName;
|
|
62
|
+
fields.push(deduceFieldFromValue(value, name));
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
return {fields, metadata: {}};
|