Skip to main content

vortex_array/arrays/widen/compute/
between.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::Constant;
9use crate::arrays::Widen;
10use crate::arrays::widen::WidenArraySlotsExt;
11use crate::arrays::widen::compute::constant_fits_values;
12use crate::arrays::widen::compute::constant_in_values_dtype;
13use crate::builtins::ArrayBuiltins;
14use crate::scalar_fn::fns::between::BetweenOptions;
15use crate::scalar_fn::fns::between::BetweenReduce;
16use crate::scalar_fn::fns::between::as_two_compares;
17
18impl BetweenReduce for Widen {
19    fn between(
20        array: ArrayView<'_, Self>,
21        lower: &ArrayRef,
22        upper: &ArrayRef,
23        options: &BetweenOptions,
24    ) -> VortexResult<Option<ArrayRef>> {
25        let values = array.values();
26
27        if let (Some(lower), Some(upper)) = (lower.as_constant(), upper.as_constant())
28            && constant_fits_values(lower, values.dtype()) == Some(true)
29            && constant_fits_values(upper, values.dtype()) == Some(true)
30        {
31            let lower = constant_in_values_dtype(lower, values, array.len())?;
32            let upper = constant_in_values_dtype(upper, values, array.len())?;
33            return values
34                .clone()
35                .between(lower, upper, options.clone())
36                .map(Some);
37        }
38
39        // Each compare of a `Widen` with a constant or another `Widen` runs the compare kernel of
40        // `Widen`. Any other bound declines, so that `Between` decodes the `Widen` once.
41        let is_operand = |bound: &ArrayRef| bound.is::<Constant>() || bound.is::<Widen>();
42        if !is_operand(lower) || !is_operand(upper) {
43            return Ok(None);
44        }
45
46        as_two_compares(array.array(), lower, upper, options).map(Some)
47    }
48}