Repository navigation
Apply the row policy of a Merge table's local underlying table before PREWHERE and WHERE - #128013
Conversation
… PREWHERE and WHERE `ReadFromMerge` applied the row policy of a local child as a `FilterStep` above the child's read. The query's PREWHERE is forwarded into that read, and the query's WHERE is pushed below the Merge, merged with the policy filter by the child plan's optimization and moved into the read's PREWHERE as one unordered conjunction. Either way the query's condition was evaluated on the rows the policy hides: a condition that throws on a value (`toUInt64(payload) > 10`, `throwIf`) printed hidden values and works as an oracle over hidden rows. A direct read of the same table is not affected, because the planner gives the read the policy as `SelectQueryInfo::row_level_filter`, which the reads evaluate as a separate step before every PREWHERE step. ClickHouse#125765 fixed the children that process the query themselves; a local child read at `FetchColumns` still got the late filter. Now the child's read gets the policy as its row-level filter whenever the planner's own push-down rule says the storage can evaluate it there, and the late `FilterStep` is not planted: - The rule (`supportsPrewhere`, not remote, every column inside the PREWHERE contract) was written out twice, in the planner and in the old interpreter. It is now `canPushDownRowPolicyFilter`, used by both and by `Merge`. - `RowPolicyData::makeRowLevelFilter` returns the policy as a `FilterDAGInfo`, or nothing when the rule fails or a column is not physical. When the Merge table has a policy of its own (forwarded to every child as `row_level_filter`), both are AND-ed into one filter that keeps every input column, as ClickHouse#102860 proposes; this also removes the `NOT_FOUND_COLUMN_IN_BLOCK` of ClickHouse#102650. - The policy's IN-subquery sets are still built by a step above the read (`addCreatingSetsStep`); a Buffer child's in-memory read needs it. - `ALIAS` columns in the policy are expanded before analysis, since a read cannot produce them. When the read cannot evaluate the policy (e.g. a `Memory` child's policy over a `DEFAULT` column) the previous path is kept. A direct read of such a table keeps the policy as a WHERE filter and orders it the same way; that is the planner's behaviour and is not changed here. Behaviour changes, both matching a direct read: a Merge child's plan shows a `Row level filter` instead of a `Filter` step, and `apply_row_policy_after_final = 0` now applies a Merge child's policy before FINAL. Test: 05453_merge_child_row_policy_before_prewhere, eight arms (MergeTree PREWHERE and WHERE, Memory, FINAL, IN subquery over Buffer, ALIAS, policies on both tables, nested Merge) plus a control showing the condition throws on the hidden value while it is visible. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…arded one `RowPolicyData::makeRowLevelFilter` found the child policy's filter node by name after `ActionsDAG::merge`. The forwarded row-level filter can itself be another `RowPolicyData`'s filter: for a `Merge` over a `Merge` with policies on the inner `Merge` and on the leaf table, both are named `__row_policy_filter`. `merge` lists the forwarded outputs first, so the lookup returned the forwarded node and the filter became `and(F, F)`. The leaf policy was dropped and, as the read was marked as applying the policy, the late filter was skipped too, so rows hidden by the leaf policy were returned. The child policy's filter is now aliased under a name that does not occur among the forwarded DAG's inputs and outputs before the merge. Test: a new arm of 05453 (nested `Merge`, policies at both levels; rows and PREWHERE). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…r_final` The FINAL arm of 05453 had one row per key, so FINAL deduplicated nothing and could not show whether a child's policy ran before or after FINAL. The child's policy is now the read's row-level filter, so the default "after FINAL" comes from `ReadFromMergeTree::isRowPolicyDeferredAfterFinal`. New arm: a versioned `ReplacingMergeTree` with merges stopped and two parts for one key, the newer version hidden by the policy. Through the `Merge` table, `apply_row_policy_after_final = 1` returns 0 and `= 0` returns 1, as a direct read of the table does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…REWHERE contract stays above the read `canPushDownRowPolicyFilter` refuses a policy either when the storage has no PREWHERE support or when a policy column is outside its `supportedPrewhereColumns`. Only the first refusal was tested through `Merge`. New arm: a `Memory` child whose policy reads a `DEFAULT` column, which `StorageMemory::supportedPrewhereColumns` excludes. Rows cannot tell a pushed-down policy from a late one here, so the arm checks the plan: the child read of the plain `Memory` arm has a row-level filter, the `DEFAULT` arm's read has none. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…king `RowPolicyData` expanded `ALIAS` columns in the child's policy unconditionally. The expansion is analysed over the table's ordinary columns only, so an `ALIAS` such as `tv ALIAS concat(_table, ':', tenant)` made every query on the `Merge` fail with `Missing columns: '_table'`. Expand `ALIAS` columns with the virtual columns visible to the expansion, and keep the expansion only if the result reads ordinary columns. Otherwise the policy is analysed over the `ALIAS` column as before, and is applied above the read. A policy over a virtual column itself is still rejected, as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the new comments in StorageMerge and ColumnsDescription to their invariants; drop one that restated a function name. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review: my own cold review plus an independent model (engine: codex; 8 findings in total).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261027-224300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261027-222600 |
|
cc @tavplubix @pufit, could you review this and add the |
|
Workflow [PR], commit [333c437] Summary: ❌
AI ReviewSummaryThis PR moves a child table's row policy for a Final Verdict
|
`replaceAliasColumnsInQuery` expands only identifiers that name a declared column, so a child policy such as `USING a.t = 'a'` with `a` an `ALIAS` column kept reading `a.t`. The read cannot produce that, so the policy stayed a filter above the read, and the query's `PREWHERE` still ran on the hidden rows first. Rewrite such a subcolumn into `getSubcolumn(a, 't')` before the `ALIAS` expansion, as the analyzer resolves it, including inside lambda bodies. A subquery, and an identifier whose name or any dotted prefix of it is a lambda argument, are left as they are, since they do not name this table's columns; such a policy keeps the late filter, as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 333c437Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Both Session id: cron:our-pr-ci-monitor:20261028-200004 |
fm4v
left a comment
There was a problem hiding this comment.
Verified with the CI binary of 333c437 against master a018bd2bbaa6:
- The reproducer from the description, plus
throwIf(payload = 'secret')inWHERE,FINALwithapply_row_policy_after_final = 0, anIN (SELECT ...)child policy combined with a policy on theMergetable, and aMergeTree+Logpair of children: master throws on the hidden rows in every case, this PR returns the policy-filtered counts. - #102650, #125912 and #125027 reproduce on master and are fixed here, so I added them as
Closes:.
The only red check is Build profile diff, an infrastructure failure (LogClusterNotReady).
cce2803
Backport #128013 to 26.10: Apply the row policy of a `Merge` table's local underlying table before `PREWHERE` and `WHERE`
…is not on this branch On this branch a `merge` child's row policy is applied above the child's read, so with a `Distributed` sibling raising the stage it fails with NOT_FOUND_COLUMN_IN_BLOCK even without a view. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…is not on this branch On this branch a `merge` child's row policy is applied above the child's read, so with a `Distributed` sibling raising the stage it fails with NOT_FOUND_COLUMN_IN_BLOCK even without a view. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…is not on this branch On this branch a `merge` child's row policy is applied above the child's read, so with a `Distributed` sibling raising the stage it fails with NOT_FOUND_COLUMN_IN_BLOCK even without a view. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…is not on this branch On this branch a `merge` child's row policy is applied above the child's read, so with a `Distributed` sibling raising the stage it fails with NOT_FOUND_COLUMN_IN_BLOCK even without a view. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #102650
Closes: #125912
Closes: #125027
Related: #125765
Related: #102860
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a
Mergetable evaluating the query'sPREWHEREandWHEREon rows that a row policy of a local underlying table hides. A condition that throws on a value, such astoUInt64(column) > 10, printed values of the hidden rows. The policy is now applied by the underlying table's read beforePREWHERE, as for a direct read of that table.Description
On 26.8.16.41, with a policy
USING tenant = 'a'on a MergeTree tabletandm = Merge(currentDatabase(), '^t$'),SELECT count() FROM m PREWHERE toUInt64(payload) > 10(and theWHEREform) fails withCannot parse string 'secret', a value of a hidden row, while the same query ontreturns 2. WiththrowIfit answers any question about hidden rows.Root cause:
ReadFromMergeapplied a child's policy as aFilterStepabove the child's read. The forwardedPREWHEREruns inside that read, and a pushed-downWHEREis merged with the policy filter intoPREWHERE. #125765 covered only children that run the query themselves.Change: the child's read gets the policy as its row-level filter, evaluated before
PREWHERE, whenever the planner's push-down rule allows it (now one helper for the planner,InterpreterSelectQueryandMerge). A policy on theMergeitself is combined with the child's into one filter, as #102860 does; this also removes theNOT_FOUND_COLUMN_IN_BLOCKof #102650.ALIAScolumns and their subcolumns in a policy are expanded first. Withapply_row_policy_after_final = 0a child's policy now runs beforeFINAL, as for a direct read. Two policies keep the late filter, as before: one the read cannot evaluate (aMemoryDEFAULTcolumn, late on a direct read too) and one over anALIASof a virtual column (_table).Validation: a new test of thirteen arms (MergeTree,
Memory,MemoryDEFAULT,FINALunder both settings,INoverBuffer,ALIASover a column, its subcolumn and_table, a lambda argument named like it, policies on both tables, nestedMergewith one and two policy levels) fails on master in all but the two late-filter and the lambda-argument arms, and passes here and in 50 randomized runs,DBReplicatedand plan-based parallel replicas. 175 row-policy andMergetests: no new failures.Workflow [PR]
Sync PR [sync-upstream/pr/128013]
Version info
26.11.1.1287-master(included in26.11and later)