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