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