Skip to content

Apply a row policy on a Memory table to a read shipped as a serialized query plan - #122503

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-row-policy-memory-serialized-plan
Sep 27, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-row-policy-memory-serialized-plan

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

Caused by: #116248
Related: #114955
Related: #112891

Since #116248 Memory supports PREWHERE, so the planner pushes a row policy on a Memory table into SelectQueryInfo::row_level_filter. A logical plan (a serialized query plan or a parallel replicas plan) reads through a placeholder ReadFromTableStep, which does not carry that filter, so the node executing a serialized plan rebuilt the read without the policy and returned the rows it excludes. Before #116248 the policy was an explicit filter step of the shipped plan.

This keeps the policy as an explicit filter step of a logical plan for storages whose PREWHERE support depends on the columns (a non-nullopt supportedPrewhereColumns: Memory, Buffer, File, ...), ahead of the PREWHERE filter step, so user conditions never see the excluded rows. The MergeTree family (a nullopt contract, where apply_row_policy_after_final and per-node policy resolution matter) is left as is: #114955 handles it by re-planning on the executing node, and it excludes exactly the storages handled here, so the two do not apply the policy twice.

This fixes the master failures of 03812_row_policy_after_changing_initial_user_roles and 03812_row_policy_after_replacing_initial_user in Stateless tests (amd_asan_ubsan, distributed plan, parallel) with prefer_localhost_replica = 0, which started at the merge of #116248:

The new test 05259_row_policy_serialized_query_plan_memory fails on the master binary and passes with the change.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


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

Version info

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

…zed query plan

Since #116248 `Memory` supports PREWHERE, so the planner pushes a row policy into
`SelectQueryInfo::row_level_filter`. A logical plan reads through a placeholder
`ReadFromTableStep`, which does not carry that filter, so the node executing a serialized
plan rebuilt the read without the policy and returned the rows it excludes. Before #116248
the policy was an explicit filter step of the plan. Keep it one in a logical plan for storages
whose PREWHERE support depends on the columns, ahead of the PREWHERE filter step.

The `MergeTree` family (a `nullopt` PREWHERE contract) is left as is; it is handled by
#114955 by re-planning on the executing node.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=e7aebccc74b28ac3f7829f11dc1ffdd8ff49b83f&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%29
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=beb4d3dad37bf154e5b710f2b5c57ff3725f8b48&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%29
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=199f114be9fc953c781745db345deffbc2c0031a&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%29

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ae46270]

Summary: ✅


AI Review

Summary

This PR keeps row policies on column-constrained PREWHERE storages as explicit plan filters when building a logical plan, which fixes the Memory/serialized-plan leak described in the PR. I found one blocker in the generalization of that rule: the new gate also catches Merge, whose worker-side resolveStorages path already re-plans the read and re-applies row policies, so the serialized filter is evaluated twice there.

Findings

❌ Blockers

  • src/Planner/PlannerJoinTree.cpp:2065 supportedPrewhereColumns().has_value() is broader than the affected storage family. StorageMerge also returns a concrete PREWHERE column set, but serialized Merge reads are rebuilt on the worker through InterpreterSelectQueryAnalyzer in src/Processors/QueryPlan/resolveStorages.cpp and StorageMerge then re-applies row policies to its children in src/Storages/StorageMerge.cpp. Adding the row policy as an explicit serialized FilterStep here therefore evaluates it twice for Merge under serialize_query_plan = 1 / logical-plan execution. Row-policy expressions are not required to be deterministic (src/Access/RowPolicy.cpp only bans arrayJoin), so this is a correctness bug, not just extra work: USING rand() % 2 = 0 would drop rows twice. Narrow the condition to storages whose worker path does not already re-plan row policies, and add a regression for logical-plan reads through ENGINE = Merge.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit ae46270.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.50% 79.60% +0.10%

Changed lines: Changed C/C++ lines covered: 4/4 (100.00%) · 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 Sep 27, 2026
@alexey-milovidov alexey-milovidov added the blocker This issue / pr blocks a new release label Sep 27, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Sep 27, 2026
/// that node cannot tell whether the policy was pushed down, so keep the policy as an
/// explicit filter step of the plan, ahead of the PREWHERE filter step, so that the
/// user's conditions never see the excluded rows.
if (select_query_options.build_logical_plan && storage->supportedPrewhereColumns().has_value())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

