-
Notifications
You must be signed in to change notification settings - Fork 170
Return null for mean of all-null input #8389
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
Draft
dimitarvdimitrov
wants to merge
3
commits into
develop
Choose a base branch
from
mitko/aggregate/mean-all-null-returns-null
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+104
−24
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ use vortex_error::vortex_bail; | |
|
|
||
| use crate::ArrayRef; | ||
| use crate::ExecutionCtx; | ||
| use crate::IntoArray; | ||
| use crate::aggregate_fn::Accumulator; | ||
| use crate::aggregate_fn::AggregateFnId; | ||
| use crate::aggregate_fn::AggregateFnVTable; | ||
|
|
@@ -17,6 +18,7 @@ use crate::aggregate_fn::combined::CombinedOptions; | |
| use crate::aggregate_fn::combined::PairOptions; | ||
| use crate::aggregate_fn::fns::count::Count; | ||
| use crate::aggregate_fn::fns::sum::Sum; | ||
| use crate::arrays::ConstantArray; | ||
| use crate::builtins::ArrayBuiltins; | ||
| use crate::dtype::DType; | ||
| use crate::dtype::Nullability; | ||
|
|
@@ -89,7 +91,15 @@ impl BinaryCombined for Mean { | |
| }; | ||
| let sum_cast = sum.cast(target.clone())?; | ||
| let count_cast = count.cast(target)?; | ||
| sum_cast.binary(count_cast, Operator::Div) | ||
| let mean = sum_cast.binary(count_cast, Operator::Div)?; | ||
| // Nulls are skipped during accumulation, so an all-null group has a count of zero and | ||
| // the division produces 0/0 = NaN. The mean of an empty group is null (as in SQL), so | ||
| // mask out zero-count entries. This matches `finalize_scalar`. | ||
| let non_empty = count.binary( | ||
| ConstantArray::new(0u64, count.len()).into_array(), | ||
| Operator::NotEq, | ||
| )?; | ||
| mean.mask(non_empty) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| } | ||
|
|
||
| fn finalize_scalar(&self, left_scalar: Scalar, right_scalar: Scalar) -> VortexResult<Scalar> { | ||
|
|
@@ -104,9 +114,7 @@ impl BinaryCombined for Mean { | |
| let sum = sum_cast.as_primitive().typed_value::<f64>(); | ||
| let count = count_cast.as_primitive().typed_value::<f64>(); | ||
| let value = match (sum, count) { | ||
| (None, _) | (_, None) => return Ok(Scalar::null(target)), // Sum overflowed | ||
| // A count of zero yields 0/0 = NaN, matching the array `finalize` path: nulls are | ||
| // skipped during accumulation, so an all-null input is an empty mean, not null. | ||
| (None, _) | (_, None) | (_, Some(0.0)) => return Ok(Scalar::null(target)), // Sum overflowed | ||
| (Some(s), Some(c)) => s / c, | ||
| }; | ||
| Ok(Scalar::primitive(value, Nullability::Nullable)) | ||
|
|
@@ -164,12 +172,13 @@ mod tests { | |
| use vortex_error::VortexResult; | ||
|
|
||
| use super::*; | ||
| use crate::IntoArray; | ||
| use crate::LEGACY_SESSION; | ||
| use crate::VortexSessionExecute; | ||
| use crate::aggregate_fn::DynGroupedAccumulator; | ||
| use crate::aggregate_fn::GroupedAccumulator; | ||
| use crate::arrays::BoolArray; | ||
| use crate::arrays::ChunkedArray; | ||
| use crate::arrays::ConstantArray; | ||
| use crate::arrays::FixedSizeListArray; | ||
| use crate::arrays::PrimitiveArray; | ||
| use crate::validity::Validity; | ||
|
|
||
|
|
@@ -232,11 +241,84 @@ mod tests { | |
| } | ||
|
|
||
| #[test] | ||
| fn mean_all_null_returns_nan() -> VortexResult<()> { | ||
| fn mean_all_null_returns_null() -> VortexResult<()> { | ||
| let array = PrimitiveArray::from_option_iter::<f64, _>([None, None, None]).into_array(); | ||
| let mut ctx = LEGACY_SESSION.create_execution_ctx(); | ||
| let result = mean(&array, &mut ctx)?; | ||
| assert!(result.as_primitive().as_::<f64>().is_some_and(f64::is_nan)); | ||
| assert_eq!(result.as_primitive().as_::<f64>(), None); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Group inputs and expected means, exercised identically through the scalar-partial path | ||
| /// (`finalize_scalar`) and the grouped array path (`finalize`). | ||
| /// | ||
| /// Note that `Sum` skips NaN values while `Count` counts them as valid, so NaN elements | ||
| /// reduce the mean rather than poisoning it, e.g. `mean(NaN, 1, null) = 1/2`. | ||
| fn mean_cases() -> Vec<(Vec<Option<f64>>, Option<f64>)> { | ||
| vec![ | ||
| (vec![Some(f64::NAN), Some(1.0), None], Some(0.5)), | ||
| (vec![Some(f64::NAN), Some(1.0), Some(1.0)], Some(2.0 / 3.0)), | ||
| (vec![None, None, Some(f64::NAN)], Some(0.0)), | ||
| (vec![Some(f64::NAN), Some(1.0), Some(1.0)], Some(2.0 / 3.0)), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks wrong |
||
| (vec![None, None, None], None), | ||
| (vec![Some(1.0), Some(2.0), Some(3.0)], Some(2.0)), | ||
| ] | ||
| } | ||
|
|
||
| fn assert_mean(actual: Option<f64>, expected: Option<f64>, case: usize) { | ||
| match expected { | ||
| Some(e) if e.is_nan() => assert!( | ||
| actual.is_some_and(f64::is_nan), | ||
| "case {case}: expected NaN, got {actual:?}" | ||
| ), | ||
| _ => assert_eq!(actual, expected, "case {case}"), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn mean_via_combined_partials() -> VortexResult<()> { | ||
| let mut ctx = LEGACY_SESSION.create_execution_ctx(); | ||
| for (case, (group, expected)) in mean_cases().into_iter().enumerate() { | ||
| let mut acc = Accumulator::try_new( | ||
| Mean::combined(), | ||
| PairOptions(EmptyOptions, EmptyOptions), | ||
| DType::Primitive(PType::F64, Nullability::Nullable), | ||
| )?; | ||
| // Two batches per group so the result goes through partial combination and | ||
| // `finalize_scalar`. | ||
| let (head, tail) = group.split_at(2); | ||
| let head = PrimitiveArray::from_option_iter(head.iter().copied()).into_array(); | ||
| let tail = PrimitiveArray::from_option_iter(tail.iter().copied()).into_array(); | ||
| acc.accumulate(&head, &mut ctx)?; | ||
| acc.accumulate(&tail, &mut ctx)?; | ||
| let result = acc.finish()?; | ||
| assert_mean(result.as_primitive().as_::<f64>(), expected, case); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn mean_via_grouped_finalize() -> VortexResult<()> { | ||
| let cases = mean_cases(); | ||
| let elements = PrimitiveArray::from_option_iter( | ||
| cases.iter().flat_map(|(group, _)| group.iter().copied()), | ||
| ) | ||
| .into_array(); | ||
| let groups = FixedSizeListArray::try_new(elements, 3, Validity::NonNullable, cases.len())?; | ||
|
|
||
| let mut acc = GroupedAccumulator::try_new( | ||
| Mean::combined(), | ||
| PairOptions(EmptyOptions, EmptyOptions), | ||
| DType::Primitive(PType::F64, Nullability::Nullable), | ||
| )?; | ||
| let mut ctx = LEGACY_SESSION.create_execution_ctx(); | ||
| acc.accumulate_list(&groups.into_array(), &mut ctx)?; | ||
| let result = acc.finish()?; | ||
|
|
||
| for (case, (_, expected)) in cases.into_iter().enumerate() { | ||
| let actual = result.execute_scalar(case, &mut ctx)?; | ||
| assert_mean(actual.as_primitive().as_::<f64>(), expected, case); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't this need to be
targetdtype?