@luma.gl/graph 10.0.0-beta.3 → 10.0.0-beta.4

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 (125) hide show
  1. package/dist/arrow/gpu/arrow-gpu-table-conversion.cjs +66 -30
  2. package/dist/arrow/gpu/arrow-gpu-table-conversion.d.ts +7 -1
  3. package/dist/arrow/gpu/arrow-gpu-table-conversion.d.ts.map +1 -1
  4. package/dist/arrow/gpu/arrow-gpu-table-conversion.js +78 -32
  5. package/dist/arrow/gpu/arrow-gpu-table-conversion.js.map +1 -1
  6. package/dist/arrow/gpu/arrow-gpu-vector-conversion.cjs +7 -0
  7. package/dist/arrow/gpu/arrow-gpu-vector-conversion.d.ts +2 -0
  8. package/dist/arrow/gpu/arrow-gpu-vector-conversion.d.ts.map +1 -1
  9. package/dist/arrow/gpu/arrow-gpu-vector-conversion.js +14 -0
  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/vectors/arrow-temporal-gpu-vector.cjs +1 -2
  13. package/dist/arrow/vectors/arrow-temporal-gpu-vector.js +2 -2
  14. package/dist/arrow/vectors/arrow-temporal-gpu-vector.js.map +1 -1
  15. package/dist/dist.dev.js +62 -27
  16. package/dist/dist.min.js +48 -47
  17. package/dist/gpu-core/gpu-command-graph-budget-controller.cjs +10 -2
  18. package/dist/gpu-core/gpu-command-graph-budget-controller.d.ts +4 -0
  19. package/dist/gpu-core/gpu-command-graph-budget-controller.d.ts.map +1 -1
  20. package/dist/gpu-core/gpu-command-graph-budget-controller.js +10 -2
  21. package/dist/gpu-core/gpu-command-graph-budget-controller.js.map +1 -1
  22. package/dist/gpu-core/gpu-group-aggregation.cjs +45 -21
  23. package/dist/gpu-core/gpu-group-aggregation.d.ts +4 -0
  24. package/dist/gpu-core/gpu-group-aggregation.d.ts.map +1 -1
  25. package/dist/gpu-core/gpu-group-aggregation.js +58 -24
  26. package/dist/gpu-core/gpu-group-aggregation.js.map +1 -1
  27. package/dist/gpu-core/index.cjs.map +1 -1
  28. package/dist/gpu-dataframe/gpu-global-aggregation-compiler.cjs +6 -2
  29. package/dist/gpu-dataframe/gpu-global-aggregation-compiler.d.ts +2 -1
  30. package/dist/gpu-dataframe/gpu-global-aggregation-compiler.d.ts.map +1 -1
  31. package/dist/gpu-dataframe/gpu-global-aggregation-compiler.js +6 -2
  32. package/dist/gpu-dataframe/gpu-global-aggregation-compiler.js.map +1 -1
  33. package/dist/gpu-dataframe/gpu-global-sort-compiler.cjs +61 -54
  34. package/dist/gpu-dataframe/gpu-global-sort-compiler.d.ts +6 -1
  35. package/dist/gpu-dataframe/gpu-global-sort-compiler.d.ts.map +1 -1
  36. package/dist/gpu-dataframe/gpu-global-sort-compiler.js +61 -55
  37. package/dist/gpu-dataframe/gpu-global-sort-compiler.js.map +1 -1
  38. package/dist/gpu-dataframe/gpu-group-aggregation-compiler.cjs +88 -13
  39. package/dist/gpu-dataframe/gpu-group-aggregation-compiler.d.ts +11 -4
  40. package/dist/gpu-dataframe/gpu-group-aggregation-compiler.d.ts.map +1 -1
  41. package/dist/gpu-dataframe/gpu-group-aggregation-compiler.js +96 -16
  42. package/dist/gpu-dataframe/gpu-group-aggregation-compiler.js.map +1 -1
  43. package/dist/gpu-dataframe/gpu-group-by-query.cjs +12 -3
  44. package/dist/gpu-dataframe/gpu-group-by-query.d.ts +8 -1
  45. package/dist/gpu-dataframe/gpu-group-by-query.d.ts.map +1 -1
  46. package/dist/gpu-dataframe/gpu-group-by-query.js +13 -3
  47. package/dist/gpu-dataframe/gpu-group-by-query.js.map +1 -1
  48. package/dist/gpu-dataframe/gpu-join-compiler.cjs +53 -2
  49. package/dist/gpu-dataframe/gpu-join-compiler.d.ts +9 -1
  50. package/dist/gpu-dataframe/gpu-join-compiler.d.ts.map +1 -1
  51. package/dist/gpu-dataframe/gpu-join-compiler.js +51 -0
  52. package/dist/gpu-dataframe/gpu-join-compiler.js.map +1 -1
  53. package/dist/gpu-dataframe/gpu-query-compiler.cjs +24 -0
  54. package/dist/gpu-dataframe/gpu-query-compiler.d.ts +3 -1
  55. package/dist/gpu-dataframe/gpu-query-compiler.d.ts.map +1 -1
  56. package/dist/gpu-dataframe/gpu-query-compiler.js +25 -0
  57. package/dist/gpu-dataframe/gpu-query-compiler.js.map +1 -1
  58. package/dist/gpu-dataframe/index.cjs.map +3 -3
  59. package/dist/gpu-graph/index.cjs.map +1 -1
  60. package/dist/gpu-graph-algorithms/benchmarks.cjs.map +2 -2
  61. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.cjs +182 -62
  62. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.d.ts +29 -2
  63. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.d.ts.map +1 -1
  64. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.js +200 -64
  65. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.js.map +1 -1
  66. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.cjs +135 -9
  67. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.d.ts +55 -8
  68. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.d.ts.map +1 -1
  69. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.js +145 -14
  70. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.js.map +1 -1
  71. package/dist/gpu-graph-algorithms/index.cjs.map +3 -3
  72. package/dist/gpu-physics/articulation-tree-shader.cjs +177 -0
  73. package/dist/gpu-physics/articulation-tree-shader.d.ts +3 -0
  74. package/dist/gpu-physics/articulation-tree-shader.d.ts.map +1 -0
  75. package/dist/gpu-physics/articulation-tree-shader.js +159 -0
  76. package/dist/gpu-physics/articulation-tree-shader.js.map +1 -0
  77. package/dist/gpu-physics/gpu-articulation-tree.cjs +203 -0
  78. package/dist/gpu-physics/gpu-articulation-tree.d.ts +81 -0
  79. package/dist/gpu-physics/gpu-articulation-tree.d.ts.map +1 -0
  80. package/dist/gpu-physics/gpu-articulation-tree.js +205 -0
  81. package/dist/gpu-physics/gpu-articulation-tree.js.map +1 -0
  82. package/dist/gpu-physics/gpu-rigid-body-world.cjs +62 -0
  83. package/dist/gpu-physics/gpu-rigid-body-world.d.ts +12 -0
  84. package/dist/gpu-physics/gpu-rigid-body-world.d.ts.map +1 -1
  85. package/dist/gpu-physics/gpu-rigid-body-world.js +76 -0
  86. package/dist/gpu-physics/gpu-rigid-body-world.js.map +1 -1
  87. package/dist/gpu-physics/index.cjs +3 -0
  88. package/dist/gpu-physics/index.cjs.map +3 -3
  89. package/dist/gpu-physics/index.d.ts +1 -0
  90. package/dist/gpu-physics/index.d.ts.map +1 -1
  91. package/dist/gpu-physics/index.js +1 -0
  92. package/dist/gpu-physics/index.js.map +1 -1
  93. package/dist/gpu-physics/rigid-body-joints.d.ts +2 -0
  94. package/dist/gpu-physics/rigid-body-joints.d.ts.map +1 -1
  95. package/dist/gpu-physics/rigid-body-joints.js.map +1 -1
  96. package/dist/gpu-project/graph.cjs.map +2 -2
  97. package/dist/gpu-simple-graph/index.cjs.map +2 -2
  98. package/dist/gpu-sql/gpu-sql.cjs +167 -47
  99. package/dist/gpu-sql/gpu-sql.d.ts +6 -1
  100. package/dist/gpu-sql/gpu-sql.d.ts.map +1 -1
  101. package/dist/gpu-sql/gpu-sql.js +187 -47
  102. package/dist/gpu-sql/gpu-sql.js.map +1 -1
  103. package/dist/gpu-sql/index.cjs.map +3 -3
  104. package/dist/index.cjs.map +1 -1
  105. package/package.json +8 -8
  106. package/src/arrow/gpu/arrow-gpu-table-conversion.ts +94 -40
  107. package/src/arrow/gpu/arrow-gpu-vector-conversion.ts +19 -0
  108. package/src/arrow/vectors/arrow-temporal-gpu-vector.ts +2 -2
  109. package/src/gpu-core/gpu-command-graph-budget-controller.ts +22 -2
  110. package/src/gpu-core/gpu-group-aggregation.ts +68 -30
  111. package/src/gpu-dataframe/gpu-global-aggregation-compiler.ts +9 -2
  112. package/src/gpu-dataframe/gpu-global-sort-compiler.ts +95 -78
  113. package/src/gpu-dataframe/gpu-group-aggregation-compiler.ts +191 -24
  114. package/src/gpu-dataframe/gpu-group-by-query.ts +27 -4
  115. package/src/gpu-dataframe/gpu-join-compiler.ts +75 -0
  116. package/src/gpu-dataframe/gpu-query-compiler.ts +41 -1
  117. package/src/gpu-graph-algorithms/README.md +4 -2
  118. package/src/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.ts +248 -66
  119. package/src/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.ts +218 -20
  120. package/src/gpu-physics/articulation-tree-shader.ts +169 -0
  121. package/src/gpu-physics/gpu-articulation-tree.ts +279 -0
  122. package/src/gpu-physics/gpu-rigid-body-world.ts +89 -0
  123. package/src/gpu-physics/index.ts +7 -0
  124. package/src/gpu-physics/rigid-body-joints.ts +2 -0
  125. package/src/gpu-sql/gpu-sql.ts +248 -57
@@ -29,6 +29,7 @@ var import_gpu_group_by_query = require("../gpu-dataframe/gpu-group-by-query.cjs
29
29
  var import_gpu_global_sort_compiler = require("../gpu-dataframe/gpu-global-sort-compiler.cjs");
30
30
  var import_gpu_expression = require("../gpu-dataframe/gpu-expression.cjs");
31
31
  var import_portable_expression = require("../gpu-dataframe/portable-expression.cjs");
32
+ var import_gpu_global_aggregation_compiler = require("../gpu-dataframe/gpu-global-aggregation-compiler.cjs");
32
33
  var import_gpu_join_compiler = require("../gpu-dataframe/gpu-join-compiler.cjs");
33
34
  class GPUSQLContext {
34
35
  tables;
@@ -49,7 +50,7 @@ class GPUSQLContext {
49
50
  /** Parses and validates one immutable SELECT plan without touching registered GPU sources. */
50
51
  query(source, options = {}) {
51
52
  const statement = parseGPUSQLStatement(source);
52
- return new GPUSQLQuery(source, buildGPUSQLPlan(this.tables, statement, options.parameters ?? {}));
53
+ return new GPUSQLQuery(source, buildGPUSQLPlan(this.tables, statement, options.parameters ?? {}, options.maxMeanScratchBytes));
53
54
  }
54
55
  }
55
56
  class GPUSQLQuery {
@@ -71,8 +72,8 @@ class GPUSQLQuery {
71
72
  return this.plan.plan.compile(graph);
72
73
  }
73
74
  }
74
- function buildGPUSQLPlan(tables, statement, parameters) {
75
- var _a;
75
+ function buildGPUSQLPlan(tables, statement, parameters, maxMeanScratchBytes) {
76
+ var _a, _b;
76
77
  const frame = tables[statement.table.toLowerCase()];
77
78
  if (!frame)
78
79
  throw new Error(`GPUSQL table "${statement.table}" is not registered`);
@@ -87,6 +88,7 @@ function buildGPUSQLPlan(tables, statement, parameters) {
87
88
  explanation: Object.freeze({
88
89
  table: statement.table,
89
90
  filtered: Boolean(statement.predicate),
91
+ ...statement.having ? { having: true } : {},
90
92
  execution,
91
93
  columns: Object.freeze([...columns]),
92
94
  workingColumns: Object.freeze([...workingColumns]),
@@ -95,35 +97,76 @@ function buildGPUSQLPlan(tables, statement, parameters) {
95
97
  ...statement.limit !== void 0 || statement.offset !== void 0 ? { page: Object.freeze({ offset: statement.offset ?? 0, limit: statement.limit }) } : {}
96
98
  })
97
99
  });
100
+ const bindJoin = (join) => {
101
+ const right = tables[join.table.toLowerCase()];
102
+ if (!right)
103
+ throw new Error(`GPUSQL table "${join.table}" is not registered`);
104
+ const rightQualifier = join.alias ?? join.table;
105
+ const { left: firstOperand, right: secondOperand } = join;
106
+ const matchesLeft = (reference) => matchesGPUSQLColumn(reference, qualifier, frame.columnNames);
107
+ const matchesRight = (reference) => matchesGPUSQLColumn(reference, rightQualifier, right.columnNames);
108
+ const forward = matchesLeft(firstOperand) && matchesRight(secondOperand);
109
+ const reversed = matchesLeft(secondOperand) && matchesRight(firstOperand);
110
+ if (forward && reversed) {
111
+ throw new Error("GPUSQL JOIN key references are ambiguous; qualify the columns");
112
+ }
113
+ const leftKey = resolve(reversed ? secondOperand : firstOperand);
114
+ const rightKey = resolveGPUSQLColumn(reversed ? firstOperand : secondOperand, rightQualifier, right.columnNames);
115
+ return { right, leftKey, rightKey };
116
+ };
117
+ const joinMethods = {
118
+ left: "leftJoin",
119
+ semi: "semiJoin",
120
+ anti: "antiJoin",
121
+ inner: "innerJoin"
122
+ };
98
123
  const aggregates = statement.items.filter((item) => item.aggregate);
99
- if (aggregates.length > 0) {
100
- if (statement.join || statement.orderBy || statement.limit !== void 0 || statement.offset !== void 0) {
101
- throw new Error("GPUSQL aggregate queries do not yet support joins, ordering, or pagination");
124
+ if (statement.having && !statement.groupBy) {
125
+ throw new Error("GPUSQL HAVING requires GROUP BY");
126
+ }
127
+ if (aggregates.length > 0 || statement.having || statement.groupBy && statement.orderBy) {
128
+ if (!statement.groupBy && (statement.orderBy || statement.limit !== void 0 || statement.offset !== void 0)) {
129
+ throw new Error("GPUSQL aggregate queries without GROUP BY do not yet support ordering or pagination");
102
130
  }
103
- const definitions = {};
131
+ let aggregateJoin;
132
+ if (statement.join) {
133
+ const { right, leftKey, rightKey } = bindJoin(statement.join);
134
+ const type = statement.join.type;
135
+ const join = query[joinMethods[type]](right, { leftOn: leftKey, rightOn: rightKey });
136
+ aggregateJoin = { right, type, options: join.options };
137
+ }
138
+ const definitions = /* @__PURE__ */ Object.create(null);
104
139
  const reservedNames = new Set(frame.columnNames);
105
140
  const outputNames = /* @__PURE__ */ new Map();
141
+ const aggregateOutputs = /* @__PURE__ */ new Map();
106
142
  let inputIndex = 0;
107
- for (const item of aggregates) {
108
- const input = item.aggregateInput && lowerSQLExpression(item.aggregateInput, parameters, resolve);
143
+ const registerAggregate = (operation, input, alias) => {
109
144
  let inputColumn = (input == null ? void 0 : input.kind) === "column" ? input.name : void 0;
110
145
  if (input && input.kind !== "column") {
111
- if (!item.alias) {
146
+ if (!alias)
112
147
  throw new Error("GPUSQL computed aggregate inputs require an explicit AS alias");
113
- }
114
148
  do {
115
149
  inputColumn = "__sqlAggregateInput" + inputIndex++;
116
150
  } while (reservedNames.has(inputColumn));
117
151
  reservedNames.add(inputColumn);
118
152
  query = query.withColumn(inputColumn, new import_gpu_expression.GPUExpression(input));
119
153
  }
120
- const name = item.alias ?? `${item.aggregate}_${inputColumn ?? "all"}`;
121
- outputNames.set(item, name);
122
- if (definitions[name])
154
+ const name = alias ?? `${operation}_${inputColumn ?? "all"}`;
155
+ if (Object.hasOwn(definitions, name)) {
123
156
  throw new Error(`GPUSQL aggregate alias "${name}" is duplicated`);
124
- definitions[name] = item.aggregate === "count" ? inputColumn === void 0 ? "count" : { count: inputColumn } : { [item.aggregate]: inputColumn };
157
+ }
158
+ aggregateOutputs.set(JSON.stringify([operation, input ?? null]), name);
159
+ definitions[name] = operation === "count" ? inputColumn === void 0 ? "count" : { count: inputColumn } : { [operation]: inputColumn };
160
+ return name;
161
+ };
162
+ for (const item of aggregates) {
163
+ const input = item.aggregateInput && lowerSQLExpression(item.aggregateInput, parameters, resolve);
164
+ outputNames.set(item, registerAggregate(item.aggregate, input, item.alias));
125
165
  }
126
166
  if (statement.groupBy !== void 0) {
167
+ if (maxMeanScratchBytes !== void 0 && (!Number.isSafeInteger(maxMeanScratchBytes) || maxMeanScratchBytes < 4)) {
168
+ throw new Error("GPUSQL mean scratch budget requires at least four bytes");
169
+ }
127
170
  const keyItems = statement.items.filter((item) => !item.aggregate);
128
171
  const occupied = new Set(Object.keys(definitions));
129
172
  const selectedKeys = /* @__PURE__ */ new Map();
@@ -190,30 +233,82 @@ function buildGPUSQLPlan(tables, statement, parameters) {
190
233
  if ([...selectedKeys.values()].some((key2) => !uniqueKeys.has(key2))) {
191
234
  throw new Error("GPUSQL grouped queries must group by every non-aggregate SELECT expression");
192
235
  }
193
- const { predicates, selectedColumns, derivedColumns } = query;
194
236
  const columns = statement.items.map((item) => outputNames.get(item));
237
+ if (!statement.orderBy && (statement.limit !== void 0 || statement.offset !== void 0)) {
238
+ throw new Error("GPUSQL LIMIT or OFFSET currently requires ORDER BY");
239
+ }
240
+ const bindAggregateCall = (call) => {
241
+ const name = call.callee.type === "Identifier" ? String(call.callee["name"]).toLowerCase() : "";
242
+ const operation = name === "avg" ? "mean" : name;
243
+ if (operation !== "count" && operation !== "sum" && operation !== "min" && operation !== "max" && operation !== "mean")
244
+ return void 0;
245
+ if (call.arguments.length !== 1)
246
+ throw new Error("GPUSQL aggregates require one argument");
247
+ const argument = call.arguments[0];
248
+ const wildcard = argument.type === "Identifier" && argument["sqlWildcard"] === true;
249
+ if (wildcard && operation !== "count") {
250
+ throw new Error("GPUSQL only permits COUNT(*) as a wildcard aggregation");
251
+ }
252
+ const input = wildcard ? void 0 : lowerSQLExpression(argument, parameters, resolve);
253
+ const output = aggregateOutputs.get(JSON.stringify([operation, input ?? null]));
254
+ if (output !== void 0)
255
+ return (0, import_gpu_expression.column)(output).node;
256
+ let hiddenName;
257
+ do {
258
+ hiddenName = "__sqlPostGroupAggregate" + inputIndex++;
259
+ } while (occupied.has(hiddenName));
260
+ occupied.add(hiddenName);
261
+ return (0, import_gpu_expression.column)(registerAggregate(operation, input, hiddenName)).node;
262
+ };
263
+ const having = statement.having ? new import_gpu_expression.GPUExpression(lowerSQLExpression(statement.having, parameters, (reference) => resolveGPUSQLColumn(reference, "", columns), bindAggregateCall)) : void 0;
264
+ const ordering = (_a = statement.orderBy) == null ? void 0 : _a.map(({ expression, direction, nulls }, index) => {
265
+ const column2 = expression.type === "Literal" && typeof expression.value === "number" ? getGPUSQLProjectionItem(columns, expression.value, "ORDER BY") : expression.type === "Identifier" && !("gpuSQLParameter" in expression) ? expression.name : void 0;
266
+ const aggregate2 = expression.type === "CallExpression" ? bindAggregateCall(expression) : void 0;
267
+ const sortColumn = (aggregate2 == null ? void 0 : aggregate2.kind) === "column" ? aggregate2.name : column2;
268
+ if (sortColumn === void 0 || !aggregate2 && !columns.includes(sortColumn)) {
269
+ throw new Error("GPUSQL grouped ORDER BY requires a SELECT output name, position, or aggregate call");
270
+ }
271
+ return {
272
+ column: sortColumn,
273
+ options: {
274
+ direction,
275
+ nulls: nulls ?? "last",
276
+ nans: "last",
277
+ algorithm: "auto",
278
+ ...index === 0 ? { limit: statement.limit, offset: statement.offset } : {}
279
+ }
280
+ };
281
+ });
282
+ const { predicates: predicates2, selectedColumns: selectedColumns2, derivedColumns: derivedColumns2 } = query;
283
+ if (Object.keys(definitions).length === 0) {
284
+ throw new Error("GPUSQL GROUP BY requires at least one aggregation");
285
+ }
195
286
  const normalized = (0, import_gpu_group_by_query.normalizeGPUDataFrameAggregations)({ ...query, source: frame }, keys[0].output, definitions);
196
287
  if (keys.length > 1 || keys[0].output === void 0 || !frame.dictionaries[keys[0].source]) {
197
288
  for (const { source } of keys) {
198
- const format = (_a = frame.schema.fields.find((field) => field.name === source)) == null ? void 0 : _a.format;
199
- if (!derivedColumns.some((column2) => column2.name === source) && format !== "uint32" && format !== "sint32" && format !== "float32") {
289
+ const format = (_b = frame.schema.fields.find((field) => field.name === source)) == null ? void 0 : _b.format;
290
+ if (!derivedColumns2.some((column2) => column2.name === source) && format !== "uint32" && format !== "sint32" && format !== "float32") {
200
291
  throw new Error("GPUSQL GROUP BY requires 32-bit numeric or dictionary keys");
201
292
  }
202
293
  }
203
294
  return describe({
204
- compile: (graph) => (0, import_gpu_group_aggregation_compiler.compileGPUDataFrameObservedAggregation)(frame, predicates, selectedColumns, derivedColumns, keys, normalized, graph, columns)
205
- }, "observed-grouping", columns, selectedColumns, keys.length);
295
+ compile: (graph) => (0, import_gpu_group_aggregation_compiler.compileGPUDataFrameObservedAggregation)(frame, predicates2, selectedColumns2, derivedColumns2, keys, normalized, graph, columns, ordering, having, aggregateJoin, maxMeanScratchBytes)
296
+ }, "observed-grouping", columns, selectedColumns2, keys.length);
206
297
  }
207
298
  const [key] = keys;
208
299
  const { groupCount } = query.groupBy(key.source);
209
300
  return describe({
210
- compile: (graph) => (0, import_gpu_group_aggregation_compiler.compileGPUDataFrameGroupedAggregation)(frame, predicates, selectedColumns, derivedColumns, key.source, groupCount, normalized, graph, { key: key.output, columns })
211
- }, "dense-grouping", columns, selectedColumns, 1);
301
+ compile: (graph) => (0, import_gpu_group_aggregation_compiler.compileGPUDataFrameGroupedAggregation)(frame, predicates2, selectedColumns2, derivedColumns2, key.source, groupCount, normalized, graph, { key: key.output, columns }, ordering, having, aggregateJoin, maxMeanScratchBytes)
302
+ }, "dense-grouping", columns, selectedColumns2, 1);
212
303
  }
213
304
  if (aggregates.length !== statement.items.length) {
214
305
  throw new Error("GPUSQL global aggregates cannot mix ordinary source columns");
215
306
  }
216
- return describe(query.aggregate(definitions), "global-aggregation", statement.items.map((item) => outputNames.get(item)), query.selectedColumns);
307
+ const aggregate = query.aggregate(definitions);
308
+ const { predicates, selectedColumns, derivedColumns } = query;
309
+ return describe(aggregateJoin ? {
310
+ compile: (graph) => (0, import_gpu_global_aggregation_compiler.compileGPUDataFrameAggregation)(frame, predicates, selectedColumns, derivedColumns, aggregate.definitions, graph, aggregateJoin)
311
+ } : aggregate, "global-aggregation", statement.items.map((item) => outputNames.get(item)), query.selectedColumns);
217
312
  }
218
313
  if (statement.groupBy !== void 0)
219
314
  throw new Error("GPUSQL GROUP BY requires at least one aggregation");
@@ -243,31 +338,12 @@ function buildGPUSQLPlan(tables, statement, parameters) {
243
338
  if (statement.orderBy || statement.limit !== void 0 || statement.offset !== void 0) {
244
339
  throw new Error("GPUSQL joined results do not yet support ORDER BY or pagination");
245
340
  }
246
- const right = tables[statement.join.table.toLowerCase()];
247
- if (!right)
248
- throw new Error(`GPUSQL table "${statement.join.table}" is not registered`);
249
- const rightQualifier = statement.join.alias ?? statement.join.table;
250
- const { left: firstOperand, right: secondOperand } = statement.join;
251
- const matchesLeft = (reference) => matchesGPUSQLColumn(reference, qualifier, frame.columnNames);
252
- const matchesRight = (reference) => matchesGPUSQLColumn(reference, rightQualifier, right.columnNames);
253
- const forward = matchesLeft(firstOperand) && matchesRight(secondOperand);
254
- const reversed = matchesLeft(secondOperand) && matchesRight(firstOperand);
255
- if (forward && reversed) {
256
- throw new Error("GPUSQL JOIN key references are ambiguous; qualify the columns");
257
- }
258
- const leftKey = resolve(reversed ? secondOperand : firstOperand);
259
- const rightKey = resolveGPUSQLColumn(reversed ? firstOperand : secondOperand, rightQualifier, right.columnNames);
341
+ const { right, leftKey, rightKey } = bindJoin(statement.join);
260
342
  const keyIsSelected = selected.includes(leftKey);
261
343
  const workingColumns = keyIsSelected ? selected : [...selected, leftKey];
262
344
  const workingQuery = query.select(workingColumns);
263
345
  const joinType = statement.join.type;
264
- const method = {
265
- left: "leftJoin",
266
- semi: "semiJoin",
267
- anti: "antiJoin",
268
- inner: "innerJoin"
269
- };
270
- const join = workingQuery[method[joinType]](right, { leftOn: leftKey, rightOn: rightKey });
346
+ const join = workingQuery[joinMethods[joinType]](right, { leftOn: leftKey, rightOn: rightKey });
271
347
  if (keyIsSelected)
272
348
  return describe(join, "unique-right-join", selected, workingColumns, 1);
273
349
  const { predicates, derivedColumns } = workingQuery;
@@ -337,11 +413,51 @@ function resolveGPUSQLColumn(reference, qualifier, columns) {
337
413
  }
338
414
  return reference.name;
339
415
  }
416
+ function splitGPUSQLHaving(source) {
417
+ const tokens = /"(?:""|[^"])*"|'(?:''|[^'])*'|:[A-Za-z_]\w*|[A-Za-z_]\w*|[();]|--[^\r\n]*|\/\*/g;
418
+ let depth = 0;
419
+ let start = -1;
420
+ let predicateStart = -1;
421
+ let end = source.length;
422
+ let tailStarted = false;
423
+ for (const token of source.matchAll(tokens)) {
424
+ if (token[0].startsWith("--"))
425
+ continue;
426
+ if (token[0] === "/*") {
427
+ throw new Error("GPUSQL does not support SQL comments");
428
+ }
429
+ if (token[0] === "(")
430
+ depth++;
431
+ if (token[0] === ")")
432
+ depth--;
433
+ if (depth !== 0)
434
+ continue;
435
+ const keyword = token[0].toUpperCase();
436
+ if (start !== -1 && ["ORDER", "LIMIT", "OFFSET", ";"].includes(keyword)) {
437
+ end = token.index;
438
+ break;
439
+ }
440
+ if (start === -1 && ["ORDER", "LIMIT", "OFFSET", ";"].includes(keyword))
441
+ tailStarted = true;
442
+ if (start === -1 && keyword === "HAVING") {
443
+ if (tailStarted)
444
+ throw new Error("GPUSQL HAVING must precede ORDER BY and pagination");
445
+ start = token.index;
446
+ predicateStart = start + token[0].length;
447
+ }
448
+ }
449
+ return start === -1 ? { select: source } : {
450
+ select: source.slice(0, start) + " " + source.slice(end),
451
+ having: source.slice(predicateStart, end)
452
+ };
453
+ }
340
454
  function parseGPUSQLStatement(source) {
341
455
  var _a, _b, _c;
342
- const statement = (0, import_parser.parseSQLSelectStatement)(source, {
456
+ const { select, having } = splitGPUSQLHaving(source);
457
+ const options = {
343
458
  parseParameter: (name) => ({ type: "Identifier", name, gpuSQLParameter: true })
344
- });
459
+ };
460
+ const statement = (0, import_parser.parseSQLSelectStatement)(select, options);
345
461
  if (statement.offset !== void 0 && statement.offset > 4294967295) {
346
462
  throw new Error("GPUSQL OFFSET must fit an unsigned 32-bit row count");
347
463
  }
@@ -405,6 +521,7 @@ function parseGPUSQLStatement(source) {
405
521
  alias: statement.from.alias,
406
522
  join: loweredJoin,
407
523
  predicate: statement.where,
524
+ having: having === void 0 ? void 0 : (0, import_parser.parseSQLSelectStatement)("SELECT (" + having + "\n) FROM __gpu_having", options).select[0].expression,
408
525
  groupBy: ((_b = statement.groupBy) == null ? void 0 : _b.length) ? statement.groupBy.map((expression) => expression) : void 0,
409
526
  orderBy: ((_c = statement.orderBy) == null ? void 0 : _c.length) ? statement.orderBy.map((sort) => ({
410
527
  expression: sort.expression,
@@ -435,7 +552,7 @@ function readGPUSQLColumn(node) {
435
552
  }
436
553
  throw new Error("GPUSQL expected a table or column identifier");
437
554
  }
438
- function lowerSQLExpression(node, parameters, resolve) {
555
+ function lowerSQLExpression(node, parameters, resolve, resolveCall) {
439
556
  const expression = node;
440
557
  try {
441
558
  return (0, import_portable_expression.lowerPortableExpression)(expression, (leaf) => {
@@ -450,6 +567,9 @@ function lowerSQLExpression(node, parameters, resolve) {
450
567
  return (0, import_gpu_expression.column)(resolve(readGPUSQLColumn(leaf))).node;
451
568
  }
452
569
  if (leaf.type === "CallExpression" && leaf.callee.type === "Identifier") {
570
+ const replacement = resolveCall == null ? void 0 : resolveCall(leaf);
571
+ if (replacement)
572
+ return replacement;
453
573
  const name = leaf.callee["name"];
454
574
  const functionName = typeof name === "string" && leaf["sqlFunction"] === true ? name.toLowerCase() : name;
455
575
  if (name !== "isNull" && name !== "isNotNull" && name !== "in" && name !== "cast" && functionName !== "coalesce" && functionName !== "nullif" && !(0, import_gpu_expression.isGPUScalarFunction)(functionName)) {
@@ -4,9 +4,12 @@ import type { GPUDataFrameJoinType } from "../gpu-dataframe/gpu-join-query.js";
4
4
  import type { CompiledGPUDataFrameQuery, GPUDataFrameQueryParameters } from "../gpu-dataframe/gpu-query-compiler.js";
5
5
  /** Explicit GPU dataframe registrations; Arrow remains exclusively an adapter boundary. */
6
6
  export type GPUSQLTables = Readonly<Record<string, GPUDataFrame>>;
7
- /** Defaults for named SQL parameters such as `:minimumFare`; values remain caller-controlled. */
7
+ /** Configuration for GPU-only SQL planning. */
8
8
  export type GPUSQLQueryOptions = Readonly<{
9
+ /** Defaults for named SQL parameters such as :minimumFare. */
9
10
  parameters?: GPUDataFrameQueryParameters;
11
+ /** Maximum temporary count bytes per grouped AVG operation. */
12
+ maxMeanScratchBytes?: number;
10
13
  }>;
11
14
  /** Execution route selected by the GPU SQL planner. */
12
15
  export type GPUSQLExecution = 'projection' | 'global-sort' | 'global-aggregation' | 'dense-grouping' | 'observed-grouping' | 'unique-right-join';
@@ -16,6 +19,8 @@ export type GPUSQLExplanation = Readonly<{
16
19
  table: string;
17
20
  /** Whether a source-row WHERE predicate runs before the terminal operation. */
18
21
  filtered: boolean;
22
+ /** A post-aggregation HAVING predicate filters the grouped result. */
23
+ having?: true;
19
24
  /** Terminal GPU dataframe execution route. */
20
25
  execution: GPUSQLExecution;
21
26
  /** Result fields in SELECT order. */
@@ -1 +1 @@
1
- {"version":3,"file":"gpu-sql.d.ts","sourceRoot":"","sources":["../../src/gpu-sql/gpu-sql.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAC,e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