Repository navigation
Skip missed rows that repeat the previous row in the adaptive aggregator's frozen loop when the aggregates ignore repeated values - #131793
Conversation
…tor's frozen loop Once a thread's adaptive hash table is frozen, the argument-record loop of `executeFrozenImpl` staged every row whose key missed it. A row with the keys and arguments of the missed row before it cannot change a state when every aggregate function ignores repeated values (`ignoresRepeatedValues`): the previous row's record reaches the same state with the same values. On ClickBench `hits`, stored by (CounterID, EventDate, UserID, ...), `GROUP BY ClientIP` with `uniq(UserID)` staged 99.2M of its 100M rows; now 23.0M. Such a row is now compared with the previous one (raw bytes of fixed-width columns, contents of `String` columns; any other column, an ineligible function or the top-K pruning keeps the old loop) and is not staged: - a block stops comparing when under half of its first 256 comparisons find a repeat, or when under 32 comparisons were made by its 256th staged row (the 256-row/50% rule of the grouped `uniqExact` repeat skip); the rest of the block runs the old loop; - a skipped row still counts in `frozen.staged_records/staged_bytes` as the record it repeats, with that record's bytes, so `adaptiveStagingRepeats` sees what it saw before (without this, key-ordered data never thaws and gets 1.27-1.46x slower, per ClickHouse#131738; at the batch's mean cost, long repeated `String` values lost master's thaw); - `uniqUpTo` gets the property, except with one floating-point argument (its state compares values with `==`, so each NaN counts); `-If` forwards it, the condition being one of the compared arguments. Keyless `uniqUpTo` of a `LowCardinality(String)` or `LowCardinality(FixedString)` argument therefore now adds each used dictionary value once, which is exact by the same idempotence. `chassert(scratch)` before the scratch copy in `appendDelayedRecords` states an invariant the static analyzer cannot derive (a value serialized into the scratch implies the scratch was set); without it clang-analyzer reports a null `memcpy` source on an infeasible path of the larger `executeFrozenImpl`. Xeon 6975P-C, release builds (None, no LTO), server pinned to one NUMA node, 5 paired rounds, 4 threads, ratio = PR / master [95% CI]: uniq(UserID) by ClientIP 0.711 [0.679, 0.745] 1292 -> 357 MB uniqExact(UserID) 0.752 [0.705, 0.803] uniqUpTo(3) / uniqUpTo(100) 0.820 / 0.737 uniqExact(UserID, SearchPhrase) 0.738 [0.592, 0.920] max(UserID) 0.822 length(groupUniqArray(UserID)) 0.690 uniqIf(UserID, IsRefresh = 0) 0.753 [0.576, 0.984] controls (uniqCombined, sum, top-K, keys or arguments that never repeat, key-ordered table) 0.893 .. 1.124, every CI contains 1 16 threads, 3 rounds: uniq 0.698, uniqExact 0.737, uniqExact(2 args) 0.697, controls 1.029 .. 1.074. Thaw counts identical in every query; results identical to master and to `enable_adaptive_aggregator = 0`; stripped binary +172 KiB. The analysis and the patch are from ClickHouse#131738. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Delete the comments that restate the new helpers' names and shorten two design comments to one line each. 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; 7 findings over 3 runs) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261115-124300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261115-061000 |
|
Workflow [PR], commit [953c4b0] Summary: ✅
AI ReviewSummaryReviewed #131793 at Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/Producer.cpp.o |
2.78 MiB | 3.09 MiB | +324.02 KiB (+11.39%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniqUpTo.cpp.o |
1.31 MiB | 1.33 MiB | +16.57 KiB (+1.23%) |
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.
Compile time of recompiled translation units
13 translation units recompiled, 271 s compile time in total, 13 of them have a recent master baseline.
CI finish ledger - 20e68b0Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261115-170017 |
|
Fixing PR for |
|
@groeneai Fix conflicts. |
…-skip-repeated-misses Resolve the conflict in AdaptiveAggregationImpl.h: keep master's [[nodiscard]] on the batch getters and add it to the new getRepeatedRecords(). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master enabled modernize-use-nodiscard, which marks these const overrides, the same as the other overrides in AggregateFunctionUniqUpTo.cpp. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Merged master in 50ca8a5. The only conflict was in |
CI finish ledger - e288559Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
All five Session id: cron:our-pr-ci-monitor:20261117-073017 |
…-skip-repeated-misses # Conflicts: # src/Interpreters/AdaptiveAggregation/Producer.cpp
…String keys The merge of master brought ClickHouse#129180's shortcut into the frozen loop of argument records: a key of fixed-width values and an empty String that repeats the last missed key is staged with that key's hash, without being hashed or probed. The shortcut staged the row before the repeat check, so with the `keysN_packed_string` methods the repeats of such a key were staged even when every aggregate ignores repeated values. The repeat check is now a lambda that both paths call. A row on the shortcut takes the last missed key's hash, so the check compares it with the previous row exactly as on the probed path. The test gains a `GROUP BY k, e` case whose String key is empty in the first half of each key: results equal `enable_adaptive_aggregator = 0`, and it stages less than half the records of a `sum` query. Measured on the debug build: 40768 staged records with the change, 120736 without it, 200704 for `sum`; the binary without the change fails only that check (`1 0`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-skip-repeated-misses # Conflicts: # src/Interpreters/AdaptiveAggregation/Producer.cpp
|
Merged master again in d7b8bdd and 301efba. The conflicts were in the frozen argument-record loop, with #129180 and #131551; the loop now returns e0f528a also applies the repeat skip to the empty-string shortcut of #129180, which otherwise staged every repeat of a key of fixed-width values and an empty |
…stable The case read 500000 rows in 8192-row blocks, so each producer reached its first thaw check with about 8 sampled keys behind its distinct-key estimate, and the true repeat factor (32) sits close to the thaw bound. Under CPU contention all four producers could miss it: `Stateless tests (amd_debug, flaky check)` failed 1 of 50 with `1 0`. The same noise let the mean-cost accounting mutant thaw in 40% of runs, so the case caught it only part of the time. With 2000000 rows in 65536-row blocks, measured on a debug build (4 threads, server pinned to 2-4 CPUs, 12-16 queries at once, part of the runs with the failing CI run's settings): - this PR: at least 3 of 4 producers thaw in 600/600 runs (old case: only 1 of 4 in 48/600); - master (no skip): at least 2 of 4 in 300/300; - mean-cost mutant: no producer thaws in 400/400 (old case: 81/200 thawed). Whole test: 300/300 with random settings; 60/60 red on the mean-cost mutant (this line only); 30/30 red on the mutant that drops the skipped rows from the evidence (both thaw lines). Wall time 0.65 s without random settings. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #131738
Related: #127940
Related: #130269
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
GROUP BYwithuniq,uniqExact(COUNT(DISTINCT)),uniqHLL12,uniqTheta,groupUniqArray, anduniqUpTo,minandmaxof anything but one floating-point argument (also with-If) is faster and uses less memory when consecutive rows repeat the key and the arguments, as in data stored in an order correlated with them: the adaptive aggregator no longer stages such a row.SELECT ClientIP, uniq(UserID) FROM hits GROUP BY ClientIPis 1.4x faster with 4 threads and uses 72% less memory.Description
Once a thread's adaptive hash table freezes, each row whose key misses it is staged as a record with its arguments and looked up at the merge. A row with the keys and arguments of the previous missed row adds nothing when every aggregate function ignores repeated values (
IAggregateFunction::ignoresRepeatedValues, #127940). ClickBenchhitsis stored by(CounterID, EventDate, UserID, ...), soGROUP BY ClientIPwithuniq(UserID)staged 99.2M of its 100M rows; now 23.0M.The argument-record loop now compares such a miss with the previous row (bytes of fixed-width columns,
Stringcontents; any other column, an ineligible function or the top-K pruning keeps the old loop) and does not stage it:uniqUpTogets the property, except with one floating-point argument (each NaN counts), and-Ifforwards it. KeylessuniqUpToof aLowCardinality(String)orLowCardinality(FixedString)argument therefore now adds each used dictionary value once.#130269 folds more misses for
min/max; the two compose.Validation: a new test compares every case with
enable_adaptive_aggregator = 0and checks the skip and thaw parity. Xeon 6975P-C, release builds, 5 paired rounds, 4 threads, ratio = PR / master [95% CI]:uniq0.71 [0.68, 0.75] (1292 -> 357 MB),uniqExact0.75,uniqUpTo0.74 to 0.82,uniqExact(UserID, SearchPhrase)0.74,max0.82,groupUniqArray0.69,uniqIf0.75; 16 threads, 3 rounds:uniq0.70,uniqExact0.74. Controls (ineligible functions, non-repeating data, a key-ordered table): 0.89 to 1.12, every interval contains 1. Results identical. The CI queries are theClientIProws ofuniq.xml. Analysis and patch: #131738.Workflow [PR]
Sync PR [sync-upstream/pr/131793]