vortex_vector/decimal/
vector.rs

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