Repository navigation
Fix wrong result when a sumIf alias is referenced more than once - #122726
Conversation
With `enable_identifier_resolve_cache` (default since 26.6) every reference
to an alias resolves to the same query tree node, so in
SELECT sumIf(2, number > 1) AS r, r + 0, r + 1 FROM numbers(12)
SELECT sumIf(2, number > 1) AS r FROM numbers(12) HAVING r > 3 AND r < 15
all references share one `sumIf(2, number > 1)` node. `SumIfToCountIfPass`
rewrote the node it visited in place into `countIf(number > 1)` and wrapped
only that reference in `multiply(2, ...)`. Every later reference then saw
`countIf`, failed the `sum`/`sumIf` entry check and kept the bare `countIf`,
so it lost the factor: `r + 1` returned 11 instead of 21, and the HAVING
compared `countIf` with 15 and returned the row. The same happened for
`sum(if(cond, 0, k))` at default settings and for `sum(if(cond, k, 0))`
with `optimize_rewrite_aggregate_function_with_if = 0`.
The pass now leaves the visited node unchanged and assigns a new `countIf`
node (wrapped in `multiply` when the constant is not 1) to each reference,
like `RewriteAggregateFunctionWithIfPass` does. Every reference is rewritten
on its own visit, and the tree with the cache on is the same as with it off.
Cloning shared nodes in the cache was rejected in ClickHouse#108553, which made the
pass it fixed independent of node sharing instead.
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).
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20260928-172600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20260928-142200 |
|
Workflow [PR], commit [72ed40e] Summary: ✅
AI ReviewSummaryThis PR fixes the shared-query-tree alias bug in Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 72ed40e.
Changed lines: Changed C/C++ lines covered: 26/28 (92.86%) · Uncovered code |
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, 8 s compile time in total, 7 of them have a recent master baseline. |
Backport #122726 to 26.10: Fix wrong result when a `sumIf` alias is referenced more than once
Related: #122404
Related: #122512
Related: #108553
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a wrong result when an alias of
sumIf(k, cond)orsum(if(cond, 0, k))with a constantkother than 1 is referenced more than once, for exampleSELECT sumIf(2, cond) AS r, r + 0, r + 1orHAVING r > 3 AND r < 15: every reference after the first returnedcountIfwithout the factork.Description
On 26.6 to 26.9,
SELECT sumIf(2, number > 1) AS r, r + 0, r + 1 FROM numbers(12)returns20 20 11instead of20 20 21, andSELECT sumIf(2, number > 1) AS r FROM numbers(12) HAVING r > 3 AND r < 15returns20instead of no rows. 26.3 is correct. It also reproduces on play.clickhouse.com.Cause. With
enable_identifier_resolve_cache(default since 26.6) every reference to an alias is the same query tree node.SumIfToCountIfPassrewrote that node in place intocountIf(cond)and wrapped only the reference it was visiting ink * ..., so the other references kept the barecountIfand lostk.Change. The pass no longer modifies the visited node: it builds a new
countIf(andmultiply) node for each reference, asRewriteAggregateFunctionWithIfPassalready does. This follows #108553, where cloning in the cache was declined and the pass was made independent of node sharing. Found while fixing #122404 (#122512 fixesIfConstantConditionPass).Validation. The new test fails on master in every statement and passes 50 randomized runs. No new failures in 184 related tests.
remote()returns the right values, also with unfixed shards.Workflow [PR]
Sync PR [sync-upstream/pr/122726]
Version info
26.11.1.2450-master(included in26.11and later)26.10.5.42