@luma.gl/graph 10.0.0-alpha.8 → 10.0.0-alpha.9
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/gpu-sql/{lu-sql.cjs → gpu-sql.cjs} +68 -68
- package/dist/gpu-sql/{lu-sql.d.ts → gpu-sql.d.ts} +14 -14
- package/dist/gpu-sql/gpu-sql.d.ts.map +1 -0
- package/dist/gpu-sql/{lu-sql.js → gpu-sql.js} +68 -68
- package/dist/gpu-sql/gpu-sql.js.map +1 -0
- package/dist/gpu-sql/index.cjs +5 -5
- package/dist/gpu-sql/index.cjs.map +2 -2
- package/dist/gpu-sql/index.d.ts +2 -2
- package/dist/gpu-sql/index.d.ts.map +1 -1
- package/dist/gpu-sql/index.js +1 -1
- package/dist/gpu-sql/index.js.map +1 -1
- package/package.json +4 -4
- package/src/gpu-project/README.md +4 -4
- package/src/gpu-sql/{lu-sql.ts → gpu-sql.ts} +134 -125
- package/src/gpu-sql/index.ts +2 -2
- package/dist/gpu-sql/lu-sql.d.ts.map +0 -1
- package/dist/gpu-sql/lu-sql.js.map +0 -1
|
@@ -4,19 +4,19 @@
|
|
|
4
4
|
// SPDX-FileComment: Independently implemented for WebGPU; inspired by relational SQL semantics.
|
|
5
5
|
|
|
6
6
|
import type {GPUCommandGraph} from '@luma.gl/graph/gpu-core';
|
|
7
|
-
import {GPUDataFrame
|
|
8
|
-
import {GPUDataFrameQuery
|
|
7
|
+
import {GPUDataFrame} from '../gpu-dataframe/gpu-data-frame';
|
|
8
|
+
import {GPUDataFrameQuery} from '../gpu-dataframe/gpu-data-frame-query';
|
|
9
9
|
import {
|
|
10
|
-
GPUExpression
|
|
10
|
+
GPUExpression,
|
|
11
11
|
column,
|
|
12
12
|
literal,
|
|
13
13
|
parameter,
|
|
14
|
-
type GPUExpressionNode
|
|
14
|
+
type GPUExpressionNode
|
|
15
15
|
} from '../gpu-dataframe/gpu-expression';
|
|
16
|
-
import type {GPUDataFrameJoinType
|
|
16
|
+
import type {GPUDataFrameJoinType} from '../gpu-dataframe/gpu-join-query';
|
|
17
17
|
import type {
|
|
18
|
-
CompiledGPUDataFrameQuery
|
|
19
|
-
GPUDataFrameQueryParameters
|
|
18
|
+
CompiledGPUDataFrameQuery,
|
|
19
|
+
GPUDataFrameQueryParameters
|
|
20
20
|
} from '../gpu-dataframe/gpu-query-compiler';
|
|
21
21
|
|
|
22
22
|
const MAXIMUM_SQL_LENGTH = 65_536;
|
|
@@ -24,31 +24,34 @@ const MAXIMUM_SQL_TOKENS = 4_096;
|
|
|
24
24
|
const MAXIMUM_EXPRESSION_DEPTH = 64;
|
|
25
25
|
|
|
26
26
|
/** Explicit GPU dataframe registrations; Arrow remains exclusively an adapter boundary. */
|
|
27
|
-
export type
|
|
27
|
+
export type GPUSQLTables = Readonly<Record<string, GPUDataFrame>>;
|
|
28
28
|
|
|
29
29
|
/** Defaults for named SQL parameters such as `:minimumFare`; values remain caller-controlled. */
|
|
30
|
-
export type
|
|
30
|
+
export type GPUSQLQueryOptions = Readonly<{parameters?: GPUDataFrameQueryParameters}>;
|
|
31
31
|
|
|
32
|
-
type
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
type GPUSQLToken = Readonly<{
|
|
33
|
+
kind: 'identifier' | 'number' | 'parameter' | 'symbol';
|
|
34
|
+
value: string;
|
|
35
|
+
}>;
|
|
36
|
+
type GPUSQLSelectItem = Readonly<{
|
|
37
|
+
expression?: GPUExpressionNode;
|
|
35
38
|
aggregate?: 'count' | 'sum' | 'min' | 'max' | 'mean';
|
|
36
39
|
aggregateColumn?: string;
|
|
37
40
|
alias?: string;
|
|
38
41
|
star?: boolean;
|
|
39
42
|
}>;
|
|
40
|
-
type
|
|
41
|
-
items: readonly
|
|
43
|
+
type GPUSQLStatement = Readonly<{
|
|
44
|
+
items: readonly GPUSQLSelectItem[];
|
|
42
45
|
table: string;
|
|
43
46
|
alias?: string;
|
|
44
47
|
join?: Readonly<{
|
|
45
|
-
type:
|
|
48
|
+
type: GPUDataFrameJoinType;
|
|
46
49
|
table: string;
|
|
47
50
|
alias?: string;
|
|
48
51
|
left: string;
|
|
49
52
|
right: string;
|
|
50
53
|
}>;
|
|
51
|
-
predicate?:
|
|
54
|
+
predicate?: GPUExpressionNode;
|
|
52
55
|
groupBy?: string;
|
|
53
56
|
orderBy?: Readonly<{
|
|
54
57
|
column: string;
|
|
@@ -58,33 +61,39 @@ type LuSQLStatement = Readonly<{
|
|
|
58
61
|
limit?: number;
|
|
59
62
|
}>;
|
|
60
63
|
|
|
61
|
-
type
|
|
62
|
-
compile: (graph: GPUCommandGraph<
|
|
64
|
+
type GPUSQLCompiledPlan = Readonly<{
|
|
65
|
+
compile: (graph: GPUCommandGraph<GPUDataFrameQueryParameters>) => CompiledGPUDataFrameQuery;
|
|
63
66
|
}>;
|
|
64
67
|
|
|
65
|
-
type
|
|
68
|
+
type GPUSQLAggregationDefinitions = Record<
|
|
66
69
|
string,
|
|
67
70
|
'count' | {sum: string} | {min: string} | {max: string} | {mean: string}
|
|
68
71
|
>;
|
|
69
72
|
|
|
70
73
|
/** Runtime-validated SQL names cross the strongly typed dataframe boundary exactly once. */
|
|
71
|
-
type
|
|
72
|
-
predicates: readonly
|
|
73
|
-
filter: (predicate:
|
|
74
|
-
select: (names: readonly string[]) =>
|
|
75
|
-
withColumn: (
|
|
74
|
+
type GPUSQLDynamicQuery = GPUSQLCompiledPlan & {
|
|
75
|
+
predicates: readonly GPUExpression<boolean, string>[];
|
|
76
|
+
filter: (predicate: GPUExpression<boolean, string>) => GPUSQLDynamicQuery;
|
|
77
|
+
select: (names: readonly string[]) => GPUSQLDynamicQuery;
|
|
78
|
+
withColumn: (
|
|
79
|
+
name: string,
|
|
80
|
+
expression: GPUExpression<number | null, string>
|
|
81
|
+
) => GPUSQLDynamicQuery;
|
|
76
82
|
groupBy: (key: string) => {
|
|
77
|
-
aggregate: (definitions:
|
|
83
|
+
aggregate: (definitions: GPUSQLAggregationDefinitions) => GPUSQLCompiledPlan;
|
|
78
84
|
};
|
|
79
|
-
aggregate: (definitions:
|
|
85
|
+
aggregate: (definitions: GPUSQLAggregationDefinitions) => GPUSQLCompiledPlan;
|
|
80
86
|
sortByGlobal: (
|
|
81
87
|
name: string,
|
|
82
88
|
options: {direction: 'ascending' | 'descending'; nulls?: 'first' | 'last'}
|
|
83
|
-
) =>
|
|
84
|
-
innerJoin: (
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
89
|
+
) => GPUSQLCompiledPlan & {topK: (limit: number) => GPUSQLCompiledPlan};
|
|
90
|
+
innerJoin: (
|
|
91
|
+
right: GPUDataFrame,
|
|
92
|
+
options: {leftOn: string; rightOn: string}
|
|
93
|
+
) => GPUSQLCompiledPlan;
|
|
94
|
+
leftJoin: (right: GPUDataFrame, options: {leftOn: string; rightOn: string}) => GPUSQLCompiledPlan;
|
|
95
|
+
semiJoin: (right: GPUDataFrame, options: {leftOn: string; rightOn: string}) => GPUSQLCompiledPlan;
|
|
96
|
+
antiJoin: (right: GPUDataFrame, options: {leftOn: string; rightOn: string}) => GPUSQLCompiledPlan;
|
|
88
97
|
};
|
|
89
98
|
|
|
90
99
|
/**
|
|
@@ -93,17 +102,17 @@ type LuSQLDynamicQuery = LuSQLCompiledPlan & {
|
|
|
93
102
|
* Parsing and planning never upload data, submit work, or read GPU buffers. Arrow upload/output
|
|
94
103
|
* remain explicit `@luma.gl/graph/arrow` operations around the shared GPU table execution core.
|
|
95
104
|
*/
|
|
96
|
-
export class
|
|
97
|
-
private readonly tables: Readonly<Record<string,
|
|
105
|
+
export class GPUSQLContext {
|
|
106
|
+
private readonly tables: Readonly<Record<string, GPUDataFrame>>;
|
|
98
107
|
|
|
99
|
-
constructor(tables:
|
|
100
|
-
const registered: Record<string,
|
|
108
|
+
constructor(tables: GPUSQLTables) {
|
|
109
|
+
const registered: Record<string, GPUDataFrame> = {};
|
|
101
110
|
for (const [name, table] of Object.entries(tables)) {
|
|
102
|
-
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name) || !(table instanceof
|
|
103
|
-
throw new Error('
|
|
111
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name) || !(table instanceof GPUDataFrame)) {
|
|
112
|
+
throw new Error('GPUSQL requires valid names mapped to existing GPU dataframes');
|
|
104
113
|
}
|
|
105
114
|
const canonical = name.toLowerCase();
|
|
106
|
-
if (registered[canonical]) throw new Error(`
|
|
115
|
+
if (registered[canonical]) throw new Error(`GPUSQL table "${name}" is already registered`);
|
|
107
116
|
registered[canonical] = table;
|
|
108
117
|
}
|
|
109
118
|
this.tables = Object.freeze(registered);
|
|
@@ -111,56 +120,56 @@ export class LuSQLContext {
|
|
|
111
120
|
}
|
|
112
121
|
|
|
113
122
|
/** Parses and validates one immutable SELECT plan without touching registered GPU sources. */
|
|
114
|
-
query(source: string, options:
|
|
115
|
-
const parser = new
|
|
123
|
+
query(source: string, options: GPUSQLQueryOptions = {}): GPUSQLQuery {
|
|
124
|
+
const parser = new GPUSQLParser(source, options.parameters ?? {});
|
|
116
125
|
const statement = parser.parse();
|
|
117
|
-
return new
|
|
126
|
+
return new GPUSQLQuery(source, buildGPUSQLPlan(this.tables, statement));
|
|
118
127
|
}
|
|
119
128
|
}
|
|
120
129
|
|
|
121
130
|
/** Immutable SQL plan lowered directly into existing reusable GPU dataframe command graphs. */
|
|
122
|
-
export class
|
|
131
|
+
export class GPUSQLQuery {
|
|
123
132
|
/** Original application-owned SQL text, retained solely for diagnostics and inspection. */
|
|
124
133
|
readonly sql: string;
|
|
125
|
-
private readonly plan:
|
|
134
|
+
private readonly plan: GPUSQLCompiledPlan;
|
|
126
135
|
|
|
127
136
|
/** @internal */
|
|
128
|
-
constructor(sql: string, plan:
|
|
137
|
+
constructor(sql: string, plan: GPUSQLCompiledPlan) {
|
|
129
138
|
this.sql = sql;
|
|
130
139
|
this.plan = plan;
|
|
131
140
|
Object.freeze(this);
|
|
132
141
|
}
|
|
133
142
|
|
|
134
143
|
/** Compiles existing dataframe operations into the caller-owned GPU command graph. */
|
|
135
|
-
compile(graph: GPUCommandGraph<
|
|
144
|
+
compile(graph: GPUCommandGraph<GPUDataFrameQueryParameters>): CompiledGPUDataFrameQuery {
|
|
136
145
|
return this.plan.compile(graph);
|
|
137
146
|
}
|
|
138
147
|
}
|
|
139
148
|
|
|
140
|
-
function
|
|
141
|
-
tables: Readonly<Record<string,
|
|
142
|
-
statement:
|
|
143
|
-
):
|
|
149
|
+
function buildGPUSQLPlan(
|
|
150
|
+
tables: Readonly<Record<string, GPUDataFrame>>,
|
|
151
|
+
statement: GPUSQLStatement
|
|
152
|
+
): GPUSQLCompiledPlan {
|
|
144
153
|
const frame = tables[statement.table.toLowerCase()];
|
|
145
|
-
if (!frame) throw new Error(`
|
|
146
|
-
let query = new
|
|
154
|
+
if (!frame) throw new Error(`GPUSQL table "${statement.table}" is not registered`);
|
|
155
|
+
let query = new GPUDataFrameQuery(frame, [], frame.columnNames) as unknown as GPUSQLDynamicQuery;
|
|
147
156
|
const qualifier = statement.alias ?? statement.table;
|
|
148
|
-
const resolve = (name: string): string =>
|
|
157
|
+
const resolve = (name: string): string => resolveGPUSQLColumn(name, qualifier, frame.columnNames);
|
|
149
158
|
if (statement.predicate) {
|
|
150
159
|
query = query.filter(
|
|
151
|
-
new
|
|
160
|
+
new GPUExpression<boolean, string>(resolveGPUSQLExpression(statement.predicate, resolve))
|
|
152
161
|
);
|
|
153
162
|
}
|
|
154
163
|
|
|
155
164
|
const aggregates = statement.items.filter(item => item.aggregate);
|
|
156
165
|
if (aggregates.length > 0) {
|
|
157
166
|
if (statement.join || statement.orderBy || statement.limit !== undefined) {
|
|
158
|
-
throw new Error('
|
|
167
|
+
throw new Error('GPUSQL aggregate queries do not yet support joins, ordering, or limits');
|
|
159
168
|
}
|
|
160
|
-
const definitions:
|
|
169
|
+
const definitions: GPUSQLAggregationDefinitions = {};
|
|
161
170
|
for (const item of aggregates) {
|
|
162
171
|
const name = item.alias ?? `${item.aggregate}_${item.aggregateColumn ?? 'all'}`;
|
|
163
|
-
if (definitions[name]) throw new Error(`
|
|
172
|
+
if (definitions[name]) throw new Error(`GPUSQL aggregate alias "${name}" is duplicated`);
|
|
164
173
|
definitions[name] =
|
|
165
174
|
item.aggregate === 'count'
|
|
166
175
|
? 'count'
|
|
@@ -175,51 +184,51 @@ function buildLuSQLPlan(
|
|
|
175
184
|
const nonAggregates = statement.items.filter(item => !item.aggregate);
|
|
176
185
|
if (
|
|
177
186
|
nonAggregates.length !== 1 ||
|
|
178
|
-
|
|
187
|
+
resolveGPUSQLProjectionName(nonAggregates[0], resolve) !== key
|
|
179
188
|
) {
|
|
180
|
-
throw new Error('
|
|
189
|
+
throw new Error('GPUSQL grouped queries must select their single GROUP BY key');
|
|
181
190
|
}
|
|
182
191
|
return query.groupBy(key).aggregate(definitions);
|
|
183
192
|
}
|
|
184
193
|
if (aggregates.length !== statement.items.length) {
|
|
185
|
-
throw new Error('
|
|
194
|
+
throw new Error('GPUSQL global aggregates cannot mix ordinary source columns');
|
|
186
195
|
}
|
|
187
196
|
return query.aggregate(definitions);
|
|
188
197
|
}
|
|
189
|
-
if (statement.groupBy) throw new Error('
|
|
198
|
+
if (statement.groupBy) throw new Error('GPUSQL GROUP BY requires at least one aggregation');
|
|
190
199
|
|
|
191
200
|
const selected: string[] = [];
|
|
192
201
|
for (const item of statement.items) {
|
|
193
202
|
if (item.star) {
|
|
194
203
|
if (statement.items.length !== 1)
|
|
195
|
-
throw new Error('
|
|
204
|
+
throw new Error('GPUSQL SELECT * cannot include other columns');
|
|
196
205
|
selected.push(...frame.columnNames);
|
|
197
206
|
continue;
|
|
198
207
|
}
|
|
199
|
-
const node =
|
|
208
|
+
const node = resolveGPUSQLExpression(item.expression!, resolve);
|
|
200
209
|
const name = item.alias ?? (node.kind === 'column' ? node.name : undefined);
|
|
201
|
-
if (!name) throw new Error('
|
|
202
|
-
if (selected.includes(name)) throw new Error(`
|
|
210
|
+
if (!name) throw new Error('GPUSQL computed SELECT expressions require an explicit AS alias');
|
|
211
|
+
if (selected.includes(name)) throw new Error(`GPUSQL output column "${name}" is duplicated`);
|
|
203
212
|
if (node.kind !== 'column' || node.name !== name) {
|
|
204
213
|
if (frame.columnNames.includes(name)) {
|
|
205
|
-
throw new Error(`
|
|
214
|
+
throw new Error(`GPUSQL derived alias "${name}" conflicts with an existing source column`);
|
|
206
215
|
}
|
|
207
|
-
query = query.withColumn(name, new
|
|
216
|
+
query = query.withColumn(name, new GPUExpression<number | null, string>(node));
|
|
208
217
|
}
|
|
209
218
|
selected.push(name);
|
|
210
219
|
}
|
|
211
220
|
|
|
212
221
|
if (statement.join) {
|
|
213
222
|
if (statement.orderBy || statement.limit !== undefined) {
|
|
214
|
-
throw new Error('
|
|
223
|
+
throw new Error('GPUSQL joined results do not yet support ORDER BY or LIMIT');
|
|
215
224
|
}
|
|
216
225
|
const right = tables[statement.join.table.toLowerCase()];
|
|
217
|
-
if (!right) throw new Error(`
|
|
226
|
+
if (!right) throw new Error(`GPUSQL table "${statement.join.table}" is not registered`);
|
|
218
227
|
const leftKey = resolve(statement.join.left);
|
|
219
228
|
const rightQualifier = statement.join.alias ?? statement.join.table;
|
|
220
|
-
const rightKey =
|
|
229
|
+
const rightKey = resolveGPUSQLColumn(statement.join.right, rightQualifier, right.columnNames);
|
|
221
230
|
if (!selected.includes(leftKey)) {
|
|
222
|
-
throw new Error('
|
|
231
|
+
throw new Error('GPUSQL joins require the left join key to remain selected');
|
|
223
232
|
}
|
|
224
233
|
query = query.select(selected);
|
|
225
234
|
const options = {leftOn: leftKey, rightOn: rightKey};
|
|
@@ -240,7 +249,7 @@ function buildLuSQLPlan(
|
|
|
240
249
|
selected.find(name => name.toLowerCase() === statement.orderBy!.column.toLowerCase()) ??
|
|
241
250
|
resolve(statement.orderBy.column);
|
|
242
251
|
if (!selected.includes(orderColumn)) {
|
|
243
|
-
throw new Error('
|
|
252
|
+
throw new Error('GPUSQL ORDER BY requires the sort column in the SELECT projection');
|
|
244
253
|
}
|
|
245
254
|
const sort = query.select(selected).sortByGlobal(orderColumn, {
|
|
246
255
|
direction: statement.orderBy.direction,
|
|
@@ -248,72 +257,72 @@ function buildLuSQLPlan(
|
|
|
248
257
|
});
|
|
249
258
|
return statement.limit === undefined ? sort : sort.topK(statement.limit);
|
|
250
259
|
}
|
|
251
|
-
if (statement.limit !== undefined) throw new Error('
|
|
260
|
+
if (statement.limit !== undefined) throw new Error('GPUSQL LIMIT currently requires ORDER BY');
|
|
252
261
|
if (query.predicates.length === 0) {
|
|
253
262
|
const firstColumn = frame.columnNames[0];
|
|
254
|
-
if (!firstColumn) throw new Error('
|
|
263
|
+
if (!firstColumn) throw new Error('GPUSQL SELECT requires at least one GPU source column');
|
|
255
264
|
query = query.filter(column(firstColumn).isValid().or(column(firstColumn).isNull()));
|
|
256
265
|
}
|
|
257
266
|
return query.select(selected);
|
|
258
267
|
}
|
|
259
268
|
|
|
260
|
-
function
|
|
261
|
-
item:
|
|
269
|
+
function resolveGPUSQLProjectionName(
|
|
270
|
+
item: GPUSQLSelectItem,
|
|
262
271
|
resolve: (name: string) => string
|
|
263
272
|
): string {
|
|
264
273
|
if (item.expression?.kind !== 'column') {
|
|
265
|
-
throw new Error('
|
|
274
|
+
throw new Error('GPUSQL grouped source expressions must reference the group key directly');
|
|
266
275
|
}
|
|
267
276
|
return resolve(item.expression.name);
|
|
268
277
|
}
|
|
269
278
|
|
|
270
|
-
function
|
|
279
|
+
function resolveGPUSQLColumn(name: string, qualifier: string, columns: readonly string[]): string {
|
|
271
280
|
const parts = name.split('.');
|
|
272
281
|
if (
|
|
273
282
|
parts.length > 2 ||
|
|
274
283
|
(parts.length === 2 && parts[0].toLowerCase() !== qualifier.toLowerCase())
|
|
275
284
|
) {
|
|
276
|
-
throw new Error(`
|
|
285
|
+
throw new Error(`GPUSQL column "${name}" has an unknown table qualifier`);
|
|
277
286
|
}
|
|
278
287
|
const requested = parts[parts.length - 1];
|
|
279
288
|
const matches = columns.filter(candidate => candidate.toLowerCase() === requested.toLowerCase());
|
|
280
|
-
if (matches.length !== 1) throw new Error(`
|
|
289
|
+
if (matches.length !== 1) throw new Error(`GPUSQL column "${name}" does not exist`);
|
|
281
290
|
return matches[0];
|
|
282
291
|
}
|
|
283
292
|
|
|
284
|
-
function
|
|
285
|
-
expression:
|
|
293
|
+
function resolveGPUSQLExpression(
|
|
294
|
+
expression: GPUExpressionNode,
|
|
286
295
|
resolve: (name: string) => string
|
|
287
|
-
):
|
|
296
|
+
): GPUExpressionNode {
|
|
288
297
|
switch (expression.kind) {
|
|
289
298
|
case 'column':
|
|
290
299
|
return {kind: 'column', name: resolve(expression.name)};
|
|
291
300
|
case 'unary':
|
|
292
|
-
return {...expression, operand:
|
|
301
|
+
return {...expression, operand: resolveGPUSQLExpression(expression.operand, resolve)};
|
|
293
302
|
case 'binary':
|
|
294
303
|
return {
|
|
295
304
|
...expression,
|
|
296
|
-
left:
|
|
297
|
-
right:
|
|
305
|
+
left: resolveGPUSQLExpression(expression.left, resolve),
|
|
306
|
+
right: resolveGPUSQLExpression(expression.right, resolve)
|
|
298
307
|
};
|
|
299
308
|
default:
|
|
300
309
|
return expression;
|
|
301
310
|
}
|
|
302
311
|
}
|
|
303
312
|
|
|
304
|
-
class
|
|
305
|
-
private readonly tokens: readonly
|
|
306
|
-
private readonly parameters:
|
|
313
|
+
class GPUSQLParser {
|
|
314
|
+
private readonly tokens: readonly GPUSQLToken[];
|
|
315
|
+
private readonly parameters: GPUDataFrameQueryParameters;
|
|
307
316
|
private position = 0;
|
|
308
317
|
|
|
309
|
-
constructor(source: string, parameters:
|
|
310
|
-
this.tokens =
|
|
318
|
+
constructor(source: string, parameters: GPUDataFrameQueryParameters) {
|
|
319
|
+
this.tokens = tokenizeGPUSQL(source);
|
|
311
320
|
this.parameters = parameters;
|
|
312
321
|
}
|
|
313
322
|
|
|
314
|
-
parse():
|
|
323
|
+
parse(): GPUSQLStatement {
|
|
315
324
|
this.expectKeyword('SELECT');
|
|
316
|
-
const items:
|
|
325
|
+
const items: GPUSQLSelectItem[] = [];
|
|
317
326
|
do {
|
|
318
327
|
items.push(this.parseSelectItem());
|
|
319
328
|
} while (this.takeSymbol(','));
|
|
@@ -326,9 +335,9 @@ class LuSQLParser {
|
|
|
326
335
|
if (this.takeKeyword('GROUP')) {
|
|
327
336
|
this.expectKeyword('BY');
|
|
328
337
|
groupBy = this.parseIdentifier();
|
|
329
|
-
if (this.takeSymbol(',')) throw new Error('
|
|
338
|
+
if (this.takeSymbol(',')) throw new Error('GPUSQL currently supports one GROUP BY column');
|
|
330
339
|
}
|
|
331
|
-
let orderBy:
|
|
340
|
+
let orderBy: GPUSQLStatement['orderBy'];
|
|
332
341
|
if (this.takeKeyword('ORDER')) {
|
|
333
342
|
this.expectKeyword('BY');
|
|
334
343
|
const sortColumn = this.parseIdentifier();
|
|
@@ -340,22 +349,22 @@ class LuSQLParser {
|
|
|
340
349
|
nulls = this.takeKeyword('FIRST') ? 'first' : this.expectKeyword('LAST') && 'last';
|
|
341
350
|
}
|
|
342
351
|
orderBy = {column: sortColumn, direction, ...(nulls ? {nulls} : {})};
|
|
343
|
-
if (this.takeSymbol(',')) throw new Error('
|
|
352
|
+
if (this.takeSymbol(',')) throw new Error('GPUSQL currently supports one ORDER BY column');
|
|
344
353
|
}
|
|
345
354
|
let limit: number | undefined;
|
|
346
355
|
if (this.takeKeyword('LIMIT')) {
|
|
347
356
|
const token = this.take();
|
|
348
357
|
if (token?.kind !== 'number' || !/^\d+$/.test(token.value)) {
|
|
349
|
-
throw new Error('
|
|
358
|
+
throw new Error('GPUSQL LIMIT requires a nonnegative integer');
|
|
350
359
|
}
|
|
351
360
|
limit = Number(token.value);
|
|
352
361
|
if (!Number.isSafeInteger(limit) || limit > 0xffffffff) {
|
|
353
|
-
throw new Error('
|
|
362
|
+
throw new Error('GPUSQL LIMIT must fit an unsigned 32-bit row count');
|
|
354
363
|
}
|
|
355
364
|
}
|
|
356
365
|
this.takeSymbol(';');
|
|
357
366
|
if (this.peek())
|
|
358
|
-
throw new Error(`
|
|
367
|
+
throw new Error(`GPUSQL does not support unexpected token "${this.peek()!.value}"`);
|
|
359
368
|
return {
|
|
360
369
|
items,
|
|
361
370
|
table,
|
|
@@ -368,7 +377,7 @@ class LuSQLParser {
|
|
|
368
377
|
};
|
|
369
378
|
}
|
|
370
379
|
|
|
371
|
-
private parseSelectItem():
|
|
380
|
+
private parseSelectItem(): GPUSQLSelectItem {
|
|
372
381
|
if (this.takeSymbol('*')) return {star: true};
|
|
373
382
|
const token = this.peek();
|
|
374
383
|
if (
|
|
@@ -382,13 +391,13 @@ class LuSQLParser {
|
|
|
382
391
|
const aggregateColumn = this.takeSymbol('*') ? undefined : this.parseIdentifier();
|
|
383
392
|
this.expectSymbol(')');
|
|
384
393
|
if (!aggregateColumn && aggregate !== 'count') {
|
|
385
|
-
throw new Error('
|
|
394
|
+
throw new Error('GPUSQL only permits COUNT(*) as a wildcard aggregation');
|
|
386
395
|
}
|
|
387
396
|
if (aggregateColumn && aggregate === 'count') {
|
|
388
|
-
throw new Error('
|
|
397
|
+
throw new Error('GPUSQL currently supports COUNT(*) but not nullable COUNT(column)');
|
|
389
398
|
}
|
|
390
399
|
return {
|
|
391
|
-
aggregate: aggregate === 'avg' ? 'mean' : (aggregate as
|
|
400
|
+
aggregate: aggregate === 'avg' ? 'mean' : (aggregate as GPUSQLSelectItem['aggregate']),
|
|
392
401
|
...(aggregateColumn ? {aggregateColumn} : {}),
|
|
393
402
|
...(this.takeKeyword('AS') ? {alias: this.parseIdentifier()} : {})
|
|
394
403
|
};
|
|
@@ -400,8 +409,8 @@ class LuSQLParser {
|
|
|
400
409
|
};
|
|
401
410
|
}
|
|
402
411
|
|
|
403
|
-
private parseOptionalJoin():
|
|
404
|
-
let type:
|
|
412
|
+
private parseOptionalJoin(): GPUSQLStatement['join'] {
|
|
413
|
+
let type: GPUDataFrameJoinType = 'inner';
|
|
405
414
|
if (this.takeKeyword('LEFT')) {
|
|
406
415
|
this.takeKeyword('OUTER');
|
|
407
416
|
type = 'left';
|
|
@@ -413,7 +422,7 @@ class LuSQLParser {
|
|
|
413
422
|
this.takeKeyword('INNER');
|
|
414
423
|
}
|
|
415
424
|
if (!this.takeKeyword('JOIN')) {
|
|
416
|
-
if (type !== 'inner') throw new Error('
|
|
425
|
+
if (type !== 'inner') throw new Error('GPUSQL join modifier must be followed by JOIN');
|
|
417
426
|
return undefined;
|
|
418
427
|
}
|
|
419
428
|
const table = this.parseIdentifier();
|
|
@@ -425,9 +434,9 @@ class LuSQLParser {
|
|
|
425
434
|
return {type, table, ...(alias ? {alias} : {}), left, right};
|
|
426
435
|
}
|
|
427
436
|
|
|
428
|
-
private parseExpression(depth: number, minimumPrecedence = 0):
|
|
437
|
+
private parseExpression(depth: number, minimumPrecedence = 0): GPUExpressionNode {
|
|
429
438
|
if (depth > MAXIMUM_EXPRESSION_DEPTH)
|
|
430
|
-
throw new Error('
|
|
439
|
+
throw new Error('GPUSQL expression nesting exceeds its safe limit');
|
|
431
440
|
let expression = this.parseUnary(depth + 1);
|
|
432
441
|
while (true) {
|
|
433
442
|
if (minimumPrecedence <= 3 && this.takeKeyword('IS')) {
|
|
@@ -441,7 +450,7 @@ class LuSQLParser {
|
|
|
441
450
|
continue;
|
|
442
451
|
}
|
|
443
452
|
const token = this.peek();
|
|
444
|
-
const operation = token ?
|
|
453
|
+
const operation = token ? getGPUSQLBinaryOperator(token.value) : undefined;
|
|
445
454
|
if (!operation || operation.precedence < minimumPrecedence) break;
|
|
446
455
|
this.take();
|
|
447
456
|
expression = {
|
|
@@ -454,7 +463,7 @@ class LuSQLParser {
|
|
|
454
463
|
return expression;
|
|
455
464
|
}
|
|
456
465
|
|
|
457
|
-
private parseUnary(depth: number):
|
|
466
|
+
private parseUnary(depth: number): GPUExpressionNode {
|
|
458
467
|
if (this.takeKeyword('NOT')) {
|
|
459
468
|
return {kind: 'unary', operator: 'not', operand: this.parseExpression(depth, 3)};
|
|
460
469
|
}
|
|
@@ -467,15 +476,15 @@ class LuSQLParser {
|
|
|
467
476
|
return expression;
|
|
468
477
|
}
|
|
469
478
|
const token = this.take();
|
|
470
|
-
if (!token) throw new Error('
|
|
479
|
+
if (!token) throw new Error('GPUSQL expected an expression');
|
|
471
480
|
if (token.kind === 'number') {
|
|
472
481
|
const value = Number(token.value);
|
|
473
|
-
if (!Number.isFinite(value)) throw new Error('
|
|
482
|
+
if (!Number.isFinite(value)) throw new Error('GPUSQL numeric literals must be finite');
|
|
474
483
|
return literal(value).node;
|
|
475
484
|
}
|
|
476
485
|
if (token.kind === 'parameter') {
|
|
477
486
|
if (!Object.hasOwn(this.parameters, token.value)) {
|
|
478
|
-
throw new Error(`
|
|
487
|
+
throw new Error(`GPUSQL parameter ":${token.value}" requires an explicit default`);
|
|
479
488
|
}
|
|
480
489
|
return parameter(token.value, this.parameters[token.value]).node;
|
|
481
490
|
}
|
|
@@ -485,7 +494,7 @@ class LuSQLParser {
|
|
|
485
494
|
if (/^FALSE$/i.test(token.value)) return literal(false).node;
|
|
486
495
|
return column(token.value).node;
|
|
487
496
|
}
|
|
488
|
-
throw new Error(`
|
|
497
|
+
throw new Error(`GPUSQL does not support expression token "${token.value}"`);
|
|
489
498
|
}
|
|
490
499
|
|
|
491
500
|
private parseOptionalTableAlias(): string | undefined {
|
|
@@ -500,12 +509,12 @@ class LuSQLParser {
|
|
|
500
509
|
private parseIdentifier(): string {
|
|
501
510
|
const token = this.take();
|
|
502
511
|
if (token?.kind !== 'identifier')
|
|
503
|
-
throw new Error('
|
|
512
|
+
throw new Error('GPUSQL expected a table or column identifier');
|
|
504
513
|
return token.value;
|
|
505
514
|
}
|
|
506
515
|
|
|
507
516
|
private expectKeyword(keyword: string): true {
|
|
508
|
-
if (!this.takeKeyword(keyword)) throw new Error(`
|
|
517
|
+
if (!this.takeKeyword(keyword)) throw new Error(`GPUSQL expected ${keyword}`);
|
|
509
518
|
return true;
|
|
510
519
|
}
|
|
511
520
|
|
|
@@ -517,7 +526,7 @@ class LuSQLParser {
|
|
|
517
526
|
}
|
|
518
527
|
|
|
519
528
|
private expectSymbol(symbol: string): void {
|
|
520
|
-
if (!this.takeSymbol(symbol)) throw new Error(`
|
|
529
|
+
if (!this.takeSymbol(symbol)) throw new Error(`GPUSQL expected "${symbol}"`);
|
|
521
530
|
}
|
|
522
531
|
|
|
523
532
|
private takeSymbol(symbol: string): boolean {
|
|
@@ -527,43 +536,43 @@ class LuSQLParser {
|
|
|
527
536
|
return true;
|
|
528
537
|
}
|
|
529
538
|
|
|
530
|
-
private take():
|
|
539
|
+
private take(): GPUSQLToken | undefined {
|
|
531
540
|
const token = this.tokens[this.position];
|
|
532
541
|
if (token) this.position++;
|
|
533
542
|
return token;
|
|
534
543
|
}
|
|
535
544
|
|
|
536
|
-
private peek():
|
|
545
|
+
private peek(): GPUSQLToken | undefined {
|
|
537
546
|
return this.tokens[this.position];
|
|
538
547
|
}
|
|
539
548
|
}
|
|
540
549
|
|
|
541
|
-
function
|
|
550
|
+
function tokenizeGPUSQL(source: string): GPUSQLToken[] {
|
|
542
551
|
if (typeof source !== 'string' || source.length === 0 || source.length > MAXIMUM_SQL_LENGTH) {
|
|
543
|
-
throw new Error('
|
|
552
|
+
throw new Error('GPUSQL requires a nonempty, safely bounded SELECT statement');
|
|
544
553
|
}
|
|
545
554
|
const expression =
|
|
546
555
|
/\s+|(?:--[^\n\r]*)|(?::([A-Za-z_][A-Za-z0-9_]*))|([A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)?)|(\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)|(<=|>=|<>|!=|[(),;*+\-/=<>])/gy;
|
|
547
|
-
const tokens:
|
|
556
|
+
const tokens: GPUSQLToken[] = [];
|
|
548
557
|
let cursor = 0;
|
|
549
558
|
while (cursor < source.length) {
|
|
550
559
|
expression.lastIndex = cursor;
|
|
551
560
|
const match = expression.exec(source);
|
|
552
|
-
if (!match) throw new Error(`
|
|
561
|
+
if (!match) throw new Error(`GPUSQL contains an unsupported token at character ${cursor}`);
|
|
553
562
|
cursor = expression.lastIndex;
|
|
554
563
|
if (match[1]) tokens.push({kind: 'parameter', value: match[1]});
|
|
555
564
|
else if (match[2]) tokens.push({kind: 'identifier', value: match[2]});
|
|
556
565
|
else if (match[3]) tokens.push({kind: 'number', value: match[3]});
|
|
557
566
|
else if (match[4]) tokens.push({kind: 'symbol', value: match[4]});
|
|
558
567
|
if (tokens.length > MAXIMUM_SQL_TOKENS)
|
|
559
|
-
throw new Error('
|
|
568
|
+
throw new Error('GPUSQL statement exceeds its safe token limit');
|
|
560
569
|
}
|
|
561
570
|
return tokens;
|
|
562
571
|
}
|
|
563
572
|
|
|
564
|
-
function
|
|
573
|
+
function getGPUSQLBinaryOperator(value: string):
|
|
565
574
|
| Readonly<{
|
|
566
|
-
operator: Extract<
|
|
575
|
+
operator: Extract<GPUExpressionNode, {kind: 'binary'}>['operator'];
|
|
567
576
|
precedence: number;
|
|
568
577
|
}>
|
|
569
578
|
| undefined {
|
package/src/gpu-sql/index.ts
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
export {
|
|
6
|
-
export type {
|
|
5
|
+
export {GPUSQLContext, GPUSQLQuery} from './gpu-sql';
|
|
6
|
+
export type {GPUSQLQueryOptions, GPUSQLTables} from './gpu-sql';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lu-sql.d.ts","sourceRoot":"","sources":["../../src/gpu-sql/lu-sql.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAC,YAAY,IAAI,WAAW,EAAC,2CAAwC;AAU5E,OAAO,KAAK,EACV,yBAAyB,IAAI,wBAAwB,EACrD,2BAA2B,IAAI,0BAA0B,EAC1D,+CAA4C;AAM7C,2FAA2F;AAC3F,MAAM,MAAM,WAAW,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;AAEhE,iGAAiG;AACjG,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC;IAAC,UAAU,CAAC,EAAE,0BAA0B,CAAA;CAAC,CAAC,CAAC;AA+BpF,KAAK,iBAAiB,GAAG,QAAQ,CAAC;IAChC,OAAO,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,0BAA0B,CAAC,KAAK,wBAAwB,CAAC;CAC3F,CAAC,CAAC;AA2BH;;;;;GAKG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAwC;gBAEnD,MAAM,EAAE,WAAW;IAc/B,8FAA8F;IAC9F,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,UAAU;CAKnE;AAED,+FAA+F;AAC/F,qBAAa,UAAU;IACrB,2FAA2F;IAC3F,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAoB;IAEzC,gBAAgB;gBACJ,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,iBAAiB;IAMhD,sFAAsF;IACtF,OAAO,CAAC,KAAK,EAAE,eAAe,CAAC,0BAA0B,CAAC,GAAG,wBAAwB;CAGtF"}
|