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    #[allow(clippy::same_name_method)]
56    pub fn slots(&self) -> &'a [Option<ArrayRef>] {
57        self.array.slots()
58    }
59
60    /// Returns the logical dtype.
61    #[allow(clippy::same_name_method)]
62    pub fn dtype(&self) -> &DType {
63        self.array.dtype()
64    }
65
66    /// Returns the number of rows.
67    #[allow(clippy::same_name_method)]
68    pub fn len(&self) -> usize {
69        self.array.len()
70    }
71
72    /// Returns `true` when the array has no rows.
73    #[allow(clippy::same_name_method)]
74    pub fn is_empty(&self) -> bool {
75        self.array.len() == 0
76    }
77
78    /// Returns the encoding ID.
79    pub fn encoding_id(&self) -> ArrayId {
80        self.array.encoding_id()
81    }
82
83    /// Returns the array's statistics set.
84    pub fn statistics(&self) -> StatsSetRef<'_> {
85        self.array.statistics()
86    }
87
88    /// Returns the array's validity representation.
89    pub fn validity(&self) -> VortexResult<Validity> {
90        self.array.validity()
91    }
92
93    /// Clone the underlying [`ArrayRef`] and return it as an owned typed handle.
94    pub fn into_owned(self) -> Array<V> {
95        // SAFETY: we are ourselves type checked as 'V'
96        unsafe { Array::<V>::from_array_ref_unchecked(self.array.clone()) }
97    }
98}
99
100impl<V: VTable> AsRef<ArrayRef> for ArrayView<'_, V> {
101    fn as_ref(&self) -> &ArrayRef {
102        self.array
103    }
104}
105
106impl<V: VTable> Deref for ArrayView<'_, V> {
107    type Target = V::TypedArrayData;
108
109    fn deref(&self) -> &V::TypedArrayData {
110        self.data
111    }
112}
113
114impl<V: VTable> Debug for ArrayView<'_, V> {
115    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
116        f.debug_struct("ArrayView")
117            .field("encoding", &self.array.encoding_id())
118            .field("dtype", self.array.dtype())
119            .field("len", &self.array.len())
120            .finish()
121    }
122}