Skip to main content

vortex_array/arrays/widen/compute/
compare.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_buffer::BitBuffer;
5use vortex_error::VortexExpect;
6use vortex_error::VortexResult;
7
8use crate::ArrayRef;
9use crate::ExecutionCtx;
10use crate::IntoArray;
11use crate::array::ArrayView;
12use crate::arrays::BoolArray;
13use crate::arrays::ConstantArray;
14use crate::arrays::Widen;
15use crate::arrays::widen::WidenArraySlotsExt;
16use crate::arrays::widen::compute::constant_fits_values;
17use crate::arrays::widen::compute::constant_in_values_dtype;
18use crate::arrays::widen::compute::ptype_holding_both_values;
19use crate::builtins::ArrayBuiltins;
20use crate::dtype::DType;
21use crate::dtype::DecimalDType;
22use crate::scalar::Scalar;
23use crate::scalar_fn::fns::binary::CompareKernel;
24use crate::scalar_fn::fns::operators::CompareOperator;
25use crate::scalar_fn::fns::operators::Operator;
26
27impl CompareKernel for Widen {
28    fn compare(
29        lhs: ArrayView<'_, Self>,
30        rhs: &ArrayRef,
31        operator: CompareOperator,
32        ctx: &mut ExecutionCtx,
33    ) -> VortexResult<Option<ArrayRef>> {
34        if !lhs.dtype().eq_ignore_nullability(rhs.dtype()) {
35            return Ok(None);
36        }
37
38        if let Some(rhs) = rhs.as_opt::<Widen>() {
39            return compare_narrow_values(lhs, rhs, operator);
40        }
41
42        // `CompareExecuteAdaptor` answers a null constant before it calls this kernel.
43        let Some(constant) = rhs.as_constant() else {
44            return Ok(None);
45        };
46        let values = lhs.values();
47        let Some(fits) = constant_fits_values(constant, values.dtype()) else {
48            return Ok(None);
49        };
50        if !fits {
51            return compare_out_of_range(lhs, constant, operator, ctx).map(Some);
52        }
53
54        let constant = constant_in_values_dtype(constant, values, lhs.len())?;
55
56        values.binary(constant, Operator::from(operator)).map(Some)
57    }
58}
59
60/// Compares the values of two `Widen` arrays at the wider of the two value types.
61///
62/// Decimal values with the same scale compare at the larger of the two child precisions, which
63/// holds every value of both children.
64fn compare_narrow_values(
65    lhs: ArrayView<'_, Widen>,
66    rhs: ArrayView<'_, Widen>,
67    operator: CompareOperator,
68) -> VortexResult<Option<ArrayRef>> {
69    let lhs_values = lhs.values();
70    let rhs_values = rhs.values();
71
72    let dtype = match (lhs_values.dtype(), rhs_values.dtype()) {
73        (DType::Primitive(..), DType::Primitive(..)) => {
74            let Some(ptype) = ptype_holding_both_values(lhs, rhs) else {
75                return Ok(None);
76            };
77
78            DType::from(ptype)
79        }
80        (DType::Decimal(lhs_dtype, _), DType::Decimal(rhs_dtype, _))
81            if lhs_dtype.scale() == rhs_dtype.scale() =>
82        {
83            DType::Decimal(
84                DecimalDType::new(
85                    lhs_dtype.precision().max(rhs_dtype.precision()),
86                    lhs_dtype.scale(),
87                ),
88                lhs_values.dtype().nullability(),
89            )
90        }
91        _ => return Ok(None),
92    };
93    let lhs_values = lhs_values.cast(dtype.with_nullability(lhs_values.dtype().nullability()))?;
94    let rhs_values = rhs_values.cast(dtype.with_nullability(rhs_values.dtype().nullability()))?;
95
96    lhs_values
97        .binary(rhs_values, Operator::from(operator))
98        .map(Some)
99}
100
101/// Answers a compare of `lhs` with a valid `constant` outside the range of its values.
102///
103/// Every value is on the same side of the constant, so every valid row has the same answer, and the
104/// result takes the validity of the values. Without nulls, the result is a [`ConstantArray`], so
105/// that later steps such as a filter keep their constant fast paths.
106fn compare_out_of_range(
107    lhs: ArrayView<'_, Widen>,
108    constant: &Scalar,
109    operator: CompareOperator,
110    ctx: &mut ExecutionCtx,
111) -> VortexResult<ArrayRef> {
112    // The range of the values holds zero, so the constant is below the range exactly when it is
113    // below zero. An unsigned constant outside the range is always above it.
114    let zero = Scalar::zero_value(constant.dtype());
115    let below = constant
116        .partial_cmp(&zero)
117        .vortex_expect("a scalar compares with the zero of its own dtype")
118        .is_lt();
119    let answer = match operator {
120        CompareOperator::Eq => false,
121        CompareOperator::NotEq => true,
122        CompareOperator::Lt | CompareOperator::Lte => !below,
123        CompareOperator::Gt | CompareOperator::Gte => below,
124    };
125
126    let validity = lhs
127        .values()
128        .validity()?
129        .union_nullability(constant.dtype().nullability());
130    if validity.definitely_no_nulls() {
131        let answer = Scalar::bool(answer, validity.nullability());
132        return Ok(ConstantArray::new(answer, lhs.len()).into_array());
133    }
134
135    let bits = BitBuffer::full_in(answer, lhs.len(), ctx.allocator().clone());
136    Ok(BoolArray::try_new(bits, validity)?.into_array())
137}