vortex_vector/decimal/
vector_mut.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Definition and implementation of [`DecimalVectorMut`].
5
6use vortex_dtype::{
7    DecimalDType, DecimalType, DecimalTypeDowncast, DecimalTypeUpcast, NativeDecimalType,
8    PrecisionScale, i256, match_each_decimal_value_type,
9};
10use vortex_error::vortex_panic;
11use vortex_mask::MaskMut;
12
13use crate::decimal::{DVectorMut, DecimalVector};
14use crate::{VectorMutOps, match_each_dvector_mut};
15
16/// An enum over all supported decimal mutable vector types.
17#[derive(Clone, Debug)]
18pub enum DecimalVectorMut {
19    /// A decimal vector with 8-bit integer representation.
20    D8(DVectorMut<i8>),
21    /// A decimal vector with 16-bit integer representation.
22    D16(DVectorMut<i16>),
23    /// A decimal vector with 32-bit integer representation.
24    D32(DVectorMut<i32>),
25    /// A decimal vector with 64-bit integer representation.
26    D64(DVectorMut<i64>),
27    /// A decimal vector with 128-bit integer representation.
28    D128(DVectorMut<i128>),
29    /// A decimal vector with 256-bit integer representation.
30    D256(DVectorMut<i256>),
31}
32
33impl DecimalVectorMut {
34    /// Returns the [`DecimalType`] of the decimal vector.
35    pub fn decimal_type(&self) -> DecimalType {
36        match self {
37            Self::D8(_) => DecimalType::I8,
38            Self::D16(_) => DecimalType::I16,
39            Self::D32(_) => DecimalType::I32,
40            Self::D64(_) => DecimalType::I64,
41            Self::D128(_) => DecimalType::I128,
42            Self::D256(_) => DecimalType::I256,
43        }
44    }
45
46    /// Create a new mutable decimal vector with the given primitive type and capacity.
47    pub fn with_capacity(decimal_dtype: &DecimalDType, capacity: usize) -> Self {
48        let decimal_type = DecimalType::smallest_decimal_value_type(decimal_dtype);
49        match_each_decimal_value_type!(decimal_type, |D| {
50            let ps = PrecisionScale::<D>::new(decimal_dtype.precision(), decimal_dtype.scale());
51            DVectorMut::<D>::with_capacity(ps, capacity).into()
52        })
53    }
54}
55
56impl VectorMutOps for DecimalVectorMut {
57    type Immutable = DecimalVector;
58
59    fn len(&self) -> usize {
60        match_each_dvector_mut!(self, |d| { d.len() })
61    }
62
63    fn validity(&self) -> &MaskMut {
64        match_each_dvector_mut!(self, |d| { d.validity() })
65    }
66
67    fn capacity(&self) -> usize {
68        match_each_dvector_mut!(self, |d| { d.capacity() })
69    }
70
71    fn reserve(&mut self, additional: usize) {
72        match_each_dvector_mut!(self, |d| { d.reserve(additional) })
73    }
74
75    fn clear(&mut self) {
76        match_each_dvector_mut!(self, |d| { d.clear() })
77    }
78
79    fn truncate(&mut self, len: usize) {
80        match_each_dvector_mut!(self, |d| { d.truncate(len) })
81    }
82
83    fn extend_from_vector(&mut self, other: &DecimalVector) {
84        match (self, other) {
85            (Self::D8(s), DecimalVector::D8(o)) => s.extend_from_vector(o),
86            (Self::D16(s), DecimalVector::D16(o)) => s.extend_from_vector(o),
87            (Self::D32(s), DecimalVector::D32(o)) => s.extend_from_vector(o),
88            (Self::D64(s), DecimalVector::D64(o)) => s.extend_from_vector(o),
89            (Self::D128(s), DecimalVector::D128(o)) => s.extend_from_vector(o),
90            (Self::D256(s), DecimalVector::D256(o)) => s.extend_from_vector(o),
91            _ => vortex_panic!("Mismatched decimal vector types in extend_from_vector"),
92        }
93    }
94
95    fn append_nulls(&mut self, n: usize) {
96        match_each_dvector_mut!(self, |d| { d.append_nulls(n) })
97    }
98
99    fn freeze(self) -> DecimalVector {
100        match_each_dvector_mut!(self, |d| { d.freeze().into() })
101    }
102
103    fn split_off(&mut self, at: usize) -> Self {
104        match_each_dvector_mut!(self, |d| { d.split_off(at).into() })
105    }
106
107    fn unsplit(&mut self, other: Self) {
108        match (self, other) {
109            (Self::D8(s), Self::D8(o)) => s.unsplit(o),
110            (Self::D16(s), Self::D16(o)) => s.unsplit(o),
111            (Self::D32(s), Self::D32(o)) => s.unsplit(o),
112            (Self::D64(s), Self::D64(o)) => s.unsplit(o),
113            (Self::D128(s), Self::D128(o)) => s.unsplit(o),
114            (Self::D256(s), Self::D256(o)) => s.unsplit(o),
115            _ => vortex_panic!("Mismatched decimal vector types in unsplit"),
116        }
117    }
118}
119
120impl DecimalTypeDowncast for DecimalVectorMut {
121    type Output<T: NativeDecimalType> = DVectorMut<T>;
122
123    fn into_i8(self) -> Self::Output<i8> {
124        if let Self::D8(vec) = self {
125            return vec;
126        }
127        vortex_panic!("DecimalVectorMut is not of type D8");
128    }
129
130    fn into_i16(self) -> Self::Output<i16> {
131        if let Self::D16(vec) = self {
132            return vec;
133        }
134        vortex_panic!("DecimalVectorMut is not of type D16");
135    }
136
137    fn into_i32(self) -> Self::Output<i32> {
138        if let Self::D32(vec) = self {
139            return vec;
140        }
141        vortex_panic!("DecimalVectorMut is not of type D32");
142    }
143
144    fn into_i64(self) -> Self::Output<i64> {
145        if let Self::D64(vec) = self {
146            return vec;
147        }
148        vortex_panic!("DecimalVectorMut is not of type D64");
149    }
150
151    fn into_i128(self) -> Self::Output<i128> {
152        if let Self::D128(vec) = self {
153            return vec;
154        }
155        vortex_panic!("DecimalVectorMut is not of type D128");
156    }
157
158    fn into_i256(self) -> Self::Output<i256> {
159        if let Self::D256(vec) = self {
160            return vec;
161        }
162        vortex_panic!("DecimalVectorMut is not of type D256");
163    }
164}
165
166impl DecimalTypeUpcast for DecimalVectorMut {
167    type Input<T: NativeDecimalType> = DVectorMut<T>;
168
169    fn from_i8(input: Self::Input<i8>) -> Self {
170        Self::D8(input)
171    }
172
173    fn from_i16(input: Self::Input<i16>) -> Self {
174        Self::D16(input)
175    }
176
177    fn from_i32(input: Self::Input<i32>) -> Self {
178        Self::D32(input)
179    }
180
181    fn from_i64(input: Self::Input<i64>) -> Self {
182        Self::D64(input)
183    }
184
185    fn from_i128(input: Self::Input<i128>) -> Self {
186        Self::D128(input)
187    }
188
189    fn from_i256(input: Self::Input<i256>) -> Self {
190        Self::D256(input)
191    }
192}