@luma.gl/graph 10.0.0-alpha.11 → 10.0.0-alpha.13

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.
Files changed (131) hide show
  1. package/LICENSE +4 -4
  2. package/THIRD-PARTY-NOTICES.md +19 -0
  3. package/dist/arrow/gpu/arrow-gpu-vector-adapters.cjs +6 -6
  4. package/dist/arrow/gpu/arrow-gpu-vector-adapters.d.ts.map +1 -1
  5. package/dist/arrow/gpu/arrow-gpu-vector-adapters.js +16 -6
  6. package/dist/arrow/gpu/arrow-gpu-vector-adapters.js.map +1 -1
  7. package/dist/arrow/gpu/arrow-gpu-vector-conversion.cjs +3 -2
  8. package/dist/arrow/gpu/arrow-gpu-vector-conversion.d.ts.map +1 -1
  9. package/dist/arrow/gpu/arrow-gpu-vector-conversion.js +3 -2
  10. package/dist/arrow/gpu/arrow-gpu-vector-conversion.js.map +1 -1
  11. package/dist/arrow/index.cjs.map +2 -2
  12. package/dist/arrow/internal.cjs.map +2 -2
  13. package/dist/dist.dev.js +2148 -454
  14. package/dist/dist.min.js +437 -123
  15. package/dist/experimental/index.cjs.map +1 -1
  16. package/dist/experimental/simulation/index.cjs.map +1 -1
  17. package/dist/gpu-core/compiled-gpu-graph.cjs +15 -0
  18. package/dist/gpu-core/compiled-gpu-graph.d.ts +1 -0
  19. package/dist/gpu-core/compiled-gpu-graph.d.ts.map +1 -1
  20. package/dist/gpu-core/compiled-gpu-graph.js +15 -0
  21. package/dist/gpu-core/compiled-gpu-graph.js.map +1 -1
  22. package/dist/gpu-core/gpu-command-graph.cjs +9 -0
  23. package/dist/gpu-core/gpu-command-graph.d.ts +2 -0
  24. package/dist/gpu-core/gpu-command-graph.d.ts.map +1 -1
  25. package/dist/gpu-core/gpu-command-graph.js +9 -0
  26. package/dist/gpu-core/gpu-command-graph.js.map +1 -1
  27. package/dist/gpu-core/gpu-expression-plan.cjs +4 -0
  28. package/dist/gpu-core/gpu-expression-plan.d.ts.map +1 -1
  29. package/dist/gpu-core/gpu-expression-plan.js +5 -0
  30. package/dist/gpu-core/gpu-expression-plan.js.map +1 -1
  31. package/dist/gpu-core/gpu-graph-compiler.cjs +48 -0
  32. package/dist/gpu-core/gpu-graph-compiler.d.ts +1 -0
  33. package/dist/gpu-core/gpu-graph-compiler.d.ts.map +1 -1
  34. package/dist/gpu-core/gpu-graph-compiler.js +50 -0
  35. package/dist/gpu-core/gpu-graph-compiler.js.map +1 -1
  36. package/dist/gpu-core/gpu-group-aggregation.cjs +7 -15
  37. package/dist/gpu-core/gpu-group-aggregation.d.ts.map +1 -1
  38. package/dist/gpu-core/gpu-group-aggregation.js +7 -18
  39. package/dist/gpu-core/gpu-group-aggregation.js.map +1 -1
  40. package/dist/gpu-core/gpu-reduction.cjs +6 -14
  41. package/dist/gpu-core/gpu-reduction.d.ts.map +1 -1
  42. package/dist/gpu-core/gpu-reduction.js +6 -15
  43. package/dist/gpu-core/gpu-reduction.js.map +1 -1
  44. package/dist/gpu-core/gpu-semantic-aggregation-operation.cjs +112 -0
  45. package/dist/gpu-core/gpu-semantic-aggregation-operation.d.ts +71 -0
  46. package/dist/gpu-core/gpu-semantic-aggregation-operation.d.ts.map +1 -0
  47. package/dist/gpu-core/gpu-semantic-aggregation-operation.js +92 -0
  48. package/dist/gpu-core/gpu-semantic-aggregation-operation.js.map +1 -0
  49. package/dist/gpu-core/gpu-shader-pass-kernel.cjs +267 -0
  50. package/dist/gpu-core/gpu-shader-pass-kernel.d.ts +29 -0
  51. package/dist/gpu-core/gpu-shader-pass-kernel.d.ts.map +1 -0
  52. package/dist/gpu-core/gpu-shader-pass-kernel.js +260 -0
  53. package/dist/gpu-core/gpu-shader-pass-kernel.js.map +1 -0
  54. package/dist/gpu-core/gpu-shader-pass-operation.cjs +85 -0
  55. package/dist/gpu-core/gpu-shader-pass-operation.d.ts +32 -0
  56. package/dist/gpu-core/gpu-shader-pass-operation.d.ts.map +1 -0
  57. package/dist/gpu-core/gpu-shader-pass-operation.js +74 -0
  58. package/dist/gpu-core/gpu-shader-pass-operation.js.map +1 -0
  59. package/dist/gpu-core/index.cjs +8 -0
  60. package/dist/gpu-core/index.cjs.map +3 -3
  61. package/dist/gpu-core/index.d.ts +4 -0
  62. package/dist/gpu-core/index.d.ts.map +1 -1
  63. package/dist/gpu-core/index.js +2 -0
  64. package/dist/gpu-core/index.js.map +1 -1
  65. package/dist/gpu-data/cpu-gpu-vector-transfer.cjs +7 -2
  66. package/dist/gpu-data/cpu-gpu-vector-transfer.d.ts.map +1 -1
  67. package/dist/gpu-data/cpu-gpu-vector-transfer.js +9 -6
  68. package/dist/gpu-data/cpu-gpu-vector-transfer.js.map +1 -1
  69. package/dist/gpu-data/gpu-data-buffer-props.cjs +41 -0
  70. package/dist/gpu-data/gpu-data-buffer-props.d.ts +5 -0
  71. package/dist/gpu-data/gpu-data-buffer-props.d.ts.map +1 -0
  72. package/dist/gpu-data/gpu-data-buffer-props.js +19 -0
  73. package/dist/gpu-data/gpu-data-buffer-props.js.map +1 -0
  74. package/dist/gpu-data/index.cjs.map +3 -3
  75. package/dist/gpu-dggs/benchmarks.cjs.map +1 -1
  76. package/dist/gpu-graph/index.cjs +5 -0
  77. package/dist/gpu-graph/index.cjs.map +3 -3
  78. package/dist/gpu-graph/index.d.ts +2 -0
  79. package/dist/gpu-graph/index.d.ts.map +1 -1
  80. package/dist/gpu-graph/index.js +1 -0
  81. package/dist/gpu-graph/index.js.map +1 -1
  82. package/dist/gpu-graph-algorithms/benchmarks.cjs.map +1 -1
  83. package/dist/gpu-graph-algorithms/index.cjs.map +1 -1
  84. package/dist/gpu-physics/index.cjs.map +1 -1
  85. package/dist/gpu-project/benchmarks.cjs.map +2 -2
  86. package/dist/gpu-project/crs.cjs.map +2 -2
  87. package/dist/gpu-project/graph.cjs.map +3 -3
  88. package/dist/gpu-project/index.cjs.map +2 -2
  89. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.cjs +53 -5
  90. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.d.ts +3 -1
  91. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.d.ts.map +1 -1
  92. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.js +58 -5
  93. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.js.map +1 -1
  94. package/dist/gpu-simple-graph/index.cjs.map +3 -3
  95. package/dist/gpu-sql/gpu-sql.cjs +112 -100
  96. package/dist/gpu-sql/gpu-sql.d.ts +1 -1
  97. package/dist/gpu-sql/gpu-sql.d.ts.map +1 -1
  98. package/dist/gpu-sql/gpu-sql.js +120 -102
  99. package/dist/gpu-sql/gpu-sql.js.map +1 -1
  100. package/dist/gpu-sql/index.cjs.map +2 -2
  101. package/dist/gpu-tables/index.cjs.map +2 -2
  102. package/dist/gpu-tables/table/gpu-table.cjs +2 -2
  103. package/dist/gpu-tables/table/gpu-table.d.ts.map +1 -1
  104. package/dist/gpu-tables/table/gpu-table.js +2 -2
  105. package/dist/gpu-tables/table/gpu-table.js.map +1 -1
  106. package/dist/index.cjs +8 -0
  107. package/dist/index.cjs.map +3 -3
  108. package/dist/index.d.ts +4 -0
  109. package/dist/index.d.ts.map +1 -1
  110. package/dist/index.js +2 -0
  111. package/dist/index.js.map +1 -1
  112. package/package.json +10 -8
  113. package/src/arrow/gpu/arrow-gpu-vector-adapters.ts +17 -6
  114. package/src/arrow/gpu/arrow-gpu-vector-conversion.ts +3 -2
  115. package/src/gpu-core/compiled-gpu-graph.ts +18 -0
  116. package/src/gpu-core/gpu-command-graph.ts +10 -0
  117. package/src/gpu-core/gpu-expression-plan.ts +6 -0
  118. package/src/gpu-core/gpu-graph-compiler.ts +61 -0
  119. package/src/gpu-core/gpu-group-aggregation.ts +38 -50
  120. package/src/gpu-core/gpu-reduction.ts +32 -42
  121. package/src/gpu-core/gpu-semantic-aggregation-operation.ts +150 -0
  122. package/src/gpu-core/gpu-shader-pass-kernel.ts +307 -0
  123. package/src/gpu-core/gpu-shader-pass-operation.ts +103 -0
  124. package/src/gpu-core/index.ts +8 -0
  125. package/src/gpu-data/cpu-gpu-vector-transfer.ts +10 -7
  126. package/src/gpu-data/gpu-data-buffer-props.ts +25 -0
  127. package/src/gpu-graph/index.ts +9 -0
  128. package/src/gpu-simple-graph/compiled-gpu-expression-graph.ts +59 -5
  129. package/src/gpu-sql/gpu-sql.ts +130 -103
  130. package/src/gpu-tables/table/gpu-table.ts +2 -1
  131. package/src/index.ts +11 -0
@@ -21,12 +21,12 @@ __export(gpu_sql_exports, {
21
21
  GPUSQLQuery: () => GPUSQLQuery
22
22
  });
23
23
  module.exports = __toCommonJS(gpu_sql_exports);
24
+ var import_sql = require("@math.gl/core/sql");
24
25
  var import_gpu_data_frame = require("../gpu-dataframe/gpu-data-frame.cjs");
25
26
  var import_gpu_data_frame_query = require("../gpu-dataframe/gpu-data-frame-query.cjs");
26
27
  var import_gpu_expression = require("../gpu-dataframe/gpu-expression.cjs");
27
28
  const MAXIMUM_SQL_LENGTH = 65536;
28
29
  const MAXIMUM_SQL_TOKENS = 4096;
29
- const MAXIMUM_EXPRESSION_DEPTH = 64;
30
30
  class GPUSQLContext {
31
31
  tables;
32
32
  constructor(tables) {
@@ -151,13 +151,14 @@ function buildGPUSQLPlan(tables, statement) {
151
151
  }
152
152
  }
153
153
  if (statement.orderBy) {
154
- const orderColumn = selected.find((name) => name.toLowerCase() === statement.orderBy.column.toLowerCase()) ?? resolve(statement.orderBy.column);
154
+ const { column: requested, direction, nulls } = statement.orderBy;
155
+ const orderColumn = selected.includes(requested) ? requested : resolve(requested);
155
156
  if (!selected.includes(orderColumn)) {
156
157
  throw new Error("GPUSQL ORDER BY requires the sort column in the SELECT projection");
157
158
  }
158
159
  const sort = query.select(selected).sortByGlobal(orderColumn, {
159
- direction: statement.orderBy.direction,
160
- ...statement.orderBy.nulls ? { nulls: statement.orderBy.nulls } : {}
160
+ direction,
161
+ ...nulls ? { nulls } : {}
161
162
  });
162
163
  return statement.limit === void 0 ? sort : sort.topK(statement.limit);
163
164
  }
@@ -184,10 +185,9 @@ function resolveGPUSQLColumn(name, qualifier, columns) {
184
185
  throw new Error(`GPUSQL column "${name}" has an unknown table qualifier`);
185
186
  }
186
187
  const requested = parts[parts.length - 1];
187
- const matches = columns.filter((candidate) => candidate.toLowerCase() === requested.toLowerCase());
188
- if (matches.length !== 1)
188
+ if (!columns.includes(requested))
189
189
  throw new Error(`GPUSQL column "${name}" does not exist`);
190
- return matches[0];
190
+ return requested;
191
191
  }
