Skip to main content

vortex_alp/alp_rd/
ops.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_array::ArrayView;
5use vortex_array::ExecutionCtx;
6use vortex_array::dtype::PType;
7use vortex_array::scalar::Scalar;
8use vortex_array::vtable::OperationsVTable;
9use vortex_error::VortexExpect;
10use vortex_error::VortexResult;
11
12use crate::ALPRD;
13use crate::ALPRDArrayExt;
14use crate::ALPRDArraySlotsExt;
15
16impl OperationsVTable<ALPRD> for ALPRD {
17    type ProbeState = ();
18
19    fn scalar_at(
20        array: ArrayView<'_, ALPRD>,
21        index: usize,
22        ctx: &mut ExecutionCtx,
23    ) -> VortexResult<Scalar> {
24        // The left value can either be a direct value, or an exception.
25        // The exceptions array represents exception positions with non-null values.
26        let maybe_patched_value = match array.left_parts_patches() {
27            Some(patches) => patches.get_patched(index)?,
28            None => None,
29        };
30        let left = match maybe_patched_value {
31            Some(patched_value) => patched_value
32                .as_primitive()
33                .as_::<u16>()
34                .vortex_expect("patched values must be non-null"),
35            _ => {
36                let left_code: u16 = array
37                    .left_parts()
38                    .execute_scalar(index, ctx)?
39                    .as_primitive()
40                    .as_::<u16>()
41                    .vortex_expect("left_code must be non-null");
42                array.left_parts_dictionary()[left_code as usize]
43            }
44        };
45
46        // combine left and right values
47        Ok(if array.dtype().as_ptype() == PType::F32 {
48            let right: u32 = array
49                .right_parts()
50                .execute_scalar(index, ctx)?
51                .as_primitive()
52                .as_::<u32>()
53                .vortex_expect("non-null");
54            let packed = f32::from_bits((left as u32) << array.right_bit_width() | right);
55            Scalar::primitive(packed, array.dtype().nullability())
56        } else {
57            let right: u64 = array
58                .right_parts()
59                .execute_scalar(index, ctx)?
60                .as_primitive()
61                .as_::<u64>()
62                .vortex_expect("non-null");
63            let packed = f64::from_bits(((left as u64) << array.right_bit_width()) | right);
64            Scalar::primitive(packed, array.dtype().nullability())
65        })
66    }
67}
68
69#[cfg(test)]
70mod test {
71    use std::sync::LazyLock;
72
73    use rstest::rstest;
74    use vortex_array::VortexSessionExecute;
75    use vortex_array::arrays::PrimitiveArray;
76    use vortex_array::assert_arrays_eq;
77    use vortex_array::dtype::NativePType;
78    use vortex_array::scalar::Scalar;
79    use vortex_session::VortexSession;
80
81    use crate::ALPRDArrayExt;
82    use crate::ALPRDFloat;
83    use crate::RDEncoder;
84    use crate::RDEncoderExt;
85
86    static SESSION: LazyLock<VortexSession> = LazyLock::new(|| {
87        let session = vortex_array::array_session();
88        crate::initialize(&session);
89        session
90    });
91
92    #[rstest]
93    #[case(0.1f32, 0.2f32, 3e25f32)]
94    #[case(0.1f64, 0.2f64, 3e100f64)]
95    fn test_slice<T: ALPRDFloat + NativePType>(#[case] a: T, #[case] b: T, #[case] outlier: T) {
96        let mut ctx = SESSION.create_execution_ctx();
97        let array = PrimitiveArray::from_iter([a, b, outlier]);
98        let encoded = RDEncoder::new(&[a, b]).encode(array.as_view());
99
100        assert!(encoded.left_parts_patches().is_some());
101        assert_arrays_eq!(encoded, array, &mut ctx);
102    }
103
104    #[rstest]
105    #[case(0.1f32, 0.2f32, 3e25f32)]
106    #[case(0.1f64, 0.2f64, 3e100f64)]
107    fn test_scalar_at<T: ALPRDFloat + NativePType + Into<Scalar>>(
108        #[case] a: T,
109        #[case] b: T,
110        #[case] outlier: T,
111    ) {
112        let mut ctx = SESSION.create_execution_ctx();
113        let array = PrimitiveArray::from_iter([a, b, outlier]);
114        let encoded = RDEncoder::new(&[a, b]).encode(array.as_view());
115        assert!(encoded.left_parts_patches().is_some());
116        assert_arrays_eq!(encoded, array, &mut ctx);
117    }
118
119    #[test]
120    fn nullable_scalar_at() {
121        let mut ctx = SESSION.create_execution_ctx();
122        let a = 0.1f64;
123        let b = 0.2f64;
124        let outlier = 3e100f64;
125        let array = PrimitiveArray::from_option_iter([Some(a), Some(b), Some(outlier)]);
126        let encoded = RDEncoder::new(&[a, b]).encode(array.as_view());
127        assert!(encoded.left_parts_patches().is_some());
128        assert_arrays_eq!(
129            encoded,
130            PrimitiveArray::from_option_iter([Some(a), Some(b), Some(outlier)]),
131            &mut ctx
132        );
133    }
134}