Repository navigation
Merge the lazy FINAL in-order deduplicating aggregation in batches, not one group at a time - #123326
Conversation
The deduplicating aggregation that lazy FINAL builds over the intersecting parts was constructed with aggregation_in_order_max_block_bytes = 0. The internal plan is optimized with the query's settings, so with optimize_aggregation_in_order = 1 it aggregates in order, and a byte budget of 0 makes AggregatingInOrderTransform and FinishAggregatingInOrderTransform flush at every key. Every group then became its own chunk and was merged separately by Aggregator::mergeBlocks: 25000 merges (and 50000 log lines) for the 35000-row reproducer from ClickHouse#114579, versus 3 for an ordinary in-order GROUP BY. Pass the query's aggregation_in_order_max_block_bytes, as every other AggregatingStep builder does. Results are unchanged; on a debug build the reproducer query goes from 0.80 s to 0.02 s and an 80000-row single-thread case from 11.9 s to 0.08 s. Closes ClickHouse#114579 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (1 nit).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261001-191000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261001-182600 |
|
Workflow [PR], commit [2b902f0] Summary: ✅
AI ReviewSummaryReviewed PR #123326 at Final VerdictStatus: ✅ Approve. |
|
📊 Cloud Performance Report ✅ AI verdict: This PR changes one argument in the lazy FINAL plan builder (LazyReadReplacingFinalSource), which now passes aggregation_in_order_max_block_bytes instead of 0. That code runs only for FINAL reads on ReplacingMergeTree with query_plan_optimize_lazy_final enabled, and no ClickBench or TPC-H query does that. The flagged ClickBench regressions, Q16 (+16.4%) and Q33 (+5.3%), are downgraded to not sure, and the suppressed Q17 (+13.1%) stays suppressed. All three are memory-heavy GROUP BY queries, and some source runs shared nodes with other benchmark runs and were noticeably noisier than base, so contention is the likely cause rather than this diff. If you want a real signal for this change, look at the new lazy_final_aggregation_in_order perf test. clickbenchFlagged queries (3 of 43)
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🟢 No significant changes Debug info
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 18 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 6b5a77cEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
146 of 146 check runs completed (110 passed, 35 not selected, 1 red: the shard above) and Session id: cron:our-pr-ci-monitor:20261002-003227 |
|
@groeneai, add a perf test. none of the existing shows speed ups. |
The deduplicating aggregation of lazy FINAL runs in order with optimize_aggregation_in_order = 1. The test reads 20000 filtered keys from two intersecting parts. Against the CI amd_release binaries under perf.py with the perf-comparison configs, master takes 0.45 s (70527 merges of partially aggregated blocks), the fix 0.02 s (2 merges). Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Added Locally under |
With a randomized read_in_order_two_level_merge_threshold below the fixture's 2 parts, the in-order read can merge both parts into a single stream. The deduplicating aggregation is then single-stream, finalizes in place and never calls Aggregator::mergeBlocks, so the test saw 0 merge lines and failed its lower bound (once in Stateless tests (amd_tsan, s3 storage, targeted)), with and without the fix. Clamp the setting to at least 2 with a Random settings limits line. With the clamp: 500/500 randomized runs pass on the fixed build, the unfixed build still fails 20/20 (10191..25000 merges). With the line disabled, 3/300 runs fail with the same "batched merges: 0" diff. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 0081b59Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The build failure dropped the 87 jobs that depend on it, so this head has no functional test results yet. The 8 jobs that ran passed: Session id: cron:our-pr-ci-monitor:20261009-033007 |
…-aggregation-in-order-batched-merges # Conflicts: # src/Processors/Sources/LazyReadReplacingFinalSource.cpp
…-aggregation-in-order-batched-merges Co-Authored-By: Claude Opus 5.5 <[email protected]>
7ccfda5
…the performance tests
The setup of the test asserts two active parts with
`throwIf(count() != 2) FROM system.parts`. The Iceberg configuration
rewrites its ReplacingMergeTree table to IcebergLocal/IcebergS3, which
has no rows in `system.parts`, so the setup fails with Code 395
("expected 2 active parts") and so does the whole Iceberg perf shard.
The test came with ClickHouse#123326, whose last CI run (2026-10-26) predates
ClickHouse#128261, which started running the Iceberg jobs on the tests a PR adds,
so it never ran in this configuration before it reached master.
The reason is the one of lazy_final_early_exit_limit.xml,
lazy_materialization_final_limit.xml and read_in_reverse_order_final.xml,
which have the same check over `system.parts` plus FINAL.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #114579
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Lazy
FINAL(query_plan_optimize_lazy_final) is much faster withoptimize_aggregation_in_order: its deduplicating aggregation now merges groups in blocks bounded byaggregation_in_order_max_block_bytes, like an ordinary in-orderGROUP BY, instead of one group at a time (25000 merges down to 3 on the reproducer from #114579). Closes #114579.Description
LazyReadReplacingFinalSource::buildPlanFromReadingStepbuilt the deduplicatingAggregatingStepwithaggregation_in_order_max_block_bytes = 0. The internal plan is optimized with the query's settings, so withoptimize_aggregation_in_order = 1it aggregates in order, and a budget of 0 makesAggregatingInOrderTransformandFinishAggregatingInOrderTransformflush at every key: oneAggregator::mergeBlockscall (two log lines) per group. Every otherAggregatingStepbuilder passes the query's setting; this one now does too. Results are unchanged.Debug build,
clickhouse local, query time, median of 3, logs off (merges counted attrace):optimize_aggregation_in_ordermax_threads = 4index_granularity = 256,max_threads = 1The new test counts the
Merging partially aggregated blockslines of the #114579 query: 25000 on master, 3 with the fix (it accepts 1 to 100).The performance test
lazy_final_aggregation_in_order.xmlreads 20000 filtered keys from two intersecting parts. Underperf.pywith the perf-comparison configs (max_threads = 12, trace log), on the CIamd_releasebinaries: master 0.45 s per run (70527 merges), the fix 0.02 s (2 merges).Workflow [PR]
Sync PR [sync-upstream/pr/123326]
Version info
26.11.1.1477-master(included in26.11and later)