Repository navigation
Fix logical error on Merge table with a bare-column row policy - #109843
alexey-milovidov merged 10 commits into
Conversation
A row policy whose filter is a bare existing column (e.g. USING flag) aborted a SELECT over a Merge table with "Cannot determine row level filter; 0 columns deleted, 0 columns added". ReadFromMerge::RowPolicyData derived the filter column as "filter actions output columns minus input columns", assuming the filter produces exactly one new column. For a bare-column policy the result column coincides with an input column, so the diff was empty and the assertion fired. Simply reusing that input column as the filter column is also wrong: the filter column is removed from the stream after filtering, which would drop the data column and hit "Duplicate column name ... in row policy actions output". Wrap the filter result in a uniquely-named alias and use that as the removable filter column, so the data columns always pass through. This matches the non-Merge row-policy path, where the filter column is a dedicated result column. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260708-203500 |
|
cc @azat @KochetovNicolai — could you review this? It fixes a logical error ("Cannot determine row level filter; 0 columns deleted, 0 columns added", AST-fuzzer-found on arm_msan stress) when a Merge table's child has a row policy whose filter is a bare existing column: RowPolicyData now aliases the filter result to a dedicated removable column instead of assuming the filter always adds a new column. |
|
Workflow [PR], commit [fea2b1f] Summary: ✅
AI ReviewSummaryThis PR fixes Final VerdictStatus: ✅ Approve |
The generated __row_policy_filter alias was only checked against the current DAG outputs, not the child table's real columns. A source table with a real __row_policy_filter column of a different type would flow into the same block as the alias on SELECT *, so Block::insert threw on the duplicate name (AMBIGUOUS_COLUMN_NAME / block structure mismatch). Now the name is also made unique against the child storage columns. Regression test extended with a real __row_policy_filter String column. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Fixed in 13eb792. The helper name is now made unique against the child table's real columns too, not just the current DAG outputs: NameSet reserved_names;
for (const auto & column : needed_columns)
reserved_names.insert(column.name);
filter_column_name = "__row_policy_filter";
for (size_t i = 0; actions_dag.tryFindInOutputs(filter_column_name) != nullptr || reserved_names.contains(filter_column_name); ++i)
filter_column_name = "__row_policy_filter_" + std::to_string(i);Confirmed both directions on a debug build:
Added a stateless case for this in |
The row-policy filter for a Merge child wrapped the policy predicate in a __row_policy_filter alias and appended it to the actions DAG outputs. For an expression policy such as USING a > 1 that left the raw predicate output (greater(a, 1)) in the outputs next to the alias. FilterStep removes only the alias, so the synthetic predicate column stayed in the stream and correctness relied on later unused-column pruning. With query_plan_enable_optimizations = 0 that pruning is skipped: a single table leaked the predicate column, and a Merge over children with different policies could fail in Pipe::unitePipes on mismatched headers. Rebuild the post-filter outputs as the source (input) columns plus the alias, dropping the raw predicate output regardless of plan optimizations. Added an EXPLAIN-header regression with query_plan_enable_optimizations = 0. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Fixed in 4086b39. The post-filter outputs are now rebuilt as the source (input) columns plus the Regression added to SELECT count() FROM (EXPLAIN header = 1 SELECT * FROM 03500_m3 SETTINGS query_plan_enable_optimizations = 0) WHERE explain ILIKE '%greater(a, 1)%';Returns 2 before the fix (predicate leaked into the child stream) and 0 after. |
CI finish ledger — 4086b39Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260709-043000 |
|
Fixing PR for AST fuzzer STID 2945-38b2 (Column n UInt64 query tree node does not have valid source node) created: #109863 — a trunk analyzer bug (correlated recursive CTE with allow_experimental_correlated_subqueries=1), unrelated to this PR's Merge row-policy change. |
…row-policy-bare-column
A query-level column alias matching the helper name (SELECT flag AS __row_policy_filter) is applied in the outer plan above the Merge child read and never enters the child stream the row-policy filter operates on, so it cannot clash with the generated helper alias. The helper name is also already reserved against storage ALIAS columns via getAll(). Test confirms no AMBIGUOUS_COLUMN_NAME, correct rows, with and without plan optimizations, single and multiple children. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
This scenario does not reproduce on HEAD. Verified Added a regression in |
CI finish ledger — 8e41051Every failure below has an owner. Only
Session id: cron:our-pr-ci-monitor:20260714-093000 |
…row-policy-bare-column
CI finish ledger — 31bfb2cEvery failure below has an owner. Only
Everything else is green on this run: |
…row-policy-bare-column
A child table can have a real quoted column whose name equals the row policy predicate's column name, e.g. `greater(a, 1)` for USING a > 1. The row-policy helper alias binds to the computed predicate node, not to that user column, because the row-policy actions DAG's inputs are only the columns the predicate references (a). The user column is not a node in that DAG, so it cannot shadow the predicate function in findInOutputs, and the Merge filters on a > 1 rather than on the user column. No silent wrong result and no AMBIGUOUS_COLUMN_NAME. Values are chosen so a mis-bind would return a different set (a = 0, 1) than the correct a > 1 (a = 2, 3), for both a UInt8 (would silently mis-filter) and a String (would throw on the filter type) collision column. Test-only: the reported defect does not reproduce on HEAD. Co-Authored-By: Claude Opus 4.8 <[email protected]>
CI finish ledger — e7cbbd1Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. This PR's diff is
Session id: cron:our-pr-ci-monitor:20260722-130000 |
…row-policy-bare-column
CI finish ledger — 36009c0Every failure below has an owner. Both are unrelated to this PR's change.
Otherwise the PR is unchanged and merge-ready: approved by @alexey-milovidov, AI Review ✅ Approve, 0 unresolved threads, Bugfix validation OK on both arches, |
CI finish ledger - 36009c0Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Fast test, Style check and all Bugfix-validation jobs are green on this head. Session id: cron:our-pr-ci-monitor:20260726-163000 |
|
Fixing PR for the The failure is the plain-rewritable |
…row-policy-bare-column
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 17/17 (100.00%) · Uncovered code |
|
🕵 This fix never reached any active release branch, and the row-policy logical error it fixes is still reachable on all five. I reproduced it on the shipped release binaries ( CREATE TABLE child1 (a UInt64, flag UInt8) ENGINE = MergeTree ORDER BY a;
INSERT INTO child1 VALUES (1,1),(2,0),(3,1);
CREATE TABLE merge_tbl (a UInt64, flag UInt8) ENGINE = Merge(currentDatabase(), '^child');
CREATE ROW POLICY p_bare ON child1 USING flag TO ALL;
SELECT * FROM merge_tbl ORDER BY a;26.7, 26.6, 26.5 and 26.3 all give I simulated the port the way @alexey-milovidov, could you apply One caveat: I verified the ports merge cleanly and contain the whole fix, but I did not build any release branch, so compilation is still for the backport PRs' own CI to confirm. |
Related: #102860
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a logical error ("Cannot determine row level filter; 0 columns deleted, 0 columns added") when reading from a
Mergetable whose child table has a row policy whose filter is a bare existing column (for exampleCREATE ROW POLICY ... USING flag).Description
Found by the AST fuzzer during
Stress test (arm_msan)on master (STID 4390-4e70). Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=c1edd5f0ae54c332c83dd22bba93ee06f5e9409a&name_0=MasterCI&name_1=Stress%20test%20%28arm_msan%29ReadFromMerge::RowPolicyDatacomputed the row-policy filter column as "filter actions output columns minus input columns", assuming the filter always adds exactly one new column. When the policy expression is a bare existing column, the filter result column coincides with an input column, so that diff is empty and the assertion fires. Naively reusing the input column as the filter column is also wrong, because the filter column is removed from the stream after filtering, which would drop the data column and trigger "Duplicate column name ... in row policy actions output".The fix wraps the filter result in a uniquely-named alias and uses that alias as the removable filter column, so the data columns always pass through. This matches the non-Merge row-policy path, where the filter column is a dedicated result column. Verified that the Merge output now matches the direct-table row-policy semantics for bare-column, expression, and compound policies.