|
| 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::dtype::DType; |
| 14 | +use crate::dtype::Nullability; |
| 15 | +use crate::scalar::Scalar; |
| 16 | + |
| 17 | +/// Compute whether every value in an array is null. |
| 18 | +#[derive(Clone, Debug)] |
| 19 | +pub struct AllNull; |
| 20 | + |
| 21 | +impl AggregateFnVTable for AllNull { |
| 22 | + type Options = EmptyOptions; |
| 23 | + type Partial = bool; |
| 24 | + |
| 25 | + fn id(&self) -> AggregateFnId { |
| 26 | + AggregateFnId::new("vortex.all_null") |
| 27 | + } |
| 28 | + |
| 29 | + fn serialize(&self, _options: &Self::Options) -> VortexResult<Option<Vec<u8>>> { |
| 30 | + Ok(None) |
| 31 | + } |
| 32 | + |
| 33 | + fn return_dtype(&self, _options: &Self::Options, _input_dtype: &DType) -> Option<DType> { |
| 34 | + Some(DType::Bool(Nullability::NonNullable)) |
| 35 | + } |
| 36 | + |
| 37 | + fn partial_dtype(&self, options: &Self::Options, input_dtype: &DType) -> Option<DType> { |
| 38 | + self.return_dtype(options, input_dtype) |
| 39 | + } |
| 40 | + |
| 41 | + fn empty_partial( |
| 42 | + &self, |
| 43 | + _options: &Self::Options, |
| 44 | + _input_dtype: &DType, |
| 45 | + ) -> VortexResult<Self::Partial> { |
| 46 | + Ok(true) |
| 47 | + } |
| 48 | + |
| 49 | + fn combine_partials(&self, partial: &mut Self::Partial, other: Scalar) -> VortexResult<()> { |
| 50 | + *partial &= bool::try_from(&other)?; |
| 51 | + Ok(()) |
| 52 | + } |
| 53 | + |
| 54 | + fn to_scalar(&self, partial: &Self::Partial) -> VortexResult<Scalar> { |
| 55 | + Ok(Scalar::bool(*partial, Nullability::NonNullable)) |
| 56 | + } |
| 57 | + |
| 58 | + fn reset(&self, partial: &mut Self::Partial) { |
| 59 | + *partial = true; |
| 60 | + } |
| 61 | + |
| 62 | + fn is_saturated(&self, partial: &Self::Partial) -> bool { |
| 63 | + !*partial |
| 64 | + } |
| 65 | + |
| 66 | + fn try_accumulate( |
| 67 | + &self, |
| 68 | + state: &mut Self::Partial, |
| 69 | + batch: &ArrayRef, |
| 70 | + ctx: &mut ExecutionCtx, |
| 71 | + ) -> VortexResult<bool> { |
| 72 | + *state &= batch.invalid_count(ctx)? == batch.len(); |
| 73 | + Ok(true) |
| 74 | + } |
| 75 | + |
| 76 | + fn accumulate( |
| 77 | + &self, |
| 78 | + partial: &mut Self::Partial, |
| 79 | + batch: &Columnar, |
| 80 | + ctx: &mut ExecutionCtx, |
| 81 | + ) -> VortexResult<()> { |
| 82 | + *partial &= match batch { |
| 83 | + Columnar::Constant(c) => c.is_empty() || c.scalar().is_null(), |
| 84 | + Columnar::Canonical(c) => { |
| 85 | + let array = c.clone().into_array(); |
| 86 | + array.invalid_count(ctx)? == array.len() |
| 87 | + } |
| 88 | + }; |
| 89 | + Ok(()) |
| 90 | + } |
| 91 | + |
| 92 | + fn finalize(&self, partials: ArrayRef) -> VortexResult<ArrayRef> { |
| 93 | + Ok(partials) |
| 94 | + } |
| 95 | + |
| 96 | + fn finalize_scalar(&self, partial: &Self::Partial) -> VortexResult<Scalar> { |
| 97 | + self.to_scalar(partial) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#[cfg(test)] |
| 102 | +mod tests { |
| 103 | + use vortex_error::VortexResult; |
| 104 | + |
| 105 | + use crate::IntoArray; |
| 106 | + use crate::LEGACY_SESSION; |
| 107 | + use crate::VortexSessionExecute; |
| 108 | + use crate::aggregate_fn::Accumulator; |
| 109 | + use crate::aggregate_fn::DynAccumulator; |
| 110 | + use crate::aggregate_fn::EmptyOptions; |
| 111 | + use crate::aggregate_fn::fns::all_null::AllNull; |
| 112 | + use crate::arrays::PrimitiveArray; |
| 113 | + use crate::dtype::DType; |
| 114 | + use crate::dtype::Nullability; |
| 115 | + use crate::dtype::PType; |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn all_null_aggregate_fn() -> VortexResult<()> { |
| 119 | + let mut ctx = LEGACY_SESSION.create_execution_ctx(); |
| 120 | + let dtype = DType::Primitive(PType::I32, Nullability::Nullable); |
| 121 | + let mut acc = Accumulator::try_new(AllNull, EmptyOptions, dtype)?; |
| 122 | + |
| 123 | + let batch = PrimitiveArray::from_option_iter::<i32, _>([None, None, None]).into_array(); |
| 124 | + acc.accumulate(&batch, &mut ctx)?; |
| 125 | + |
| 126 | + assert!(bool::try_from(&acc.finish()?)?); |
| 127 | + Ok(()) |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn all_null_false_with_non_nulls() -> VortexResult<()> { |
| 132 | + let mut ctx = LEGACY_SESSION.create_execution_ctx(); |
| 133 | + let dtype = DType::Primitive(PType::I32, Nullability::Nullable); |
| 134 | + let mut acc = Accumulator::try_new(AllNull, EmptyOptions, dtype)?; |
| 135 | + |
| 136 | + let batch = PrimitiveArray::from_option_iter([Some(1i32), None, Some(3)]).into_array(); |
| 137 | + acc.accumulate(&batch, &mut ctx)?; |
| 138 | + |
| 139 | + assert!(!bool::try_from(&acc.finish()?)?); |
| 140 | + Ok(()) |
| 141 | + } |
| 142 | +} |
0 commit comments