Skip to main content

vortex_alp/alp/compute/
take.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_array::ArrayRef;
5use vortex_array::ArrayView;
6use vortex_array::ExecutionCtx;
7use vortex_array::IntoArray;
8use vortex_array::arrays::dict::TakeExecute;
9use vortex_error::VortexResult;
10
11use crate::ALP;
12use crate::ALPArrayExt;
13use crate::ALPArraySlotsExt;
14
15impl TakeExecute for ALP {
16    fn take(
17        array: ArrayView<'_, Self>,
18        indices: &ArrayRef,
19        ctx: &mut ExecutionCtx,
20    ) -> VortexResult<Option<ArrayRef>> {
21        let taken_encoded = array.encoded().take(indices.clone())?;
22        let taken_patches = array
23            .patches()
24            .map(|p| p.take(indices, ctx))
25            .transpose()?
26            .flatten()
27            .map(|patches| {
28                patches.cast_values(
29                    &array
30                        .dtype()
31                        .with_nullability(taken_encoded.dtype().nullability()),
32                )
33            })
34            .transpose()?;
35        Ok(Some(
36            ALP::new(taken_encoded, array.exponents(), taken_patches).into_array(),
37        ))
38    }
39}
40
41#[cfg(test)]
42mod test {
43    use rstest::rstest;
44    use vortex_array::IntoArray;
45    use vortex_array::ToCanonical;
46    use vortex_array::arrays::PrimitiveArray;
47    use vortex_array::compute::conformance::take::test_take_conformance;
48    use vortex_buffer::buffer;
49
50    use crate::alp_encode;
51
52    #[rstest]
53    #[case(buffer![1.23f32, 4.56, 7.89, 10.11, 12.13].into_array())]
54    #[case(buffer![100.1f64, 200.2, 300.3, 400.4, 500.5].into_array())]
55    #[case(PrimitiveArray::from_option_iter([Some(1.1f32), None, Some(2.2), Some(3.3), None]).into_array())]
56    #[case(buffer![42.42f64].into_array())]
57    fn test_take_alp_conformance(#[case] array: vortex_array::ArrayRef) {
58        let alp = alp_encode(&array.to_primitive(), None).unwrap();
59        test_take_conformance(&alp.into_array());
60    }
61}