Skip to content

Strengthen QCC TopK test 04338: ProfileEvents usage check, ORDER BY ASC, negative LIMIT - #110381

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/04338-qcc-topk-profileevents-neglimit
Jul 14, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/04338-qcc-topk-profileevents-neglimit

Conversation

@groeneai

@groeneai groeneai commented Jul 14, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

Description

Follow-up to the review on merged PR #104478 (@ PedroTadim asked to verify the query condition cache is actually used and to cover negative limits and ORDER BY ... DESC).

Strengthens 04338_query_condition_cache_topk_part_set_invalidation:

  • ProfileEvents QCC-usage check. The existing test only asserted result correctness, which can stay green while the cache silently stops firing. Added system.query_log ProfileEvents['QueryConditionCacheHits'] assertions: the warm run has 0 hits, the cached run has hits, and the run after DROP PARTITION has 0 hits (proving the part-set-salt invalidation, not just a correct result).
  • ORDER BY ... ASC case. After warming ORDER BY k DESC, an ORDER BY k ASC query must not reuse the DESC entry, since condition_hash folds the sort direction. Asserted via 0 hits.
  • Negative LIMIT case. A negative LIMIT becomes a NegativeLimit plan step with no __topKFilter, so it never engages the TopK-salted QCC path. Added a correctness case (matches the use_query_condition_cache = 0 ground truth) confirming it neither reuses nor poisons the TopK entries.

Verified: 20/20 passes with full randomization; base pass with --no-random-settings. The ORDER BY ... DESC path was already covered by the original test.

Version info

  • Merged into: 26.7.1.950 (included in 26.7 and later)

…ative LIMIT

Adds system.query_log ProfileEvents assertions (QueryConditionCacheHits) to
prove the query condition cache is actually used on the cached run and
invalidated after DROP PARTITION, rather than only checking result
correctness. Also adds an ORDER BY ASC case (distinct direction key must not
reuse the DESC entry) and a negative LIMIT case (NegativeLimit plan, no
__topKFilter, so the TopK-QCC path does not engage).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? N/A (test-strengthening, not a bug fix). QCC-usage is deterministic: warm run QueryConditionCacheHits=0, cached run >0, after DROP PARTITION =0, verified repeatedly.
b Root cause explained? The old test asserted only result correctness, which can stay green if the cache silently stops firing. ProfileEvents on system.query_log prove the cache is used and that the part-set salt invalidates entries.
c Fix matches root cause? Yes: adds ProfileEvents['QueryConditionCacheHits'] assertions plus ASC (direction-fold) and negative-LIMIT (no __topKFilter) cases directly exercising the concerns raised.
d Test intent preserved / new tests added? Preserved; the original DESC correctness + part-set-invalidation checks are unchanged. New assertions added on top.
e Both directions demonstrated? Verified 20/20 with full randomization and a clean base pass with --no-random-settings.
f Fix is general across code paths? N/A (test-only change).
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-only change).
h Backward compatible? (maintainer-approved exception only) N/A (test-only change).
i Invariants and contracts preserved? N/A (test-only change).

