vortex_vector/decimal/
vector_mut.rs1use vortex_dtype::DecimalDType;
7use vortex_dtype::DecimalType;
8use vortex_dtype::DecimalTypeDowncast;
9use vortex_dtype::DecimalTypeUpcast;
10use vortex_dtype::NativeDecimalType;
11use vortex_dtype::PrecisionScale;
12use vortex_dtype::i256;
13use vortex_dtype::match_each_decimal_value_type;
14use vortex_error::vortex_panic;
15use vortex_mask::MaskMut;
16
17use crate::VectorMutOps;
18use crate::decimal::DVectorMut;
19use crate::decimal::DecimalScalar;
20use crate::decimal::DecimalVector;
21use crate::match_each_dvector_mut;
22
23#[derive(Clone, Debug)]
25pub enum DecimalVectorMut {
26 D8(DVectorMut<i8>),
28 D16(DVectorMut<i16>),
30 D32(DVectorMut<i32>),
32 D64(DVectorMut<i64>),
34 D128(DVectorMut<i128>),
36 D256(DVectorMut<i256>),
38}
39
40impl DecimalVectorMut {
41 pub fn decimal_type(&self) -> DecimalType {
43 match self {
44 Self::D8(_) => DecimalType::I8,
45 Self::D16(_) => DecimalType::I16,
46 Self::D32(_) => DecimalType::I32,
47 Self::D64(_) => DecimalType::I64,
48 Self::D128(_) => DecimalType::I128,
49 Self::D256(_) => DecimalType::I256,
50 }
51 }
52
53 pub fn with_capacity(decimal_dtype: &DecimalDType, capacity: usize) -> Self {
55 let decimal_type = DecimalType::smallest_decimal_value_type(decimal_dtype);
56 match_each_decimal_value_type!(decimal_type, |D| {
57 let ps = PrecisionScale::<D>::new(decimal_dtype.precision(), decimal_dtype.scale());
58 DVectorMut::<D>::with_capacity(ps, capacity).into()
59 })
60 }
61}
62
63impl VectorMutOps for DecimalVectorMut {
64 type Immutable = DecimalVector;
65
66 fn len(&self) -> usize {
67 match_each_dvector_mut!(self, |d| { d.len() })
68 }
69
70 fn validity(&self) -> &MaskMut {
71 match_each_dvector_mut!(self, |d| { d.validity() })
72 }
73
74 fn capacity(&self) -> usize {
75 match_each_dvector_mut!(self, |d| { d.capacity() })
76 }
77
78 fn reserve(&mut self, additional: usize) {
79 match_each_dvector_mut!(self, |d| { d.reserve(additional) })
80 }
81
82 fn clear(&mut self) {
83 match_each_dvector_mut!(self, |d| { d.clear() })
84 }
85
86 fn truncate(&mut self, len: usize) {
87 match_each_dvector_mut!(self, |d| { d.truncate(len) })
88 }
89
90 fn extend_from_vector(&mut self, other: &DecimalVector) {
91 match (self, other) {
92 (Self::D8(s), DecimalVector::D8(o)) => s.extend_from_vector(o),
93 (Self::D16(s), DecimalVector::D16(o)) => s.extend_from_vector(o),
94 (Self::D32(s), DecimalVector::D32(o)) => s.extend_from_vector(o),
95 (Self::D64(s), DecimalVector::D64(o)) => s.extend_from_vector(o),
96 (Self::D128(s), DecimalVector::D128(o)) => s.extend_from_vector(o),
97 (Self::D256(s), DecimalVector::D256(o)) => s.extend_from_vector(o),
98 _ => vortex_panic!("Mismatched decimal vector types in extend_from_vector"),
99 }
100 }
101
102 fn append_nulls(&mut self, n: usize) {
103 match_each_dvector_mut!(self, |d| { d.append_nulls(n) })
104 }
105
106 fn append_zeros(&mut self, n: usize) {
107 match_each_dvector_mut!(self, |d| { d.append_zeros(n) })
108 }
109
110 #[expect(
111 clippy::many_single_char_names,
112 reason = "single-letter names s/o are clear for matching variants"
113 )]
114 fn append_scalars(&mut self, scalar: &DecimalScalar, n: usize) {
115 match (self, scalar) {
116 (Self::D8(s), DecimalScalar::D8(o)) => s.append_scalars(o, n),
117 (Self::D16(s), DecimalScalar::D16(o)) => s.append_scalars(o, n),
118 (Self::D32(s), DecimalScalar::D32(o)) => s.append_scalars(o, n),
119 (Self::D64(s), DecimalScalar::D64(o)) => s.append_scalars(o, n),
120 (Self::D128(s), DecimalScalar::D128(o)) => s.append_scalars(o, n),
121 (Self::D256(s), DecimalScalar::D256(o)) => s.append_scalars(o, n),
122 _ => vortex_panic!("Mismatched decimal vector and scalar types in append_scalar"),
123 }
124 }
125
126 fn freeze(self) -> DecimalVector {
127 match_each_dvector_mut!(self, |d| { d.freeze().into() })
128 }
129
130 fn split_off(&mut self, at: usize) -> Self {
131 match_each_dvector_mut!(self, |d| { d.split_off(at).into() })
132 }
133
134 fn unsplit(&mut self, other: Self) {
135 match (self, other) {
136 (Self::D8(s), Self::D8(o)) => s.unsplit(o),
137 (Self::D16(s), Self::D16(o)) => s.unsplit(o),
138 (Self::D32(s), Self::D32(o)) => s.unsplit(o),
139 (Self::D64(s), Self::D64(o)) => s.unsplit(o),
140 (Self::D128(s), Self::D128(o)) => s.unsplit(o),
141 (Self::D256(s), Self::D256(o)) => s.unsplit(o),
142 _ => vortex_panic!("Mismatched decimal vector types in unsplit"),
143 }
144 }
145}
146
147impl DecimalTypeDowncast for DecimalVectorMut {
148 type Output<T: NativeDecimalType> = DVectorMut<T>;
149
150 fn into_i8(self) -> Self::Output<i8> {
151 if let Self::D8(vec) = self {
152 return vec;
153 }
154 vortex_panic!("DecimalVectorMut is not of type D8");
155 }
156
157 fn into_i16(self) -> Self::Output<i16> {
158 if let Self::D16(vec) = self {
159 return vec;
160 }
161 vortex_panic!("DecimalVectorMut is not of type D16");
162 }
163
164 fn into_i32(self) -> Self::Output<i32> {
165 if let Self::D32(vec) = self {
166 return vec;
167 }
168 vortex_panic!("DecimalVectorMut is not of type D32");
169 }
170
171 fn into_i64(self) -> Self::Output<i64> {
172 if let Self::D64(vec) = self {
173 return vec;
174 }
175 vortex_panic!("DecimalVectorMut is not of type D64");
176 }
177
178 fn into_i128(self) -> Self::Output<i128> {
179 if let Self::D128(vec) = self {
180 return vec;
181 }
182 vortex_panic!("DecimalVectorMut is not of type D128");
183 }
184
185 fn into_i256(self) -> Self::Output<i256> {
186 if let Self::D256(vec) = self {
187 return vec;
188 }
189 vortex_panic!("DecimalVectorMut is not of type D256");
190 }
191}
192
193impl DecimalTypeUpcast for DecimalVectorMut {
194 type Input<T: NativeDecimalType> = DVectorMut<T>;
195
196 fn from_i8(input: Self::Input<i8>) -> Self {
197 Self::D8(input)
198 }
199
200 fn from_i16(input: Self::Input<i16>) -> Self {
201 Self::D16(input)
202 }
203
204 fn from_i32(input: Self::Input<i32>) -> Self {
205 Self::D32(input)
206 }
207
208 fn from_i64(input: Self::Input<i64>) -> Self {
209 Self::D64(input)
210 }
211
212 fn from_i128(input: Self::Input<i128>) -> Self {
213 Self::D128(input)
214 }
215
216 fn from_i256(input: Self::Input<i256>) -> Self {
217 Self::D256(input)
218 }
219}