Skip to content

Do not consult the query condition cache under a masking policy - #130148

Merged
azat merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/qcc-consult-masking-policy-gate
Nov 8, 2026
Merged

azat merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/qcc-consult-masking-policy-gate

Conversation

@groeneai

@groeneai groeneai commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

Related: https://github.com/ClickHouse/clickhouse-private/pull/83905

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Description

A masking policy is applied to a MergeTree read as an on-the-fly UPDATE that getAlterConversionsForPart adds. It is not in the mutations snapshot and does not change the condition hash, so filterPartsByQueryConditionCache let a masked read reuse entries that an unmasked read of the same predicate recorded over the raw values. The masked read could then skip granules that do match the masked values and return fewer rows.

The consult now declines when the table has an enabled masking policy for the current user, with the hasEnabledMaskingPolicies check the statistics-pruning gate in this file already uses. Writes need no change: a masked read of a masked column has on-the-fly mutation steps, which appliesMutationsBeforePrewhere already keeps from writing entries.

Outside Cloud builds hasEnabledMaskingPolicies is always false, so there is no behavior change there; the query condition cache tests pass. The behavioral test is in the private repository, as requested in the review there. No related open issue found.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.1670-master (included in 26.11 and later)
  • Backported to: 26.10.12.11

A masking policy reaches a MergeTree read as a synthetic on-the-fly UPDATE that
MergeTreeData::getAlterConversionsForPart appends to the part's AlterConversions.
It is not part of the mutations snapshot, and it does not change the filter DAG
the condition hash is derived from. So the consult gate in
filterPartsByQueryConditionCache (which declines only on snapshot state) passes,
and a masked read of predicate P reuses entries an unmasked read of P recorded
over the raw column values. Those entries can mark granules as non-matching that
do match the masked values, so the masked read silently drops rows; in the other
direction, the number of granules skipped leaks where raw values match.

Decline the consult when the table has an enabled masking policy for the current
user, with the same hasEnabledMaskingPolicies idiom the statistics-pruning gate in
this file already uses. The write side needs no change: a masked read of a masked
column carries on-the-fly mutation steps, so MergeTreeReadTask::
appliesMutationsBeforePrewhere already keeps it from writing entries (the
mechanism 03229_query_condition_cache_on_fly_mutations covers), and a read that
touches no masked column sees raw values equal to masked ones.

Outside the Cloud build hasEnabledMaskingPolicies always returns false, so public
behaviour is unchanged. The behavioural test lives in the private repository,
where masking policies can be created.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding; 1 full pass), plus one note from my own review.

# Sev Finding Verdict Evidence / action
1 ⚠️ The behavioral test is in the private repository, so the no-consult behavior is not shown by a test in this PR DISAGREE The new conjunct is part of the early return, which comes before every query_condition_cache->read and before the QueryConditionCacheHits / QueryConditionCacheMisses increments, so a read under a masking policy neither consults the cache nor counts a lookup. A public test cannot create a masking policy; the test stays in the private PR, as requested in its review.
2 💡 The check is per table: a policy on one column also disables the cache for predicates on unmasked columns noted Same per-table hasEnabledMaskingPolicies check as the statistics-pruning gate in this file; it only has an effect in Cloud builds.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261108-131400

@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Not possible in a public build: CREATE MASKING POLICY throws SUPPORT_IS_DISABLED. The deterministic repro is the behavioral test in the private PR, kept there as requested in its review.
b Root cause explained? The masking UPDATE added by getAlterConversionsForPart is outside the mutations snapshot and the condition hash, so masked and unmasked reads of one predicate share entries, and the consult gate checks only snapshot state.
c Fix matches root cause? Yes: the consult declines exactly when the values a read filters can differ from those an entry was computed on (an enabled policy on this table). Keying entries by the policy set was not done: both sides would need the same salt, and a wrong policy-set identity fails open.
d Test intent preserved / new tests added? No public test is possible (as in #126866); the test is in the private PR. Existing query condition cache tests unchanged.
e Both directions demonstrated? Public: the path is unreachable, so I showed it is unchanged: the query condition cache tests give the same result with and without the change (48/49, the one failure needs an S3 policy on this host), and a warm read still records QueryConditionCacheHits = 1.
f Fix is general across code paths? All MergeTree consults (plain, skip-index, TopK-salted, TopK-reuse keys) are below this early return, which has one caller. Writers already skip masked reads of masked columns (appliesMutationsBeforePrewhere). Non-MergeTree caches have no masking path.
g Fix generalizes across inputs (params/datatypes/wrappers)? The gate is per table and user, independent of column types, wrappers, predicate shape, PREWHERE/WHERE, TopK or sampling. Projection parts use the parent table's storage. Empty part lists are guarded.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, format or key change; public builds unchanged; masked reads lose cache pruning only on tables with a policy.
i Invariants and contracts preserved? The conjunct joins the existing early return, so parts are untouched on decline as before; front() only after !empty(); all parts of one read share one storage; the check is const and takes no locks.

Session id: cron:clickhouse-impl-slot-4:20261108-124800

@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7a63718]

Summary: ✅


AI Review

Summary

The change prevents MergeTree reads with an enabled masking policy from consulting the query condition cache, so entries computed from unmasked values cannot discard matching masked rows. Reviewed PR head 7a63718c49f3e1d7d38f2083b7b80bbacfbc2c8d; no actionable findings.

Missing context / blind spots
  • ⚠️ The author places the behavioral regression test in the linked private PR, which these credentials cannot access (HTTP 404). Public builds reject CREATE MASKING POLICY, so the changed branch could not be exercised locally. Inspecting that test and its Cloud CI results would close this validation gap; a duplicate public test is not requested.
Final Verdict

✅ Approve — no code changes requested.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Nov 8, 2026
@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author

cc @azat @kcmannem, could you review this and add the can be tested label so CI runs? The query condition cache consult now returns early when the table has an enabled masking policy (the same hasEnabledMaskingPolicies check the statistics-pruning gate uses), because the policy's read-time UPDATE is in neither the mutations snapshot nor the condition hash; @azat, this is the public half you asked to be assigned to, and since I cannot assign, this cc stands in for it.

@azat azat self-assigned this Nov 8, 2026
@azat azat added the can be tested Allows running workflows for external contributors label Nov 8, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Nov 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 7a63718c4 with master 7b5f2bf32 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 4eada96ac; 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.

Compile time of recompiled translation units

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

Job report

@azat
azat enabled auto-merge November 8, 2026 17:24
@azat
azat disabled auto-merge November 8, 2026 17:25
@azat
azat added this pull request to the merge queue Nov 8, 2026
Merged via the queue into ClickHouse:master with commit 1b80547 Nov 8, 2026
374 of 375 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 8, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 20, 2026
robot-clickhouse added a commit that referenced this pull request Nov 24, 2026
Cherry pick #130148 to 26.10: Do not consult the query condition cache under a masking policy
robot-clickhouse added a commit that referenced this pull request Nov 24, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 24, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 24, 2026
Backport #130148 to 26.10: Do not consult the query condition cache under a masking policy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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 v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants