This is an automated email from the ASF dual-hosted git repository.
jonah pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 6267ede369 Added check for aggregate functions in optimizer rules
(#12860)
6267ede369 is described below
commit 6267ede369f3458f517bf0306bb7191f42672916
Author: Jonathan Chen <[email protected]>
AuthorDate: Fri Oct 11 23:16:40 2024 -0400
Added check for aggregate functions in optimizer rules (#12860)
* fix panic
* Update datafusion/sql/src/select.rs
Co-authored-by: Jonah Gao <[email protected]>
* move issue
---------
Co-authored-by: Jonah Gao <[email protected]>
---
datafusion/sql/src/select.rs | 10 ++++++++++
datafusion/sqllogictest/test_files/aggregate.slt | 10 ++++++++++
2 files changed, 20 insertions(+)
diff --git a/datafusion/sql/src/select.rs b/datafusion/sql/src/select.rs
index c029fe2a23..69c7745165 100644
--- a/datafusion/sql/src/select.rs
+++ b/datafusion/sql/src/select.rs
@@ -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(
diff --git a/datafusion/sqllogictest/test_files/aggregate.slt
b/datafusion/sqllogictest/test_files/aggregate.slt
index 54ce91b8e7..ce382a9bf8 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -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;
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]