@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
@@ -143,91 +143,18 @@ function addGPUGlobalSortToGraph<Selection extends GPUTypeMap>(
143
143
  keys: readonly GPUGlobalSortKey<keyof Selection & string>[],
144
144
  resultColumns?: readonly (keyof Selection & string)[]
145
145
  ): GPUDataFrameQueryExtensionResult<Selection, CompiledGPUDataFrameGlobalSort<Selection>> {
146
- const graph = context.graph;
147
146
  const prefix = `${context.queryId}-global-sort`;
148
147
  const ownedVectors: GPUVector[] = [];
149
148
 
150
149
  try {
151
150
  const {column, options} = keys[0];
152
- const length = context.table.numRows;
153
- const globalRowIndices = createGPUAnalyticsOutputVector(
154
- graph.device,
155
- `${prefix}-row-indices`,
156
- 'uint32',
157
- length
158
- );
159
- ownedVectors.push(globalRowIndices);
160
- const globalSelectedCount = createGPUAnalyticsOutputVector(
161
- graph.device,
162
- `${prefix}-selected-count`,
163
- 'uint32',
164
- 1
165
- );
166
- ownedVectors.push(globalSelectedCount);
167
- const globalRows = graph.importGPUVector(`${prefix}-row-vector`, globalRowIndices).data[0];
168
- const selectedCount = graph.importGPUVector(`${prefix}-count-vector`, globalSelectedCount)
169
- .data[0];
170
-
171
- graph.add(
172
- new GPUReduction({
173
- id: `${prefix}-count-selected`,
174
- input: context.selectedCounts,
175
- output: selectedCount,
176
- operation: 'sum'
177
- })
151
+ const {globalRowIndices, globalSelectedCount} = addGPUGlobalOrdering(
152
+ context,
153
+ keys,
154
+ ownedVectors,
155
+ prefix
178
156
  );
179
157
 
180
- if (length > 0) {
181
- const scratch = createGPUGlobalSortScratch(graph, prefix, length);
182
- // Stable sorts transfer the ordering of less significant keys through ties.
183
- for (let index = keys.length - 1; index >= 0; index--) {
184
- addGPUGlobalSortKeyPasses(
185
- context,
186
- `${prefix}-key-${index}`,
187
- keys[index],
188
- scratch,
189
- index === keys.length - 1
190
- );
191
- }
192
-
193
- addGPUGlobalSortPublishPass(graph, `${prefix}-publish`, {
194
- scratch,
195
- output: globalRows,
196
- count: selectedCount,
197
- offset: options.offset ?? 0,
198
- limit: options.limit ?? MAXIMUM_UINT32
199
- });
200
-
201
- if (options.offset || options.limit !== undefined) {
202
- let globalOffset = 0;
203
- let fallbackOffset = 0;
204
- for (const [batchIndex, batch] of context.table.batches.entries()) {
205
- if (batch.numRows > 0) {
206
- const selection = context.selectionMask.data[batchIndex];
207
- addGPUGlobalTopKSelectionPass(graph, `${prefix}-select-batch-${batchIndex}`, {
208
- ranks: scratch.sourceClasses,
209
- selection,
210
- globalOffset,
211
- offset: options.offset ?? 0,
212
- limit: options.limit ?? MAXIMUM_UINT32
213
- });
214
- graph.add(
215
- new GPUVisibilityWorkflow({
216
- id: `${prefix}-visibility-batch-${batchIndex}`,
217
- predicates: [{kind: 'selection', mask: selection}],
218
- outputMask: selection,
219
- output: context.rowIndices.data[batchIndex],
220
- count: context.selectedCounts.data[batchIndex],
221
- firstSourceIndex: batch.sourceInfo?.sourceRowIndexOffset ?? fallbackOffset
222
- })
223
- );
224
- }
225
- globalOffset += batch.numRows;
226
- fallbackOffset += batch.numRows;
227
- }
228
- }
229
- }
230
-
231
158
  const projection = resultColumns && projectGPUDataFrameQueryResult(context, resultColumns);
232
159
  return {
233
160
  table: projection?.table ?? context.table,
@@ -252,6 +179,96 @@ function addGPUGlobalSortToGraph<Selection extends GPUTypeMap>(
252
179
  }
253
180
  }
254
181
 
182
+ /** Orders a prepared table and its selection within the caller's open graph. @internal */
183
+ export function addGPUGlobalOrdering<Selection extends GPUTypeMap>(
184
+ context: GPUDataFrameQueryExtensionContext<Selection>,
185
+ keys: readonly GPUGlobalSortKey<keyof Selection & string>[],
186
+ ownedVectors: GPUVector[],
187
+ prefix: string
188
+ ): {globalRowIndices: GPUVector<'uint32'>; globalSelectedCount: GPUVector<'uint32'>} {
189
+ const {graph} = context;
190
+ const {options} = keys[0];
191
+ const length = context.table.numRows;
192
+ const globalRowIndices = createGPUAnalyticsOutputVector(
193
+ graph.device,
194
+ `${prefix}-row-indices`,
195
+ 'uint32',
196
+ length
197
+ );
198
+ ownedVectors.push(globalRowIndices);
199
+ const globalSelectedCount = createGPUAnalyticsOutputVector(
200
+ graph.device,
201
+ `${prefix}-selected-count`,
202
+ 'uint32',
203
+ 1
204
+ );
205
+ ownedVectors.push(globalSelectedCount);
206
+ const globalRows = graph.importGPUVector(`${prefix}-row-vector`, globalRowIndices).data[0];
207
+ const selectedCount = graph.importGPUVector(`${prefix}-count-vector`, globalSelectedCount)
208
+ .data[0];
209
+
210
+ graph.add(
211
+ new GPUReduction({
212
+ id: `${prefix}-count-selected`,
213
+ input: context.selectedCounts,
214
+ output: selectedCount,
215
+ operation: 'sum'
216
+ })
217
+ );
218
+
219
+ if (length > 0) {
220
+ const scratch = createGPUGlobalSortScratch(graph, prefix, length);
221
+ // Stable sorts transfer the ordering of less significant keys through ties.
222
+ for (let index = keys.length - 1; index >= 0; index--) {
223
+ addGPUGlobalSortKeyPasses(
224
+ context,
225
+ `${prefix}-key-${index}`,
226
+ keys[index],
227
+ scratch,
228
+ index === keys.length - 1
229
+ );
230
+ }
231
+
232
+ addGPUGlobalSortPublishPass(graph, `${prefix}-publish`, {
233
+ scratch,
234
+ output: globalRows,
235
+ count: selectedCount,
236
+ offset: options.offset ?? 0,
237
+ limit: options.limit ?? MAXIMUM_UINT32
238
+ });
239
+
240
+ if (options.offset || options.limit !== undefined) {
241
+ let globalOffset = 0;
242
+ let fallbackOffset = 0;
243
+ for (const [batchIndex, batch] of context.table.batches.entries()) {
244
+ if (batch.numRows > 0) {
245
+ const selection = context.selectionMask.data[batchIndex];
246
+ addGPUGlobalTopKSelectionPass(graph, `${prefix}-select-batch-${batchIndex}`, {
247
+ ranks: scratch.sourceClasses,
248
+ selection,
249
+ globalOffset,
250
+ offset: options.offset ?? 0,
251
+ limit: options.limit ?? MAXIMUM_UINT32
252
+ });
253
+ graph.add(
254
+ new GPUVisibilityWorkflow({
255
+ id: `${prefix}-visibility-batch-${batchIndex}`,
256
+ predicates: [{kind: 'selection', mask: selection}],
257
+ outputMask: selection,
258
+ output: context.rowIndices.data[batchIndex],
259
+ count: context.selectedCounts.data[batchIndex],
260
+ firstSourceIndex: batch.sourceInfo?.sourceRowIndexOffset ?? fallbackOffset
261
+ })
262
+ );
263
+ }
264
+ globalOffset += batch.numRows;
265
+ fallbackOffset += batch.numRows;
266
+ }
267
+ }
268
+ }
269
+ return {globalRowIndices, globalSelectedCount};
270
+ }
271
+
255
272
  /** Stages and stably sorts one key, starting from the permutation of less significant keys. */
256
273
  function addGPUGlobalSortKeyPasses<Selection extends GPUTypeMap>(
257
274
  context: GPUDataFrameQueryExtensionContext<Selection>,
@@ -20,7 +20,7 @@ import {
20
20
  GPUSort,
21
21
  createTransientView
22
22
  } from '@luma.gl/graph/gpu-core';
23
- import {GPUMask} from '@luma.gl/graph/gpu-core';
23
+ import {GPUMask, GPUVisibilityWorkflow} from '@luma.gl/graph/gpu-core';
24
24
  import {
25
25
  createTransientVectorView,
26
26
  getViewElementOffset,
@@ -34,12 +34,18 @@ import {
34
34
  getGPUAnalyticsSelectionMask,
35
35
  validateGPUAnalyticsOutputLength
36
36
  } from './gpu-analytics-compiler-utils';
37
- import type {GPUDataFrame, GPUDataFrameDictionaries, GPUDataFrameValidity} from './gpu-data-frame';
37
+ import {
38
+ GPUDataFrame,
39
+ type GPUDataFrameDictionaries,
40
+ type GPUDataFrameValidity
41
+ } from './gpu-data-frame';
38
42
  import type {GPUDataFrameDerivedColumn} from './gpu-data-frame-query';
39
43
  import type {GPUExpression} from './gpu-expression';
40
44
  import type {GPUDataFrameAggregationDefinition} from './gpu-group-by-query';
41
45
  import {
46
+ addGPUDataFrameResultPredicate,
42
47
  CompiledGPUDataFrameQuery,
48
+ projectGPUDataFrameQueryResult,
43
49
  compileGPUDataFrameQuery,
44
50
  type CompiledGPUDataFrameQueryProps,
45
51
  type GPUDataFrameQueryExtensionContext,
@@ -47,6 +53,10 @@ import {
47
53
  type GPUDataFrameQueryParameters
48
54
  } from './gpu-query-compiler';
49
55
 
56
+ import {addGPUGlobalOrdering, type GPUGlobalSortKey} from './gpu-global-sort-compiler';
57
+
58
+ import {prepareGPUJoinAggregation, type GPUAggregateJoin} from './gpu-join-compiler';
59
+
50
60
  const UINT32_BYTE_LENGTH = Uint32Array.BYTES_PER_ELEMENT;
51
61
  const MAXIMUM_UINT32 = 0xffffffff;
52
62
 
@@ -63,10 +73,21 @@ export class CompiledGPUDataFrameGroupedAggregation<
63
73
  /** Result table capacity. Observed grouping publishes the actual count in selectedCounts. */
64
74
  readonly groupCount: number;
65
75
 
76
+ /** Group row indices in global ORDER BY order, when requested. */
77
+ readonly globalRowIndices?: GPUVector<'uint32'>;
78
+ /** Number of groups left after applying the page, when ordered. */
79
+ readonly globalSelectedCount?: GPUVector<'uint32'>;
80
+
66
81
  /** @internal */
67
- constructor(props: CompiledGPUDataFrameQueryProps<T>, groupCount: number) {
82
+ constructor(
83
+ props: CompiledGPUDataFrameQueryProps<T>,
84
+ groupCount: number,
85
+ ordering?: ReturnType<typeof addGPUGlobalOrdering>
86
+ ) {
68
87
  super(props);
69
88
  this.groupCount = groupCount;
89
+ this.globalRowIndices = ordering?.globalRowIndices;
90
+ this.globalSelectedCount = ordering?.globalSelectedCount;
70
91
  }
71
92
  }
72
93
 
@@ -90,7 +111,11 @@ export function compileGPUDataFrameObservedAggregation(
90
111
  keys: GPUGroupingKeys,
91
112
  definitions: readonly GPUDataFrameAggregationDefinition[],
92
113
  graph: GPUCommandGraph<GPUDataFrameQueryParameters>,
93
- columns: readonly string[]
114
+ columns: readonly string[],
115
+ ordering?: readonly GPUGlobalSortKey[],
116
+ having?: GPUExpression<boolean, string>,
117
+ join?: GPUAggregateJoin,
118
+ maxMeanScratchBytes?: number
94
119
  ): CompiledGPUDataFrameGroupedAggregation {
95
120
  if (source.numRows > 0) validateGPUGroupingCapacity(source, source.numRows, graph);
96
121
  validateGPUGroupingSourceColumns(
@@ -104,8 +129,21 @@ export function compileGPUDataFrameObservedAggregation(
104
129
  GPUTypeMap,
105
130
  CompiledGPUDataFrameGroupedAggregation
106
131
  >(source, predicates, selectedColumns, graph, derivedColumns, {
107
- prepare: context =>
108
- addGPUGroupedAggregationToGraph(context, keys, source.numRows, definitions, columns, true)
132
+ prepare: context => {
133
+ const prepare = (input: typeof context) =>
134
+ addGPUGroupedAggregationToGraph(
135
+ input,
136
+ keys,
137
+ source.numRows,
138
+ definitions,
139
+ columns,
140
+ true,
141
+ ordering,
142
+ having,
143
+ maxMeanScratchBytes
144
+ );
145
+ return join ? prepareGPUJoinAggregation(source, context, join, prepare) : prepare(context);
146
+ }
109
147
  });
110
148
  }
111
149
 
@@ -123,7 +161,11 @@ export function compileGPUDataFrameGroupedAggregation<
123
161
  groupCount: number,
124
162
  definitions: readonly GPUDataFrameAggregationDefinition[],
125
163
  graph: GPUCommandGraph<GPUDataFrameQueryParameters>,
126
- projection?: GPUGroupedOutputProjection
164
+ projection?: GPUGroupedOutputProjection,
165
+ ordering?: readonly GPUGlobalSortKey<keyof Result & string>[],
166
+ having?: GPUExpression<boolean, string>,
167
+ join?: GPUAggregateJoin,
168
+ maxMeanScratchBytes?: number
127
169
  ): CompiledGPUDataFrameGroupedAggregation<Result> {
128
170
  validateGPUGroupingCapacity(source, groupCount, graph);
129
171
  validateGPUGroupingSourceColumns(source, [key], definitions);
@@ -133,14 +175,21 @@ export function compileGPUDataFrameGroupedAggregation<
133
175
  Result,
134
176
  CompiledGPUDataFrameGroupedAggregation<Result>
135
177
  >(source, predicates, selectedColumns, graph, derivedColumns, {
136
- prepare: context =>
137
- addGPUGroupedAggregationToGraph<Selection, Result>(
138
- context,
139
- [{source: key, output: projection?.key ?? key}],
140
- groupCount,
141
- definitions,
142
- projection?.columns
143
- )
178
+ prepare: context => {
179
+ const prepare = (input: typeof context) =>
180
+ addGPUGroupedAggregationToGraph<Selection, Result>(
181
+ input,
182
+ [{source: key, output: projection?.key ?? key}],
183
+ groupCount,
184
+ definitions,
185
+ projection?.columns,
186
+ false,
187
+ ordering,
188
+ having,
189
+ maxMeanScratchBytes
190
+ );
191
+ return join ? prepareGPUJoinAggregation(source, context, join, prepare) : prepare(context);
192
+ }
144
193
  });
145
194
  }
146
195
 
@@ -196,12 +245,16 @@ function addGPUGroupedAggregationToGraph<Selection extends GPUTypeMap, Result ex
196
245
  groupCount: number,
197
246
  definitions: readonly GPUDataFrameAggregationDefinition[],
198
247
  columns?: readonly string[],
199
- observed = false
248
+ observed = false,
249
+ orderBy?: readonly GPUGlobalSortKey<keyof Result & string>[],
250
+ having?: GPUExpression<boolean, string>,
251
+ maxMeanScratchBytes?: number
200
252
  ): GPUDataFrameQueryExtensionResult<Result, CompiledGPUDataFrameGroupedAggregation<Result>> {
201
253
  const graph = context.graph;
202
254
  const prefix = `${context.queryId}-group`;
203
255
  const ownedVectors: GPUVector[] = [];
204
256
  let resultTable: GPUTable<Result> | undefined;
257
+ let projection: GPUDataFrame<Result> | undefined;
205
258
 
206
259
  try {
207
260
  const inputs = groupingKeys.map(({source}, index) =>
@@ -254,7 +307,7 @@ function addGPUGroupedAggregationToGraph<Selection extends GPUTypeMap, Result ex
254
307
  };
255
308
  })
256
309
  : [];
257
- const rowSelection = observed
310
+ let rowSelection = observed
258
311
  ? addGPUObservedGroups(context, prefix, inputs, outputs, nullableKeys, ownedVectors)
259
312
  : undefined;
260
313
  const keys = rowSelection?.keys ?? (inputs[0] as GraphVectorView<'uint32'>);
@@ -333,7 +386,8 @@ function addGPUGroupedAggregationToGraph<Selection extends GPUTypeMap, Result ex
333
386
  values: state.values,
334
387
  mask: state.mask,
335
388
  output: graph.importGPUVector(`${metricId}-output`, output).data[0],
336
- operation: definition.operation
389
+ operation: definition.operation,
390
+ maxMeanScratchBytes
337
391
  })
338
392
  );
339
393
  }
@@ -343,7 +397,12 @@ function addGPUGroupedAggregationToGraph<Selection extends GPUTypeMap, Result ex
343
397
  groupingKeys,
344
398
  definitions,
345
399
  outputVectors,
346
- columns,
400
+ columns && [
401
+ ...columns,
402
+ ...definitions
403
+ .filter(definition => !columns.includes(definition.name))
404
+ .map(definition => definition.name)
405
+ ],
347
406
  observed
348
407
  );
349
408
  const dictionaries = Object.freeze(
@@ -356,11 +415,117 @@ function addGPUGroupedAggregationToGraph<Selection extends GPUTypeMap, Result ex
356
415
  )
357
416
  ) as Readonly<GPUDataFrameDictionaries<Result>>;
