Skip to content

Fix QCC write by topK query with split AND-chain filters - #112083

Merged
alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
shankar-iyer:fix_top_qcc_again
Aug 11, 2026
Merged

alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
shankar-iyer:fix_top_qcc_again

Conversation

@shankar-iyer

Copy link
Copy Markdown
Member

Ref : https://github.com/ClickHouse/clickhouse-private/issues/65449

topK queries in following structure were not updating the QCC during execution. Hence QCC pruning for these queries was nil.

SELECT
    *,
    concat(substring(check_name, 1, 15), '...', substring(check_name, -15)) AS check,
    substring(test_name, -50) AS test,
    check_start_time,
    test_status,
    substring(commit_sha, 1, 10) AS commit_short,
    pull_request_number AS prnum,
    multiIf((toDateTime('2022-11-18 21:48:54') - toIntervalDay('1')) <= check_start_time, '<1d', (toDateTime('2022-11-18 21:48:54') - toIntervalDay('7')) <= check_start_time, '<1w', '') AS last
FROM checks
WHERE (test_status NOT IN ('OK', 'SKIPPED')) AND (test_name LIKE '%02275_full_sort_join_long%') AND ((toDateTime('2022-11-18 21:48:54') - toIntervalDay('10')) <= check_start_time)
ORDER BY check_start_time DESC
LIMIT 20
FORMAT hash

FilterStep::transformPipeline splits a conjunctive WHERE into one FilterTransform per atom, but only the residual transform was given the query condition cache condition. A granule fully eliminated by any earlier atom was therefore never recorded: that atom has no condition to write under, and its empty chunk, which carries the MarkRangesInfo identifying the granule, is dropped by skip_empty_chunks before it reaches the residual. Pass the condition to every atom - the chain is conjunctive, so a chunk emptied at any atom proves no row of those mark ranges can satisfy the whole and(...).

Changelog category

  • Improvement

Changelog entry

  • Fixed the query condition cache not being populated for granules eliminated by any but the last conjunct of a WHERE clause with multiple AND conditions.

@clickhouse-gh

clickhouse-gh Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2a72b52]

Summary: ✅


AI Review

Summary

This PR propagates the query condition cache key to every split AND-atom FilterTransform, so granules that go empty before the residual filter can still be recorded, and adds focused stateless coverage for the split-chain cases. I did not find a correctness, safety, or evidence gap in the current code: the earlier row-policy concern is resolved in the current planner, and the TopK-specific gate/salt behavior is already covered by the existing query condition cache TopK tests.

Final Verdict

✅ No blocking or major issues found.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.80% 78.80% +0.00%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 27, 2026
Comment thread src/Processors/QueryPlan/FilterStep.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.30% +0.10%
Functions 92.00% 92.00% +0.00%
Branches 78.40% 78.40% +0.00%

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

Full report · Diff report

@shankar-iyer

Copy link
Copy Markdown
Member Author

@alexey-milovidov After this fix, setting use_query_condition_cache_for_top_k (added in #111492) can be enabled by default.

alexey-milovidov and others added 3 commits August 6, 2026 19:33
Query condition cache entries are keyed by (table UUID, part name, condition hash), so a background
merge landing between the priming and the reusing query replaces the parts and the second run reads
zero hits. Add `SYSTEM STOP MERGES` right after each `CREATE TABLE` to make the hit counts stable in CI.

Co-Authored-By: Claude Fable 5 <[email protected]>
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 11, 2026
Merged via the queue into ClickHouse:master with commit f9a60bf Aug 11, 2026
181 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 11, 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 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.

3 participants