Session id: cron:clickhouse-worker-slot-3:20260714-115200

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 14, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @shankar-iyer for review: test-only follow-up strengthening the QCC TopK part-set-invalidation test with ProfileEvents-based cache-usage assertions, plus ORDER BY ASC and negative LIMIT cases (requested by PedroTadim on #104478).

@clickhouse-gh

clickhouse-gh Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [29174af]

Summary: ✅


AI Review

Summary

This PR strengthens 04338_query_condition_cache_topk_part_set_invalidation by adding QueryConditionCacheHits assertions around the warm/cached/after-DROP PARTITION runs and by covering the ORDER BY ... ASC and negative-LIMIT cases more directly. The current revision also closes the earlier vacuous-pass gaps around the DESC warmup and the post-negative positive rerun, so I did not find a remaining correctness, coverage, or PR-metadata issue in the changed test.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 14, 2026
@PedroTadim PedroTadim self-assigned this Jul 14, 2026
@PedroTadim
PedroTadim enabled auto-merge July 14, 2026 12:15
…gative-LIMIT blocks

Address review on 04338: (1) before using the ASC 0-hits as evidence that
sort direction is part of the QCC key, prove the warm DESC LIMIT 5 query
actually populated a TopK entry by rerunning it and asserting a cache hit;
otherwise the ASC miss could be expected for the wrong reason on the one-part
post-drop dataset. (2) For negative LIMIT, add a cache-state assertion
(QueryConditionCacheHits = 0 on the negative-limit run, so it does not reuse
the TopK entry) plus a follow-up positive LIMIT 5 that still hits, proving the
negative run neither reuses nor poisons the TopK-salted entries. Test-only.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
auto-merge was automatically disabled July 14, 2026 12:59

Head branch was pushed to by a user without write access

@groeneai

Copy link
Copy Markdown
Collaborator Author

Both gaps addressed in 6d4c60a (test-only).

  1. Direction-fold block: before the ASC 0-hits assertion, the warm `ORDER BY k DESC LIMIT 5` is now rerun once and asserted to hit (`QueryConditionCacheHits > 0`), so the ASC miss is only used as evidence once the DESC entry is proven to exist. Measured on the one-part post-drop dataset: DESC warm populates 1 entry, DESC rerun = 1 hit, ASC = 0 hits (fresh direction key).

  2. Negative-LIMIT block: it now warms a positive TopK entry, runs the negative-limit query with a `QueryConditionCacheHits = 0` assertion (it does not reuse the TopK entry), then reruns the positive `LIMIT 5` and asserts it still hits (`> 0`), proving the negative run neither reuses nor poisons the TopK-salted entries. Confirmed `LIMIT -5` plans as `NegativeLimit` with no `__topKFilter`, so it registers only a plain-WHERE miss.

Verified deterministic: 10 clean runs plus 20x with randomization enabled, all pass.

… rerun

The post-negative positive rerun used FORMAT Null and only checked
QueryConditionCacheHits > 0, so a write-side key collision that overwrote the
positive TopK entry while the negative-LIMIT query finished would still show a
cache hit and keep the test green. Materialize that rerun and compare it against
a use_query_condition_cache = 0 ground truth so a poisoned entry is caught.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 04338_neg_pos2 now materializes ORDER BY k DESC LIMIT 5 and compares row-for-row against a use_query_condition_cache = 0 ground truth; both return 499007,498007,497007,496007,495007 on every run.
b Root cause explained? The old 04338_neg_pos2 used FORMAT Null and asserted only QueryConditionCacheHits > 0. A write-side key collision where 04338_neg (legitimately 0 hits) overwrites the positive TopK entry while finishing would leave the FORMAT Null rerun green: a cache hit is observed but the returned rows are never inspected.
c Fix matches root cause? Yes. Replacing the FORMAT Null rerun with a materialized rerun plus a QCC-off ground truth makes a poisoned entry produce a row-level diff, which is exactly the gap the bot identified.
d Test intent preserved / new tests added? Preserved and strengthened. The QueryConditionCacheHits > 0 assertion is kept; the new row-correctness check is added on top, so the negative-LIMIT-does-not-poison half of the contract is now proven.
e Demonstrated both directions? Yes. The hardened test FAILS on a stale reference (row diff would surface a poisoned entry) and PASSES 3/3 with and 3/3 without randomization on the current binary (buildId D9F9075F3931C5598285D2C86DEA6985C307C342, matches server).
f Fix general, not a narrow patch? N/A (test-only change, not a code bug).
g Fix generalizes across inputs? N/A (test-only change, not a code bug).
h Backward compatible? Preserved. No server behavior, setting, or format changed.
i Invariants and contracts preserved? Preserved. No source code touched; the test still exercises the TopK-salted QCC path (FilterStep above ReadFromMergeTree, dynamic filtering enabled).

Session id: cron:clickhouse-worker-slot-3:20260714-135900

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 29174af

All checks green. No test-level failures on this head. Only CH Inc sync (private, not actionable by us) is exempt from ownership.

Check / test Reason Owner / fixing PR
(all functional/stress/integration/fuzzer checks) — all passed
CH Inc sync — CH Inc sync (private, not actionable)

Session id: cron:our-pr-ci-monitor:20260714-183000

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 14, 2026
Merged via the queue into ClickHouse:master with commit 4033f4a Jul 14, 2026
176 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 14, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…-topk-profileevents-neglimit

Strengthen QCC TopK test 04338: ProfileEvents usage check, ORDER BY ASC, negative LIMIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-ci 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.

4 participants