Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions datafusion/optimizer/src/eliminate_group_by_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ impl OptimizerRule for EliminateGroupByConstant {
.group_expr
.iter()
.partition(|expr| is_redundant_group_expr(expr, &group_by_columns));

if redundant.is_empty()
|| (required.is_empty() && aggregate.aggr_expr.is_empty())
{
// Return now if no simplification can be done. We also bail out
// if applying the optimization would eliminate all of the
// grouping expressions (e.g., GROUP BY on only constant
// expressions): this would turn a grouped aggregate into an
// ungrouped aggregate, which changes query semantics (grouped
// aggregates produce an empty result set on an empty input,
// whereas ungrouped aggregates return a single row).
if redundant.is_empty() || required.is_empty() {
return Ok(Transformed::no(LogicalPlan::Aggregate(aggregate)));
}

Expand Down Expand Up @@ -221,16 +225,15 @@ mod tests {
}

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

assert_optimized_plan_equal!(plan, @r#"
Projection: Utf8("test"), UInt32(123), count(test.c)
Aggregate: groupBy=[[]], aggr=[[count(test.c)]]
TableScan: test
Aggregate: groupBy=[[Utf8("test"), UInt32(123)]], aggr=[[count(test.c)]]
TableScan: test
"#)
}

Expand Down
17 changes: 17 additions & 0 deletions datafusion/sqllogictest/test_files/aggregate.slt
Comment thread
kumarUjjawal marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -8047,6 +8047,23 @@ CREATE TABLE t1(v1 int);
statement error DataFusion error: Error during planning: Aggregate functions are not allowed in the WHERE clause. Consider using HAVING instead
SELECT v1 FROM t1 WHERE ((count(v1) % 1) << 1) > 0;

# issue: https://github.com/apache/datafusion/issues/11748
query R
SELECT AVG(v1) FROM t1 GROUP BY false HAVING false;
----

query R
SELECT AVG(v1) FROM t1 GROUP BY false;
----

statement ok
INSERT INTO t1 VALUES (1), (2), (3);

query R
SELECT AVG(v1) FROM t1 GROUP BY false;
----
2

statement ok
DROP TABLE t1;

Expand Down
20 changes: 9 additions & 11 deletions datafusion/sqllogictest/test_files/optimizer_group_by_constant.slt
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ FROM test_table t
group by 1, 2, 3
----
logical_plan

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

these plans are changing but I don't see the actual query results changing. Is that expected?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These all have the new filter, but their outputs are unchanged because the filter passes whenever count > 0. The empty input tests are in aggregate.slt. Those tests on main would return 1 row of NULL, these changes return 0 rows.

01)Projection: Int64(123), Int64(456), Int64(789), count(Int64(1)), avg(t.c12)
02)--Aggregate: groupBy=[[]], aggr=[[count(Int64(1)), avg(t.c12)]]
03)----SubqueryAlias: t
04)------TableScan: test_table projection=[c12]
01)Aggregate: groupBy=[[Int64(123), Int64(456), Int64(789)]], aggr=[[count(Int64(1)), avg(t.c12)]]
02)--SubqueryAlias: t
03)----TableScan: test_table projection=[c12]

query TT
EXPLAIN
Expand All @@ -72,8 +71,8 @@ FROM test_table t
GROUP BY 1, 2
----
logical_plan
01)Projection: Date32("2023-05-04") AS dt, Boolean(true) AS today_filter, count(Int64(1))
02)--Aggregate: groupBy=[[]], aggr=[[count(Int64(1))]]
01)Projection: to_date(Utf8("2023-05-04")) AS dt, date_part(Utf8("DAY"),now()) < Int64(1000) AS today_filter, count(Int64(1))
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))]]
03)----SubqueryAlias: t
04)------TableScan: test_table projection=[]

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

query TT
EXPLAIN
Expand All @@ -119,7 +117,7 @@ logical_plan

# Config reset

# The SLT runner sets `target_partitions` to 4 instead of using the default, so
# The SLT runner sets `target_partitions` to 4 instead of using the default, so
# reset it explicitly.
statement ok
set datafusion.execution.target_partitions = 4;
Expand Down
Loading