Skip to content

Do not write TopK query condition cache entries from reads with on-fly mutations or patch parts - #122617

Merged
fm4v merged 1 commit into
masterfrom
fix-qcc-topk-on-fly-mutations
Sep 29, 2026
Merged

fm4v merged 1 commit into
masterfrom
fix-qcc-topk-on-fly-mutations

Conversation

@fm4v

@fm4v fm4v commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

The __topKFilter PREWHERE of an ORDER BY ... LIMIT read (cached since #114659) drops granules based on a threshold derived from the rows of all parts in the read. The write paths only guard on appliesMutationsBeforePrewhere of the part being read, so a part without mutations of its own recorded its granules as empty under a threshold tightened by rows that an on-fly mutation or a patch part rewrote in another part. Part names do not change until the mutation is materialized, so a later read that does not apply it (apply_mutations_on_fly = 0, apply_patch_parts = 0, or any read after KILL MUTATION) consulted the entry and skipped rows.

The fix disables query condition cache writes in ReadFromMergeTree::initializePipeline for TopK reads whose mutations snapshot has data mutations or patch parts. This mirrors the consult side (filterPartsByQueryConditionCache), which already never reads the cache under such a snapshot. It covers both the TopK-salted PREWHERE write and the WHERE (MarkRangesInfo) carrier.

The new test fails on unfixed master (checked with the CI build of 7264abe5d4cf on a local server): all three cases return 1000000..1000004 instead of 1000..1004.

Closes: #122564
Related: #114659

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Version info

  • Merged into: 26.10.1.970-master (included in 26.10 and later)

…y mutations or patch parts

A TopK read drops granules based on a threshold derived from the rows of all parts, so a part without mutations of its own could record granules as empty under a threshold tightened by rows that an on-fly mutation or a patch part rewrote in another part. A later read that does not apply them (`apply_mutations_on_fly = 0`, `apply_patch_parts = 0`, or after `KILL MUTATION`) then skipped rows.

Closes: #122564
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9028e83]

Summary: ✅


AI Review

Summary

This PR fixes a real read/write asymmetry in TopK QueryConditionCache handling: the consult path already refuses cache reuse when the mutations snapshot has data mutations or patch parts, and the new initializePipeline guard now suppresses the matching write-side carriers for the same stamped TopK reads. I did not find a remaining correctness gap in the current code, and the added stateless test covers the ALTER UPDATE, post-KILL MUTATION, and patch-part repros described in the PR.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 9028e83.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.50% 79.50% +0.00%

Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Sep 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9028e83f2 with master e5e4f8fc8 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 7264abe5d; 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, 31 s compile time in total, 7 of them have a recent master baseline.

Job report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TopK PREWHERE cache entries keep thresholds from other parts' on-fly mutations

2 participants