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