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;
14
15impl OperationsVTable<ALPRD> for ALPRD {
16    fn scalar_at(
17        array: ArrayView<'_, ALPRD>,
18        index: usize,
19        _ctx: &mut ExecutionCtx,
20    ) -> VortexResult<Scalar> {
21        // The left value can either be a direct value, or an exception.
22        // The exceptions array represents exception positions with non-null values.
23        let maybe_patched_value = match array.left_parts_patches() {
24            Some(patches) => patches.get_patched(index)?,
25            None => None,
26        };
27        let left = match maybe_patched_value {
28            Some(patched_value) => patched_value
29                .as_primitive()
30                .as_::<u16>()
31                .vortex_expect("patched values must be non-null"),
32            _ => {
33                let left_code: u16 = array
34                    .left_parts()
35                    .scalar_at(index)?
36                    .as_primitive()
37                    .as_::<u16>()
38                    .vortex_expect("left_code must be non-null");
39                array.left_parts_dictionary()[left_code as usize]
40            }
41        };
42
43        // combine left and right values
44        Ok(if array.dtype().as_ptype() == PType::F32 {
45            let right: u32 = array
46                .right_parts()
47                .scalar_at(index)?
48                .as_primitive()
49                .as_::<u32>()
50                .vortex_expect("non-null");
51            let packed = f32::from_bits((left as u32) << array.right_bit_width() | right);
52            Scalar::primitive(packed, array.dtype().nullability())
53        } else {
54            let right: u64 = array
55                .right_parts()
56                .scalar_at(index)?
57                .as_primitive()
58                .as_::<u64>()
59                .vortex_expect("non-null");
60            let packed = f64::from_bits(((left as u64) << array.right_bit_width()) | right);
61            Scalar::primitive(packed, array.dtype().nullability())
62        })
63    }
64}
65
66#[cfg(test)]
67mod test {
68    use rstest::rstest;
69    use vortex_array::arrays::PrimitiveArray;
70    use vortex_array::assert_arrays_eq;
71    use vortex_array::scalar::Scalar;
72
73    use crate::ALPRDArrayExt;
74    use crate::ALPRDFloat;
75    use crate::RDEncoder;
76
77    #[rstest]
78    #[case(0.1f32, 0.2f32, 3e25f32)]
79    #[case(0.1f64, 0.2f64, 3e100f64)]
80    fn test_slice<T: ALPRDFloat>(#[case] a: T, #[case] b: T, #[case] outlier: T) {
81        let array = PrimitiveArray::from_iter([a, b, outlier]);
82        let encoded = RDEncoder::new(&[a, b]).encode(&array);
83
84        assert!(encoded.left_parts_patches().is_some());
85        assert_arrays_eq!(encoded, array);
86    }
87
88    #[rstest]
89    #[case(0.1f32, 0.2f32, 3e25f32)]
90    #[case(0.1f64, 0.2f64, 3e100f64)]
91    fn test_scalar_at<T: ALPRDFloat + Into<Scalar>>(
92        #[case] a: T,
93        #[case] b: T,
94        #[case] outlier: T,
95    ) {
96        let array = PrimitiveArray::from_iter([a, b, outlier]);
97        let encoded = RDEncoder::new(&[a, b]).encode(&array);
98        assert!(encoded.left_parts_patches().is_some());
99        assert_arrays_eq!(encoded, array);
100    }
101
102    #[test]
103    fn nullable_scalar_at() {
104        let a = 0.1f64;
105        let b = 0.2f64;
106        let outlier = 3e100f64;
107        let array = PrimitiveArray::from_option_iter([Some(a), Some(b), Some(outlier)]);
108        let encoded = RDEncoder::new(&[a, b]).encode(&array);
109        assert!(encoded.left_parts_patches().is_some());
110        assert_arrays_eq!(
111            encoded,
112            PrimitiveArray::from_option_iter([Some(a), Some(b), Some(outlier)])
113        );
114    }
115}