Skip to content

Fix AMBIGUOUS_COLUMN_NAME in a Buffer PREWHERE with several conditions on a converted column - #124632

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/prewhere-steps-shadowed-input-name
Oct 9, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/prewhere-steps-shadowed-input-name

Conversation

@groeneai

@groeneai groeneai commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #124581
Related: #113900

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

A master-only regression from #113900 (in no release). A Buffer whose column type differs from its destination's forwards PREWHERE and row policies behind a converting prefix, _CAST(v, '<Buffer type>') AS v. Since #113900 the selected v is the destination's own column again, while the conditions still read the conversion, so the forwarded expression has an input and a computed node that are both named v.

tryBuildPrewhereSteps passes columns between read steps by name. With two conditions on v (PREWHERE v > 1 AND v < 5), step 0 outputs both nodes as v, and building it throws AMBIGUOUS_COLUMN_NAME on MergeTree, Memory and Parquet destinations. When v is not selected, the Parquet reader binds the second step's v to the stored column instead and returns wrong rows. 26.8.18.2 and 26.9.9.28 return the right rows for the MergeTree and Parquet cases.

tryBuildPrewhereSteps now returns false when ActionsDAG::hasInputNameShadowedByComputedNode() holds, so the readers evaluate such a PREWHERE in one step, which the function documents as equivalent. Lazy materialization already declines its name-based rewrite for this shape. The PREWHERE of ordinary queries (WHERE, explicit PREWHERE, views, subqueries, CAST(x, ...) AS x) has no such node, so it is still split.

Test 05352_buffer_converted_column_prewhere_steps covers MergeTree (retyped by ALTER, and narrowed), Memory, Parquet PREWHERE and row policy, and a Merge destination, plus controls. On master eight of its queries fail with AMBIGUOUS_COLUMN_NAME and the Parquet query that does not select v returns wrong rows; with the change all return the expected rows. It and the four tests of #113900 pass 50 randomized runs each.

🤖 Generated with Claude Code


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

Version info

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

…s name

A Buffer whose column type differs from its destination forwards PREWHERE and
row policies with a converting prefix, `_CAST(v, 'Buffer type') AS v`, in
front of the filter. Since ClickHouse#113900 the pass-through output `v` is the
destination-typed input again, while the conditions still read the conversion,
so the forwarded DAG has two reachable nodes named `v` with different types.

`tryBuildPrewhereSteps` hands columns from one step to the next by name. With
two conditions on `v`, step 0 then exports both the conversion (for step 1)
and the input (for the final output) as `v`, and building its actions throws
`AMBIGUOUS_COLUMN_NAME`. When `v` is not selected, the Parquet reader instead
binds the later step's `v` to the stored column and returns wrong rows, for a
row policy as well as for a PREWHERE.

Return `false` from `tryBuildPrewhereSteps` when
`ActionsDAG::hasInputNameShadowedByComputedNode` holds, so the MergeTree,
Memory and Parquet readers evaluate such a PREWHERE in one step, which by the
function's own contract gives the same result. Lazy materialization already
declines its name-based rewrite for the same shape. Ordinary queries do not
produce it (their computed nodes are `__tableN.*` or expression-named), so
their PREWHERE is still split.

Closes ClickHouse#124581

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-issue PR origin: picked up an external GitHub issue 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) plus my own cold review (3 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test asserts rows only, so it stays green if PREWHERE stops being split at all, or if the row policy stops reaching the destination read DISAGREE Without the fix all 8 W queries fail with AMBIGUOUS_COLUMN_NAME, W7's from Parquet::Reader::preparePrewhere -> tryBuildPrewhereSteps. A guard firing on ordinary queries breaks 03143_prewhere_profile_events (expects 10052503 rows read by PREWHERE readers, a single step reads 10000000).
2 💡 No arm combines the row policy with v not selected noted, not blocking Both shapes go through the same add_step call, covered by W7 and the Parquet half of C1.
3 💡 The Parquet half of C1 is a witness (wrong rows on master), not a control noted, not blocking none

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

Session id: cron:clickhouse-review-slot-9:20261008-130122

@clickhouse-gh

clickhouse-gh Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9a77373]

Summary: ✅


AI Review

Summary

This PR makes tryBuildPrewhereSteps fall back to single-step evaluation when a PREWHERE DAG contains a computed node that reuses an input name, which matches how split PREWHERE steps pass intermediate values by result_name. The fix is consistent with the existing ActionsDAG/ExpressionActions name-resolution model, and the new stateless test covers the reproduced MergeTree, Memory, Parquet, row-policy, Merge, and non-projected-column cases described in the PR. I did not identify any remaining correctness issue in the current diff or any new finding worth posting as an inline review comment.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 9a77373.

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

Changed lines: Changed C/C++ lines covered: 5/6 (83.33%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 8, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9a7737316 with master c9d4edb56 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline.

Job report

@PedroTadim PedroTadim self-assigned this Oct 9, 2026

/// Steps pass columns to each other by name, which cannot tell an input from a computed node
/// that reuses its name (e.g. a `Buffer` converting its destination's column).
if (prewhere_info->prewhere_actions.hasInputNameShadowedByComputedNode())

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 won't this create performance regressions in other places?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

No, I measured it on a build with temporary counters (not part of this PR):

  • The check is true only when the PREWHERE DAG has a computed node named like one of its inputs. Over the parallel stateless suite (15,540 tests, 550 skipped because they need S3, Kafka or other external services), tryBuildPrewhereSteps ran 24,850 times and the check fired for 16 queries, all of them PREWHERE through a Buffer whose destination declares the column with another type (00910_buffer_prewhere_different_types, 04652_nested_merge_prewhere_type_mismatch, 04743_buffer_two_filters_converted_column_parquet, 05352). 6 have a single condition, which is never split anyway. Of the 10 with several conditions (all in 05352), 9 fail with AMBIGUOUS_COLUMN_NAME or return wrong rows on master; the remaining one (SELECT k ... PREWHERE v > 1 AND v < 5) now runs in one step, and with both conditions on the same column the split saved no read there.
  • Cost: it runs where the read is set up (per stream and per part), never per block. 0.77 µs per call against 24.7 µs for the whole tryBuildPrewhereSteps (debug build).
  • The performance comparison of this PR has 0 slower queries out of 3,875 (39 of them from PREWHERE tests); the 3 unstable ones are iceberg_suite_s3_rw_features, optimize_trivial_group_by_limit and least_greatest_hits.

@PedroTadim
PedroTadim added this pull request to the merge queue Oct 9, 2026
Merged via the queue into ClickHouse:master with commit 14061c6 Oct 9, 2026
152 of 153 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 9, 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. groeneai-origin-issue PR origin: picked up an external GitHub issue 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.

Buffer PREWHERE with AND on a retyped column fails: AMBIGUOUS_COLUMN_NAME

3 participants