Skip to main content

vortex_array/arrays/widen/compute/
zip.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::ExecutionCtx;
8use crate::array::ArrayView;
9use crate::arrays::Widen;
10use crate::arrays::widen::WidenArraySlotsExt;
11use crate::arrays::widen::compute::cast_keeping_nullability;
12use crate::arrays::widen::compute::ptype_holding_both_values;
13use crate::arrays::widen::compute::ptype_holding_values_and_scalar;
14use crate::arrays::widen::compute::wrap_values;
15use crate::builtins::ArrayBuiltins;
16use crate::scalar_fn::fns::zip::ZipKernel;
17
18/// Zips the values of a [`Widen`] with another `Widen` or a constant at the type that holds both.
19/// The adaptor normalizes either branch to `array` and adjusts the mask accordingly.
20impl ZipKernel for Widen {
21    fn zip(
22        array: ArrayView<'_, Self>,
23        if_false: &ArrayRef,
24        mask: &ArrayRef,
25        _ctx: &mut ExecutionCtx,
26    ) -> VortexResult<Option<ArrayRef>> {
27        let (if_false_values, ptype) = if let Some(other) = if_false.as_opt::<Widen>() {
28            let Some(ptype) = ptype_holding_both_values(array, other) else {
29                return Ok(None);
30            };
31            (other.values().clone(), ptype)
32        } else if let Some(constant) = if_false.as_constant() {
33            let Some(ptype) = ptype_holding_values_and_scalar(array, constant) else {
34                return Ok(None);
35            };
36            (if_false.clone(), ptype)
37        } else {
38            return Ok(None);
39        };
40
41        let if_true_values = cast_keeping_nullability(array.values(), ptype)?;
42        let if_false_values = cast_keeping_nullability(&if_false_values, ptype)?;
43
44        let zipped = mask.zip(if_true_values, if_false_values)?;
45        let dtype = array.dtype().with_nullability(zipped.dtype().nullability());
46
47        wrap_values(zipped, dtype).map(Some)
48    }
49}