Skip to content

Key query condition cache entries by apply_deleted_mask - #113239

Closed
fm4v wants to merge 1 commit into
masterfrom
nik/qcc-key-apply-deleted-mask
Closed

fm4v wants to merge 1 commit into
masterfrom
nik/qcc-key-apply-deleted-mask

Conversation

@fm4v

@fm4v fm4v commented Aug 4, 2026 •

Copy link
Copy Markdown
Member

Related: #112947
Related: #107145

Changelog category (leave one):

  • Improvement

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

Query condition cache entries are now keyed by apply_deleted_mask, so reads that show lightweight-deleted rows and ordinary reads no longer share entries.

Description

A read with apply_deleted_mask = 0 sees lightweight-deleted rows. A granule whose only matching rows are deleted is therefore "no match" for an ordinary read and "may match" for that one, and the two verdicts are not interchangeable. They shared a cache key all the same: (table uuid, part name, condition hash).

Only one direction is unsound. An entry written by an ordinary read, consumed by an apply_deleted_mask = 0 read, makes it skip a granule holding exactly the deleted rows it was asked to return. The reverse is conservative: the writer saw a superset of the rows, so its "no match" still holds.

The fix

QueryConditionCache::makeKey mixes the flag into the key, so the two get separate key spaces. The salt is added only when the mask is off, which leaves the keys of ordinary reads byte-identical and invalidates nothing on upgrade.

read and write take the flag explicitly rather than each site salting its own condition hash. Three places compute a condition hash independently (index analysis in ReadFromMergeTree, the PREWHERE write in MergeTreeSelectProcessor, the consult in MergeTreeDataSelectExecutor) and a fourth writes through MarkRangesInfo attached to the chunk. Salting at each of them means a missed site is a silent wrong-results bug; making it a parameter means a missed site does not compile. MarkRangesInfo carries the flag from MergeTreeSelectProcessor to FilterTransform for the WHERE write.

File- and object-storage tables have no deleted rows and pass true.

Relationship to #112947

That PR needs the same guarantee and takes the cheap route: queries with apply_deleted_mask = 0 neither read nor write the cache. That is the right shape for a backport (it is three lines and cannot regress anything), and it is what should ship to 26.3/26.4/26.5.

This is the version for master: nothing has to be excluded, so apply_deleted_mask = 0 keeps its pruning. The two are not independent at the end state, and the order matters. Whichever of them merges second has to remove #112947's two gates (in MergeTreeIOSettings and MergeTreeDataSelectExecutor) and flip the block in 04669_query_condition_cache_lightweight_delete that pins the no-caching behaviour. Left in place, those gates keep apply_deleted_mask = 0 out of the cache and the last assertion of 04700_query_condition_cache_apply_deleted_mask (a repeated apply_deleted_mask = 0 query prunes) fails. Landing #112947 first and removing its gates here is the cleaner sequence.

Tests

New 04700_query_condition_cache_apply_deleted_mask:

  • an ordinary read does not consume an apply_deleted_mask = 0 entry (fails before this change: it hits and prunes)
  • the reverse direction does not either
  • an apply_deleted_mask = 0 read still prunes on a repeat within its own key space
  • the deleted rows come back for a reader that asks for them however warm the cache is

A read with `apply_deleted_mask = 0` sees lightweight-deleted rows, so a
granule whose only matching rows are deleted is "no match" for an ordinary
read and "may match" for that one. The two verdicts are not interchangeable,
yet they shared a cache key: `(table uuid, part name, condition hash)`.

Give the two their own key spaces instead. The salt is mixed in only when
the mask is off, so the keys of ordinary reads are unchanged and no entry
is invalidated. `read` and `write` take the flag explicitly so every call
site has to name what it means; file- and object-storage tables have no
deleted rows and pass true.

This also makes it unnecessary to keep such queries out of the cache
altogether, which is what
#112947 does as the cheap,
backportable version of the same guarantee.
@clickhouse-gh

clickhouse-gh Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a239b7c]

Summary: ❌

job_name test_name status info comment
Fast test FAIL
04700_query_condition_cache_apply_deleted_mask FAIL cidb
Code Review DROPPED
Fast test (arm_darwin) DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_debug) DROPPED
Build (arm_asan_ubsan) DROPPED

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 4, 2026
@fm4v fm4v closed this Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant