vortex_vector/decimal/
generic.rs1use std::fmt::Debug;
7use std::ops::BitAnd;
8use std::ops::RangeBounds;
9
10use vortex_buffer::Buffer;
11use vortex_dtype::NativeDecimalType;
12use vortex_dtype::PrecisionScale;
13use vortex_error::VortexExpect;
14use vortex_error::VortexResult;
15use vortex_error::vortex_bail;
16use vortex_mask::Mask;
17
18use crate::VectorOps;
19use crate::decimal::DScalar;
20use crate::decimal::DVectorMut;
21
22#[derive(Debug, Clone)]
30pub struct DVector<D> {
31 pub(super) ps: PrecisionScale<D>,
33 pub(super) elements: Buffer<D>,
35 pub(super) validity: Mask,
37}
38
39impl<D: NativeDecimalType + PartialEq> PartialEq for DVector<D> {
40 fn eq(&self, other: &Self) -> bool {
41 if self.elements.len() != other.elements.len() {
42 return false;
43 }
44 if self.ps != other.ps {
46 return false;
47 }
48 if self.validity != other.validity {
50 return false;
51 }
52 self.elements
54 .iter()
55 .zip(other.elements.iter())
56 .enumerate()
57 .all(|(i, (a, b))| !self.validity.value(i) | (a == b))
58 }
59}
60
61impl<D: NativeDecimalType + Eq> Eq for DVector<D> {}
62
63impl<D: NativeDecimalType> DVector<D> {
64 pub fn new(ps: PrecisionScale<D>, elements: Buffer<D>, validity: Mask) -> Self {
74 Self::try_new(ps, elements, validity).vortex_expect("Failed to create `DVector`")
75 }
76
77 pub fn try_new(
87 ps: PrecisionScale<D>,
88 elements: Buffer<D>,
89 validity: Mask,
90 ) -> VortexResult<Self> {
91 if elements.len() != validity.len() {
92 vortex_bail!(
93 "Elements length {} does not match validity length {}",
94 elements.len(),
95 validity.len()
96 );
97 }
98
99 for (element, is_valid) in elements.iter().zip(validity.to_bit_buffer().iter()) {
103 if is_valid && !ps.is_valid(*element) {
104 vortex_bail!(
105 "One or more elements (e.g. {element}) are out of bounds for precision {} and scale {}",
106 ps.precision(),
107 ps.scale(),
108 );
109 }
110 }
111
112 Ok(Self {
113 ps,
114 elements,
115 validity,
116 })
117 }
118
119 pub unsafe fn new_unchecked(
129 ps: PrecisionScale<D>,
130 elements: Buffer<D>,
131 validity: Mask,
132 ) -> Self {
133 if cfg!(debug_assertions) {
134 Self::try_new(ps, elements, validity).vortex_expect("Failed to create `DVector`")
135 } else {
136 Self {
137 ps,
138 elements,
139 validity,
140 }
141 }
142 }
143
144 pub fn into_parts(self) -> (PrecisionScale<D>, Buffer<D>, Mask) {
147 (self.ps, self.elements, self.validity)
148 }
149
150 pub fn precision_scale(&self) -> PrecisionScale<D> {
152 self.ps
153 }
154
155 pub fn precision(&self) -> u8 {
157 self.ps.precision()
158 }
159
160 pub fn scale(&self) -> i8 {
162 self.ps.scale()
163 }
164
165 pub fn elements(&self) -> &Buffer<D> {
167 &self.elements
168 }
169
170 pub fn get(&self, index: usize) -> Option<&D> {
183 self.validity.value(index).then(|| &self.elements[index])
184 }
185}
186
187impl<D: NativeDecimalType> AsRef<[D]> for DVector<D> {
188 fn as_ref(&self) -> &[D] {
189 &self.elements
190 }
191}
192
193impl<D: NativeDecimalType> VectorOps for DVector<D> {
194 type Mutable = DVectorMut<D>;
195 type Scalar = DScalar<D>;
196
197 fn len(&self) -> usize {
198 self.elements.len()
199 }
200
201 fn validity(&self) -> &Mask {
202 &self.validity
203 }
204
205 fn mask_validity(&mut self, mask: &Mask) {
206 self.validity = self.validity.bitand(mask);
207 }
208
209 fn scalar_at(&self, index: usize) -> DScalar<D> {
210 assert!(index < self.len());
211
212 let is_valid = self.validity.value(index);
213 let value = is_valid.then(|| self.elements[index]);
214
215 unsafe { DScalar::<D>::new_unchecked(self.ps, value) }
217 }
218
219 fn slice(&self, range: impl RangeBounds<usize> + Clone + Debug) -> Self {
220 let elements = self.elements.slice(range.clone());
221 let validity = self.validity.slice(range);
222 Self {
223 ps: self.ps,
224 elements,
225 validity,
226 }
227 }
228
229 fn clear(&mut self) {
230 self.elements.clear();
231 self.validity.clear();
232 }
233
234 fn try_into_mut(self) -> Result<DVectorMut<D>, Self> {
235 let elements = match self.elements.try_into_mut() {
236 Ok(elements) => elements,
237 Err(elements) => {
238 return Err(Self {
239 ps: self.ps,
240 elements,
241 validity: self.validity,
242 });
243 }
244 };
245
246 match self.validity.try_into_mut() {
247 Ok(validity_mut) => Ok(DVectorMut {
248 ps: self.ps,
249 elements,
250 validity: validity_mut,
251 }),
252 Err(validity) => Err(Self {
253 ps: self.ps,
254 elements: elements.freeze(),
255 validity,
256 }),
257 }
258 }
259
260 fn into_mut(self) -> DVectorMut<D> {
261 DVectorMut {
262 ps: self.ps,
263 elements: self.elements.into_mut(),
264 validity: self.validity.into_mut(),
265 }
266 }
267}