Repository navigation
Apply a Merge child's row policy once, and reject it on a child that processes the query itself - #125765
Conversation
…processes the query `ReadFromMerge::convertAndFilterSourceStream` added every child's row policy as a `FilterStep` on top of the child plan, whatever built that plan. When a `Merge` table also reads a `Distributed` table its stage is above `FetchColumns`, so `createPlanForTable` builds a local child (e.g. MergeTree) with `InterpreterSelectQueryAnalyzer`, whose planner already applies the child's policy below its own aggregation. The `Merge` then applied it a second time over the processed header, and every query failed with `NOT_FOUND_COLUMN_IN_BLOCK`. The child plan now records that the interpreter built it, and the `Merge` skips its own copy. A child that reports a stage above `FetchColumns` itself (`Distributed`, and tables over one) sorts, limits, aggregates and computes extremes before the `Merge` sees a row, so a filter above it was applied too late: `ORDER BY ... LIMIT` returned no rows, `extremes = 1` printed values of rows the policy hides or threw `Got 1 in extremes chunk, expected 2`, and aggregates failed with `NOT_FOUND_COLUMN_IN_BLOCK`. Such a child is now rejected with `ILLEGAL_PREWHERE`, the rule a direct read of that table follows since ClickHouse#112326. Related: ClickHouse#125580 Related: ClickHouse#112326 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rejected The CREATE ROW POLICY documentation described the policy of a Merge child that reads remotely as applied above the child's read, which this branch replaces with an ILLEGAL_PREWHERE error for the users the policy applies to. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the comment above the Merge child row-policy rejection to one line; the cross-reference to the direct-read rule lives in the previous commit message. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (REVIEW lane): 1 finding AGREEd and fixed, 1 noted, 0 open
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261014-094500 |
|
Workflow [PR], commit [a92563f] Summary: ✅
AI ReviewSummaryThis fixes duplicate child row-policy filtering in Missing context / blind spots
Final VerdictStatus: ✅ Approve. No required code changes identified. |
|
cc @yariks5s @PedroTadim, could you review this? A |
There was a problem hiding this comment.
Ok. Technically, it is a backward-incompatible change. But a workaround is possible - set up the row policy on the remote and child tables instead of the Merge table. This is a natural way to handle row policies on Merge tables, so I expect no problems.
There was a problem hiding this comment.
Agreed. I changed the changelog category to Backward Incompatible Change and added this workaround to the changelog entry.
55f3778
… 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]>
Related: #125580
Related: #112326
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A row policy on a
Distributedtable (or another table that processes the query itself) read through aMergetable now makes queries to theMergetable fail withILLEGAL_PREWHEREfor the users the policy applies to, like queries to that table itself. Before, theMergetable applied such a policy after the table had already sorted, limited and computedextremes, so queries could return wrong rows and show values of hidden rows inextremes. Define the row policy on the underlying tables of the remote servers and on the local child tables instead. This also fixesNOT_FOUND_COLUMN_IN_BLOCKon every query over aMergetable whose child has a row policy when theMergetable also reads aDistributedtable.Description
On 26.8.21.12, every query on a
Mergetable over a MergeTree table with a row policy and aDistributedtable fails withNOT_FOUND_COLUMN_IN_BLOCK; when the row policy is on theDistributedchild,ORDER BY a LIMIT 2returns no rows andextremes = 1prints values of rows the policy hides:Selecting only
athrowsGot 1 in extremes chunk, expected 2(#125580).Root cause.
ReadFromMergeadded each child's row policy as a filter on top of the child's plan, whatever built it. With aDistributedsibling, a local child is planned by the interpreter, which already applies its policy; the second copy runs over a header without the policy's columns. A child that processes the query itself (Distributed, tables over one) sorts, limits, aggregates and computes extremes before that filter.Change. A child plan built by the interpreter records that it applies its own policy, and the
Mergeskips its filter. A child with a row policy whose own processing stage is aboveFetchColumnsis rejected withILLEGAL_PREWHEREfor every query, as a direct read is since #112326; theCREATE ROW POLICYdocumentation says so.Validation. The new test fails on master on every line and passes with the fix, also with
serialize_query_plan = 1, parallel replicas and aReplicateddatabase. No otherMergeor row-policy stateless test changes its result.Workflow [PR]
Sync PR [sync-upstream/pr/125765]
Version info
26.10.1.2417-master(included in26.10and later)