Repository navigation
Do not create a second query condition cache entry when no skip index ran - #116259
alexey-milovidov merged 8 commits into
Conversation
… ran getSkipIndexProfiledConditionHash salts a condition hash with the effective skip-index profile so that skip-index-derived exclusions are only consulted by a query that ran the same index set (issue ClickHouse#108519). It had no early return for an empty effective set, and a SipHash of the bare hash is never the bare hash, so on a table with no secondary index the profiled key was still distinct from the bare row-level key. Index analysis (ReadFromMergeTree) and the row-level filter (FilterTransform) then recorded the same predicate under two keys: two zeroed O(marks per part) bitmaps per part, plus two lookups per part on the read side, each returning a full bitmap copy. Return the bare hash when the effective skip-index set is empty, and skip the lookup that is then a duplicate. This is not a weakening of issue ClickHouse#108519: with no effective index nothing profile-dependent can have been recorded, and the exclusions written under that key can only come from primary-key or partition pruning, for which the row-level key is already sound. The collapse is exact because write only ever clears bits into an all-true entry while reads AND-merge, so one key holds the same union of cleared bits as two. That is a dependency worth stating: if a future change ever sets a matching_marks bit back to true, the collapse stops being equivalent. distributed_index_analysis is excluded, because there each replica runs index analysis on its own metadata (StorageMergeTreeAnalyzeIndexes) while the coordinator keys the ranges it gets back with its own index set, so a locally empty set is no evidence that no skip index contributed. Its activation gate can fire on a table with no secondary index because get_indexes_size also counts the primary-key index. The single helper serves all three call sites (one write, two reads), so write and read keys move together and cannot diverge. UsefulSkipIndexes::empty() is used rather than useful_indices.empty() because a TopK-filtering index also makes the set non-empty. No use_skip_indexes conjunct is needed: buildIndexes returns before populating skip_indexes when that setting is off. Measured on the issue ClickHouse#115998 shape (10M rows, index_granularity = 8, five parts of 250k marks, 1500 independent point lookups), with the cache sized above the working set so the entry gauge is not eviction-pinned: 14370 entries / 452.7 MB before, 7500 / 236.3 MB after, i.e. one entry per part and predicate. Wall clock is unchanged at that fixture scale and is not claimed. Reported by Avogar on pull request ClickHouse#116134. Related: ClickHouse#115998
Review round 1: both new-test oracles could pass for the wrong reason. The entry test asserted only that the collapsed entry kept at least one matching granule. Recomputing the two pre-fix bitmaps shows the survivor must keep exactly 2 granules, while the profiled-key-only state keeps 25 and the bare-key-only state 27, so the boolean form passed in all three and could not detect loss of the row-level writer. It now asserts the exact count. Demonstrated by dropping the max_block_size line, which takes the count to 25 while the old form still passed. Nothing pinned the read side at all. Reverting only the guard that skips the duplicate lookup changes no ProfileEvent: the two keys are equal, so the second lookup returns the same entry, merges idempotently, and the one hit per consultation is counted by the caller rather than per key. QueryConditionCache::read does emit one TEST-level line per key looked up, and send_logs_level ships those to the client independently of the server log level, so a second test counts them per part: one with no effective skip index, two when one really ran. Verified against a build with only that guard reverted, which reddens the new test alone and leaves the entry test green. Counting hits and misses together makes the number independent of cache state, so this test needs no cache drop and no no-parallel tag. Also scope the entry test's SYSTEM FLUSH LOGS to query_log, which the style check requires, and record on the no-parallel tag that the last check is what needs it, since every count is already table_uuid-scoped. No source change in this round.
Review round 2. Both new tests assert cache entry and lookup counts, and filterPartsByQueryConditionCache returns before any cache access unless allow_experimental_analyzer is set, so with the old analyzer every count is zero. The stateless job that carries the "old analyzer" option installs allow_experimental_analyzer = 0 in the server's default profile, which makes both tests fail there rather than merely lose coverage: measured on an old-analyzer server, the lookup test reads 0 against a reference of 1 then 2. Tag both no-old-analyzer. That states what is true, namely that the behaviour under test does not exist without the analyzer, whereas setting allow_experimental_analyzer inside the tests would run the new code path on a job whose purpose is to exercise the old one. Verified on one binary against two servers: the tests pass unchanged with the analyzer on, skip with reason old-analyzer with it off, and fail with it off once the tag is removed. Earlier rounds pinned every setting the test runner randomizes, which does not cover a job flavour that installs a different default in the server profile. No source change in this round.
Internal second-model review (3 rounds, 6 findings, all resolved)I reviewed this cold myself and had an independent model review it, three times. Six
I also declined one requested remedy while agreeing with the finding behind it: adding a Verified independently during the cold reviews: the collapse is exact because |
|
Workflow [PR], commit [08aa036] Summary: ✅
AI ReviewSummaryThis PR collapses the skip-index-profiled query-condition-cache key onto the bare condition hash when no effective skip index ran, and skips the redundant read-side probe. I did not find a source-level correctness issue in that change, but one regression-test gap remains. Findings
Final VerdictLLVM Coverage Report |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20260825-071100 |
|
cc @tiandiwonder @CurtizJ, could you review this? |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units70 translation units recompiled, 867 s compile time in total, 70 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
|
📊 Cloud Performance Report
Release-transition baseline: compared against the most recent available 26.9 masters at or before the trusted base-commit anchor because no 26.10 master baseline exists there. This PR skips the second, redundant query condition cache lookup on tables with no effective skip index. That only changes filtered reads, and saving one lookup per part costs microseconds. Most flagged improvements are GROUP BY queries with no WHERE clause (Q16, Q18, Q32, Q33, Q34) that never touch the cache, so they and Q31 are downgraded to not sure. Q32 (-47.5%) cannot be overridden because its delta is so large, but it too is off-path and is most likely a baseline difference (the PR build reports 26.10, the baseline 26.9). Q23 (-41.9%) does run the changed lookup, but the gain is far larger than the change explains and master has been trending on that query recently, so treat it with caution. clickbench🟢 2 improved · Flagged queries (7 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
…A check overclaiming The lookup counter piped the client's stderr straight into grep, so the pipeline's status came from grep and a query that threw after emitting some lookup lines still produced the expected number. Measured: with max_rows_to_read = 1 the query fails with TOO_MANY_ROWS after both lookups are logged, and the arm still read 1. The client's status is now checked before the lines are counted, and a failure prints the exception instead of a count. The distributed_index_analysis check ran on a two-part table with the default activation thresholds, so remote analysis never started: DistributedIndexAnalysisMicroseconds was 0 and no replica was scheduled. It does pin something real, and dropping the setting from the guard makes it fail, but that is the key being salted from the raw session setting, not the remote-analysis path its label claimed. Lowering the thresholds and pointing the query at a reachable replica cluster does start remote analysis, and the entry count is identical either way, so the heavier fixture would add no coverage. The label and a comment now say what the check observes.
CI finish ledger c8932d6Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260825-170000 |
…OUPING SETS The memory-bound-only group only asserted that tokens are absent, so an implementation that declined the aggregation rewrite for that setting combination would have satisfied it: it has neither `by hash(` nor a memory-efficient merge. Two rows now name the strategy positively, as the group above already does. Measured on `distributed_aggregation_memory_efficient = 0, enable_memory_bound_merging_of_aggregation_results = 1`: `Aggregating (partial)` and `MergingAggregated (merge)` are 2 on this branch and 0 on a build with the guard removed, where `by hash(` is 2 instead. The comment on the `serialize_query_plan = 1` row now says that it pins the outcome rather than the strategy. No explain observes a rewrite made on the shard side: `EXPLAIN PLAN distributed = 1` prints the plan as it was shipped, and `EXPLAIN PIPELINE distributed = 1` throws `NOT_IMPLEMENTED` with `serialize_query_plan = 1` (`ReadFromRemote.cpp:990`). The row is still specific to this defect: with the guard removed it fails with "pushed bucket N twice". `GROUPING SETS` in the guard comment is now inline code, as `AGENTS.md` requires for literal SQL names. An earlier round of this branch declined that same request on the grounds that no such repository rule exists; that was wrong, the rule is at `.claude/CLAUDE.md:7` and predates this branch. The same round declined the request to put the supplied report URL in the commit message, and that was wrong too, for the same reason: `.claude/CLAUDE.md:169`. Both links, per that rule: Failing check this commit answers: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=116446&sha=c5ac60fd8dcd6e81e274126b140fb9828bbb54e5&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%2C%20selected%20tests%29 ClickHouse#116446 Report the fix was written for: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=116259&sha=2e55a2277e66c6762e444c2cb6dc18a585cbfc5b&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 ClickHouse#116259 Validation: 40/40 with the promise settings, `serialize_query_plan`, `prefer_localhost_replica`, `optimize_aggregation_in_order` and `max_threads` randomized; the guard-removed build reddens the file in both `serialize_query_plan` modes, on a fresh server per arm.
`enable_memory_bound_merging_of_aggregation_results` is randomized by the runner (`tests/clickhouse-test:1597`) and every arm that set only `distributed_aggregation_memory_efficient = 1` left it open, so those arms did not say which of the two settings they exercised, and a guard tied only to the randomized one could pass. It is pinned in the session block now, and the two promise sources have an arm each: `(1, 0)` and `(0, 1)`, each asserting the whole demoted shape in one row (no shuffle, plus the partial aggregation and its merge) so a plan left undistributed does not satisfy them. Measured standalone on those two combinations: both rows are 1 on this branch and 0 on a build with the guard removed, where `by hash(` is 2 instead. The four rows they replace are folded into them, so the truth table is now `(0,0)` keeps the shuffle and `(1,0)`, `(0,1)`, `(1,1)` demote. Report this branch is fixing, per `AGENTS.md`: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=116259&sha=2e55a2277e66c6762e444c2cb6dc18a585cbfc5b&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 ClickHouse#116259 Validation: 40/40 with both promise settings, `serialize_query_plan`, `distributed_plan_force_shuffle_aggregation`, `prefer_localhost_replica`, `optimize_aggregation_in_order` and `max_threads` randomized; the guard-removed build reddens the file in both `serialize_query_plan` modes, one fresh server per arm.
Measured against a build whose aggregation rewrite returns without transforming the step: the row passes in both `serialize_query_plan` modes, so it does not separate the demotion from an aggregation left unrewritten above the gather. The rows above separate the two, and they can only do it when the plan is not shipped, which the comment now says. Report this branch is fixing, per `AGENTS.md`: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=116259&sha=2e55a2277e66c6762e444c2cb6dc18a585cbfc5b&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 ClickHouse#116259
…en-no-skip-index # Conflicts: # src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp
…yzer tag The comment on the early return in getSkipIndexProfiledConditionHash now states only the two invariants a reader needs there: with no effective skip index no exclusion depends on the index profile, and that does not hold under distributed_index_analysis. The note on why a second probe of an identical key is skipped, and the sentence added to the filterPartsByQueryConditionCache overview, described this change rather than the code, so they are removed. master removed the old-analyzer test mode and the `no-old-analyzer` tag in 182a59e, so both tests stop carrying it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`SET max_block_size = 8` is needed so the row-level writer sees whole granules, but it stayed in effect for the final `system.query_log` scan, which reads every other test's queries too. Measured on a 43k-row query_log: ~162 ms of user CPU per scan at 8-row blocks against ~16 ms at the default, and the cost grows with the log. Reset it before the scan, as 05078_external_distinct_equality does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
On a table with no applicable data skipping index, a query no longer stores the same predicate under two query condition cache keys. This halves the number of
O(marks per part)cache entries such a query allocates, and removes one cache lookup per part. Queries withdistributed_index_analysisenabled keep both keys.Description
Reported by @ Avogar on #116134, on the #115998 reproducer.
getSkipIndexProfiledConditionHashsalts a condition hash with the effective skip-index profile, so skip-index-derived exclusions are only read back by a query that ran the same index set (#108519). It had no early return for an empty effective set, and aSipHashof the bare hash is never the bare hash, so on a table with no secondary index the profiled key was still distinct from the row-level one. Index analysis and the row-level filter then recorded one predicate twice: two zeroedO(marks per part)bitmaps, and two lookups, per part.The fix returns the bare hash when the effective set is empty, and skips the now-duplicate lookup. This is not a weakening of #108519: with no effective index nothing profile-dependent can have been recorded. The collapse is exact because
writeonly clears bits into an all-trueentry while reads AND-merge, so one entry holds the same union of cleared bits as two.distributed_index_analysisis excluded: there a replica runs index analysis on its own metadata while the coordinator keys the result with its own index set, so a locally empty set does not prove no skip index contributed.Measured on the #115998 shape (10M rows,
index_granularity = 8, 5 parts x 250k marks, 1500 point lookups), with the cache sized above the working set so the gauge is not eviction-pinned: 14370 entries / 452.7 MB before, 7500 / 236.3 MB after, i.e. one entry per part and predicate. Wall clock is unchanged at this scale and is not claimed; @ Avogar's figures are his, on another shape.For a table that does have an applicable skip index nothing changes: both keys are still written and looked up, which the new tests assert. Each fails without its half of the fix.
Workflow [PR]
Sync PR [sync-upstream/pr/116259]
Version info
26.10.1.1250-master(included in26.10and later)