Skip to content

Fix wrong results for duplicate-ALIAS GROUP BY on a Distributed table under two-level aggregation - #108855

Merged
alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
groeneai:fix-distributed-alias-unmerged-groups
Aug 18, 2026
Merged

alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
groeneai:fix-distributed-alias-unmerged-groups

Conversation

@groeneai

@groeneai groeneai commented Jun 29, 2026 •

Copy link
Copy Markdown
Collaborator

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):

Fix wrong results for GROUP BY on a Distributed table over duplicate ALIAS columns that expand to the same expression (e.g. two columns both defined as toString(x)) under two-level / memory-efficient distributed aggregation. The duplicate keys were collapsed into one on the shards but merged by the full key set on the initiator, so under two-level aggregation equal groups from different shards landed in different buckets and were not merged, returning a group once per shard with a split count().

Description

Closes: #112987

Continuation of the duplicate-ALIAS-on-Distributed family (issue #106403, PR #107913, PR #108725). Those PRs fixed NUMBER_OF_COLUMNS_DOESNT_MATCH in the shard query-tree reconstruction. This PR fixes a distinct, silent wrong-results variant in the aggregation-state merge that those changes did not touch (it reproduces on a build that already contains them).

Root cause. For SELECT a1, a2, count() FROM dist GROUP BY a1, a2 where a1 and a2 are ALIAS columns both equal to toString(x), the shard query tree inlines a1/a2 to the same expression and deduplicates the GROUP BY to a single key, so the shard aggregates and computes its two-level bucket numbers from one key. On the initiator the columns stay distinct, so MergingAggregated was configured with two keys. Two-level bucket numbers are hash(keys) % 256, and hashing one key versus two identical keys yields different buckets. When the per-shard partial states mix single-level and two-level blocks (shards with different data sizes straddling group_by_two_level_threshold, or sanitizer/concurrency timing), the initiator re-buckets single-level blocks by two keys while the shards' two-level blocks were bucketed by one key. Equal groups land in different buckets and are never merged — one logical group is returned once per shard with its count() split.

Fix. The initiator now merges (and buckets) by only the representative key columns, matching the shard's collapsed key set:

  • buildShardCollapseFanOut additionally reports which reconstructed columns are fan-out duplicates of an earlier column (several expected columns mapping onto the same shard column), propagated through JoinTreeQueryPlan.
  • addMergingAggregatedStep drops the duplicate key columns before MergingAggregated (so the merge sees the shard's single-key layout) and reconstructs them after the merge as aliases of their representative, leaving the output header unchanged.

Affects both the memory-efficient and the simple Aggregator::mergeBlocks merge paths. A deterministic regression test (asymmetric per-shard data forcing the single/two-level mix) and a settings-pinned variant on the existing symmetric cluster (the form originally surfaced by the sanitizer flaky-check) are added to 04357_distributed_alias_same_expression.

A deterministic local reproduction (two localhost shards reading tables of different sizes, group_by_two_level_threshold = 50, prefer_localhost_replica = 0): a query with 1000 distinct groups returned 1010 rows before the fix (10 shared groups split across shards) and the correct 1000 rows after.

Version info

  • Merged into: 26.8.1.1660 (included in 26.8 and later)

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Two localhost shards reading tables of different sizes (1000 vs 10 distinct groups) over a 2-shard cluster, with group_by_two_level_threshold = 50, group_by_two_level_threshold_bytes = 100000000, prefer_localhost_replica = 0: SELECT a1, a2, count() c FROM dist GROUP BY a1, a2 returns 1010 rows (10 shared groups split, count() halved) instead of 1000. Reproduces on both distributed_aggregation_memory_efficient 0 and 1.
b Root cause explained? The shard inlines a1/a2 to the same expression toString(x) and deduplicates GROUP BY to one key, computing two-level bucket numbers from one key. The initiator keeps the columns distinct, so MergingAggregated was configured with two keys. Two-level bucket = hash(keys) % 256; hashing one key vs two identical keys gives different buckets. When per-shard partials mix single-level and two-level blocks, the initiator re-buckets single-level blocks by two keys while the shards' two-level blocks were bucketed by one key, so equal groups land in different buckets and are never merged.
c Fix matches root cause? Yes. The initiator now merges and buckets by only the representative key columns, matching the shard's collapsed key set. It is not a band-aid: it makes the two sides' bucketing consistent, which is the exact mechanism of the bug.
d Test intent preserved / new tests added? New tests added to 04357_distributed_alias_same_expression (deterministic asymmetric repro + settings-pinned symmetric variant). No existing assertion weakened.
e Both directions demonstrated? Yes. Pre-fix: 1010 rows / 0 merged shared groups. Post-fix: 1000 rows / 10 merged shared groups (count 2). Verified for both merge paths.
f Fix is general across code paths? Yes. Fixes both the memory-efficient merge (MergingAggregatedMemoryEfficientTransform) and the simple Aggregator::mergeBlocks path, since both honor the incoming two-level bucket numbers. Handles N-way duplicate collapse (verified with 3 identical aliases) and multiple independent duplicate groups.
g Fix generalizes across inputs? Yes. Verified across group counts, key order (duplicate-first), a distinct key interleaved between duplicates (GROUP BY a1, x, a2), two interleaved duplicate-pairs, HAVING, and aggregate-over-expression. The dedup is driven by the shard-collapse mapping, independent of datatype.
h Backward compatible? Yes. Pure query-planner change; no setting default, on-disk/wire format, or validation change. It only corrects wrong results, so old behavior is not something to preserve.
i Invariants and contracts preserved? Yes. The merge's positional key layout (keys at positions 0..keys_size-1, then aggregate states) is preserved: the pre-merge drop step keeps representative keys in order and removes only duplicates; aggregates are matched by name. The post-merge reconstruction restores the expected output header. Verified via EXPLAIN and value checks for interleaved/duplicate-first key orders.

Session id: cron:clickhouse-worker-slot-0:20260629-190500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @yakov-olkhovskiy @KochetovNicolai — could you review this? It's a follow-up to the duplicate-ALIAS-on-Distributed family (#107913 / #108725) fixing a distinct silent wrong-results variant: under two-level distributed aggregation the shard collapses the duplicate GROUP BY keys to one and buckets by one key, but the initiator merged by two, so equal groups from different shards landed in different two-level buckets and were never merged. The fix makes the initiator merge by only the representative key set and reconstructs the duplicates after the merge.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [46155a3]

Summary: ✅


AI Review

Summary

This PR fixes the duplicate-ALIAS distributed two-level aggregation mismatch by merging on the shard's representative key set and reconstructing the dropped keys afterwards, and the plain GROUP BY / ROLLUP / CUBE path looks sound. I still see one new wrong-results gap in how the collapse map is propagated through composed plans, plus the still-open GROUPING SETS behavior regression, so this is not ready to merge as-is.

Findings

❌ Blockers

  • [src/Planner/PlannerJoinTree.cpp:2336] shard_collapse_duplicate_keys is only attached to the leaf JoinTreeQueryPlan. The wrappers that rebuild a new JoinTreeQueryPlan above it, joinPlansWithStep and buildQueryPlanForArrayJoinNode, never carry that field forward, so any JOIN / CROSS JOIN / ARRAY JOIN between the distributed read and the outer aggregation drops the collapse map and falls back to the old full-key merge. Queries that route the duplicate ALIAS keys through one of those carriers can still return split counts under two-level aggregation. Propagate or merge the map through those wrappers and add a regression with a join-shaped carrier.

⚠️ Majors

  • [src/Planner/Planner.cpp:824] The GROUPING SETS fail-close gate still keys only on the raw group_by_two_level_threshold* settings, not on whether the execution pipeline can actually build two-level states. AggregatingStep::transformPipeline clears both thresholds whenever the pipeline is single-stream and not spilling, so some queries that would execute correctly on the single-level path now raise NOT_IMPLEMENTED only because the settings are non-zero. I would prefer the exact merge-side check here rather than shipping the conservative reject.
Final Verdict

Request changes. The main merge fix is on the right track, but the duplicate-key collapse must survive plan composition, and the GROUPING SETS fallback still regresses some safe queries.

LLVM Coverage Report

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

Changed lines: Changed C/C++ lines covered: 113/115 (98.26%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 29, 2026
Comment thread src/Planner/Planner.cpp Resolved
… under two-level aggregation

When a Distributed table has several ALIAS columns expanding to the same expression
(e.g. a1, a2 both = toString(x)) and they are used as GROUP BY keys, the shard inlines
them into one expression and deduplicates the GROUP BY to a single key before computing
its two-level aggregation bucket numbers. The initiator, however, keeps the columns
distinct and merged the partial aggregation states by the full (duplicated) key set.

Two-level bucket numbers are computed from a hash of the key columns, so hashing one key
and hashing two identical keys give different buckets. When the per-shard partial states
mix single-level and two-level blocks (e.g. shards with different data sizes straddling
group_by_two_level_threshold, or under sanitizer/concurrency timing), the initiator
re-buckets single-level blocks by two keys while two-level blocks from the shard were
bucketed by one key. Equal groups then land in different buckets and are never merged, so
one logical group is returned once per shard with its count split, instead of one merged
row.

The initiator now merges (and buckets) by only the representative key columns, matching
the shard's collapsed key set: buildShardCollapseFanOut reports which reconstructed
columns are duplicates of an earlier one, and addMergingAggregatedStep drops the duplicate
keys before MergingAggregated and reconstructs them after the merge.

The post-merge reconstruction emits the canonical aggregated layout (all original GROUP BY
keys first, in their declared order, then the aggregate-state columns) rather than appending
the dropped duplicate keys at the end. WITH ROLLUP / WITH CUBE reach this path with an empty
grouping_sets_parameters_list, and the following RollupStep / CubeStep read the merged block
positionally (the first keys_size columns as keys, the rest as aggregate states). Appending
the duplicates at the end made the count() state be read as a key and a reconstructed key be
read as the aggregate state, throwing 'Bad cast ... to ColumnAggregateFunction' or producing
wrong rollup/cube totals; emitting the canonical layout keeps that path correct.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai
groeneai force-pushed the fix-distributed-alias-unmerged-groups branch from 09a8892 to a67a794 Compare June 30, 2026 00:07
Comment thread src/Planner/Planner.cpp Resolved
…-ALIAS two-level merge fix

The existing 04357_distributed_alias_same_expression regression lines for this
wrong-results variant pin group_by_two_level_threshold = 1, which forces BOTH
localhost shards to two-level aggregation. With consistent bucketing on both
shards the bug cannot reproduce, so the changed test passed on master HEAD and
the per-arch Bugfix Validation jobs reported "bug does not reproduce on this
arch" (SKIPPED). The merge-blocking new_tests_check.py post-hook requires at
least one per-arch job to actually validate the bug (is_success, not SKIPPED),
so the PR was blocked.

The bug only manifests when one shard converts to two-level while another stays
single-level, so equal groups from the two shards are bucketed differently and
never merged. That single-level/two-level mix needs genuinely asymmetric
per-shard data, which test_cluster_two_shards_different_databases provides
(shard_0 with 1000 groups goes two-level, shard_1 with 10 groups stays
single-level). This new test reproduces the wrong result deterministically on
master HEAD (1010 rows / 0 merged groups) and passes with the fix (1000 rows /
10 merged groups), validated through the clickhouse-test harness against the
exact master-HEAD amd_asan_ubsan and amd_debug binaries used by CI.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pushed 54d5ae6 — adds a deterministic Bugfix Validation regression test.

The Bugfix validation (functional tests) jobs were reporting "bug does not reproduce on this arch" (per-arch SKIPPED), which the merge-blocking new_tests_check.py post-hook does not count as a validation (it needs is_success, i.e. the changed test must actually FAIL on master HEAD). The existing 04357 lines for this variant pin group_by_two_level_threshold = 1, forcing both localhost shards to two-level: with consistent bucketing the bug cannot fire, so the test passed on master HEAD.

The wrong-result only happens when one shard goes two-level while another stays single-level, so the per-shard states are bucketed differently and the collapsed duplicate-ALIAS groups are never merged. That mix needs asymmetric per-shard data, which test_cluster_two_shards_different_databases provides. New test 04489_distributed_alias_same_expression_mix_levels puts 1000 groups on shard_0 (two-level) and 10 on shard_1 (single-level):

  • master HEAD: 1010 rows / 0 merged groups (the 10 shared groups returned twice with count 1)
  • with the fix: 1000 rows / 10 merged groups (count 2)

Verified through the clickhouse-test harness against the exact master-HEAD amd_asan_ubsan and amd_debug CI binaries (FAIL 10/10 on master, PASS 30/30 with randomization on the fix). Kept as a separate no-parallel file so 04357 stays parallel-safe.

GROUP BY GROUPING SETS where one set holds two ALIAS columns that expand to
the same expression (collapsed by the shard, e.g. GROUPING SETS ((a1, a2), (a1))
with a1, a2 both = toString(x)) hit the same two-level bucketing mismatch this PR
fixes for plain GROUP BY, but it bypassed the fix.

The non-grouping-sets branch of addMergingAggregatedStep merges on the shard's
collapsed key set and reconstructs the duplicate columns afterwards. That path is
guarded by grouping_sets_parameters_list.empty(), so GROUPING SETS stayed on the
old full-key merge: the shard deduplicates the set to one key before computing its
two-level bucket numbers, while the initiator's per-grouping-set Aggregator is
built from grouping_sets_parameters_list.used_keys and buckets by both keys. With
a single-level/two-level partial mix across shards the same group lands in
different buckets and is never merged, silently returning split counts.

Carrying the collapsed representative set through the grouping-sets merge
machinery (GroupingSetsParamsList + makeCreatingMissingKeysForGroupingSetDAG +
MergingAggregatedTransform) is out of scope here, so reject the combination with
NOT_IMPLEMENTED instead of returning wrong results. Only a grouping set that
actually contains both a collapsed duplicate and its representative is rejected;
sets that keep one such key each (e.g. ((a1), (a2))) bucket consistently with the
shard and are unaffected, as is any non-distributed or non-collapsed query.

Adds GROUPING SETS coverage to 04357_distributed_alias_same_expression
(the rejected forms plus a safe ((a1), (a2)) form).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate — GROUPING SETS fixup (85192f4)

Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. SELECT a1, a2, count() FROM <dist duplicate-ALIAS table> GROUP BY GROUPING SETS ((a1, a2), (a1)) with asymmetric per-shard data (shard_0 1000 groups two-level, shard_1 10 groups single-level), group_by_two_level_threshold=50, prefer_localhost_replica=0. Returns 1010 rows / 0 merged groups every run on the pre-fix binary.
b Root cause explained? The grouping_sets_parameters_list.empty() guard kept GROUPING SETS on the old full-key merge. The shard deduplicates a set's collapsed duplicate keys to one key and writes two-level bucket numbers from it, while the initiator's per-set Aggregator (built from used_keys) re-buckets single-level partials by both keys. A single/two-level partial mix then puts the same group in different buckets, never merging it → split counts.
c Fix matches root cause? Yes. The unsafe combination (a grouping set containing both a collapsed duplicate and its representative) is rejected with NOT_IMPLEMENTED before it can produce the mismatched bucketing, converting silent wrong results into a clear error.
d Test intent preserved / new tests added? Added GROUPING SETS coverage to 04357: two rejected forms (((a1,a2),(a1)), plain + two-level-forced) and a safe ((a1),(a2)) positive case. No existing assertion weakened.
e Both directions demonstrated? Yes. Without the fix the query returns rows (split counts on asymmetric data); with the fix it is rejected. The { serverError NOT_IMPLEMENTED } lines fail on master HEAD (rows instead of error) and pass on the PR.
f Fix is general across code paths? Yes. The check covers every grouping set in the list and every duplicate→representative mapping, any key order (((a2,a1),(a1)) rejected), N-way alias chains, and the duplicate-in-one-set-only shape (((a1,a2),(y))). It is gated on shard_collapse_duplicate_keys so non-distributed and non-collapsed queries are untouched.
g Fix generalizes across inputs? Yes. Rejection is a plan-time decision independent of data/settings (verified 50/50 across random group_by_two_level_threshold and distributed_aggregation_memory_efficient 0/1). Safe shapes (((a1),(a2)), ((a1,y),(a2)), distinct-key sets, single-set ((a1,a2)) which the analyzer collapses to plain GROUP BY → already-fixed path) are not over-rejected and return correct merged results.
h Backward compatible? N/A — no setting default or on-disk/wire format change. A query shape that previously returned silently wrong results now raises a catchable NOT_IMPLEMENTED; the safe sibling shapes and the rewrite (((a1),(a2)) or distinct expressions) continue to work.
i Invariants and contracts preserved? Yes. The reject is added only in the else (grouping-sets) branch and leaves merge_keys = analysis_keys for every non-rejected case, so the existing grouping-sets/ROLLUP/CUBE merge layout and the plain-GROUP-BY collapse/reconstruct path are unchanged. Regression: full 04357 passes (89/89), 04489 passes, 44/44 grouping-sets/rollup/cube tests pass.

Session id: cron:clickhouse-worker-slot-1:20260630-054600

…sentatives

The previous rejection in addMergingAggregatedStep only fired when a single
grouping set literally contained a collapsed duplicate key AND its
representative (the first expected column for that shard column). With three or
more ALIAS columns all expanding to the same expression, a grouping set can
hold two collapsed duplicates without naming the representative, e.g.
GROUPING SETS ((a1), (a2, a3)) with a1, a2, a3 all = toString(x) yields the
collapse map {a2 -> a1, a3 -> a1}: the (a2, a3) set contains neither a1 nor a
duplicate of an in-set representative by the old check, so it slipped past the
rejection. The shard still deduplicates that set to one key before computing
two-level bucket numbers while the initiator buckets it by two keys, so a
single/two-level partial mix splits the same group across buckets and returns
wrong (split) counts.

Each used key of a grouping set is now canonicalized through the collapse map
(a duplicate maps to its representative; any other key maps to itself), and the
set is rejected when two of its keys share a canonical representative. This
subsumes the old representative-presence check and closes the representative-
absent shape.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate, GROUPING SETS 3-alias generalization (e6109a1)

Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. On the parent binary (85192f4), SELECT a1,a2,a3,count() FROM dist3 GROUP BY GROUPING SETS ((a1),(a2,a3)) SETTINGS prefer_localhost_replica=0 returns split rows (not rejected). On the fixed binary it throws NOT_IMPLEMENTED every run, stable across group_by_two_level_threshold {0,1,5,50} x distributed_aggregation_memory_efficient {0,1}.
b Root cause explained? The 85192f4 reject built a NameSet of a grouping set's used_keys and threw only when a key was a known duplicate AND its representative name was also literally in that same set. With 3+ aliases all expanding to one expression, the collapse map is {a2->a1, a3->a1}; the set (a2,a3) holds two duplicates of the same shard column but never names the representative a1, so the used_keys_set.contains(representative) test missed it. The shard still dedups (a2,a3) to one key and writes two-level bucket numbers from one key, while the initiator buckets that set by two keys, leaving the split-bucket wrong-result path the bot flagged.
c Fix matches root cause? Yes. Each used key is canonicalized through shard_collapse_duplicate_keys (duplicate -> representative; otherwise identity) and the set is rejected when two keys share a canonical representative. This is exactly the bot's requested condition and subsumes the old representative-presence check.
d Test intent preserved / new tests added? Existing assertions unchanged. Added a 3-alias table to 04357_distributed_alias_same_expression.sql: ((a1),(a2,a3)) and ((a2,a3),(a1)) as { serverError NOT_IMPLEMENTED } (the representative-absent shapes), plus the safe ((a1),(a2),(a3)) asserting merged output.
e Both directions demonstrated? Yes. Parent 85192f4 (Build ID 24744b06): ((a1),(a2,a3)) NOT rejected, returns split rows. Fixed e6109 (Build ID 1e42b0e9): rejected with NOT_IMPLEMENTED. Both verified on the same 2-shard-localhost server.
f Fix is general across code paths? Yes. The reject lives in the single grouping-sets branch of addMergingAggregatedStep. The plain GROUP BY (incl. single-set collapse) and ROLLUP/CUBE paths were fixed by the earlier commits in this PR; this completes the family. No sibling merge path is left with the duplicate-key bucket hole.
g Fix generalizes across inputs? Yes. Canonicalization handles any alias count (2, 3, N) and any key order (dup-first, rep-first, rep-absent), verified: ((a1),(a2,a3)), ((a2,a3),(a1)), ((a2,a3)), ((a1,a2),(a3)) all reject; ((a1),(a2),(a3)), ((a1),(a2)), distinct-key sets, single-set collapse, and non-distributed local grouping sets are not rejected.
h Backward compatible? N/A for restoring old behavior in the wrong-results sense: the affected combination previously returned silently-incorrect results, so converting it to a clear catchable NOT_IMPLEMENTED is the safe and correct change. No setting default, on-disk, or wire format changed.
i Invariants and contracts preserved? Yes. The reject is a plan-time guard before MergingAggregated; it does not touch the grouping-sets merge machinery or block layout. The canonical aggregated layout invariant that RollupStep/CubeStep rely on (fixed in a67a794) is untouched. No over-rejection of safe shapes (verified).

Session id: cron:clickhouse-worker-slot-2:20260630-075900

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — e6109a1

Every failure below has an owner: a fixing PR (ours or external). No failure is PR-caused (the diff is the distributed two-level-aggregation duplicate-ALIAS planner fix; none of these tests are in that path or reproduce the wrong-results bug). Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, parallel) / 00115_shard_in_incomplete_result flaky (connection-pool desync: Code 102 expected TablesStatusResponse, got ProfileInfo; CI rerun 18/18 passed, not reproducible) #108854 (external, open)
Stress test (amd_tsan) / Hung check deadlock (chronic trunk family, 518 PRs + 248 master hits/14d) #108212 (ours, merged) / #105905 (ours, open)
Stress test (arm_tsan) / Hung check deadlock (same chronic trunk family) #108212 (ours, merged) / #105905 (ours, open)
CH Inc sync — CH Inc sync (private, not actionable by us)

The 00115 failure is the engine-wide #93018 connection-pool dirty-ProfileInfo race (regressed by #95466) that @alexey-milovidov is fixing at the source in #108854; it is unrelated to this PR's planner change.

Session id: cron:our-pr-ci-monitor:20260630-130000

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, we fixed many unrelated tests. Please update the branch.

@groeneai

Copy link
Copy Markdown
Collaborator Author

The PR is green on the current head and one narrow decision is the only thing outstanding, so here is what a reviewer needs.

The guard debated in the open Planner.cpp thread affects only the GROUPING SETS branch. The statement that actually flakes in CI is a plain GROUP BY (04357_distributed_alias_same_expression.sql:282), which takes the other branch at Planner.cpp:795. The flake fix therefore does not depend on how that guard question is settled.

The decision: keep the conservative NOT_IMPLEMENTED for duplicate-ALIAS grouping sets over a Distributed table, which never returns wrong results but does reject some single-level executions that work today, or move the check to the merge side, which knows whether it received bucketed data and is exact by construction. I measured the over-rejection's blast radius on this head: of the 15 stateless tests combining GROUPING SETS with a Distributed table, all pass. I am ready to implement the merge-side version if that is preferred.

The "I would prefer the exact merge-side fix" comment in that thread is from clickhouse-gh[bot], so the choice is still open.

Cost of the flake over 30 days, flaky-check reruns excluded: 132 failures across 124 distinct pull requests, plus 12 on master, still firing today.

@alexey-milovidov

Copy link
Copy Markdown
Member

@PedroTadim, the delay is because I'm actually trying to read this...

@alexey-milovidov

Copy link
Copy Markdown
Member

some queries that would execute correctly on the single-level path now raise NOT_IMPLEMENTED only because the settings are non-zero

I think this is tolerable.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Confirmed: the conservative NOT_IMPLEMENTED is the arm already implemented on this branch, so the ruling needs no diff change, and I am dropping the merge-side alternative.

CI is green on the current head 46155a3d2b8 (158 success, 17 skipped, 0 failures), and nothing else is open on my side.

The branch is 6308 commits behind master since I last merged it on 7 August. I will refresh it if you prefer, otherwise it is ready as it stands.

@alexey-milovidov alexey-milovidov left a comment

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.

This is a band-aid like fix.

@alexey-milovidov alexey-milovidov self-assigned this Aug 18, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 18, 2026
Merged via the queue into ClickHouse:master with commit 9ca234c Aug 18, 2026
179 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 18, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 1, 2026
Refresh the branch against master d466df7 so it is ready
to review. The two CI failures the finish ledger owned have since been fixed on master
(ClickHouse#108855 and ClickHouse#112484), which is what was blocking Mergeable Check.

No conflict: the clone() triple copy still applies verbatim and master's newer restores in
the same function are preserved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong results: GROUP BY two ALIAS columns with the same expression over a Distributed table duplicates groups under two-level aggregation

4 participants