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