Skip to content

Keep the bucket-order promise of a shard aggregation in the Cascades optimizer - #119814

Merged
alexey-milovidov merged 8 commits into
masterfrom
fix-cascades-shard-partial-aggregation-bucket-order
Sep 25, 2026
Merged

alexey-milovidov merged 8 commits into
masterfrom
fix-cascades-shard-partial-aggregation-bucket-order

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

Related: #115986
Related: #116446

A shard of a plain distributed query plans its aggregation to the WithMergeableState stage, and with distributed_aggregation_memory_efficient or enable_memory_bound_merging_of_aggregation_results (both on by default) that AggregatingStep promises the initiator's merge to deliver its two-level buckets in order. The initiator reads each shard as one stream. With enable_cascades_optimizer the Cascades AggregationImplementation rule offered the non-final aggregation only at the full node count, and the DistributionEnforcer gathered the instances straight into that stream. Each instance orders only its own share, so the initiator saw a bucket twice and failed with Logical error: 'GroupingAggregatedTransform pushed bucket 3 twice'.

The stress test runs its queries with enable_cascades_optimizer = 1, which is how test 04878 (added by #116446 for the rule-based rewrite) hit this on master: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=64bddd33323e3583dec578d27135bbb6e5359675&name_0=MasterCI&name_1=Stress%20test%20%28arm_release%29

Now a non-final aggregation that promises bucket order, and is not the partial half of a two-stage split, runs on a single node, and TwoStageAggregationTransformation splits such a step too, with a non-final merge that restores the bucket order over the gathered partials - the same demotion the rule-based rewrite got in #116446. The partial half of a split is marked on its GroupExpression, so it keeps its multi-node variants and is not split again. An aggregation in order stays on a single node, as in the rule-based planner. A shard plan without the promise is unchanged.

Test 05210 pins the plan shape and the results under the Cascades optimizer. Test 04878 reads the plan of the rule-based rewrite, so it now turns the Cascades optimizer off explicitly.

Changelog category (leave one):

  • Improvement

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

Fix the logical error GroupingAggregatedTransform pushed bucket N twice on the initiator of a distributed query whose shards planned their aggregation with the experimental enable_cascades_optimizer: a shard's partial aggregation that must deliver its buckets in order no longer runs on several nodes gathered into one stream, it is split into a partial aggregation and a merge instead.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/119814]

Version info

  • Merged into: 26.10.1.650-master (included in 26.10 and later)
  • Backported to: 26.9.26.29

…optimizer

A shard of a plain distributed query plans its aggregation to the `WithMergeableState` stage, and with `distributed_aggregation_memory_efficient` or `enable_memory_bound_merging_of_aggregation_results` that `AggregatingStep` promises the initiator's merge to deliver its two-level buckets in order. The initiator reads each shard as one stream. With `enable_cascades_optimizer` (the stress test turns it on) the Cascades `AggregationImplementation` rule offered the non-final aggregation only at the full node count, and the `DistributionEnforcer` gathered the instances straight into that stream. Each instance orders only its own share, so the initiator saw a bucket twice and failed with `Logical error: 'GroupingAggregatedTransform pushed bucket 3 twice'` (test 04878 under the stress runner's client options).

Now a non-final aggregation that promises bucket order, and is not the partial half of a two-stage split, runs on a single node, and `TwoStageAggregationTransformation` splits such a step too, with a non-final merge that restores the bucket order over the gathered partials - the same demotion the rule-based rewrite got in #116446. The partial half of a split is marked on its `GroupExpression`, so it keeps its multi-node variants and is not split again. An aggregation in order stays on a single node, as in the rule-based planner.

Test 05210 pins the plan shape and the results under the Cascades optimizer. Test 04878 reads the plan of the rule-based rewrite, so it now turns the Cascades optimizer off explicitly.

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

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bc3814d]

Summary: ✅


AI Review

Summary

This PR teaches the Cascades distributed-aggregation rules to preserve the bucket-order contract for shard-side non-final aggregations, and it backs that up with focused stateless coverage for the affected setting combinations and execution modes. I re-checked the current diff against the existing review threads and the surrounding planner code, and I did not find a remaining correctness, safety, or evidence gap.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit bc3814d.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.80% +0.10%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 44/44 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Sep 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bc3814d14 with master f90a58a8c (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

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

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

Job report

The flaky check runs `nproc - 1` copies of a changed test at once with the
thread fuzzer on, and each query here spawns nine server-side pipelines, so
`max_threads = 16` reached 185s against the 180s per-test limit. Four threads
still flush a producer's two-level states in parallel, and the producers the
merge interleaves come from `distributed_plan_workers_num` regardless.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119814&sha=7b236cf379b03e629816446892c9d2fd401aab95&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

@Algunenano Algunenano 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.

The explanation makes sense to me, but I think it's better if @davenger has a look at the recent changes including this PR

alexey-milovidov and others added 2 commits September 19, 2026 14:58
`split_for_bucket_order` already implied `!getFinal()`; scope it inside the
non-final branch instead of repeating the condition.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (head 81e74e9). The only red, Upgrade check (amd_release) with Transactions require Keeper to advertise ... feature flags on table attach, is unrelated to this PR: the stress and upgrade runners randomized Keeper feature flags that transactions need, fixed on master by #120932, which this merge pulls in. The two remaining review threads are addressed: the repeated getFinal check was scoped earlier, and the changelog category is now Improvement since the change only touches the experimental enable_cascades_optimizer path.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 25, 2026
Merged via the queue into master with commit f963ba3 Sep 25, 2026
342 of 343 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-cascades-shard-partial-aggregation-bucket-order branch September 25, 2026 02:51
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 25, 2026
@alexey-milovidov alexey-milovidov mentioned this pull request Sep 25, 2026
1 task done
pull Bot pushed a commit to TCeason/ClickHouse that referenced this pull request Sep 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

Could this PR get v26.9-must-backport? 26.9 does not have it, and its Stress tests keep aborting on the route it fixes. All three 26.9 occurrences of STID 3994-4c1d in the last 30 days are the 04878 query that starts with -- The aggregation must complete, running with enable_cascades_optimizer = true, make_distributed_plan = true and distributed_plan_force_shuffle_aggregation = true:

No master run has hit it since this PR merged, and 26.10 already includes it. Cherry-picked onto the 26.9 tip 356bd56f736f, git merge-tree reports no conflict: GroupExpression.h and AggregationImplementations.cpp on 26.9 are identical to master's version right before this PR. 26.8 has the same pre-fix files, but its stress runner never turns enable_cascades_optimizer on, so it has no occurrences.

@PedroTadim

Copy link
Copy Markdown
Member

Backporting to fix CI

@robot-clickhouse robot-clickhouse added pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Oct 29, 2026
PedroTadim added a commit that referenced this pull request Oct 29, 2026
Backport #119814 to 26.9: Keep the bucket-order promise of a shard aggregation in the Cascades optimizer
leshikus pushed a commit that referenced this pull request Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.9-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants