Skip to content

Skew-robust Sharded Aggregation - #108067

Closed
nihalzp wants to merge 30 commits into
ClickHouse:masterfrom
nihalzp:skew-robust-sharded-aggregation
Closed

nihalzp wants to merge 30 commits into
ClickHouse:masterfrom
nihalzp:skew-robust-sharded-aggregation

Conversation

@nihalzp

@nihalzp nihalzp commented Jun 21, 2026

Copy link
Copy Markdown
Member

Builds on top of:

Changelog category (leave one):

  • Performance Improvement

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

Optimizes the sharded aggregation for skewed data. While sharding, hot keys (the few that dominate the data) are detected on the fly and spread across all threads instead of overloading a single shard, while the remaining cold keys are still sharded by hash and aggregated without a merge phase. This keeps GROUP BY parallel even on heavily skewed data or queries with very few distinct keys, which the initial sharded aggregation handled very poorly. Controlled by the same enable_sharding_aggregator setting which is disabled by default.

@clickhouse-gh

clickhouse-gh Bot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c6b4076]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug, targeted) FAIL
Logical error: Bad cast from type A to B (STID: 1499-1dc7) FAIL cidb
AST fuzzer (amd_debug, targeted, old_compatibility) FAIL
Logical error: Bad cast from type A to B (STID: 1499-1dc7) FAIL cidb
Stateless tests (amd_tsan, flaky check) FAIL
04417_sharded_aggregation_mixed_cardinality FAIL cidb
04417_sharded_aggregation_mixed_cardinality FAIL cidb
04417_sharded_aggregation_mixed_cardinality FAIL cidb
04417_sharded_aggregation_mixed_cardinality FAIL cidb
04417_sharded_aggregation_mixed_cardinality FAIL cidb
Too many test failures FAIL cidb
Stateless tests (amd_debug, flaky check) FAIL
04417_sharded_aggregation_mixed_cardinality FAIL cidb
04415_sharded_aggregation_hot_cold_injection FAIL cidb
04417_sharded_aggregation_mixed_cardinality FAIL cidb
04417_sharded_aggregation_mixed_cardinality FAIL cidb
04417_sharded_aggregation_mixed_cardinality FAIL cidb
Too many test failures FAIL cidb
Performance Comparison (amd_release, master_head, 1/6) FAIL Performance dashboard
agg_functions_argmin_argmax #0::old FAIL query history
agg_functions_argmin_argmax #0::new FAIL query history
agg_functions_argmin_argmax #1::old FAIL query history
agg_functions_argmin_argmax #1::new FAIL query history
agg_functions_argmin_argmax #2::old FAIL query history
agg_functions_argmin_argmax #2::new FAIL query history
agg_functions_argmin_argmax #4::old FAIL query history
agg_functions_argmin_argmax #4::new FAIL query history
agg_functions_argmin_argmax #5::old FAIL query history
agg_functions_argmin_argmax #5::new FAIL query history
30 more test cases not shown
Performance Comparison (amd_release, master_head, 2/6) FAIL Performance dashboard
group_by_sundy_li #0::old FAIL query history
group_by_sundy_li #0::new FAIL query history
group_by_sundy_li #1::old FAIL query history
group_by_sundy_li #1::new FAIL query history
group_by_sundy_li #2::old FAIL query history
group_by_sundy_li #2::new FAIL query history
jit_aggregate_functions #346::old FAIL query history
jit_aggregate_functions #346::new FAIL query history
jit_aggregate_functions #380::old FAIL query history
jit_aggregate_functions #380::new FAIL query history
16 more test cases not shown
Performance Comparison (amd_release, master_head, 3/6) FAIL Performance dashboard
agg_functions_min_max_any #1::old FAIL query history
agg_functions_min_max_any #1::new FAIL query history
agg_functions_min_max_any #2::old FAIL query history
agg_functions_min_max_any #2::new FAIL query history
agg_functions_min_max_any #15::old FAIL query history
agg_functions_min_max_any #15::new FAIL query history
agg_functions_min_max_any #16::old FAIL query history
agg_functions_min_max_any #16::new FAIL query history
agg_functions_min_max_any #29::old FAIL query history
agg_functions_min_max_any #29::new FAIL query history
46 more test cases not shown
Performance Comparison (amd_release, master_head, 4/6) FAIL Performance dashboard
codecs_int_insert #1::old FAIL query history
codecs_int_insert #1::new FAIL query history
groupby_onekey_nullable #4::old FAIL query history
groupby_onekey_nullable #4::new FAIL query history
groupby_onekey_nullable #6::old FAIL query history
groupby_onekey_nullable #6::new FAIL query history
window_functions #0::old FAIL query history
window_functions #0::new FAIL query history
window_functions #1::old FAIL query history
window_functions #1::new FAIL query history
Performance Comparison (amd_release, master_head, 5/6) FAIL Performance dashboard
group_by_multiple_strings #3::old FAIL query history
group_by_multiple_strings #3::new FAIL query history
group_by_multiple_strings #7::old FAIL query history
group_by_multiple_strings #7::new FAIL query history
group_by_multiple_strings #20::old FAIL query history
group_by_multiple_strings #20::new FAIL query history
group_by_multiple_strings #21::old FAIL query history
group_by_multiple_strings #21::new FAIL query history
group_by_multiple_strings #26::old FAIL query history
group_by_multiple_strings #26::new FAIL query history
18 more test cases not shown
Performance Comparison (amd_release, master_head, 6/6) FAIL Performance dashboard
cryptographic_hashes #18::old FAIL query history
cryptographic_hashes #18::new FAIL query history
json_type_parsing #0::old FAIL query history
json_type_parsing #0::new FAIL query history
json_type_parsing #1::old FAIL query history
json_type_parsing #1::new FAIL query history

