|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! Definition and implementation of [`ListViewVector`]. |
| 5 | +
|
| 6 | +use std::sync::Arc; |
| 7 | + |
| 8 | +use vortex_error::{VortexExpect, VortexResult}; |
| 9 | +use vortex_mask::Mask; |
| 10 | + |
| 11 | +use super::ListViewVectorMut; |
| 12 | +use crate::Vector; |
| 13 | +use crate::ops::{VectorMutOps, VectorOps}; |
| 14 | +use crate::primitive::PrimitiveVector; |
| 15 | + |
| 16 | +/// A vector of variable-width lists. |
| 17 | +/// |
| 18 | +/// Each list is defined by 2 integers: an offset and a size (a "list view"), which point into a |
| 19 | +/// child `elements` vector. |
| 20 | +/// |
| 21 | +/// Note that the list views **do not** need to be sorted, nor do they have to be contiguous or |
| 22 | +/// fully cover the `elements` vector. This means that multiple views can be pointing to the same |
| 23 | +/// elements. |
| 24 | +/// |
| 25 | +/// # Structure |
| 26 | +/// |
| 27 | +/// - `elements`: The child vector of all list elements, stored as an [`Arc<Vector>`]. |
| 28 | +/// - `offsets`: A [`PrimitiveVector`] containing the starting offset of each list in the `elements` |
| 29 | +/// vector. |
| 30 | +/// - `sizes`: A [`PrimitiveVector`] containing the size (number of elements) of each list. |
| 31 | +/// - `validity`: A [`Mask`] indicating which lists are null. |
| 32 | +#[derive(Debug, Clone)] |
| 33 | +pub struct ListViewVector { |
| 34 | + /// The child vector of elements. |
| 35 | + pub(super) elements: Arc<Vector>, |
| 36 | + |
| 37 | + /// Offsets for each list into the elements vector. |
| 38 | + pub(super) offsets: PrimitiveVector, |
| 39 | + |
| 40 | + /// Sizes (lengths) of each list. |
| 41 | + pub(super) sizes: PrimitiveVector, |
| 42 | + |
| 43 | + /// The validity mask (where `true` represents a list is **not** null). |
| 44 | + /// |
| 45 | + /// Note that the `elements` vector will have its own internal validity, denoting if individual |
| 46 | + /// list elements are null. |
| 47 | + pub(super) validity: Mask, |
| 48 | + |
| 49 | + /// The length of the vector (which is the same as the length of the validity mask). |
| 50 | + /// |
| 51 | + /// This is stored here as a convenience, as the validity also tracks this information. |
| 52 | + pub(super) len: usize, |
| 53 | +} |
| 54 | + |
| 55 | +impl ListViewVector { |
| 56 | + /// Creates a new [`ListViewVector`] from its components. |
| 57 | + /// |
| 58 | + /// # Panics |
| 59 | + /// |
| 60 | + /// TODO |
| 61 | + pub fn new( |
| 62 | + elements: Arc<Vector>, |
| 63 | + offsets: PrimitiveVector, |
| 64 | + sizes: PrimitiveVector, |
| 65 | + validity: Mask, |
| 66 | + ) -> Self { |
| 67 | + Self::try_new(elements, offsets, sizes, validity) |
| 68 | + .vortex_expect("Invalid ListViewVector construction") |
| 69 | + } |
| 70 | + |
| 71 | + /// Attempts to create a new [`ListViewVector`] from its components. |
| 72 | + /// |
| 73 | + /// # Errors |
| 74 | + /// |
| 75 | + /// TODO |
| 76 | + pub fn try_new( |
| 77 | + _elements: Arc<Vector>, |
| 78 | + _offsets: PrimitiveVector, |
| 79 | + _sizes: PrimitiveVector, |
| 80 | + _validity: Mask, |
| 81 | + ) -> VortexResult<Self> { |
| 82 | + todo!() |
| 83 | + } |
| 84 | + |
| 85 | + /// Creates a new [`ListViewVector`] without validation. |
| 86 | + /// |
| 87 | + /// # Safety |
| 88 | + /// |
| 89 | + /// TODO |
| 90 | + pub unsafe fn new_unchecked( |
| 91 | + elements: Arc<Vector>, |
| 92 | + offsets: PrimitiveVector, |
| 93 | + sizes: PrimitiveVector, |
| 94 | + validity: Mask, |
| 95 | + ) -> Self { |
| 96 | + let len = validity.len(); |
| 97 | + |
| 98 | + if cfg!(debug_assertions) { |
| 99 | + Self::new(elements, offsets, sizes, validity) |
| 100 | + } else { |
| 101 | + Self { |
| 102 | + elements, |
| 103 | + offsets, |
| 104 | + sizes, |
| 105 | + validity, |
| 106 | + len, |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /// Decomposes the [`ListViewVector`] into its constituent parts (child elements, offsets, |
| 112 | + /// sizes, and validity). |
| 113 | + pub fn into_parts(self) -> (Arc<Vector>, PrimitiveVector, PrimitiveVector, Mask) { |
| 114 | + (self.elements, self.offsets, self.sizes, self.validity) |
| 115 | + } |
| 116 | + |
| 117 | + /// Returns a reference to the `elements` vector. |
| 118 | + #[inline] |
| 119 | + pub fn elements(&self) -> &Vector { |
| 120 | + &self.elements |
| 121 | + } |
| 122 | + |
| 123 | + /// Returns a reference to the integer `offsets` vector. |
| 124 | + #[inline] |
| 125 | + pub fn offsets(&self) -> &PrimitiveVector { |
| 126 | + &self.offsets |
| 127 | + } |
| 128 | + |
| 129 | + /// Returns a reference to the integer `sizes` vector. |
| 130 | + #[inline] |
| 131 | + pub fn sizes(&self) -> &PrimitiveVector { |
| 132 | + &self.sizes |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl VectorOps for ListViewVector { |
| 137 | + type Mutable = ListViewVectorMut; |
| 138 | + |
| 139 | + fn len(&self) -> usize { |
| 140 | + self.len |
| 141 | + } |
| 142 | + |
| 143 | + fn validity(&self) -> &Mask { |
| 144 | + &self.validity |
| 145 | + } |
| 146 | + |
| 147 | + fn try_into_mut(self) -> Result<ListViewVectorMut, Self> { |
| 148 | + // Try to unwrap the `Arc`. |
| 149 | + let elements = match Arc::try_unwrap(self.elements) { |
| 150 | + Ok(elements) => elements, |
| 151 | + Err(elements) => return Err(Self { elements, ..self }), |
| 152 | + }; |
| 153 | + |
| 154 | + // Try to make the validity mutable. |
| 155 | + let validity = match self.validity.try_into_mut() { |
| 156 | + Ok(v) => v, |
| 157 | + Err(validity) => { |
| 158 | + return Err(Self { |
| 159 | + elements: Arc::new(elements), |
| 160 | + validity, |
| 161 | + ..self |
| 162 | + }); |
| 163 | + } |
| 164 | + }; |
| 165 | + |
| 166 | + // Try to make the offsets mutable. |
| 167 | + let offsets = match self.offsets.try_into_mut() { |
| 168 | + Ok(mutable_offsets) => mutable_offsets, |
| 169 | + Err(offsets) => { |
| 170 | + return Err(Self { |
| 171 | + offsets, |
| 172 | + sizes: self.sizes, |
| 173 | + elements: Arc::new(elements), |
| 174 | + validity: validity.freeze(), |
| 175 | + len: self.len, |
| 176 | + }); |
| 177 | + } |
| 178 | + }; |
| 179 | + |
| 180 | + // Try to make the sizes mutable. |
| 181 | + let sizes = match self.sizes.try_into_mut() { |
| 182 | + Ok(mutable_sizes) => mutable_sizes, |
| 183 | + Err(sizes) => { |
| 184 | + return Err(Self { |
| 185 | + offsets: offsets.freeze(), |
| 186 | + sizes, |
| 187 | + elements: Arc::new(elements), |
| 188 | + validity: validity.freeze(), |
| 189 | + len: self.len, |
| 190 | + }); |
| 191 | + } |
| 192 | + }; |
| 193 | + |
| 194 | + // Try to make the elements mutable. |
| 195 | + match elements.try_into_mut() { |
| 196 | + Ok(mut_elements) => Ok(ListViewVectorMut { |
| 197 | + offsets, |
| 198 | + sizes, |
| 199 | + elements: Box::new(mut_elements), |
| 200 | + validity, |
| 201 | + len: self.len, |
| 202 | + }), |
| 203 | + Err(elements) => Err(Self { |
| 204 | + offsets: offsets.freeze(), |
| 205 | + sizes: sizes.freeze(), |
| 206 | + elements: Arc::new(elements), |
| 207 | + validity: validity.freeze(), |
| 208 | + len: self.len, |
| 209 | + }), |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments