vortex_array/arrays/decimal/compute/
take.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use num_traits::AsPrimitive;
5use vortex_buffer::Buffer;
6use vortex_dtype::{NativePType, match_each_integer_ptype};
7use vortex_error::VortexResult;
8use vortex_scalar::{NativeDecimalType, match_each_decimal_value_type};
9
10use crate::arrays::{DecimalArray, DecimalVTable};
11use crate::compute::{TakeKernel, TakeKernelAdapter};
12use crate::vtable::ValidityHelper;
13use crate::{Array, ArrayRef, ToCanonical, register_kernel};
14
15impl TakeKernel for DecimalVTable {
16    fn take(&self, array: &DecimalArray, indices: &dyn Array) -> VortexResult<ArrayRef> {
17        let indices = indices.to_primitive()?;
18        let validity = array.validity().take(indices.as_ref())?;
19
20        // TODO(joe): if the true count of take indices validity is low, only take array values with
21        // valid indices.
22        let decimal = match_each_decimal_value_type!(array.values_type(), |D| {
23            match_each_integer_ptype!(indices.ptype(), |I| {
24                let buffer =
25                    take_to_buffer::<I, D>(indices.as_slice::<I>(), array.buffer::<D>().as_slice());
26                DecimalArray::new(buffer, array.decimal_dtype(), validity)
27            })
28        });
29
30        Ok(decimal.to_array())
31    }
32}
33
34register_kernel!(TakeKernelAdapter(DecimalVTable).lift());
35
36#[inline]
37fn take_to_buffer<I: NativePType + AsPrimitive<usize>, T: NativeDecimalType>(
38    indices: &[I],
39    values: &[T],
40) -> Buffer<T> {
41    indices.iter().map(|idx| values[idx.as_()]).collect()
42}
43
44#[cfg(test)]
45mod tests {
46    use vortex_buffer::buffer;
47    use vortex_dtype::{DecimalDType, Nullability};
48    use vortex_scalar::{DecimalValue, Scalar};
49
50    use crate::IntoArray;
51    use crate::arrays::{DecimalArray, DecimalVTable, PrimitiveArray};
52    use crate::compute::take;
53    use crate::validity::Validity;
54
55    #[test]
56    fn test_take() {
57        let array = DecimalArray::new(
58            buffer![10i128, 11i128, 12i128, 13i128],
59            DecimalDType::new(19, 1),
60            Validity::NonNullable,
61        );
62
63        let indices = buffer![0, 2, 3].into_array();
64        let taken = take(array.as_ref(), indices.as_ref()).unwrap();
65        let taken_decimals = taken.as_::<DecimalVTable>();
66        assert_eq!(
67            taken_decimals.buffer::<i128>(),
68            buffer![10i128, 12i128, 13i128]
69        );
70        assert_eq!(taken_decimals.decimal_dtype(), DecimalDType::new(19, 1));
71    }
72
73    #[test]
74    fn test_take_null_indices() {
75        let array = DecimalArray::new(
76            buffer![i128::MAX, 11i128, 12i128, 13i128],
77            DecimalDType::new(19, 1),
78            Validity::NonNullable,
79        );
80
81        let indices = PrimitiveArray::from_option_iter([None, Some(2), Some(3)]).into_array();
82        let taken = take(array.as_ref(), indices.as_ref()).unwrap();
83
84        assert!(taken.scalar_at(0).unwrap().is_null());
85        assert_eq!(
86            taken.scalar_at(1).unwrap(),
87            Scalar::decimal(
88                DecimalValue::I128(12i128),
89                array.decimal_dtype(),
90                Nullability::Nullable
91            )
92        );
93
94        assert_eq!(
95            taken.scalar_at(2).unwrap(),
96            Scalar::decimal(
97                DecimalValue::I128(13i128),
98                array.decimal_dtype(),
99                Nullability::Nullable
100            )
101        );
102    }
103}