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