Repository navigation
Improve merging of low cardinality GROUP BY with Adaptive Aggregator - #129565
Conversation
… of their tracked memory
…-adaptive-merge # Conflicts: # src/Processors/Transforms/AggregatingTransform.cpp
|
Workflow [PR], commit [af91980] Summary: ✅
AI ReviewSummaryReviewed PR #129565 at Consumers traced for the indexed source representation:
Validation used the current-head CI report, which reports 14,785 passed tests, zero failures, and 4,311 skipped tests. Focused artifacts from Final VerdictStatus: ✅ Approve. No new inline findings to post. Normal CI checks still apply to the latest merge commit. |
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/Merge.cpp.o |
9.60 MiB | 10.07 MiB | +482.39 KiB (+4.91%) |
src/CMakeFiles/dbms.dir/Interpreters/Aggregator.cpp.o |
28.30 MiB | 28.56 MiB | +265.72 KiB (+0.92%) |
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/AdaptiveAggregation.cpp.o |
186.68 KiB | 248.37 KiB | +61.70 KiB (+33.05%) |
src/CMakeFiles/dbms.dir/Processors/Transforms/AggregatingTransform.cpp.o |
608.72 KiB | 626.61 KiB | +17.89 KiB (+2.94%) |
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.
Slowest function optimization changes (ThinLTO)
Median per-function time ratio to the master baseline: programs/clickhouse ×0.64, programs/clickhouse-keeper ×0.67 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.
| Binary | Function | Master | PR | Δ vs median |
|---|---|---|---|---|
programs/clickhouse |
_ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… |
3.6 s | 7.3 s | +5.0 s (+220%) |
Compile time of recompiled translation units
98 translation units recompiled, 999 s compile time in total, 98 of them have a recent master baseline.
Median compile-time ratio to the baselines is ×0.94 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -47.6 s (-5%) in total before that adjustment.
Symbol sizes
| Binary | Symbol | Master | PR | Δ |
|---|---|---|---|---|
programs/clickhouse |
void DB::(anonymous namespace)::visitTwoLevelVariant<DB::Aggregator::mergeAndConvertAdaptiveBucket(… |
142.29 KiB | removed | -142.29 KiB (-100.00%) |
programs/clickhouse-keeper |
void DB::(anonymous namespace)::visitTwoLevelVariant<DB::Aggregator::mergeAndConvertAdaptiveBucket(… |
142.23 KiB | removed | -142.23 KiB (-100.00%) |
programs/clickhouse-keeper |
void DB::(anonymous namespace)::visitTwoLevelVariant<DB::Aggregator::mergeAndConvertAdaptiveBucket(… |
new | 129.20 KiB | +129.20 KiB |
programs/clickhouse |
void DB::(anonymous namespace)::visitTwoLevelVariant<DB::Aggregator::mergeAndConvertAdaptiveBucket(… |
new | 129.20 KiB | +129.20 KiB |
programs/clickhouse |
DB::Aggregator::mergeSingleLevelPartitionsAndConvertToChunk(std::__1::vector<std::__1::shared_ptr<D… |
new | 100.07 KiB | +100.07 KiB |
programs/clickhouse-keeper |
DB::Aggregator::mergeSingleLevelPartitionsAndConvertToChunk(std::__1::vector<std::__1::shared_ptr<D… |
new | 100.07 KiB | +100.07 KiB |
programs/clickhouse-keeper |
DB::Aggregator::mergeSingleLevelPartitionAndConvertToChunk(std::__1::vector<std::__1::shared_ptr<DB… |
67.94 KiB | removed | -67.94 KiB (-100.00%) |
programs/clickhouse |
DB::Aggregator::mergeSingleLevelPartitionAndConvertToChunk(std::__1::vector<std::__1::shared_ptr<DB… |
67.94 KiB | removed | -67.94 KiB (-100.00%) |
…f the partition layout
diegomestre2
left a comment
There was a problem hiding this comment.
Thanks for answering the questions and for addressing what was good to add. No new remarks from my side. Once CI passes you can merge it! Great work again!
…-adaptive-merge # Conflicts: # src/Interpreters/AdaptiveAggregation/AdaptiveAggregation.h # src/Interpreters/AdaptiveAggregation/AdaptiveTopKPruning.h # src/Interpreters/AdaptiveAggregation/Merge.cpp # src/Interpreters/Aggregator.h # src/Processors/Transforms/AggregatingTransform.cpp
Since ClickHouse#129565 the bucket top-K pass runs under `serialize_query_plan`, so the pin and its comment are stale. The same lines were removed from the sibling bucket top-K tests there. 20 runs with `serialize_query_plan = 1` pass, and every pruned query converts with the bucket top-K. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s_estimation` Since ClickHouse#129565 the merge by hash partitions packs the partitions a worker claims into one table, and the `query_10` estimate randomly fell 2.7x..4x below the expected value in every run with the randomized `enable_parallel_single_level_merge = 1`. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=12d028ce436f528850e5c8b021e5a28e52b8cd2c&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_release%2C%20parallel%2C%202%2F2%29 Related: ClickHouse#131357 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ated from PromQL" This reverts commit 4d76414 (ClickHouse#128764), reversing changes made to 8886954. ClickHouse#129565 fixed the staging of the adaptive aggregator that made the grid aggregation of the SQL generated from PromQL slower, so that SQL can use the aggregator again. The conflict in the includes of PrometheusHTTPProtocolAPI.cpp is resolved by keeping `Server/PrometheusMetricsWriter.h`, added later by ClickHouse#125250. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the five conflicts of the 26.8 cherry-pick of ClickHouse#131595. They are all regions where master has code that 26.8 does not have: the deferral of large merges (ClickHouse#117153: mergeAndDestroyBatchOrDefer, DeferredMerges, mergeAndDestroyDetachedBatch and its catch), isSelfMerge (ClickHouse#130678), the bound of zero-sized nested states (ClickHouse#130120), and the partition-by-partition merge of the adaptive aggregator (ClickHouse#129565). None of them is labelled for 26.8, and the resolution takes none of them. 26.8 has no catch around the per-function loop of the batch merge (master got it in ClickHouse#117153), so a throw in the merge of one function would still leak the queued states of every function after it. The resolution adds a 26.8 mergeAndDestroyDetachedBatch, the master function without the deferral: the per-function loop, and a catch that destroys the states of the following functions. The map mergeDataImpl (also the JIT path) and the flush of mergeSingleLevelPartitionImpl call it. The collection loop of mergeSingleLevelPartitionImpl is wrapped in the try of ClickHouse#131595, so it is re-indented. In IAggregateFunctionHelper::mergeAndDestroyBatch the 26.8 assertion is kept (no isSelfMerge). AggregateFunctionTuple.cpp and the test merge cleanly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the five conflicts of the 26.9 cherry-pick of #131595. They are all regions where master has code that 26.9 does not have: the deferral of large merges (#117153: mergeAndDestroyBatchOrDefer, DeferredMerges, mergeAndDestroyDetachedBatch and its catch), isSelfMerge (#130678), the bound of zero-sized nested states (#130120), and the partition-by-partition merge of the adaptive aggregator (#129565). None of them is labelled for 26.9, and the resolution takes none of them. 26.9 has no catch around the per-function loop of the batch merge (master got it in #117153), so a throw in the merge of one function would still leak the queued states of every function after it. The resolution adds a 26.9 mergeAndDestroyDetachedBatch, the master function without the deferral: the per-function loop, and a catch that destroys the states of the following functions. The map mergeDataImpl (also the JIT path) and the flush of mergeSingleLevelPartitionImpl call it. The collection loop of mergeSingleLevelPartitionImpl is wrapped in the try of #131595, so it is re-indented. In IAggregateFunctionHelper::mergeAndDestroyBatch the 26.9 assertion is kept (no isSelfMerge). AggregateFunctionTuple.cpp and the test merge cleanly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the five conflicts of the 26.10 cherry-pick of #131595. They are all regions where master has code that 26.10 does not have: the deferral of large merges (#117153: mergeAndDestroyBatchOrDefer, DeferredMerges, mergeAndDestroyDetachedBatch and its catch), isSelfMerge (#130678), the bound of zero-sized nested states (#130120), and the partition-by-partition merge of the adaptive aggregator (#129565). None of them is labelled for 26.10, and the resolution takes none of them. 26.10 has no catch around the per-function loop of the batch merge (master got it in #117153), so a throw in the merge of one function would still leak the queued states of every function after it. The resolution adds a 26.10 mergeAndDestroyDetachedBatch, the master function without the deferral: the per-function loop, and a catch that destroys the states of the following functions. The map mergeDataImpl (also the JIT path) and the flush of mergeSingleLevelPartitionImpl call it. The collection loop of mergeSingleLevelPartitionImpl is wrapped in the try of #131595, so it is re-indented. In IAggregateFunctionHelper::mergeAndDestroyBatch the 26.10 assertion is kept (no isSelfMerge). AggregateFunctionTuple.cpp and the test merge cleanly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #123531
Background
With the adaptive aggregator (
enable_adaptive_aggregator), every thread aggregates into its local hash table until the table freezes atadaptive_aggregator_freeze_thresholdkeys oradaptive_aggregator_freeze_threshold_bytesbytes. A frozen table keeps updating the keys it holds and stages the rows of the other keys, which the merge aggregates. Master merges the tables in one of two ways:The byte bound of the freeze counted the table's tracked memory, which includes the memory that its states own. A table of few keys with growing states, such as the sets of
uniqExactor the samples of thetimeSeries*functions, froze early and staged the rows of every key that came later, although its cells fit in the cache.Changes
AggregatedDataVariants::hashTableBufferBytes): the cells that every probe walks. The memory of the states grows through the keys that a table already holds, which a freeze does not stop, so it no longer freezes a table.AggregatedDataVariants::CellIndex): a counting sort of the cell pointers and hashes by the partitions of the staged records, whose number grows with the threads up to 4096, 2048 at 64 threads. The adaptive merge reads the cells of a merge unit, a run of the partitions of one bucket, in place, so no table converts to two-level. The string hash table of aFixedStringkey, or of aStringkey withenable_packed_string_keys_in_aggregation = 0, cannot be indexed and still converts.count,uniqExactoruniqExactIfalso serves an aggregation in which no table froze.Results
On AWS
c8a.16xlarge(64 cores, one thread per core), baseline → this PR.groupArray(2)(number), 15K keysgroupArray(4)(number), 16K keysuniqExact(number % 3), 15K keysany(toString(number)), 16K keysargMax(toString(number), number), 16K keysgroupBitmap(number % 1000), 16K keysClickBench
hits(100M rows), one server per binary. Q8 and Q9 no longer freeze on the memory of theiruniqExactsets, so no thread allocates staging buffers and no table converts to two-level.GROUP BY RegionID:count(DISTINCT UserID), top 10GROUP BY RegionID:sum,count,avg,count(DISTINCT UserID), top 10GROUP BY SearchEngineID, SearchPhrase:count, whereSearchPhrase <> '', top 10GROUP BY UserID:count, top 10Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The adaptive aggregator (
enable_adaptive_aggregator) merges the hash tables of its threads through an index of their cells, instead of converting them to two-level tables (seegroup_by_two_level_threshold) or walking every table for every hash partition (enable_parallel_single_level_merge).GROUP BYqueries whose tables hold 15-16 thousand keys per thread without freezing, with states such asgroupArray,uniqExact,anyorgroupBitmap, run 1.34x faster at 64 threads, and ClickBench Q8 and Q9 use 21-25% less memory at 64 threads. The byte bound of the freeze,adaptive_aggregator_freeze_threshold_bytes, now counts the hash-table buffer of a table instead of its tracked memory, so tables of few keys with growing states keep aggregating in place.Workflow [PR]
Sync PR [sync-upstream/pr/129565]
Version info
26.11.1.2161-master(included in26.11and later)