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, 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/// An enum over all supported decimal mutable vector types.
17#[derive(Clone, Debug)]
18pub enum DecimalVector {
19    /// A decimal vector with 8-bit integer representation.
20    D8(DVector<i8>),
21    /// A decimal vector with 16-bit integer representation.
22    D16(DVector<i16>),
23    /// A decimal vector with 32-bit integer representation.
24    D32(DVector<i32>),
25    /// A decimal vector with 64-bit integer representation.
26    D64(DVector<i64>),
27    /// A decimal vector with 128-bit integer representation.
28    D128(DVector<i128>),
29    /// A decimal vector with 256-bit integer representation.
30    D256(DVector<i256>),
31}
32
33impl DecimalVector {
34    /// Returns the precision of the decimal vector.
35    pub fn precision(&self) -> u8 {
36        match_each_dvector!(self, |v| { v.precision() })
37    }
38
39    /// Returns the scale of the decimal vector.
40    pub fn scale(&self) -> i8 {
41        match_each_dvector!(self, |v| { v.scale() })
42    }
43
44    /// Returns the physical [`DecimalType`] of the decimal vector.
45    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}