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