Skip to content

Fix grouping function on Distributed table with single shard - #101030

Merged
alexey-milovidov merged 18 commits into
masterfrom
fix-grouping-distributed-single-shard
Apr 24, 2026
Merged

alexey-milovidov merged 18 commits into
masterfrom
fix-grouping-distributed-single-shard

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 28, 2026 •

Copy link
Copy Markdown
Member

The grouping function (used with GROUPING SETS, ROLLUP, CUBE) failed with "Method executeImpl is not supported for 'grouping' function" when querying a Distributed table backed by a single shard.

Root cause: buildQueryTreeForShard called removeGroupingFunctionSpecializations which replaced specialized functions (e.g. groupingForGroupingSets) with the generic FunctionGrouping that throws on execute. The same query tree was then reused for getSampleBlock / plan building, which executes functions with 0 rows for header computation, triggering the exception.

Fix: move removeGroupingFunctionSpecializations out of buildQueryTreeForShard and apply it only on clones of the query tree right before AST conversion for shard execution. This keeps the original tree with specialized (executable) functions intact for plan building, while the AST sent to shards still contains the generic grouping name that the shard's analyzer can re-resolve.

Closes #71270

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 grouping/GROUPING SETS queries on Distributed tables with a single shard that failed with "Method executeImpl is not supported for 'grouping' function".

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.68

The `grouping` function (used with `GROUPING SETS`, `ROLLUP`, `CUBE`)
failed with "Method executeImpl is not supported for 'grouping' function"
when querying a Distributed table backed by a single shard.

Root cause: `buildQueryTreeForShard` called
`removeGroupingFunctionSpecializations` which replaced specialized
functions (e.g. `groupingForGroupingSets`) with the generic
`FunctionGrouping` that throws on execute.  The same query tree was
then reused for `getSampleBlock` / plan building, which executes
functions with 0 rows for header computation, triggering the exception.

Fix: move `removeGroupingFunctionSpecializations` out of
`buildQueryTreeForShard` and apply it only on clones of the query tree
right before AST conversion for shard execution.  This keeps the
original tree with specialized (executable) functions intact for plan
building, while the AST sent to shards still contains the generic
`grouping` name that the shard's analyzer can re-resolve.

Closes #71270

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [302671e]

Summary: ✅


AI Review

Summary

This PR fixes grouping/GROUPING SETS behavior on single-shard Distributed queries by preserving specialized grouping functions in planning/query-tree paths while normalizing only the AST sent to shards. It also adds targeted stateless coverage for remote, real Distributed, and parallel-replicas paths. I did not find new high-confidence correctness, safety, or performance issues in the current head.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 28, 2026
alexey-milovidov and others added 2 commits March 28, 2026 23:46
Address review feedback: the original test only used `remote()` table function,
which does not exercise the `StorageDistributed::read` code path.  Add test cases
that create a real `Distributed` table over a local `MergeTree` table and run
`GROUPING SETS`, `ROLLUP`, and `CUBE` queries with the `grouping` function,
including with `prefer_localhost_replica = 0`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
alexey-milovidov and others added 5 commits March 29, 2026 01:41
The `serialize_query_plan` setting causes the specialized grouping
functions (`groupingForGroupingSets`, etc.) to be serialized in the
query plan via `ActionsDAG::serialize`. On the shard side,
`ActionsDAG::deserialize` looks up the function by name in the
`FunctionFactory`, but these internal functions are not registered
there, causing `UNKNOWN_FUNCTION` errors.

This is a pre-existing limitation unrelated to the fix in this PR.
Disable `serialize_query_plan` in the test to work around it.

Also add a parallel replicas test case (Part 3) to exercise the
`executeQueryWithParallelReplicas` and `findParallelReplicasQuery`
code paths, as requested in review.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
This file was accidentally included in commit b884ff7 and is
unrelated to the PR.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Parallel replicas require the analyzer by default (`parallel_replicas_only_with_analyzer = true`),
so the test was silently falling back to a non-parallel path in the old-analyzer CI run,
producing no output and causing a reference mismatch.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
alexey-milovidov and others added 2 commits March 30, 2026 02:00
…lan = 1`

The `buildQueryPlanForParallelReplicas` code path is only reached when
`serialize_query_plan` is enabled (or in follower mode).  The previous
parallel replicas test ran with `serialize_query_plan = 0`, so it only
exercised the `executeQueryWithParallelReplicas` path.  Add a Part 4
that explicitly sets `serialize_query_plan = 1` to cover the
`findParallelReplicasQuery` fix as well.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@clickhouse-gh

clickhouse-gh Bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.30% 76.20% -0.10%

Changed lines: 100.00% (33/33) · Uncovered code

Full report · Diff report

@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.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Apr 24, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 24, 2026
Merged via the queue into master with commit cf81ad1 Apr 24, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-grouping-distributed-single-shard branch April 24, 2026 16:38
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 24, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to 26.3 (LTS), 26.2, 26.1, 25.8 (LTS), but no backport label was found.

Affected code: buildQueryTreeForShard in src/Storages/buildQueryTreeForShard.cpp and its callers in SelectStreamFactory.cpp, executeQuery.cpp, findParallelReplicasQuery.cpp, StorageDistributed.cpp — core code present in all supported branches.

Why: Fix for grouping function exception on Distributed tables — buildQueryTreeForShard has existed since the analyzer was introduced for distributed queries, well before all supported branches. The linked issue #71270 has been open for a long time. All users with distributed tables using GROUPING SETS/ROLLUP/CUBE with the grouping function are affected.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

BorisTyshkevich pushed a commit to BorisTyshkevich/ClickHouse that referenced this pull request Nov 15, 2026
…ldren

A Merge of a local table and a Distributed table is read at
WithMergeableState, so ReadFromMerge::createPlanForTable plans each local
child by printing the already-analyzed query tree and re-analyzing it.
GroupingFunctionsResolvePass has replaced every grouping(...) in that tree
with an analyzer-built specialization such as
__groupingForGroupingSets(__grouping_set, <keys>, <state constants>), and
__grouping_set is a column produced by aggregation, so the printed query
fails in the child with UNKNOWN_IDENTIFIER `__grouping_set`. The same
happens for ROLLUP and CUBE and for a nested Merge child.

Every place that prints an analyzed tree for a remote analyzer
(StorageDistributed::read, SelectStreamFactory, ClusterProxy, parallel
replicas, IStorageCluster) already calls removeGroupingFunctionSpecializations
on a clone first; this local re-analysis site was missing from that list.
Do the same here. The clone keeps the original tree intact, because the
de-specialized nodes would throw if executed. The child runs the resolve
passes, which include GroupingFunctionsResolvePass, so it rebuilds the
specializations from its own GROUP BY.

Related: ClickHouse#124125 (comment)
Related: ClickHouse#101030

Co-Authored-By: Claude Opus 5.5 <[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.

grouping function doesn't work anymore on a distributed table when allow_experimental_analyze=1

3 participants