Skip to content

Fix incorrect result with partitioned GROUP BY - #104869

Merged
alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
nihalzp:fix-partitioned-aggregation
May 19, 2026
Merged

alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
nihalzp:fix-partitioned-aggregation

Conversation

@nihalzp

@nihalzp nihalzp commented May 13, 2026 •

Copy link
Copy Markdown
Member

The following query gives wrong result:

CREATE TABLE t (a UInt32, b UInt32)
ENGINE = MergeTree
ORDER BY tuple()
PARTITION BY a;

INSERT INTO t SELECT number % 8, number FROM numbers(1000);

SELECT intDiv(a, 2) AS g, sum(b)
FROM t
GROUP BY g
ORDER BY g
SETTINGS allow_aggregate_partitions_independently = 1;

Returns:

0	62000
0	62125
1	62250
1	62375
2	62625
2	62500
3	62875
3	62750

But since it's a group by, the first column should be all unique values.

This happens because allOutputsDependsOnlyOnAllowedNodes skips validation if the partition output column is only an input column; as a result, we can miss cases like the above example. There is no need to do this special input check here because the other overload allOutputsDependsOnlyOnAllowedNodes can handle it anyway.

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 incorrect aggregation result when grouping by a non-injective function of a column that is also the partition key (e.g. PARTITION BY a with GROUP BY intDiv(a, 2) or a % 2). Values from different partitions that mapped to the same group were not merged, producing duplicate group rows under allow_aggregate_partitions_independently = 1.

Version info

  • Merged into: 26.5.1.847

@clickhouse-gh

clickhouse-gh Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [904575d]

Summary: ✅


AI Review

Summary

This PR fixes incorrect results in partition-independent aggregation by validating only actual partition or sharding key output nodes (instead of all DAG outputs including source INPUT nodes). The change matches the reported failure mode, updates the existing optimizer expectation test, and adds a focused regression test that reproduces the bad result pattern and verifies correct merged output.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 13, 2026
@yariks5s yariks5s self-assigned this May 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.20% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 100.00% (15/15) | lost baseline coverage: 2 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov merged commit 247dae4 into ClickHouse:master May 19, 2026
326 of 328 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 2026
alexey-milovidov added a commit to BoloniniD/ClickHouse that referenced this pull request May 20, 2026
libfuzzer arms `setitimer(ITIMER_REAL)` with an interval of
`UnitTimeoutSec / 2 + 1` seconds (11 s for our 20 s timeout), so SIGALRM
fires periodically — not only at the actual timeout. The wrapper handler
in `clickhouse_fuzzer.cpp` had three issues that together produced
spurious "timeout after 21 seconds" failures (seen on PRs ClickHouse#99740 and
ClickHouse#104869):

1. SIGALRM has no per-thread routing. The handler is installed
   process-wide via `sigaction`, so the kernel can deliver it to either
   the libfuzzer main thread or the runner thread. When it landed on the
   runner, `fuzzerSigalrmHandler` `pthread_kill`'d SIGUSR1 to itself and
   raced with a concurrent invocation on the main thread, with two
   competing `sigaction` restores corrupting handler state.
2. The slow runner-stack dump (shells out to llvm-symbolizer) ran on
   every periodic SIGALRM, even when the iteration was nowhere near its
   timeout. The dump alone takes many seconds; combined with the 3-second
   wait-for-stack window it could push an otherwise-fast iteration past
   the 20 s limit and trigger a real timeout.
3. The wrapper permanently restored libfuzzer's original handler before
   `raise(SIGALRM)`, making itself one-shot. A periodic SIGALRM at second
   ~11 consumed the wrapper, so when the genuine timeout fired at
   second 20 there was no longer any wrapper to dump the runner stack.

Fix all three:

- Block SIGALRM on the runner thread at the top of `runLibFuzzer` so the
  wrapper only ever runs on the libfuzzer main thread.
- Track per-iteration start time via `clock_gettime(CLOCK_MONOTONIC)` and
  skip the SIGUSR1 stack-dump unless elapsed ≥ 15 s, so the 11 s
  periodic alarms pass through without disturbing the runner. Guard with
  an atomic to keep the dump single-shot per process.
- Call libfuzzer's original handler directly (via the saved
  `sigaction.sa_sigaction` / `sa_handler`) instead of permanently
  restoring it. The wrapper stays installed and keeps intercepting
  later alarms.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104869&sha=4bb6825eb6220feaca31f3d2608dae09d5de7927&name_0=PR&name_1=libFuzzer%20tests
ClickHouse#104869

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants