From 6bafabe43666c5a25ef8d2eb81ea4cb627cea1bd Mon Sep 17 00:00:00 2001 From: blaginin Date: Sun, 29 Mar 2026 22:21:10 +0100 Subject: [PATCH 1/6] `Count` and `Mean` aggregates Signed-off-by: blaginin Co-authored-by: Claude --- .../src/aggregate_fn/accumulator_grouped.rs | 4 +- .../src/aggregate_fn/fns/count/mod.rs | 267 ++++++++++ vortex-array/src/aggregate_fn/fns/mean/mod.rs | 472 ++++++++++++++++++ vortex-array/src/aggregate_fn/fns/mod.rs | 2 + 4 files changed, 743 insertions(+), 2 deletions(-) create mode 100644 vortex-array/src/aggregate_fn/fns/count/mod.rs create mode 100644 vortex-array/src/aggregate_fn/fns/mean/mod.rs diff --git a/vortex-array/src/aggregate_fn/accumulator_grouped.rs b/vortex-array/src/aggregate_fn/accumulator_grouped.rs index a4d9c38b60e..15b0fbc9326 100644 --- a/vortex-array/src/aggregate_fn/accumulator_grouped.rs +++ b/vortex-array/src/aggregate_fn/accumulator_grouped.rs @@ -237,7 +237,7 @@ impl GroupedAccumulator { if validity.value(offset) { let group = elements.slice(offset..offset + size)?; accumulator.accumulate(&group, ctx)?; - states.append_scalar(&accumulator.finish()?)?; + states.append_scalar(&accumulator.flush()?)?; } else { states.append_null() } @@ -309,7 +309,7 @@ impl GroupedAccumulator { if validity.value(i) { let group = elements.slice(offset..offset + size)?; accumulator.accumulate(&group, ctx)?; - states.append_scalar(&accumulator.finish()?)?; + states.append_scalar(&accumulator.flush()?)?; } else { states.append_null() } diff --git a/vortex-array/src/aggregate_fn/fns/count/mod.rs b/vortex-array/src/aggregate_fn/fns/count/mod.rs new file mode 100644 index 00000000000..3c483372186 --- /dev/null +++ b/vortex-array/src/aggregate_fn/fns/count/mod.rs @@ -0,0 +1,267 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright the Vortex contributors + +use vortex_error::VortexExpect; +use vortex_error::VortexResult; + +use crate::ArrayRef; +use crate::Columnar; +use crate::ExecutionCtx; +use crate::aggregate_fn::Accumulator; +use crate::aggregate_fn::AggregateFnId; +use crate::aggregate_fn::AggregateFnVTable; +use crate::aggregate_fn::DynAccumulator; +use crate::aggregate_fn::EmptyOptions; +use crate::dtype::DType; +use crate::dtype::Nullability; +use crate::dtype::PType; +use crate::scalar::Scalar; + +/// Return the count of non-null elements in an array. +/// +/// See [`Count`] for details. +pub fn count(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult { + let mut acc = Accumulator::try_new(Count, EmptyOptions, array.dtype().clone())?; + acc.accumulate(array, ctx)?; + let result = acc.finish()?; + + Ok(result + .as_primitive() + .typed_value::() + .vortex_expect("count result should not be null")) +} + +/// Count the number of non-null elements in an array. +/// +/// Applies to all types. Returns a `u64` count. +/// The identity value is zero. +#[derive(Clone, Debug)] +pub struct Count; + +impl AggregateFnVTable for Count { + type Options = EmptyOptions; + type Partial = u64; + + fn id(&self) -> AggregateFnId { + AggregateFnId::new_ref("vortex.count") + } + + fn serialize(&self, _options: &Self::Options) -> VortexResult>> { + Ok(Some(vec![])) + } + + fn deserialize( + &self, + _metadata: &[u8], + _session: &vortex_session::VortexSession, + ) -> VortexResult { + Ok(EmptyOptions) + } + + fn return_dtype(&self, _options: &Self::Options, _input_dtype: &DType) -> Option { + Some(DType::Primitive(PType::U64, Nullability::NonNullable)) + } + + fn partial_dtype(&self, options: &Self::Options, input_dtype: &DType) -> Option { + self.return_dtype(options, input_dtype) + } + + fn empty_partial( + &self, + _options: &Self::Options, + _input_dtype: &DType, + ) -> VortexResult { + Ok(0u64) + } + + fn combine_partials(&self, partial: &mut Self::Partial, other: Scalar) -> VortexResult<()> { + let val = other + .as_primitive() + .typed_value::() + .vortex_expect("count partial should not be null"); + *partial += val; + Ok(()) + } + + fn to_scalar(&self, partial: &Self::Partial) -> VortexResult { + Ok(Scalar::primitive(*partial, Nullability::NonNullable)) + } + + fn reset(&self, partial: &mut Self::Partial) { + *partial = 0; + } + + #[inline] + fn is_saturated(&self, _partial: &Self::Partial) -> bool { + false + } + + fn accumulate( + &self, + partial: &mut Self::Partial, + batch: &Columnar, + _ctx: &mut ExecutionCtx, + ) -> VortexResult<()> { + match batch { + Columnar::Constant(c) => { + if !c.scalar().is_null() { + *partial += c.len() as u64; + } + } + Columnar::Canonical(c) => { + let valid = c.as_ref().valid_count()?; + *partial += valid as u64; + } + } + Ok(()) + } + + fn finalize(&self, partials: ArrayRef) -> VortexResult { + Ok(partials) + } + + fn finalize_scalar(&self, partial: &Self::Partial) -> VortexResult { + self.to_scalar(partial) + } +} + +#[cfg(test)] +mod tests { + use vortex_buffer::buffer; + use vortex_error::VortexResult; + + use crate::IntoArray; + use crate::LEGACY_SESSION; + use crate::VortexSessionExecute; + use crate::aggregate_fn::Accumulator; + use crate::aggregate_fn::AggregateFnVTable; + use crate::aggregate_fn::DynAccumulator; + use crate::aggregate_fn::EmptyOptions; + use crate::aggregate_fn::fns::count::Count; + use crate::aggregate_fn::fns::count::count; + use crate::arrays::ChunkedArray; + use crate::arrays::ConstantArray; + use crate::arrays::PrimitiveArray; + use crate::dtype::DType; + use crate::dtype::Nullability; + use crate::dtype::PType; + use crate::scalar::Scalar; + use crate::validity::Validity; + + #[test] + fn count_all_valid() -> VortexResult<()> { + let array = + PrimitiveArray::new(buffer![1i32, 2, 3, 4, 5], Validity::NonNullable).into_array(); + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + assert_eq!(count(&array, &mut ctx)?, 5); + Ok(()) + } + + #[test] + fn count_with_nulls() -> VortexResult<()> { + let array = PrimitiveArray::from_option_iter([Some(1i32), None, Some(3), None, Some(5)]) + .into_array(); + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + assert_eq!(count(&array, &mut ctx)?, 3); + Ok(()) + } + + #[test] + fn count_all_null() -> VortexResult<()> { + let array = PrimitiveArray::from_option_iter::([None, None, None]).into_array(); + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + assert_eq!(count(&array, &mut ctx)?, 0); + Ok(()) + } + + #[test] + fn count_empty() -> VortexResult<()> { + let dtype = DType::Primitive(PType::I32, Nullability::NonNullable); + let mut acc = Accumulator::try_new(Count, EmptyOptions, dtype)?; + let result = acc.finish()?; + assert_eq!(result.as_primitive().typed_value::(), Some(0)); + Ok(()) + } + + #[test] + fn count_multi_batch() -> VortexResult<()> { + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + let dtype = DType::Primitive(PType::I32, Nullability::Nullable); + let mut acc = Accumulator::try_new(Count, EmptyOptions, dtype)?; + + let batch1 = PrimitiveArray::from_option_iter([Some(1i32), None, Some(3)]).into_array(); + acc.accumulate(&batch1, &mut ctx)?; + + let batch2 = PrimitiveArray::from_option_iter([None, Some(5i32)]).into_array(); + acc.accumulate(&batch2, &mut ctx)?; + + let result = acc.finish()?; + assert_eq!(result.as_primitive().typed_value::(), Some(3)); + Ok(()) + } + + #[test] + fn count_finish_resets_state() -> VortexResult<()> { + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + let dtype = DType::Primitive(PType::I32, Nullability::Nullable); + let mut acc = Accumulator::try_new(Count, EmptyOptions, dtype)?; + + let batch1 = PrimitiveArray::from_option_iter([Some(1i32), None]).into_array(); + acc.accumulate(&batch1, &mut ctx)?; + let result1 = acc.finish()?; + assert_eq!(result1.as_primitive().typed_value::(), Some(1)); + + let batch2 = PrimitiveArray::from_option_iter([Some(2i32), Some(3), None]).into_array(); + acc.accumulate(&batch2, &mut ctx)?; + let result2 = acc.finish()?; + assert_eq!(result2.as_primitive().typed_value::(), Some(2)); + Ok(()) + } + + #[test] + fn count_state_merge() -> VortexResult<()> { + let dtype = DType::Primitive(PType::I32, Nullability::NonNullable); + let mut state = Count.empty_partial(&EmptyOptions, &dtype)?; + + let scalar1 = Scalar::primitive(5u64, Nullability::NonNullable); + Count.combine_partials(&mut state, scalar1)?; + + let scalar2 = Scalar::primitive(3u64, Nullability::NonNullable); + Count.combine_partials(&mut state, scalar2)?; + + let result = Count.to_scalar(&state)?; + Count.reset(&mut state); + assert_eq!(result.as_primitive().typed_value::(), Some(8)); + Ok(()) + } + + #[test] + fn count_constant_non_null() -> VortexResult<()> { + let array = ConstantArray::new(42i32, 10); + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + assert_eq!(count(&array.into_array(), &mut ctx)?, 10); + Ok(()) + } + + #[test] + fn count_constant_null() -> VortexResult<()> { + let array = ConstantArray::new( + Scalar::null(DType::Primitive(PType::I32, Nullability::Nullable)), + 10, + ); + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + assert_eq!(count(&array.into_array(), &mut ctx)?, 0); + Ok(()) + } + + #[test] + fn count_chunked() -> VortexResult<()> { + let chunk1 = PrimitiveArray::from_option_iter([Some(1i32), None, Some(3)]); + let chunk2 = PrimitiveArray::from_option_iter([None, Some(5i32), None]); + let dtype = chunk1.dtype().clone(); + let chunked = ChunkedArray::try_new(vec![chunk1.into_array(), chunk2.into_array()], dtype)?; + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + assert_eq!(count(&chunked.into_array(), &mut ctx)?, 3); + Ok(()) + } +} diff --git a/vortex-array/src/aggregate_fn/fns/mean/mod.rs b/vortex-array/src/aggregate_fn/fns/mean/mod.rs new file mode 100644 index 00000000000..dfa981b59af --- /dev/null +++ b/vortex-array/src/aggregate_fn/fns/mean/mod.rs @@ -0,0 +1,472 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright the Vortex contributors + +use num_traits::ToPrimitive; +use vortex_error::VortexExpect; +use vortex_error::VortexResult; +use vortex_error::vortex_bail; +use vortex_mask::Mask; + +use crate::ArrayRef; +use crate::Canonical; +use crate::Columnar; +use crate::DynArray; +use crate::ExecutionCtx; +use crate::IntoArray; +use crate::aggregate_fn::Accumulator; +use crate::aggregate_fn::AggregateFnId; +use crate::aggregate_fn::AggregateFnVTable; +use crate::aggregate_fn::DynAccumulator; +use crate::aggregate_fn::EmptyOptions; +use crate::arrays::PrimitiveArray; +use crate::canonical::ToCanonical; +use crate::dtype::DType; +use crate::dtype::FieldName; +use crate::dtype::Nullability; +use crate::dtype::PType; +use crate::dtype::StructFields; +use crate::match_each_native_ptype; +use crate::scalar::Scalar; +use crate::validity::Validity; + +/// Compute the arithmetic mean of an array. +/// +/// See [`Mean`] for details. +pub fn mean(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult { + let mut acc = Accumulator::try_new(Mean, EmptyOptions, array.dtype().clone())?; + acc.accumulate(array, ctx)?; + acc.finish() +} + +/// Compute the arithmetic mean of an array, returning `f64`. +/// +/// Applies to boolean and primitive numeric types. Returns a nullable `f64`. +/// Internally tracks sum and count, returning `sum / count` on finalize. +/// If there are no valid elements, returns null. +/// +/// The partial state is a struct `{sum: f64, count: u64}` so that partials from +/// different accumulators can be correctly combined via weighted addition. +#[derive(Clone, Debug)] +pub struct Mean; + +/// Internal accumulation state for [`Mean`]. +pub struct MeanPartial { + sum: f64, + count: u64, +} + +fn partial_struct_dtype() -> DType { + DType::Struct( + StructFields::new( + [FieldName::from("sum"), FieldName::from("count")].into(), + vec![ + DType::Primitive(PType::F64, Nullability::NonNullable), + DType::Primitive(PType::U64, Nullability::NonNullable), + ], + ), + Nullability::Nullable, + ) +} + +impl AggregateFnVTable for Mean { + type Options = EmptyOptions; + type Partial = MeanPartial; + + fn id(&self) -> AggregateFnId { + AggregateFnId::new_ref("vortex.mean") + } + + fn serialize(&self, _options: &Self::Options) -> VortexResult>> { + Ok(Some(vec![])) + } + + fn deserialize( + &self, + _metadata: &[u8], + _session: &vortex_session::VortexSession, + ) -> VortexResult { + Ok(EmptyOptions) + } + + fn return_dtype(&self, _options: &Self::Options, input_dtype: &DType) -> Option { + match input_dtype { + DType::Bool(_) | DType::Primitive(..) => { + Some(DType::Primitive(PType::F64, Nullability::Nullable)) + } + _ => None, + } + } + + fn partial_dtype(&self, _options: &Self::Options, input_dtype: &DType) -> Option { + match input_dtype { + DType::Bool(_) | DType::Primitive(..) => Some(partial_struct_dtype()), + _ => None, + } + } + + fn empty_partial( + &self, + _options: &Self::Options, + _input_dtype: &DType, + ) -> VortexResult { + Ok(MeanPartial { sum: 0.0, count: 0 }) + } + + fn combine_partials(&self, partial: &mut Self::Partial, other: Scalar) -> VortexResult<()> { + if other.is_null() { + return Ok(()); + } + let s = other.as_struct(); + let sum_scalar = s + .field("sum") + .vortex_expect("mean partial must have sum field"); + let count_scalar = s + .field("count") + .vortex_expect("mean partial must have count field"); + + partial.sum += sum_scalar + .as_primitive() + .typed_value::() + .vortex_expect("sum field should not be null"); + partial.count += count_scalar + .as_primitive() + .typed_value::() + .vortex_expect("count field should not be null"); + Ok(()) + } + + fn to_scalar(&self, partial: &Self::Partial) -> VortexResult { + if partial.count == 0 { + Ok(Scalar::null(partial_struct_dtype())) + } else { + Ok(Scalar::struct_( + partial_struct_dtype(), + vec![ + Scalar::primitive(partial.sum, Nullability::NonNullable), + Scalar::primitive(partial.count, Nullability::NonNullable), + ], + )) + } + } + + fn reset(&self, partial: &mut Self::Partial) { + partial.sum = 0.0; + partial.count = 0; + } + + #[inline] + fn is_saturated(&self, _partial: &Self::Partial) -> bool { + false + } + + fn accumulate( + &self, + partial: &mut Self::Partial, + batch: &Columnar, + _ctx: &mut ExecutionCtx, + ) -> VortexResult<()> { + match batch { + Columnar::Constant(c) => { + if !c.scalar().is_null() { + let val = scalar_to_f64(c.scalar())?; + partial.sum += val * c.len() as f64; + partial.count += c.len() as u64; + } + } + Columnar::Canonical(canonical) => match canonical { + Canonical::Primitive(prim) => { + let mask = prim.validity_mask()?; + match_each_native_ptype!(prim.ptype(), |T| { + accumulate_values(partial, prim.as_slice::(), &mask); + }); + } + Canonical::Bool(bool_arr) => { + let mask = bool_arr.validity_mask()?; + let bits = bool_arr.to_bit_buffer(); + match &mask { + Mask::AllTrue(_) => { + partial.sum += bits.true_count() as f64; + partial.count += bool_arr.len() as u64; + } + Mask::AllFalse(_) => {} + Mask::Values(validity) => { + let valid_count = validity.true_count(); + let valid_and_true = (&bits & validity.bit_buffer()).true_count(); + partial.sum += valid_and_true as f64; + partial.count += valid_count as u64; + } + } + } + _ => vortex_bail!("Unsupported canonical type for mean: {}", batch.dtype()), + }, + } + Ok(()) + } + + fn finalize(&self, partials: ArrayRef) -> VortexResult { + let struct_arr = partials.to_struct(); + let sums = struct_arr.unmasked_field_by_name("sum")?; + let counts = struct_arr.unmasked_field_by_name("count")?; + let validity_mask = struct_arr.validity_mask()?; + + let sum_prim = sums.to_primitive(); + let count_prim = counts.to_primitive(); + let sum_values = sum_prim.as_slice::(); + let count_values = count_prim.as_slice::(); + + let means: vortex_buffer::Buffer = sum_values + .iter() + .zip(count_values.iter()) + .map(|(s, c)| if *c == 0 { 0.0 } else { s / *c as f64 }) + .collect(); + + let validity = match validity_mask { + Mask::AllTrue(_) => { + let valid_bits: Vec = count_values.iter().map(|c| *c > 0).collect(); + if valid_bits.iter().all(|v| *v) { + Validity::AllValid + } else { + Validity::from_iter(valid_bits) + } + } + Mask::AllFalse(_) => Validity::AllInvalid, + Mask::Values(v) => { + let valid_bits: Vec = count_values + .iter() + .zip(v.bit_buffer().iter()) + .map(|(c, group_valid)| group_valid && *c > 0) + .collect(); + Validity::from_iter(valid_bits) + } + }; + + Ok(PrimitiveArray::new(means, validity).into_array()) + } + + fn finalize_scalar(&self, partial: &Self::Partial) -> VortexResult { + if partial.count == 0 { + Ok(Scalar::null(DType::Primitive( + PType::F64, + Nullability::Nullable, + ))) + } else { + Ok(Scalar::primitive( + partial.sum / partial.count as f64, + Nullability::Nullable, + )) + } + } +} + +fn scalar_to_f64(scalar: &Scalar) -> VortexResult { + match scalar.dtype() { + DType::Bool(_) => { + let v = scalar.as_bool().value().vortex_expect("checked non-null"); + Ok(if v { 1.0 } else { 0.0 }) + } + DType::Primitive(..) => f64::try_from(scalar), + _ => vortex_bail!("Cannot convert {} to f64 for mean", scalar.dtype()), + } +} + +fn accumulate_values(partial: &mut MeanPartial, values: &[T], mask: &Mask) { + match mask { + Mask::AllTrue(_) => { + partial.count += values.len() as u64; + for v in values { + partial.sum += v.to_f64().unwrap_or(0.0); + } + } + Mask::AllFalse(_) => {} + Mask::Values(v) => { + for (val, valid) in values.iter().zip(v.bit_buffer().iter()) { + if valid { + partial.count += 1; + partial.sum += val.to_f64().unwrap_or(0.0); + } + } + } + } +} + +#[cfg(test)] +mod tests { + use vortex_buffer::buffer; + use vortex_error::VortexResult; + + use crate::IntoArray; + use crate::LEGACY_SESSION; + use crate::VortexSessionExecute; + use crate::aggregate_fn::Accumulator; + use crate::aggregate_fn::AggregateFnVTable; + use crate::aggregate_fn::DynAccumulator; + use crate::aggregate_fn::EmptyOptions; + use crate::aggregate_fn::fns::mean::Mean; + use crate::aggregate_fn::fns::mean::mean; + use crate::aggregate_fn::fns::mean::partial_struct_dtype; + use crate::arrays::BoolArray; + use crate::arrays::ChunkedArray; + use crate::arrays::ConstantArray; + use crate::arrays::PrimitiveArray; + use crate::dtype::DType; + use crate::dtype::Nullability; + use crate::dtype::PType; + use crate::scalar::Scalar; + use crate::validity::Validity; + + #[test] + fn mean_all_valid() -> VortexResult<()> { + let array = PrimitiveArray::new(buffer![1.0f64, 2.0, 3.0, 4.0, 5.0], Validity::NonNullable) + .into_array(); + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + let result = mean(&array, &mut ctx)?; + assert_eq!(result.as_primitive().as_::(), Some(3.0)); + Ok(()) + } + + #[test] + fn mean_with_nulls() -> VortexResult<()> { + let array = PrimitiveArray::from_option_iter([Some(2.0f64), None, Some(4.0)]).into_array(); + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + let result = mean(&array, &mut ctx)?; + assert_eq!(result.as_primitive().as_::(), Some(3.0)); + Ok(()) + } + + #[test] + fn mean_all_null() -> VortexResult<()> { + let array = PrimitiveArray::from_option_iter::([None, None, None]).into_array(); + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + let result = mean(&array, &mut ctx)?; + assert!(result.is_null()); + Ok(()) + } + + #[test] + fn mean_empty() -> VortexResult<()> { + let dtype = DType::Primitive(PType::F64, Nullability::NonNullable); + let mut acc = Accumulator::try_new(Mean, EmptyOptions, dtype)?; + let result = acc.finish()?; + assert!(result.is_null()); + Ok(()) + } + + #[test] + fn mean_integers() -> VortexResult<()> { + let array = PrimitiveArray::new(buffer![10i32, 20, 30], Validity::NonNullable).into_array(); + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + let result = mean(&array, &mut ctx)?; + assert_eq!(result.as_primitive().as_::(), Some(20.0)); + Ok(()) + } + + #[test] + fn mean_multi_batch() -> VortexResult<()> { + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + let dtype = DType::Primitive(PType::F64, Nullability::NonNullable); + let mut acc = Accumulator::try_new(Mean, EmptyOptions, dtype)?; + + let batch1 = + PrimitiveArray::new(buffer![1.0f64, 2.0, 3.0], Validity::NonNullable).into_array(); + acc.accumulate(&batch1, &mut ctx)?; + + let batch2 = PrimitiveArray::new(buffer![4.0f64, 5.0], Validity::NonNullable).into_array(); + acc.accumulate(&batch2, &mut ctx)?; + + let result = acc.finish()?; + assert_eq!(result.as_primitive().as_::(), Some(3.0)); + Ok(()) + } + + #[test] + fn mean_finish_resets_state() -> VortexResult<()> { + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + let dtype = DType::Primitive(PType::F64, Nullability::NonNullable); + let mut acc = Accumulator::try_new(Mean, EmptyOptions, dtype)?; + + let batch1 = PrimitiveArray::new(buffer![2.0f64, 4.0], Validity::NonNullable).into_array(); + acc.accumulate(&batch1, &mut ctx)?; + let result1 = acc.finish()?; + assert_eq!(result1.as_primitive().as_::(), Some(3.0)); + + let batch2 = + PrimitiveArray::new(buffer![10.0f64, 20.0, 30.0], Validity::NonNullable).into_array(); + acc.accumulate(&batch2, &mut ctx)?; + let result2 = acc.finish()?; + assert_eq!(result2.as_primitive().as_::(), Some(20.0)); + Ok(()) + } + + #[test] + fn mean_state_merge() -> VortexResult<()> { + let dtype = DType::Primitive(PType::F64, Nullability::NonNullable); + let mut state = Mean.empty_partial(&EmptyOptions, &dtype)?; + + // Partition 1: mean of [2, 4] → sum=6, count=2 + let partial1 = Scalar::struct_( + partial_struct_dtype(), + vec![ + Scalar::primitive(6.0f64, Nullability::NonNullable), + Scalar::primitive(2u64, Nullability::NonNullable), + ], + ); + Mean.combine_partials(&mut state, partial1)?; + + // Partition 2: mean of [10, 20, 30] → sum=60, count=3 + let partial2 = Scalar::struct_( + partial_struct_dtype(), + vec![ + Scalar::primitive(60.0f64, Nullability::NonNullable), + Scalar::primitive(3u64, Nullability::NonNullable), + ], + ); + Mean.combine_partials(&mut state, partial2)?; + + // Combined: (6 + 60) / (2 + 3) = 13.2 + let result = Mean.finalize_scalar(&state)?; + assert_eq!(result.as_primitive().as_::(), Some(13.2)); + Ok(()) + } + + #[test] + fn mean_constant_non_null() -> VortexResult<()> { + let array = ConstantArray::new(5.0f64, 4); + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + let result = mean(&array.into_array(), &mut ctx)?; + assert_eq!(result.as_primitive().as_::(), Some(5.0)); + Ok(()) + } + + #[test] + fn mean_constant_null() -> VortexResult<()> { + let array = ConstantArray::new( + Scalar::null(DType::Primitive(PType::F64, Nullability::Nullable)), + 10, + ); + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + let result = mean(&array.into_array(), &mut ctx)?; + assert!(result.is_null()); + Ok(()) + } + + #[test] + fn mean_bool() -> VortexResult<()> { + let array: BoolArray = [true, false, true, true].into_iter().collect(); + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + let result = mean(&array.into_array(), &mut ctx)?; + assert_eq!(result.as_primitive().as_::(), Some(0.75)); + Ok(()) + } + + #[test] + fn mean_chunked() -> VortexResult<()> { + let chunk1 = PrimitiveArray::from_option_iter([Some(1.0f64), None, Some(3.0)]); + let chunk2 = PrimitiveArray::from_option_iter([Some(5.0f64), None]); + let dtype = chunk1.dtype().clone(); + let chunked = ChunkedArray::try_new(vec![chunk1.into_array(), chunk2.into_array()], dtype)?; + let mut ctx = LEGACY_SESSION.create_execution_ctx(); + let result = mean(&chunked.into_array(), &mut ctx)?; + assert_eq!(result.as_primitive().as_::(), Some(3.0)); + Ok(()) + } +} diff --git a/vortex-array/src/aggregate_fn/fns/mod.rs b/vortex-array/src/aggregate_fn/fns/mod.rs index 4c233ba4d27..4e6df22299a 100644 --- a/vortex-array/src/aggregate_fn/fns/mod.rs +++ b/vortex-array/src/aggregate_fn/fns/mod.rs @@ -1,8 +1,10 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: Copyright the Vortex contributors +pub mod count; pub mod is_constant; pub mod is_sorted; +pub mod mean; pub mod min_max; pub mod nan_count; pub mod sum; From ee67d8025f2b62b8b7314c56bfab222459bd44fb Mon Sep 17 00:00:00 2001 From: blaginin Date: Sun, 29 Mar 2026 22:24:22 +0100 Subject: [PATCH 2/6] validity cleanup Co-authored-by: Claude Signed-off-by: blaginin --- vortex-array/src/aggregate_fn/fns/mean/mod.rs | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/vortex-array/src/aggregate_fn/fns/mean/mod.rs b/vortex-array/src/aggregate_fn/fns/mean/mod.rs index dfa981b59af..024fb4ea16b 100644 --- a/vortex-array/src/aggregate_fn/fns/mean/mod.rs +++ b/vortex-array/src/aggregate_fn/fns/mean/mod.rs @@ -220,25 +220,14 @@ impl AggregateFnVTable for Mean { .map(|(s, c)| if *c == 0 { 0.0 } else { s / *c as f64 }) .collect(); - let validity = match validity_mask { - Mask::AllTrue(_) => { - let valid_bits: Vec = count_values.iter().map(|c| *c > 0).collect(); - if valid_bits.iter().all(|v| *v) { - Validity::AllValid - } else { - Validity::from_iter(valid_bits) - } - } - Mask::AllFalse(_) => Validity::AllInvalid, - Mask::Values(v) => { - let valid_bits: Vec = count_values - .iter() - .zip(v.bit_buffer().iter()) - .map(|(c, group_valid)| group_valid && *c > 0) - .collect(); - Validity::from_iter(valid_bits) - } - }; + // A mean is valid when the group itself was valid AND had at least one + // non-null element (count > 0). + let validity = Validity::from_iter( + count_values + .iter() + .enumerate() + .map(|(i, c)| validity_mask.value(i) && *c > 0), + ); Ok(PrimitiveArray::new(means, validity).into_array()) } From 4e16945e3e07313917a96058848f9ee7e6001467 Mon Sep 17 00:00:00 2001 From: blaginin Date: Thu, 2 Apr 2026 16:13:10 +0100 Subject: [PATCH 3/6] generic kernels Signed-off-by: blaginin --- vortex-array/src/aggregate_fn/accumulator.rs | 25 +++++++++++++++++- .../src/aggregate_fn/fns/count/mod.rs | 26 +++++++++++++++++++ vortex-array/src/aggregate_fn/session.rs | 19 ++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/vortex-array/src/aggregate_fn/accumulator.rs b/vortex-array/src/aggregate_fn/accumulator.rs index 95d5268b969..9aa15149ff7 100644 --- a/vortex-array/src/aggregate_fn/accumulator.rs +++ b/vortex-array/src/aggregate_fn/accumulator.rs @@ -101,7 +101,9 @@ impl DynAccumulator for Accumulator { ); let session = ctx.session().clone(); - let kernels = &session.aggregate_fns().kernels; + let agg_fns = session.aggregate_fns(); + let kernels = &agg_fns.kernels; + let generic_kernels = &agg_fns.generic_kernels; let mut batch = batch.clone(); for _ in 0..*MAX_ITERATIONS { @@ -131,6 +133,27 @@ impl DynAccumulator for Accumulator { return Ok(()); } + // Try encoding-agnostic kernels before decompressing. + let generic_r = generic_kernels.read(); + if let Some(result) = generic_r + .get(&self.aggregate_fn.id()) + .and_then(|kernel| { + kernel + .aggregate(&self.aggregate_fn, &batch, ctx) + .transpose() + }) + .transpose()? + { + vortex_ensure!( + result.dtype() == &self.partial_dtype, + "Aggregate kernel returned {}, expected {}", + result.dtype(), + self.partial_dtype, + ); + self.vtable.combine_partials(&mut self.partial, result)?; + return Ok(()); + } + // Execute one step and try again batch = batch.execute(ctx)?; } diff --git a/vortex-array/src/aggregate_fn/fns/count/mod.rs b/vortex-array/src/aggregate_fn/fns/count/mod.rs index 3c483372186..cf398ce6274 100644 --- a/vortex-array/src/aggregate_fn/fns/count/mod.rs +++ b/vortex-array/src/aggregate_fn/fns/count/mod.rs @@ -6,12 +6,15 @@ use vortex_error::VortexResult; use crate::ArrayRef; use crate::Columnar; +use crate::DynArray; use crate::ExecutionCtx; use crate::aggregate_fn::Accumulator; use crate::aggregate_fn::AggregateFnId; +use crate::aggregate_fn::AggregateFnRef; use crate::aggregate_fn::AggregateFnVTable; use crate::aggregate_fn::DynAccumulator; use crate::aggregate_fn::EmptyOptions; +use crate::aggregate_fn::kernels::DynAggregateKernel; use crate::dtype::DType; use crate::dtype::Nullability; use crate::dtype::PType; @@ -31,6 +34,29 @@ pub fn count(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult { .vortex_expect("count result should not be null")) } +/// Encoding-agnostic count kernel. +/// +/// Count-non-null only depends on validity, not data values. Since every encoding +/// exposes validity independently, this avoids decompressing the data. +#[derive(Debug)] +pub(crate) struct CountKernel; + +impl DynAggregateKernel for CountKernel { + fn aggregate( + &self, + aggregate_fn: &AggregateFnRef, + batch: &ArrayRef, + _ctx: &mut ExecutionCtx, + ) -> VortexResult> { + if !aggregate_fn.is::() { + return Ok(None); + } + + let count = batch.valid_count()? as u64; + Ok(Some(Scalar::primitive(count, Nullability::NonNullable))) + } +} + /// Count the number of non-null elements in an array. /// /// Applies to all types. Returns a `u64` count. diff --git a/vortex-array/src/aggregate_fn/session.rs b/vortex-array/src/aggregate_fn/session.rs index 6e85ee97b6b..02fdc66243b 100644 --- a/vortex-array/src/aggregate_fn/session.rs +++ b/vortex-array/src/aggregate_fn/session.rs @@ -12,6 +12,8 @@ use vortex_utils::aliases::hash_map::HashMap; use crate::aggregate_fn::AggregateFnId; use crate::aggregate_fn::AggregateFnPluginRef; use crate::aggregate_fn::AggregateFnVTable; +use crate::aggregate_fn::fns::count::Count; +use crate::aggregate_fn::fns::count::CountKernel; use crate::aggregate_fn::fns::is_constant::IsConstant; use crate::aggregate_fn::fns::is_sorted::IsSorted; use crate::aggregate_fn::fns::min_max::MinMax; @@ -36,6 +38,9 @@ pub struct AggregateFnSession { registry: AggregateFnRegistry, pub(super) kernels: RwLock>, + /// Encoding-agnostic kernels keyed by aggregate function ID. + /// These are checked as a fallback after encoding-specific kernels. + pub(super) generic_kernels: RwLock>, pub(super) grouped_kernels: RwLock>, } @@ -46,10 +51,12 @@ impl Default for AggregateFnSession { let this = Self { registry: AggregateFnRegistry::default(), kernels: RwLock::new(HashMap::default()), + generic_kernels: RwLock::new(HashMap::default()), grouped_kernels: RwLock::new(HashMap::default()), }; // Register the built-in aggregate functions + this.register(Count); this.register(IsConstant); this.register(IsSorted); this.register(MinMax); @@ -62,6 +69,9 @@ impl Default for AggregateFnSession { this.register_aggregate_kernel(Dict::ID, Some(IsConstant.id()), &DictIsConstantKernel); this.register_aggregate_kernel(Dict::ID, Some(IsSorted.id()), &DictIsSortedKernel); + // Register encoding-agnostic kernels. + this.register_generic_kernel(Count.id(), &CountKernel); + this } } @@ -88,6 +98,15 @@ impl AggregateFnSession { ) { self.kernels.write().insert((array_id, agg_fn_id), kernel); } + + /// Register an encoding-agnostic aggregate kernel for a specific aggregate function. + pub fn register_generic_kernel( + &self, + agg_fn_id: AggregateFnId, + kernel: &'static dyn DynAggregateKernel, + ) { + self.generic_kernels.write().insert(agg_fn_id, kernel); + } } /// Extension trait for accessing aggregate function session data. From 575672ad697c71e28991e496b5676c03053672ca Mon Sep 17 00:00:00 2001 From: blaginin Date: Thu, 2 Apr 2026 16:16:27 +0100 Subject: [PATCH 4/6] `try_accumulate` feels cleaner Signed-off-by: blaginin --- vortex-array/src/aggregate_fn/accumulator.rs | 30 +++-------- .../src/aggregate_fn/fns/count/mod.rs | 52 +++++-------------- vortex-array/src/aggregate_fn/session.rs | 19 ------- vortex-array/src/aggregate_fn/vtable.rs | 16 ++++++ 4 files changed, 35 insertions(+), 82 deletions(-) diff --git a/vortex-array/src/aggregate_fn/accumulator.rs b/vortex-array/src/aggregate_fn/accumulator.rs index 9aa15149ff7..d49820f6dd1 100644 --- a/vortex-array/src/aggregate_fn/accumulator.rs +++ b/vortex-array/src/aggregate_fn/accumulator.rs @@ -100,10 +100,13 @@ impl DynAccumulator for Accumulator { batch.dtype() ); + // Allow the vtable to short-circuit on the raw array before decompression. + if self.vtable.try_accumulate(&mut self.partial, batch, ctx)? { + return Ok(()); + } + let session = ctx.session().clone(); - let agg_fns = session.aggregate_fns(); - let kernels = &agg_fns.kernels; - let generic_kernels = &agg_fns.generic_kernels; + let kernels = &session.aggregate_fns().kernels; let mut batch = batch.clone(); for _ in 0..*MAX_ITERATIONS { @@ -133,27 +136,6 @@ impl DynAccumulator for Accumulator { return Ok(()); } - // Try encoding-agnostic kernels before decompressing. - let generic_r = generic_kernels.read(); - if let Some(result) = generic_r - .get(&self.aggregate_fn.id()) - .and_then(|kernel| { - kernel - .aggregate(&self.aggregate_fn, &batch, ctx) - .transpose() - }) - .transpose()? - { - vortex_ensure!( - result.dtype() == &self.partial_dtype, - "Aggregate kernel returned {}, expected {}", - result.dtype(), - self.partial_dtype, - ); - self.vtable.combine_partials(&mut self.partial, result)?; - return Ok(()); - } - // Execute one step and try again batch = batch.execute(ctx)?; } diff --git a/vortex-array/src/aggregate_fn/fns/count/mod.rs b/vortex-array/src/aggregate_fn/fns/count/mod.rs index cf398ce6274..546315b066c 100644 --- a/vortex-array/src/aggregate_fn/fns/count/mod.rs +++ b/vortex-array/src/aggregate_fn/fns/count/mod.rs @@ -10,11 +10,9 @@ use crate::DynArray; use crate::ExecutionCtx; use crate::aggregate_fn::Accumulator; use crate::aggregate_fn::AggregateFnId; -use crate::aggregate_fn::AggregateFnRef; use crate::aggregate_fn::AggregateFnVTable; use crate::aggregate_fn::DynAccumulator; use crate::aggregate_fn::EmptyOptions; -use crate::aggregate_fn::kernels::DynAggregateKernel; use crate::dtype::DType; use crate::dtype::Nullability; use crate::dtype::PType; @@ -34,29 +32,6 @@ pub fn count(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult { .vortex_expect("count result should not be null")) } -/// Encoding-agnostic count kernel. -/// -/// Count-non-null only depends on validity, not data values. Since every encoding -/// exposes validity independently, this avoids decompressing the data. -#[derive(Debug)] -pub(crate) struct CountKernel; - -impl DynAggregateKernel for CountKernel { - fn aggregate( - &self, - aggregate_fn: &AggregateFnRef, - batch: &ArrayRef, - _ctx: &mut ExecutionCtx, - ) -> VortexResult> { - if !aggregate_fn.is::() { - return Ok(None); - } - - let count = batch.valid_count()? as u64; - Ok(Some(Scalar::primitive(count, Nullability::NonNullable))) - } -} - /// Count the number of non-null elements in an array. /// /// Applies to all types. Returns a `u64` count. @@ -122,24 +97,23 @@ impl AggregateFnVTable for Count { false } + fn try_accumulate( + &self, + state: &mut Self::Partial, + batch: &ArrayRef, + _ctx: &mut ExecutionCtx, + ) -> VortexResult { + *state += batch.valid_count()? as u64; + Ok(true) + } + fn accumulate( &self, - partial: &mut Self::Partial, - batch: &Columnar, + _partial: &mut Self::Partial, + _batch: &Columnar, _ctx: &mut ExecutionCtx, ) -> VortexResult<()> { - match batch { - Columnar::Constant(c) => { - if !c.scalar().is_null() { - *partial += c.len() as u64; - } - } - Columnar::Canonical(c) => { - let valid = c.as_ref().valid_count()?; - *partial += valid as u64; - } - } - Ok(()) + unreachable!("Count::try_accumulate handles all arrays") } fn finalize(&self, partials: ArrayRef) -> VortexResult { diff --git a/vortex-array/src/aggregate_fn/session.rs b/vortex-array/src/aggregate_fn/session.rs index 02fdc66243b..6e85ee97b6b 100644 --- a/vortex-array/src/aggregate_fn/session.rs +++ b/vortex-array/src/aggregate_fn/session.rs @@ -12,8 +12,6 @@ use vortex_utils::aliases::hash_map::HashMap; use crate::aggregate_fn::AggregateFnId; use crate::aggregate_fn::AggregateFnPluginRef; use crate::aggregate_fn::AggregateFnVTable; -use crate::aggregate_fn::fns::count::Count; -use crate::aggregate_fn::fns::count::CountKernel; use crate::aggregate_fn::fns::is_constant::IsConstant; use crate::aggregate_fn::fns::is_sorted::IsSorted; use crate::aggregate_fn::fns::min_max::MinMax; @@ -38,9 +36,6 @@ pub struct AggregateFnSession { registry: AggregateFnRegistry, pub(super) kernels: RwLock>, - /// Encoding-agnostic kernels keyed by aggregate function ID. - /// These are checked as a fallback after encoding-specific kernels. - pub(super) generic_kernels: RwLock>, pub(super) grouped_kernels: RwLock>, } @@ -51,12 +46,10 @@ impl Default for AggregateFnSession { let this = Self { registry: AggregateFnRegistry::default(), kernels: RwLock::new(HashMap::default()), - generic_kernels: RwLock::new(HashMap::default()), grouped_kernels: RwLock::new(HashMap::default()), }; // Register the built-in aggregate functions - this.register(Count); this.register(IsConstant); this.register(IsSorted); this.register(MinMax); @@ -69,9 +62,6 @@ impl Default for AggregateFnSession { this.register_aggregate_kernel(Dict::ID, Some(IsConstant.id()), &DictIsConstantKernel); this.register_aggregate_kernel(Dict::ID, Some(IsSorted.id()), &DictIsSortedKernel); - // Register encoding-agnostic kernels. - this.register_generic_kernel(Count.id(), &CountKernel); - this } } @@ -98,15 +88,6 @@ impl AggregateFnSession { ) { self.kernels.write().insert((array_id, agg_fn_id), kernel); } - - /// Register an encoding-agnostic aggregate kernel for a specific aggregate function. - pub fn register_generic_kernel( - &self, - agg_fn_id: AggregateFnId, - kernel: &'static dyn DynAggregateKernel, - ) { - self.generic_kernels.write().insert(agg_fn_id, kernel); - } } /// Extension trait for accessing aggregate function session data. diff --git a/vortex-array/src/aggregate_fn/vtable.rs b/vortex-array/src/aggregate_fn/vtable.rs index feaebe61f56..e2bdf7ab602 100644 --- a/vortex-array/src/aggregate_fn/vtable.rs +++ b/vortex-array/src/aggregate_fn/vtable.rs @@ -105,6 +105,22 @@ pub trait AggregateFnVTable: 'static + Sized + Clone + Send + Sync { /// final result is fully determined. fn is_saturated(&self, state: &Self::Partial) -> bool; + /// Try to accumulate the raw array before decompression. + /// + /// Returns `true` if the array was handled, `false` to fall through to + /// the default kernel dispatch and canonicalization path. + /// + /// This is useful for aggregates that only depend on array metadata (e.g., validity) + /// rather than the encoded data, avoiding unnecessary decompression. + fn try_accumulate( + &self, + _state: &mut Self::Partial, + _batch: &ArrayRef, + _ctx: &mut ExecutionCtx, + ) -> VortexResult { + Ok(false) + } + /// Accumulate a new canonical array into the accumulator state. fn accumulate( &self, From 3c8b12b8e8f0d7b9fbe844c6b637ae75476faee8 Mon Sep 17 00:00:00 2001 From: blaginin Date: Thu, 2 Apr 2026 17:43:36 +0100 Subject: [PATCH 5/6] only mean; mean is not serializable Signed-off-by: blaginin --- vortex-array/src/aggregate_fn/accumulator.rs | 5 - .../src/aggregate_fn/fns/count/mod.rs | 267 ------------------ vortex-array/src/aggregate_fn/fns/mean/mod.rs | 7 +- vortex-array/src/aggregate_fn/fns/mod.rs | 1 - vortex-array/src/aggregate_fn/vtable.rs | 16 -- 5 files changed, 5 insertions(+), 291 deletions(-) delete mode 100644 vortex-array/src/aggregate_fn/fns/count/mod.rs diff --git a/vortex-array/src/aggregate_fn/accumulator.rs b/vortex-array/src/aggregate_fn/accumulator.rs index d49820f6dd1..95d5268b969 100644 --- a/vortex-array/src/aggregate_fn/accumulator.rs +++ b/vortex-array/src/aggregate_fn/accumulator.rs @@ -100,11 +100,6 @@ impl DynAccumulator for Accumulator { batch.dtype() ); - // Allow the vtable to short-circuit on the raw array before decompression. - if self.vtable.try_accumulate(&mut self.partial, batch, ctx)? { - return Ok(()); - } - let session = ctx.session().clone(); let kernels = &session.aggregate_fns().kernels; diff --git a/vortex-array/src/aggregate_fn/fns/count/mod.rs b/vortex-array/src/aggregate_fn/fns/count/mod.rs deleted file mode 100644 index 546315b066c..00000000000 --- a/vortex-array/src/aggregate_fn/fns/count/mod.rs +++ /dev/null @@ -1,267 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: Copyright the Vortex contributors - -use vortex_error::VortexExpect; -use vortex_error::VortexResult; - -use crate::ArrayRef; -use crate::Columnar; -use crate::DynArray; -use crate::ExecutionCtx; -use crate::aggregate_fn::Accumulator; -use crate::aggregate_fn::AggregateFnId; -use crate::aggregate_fn::AggregateFnVTable; -use crate::aggregate_fn::DynAccumulator; -use crate::aggregate_fn::EmptyOptions; -use crate::dtype::DType; -use crate::dtype::Nullability; -use crate::dtype::PType; -use crate::scalar::Scalar; - -/// Return the count of non-null elements in an array. -/// -/// See [`Count`] for details. -pub fn count(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult { - let mut acc = Accumulator::try_new(Count, EmptyOptions, array.dtype().clone())?; - acc.accumulate(array, ctx)?; - let result = acc.finish()?; - - Ok(result - .as_primitive() - .typed_value::() - .vortex_expect("count result should not be null")) -} - -/// Count the number of non-null elements in an array. -/// -/// Applies to all types. Returns a `u64` count. -/// The identity value is zero. -#[derive(Clone, Debug)] -pub struct Count; - -impl AggregateFnVTable for Count { - type Options = EmptyOptions; - type Partial = u64; - - fn id(&self) -> AggregateFnId { - AggregateFnId::new_ref("vortex.count") - } - - fn serialize(&self, _options: &Self::Options) -> VortexResult>> { - Ok(Some(vec![])) - } - - fn deserialize( - &self, - _metadata: &[u8], - _session: &vortex_session::VortexSession, - ) -> VortexResult { - Ok(EmptyOptions) - } - - fn return_dtype(&self, _options: &Self::Options, _input_dtype: &DType) -> Option { - Some(DType::Primitive(PType::U64, Nullability::NonNullable)) - } - - fn partial_dtype(&self, options: &Self::Options, input_dtype: &DType) -> Option { - self.return_dtype(options, input_dtype) - } - - fn empty_partial( - &self, - _options: &Self::Options, - _input_dtype: &DType, - ) -> VortexResult { - Ok(0u64) - } - - fn combine_partials(&self, partial: &mut Self::Partial, other: Scalar) -> VortexResult<()> { - let val = other - .as_primitive() - .typed_value::() - .vortex_expect("count partial should not be null"); - *partial += val; - Ok(()) - } - - fn to_scalar(&self, partial: &Self::Partial) -> VortexResult { - Ok(Scalar::primitive(*partial, Nullability::NonNullable)) - } - - fn reset(&self, partial: &mut Self::Partial) { - *partial = 0; - } - - #[inline] - fn is_saturated(&self, _partial: &Self::Partial) -> bool { - false - } - - fn try_accumulate( - &self, - state: &mut Self::Partial, - batch: &ArrayRef, - _ctx: &mut ExecutionCtx, - ) -> VortexResult { - *state += batch.valid_count()? as u64; - Ok(true) - } - - fn accumulate( - &self, - _partial: &mut Self::Partial, - _batch: &Columnar, - _ctx: &mut ExecutionCtx, - ) -> VortexResult<()> { - unreachable!("Count::try_accumulate handles all arrays") - } - - fn finalize(&self, partials: ArrayRef) -> VortexResult { - Ok(partials) - } - - fn finalize_scalar(&self, partial: &Self::Partial) -> VortexResult { - self.to_scalar(partial) - } -} - -#[cfg(test)] -mod tests { - use vortex_buffer::buffer; - use vortex_error::VortexResult; - - use crate::IntoArray; - use crate::LEGACY_SESSION; - use crate::VortexSessionExecute; - use crate::aggregate_fn::Accumulator; - use crate::aggregate_fn::AggregateFnVTable; - use crate::aggregate_fn::DynAccumulator; - use crate::aggregate_fn::EmptyOptions; - use crate::aggregate_fn::fns::count::Count; - use crate::aggregate_fn::fns::count::count; - use crate::arrays::ChunkedArray; - use crate::arrays::ConstantArray; - use crate::arrays::PrimitiveArray; - use crate::dtype::DType; - use crate::dtype::Nullability; - use crate::dtype::PType; - use crate::scalar::Scalar; - use crate::validity::Validity; - - #[test] - fn count_all_valid() -> VortexResult<()> { - let array = - PrimitiveArray::new(buffer![1i32, 2, 3, 4, 5], Validity::NonNullable).into_array(); - let mut ctx = LEGACY_SESSION.create_execution_ctx(); - assert_eq!(count(&array, &mut ctx)?, 5); - Ok(()) - } - - #[test] - fn count_with_nulls() -> VortexResult<()> { - let array = PrimitiveArray::from_option_iter([Some(1i32), None, Some(3), None, Some(5)]) - .into_array(); - let mut ctx = LEGACY_SESSION.create_execution_ctx(); - assert_eq!(count(&array, &mut ctx)?, 3); - Ok(()) - } - - #[test] - fn count_all_null() -> VortexResult<()> { - let array = PrimitiveArray::from_option_iter::([None, None, None]).into_array(); - let mut ctx = LEGACY_SESSION.create_execution_ctx(); - assert_eq!(count(&array, &mut ctx)?, 0); - Ok(()) - } - - #[test] - fn count_empty() -> VortexResult<()> { - let dtype = DType::Primitive(PType::I32, Nullability::NonNullable); - let mut acc = Accumulator::try_new(Count, EmptyOptions, dtype)?; - let result = acc.finish()?; - assert_eq!(result.as_primitive().typed_value::(), Some(0)); - Ok(()) - } - - #[test] - fn count_multi_batch() -> VortexResult<()> { - let mut ctx = LEGACY_SESSION.create_execution_ctx(); - let dtype = DType::Primitive(PType::I32, Nullability::Nullable); - let mut acc = Accumulator::try_new(Count, EmptyOptions, dtype)?; - - let batch1 = PrimitiveArray::from_option_iter([Some(1i32), None, Some(3)]).into_array(); - acc.accumulate(&batch1, &mut ctx)?; - - let batch2 = PrimitiveArray::from_option_iter([None, Some(5i32)]).into_array(); - acc.accumulate(&batch2, &mut ctx)?; - - let result = acc.finish()?; - assert_eq!(result.as_primitive().typed_value::(), Some(3)); - Ok(()) - } - - #[test] - fn count_finish_resets_state() -> VortexResult<()> { - let mut ctx = LEGACY_SESSION.create_execution_ctx(); - let dtype = DType::Primitive(PType::I32, Nullability::Nullable); - let mut acc = Accumulator::try_new(Count, EmptyOptions, dtype)?; - - let batch1 = PrimitiveArray::from_option_iter([Some(1i32), None]).into_array(); - acc.accumulate(&batch1, &mut ctx)?; - let result1 = acc.finish()?; - assert_eq!(result1.as_primitive().typed_value::(), Some(1)); - - let batch2 = PrimitiveArray::from_option_iter([Some(2i32), Some(3), None]).into_array(); - acc.accumulate(&batch2, &mut ctx)?; - let result2 = acc.finish()?; - assert_eq!(result2.as_primitive().typed_value::(), Some(2)); - Ok(()) - } - - #[test] - fn count_state_merge() -> VortexResult<()> { - let dtype = DType::Primitive(PType::I32, Nullability::NonNullable); - let mut state = Count.empty_partial(&EmptyOptions, &dtype)?; - - let scalar1 = Scalar::primitive(5u64, Nullability::NonNullable); - Count.combine_partials(&mut state, scalar1)?; - - let scalar2 = Scalar::primitive(3u64, Nullability::NonNullable); - Count.combine_partials(&mut state, scalar2)?; - - let result = Count.to_scalar(&state)?; - Count.reset(&mut state); - assert_eq!(result.as_primitive().typed_value::(), Some(8)); - Ok(()) - } - - #[test] - fn count_constant_non_null() -> VortexResult<()> { - let array = ConstantArray::new(42i32, 10); - let mut ctx = LEGACY_SESSION.create_execution_ctx(); - assert_eq!(count(&array.into_array(), &mut ctx)?, 10); - Ok(()) - } - - #[test] - fn count_constant_null() -> VortexResult<()> { - let array = ConstantArray::new( - Scalar::null(DType::Primitive(PType::I32, Nullability::Nullable)), - 10, - ); - let mut ctx = LEGACY_SESSION.create_execution_ctx(); - assert_eq!(count(&array.into_array(), &mut ctx)?, 0); - Ok(()) - } - - #[test] - fn count_chunked() -> VortexResult<()> { - let chunk1 = PrimitiveArray::from_option_iter([Some(1i32), None, Some(3)]); - let chunk2 = PrimitiveArray::from_option_iter([None, Some(5i32), None]); - let dtype = chunk1.dtype().clone(); - let chunked = ChunkedArray::try_new(vec![chunk1.into_array(), chunk2.into_array()], dtype)?; - let mut ctx = LEGACY_SESSION.create_execution_ctx(); - assert_eq!(count(&chunked.into_array(), &mut ctx)?, 3); - Ok(()) - } -} diff --git a/vortex-array/src/aggregate_fn/fns/mean/mod.rs b/vortex-array/src/aggregate_fn/fns/mean/mod.rs index 024fb4ea16b..6eed9b0aa6f 100644 --- a/vortex-array/src/aggregate_fn/fns/mean/mod.rs +++ b/vortex-array/src/aggregate_fn/fns/mean/mod.rs @@ -77,7 +77,10 @@ impl AggregateFnVTable for Mean { } fn serialize(&self, _options: &Self::Options) -> VortexResult>> { - Ok(Some(vec![])) + // This function is not serializable until: + // - we decide on algo for compilation (and hence what should be the intermediate state) + // - we decide on return type (should mean(decimals) be a decimal?) + Ok(None) } fn deserialize( @@ -85,7 +88,7 @@ impl AggregateFnVTable for Mean { _metadata: &[u8], _session: &vortex_session::VortexSession, ) -> VortexResult { - Ok(EmptyOptions) + unimplemented!("Mean is not deserializable") } fn return_dtype(&self, _options: &Self::Options, input_dtype: &DType) -> Option { diff --git a/vortex-array/src/aggregate_fn/fns/mod.rs b/vortex-array/src/aggregate_fn/fns/mod.rs index 4e6df22299a..13afde10f6c 100644 --- a/vortex-array/src/aggregate_fn/fns/mod.rs +++ b/vortex-array/src/aggregate_fn/fns/mod.rs @@ -1,7 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: Copyright the Vortex contributors -pub mod count; pub mod is_constant; pub mod is_sorted; pub mod mean; diff --git a/vortex-array/src/aggregate_fn/vtable.rs b/vortex-array/src/aggregate_fn/vtable.rs index e2bdf7ab602..feaebe61f56 100644 --- a/vortex-array/src/aggregate_fn/vtable.rs +++ b/vortex-array/src/aggregate_fn/vtable.rs @@ -105,22 +105,6 @@ pub trait AggregateFnVTable: 'static + Sized + Clone + Send + Sync { /// final result is fully determined. fn is_saturated(&self, state: &Self::Partial) -> bool; - /// Try to accumulate the raw array before decompression. - /// - /// Returns `true` if the array was handled, `false` to fall through to - /// the default kernel dispatch and canonicalization path. - /// - /// This is useful for aggregates that only depend on array metadata (e.g., validity) - /// rather than the encoded data, avoiding unnecessary decompression. - fn try_accumulate( - &self, - _state: &mut Self::Partial, - _batch: &ArrayRef, - _ctx: &mut ExecutionCtx, - ) -> VortexResult { - Ok(false) - } - /// Accumulate a new canonical array into the accumulator state. fn accumulate( &self, From 46db9fab01a5f9831f9b10872b8b9dd10e70eec1 Mon Sep 17 00:00:00 2001 From: blaginin Date: Thu, 2 Apr 2026 17:56:04 +0100 Subject: [PATCH 6/6] lock file update Signed-off-by: blaginin --- vortex-array/public-api.lock | 84 ++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/vortex-array/public-api.lock b/vortex-array/public-api.lock index c193f10da92..0c6c1ff06cd 100644 --- a/vortex-array/public-api.lock +++ b/vortex-array/public-api.lock @@ -186,6 +186,56 @@ pub fn vortex_array::aggregate_fn::fns::is_sorted::is_strict_sorted(array: &vort pub fn vortex_array::aggregate_fn::fns::is_sorted::make_is_sorted_partial_dtype(element_dtype: &vortex_array::dtype::DType) -> vortex_array::dtype::DType +pub mod vortex_array::aggregate_fn::fns::mean + +pub struct vortex_array::aggregate_fn::fns::mean::Mean + +impl core::clone::Clone for vortex_array::aggregate_fn::fns::mean::Mean + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::clone(&self) -> vortex_array::aggregate_fn::fns::mean::Mean + +impl core::fmt::Debug for vortex_array::aggregate_fn::fns::mean::Mean + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result + +impl vortex_array::aggregate_fn::AggregateFnVTable for vortex_array::aggregate_fn::fns::mean::Mean + +pub type vortex_array::aggregate_fn::fns::mean::Mean::Options = vortex_array::aggregate_fn::EmptyOptions + +pub type vortex_array::aggregate_fn::fns::mean::Mean::Partial = vortex_array::aggregate_fn::fns::mean::MeanPartial + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::accumulate(&self, partial: &mut Self::Partial, batch: &vortex_array::Columnar, _ctx: &mut vortex_array::ExecutionCtx) -> vortex_error::VortexResult<()> + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::coerce_args(&self, options: &Self::Options, input_dtype: &vortex_array::dtype::DType) -> vortex_error::VortexResult + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::combine_partials(&self, partial: &mut Self::Partial, other: vortex_array::scalar::Scalar) -> vortex_error::VortexResult<()> + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::deserialize(&self, _metadata: &[u8], _session: &vortex_session::VortexSession) -> vortex_error::VortexResult + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::empty_partial(&self, _options: &Self::Options, _input_dtype: &vortex_array::dtype::DType) -> vortex_error::VortexResult + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::finalize(&self, partials: vortex_array::ArrayRef) -> vortex_error::VortexResult + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::finalize_scalar(&self, partial: &Self::Partial) -> vortex_error::VortexResult + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::id(&self) -> vortex_array::aggregate_fn::AggregateFnId + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::is_saturated(&self, _partial: &Self::Partial) -> bool + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::partial_dtype(&self, _options: &Self::Options, input_dtype: &vortex_array::dtype::DType) -> core::option::Option + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::reset(&self, partial: &mut Self::Partial) + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::return_dtype(&self, _options: &Self::Options, input_dtype: &vortex_array::dtype::DType) -> core::option::Option + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::serialize(&self, _options: &Self::Options) -> vortex_error::VortexResult>> + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::to_scalar(&self, partial: &Self::Partial) -> vortex_error::VortexResult + +pub struct vortex_array::aggregate_fn::fns::mean::MeanPartial + +pub fn vortex_array::aggregate_fn::fns::mean::mean(array: &vortex_array::ArrayRef, ctx: &mut vortex_array::ExecutionCtx) -> vortex_error::VortexResult + pub mod vortex_array::aggregate_fn::fns::min_max pub struct vortex_array::aggregate_fn::fns::min_max::MinMax @@ -684,6 +734,40 @@ pub fn vortex_array::aggregate_fn::fns::is_sorted::IsSorted::serialize(&self, op pub fn vortex_array::aggregate_fn::fns::is_sorted::IsSorted::to_scalar(&self, partial: &Self::Partial) -> vortex_error::VortexResult +impl vortex_array::aggregate_fn::AggregateFnVTable for vortex_array::aggregate_fn::fns::mean::Mean + +pub type vortex_array::aggregate_fn::fns::mean::Mean::Options = vortex_array::aggregate_fn::EmptyOptions + +pub type vortex_array::aggregate_fn::fns::mean::Mean::Partial = vortex_array::aggregate_fn::fns::mean::MeanPartial + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::accumulate(&self, partial: &mut Self::Partial, batch: &vortex_array::Columnar, _ctx: &mut vortex_array::ExecutionCtx) -> vortex_error::VortexResult<()> + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::coerce_args(&self, options: &Self::Options, input_dtype: &vortex_array::dtype::DType) -> vortex_error::VortexResult + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::combine_partials(&self, partial: &mut Self::Partial, other: vortex_array::scalar::Scalar) -> vortex_error::VortexResult<()> + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::deserialize(&self, _metadata: &[u8], _session: &vortex_session::VortexSession) -> vortex_error::VortexResult + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::empty_partial(&self, _options: &Self::Options, _input_dtype: &vortex_array::dtype::DType) -> vortex_error::VortexResult + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::finalize(&self, partials: vortex_array::ArrayRef) -> vortex_error::VortexResult + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::finalize_scalar(&self, partial: &Self::Partial) -> vortex_error::VortexResult + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::id(&self) -> vortex_array::aggregate_fn::AggregateFnId + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::is_saturated(&self, _partial: &Self::Partial) -> bool + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::partial_dtype(&self, _options: &Self::Options, input_dtype: &vortex_array::dtype::DType) -> core::option::Option + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::reset(&self, partial: &mut Self::Partial) + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::return_dtype(&self, _options: &Self::Options, input_dtype: &vortex_array::dtype::DType) -> core::option::Option + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::serialize(&self, _options: &Self::Options) -> vortex_error::VortexResult>> + +pub fn vortex_array::aggregate_fn::fns::mean::Mean::to_scalar(&self, partial: &Self::Partial) -> vortex_error::VortexResult + impl vortex_array::aggregate_fn::AggregateFnVTable for vortex_array::aggregate_fn::fns::min_max::MinMax pub type vortex_array::aggregate_fn::fns::min_max::MinMax::Options = vortex_array::aggregate_fn::EmptyOptions