StorageMerge::supportedPrewhereColumns() returns a concrete set, so this branch also fires for Merge tables. That breaks the “handle it here” assumption for serialized plans: on the worker, resolveStorages rebuilds a Merge read by running InterpreterSelectQueryAnalyzer again (resolveStorages.cpp:211-259), and StorageMerge then reapplies row policies to its children (StorageMerge.cpp:1290-1299). The explicit FilterStep added here would therefore evaluate the same policy a second time on top of the rebuilt read. That is observable because row-policy expressions are not required to be deterministic (RowPolicy.cpp only rejects arrayJoin): USING rand() % 2 = 0 becomes roughly 25% instead of 50%, and even deterministic IN (subquery) policies now build their sets twice. This needs a narrower gate than supportedPrewhereColumns().has_value() (at least excluding StorageMerge / any other storage whose worker path already re-plans and re-resolves row policies).

@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Sep 27, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 27, 2026
…to the read

The shipped read recorded `FilterStep` only when the planner set `row_policy_filter_not_pushed`,
so a policy that reaches the plan as a filter step on any other path was recorded as `NotInPlan`
and the executing node applied it a second time. ClickHouse#122503 adds such a path: under a logical plan it
ships a `Memory` policy as a filter step without that flag. With its change applied on top of this
branch, the nondeterministic `memory nd` and `memory materialized nd` arms of
04908_row_policy_serialized_query_plan read TWICE instead of ONCE; with this change they read ONCE
and its own 05259 test still passes.

Record `FilterStep` whenever a policy was built for the read and not pushed into it. On this
branch that is the same condition as the flag, so behaviour here does not change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 27, 2026
Merged via the queue into master with commit f3cf0a2 Sep 27, 2026
172 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-row-policy-memory-serialized-plan branch September 27, 2026 20:37
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 27, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 2, 2026
…wo test comments

The remote-replica-first arm arms `slowdown_parallel_replicas_local_plan_read`,
and since 67965ab the style check requires `no-parallel` and `no-fasttest`
on every stateless test that arms a server fail point.

Since ClickHouse#122503, a serialized plan keeps a `Memory` or `Buffer` row policy as a
filter step whenever the storage reports its PREWHERE columns, so the policy is
shipped in the plan rather than applied inside the executing node's read. The
two comments in 04908_row_policy_serialized_query_plan now say so; the asserted
values do not change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Bartok9 pushed a commit to Bartok9/ClickHouse that referenced this pull request Oct 3, 2026
…alized query plan

`StorageFromMergeTreeProjection` supports PREWHERE and keeps the default
(nullopt) `supportedPrewhereColumns()` contract, so the planner pushed a
`_table_function.mergeTreeProjection` row policy into
`SelectQueryInfo::row_level_filter`. Under a logical plan the read is a
placeholder `ReadFromTableFunctionStep`, which serializes only the table
function AST and FINAL/SAMPLE flags, and the executing server reads this
storage directly (`readRequiresAnalyzedQuery()` is false), so with
`serialize_query_plan = 1` a `cluster()` read returned the rows the policy
excludes (reproduces on 26.8.10.6 and master).

Keep such a policy as the explicit filter step at the front of
`where_filters` under `build_logical_plan`, the placement ClickHouse#122503 uses for
storages with a column-set PREWHERE contract. `mergeTreeProjection` is the
only table function storage that reached the pushed branch (remote and
non-PREWHERE table functions already take the `else` filter step). The
shipped step needs no receiver change, so an older executing server is
fixed too; no setting, protocol or step version changes.

The receiver re-plan hook (`readRequiresAnalyzedQuery() = true`) was
rejected: a correct re-plan there would apply `additional_table_filters`
twice and check `max_columns_to_read` on the widened header, which needs
root-read exemptions that do not exist on master.

Related: ClickHouse#112891

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). 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.

2 participants