Skip to main content

vortex_array/arrays/bool/compute/
rules.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5
6use crate::ArrayRef;
7use crate::IntoArray;
8use crate::arrays::BoolArray;
9use crate::arrays::BoolVTable;
10use crate::arrays::FilterReduceAdaptor;
11use crate::arrays::MaskedArray;
12use crate::arrays::MaskedVTable;
13use crate::arrays::SliceReduceAdaptor;
14use crate::optimizer::rules::ArrayParentReduceRule;
15use crate::optimizer::rules::ParentRuleSet;
16use crate::scalar_fn::fns::cast::CastReduceAdaptor;
17use crate::scalar_fn::fns::mask::MaskReduceAdaptor;
18use crate::vtable::ValidityHelper;
19
20pub(crate) const RULES: ParentRuleSet<BoolVTable> = ParentRuleSet::new(&[
21    ParentRuleSet::lift(&BoolMaskedValidityRule),
22    ParentRuleSet::lift(&CastReduceAdaptor(BoolVTable)),
23    ParentRuleSet::lift(&MaskReduceAdaptor(BoolVTable)),
24    ParentRuleSet::lift(&SliceReduceAdaptor(BoolVTable)),
25    ParentRuleSet::lift(&FilterReduceAdaptor(BoolVTable)),
26]);
27
28/// Rule to push down validity masking from MaskedArray parent into BoolArray child.
29///
30/// When a BoolArray is wrapped by a MaskedArray, this rule merges the mask's validity
31/// with the BoolArray's existing validity, eliminating the need for the MaskedArray wrapper.
32#[derive(Default, Debug)]
33pub struct BoolMaskedValidityRule;
34
35impl ArrayParentReduceRule<BoolVTable> for BoolMaskedValidityRule {
36    type Parent = MaskedVTable;
37
38    fn reduce_parent(
39        &self,
40        array: &BoolArray,
41        parent: &MaskedArray,
42        child_idx: usize,
43    ) -> VortexResult<Option<ArrayRef>> {
44        if child_idx > 0 {
45            return Ok(None);
46        }
47
48        // Merge the parent's validity mask into the child's validity
49        // TODO(joe): make this lazy
50        Ok(Some(
51            BoolArray::new(
52                array.to_bit_buffer(),
53                array.validity().clone().and(parent.validity().clone())?,
54            )
55            .into_array(),
56        ))
57    }
58}