Skip to content

Fix NOT_FOUND_COLUMN_IN_BLOCK when an alias of a nested single-element tuple comparison is reused by GROUP BY - #124546

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/comparison-tuple-elimination-nested-single-element
Oct 11, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/comparison-tuple-elimination-nested-single-element

Conversation

@groeneai

@groeneai groeneai commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #122404
Related: #108553

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed NOT_FOUND_COLUMN_IN_BLOCK when a comparison of single-element tuples whose elements are tuples, such as tuple((a, b)) = tuple((c, d)), is part of an alias used in GROUP BY and also in ORDER BY, HAVING or WHERE. Such comparisons are now rewritten completely, so WHERE tuple(tuple(x)) = tuple(tuple(y)) over a join no longer returns rows where x and y are both NULL, the same as WHERE tuple(x) = tuple(y).

Description

On 26.8.2.7, SELECT NOT (tuple(tuple(n, 1)) = tuple(tuple(n, 2))) AS r FROM t GROUP BY r ORDER BY r fails with Code: 10 NOT_FOUND_COLUMN_IN_BLOCK on default settings (also on 26.6, 26.9 and master; 1 with enable_identifier_resolve_cache = 0).

ComparisonTupleEliminationPass replaces tuple(x) = tuple(y) by x = y, which is again a tuple comparison when x is a tuple, and the visitor never re-enters a replacement, so one visit removes one nesting level. With the identifier resolve cache (default since 26.6) the GROUP BY key and its ORDER BY / HAVING / WHERE references are one node visited once per reference, while the projection copy is visited once, so the two end up with different names.

The change: re-enter the replacement, so one visit reaches the fixed point and the result does not depend on node sharing.

Side effect: l CROSS JOIN r WHERE tuple(tuple(l.key)) = tuple(tuple(r.key)) over Nullable keys returned the (NULL, NULL) pair (2 rows, the leftover tuple equality became a join key) and now returns 1 row, like tuple(l.key) = tuple(r.key) does. JOIN ON is still not rewritten (#67493).

The new test covers 11 queries (=/!=, a constant on either side, HAVING, WHERE, 3 levels, a nested tuple inside a 2-element tuple, Nullable and LowCardinality columns, WITH ROLLUP, remote(), the join): all fail on master and pass with the fix, 50/50 with random settings. A differential run of 3164 existing tests changed no result.

The same issue in other passes is fixed separately:
Related: #123925
Related: #124480
Related: #124482


Workflow [PR]
Sync PR [sync-upstream/pr/124546]

Version info

  • Merged into: 26.10.1.2090-master (included in 26.10 and later)

groeneai and others added 2 commits October 7, 2026 23:21
`ComparisonTupleEliminationPass` replaces `tuple(x) = tuple(y)` by `x = y`, which
is again a tuple comparison when `x` is a tuple, and the visitor never re-enters a
replacement root, so one visit removed one nesting level. With the identifier
resolve cache the GROUP BY key and its ORDER BY / HAVING / WHERE references are one
node visited once per reference while the projection keeps its own copy, so their
rewrites diverged and the query failed with NOT_FOUND_COLUMN_IN_BLOCK. Re-entering
the replacement makes one visit reach the fixed point.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…omparison

Replace the EXPLAIN check with result checks: a WHERE equality of nested
single-element tuples over a CROSS JOIN is now rewritten to `l.key = r.key`,
so the (NULL, NULL) pair is no longer matched, the same as for
`tuple(l.key) = tuple(r.key)`; and the shared alias also works over `remote()`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-self PR origin: self-found defect (impact-bar validated) labels Oct 8, 2026
@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding in round 1, 0 in round 2) plus my own cold review (2 nits).

# Sev Finding Verdict Evidence / action
1 💡 Related issues and PRs were only inside the template's HTML comment, not as visible full-URL Related: lines AGREE, fixed in the PR body visible Related: lines added; round 2 on the same head: 0 findings
2 💡 The re-entry also runs after a multi-element replacement AGREE, no change its root is and/not, so enterImpl returns at the function-name check
3 💡 The shared-alias queries alone do not separate converging from declining the nested rewrite AGREE, no change the join query does (declining keeps 2), so it stays in the test

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-self-slot-58:20261008-031900

@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The test's first query fails on master with Code: 10 NOT_FOUND_COLUMN_IN_BLOCK every time (20/20 randomized runner runs, and each query alone through clickhouse local).
b Root cause explained? Yes. A single-element tuple comparison is replaced by the comparison of its elements, which can be a tuple comparison again, and the replacement is never re-entered, so one visit removes one level. With the identifier resolve cache the GROUP BY key is one node visited once per reference, the projection copy once, so their names differ.
c Fix matches root cause? Yes. The pass re-enters its own replacement, so one visit reaches the fixed point and the rewrite no longer depends on how many times a shared node is visited. No guard, no setting, no copy at the cache.
d Test intent preserved / new tests added? Yes. New test 05350_comparison_tuple_elimination_shared_alias with 11 queries; all fail on master.
e Both directions demonstrated? Yes. Master binary: 10 queries Code: 10, the join query returns 2. Fixed binary: reference byte-exact, cache on and off, runner OK, 50/50 random settings plus 50/50 without. Removing the re-entry reproduces master; declining the nested rewrite instead fixes the error but fails the join query.
f Fix is general across code paths? Yes. Both single-element paths (tuple vs tuple, tuple vs constant in either order) go through the same re-entry; multi-element paths already converged; the JOIN ON and table-function exclusions are unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. =/!=, constant on either side, ORDER BY/HAVING/WHERE references, 3 levels, nested inside a multi-element tuple, Nullable, LowCardinality, WITH ROLLUP, remote(); group_by_use_nulls and NULL elements checked by probes against the cache-off result.
h Backward compatible? (maintainer-approved exception only) Yes, no setting or format. One result change: a nested single-element tuple equality in WHERE that becomes a join key no longer matches (NULL, NULL), the same as the single-level spelling. 3164 existing tests: no changed result.
i Invariants and contracts preserved? Yes. After the replacement nothing reads the replaced node; recursion works on strictly smaller subtrees and stops at the first non-rewritable node; result types are re-checked on every re-entered node.

Session id: cron:clickhouse-impl-self-slot-57:20261008-024300

@clickhouse-gh

clickhouse-gh Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8de8557]

Summary: ✅


AI Review

Summary

This PR makes ComparisonTupleEliminationPass re-enter its own replacement so nested single-element tuple comparisons reach a fixed point in one visit, and it adds stateless coverage for the shared-alias regression, remote(), and the NULL-join behavior change described in the PR. I did not find any blocker or major issue in the current code or diff; the fix matches the stated root cause, the touched surface is narrow, and the PR CI is green.

Final Verdict

✅ Looks good to merge.

LLVM Coverage Report

Measured on commit 8de8557.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 4/4 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 8, 2026
@groeneai
groeneai requested review from novikd and vdimir October 8, 2026 03:53
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Oct 8, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 10, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 10, 2026
Merged via the queue into ClickHouse:master with commit 7ecb788 Oct 11, 2026
152 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). groeneai-origin-self PR origin: self-found defect (impact-bar validated) pr-bugfix Pull request with bugfix, not backported by default 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.

3 participants