Skip to content

Keep the row policy in a read shipped as a serialized query plan - #131123

Open
yariks5s wants to merge 9 commits into
masterfrom
yarik/row-policy-in-read-from-table-step
Open

yariks5s wants to merge 9 commits into
masterfrom
yarik/row-policy-in-read-from-table-step

Conversation

@yariks5s

@yariks5s yariks5s commented Nov 12, 2026 •

Copy link
Copy Markdown
Member
description A read that the initiator sends as a serialized query plan (`serialize_query_plan = 1`) lost the row policy of a `MergeTree` table. The planner gives such a policy to the storage as `row_level_filter`, and the `ReadFromTableStep` placeholder did not carry it. The server that executes the plan built the read without the filter and returned all rows.

Example: a policy USING y < 5 on a 10-row table, read through a Distributed table with prefer_localhost_replica = 0. SELECT count() returned 10 with serialize_query_plan = 1 and 5 with serialize_query_plan = 0. With parallel replicas, a policy on the first sorting key column and ORDER BY on the second gave the exception Coordination mode mismatch ...: got Default, expected WithOrder.

The fix: ReadFromTableStep carries the filter (step version 1, written since plan version 21). The server that executes the plan gives the filter to the storage, which applies it in PREWHERE as for a local read, so FINAL and apply_row_policy_after_final work the same in both modes. If the table on that server cannot apply the filter inside the read, the query fails with ILLEGAL_PREWHERE.

The policy in the plan is the policy that the initiator resolves. The executing server does not add its own, so the policy must be the same on all servers. The docs say this now.

This is a small alternative to #114955 and completes the approach of #107745, which failed with THERE_IS_NO_COLUMN because the step header still contained the columns that the filter removes.

Closes: #112891
Related: #114955
Related: #107745

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix a SELECT row policy on a MergeTree table being ignored when the read is sent to another server as a serialized query plan (serialize_query_plan = 1): reads through a Distributed table with prefer_localhost_replica = 0 returned rows that the policy excludes.


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

A read sent as a serialized query plan lost the row policy that the storage applies inside the read. The step now carries the filter (step version 1), and the server that executes the plan gives it to the storage.
@mintlify

mintlify Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
ClickHouse-docs 🟢 Ready View Preview Nov 17, 2026, 5:50 PM

@clickhouse-gh

clickhouse-gh Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6096552]

Summary: ✅


AI Review

Summary

Reviewed head 60965526eb61471652551cc4738eb31499eb20a2. The change preserves the initiator's row policy in ReadFromTableStep, reconstructs the policy's input columns, and gives the logical read its filtered output header. Both previous Major findings are addressed: the obsolete exception hints are removed, and the shipped-plan regression covers a policy containing an IN subquery. The existing sender-trust model is accepted as documented. No outstanding findings. The complete regression passed on the PR's CI binary; ten additional policy cases passed all 40 comparisons covering aliases, subcolumns, constants, LowCardinality, filter ordering, subqueries, and lambdas.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 12, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Nov 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 60965526e with master 75fcef2b7 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 2f06a797f; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

8 object files changed (+31.37 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/QueryPlan/ReadFromTableStep.cpp.o 82.70 KiB 102.81 KiB +20.11 KiB (+24.32%)

711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

131 translation units recompiled, 1153 s compile time in total, 131 of them have a recent master baseline.

Job report

# Conflicts:
#	src/Processors/QueryPlan/ReadFromTableStep.h

This branch was successfully deployed

1 active (outdated) deployment
staging - docs — e87e2128 Deployed Nov 17, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Row policy on the shard-local table is bypassed with serialize_query_plan = 1 on a non-local replica

1 participant