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

Function sum_array

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

Returns the sum of values in the array. This doesn't detect overflow. Once overflowing, the result will wrap around. For an overflow-checking variant, use [`sum_array_checked`] instead.

(
    array: A,
)

Source from the content-addressed store, hash-verified

542/// This doesn't detect overflow. Once overflowing, the result will wrap around.
543/// For an overflow-checking variant, use [`sum_array_checked`] instead.
544pub fn sum_array<T: ArrowNumericType, A: ArrayAccessor<Item = T::Native>>(
545 array: A,
546) -> Option<T::Native> {
547 match array.data_type() {
548 DataType::Dictionary(_, _) => {
549 let null_count = array.null_count();
550
551 if null_count == array.len() {
552 return None;
553 }
554
555 let iter = ArrayIter::new(array);
556 let sum = iter
557 .into_iter()
558 .fold(T::default_value(), |accumulator, value| {
559 if let Some(value) = value {
560 accumulator.add_wrapping(value)
561 } else {
562 accumulator
563 }
564 });
565
566 Some(sum)
567 }
568 DataType::RunEndEncoded(run_ends, _) => match run_ends.data_type() {
569 DataType::Int16 => ree::sum_wrapping::<types::Int16Type, T>(&array),
570 DataType::Int32 => ree::sum_wrapping::<types::Int32Type, T>(&array),
571 DataType::Int64 => ree::sum_wrapping::<types::Int64Type, T>(&array),
572 _ => unreachable!(),
573 },
574 _ => sum::<T>(as_primitive_array(&array)),
575 }
576}
577
578/// Returns the sum of values in the array.
579///

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