|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_error::VortexExpect; |
| 5 | +use vortex_error::VortexResult; |
| 6 | + |
| 7 | +use crate::ArrayRef; |
| 8 | +use crate::Columnar; |
| 9 | +use crate::ExecutionCtx; |
| 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::dtype::PType; |
| 16 | +use crate::scalar::Scalar; |
| 17 | + |
| 18 | +/// Count the total number of elements in an array, including nulls. |
| 19 | +/// |
| 20 | +/// Applies to all types. Returns a `u64` count. |
| 21 | +/// The identity value is zero. |
| 22 | +/// |
| 23 | +/// Unlike [`Count`][crate::aggregate_fn::fns::count::Count], this aggregate includes |
| 24 | +/// null elements in the total. It is primarily used as a marker inside pruning |
| 25 | +/// predicates that need to refer to the scope row count. |
| 26 | +#[derive(Clone, Debug)] |
| 27 | +pub struct RowCount; |
| 28 | + |
| 29 | +impl AggregateFnVTable for RowCount { |
| 30 | + type Options = EmptyOptions; |
| 31 | + type Partial = u64; |
| 32 | + |
| 33 | + fn id(&self) -> AggregateFnId { |
| 34 | + AggregateFnId::new("vortex.row_count") |
| 35 | + } |
| 36 | + |
| 37 | + fn serialize(&self, _options: &Self::Options) -> VortexResult<Option<Vec<u8>>> { |
| 38 | + unimplemented!("RowCount is not yet serializable"); |
| 39 | + } |
| 40 | + |
| 41 | + fn return_dtype(&self, _options: &Self::Options, _input_dtype: &DType) -> Option<DType> { |
| 42 | + Some(DType::Primitive(PType::U64, 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(0u64) |
| 55 | + } |
| 56 | + |
| 57 | + fn combine_partials(&self, partial: &mut Self::Partial, other: Scalar) -> VortexResult<()> { |
| 58 | + let val = other |
| 59 | + .as_primitive() |
| 60 | + .typed_value::<u64>() |
| 61 | + .vortex_expect("row_count partial should not be null"); |
| 62 | + *partial += val; |
| 63 | + Ok(()) |
| 64 | + } |
| 65 | + |
| 66 | + fn to_scalar(&self, partial: &Self::Partial) -> VortexResult<Scalar> { |
| 67 | + Ok(Scalar::primitive(*partial, Nullability::NonNullable)) |
| 68 | + } |
| 69 | + |
| 70 | + fn reset(&self, partial: &mut Self::Partial) { |
| 71 | + *partial = 0; |
| 72 | + } |
| 73 | + |
| 74 | + #[inline] |
| 75 | + fn is_saturated(&self, _partial: &Self::Partial) -> bool { |
| 76 | + false |
| 77 | + } |
| 78 | + |
| 79 | + fn try_accumulate( |
| 80 | + &self, |
| 81 | + state: &mut Self::Partial, |
| 82 | + batch: &ArrayRef, |
| 83 | + _ctx: &mut ExecutionCtx, |
| 84 | + ) -> VortexResult<bool> { |
| 85 | + *state += batch.len() as u64; |
| 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 | + unreachable!("RowCount::try_accumulate handles all arrays") |
| 96 | + } |
| 97 | + |
| 98 | + fn finalize(&self, partials: ArrayRef) -> VortexResult<ArrayRef> { |
| 99 | + Ok(partials) |
| 100 | + } |
| 101 | + |
| 102 | + fn finalize_scalar(&self, partial: &Self::Partial) -> VortexResult<Scalar> { |
| 103 | + self.to_scalar(partial) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +#[cfg(test)] |
| 108 | +mod tests { |
| 109 | + use vortex_buffer::buffer; |
| 110 | + use vortex_error::VortexResult; |
| 111 | + |
| 112 | + use crate::IntoArray; |
| 113 | + use crate::LEGACY_SESSION; |
| 114 | + use crate::VortexSessionExecute; |
| 115 | + use crate::aggregate_fn::Accumulator; |
| 116 | + use crate::aggregate_fn::DynAccumulator; |
| 117 | + use crate::aggregate_fn::EmptyOptions; |
| 118 | + use crate::aggregate_fn::fns::row_count::RowCount; |
| 119 | + use crate::arrays::PrimitiveArray; |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn row_count_all_valid() -> VortexResult<()> { |
| 123 | + let array = buffer![1i32, 2, 3, 4, 5].into_array(); |
| 124 | + let mut ctx = LEGACY_SESSION.create_execution_ctx(); |
| 125 | + let mut acc = Accumulator::try_new(RowCount, EmptyOptions, array.dtype().clone())?; |
| 126 | + acc.accumulate(&array, &mut ctx)?; |
| 127 | + let result = acc.finish()?; |
| 128 | + assert_eq!(result.as_primitive().typed_value::<u64>(), Some(5)); |
| 129 | + Ok(()) |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn row_count_includes_nulls() -> VortexResult<()> { |
| 134 | + let array = PrimitiveArray::from_option_iter([Some(1i32), None, Some(3), None, Some(5)]) |
| 135 | + .into_array(); |
| 136 | + let mut ctx = LEGACY_SESSION.create_execution_ctx(); |
| 137 | + let mut acc = Accumulator::try_new(RowCount, EmptyOptions, array.dtype().clone())?; |
| 138 | + acc.accumulate(&array, &mut ctx)?; |
| 139 | + let result = acc.finish()?; |
| 140 | + assert_eq!(result.as_primitive().typed_value::<u64>(), Some(5)); |
| 141 | + Ok(()) |
| 142 | + } |
| 143 | +} |
0 commit comments