Skip to main content

vortex_array/compute/
mask.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::ops::Not;
5
6use vortex_error::VortexResult;
7use vortex_mask::Mask;
8
9use crate::Array;
10use crate::ArrayRef;
11use crate::IntoArray;
12use crate::arrays::ConstantArray;
13use crate::builtins::ArrayBuiltins;
14use crate::scalar::Scalar;
15
16/// Replace values with null where the mask is true.
17///
18/// The returned array is nullable but otherwise has the same dtype and length as `array`.
19///
20/// This function returns a lazy `ScalarFnArray` wrapping the [`Mask`](crate::expr::mask::Mask)
21/// expression that defers the actual masking operation until execution time. The mask is inverted
22/// (true=mask-out becomes true=keep) and passed as a boolean child to the expression.
23pub fn mask(array: &dyn Array, mask: &Mask) -> VortexResult<ArrayRef> {
24    let mask = mask.not();
25    match mask {
26        Mask::AllTrue(_) => array.to_array().cast(array.dtype().as_nullable()),
27        Mask::AllFalse(_) => Ok(ConstantArray::new(
28            Scalar::null(array.dtype().as_nullable()),
29            array.len(),
30        )
31        .into_array()),
32        Mask::Values(val) => array.to_array().mask(val.into_array()),
33    }
34}