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)]
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 DecimalVector {
41    /// Returns the precision of the decimal vector.
42    pub fn precision(&self) -> u8 {
43        match_each_dvector!(self, |v| { v.precision() })
44    }
45
46    /// Returns the scale of the decimal vector.
47    pub fn scale(&self) -> i8 {
48        match_each_dvector!(self, |v| { v.scale() })
49    }
50
51    /// Returns the physical [`DecimalType`] of the decimal vector.
52    pub fn decimal_type(&self) -> DecimalType {
53        match self {
54            Self::D8(_) => DecimalType::I8,
55            Self::D16(_) => DecimalType::I16,
56            Self::D32(_) => DecimalType::I32,
57            Self::D64(_) => DecimalType::I64,
58            Self::D128(_) => DecimalType::I128,
59            Self::D256(_) => DecimalType::I256,
60        }
61    }
62}
63
64impl VectorOps for DecimalVector {
65    type Mutable = DecimalVectorMut;
66    type Scalar = DecimalScalar;
67
68    fn len(&self) -> usize {
69        match_each_dvector!(self, |v| { v.len() })
70    }
71
72    fn validity(&self) -> &Mask {
73        match_each_dvector!(self, |v| { v.validity() })
74    }
75
76    fn mask_validity(&mut self, mask: &Mask) {
77        match_each_dvector!(self, |v| { v.mask_validity(mask) })
78    }
79
80    fn scalar_at(&self, index: usize) -> DecimalScalar {
81        match_each_dvector!(self, |v| { v.scalar_at(index).into() })
82    }
83
84    fn slice(&self, range: impl RangeBounds<usize> + Clone + Debug) -> Self {
85        match_each_dvector!(self, |v| { DecimalVector::from(v.slice(range)) })
86    }
87
88    fn clear(&mut self) {
89        match_each_dvector!(self, |v| { v.clear() })
90    }
91
92    fn try_into_mut(self) -> Result<DecimalVectorMut, Self> {
93        match_each_dvector!(self, |v| {
94            v.try_into_mut()
95                .map(DecimalVectorMut::from)
96                .map_err(Self::from)
97        })
98    }
99
100    fn into_mut(self) -> DecimalVectorMut {
101        match_each_dvector!(self, |v| { DecimalVectorMut::from(v.into_mut()) })
102    }
103}
104
105impl DecimalTypeDowncast for DecimalVector {
106    type Output<T: NativeDecimalType> = DVector<T>;
107
108    fn into_i8(self) -> Self::Output<i8> {
109        if let Self::D8(vec) = self {
110            return vec;
111        }
112        vortex_panic!("DecimalVector is not of type D8");
113    }
114
115    fn into_i16(self) -> Self::Output<i16> {
116        if let Self::D16(vec) = self {
117            return vec;
118        }
119        vortex_panic!("DecimalVector is not of type D16");
120    }
121
122    fn into_i32(self) -> Self::Output<i32> {
123        if let Self::D32(vec) = self {
124            return vec;
125        }
126        vortex_panic!("DecimalVector is not of type D32");
127    }
128
129    fn into_i64(self) -> Self::Output<i64> {
130        if let Self::D64(vec) = self {
131            return vec;
132        }
133        vortex_panic!("DecimalVector is not of type D64");
134    }
135
136    fn into_i128(self) -> Self::Output<i128> {
137        if let Self::D128(vec) = self {
138            return vec;
139        }
140        vortex_panic!("DecimalVector is not of type D128");
141    }
142
143    fn into_i256(self) -> Self::Output<i256> {
144        if let Self::D256(vec) = self {
145            return vec;
146        }
147        vortex_panic!("DecimalVector is not of type D256");
148    }
149}
150
151impl DecimalTypeUpcast for DecimalVector {
152    type Input<T: NativeDecimalType> = DVector<T>;
153
154    fn from_i8(input: Self::Input<i8>) -> Self {
155        Self::D8(input)
156    }
157
158    fn from_i16(input: Self::Input<i16>) -> Self {
159        Self::D16(input)
160    }
161
162    fn from_i32(input: Self::Input<i32>) -> Self {
163        Self::D32(input)
164    }
165
166    fn from_i64(input: Self::Input<i64>) -> Self {
167        Self::D64(input)
168    }
169
170    fn from_i128(input: Self::Input<i128>) -> Self {
171        Self::D128(input)
172    }
173
174    fn from_i256(input: Self::Input<i256>) -> Self {
175        Self::D256(input)
176    }
177}