Skip to main content

vortex_array/arrays/struct_/compute/
slice.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::ops::Range;
5
6use itertools::Itertools;
7use vortex_error::VortexResult;
8
9use crate::ArrayRef;
10use crate::IntoArray;
11use crate::arrays::SliceReduce;
12use crate::arrays::StructArray;
13use crate::arrays::StructVTable;
14use crate::vtable::ValidityHelper;
15
16impl SliceReduce for StructVTable {
17    fn slice(array: &Self::Array, range: Range<usize>) -> VortexResult<Option<ArrayRef>> {
18        let fields: Vec<_> = array
19            .unmasked_fields()
20            .iter()
21            .map(|field| field.slice(range.clone()))
22            .try_collect()?;
23
24        // SAFETY: Slicing preserves all StructArray invariants
25        Ok(Some(
26            unsafe {
27                StructArray::new_unchecked(
28                    fields,
29                    array.struct_fields().clone(),
30                    range.len(),
31                    array.validity().slice(range)?,
32                )
33            }
34            .into_array(),
35        ))
36    }
37}