Skip to main content

vortex_array/arrays/dict/compute/
compare.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5
6use super::Dict;
7use super::DictArray;
8use crate::ArrayRef;
9use crate::Canonical;
10use crate::ExecutionCtx;
11use crate::IntoArray;
12use crate::array::ArrayView;
13use crate::arrays::ConstantArray;
14use crate::arrays::dict::DictArrayExt;
15use crate::arrays::dict::DictArraySlotsExt;
16use crate::builtins::ArrayBuiltins;
17use crate::scalar_fn::builtin::compare::CompareKernel;
18use crate::scalar_fn::builtin::operators::CompareOperator;
19
20impl CompareKernel for Dict {
21    fn compare(
22        lhs: ArrayView<'_, Dict>,
23        rhs: &ArrayRef,
24        operator: CompareOperator,
25        ctx: &mut ExecutionCtx,
26    ) -> VortexResult<Option<ArrayRef>> {
27        // if we have more values than codes, it is faster to canonicalise first.
28        if lhs.values().len() > lhs.codes().len() {
29            return Ok(None);
30        }
31
32        // If the RHS is constant, then we just need to compare against our encoded values.
33        if let Some(rhs) = rhs.as_constant() {
34            let compare_result = lhs.values().clone().compare(
35                ConstantArray::new(rhs.clone(), lhs.values().len()).into_array(),
36                operator,
37            )?;
38
39            // SAFETY: values len preserved, codes all still point to valid values
40            let result = unsafe {
41                DictArray::new_unchecked(lhs.codes().clone(), compare_result)
42                    .set_all_values_referenced(lhs.has_all_values_referenced())
43                    .into_array()
44            };
45
46            // We canonicalize the result because dictionary-encoded bools is dumb.
47            return Ok(Some(result.execute::<Canonical>(ctx)?.into_array()));
48        }
49
50        // It's a little more complex, but we could perform a comparison against the dictionary
51        // values in the future.
52        Ok(None)
53    }
54}