Skip to content

Refactor Adaptive Aggregator - #120335

Closed
nihalzp wants to merge 31 commits into
ClickHouse:masterfrom
nihalzp:adaptive-aggregator-refactor
Closed

nihalzp wants to merge 31 commits into
ClickHouse:masterfrom
nihalzp:adaptive-aggregator-refactor

Conversation

@nihalzp

@nihalzp nihalzp commented Sep 16, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...


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

@nihalzp
nihalzp marked this pull request as draft September 16, 2026 08:23
@yariks5s yariks5s self-assigned this Sep 16, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1ca9ad2]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_tsan, flaky check) FAIL
Server liveness check failed FAIL cidb
ThreadSanitizer: data race (STID: 3030-3baa) FAIL cidb
Stateless tests (amd_msan, flaky check) FAIL
05145_adaptive_aggregation_input_port FAIL cidb
05145_adaptive_aggregation_input_port FAIL cidb
Stateless tests (amd_binary, flaky check) FAIL
05145_adaptive_aggregation_input_port FAIL cidb
05145_adaptive_aggregation_input_port FAIL cidb
05145_adaptive_aggregation_input_port FAIL cidb
Stateless tests (amd_tsan, parallel, selected tests) FAIL
Server liveness check failed FAIL cidb
ThreadSanitizer: data race (STID: 3030-3baa) FAIL cidb
Unit tests (asan_ubsan) FAIL
Unit tests (tsan) FAIL
Unit tests (msan) FAIL
Fast test (arm_darwin) DROPPED
Build (amd_darwin) DROPPED
Build (arm_darwin) DROPPED

AI Review

Summary

This PR factors adaptive aggregation into a dedicated staging pipeline and adds coverage around completion, spilling, and lifecycle edges. The direction looks reasonable, but the current branch still leaves an already-open pressure/backpressure correctness gap in the coalescing and publisher handshake, and two of the new regression tests do not yet give reliable proof for the refactor, so I would not merge it yet.

PR Metadata

Changelog category = Not for changelog looks correct for a refactor. Changelog entry is not required for that category, but leaving the template placeholder ... makes the body look unedited; delete it and use the description above the template to say what was refactored and why the new adaptive-aggregation lifecycle coverage was needed.

Findings

⚠️ Majors

  • [src/Processors/Transforms/AggregatingTransform.cpp:1324-1328; src/Processors/Transforms/AdaptiveAggregationCoalescingTransform.cpp:82-88] The producer resumes from staging_output->canPush(), but that is only downstream demand, not proof that publication finished. On an under-pressure all-hit block, the empty forwarded chunk can be acknowledged before the coalescer has flushed its buffered tail, and the re-sample at AdaptiveAggregationCoalescingTransform.cpp:88 can drop the producer's earlier pressure verdict if memory dipped after the forwarded columns were released. That leaves staged bytes invisible until another miss or end of input and weakens the max_bytes_before_external_group_by backpressure contract.
    Suggested fix: resume from an explicit publisher-side completion signal, and carry a latched force_flush bit from the producer into the coalescer instead of recomputing pressure there.
  • [tests/queries/0_stateless/05145_adaptive_aggregation_input_port.sql:56-58] The final log assertion groups rows by four fixed log_comment values over a 10-minute window. A retry on the same server within that window will fold the previous attempt's query_log and processors_profile_log rows into the same groups, so the counts no longer prove this run's input-port accounting.
    Suggested fix: anchor the query to a per-run watermark or a unique suffix instead of event_time >= now() - 600 together with LIKE 'adaptive_input_port_%'.
Tests
  • ⚠️ [tests/queries/0_stateless/05142_adaptive_staged_chunk_operations.sh:24-60] The new split-path regression exercises count() and a general aggregate payload, but not the zero-column GROUP BY k carrier. That carrier still goes through StagedChunk::cut, publication, and the params.aggregates_size == 0 drain fast path, so a split-specific bug there would not fail this test. Add a third key_only arm that forces AdaptiveAggregationStagedChunkSplits and verifies the exact key set survives.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions:

  1. Fix the publication and acknowledgement gap so producer pressure drains run only after the publisher has definitely registered every required chunk.
  2. Make 05145_adaptive_aggregation_input_port.sql isolate its own log rows.
  3. Extend the split-path regression coverage to the key-only carrier.

@clickhouse-gh clickhouse-gh Bot added the comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). label Sep 16, 2026
current_chunk.clear();
has_input = false;
const auto threshold = params->params.max_bytes_before_external_group_by;
flush_pending = !coalescer.empty() && threshold && getCurrentQueryMemoryUsage() > static_cast<Int64>(threshold);

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.

This re-checks current query memory instead of honoring the producer's earlier under_pressure decision. Aggregator::executeOnBlock forwards a block when under_pressure && adaptive->has_buffered_staging specifically so the coalescer flushes its hidden tail before resumeAdaptiveBlock runs the pressure drain. By the time this code runs the forwarded block's columns may already be released, so getCurrentQueryMemoryUsage() can fall back under the threshold and leave pending_chunks buffered. The producer has already cleared has_buffered_staging on that path, so the subsequent drain sees an empty backlog and those staged bytes stay invisible until another miss or end of input. That weakens max_bytes_before_external_group_by after transient spikes.\n\nPlease latch a force_flush bit from the producer-side pressure decision and flush pending chunks here whenever that bit is set, instead of re-sampling memory after acknowledgement.

