Skip to content

Commit 1d48d4b

Browse files
groeneaiclaude
andcommitted
Fix missing rows of the empty grouping set in GROUPING SETS without aggregate functions
A `GROUP BY GROUPING SETS` query without aggregate functions lost the row of every empty grouping set `()`: `SELECT number FROM numbers(3) GROUP BY GROUPING SETS ((number), ())` returned 3 rows instead of 4, `GROUPING SETS (())` returned one row per input row. Each grouping set has its own Aggregator. For `()` without aggregate functions it has no keys and no aggregates, so prepareChunkAndFillWithoutKey built a chunk without columns, finalizeChunk derived its row count from the first column (0), and ConvertingAggregatedToChunksTransform dropped it. Any aggregate function adds a column, which is why `any(1)` / `count()` forms were correct. Keep the declared row count for a column-less chunk. A set list of only `()` was not aggregated at all: GroupingFunctionsResolvePass flattens a single grouping set when group_by_use_nulls = 0, which for `(())` removes the GROUP BY; analyzeAggregation / hasAggregation treated no keys and no aggregates as no aggregation (`((), ())`, or `(())` with group_by_use_nulls = 1). Do not flatten an empty single set, and count grouping sets as aggregation. On a distributed initiator, MergingAggregatedTransform::generate rebuilt each merged chunk through ExpressionActions::execute(Block &), which recounts rows from the column-less block (0), so the row the shards sent was lost again. Pass the chunk's own row count. The result now equals the same query with an aggregate function added. Three references pinned the missing row (04213, 04416, 04611); each new value equals the same query with count() kept, on the unpatched binary. Closes #61461 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 33ce930 commit 1d48d4b

10 files changed

Lines changed: 81 additions & 8 deletions

‎src/Analyzer/Passes/GroupingFunctionsResolvePass.cpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,12 @@ void resolveGroupingFunctions(QueryTreeNodePtr & query_node, ContextPtr context)
224224
{
225225
auto grouping_set_list_node = query_node_typed.getGroupBy().getNodes().front();
226226
auto & grouping_set_list_node_typed = grouping_set_list_node->as<ListNode &>();
227-
query_node_typed.getGroupBy().getNodes() = std::move(grouping_set_list_node_typed.getNodes());
228-
query_node_typed.setIsGroupByWithGroupingSets(false);
227+
/// Flattening `GROUPING SETS (())` would leave no GROUP BY, so the query would not aggregate at all.
228+
if (!grouping_set_list_node_typed.getNodes().empty())
229+
{
230+
query_node_typed.getGroupBy().getNodes() = std::move(grouping_set_list_node_typed.getNodes());
231+
query_node_typed.setIsGroupByWithGroupingSets(false);
232+
}
229233
}
230234

231235
if (query_node_typed.isGroupByWithGroupingSets())

‎src/Interpreters/Aggregator.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,6 +4093,9 @@ Aggregator::AggregatedChunk Aggregator::prepareChunkAndFillWithoutKey(Aggregated
40934093
}
40944094

40954095
Chunk chunk = finalizeChunk(params, std::move(out_cols), final);
4096+
/// Without keys and aggregate functions there is no column to carry the row.
4097+
if (!chunk.hasColumns())
4098+
chunk.setColumns(Columns{}, rows);
40964099

40974100
if (final)
40984101
destroyWithoutKey(data_variants);

‎src/Planner/PlannerExpressionAnalysis.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ std::optional<AggregationAnalysisResult> analyzeAggregation(
305305
}
306306
}
307307

308-
if (aggregation_keys.empty() && aggregates_descriptions.empty())
308+
if (aggregation_keys.empty() && aggregates_descriptions.empty() && grouping_sets_parameters_list.empty())
309309
return {};
310310

