vortex_array/arrays/decimal/vtable/
operations.rs1use vortex_dtype::match_each_decimal_value_type;
5use vortex_error::VortexResult;
6
7use crate::arrays::DecimalArray;
8use crate::arrays::DecimalVTable;
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 use vortex_dtype::DecimalDType;
29 use vortex_dtype::Nullability;
30
31 use crate::Array;
32 use crate::arrays::DecimalArray;
33 use crate::arrays::DecimalVTable;
34 use crate::scalar::DecimalValue;
35 use crate::scalar::Scalar;
36 use crate::validity::Validity;
37
38 #[test]
39 fn test_slice() {
40 let array = DecimalArray::new(
41 buffer![100i128, 200i128, 300i128, 4000i128],
42 DecimalDType::new(3, 2),
43 Validity::NonNullable,
44 )
45 .to_array();
46
47 let sliced = array.slice(1..3).unwrap();
48 assert_eq!(sliced.len(), 2);
49
50 let decimal = sliced.as_::<DecimalVTable>();
51 assert_eq!(decimal.buffer::<i128>(), buffer![200i128, 300i128]);
52 }
53
54 #[test]
55 fn test_slice_nullable() {
56 let array = DecimalArray::new(
57 buffer![100i128, 200i128, 300i128, 4000i128],
58 DecimalDType::new(3, 2),
59 Validity::from_iter([false, true, false, true]),
60 )
61 .to_array();
62
63 let sliced = array.slice(1..3).unwrap();
64 assert_eq!(sliced.len(), 2);
65 }
66
67 #[test]
68 fn test_scalar_at() {
69 let array = DecimalArray::new(
70 buffer![100i128],
71 DecimalDType::new(3, 2),
72 Validity::NonNullable,
73 );
74
75 assert_eq!(
76 array.scalar_at(0).unwrap(),
77 Scalar::decimal(
78 DecimalValue::I128(100),
79 DecimalDType::new(3, 2),
80 Nullability::NonNullable
81 )
82 );
83 }
84}