block_size=65536
fi

for payload in count general; do

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.

This split-path regression only covers count() and a general aggregate payload. The refactor also changes the zero-column carrier used by GROUP BY k with no aggregates: it still goes through StagedChunk::cut, publishAdaptiveChunk, and the params.aggregates_size == 0 drain fast path, but none of the assertions here would fail if that shape dropped or misrouted keys under pressure splitting.

Please add a third key_only arm that forces AdaptiveAggregationStagedChunkSplits and verifies the exact key set survives.

'AdaptiveAggregationPublishTransform')
AND q.type = 'QueryFinish'
AND q.current_database = currentDatabase()
AND q.event_date >= yesterday() AND q.event_time >= now() - 600

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 final log assertion groups rows by four fixed log_comment values over a 10-minute sliding window. If this test is retried on the same server within that window, old query_log and processors_profile_log rows from the previous attempt will be folded into the same groups, so the counts stop proving this run's input-port accounting.

Please anchor the query to a per-run watermark or unique suffix instead of event_time >= now() - 600 together with LIKE 'adaptive_input_port_%'.

@clickhouse-gh

clickhouse-gh Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 1ca9ad297 with master 83a6d57ed (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

⚠️ Significant changes: object file sizes.

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.

Object file sizes ⚠️

17 object files changed (+2.11 MiB total), 8 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/AdaptiveAggregationKernels.cpp.o new 710.38 KiB +710.38 KiB
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/AdaptiveAggregationDrain.cpp.o new 547.28 KiB +547.28 KiB
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/AdaptiveAggregation.cpp.o new 257.90 KiB +257.90 KiB
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/AdaptiveAggregationStaging.cpp.o new 216.97 KiB +216.97 KiB
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/AdaptiveAggregationPressure.cpp.o new 165.23 KiB +165.23 KiB
src/CMakeFiles/dbms.dir/Processors/Transforms/AdaptiveAggregationCoalescingTransform.cpp.o new 91.15 KiB +91.15 KiB
src/CMakeFiles/dbms.dir/Processors/Transforms/AdaptiveAggregationPublishTransform.cpp.o new 84.35 KiB +84.35 KiB
src/CMakeFiles/dbms.dir/Processors/Transforms/AdaptiveAggregationPartitionTransform.cpp.o new 69.96 KiB +69.96 KiB
src/CMakeFiles/dbms.dir/Processors/Transforms/AggregatingTransform.cpp.o 521.87 KiB 568.27 KiB +46.40 KiB (+8.89%)
src/CMakeFiles/dbms.dir/Interpreters/Aggregator.cpp.o 20.34 MiB 20.31 MiB -35.04 KiB (-0.17%)

718 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

85 translation units recompiled, 737 s compile time in total, 77 of them have a recent master baseline.

Translation units without a recent master baseline:

  • src/Interpreters/AdaptiveAggregation/AdaptiveAggregationDrain.cpp: 7.3 s
  • src/Interpreters/AdaptiveAggregation/AdaptiveAggregationKernels.cpp: 7.1 s
  • src/Interpreters/AdaptiveAggregation/AdaptiveAggregation.cpp: 6.2 s
  • src/Processors/Transforms/AdaptiveAggregationPublishTransform.cpp: 5.4 s
  • src/Processors/Transforms/AdaptiveAggregationCoalescingTransform.cpp: 5.4 s
  • src/Interpreters/AdaptiveAggregation/AdaptiveAggregationPressure.cpp: 5.4 s
  • src/Processors/Transforms/AdaptiveAggregationPartitionTransform.cpp: 5.3 s
Symbol sizes
Binary Symbol Master PR Δ
programs/clickhouse-keeper DB::Aggregator::executeFrozen(std::__1::vector<COW<DB::IColumn>::immutable_ptr<DB::IColumn>, std::_… 166.81 KiB removed -166.81 KiB (-100.00%)
programs/clickhouse DB::Aggregator::executeFrozen(std::__1::vector<COW<DB::IColumn>::immutable_ptr<DB::IColumn>, std::_… 166.81 KiB removed -166.81 KiB (-100.00%)
programs/clickhouse DB::Aggregator::executeFrozen(unsigned long, unsigned long, DB::AggregatedDataVariants&, std::__1::… new 158.36 KiB +158.36 KiB
programs/clickhouse-keeper DB::Aggregator::executeFrozen(unsigned long, unsigned long, DB::AggregatedDataVariants&, std::__1::… new 158.36 KiB +158.36 KiB

Job report

@nihalzp

nihalzp commented Nov 1, 2026

Copy link
Copy Markdown
Member Author

The problems are addressed by

@nihalzp nihalzp closed this Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants