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::VortexSessionExecute;
10use crate::array::ArrayView;
11use crate::arrays::Null;
12use crate::arrays::NullArray;
13use crate::arrays::PrimitiveArray;
14use crate::arrays::dict::TakeReduce;
15use crate::arrays::dict::TakeReduceAdaptor;
16use crate::match_each_integer_ptype;
17use crate::optimizer::rules::ParentRuleSet;
18
19impl TakeReduce for Null {
20    #[expect(clippy::cast_possible_truncation)]
21    fn take(array: ArrayView<'_, Null>, indices: &ArrayRef) -> VortexResult<Option<ArrayRef>> {
22        #[allow(clippy::disallowed_methods)]
23        let mut ctx = crate::legacy_session().create_execution_ctx();
24        let indices = indices.clone().execute::<PrimitiveArray>(&mut ctx)?;
25
26        // Enforce all indices are valid
27        match_each_integer_ptype!(indices.ptype(), |T| {
28            for index in indices.as_slice::<T>() {
29                if (*index as usize) >= array.len() {
30                    vortex_bail!(OutOfBounds: *index as usize, 0, array.len());
31                }
32            }
33        });
34
35        Ok(Some(NullArray::new(indices.len()).into_array()))
36    }
37}
38
39impl Null {
40    pub const TAKE_RULES: ParentRuleSet<Self> =
41        ParentRuleSet::new(&[ParentRuleSet::lift(&TakeReduceAdaptor::<Self>(Self))]);
42}