Skip to main content

vortex_array/array/
view.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::fmt::Debug;
5use std::fmt::Formatter;
6use std::ops::Deref;
7
8use vortex_error::VortexResult;
9
10use crate::ArrayRef;
11use crate::array::Array;
12use crate::array::ArrayId;
13use crate::array::VTable;
14use crate::dtype::DType;
15use crate::stats::StatsSetRef;
16use crate::validity::Validity;
17
18/// A lightweight, `Copy`-able typed view into an [`ArrayRef`].
19///
20/// Vtable methods receive `ArrayView<V>` so they can inspect common array metadata and
21/// encoding-specific data without cloning or downcasting an [`ArrayRef`]. The view is only valid for
22/// the lifetime of the borrowed array.
23pub struct ArrayView<'a, V: VTable> {
24    array: &'a ArrayRef,
25    data: &'a V::TypedArrayData,
26}
27
28impl<V: VTable> Copy for ArrayView<'_, V> {}
29
30impl<V: VTable> Clone for ArrayView<'_, V> {
31    fn clone(&self) -> Self {
32        *self
33    }
34}
35
36impl<'a, V: VTable> ArrayView<'a, V> {
37    /// # Safety
38    /// Caller must ensure `data` is the `V::TypedArrayData` stored inside `array`.
39    pub(crate) unsafe fn new_unchecked(array: &'a ArrayRef, data: &'a V::TypedArrayData) -> Self {
40        debug_assert!(array.is::<V>());
41        Self { array, data }
42    }
43
44    /// Returns the erased array handle that owns this view.
45    pub fn array(&self) -> &'a ArrayRef {
46        self.array
47    }
48
49    /// Returns the encoding-specific data stored by this array.
50    pub fn data(&self) -> &'a V::TypedArrayData {
51        self.data
52    }
53
54    /// Returns this array's child slots.
55    pub fn slots(&self) -> &'a [Option<ArrayRef>] {
56        self.array.slots()
57    }
58
59    /// Returns the logical dtype.
60    pub fn dtype(&self) -> &DType {
61        self.array.dtype()
62    }
63
64    /// Returns the number of rows.
65    pub fn len(&self) -> usize {
66        self.array.len()
67    }
68
69    /// Returns `true` when the array has no rows.
70    pub fn is_empty(&self) -> bool {
71        self.array.len() == 0
72    }
73
74    /// Returns the encoding ID.
75    pub fn encoding_id(&self) -> ArrayId {
76        self.array.encoding_id()
77    }
78
79    /// Returns the array's statistics set.
80    pub fn statistics(&self) -> StatsSetRef<'_> {
81        self.array.statistics()
82    }
83
84    /// Returns the array's validity representation.
85    pub fn validity(&self) -> VortexResult<Validity> {
86        self.array.validity()
87    }
88
89    /// Clone the underlying [`ArrayRef`] and return it as an owned typed handle.
90    pub fn into_owned(self) -> Array<V> {
91        // SAFETY: we are ourselves type checked as 'V'
92        unsafe { Array::<V>::from_array_ref_unchecked(self.array.clone()) }
93    }
94}
95
96impl<V: VTable> AsRef<ArrayRef> for ArrayView<'_, V> {
97    fn as_ref(&self) -> &ArrayRef {
98        self.array
99    }
100}
101
102impl<V: VTable> Deref for ArrayView<'_, V> {
103    type Target = V::TypedArrayData;
104
105    fn deref(&self) -> &V::TypedArrayData {
106        self.data
107    }
108}
109
110impl<V: VTable> Debug for ArrayView<'_, V> {
111    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
112        f.debug_struct("ArrayView")
113            .field("encoding", &self.array.encoding_id())
114            .field("dtype", self.array.dtype())
115            .field("len", &self.array.len())
116            .finish()
117    }
118}