vortex_array/arrays/dict/compute/
like.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::IntoArray;
11use crate::arrays::ConstantArray;
12use crate::compute::LikeKernel;
13use crate::compute::LikeKernelAdapter;
14use crate::compute::LikeOptions;
15use crate::compute::like;
16use crate::register_kernel;
17
18impl LikeKernel for DictVTable {
19    fn like(
20        &self,
21        array: &DictArray,
22        pattern: &dyn Array,
23        options: LikeOptions,
24    ) -> VortexResult<Option<ArrayRef>> {
25        // if we have more values than codes, it is faster to canonicalise first.
26        if array.values().len() > array.codes().len() {
27            return Ok(None);
28        }
29        if let Some(pattern) = pattern.as_constant() {
30            let pattern = ConstantArray::new(pattern, array.values().len()).into_array();
31            let values = like(array.values(), &pattern, options)?;
32
33            // SAFETY: LIKE preserves the len of the values, so codes are still pointing at
34            //  valid positions.
35            // Preserve all_values_referenced since codes are unchanged
36            unsafe {
37                Ok(Some(
38                    DictArray::new_unchecked(array.codes().clone(), values)
39                        .set_all_values_referenced(array.has_all_values_referenced())
40                        .into_array(),
41                ))
42            }
43        } else {
44            Ok(None)
45        }
46    }
47}
48
49register_kernel!(LikeKernelAdapter(DictVTable).lift());