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::ArrayRef;
5use vortex_array::DynArray;
6use vortex_array::ExecutionCtx;
7use vortex_array::IntoArray;
8use vortex_array::arrays::dict::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::IntoArray;
61    use vortex_array::ToCanonical;
62    use vortex_array::arrays::PrimitiveArray;
63    use vortex_array::assert_arrays_eq;
64    use vortex_array::compute::conformance::take::test_take_conformance;
65
66    use crate::ALPRDFloat;
67    use crate::RDEncoder;
68
69    #[rstest]
70    #[case(0.1f32, 0.2f32, 3e25f32)]
71    #[case(0.1f64, 0.2f64, 3e100f64)]
72    fn test_take<T: ALPRDFloat>(#[case] a: T, #[case] b: T, #[case] outlier: T) {
73        use vortex_array::IntoArray as _;
74        use vortex_buffer::buffer;
75
76        let array = PrimitiveArray::from_iter([a, b, outlier]);
77        let encoded = RDEncoder::new(&[a, b]).encode(&array);
78
79        assert!(encoded.left_parts_patches().is_some());
80        assert!(
81            encoded
82                .left_parts_patches()
83                .unwrap()
84                .dtype()
85                .is_unsigned_int()
86        );
87
88        let taken = encoded
89            .take(buffer![0, 2].into_array())
90            .unwrap()
91            .to_primitive();
92
93        assert_arrays_eq!(taken, PrimitiveArray::from_iter([a, outlier]));
94    }
95
96    #[rstest]
97    #[case(0.1f32, 0.2f32, 3e25f32)]
98    #[case(0.1f64, 0.2f64, 3e100f64)]
99    fn take_with_nulls<T: ALPRDFloat>(#[case] a: T, #[case] b: T, #[case] outlier: T) {
100        let array = PrimitiveArray::from_iter([a, b, outlier]);
101        let encoded = RDEncoder::new(&[a, b]).encode(&array);
102
103        assert!(encoded.left_parts_patches().is_some());
104        assert!(
105            encoded
106                .left_parts_patches()
107                .unwrap()
108                .dtype()
109                .is_unsigned_int()
110        );
111
112        let taken = encoded
113            .take(PrimitiveArray::from_option_iter([Some(0), Some(2), None]).into_array())
114            .unwrap()
115            .to_primitive();
116
117        assert_arrays_eq!(
118            taken,
119            PrimitiveArray::from_option_iter([Some(a), Some(outlier), None])
120        );
121    }
122
123    #[rstest]
124    #[case(0.1f32, 0.2f32, 3e25f32)]
125    #[case(0.1f64, 0.2f64, 3e100f64)]
126    fn test_take_conformance_alprd<T: ALPRDFloat>(#[case] a: T, #[case] b: T, #[case] outlier: T) {
127        test_take_conformance(
128            &RDEncoder::new(&[a, b])
129                .encode(&PrimitiveArray::from_iter([a, b, outlier, b, outlier]))
130                .into_array(),
131        );
132    }
133
134    #[rstest]
135    #[case(0.1f32, 3e25f32)]
136    #[case(0.5f64, 1e100f64)]
137    fn test_take_with_nulls_conformance<T: ALPRDFloat>(#[case] a: T, #[case] outlier: T) {
138        test_take_conformance(
139            &RDEncoder::new(&[a])
140                .encode(&PrimitiveArray::from_option_iter([
141                    Some(a),
142                    None,
143                    Some(outlier),
144                    Some(a),
145                    None,
146                ]))
147                .into_array(),
148        );
149    }
150}