192
192
  function resolveGPUSQLExpression(expression, resolve) {
193
193
  switch (expression.kind) {
@@ -223,7 +223,7 @@ class GPUSQLParser {
223
223
  const table = this.parseIdentifier();
224
224
  const alias = this.parseOptionalTableAlias();
225
225
  const join = this.parseOptionalJoin();
226
- const predicate = this.takeKeyword("WHERE") ? this.parseExpression(0) : void 0;
226
+ const predicate = this.takeKeyword("WHERE") ? this.parseExpression() : void 0;
227
227
  let groupBy;
228
228
  if (this.takeKeyword("GROUP")) {
229
229
  this.expectKeyword("BY");
@@ -292,7 +292,7 @@ class GPUSQLParser {
292
292
  ...this.takeKeyword("AS") ? { alias: this.parseIdentifier() } : {}
293
293
  };
294
294
  }
295
- const expression = this.parseExpression(0);
295
+ const expression = this.parseExpression();
296
296
  return {
297
297
  ...expression ? { expression } : {},
298
298
  ...this.takeKeyword("AS") ? { alias: this.parseIdentifier() } : {}
@@ -323,72 +323,47 @@ class GPUSQLParser {
323
323
  const right = this.parseIdentifier();
324
324
  return { type, table, ...alias ? { alias } : {}, left, right };
325
325
  }
326
- parseExpression(depth, minimumPrecedence = 0) {
327
- if (depth > MAXIMUM_EXPRESSION_DEPTH)
328
- throw new Error("GPUSQL expression nesting exceeds its safe limit");
329
- let expression = this.parseUnary(depth + 1);
330
- while (true) {
331
- if (minimumPrecedence <= 3 && this.takeKeyword("IS")) {
332
- const negated = this.takeKeyword("NOT");
333
- this.expectKeyword("NULL");
334
- expression = {
335
- kind: "unary",
336
- operator: negated ? "is-valid" : "is-null",
337
- operand: expression
338
- };
339
- continue;
326
+ parseExpression() {
327
+ const tokens = [];
328
+ let depth = 0;
329
+ let expectsOperand = true;
330
+ for (let token = this.peek(); token; token = this.peek()) {
331
+ const value = token.value;
332
+ if (depth === 0) {
333
+ if (token.kind === "symbol" && (value === "," || value === ";"))
334
+ break;
335
+ if (token.kind === "identifier" && !expectsOperand && /^(AS|FROM|GROUP|ORDER|LIMIT)$/i.test(value)) {
336
+ break;
337
+ }
340
338
  }
341
- const token = this.peek();
342
- const operation = token ? getGPUSQLBinaryOperator(token.value) : void 0;
343
- if (!operation || operation.precedence < minimumPrecedence)
344
- break;
345
- this.take();
346
- expression = {
347
- kind: "binary",
348
- operator: operation.operator,
349
- left: expression,
350
- right: this.parseExpression(depth + 1, operation.precedence + 1)
351
- };
352
- }
353
- return expression;
354
- }
355
- parseUnary(depth) {
356
- if (this.takeKeyword("NOT")) {
357
- return { kind: "unary", operator: "not", operand: this.parseExpression(depth, 3) };
358
- }
359
- if (this.takeSymbol("-")) {
360
- return { kind: "unary", operator: "negate", operand: this.parseUnary(depth + 1) };
361
- }
362
- if (this.takeSymbol("(")) {
363
- const expression = this.parseExpression(depth + 1);
364
- this.expectSymbol(")");
365
- return expression;
366
- }
367
- const token = this.take();
368
- if (!token)
369
- throw new Error("GPUSQL expected an expression");
370
- if (token.kind === "number") {
371
- const value = Number(token.value);
372
- if (!Number.isFinite(value))
373
- throw new Error("GPUSQL numeric literals must be finite");
374
- return (0, import_gpu_expression.literal)(value).node;
375
- }
376
- if (token.kind === "parameter") {
377
- if (!Object.hasOwn(this.parameters, token.value)) {
378
- throw new Error(`GPUSQL parameter ":${token.value}" requires an explicit default`);
339
+ switch (token.kind) {
340
+ case "parameter":
341
+ tokens.push(`":${value}"`);
342
+ expectsOperand = false;
343
+ break;
344
+ case "identifier": {
345
+ const operator = /^NOT$/i.test(value) || !expectsOperand && /^(AND|OR|IS)$/i.test(value);
346
+ const keyword = operator || /^(NULL|TRUE|FALSE)$/i.test(value);
347
+ tokens.push(keyword ? value : `"${value}"`);
348
+ expectsOperand = operator;
349
+ break;
350
+ }
351
+ case "symbol":
352
+ if (value === "(")
353
+ depth++;
354
+ if (value === ")")
355
+ depth--;
356
+ tokens.push(value);
357
+ expectsOperand = value !== ")";
358
+ break;
359
+ case "number":
360
+ tokens.push(value);
361
+ expectsOperand = false;
362
+ break;
379
363
  }
380
- return (0, import_gpu_expression.parameter)(token.value, this.parameters[token.value]).node;
381
- }
382
- if (token.kind === "identifier") {
383
- if (/^NULL$/i.test(token.value))
384
- return (0, import_gpu_expression.literal)(null).node;
385
- if (/^TRUE$/i.test(token.value))
386
- return (0, import_gpu_expression.literal)(true).node;
387
- if (/^FALSE$/i.test(token.value))
388
- return (0, import_gpu_expression.literal)(false).node;
389
- return (0, import_gpu_expression.column)(token.value).node;
364
+ this.take();
390
365
  }
391
- throw new Error(`GPUSQL does not support expression token "${token.value}"`);
366
+ return lowerSQLExpression((0, import_sql.parseSQLExpression)(tokens.join(" ")), this.parameters);
392
367
  }
393
368
  parseOptionalTableAlias() {
394
369
  if (this.takeKeyword("AS"))
@@ -461,36 +436,73 @@ function tokenizeGPUSQL(source) {
461
436
  }
462
437
  return tokens;
463
438
  }
464
- function getGPUSQLBinaryOperator(value) {
465
- switch (value.toUpperCase()) {
466
- case "OR":
467
- return { operator: "or", precedence: 1 };
468
- case "AND":
469
- return { operator: "and", precedence: 2 };
470
- case "=":
471
- return { operator: "equal", precedence: 3 };
472
- case "!=":
473
- case "<>":
474
- return { operator: "not-equal", precedence: 3 };
475
- case ">":
476
- return { operator: "greater-than", precedence: 3 };
477
- case ">=":
478
- return { operator: "greater-than-or-equal", precedence: 3 };
479
- case "<":
480
- return { operator: "less-than", precedence: 3 };
481
- case "<=":
482
- return { operator: "less-than-or-equal", precedence: 3 };
483
- case "+":
484
- return { operator: "add", precedence: 4 };
485
- case "-":
486
- return { operator: "subtract", precedence: 4 };
487
- case "*":
488
- return { operator: "multiply", precedence: 5 };
489
- case "/":
490
- return { operator: "divide", precedence: 5 };
439
+ const SQL_BINARY_OPERATORS = {
440
+ "||": "or",
441
+ "&&": "and",
442
+ "==": "equal",
443
+ "!=": "not-equal",
444
+ ">": "greater-than",
445
+ ">=": "greater-than-or-equal",
446
+ "<": "less-than",
447
+ "<=": "less-than-or-equal",
448
+ "+": "add",
449
+ "-": "subtract",
450
+ "*": "multiply",
451
+ "/": "divide"
452
+ };
453
+ function lowerSQLExpression(node, parameters) {
454
+ const expression = node;
455
+ switch (expression.type) {
456
+ case "Identifier": {
457
+ if (!expression.name.startsWith(":"))
458
+ return (0, import_gpu_expression.column)(expression.name).node;
459
+ const name = expression.name.slice(1);
460
+ if (!Object.hasOwn(parameters, name)) {
461
+ throw new Error(`GPUSQL parameter ":${name}" requires an explicit default`);
462
+ }
463
+ return (0, import_gpu_expression.parameter)(name, parameters[name]).node;
464
+ }
465
+ case "Literal": {
466
+ const value = expression.value;
467
+ if (value === null || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) {
468
+ return (0, import_gpu_expression.literal)(value).node;
469
+ }
470
+ break;
471
+ }
472
+ case "UnaryExpression":
473
+ if (expression.operator === "!" || expression.operator === "-") {
474
+ return {
475
+ kind: "unary",
476
+ operator: expression.operator === "!" ? "not" : "negate",
477
+ operand: lowerSQLExpression(expression.argument, parameters)
478
+ };
479
+ }
480
+ break;
481
+ case "BinaryExpression": {
482
+ const operator = SQL_BINARY_OPERATORS[expression.operator];
483
+ if (operator) {
484
+ return {
485
+ kind: "binary",
486
+ operator,
487
+ left: lowerSQLExpression(expression.left, parameters),
488
+ right: lowerSQLExpression(expression.right, parameters)
489
+ };
490
+ }
491
+ break;
492
+ }
493
+ case "CallExpression":
494
+ if (expression.arguments.length === 1 && (expression.callee["name"] === "isNull" || expression.callee["name"] === "isNotNull")) {
495
+ return {
496
+ kind: "unary",
497
+ operator: expression.callee["name"] === "isNull" ? "is-null" : "is-valid",
498
+ operand: lowerSQLExpression(expression.arguments[0], parameters)
499
+ };
500
+ }
501
+ break;
491
502
  default:
492
- return void 0;
503
+ break;
493
504
  }
505
+ throw new Error(`GPUSQL does not support SQL expression "${expression.type}"`);
494
506
  }
495
507
  // Annotate the CommonJS export names for ESM import in node:
496
508
  0 && (module.exports = {
@@ -11,7 +11,7 @@ type GPUSQLCompiledPlan = Readonly<{
11
11
  compile: (graph: GPUCommandGraph<GPUDataFrameQueryParameters>) => CompiledGPUDataFrameQuery;
12
12
  }>;
13
13
  /**
14
- * Dependency-free SQL frontend over explicitly registered, already GPU-resident dataframes.
14
+ * SQL frontend over explicitly registered, already GPU-resident dataframes.
15
15
  *
16
16
  * Parsing and planning never upload data, submit work, or read GPU buffers. Arrow upload/output
17
17
  * remain explicit `@luma.gl/graph/arrow` operations around the shared GPU table execution core.
@@ -1 +1 @@
1
- {"version":3,"file":"gpu-sql.d.ts","sourceRoot":"","sources":["../../src/gpu-sql/gpu-sql.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAC,YAAY,EAAC,2CAAwC;AAU7D,OAAO,KAAK,EACV,yBAAyB,EACzB,2BAA2B,EAC5B,+CAA4C;AAM7C,2FAA2F;AAC3F,MAAM,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;AAElE,iGAAiG;AACjG,MAAM,MAAM,kBAAkB,GAAG,QAAQ,CAAC;IAAC,UAAU,CAAC,EAAE,2BAA2B,CAAA;CAAC,CAAC,CAAC;AAkCtF,KAAK,kBAAkB,GAAG,QAAQ,CAAC;IACjC,OAAO,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,2BAA2B,CAAC,KAAK,yBAAyB,CAAC;CAC7F,CAAC,CAAC;AAiCH;;;;;GAKG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAyC;gBAEpD,MAAM,EAAE,YAAY;IAchC,8FAA8F;IAC9F,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,WAAW;CAKrE;AAED,+FAA+F;AAC/F,qBAAa,WAAW;IACtB,2FAA2F;IAC3F,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAqB;IAE1C,gBAAgB;gBACJ,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,kBAAkB;IAMjD,sFAAsF;IACtF,OAAO,CAAC,KAAK,EAAE,eAAe,CAAC,2BAA2B,CAAC,GAAG,yBAAyB;CAGxF"}
1
+ {"version":3,"file":"gpu-sql.d.ts","sourceRoot":"","sources":["../../src/gpu-sql/gpu-sql.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAC,YAAY,EAAC,2CAAwC;AAU7D,OAAO,KAAK,EACV,yBAAyB,EACzB,2BAA2B,EAC5B,+CAA4C;AAK7C,2FAA2F;AAC3F,MAAM,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;AAElE,iGAAiG;AACjG,MAAM,MAAM,kBAAkB,GAAG,QAAQ,CAAC;IAAC,UAAU,CAAC,EAAE,2BAA2B,CAAA;CAAC,CAAC,CAAC;AAkCtF,KAAK,kBAAkB,GAAG,QAAQ,CAAC;IACjC,OAAO,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,2BAA2B,CAAC,KAAK,yBAAyB,CAAC;CAC7F,CAAC,CAAC;AAiCH;;;;;GAKG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAyC;gBAEpD,MAAM,EAAE,YAAY;IAchC,8FAA8F;IAC9F,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,WAAW;CAKrE;AAED,+FAA+F;AAC/F,qBAAa,WAAW;IACtB,2FAA2F;IAC3F,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAqB;IAE1C,gBAAgB;gBACJ,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,kBAAkB;IAMjD,sFAAsF;IACtF,OAAO,CAAC,KAAK,EAAE,eAAe,CAAC,2BAA2B,CAAC,GAAG,yBAAyB;CAGxF"}
@@ -2,14 +2,14 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
  // SPDX-FileComment: Independently implemented for WebGPU; inspired by relational SQL semantics.
5
+ import { parseSQLExpression } from '@math.gl/core/sql';
5
6
  import { GPUDataFrame } from "../gpu-dataframe/gpu-data-frame.js";
6
7
  import { GPUDataFrameQuery } from "../gpu-dataframe/gpu-data-frame-query.js";
7
8
  import { GPUExpression, column, literal, parameter } from "../gpu-dataframe/gpu-expression.js";
8
9
  const MAXIMUM_SQL_LENGTH = 65_536;
9
10
  const MAXIMUM_SQL_TOKENS = 4_096;
10
- const MAXIMUM_EXPRESSION_DEPTH = 64;
11
11
  /**
12
- * Dependency-free SQL frontend over explicitly registered, already GPU-resident dataframes.
12
+ * SQL frontend over explicitly registered, already GPU-resident dataframes.
13
13
  *
14
14
  * Parsing and planning never upload data, submit work, or read GPU buffers. Arrow upload/output
15
15
  * remain explicit `@luma.gl/graph/arrow` operations around the shared GPU table execution core.
@@ -143,14 +143,14 @@ function buildGPUSQLPlan(tables, statement) {
143
143
  }
144
144
  }
145
145
  if (statement.orderBy) {
146
- const orderColumn = selected.find(name => name.toLowerCase() === statement.orderBy.column.toLowerCase()) ??
147
- resolve(statement.orderBy.column);
146
+ const { column: requested, direction, nulls } = statement.orderBy;
147
+ const orderColumn = selected.includes(requested) ? requested : resolve(requested);
148
148
  if (!selected.includes(orderColumn)) {
149
149
  throw new Error('GPUSQL ORDER BY requires the sort column in the SELECT projection');
150
150
  }
151
151
  const sort = query.select(selected).sortByGlobal(orderColumn, {
152
- direction: statement.orderBy.direction,
153
- ...(statement.orderBy.nulls ? { nulls: statement.orderBy.nulls } : {})
152
+ direction,
153
+ ...(nulls ? { nulls } : {})
154
154
  });
155
155
  return statement.limit === undefined ? sort : sort.topK(statement.limit);
156
156
  }
@@ -177,10 +177,9 @@ function resolveGPUSQLColumn(name, qualifier, columns) {
177
177
  throw new Error(`GPUSQL column "${name}" has an unknown table qualifier`);
178
178
  }
179
179
  const requested = parts[parts.length - 1];
180
- const matches = columns.filter(candidate => candidate.toLowerCase() === requested.toLowerCase());
181
- if (matches.length !== 1)
180
+ if (!columns.includes(requested))
182
181
  throw new Error(`GPUSQL column "${name}" does not exist`);
183
- return matches[0];
182
+ return requested;
184
183
  }
185
184
  function resolveGPUSQLExpression(expression, resolve) {
186
185
  switch (expression.kind) {
@@ -216,7 +215,7 @@ class GPUSQLParser {
216
215
  const table = this.parseIdentifier();
217
216
  const alias = this.parseOptionalTableAlias();
218
217
  const join = this.parseOptionalJoin();
219
- const predicate = this.takeKeyword('WHERE') ? this.parseExpression(0) : undefined;
218
+ const predicate = this.takeKeyword('WHERE') ? this.parseExpression() : undefined;
220
219
  let groupBy;
221
220
  if (this.takeKeyword('GROUP')) {
222
221
  this.expectKeyword('BY');
@@ -288,7 +287,7 @@ class GPUSQLParser {
288
287
  ...(this.takeKeyword('AS') ? { alias: this.parseIdentifier() } : {})
289
288
  };
290
289
  }
291
- const expression = this.parseExpression(0);
290
+ const expression = this.parseExpression();
292
291
  return {
293
292
  ...(expression ? { expression } : {}),
294
293
  ...(this.takeKeyword('AS') ? { alias: this.parseIdentifier() } : {})
@@ -322,72 +321,50 @@ class GPUSQLParser {
322
321
  const right = this.parseIdentifier();
323
322
  return { type, table, ...(alias ? { alias } : {}), left, right };
324
323
  }
325
- parseExpression(depth, minimumPrecedence = 0) {
326
- if (depth > MAXIMUM_EXPRESSION_DEPTH)
327
- throw new Error('GPUSQL expression nesting exceeds its safe limit');
328
- let expression = this.parseUnary(depth + 1);
329
- while (true) {
330
- if (minimumPrecedence <= 3 && this.takeKeyword('IS')) {
331
- const negated = this.takeKeyword('NOT');
332
- this.expectKeyword('NULL');
333
- expression = {
334
- kind: 'unary',
335
- operator: negated ? 'is-valid' : 'is-null',
336
- operand: expression
337
- };
338
- continue;
324
+ parseExpression() {
325
+ const tokens = [];
326
+ let depth = 0;
327
+ let expectsOperand = true;
328
+ for (let token = this.peek(); token; token = this.peek()) {
329
+ const value = token.value;
330
+ if (depth === 0) {
331
+ if (token.kind === 'symbol' && (value === ',' || value === ';'))
332
+ break;
333
+ if (token.kind === 'identifier' &&
334
+ !expectsOperand &&
335
+ /^(AS|FROM|GROUP|ORDER|LIMIT)$/i.test(value)) {
336
+ break;
337
+ }
339
338
  }
340
- const token = this.peek();
341
- const operation = token ? getGPUSQLBinaryOperator(token.value) : undefined;
342
- if (!operation || operation.precedence < minimumPrecedence)
343
- break;
344
- this.take();
345
- expression = {
346
- kind: 'binary',
347
- operator: operation.operator,
348
- left: expression,
349
- right: this.parseExpression(depth + 1, operation.precedence + 1)
350
- };
351
- }
352
- return expression;
353
- }
354
- parseUnary(depth) {
355
- if (this.takeKeyword('NOT')) {
356
- return { kind: 'unary', operator: 'not', operand: this.parseExpression(depth, 3) };
357
- }
358
- if (this.takeSymbol('-')) {
359
- return { kind: 'unary', operator: 'negate', operand: this.parseUnary(depth + 1) };
360
- }
361
- if (this.takeSymbol('(')) {
362
- const expression = this.parseExpression(depth + 1);
363
- this.expectSymbol(')');
364
- return expression;
365
- }
366
- const token = this.take();
367
- if (!token)
368
- throw new Error('GPUSQL expected an expression');
369
- if (token.kind === 'number') {
370
- const value = Number(token.value);
371
- if (!Number.isFinite(value))
372
- throw new Error('GPUSQL numeric literals must be finite');
373
- return literal(value).node;
374
- }
375
- if (token.kind === 'parameter') {
376
- if (!Object.hasOwn(this.parameters, token.value)) {
377
- throw new Error(`GPUSQL parameter ":${token.value}" requires an explicit default`);
339
+ switch (token.kind) {
340
+ case 'parameter':
341
+ // The shared AST leaves parameter binding to the GPU planner.
342
+ tokens.push(`":${value}"`);
343
+ expectsOperand = false;
344
+ break;
345
+ case 'identifier': {
346
+ const operator = /^NOT$/i.test(value) || (!expectsOperand && /^(AND|OR|IS)$/i.test(value));
347
+ const keyword = operator || /^(NULL|TRUE|FALSE)$/i.test(value);
348
+ tokens.push(keyword ? value : `"${value}"`);
349
+ expectsOperand = operator;
350
+ break;
351
+ }
352
+ case 'symbol':
353
+ if (value === '(')
354
+ depth++;
355
+ if (value === ')')
356
+ depth--;
357
+ tokens.push(value);
358
+ expectsOperand = value !== ')';
359
+ break;
360
+ case 'number':
361
+ tokens.push(value);
362
+ expectsOperand = false;
363
+ break;
378
364
  }
379
- return parameter(token.value, this.parameters[token.value]).node;
380
- }
381
- if (token.kind === 'identifier') {
382
- if (/^NULL$/i.test(token.value))
383
- return literal(null).node;
384
- if (/^TRUE$/i.test(token.value))
385
- return literal(true).node;
386
- if (/^FALSE$/i.test(token.value))
387
- return literal(false).node;
388
- return column(token.value).node;
365
+ this.take();
389
366
  }
390
- throw new Error(`GPUSQL does not support expression token "${token.value}"`);
367
+ return lowerSQLExpression(parseSQLExpression(tokens.join(' ')), this.parameters);
391
368
  }
392
369
  parseOptionalTableAlias() {
393
370
  if (this.takeKeyword('AS'))
@@ -463,35 +440,76 @@ function tokenizeGPUSQL(source) {
463
440
  }
464
441
  return tokens;
465
442
  }
466
- function getGPUSQLBinaryOperator(value) {
467
- switch (value.toUpperCase()) {
468
- case 'OR':
469
- return { operator: 'or', precedence: 1 };
470
- case 'AND':
471
- return { operator: 'and', precedence: 2 };
472
- case '=':
473
- return { operator: 'equal', precedence: 3 };
474
- case '!=':
475
- case '<>':
476
- return { operator: 'not-equal', precedence: 3 };
477
- case '>':
478
- return { operator: 'greater-than', precedence: 3 };
479
- case '>=':
480
- return { operator: 'greater-than-or-equal', precedence: 3 };
481
- case '<':
482
- return { operator: 'less-than', precedence: 3 };
483
- case '<=':
484
- return { operator: 'less-than-or-equal', precedence: 3 };
485
- case '+':
486
- return { operator: 'add', precedence: 4 };
487
- case '-':
488
- return { operator: 'subtract', precedence: 4 };
489
- case '*':
490
- return { operator: 'multiply', precedence: 5 };
491
- case '/':
492
- return { operator: 'divide', precedence: 5 };
443
+ const SQL_BINARY_OPERATORS = {
444
+ '||': 'or',
445
+ '&&': 'and',
446
+ '==': 'equal',
447
+ '!=': 'not-equal',
448
+ '>': 'greater-than',
449
+ '>=': 'greater-than-or-equal',
450
+ '<': 'less-than',
451
+ '<=': 'less-than-or-equal',
452
+ '+': 'add',
453
+ '-': 'subtract',
454
+ '*': 'multiply',
455
+ '/': 'divide'
456
+ };
457
+ function lowerSQLExpression(node, parameters) {
458
+ // math.gl types child nodes as generic expressions, though the SQL parser emits the AST union.
459
+ const expression = node;
460
+ switch (expression.type) {
461
+ case 'Identifier': {
462
+ if (!expression.name.startsWith(':'))
463
+ return column(expression.name).node;
464
+ const name = expression.name.slice(1);
465
+ if (!Object.hasOwn(parameters, name)) {
466
+ throw new Error(`GPUSQL parameter ":${name}" requires an explicit default`);
467
+ }
468
+ return parameter(name, parameters[name]).node;
469
+ }
470
+ case 'Literal': {
471
+ const value = expression.value;
472
+ if (value === null ||
473
+ typeof value === 'boolean' ||
474
+ (typeof value === 'number' && Number.isFinite(value))) {
475
+ return literal(value).node;
476
+ }
477
+ break;
478
+ }
479
+ case 'UnaryExpression':
480
+ if (expression.operator === '!' || expression.operator === '-') {
481
+ return {
482
+ kind: 'unary',
483
+ operator: expression.operator === '!' ? 'not' : 'negate',
484
+ operand: lowerSQLExpression(expression.argument, parameters)
485
+ };
486
+ }
487
+ break;
488
+ case 'BinaryExpression': {
489
+ const operator = SQL_BINARY_OPERATORS[expression.operator];
490
+ if (operator) {
491
+ return {
492
+ kind: 'binary',
493
+ operator,
494
+ left: lowerSQLExpression(expression.left, parameters),
495
+ right: lowerSQLExpression(expression.right, parameters)
496
+ };
497
+ }
498
+ break;
499
+ }
500
+ case 'CallExpression':
501
+ if (expression.arguments.length === 1 &&
502
+ (expression.callee['name'] === 'isNull' || expression.callee['name'] === 'isNotNull')) {
503
+ return {
504
+ kind: 'unary',
505
+ operator: expression.callee['name'] === 'isNull' ? 'is-null' : 'is-valid',
506
+ operand: lowerSQLExpression(expression.arguments[0], parameters)
507
+ };
508
+ }
509
+ break;
493
510
  default:
494
- return undefined;
511
+ break;
495
512
  }
513
+ throw new Error(`GPUSQL does not support SQL expression "${expression.type}"`);
496
514
  }
497
515
  //# sourceMappingURL=gpu-sql.js.map