Skip to content

Fix logical error in tuple comparison with group_by_use_nulls and ROLLUP/CUBE - #96426

Merged
alexey-milovidov merged 2 commits into
masterfrom
fix-tuple-comparison-group-by-use-nulls
Feb 10, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
fix-tuple-comparison-group-by-use-nulls

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 9, 2026 •

Copy link
Copy Markdown
Member

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
#96419

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Version info

  • Merged into: 26.2.1.501

…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]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [59edff3]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Feb 9, 2026
@alexey-milovidov
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
alexey-milovidov marked this pull request as ready for review February 9, 2026 08:37
@alexey-milovidov alexey-milovidov self-assigned this Feb 10, 2026
@alexey-milovidov
alexey-milovidov merged commit 70b2493 into master Feb 10, 2026
259 of 260 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-tuple-comparison-group-by-use-nulls branch February 10, 2026 02:27
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 10, 2026
@KochetovNicolai KochetovNicolai added the post-approved Approved, but after the PR is merged. label Mar 13, 2026
@clickgapai

Copy link
Copy Markdown
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

post-approved Approved, but after the PR is merged. pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants