Skip to content

Apache DataFusion SQL Query Engine

fix(agg/corr): return NULL when variance is zero or samples < 2 (#17621)

dennis zhuang9bc82029ae69
diff --git a/datafusion/functions-aggregate/src/correlation.rs b/datafusion/functions-aggregate/src/correlation.rs
index 47fdb7504d..20f23662ca 100644
--- a/datafusion/functions-aggregate/src/correlation.rs
+++ b/datafusion/functions-aggregate/src/correlation.rs
@@ -194,6 +194,11 @@ impl Accumulator for CorrelationAccumulator {
}
fn evaluate(&mut self) -> Result<ScalarValue> {
+ let n = self.covar.get_count();
+ if n < 2 {
+ return Ok(ScalarValue::Float64(None));
+ }
+
let covar = self.covar.evaluate()?;
let stddev1 = self.stddev1.evaluate()?;
let stddev2 = self.stddev2.evaluate()?;
@@ -202,7 +207,7 @@ impl Accumulator for CorrelationAccumulator {
if let ScalarValue::Float64(Some(s1)) = stddev1 {
if let ScalarValue::Float64(Some(s2)) = stddev2 {
if s1 == 0_f64 || s2 == 0_f64 {
- return Ok(ScalarValue::Float64(Some(0_f64)));
+ return Ok(ScalarValue::Float64(None));
} else {
return Ok(ScalarValue::Float64(Some(c / s1 / s2)));
}
@@ -459,11 +464,8 @@ impl GroupsAccumulator for CorrelationGroupsAccumulator {
// the `denominator` state is 0. In these cases, the final aggregation
// result should be `Null` (according to PostgreSQL's behavior).
//
- // TODO: Old datafusion implementation returns 0.0 for these invalid cases.
- // Update this to match PostgreSQL's behavior.
for i in 0..n {
if self.count[i] < 2 {
- // TODO: Evaluate as `Null` (see notes above)
values.push(0.0);
nulls.append_null();
continue;
@@ -484,7 +486,6 @@ impl GroupsAccumulator for CorrelationGroupsAccumulator {
((sum_xx - sum_x * mean_x) * (sum_yy - sum_y * mean_y)).sqrt();
if denominator == 0.0 {
- // TODO: Evaluate as `Null` (see notes above)
values.push(0.0);
nulls.append_null();
} else {
diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt
index 5375159c51..4601d595e3 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -557,7 +557,7 @@ SELECT corr(c2, c12) FROM aggregate_test_100
query R
select corr(sq.column1, sq.column2) from (values (1.1, 2.2)) as sq
----
-0
+NULL
# all_nulls_query_correlation
query R
@@ -2740,7 +2740,7 @@ CREATE OR REPLACE TABLE corr_single_row(
query R
SELECT corr(x, y) FROM corr_single_row;
----
-0
+NULL
# correlation with all nulls
statement ok