Skip to content

Commit f5439c8

Browse files
authored
AVG(null) is NULL (not zero) (#5008)
* avg(null) should be null * Fix code
1 parent 9c11996 commit f5439c8

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

datafusion/core/tests/sqllogictests/test_files/aggregate.slt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,3 +1183,47 @@ query RRRR
11831183
select var(sq.column1), var_pop(sq.column1), stddev(sq.column1), stddev_pop(sq.column1) from (values (1.0), (3.0)) as sq;
11841184
----
11851185
2 1 1.4142135623730951 1
1186+
1187+
1188+
# sum / count for all nulls
1189+
statement ok
1190+
create table the_nulls as values (null::bigint, 1), (null::bigint, 1), (null::bigint, 2);
1191+
1192+
# counts should be zeros (even for nulls)
1193+
query II
1194+
SELECT count(column1), column2 from the_nulls group by column2 order by column2;
1195+
----
1196+
0 1
1197+
0 2
1198+
1199+
# sums should be null
1200+
query II
1201+
SELECT sum(column1), column2 from the_nulls group by column2 order by column2;
1202+
----
1203+
NULL 1
1204+
NULL 2
1205+
1206+
# avg should be null
1207+
query II
1208+
SELECT avg(column1), column2 from the_nulls group by column2 order by column2;
1209+
----
1210+
NULL 1
1211+
NULL 2
1212+
1213+
# min should be null
1214+
query II
1215+
SELECT min(column1), column2 from the_nulls group by column2 order by column2;
1216+
----
1217+
NULL 1
1218+
NULL 2
1219+
1220+
# max should be null
1221+
query II
1222+
SELECT max(column1), column2 from the_nulls group by column2 order by column2;
1223+
----
1224+
NULL 1
1225+
NULL 2
1226+
1227+
1228+
statement ok
1229+
drop table the_nulls;

datafusion/physical-expr/src/aggregate/average.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl RowAccumulator for AvgRowAccumulator {
261261
assert_eq!(self.sum_datatype, DataType::Float64);
262262
Ok(match accessor.get_u64_opt(self.state_index()) {
263263
None => ScalarValue::Float64(None),
264-
Some(0) => ScalarValue::Float64(Some(0.0)),
264+
Some(0) => ScalarValue::Float64(None),
265265
Some(n) => ScalarValue::Float64(
266266
accessor
267267
.get_f64_opt(self.state_index() + 1)

0 commit comments

Comments
 (0)