Skip to content

Commit

Permalink
Added check for aggregate functions in optimizer rules (apache#12860)
Browse files Browse the repository at this point in the history
* fix panic

* Update datafusion/sql/src/select.rs

Co-authored-by: Jonah Gao <[email protected]>

* move issue

---------

Co-authored-by: Jonah Gao <[email protected]>
  • Loading branch information
2 people authored and hailelagi committed Oct 14, 2024
1 parent 6fb557c commit f10227d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions datafusion/sql/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,16 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {

let filter_expr =
self.sql_to_expr(predicate_expr, plan.schema(), planner_context)?;

// Check for aggregation functions
let aggregate_exprs =
find_aggregate_exprs(std::slice::from_ref(&filter_expr));
if !aggregate_exprs.is_empty() {
return plan_err!(
"Aggregate functions are not allowed in the WHERE clause. Consider using HAVING instead"
);
}

let mut using_columns = HashSet::new();
expr_to_columns(&filter_expr, &mut using_columns)?;
let filter_expr = normalize_col_with_schemas_and_ambiguity_check(
Expand Down
10 changes: 10 additions & 0 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -5902,3 +5902,13 @@ ORDER BY k;
----
1 1.8125 6.8007813 Float16 Float16
2 8.5 8.5 Float16 Float16

statement ok
CREATE TABLE t1(v1 int);

# issue: https://github.com/apache/datafusion/issues/12814
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;

statement ok
DROP TABLE t1;

0 comments on commit f10227d

Please sign in to comment.