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