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::arrays::Decimal;
8use crate::arrays::decimal::vtable::DecimalArray;
9use crate::match_each_decimal_value_type;
10use crate::scalar::DecimalValue;
11use crate::scalar::Scalar;
12use crate::vtable::OperationsVTable;
13
14impl OperationsVTable<Decimal> for Decimal {
15    fn scalar_at(
16        array: &DecimalArray,
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::DynArray;
35    use crate::IntoArray;
36    use crate::arrays::Decimal;
37    use crate::arrays::DecimalArray;
38    use crate::dtype::DecimalDType;
39    use crate::dtype::Nullability;
40    use crate::scalar::DecimalValue;
41    use crate::scalar::Scalar;
42    use crate::validity::Validity;
43
44    #[test]
45    fn test_slice() {
46        let array = DecimalArray::new(
47            buffer![100i128, 200i128, 300i128, 4000i128],
48            DecimalDType::new(3, 2),
49            Validity::NonNullable,
50        )
51        .into_array();
52
53        let sliced = array.slice(1..3).unwrap();
54        assert_eq!(sliced.len(), 2);
55
56        let decimal = sliced.as_::<Decimal>();
57        assert_eq!(decimal.buffer::<i128>(), buffer![200i128, 300i128]);
58    }
59
60    #[test]
61    fn test_slice_nullable() {
62        let array = DecimalArray::new(
63            buffer![100i128, 200i128, 300i128, 4000i128],
64            DecimalDType::new(3, 2),
65            Validity::from_iter([false, true, false, true]),
66        )
67        .into_array();
68
69        let sliced = array.slice(1..3).unwrap();
70        assert_eq!(sliced.len(), 2);
71    }
72
73    #[test]
74    fn test_scalar_at() {
75        let array = DecimalArray::new(
76            buffer![100i128],
77            DecimalDType::new(3, 2),
78            Validity::NonNullable,
79        );
80
81        assert_eq!(
82            array.scalar_at(0).unwrap(),
83            Scalar::decimal(
84                DecimalValue::I128(100),
85                DecimalDType::new(3, 2),
86                Nullability::NonNullable
87            )
88        );
89    }
90}