Skip to content

Do not eliminate aggregators of GROUPING SETS keys with group_by_use_nulls - #125575

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix/aggregators-of-group-by-keys-grouping-sets-use-nulls
Oct 14, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix/aggregators-of-group-by-keys-grouping-sets-use-nulls

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

With group_by_use_nulls, the keys of GROUPING SETS become Nullable in the output of the aggregation, even when there is only one set. The argument of the aggregate function keeps its original type. optimize_aggregators_of_group_by_keys replaced anyHeavy(b) with the key b, so a column typed UInt64 actually held Nullable(UInt64). An outer aggregation then read it as a plain column. In release builds that gives a wrong result. In debug and sanitizer builds it throws the Bad cast from type DB::ColumnNullable to DB::ColumnVector<unsigned long> exception.

CREATE TABLE test (a UInt64, b UInt64) ENGINE = MergeTree ORDER BY a;
INSERT INTO test SELECT number, number % 100 FROM numbers(10000);
SELECT sum(s) FROM (SELECT b AS k, anyHeavy(b) AS s FROM test GROUP BY GROUPING SETS ((k))) SETTINGS group_by_use_nulls = 1;
-- garbage instead of 4950

This change skips the optimization in that case in the analyzer. The old interpreter is not affected: it rewrites a single-set GROUPING SETS into a plain GROUP BY before the optimization, and it already skipped multi-set GROUPING SETS.

The stress test of #123531 found it: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=123531&sha=808640fa34da62bf52e599b53b13ae30ab1de3c2&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29

Related: #123531

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 a wrong result, or an exception in debug builds, when optimize_aggregators_of_group_by_keys eliminated any, min or max of a GROUPING SETS key with group_by_use_nulls.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.2406-master (included in 26.10 and later)
  • Backported to: 26.9.20.10, 26.8.28.11, 26.7.31.11, 26.3.52.6

…se_nulls`

With `group_by_use_nulls` the keys of `GROUPING SETS` become `Nullable` in the output of the
aggregation, even with a single set, while the argument of the aggregate function keeps its type.
`optimize_aggregators_of_group_by_keys` replaced `any(k)` with `k`, so the column typed as `UInt64`
was `Nullable(UInt64)`, and an outer `sum` read it as a plain column: a wrong result in release
builds, and the `Bad cast from type DB::ColumnNullable to DB::ColumnVector<unsigned long>`
exception in debug and sanitizer builds. Skip the optimization in this case in both the analyzer
and the old interpreter.

Found by the stress test of #123531:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=123531&sha=808640fa34da62bf52e599b53b13ae30ab1de3c2&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [15fca46]

Summary: ✅


AI Review

Summary

The PR disables optimize_aggregators_of_group_by_keys for GROUPING SETS with group_by_use_nulls, preserving aggregate result types and preventing wrong results or debug-build exceptions. No unresolved findings remain; the prior finding about the old-interpreter scope is addressed in the current code and description. The new regression test passed in the head commit's Fast test report.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 13, 2026
Comment thread src/Interpreters/TreeOptimizer.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Oct 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 15fca465c246839a9dcda1fce7b651881050638d was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

alexey-milovidov and others added 3 commits October 13, 2026 18:48
`TreeRewriter::normalize` already rewrites single-set `GROUPING SETS` into a plain
`GROUP BY` before `TreeOptimizer::apply`, and multi-set ones were already skipped, so the
guard in `TreeOptimizer` never changed anything. The query with `enable_analyzer = 0` ran
on the analyzer anyway, because the setting is rewritten to `1`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 14, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good.

@alexey-milovidov alexey-milovidov self-assigned this Oct 14, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 14, 2026
Merged via the queue into master with commit 191c3ed Oct 14, 2026
177 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/aggregators-of-group-by-keys-grouping-sets-use-nulls branch October 14, 2026 20:54
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 14, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 14, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 14, 2026
Backport #125575 to 26.9: Do not eliminate aggregators of `GROUPING SETS` keys with `group_by_use_nulls`
clickhouse-gh Bot added a commit that referenced this pull request Oct 14, 2026
Backport #125575 to 26.8: Do not eliminate aggregators of `GROUPING SETS` keys with `group_by_use_nulls`
clickhouse-gh Bot added a commit that referenced this pull request Oct 15, 2026
Backport #125575 to 26.7: Do not eliminate aggregators of `GROUPING SETS` keys with `group_by_use_nulls`
alexey-milovidov added a commit that referenced this pull request Oct 15, 2026
Backport #125575 to 26.3: Do not eliminate aggregators of `GROUPING SETS` keys with `group_by_use_nulls`
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 15, 2026
…GROUPING SETS` keys with `group_by_use_nulls`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore 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! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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