Skip to content

Add a test for PREWHERE steps computing two columns with one name - #127016

Merged
alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:fix-prewhere-steps-duplicate-computed-name
Nov 8, 2026
Merged

alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:fix-prewhere-steps-duplicate-computed-name

Conversation

@groeneai

@groeneai groeneai commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #128452
Related: #111955
Related: #122160

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Description

This PR only adds a regression test.

SELECT t1.id, t2.reviewer FROM t1 LEFT JOIN t2 ON t1.id = t2.id AND t2.enabled = true AND t2.reviewer > 0 WHERE t2.enabled = true AND t2.reviewer = 100 SETTINGS join_use_nulls = 1, allow_reorder_prewhere_conditions = 0 fails on 26.10.2.103 and 26.8.21.12 with AMBIGUOUS_COLUMN_NAME (Block structure mismatch). With join_use_nulls = 1 the WHERE copy of the repeated ON condition is computed on toNullable(col) under the same name, and a PREWHERE split into steps kept the first copy in the block when it computed the second one. CI hit it in 04650_join_use_nulls_repeated_on_condition_in_where on #122160 (report).

Master no longer fails since #128452, which removes the filter column of an intermediate PREWHERE step when nothing later reads it. 05427_prewhere_steps_duplicate_computed_name covers both ON orders, a Memory table, a step with two conditions, and a default expression evaluated after the steps. It passes on master 5b24a5f (also 100 randomized runs and with parallel replicas) and fails on 26.10.2.103 in 4 of its 5 queries.

An earlier version of this PR also changed tryBuildPrewhereSteps; that change is dropped.


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

Version info

  • Merged into: 26.11.1.1650-master (included in 26.11 and later)

groeneai and others added 3 commits October 20, 2026 17:11
…h one name

With join_use_nulls = 1, the WHERE copy of a JOIN ON condition repeated in
WHERE is computed on toNullable(col) but keeps the name of the ON copy, so
the PREWHERE DAG holds two different nodes with one name and two types
(e.g. equals(__table2.enabled, 1_Bool) as Nullable(UInt8) and UInt8).
tryBuildPrewhereSteps clones every node into its step under its own name,
the steps pass columns to each other through a Block by name, and a
non-last step keeps its filter column in the block. When the two copies
fall into non-adjacent, non-last steps, the later step inserts a column of
the same name and another type and Block::insert throws 352. Two repeated
two-condition groups collide the same way through the generated `and` name.

Clone the non-INPUT nodes whose name is shared with another node, and that
are not outputs of PREWHERE, under new names (`__prewhere_<name>`). Only the
step clones are renamed: the original DAG, and so its conjunct hashes used by
the query condition cache attribution, are unchanged, and the step
boundaries stay where they are, so an earlier condition still filters the
rows a later default expression is evaluated on (a one-step fallback would
throw there). The helper has the same shape as the one in ClickHouse#125670, which
renames for a computed node named like a stored column.

Same class as ClickHouse#111955, fixed for the FilterStep split by ClickHouse#112007.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The new names given to the step clones of nodes that share a name were made
unique only against the names of the PREWHERE DAG. Stored columns read after
PREWHERE are not in that DAG but land in the same block, and the readers skip
a stored column whose name a step already produced (`getReadTaskColumns` for
MergeTree, `read_missing_columns` for Memory). So a table with a column named
like a new name, e.g. `__prewhere_equals(__table2.enabled, 1_Bool)`, returned
the step's predicate value instead of the stored value.

Also skip a candidate name that is a physical column or subcolumn of the
table, with the same `is_stored_column` helper as ClickHouse#125670.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A step with two or more conditions gets a generated `and` filter named
after its children. With the renamed children that name is new, and a
non-last step keeps the filter in the block, so a stored column with the
same name is not read and the query returns the filter value instead.
Alias such a filter to a fresh name, as ClickHouse#125670 does, and share its two
name helpers so the two PRs fold mechanically.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

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

Pre-publication review by an independent model (engine: codex; 3 findings over 3 rounds) plus my own review (2 nits).

