Skip to content

Parallelize single-level to two-level conversion in Aggregator::prepareVariantsToMerge - #114691

Merged
alexey-milovidov merged 6 commits into
masterfrom
fix-114640-parallel-two-level-conversion
Aug 15, 2026
Merged

alexey-milovidov merged 6 commits into
masterfrom
fix-114640-parallel-two-level-conversion

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 13, 2026 •

Copy link
Copy Markdown
Member

Closes: #114640
Related: #107490
Related: ClickHouse/ClickBench#1360

When some per-thread aggregation states convert themselves to two-level mid-scan, Aggregator::prepareVariantsToMerge converts 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. uniqExact over ~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 to max_untracked_memory before 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):

build Q10 Q11
master c79b0d9227e (4 min before #107490) 0.133 0.092
master 32129f76fca (4 min after #107490) 0.297 0.165
master 07-11 (ab8535b3ab0) 0.391 0.213
master 07-11 + group_by_two_level_threshold_bytes=0 0.093 0.102
pre-#107490 + group_by_two_level_threshold=1 (uniform early conversion) 0.132 0.095

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-48xl and c8g.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 ThreadPoolCallbackRunnerLocal pattern used elsewhere in the file. Converting distinct variants concurrently is safe: AggregatedDataVariants::convertToTwoLevel touches only the variant's own members.

Changelog category (leave one):

  • Performance Improvement

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 BY queries 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

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.8.1.1469 (included in 26.8 and later)

…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
@clickhouse-gh

clickhouse-gh Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2b63a91]

Summary: ✅


AI Review

Summary

This PR parallelizes the remaining single-level to two-level conversion in Aggregator::prepareVariantsToMerge for the short GROUP BY workload that regressed after the bytes-based two-level threshold started firing mid-scan. After reviewing the current diff, the surrounding aggregation code, and the full prior discussion, I did not find a remaining correctness, safety, or performance issue that warrants a new inline comment.

Missing context / blind spots
  • ⚠️ The broader post-update CI matrix was still in progress during this review, so I could not use the pending ASan/TSan/MSan and full build results as additional validation of the new parallel conversion path.
Final Verdict

✅ No new findings.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.80% 86.70% -0.10%
Functions 92.00% 92.00% +0.00%
Branches 79.20% 79.20% +0.00%

Changed lines: Changed C/C++ lines covered: 28/29 (96.55%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Aug 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 2b63a91ae with master 6d0a17390 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 693.26 MiB 690.24 MiB -3.02 MiB (-0.44%)

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 units

7 translation units recompiled, 53 s compile time in total, 7 of them have a recent master baseline.

Job report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Validation of this PR's CI build (build_amd_release @ 2a9e54c7201) on c7a.metal-48xl under the exact ClickBench daily protocol (result files: ClickHouse/ClickBench#1360, system clickhouse-i114640-fix):

build Q10 hot Q11 hot
master just before #107490 (pre-regression baseline) 0.124 0.078
master 07-11 (regressed) 0.391 0.213
current master (daily CDN build, 08-13) 0.187 0.193
this PR 0.096 0.097

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.

@clickhouse-gh

clickhouse-gh Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🟢 AI verdict: improvement — 4 query(s) improved out of 35 analysed

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)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
🟢 18 improvement 1298 1205 -7.2% <0.0001 aggregation: This is a GROUP BY query, and the PR parallelizes the two-level aggregation-variant conversion in the merge phase, so the -7.2% median drop is on-path.
🟢 23 improvement 167 74 ×2.3 faster <0.0001 aggregation: Heavy GROUP BY query on the exact path this PR speeds up; the ×2.3 faster delta is large and consistent even though the source measurements were noisy.

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)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
🟢 7 improvement 574 70 ×8.2 faster <0.0001 aggregation: Q7 does a join then GROUP BY; the parallel two-level conversion removes the sequential merge cost, matching the ×8.2 faster shift confirmed by both tests.
🟢 8 improvement 175 81 ×2.2 faster <0.0001 aggregation: Q8 aggregates after a multi-way join; the ×2.2 faster median aligns with the cheaper two-level conversion this PR introduces.

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
  • StressHouse run: 8114c1e8-1741-4799-be9c-b014bfd49243
  • MIRAI run: 225cbb53-a816-43ed-a891-b00fb6ba04b4
  • PR check IDs:
    • clickbench_1394554_1786797559
    • clickbench_1394567_1786797558
    • clickbench_1394574_1786797558
    • tpch_adapted_1_official_1394585_1786797558
    • tpch_adapted_1_official_1394610_1786797559
    • tpch_adapted_1_official_1394624_1786797559

Comment thread src/Interpreters/Aggregator.cpp Resolved
Comment thread src/Interpreters/Aggregator.cpp Resolved

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Aug 15, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 15, 2026
Merged via the queue into master with commit 98ee3b2 Aug 15, 2026
181 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-114640-parallel-two-level-conversion branch August 15, 2026 17:09
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

3 participants