Skip to content

Commit

Permalink
AVG(null) is NULL (not zero) (apache#5008)
Browse files Browse the repository at this point in the history
* avg(null) should be null

* Fix code
  • Loading branch information
alamb authored Jan 21, 2023
1 parent 9c11996 commit f5439c8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
44 changes: 44 additions & 0 deletions datafusion/core/tests/sqllogictests/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1183,3 +1183,47 @@ query RRRR
select var(sq.column1), var_pop(sq.column1), stddev(sq.column1), stddev_pop(sq.column1) from (values (1.0), (3.0)) as sq;
----
2 1 1.4142135623730951 1


# sum / count for all nulls
statement ok
create table the_nulls as values (null::bigint, 1), (null::bigint, 1), (null::bigint, 2);

# counts should be zeros (even for nulls)
query II
SELECT count(column1), column2 from the_nulls group by column2 order by column2;
----
0 1
0 2

# sums should be null
query II
SELECT sum(column1), column2 from the_nulls group by column2 order by column2;
----
NULL 1
NULL 2

# avg should be null
query II
SELECT avg(column1), column2 from the_nulls group by column2 order by column2;
----
NULL 1
NULL 2

# min should be null
query II
SELECT min(column1), column2 from the_nulls group by column2 order by column2;
----
NULL 1
NULL 2

# max should be null
query II
SELECT max(column1), column2 from the_nulls group by column2 order by column2;
----
NULL 1
NULL 2


statement ok
drop table the_nulls;
2 changes: 1 addition & 1 deletion datafusion/physical-expr/src/aggregate/average.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl RowAccumulator for AvgRowAccumulator {
assert_eq!(self.sum_datatype, DataType::Float64);
Ok(match accessor.get_u64_opt(self.state_index()) {
None => ScalarValue::Float64(None),
Some(0) => ScalarValue::Float64(Some(0.0)),
Some(0) => ScalarValue::Float64(None),
Some(n) => ScalarValue::Float64(
accessor
.get_f64_opt(self.state_index() + 1)
Expand Down

0 comments on commit f5439c8

Please sign in to comment.