Repository navigation
Build each sum/count/avg replacement once in FuseFunctionsPass - #128371
alexey-milovidov merged 1 commit into
Conversation
`tryFuseSumCountAvg` called `replaceWithSumCount` for every node of a group. Each call formats the node's AST for its alias and resolves new `tupleElement` functions (plus `toFloat64` and `divide` for `avg`) through `FunctionFactory`. When `RewriteSumFunctionWithSumAndCountPass` turns ClickBench Q30's 90 `sum(x + k)` into `sum(x) + k * count(x)`, the pass sees 90 `sum(x)` and 89 `count(x)` over one column and repeats that work 177 times with identical results. Now the first node of each function name in a group goes through `replaceWithSumCount`, and every later equal node gets a copy built by `copySumCountReplacement`. The copy reuses the resolved functions and the alias, and keeps sharing the one `sumCount` node, as the original code already did across all replacements of a group. The fast path is taken only for nodes over a COLUMN argument that have no alias and are not parenthesized. Other nodes keep the old path: - an alias is user-visible and per node; - `isEqual` and the tree hash ignore the parenthesized flag, but `formatForErrorMessage` prints it, so `sum(x)` and `(sum(x))` land in one group with different aliases; - non-column arguments can be equal but format differently (`x * 2` vs `multiply(x, 2)`). The resulting query tree is identical to the one built before, including node sharing, so `EXPLAIN QUERY TREE` output does not change. The test gets a query with `sum`/`count` twins from the arithmetic rewrite, `avg` twins and a parenthesized `avg` twin; its reference was generated with the binary before this change. Related: ClickHouse#128352 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review (2 nits).
Severity: Session id: cron:clickhouse-review-slot-9:20261031-122100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261031-113800 |
|
cc @vdimir @yakov-olkhovskiy, could you review this and add the |
|
Workflow [PR], commit [dc753dc] Summary: ✅
AI ReviewSummary
Final Verdict✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline. |
d2a4a8a
Related: #128352
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Faster planning of queries with many
sum,countoravgover the same column, such as ClickBench Q30:optimize_syntax_fuse_functionsnow builds the replacement of each function once and copies it to the other equal aggregates.EXPLAIN PLANof Q30 executes 3.9% fewer instructions and 4.3% fewer CPU cycles.Description
optimize_arithmetic_operations_in_aggregate_functionsturns the 90SUM(ResolutionWidth + k)of ClickBench Q30 intosum(x) + k * count(x), soFuseFunctionsPasssees 90sum(x)and 89count(x)over one column. For each of them,replaceWithSumCountformatted the node's AST for its alias and resolved newtupleElementfunctions (plustoFloat64anddivideforavg) throughFunctionFactory, with the same result every time.Now the first node of each function name in a group is replaced as before. Every other equal node gets a copy that reuses its resolved functions and alias and shares the same
sumCountnode, as all replacements of a group already did. Only nodes over a column with no alias and no parentheses are copied:isEqualignores parentheses, but the formatted alias keeps them, sosum(x)and(sum(x))get different aliases. The query tree is unchanged, node sharing included.Release build, x86,
clickhouse localpinned to one idle core,perf statover 200 xEXPLAIN PLANQ30, 12 rounds, master vs this PR:SUM,COUNTandAVGis unchanged.The gain is in planning, about 0.4 ms per Q30. @ snelheidai measured -4.3% instructions and -9.6% cycles on Graviton3, on top of #128359: #128352 (comment)
Results,
EXPLAIN QUERY TREEandEXPLAIN actionsof 26 queries (aliases, parentheses,IGNORE NULLS, non-column arguments,HAVING/ORDER BY,remote, Q30) are identical to master on release and debug builds. The test gets the Q30 shape, equalavgnodes and a parenthesized one; its reference comes from master.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/128371]
Version info
26.11.1.994-master(included in26.11and later)