vortex_vector/
vector.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Definition of the [`Vector`] type, which represents immutable and fully decompressed (canonical)
5//! array data.
6//!
7//! [`Vector`] can be transformed into the [`VectorMut`] type if it is owned.
8
9use std::fmt::Debug;
10use std::ops::RangeBounds;
11
12use vortex_error::vortex_panic;
13
14use crate::binaryview::{BinaryVector, StringVector};
15use crate::bool::BoolVector;
16use crate::decimal::DecimalVector;
17use crate::fixed_size_list::FixedSizeListVector;
18use crate::listview::ListViewVector;
19use crate::null::NullVector;
20use crate::primitive::PrimitiveVector;
21use crate::struct_::StructVector;
22use crate::{Scalar, VectorMut, VectorOps, match_each_vector};
23
24/// An enum over all kinds of immutable vectors, which represent fully decompressed (canonical)
25/// array data.
26///
27/// Most of the behavior of `Vector` is described by the [`VectorOps`] trait. Note that vectors are
28/// **always** considered as nullable, and it is the responsibility of the user to not add any
29/// nullable data to a vector they want to keep as non-nullable.
30///
31/// The mutable equivalent of this type is [`VectorMut`], which implements the
32/// [`VectorMutOps`](crate::VectorMutOps) trait.
33#[derive(Debug, Clone)]
34pub enum Vector {
35    /// Null vectors.
36    Null(NullVector),
37    /// Boolean vectors.
38    Bool(BoolVector),
39    /// Decimal vectors.
40    ///
41    /// Note that [`DecimalVector`] is an enum over the different possible (generic)
42    /// [`DVector<D>`](crate::decimal::DVector)s.
43    ///
44    /// See the [documentation](crate::decimal) for more information.
45    Decimal(DecimalVector),
46    /// Primitive vectors.
47    ///
48    /// Note that [`PrimitiveVector`] is an enum over the different possible (generic)
49    /// [`PVector<T>`](crate::primitive::PVector)s.
50    ///
51    /// See the [documentation](crate::primitive) for more information.
52    Primitive(PrimitiveVector),
53    /// String vectors
54    String(StringVector),
55    /// Binary vectors
56    Binary(BinaryVector),
57    /// Vectors of Lists with variable sizes.
58    List(ListViewVector),
59    /// Vectors of Lists with fixed sizes.
60    FixedSizeList(FixedSizeListVector),
61    /// Vectors of Struct elements.
62    Struct(StructVector),
63}
64
65impl VectorOps for Vector {
66    type Mutable = VectorMut;
67
68    fn len(&self) -> usize {
69        match_each_vector!(self, |v| { v.len() })
70    }
71
72    fn validity(&self) -> &vortex_mask::Mask {
73        match_each_vector!(self, |v| { v.validity() })
74    }
75
76    fn scalar_at(&self, index: usize) -> Scalar {
77        match_each_vector!(self, |v| { v.scalar_at(index) })
78    }
79
80    fn slice(&self, range: impl RangeBounds<usize> + Clone + Debug) -> Self {
81        match_each_vector!(self, |v| { Vector::from(v.slice(range)) })
82    }
83
84    fn try_into_mut(self) -> Result<VectorMut, Self> {
85        match_each_vector!(self, |v| {
86            v.try_into_mut().map(VectorMut::from).map_err(Vector::from)
87        })
88    }
89
90    fn into_mut(self) -> VectorMut {
91        match_each_vector!(self, |v| { VectorMut::from(v.into_mut()) })
92    }
93}
94
95impl Vector {
96    /// Returns a reference to the inner [`NullVector`] if `self` is of that variant.
97    pub fn as_null(&self) -> &NullVector {
98        if let Vector::Null(v) = self {
99            return v;
100        }
101        vortex_panic!("Expected NullVector, got {self:?}");
102    }
103
104    /// Returns a reference to the inner [`BoolVector`] if `self` is of that variant.
105    pub fn as_bool(&self) -> &BoolVector {
106        if let Vector::Bool(v) = self {
107            return v;
108        }
109        vortex_panic!("Expected BoolVector, got {self:?}");
110    }
111
112    /// Returns a reference to the inner [`PrimitiveVector`] if `self` is of that variant.
113    pub fn as_primitive(&self) -> &PrimitiveVector {
114        if let Vector::Primitive(v) = self {
115            return v;
116        }
117        vortex_panic!("Expected PrimitiveVector, got {self:?}");
118    }
119
120    /// Returns a reference to the inner [`StringVector`] if `self` is of that variant.
121    pub fn as_string(&self) -> &StringVector {
122        if let Vector::String(v) = self {
123            return v;
124        }
125        vortex_panic!("Expected StringVector, got {self:?}");
126    }
127
128    /// Returns a reference to the inner [`BinaryVector`] if `self` is of that variant.
129    pub fn as_binary(&self) -> &BinaryVector {
130        if let Vector::Binary(v) = self {
131            return v;
132        }
133        vortex_panic!("Expected BinaryVector, got {self:?}");
134    }
135
136    /// Returns a reference to the inner [`ListViewVector`] if `self` is of that variant.
137    pub fn as_list(&self) -> &ListViewVector {
138        if let Vector::List(v) = self {
139            return v;
140        }
141        vortex_panic!("Expected ListViewVector, got {self:?}");
142    }
143
144    /// Returns a reference to the inner [`FixedSizeListVector`] if `self` is of that variant.
145    pub fn as_fixed_size_list(&self) -> &FixedSizeListVector {
146        if let Vector::FixedSizeList(v) = self {
147            return v;
148        }
149        vortex_panic!("Expected FixedSizeListVector, got {self:?}");
150    }
151
152    /// Returns a reference to the inner [`StructVector`] if `self` is of that variant.
153    pub fn as_struct(&self) -> &StructVector {
154        if let Vector::Struct(v) = self {
155            return v;
156        }
157        vortex_panic!("Expected StructVector, got {self:?}");
158    }
159
160    /// Consumes `self` and returns the inner [`NullVector`] if `self` is of that variant.
161    pub fn into_null(self) -> NullVector {
162        if let Vector::Null(v) = self {
163            return v;
164        }
165        vortex_panic!("Expected NullVector, got {self:?}");
166    }
167
168    /// Consumes `self` and returns the inner [`BoolVector`] if `self` is of that variant.
169    pub fn into_bool(self) -> BoolVector {
170        if let Vector::Bool(v) = self {
171            return v;
172        }
173        vortex_panic!("Expected BoolVector, got {self:?}");
174    }
175
176    /// Consumes `self` and returns the inner [`PrimitiveVector`] if `self` is of that variant.
177    pub fn into_primitive(self) -> PrimitiveVector {
178        if let Vector::Primitive(v) = self {
179            return v;
180        }
181        vortex_panic!("Expected PrimitiveVector, got {self:?}");
182    }
183
184    /// Consumes `self` and returns the inner [`DecimalVector`] if `self` is of that variant.
185    pub fn into_decimal(self) -> DecimalVector {
186        if let Vector::Decimal(v) = self {
187            return v;
188        }
189        vortex_panic!("Expected DecimalVector, got {self:?}");
190    }
191
192    /// Consumes `self` and returns the inner [`StringVector`] if `self` is of that variant.
193    #[allow(clippy::same_name_method)] // Same as VarBinTypeDowncast
194    pub fn into_string(self) -> StringVector {
195        if let Vector::String(v) = self {
196            return v;
197        }
198        vortex_panic!("Expected StringVector, got {self:?}");
199    }
200
201    /// Consumes `self` and returns the inner [`BinaryVector`] if `self` is of that variant.
202    #[allow(clippy::same_name_method)] // Same as VarBinTypeDowncast
203    pub fn into_binary(self) -> BinaryVector {
204        if let Vector::Binary(v) = self {
205            return v;
206        }
207        vortex_panic!("Expected BinaryVector, got {self:?}");
208    }
209
210    /// Consumes `self` and returns the inner [`ListViewVector`] if `self` is of that variant.
211    pub fn into_list(self) -> ListViewVector {
212        if let Vector::List(v) = self {
213            return v;
214        }
215        vortex_panic!("Expected ListViewVector, got {self:?}");
216    }
217
218    /// Consumes `self` and returns the inner [`FixedSizeListVector`] if `self` is of that
219    /// variant.
220    pub fn into_fixed_size_list(self) -> FixedSizeListVector {
221        if let Vector::FixedSizeList(v) = self {
222            return v;
223        }
224        vortex_panic!("Expected FixedSizeListVector, got {self:?}");
225    }
226
227    /// Consumes `self` and returns the inner [`StructVector`] if `self` is of that variant.
228    pub fn into_struct(self) -> StructVector {
229        if let Vector::Struct(v) = self {
230            return v;
231        }
232        vortex_panic!("Expected StructVector, got {self:?}");
233    }
234}