Repository navigation
Cherry pick #107145 to 26.4: Fix query condition cache poisoning by on-fly mutations - #110643
Merged
robot-clickhouse-ci-1 merged 8 commits intoJul 16, 2026
Merged
Conversation
The query condition cache records, per (table, part, predicate hash), which marks match a PREWHERE/WHERE predicate so later queries with the same predicate can skip non-matching marks. On-fly mutations (lightweight UPDATE/DELETE, via apply_mutations_on_fly) and patch parts are spliced into the readers chain ahead of PREWHERE, so they drop or rewrite rows before PREWHERE evaluates them. A mark could therefore become fully non-matching only because of the mutation, not because of the predicate itself, yet it was still attributed to the predicate's hash. This poisoned the cache: a later query that shares the same predicate but does not apply the mutations (apply_mutations_on_fly = 0) hit the cached entry and wrongly skipped those marks, returning too few rows. The effect was masked at large index_granularity (the granule is recorded as "may match" and PREWHERE re-runs per row) and surfaced deterministically at small index_granularity, which is why 03047_on_fly_update_delete was flaky under randomized index_granularity. The read path already bypasses the cache when on-fly mutations or patch parts are active (MergeTreeDataSelectExecutor); this makes the write path symmetric by skipping the cache write/attribution in MergeTreeSelectProcessor whenever the read task applies mutations before PREWHERE, mirroring the existing readersChainCanSkipMarksBeforePrewhere guard for skip indexes (issue #104781). Co-Authored-By: Claude Opus 4.8 <[email protected]>
…mutations The previous commit only gated the PREWHERE attribution sites in MergeTreeSelectProcessor. The WHERE predicate uses a separate cache write path: MergeTreeSelectProcessor attaches a MarkRangesInfo to every data chunk, and the downstream FilterTransform::writeIntoQueryConditionCache records those mark ranges under the WHERE condition hash whenever it fully filters a chunk. With an on-fly UPDATE that rewrites the WHERE column, a query with apply_mutations_on_fly = 1 can mark originally-matching granules as non-matching (the mutation, not the predicate, dropped the rows), poisoning the cache. A later apply_mutations_on_fly = 0 query reads that entry (its mutation snapshot has no data mutations, so the read path does not bypass the cache) and returns too few rows. Skip attaching the MarkRangesInfo when the read task applies mutations or patch parts ahead of the filter, which starves the WHERE-side writer. This mirrors the PREWHERE guard and keeps the write path symmetric with the read path (MergeTreeDataSelectExecutor already bypasses the cache when mutations are active). Add a WHERE-clause regression to 03229_query_condition_cache_on_fly_mutations: an on-fly UPDATE changing the WHERE column at index_granularity = 1, with move-to-prewhere disabled so the predicate is evaluated by FilterTransform. Co-Authored-By: Claude Opus 4.8 <[email protected]>
… is present A SELECT row policy is prepended as a filter step ahead of PREWHERE (getPrewhereActions), but the PREWHERE attribution path keys the cache write only on the query PREWHERE predicate's hash, not on the row policy. A user whose policy hides a mark could therefore populate a PREWHERE cache entry for that mark; a later query by a user without that policy shares the prewhere-only key, reads the entry in filterPartsByQueryConditionCache, and skips rows it should see. Gate the three PREWHERE attribution sites in MergeTreeSelectProcessor on the absence of row_level_filter, mirroring the existing readersChainCanSkipMarksBeforePrewhere (Issue #104781) and appliesMutationsBeforePrewhere guards. The WHERE-side writer is already scoped by the full filter_actions_dag key (which includes the row policy), so it needs no change. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The row-policy regression assumed a user without any policy on a policied table sees all rows, but the test config enables throw_on_unmatched_row_policies, so that user is denied (ACCESS_DENIED) and the test failed in Fast test. Use two users that both have a policy: one restricted to v < 50 and one with an always-true policy that must see every row. The always-true policy is dropped by the planner so the show user shares the poisoned PREWHERE cache key, still exercising the bug. Also add no-parallel-replicas to the test (the query condition cache is per replica, so the poisoning is deterministic only on a single replica) to stop random parallel-replicas runs from erroring with INCONSISTENT_CLUSTER_DEFINITION. Add a fourth case to 03229_query_condition_cache_on_fly_mutations that exercises a patch part (alter_update_mode = 'lightweight_force') rather than an on-fly mutation step, covering the info->patch_parts half of the appliesMutationsBeforePrewhere guard which the existing heavy ALTER UPDATE cases do not reach. apply_patch_parts = 1 poisons, apply_patch_parts = 0 must still see the original rows. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The patch-part case (Case 4) in 03229_query_condition_cache_on_fly_mutations inherited mutations_sync = 0 from the earlier on-fly mutation cases, so the lightweight ALTER ... UPDATE could return before the patch part was committed. The apply_patch_parts = 1 read could then see no patch, return 50, and never exercise the info->patch_parts guard. Set mutations_sync = 1 on the ALTER so the patch part is committed before the read. Output is unchanged. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…tions Fix query condition cache poisoning by on-fly mutations
…ypick/26.4/107145 # Conflicts: # src/Storages/MergeTree/MergeTreeSelectProcessor.cpp
…th master) BuzzHouse enables random entries from `system.fail_points`; some of them break the fuzzer or abort the server and must be skipped. The 26.4 exclusion list in `loadServerConfigurations` was stale: `wide_part_writer_fail_in_add_streams` (present on 26.4) was not excluded, which can cause BuzzHouse client failures. Also add `tcp_handler_fail_connection_setup`, `trigger_sanitizer_error` and `merge_tree_load_statistics_throw` for parity with master. Co-Authored-By: Claude Opus 4.8 <[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.
Original pull-request #107145
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job