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::array::ProbeState;
10use crate::arrays::Decimal;
11use crate::match_each_decimal_value_type;
12use crate::scalar::DecimalValue;
13use crate::scalar::Scalar;
14
15impl OperationsVTable<Decimal> for Decimal {
16    type ProbeState = ();
17
18    fn probe_scalar(
19        state: &mut ProbeState<'_, Decimal>,
20        index: usize,
21        ctx: &mut ExecutionCtx,
22    ) -> VortexResult<Scalar> {
23        let array = state.array();
24        if !state.is_valid(index, ctx)? {
25            return Ok(Scalar::null(array.dtype().clone()));
26        }
27        // A canonical file node keeps its buffer without checking the values against its precision.
28        match_each_decimal_value_type!(array.values_type(), |D| {
29            Scalar::try_decimal(
30                DecimalValue::from(array.buffer::<D>()[index]),
31                array.decimal_dtype(),
32                array.dtype().nullability(),
33            )
34        })
35    }
36
37    fn scalar_at(
38        array: ArrayView<'_, Decimal>,
39        index: usize,
40        ctx: &mut ExecutionCtx,
41    ) -> VortexResult<Scalar> {
42        Self::probe_scalar(&mut ProbeState::once(array), index, ctx)
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use std::sync::LazyLock;
49
50    use vortex_buffer::buffer;
51    use vortex_error::VortexResult;
52    use vortex_session::VortexSession;
53
54    use crate::IntoArray;
55    use crate::VortexSessionExecute;
56    use crate::arrays::Decimal;
57    use crate::arrays::DecimalArray;
58    use crate::assert_arrays_eq;
59    use crate::dtype::DecimalDType;
60    use crate::dtype::Nullability;
61    use crate::optimizer::ArrayOptimizer;
62    use crate::scalar::DecimalValue;
63    use crate::scalar::Scalar;
64    use crate::validity::Validity;
65
66    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
67
68    #[test]
69    fn test_slice() {
70        let array =
71            DecimalArray::from_iter([100i16, 200, 300, 400], DecimalDType::new(3, 2)).into_array();
72
73        let sliced = array.slice(1..3).unwrap().optimize_ctx(&SESSION).unwrap();
74        assert_eq!(sliced.len(), 2);
75
76        let decimal = sliced.as_::<Decimal>();
77        assert_eq!(decimal.buffer::<i16>(), buffer![200i16, 300]);
78    }
79
80    #[test]
81    fn test_slice_nullable() {
82        let array = DecimalArray::new(
83            buffer![100i16, 200, 300, 400],
84            DecimalDType::new(3, 2),
85            Validity::from_iter([false, true, false, true]),
86            &mut SESSION.create_execution_ctx(),
87        )
88        .into_array();
89
90        let sliced = array.slice(1..3).unwrap();
91        assert_eq!(sliced.len(), 2);
92        assert_arrays_eq!(
93            sliced,
94            DecimalArray::from_option_iter([Some(200i16), None], DecimalDType::new(3, 2)),
95            &mut SESSION.create_execution_ctx()
96        );
97    }
98
99    #[test]
100    fn test_scalar_at() {
101        let array = DecimalArray::from_iter([100i16], DecimalDType::new(3, 2));
102
103        assert_eq!(
104            array
105                .execute_scalar(0, &mut SESSION.create_execution_ctx())
106                .unwrap(),
107            Scalar::decimal(
108                DecimalValue::I16(100),
109                DecimalDType::new(3, 2),
110                Nullability::NonNullable
111            )
112        );
113    }
114
115    #[test]
116    fn test_scalar_at_ignores_value_outside_precision_in_null_slot() -> VortexResult<()> {
117        let mut ctx = SESSION.create_execution_ctx();
118        let array = DecimalArray::new(
119            buffer![5i8, 100i8],
120            DecimalDType::new(2, 0),
121            Validity::from_iter([true, false]),
122            &mut ctx,
123        );
124
125        assert_eq!(
126            array.execute_scalar(0, &mut ctx)?,
127            Scalar::decimal(
128                DecimalValue::I8(5),
129                DecimalDType::new(2, 0),
130                Nullability::Nullable
131            )
132        );
133        assert!(array.execute_scalar(1, &mut ctx)?.is_null());
134        Ok(())
135    }
136}