Skip to main content

vortex_array/arrays/primitive/compute/
slice.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::ops::Range;
5
6use vortex_error::VortexResult;
7
8use crate::ArrayRef;
9use crate::IntoArray;
10use crate::array::ArrayView;
11use crate::arrays::Primitive;
12use crate::arrays::PrimitiveArray;
13use crate::arrays::slice::SliceReduce;
14
15impl SliceReduce for Primitive {
16    fn slice(array: ArrayView<'_, Self>, range: Range<usize>) -> VortexResult<Option<ArrayRef>> {
17        let byte_width = array.ptype().byte_width();
18        let byte_range = range.start * byte_width..range.end * byte_width;
19        let values = array.buffer_handle().slice(byte_range);
20        let validity = array.validity()?.slice(range)?;
21
22        // SAFETY:
23        //slicing an existing PrimitiveArray on element boundaries preserves the buffer
24        // alignment, ptype, length, and validity invariants.
25        let array = unsafe {
26            PrimitiveArray::new_unchecked_from_handle(values, array.ptype(), validity).into_array()
27        };
28
29        Ok(Some(array))
30    }
31}