vortex_vector/decimal/
vector.rs1use std::fmt::Debug;
7use std::ops::RangeBounds;
8
9use vortex_dtype::{DecimalType, DecimalTypeDowncast, DecimalTypeUpcast, NativeDecimalType, i256};
10use vortex_error::vortex_panic;
11use vortex_mask::Mask;
12
13use crate::decimal::{DVector, DecimalVectorMut};
14use crate::{Scalar, VectorOps, match_each_dvector};
15
16#[derive(Clone, Debug)]
18pub enum DecimalVector {
19 D8(DVector<i8>),
21 D16(DVector<i16>),
23 D32(DVector<i32>),
25 D64(DVector<i64>),
27 D128(DVector<i128>),
29 D256(DVector<i256>),
31}
32
33impl DecimalVector {
34 pub fn precision(&self) -> u8 {
36 match_each_dvector!(self, |v| { v.precision() })
37 }
38
39 pub fn scale(&self) -> i8 {
41 match_each_dvector!(self, |v| { v.scale() })
42 }
43
44 pub fn decimal_type(&self) -> DecimalType {
46 match self {
47 Self::D8(_) => DecimalType::I8,
48 Self::D16(_) => DecimalType::I16,
49 Self::D32(_) => DecimalType::I32,
50 Self::D64(_) => DecimalType::I64,
51 Self::D128(_) => DecimalType::I128,
52 Self::D256(_) => DecimalType::I256,
53 }
54 }
55}
56
57impl VectorOps for DecimalVector {
58 type Mutable = DecimalVectorMut;
59
60 fn len(&self) -> usize {
61 match_each_dvector!(self, |v| { v.len() })
62 }
63
64 fn validity(&self) -> &Mask {
65 match_each_dvector!(self, |v| { v.validity() })
66 }
67
68 fn scalar_at(&self, index: usize) -> Scalar {
69 match_each_dvector!(self, |v| { v.scalar_at(index) })
70 }
71
72 fn slice(&self, range: impl RangeBounds<usize> + Clone + Debug) -> Self {
73 match_each_dvector!(self, |v| { DecimalVector::from(v.slice(range)) })
74 }
75
76 fn try_into_mut(self) -> Result<DecimalVectorMut, Self> {
77 match_each_dvector!(self, |v| {
78 v.try_into_mut()
79 .map(DecimalVectorMut::from)
80 .map_err(Self::from)
81 })
82 }
83
84 fn into_mut(self) -> DecimalVectorMut {
85 match_each_dvector!(self, |v| { DecimalVectorMut::from(v.into_mut()) })
86 }
87}
88
89impl DecimalTypeDowncast for DecimalVector {
90 type Output<T: NativeDecimalType> = DVector<T>;
91
92 fn into_i8(self) -> Self::Output<i8> {
93 if let Self::D8(vec) = self {
94 return vec;
95 }
96 vortex_panic!("DecimalVector is not of type D8");
97 }
98
99 fn into_i16(self) -> Self::Output<i16> {
100 if let Self::D16(vec) = self {
101 return vec;
102 }
103 vortex_panic!("DecimalVector is not of type D16");
104 }
105
106 fn into_i32(self) -> Self::Output<i32> {
107 if let Self::D32(vec) = self {
108 return vec;
109 }
110 vortex_panic!("DecimalVector is not of type D32");
111 }
112
113 fn into_i64(self) -> Self::Output<i64> {
114 if let Self::D64(vec) = self {
115 return vec;
116 }
117 vortex_panic!("DecimalVector is not of type D64");
118 }
119
120 fn into_i128(self) -> Self::Output<i128> {
121 if let Self::D128(vec) = self {
122 return vec;
123 }
124 vortex_panic!("DecimalVector is not of type D128");
125 }
126
127 fn into_i256(self) -> Self::Output<i256> {
128 if let Self::D256(vec) = self {
129 return vec;
130 }
131 vortex_panic!("DecimalVector is not of type D256");
132 }
133}
134
135impl DecimalTypeUpcast for DecimalVector {
136 type Input<T: NativeDecimalType> = DVector<T>;
137
138 fn from_i8(input: Self::Input<i8>) -> Self {
139 Self::D8(input)
140 }
141
142 fn from_i16(input: Self::Input<i16>) -> Self {
143 Self::D16(input)
144 }
145
146 fn from_i32(input: Self::Input<i32>) -> Self {
147 Self::D32(input)
148 }
149
150 fn from_i64(input: Self::Input<i64>) -> Self {
151 Self::D64(input)
152 }
153
154 fn from_i128(input: Self::Input<i128>) -> Self {
155 Self::D128(input)
156 }
157
158 fn from_i256(input: Self::Input<i256>) -> Self {
159 Self::D256(input)
160 }
161}