358
417
 
418
+ if ((orderBy || having) && !rowSelection) {
419
+ const mask = createGPUGroupedOutputVector(
420
+ graph.device,
421
+ prefix + '-dense-mask',
422
+ groupCount,
423
+ 'uint32'
424
+ );
425
+ const rows = createGPUGroupedOutputVector(
426
+ graph.device,
427
+ prefix + '-dense-rows',
428
+ groupCount,
429
+ 'uint32'
430
+ );
431
+ const count = createGPUGroupedOutputVector(
432
+ graph.device,
433
+ prefix + '-dense-count',
434
+ 1,
435
+ 'uint32'
436
+ );
437
+ ownedVectors.push(mask, rows, count);
438
+ addGPUGroupingPass(
439
+ graph,
440
+ prefix + '-dense-selection',
441
+ groupCount,
442
+ {},
443
+ {
444
+ mask: graph.importGPUVector(prefix + '-dense-mask-init', mask).data[0],
445
+ rows: graph.importGPUVector(prefix + '-dense-rows-init', rows).data[0],
446
+ count: graph.importGPUVector(prefix + '-dense-count-init', count).data[0]
447
+ },
448
+ `mask[index] = 1u; rows[index] = index; if (index == 0u) { count[0] = ${groupCount}u; }`
449
+ );
450
+ rowSelection = {keys, mask, rows, count};
451
+ }
452
+ if (having && rowSelection && groupCount > 0) {
453
+ const selectionMask = graph.importGPUVector(prefix + '-having-selection', rowSelection.mask);
454
+ const predicateMask = createTransientVectorView(
455
+ graph,
456
+ prefix + '-having-mask',
457
+ selectionMask
458
+ );
459
+ const groupedFrame = new GPUDataFrame({
460
+ table: resultTable,
461
+ validity: validity as GPUDataFrameValidity<Result>,
462
+ dictionaries,
463
+ ownership: 'borrowed'
464
+ });
465
+ try {
466
+ addGPUDataFrameResultPredicate(
467
+ graph,
468
+ prefix + '-having',
469
+ groupedFrame,
470
+ having,
471
+ predicateMask
472
+ );
473
+ } finally {
474
+ groupedFrame.destroy();
475
+ }
476
+ const mask = createGPUGroupedOutputVector(
477
+ graph.device,
478
+ prefix + '-having-result',
479
+ groupCount,
480
+ 'uint32'
481
+ );
482
+ ownedVectors.push(mask);
483
+ graph.add(
484
+ new GPUVisibilityWorkflow({
485
+ id: prefix + '-having-filter',
486
+ predicates: [
487
+ {kind: 'selection', mask: selectionMask.data[0]},
488
+ {kind: 'selection', mask: predicateMask.data[0]}
489
+ ],
490
+ outputMask: graph.importGPUVector(prefix + '-having-result-view', mask).data[0],
491
+ output: graph.importGPUVector(prefix + '-having-rows', rowSelection.rows).data[0],
492
+ count: graph.importGPUVector(prefix + '-having-count', rowSelection.count).data[0]
493
+ })
494
+ );
495
+ rowSelection = {...rowSelection, mask};
496
+ }
497
+
498
+ const ordering =
499
+ orderBy && rowSelection
500
+ ? addGPUGlobalOrdering(
501
+ {
502
+ graph,
503
+ queryId: prefix + '-ordered',
504
+ table: resultTable,
505
+ validity: validity as GPUDataFrameValidity<Result>,
506
+ dictionaries,
507
+ selectionMask: graph.importGPUVector(prefix + '-ordered-mask', rowSelection.mask),
508
+ rowIndices: graph.importGPUVector(prefix + '-ordered-rows', rowSelection.rows),
509
+ selectedCounts: graph.importGPUVector(prefix + '-ordered-count', rowSelection.count)
510
+ },
511
+ orderBy,
512
+ ownedVectors,
513
+ prefix + '-ordered'
514
+ )
515
+ : undefined;
516
+
517
+ const resultValidity = Object.freeze(validity) as Readonly<GPUDataFrameValidity<Result>>;
518
+ if (columns && resultTable.schema.fields.length !== columns.length) {
519
+ projection = projectGPUDataFrameQueryResult(
520
+ {table: resultTable, validity: resultValidity, dictionaries},
521
+ columns as readonly (keyof Result & string)[]
522
+ );
523
+ }
359
524
  return {
360
- table: resultTable,
361
- validity: Object.freeze(validity) as Readonly<GPUDataFrameValidity<Result>>,
362
- dictionaries,
363
- ownedTables: [resultTable],
525
+ table: projection?.table ?? resultTable,
526
+ validity: projection?.validity ?? resultValidity,
527
+ dictionaries: projection?.dictionaries ?? dictionaries,
528
+ ownedTables: projection ? [projection, resultTable] : [resultTable],
364
529
  ownedVectors,
365
530
  createCompiled: props =>
366
531
  new CompiledGPUDataFrameGroupedAggregation(
@@ -383,10 +548,12 @@ function addGPUGroupedAggregationToGraph<Selection extends GPUTypeMap, Result ex
383
548
  ]
384
549
  }
