Skip to main content

vortex_array/arrays/decimal/vtable/
operations.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5
6use crate::ExecutionCtx;
7use crate::array::ArrayView;
8use crate::array::OperationsVTable;
9use crate::arrays::Decimal;
10use crate::match_each_decimal_value_type;
11use crate::scalar::DecimalValue;
12use crate::scalar::Scalar;
13
14impl OperationsVTable<Decimal> for Decimal {
15    fn scalar_at(
16        array: ArrayView<'_, Decimal>,
17        index: usize,
18        _ctx: &mut ExecutionCtx,
19    ) -> VortexResult<Scalar> {
20        Ok(match_each_decimal_value_type!(array.values_type(), |D| {
21            Scalar::decimal(
22                DecimalValue::from(array.buffer::<D>()[index]),
23                array.decimal_dtype(),
24                array.dtype().nullability(),
25            )
26        }))
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use vortex_buffer::buffer;
33
34    use crate::IntoArray;
35    use crate::arrays::Decimal;
36    use crate::arrays::DecimalArray;
37    use crate::dtype::DecimalDType;
38    use crate::dtype::Nullability;
39    use crate::scalar::DecimalValue;
40    use crate::scalar::Scalar;
41    use crate::validity::Validity;
42
43    #[test]
44    fn test_slice() {
45        let array = DecimalArray::new(
46            buffer![100i128, 200i128, 300i128, 4000i128],
47            DecimalDType::new(3, 2),
48            Validity::NonNullable,
49        )
50        .into_array();
51
52        let sliced = array.slice(1..3).unwrap();
53        assert_eq!(sliced.len(), 2);
54
55        let decimal = sliced.as_::<Decimal>();
56        assert_eq!(decimal.buffer::<i128>(), buffer![200i128, 300i128]);
57    }
58
59    #[test]
60    fn test_slice_nullable() {
61        let array = DecimalArray::new(
62            buffer![100i128, 200i128, 300i128, 4000i128],
63            DecimalDType::new(3, 2),
64            Validity::from_iter([false, true, false, true]),
65        )
66        .into_array();
67
68        let sliced = array.slice(1..3).unwrap();
69        assert_eq!(sliced.len(), 2);
70    }
71
72    #[test]
73    fn test_scalar_at() {
74        let array = DecimalArray::new(
75            buffer![100i128],
76            DecimalDType::new(3, 2),
77            Validity::NonNullable,
78        );
79
80        assert_eq!(
81            array.scalar_at(0).unwrap(),
82            Scalar::decimal(
83                DecimalValue::I128(100),
84                DecimalDType::new(3, 2),
85                Nullability::NonNullable
86            )
87        );
88    }
89}