Skip to main content

vortex_array/arrays/extension/compute/
compare.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::IntoArray;
9use crate::array::ArrayView;
10use crate::arrays::ConstantArray;
11use crate::arrays::Extension;
12use crate::arrays::extension::ExtensionArrayExt;
13use crate::builtins::ArrayBuiltins;
14use crate::scalar_fn::builtin::compare::CompareKernel;
15use crate::scalar_fn::builtin::operators::CompareOperator;
16
17impl CompareKernel for Extension {
18    fn compare(
19        lhs: ArrayView<'_, Extension>,
20        rhs: &ArrayRef,
21        operator: CompareOperator,
22        _ctx: &mut ExecutionCtx,
23    ) -> VortexResult<Option<ArrayRef>> {
24        // Storage values are only comparable when both sides share the same extension dtype
25        // (e.g. timestamps in different units must not compare their raw storage).
26        if !lhs.dtype().eq_ignore_nullability(rhs.dtype()) {
27            return Ok(None);
28        }
29
30        // If the RHS is a constant, we can extract the storage scalar.
31        if let Some(const_ext) = rhs.as_constant() {
32            let storage_scalar = const_ext.as_extension().to_storage_scalar();
33            return lhs
34                .storage_array()
35                .clone()
36                .compare(
37                    ConstantArray::new(storage_scalar, lhs.len()).into_array(),
38                    operator,
39                )
40                .map(Some);
41        }
42
43        // If the RHS is an extension array matching ours, we can extract the storage.
44        if let Some(rhs_ext) = rhs.as_opt::<Extension>() {
45            return lhs
46                .storage_array()
47                .clone()
48                .compare(rhs_ext.storage_array().clone(), operator)
49                .map(Some);
50        }
51
52        // Otherwise, we need the RHS to handle this comparison.
53        Ok(None)
54    }
55}