-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmod.rs
215 lines (191 loc) · 6.88 KB
/
mod.rs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
mod compare;
use std::ops::AddAssign;
use num_traits::{CheckedShl, CheckedShr, WrappingAdd, WrappingSub};
use vortex_array::compute::{
CompareFn, FilterKernel, FilterKernelAdapter, KernelRef, ScalarAtFn, SearchResult,
SearchSortedFn, SearchSortedSide, SliceFn, TakeFn, filter, scalar_at, search_sorted, slice,
take,
};
use vortex_array::variants::PrimitiveArrayTrait;
use vortex_array::vtable::ComputeVTable;
use vortex_array::{Array, ArrayComputeImpl, ArrayRef};
use vortex_dtype::{NativePType, match_each_integer_ptype};
use vortex_error::{VortexError, VortexExpect as _, VortexResult};
use vortex_mask::Mask;
use vortex_scalar::{PValue, Scalar};
use crate::{FoRArray, FoREncoding};
impl ArrayComputeImpl for FoRArray {
const FILTER: Option<KernelRef> = FilterKernelAdapter(FoREncoding).some();
}
impl ComputeVTable for FoREncoding {
fn compare_fn(&self) -> Option<&dyn CompareFn<&dyn Array>> {
Some(self)
}
fn scalar_at_fn(&self) -> Option<&dyn ScalarAtFn<&dyn Array>> {
Some(self)
}
fn search_sorted_fn(&self) -> Option<&dyn SearchSortedFn<&dyn Array>> {
Some(self)
}
fn slice_fn(&self) -> Option<&dyn SliceFn<&dyn Array>> {
Some(self)
}
fn take_fn(&self) -> Option<&dyn TakeFn<&dyn Array>> {
Some(self)
}
}
impl TakeFn<&FoRArray> for FoREncoding {
fn take(&self, array: &FoRArray, indices: &dyn Array) -> VortexResult<ArrayRef> {
FoRArray::try_new(
take(array.encoded(), indices)?,
array.reference_scalar().clone(),
)
.map(|a| a.into_array())
}
}
impl FilterKernel for FoREncoding {
fn filter(&self, array: &FoRArray, mask: &Mask) -> VortexResult<ArrayRef> {
FoRArray::try_new(
filter(array.encoded(), mask)?,
array.reference_scalar().clone(),
)
.map(|a| a.into_array())
}
}
impl ScalarAtFn<&FoRArray> for FoREncoding {
fn scalar_at(&self, array: &FoRArray, index: usize) -> VortexResult<Scalar> {
let encoded_pvalue = scalar_at(array.encoded(), index)?.reinterpret_cast(array.ptype());
let encoded_pvalue = encoded_pvalue.as_primitive();
let reference = array.reference_scalar();
let reference = reference.as_primitive();
Ok(match_each_integer_ptype!(array.ptype(), |$P| {
encoded_pvalue
.typed_value::<$P>()
.map(|v|
v.wrapping_add(
reference
.typed_value::<$P>()
.vortex_expect("FoRArray Reference value cannot be null")))
.map(|v| Scalar::primitive::<$P>(v, array.dtype().nullability()))
.unwrap_or_else(|| Scalar::null(array.dtype().clone()))
}))
}
}
impl SliceFn<&FoRArray> for FoREncoding {
fn slice(&self, array: &FoRArray, start: usize, stop: usize) -> VortexResult<ArrayRef> {
FoRArray::try_new(
slice(array.encoded(), start, stop)?,
array.reference_scalar().clone(),
)
.map(|a| a.into_array())
}
}
impl SearchSortedFn<&FoRArray> for FoREncoding {
fn search_sorted(
&self,
array: &FoRArray,
value: &Scalar,
side: SearchSortedSide,
) -> VortexResult<SearchResult> {
match_each_integer_ptype!(array.ptype(), |$P| {
search_sorted_typed::<$P>(array, value, side)
})
}
}
fn search_sorted_typed<T>(
array: &FoRArray,
value: &Scalar,
side: SearchSortedSide,
) -> VortexResult<SearchResult>
where
T: NativePType
+ for<'a> TryFrom<&'a Scalar, Error = VortexError>
+ TryFrom<PValue, Error = VortexError>
+ CheckedShr
+ CheckedShl
+ WrappingSub
+ WrappingAdd
+ AddAssign
+ Into<PValue>,
{
let min: T = array
.reference_scalar()
.as_primitive()
.typed_value::<T>()
.vortex_expect("Reference value cannot be null");
let primitive_value: T = value.cast(array.dtype())?.as_ref().try_into()?;
// Make sure that smaller values are still smaller and not larger than (which they would be after wrapping_sub)
if primitive_value.is_lt(min) {
return Ok(SearchResult::NotFound(array.invalid_count()?));
}
// When the values in the array are shifted, not all values in the domain are representable in the compressed
// space. Multiple different search values can translate to same value in the compressed space.
let target = primitive_value.wrapping_sub(&min);
let target_scalar = Scalar::primitive(target, value.dtype().nullability())
.reinterpret_cast(array.ptype().to_unsigned());
search_sorted(array.encoded(), target_scalar, side)
}
#[cfg(test)]
mod test {
use vortex_array::arrays::PrimitiveArray;
use vortex_array::compute::{SearchResult, SearchSortedSide, scalar_at, search_sorted};
use crate::for_compress;
#[test]
fn for_scalar_at() {
let for_arr = for_compress(PrimitiveArray::from_iter([-100, 1100, 1500, 1900])).unwrap();
assert_eq!(scalar_at(&for_arr, 0).unwrap(), (-100).into());
assert_eq!(scalar_at(&for_arr, 1).unwrap(), 1100.into());
assert_eq!(scalar_at(&for_arr, 2).unwrap(), 1500.into());
assert_eq!(scalar_at(&for_arr, 3).unwrap(), 1900.into());
}
#[test]
fn for_search() {
let for_arr = for_compress(PrimitiveArray::from_iter([1100, 1500, 1900])).unwrap();
assert_eq!(
search_sorted(&for_arr, 1500, SearchSortedSide::Left).unwrap(),
SearchResult::Found(1)
);
assert_eq!(
search_sorted(&for_arr, 2000, SearchSortedSide::Left).unwrap(),
SearchResult::NotFound(3)
);
assert_eq!(
search_sorted(&for_arr, 1000, SearchSortedSide::Left).unwrap(),
SearchResult::NotFound(0)
);
}
#[test]
fn search_with_shift_notfound() {
let for_arr = for_compress(PrimitiveArray::from_iter([62, 114])).unwrap();
assert_eq!(
search_sorted(&for_arr, 63, SearchSortedSide::Left).unwrap(),
SearchResult::NotFound(1)
);
assert_eq!(
search_sorted(&for_arr, 61, SearchSortedSide::Left).unwrap(),
SearchResult::NotFound(0)
);
assert_eq!(
search_sorted(&for_arr, 113, SearchSortedSide::Left).unwrap(),
SearchResult::NotFound(1)
);
assert_eq!(
search_sorted(&for_arr, 115, SearchSortedSide::Left).unwrap(),
SearchResult::NotFound(2)
);
}
#[test]
fn search_with_nulls() {
let for_arr = for_compress(PrimitiveArray::from_option_iter([
None,
None,
Some(-8739),
Some(-29),
]))
.unwrap();
assert_eq!(
search_sorted(&for_arr, -22360, SearchSortedSide::Left).unwrap(),
SearchResult::NotFound(2)
);
}
}