311311
/** For non ordinary GROUP BY we add virtual __grouping_set column

‎src/Planner/PlannerExpressionAnalysis.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ class PlannerExpressionsAnalysisResult
9494

9595
bool hasAggregation() const
9696
{
97-
return !aggregation_analysis_result.aggregation_keys.empty() || !aggregation_analysis_result.aggregate_descriptions.empty();
97+
return !aggregation_analysis_result.aggregation_keys.empty() || !aggregation_analysis_result.aggregate_descriptions.empty()
98+
|| !aggregation_analysis_result.grouping_sets_parameters_list.empty();
9899
}
99100

100101
AggregationAnalysisResult & getAggregation()

‎src/Processors/Transforms/MergingAggregatedTransform.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,10 @@ Chunk MergingAggregatedTransform::generate()
267267
auto res_header = params->params.getHeader(params->header, params->final);
268268
for (auto & agg_chunk : merged_chunks)
269269
{
270+
size_t num_rows = agg_chunk.chunk.getNumRows();
270271
auto block = res_header.cloneWithColumns(agg_chunk.chunk.detachColumns());
271-
grouping_set.creating_missing_keys_actions->execute(block);
272-
agg_chunk.chunk = Chunk(block.getColumns(), block.rows());
272+
grouping_set.creating_missing_keys_actions->execute(block, num_rows);
273+
agg_chunk.chunk = Chunk(block.getColumns(), num_rows);
273274
}
274275
}
275276

‎tests/queries/0_stateless/04213_optimize_trivial_group_by_limit_query.reference‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
6
1919
6
2020
6
21-
5
22-
5
21+
6
22+
6
2323
5
2424
5
2525
5 throw

‎tests/queries/0_stateless/04416_group_by_use_nulls_low_cardinality_constant.reference‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
LowCardinality(Nullable(String))
2+
LowCardinality(Nullable(String))
3+
Nullable(String)
24
Nullable(String)
35
1
46
1

‎tests/queries/0_stateless/04611_correlated_subquery_grouping_sets_use_nulls.reference‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
\N
1010
\N
1111
\N
12+
\N
13+
\N
1214
0
1315
0
1416
0
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
keys and empty set 0 0
2+
keys and empty set 0 1
3+
keys and empty set 1 0
4+
keys and empty set 2 0
5+
keys and empty set, use_nulls 0 0
6+
keys and empty set, use_nulls 1 0
7+
keys and empty set, use_nulls 2 0
8+
keys and empty set, use_nulls \N 1
9+
issue 0 0 3
10+
issue 1 0 1
11+
issue 1 1 0
12+
issue 1 2 0
13+
only empty set 1
14+
only empty set, use_nulls 1
15+
three empty sets 1
16+
three empty sets 1
17+
three empty sets 1
18+
pruned aggregate 4
19+
pruned aggregate, two empty sets 2
20+
pruned aggregate, one empty set 1
21+
empty input \N
22+
empty input, only empty set 1
23+
several streams 1001 1
24+
distributed 0
25+
distributed 1
26+
distributed 2
27+
distributed \N
28+
distributed, only empty set 1
29+
correlated subquery true
30+
correlated subquery \N
31+
correlated subquery, no substitution true
32+
correlated subquery, no substitution \N
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- Tags: shard
2+
3+
-- An empty grouping set `()` contributes one row even when the query has no aggregate functions.
4+
-- https://github.com/ClickHouse/ClickHouse/issues/61461
5+
6+
SELECT 'keys and empty set', number, grouping(number) FROM numbers(3) GROUP BY GROUPING SETS ((number), ()) ORDER BY ALL;
7+
SELECT 'keys and empty set, use_nulls', number, grouping(number) FROM numbers(3) GROUP BY GROUPING SETS ((number), ()) ORDER BY ALL SETTINGS group_by_use_nulls = 1;
8+
SELECT 'issue', a, b, grouping(a, b) FROM (SELECT 1 a, 1 b UNION ALL SELECT 1 a, 2 b) GROUP BY GROUPING SETS ((a, b), (a), ()) ORDER BY ALL;
9+
10+
SELECT 'only empty set', 1 FROM numbers(3) GROUP BY GROUPING SETS (());
11+
SELECT 'only empty set, use_nulls', 1 FROM numbers(3) GROUP BY GROUPING SETS (()) SETTINGS group_by_use_nulls = 1;
12+
SELECT 'three empty sets', 1 FROM (SELECT 1 a UNION ALL SELECT 2 a) GROUP BY GROUPING SETS ((), (), ());
13+
14+
-- The unused inner count() is removed by the analyzer.
15+
SELECT 'pruned aggregate', count() FROM (SELECT number, count() FROM numbers(3) GROUP BY GROUPING SETS ((number), ()));
16+
SELECT 'pruned aggregate, two empty sets', count() FROM (SELECT 1 AS x, count() FROM numbers(3) GROUP BY GROUPING SETS ((), ()));
17+
SELECT 'pruned aggregate, one empty set', count() FROM (SELECT 1 AS x, count() FROM numbers(3) GROUP BY GROUPING SETS (())) SETTINGS group_by_use_nulls = 1;
18+
19+
SELECT 'empty input', number FROM numbers(0) GROUP BY GROUPING SETS ((number), ()) SETTINGS group_by_use_nulls = 1;
20+
SELECT 'empty input, only empty set', 1 FROM numbers(0) GROUP BY GROUPING SETS (());
21+
22+
SELECT 'several streams', count(), countIf(isNull(number)) FROM (SELECT number FROM numbers_mt(1000) GROUP BY GROUPING SETS ((number), ())) SETTINGS group_by_use_nulls = 1, max_threads = 4, max_block_size = 100;
23+
24+
SELECT 'distributed', number FROM remote('127.0.0.{1,2}', numbers(3)) GROUP BY GROUPING SETS ((number), ()) ORDER BY ALL SETTINGS group_by_use_nulls = 1;
25+
SELECT 'distributed, only empty set', 1 FROM remote('127.0.0.{1,2}', numbers(3)) GROUP BY GROUPING SETS (());
26+
27+
SELECT 'correlated subquery', (SELECT c0) FROM (SELECT 1::Bool) t0(c0) GROUP BY GROUPING SETS ((c0), ()) ORDER BY c0 NULLS LAST SETTINGS group_by_use_nulls = 1;
28+
SELECT 'correlated subquery, no substitution', (SELECT c0) FROM (SELECT 1::Bool) t0(c0) GROUP BY GROUPING SETS ((c0), ()) ORDER BY c0 NULLS LAST SETTINGS group_by_use_nulls = 1, correlated_subqueries_substitute_equivalent_expressions = 0;

0 commit comments

Comments
 (0)