Repository navigation
Conversation
…query condition cache The query condition cache keeps one bit per granule (no match / maybe), so a repeated `SELECT count() FROM t WHERE <filter>` still reads every granule with a match. A count-only read whose whole filter is a deterministic PREWHERE now also records the number of rows that passed PREWHERE per completely read granule (UInt16; granules with 65535 or more matches are not counted), under the same key and guards as the bits. The exact-count path of `optimizeUseAggregateProjections` removes those granules from the ranges to read and adds their counts. The counts live in a separate LRU that may use only the part of `query_condition_cache_size` that the bits leave free, so the bits behave as before. Patch by @snelheidai from ClickHouse#127947, tests from the issue renumbered to 05493-05495. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y condition cache counted `analyzeProjectionCandidate` keeps a part on the parent read when the exact count optimization cut its ranges, because a projection part returns every matching row of its part. It recognizes such a part by a non-empty `exact_ranges`. The marks counted from the query condition cache were removed from `ranges` without being added to `exact_ranges`, so a part counted only by the cache could be replaced by a normal projection (for example with `prefer_optimize_projection = 1`) and its counted rows were then returned again: a repeated `SELECT count() FROM t PREWHERE s LIKE '%needle%'` over a table with a projection `(SELECT s ORDER BY s)` returned 100040 instead of 100020. Add the counted marks to `exact_ranges` too. A new section of `05366_exact_count_with_normal_projection` covers it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…andomized settings Every section uses a freshly created table, which has a new UUID and so no entry in the query condition cache: drop `SYSTEM DROP QUERY CONDITION CACHE` and the `no-parallel` tag. Clamp the randomized settings the tests depend on with a `Random settings limits` line instead of body `SET`s, and drop the obsolete `query_plan_optimize_prewhere`. Reads through the columns cache record and use exact counts as well (measured: a count read with 100 columns cache hits recorded counts, and the next count was answered from them with the right result), and the tests pass with `use_columns_cache = 1` forced, so it is no longer pinned to 0. The pending DROP COLUMN pruning workaround of 05495 is not needed on master any more. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ore narrowing it `computeMarkMatchCounts` emitted the count of a granule as `UInt32`, while `writeMatchCounts` checks it against `UNKNOWN_MATCH_COUNT` only after that narrowing. A granule of a non-adaptive part with 2^32 + 7 matching rows was therefore cached as an exact 7. Carry the count as `size_t` until the limit check, which is the only place that narrows it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… cache `writeMatchCounts` constructed the per-part `CountsEntry` (2 bytes per mark of the part, filled) and only then checked whether it fits into the space the `MatchingMarks` leave free. When it does not fit, every finished read task of the part allocated and filled the whole array just to throw it away. Compute the weight from the mark count first, with overflow saturation, and construct the entry only when it fits. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Two read tasks of the same part that both missed the lock-free lookup allocated a whole counts array each, and the second one was dropped because the first had inserted its own meanwhile. Match counts are inserted only under `counts_size_mutex`, so a second lookup under it sees every insertion. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…on cache `count(expr)` must evaluate its argument, which can throw, so it neither records nor uses the counts, like the statistics shortcut for `count()` in the same file. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the comments of the match counts of the query condition cache to their invariants and drop the ones that restate the code. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 5 rounds, 7 findings) plus my own review (10 nits).
Severity: ❌ blocker / |
|
cc @KochetovNicolai @Algunenano, could you review this and add the |
|
Workflow [PR], commit [3ffb168] Summary: ✅
AI ReviewSummaryThe PR answers repeated bare Changed-output consumers traced: Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes88 object files changed (+229.99 KiB total), 0 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units478 translation units recompiled, 5979 s compile time in total, 478 of them have a recent master baseline. |
Dropping or renaming a MergeTree table without a UUID (in an Ordinary database) calls `Context::clearCaches`, which clears the whole query condition cache, match counts included. Parallel tests do that (01053, 01114, 05218, 05214, 05219 and others), so a count warmed up by these tests could be gone by the query that checks it: counted -> read in the targeted jobs, where those tests run next to these ones 50 times. Like the other query condition cache tests that check the cache is used, they run without parallel tests now. The normal projection case moves from 05366 into 05493, so that 05366 stays as on master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - fdb44ccEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no Session id: cron:our-pr-ci-monitor:20261104-003004 |
|
Fixing PR for the Performance Comparison (amd_release, master_head, 4/6) job timeout: #129176 |
…ch-counts # Conflicts: # src/Processors/QueryPlan/Optimizations/optimizeUseAggregateProjection.cpp # src/Processors/QueryPlan/ReadFromMergeTree.cpp # src/Processors/QueryPlan/ReadFromMergeTree.h
Closes: #127947
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A repeated
SELECT count() FROM t WHERE <filter>whose whole filter is deterministic and evaluated inPREWHEREis now answered from exact per-granule match counts kept in the query condition cache, instead of reading again every granule that has a match. The counts use only the part ofquery_condition_cache_sizethat the cached conditions leave free.Description
The query condition cache keeps one bit per granule, so a repeated count reads every granule with a match again (ClickBench q21 still reads 3,160 of 12,322 granules, #127947). This carries the patch from #127947 by @ snelheidai: a count-only read whose whole filter is a deterministic
PREWHEREalso records how many rows passed it in each completely read granule (UInt16; 65535 or more matches are not counted), under the same key and guards as the bits. The exact-count path skips those granules and adds their counts. The counts live in a separate LRU limited to the space the bits leave free, so the bits behave as before.Added on top of the patch: counted granules also go into
exact_ranges, otherwise a normal projection could replace a counted part and return its rows twice (100040 instead of 100020 withprefer_optimize_projection = 1).Option A is implemented, as asked in #127947 (comment): on by default, no setting of its own (
optimize_use_implicit_projections = 0turns it off with the other exact-count optimizations). Option B would add a setting, default off, checked at the start ofReadFromMergeTree::getQueryConditionCacheKeyForMatchCounts. @alexey-milovidov, A or B? The case for B: counts share the bits' key, so a key collision like the one #122966 fixes can make a count wrong, in either direction, even where the bits stay right. With A, ClickBench q21 hot runs are answered from the counts.Validation: the issue's tests (05493-05495, gtest) with a normal projection case in 05493, and a differential test over 720 random tables (about 2,000 counts, randomized settings) found no wrong count; the issue's patch fails it on the projection case. On 20M rows a repeated count reads 1 row instead of 5.46M. Recording does not slow a first run (release build: median CPU within 0.5% of recording off, also when the counts do not fit).
Workflow [PR]
Sync PR [sync-upstream/pr/129051]