Skip to content

Adaptive Aggregator - #111459

Merged
alexey-milovidov merged 134 commits into
ClickHouse:masterfrom
nihalzp:adaptive-aggregator
Aug 15, 2026
Merged

alexey-milovidov merged 134 commits into
ClickHouse:masterfrom
nihalzp:adaptive-aggregator

Conversation

@nihalzp

@nihalzp nihalzp commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

Improved version in: #123531

image image image

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

New adaptive algorithm for parallel GROUP BY (controlled via setting enable_adaptive_aggregator, enabled by default): each thread aggregates into its own hash table until it holds adaptive_aggregator_freeze_threshold keys and then freezes it, so frequent keys keep updating the small cache-resident tables with no coordination, while rare keys are routed by their hash into per-bucket backlogs and aggregated exactly once, inside the bucket-parallel merge.

Version info

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

@clickhouse-gh

clickhouse-gh Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cac8fe0]

Summary: ✅


AI Review

Summary

This PR adds the adaptive GROUP BY path, supplied-hash support for string hash tables, and the bucket-local Top-K aggregation conversion. I re-checked the current diff against the prior review discussion and did not find any remaining correctness, compatibility, or regression issues that warrant new review comments.

Final Verdict

✅ No new actionable findings in the current revision.

LLVM Coverage Report

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

