vortex_array/arrays/decimal/vtable/
operations.rs1use 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 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 rstest::rstest;
51 use vortex_buffer::buffer;
52 use vortex_error::VortexResult;
53 use vortex_session::VortexSession;
54
55 use crate::IntoArray;
56 use crate::VortexSessionExecute;
57 use crate::arrays::Decimal;
58 use crate::arrays::DecimalArray;
59 use crate::assert_arrays_eq;
60 use crate::dtype::DecimalDType;
61 use crate::dtype::Nullability;
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 = DecimalArray::new(
71 buffer![100i128, 200i128, 300i128, 4000i128],
72 DecimalDType::new(3, 2),
73 Validity::NonNullable,
74 )
75 .into_array();
76
77 let sliced = array.slice(1..3).unwrap();
78 assert_eq!(sliced.len(), 2);
79
80 let decimal = sliced.as_::<Decimal>();
81 assert_eq!(decimal.buffer::<i128>(), buffer![200i128, 300i128]);
82 }
83
84 #[test]
85 fn test_slice_nullable() {
86 let array = DecimalArray::new(
87 buffer![100i128, 200i128, 300i128, 4000i128],
88 DecimalDType::new(3, 2),
89 Validity::from_iter([false, true, false, true]),
90 )
91 .into_array();
92
93 let sliced = array.slice(1..3).unwrap();
94 assert_eq!(sliced.len(), 2);
95 assert_arrays_eq!(
96 sliced,
97 DecimalArray::from_option_iter([Some(200i128), None], DecimalDType::new(3, 2)),
98 &mut SESSION.create_execution_ctx()
99 );
100 }
101
102 #[test]
103 fn test_scalar_at() {
104 let array = DecimalArray::new(
105 buffer![100i128],
106 DecimalDType::new(3, 2),
107 Validity::NonNullable,
108 );
109
110 assert_eq!(
111 array
112 .execute_scalar(0, &mut SESSION.create_execution_ctx())
113 .unwrap(),
114 Scalar::decimal(
115 DecimalValue::I128(100),
116 DecimalDType::new(3, 2),
117 Nullability::NonNullable
118 )
119 );
120 }
121
122 #[rstest]
123 #[case::positive(100i8)]
124 #[case::negative(-100i8)]
125 fn test_scalar_at_value_outside_precision_errors(#[case] value: i8) -> VortexResult<()> {
126 let array = DecimalArray::new(
128 buffer![value],
129 DecimalDType::new(2, 0),
130 Validity::NonNullable,
131 );
132
133 let err = array
134 .execute_scalar(0, &mut SESSION.create_execution_ctx())
135 .unwrap_err();
136 assert!(
137 err.to_string().contains("does not fit in precision"),
138 "unexpected error: {err}"
139 );
140 Ok(())
141 }
142
143 #[test]
144 fn test_scalar_at_ignores_value_outside_precision_in_null_slot() -> VortexResult<()> {
145 let array = DecimalArray::new(
146 buffer![5i8, 100i8],
147 DecimalDType::new(2, 0),
148 Validity::from_iter([true, false]),
149 );
150 let mut ctx = SESSION.create_execution_ctx();
151
152 assert_eq!(
153 array.execute_scalar(0, &mut ctx)?,
154 Scalar::decimal(
155 DecimalValue::I8(5),
156 DecimalDType::new(2, 0),
157 Nullability::Nullable
158 )
159 );
160 assert!(array.execute_scalar(1, &mut ctx)?.is_null());
161 Ok(())
162 }
163}