385
550
  : props,
386
- groupCount
551
+ groupCount,
552
+ ordering
387
553
  )
388
554
  };
389
555
  } catch (error) {
556
+ projection?.destroy();
390
557
  resultTable?.destroy();
391
558
  for (const vector of ownedVectors) {
392
559
  vector.destroy();
@@ -37,7 +37,12 @@ export type GPUDataFrameColumnNamesOfFormat<
37
37
  }[SelectedColumns];
38
38
 
39
39
  /** Optional dense key-domain size when source categorical labels cannot establish it. */
40
- export type GPUDataFrameGroupByOptions = Readonly<{groupCount?: number}>;
40
+ export type GPUDataFrameGroupByOptions = Readonly<{
41
+ /** Dense category count when no dictionary supplies it. */
42
+ groupCount?: number;
43
+ /** Maximum temporary bytes for a partition of AVG accumulators. */
44
+ maxMeanScratchBytes?: number;
45
+ }>;
41
46
 
42
47
  /** One named count or a float32 statistic referencing a selected numerical column. */
43
48
  export type GPUDataFrameAggregationValue<
@@ -85,6 +90,8 @@ export class GPUDataFrameGroupByQuery<
85
90
  readonly key: Key;
86
91
  /** Number of output groups inferred from labels or supplied explicitly by the caller. */
87
92
  readonly groupCount: number;
93
+ /** Optional temporary AVG count budget. */
94
+ readonly maxMeanScratchBytes?: number;
88
95
 
89
96
  /** Validates grouping metadata entirely on the CPU. @internal */
90
97
  constructor(
@@ -116,6 +123,13 @@ export class GPUDataFrameGroupByQuery<
116
123
  throw new Error('GPUDataFrame grouping requires a positive uint32 group count');
117
124
  }
118
125
 
126
+ if (
127
+ options.maxMeanScratchBytes !== undefined &&
128
+ (!Number.isSafeInteger(options.maxMeanScratchBytes) || options.maxMeanScratchBytes < 4)
129
+ ) {
130
+ throw new Error('GPUDataFrame mean scratch budget requires at least four bytes');
131
+ }
132
+ this.maxMeanScratchBytes = options.maxMeanScratchBytes;
119
133
  this.query = query;
120
134
  this.key = key;
121
135
  this.groupCount = groupCount;
@@ -132,7 +146,7 @@ export class GPUDataFrameGroupByQuery<
132
146
  Key,
133
147
  Definitions,
134
148
  Source
135
- >(this.query, this.key, this.groupCount, definitions);
149
+ >(this.query, this.key, this.groupCount, definitions, this.maxMeanScratchBytes);
136
150
  }
