|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_error::VortexResult; |
| 5 | + |
| 6 | +use crate::ArrayRef; |
| 7 | +use crate::Columnar; |
| 8 | +use crate::ExecutionCtx; |
| 9 | +use crate::IntoArray; |
| 10 | +use crate::aggregate_fn::AggregateFnId; |
| 11 | +use crate::aggregate_fn::AggregateFnVTable; |
| 12 | +use crate::aggregate_fn::EmptyOptions; |
| 13 | +use crate::aggregate_fn::fns::nan_count::nan_count; |
| 14 | +use crate::dtype::DType; |
| 15 | +use crate::dtype::Nullability; |
| 16 | +use crate::scalar::Scalar; |
| 17 | + |
| 18 | +/// Compute whether every value in an array is NaN. |
| 19 | +/// |
| 20 | +/// This is a pruning aggregate, not just a convenience wrapper around |
| 21 | +/// [`NanCount`][crate::aggregate_fn::fns::nan_count::NanCount]. Pruning aggregates must prove a |
| 22 | +/// row-wise fact for every value in the scope, so their partials remain valid when a stats column is |
| 23 | +/// sliced or concatenated alongside the data. [`NanCount`][crate::aggregate_fn::fns::nan_count::NanCount] |
| 24 | +/// carries cross-row count information instead, so it is useful as a legacy storage format but not |
| 25 | +/// as the pruning expression itself. |
| 26 | +#[derive(Clone, Debug)] |
| 27 | +pub struct AllNan; |
| 28 | + |
| 29 | +impl AggregateFnVTable for AllNan { |
| 30 | + type Options = EmptyOptions; |
| 31 | + type Partial = bool; |
| 32 | + |
| 33 | + fn id(&self) -> AggregateFnId { |
| 34 | + AggregateFnId::new("vortex.all_nan") |
| 35 | + } |
| 36 | + |
| 37 | + fn serialize(&self, _options: &Self::Options) -> VortexResult<Option<Vec<u8>>> { |
| 38 | + Ok(None) |
| 39 | + } |
| 40 | + |
| 41 | + fn return_dtype(&self, _options: &Self::Options, _input_dtype: &DType) -> Option<DType> { |
| 42 | + Some(DType::Bool(Nullability::NonNullable)) |
| 43 | + } |
| 44 | + |
| 45 | + fn partial_dtype(&self, options: &Self::Options, input_dtype: &DType) -> Option<DType> { |
| 46 | + self.return_dtype(options, input_dtype) |
| 47 | + } |
| 48 | + |
| 49 | + fn empty_partial( |
| 50 | + &self, |
| 51 | + _options: &Self::Options, |
| 52 | + input_dtype: &DType, |
| 53 | + ) -> VortexResult<Self::Partial> { |
| 54 | + Ok(has_nans(input_dtype)) |
| 55 | + } |
| 56 | + |
| 57 | + fn combine_partials(&self, partial: &mut Self::Partial, other: Scalar) -> VortexResult<()> { |
| 58 | + *partial &= bool::try_from(&other)?; |
| 59 | + Ok(()) |
| 60 | + } |
| 61 | + |
| 62 | + fn to_scalar(&self, partial: &Self::Partial) -> VortexResult<Scalar> { |
| 63 | + Ok(Scalar::bool(*partial, Nullability::NonNullable)) |
| 64 | + } |
| 65 | + |
| 66 | + fn reset(&self, partial: &mut Self::Partial) { |
| 67 | + *partial = true; |
| 68 | + } |
| 69 | + |
| 70 | + fn is_saturated(&self, partial: &Self::Partial) -> bool { |
| 71 | + !*partial |
| 72 | + } |
| 73 | + |
| 74 | + fn try_accumulate( |
| 75 | + &self, |
| 76 | + state: &mut Self::Partial, |
| 77 | + batch: &ArrayRef, |
| 78 | + ctx: &mut ExecutionCtx, |
| 79 | + ) -> VortexResult<bool> { |
| 80 | + if !has_nans(batch.dtype()) { |
| 81 | + *state = false; |
| 82 | + return Ok(true); |
| 83 | + } |
| 84 | + |
| 85 | + *state &= nan_count(batch, ctx)? == batch.len(); |
| 86 | + Ok(true) |
| 87 | + } |
| 88 | + |
| 89 | + fn accumulate( |
| 90 | + &self, |
| 91 | + partial: &mut Self::Partial, |
| 92 | + batch: &Columnar, |
| 93 | + ctx: &mut ExecutionCtx, |
| 94 | + ) -> VortexResult<()> { |
| 95 | + let array = match batch { |
| 96 | + Columnar::Constant(c) => c.clone().into_array(), |
| 97 | + Columnar::Canonical(c) => c.clone().into_array(), |
| 98 | + }; |
| 99 | + if !has_nans(array.dtype()) { |
| 100 | + *partial = false; |
| 101 | + return Ok(()); |
| 102 | + } |
| 103 | + |
| 104 | + *partial &= nan_count(&array, ctx)? == array.len(); |
| 105 | + Ok(()) |
| 106 | + } |
| 107 | + |
| 108 | + fn finalize(&self, partials: ArrayRef) -> VortexResult<ArrayRef> { |
| 109 | + Ok(partials) |
| 110 | + } |
| 111 | + |
| 112 | + fn finalize_scalar(&self, partial: &Self::Partial) -> VortexResult<Scalar> { |
| 113 | + self.to_scalar(partial) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +fn has_nans(dtype: &DType) -> bool { |
| 118 | + matches!(dtype, DType::Primitive(ptype, _) if ptype.is_float()) |
| 119 | +} |
| 120 | + |
| 121 | +#[cfg(test)] |
| 122 | +mod tests { |
| 123 | + use vortex_error::VortexResult; |
| 124 | + |
| 125 | + use crate::IntoArray; |
| 126 | + use crate::LEGACY_SESSION; |
| 127 | + use crate::VortexSessionExecute; |
| 128 | + use crate::aggregate_fn::Accumulator; |
| 129 | + use crate::aggregate_fn::DynAccumulator; |
| 130 | + use crate::aggregate_fn::EmptyOptions; |
| 131 | + use crate::aggregate_fn::fns::all_nan::AllNan; |
| 132 | + use crate::arrays::PrimitiveArray; |
| 133 | + use crate::dtype::DType; |
| 134 | + use crate::dtype::Nullability; |
| 135 | + use crate::dtype::PType; |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn all_nan_aggregate_fn() -> VortexResult<()> { |
| 139 | + let mut ctx = LEGACY_SESSION.create_execution_ctx(); |
| 140 | + let dtype = DType::Primitive(PType::F32, Nullability::Nullable); |
| 141 | + let mut acc = Accumulator::try_new(AllNan, EmptyOptions, dtype)?; |
| 142 | + |
| 143 | + let batch = PrimitiveArray::from_option_iter([Some(f32::NAN), Some(f32::NAN)]).into_array(); |
| 144 | + acc.accumulate(&batch, &mut ctx)?; |
| 145 | + |
| 146 | + assert!(bool::try_from(&acc.finish()?)?); |
| 147 | + Ok(()) |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn all_nan_false_with_non_nan() -> VortexResult<()> { |
| 152 | + let mut ctx = LEGACY_SESSION.create_execution_ctx(); |
| 153 | + let dtype = DType::Primitive(PType::F32, Nullability::Nullable); |
| 154 | + let mut acc = Accumulator::try_new(AllNan, EmptyOptions, dtype)?; |
| 155 | + |
| 156 | + let batch = PrimitiveArray::from_option_iter([Some(f32::NAN), Some(1.0f32)]).into_array(); |
| 157 | + acc.accumulate(&batch, &mut ctx)?; |
| 158 | + |
| 159 | + assert!(!bool::try_from(&acc.finish()?)?); |
| 160 | + Ok(()) |
| 161 | + } |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn all_nan_false_for_non_float_values() -> VortexResult<()> { |
| 165 | + let mut ctx = LEGACY_SESSION.create_execution_ctx(); |
| 166 | + let dtype = DType::Primitive(PType::I32, Nullability::Nullable); |
| 167 | + let mut acc = Accumulator::try_new(AllNan, EmptyOptions, dtype)?; |
| 168 | + |
| 169 | + let batch = PrimitiveArray::from_option_iter([Some(1i32), None]).into_array(); |
| 170 | + acc.accumulate(&batch, &mut ctx)?; |
| 171 | + |
| 172 | + assert!(!bool::try_from(&acc.finish()?)?); |
| 173 | + Ok(()) |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn all_nan_false_for_empty_non_float_values() -> VortexResult<()> { |
| 178 | + let dtype = DType::Primitive(PType::I32, Nullability::Nullable); |
| 179 | + let mut acc = Accumulator::try_new(AllNan, EmptyOptions, dtype)?; |
| 180 | + |
| 181 | + assert!(!bool::try_from(&acc.finish()?)?); |
| 182 | + Ok(()) |
| 183 | + } |
| 184 | +} |
0 commit comments