-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmod.rs
266 lines (232 loc) · 7.96 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
mod compare;
use std::fmt::Debug;
use vortex_array::arrays::ConstantArray;
use vortex_array::compute::{
BetweenFn, BetweenOptions, CompareFn, FilterKernel, FilterKernelAdapter, KernelRef, ScalarAtFn,
SliceFn, StrictComparison, TakeFn, between, filter, scalar_at, slice, take,
};
use vortex_array::variants::PrimitiveArrayTrait;
use vortex_array::vtable::ComputeVTable;
use vortex_array::{Array, ArrayComputeImpl, ArrayRef};
use vortex_dtype::NativePType;
use vortex_error::VortexResult;
use vortex_mask::Mask;
use vortex_scalar::{Scalar, ScalarType};
use crate::{ALPArray, ALPEncoding, ALPFloat, match_each_alp_float_ptype};
impl ArrayComputeImpl for ALPArray {
const FILTER: Option<KernelRef> = FilterKernelAdapter(ALPEncoding).some();
}
impl ComputeVTable for ALPEncoding {
fn between_fn(&self) -> Option<&dyn BetweenFn<&dyn Array>> {
Some(self)
}
fn compare_fn(&self) -> Option<&dyn CompareFn<&dyn Array>> {
Some(self)
}
fn scalar_at_fn(&self) -> Option<&dyn ScalarAtFn<&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 ScalarAtFn<&ALPArray> for ALPEncoding {
fn scalar_at(&self, array: &ALPArray, index: usize) -> VortexResult<Scalar> {
if !array.encoded().is_valid(index)? {
return Ok(Scalar::null(array.dtype().clone()));
}
if let Some(patches) = array.patches() {
if let Some(patch) = patches.get_patched(index)? {
return patch.cast(array.dtype());
}
}
let encoded_val = scalar_at(array.encoded(), index)?;
Ok(match_each_alp_float_ptype!(array.ptype(), |$T| {
let encoded_val: <$T as ALPFloat>::ALPInt = encoded_val.as_ref().try_into().unwrap();
Scalar::primitive(<$T as ALPFloat>::decode_single(
encoded_val,
array.exponents(),
), array.dtype().nullability())
}))
}
}
impl TakeFn<&ALPArray> for ALPEncoding {
fn take(&self, array: &ALPArray, indices: &dyn Array) -> VortexResult<ArrayRef> {
let taken_encoded = take(array.encoded(), indices)?;
let taken_patches = array
.patches()
.map(|p| p.take(indices))
.transpose()?
.flatten()
.map(|p| {
p.cast_values(
&array
.dtype()
.with_nullability(taken_encoded.dtype().nullability()),
)
})
.transpose()?;
Ok(ALPArray::try_new(taken_encoded, array.exponents(), taken_patches)?.into_array())
}
}
impl SliceFn<&ALPArray> for ALPEncoding {
fn slice(&self, array: &ALPArray, start: usize, end: usize) -> VortexResult<ArrayRef> {
Ok(ALPArray::try_new(
slice(array.encoded(), start, end)?,
array.exponents(),
array
.patches()
.map(|p| p.slice(start, end))
.transpose()?
.flatten(),
)?
.into_array())
}
}
impl FilterKernel for ALPEncoding {
fn filter(&self, array: &ALPArray, mask: &Mask) -> VortexResult<ArrayRef> {
let patches = array
.patches()
.map(|p| p.filter(mask))
.transpose()?
.flatten();
Ok(
ALPArray::try_new(filter(array.encoded(), mask)?, array.exponents(), patches)?
.into_array(),
)
}
}
impl BetweenFn<&ALPArray> for ALPEncoding {
fn between(
&self,
array: &ALPArray,
lower: &dyn Array,
upper: &dyn Array,
options: &BetweenOptions,
) -> VortexResult<Option<ArrayRef>> {
let (Some(lower), Some(upper)) = (lower.as_constant(), upper.as_constant()) else {
return Ok(None);
};
if array.patches().is_some() {
return Ok(None);
}
match_each_alp_float_ptype!(array.ptype(), |$F| {
between_impl::<$F>(array, $F::try_from(lower)?, $F::try_from(upper)?, options)
})
.map(Some)
}
}
fn between_impl<T: NativePType + ALPFloat>(
array: &ALPArray,
lower: T,
upper: T,
options: &BetweenOptions,
) -> VortexResult<ArrayRef>
where
Scalar: From<T::ALPInt>,
<T as ALPFloat>::ALPInt: ScalarType + Debug,
{
let exponents = array.exponents();
// There are always compared
// the below bound is `value {< | <=} x`, either value encodes into the ALPInt domain
// in which case we can leave the comparison unchanged `enc(value) {< | <=} x` or it doesn't
// and we encode into value below enc_below(value) < value < x, in which case the comparison
// becomes enc(value) < x. See `alp_scalar_compare` for more details.
// note that if the value doesn't encode than value != x, so must use strict comparison.
let (lower_enc, lower_strict) = T::encode_single(lower, exponents)
.map(|x| (x, options.lower_strict))
.unwrap_or_else(|| (T::encode_below(lower, exponents), StrictComparison::Strict));
// the upper value `x { < | <= } value` similarly encodes or `x < value < enc_above(value())`
let (upper_enc, upper_strict) = T::encode_single(upper, exponents)
.map(|x| (x, options.upper_strict))
.unwrap_or_else(|| (T::encode_above(upper, exponents), StrictComparison::Strict));
let options = BetweenOptions {
lower_strict,
upper_strict,
};
between(
array.encoded(),
&ConstantArray::new(lower_enc, array.len()),
&ConstantArray::new(upper_enc, array.len()),
&options,
)
}
#[cfg(test)]
mod tests {
use itertools::Itertools;
use vortex_array::ToCanonical;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::compute::{BetweenOptions, StrictComparison};
use crate::ALPArray;
use crate::alp::compute::between_impl;
fn between_test(arr: &ALPArray, lower: f32, upper: f32, options: &BetweenOptions) -> bool {
let res = between_impl(arr, lower, upper, options)
.unwrap()
.to_bool()
.unwrap()
.boolean_buffer()
.iter()
.collect_vec();
assert_eq!(res.len(), 1);
res[0]
}
#[test]
fn comparison_range() {
let value = 0.0605_f32;
let array = PrimitiveArray::from_iter([value; 1]);
let encoded = crate::alp::compress::alp_encode(&array).unwrap();
assert!(encoded.patches().is_none());
assert_eq!(
encoded.encoded().to_primitive().unwrap().as_slice::<i32>(),
vec![605; 1]
);
assert!(between_test(
&encoded,
0.0605_f32,
0.0605,
&BetweenOptions {
lower_strict: StrictComparison::NonStrict,
upper_strict: StrictComparison::NonStrict,
},
));
assert!(!between_test(
&encoded,
0.0605_f32,
0.0605,
&BetweenOptions {
lower_strict: StrictComparison::Strict,
upper_strict: StrictComparison::NonStrict,
},
));
assert!(!between_test(
&encoded,
0.0605_f32,
0.0605,
&BetweenOptions {
lower_strict: StrictComparison::NonStrict,
upper_strict: StrictComparison::Strict,
},
));
assert!(between_test(
&encoded,
0.060499_f32,
0.06051,
&BetweenOptions {
lower_strict: StrictComparison::NonStrict,
upper_strict: StrictComparison::NonStrict,
},
));
assert!(between_test(
&encoded,
0.06_f32,
0.06051,
&BetweenOptions {
lower_strict: StrictComparison::NonStrict,
upper_strict: StrictComparison::Strict,
},
))
}
}