137
151
  }
138
152
 
@@ -150,6 +164,8 @@ export class GPUDataFrameGroupedAggregationQuery<
150
164
  readonly key: Key;
151
165
  /** Complete category domain and grouped output row count. */
152
166
  readonly groupCount: number;
167
+ /** Optional temporary AVG count budget. */
168
+ readonly maxMeanScratchBytes?: number;
153
169
  /** Named operations normalized in the caller's original object insertion order. */
154
170
  readonly definitions: readonly GPUDataFrameAggregationDefinition[];
155
171
 
@@ -158,8 +174,10 @@ export class GPUDataFrameGroupedAggregationQuery<
158
174
  query: GPUDataFrameQuery<Logical, SelectedColumns, Source>,
159
175
  key: Key,
160
176
  groupCount: number,
161
- definitions: Definitions
177
+ definitions: Definitions,
178
+ maxMeanScratchBytes?: number
162
179
  ) {
180
+ this.maxMeanScratchBytes = maxMeanScratchBytes;
163
181
  this.query = query;
164
182
  this.key = key;
165
183
  this.groupCount = groupCount;
@@ -185,7 +203,12 @@ export class GPUDataFrameGroupedAggregationQuery<
185
203
  this.key,
186
204
  this.groupCount,
187
205
  this.definitions,
188
- graph
206
+ graph,
207
+ undefined,
208
+ undefined,
209
+ undefined,
210
+ undefined,
211
+ this.maxMeanScratchBytes
189
212
  );
