Open History to see the last commit
1---
2id: groupingCompiler
3title: groupingCompiler
4---
5
6```ts
7const groupingCompiler: object;
8```
9
10Defined in: [packages/db/src/query/compiler/grouping.ts:7](https://github.com/TanStack/db/blob/main/packages/db/src/query/compiler/grouping.ts#L7)
11
12The grouping compiler. Aggregate values and `groupKeys` carry it onto the
13query IR, so apps that never group or aggregate do not bundle it.
14
15## Type Declaration
16
17### processGroupBy()
18
19```ts
20processGroupBy: (pipeline, groupByClause, valueIdentity, isSourceRef, havingClauses?, selectClause?, fnHavingClauses?, aggregateCollectionId?, mainSource?, sanitizeCallbackRows) => NamespacedAndKeyedStream;
21```
22
23Processes the GROUP BY clause with optional HAVING and SELECT
24Works with the new $selected structure from early SELECT processing
25
26#### Parameters
27
28##### pipeline
29
30[`NamespacedAndKeyedStream`](../type-aliases/NamespacedAndKeyedStream.md)
31
32##### groupByClause
33
34[`GroupBy`](../@tanstack/namespaces/IR/type-aliases/GroupBy.md)
35
36##### valueIdentity
37
38`ValueIdentity`
39
40##### isSourceRef
41
42(`ref`) => `boolean`
43
44Whether a ref reads a field of the query's own rows.
45
46##### havingClauses?
47
48[`Where`](../@tanstack/namespaces/IR/type-aliases/Where.md)[]
49
50##### selectClause?
51
52[`Select`](../@tanstack/namespaces/IR/type-aliases/Select.md)
53
54##### fnHavingClauses?
55
56(`row`) => `any`[]
57
58##### aggregateCollectionId?
59
60`string`
61
62##### mainSource?
63
64`string`
65
66##### sanitizeCallbackRows?
67
68`boolean` = `false`
69
70#### Returns
71
72[`NamespacedAndKeyedStream`](../type-aliases/NamespacedAndKeyedStream.md)
73
74### replaceAggregatesByRefs()
75
76```ts
77replaceAggregatesByRefs: (havingExpr, selectClause, resultAlias) => BasicExpression;
78```
79
80Transforms expressions to replace aggregate functions with references to computed values.
81
82For aggregate expressions, finds matching aggregates in the SELECT clause and replaces them
83with PropRef([resultAlias, alias]) to reference the computed aggregate value.
84
85Ref expressions (table columns and $selected fields) and value expressions are passed through unchanged.
86Function expressions are recursively transformed.
87
88#### Parameters
89
90##### havingExpr
91
92The expression to transform (can be aggregate, ref, func, or val)
93
94[`Aggregate`](../@tanstack/namespaces/IR/classes/Aggregate.md)\<`any`\> | [`BasicExpression`](../@tanstack/namespaces/IR/type-aliases/BasicExpression.md)
95
96##### selectClause
97
98[`Select`](../@tanstack/namespaces/IR/type-aliases/Select.md)
99
100The SELECT clause containing aliases and aggregate definitions
101
102##### resultAlias
103
104`string` = `...`
105
106The namespace alias for SELECT results (default: '$selected')
107
108#### Returns
109
110[`BasicExpression`](../@tanstack/namespaces/IR/type-aliases/BasicExpression.md)
111
112A transformed BasicExpression that references computed values instead of raw expressions