vortex_array/array/
mod.rs1mod canonical;
2mod convert;
3mod implementation;
4mod statistics;
5mod validity;
6mod variants;
7mod visitor;
8
9use std::any::Any;
10use std::fmt::{Debug, Display, Formatter};
11use std::sync::Arc;
12
13pub use canonical::*;
14pub use convert::*;
15pub use implementation::*;
16pub use statistics::*;
17pub use validity::*;
18pub use variants::*;
19pub use visitor::*;
20use vortex_dtype::DType;
21use vortex_error::{VortexExpect, VortexResult};
22use vortex_mask::Mask;
23
24use crate::arrays::{
25 BoolEncoding, ExtensionEncoding, ListEncoding, NullEncoding, PrimitiveEncoding, StructEncoding,
26 VarBinEncoding, VarBinViewEncoding,
27};
28use crate::builders::ArrayBuilder;
29use crate::compute::{ComputeFn, InvocationArgs, Output};
30use crate::stats::StatsSetRef;
31use crate::vtable::{EncodingVTable, VTableRef};
32use crate::{Canonical, EncodingId};
33
34pub trait Array: Send + Sync + Debug + ArrayStatistics + ArrayVariants + ArrayVisitor {
40 fn as_any(&self) -> &dyn Any;
42
43 fn as_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
45
46 fn to_array(&self) -> ArrayRef;
48
49 fn into_array(self) -> ArrayRef
51 where
52 Self: Sized;
53
54 fn len(&self) -> usize;
56
57 fn is_empty(&self) -> bool {
59 self.len() == 0
60 }
61
62 fn dtype(&self) -> &DType;
64
65 fn encoding(&self) -> EncodingId;
67
68 fn vtable(&self) -> VTableRef;
70
71 fn is_encoding(&self, encoding: EncodingId) -> bool {
73 self.encoding() == encoding
74 }
75
76 fn is_arrow(&self) -> bool {
79 self.is_encoding(NullEncoding.id())
80 || self.is_encoding(BoolEncoding.id())
81 || self.is_encoding(PrimitiveEncoding.id())
82 || self.is_encoding(VarBinEncoding.id())
83 || self.is_encoding(VarBinViewEncoding.id())
84 }
85
86 fn is_canonical(&self) -> bool {
89 self.is_encoding(NullEncoding.id())
90 || self.is_encoding(BoolEncoding.id())
91 || self.is_encoding(PrimitiveEncoding.id())
92 || self.is_encoding(StructEncoding.id())
93 || self.is_encoding(ListEncoding.id())
94 || self.is_encoding(VarBinViewEncoding.id())
95 || self.is_encoding(ExtensionEncoding.id())
96 }
97
98 fn is_valid(&self, index: usize) -> VortexResult<bool>;
100
101 fn is_invalid(&self, index: usize) -> VortexResult<bool>;
103
104 fn all_valid(&self) -> VortexResult<bool>;
108
109 fn all_invalid(&self) -> VortexResult<bool>;
113
114 fn valid_count(&self) -> VortexResult<usize>;
116
117 fn invalid_count(&self) -> VortexResult<usize>;
119
120 fn validity_mask(&self) -> VortexResult<Mask>;
122
123 fn to_canonical(&self) -> VortexResult<Canonical>;
125
126 fn append_to_builder(&self, builder: &mut dyn ArrayBuilder) -> VortexResult<()>;
130
131 fn statistics(&self) -> StatsSetRef<'_>;
134
135 fn with_children(&self, children: &[ArrayRef]) -> VortexResult<ArrayRef>;
137
138 fn invoke(&self, compute_fn: &ComputeFn, args: &InvocationArgs)
155 -> VortexResult<Option<Output>>;
156}
157
158impl Array for Arc<dyn Array> {
159 fn as_any(&self) -> &dyn Any {
160 self.as_ref().as_any()
161 }
162
163 fn as_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
164 self
165 }
166
167 fn to_array(&self) -> ArrayRef {
168 self.clone()
169 }
170
171 fn into_array(self) -> ArrayRef {
172 self
173 }
174
175 fn len(&self) -> usize {
176 self.as_ref().len()
177 }
178
179 fn dtype(&self) -> &DType {
180 self.as_ref().dtype()
181 }
182
183 fn encoding(&self) -> EncodingId {
184 self.as_ref().encoding()
185 }
186
187 fn vtable(&self) -> VTableRef {
188 self.as_ref().vtable()
189 }
190
191 fn is_valid(&self, index: usize) -> VortexResult<bool> {
192 self.as_ref().is_valid(index)
193 }
194
195 fn is_invalid(&self, index: usize) -> VortexResult<bool> {
196 self.as_ref().is_invalid(index)
197 }
198
199 fn all_valid(&self) -> VortexResult<bool> {
200 self.as_ref().all_valid()
201 }
202
203 fn all_invalid(&self) -> VortexResult<bool> {
204 self.as_ref().all_invalid()
205 }
206
207 fn valid_count(&self) -> VortexResult<usize> {
208 self.as_ref().valid_count()
209 }
210
211 fn invalid_count(&self) -> VortexResult<usize> {
212 self.as_ref().invalid_count()
213 }
214
215 fn validity_mask(&self) -> VortexResult<Mask> {
216 self.as_ref().validity_mask()
217 }
218
219 fn to_canonical(&self) -> VortexResult<Canonical> {
220 self.as_ref().to_canonical()
221 }
222
223 fn append_to_builder(&self, builder: &mut dyn ArrayBuilder) -> VortexResult<()> {
224 self.as_ref().append_to_builder(builder)
225 }
226
227 fn statistics(&self) -> StatsSetRef<'_> {
228 self.as_ref().statistics()
229 }
230
231 fn with_children(&self, children: &[ArrayRef]) -> VortexResult<ArrayRef> {
232 self.as_ref().with_children(children)
233 }
234
235 fn invoke(
236 &self,
237 compute_fn: &ComputeFn,
238 args: &InvocationArgs,
239 ) -> VortexResult<Option<Output>> {
240 self.as_ref().invoke(compute_fn, args)
241 }
242}
243
244pub type ArrayRef = Arc<dyn Array>;
246
247impl ToOwned for dyn Array {
248 type Owned = ArrayRef;
249
250 fn to_owned(&self) -> Self::Owned {
251 self.to_array()
252 }
253}
254
255impl<A: Array + Clone + 'static> TryFromArrayRef for A {
256 fn try_from_array(array: ArrayRef) -> Result<Self, ArrayRef> {
257 let fallback = array.clone();
258 if let Ok(array) = array.as_any_arc().downcast::<A>() {
259 drop(fallback);
261 Ok(Arc::unwrap_or_clone(array))
262 } else {
263 Err(fallback)
264 }
265 }
266}
267
268impl<A: Array + Clone + 'static> TryFromArrayRef for Arc<A> {
269 fn try_from_array(array: ArrayRef) -> Result<Self, ArrayRef> {
270 let fallback = array.clone();
271 array.as_any_arc().downcast::<A>().map_err(|_| fallback)
272 }
273}
274
275pub trait ArrayExt: Array {
276 fn as_<A: Array + 'static>(&self) -> &A {
278 self.as_any()
279 .downcast_ref::<A>()
280 .vortex_expect("Failed to downcast")
281 }
282
283 fn as_opt<A: Array + 'static>(&self) -> Option<&A> {
285 self.as_any().downcast_ref::<A>()
286 }
287
288 fn is<A: Array + 'static>(&self) -> bool {
290 self.as_opt::<A>().is_some()
291 }
292}
293
294impl<A: Array + ?Sized> ArrayExt for A {}
295
296impl Display for dyn Array {
297 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
298 write!(
299 f,
300 "{}({}, len={})",
301 self.encoding(),
302 self.dtype(),
303 self.len()
304 )
305 }
306}
307
308#[macro_export]
309macro_rules! try_from_array_ref {
310 ($Array:ty) => {
311 impl TryFrom<$crate::ArrayRef> for $Array {
312 type Error = vortex_error::VortexError;
313
314 fn try_from(value: $crate::ArrayRef) -> Result<Self, Self::Error> {
315 Ok(::std::sync::Arc::unwrap_or_clone(
316 value.as_any_arc().downcast::<Self>().map_err(|_| {
317 vortex_error::vortex_err!(
318 "Cannot downcast to {}",
319 std::any::type_name::<Self>()
320 )
321 })?,
322 ))
323 }
324 }
325 };
326}