Skip to main content

vortex_alp/alp_rd/compute/
take.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_array::Array;
5use vortex_array::ArrayRef;
6use vortex_array::ExecutionCtx;
7use vortex_array::IntoArray;
8use vortex_array::arrays::TakeExecute;
9use vortex_array::builtins::ArrayBuiltins;
10use vortex_array::scalar::Scalar;
11use vortex_error::VortexResult;
12
13use crate::ALPRDArray;
14use crate::ALPRDVTable;
15
16impl TakeExecute for ALPRDVTable {
17    fn take(
18        array: &ALPRDArray,
19        indices: &ArrayRef,
20        ctx: &mut ExecutionCtx,
21    ) -> VortexResult<Option<ArrayRef>> {
22        let taken_left_parts = array.left_parts().take(indices.to_array())?;
23        let left_parts_exceptions = array
24            .left_parts_patches()
25            .map(|patches| patches.take(indices, ctx))
26            .transpose()?
27            .flatten()
28            .map(|p| {
29                let values_dtype = p
30                    .values()
31                    .dtype()
32                    .with_nullability(taken_left_parts.dtype().nullability());
33                p.cast_values(&values_dtype)
34            })
35            .transpose()?;
36        let right_parts = array
37            .right_parts()
38            .take(indices.to_array())?
39            .fill_null(Scalar::zero_value(array.right_parts().dtype()))?;
40
41        Ok(Some(
42            ALPRDArray::try_new(
43                array
44                    .dtype()
45                    .with_nullability(taken_left_parts.dtype().nullability()),
46                taken_left_parts,
47                array.left_parts_dictionary().clone(),
48                right_parts,
49                array.right_bit_width(),
50                left_parts_exceptions,
51            )?
52            .into_array(),
53        ))
54    }
55}
56
57#[cfg(test)]
58mod test {
59    use rstest::rstest;
60    use vortex_array::ToCanonical;
61    use vortex_array::arrays::PrimitiveArray;
62    use vortex_array::assert_arrays_eq;
63    use vortex_array::compute::conformance::take::test_take_conformance;
64
65    use crate::ALPRDFloat;
66    use crate::RDEncoder;
67
68    #[rstest]
69    #[case(0.1f32, 0.2f32, 3e25f32)]
70    #[case(0.1f64, 0.2f64, 3e100f64)]
71    fn test_take<T: ALPRDFloat>(#[case] a: T, #[case] b: T, #[case] outlier: T) {
72        use vortex_array::IntoArray as _;
73        use vortex_buffer::buffer;
74
75        let array = PrimitiveArray::from_iter([a, b, outlier]);
76        let encoded = RDEncoder::new(&[a, b]).encode(&array);
77
78        assert!(encoded.left_parts_patches().is_some());
79        assert!(
80            encoded
81                .left_parts_patches()
82                .unwrap()
83                .dtype()
84                .is_unsigned_int()
85        );
86
87        let taken = encoded
88            .take(buffer![0, 2].into_array())
89            .unwrap()
90            .to_primitive();
91
92        assert_arrays_eq!(taken, PrimitiveArray::from_iter([a, outlier]));
93    }
94
95    #[rstest]
96    #[case(0.1f32, 0.2f32, 3e25f32)]
97    #[case(0.1f64, 0.2f64, 3e100f64)]
98    fn take_with_nulls<T: ALPRDFloat>(#[case] a: T, #[case] b: T, #[case] outlier: T) {
99        let array = PrimitiveArray::from_iter([a, b, outlier]);
100        let encoded = RDEncoder::new(&[a, b]).encode(&array);
101
102        assert!(encoded.left_parts_patches().is_some());
103        assert!(
104            encoded
105                .left_parts_patches()
106                .unwrap()
107                .dtype()
108                .is_unsigned_int()
109        );
110
111        let taken = encoded
112            .take(PrimitiveArray::from_option_iter([Some(0), Some(2), None]).to_array())
113            .unwrap()
114            .to_primitive();
115
116        assert_arrays_eq!(
117            taken,
118            PrimitiveArray::from_option_iter([Some(a), Some(outlier), None])
119        );
120    }
121
122    #[rstest]
123    #[case(0.1f32, 0.2f32, 3e25f32)]
124    #[case(0.1f64, 0.2f64, 3e100f64)]
125    fn test_take_conformance_alprd<T: ALPRDFloat>(#[case] a: T, #[case] b: T, #[case] outlier: T) {
126        test_take_conformance(
127            &RDEncoder::new(&[a, b])
128                .encode(&PrimitiveArray::from_iter([a, b, outlier, b, outlier]))
129                .to_array(),
130        );
131    }
132
133    #[rstest]
134    #[case(0.1f32, 3e25f32)]
135    #[case(0.5f64, 1e100f64)]
136    fn test_take_with_nulls_conformance<T: ALPRDFloat>(#[case] a: T, #[case] outlier: T) {
137        test_take_conformance(
138            &RDEncoder::new(&[a])
139                .encode(&PrimitiveArray::from_option_iter([
140                    Some(a),
141                    None,
142                    Some(outlier),
143                    Some(a),
144                    None,
145                ]))
146                .to_array(),
147        );
148    }
149}