Skip to content

Commit b7c63e2

Browse files
HairstonEkosiew
authored andcommitted
fix: eliminate group by constant empty input (apache#22132)
## Which issue does this PR close? - Closes apache#11748 ## Rationale for this change `EliminateGroupByConstant` removes GROUP BY expressions that are constants. If all of the GROUP BY expressions are constants, eliminating all of them results in converting a grouped aggregate into an ungrouped (global) aggregate. This changes the semantics of the query: a grouped aggregate query on an empty input returns zero rows, whereas an ungrouped aggregate query produces a single row. ## What changes are included in this PR? `EliminateGroupByConstant` now declines to eliminate constant GROUP BY expressions, if doing so would result in removing all of the grouping expressions. ## Are these changes tested? Yes. Sqllogictest reproducer for the original issue, updated unit test and optimizer SLT expectations. ## Are there any user-facing changes? Queries with all-constant GROUP BY may now return fewer (correct) rows.
1 parent 88e18bb commit b7c63e2

3 files changed

Lines changed: 37 additions & 19 deletions

File tree

‎datafusion/optimizer/src/eliminate_group_by_constant.rs‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ impl OptimizerRule for EliminateGroupByConstant {
6464
.group_expr
6565
.iter()
6666
.partition(|expr| is_redundant_group_expr(expr, &group_by_columns));
67-
68-
if redundant.is_empty()
69-
|| (required.is_empty() && aggregate.aggr_expr.is_empty())
70-
{
67+
// Return now if no simplification can be done. We also bail out
68+
// if applying the optimization would eliminate all of the
69+
// grouping expressions (e.g., GROUP BY on only constant
70+
// expressions): this would turn a grouped aggregate into an
71+
// ungrouped aggregate, which changes query semantics (grouped
72+
// aggregates produce an empty result set on an empty input,
73+
// whereas ungrouped aggregates return a single row).
74+
if redundant.is_empty() || required.is_empty() {
7175
return Ok(Transformed::no(LogicalPlan::Aggregate(aggregate)));
7276
}
7377

@@ -221,16 +225,15 @@ mod tests {
221225
}
222226

223227
#[test]
224-
fn test_eliminate_constant() -> Result<()> {
228+
fn test_no_op_only_constant_with_aggregate() -> Result<()> {
225229
let scan = test_table_scan()?;
226230
let plan = LogicalPlanBuilder::from(scan)
227231
.aggregate(vec![lit("test"), lit(123u32)], vec![count(col("c"))])?
228232
.build()?;
229233

230234
assert_optimized_plan_equal!(plan, @r#"
231-
Projection: Utf8("test"), UInt32(123), count(test.c)
232-
Aggregate: groupBy=[[]], aggr=[[count(test.c)]]
233-
TableScan: test
235+
Aggregate: groupBy=[[Utf8("test"), UInt32(123)]], aggr=[[count(test.c)]]
236+
TableScan: test
234237
"#)
235238
}
236239

‎datafusion/sqllogictest/test_files/aggregate.slt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8047,6 +8047,23 @@ CREATE TABLE t1(v1 int);
80478047
statement error DataFusion error: Error during planning: Aggregate functions are not allowed in the WHERE clause. Consider using HAVING instead
80488048
SELECT v1 FROM t1 WHERE ((count(v1) % 1) << 1) > 0;
80498049

8050+
# issue: https://github.com/apache/datafusion/issues/11748
8051+
query R
8052+
SELECT AVG(v1) FROM t1 GROUP BY false HAVING false;
8053+
----
8054+
8055+
query R
8056+
SELECT AVG(v1) FROM t1 GROUP BY false;
8057+
----
8058+
8059+
statement ok
8060+
INSERT INTO t1 VALUES (1), (2), (3);
8061+
8062+
query R
8063+
SELECT AVG(v1) FROM t1 GROUP BY false;
8064+
----
8065+
2
8066+
80508067
statement ok
80518068
DROP TABLE t1;
80528069

‎datafusion/sqllogictest/test_files/optimizer_group_by_constant.slt‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,9 @@ FROM test_table t
6060
group by 1, 2, 3
6161
----
6262
logical_plan
63-
01)Projection: Int64(123), Int64(456), Int64(789), count(Int64(1)), avg(t.c12)
64-
02)--Aggregate: groupBy=[[]], aggr=[[count(Int64(1)), avg(t.c12)]]
65-
03)----SubqueryAlias: t
66-
04)------TableScan: test_table projection=[c12]
63+
01)Aggregate: groupBy=[[Int64(123), Int64(456), Int64(789)]], aggr=[[count(Int64(1)), avg(t.c12)]]
64+
02)--SubqueryAlias: t
65+
03)----TableScan: test_table projection=[c12]
6766

6867
query TT
6968
EXPLAIN
@@ -72,8 +71,8 @@ FROM test_table t
7271
GROUP BY 1, 2
7372
----
7473
logical_plan
75-
01)Projection: Date32("2023-05-04") AS dt, Boolean(true) AS today_filter, count(Int64(1))
76-
02)--Aggregate: groupBy=[[]], aggr=[[count(Int64(1))]]
74+
01)Projection: to_date(Utf8("2023-05-04")) AS dt, date_part(Utf8("DAY"),now()) < Int64(1000) AS today_filter, count(Int64(1))
75+
02)--Aggregate: groupBy=[[Date32("2023-05-04") AS to_date(Utf8("2023-05-04")), Boolean(true) AS date_part(Utf8("DAY"),now()) < Int64(1000)]], aggr=[[count(Int64(1))]]
7776
03)----SubqueryAlias: t
7877
04)------TableScan: test_table projection=[]
7978

@@ -90,10 +89,9 @@ FROM test_table t
9089
GROUP BY 1
9190
----
9291
logical_plan
93-
01)Projection: Boolean(true) AS NOT date_part(Utf8("MONTH"),now()) BETWEEN Int64(50) AND Int64(60), count(Int64(1))
94-
02)--Aggregate: groupBy=[[]], aggr=[[count(Int64(1))]]
95-
03)----SubqueryAlias: t
96-
04)------TableScan: test_table projection=[]
92+
01)Aggregate: groupBy=[[Boolean(true) AS NOT date_part(Utf8("MONTH"),now()) BETWEEN Int64(50) AND Int64(60)]], aggr=[[count(Int64(1))]]
93+
02)--SubqueryAlias: t
94+
03)----TableScan: test_table projection=[]
9795

9896
query TT
9997
EXPLAIN
@@ -119,7 +117,7 @@ logical_plan
119117

120118
# Config reset
121119

122-
# The SLT runner sets `target_partitions` to 4 instead of using the default, so
120+
# The SLT runner sets `target_partitions` to 4 instead of using the default, so
123121
# reset it explicitly.
124122
statement ok
125123
set datafusion.execution.target_partitions = 4;

0 commit comments

Comments
 (0)