Repository navigation
Do not analyze the shared row policy AST in place in Merge - #113563
Merged
Merged
Conversation
`RowPolicyFilter::expression` is the parsed policy condition owned by `RowPolicyCache`. It is parsed once, in `RowPolicyCache::PolicyInfo::setPolicy`, and then handed out as is: every query of every user reading the table gets the same AST nodes, and a policy defined on a whole database is shared by all of its tables. `FiltersMixer` combines the parsed conditions without copying them, so the pointer a query receives points straight into the cache. `ReadFromMerge::RowPolicyData` passed that AST to `TreeRewriter` and `ExpressionAnalyzer`, both of which rewrite the AST they are given in place: they normalize identifiers, substitute the results of scalar subqueries for the subqueries themselves, and record `ASTLiteral::unique_column_name`. So reading a `Merge` table mutated the cache. Two consequences: - A scalar subquery in a policy was evaluated once and then frozen. With `CREATE ROW POLICY p ON src USING x <= (SELECT max(v) FROM limits)`, one `SELECT` through a `Merge` table over `src` replaced the subquery with its value in the cached AST, and every later query - including direct reads of `src` by other users - kept using that value for the rest of the server's lifetime. - Concurrent queries raced on the shared AST. ThreadSanitizer reported the write to `ASTLiteral::unique_column_name` from `ActionsMatcher::visit` against a concurrent `ASTLiteral::clone` performed by `buildQueryTree` for another table protected by the same policy: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=66467&sha=b48cf3b340cdcc0e2275b3c1a5a4ba34034deb4d&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29 #66467 Analyze a clone instead. `generateFilterActions` in `InterpreterSelectQuery` already clones the policy expression for the same reason, as does `MergeTreeDataSelectExecutor::getSampling` for the sampling key, and `ASTLiteral::clone` resets `unique_column_name` precisely because it is per-analysis scratch state. This was the last consumer of a row policy filter that rewrote the cached AST: the other ones either clone (the analyzer, through `buildQueryTree`), only visit it with a const visitor (`StorageMergeTreeTextIndex`), or only test `isAlwaysTrue`. Co-Authored-By: Claude Fable 5 <[email protected]>
Contributor
|
Workflow [PR], commit [c810f42] Summary: ✅
AI ReviewSummaryThis PR fixes a real Final VerdictStatus: ✅ Approve LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code |
Contributor
Build profile diff (arm_release)No arm_release build profile data for commit c810f42 - the build was skipped, reused from cache, or predates profile upload. |
The test 04812_merge_row_policy_shared_ast failed in the ParallelReplicas run of the stateless tests: the policy condition is analyzed anew on the remote side of a parallel-replicas query, where the session default database is not the test database, so the unqualified table of the scalar subquery did not resolve (`Unknown table expression identifier 't_04812_limit'`, UNKNOWN_TABLE). Convert the test to a shell test to interpolate the database name into the `CREATE ROW POLICY` statement, because a parameterized identifier inside the policy condition would be stored unsubstituted. Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113563&sha=54e05810fa932e7718e3ad266795423362641c66&name_0=PR&name_1=Stateless%20tests%20(amd_llvm_coverage,%20ParallelReplicas,%20s3%20storage,%20parallel) PR: #113563 Co-Authored-By: Claude Fable 5 <[email protected]>
This was referenced Aug 9, 2026
KochetovNicolai
approved these changes
Aug 11, 2026
This was referenced Aug 11, 2026
alexey-milovidov
added a commit
that referenced
this pull request
Aug 12, 2026
Backport #113563 to 26.5: Do not analyze the shared row policy AST in place in `Merge`
alexey-milovidov
added a commit
that referenced
this pull request
Aug 12, 2026
Backport #113563 to 26.3: Do not analyze the shared row policy AST in place in `Merge`
clickhouse-gh Bot
added a commit
that referenced
this pull request
Aug 12, 2026
Backport #113563 to 26.7: Do not analyze the shared row policy AST in place in `Merge`
alexey-milovidov
added a commit
that referenced
this pull request
Aug 12, 2026
Backport #113563 to 25.8: Do not analyze the shared row policy AST in place in `Merge`
alexey-milovidov
added a commit
that referenced
this pull request
Aug 16, 2026
… `Merge` Backport #113563 to 26.6: Do not analyze the shared row policy AST in place in `Merge`
kewin-robetti
pushed a commit
to viasoftkorp/ClickHouse
that referenced
this pull request
Aug 24, 2026
…icy AST in place in `Merge`
kewin-robetti
pushed a commit
to viasoftkorp/ClickHouse
that referenced
this pull request
Aug 24, 2026
…icy AST in place in `Merge`
kewin-robetti
pushed a commit
to viasoftkorp/ClickHouse
that referenced
this pull request
Aug 24, 2026
…icy AST in place in `Merge`
VighneshPath
pushed a commit
to VighneshPath/ClickHouse
that referenced
this pull request
Sep 16, 2026
…icy AST in place in `Merge`
bytehalo-infra
added a commit
to sentioxyz/ClickHouse
that referenced
this pull request
Sep 25, 2026
OTHER: row policy with a scalar subquery, read through Merge -> wrong (stale) plus race. Effect in the production build a394e92: its upstream test(s) 04812_merge_row_policy_shared_ast fail on it and pass on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/server_triage_PROD263_vs_UP2688.tsv). Upstream patch unchanged, with its test(s): 04812_merge_row_policy_shared_ast. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related: #68569
Related: #68321
Related: #66467
Fixes a
ThreadSanitizerdata race found by the stress test:https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=66467&sha=b48cf3b340cdcc0e2275b3c1a5a4ba34034deb4d&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29
The report is unrelated to that pull request (it is about the
Chimpcodec); the race is onmaster. There is no open issue for it. I could not find an existing fix - the closest one, #68321, fixed the same class of bug for the sampling key AST and closed #68569, where the sameconst_castinActionsVisitorwas already questioned.RowPolicyFilter::expressionis the parsed policy condition owned byRowPolicyCache. It is parsed once, inRowPolicyCache::PolicyInfo::setPolicy, and then handed out as is: every query of every user reading the table gets the same AST nodes, and a policy defined on a whole database is shared by all of its tables.FiltersMixercombines the parsed conditions without copying them, so the pointer a query receives points straight into the cache.ReadFromMerge::RowPolicyDatapassed that AST toTreeRewriterandExpressionAnalyzer, both of which rewrite the AST they are given in place - they normalize identifiers, substitute the results of scalar subqueries for the subqueries themselves, and recordASTLiteral::unique_column_name. So reading aMergetable mutated the shared cache. This has two consequences.A scalar subquery inside a policy is evaluated once and then frozen. This is deterministic and does not need any concurrency:
The one
SELECTthroughmreplaced the subquery with the literal3in the cached policy AST, so the policy keeps that value for the rest of the server's lifetime, for every user and for direct reads of the table as well. Because the frozen value can be either more or less permissive than the current one, a policy can end up showing rows it is supposed to hide.Concurrent queries race on the shared AST.
ThreadSanitizerreported the write toASTLiteral::unique_column_namefromActionsMatcher::visit, reached throughReadFromMerge::RowPolicyData, against a concurrentASTLiteral::cloneperformed bybuildQueryTreefor another table protected by the same policy.The fix is to analyze a clone.
generateFilterActionsinInterpreterSelectQueryalready clones the policy expression for the same reason, as doesMergeTreeDataSelectExecutor::getSamplingfor the sampling key, andASTLiteral::cloneresetsunique_column_nameprecisely because it is per-analysis scratch state.ReadFromMerge::RowPolicyDatawas the last consumer of a row policy filter that rewrote the cached AST: the other ones either clone (the analyzer, throughbuildQueryTree), only visit it with a const visitor (StorageMergeTreeTextIndex), or only testisAlwaysTrue. After this change nothing writes to the shared AST - the only remaining mutable member ofIASTis the atomic reference counter.The new test
04812_merge_row_policy_shared_astreproduces the frozen subquery: it prints4 4 4 4before the change and4 4 8 8after.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a row policy containing a scalar subquery being evaluated only once and then reused forever after the table had been read through a
Mergetable. The parsed policy condition is cached and shared by all queries, and reading it throughMergerewrote the cached expression in place, which also caused a data race between concurrent queries using the same policy.Version info
26.7.4.22,26.6.3.43,26.5.7.39,26.3.18.20,25.8.30.17