From 069118bef40b63549f4771e03ea1452a9293ee05 Mon Sep 17 00:00:00 2001 From: pchintar <89355405+pchintar@users.noreply.github.com> Date: Fri, 5 Jun 2026 15:03:43 +0530 Subject: [PATCH] Coerce aggregate FILTER predicates to boolean --- .../optimizer/src/analyzer/type_coercion.rs | 11 ++++++ .../sqllogictest/test_files/aggregate.slt | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/datafusion/optimizer/src/analyzer/type_coercion.rs b/datafusion/optimizer/src/analyzer/type_coercion.rs index df3ccc282564c..032fe2524096e 100644 --- a/datafusion/optimizer/src/analyzer/type_coercion.rs +++ b/datafusion/optimizer/src/analyzer/type_coercion.rs @@ -715,6 +715,12 @@ impl TreeNodeRewriter for TypeCoercionRewriter<'_> { }) => { let new_expr = coerce_arguments_for_signature(args, self.schema, func.as_ref())?; + + let filter = filter + .map(|filter| filter.cast_to(&DataType::Boolean, self.schema)) + .transpose()? + .map(Box::new); + Ok(Transformed::yes(Expr::AggregateFunction( expr::AggregateFunction::new_udf( func, @@ -752,6 +758,11 @@ impl TreeNodeRewriter for TypeCoercionRewriter<'_> { } }; + let filter = filter + .map(|filter| filter.cast_to(&DataType::Boolean, self.schema)) + .transpose()? + .map(Box::new); + let new_expr = Expr::from(WindowFunction { fun, params: expr::WindowFunctionParams { diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt index 2861b50580407..35ffdbeeeca09 100644 --- a/datafusion/sqllogictest/test_files/aggregate.slt +++ b/datafusion/sqllogictest/test_files/aggregate.slt @@ -6266,6 +6266,40 @@ GROUP BY g ---- 0 0 +# query_with_untyped_null_filter +query I +SELECT count(*) FILTER (WHERE NULL) +---- +0 + +query I +SELECT count(1) FILTER (WHERE NULL) +---- +0 + +query I +SELECT sum(1) FILTER (WHERE NULL) +---- +NULL + +query R +SELECT avg(1) FILTER (WHERE NULL) +---- +NULL + +# window_aggregate_with_untyped_null_filter +query I +SELECT count(*) FILTER (WHERE NULL) OVER () +FROM (VALUES (1)) AS t(x) +---- +0 + +query I +SELECT sum(1) FILTER (WHERE NULL) OVER () +FROM (VALUES (1)) AS t(x) +---- +NULL + # query_with_and_without_filter query III rowsort SELECT