Skip to content
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

fix: Fix stddev indeterministically producing NAN #13248

Merged
merged 1 commit into from
Nov 5, 2024
Merged
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
33 changes: 33 additions & 0 deletions datafusion/functions-aggregate/src/variance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ fn merge(
mean2: f64,
m22: f64,
) -> (u64, f64, f64) {
debug_assert!(count != 0 || count2 != 0, "Cannot merge two empty states");
Copy link
Member

Choose a reason for hiding this comment

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

I don't exactly know how it works. Is supporting this not needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is a precondition of using this function. So support is not needed here, but expected to be handled by callers. When both counts are 0 then also mean and m2 is 0 and the correct result would also be all 0.

Copy link
Member

Choose a reason for hiding this comment

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

i should have checked the callers. thanks. 👍

let new_count = count + count2;
let new_mean =
mean * count as f64 / new_count as f64 + mean2 * count2 as f64 / new_count as f64;
Expand Down Expand Up @@ -573,6 +574,9 @@ impl GroupsAccumulator for VarianceGroupsAccumulator {
partial_m2s,
opt_filter,
|group_index, partial_count, partial_mean, partial_m2| {
if partial_count == 0 {
return;
}
let (new_count, new_mean, new_m2) = merge(
self.counts[group_index],
self.means[group_index],
Expand Down Expand Up @@ -612,3 +616,32 @@ impl GroupsAccumulator for VarianceGroupsAccumulator {
+ self.counts.capacity() * size_of::<u64>()
}
}

#[cfg(test)]
mod tests {
use datafusion_expr::EmitTo;

use super::*;

#[test]
fn test_groups_accumulator_merge_empty_states() -> Result<()> {
let state_1 = vec![
Arc::new(UInt64Array::from(vec![0])) as ArrayRef,
Arc::new(Float64Array::from(vec![0.0])),
Arc::new(Float64Array::from(vec![0.0])),
];
let state_2 = vec![
Arc::new(UInt64Array::from(vec![2])) as ArrayRef,
Arc::new(Float64Array::from(vec![1.0])),
Arc::new(Float64Array::from(vec![1.0])),
];
let mut acc = VarianceGroupsAccumulator::new(StatsType::Sample);
acc.merge_batch(&state_1, &[0], None, 1)?;
acc.merge_batch(&state_2, &[0], None, 1)?;
let result = acc.evaluate(EmitTo::All)?;
let result = result.as_any().downcast_ref::<Float64Array>().unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result.value(0), 1.0);
Ok(())
}
}