AI Review

Summary

This PR makes sharded GROUP BY skew-aware and turns enable_sharding_aggregator on by default. The core skew design is plausible, and the PR has broad result-equality coverage plus benchmark evidence, but the current code still has rollout/compatibility and correctness gaps that should block merge.

PR Metadata
  • 💡 Changelog category as Performance Improvement is appropriate.
  • 💡 Changelog entry is required and present, but it is stale: it says enable_sharding_aggregator is disabled by default while src/Core/Settings.cpp now defaults it to true. If the default remains true, replace the last sentence with: "Controlled by enable_sharding_aggregator, enabled by default starting in 26.7; set it to 0 to restore the previous aggregation path."
Findings

❌ Blockers

  • [src/Core/Settings.cpp:4079] enable_sharding_aggregator is default-on while the sharded path still clears params.max_bytes_before_external_group_by, so queries that previously spilled under max_bytes_before_external_group_by / max_bytes_ratio_before_external_group_by can silently switch to an in-memory-only aggregation path. This changes resource-limit semantics for existing users. Existing thread: Skew-robust Sharded Aggregation #108067 (comment)
    Suggested fix: keep the default false, or make canUseShardedAggregation reject the sharded path whenever external aggregation is requested and update tests/changelog accordingly.
  • [src/Processors/Transforms/ColdShardAggregatingTransform.cpp:127] Full aggregate-projection reads set params.only_merge, but the new cold shard always uses executeOnBlock for its raw input. Projection input columns are already aggregate states, so this path can aggregate state columns as raw values or throw instead of using mergeOnBlock like AggregatingTransform does.
    Suggested fix: reject params.only_merge in canUseShardedAggregation, or make the cold raw-input branch call mergeOnBlock when params->params.only_merge is true.

⚠️ Majors

  • [src/Processors/Transforms/ColdShardAggregatingTransform.cpp:115] Rows consumed by the cold branch are not counted by rows_before_aggregation. QueryPipeline::initRowsBeforeAggregation attaches the counter only to AggregatingTransform and AggregatingInOrderTransform, so high-cardinality sharded queries can report 0 or count only hot/merge-path rows even though cold shards aggregated the main input.
    Suggested fix: wire the counter into ColdShardAggregatingTransform and add only non-merge raw input rows, or count once before sharding.
Tests
  • ⚠️ Add a regression test for sharded aggregation over a full aggregate projection (params.only_merge) with multiple projection streams.
  • ⚠️ Add a focused rows_before_aggregation = 1 test that compares the statistic with and without enable_sharding_aggregator.
  • ⚠️ Update the external aggregation coverage so the chosen contract is explicit: either sharded aggregation is not selected when external aggregation is requested, or spill semantics are preserved.
