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