|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::sync::Arc; |
| 5 | + |
| 6 | +use vortex_dtype::DType; |
| 7 | +use vortex_error::{VortexExpect, VortexResult, vortex_ensure}; |
| 8 | + |
| 9 | +use crate::stats::ArrayStats; |
| 10 | +use crate::validity::Validity; |
| 11 | +use crate::{Array, ArrayRef}; |
| 12 | + |
| 13 | +mod vtable; |
| 14 | +pub use vtable::{FixedSizeListEncoding, FixedSizeListVTable}; |
| 15 | + |
| 16 | +#[cfg(test)] |
| 17 | +mod tests; |
| 18 | + |
| 19 | +/// The canonical encoding for fixed-size list arrays. |
| 20 | +#[derive(Clone, Debug)] |
| 21 | +pub struct FixedSizeListArray { |
| 22 | + /// The [`DType`] of the fixed-size list. |
| 23 | + /// |
| 24 | + /// This type **must** be the variant [`DType::FixedSizeList`]. |
| 25 | + dtype: DType, |
| 26 | + |
| 27 | + /// The `elements` data array, where each fixed-size list scalar is a _slice_ of the `elements` |
| 28 | + /// array, and each inner list element is a _scalar_ of the `elements` array. |
| 29 | + /// |
| 30 | + /// The fixed-size list scalars (or the elements of the array) are contiguous (regardless of |
| 31 | + /// nullability for easy lookups), each with equal size in memory. |
| 32 | + elements: ArrayRef, |
| 33 | + |
| 34 | + /// The size of each fixed-size list scalar in the array. |
| 35 | + /// |
| 36 | + /// We store the size of each fixed-size list in the array as a field for convenience. |
| 37 | + list_size: u32, |
| 38 | + |
| 39 | + /// The validity / null map of the array. |
| 40 | + /// |
| 41 | + /// Note that this null map refers to the fixed-size list scalars, **not** the elements of the |
| 42 | + /// _individual_ fixed-size list scalars. The `elements` array will track individual value |
| 43 | + /// nullability. |
| 44 | + validity: Validity, |
| 45 | + |
| 46 | + /// The length of the array. |
| 47 | + /// |
| 48 | + /// Note that this is different from the size of each fixed-size list scalar (`list_size`). |
| 49 | + /// |
| 50 | + /// The main reason we need to store this (rather than calculate it on the fly via `list_size` |
| 51 | + /// and `elements.len()`) is because in the degenerate case where `list_size == 0`, we cannot |
| 52 | + /// use `0 / 0` to determine the length. |
| 53 | + len: usize, |
| 54 | + |
| 55 | + /// The stats for this array. |
| 56 | + stats_set: ArrayStats, |
| 57 | +} |
| 58 | + |
| 59 | +impl FixedSizeListArray { |
| 60 | + /// Creates a new `FixedSizeListArray`. This is simply a wrapper around [`try_new()`]. |
| 61 | + /// |
| 62 | + /// # Panics |
| 63 | + /// |
| 64 | + /// Panics if the inputs are invalid. See |
| 65 | + /// |
| 66 | + /// [`try_new()`]: Self::try_new |
| 67 | + pub fn new(elements: ArrayRef, list_size: u32, validity: Validity, len: usize) -> Self { |
| 68 | + Self::try_new(elements, list_size, validity, len) |
| 69 | + .vortex_expect("FixedSizeListArray `try_new` failed") |
| 70 | + } |
| 71 | + |
| 72 | + /// Attempts to create a new `FixedSizeListArray`. |
| 73 | + /// |
| 74 | + /// # Errors |
| 75 | + /// |
| 76 | + /// Returns an error if the inputs are invalid. The inputs are **valid** if: |
| 77 | + /// |
| 78 | + /// - The `list_size` is 0 and: |
| 79 | + /// - The `elements` array is empty. |
| 80 | + /// - The `len` is equal to the length of the `validity` map. |
| 81 | + /// - The length of the `elements` array is a multiple of the size of the fixed-size lists |
| 82 | + /// (`list_size`). |
| 83 | + /// - The `Validity` length (if it exists) times the `list_size` is equal to the length of the |
| 84 | + /// `elements` (or put another way, the length of the array divided by the size of each |
| 85 | + /// fixed-size list is equal to the length of the validity). |
| 86 | + pub fn try_new( |
| 87 | + elements: ArrayRef, |
| 88 | + list_size: u32, |
| 89 | + validity: Validity, |
| 90 | + len: usize, |
| 91 | + ) -> VortexResult<Self> { |
| 92 | + Self::validate(&elements, len, list_size, &validity)?; |
| 93 | + |
| 94 | + // SAFETY: we validate that the inputs are valid above. |
| 95 | + Ok(unsafe { Self::new_unchecked(elements, list_size, validity, len) }) |
| 96 | + } |
| 97 | + |
| 98 | + /// Creates a new `FixedSizeListArray`, assuming that the caller has validated the inputs. |
| 99 | + /// |
| 100 | + /// # Safety |
| 101 | + /// |
| 102 | + /// This function is only safe to call if the inputs are valid. See [`try_new()`] for more |
| 103 | + /// details on what the validity requirements are. |
| 104 | + /// |
| 105 | + /// [`try_new()`]: Self::try_new |
| 106 | + pub unsafe fn new_unchecked( |
| 107 | + elements: ArrayRef, |
| 108 | + list_size: u32, |
| 109 | + validity: Validity, |
| 110 | + len: usize, |
| 111 | + ) -> Self { |
| 112 | + let nullability = validity.nullability(); |
| 113 | + |
| 114 | + Self { |
| 115 | + dtype: DType::FixedSizeList(Arc::new(elements.dtype().clone()), list_size, nullability), |
| 116 | + elements, |
| 117 | + list_size, |
| 118 | + validity, |
| 119 | + len, |
| 120 | + stats_set: Default::default(), |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /// Returns the elements array. |
| 125 | + pub fn elements(&self) -> &ArrayRef { |
| 126 | + &self.elements |
| 127 | + } |
| 128 | + |
| 129 | + /// The size of each fixed-size list scalar in the array. |
| 130 | + pub const fn list_size(&self) -> u32 { |
| 131 | + self.list_size |
| 132 | + } |
| 133 | + |
| 134 | + /// Returns the elements at the given index from the list array. |
| 135 | + /// |
| 136 | + /// # Panics |
| 137 | + /// |
| 138 | + /// Panics if the index is out of bounds. |
| 139 | + pub fn fixed_size_list_at(&self, index: usize) -> ArrayRef { |
| 140 | + debug_assert!( |
| 141 | + index < self.len, |
| 142 | + "index out of bounds: the len is {} but the index is {index}", |
| 143 | + self.len |
| 144 | + ); |
| 145 | + debug_assert!( |
| 146 | + self.validity |
| 147 | + .is_valid(index) |
| 148 | + .vortex_expect("this shouldn't be a result in the first place") |
| 149 | + ); |
| 150 | + |
| 151 | + let start = self.list_size as usize * index; |
| 152 | + let end = self.list_size as usize * (index + 1); |
| 153 | + self.elements().slice(start..end) |
| 154 | + } |
| 155 | + |
| 156 | + /// Checks if the components of a `FixedSizeListArray` are valid. |
| 157 | + /// |
| 158 | + /// See [`try_new()`](Self::try_new) for the validation semantics. |
| 159 | + fn validate( |
| 160 | + elements: &dyn Array, |
| 161 | + len: usize, |
| 162 | + list_size: u32, |
| 163 | + validity: &Validity, |
| 164 | + ) -> VortexResult<()> { |
| 165 | + // A fixed-size list array where each list scalar is empty is completely useless, but we can |
| 166 | + // support it regardless. |
| 167 | + if list_size == 0 { |
| 168 | + vortex_ensure!( |
| 169 | + elements.is_empty() && validity.maybe_len().is_none_or(|vlen| vlen == len), |
| 170 | + "an empty `FixedSizeList` should have no elements" |
| 171 | + ); |
| 172 | + return Ok(()); |
| 173 | + } |
| 174 | + |
| 175 | + let num_elements = elements.len(); |
| 176 | + |
| 177 | + vortex_ensure!( |
| 178 | + len * list_size as usize == num_elements, |
| 179 | + "the `elements` array has the incorrect number of elements to construct a \ |
| 180 | + `FixedSizeList[{list_size}] array of length {len}", |
| 181 | + ); |
| 182 | + |
| 183 | + // If a validity array is present, it must be the same length as the fixed-size list array. |
| 184 | + if let Some(validity_len) = validity.maybe_len() { |
| 185 | + vortex_ensure!( |
| 186 | + len == validity_len, |
| 187 | + "validity with size {validity_len} does not match fixed-size list array size {len}", |
| 188 | + ); |
| 189 | + } |
| 190 | + |
| 191 | + Ok(()) |
| 192 | + } |
| 193 | +} |
0 commit comments