Repository navigation
Price small single-level aggregation tables in full for the automatic parallel replicas estimate - #131697
Price small single-level aggregation tables in full for the automatic parallel replicas estimate#131697groeneai wants to merge 2 commits into
Conversation
…tput estimate `Aggregator::estimateSizeOfCompressedState` prices a single-level table from at most 100 sampled states (`period = max(N / 100, 1)`) and scales by N / samples. On a skewed table a few states hold most of the bytes and the sample misses them. `03634_autopr_output_bytes_estimation` query_10 (215 `MobilePhoneModel` keys, the three largest `uniqExact` states hold 70% of the 25380 pairs) was priced at 145.9K / 149.3K, about 0.72x of the exact ~200K, and dipped to 142K, below the test's 2.5x bound of 144880 (PR ClickHouse#131529, arm_asan_ubsan azure, 2026-11-14). Since ClickHouse#129565 the default merge packs a worker's hash partitions into one table per call (ClickHouse#131357), and the same sampler prices query_10 at 91K..136K there. After the existing sample, a single-level table of at most 1000 states (more than 100, or with a `NULL` key) whose sampled serialized size fits the remaining budget is priced from all its states. The budget is one compressed block (`DBMS_DEFAULT_BUFFER_SIZE`, 1 MiB) per call, shared by the aggregate functions. The full pass serializes into a `MemoryWriteBuffer` of that size, so `WriteBuffer::CurrentBufferExhausted` stops a large missed state midway; the sample then stands. The full pass also prices the state of the `NULL` key, which `AggregationDataWithNullKey` keeps outside the hash table: `size()` counts it, `forEachMapped` never visits it, so an all-NULL group was priced as 0. Sampled passes are unchanged (the NULL state at position 0 of a sample would be weighted by the period). A first version priced every table of at most 1000 states in full. A count bound does not bound bytes: on `k = number % 1000, uniqExact(v)` over 10M rows the serial merge went from 642 to 1037 ms (estimator 48 -> 467 ms) for an unchanged estimate. Measured on a debug build: - new test 05768: base 0 0 1 1 0 0 0 0 0 0, fix 1 1 1 1 1 1 1 1 1 1 (raw 101424 / 101437 / 384 / 1770 / 100370 / 601483 / 101601 / 600066 / 100683 / 1740..1993); trigger disabled 0 0 1 1 0 0 0 0 0 0; budget 50000 0 0 1 1 0 0 0 0 0 0; NULL-key visit removed 1 0 1 1 0 1 1 0 1 1; catch removed: the third query fails with CurrentBufferExhausted. - byte bound removed (`MemoryWriteBuffer(0)`) 1 1 0 1 1 0 1 0 1 1 (2001478, 1202535, 1500118); count bound removed 1 1 1 0 1 1 1 1 1 1 (101607). - `|| has_null_key` removed from the trigger 1 1 1 1 0 1 1 0 1 1 (124, 37); budget per aggregate function 1 1 1 1 1 0 1 0 1 1 (1202535, 1500118). - exclusive count bound (`< 1000`) 1 1 1 1 1 1 0 1 1 1 (1769). - budget not zeroed after an aborted pass, or the `> 0` guard removed, 1 1 1 1 1 1 1 0 1 1 (900089); lower bound `> 101` 1 1 1 1 1 1 1 1 0 1 (188). - state priced uncompressed (`size = all->second`) 1 1 1 1 1 1 1 1 1 0 (1001412); budget 700 KiB 1 1 1 1 1 1 1 1 1 0 (384). - query_10 on CI's test.hits, 20 runs each: fix 197802..197884 (pinned serial merge), 197802..197872 (max_threads 32), 198877..204593 (default merge); base 148809..149620, 149466..149620, 155062..247124. - cost, interleaved two-server A/B with cross-over, medians of 30 runs: the 1000/1001/50000-key cases never take the full pass (instrumented probe, 0 of 76 calls) and keep their duration; 150 states of 1.02 MB serialized add 5.3..5.9 ms of estimator time; a pass aborted at 1 MiB adds 0.17 ms. Related: ClickHouse#131357 Related: ClickHouse#131358 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Return the frame-overhead comment to its place in master above the scaling it explains; shorten the NULL-key and small-table pricing comments to one line each. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (7 review rounds)Bounce cap reached - published as-is: the last fix round (test case 10 only, no source change) was not re-run through the gate. Every finding below was fixed or answered; nothing is left open.
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261114-221000 |
|
Workflow [PR], commit [af5e7ae] Summary: ❌
AI ReviewSummaryThis PR improves the automatic parallel replicas output estimate by fully serializing small aggregation tables, including the 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/Aggregator.cpp.o |
29.32 MiB | 29.59 MiB | +278.18 KiB (+0.93%) |
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
7 translation units recompiled, 140 s compile time in total, 7 of them have a recent master baseline.
CI finish ledger - af5e7aeEvery 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-020012 |
The previous commits avoided the copy of a constant argument to the size of the block only for functions that ignore repeated values. Every other function still received the full copy, although all its rows are equal. The AST fuzzer runs on PR ClickHouse#131697 hit exactly that case: `anyOrDefault(toFixedString(<const>, 1048576))` and `count(toFixedString(<const>, 1048576))` without keys asked for 64 GiB and 8 GiB per `AggregatingTransform` thread, and the server was OOM-killed. A constant of 1 MiB is not folded by the analyzer, so `count` keeps its argument and the copy happens. Now, without keys, when all arguments of a function are constants, they are copied to at most `max(1, 1 MiB / bytes of one row)` rows (one row for a function that ignores repeated values), and that copy is added repeatedly through `Aggregator::addBatchSinglePlace` until the rows of the block are covered. The function sees the same values in the same order through the same batch API, so every function and combinator is covered (`count`, `any`, `-OrDefault`, `-If`, `-Array`, `-State`, Null adapters). A constant of up to 16 bytes still gets one batch of the whole block at the default block size. The existing conversion pipeline (`convertToFullColumnIfConst`, replicated, sparse and `LowCardinality` handling) is reused on a shorter constant (`ColumnConst::cloneResized`), which replaces the separate one-row branch. Not covered: aggregation with keys (keyed consumers, the JIT loop and the adaptive producer's argument staging read arguments at block positions), and a function with both constant and non-constant arguments. CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131697&sha=af5e7aee158e8379e77dc757c0a1c04b91ea14d2&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131697&sha=af5e7aee158e8379e77dc757c0a1c04b91ea14d2&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Fixing PR for the |
Related: #131357
Related: #131358
Related: #129565
Related: #111985
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Automatic parallel replicas now estimates the size of the aggregation states of a small
GROUP BY(at most 1000 groups whose states fit into 1 MiB) from all of its states instead of a sample of 100, so a few large states, or the state of theNULLkey, are no longer missed.Description
Aggregator::estimateSizeOfCompressedStateprices a single-level table from at most 100 sampled states. On a skewed table the sample misses the few states that hold most of the bytes:03634_autopr_output_bytes_estimationquery_10(215 keys, threeuniqExactstates hold 70% of the data) is priced at about 0.72x of the exact size, which occasionally falls below the test's 2.5x bound (CI report). Since #129565 the default merge packs partitions per worker, and there the same sampler scatters between 0.45x and 1.2x (#131357, closed after #131358 pinned the test to the serial merge). The state of theNULLkey, kept outside the hash table, was never serialized.A single-level table of at most 1000 states whose sampled size fits one compressed block (1 MiB per call, shared by the aggregate functions) is priced from all its states, the
NULLkey included. The states are serialized into aMemoryWriteBufferof the remaining budget, soCurrentBufferExhaustedstops writing a large state the sample missed, and the sample stands. Larger tables and two-level buckets are sampled as before.Validation (debug build): the new test
05768fails on master and under each of fourteen mutations.query_10on CI'stest.hits: 197.8K..204.6K in 60/60 runs across the serial merge,max_threads = 32and the default merge (master: 148.8K..149.6K and 155K..247K). Cost: tables of more than 1000 states or 1 MiB never take the full pass (duration unchanged); 150 states, 1 MB in total, add about 5.5 ms of estimator time, a pass aborted on a 2 MBStringstate 0.17 ms.The #131358 pin in
03634is no longer needed for this estimate; I left it in place.Workflow [PR]
Sync PR [sync-upstream/pr/131697]