# Sev Finding Verdict Evidence / action
1 ❌ A new __prewhere_<name> could be the name of a stored column read after PREWHERE, and the readers skip such a column, so the query returned the predicate value instead of the stored one (MergeTree and Memory) AGREE, fixed @ 37924c2 the new names also avoid stored column names; test covers both engines
2 ❌ The same for the combined filter of a step with two conditions, whose name is built from the renamed conditions AGREE, fixed @ 7352bd3 such a filter is renamed when a stored column has its name (the guard #125670 uses); test covers both engines
3 ⚠️ The stored-name test cases would also pass if those cases fell back to a single step DISAGREE a single step returns the correct value there, so passing it is right; the code has no such fallback, and the cost of one (a default expression evaluated before its guard) is covered by the guarded-default case, which a one-step fallback turns into CANNOT_PARSE_TEXT
4 💡 #125670 adds the same rename helper in this function AGREE, not blocking whichever PR merges second keeps one helper

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-8:20261020-182700

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The query from the description with join_use_nulls = 1, allow_reorder_prewhere_conditions = 0 fails every time on master (MergeTree and Memory) and on 26.8.21.12 (26.3.33.24 with the ON conditions swapped). The new test fails 30/30 randomized runs on master.
b Root cause explained? The WHERE copy of a repeated ON condition is computed on toNullable(col) under the ON copy's name, so the PREWHERE DAG has two nodes with one name and two types. The split steps exchange columns by name and a non-last step keeps its filter column, so a later step inserts a same-named column of another type into the block.
c Fix matches root cause? Yes. The step clones of nodes that share a name get distinct names, so the by-name transport is unambiguous. No fallback to one step, no pinned setting.
d Test intent preserved / new tests added? New test 05427_prewhere_steps_duplicate_computed_name: both ON orders, Memory, two-condition groups, a guarded default expression, and stored columns named like a step's column or filter (MergeTree and Memory). No existing test changed.
e Both directions demonstrated? Master: arms 1, 3, 4, 5 fail with AMBIGUOUS_COLUMN_NAME; fix: all pass, 50/50 randomized. An empty rename map, a one-step fallback and erasing finished singleton filters each turn their own arm red.
f Fix is general across code paths? One function serves MergeTree, Memory and Parquet; MergeTree and Memory are fixed, Parquet measured unchanged. The FilterStep split of this class was fixed by #112007.
g Fix generalizes across inputs (params/datatypes/wrappers)? Name-based, so independent of the type: Bool vs Nullable measured, LowCardinality(String) does not reach the defect on master. Single conditions and condition groups both covered; parallel replicas (local plan 0 and 1) pass.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or protocol change. Names change only for nodes that share a name and for a step's combined filter named like a stored column.
i Invariants and contracts preserved? Every column a step adds to the block has a unique name, also against the stored columns read after PREWHERE. PREWHERE outputs keep their names, the original DAG and its conjunct hashes are untouched, and step boundaries are unchanged, so the earlier filter still runs before a later default expression.

Session id: cron:clickhouse-impl-slot-7:20261020-163900

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CurtizJ @Avogar, could you review this and add the can be tested label so CI runs? With join_use_nulls = 1 a repeated ON condition in WHERE gives the PREWHERE DAG two nodes of different types with one name, and since the split read steps pass columns by name a later step failed with AMBIGUOUS_COLUMN_NAME; the step clones of such nodes now get unique names that no stored column has.

@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e88fc6c]

Summary: ✅


AI Review

Summary

PR #127016 at e88fc6c4ae3d adds regression coverage for repeated JOIN ON/WHERE predicates with join_use_nulls and multiple PREWHERE read steps, including MergeTree, Memory, grouped conditions, and deferred default evaluation. No actionable findings. The identical SQL and reference files passed 50 targeted ASan/UBSan runs and the parallel replicas test at 6f98a2c7d072; the head commit's broader CI rerun is still in progress.

Final Verdict

✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Oct 20, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 21, 2026

@antaljanosbenjamin antaljanosbenjamin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@groeneai don't you have to revert the change you introduced in 8514664 ?

@antaljanosbenjamin antaljanosbenjamin self-assigned this Oct 21, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

