Apache DataFusion SQL Query Engine
fix: Avoid special-casing `count(1)` in `Count::value_from_stats` (#25911)
Neil Conway62a65a1dc290
diff --git a/datafusion/functions-aggregate/src/count.rs b/datafusion/functions-aggregate/src/count.rsindex ed2abe8864..7c3bfdceef 100644--- a/datafusion/functions-aggregate/src/count.rs+++ b/datafusion/functions-aggregate/src/count.rs@@ -431,11 +431,15 @@ impl AggregateUDFImpl for Count {let count = i64::try_from(num_rows - val).ok()?;return Some(ScalarValue::Int64(Some(count)));}- } else if let Some(lit_expr) = expr.downcast_ref::<expressions::Literal>()- && lit_expr.value() == &COUNT_STAR_EXPANSION- {- let num_rows = i64::try_from(num_rows).ok()?;- return Some(ScalarValue::Int64(Some(num_rows)));+ } else if let Some(lit_expr) = expr.downcast_ref::<expressions::Literal>() {+ // A non-null literal is counted once per row (this covers the common+ // `count(*)` construct). A non-null literal is never counted.+ let count = if lit_expr.value().is_null() {+ 0+ } else {+ i64::try_from(num_rows).ok()?+ };+ return Some(ScalarValue::Int64(Some(count)));}Nonediff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.sltindex b9dacfa925..a53de58134 100644--- a/datafusion/sqllogictest/test_files/aggregate.slt+++ b/datafusion/sqllogictest/test_files/aggregate.slt@@ -8860,8 +8860,8 @@ logical_plan01)Aggregate: groupBy=[[]], aggr=[[count(Int64(NULL)) AS count(NULL)]]02)--TableScan: t projection=[]physical_plan-01)AggregateExec: mode=Single, gby=[], aggr=[count(NULL)]-02)--DataSourceExec: partitions=1, partition_sizes=[1]+01)ProjectionExec: expr=[0 as count(NULL)]+02)--PlaceholderRowExecstatement count 0drop table t;@@ -8934,8 +8934,8 @@ logical_plan01)Aggregate: groupBy=[[]], aggr=[[count(Int64(1)), count(Int64(2))]]02)--TableScan: t projection=[]physical_plan-01)AggregateExec: mode=Single, gby=[], aggr=[count(Int64(1)), count(Int64(2))]-02)--DataSourceExec: partitions=1, partition_sizes=[1]+01)ProjectionExec: expr=[2 as count(Int64(1)), 2 as count(Int64(2))]+02)--PlaceholderRowExecquery IIselect count(1), count() from t;@@ -8996,9 +8996,24 @@ logical_plan02)--Aggregate: groupBy=[[]], aggr=[[count(Int64(1)), count(Int64(2))]]03)----TableScan: t projection=[]physical_plan-01)ProjectionExec: expr=[count(Int64(1))@0 * count(Int64(2))@1 as count(Int64(1)) * count(Int64(2))]-02)--AggregateExec: mode=Single, gby=[], aggr=[count(Int64(1)), count(Int64(2))]-03)----DataSourceExec: partitions=1, partition_sizes=[1]+01)ProjectionExec: expr=[2 * 2 as count(Int64(1)) * count(Int64(2))]+02)--PlaceholderRowExec++# Any non-null literal counts every row, so it can be answered from statistics+query II+select count('a'), count(true) from t;+----+2 2++query TT+explain select count('a'), count(true) from t;+----+logical_plan+01)Aggregate: groupBy=[[]], aggr=[[count(Utf8("a")), count(Boolean(true))]]+02)--TableScan: t projection=[]+physical_plan+01)ProjectionExec: expr=[2 as count(Utf8("a")), 2 as count(Boolean(true))]+02)--PlaceholderRowExecstatement count 0drop table t;