Changed lines: Changed C/C++ lines covered: 1580/1936 (81.61%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jul 22, 2026
Comment thread src/Core/Settings.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🔴 AI verdict: degradation — 1 regressed, 10 improved out of 35 analysed

This PR rewrites the string hash-table dispatch to reuse a canonical hash across find/emplace/prefetch and adds adaptive aggregation plus bucket-local Top-K, all in the GROUP BY hot path. That lines up cleanly with the results: nine string-keyed aggregation queries improved (ClickBench Q13/Q16/Q17/Q18/Q30/Q31/Q34 by 6-23%, and TPC-H Q7 ×8.0 faster and Q8 ×2.1 faster), each backed by a matching CPU-time reduction. The lone regression, ClickBench Q28 at +5.4%, is small and consistent with adaptive-aggregation staging overhead on that key distribution. No flagged query is off-path, so nothing was downgraded.

clickbench

🔴 1 regressed · 🟢 8 improved

Flagged queries (9 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
🔴 28 regression 1048 1105 +5.4% <0.0001 aggregation: Q28 is a small +5.4% regression with a matching CPU uptick; the adaptive-aggregation staging/freeze machinery can add overhead on this key distribution. Real but marginal.
🟢 13 improvement 426 344 -19.3% <0.0001 aggregation: Q13 is a string-keyed GROUP BY, exactly the path this PR rewrites (string hash table drops redundant hashing plus adaptive aggregation). A -19.3% gain here is plausible and backed by a matching CPU drop.
🟢 16 improvement 793 609 -23.2% <0.0001 aggregation: Q16 groups on string keys; the faster string-hash dispatch and reused canonical hash directly speed this path. -23.2% is consistent with the lower CPU time.
🟢 17 improvement 556 447 -19.6% <0.0001 aggregation: Q17 is another multi-key string GROUP BY on the rewritten string hash table path. -19.6% tracks the CPU-time drop and is confirmed by both tests.
🟢 18 improvement 1298 1090 -16.0% <0.0001 aggregation: Q18's string-keyed aggregation benefits from the reduced-hashing and adaptive-aggregation changes. -16.0% is a real, work-backed improvement.
🟢 23 improvement 167 74 ×2.3 faster <0.0001 aggregation: Despite noisy history, the ×2.3 faster result is backed by a large CPU-time drop (1615ms to 501ms), matching the aggregation hot-path rewrite. Treated as real.
🟢 30 improvement 279 234 -16.1% <0.0001 aggregation: Q30's GROUP BY runs on the reworked aggregation path; -16.1% is consistent with the lower CPU time.
🟢 31 improvement 395 307 -22.3% <0.0001 aggregation: Q31 groups on string keys and benefits from the string-hash and adaptive-aggregation changes. -22.3% is confirmed by both tests and the CPU drop.
🟢 34 improvement 1256 1178 -6.2% <0.0001 aggregation: Q34's string GROUP BY sees a smaller -6.2% gain, still on the rewritten aggregation path and backed by lower CPU time.

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 72 ×8.0 faster <0.0001 aggregation: TPC-H Q7 groups on nation string keys; the reduced-hashing string hash table explains the ×8.0 faster result, backed by a CPU-time collapse (8077ms to 456ms).
🟢 8 improvement 175 82 ×2.1 faster <0.0001 aggregation: TPC-H Q8's string-keyed aggregation benefits from the same rewrite; ×2.1 faster is work-backed by the lower CPU time.

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: 104ae97a-69db-44c2-9c65-bc26838e77d7
  • MIRAI run: 29b68f68-e3f6-4b1d-b003-3795465b2d66
  • PR check IDs:
    • clickbench_1337198_1786777021
    • clickbench_1337204_1786777021
    • clickbench_1337216_1786777021
    • tpch_adapted_1_official_1337227_1786777021
    • tpch_adapted_1_official_1337238_1786777021
    • tpch_adapted_1_official_1337269_1786777021

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🕵 Confirmed the remaining feedback is addressed in the current head.

Comment thread src/Interpreters/Aggregator.cpp Resolved
Merged via the queue into ClickHouse:master with commit e3362f1 Aug 15, 2026
181 checks passed
@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
@clickgapai

Copy link
Copy Markdown
Contributor

@nihalzp @alexey-milovidov — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

nihalzp pushed a commit to nihalzp/ClickHouse that referenced this pull request Aug 17, 2026
…ation seal

Aggregator::sealPendingChunks coalesces staged mini-chunks by cloning its
destination from the first source and then calling prepareForSquashing and
insertRangeFrom, both of which assert_cast every source to the destination's
class. That assumes all minis share one physical representation per aggregate
argument position, and only ColumnConst was stripped.

Lazy column replication is decided per block: HashJoinResult materializes the
blocks whose replication does not grow the output and leaves the others wrapped
in ColumnReplicated. The staging path preserves the wrapper (recursiveRemoveSparse
keeps it and ColumnReplicated::index returns a ColumnReplicated), and the frozen
kernel receives the raw block columns rather than the unwrapped copies
prepareAggregateInstructions builds. So one stream legitimately mixes wrapped and
dense columns at the same position and the cast aborts:

  Bad cast from type DB::ColumnReplicated to DB::ColumnString
  Received signal Aborted (6)

Two sites abort, both reproduced: prepareForSquashing for String, Nullable, Array,
Map, Tuple, Variant, Dynamic and Object, and insertRangeFrom for the numeric
vectors, which do not override prepareForSquashing. Normalizing the whole sources
vector including the front, before the destination is cloned, covers both and
keeps destination and sources drawn from the same normalized set. The
structureEquals gate leaves the homogeneous case allocation-free, so lazy
replication keeps its benefit wherever it is uniform. This mirrors the treatment
Squashing already applies to the identical hazard.

In a release build assert_cast degrades to static_cast, so the same input reads a
ColumnReplicated through a ColumnVector layout and copies from a bogus data
pointer instead of failing loudly.

Found by CI in Stress test (arm_msan) on an unrelated pull request. The aborting
file was added to master by ClickHouse#111459 and is on no release branch, so no backport
applies.
@nihalzp nihalzp linked an issue Aug 17, 2026 that may be closed by this pull request
rorylshanks pushed a commit to rorylshanks/ClickHouse that referenced this pull request Aug 26, 2026
`AggregatingTransform::initGenerate` used `many_data->variants.size()` as the
number of producers to wait for on the finish barrier. That vector is sized at
construction and used to be immutable afterwards, but the adaptive aggregation
made the last finisher append the early-drain routing table to it, so the
vector grows while the producers that are still arriving read its size:

    Write of size 8 ... by thread T1033:
      ClickHouse#7 DB::AggregatingTransform::initGenerate() src/Processors/Transforms/AggregatingTransform.cpp:1375
    Previous read of size 8 ... by thread T1187:
      ClickHouse#1 DB::AggregatingTransform::initGenerate() src/Processors/Transforms/AggregatingTransform.cpp:1319

Count the producers with `ManyAggregatedData::num_producers`, fixed at
construction time, so the barrier never reads the vector that the last
finisher grows. The barrier's outcome is unchanged: only the finisher whose
`fetch_add` returns `num_producers - 1` proceeds, and it is the only thread
that touches `variants` afterwards.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=ed4df189fc08473dd6cee5c84b4fce89d49ca640&name_0=MasterCI&name_1=Stress%20test%20%28arm_tsan%29

Caused by: ClickHouse#111459
@nihalzp nihalzp mentioned this pull request Sep 22, 2026
47 of 72 tasks
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

Development

Successfully merging this pull request may close these issues.

Speed up GROUP BY for large states

5 participants