I'd keep it. The pin is not only there to avoid the 352: since #116248 a WHERE over a Memory table is moved to PREWHERE. On master (927e545) without the pin, all 12 Memory result cases of 04650 plan their conditions as the Prewhere filter column of ReadFromMemoryStorage, and 10 of them have no Filter step with an AND chain left, so they never reach the split that #112007 fixed and this test was written for. With the pin, all 12 go through that Filter step. CI draws optimize_move_to_prewhere = 0 in only 5% of runs, so without the pin that coverage is almost always lost.

The PREWHERE side of the same collision is tested here by 05427, with allow_reorder_prewhere_conditions = 0 pinned so it does not depend on a random draw.

If you would still like 04650 to run with PREWHERE too, I'll revert the pin in this PR after #122160 is merged (it is not on master yet).

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too bad.

@alexey-milovidov alexey-milovidov self-assigned this Nov 7, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 7, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 7, 2026
@groeneai

groeneai commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator Author

This no longer reproduces on master. Since #128452 (merged 2026-11-01) an intermediate PREWHERE step drops its condition result when no later step uses it, so the ON copy is gone before the WHERE copy with the same name is computed. 05427 passes on master 7a516a1 (9 of 9 queries) and fails on 26.10.2.103 (8 of 9 with Code 352). The PR also no longer compiles against master, because #124553 removed columns from tryBuildPrewhereSteps; that is why the merge queue dropped it. The renaming here would only matter if a later step still needed the earlier copy, and I could not reach that from SQL.

@alexey-milovidov, should I reduce this PR to the 05427 test only (no src/ change), or close it?

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, yes - let it be test-only.

groeneai and others added 2 commits November 7, 2026 20:44
Master no longer reproduces the AMBIGUOUS_COLUMN_NAME: since ClickHouse#128452 an
intermediate PREWHERE step removes its filter column when nothing later uses it,
so the ON copy is gone before the WHERE copy with the same name is computed.
The renaming in `tryBuildPrewhereSteps` is therefore dropped, and the file is
restored to its version at the base of this branch (which also stops the PR
from conflicting with ClickHouse#124553, which removed `columns` from that function).

`05427` stays as a regression test: it passes on master 5b24a5f and fails
on 26.10.2.103 with Code 352 in 8 of its 9 queries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
They checked that the dropped renaming did not pick the name of a stored
column. Without the renaming nothing generates `__prewhere_` names, so they no
longer test anything the other cases do not.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai changed the title Fix AMBIGUOUS_COLUMN_NAME when a PREWHERE split into steps computes two different columns with one name Add a test for PREWHERE steps computing two columns with one name Nov 7, 2026
@groeneai

groeneai commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator Author

Done: the PR now only adds 05427. The src/ change is reverted in 5ddae44, and 6f98a2c drops the test's four stored-column queries, which only tested the removed renaming. The test passes on master 5b24a5f and fails on 26.10.2.103 in 4 of its 5 queries. Title, description and category (Not for changelog) are updated.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-testing Test content only (no production code). and removed pr-bugfix Pull request with bugfix, not backported by default comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 7, 2026
@groeneai

groeneai commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 6f98a2c

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 00673_subquery_prepared_set_performance flaky, not from this PR (it only adds the test 05427_prewhere_steps_duplicate_computed_name): both measured queries returned their row, only the system.query_log check max(read_rows) < 2000000 AND count() >= 2 returned 0. The runner's 201 reruns with the same settings passed. The merged #126681 is in this tree and covers a different failure of this test a fix task is moved to pending (investigating at full effort, fixing-PR link to follow here)
Mergeable Check, PR praktika rollups of the row above a fix task is moved to pending (the same one as above)

The three NOT_FAILED rows of that job are tests that passed while listed in the parallel replicas blacklist; they do not fail the job.

Session id: cron:our-pr-ci-monitor:20261107-233003

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 8, 2026
@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author

Fixing PR for 00673_subquery_prepared_set_performance created: #130153

Merged via the queue into ClickHouse:master with commit 570a0da Nov 8, 2026
187 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 8, 2026
@clickhouse-gh clickhouse-gh Bot added comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. and removed comp-testing Test content only (no production code). labels Nov 22, 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-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-not-for-changelog This PR should not be mentioned in the changelog 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