Skip to main content

vortex_array/arrays/null/compute/
take.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5use vortex_error::vortex_bail;
6
7use crate::ArrayRef;
8use crate::IntoArray;
9use crate::ToCanonical;
10use crate::array::ArrayView;
11use crate::arrays::Null;
12use crate::arrays::NullArray;
13use crate::arrays::dict::TakeReduce;
14use crate::arrays::dict::TakeReduceAdaptor;
15use crate::match_each_integer_ptype;
16use crate::optimizer::rules::ParentRuleSet;
17
18impl TakeReduce for Null {
19    #[allow(clippy::cast_possible_truncation)]
20    fn take(array: ArrayView<'_, Null>, indices: &ArrayRef) -> VortexResult<Option<ArrayRef>> {
21        let indices = indices.to_primitive();
22
23        // Enforce all indices are valid
24        match_each_integer_ptype!(indices.ptype(), |T| {
25            for index in indices.as_slice::<T>() {
26                if (*index as usize) >= array.len() {
27                    vortex_bail!(OutOfBounds: *index as usize, 0, array.len());
28                }
29            }
30        });
31
32        Ok(Some(NullArray::new(indices.len()).into_array()))
33    }
34}
35
36impl Null {
37    pub const TAKE_RULES: ParentRuleSet<Self> =
38        ParentRuleSet::new(&[ParentRuleSet::lift(&TakeReduceAdaptor::<Self>(Self))]);
39}