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