Repository navigation
Fix logical error in tuple comparison with group_by_use_nulls and ROLLUP/CUBE - #96426
Merged
alexey-milovidov merged 2 commits intoFeb 10, 2026
Merged
Conversation
…P/CUBE The `ComparisonTupleEliminationPass` optimizes single-element tuple comparisons like `tuple(2) = tuple(materialize(1))` into scalar comparisons `equals(2, materialize(1))`. With `group_by_use_nulls`, the projection's copy of the expression has `wrap_with_nullable` set, making its result type `Nullable(UInt8)` while the candidate's type is `UInt8`. The type mismatch caused the pass to skip optimization for the projection but not for the GROUP BY key, creating a name mismatch. The projection then recomputed the tuple comparison with an incorrect `Nullable(UInt8)` result type, triggering a logical error: "Unexpected return type from `equals`". The fix propagates the Nullable wrapper to the optimized candidate when the only type difference is the Nullable wrapping. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=96419&sha=b7ee113a859d63ecd96f17cab2ab7f79ae9c0145&name_0=PR&name_1=AST%20fuzzer%20%28amd_ubsan%29 Co-Authored-By: Claude Opus 4.6 <[email protected]>
Contributor
alexey-milovidov
marked this pull request as draft
February 9, 2026 08:00
…ionPass` Use `makeNullableSafe` instead of `makeNullable` to avoid throwing when the candidate result type is `LowCardinality`, which cannot be inside `Nullable`. This gracefully skips the optimization for such types instead of crashing. Co-Authored-By: Claude Opus 4.6 <[email protected]>
alexey-milovidov
marked this pull request as ready for review
February 9, 2026 08:37
Contributor
|
Hi @alexey-milovidov — while reviewing this PR I found the following:
Happy to discuss — close anything that's wrong or already addressed. |
groeneai
added a commit
to groeneai/ClickHouse
that referenced
this pull request
Sep 27, 2026
…tiIfPass
With `group_by_use_nulls`, a clause that reads a ROLLUP/CUBE/GROUPING SETS
key after the aggregation reads a copy of the key that is made Nullable.
IfChainToMultiIfPass compared the type of the `multiIf` with that Nullable
type, so it rewrote an `if` chain in the key but not in the copy. The copy
then has another name than the key and is computed from the columns it
reads instead of being read from the aggregation: the query fails with
NOT_FOUND_COLUMN_IN_BLOCK, or, when those columns are other keys, returns
the value of the chain where the key is rolled up:
SELECT a, b, if(a = 'x', a, if(b = 'y', b, a)) AS k, count()
FROM (SELECT 'x' AS a, 'w' AS b) GROUP BY ROLLUP(a, b, k) ORDER BY ALL
SETTINGS group_by_use_nulls = 1, optimize_if_chain_to_multiif = 1
returned `x w x` twice and `x \N x` instead of `x w \N` and `x \N \N`
(26.3 to 26.9 and master). The pass now compares with the type of the
function itself and keeps the copy Nullable, as ComparisonTupleEliminationPass
does since ClickHouse#96426.
The ROLLUP/CUBE/GROUPING SETS skip in IfConstantConditionPass leaves such a
chain in place when folding it would merge two keys, so without this
`ROLLUP(a, if(0, b, if(0, b, a)))` failed with NOT_FOUND_COLUMN_IN_BLOCK
under optimize_if_chain_to_multiif = 1, where master returns rows.
This is the `if` chain query of ClickHouse#103419; its `multiIf` query
(MultiIfToIfPass) is not changed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai
added a commit
to groeneai/ClickHouse
that referenced
this pull request
Sep 30, 2026
…ePerfectAffixRewritePass`
With `group_by_use_nulls`, a clause that reads a ROLLUP/CUBE/GROUPING SETS
key after the aggregation reads a copy of the key that is made `Nullable`.
`LikePerfectAffixRewritePass` replaced such a copy of a `LIKE` key with a new
`startsWith`/`endsWith` node that is not `Nullable`, so the copy lost its
`Nullable` type in the query tree. Debug and sanitizer builds abort:
SELECT s LIKE '1%' AS k, count() FROM (SELECT toString(number) AS s FROM numbers(3))
GROUP BY k WITH ROLLUP SETTINGS group_by_use_nulls = 1
fails the pass's `Rewrite should preserve type` assertion, and for a
`LowCardinality` column a function that reads the copy fails the query tree
validation after the pass (`expects argument 1 to have
LowCardinality(Nullable(UInt8)) type but receives LowCardinality(UInt8)`).
Release builds return the right rows: the planner reads the copy from the
aggregation output by name.
The pass now makes the new node `Nullable` when the original node is, as
`ComparisonTupleEliminationPass` does since ClickHouse#96426.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
grantholly-clickhouse
pushed a commit
to grantholly-clickhouse/ClickHouse
that referenced
this pull request
Oct 10, 2026
…ToIfPass`
With `group_by_use_nulls`, a clause that reads a `ROLLUP`/`CUBE`/`GROUPING SETS`
key after the aggregation reads a copy of the key that is made `Nullable`.
`MultiIfToIfPass` compared the type of the new `if` with that `Nullable` type,
so it rewrote a three-argument `multiIf` key but not its copy. The copy then
has another name than the key and is computed again from its arguments: the
query fails with `NOT_FOUND_COLUMN_IN_BLOCK`, or, when those arguments are
other keys, returns their value where the key is rolled up:
SELECT number, multiIf(number > 0, number, 0) AS k, count()
FROM numbers(3) GROUP BY ROLLUP(number, k) ORDER BY ALL
SETTINGS group_by_use_nulls = 1
returned `0 0` twice and `\N 0` instead of `0 \N` and `\N \N` (26.3 to
26.9 and master). The pass now compares with the type of the function
itself; the node is rewritten in place, so the copy stays `Nullable`, as
`ComparisonTupleEliminationPass` does since ClickHouse#96426. ClickHouse#122512 does the same
for `IfChainToMultiIfPass`, the `if` chain query of ClickHouse#103419.
Part of ClickHouse#103419.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
CurtizJ
pushed a commit
to CurtizJ/ClickHouse
that referenced
this pull request
Oct 14, 2026
With `group_by_use_nulls` and ROLLUP/CUBE/GROUPING SETS, the analyzer
replaces an expression equal to a GROUP BY key with a copy of the key made
Nullable. `IfConstantConditionPass` folded a constant-condition key to its
branch but never such a copy, whose type is the Nullable form of the branch
type, so the copy no longer matched the key by name and was computed from
the aggregation output, which lacks the other branch's columns:
SELECT if(0, b, a) AS k, count() FROM t GROUP BY k WITH ROLLUP
SETTINGS group_by_use_nulls = 1
failed with NOT_FOUND_COLUMN_IN_BLOCK. With the bottom-up fold this also
broke keys such as `if(0, b, if(0, b, a))`, which the top-down fold left
matching the copy.
The pass now folds such a copy to its branch made Nullable, as
`ComparisonTupleEliminationPass` does (ClickHouse#96426). Constant branches are left
as they were.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
ComparisonTupleEliminationPassoptimizes single-element tuple comparisons liketuple(2) = tuple(materialize(1))into scalar comparisonsequals(2, materialize(1)). Withgroup_by_use_nulls, the projection's copy of the expression haswrap_with_nullableset, making its result typeNullable(UInt8)while the candidate's type isUInt8. The type mismatch caused the pass to skip optimization for the projection but not for the GROUP BY key, creating a name mismatch. The projection then recomputed the tuple comparison with an incorrectNullable(UInt8)result type, triggering a logical error: "Unexpected return type fromequals".The fix propagates the Nullable wrapper to the optimized candidate when the only type difference is the Nullable wrapping.
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=96419&sha=b7ee113a859d63ecd96f17cab2ab7f79ae9c0145&name_0=PR&name_1=AST%20fuzzer%20%28amd_ubsan%29
#96419
Changelog category (leave one):
Version info
26.2.1.501