Skip to content

Disable enable_producing_buckets_out_of_order_in_aggregation by default - #112127

Closed
nihalzp wants to merge 6 commits into
ClickHouse:masterfrom
nihalzp:disable-out-of-order-buckets-in-aggregation
Closed

nihalzp wants to merge 6 commits into
ClickHouse:masterfrom
nihalzp:disable-out-of-order-buckets-in-aggregation

Conversation

@nihalzp

@nihalzp nihalzp commented Jul 27, 2026 •

Copy link
Copy Markdown
Member

Caused by: #80179
Related: #108727
Related: #111441

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

The setting enable_producing_buckets_out_of_order_in_aggregation (introduced in 25.9) is now disabled by default. When enabled, distributed queries using memory-efficient two-level aggregation (distributed_aggregation_memory_efficient, on by default) could return incorrect results: the initiator could finalize an aggregation bucket before all its out-of-order pieces arrived, producing duplicate rows for some GROUP BY keys. Set enable_producing_buckets_out_of_order_in_aggregation = 1 to restore the previous behavior.


With this setting enabled (the default since 25.9), a GROUP BY over a Distributed table can non-deterministically return several rows for the same key. GroupingAggregatedTransform on the initiator does not fully track buckets that arrive out of order: it can push a bucket with partial data and push the late remainder of the same bucket again, and in plans where no SortingAggregatedTransform follows (which would instead throw the logical error SortingAggregatedTransform already got bucket seen in #111441), the duplicate reaches the result silently.

Reproducer: https://fiddle.clickhouse.com/3833ebc5-f659-4162-b8a3-369dc51011dc

This disables the optimization by default until the underlying bug is fixed (an attempt in #108727). Only the session setting's default is flipped; the QueryPlanSerializationSettings default is intentionally left as is, because serialized plans omit values equal to the default, and changing it would make older readers of new plans assume the buggy behavior is on. A SettingsChangesHistory entry is added under 26.8 so compatibility handles the flip. The change should be backported to every release that has the setting (25.9+).

cc: @alexey-milovidov @rschu1ze @mlex @nickitat

@nihalzp nihalzp added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jul 27, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [971683f]

Summary: ❌

job_name test_name status info comment
Fast test (arm_darwin) FAIL
03008_deduplication_diagnostics_opt_in FAIL cidb

AI Review

Summary

This PR flips the default of enable_producing_buckets_out_of_order_in_aggregation to 0, records the flip in SettingsChangesHistory, and adds a stateless regression for the local session default and compatibility behavior. I found one blocker: the secondary-query transport still does not force this setting onto remote shards, so the fix is incomplete for mixed-version Distributed queries on the default serialize_query_plan = 0 path.

Findings

❌ Blockers

  • [src/Core/Settings.cpp:8184] The new local default does not propagate to older remote shards unless the user explicitly sets the setting. BaseSettings only serializes changed settings, and the secondary-query senders only force-add enable_packed_string_keys_in_aggregation in src/Client/MultiplexedConnections.cpp:192-197 and src/Client/HedgedConnections.cpp:230-235. On a mixed 26.8/25.9 cluster, an initiator at the new default 0 still leaves older shards at their default 1, so Distributed GROUP BY can continue to emit out-of-order buckets and return incorrect results during rolling upgrades. Suggested fix: force-send enable_producing_buckets_out_of_order_in_aggregation with secondary queries the same way enable_packed_string_keys_in_aggregation is forced today, or add an equivalent compatibility fence that disables the optimization whenever agreement cannot be guaranteed.
Final Verdict
  • Status: ❌ Block
  • Minimum required actions: make the effective enable_producing_buckets_out_of_order_in_aggregation value reach secondary queries on mixed-version clusters, and add focused coverage for that propagation path.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.60% 86.60% +0.00%
Functions 91.90% 91.80% -0.10%
Branches 78.80% 78.90% +0.10%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 27, 2026
@scanhex12 scanhex12 self-assigned this Jul 27, 2026
Comment thread src/Core/Settings.cpp Resolved
Comment thread src/Core/SettingsChangesHistory.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

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

Full report · Diff report

@nickitat

Copy link
Copy Markdown
Member

I'm trying to implement a complete fix in #112794

alexey-milovidov and others added 2 commits August 7, 2026 22:20
…kets_out_of_order_in_aggregation`

Address review: verify that fresh sessions see `0` and that
`SET compatibility = '26.7'` restores the previous default `1` via the
`SettingsChangesHistory` entry under 26.8.

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/Core/Settings.cpp
DECLARE(Float, min_os_cpu_wait_time_ratio_to_throw, 0.0, "Min ratio between OS CPU wait (OSCPUWaitMicroseconds metric) and busy (OSCPUVirtualTimeMicroseconds metric) times to consider rejecting queries. Linear interpolation between min and max ratio is used to calculate the probability, the probability is 0 at this point.", 0) \
DECLARE(Float, max_os_cpu_wait_time_ratio_to_throw, 0.0, "Max ratio between OS CPU wait (OSCPUWaitMicroseconds metric) and busy (OSCPUVirtualTimeMicroseconds metric) times to consider rejecting queries. Linear interpolation between min and max ratio is used to calculate the probability, the probability is 1 at this point.", 0) \
DECLARE(Bool, enable_producing_buckets_out_of_order_in_aggregation, true, R"(
DECLARE(Bool, enable_producing_buckets_out_of_order_in_aggregation, false, R"(

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.

Flipping the compiled default here does not actually disable this optimization on the default secondary-query path. BaseSettings only serializes changed settings to remote servers, and MultiplexedConnections::sendQuery / HedgedConnections::sendQuery currently force-add only enable_packed_string_keys_in_aggregation, not this setting. On a mixed 26.8/25.9 cluster, an initiator that leaves enable_producing_buckets_out_of_order_in_aggregation at its new default 0 will therefore send nothing to older shards when serialize_query_plan = 0; those shards keep their own default 1, can still emit out-of-order buckets, and the distributed GROUP BY is still exposed to the wrong-result bug during rolling upgrades. This needs the same force-send treatment as enable_packed_string_keys_in_aggregation (or another cross-version fence) before the default flip is actually safe cluster-wide.

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 test, added by AI, is kind of pointless.
We can remove it and no bad things will happen.

@rschu1ze rschu1ze added the hold Skip for auto-updating. label Aug 10, 2026
@nickitat

Copy link
Copy Markdown
Member

Closing in favor of #112794

@nickitat nickitat closed this Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hold Skip for auto-updating. pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants