vortex_array/arrays/bool/compute/
min_max.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::Mask;
8use vortex_scalar::Scalar;
9
10use crate::arrays::{BoolArray, BoolVTable};
11use crate::compute::{MinMaxKernel, MinMaxKernelAdapter, MinMaxResult};
12use crate::register_kernel;
13
14impl MinMaxKernel for BoolVTable {
15    fn min_max(&self, array: &BoolArray) -> VortexResult<Option<MinMaxResult>> {
16        let x = match array.validity_mask()? {
17            Mask::AllTrue(_) => array.boolean_buffer().clone(),
18            Mask::AllFalse(_) => return Ok(None),
19            Mask::Values(v) => array.boolean_buffer().bitand(v.boolean_buffer()),
20        };
21
22        // TODO(ngates): we should be able to bail out earlier as soon as we have one true and
23        //  one false value.
24        let mut slices = x.set_slices();
25        // If there are no slices, then all values are false
26        // if there is a single slice that covers the entire array, then all values are true
27        // otherwise, we have a mix of true and false values
28
29        let Some(slice) = slices.next() else {
30            // all false
31            return Ok(Some(MinMaxResult {
32                min: Scalar::new(array.dtype().clone(), false.into()),
33                max: Scalar::new(array.dtype().clone(), false.into()),
34            }));
35        };
36        if slice.0 == 0 && slice.1 == x.len() {
37            // all true
38            return Ok(Some(MinMaxResult {
39                min: Scalar::new(array.dtype().clone(), true.into()),
40                max: Scalar::new(array.dtype().clone(), true.into()),
41            }));
42        };
43
44        Ok(Some(MinMaxResult {
45            min: Scalar::new(array.dtype().clone(), false.into()),
46            max: Scalar::new(array.dtype().clone(), true.into()),
47        }))
48    }
49}
50
51register_kernel!(MinMaxKernelAdapter(BoolVTable).lift());