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        // SAFETY: The preconditions of `slice` mean that the bounds have already been checked, and
19        // slicing the components of an existing valid array is still valid.
20        unsafe {
21            ListViewArray::new_unchecked(
22                array.elements().clone(),
23                array.offsets().slice(start..end),
24                array.sizes().slice(start..end),
25                array.validity.slice(start..end),
26            )
27        }
28        .into_array()
29    }
30
31    fn scalar_at(array: &ListViewArray, index: usize) -> Scalar {
32        // By the preconditions we know that the list scalar is not null.
33        let list = array.list_elements_at(index);
34        let children: Vec<Scalar> = (0..list.len()).map(|i| list.scalar_at(i)).collect();
35
36        Scalar::list(
37            Arc::new(list.dtype().clone()),
38            children,
39            array.dtype.nullability(),
40        )
41    }
42}