Final Verdict

Status: ❌ Block

Minimum required actions: fix the external aggregation/default-on contract, handle or reject params.only_merge, and restore exact rows_before_aggregation accounting for the sharded path.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jun 21, 2026
Comment thread src/Core/Settings.cpp
- [GROUP BY optimization](/sql-reference/statements/select/group-by#group-by-optimization-depending-on-table-sorting-key)
)", 0) \
DECLARE(Bool, enable_sharding_aggregator, false, R"(
DECLARE(Bool, enable_sharding_aggregator, true, R"(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Turning this on by default changes memory/rollout semantics for users who set external aggregation limits. The sharded path still unconditionally clears params.max_bytes_before_external_group_by in AggregatingStep::transformPipeline, and the existing tests assert that max_bytes_before_external_group_by / max_bytes_ratio_before_external_group_by are ignored on this path. With this default, a query that previously spilled can now choose sharded aggregation and run fully in memory unless the user knows to disable enable_sharding_aggregator.

Please either keep this default false, or make canUseShardedAggregation reject the path when external aggregation is requested, and update SettingsChangesHistory plus the changelog text if the default really changes.

@clickhouse-gh

clickhouse-gh Bot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🟢 AI verdict: improvement — 1 query(s) improved out of 38 analysed

This PR enables the sharded GROUP BY aggregator by default and reworks the sharded-aggregation pipeline (skew-aware hot/cold key routing). ClickBench Q4 is aggregation-bound, so the -18.25% improvement is directly on-path and plausible — it's a large, consistent shift confirmed by both tests. No regressions flagged; the verdict stands as an aggregation-path win.

clickbench

🟢 1 improved

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
🟢 4 improvement 263 215 -18.2% <0.0001 aggregation: On-path: PR flips enable_sharding_aggregator on by default + rewrites the sharded GROUP BY path; -18.25% fits.

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

🟢 No significant changes

Debug info
  • StressHouse run: aa8b1bb4-f732-4f90-a42b-10eccd4206fe
  • MIRAI run: 8d71053c-3de1-417d-8051-1cc01dadbef3
  • PR check IDs:
    • clickbench_548621_1782859504
    • clickbench_548632_1782859504
    • clickbench_548639_1782859504
    • tpch_adapted_1_official_548646_1782859504
    • tpch_adapted_1_official_548670_1782859504

Comment thread src/Core/Settings.cpp Resolved
@harikrishnan94 harikrishnan94 self-assigned this Jun 29, 2026
}
else
{
if (!params->aggregator.executeOnBlock(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ColdShardAggregatingTransform is now used for the raw side of sharded aggregation even when AggregatingStep::requestOnlyMergeForAggregateProjection has set params.only_merge for a full aggregate-projection read. In that mode the input columns are already aggregate states, and the normal AggregatingTransform switches to mergeOnBlock; this cold path always calls executeOnBlock, so a projection-backed query can feed AggregateFunction(...) state columns to ordinary aggregate functions or aggregate them as raw values. Please either make canUseShardedAggregation reject params.only_merge, or call mergeOnBlock here when params->params.only_merge is true.

{
if (has_pending)
{
const size_t num_rows = pending_chunk.getNumRows();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Rows consumed on this cold branch are invisible to rows_before_aggregation. QueryPipeline::initRowsBeforeAggregation only attaches the counter to AggregatingTransform and AggregatingInOrderTransform, so with sharded aggregation enabled the statistic counts only rows routed through the merge path and misses cold rows. For high-cardinality non-hot input that can report 0 even though this transform aggregated the whole query. Please wire the counter into ColdShardAggregatingTransform and add num_rows for non-merge input chunks, or count before the sharder so every source row is included exactly once.

@clickhouse-gh

clickhouse-gh Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.40% +0.00%
Functions 92.70% 92.60% -0.10%
Branches 77.60% 77.70% +0.10%

Changed lines: Changed C/C++ lines covered: 465/490 (94.90%) · Uncovered code

Full report · Diff report

@nihalzp

nihalzp commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

Architecturally, this algorithm cannot beat the baseline regardless of any missing optimizations.

@nihalzp nihalzp closed this Jul 22, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants