GROUP BY constant with aggregation function and with empty input magically summons a row
Describe the bug
Hello, it seems that there is inconsistent behaviour for select queries with group by on constant when there are no input rows.
When I have no aggregation functions then result of such query is empty, but as soon as I add some aggregation function I get single row result.
To Reproduce
Run following query in datafusion-cli
> select 'wat' as key, count(1) as count from (select * from unnest(array[]::text[])) as b(field) group by key;
+-----+-------+
| key | count |
+-----+-------+
| wat | 0 |
+-----+-------+
1 row(s) fetched.
Elapsed 0.004 seconds.
Expected behavior
Empty result
Additional context
Query with no aggregation functions gives empty result
DataFusion CLI v46.0.1
> select 'wat' as key from (select * from unnest(array[]::text[])) as b(field) group by key;
+-----+
| key |
+-----+
+-----+
0 row(s) fetched.
Elapsed 0.004 seconds.
Postgres gives empty results in both cases
psql (17.4 (Homebrew))
Type "help" for help.
postgres=# select 'wat' as key from (select * from unnest(array[]::text[])) as b(field) group by key;
key
-----
(0 rows)
postgres=# select 'wat' as key, count(1) as count from (select * from unnest(array[]::text[])) as b(field) group by key;
key | count
-----+-------
(0 rows)
I can take a look next week, seems like a parser issue, similar to #5221 .
maybe it generate bad plan ? AggregateExec.gby should not be empty
> explain analyze select 'wat' as key, count(1) as count from (select * from unnest(array[]::text[])) as b(field) group by key;
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| plan_type | plan |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| Plan with Metrics | ProjectionExec: expr=[wat as key, count(Int64(1))@0 as count], metrics=[output_rows=1, elapsed_compute=1.112µs] |
| | AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))], metrics=[output_rows=1, elapsed_compute=4.163µs] |
| | CoalescePartitionsExec, metrics=[output_rows=24, elapsed_compute=10.677µs] |
| | AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))], metrics=[output_rows=24, elapsed_compute=4.377µs] |
| | RepartitionExec: partitioning=RoundRobinBatch(24), input_partitions=1, metrics=[fetch_time=18.502µs, repartition_time=1ns, send_time=24ns] |
| | ProjectionExec: expr=[], metrics=[output_rows=0, elapsed_compute=1ns] |
| | UnnestExec, metrics=[output_rows=0, elapsed_compute=12.387µs, input_batches=1, input_rows=1, output_batches=0] |
| | ProjectionExec: expr=[[] as __unnest_placeholder(make_array())], metrics=[output_rows=1, elapsed_compute=2.091µs] |
| | PlaceholderRowExec, metrics=[] |
| | |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+
1 row(s) fetched.
Elapsed 0.001 seconds.
@UBarney good find. It looks like the group by is removed in eliminate_group_by_constant :
> set datafusion.explain.format = "indent";
0 row(s) fetched.
Elapsed 0.001 seconds.
> explain verbose select 'wat' as key, count(1) as count from (select * from unnest(array[]::text[])) as b(field) group by key;
+------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| plan_type | plan |
+------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| initial_logical_plan | Projection: Utf8("wat") AS key, count(Int64(1)) AS count |
| | Aggregate: groupBy=[[Utf8("wat")]], aggr=[[count(Int64(1))]] |
| | SubqueryAlias: b |
| | Projection: UNNEST(make_array()) AS field |
| | Projection: UNNEST(make_array()) |
| | Projection: __unnest_placeholder(make_array(),depth=1) AS UNNEST(make_array()) |
| | Unnest: lists[__unnest_placeholder(make_array())|depth=1] structs[] |
| | Projection: CAST(make_array() AS List(Field { name: "item", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} })) AS __unnest_placeholder(make_array()) |
| | EmptyRelation |
| logical_plan after resolve_grouping_function | SAME TEXT AS ABOVE |
| logical_plan after type_coercion | SAME TEXT AS ABOVE |
| analyzed_logical_plan | SAME TEXT AS ABOVE |
| logical_plan after eliminate_nested_union | SAME TEXT AS ABOVE |
| logical_plan after simplify_expressions | Projection: Utf8("wat") AS key, count(Int64(1)) AS count |
| | Aggregate: groupBy=[[Utf8("wat")]], aggr=[[count(Int64(1))]] |
| | SubqueryAlias: b |
| | Projection: UNNEST(make_array()) AS field |
| | Projection: UNNEST(make_array()) |
| | Projection: __unnest_placeholder(make_array(),depth=1) AS UNNEST(make_array()) |
| | Unnest: lists[__unnest_placeholder(make_array())|depth=1] structs[] |
| | Projection: List([]) AS __unnest_placeholder(make_array()) |
| | EmptyRelation |
| logical_plan after replace_distinct_aggregate | SAME TEXT AS ABOVE |
| logical_plan after eliminate_join | SAME TEXT AS ABOVE |
| logical_plan after decorrelate_predicate_subquery | SAME TEXT AS ABOVE |
| logical_plan after scalar_subquery_to_join | SAME TEXT AS ABOVE |
| logical_plan after extract_equijoin_predicate | SAME TEXT AS ABOVE |
| logical_plan after eliminate_duplicated_expr | SAME TEXT AS ABOVE |
| logical_plan after eliminate_filter | SAME TEXT AS ABOVE |
| logical_plan after eliminate_cross_join | SAME TEXT AS ABOVE |
| logical_plan after eliminate_limit | SAME TEXT AS ABOVE |
| logical_plan after propagate_empty_relation | SAME TEXT AS ABOVE |
| logical_plan after eliminate_one_union | SAME TEXT AS ABOVE |
| logical_plan after filter_null_join_keys | SAME TEXT AS ABOVE |
| logical_plan after eliminate_outer_join | SAME TEXT AS ABOVE |
| logical_plan after push_down_limit | SAME TEXT AS ABOVE |
| logical_plan after push_down_filter | SAME TEXT AS ABOVE |
| logical_plan after single_distinct_aggregation_to_group_by | SAME TEXT AS ABOVE |
| logical_plan after eliminate_group_by_constant | Projection: Utf8("wat") AS key, count(Int64(1)) AS count |
| | Projection: Utf8("wat"), count(Int64(1)) |
| | Aggregate: groupBy=[[]], aggr=[[count(Int64(1))]] |
| | SubqueryAlias: b |
| | Projection: UNNEST(make_array()) AS field |
| | Projection: UNNEST(make_array()) |
| | Projection: __unnest_placeholder(make_array(),depth=1) AS UNNEST(make_array()) |
| | Unnest: lists[__unnest_placeholder(make_array())|depth=1] structs[] |
| | Projection: List([]) AS __unnest_placeholder(make_array()) |
| | EmptyRelation |
| logical_plan after optimize_projections | Projection: Utf8("wat") AS key, count(Int64(1)) AS count |
| | Aggregate: groupBy=[[]], aggr=[[count(Int64(1))]] |
| | SubqueryAlias: b |
| | Projection: |
| | Unnest: lists[__unnest_placeholder(make_array())|depth=1] structs[] |
| | Projection: List([]) AS __unnest_placeholder(make_array()) |
| | EmptyRelation |
| logical_plan after eliminate_nested_union | SAME TEXT AS ABOVE |
| logical_plan after simplify_expressions | SAME TEXT AS ABOVE |
| logical_plan after replace_distinct_aggregate | SAME TEXT AS ABOVE |
| logical_plan after eliminate_join | SAME TEXT AS ABOVE |
| logical_plan after decorrelate_predicate_subquery | SAME TEXT AS ABOVE |
| logical_plan after scalar_subquery_to_join | SAME TEXT AS ABOVE |
| logical_plan after extract_equijoin_predicate | SAME TEXT AS ABOVE |
| logical_plan after eliminate_duplicated_expr | SAME TEXT AS ABOVE |
| logical_plan after eliminate_filter | SAME TEXT AS ABOVE |
| logical_plan after eliminate_cross_join | SAME TEXT AS ABOVE |
| logical_plan after eliminate_limit | SAME TEXT AS ABOVE |
| logical_plan after propagate_empty_relation | SAME TEXT AS ABOVE |
| logical_plan after eliminate_one_union | SAME TEXT AS ABOVE |
| logical_plan after filter_null_join_keys | SAME TEXT AS ABOVE |
| logical_plan after eliminate_outer_join | SAME TEXT AS ABOVE |
| logical_plan after push_down_limit | SAME TEXT AS ABOVE |
| logical_plan after push_down_filter | SAME TEXT AS ABOVE |
| logical_plan after single_distinct_aggregation_to_group_by | SAME TEXT AS ABOVE |
| logical_plan after eliminate_group_by_constant | SAME TEXT AS ABOVE |
| logical_plan after optimize_projections | SAME TEXT AS ABOVE |
| logical_plan | Projection: Utf8("wat") AS key, count(Int64(1)) AS count |
| | Aggregate: groupBy=[[]], aggr=[[count(Int64(1))]] |
| | SubqueryAlias: b |
| | Projection: |
| | Unnest: lists[__unnest_placeholder(make_array())|depth=1] structs[] |
| | Projection: List([]) AS __unnest_placeholder(make_array()) |
| | EmptyRelation |
| initial_physical_plan | ProjectionExec: expr=[wat as key, count(Int64(1))@0 as count] |
| | AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))] |
| | AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))] |
| | ProjectionExec: expr=[] |
| | UnnestExec |
| | ProjectionExec: expr=[[] as __unnest_placeholder(make_array())] |
| | PlaceholderRowExec |
| | |
| initial_physical_plan_with_stats | ProjectionExec: expr=[wat as key, count(Int64(1))@0 as count], statistics=[Rows=Exact(1), Bytes=Absent, [(Col[0]:),(Col[1]:)]] |
| | AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))], statistics=[Rows=Exact(1), Bytes=Absent, [(Col[0]:)]] |
| | AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))], statistics=[Rows=Absent, Bytes=Absent, [(Col[0]:)]] |
| | ProjectionExec: expr=[], statistics=[Rows=Absent, Bytes=Absent, []] |
| | UnnestExec, statistics=[Rows=Absent, Bytes=Absent, [(Col[0]:)]] |
| | ProjectionExec: expr=[[] as __unnest_placeholder(make_array())], statistics=[Rows=Exact(1), Bytes=Exact(0), [(Col[0]:)]] |
| | PlaceholderRowExec, statistics=[Rows=Exact(1), Bytes=Exact(0), []] |
| | |
| initial_physical_plan_with_schema | ProjectionExec: expr=[wat as key, count(Int64(1))@0 as count], schema=[key:Utf8, count:Int64] |
| | AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))], schema=[count(Int64(1)):Int64] |
| | AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))], schema=[count(Int64(1))[count]:Int64] |
| | ProjectionExec: expr=[], schema=[] |
| | UnnestExec, schema=[__unnest_placeholder(make_array(),depth=1):Utf8;N] |
| | ProjectionExec: expr=[[] as __unnest_placeholder(make_array())], schema=[__unnest_placeholder(make_array()):List(Field { name: "item", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} })] |
| | PlaceholderRowExec, schema=[] |
| | |
| physical_plan after OutputRequirements | OutputRequirementExec |
| | ProjectionExec: expr=[wat as key, count(Int64(1))@0 as count] |
| | AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))] |
| | AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))] |
| | ProjectionExec: expr=[] |
| | UnnestExec |
| | ProjectionExec: expr=[[] as __unnest_placeholder(make_array())] |
| | PlaceholderRowExec |
| | |
| physical_plan after aggregate_statistics | SAME TEXT AS ABOVE |
| physical_plan after join_selection | SAME TEXT AS ABOVE |
| physical_plan after LimitedDistinctAggregation | SAME TEXT AS ABOVE |
| physical_plan after EnforceDistribution | OutputRequirementExec |
| | ProjectionExec: expr=[wat as key, count(Int64(1))@0 as count] |
| | AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))] |
| | CoalescePartitionsExec |
| | AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))] |
| | RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1 |
| | ProjectionExec: expr=[] |
| | UnnestExec |
| | ProjectionExec: expr=[[] as __unnest_placeholder(make_array())] |
| | PlaceholderRowExec |
| | |
| physical_plan after CombinePartialFinalAggregate | SAME TEXT AS ABOVE |
| physical_plan after EnforceSorting | SAME TEXT AS ABOVE |
| physical_plan after OptimizeAggregateOrder | SAME TEXT AS ABOVE |
| physical_plan after ProjectionPushdown | SAME TEXT AS ABOVE |
| physical_plan after coalesce_batches | SAME TEXT AS ABOVE |
| physical_plan after OutputRequirements | ProjectionExec: expr=[wat as key, count(Int64(1))@0 as count] |
| | AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))] |
| | CoalescePartitionsExec |
| | AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))] |
| | RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1 |
| | ProjectionExec: expr=[] |
| | UnnestExec |
| | ProjectionExec: expr=[[] as __unnest_placeholder(make_array())] |
| | PlaceholderRowExec |
| | |
| physical_plan after LimitAggregation | SAME TEXT AS ABOVE |
| physical_plan after LimitPushdown | SAME TEXT AS ABOVE |
| physical_plan after ProjectionPushdown | SAME TEXT AS ABOVE |
| physical_plan after SanityCheckPlan | SAME TEXT AS ABOVE |
| physical_plan | ProjectionExec: expr=[wat as key, count(Int64(1))@0 as count] |
| | AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))] |
| | CoalescePartitionsExec |
| | AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))] |
| | RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1 |
| | ProjectionExec: expr=[] |
| | UnnestExec |
| | ProjectionExec: expr=[[] as __unnest_placeholder(make_array())] |
| | PlaceholderRowExec |
| | |
| physical_plan_with_stats | ProjectionExec: expr=[wat as key, count(Int64(1))@0 as count], statistics=[Rows=Exact(1), Bytes=Absent, [(Col[0]:),(Col[1]:)]] |
| | AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))], statistics=[Rows=Exact(1), Bytes=Absent, [(Col[0]:)]] |
| | CoalescePartitionsExec, statistics=[Rows=Absent, Bytes=Absent, [(Col[0]:)]] |
| | AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))], statistics=[Rows=Absent, Bytes=Absent, [(Col[0]:)]] |
| | RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1, statistics=[Rows=Absent, Bytes=Absent, []] |
| | ProjectionExec: expr=[], statistics=[Rows=Absent, Bytes=Absent, []] |
| | UnnestExec, statistics=[Rows=Absent, Bytes=Absent, [(Col[0]:)]] |
| | ProjectionExec: expr=[[] as __unnest_placeholder(make_array())], statistics=[Rows=Exact(1), Bytes=Exact(0), [(Col[0]:)]] |
| | PlaceholderRowExec, statistics=[Rows=Exact(1), Bytes=Exact(0), []] |
| | |
| physical_plan_with_schema | ProjectionExec: expr=[wat as key, count(Int64(1))@0 as count], schema=[key:Utf8, count:Int64] |
| | AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))], schema=[count(Int64(1)):Int64] |
| | CoalescePartitionsExec, schema=[count(Int64(1))[count]:Int64] |
| | AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))], schema=[count(Int64(1))[count]:Int64] |
| | RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1, schema=[] |
| | ProjectionExec: expr=[], schema=[] |
| | UnnestExec, schema=[__unnest_placeholder(make_array(),depth=1):Utf8;N] |
| | ProjectionExec: expr=[[] as __unnest_placeholder(make_array())], schema=[__unnest_placeholder(make_array()):List(Field { name: "item", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} })] |
| | PlaceholderRowExec, schema=[] |
| | |
+------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
66 row(s) fetched.
Elapsed 0.006 seconds.
This appears to be fixed on current main — I can no longer reproduce it.
Both variants from the report now return zero rows:
SELECT 'wat' AS key, count(1) AS c FROM (SELECT * FROM unnest(array[]::bigint[])) AS b(field) GROUP BY key;
-- 0 rows
and the same against a registered empty table. The optimized plan keeps the grouping rather than dropping it:
Aggregate: groupBy=[[Utf8("wat")]], aggr=[[count(Int64(1))]]
The fix looks to be #22132 ("fix: eliminate group by constant empty input", merged 2026-07-27), which matches the root cause @Dandandan identified here — EliminateGroupByConstant now declines to eliminate constant GROUP BY expressions when doing so would remove all of the grouping expressions:
if redundant.is_empty() || required.is_empty() {
return Ok(Transformed::no(LogicalPlan::Aggregate(aggregate)));
}
That PR closed #11748 but not this one, so this issue looks safe to close as already fixed.