Repository navigation
Conversation
…p_by_use_nulls
A GROUP BY key that is an `if`/`multiIf` with a constant condition is rewritten to
the branch it always takes by `IfConstantConditionPass`, which runs after
`QueryAnalysisPass`. That branch is then the expression the aggregation actually
groups by, and the one whose type ROLLUP/CUBE/GROUPING SETS turn Nullable under
`group_by_use_nulls`. But `nullable_group_by_keys` is populated during resolution
and only ever held the un-collapsed shape.
The rewrite reaches the key and only the key, because
`registerNullableGroupByKeys` stores the key node unchanged and puts the Nullable
form in a clone, so the pass's result-type check holds for the key while every
projection, ORDER BY and HAVING copy of it is Nullable-wrapped and left alone.
A correlated reference to the collapsed-to column therefore matched no registered
key, was not converted in `resolveExpressionNode`, and kept its non-Nullable
type. The subquery's placeholder was created with that type,
`ActionsDAG::decorrelate` turns it into an input without touching `result_type`,
and `ActionsDAG::updateHeader` binds it to the header column by name only. For a
`Bool` key the resulting `FunctionCast` was built for a non-Nullable source, so
`prepareRemoveNullable` skipped `createBlockWithNestedColumns` and handed a
`ColumnNullable` to the strict `checkAndGetColumn<ColumnUInt8>` in
`createUInt8ToBoolWrapper`.
Register the collapsed shape of each key as a key as well. The predicate that
decides whether a node collapses, and to what, is extracted out of
`IfConstantConditionPass` into `tryCollapseConstantConditionFunction` and called
from both places, so what the resolver predicts and what the pass does cannot
drift apart if either is changed later. Exactly one shape is added per key,
mirroring the visitor, which replaces a node position once and then descends into
the replacement's children: `if(1, if(1, x, y), z)` collapses to the inner `if`,
so registering `x` would promote references to a column that is not a key.
A branch that is a constant is left out. A correlated reference is always a
ColumnNode, so a constant entry could never serve one, and the map is keyed by
value and type: a same-valued literal in the projection would match that entry and
be converted to Nullable, which is what
03926_group_by_use_nulls_constant_type.sql exists to prevent. For `if` the whole
node is already folded to that constant during resolution, by
`FunctionIf::getConstantResultForNonConstArguments`, so `multiIf`, which has no
such fold, is the shape that reaches this. Measured on
`GROUP BY multiIf(1, 'a', c0) WITH ROLLUP`: `toTypeName('a')` in the projection is
String before this change and after it, and was Nullable(String) in between.
This continues 310f78a, which established that this class of defect is
fixed by making the analyzer's `group_by_use_nulls` typing correct rather than by
reconciling types in the planner.
Only `Bool` made this fatal. `UInt8`, `UInt64` and `String` keys reach a cast
wrapper that tolerates the extra Nullable layer, so they returned correct values
while carrying the same type mismatch; the new test asserts their values so a
future change cannot regress them to NOT_IMPLEMENTED.
`LogicalExpressionOptimizerPass` reaches the same invariant through boundary
folding and operand pruning, and `GROUP BY c0 AND (u < 300)` with `u UInt8` still
fails the same way. That promotion cannot be expressed as this predicate and is
left for a separate change.
The test comment said the constant that a constant-condition `if` folds to is not a registered GROUP BY key. It is: the fold happens during resolution, so the constant is the written key, and the test's own reference shows a same-valued projection literal turning `Nullable` with it while the unfolded `multiIf` rows keep `String`. The doc block on `registerNullableGroupByKeys` said all shapes map to the original key node. Each maps to the key whose shape it is, which is what the appended collapsed branches require, since each of those is itself a key in the list.
The field's doc block said every registered shape maps to the original key node and listed three shapes. Registration maps each shape to the key node it is a shape of, and a key that is an `if`/`multiIf` with a constant condition contributes a fourth shape: the branch the key is later collapsed to, which is what the aggregation groups by. Comment text only, no behaviour change.
…ation
The sentence about the collapsed branch read as unconditional, but
`registerNullableGroupByKeys` skips a collapsed branch whose node type is
`CONSTANT`. `GROUP BY multiIf(1, 'a', c0)` reaches registration still a
`multiIf`, because `multiIf` has no constant fold, so its taken branch `'a'`
is not registered. Lines 50-53 of
`05061_group_by_use_nulls_constant_condition_group_by_key.reference` are the
measured proof: `toTypeName('a')` stays `String` there.
A reader who took the field documentation as the contract could remove that
guard and reintroduce the type regression it prevents.
Comment only, no behaviour change.
Internal second-model reviewIndependent cold reviews of this change: my own, and a second model reviewing the diff without my ❌ BlockersNone.
|
|
Workflow [PR], commit [5fa021e] Summary: ❌
AI ReviewSummaryThis PR fixes Final Verdict
LLVM Coverage ReportMeasured on commit 5fa021e.
Changed lines: Changed C/C++ lines covered: 30/31 (96.77%) · Uncovered code |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260831-022400 |
|
cc @KochetovNicolai @novikd, could you review this? |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units78 translation units recompiled, 507 s compile time in total, 78 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.10 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
CI finish ledger -
|
| Check / test | Reason | Owner / fixing PR |
|---|---|---|
Integration tests (amd_asan_ubsan, db disk, old analyzer, 2/6) / Container memory budget exceeded (/docker) |
the job's containers breach the /docker cgroup budget and the kernel kills processes inside it, so the shard errors at job level with no test-level failure |
#117202 (external, merged 2026-08-31 11:05:36Z), merged master @ 5fa021ee83ba187e91d986f9159254e856de7530 |
Integration tests (amd_asan_ubsan, db disk, old analyzer, 5/6) / Container memory budget exceeded (/docker) |
same signature | #117202 (external, merged 2026-08-31 11:05:36Z), merged master @ 5fa021ee83ba187e91d986f9159254e856de7530 |
Integration tests (amd_asan_ubsan, db disk, old analyzer, 6/6) / Container memory budget exceeded (/docker) |
same signature | #117202 (external, merged 2026-08-31 11:05:36Z), merged master @ 5fa021ee83ba187e91d986f9159254e856de7530 |
None of the three can be caused by this PR. Its diff is IfConstantConditionPass.cpp,
IdentifierResolveScope.h, QueryAnalyzer.cpp, Analyzer/Utils.cpp, Analyzer/Utils.h and one
stateless test, all of it analyzer-side group_by_use_nulls key rewriting, which cannot change how
much memory an unrelated integration module's containers use. Mergeable Check reported
Failed: 3, matching this set exactly, and all three failing shards report at job level with an
empty test set.
This head predates the fix: 7fc3004ed25a2dc8aa66428f4f010244dea3046a is not an ancestor of
530aa6db658d, measured by content rather than by commit distance (the symbol GDB_ATTACH_LOCK
that #117202 introduces in tests/integration/integration-tests-entrypoint.sh is absent from this
head and present at the fix). So I merged master in at a39c4b20cb752914374fe84614b0b66ee25d8d4a
to pick it up, and CI is re-running. The merge is a pure master merge: it adds no commit of my own,
the published tree is byte-identical to the tree I merged locally, its first parent is the
pre-merge head, and no contrib/ submodule pin moved.
CH Inc sync was success at this head, so nothing here is exempt.
Reports:
- https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=117241&sha=530aa6db658d7bc66b50474d2cc000ae9ec4cc0c&name_0=PR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20old%20analyzer%2C%202%2F6%29
- https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=117241&sha=530aa6db658d7bc66b50474d2cc000ae9ec4cc0c&name_0=PR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20old%20analyzer%2C%205%2F6%29
- https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=117241&sha=530aa6db658d7bc66b50474d2cc000ae9ec4cc0c&name_0=PR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20old%20analyzer%2C%206%2F6%29
Session id: cron:our-pr-ci-monitor:20260831-173000
CI finish ledger - 5fa021eEvery failure below has an owner: a fixing PR, or a full-effort fix task whose fixing-PR link will be
None of the rows above is caused by this pull request. On the Session id: |
|
Superseded by #122512, which reworks this fix, so this PR can be closed without merging. Applied to a recent master, its change makes |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
Bad cast from type DB::ColumnNullable to DB::ColumnVector<char8_t>when aGROUP BYkey is aniformultiIfwith a constant condition, the query usesROLLUP,CUBEorGROUPING SETSwithgroup_by_use_nulls = 1, and a correlated subquery references the column that key collapses to.Description
Reported in #100173 (comment) from
Stress test (amd_tsan): CI report, STID 1499-5613. Unrelated to it.Reproducer. Fails on 26.7 and 26.8 with
Code: 49, and aborts the server in debug and sanitizer builds:nullable_group_by_keysis filled from theGROUP BYkeys as resolved, butIfConstantConditionPassruns afterwards and collapsesif(1, c0, false)toc0, makingc0the real grouping key. It collapses the key and only the key, whose node is notNullable-wrapped while its projection copies are. The correlated reference toc0then matches no registered key, keeps its non-NullableBooltype, and its placeholder is bound by name to the actualNullable(Bool)header column.FunctionCast, built forBool, hands aColumnNullableto the strictcheckAndGetColumn<ColumnUInt8>increateUInt8ToBoolWrapper.The fix registers each key's collapsed shape as a key too, via a predicate extracted from
IfConstantConditionPassand shared with it, so prediction and behaviour cannot diverge. One shape is added per key, matching the pass, which visits a node position once then descends into the replacement's children:if(1, if(1, c0, false), false)collapses to the innerif, not toc0. A constant branch is not registered: it can never be a correlated reference, and registering it would make a same-valued projection literalNullable.GROUP BY c0 WITH ROLLUPwith the same projection already worked, and the reproducer now returns its values.UInt8,UInt64andStringkeys already worked and are asserted unchanged.Out of scope, distinct carriers of the same symptom:
LogicalExpressionOptimizerPass, soGROUP BY c0 AND (u < 300)withu UInt8still fails;MultiIfToIfPass, #103419; the matcher andgroupingpaths, #104350.Related: #106377, #100173
Workflow [PR]
Sync PR [sync-upstream/pr/117241]