vortex_runend/compute/
take.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use num_traits::{AsPrimitive, NumCast};
5use vortex_array::arrays::PrimitiveArray;
6use vortex_array::compute::{TakeKernel, TakeKernelAdapter, take};
7use vortex_array::search_sorted::{SearchResult, SearchSorted, SearchSortedSide};
8use vortex_array::validity::Validity;
9use vortex_array::vtable::ValidityHelper;
10use vortex_array::{Array, ArrayRef, ToCanonical, register_kernel};
11use vortex_buffer::Buffer;
12use vortex_dtype::match_each_integer_ptype;
13use vortex_error::{VortexResult, vortex_bail};
14
15use crate::{RunEndArray, RunEndVTable};
16
17impl TakeKernel for RunEndVTable {
18    #[allow(clippy::cast_possible_truncation)]
19    fn take(&self, array: &RunEndArray, indices: &dyn Array) -> VortexResult<ArrayRef> {
20        let primitive_indices = indices.to_primitive()?;
21
22        let checked_indices = match_each_integer_ptype!(primitive_indices.ptype(), |P| {
23            primitive_indices
24                .as_slice::<P>()
25                .iter()
26                .copied()
27                .map(|idx| {
28                    let usize_idx = idx as usize;
29                    if usize_idx >= array.len() {
30                        vortex_bail!(OutOfBounds: usize_idx, 0, array.len());
31                    }
32                    Ok(usize_idx)
33                })
34                .collect::<VortexResult<Vec<_>>>()?
35        });
36
37        take_indices_unchecked(array, &checked_indices, primitive_indices.validity())
38    }
39}
40
41register_kernel!(TakeKernelAdapter(RunEndVTable).lift());
42
43/// Perform a take operation on a RunEndArray by binary searching for each of the indices.
44pub fn take_indices_unchecked<T: AsPrimitive<usize>>(
45    array: &RunEndArray,
46    indices: &[T],
47    validity: &Validity,
48) -> VortexResult<ArrayRef> {
49    let ends = array.ends().to_primitive()?;
50    let ends_len = ends.len();
51
52    // TODO(joe): use the validity mask to skip search sorted.
53    let physical_indices = match_each_integer_ptype!(ends.ptype(), |I| {
54        let end_slices = ends.as_slice::<I>();
55        let buffer = Buffer::from_trusted_len_iter(
56            indices
57                .iter()
58                .map(|idx| idx.as_() + array.offset())
59                .map(|idx| {
60                    match <I as NumCast>::from(idx) {
61                        Some(idx) => end_slices.search_sorted(&idx, SearchSortedSide::Right),
62                        None => {
63                            // The idx is too large for I, therefore it's out of bounds.
64                            SearchResult::NotFound(ends_len)
65                        }
66                    }
67                })
68                .map(|result| result.to_ends_index(ends_len) as u64),
69        );
70
71        PrimitiveArray::new(buffer, validity.clone())
72    });
73
74    take(array.values(), physical_indices.as_ref())
75}
76
77#[cfg(test)]
78mod test {
79    use vortex_array::arrays::PrimitiveArray;
80    use vortex_array::compute::take;
81    use vortex_array::{Array, IntoArray, ToCanonical};
82    use vortex_dtype::{DType, Nullability, PType};
83    use vortex_scalar::{Scalar, ScalarValue};
84
85    use crate::RunEndArray;
86
87    fn ree_array() -> RunEndArray {
88        RunEndArray::encode(
89            PrimitiveArray::from_iter([1, 1, 1, 4, 4, 4, 2, 2, 5, 5, 5, 5]).into_array(),
90        )
91        .unwrap()
92    }
93
94    #[test]
95    fn ree_take() {
96        let taken = take(
97            ree_array().as_ref(),
98            PrimitiveArray::from_iter([9, 8, 1, 3]).as_ref(),
99        )
100        .unwrap();
101        assert_eq!(
102            taken.to_primitive().unwrap().as_slice::<i32>(),
103            &[5, 5, 1, 4]
104        );
105    }
106
107    #[test]
108    fn ree_take_end() {
109        let taken = take(
110            ree_array().as_ref(),
111            PrimitiveArray::from_iter([11]).as_ref(),
112        )
113        .unwrap();
114        assert_eq!(taken.to_primitive().unwrap().as_slice::<i32>(), &[5]);
115    }
116
117    #[test]
118    #[should_panic]
119    fn ree_take_out_of_bounds() {
120        take(
121            ree_array().as_ref(),
122            PrimitiveArray::from_iter([12]).as_ref(),
123        )
124        .unwrap();
125    }
126
127    #[test]
128    fn sliced_take() {
129        let sliced = ree_array().slice(4, 9).unwrap();
130        let taken = take(
131            sliced.as_ref(),
132            PrimitiveArray::from_iter([1, 3, 4]).as_ref(),
133        )
134        .unwrap();
135
136        assert_eq!(taken.len(), 3);
137        assert_eq!(taken.scalar_at(0).unwrap(), 4.into());
138        assert_eq!(taken.scalar_at(1).unwrap(), 2.into());
139        assert_eq!(taken.scalar_at(2).unwrap(), 5.into());
140    }
141
142    #[test]
143    fn ree_take_nullable() {
144        let taken = take(
145            ree_array().as_ref(),
146            PrimitiveArray::from_option_iter([Some(1), None]).as_ref(),
147        )
148        .unwrap();
149
150        assert_eq!(
151            taken.scalar_at(0).unwrap(),
152            Scalar::new(
153                DType::Primitive(PType::I32, Nullability::Nullable),
154                ScalarValue::from(1i32)
155            )
156        );
157        assert_eq!(
158            taken.scalar_at(1).unwrap(),
159            Scalar::null(DType::Primitive(PType::I32, Nullability::Nullable))
160        );
161    }
162}