Skip to content

Add query_plan_aggregation_bucket_top_k - #116124

Merged
nihalzp merged 2 commits into
ClickHouse:masterfrom
nihalzp:query-plan-aggregation-bucket-top-k
Aug 25, 2026
Merged

nihalzp merged 2 commits into
ClickHouse:masterfrom
nihalzp:query-plan-aggregation-bucket-top-k

Conversation

@nihalzp

@nihalzp nihalzp commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

Related: #115755

The bucket top-K plan optimization (materializing only each two-level bucket's best n groups when a final aggregation feeds ORDER BY over its outputs with LIMIT n and the per-bucket selection is provably exact) currently runs unconditionally under query_plan_enable_optimizations. This PR gives it a dedicated setting, query_plan_aggregation_bucket_top_k (enabled by default), makes the pushed flag visible in EXPLAIN output (Bucket top-K: n ascending/descending), and adds a test covering the shapes where the rewrite must and must not fire plus an exactness pair. The toggle was split out of #115755 per review.

Changelog category (leave one):

  • Improvement

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

Adds setting query_plan_aggregation_bucket_top_k (enabled by default) to toggle the plan optimization that materializes only each two-level bucket's best n groups when a final aggregation feeds ORDER BY over its outputs with LIMIT n. The optimization is now also visible in EXPLAIN output.


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

Version info

  • Backported to: 26.8.1.1998

@clickhouse-gh

clickhouse-gh Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [09c140b]

Summary: ✅


AI Review

Summary

This PR splits the bucket top-K rewrite under its own query_plan_aggregation_bucket_top_k setting, wires the default into compatibility, and exposes the pushed hint in EXPLAIN. The implementation looks consistent, but the added regression coverage still misses the new EXPLAIN json=1 surface, so I would keep the review open for one more test.

Tests
  • ⚠️ [tests/queries/0_stateless/04911_query_plan_aggregation_bucket_top_k.sql:15] Add a focused EXPLAIN json=1, actions=1 assertion for the Bucket Top-K object and its Ascending field. The current test only checks the text plan, so a regression in AggregatingStep::describeActions(JSONBuilder::JSONMap &) would still pass; that exact gap already existed in the first commit of this PR.
Final Verdict

Changes requested: add JSON EXPLAIN coverage for the new Bucket Top-K output.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.00% 87.00% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 31/36 (86.11%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 24, 2026
@yariks5s yariks5s self-assigned this Aug 24, 2026
SET query_plan_aggregation_bucket_top_k = 1;

-- The matching shape carries the flag.
SELECT count() FROM (EXPLAIN actions = 1 SELECT number % 1000 AS k, count() AS c FROM numbers(100000) GROUP BY k ORDER BY c DESC LIMIT 10)

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.

The new stateless coverage still exercises only EXPLAIN actions = 1, but this PR now changes EXPLAIN json = 1 as well. That means AggregatingStep::describeActions(JSONBuilder::JSONMap &) can regress without tripping CI; that is exactly what happened between the first and second commits here. Please add a focused JSON assertion for the Bucket Top-K object and its Ascending field.

@clickhouse-gh

clickhouse-gh Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 09c140b29 with master ef23be3ee (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 709.21 MiB 706.17 MiB -3.04 MiB (-0.43%)

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

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

Job report

@nihalzp
nihalzp added this pull request to the merge queue Aug 24, 2026
Merged via the queue into ClickHouse:master with commit 0285ce0 Aug 25, 2026
178 checks passed
@nihalzp
nihalzp deleted the query-plan-aggregation-bucket-top-k branch August 25, 2026 00:00
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 25, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 25, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 25, 2026
Backport #116124 to 26.8: Add `query_plan_aggregation_bucket_top_k`
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 26, 2026
alexey-milovidov added a commit that referenced this pull request Aug 27, 2026
… threshold merge serves `count` with riders

Master's #116124 put the bucket-local selection under `query_plan_aggregation_bucket_top_k`;
the shared plan pass now runs under the disjunction of that setting and
`enable_aggregation_top_k_threshold_merge`, and each optimization checks its own flag, so
disabling one no longer silently disables the other.

Also, per review: the lone-count selection (`bucket_top_k`) no longer preempts the threshold
merge when other aggregates ride along - there the merge is the better deal, since it also
skips merging the losers' other states. The selection still serves the lone `count` and the
shapes the merge does not (e.g. the ascending order).

Co-Authored-By: Claude Fable 5 <[email protected]>
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants