vortex_vector/primitive/
vector.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Definition and implementation of [`PrimitiveVector`].
5
6use std::fmt::Debug;
7use std::ops::RangeBounds;
8
9use vortex_dtype::NativePType;
10use vortex_dtype::PType;
11use vortex_dtype::PTypeDowncast;
12use vortex_dtype::PTypeUpcast;
13use vortex_dtype::half::f16;
14use vortex_error::vortex_panic;
15use vortex_mask::Mask;
16
17use crate::VectorOps;
18use crate::match_each_pvector;
19use crate::primitive::PVector;
20use crate::primitive::PrimitiveScalar;
21use crate::primitive::PrimitiveVectorMut;
22
23/// An immutable vector of primitive values.
24///
25/// The mutable equivalent of this type is [`PrimitiveVectorMut`].
26///
27/// `PrimitiveVector` is represented by an enum over all possible [`PVector`] types (which are
28/// templated by the types that implement [`NativePType`]).
29///
30/// See the documentation for [`PVector`] for more information.
31#[derive(Debug, Clone)]
32pub enum PrimitiveVector {
33    /// U8
34    U8(PVector<u8>),
35    /// U16
36    U16(PVector<u16>),
37    /// U32
38    U32(PVector<u32>),
39    /// U64
40    U64(PVector<u64>),
41    /// I8
42    I8(PVector<i8>),
43    /// I16
44    I16(PVector<i16>),
45    /// I32
46    I32(PVector<i32>),
47    /// I64
48    I64(PVector<i64>),
49    /// F16
50    F16(PVector<f16>),
51    /// F32
52    F32(PVector<f32>),
53    /// F64
54    F64(PVector<f64>),
55}
56
57impl PrimitiveVector {
58    /// Returns the [`PType`] of this [`PrimitiveVector`].
59    pub fn ptype(&self) -> PType {
60        match self {
61            Self::U8(_) => PType::U8,
62            Self::U16(_) => PType::U16,
63            Self::U32(_) => PType::U32,
64            Self::U64(_) => PType::U64,
65            Self::I8(_) => PType::I8,
66            Self::I16(_) => PType::I16,
67            Self::I32(_) => PType::I32,
68            Self::I64(_) => PType::I64,
69            Self::F16(_) => PType::F16,
70            Self::F32(_) => PType::F32,
71            Self::F64(_) => PType::F64,
72        }
73    }
74}
75
76impl VectorOps for PrimitiveVector {
77    type Mutable = PrimitiveVectorMut;
78    type Scalar = PrimitiveScalar;
79
80    fn len(&self) -> usize {
81        match_each_pvector!(self, |v| { v.len() })
82    }
83
84    fn validity(&self) -> &Mask {
85        match_each_pvector!(self, |v| { v.validity() })
86    }
87
88    fn mask_validity(&mut self, mask: &Mask) {
89        match_each_pvector!(self, |v| { v.mask_validity(mask) })
90    }
91
92    fn scalar_at(&self, index: usize) -> PrimitiveScalar {
93        match_each_pvector!(self, |v| { v.scalar_at(index).into() })
94    }
95
96    fn slice(&self, range: impl RangeBounds<usize> + Clone + Debug) -> Self {
97        match_each_pvector!(self, |v| { v.slice(range).into() })
98    }
99
100    fn clear(&mut self) {
101        match_each_pvector!(self, |v| { v.clear() })
102    }
103
104    fn try_into_mut(self) -> Result<PrimitiveVectorMut, Self> {
105        match_each_pvector!(self, |v| {
106            v.try_into_mut()
107                .map(PrimitiveVectorMut::from)
108                .map_err(Self::from)
109        })
110    }
111
112    fn into_mut(self) -> PrimitiveVectorMut {
113        match_each_pvector!(self, |v| { v.into_mut().into() })
114    }
115}
116
117impl PTypeUpcast for PrimitiveVector {
118    type Input<T: NativePType> = PVector<T>;
119
120    fn from_u8(input: Self::Input<u8>) -> Self {
121        Self::U8(input)
122    }
123
124    fn from_u16(input: Self::Input<u16>) -> Self {
125        Self::U16(input)
126    }
127
128    fn from_u32(input: Self::Input<u32>) -> Self {
129        Self::U32(input)
130    }
131
132    fn from_u64(input: Self::Input<u64>) -> Self {
133        Self::U64(input)
134    }
135
136    fn from_i8(input: Self::Input<i8>) -> Self {
137        Self::I8(input)
138    }
139
140    fn from_i16(input: Self::Input<i16>) -> Self {
141        Self::I16(input)
142    }
143
144    fn from_i32(input: Self::Input<i32>) -> Self {
145        Self::I32(input)
146    }
147
148    fn from_i64(input: Self::Input<i64>) -> Self {
149        Self::I64(input)
150    }
151
152    fn from_f16(input: Self::Input<f16>) -> Self {
153        Self::F16(input)
154    }
155
156    fn from_f32(input: Self::Input<f32>) -> Self {
157        Self::F32(input)
158    }
159
160    fn from_f64(input: Self::Input<f64>) -> Self {
161        Self::F64(input)
162    }
163}
164
165impl PTypeDowncast for PrimitiveVector {
166    type Output<T: NativePType> = PVector<T>;
167
168    fn into_u8(self) -> Self::Output<u8> {
169        if let Self::U8(v) = self {
170            return v;
171        }
172        vortex_panic!("Expected PrimitiveVector::U8, got {self:?}");
173    }
174
175    fn into_u16(self) -> Self::Output<u16> {
176        if let Self::U16(v) = self {
177            return v;
178        }
179        vortex_panic!("Expected PrimitiveVector::U16, got {self:?}");
180    }
181
182    fn into_u32(self) -> Self::Output<u32> {
183        if let Self::U32(v) = self {
184            return v;
185        }
186        vortex_panic!("Expected PrimitiveVector::U32, got {self:?}");
187    }
188
189    fn into_u64(self) -> Self::Output<u64> {
190        if let Self::U64(v) = self {
191            return v;
192        }
193        vortex_panic!("Expected PrimitiveVector::U64, got {self:?}");
194    }
195
196    fn into_i8(self) -> Self::Output<i8> {
197        if let Self::I8(v) = self {
198            return v;
199        }
200        vortex_panic!("Expected PrimitiveVector::I8, got {self:?}");
201    }
202
203    fn into_i16(self) -> Self::Output<i16> {
204        if let Self::I16(v) = self {
205            return v;
206        }
207        vortex_panic!("Expected PrimitiveVector::I16, got {self:?}");
208    }
209
210    fn into_i32(self) -> Self::Output<i32> {
211        if let Self::I32(v) = self {
212            return v;
213        }
214        vortex_panic!("Expected PrimitiveVector::I32, got {self:?}");
215    }
216
217    fn into_i64(self) -> Self::Output<i64> {
218        if let Self::I64(v) = self {
219            return v;
220        }
221        vortex_panic!("Expected PrimitiveVector::I64, got {self:?}");
222    }
223
224    fn into_f16(self) -> Self::Output<f16> {
225        if let Self::F16(v) = self {
226            return v;
227        }
228        vortex_panic!("Expected PrimitiveVector::F16, got {self:?}");
229    }
230
231    fn into_f32(self) -> Self::Output<f32> {
232        if let Self::F32(v) = self {
233            return v;
234        }
235        vortex_panic!("Expected PrimitiveVector::F32, got {self:?}");
236    }
237
238    fn into_f64(self) -> Self::Output<f64> {
239        if let Self::F64(v) = self {
240            return v;
241        }
242        vortex_panic!("Expected PrimitiveVector::F64, got {self:?}");
243    }
244}
245
246impl<'a> PTypeDowncast for &'a PrimitiveVector {
247    type Output<T: NativePType> = &'a PVector<T>;
248
249    fn into_u8(self) -> Self::Output<u8> {
250        if let PrimitiveVector::U8(v) = self {
251            return v;
252        }
253        vortex_panic!("Expected PrimitiveVector::U8, got {self:?}");
254    }
255
256    fn into_u16(self) -> Self::Output<u16> {
257        if let PrimitiveVector::U16(v) = self {
258            return v;
259        }
260        vortex_panic!("Expected PrimitiveVector::U16, got {self:?}");
261    }
262
263    fn into_u32(self) -> Self::Output<u32> {
264        if let PrimitiveVector::U32(v) = self {
265            return v;
266        }
267        vortex_panic!("Expected PrimitiveVector::U32, got {self:?}");
268    }
269
270    fn into_u64(self) -> Self::Output<u64> {
271        if let PrimitiveVector::U64(v) = self {
272            return v;
273        }
274        vortex_panic!("Expected PrimitiveVector::U64, got {self:?}");
275    }
276
277    fn into_i8(self) -> Self::Output<i8> {
278        if let PrimitiveVector::I8(v) = self {
279            return v;
280        }
281        vortex_panic!("Expected PrimitiveVector::I8, got {self:?}");
282    }
283
284    fn into_i16(self) -> Self::Output<i16> {
285        if let PrimitiveVector::I16(v) = self {
286            return v;
287        }
288        vortex_panic!("Expected PrimitiveVector::I16, got {self:?}");
289    }
290
291    fn into_i32(self) -> Self::Output<i32> {
292        if let PrimitiveVector::I32(v) = self {
293            return v;
294        }
295        vortex_panic!("Expected PrimitiveVector::I32, got {self:?}");
296    }
297
298    fn into_i64(self) -> Self::Output<i64> {
299        if let PrimitiveVector::I64(v) = self {
300            return v;
301        }
302        vortex_panic!("Expected PrimitiveVector::I64, got {self:?}");
303    }
304
305    fn into_f16(self) -> Self::Output<f16> {
306        if let PrimitiveVector::F16(v) = self {
307            return v;
308        }
309        vortex_panic!("Expected PrimitiveVector::F16, got {self:?}");
310    }
311
312    fn into_f32(self) -> Self::Output<f32> {
313        if let PrimitiveVector::F32(v) = self {
314            return v;
315        }
316        vortex_panic!("Expected PrimitiveVector::F32, got {self:?}");
317    }
318
319    fn into_f64(self) -> Self::Output<f64> {
320        if let PrimitiveVector::F64(v) = self {
321            return v;
322        }
323        vortex_panic!("Expected PrimitiveVector::F64, got {self:?}");
324    }
325}
326
327impl<'a> PTypeDowncast for &'a mut PrimitiveVector {
328    type Output<T: NativePType> = &'a mut PVector<T>;
329
330    fn into_u8(self) -> Self::Output<u8> {
331        if let PrimitiveVector::U8(v) = self {
332            return v;
333        }
334        vortex_panic!("Expected PrimitiveVector::U8, got {self:?}");
335    }
336
337    fn into_u16(self) -> Self::Output<u16> {
338        if let PrimitiveVector::U16(v) = self {
339            return v;
340        }
341        vortex_panic!("Expected PrimitiveVector::U16, got {self:?}");
342    }
343
344    fn into_u32(self) -> Self::Output<u32> {
345        if let PrimitiveVector::U32(v) = self {
346            return v;
347        }
348        vortex_panic!("Expected PrimitiveVector::U32, got {self:?}");
349    }
350
351    fn into_u64(self) -> Self::Output<u64> {
352        if let PrimitiveVector::U64(v) = self {
353            return v;
354        }
355        vortex_panic!("Expected PrimitiveVector::U64, got {self:?}");
356    }
357
358    fn into_i8(self) -> Self::Output<i8> {
359        if let PrimitiveVector::I8(v) = self {
360            return v;
361        }
362        vortex_panic!("Expected PrimitiveVector::I8, got {self:?}");
363    }
364
365    fn into_i16(self) -> Self::Output<i16> {
366        if let PrimitiveVector::I16(v) = self {
367            return v;
368        }
369        vortex_panic!("Expected PrimitiveVector::I16, got {self:?}");
370    }
371
372    fn into_i32(self) -> Self::Output<i32> {
373        if let PrimitiveVector::I32(v) = self {
374            return v;
375        }
376        vortex_panic!("Expected PrimitiveVector::I32, got {self:?}");
377    }
378
379    fn into_i64(self) -> Self::Output<i64> {
380        if let PrimitiveVector::I64(v) = self {
381            return v;
382        }
383        vortex_panic!("Expected PrimitiveVector::I64, got {self:?}");
384    }
385
386    fn into_f16(self) -> Self::Output<f16> {
387        if let PrimitiveVector::F16(v) = self {
388            return v;
389        }
390        vortex_panic!("Expected PrimitiveVector::F16, got {self:?}");
391    }
392
393    fn into_f32(self) -> Self::Output<f32> {
394        if let PrimitiveVector::F32(v) = self {
395            return v;
396        }
397        vortex_panic!("Expected PrimitiveVector::F32, got {self:?}");
398    }
399
400    fn into_f64(self) -> Self::Output<f64> {
401        if let PrimitiveVector::F64(v) = self {
402            return v;
403        }
404        vortex_panic!("Expected PrimitiveVector::F64, got {self:?}");
405    }
406}