Skip to content

Cherry pick #107145 to 26.4: Fix query condition cache poisoning by on-fly mutations - #110643

Merged
robot-clickhouse-ci-1 merged 8 commits into
backport/26.4/107145from
cherrypick/26.4/107145
Jul 16, 2026
Merged

robot-clickhouse-ci-1 merged 8 commits into
backport/26.4/107145from
cherrypick/26.4/107145

Conversation

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 6 commits June 11, 2026 04:00
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
@robot-ch-test-poll2 robot-ch-test-poll2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Jul 16, 2026
@PedroTadim PedroTadim assigned PedroTadim and unassigned alesapin Jul 16, 2026
PedroTadim and others added 2 commits July 16, 2026 08:40
…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]>
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 merged commit 66e46ca into backport/26.4/107145 Jul 16, 2026
2 checks passed
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 deleted the cherrypick/26.4/107145 branch July 16, 2026 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants