Repository navigation
Use detachColumns instead of getColumns in Aggregator merge path - #102330
Conversation
PR ClickHouse#99228 replaced `Block` with `Chunk` in the aggregation merge path but used `getColumns` (shared copy) instead of `detachColumns` (move). This keeps aggregate states alive across all buckets during parallel merge instead of freeing them incrementally, increasing memory pressure and concurrent arena operations. Restore immediate ownership transfer to match the old `std::move(block)` semantics. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Workflow [PR], commit [8f7f834] Summary: ✅
AI ReviewSummaryThis PR replaces Final VerdictStatus: ✅ Approve |
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is a great finding! But could you please provide some examples of the effect of this change?
|
To clarify my own question above — the effect is on peak memory and arena pressure during the parallel two-level merge. Mechanism
This restores the Reproducer -- forces two-level (5M keys) with ~1 KiB state per key
SELECT k, length(groupArray(s)) AS n
FROM
(
SELECT (number % 5000000) AS k, repeat('x', 100) AS s
FROM numbers(50000000)
)
GROUP BY k
FORMAT Null
SETTINGS max_threads = 8, group_by_two_level_threshold = 1;The peak |
|
@alexey-milovidov thank you for the explanation! |
…scope Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.
Reviewed by Cursor Bugbot for commit fa41fae. Configure here.
Workflow files in this branch were stale or had been deleted as a workaround for GitHub's workflow-scope check. Restoring them to match `master` so the PR diff no longer contains those unrelated changes.
|
Here is a concrete example of the effect. I built two binaries from current SELECT k, groupArray(v) AS a, groupArray(s) AS b
FROM remote('127.0.0.{1,2,3,4,5,6,7,8}:PORT', view(
SELECT (cityHash64(number) % 400000) AS k, number AS v, toString(number) AS s
FROM numbers_mt(8000000)
))
GROUP BY k
FORMAT Null
SETTINGS distributed_aggregation_memory_efficient = 0,
max_threads = 8,
group_by_two_level_threshold = 1,
group_by_two_level_threshold_bytes = 1;Peak
That is about 30% lower peak memory (~2.4 GiB saved) for this query. The reason: with |
|
I measured the effect of this change. I built two binaries from the current Workload — a two-level distributed aggregation whose final merge goes through SELECT k, groupArray(s) AS ga
FROM remote('127.0.0.{1,2,3,4}', view(
SELECT number % 200000 AS k, repeat(toString(number), 3) AS s
FROM numbers_mt(8000000)))
GROUP BY k
FORMAT Null
SETTINGS group_by_two_level_threshold = 1, group_by_two_level_threshold_bytes = 1,
distributed_aggregation_memory_efficient = 0, max_threads = 8;
That is about 18% lower peak memory (~787 MiB) on the merging side, and the Why: with |
|
Merged current I re-checked the three call sites after the merge:
Pushed as |
|
📊 Cloud Performance Report 🟢 AI verdict: This PR replaces column copies with detaches in the aggregate block-merge code, avoiding copy-on-write copies during the merge step. The three flagged ClickBench improvements (Q4 -17.5%, Q15 -16.8%, Q28 -5.8%) are all aggregation queries that run exactly that path, so the gains are directly attributable to the change rather than noise. Q4 and Q15 are large, clean deltas confirmed by both tests; Q28 is smaller but consistent. No regressions were observed. clickbench🟢 3 improved Flagged queries (3 of 43)
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
|
…rg::alter PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to declare last_version and compression_method outside an if/else that assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables flags the bare declarations and Build (arm_tidy) is built with -warnings-as-errors, so the build fails for every PR whose CI ran on master after this commit. CIDB shows 30+ unrelated PRs hitting this in the last 2 days (e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102, ClickHouse#106590). Initialize both variables to safe defaults at declaration. They are unconditionally overwritten in both if and else branches before use, so behavior is unchanged. The new defaults are only relevant if a future code path skips both assignments, in which case last_version=0 and compression_method=CompressionMethod::None are sane no-op values (the same defaults the old structured-binding form would produce through default-construction of the destructured aggregate). Co-Authored-By: Claude Opus 4.8 <[email protected]> (cherry picked from commit d72cac5)
|
Merged current `master` (branch was 309 commits behind). The Iceberg `Mutations.cpp` init-vars fix is now identical to `master`, so the net diff is back to exactly the three `getColumns()` → `detachColumns()` lines in `Aggregator::mergeBlocks`. CI was green. Addressed the remaining review thread: changed the changelog category from Pushed as `8f7f834c7d3`. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 14/16 (87.50%) | Lost baseline coverage: none · Uncovered code |
Backport #102330 to 26.4: Use detachColumns instead of getColumns in Aggregator merge path
…olumns in Aggregator merge path

The issue was found during bisecting for another issue with an agent and needs to be confirmed.
PR #99228 replaced
BlockwithChunkin the aggregation merge path but usedgetColumns(shared copy) instead ofdetachColumns(move). This keeps aggregate states alive across all buckets during parallel merge instead of freeing them incrementally, increasing memory pressure and concurrent arena operations.Restore immediate ownership transfer to match the old
std::move(block)semantics.Measured effect: about 18–30% lower peak memory on the gathered (non-memory-efficient) two-level merge path through
Aggregator::mergeBlocks, depending on the size of the per-bucket aggregate states. See the discussion below for the workloads andsystem.query_logmeasurements.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reduced peak memory usage when merging partial two-level aggregation results with large aggregate states (e.g.
groupArray), by freeing each bucket's source states incrementally during the merge instead of keeping them all alive until the merge completes.Note
Medium Risk
Touches the aggregation merge hot path and changes column ownership semantics (
getColumns->detachColumns), which could impact memory lifetime expectations or expose hidden reuse assumptions under concurrency.Overview
Restores move-style ownership transfer of
Chunkcolumns during aggregation merges by replacingChunk::getColumns()withChunk::detachColumns()inAggregator::mergeBlocks(two-level buckets, single-level/unknown bucket, and the chunks-based merge).This allows aggregate state columns to be released incrementally during parallel bucket merges instead of remaining shared/alive across buckets, reducing memory pressure and concurrent arena contention.
Reviewed by Cursor Bugbot for commit a75f1db. Bugbot is set up for automated code reviews on this repo. Configure here.
Version info
26.6.1.618(included in26.6and later)26.4.5.100