Skip to main content

vortex_array/arrays/masked/compute/
mask.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::array::ArrayView;
8use crate::arrays::Masked;
9use crate::arrays::masked::MaskedArrayExt;
10use crate::arrays::scalar_fn::ScalarFnFactoryExt;
11use crate::scalar_fn::EmptyOptions;
12use crate::scalar_fn::fns::mask::Mask as MaskExpr;
13use crate::scalar_fn::fns::mask::MaskReduce;
14use crate::validity::Validity;
15
16impl MaskReduce for Masked {
17    fn mask(array: ArrayView<'_, Masked>, mask: &ArrayRef) -> VortexResult<Option<ArrayRef>> {
18        // AND the existing validity mask with the new mask and push into child.
19        let combined_mask = array
20            .validity()?
21            .and(Validity::Array(mask.clone()))?
22            .to_array(array.len());
23        let masked_child = MaskExpr.try_new_array(
24            array.child().len(),
25            EmptyOptions,
26            [array.child().clone(), combined_mask],
27        )?;
28        Ok(Some(masked_child))
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use rstest::rstest;
35
36    use crate::IntoArray;
37    use crate::arrays::MaskedArray;
38    use crate::arrays::PrimitiveArray;
39    use crate::compute::conformance::mask::test_mask_conformance;
40    use crate::validity::Validity;
41
42    #[rstest]
43    #[case(
44        MaskedArray::try_new(
45            PrimitiveArray::from_iter([1i32, 2, 3, 4, 5]).into_array(),
46            Validity::from_iter([true, true, false, true, false])
47        ).unwrap()
48    )]
49    #[case(
50        MaskedArray::try_new(
51            PrimitiveArray::from_iter([10i32, 20, 30]).into_array(),
52            Validity::AllValid
53        ).unwrap()
54    )]
55    #[case(
56        MaskedArray::try_new(
57            PrimitiveArray::from_iter(0..100).into_array(),
58            Validity::from_iter((0..100).map(|i| i % 3 != 0))
59        ).unwrap()
60    )]
61    fn test_mask_masked_conformance(#[case] array: MaskedArray) {
62        test_mask_conformance(&array.into_array());
63    }
64}