Repository navigation
Fix wrong results with group_by_use_nulls for a key that uses another key in several places - #131726
Conversation
With `enable_identifier_resolve_cache` (the default), every reference to an identifier inside a GROUP BY key resolves to the same node, so a key such as `if((x = 1) OR (x = 2), 'a', 'b')` grouped together with `x` holds one `x` node under several parents. `registerNullableGroupByKeys` builds the key's "sub-keys already Nullable" shape from `key->clone()`, which keeps that sharing, and `convertNestedGroupByKeysToNullable` converted the shared `x` in place on its first visit. Later parents of the same node then saw an already converted `x`, did not match it as a key, and were not re-resolved, so the shape kept stale non-Nullable types and matched no resolved expression. The projection, HAVING and ORDER BY then computed the key again from the NULL of `x`, typed as the non-Nullable key: wrong rows for ROLLUP / CUBE / GROUPING SETS, and with the OR chain kept a header type mismatch that aborts a debug build in `count`. Copy a node that several parents hold before changing it in place (copy-on-write, the same idiom as LogicalExpressionOptimizerPass). Inside the key clone a node's use count is the number of parent slots holding it, so a node is now changed only while one parent owns it, and every reference is converted exactly as in the tree that is built with the cache off. A copy only "when seen again" is not enough: the first visit has already changed the shared node by then. Regression since 26.6 (ClickHouse#88043 cache, ClickHouse#107050 exact key shapes); 26.3 is correct. Found while reviewing ClickHouse#125846. 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 cold review (2 nits, no change).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261115-031000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261115-021100 |
|
Workflow [PR], commit [9b6ba1f] Summary: ✅
AI ReviewSummaryThe PR fixes incorrect values and result types with Final VerdictStatus: ✅ Approve |
CI finish ledger - c1b64e4Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261115-063013 |
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, 25 s compile time in total, 7 of them have a recent master baseline. |
Related: #125846
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong results with
group_by_use_nulls = 1andROLLUP,CUBEorGROUPING SETSwhen aGROUP BYkey uses another key in several places, for exampleif(x = 1 OR x = 2, 'a', 'b')grouped together withx. The key was computed again from theNULLof the inner key, with a non-Nullabletype, instead of being read as the key.Description
On 26.8,
SELECT x, if((x = 1) OR (x = 2) OR (x = 3), 'a', 'b') AS e, count() FROM (SELECT number % 4 AS x FROM numbers(8)) GROUP BY ROLLUP(e, x) SETTINGS group_by_use_nulls = 1returnsbforein every row wherexisNULL: theasubtotal row and the grand-total row showbinstead ofaandNULL, andtoTypeName(e)isString. 26.3 is correct; 26.6, 26.8 and 26.10 are not.enable_identifier_resolve_cache = 0gives the right result.Root cause: the identifier resolve cache makes every reference to
xinside the key one node.registerNullableGroupByKeysbuilds the key's shape with Nullable sub-keys fromkey->clone(), which keeps that sharing, andconvertNestedGroupByKeysToNullableconverts the sharedxin place on its first visit. The other parents of that node (x = 2,x = 3) are then not re-resolved and keep a non-Nullable type, so the shape matches no expression and the projection is not replaced by the key. With the OR chain kept (optimize_min_equality_disjunction_chain_length = 1000), reading such a column hits aLOGICAL_ERRORin debug builds.Change:
convertNestedGroupByKeysToNullablecopies a node held by several parents before changing it (use_count() > 1, as inLogicalExpressionOptimizerPass), so each reference is converted as in the tree built without the cache.Test:
ROLLUP,CUBE,GROUPING SETS, an inline key,HAVING, an alias above the key, a key inside a key, and the outer read; the reference equals the output with the cache off. It fails on master and passes with the fix (50 randomized runs). The other 135group_by_use_nulls/ identifier cache /ROLLUP/CUBE/GROUPING SETStests give the same results as on master.Found while reviewing #125846 (comment).
Workflow [PR]
Sync PR [sync-upstream/pr/131726]
Version info
26.11.1.2855-master(included in26.11and later)