From 9d3a47f21bfb1e226ea24efef2bf0246af14a7ea Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 21 Aug 2024 01:27:07 +0800 Subject: [PATCH] Fix: count aggregate function should not be nullable --- datafusion/physical-plan/src/aggregates/mod.rs | 12 +++++++++++- datafusion/sqllogictest/test_files/aggregate.slt | 5 +++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/datafusion/physical-plan/src/aggregates/mod.rs b/datafusion/physical-plan/src/aggregates/mod.rs index 89d4c452cca6..f253557d45da 100644 --- a/datafusion/physical-plan/src/aggregates/mod.rs +++ b/datafusion/physical-plan/src/aggregates/mod.rs @@ -46,6 +46,7 @@ use datafusion_physical_expr::{ LexRequirement, PhysicalExpr, PhysicalSortRequirement, }; +use datafusion_physical_expr_functions_aggregate::aggregate::AggregateFunctionExpr; use itertools::Itertools; pub mod group_values; @@ -817,7 +818,16 @@ fn create_schema( | AggregateMode::SinglePartitioned => { // in final mode, the field with the final result of the accumulator for expr in aggr_expr { - fields.push(expr.field()?) + let aggregte_func_expr = + expr.as_any().downcast_ref::(); + // count should not be nullable + if aggregte_func_expr.is_some() + && aggregte_func_expr.unwrap().fun().name() == "count" + { + fields.push(expr.field()?.with_nullable(false)) + } else { + fields.push(expr.field()?) + } } } } diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt index b8b93b28aff6..b7a30a0cb320 100644 --- a/datafusion/sqllogictest/test_files/aggregate.slt +++ b/datafusion/sqllogictest/test_files/aggregate.slt @@ -5364,6 +5364,11 @@ SELECT MAX(col0) FROM empty WHERE col0=1; ---- NULL +query I +select distinct count() from empty; +---- +0 + query TT EXPLAIN SELECT MIN(col0) FROM empty; ----