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::arrays::{ListViewArray, ListViewVTable};
10use crate::vtable::OperationsVTable;
11use crate::{ArrayRef, IntoArray};
12
13impl OperationsVTable<ListViewVTable> for ListViewVTable {
14    fn slice(array: &ListViewArray, range: Range<usize>) -> ArrayRef {
15        let start = range.start;
16        let end = range.end;
17
18        // We implement slice by simply slicing the views. We leave the child `elements` array alone
19        // since slicing could potentially require calculating which elements are referenced by the
20        // new set of views.
21
22        // SAFETY: The preconditions of `slice` mean that the bounds have already been checked, and
23        // slicing the components of an existing valid array is still valid.
24        unsafe {
25            ListViewArray::new_unchecked(
26                array.elements().clone(),
27                array.offsets().slice(start..end),
28                array.sizes().slice(start..end),
29                array.validity.slice(start..end),
30            )
31        }
32        .into_array()
33    }
34
35    fn scalar_at(array: &ListViewArray, index: usize) -> Scalar {
36        // By the preconditions we know that the list scalar is not null.
37        let list = array.list_elements_at(index);
38        let children: Vec<Scalar> = (0..list.len()).map(|i| list.scalar_at(i)).collect();
39
40        Scalar::list(
41            Arc::new(list.dtype().clone()),
42            children,
43            array.dtype.nullability(),
44        )
45    }
46}