MCPcopy Create free account
hub / github.com/apache/arrow-rs / sum_array_checked

Function sum_array_checked

arrow-arith/src/aggregate.rs:584–616  ·  view source on GitHub ↗

Returns the sum of values in the array. This detects overflow and returns an `Err` for that. For an non-overflow-checking variant, use [`sum_array`] instead. Additionally returns an `Err` on run-end-encoded arrays with a provided values type parameter that is incorrect.

(
    array: A,
)

Source from the content-addressed store, hash-verified

582/// Additionally returns an `Err` on run-end-encoded arrays with a provided
583/// values type parameter that is incorrect.
584pub fn sum_array_checked<T: ArrowNumericType, A: ArrayAccessor<Item = T::Native>>(
585 array: A,
586) -> Result<Option<T::Native>, ArrowError> {
587 match array.data_type() {
588 DataType::Dictionary(_, _) => {
589 let null_count = array.null_count();
590
591 if null_count == array.len() {
592 return Ok(None);
593 }
594
595 let iter = ArrayIter::new(array);
596 let sum = iter
597 .into_iter()
598 .try_fold(T::default_value(), |accumulator, value| {
599 if let Some(value) = value {
600 accumulator.add_checked(value)
601 } else {
602 Ok(accumulator)
603 }
604 })?;
605
606 Ok(Some(sum))
607 }
608 DataType::RunEndEncoded(run_ends, _) => match run_ends.data_type() {
609 DataType::Int16 => ree::sum_checked::<types::Int16Type, T>(&array),
610 DataType::Int32 => ree::sum_checked::<types::Int32Type, T>(&array),
611 DataType::Int64 => ree::sum_checked::<types::Int64Type, T>(&array),
612 _ => unreachable!(),
613 },
614 _ => sum_checked::<T>(as_primitive_array(&array)),
615 }
616}
617
618// Logic for summing run-end-encoded arrays.
619mod ree {

Callers

nothing calls this directly

Calls 5

as_primitive_arrayFunction · 0.85
data_typeMethod · 0.45
null_countMethod · 0.45
lenMethod · 0.45
into_iterMethod · 0.45

Tested by

no test coverage detected