vortex_array/arrays/listview/compute/take.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use num_traits::Zero;
5use vortex_error::VortexResult;
6
7use crate::Array;
8use crate::ArrayRef;
9use crate::IntoArray;
10use crate::arrays::ListViewArray;
11use crate::arrays::ListViewRebuildMode;
12use crate::arrays::ListViewVTable;
13use crate::arrays::TakeReduce;
14use crate::builtins::ArrayBuiltins;
15use crate::dtype::Nullability;
16use crate::match_each_integer_ptype;
17use crate::scalar::Scalar;
18use crate::vtable::ValidityHelper;
19
20// TODO(connor)[ListView]: Make use of this threshold after we start migrating operators.
21/// The threshold for triggering a rebuild of the [`ListViewArray`].
22///
23/// By default, we will not touch the underlying `elements` array of the [`ListViewArray`] since it
24/// can be potentially expensive to reorganize the array based on what views we have into it.
25///
26/// However, we also do not want to carry around a large amount of garbage data. Below this
27/// threshold of the density of the selection mask, we will rebuild the [`ListViewArray`], removing
28/// any garbage data.
29#[allow(unused)]
30const REBUILD_DENSITY_THRESHOLD: f64 = 0.1;
31
32/// [`ListViewArray`] take implementation.
33///
34/// This implementation is deliberately simple and read-optimized. We just take the `offsets` and
35/// `sizes` at the requested indices and reuse the original `elements` array. This works because
36/// `ListView` (unlike `List`) allows non-contiguous and out-of-order lists.
37///
38/// We don't slice the `elements` array because it would require computing min/max offsets and
39/// adjusting all offsets accordingly, which is not really worth the small potential memory we would
40/// be able to get back.
41///
42/// The trade-off is that we may keep unreferenced elements in memory, but this is acceptable since
43/// we're optimizing for read performance and the data isn't being copied.
44impl TakeReduce for ListViewVTable {
45 fn take(array: &ListViewArray, indices: &ArrayRef) -> VortexResult<Option<ArrayRef>> {
46 let elements = array.elements();
47 let offsets = array.offsets();
48 let sizes = array.sizes();
49
50 // Compute the new validity by combining the array's validity with the indices' validity.
51 let new_validity = array.validity().take(indices)?;
52
53 // Take the offsets and sizes arrays at the requested indices.
54 // Take can reorder offsets, create gaps, and may introduce overlaps if the `indices`
55 // contain duplicates.
56 let nullable_new_offsets = offsets.take(indices.to_array())?;
57 let nullable_new_sizes = sizes.take(indices.to_array())?;
58
59 // Since `take` returns nullable arrays, we simply cast it back to non-nullable (filled with
60 // zeros to represent null lists).
61 let new_offsets = match_each_integer_ptype!(nullable_new_offsets.dtype().as_ptype(), |O| {
62 nullable_new_offsets
63 .fill_null(Scalar::primitive(O::zero(), Nullability::NonNullable))?
64 });
65 let new_sizes = match_each_integer_ptype!(nullable_new_sizes.dtype().as_ptype(), |S| {
66 nullable_new_sizes.fill_null(Scalar::primitive(S::zero(), Nullability::NonNullable))?
67 });
68 // SAFETY: Take operation maintains all `ListViewArray` invariants:
69 // - `new_offsets` and `new_sizes` are derived from existing valid child arrays.
70 // - `new_offsets` and `new_sizes` are non-nullable.
71 // - `new_offsets` and `new_sizes` have the same length (both taken with the same
72 // `indices`).
73 // - Validity correctly reflects the combination of array and indices validity.
74 let new_array = unsafe {
75 ListViewArray::new_unchecked(elements.clone(), new_offsets, new_sizes, new_validity)
76 };
77
78 // TODO(connor)[ListView]: Ideally, we would only rebuild after all `take`s and `filter`
79 // compute functions have run, at the "top" of the operator tree. However, we cannot do this
80 // right now, so we will just rebuild every time (similar to `ListArray`).
81
82 Ok(Some(
83 new_array
84 .rebuild(ListViewRebuildMode::MakeZeroCopyToList)?
85 .into_array(),
86 ))
87 }
88}