190
213
  }
191
214
  }
@@ -490,6 +490,81 @@ function addGPULookupToGraph<Left extends GPUTypeMap, Right extends GPUTypeMap>(
490
490
  }
491
491
  }
492
492
 
493
+ /** Bound unique-right join consumed as a source-aligned mask by aggregation. @internal */
494
+ export type GPUAggregateJoin = Readonly<{
495
+ right: GPUDataFrame;
496
+ options: GPUDataFrameNormalizedJoinOptions;
497
+ type: GPUDataFrameJoinType;
498
+ }>;
499
+
500
+ /** Inserts join matching before an aggregate contribution without compacting the left columns. */
501
+ export function prepareGPUJoinAggregation<
502
+ Source extends GPUTypeMap,
503
+ Selection extends GPUTypeMap,
504
+ Result extends GPUTypeMap,
505
+ Compiled extends CompiledGPUDataFrameQuery<Result>
506
+ >(
507
+ source: GPUDataFrame<Source>,
508
+ context: GPUDataFrameQueryExtensionContext<Selection>,
509
+ join: GPUAggregateJoin,
510
+ prepare: (
511
+ context: GPUDataFrameQueryExtensionContext<Selection>
512
+ ) => GPUDataFrameQueryExtensionResult<Result, Compiled>
513
+ ): GPUDataFrameQueryExtensionResult<Result, Compiled> {
514
+ const {graph} = context;
515
+ validateGPUJoinSources(source, join.right, join.options, graph);
516
+ const right = join.right.select(join.right.columnNames);
517
+ const ownedVectors: GPUVector[] = [];
518
+ const id = context.queryId + '-aggregate-join';
519
+ try {
520
+ const state = buildGPUJoinIndex(context, right, join.options, ownedVectors, id);
521
+ const violation = graph.importGPUVector(id + '-violation', state.contractViolation).data[0];
522
+ const matches = createTransientVectorView(graph, id + '-matches', context.selectionMask);
523
+ const values = createTransientVectorView(graph, id + '-values', context.selectionMask);
524
+ const probes = createTransientVectorView(graph, id + '-probes', context.selectionMask);
525
+ const included = createTransientVectorView(graph, id + '-included', context.selectionMask);
526
+ const statistics = createTransientView(
527
+ graph,
528
+ id + '-statistics',
529
+ 'uint32',
530
+ GPU_HASH_QUERY_STATISTICS_LENGTH
531
+ );
532
+ for (const [batchIndex, keys] of state.maskedLeftKeys.data.entries()) {
533
+ if (!keys.length) continue;
534
+ const batchId = id + '-' + batchIndex;
535
+ graph.add(
536
+ new GPUHashIndexQuery({
537
+ id: batchId,
538
+ index: state.index,
539
+ keys,
540
+ values: values.data[batchIndex],
541
+ found: matches.data[batchIndex],
542
+ probes: probes.data[batchIndex],
543
+ statistics,
544
+ maxProbeCount: join.options.maxProbeCount
545
+ })
546
+ );
547
+ addGPUJoinClassifyPass(graph, batchId + '-classify', {
548
+ matches: matches.data[batchIndex],
549
+ selection: context.selectionMask.data[batchIndex],
550
+ violation,
551
+ included: included.data[batchIndex],
552
+ joinType: join.type
553
+ });
554
+ }
555
+ const result = prepare({...context, selectionMask: included});
556
+ return {
557
+ ...result,
558
+ ownedVectors: [...ownedVectors, ...(result.ownedVectors ?? [])],
559
+ ownedTables: [...(result.ownedTables ?? []), right]
560
+ };
561
+ } catch (error) {
562
+ for (const vector of ownedVectors) vector.destroy();
563
+ right.destroy();
564
+ throw error;
565
+ }
566
+ }
567
+
493
568
  /** Builds one right index without concatenation and sanitizes filtered/nullable left source keys. */
494
569
  function buildGPUJoinIndex<Left extends GPUTypeMap, Right extends GPUTypeMap>(
495
570
  context: GPUDataFrameQueryExtensionContext<Left>,