Repository navigation
Fix wrong results and NOT_FOUND_COLUMN_IN_BLOCK for a GROUP BY key inside an if chain or an ORDER BY tuple - #125828
Conversation
After `GROUP BY` only the keys and the aggregates exist, and the planner matches a subexpression to a key column by its action name. Two passes dissolved a key node into its arguments in such an expression, so the planner recomputed it from the arguments: those either do not exist after aggregation (`NOT_FOUND_COLUMN_IN_BLOCK`), or, under `group_by_use_nulls`, are not `NULL` on the subtotal rows where the key is `NULL` (wrong results). - `IfChainToMultiIfPass` spliced every nested 3-argument `if` of an else branch into one `multiIf`, also a key or its `group_by_use_nulls` copy. - `OrderByTupleEliminationPass` split every `ORDER BY tuple(...)`, also a tuple key. The analyzer expands top-level tuple keys, so such a key comes from `IfConstantConditionPass` folding `if(1, tuple(a), tuple(0))`. ClickHouse#122512 made `IfConstantConditionPass` also fold the `Nullable` copy of a constant-condition key, so the constant-condition forms of ClickHouse#125803 and ClickHouse#125804 regressed on master; a bare `if` key nested in a chain, and the `group_by_use_nulls = 0` forms, were already broken on 26.3 - 26.9. Both passes now ask `isGroupByKeyAfterAggregation`: the node is a copy on which `convertToNullable` was called (new `FunctionNode::isConvertedToNullable`, which also covers an `Array` copy whose type cannot become `Nullable`), or equal to a key by `compareGroupByKeys`. `IfChainToMultiIfPass` rewrites nodes while it walks the query, and with the identifier resolve cache a key node is shared with its references, so it classifies the query's `if` nodes by pointer when it enters the query, before rewriting it, and only when the setting is on and the query has an `if` key. The check for the copy's own rewrite now uses the same flag instead of comparing result types. Closes ClickHouse#125803 Closes ClickHouse#125804 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the collectGroupByKeyIfNodes and isGroupByKeyAfterAggregation comments to their invariants. 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 (3 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261014-154400 |
|
Workflow [PR], commit [da75a09] Summary: ✅
AI ReviewSummaryPreserves Final VerdictStatus: ✅ Approve |
|
cc @nihalzp @novikd, could you review this and add |
Backport #125828 to 26.10: Fix wrong results and `NOT_FOUND_COLUMN_IN_BLOCK` for a `GROUP BY` key inside an `if` chain or an `ORDER BY` tuple
Backport #125828 to 26.9: Fix wrong results and `NOT_FOUND_COLUMN_IN_BLOCK` for a `GROUP BY` key inside an `if` chain or an `ORDER BY` tuple
Resolve the 26.3 conflicts of the cherry-pick of #125828: - IfChainToMultiIfPass.cpp: keep the 26.3 "result type changed" return. The conflicting master hunk changes the `group_by_use_nulls` multiIf rewrite that #122512 added; 26.3 does not have #122512, so there is nothing to change there. All other hunks of #125828 apply as is. - OptimizeKeyExpressionsUtils.{h,cpp}: the files do not exist on 26.3 (master added them in the GROUP BY passes refactor). Add them with only the two helpers #125828 introduces, getGroupByKeys and isGroupByKeyAfterAggregation; compareGroupByKeys already exists in Analyzer/ValidationUtils.h on 26.3. - 05397 test: `SET enable_analyzer = 1` for the 26.3 old analyzer jobs, and drop `enable_identifier_resolve_cache = 1` from one statement (the setting does not exist on 26.3). The reference is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #125828 to 26.3: Fix wrong results and `NOT_FOUND_COLUMN_IN_BLOCK` for a `GROUP BY` key inside an `if` chain or an `ORDER BY` tuple
…BLOCK` for a `GROUP BY` key inside an `if` chain or an `ORDER BY` tuple
Backport #125828 to 26.3: Fix wrong results and `NOT_FOUND_COLUMN_IN_BLOCK` for a `GROUP BY` key inside an `if` chain or an `ORDER BY` tuple
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
NOT_FOUND_COLUMN_IN_BLOCKand wrong results when aGROUP BYkey is used inside anifchain withoptimize_if_chain_to_multiif = 1, or is a tuple used inORDER BY(for exampleif(1, tuple(a), tuple(0)) AS k ... GROUP BY k ORDER BY k). Withgroup_by_use_nulls = 1, such anifkey returned its value instead ofNULLinROLLUP,CUBEandGROUPING SETSsubtotal rows. Closes #125803. Closes #125804.Description
Closes #125803, closes #125804. Related: #122512.
On 26.9.7.6,
SELECT if(1, tuple(a), tuple(0)) AS k, count() FROM (SELECT number % 3 AS a FROM numbers(10)) GROUP BY k ORDER BY kthrowsNOT_FOUND_COLUMN_IN_BLOCK, and withoptimize_if_chain_to_multiif = 1andgroup_by_use_nulls = 1anifkey nested in anotherifreturns its value instead ofNULLinROLLUPsubtotal rows. Since #122512 the constant-condition forms of both issues fail on master too.After
GROUP BYthe planner reads a key from the aggregation result by its action name.IfChainToMultiIfPassspliced a nestedifthat is a key (or itsgroup_by_use_nullscopy) into amultiIf, andOrderByTupleEliminationPasssplit a tuple key into its elements, so the key was recomputed from arguments that are gone after aggregation, or are notNULLon subtotal rows.Both passes now keep such a node whole via one helper: the node is a copy marked by
convertToNullable(newFunctionNode::isConvertedToNullable, which also covers anArraycopy whose type cannot becomeNullable), or equals a key bycompareGroupByKeys.IfChainToMultiIfPassclassifies the query'sifnodes before rewriting it, since it changes nodes inside keys and a key can be shared with its references; this costs nothing unless the setting is on and the query has anifkey.LogicalExpressionOptimizerPass(orchains toIN) has the same pattern for keys likex = 1; it is not changed here.The new test fails on master and passes with the fix, 50/50 with randomized settings; related
group_by_use_nulls,ROLLUP,CUBE,GROUPING SETSandmultiIftests pass.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/125828]
Version info
26.11.1.197-master(included in26.11and later)26.10.8.39,26.9.34.19,26.3.70.3