vortex_array/arrays/listview/vtable/
operations.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::ops::Range;
5use std::sync::Arc;
6
7use vortex_scalar::Scalar;
8
9use crate::ArrayRef;
10use crate::IntoArray;
11use crate::arrays::ListViewArray;
12use crate::arrays::ListViewVTable;
13use crate::vtable::OperationsVTable;
14use crate::vtable::ValidityHelper;
15
16impl OperationsVTable<ListViewVTable> for ListViewVTable {
17    fn slice(array: &ListViewArray, range: Range<usize>) -> ArrayRef {
18        let start = range.start;
19        let end = range.end;
20
21        // We implement slice by simply slicing the views. We leave the child `elements` array alone
22        // since slicing could potentially require calculating which elements are referenced by the
23        // new set of views.
24
25        // SAFETY: The preconditions of `slice` mean that the bounds have already been checked, and
26        // slicing the components of an existing valid array is still valid.
27        // Additionally, slicing elements of a `ListViewArray` that is already zero-copyable to a
28        // `ListArray` does not reorder or create gaps and overlaps, slicing maintains whatever
29        // `is_zero_copy_to_list` flag it already had.
30        unsafe {
31            ListViewArray::new_unchecked(
32                array.elements().clone(),
33                array.offsets().slice(start..end),
34                array.sizes().slice(start..end),
35                array.validity().slice(start..end),
36            )
37            .with_zero_copy_to_list(array.is_zero_copy_to_list())
38        }
39        .into_array()
40    }
41
42    fn scalar_at(array: &ListViewArray, index: usize) -> Scalar {
43        // By the preconditions we know that the list scalar is not null.
44        let list = array.list_elements_at(index);
45        let children: Vec<Scalar> = (0..list.len()).map(|i| list.scalar_at(i)).collect();
46
47        Scalar::list(
48            Arc::new(list.dtype().clone()),
49            children,
50            array.dtype.nullability(),
51        )
52    }
53}