Repository navigation
Fix AMBIGUOUS_COLUMN_NAME in a Buffer PREWHERE with several conditions on a converted column - #124632
Conversation
…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]>
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).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261008-130122 |
|
Workflow [PR], commit [9a77373] Summary: ✅
AI ReviewSummaryThis PR makes Final Verdict
LLVM Coverage ReportMeasured on commit 9a77373.
Changed lines: Changed C/C++ lines covered: 5/6 (83.33%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline. |
|
|
||
| /// 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()) |
There was a problem hiding this comment.
@groeneai won't this create performance regressions in other places?
There was a problem hiding this comment.
No, I measured it on a build with temporary counters (not part of this PR):
- The check is
trueonly 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),tryBuildPrewhereStepsran 24,850 times and the check fired for 16 queries, all of them PREWHERE through aBufferwhose 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 in05352), 9 fail withAMBIGUOUS_COLUMN_NAMEor 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_limitandleast_greatest_hits.
14061c6
Closes: #124581
Related: #113900
Changelog category (leave one):
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
Bufferwhose 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 selectedvis 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 namedv.tryBuildPrewhereStepspasses columns between read steps by name. With two conditions onv(PREWHERE v > 1 AND v < 5), step 0 outputs both nodes asv, and building it throwsAMBIGUOUS_COLUMN_NAMEon MergeTree, Memory and Parquet destinations. Whenvis not selected, the Parquet reader binds the second step'svto 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.tryBuildPrewhereStepsnow returnsfalsewhenActionsDAG::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_stepscovers MergeTree (retyped byALTER, and narrowed), Memory, Parquet PREWHERE and row policy, and a Merge destination, plus controls. On master eight of its queries fail withAMBIGUOUS_COLUMN_NAMEand the Parquet query that does not selectvreturns 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
26.10.1.1953-master(included in26.10and later)