vortex_array/arrays/bool/compute/
sum.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::ops::BitAnd;
5
6use vortex_error::VortexResult;
7use vortex_mask::AllOr;
8use vortex_scalar::Scalar;
9
10use crate::arrays::{BoolArray, BoolVTable};
11use crate::compute::{SumKernel, SumKernelAdapter};
12use crate::register_kernel;
13
14impl SumKernel for BoolVTable {
15    fn sum(&self, array: &BoolArray) -> VortexResult<Scalar> {
16        let true_count: Option<u64> = match array.validity_mask()?.boolean_buffer() {
17            AllOr::All => {
18                // All-valid
19                Some(array.boolean_buffer().count_set_bits() as u64)
20            }
21            AllOr::None => {
22                // All-invalid
23                unreachable!("All-invalid boolean array should have been handled by entry-point")
24            }
25            AllOr::Some(validity_mask) => Some(
26                array
27                    .boolean_buffer()
28                    .bitand(validity_mask)
29                    .count_set_bits() as u64,
30            ),
31        };
32        Ok(Scalar::from(true_count))
33    }
34}
35
36register_kernel!(SumKernelAdapter(BoolVTable).lift());