Skip to content

Add test to verify count aggregate function should not be nullable #12085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion datafusion/physical-plan/src/aggregates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<AggregateFunctionExpr>();
// 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()?)
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -5364,6 +5364,11 @@ SELECT MAX(col0) FROM empty WHERE col0=1;
----
NULL

query I
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On main this query fails like this:

creatDataFusion CLI v41.0.0
> create table empty;
0 row(s) fetched.
Elapsed 0.008 seconds.

> select distinct count() from empty;
CombinePartialFinalAggregate
caused by
Internal error: PhysicalOptimizer rule 'CombinePartialFinalAggregate' failed, due to generate a different schema, original schema: Schema { fields: [Field { name: "count()", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }], metadata: {} }, new schema: Schema { fields: [Field { name: "count()", data_type: Int64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }], metadata: {} }.
This was likely caused by a bug in DataFusion's code and we would welcome that you file an bug report in our issue tracker

select distinct count() from empty;
----
0

query TT
EXPLAIN SELECT MIN(col0) FROM empty;
----
Expand Down