Repository navigation
Parallelize single-level to two-level conversion in Aggregator::prepareVariantsToMerge - #114691
Conversation
…reVariantsToMerge When some per-thread aggregation states converted themselves to two-level mid-scan (after the accurate aggregation memory accounting from #107490 made `group_by_two_level_threshold_bytes` engage for queries with few groups but heavy states, e.g. uniqExact), the merge preparation converted all the remaining single-level variants sequentially on one thread. Initializing a two-level variant allocates and zeroes 256 bucket tables, and there is one variant per query thread, so on machines with many cores this serial loop dominates short aggregation queries: ClickBench Q10/Q11 on c7a.metal-48xl (192 cores) regressed 2-3x. Convert the variants in parallel on the aggregator thread pool instead. Bisected and validated on c7a.metal-48xl via ClickBench manual runs: ClickHouse/ClickBench#1360 Closes: #114640
|
Workflow [PR], commit [2b63a91] Summary: ✅
AI ReviewSummaryThis PR parallelizes the remaining single-level to two-level conversion in Missing context / blind spots
Final Verdict✅ No new findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 28/29 (96.55%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 53 s compile time in total, 7 of them have a recent master baseline. |
|
Validation of this PR's CI build (
Q10/Q11 are back to pre-regression level on the affected machine (the remaining ~0.02 s vs the pre-#107490 Q11 baseline is the now-parallel conversion plus the two-level merge itself). The other 41 queries are unchanged within noise except Q23, which is the separate regression tracked in #114692 and is present in this PR's base. |
|
📊 Cloud Performance Report 🟢 AI verdict: This PR parallelizes the two-level aggregation-variant conversion in Aggregator::prepareVariantsToMerge, replacing a sequential per-thread loop that dominated short GROUP BY queries. All four flagged results are improvements on aggregation queries (clickbench Q18 -7.2% and Q23 ×2.3 faster; tpch Q7 ×8.2 faster and Q8 ×2.2 faster), and each exercises the exact merge path the change touches, so the speedups are plausible and on-path. tpch Q7 and Q8 show the largest gains, consistent with the merge cost being removed from their post-join aggregation. No regressions were flagged, and the private and public diffs are identical. clickbench🟢 2 improved Flagged queries (2 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🟢 2 improved Flagged queries (2 of 22)
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. Debug info
|
…-two-level-conversion
Closes: #114640
Related: #107490
Related: ClickHouse/ClickBench#1360
When some per-thread aggregation states convert themselves to two-level mid-scan,
Aggregator::prepareVariantsToMergeconverts all the remaining single-level variants sequentially on one thread. Initializing a two-level variant allocates and zeroes 256 bucket tables, and there is one variant per query thread, so on machines with many cores this serial loop dominates short aggregation queries.This regime became reachable after #107490: the accurate aggregation memory accounting makes
group_by_two_level_threshold_bytes(default 50 MB) engage for queries with few groups but heavy states (e.g.uniqExactover ~300 groups, where the states sum to hundreds of MB while the hash tables stay tiny). Previously the query-tracker delta under-reported such memory (each thread batches up tomax_untracked_memorybefore propagating), so the threshold effectively never fired for this shape. Once it fires mid-scan, only the variants that happen to execute a block after the crossing convert themselves (measured: ~25 of 192 on ClickBench Q11), and the merge preparation then serially converts the remaining ~170.Effect on ClickBench (
c7a.metal-48xl, 192 cores), hot runs, bisected and measured via manual ClickBench dispatches (evidence and result files in ClickHouse/ClickBench#1360):c79b0d9227e(4 min before #107490)32129f76fca(4 min after #107490)ab8535b3ab0)group_by_two_level_threshold_bytes=0group_by_two_level_threshold=1(uniform early conversion)The last two rows show the cost is specifically the serial merge-prep conversion after a mid-scan flip: disabling the bytes trigger recovers fully, and uniform early conversion (which happens in parallel, each thread converting its own variant during the scan) is also fast. The daily ClickBench dashboard shows the same regression on both
c7a.metal-48xlandc8g.metal-48xl(192 physical cores) since 2026-07-19, and not on machines with 96 or fewer physical cores.The fix converts the remaining single-level variants in parallel on the aggregator thread pool, following the existing
ThreadPoolCallbackRunnerLocalpattern used elsewhere in the file. Converting distinct variants concurrently is safe:AggregatedDataVariants::convertToTwoLeveltouches only the variant's own members.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Parallelize the single-level to two-level conversion of per-thread aggregation states before merging. This fixes a 2-3x regression of short
GROUP BYqueries with heavy aggregate states (e.g.COUNT(DISTINCT ...), ClickBench Q10/Q11) on machines with very many cores, introduced in 26.7 development builds when the aggregation memory accounting was made accurate.Documentation entry for user-facing changes
Version info
26.8.1.1469(included in26.8and later)