|
| 1 | +mod between; |
1 | 2 | mod compare; |
2 | 3 | mod filter; |
3 | 4 |
|
4 | | -use std::fmt::Debug; |
5 | | - |
6 | | -use vortex_array::arrays::ConstantArray; |
7 | | -use vortex_array::compute::{ |
8 | | - BetweenFn, BetweenOptions, ScalarAtFn, SliceFn, StrictComparison, TakeFn, between, scalar_at, |
9 | | - slice, take, |
10 | | -}; |
| 5 | +use vortex_array::compute::{ScalarAtFn, SliceFn, TakeFn, scalar_at, slice, take}; |
11 | 6 | use vortex_array::variants::PrimitiveArrayTrait; |
12 | 7 | use vortex_array::vtable::ComputeVTable; |
13 | 8 | use vortex_array::{Array, ArrayRef}; |
14 | | -use vortex_dtype::{NativePType, Nullability}; |
15 | 9 | use vortex_error::VortexResult; |
16 | | -use vortex_scalar::{Scalar, ScalarType}; |
| 10 | +use vortex_scalar::Scalar; |
17 | 11 |
|
18 | 12 | use crate::{ALPArray, ALPEncoding, ALPFloat, match_each_alp_float_ptype}; |
19 | 13 |
|
20 | 14 | impl ComputeVTable for ALPEncoding { |
21 | | - fn between_fn(&self) -> Option<&dyn BetweenFn<&dyn Array>> { |
22 | | - Some(self) |
23 | | - } |
24 | | - |
25 | 15 | fn scalar_at_fn(&self) -> Option<&dyn ScalarAtFn<&dyn Array>> { |
26 | 16 | Some(self) |
27 | 17 | } |
@@ -93,157 +83,3 @@ impl SliceFn<&ALPArray> for ALPEncoding { |
93 | 83 | .into_array()) |
94 | 84 | } |
95 | 85 | } |
96 | | - |
97 | | -impl BetweenFn<&ALPArray> for ALPEncoding { |
98 | | - fn between( |
99 | | - &self, |
100 | | - array: &ALPArray, |
101 | | - lower: &dyn Array, |
102 | | - upper: &dyn Array, |
103 | | - options: &BetweenOptions, |
104 | | - ) -> VortexResult<Option<ArrayRef>> { |
105 | | - let (Some(lower), Some(upper)) = (lower.as_constant(), upper.as_constant()) else { |
106 | | - return Ok(None); |
107 | | - }; |
108 | | - |
109 | | - if array.patches().is_some() { |
110 | | - return Ok(None); |
111 | | - } |
112 | | - |
113 | | - let nullability = |
114 | | - array.dtype().nullability() | lower.dtype().nullability() | upper.dtype().nullability(); |
115 | | - |
116 | | - match_each_alp_float_ptype!(array.ptype(), |$F| { |
117 | | - between_impl::<$F>(array, $F::try_from(lower)?, $F::try_from(upper)?, nullability, options) |
118 | | - }) |
119 | | - .map(Some) |
120 | | - } |
121 | | -} |
122 | | - |
123 | | -fn between_impl<T: NativePType + ALPFloat>( |
124 | | - array: &ALPArray, |
125 | | - lower: T, |
126 | | - upper: T, |
127 | | - nullability: Nullability, |
128 | | - options: &BetweenOptions, |
129 | | -) -> VortexResult<ArrayRef> |
130 | | -where |
131 | | - Scalar: From<T::ALPInt>, |
132 | | - <T as ALPFloat>::ALPInt: ScalarType + Debug, |
133 | | -{ |
134 | | - let exponents = array.exponents(); |
135 | | - |
136 | | - // There are always compared |
137 | | - // the below bound is `value {< | <=} x`, either value encodes into the ALPInt domain |
138 | | - // in which case we can leave the comparison unchanged `enc(value) {< | <=} x` or it doesn't |
139 | | - // and we encode into value below enc_below(value) < value < x, in which case the comparison |
140 | | - // becomes enc(value) < x. See `alp_scalar_compare` for more details. |
141 | | - // note that if the value doesn't encode than value != x, so must use strict comparison. |
142 | | - let (lower_enc, lower_strict) = T::encode_single(lower, exponents) |
143 | | - .map(|x| (x, options.lower_strict)) |
144 | | - .unwrap_or_else(|| (T::encode_below(lower, exponents), StrictComparison::Strict)); |
145 | | - |
146 | | - // the upper value `x { < | <= } value` similarly encodes or `x < value < enc_above(value())` |
147 | | - let (upper_enc, upper_strict) = T::encode_single(upper, exponents) |
148 | | - .map(|x| (x, options.upper_strict)) |
149 | | - .unwrap_or_else(|| (T::encode_above(upper, exponents), StrictComparison::Strict)); |
150 | | - |
151 | | - let options = BetweenOptions { |
152 | | - lower_strict, |
153 | | - upper_strict, |
154 | | - }; |
155 | | - |
156 | | - between( |
157 | | - array.encoded(), |
158 | | - &ConstantArray::new(Scalar::primitive(lower_enc, nullability), array.len()), |
159 | | - &ConstantArray::new(Scalar::primitive(upper_enc, nullability), array.len()), |
160 | | - &options, |
161 | | - ) |
162 | | -} |
163 | | - |
164 | | -#[cfg(test)] |
165 | | -mod tests { |
166 | | - use itertools::Itertools; |
167 | | - use vortex_array::ToCanonical; |
168 | | - use vortex_array::arrays::PrimitiveArray; |
169 | | - use vortex_array::compute::{BetweenOptions, StrictComparison}; |
170 | | - use vortex_dtype::Nullability; |
171 | | - |
172 | | - use crate::alp::compute::between_impl; |
173 | | - use crate::{ALPArray, alp_encode}; |
174 | | - |
175 | | - fn between_test(arr: &ALPArray, lower: f32, upper: f32, options: &BetweenOptions) -> bool { |
176 | | - let res = between_impl(arr, lower, upper, Nullability::Nullable, options) |
177 | | - .unwrap() |
178 | | - .to_bool() |
179 | | - .unwrap() |
180 | | - .boolean_buffer() |
181 | | - .iter() |
182 | | - .collect_vec(); |
183 | | - assert_eq!(res.len(), 1); |
184 | | - |
185 | | - res[0] |
186 | | - } |
187 | | - |
188 | | - #[test] |
189 | | - fn comparison_range() { |
190 | | - let value = 0.0605_f32; |
191 | | - let array = PrimitiveArray::from_iter([value; 1]); |
192 | | - let encoded = alp_encode(&array, None).unwrap(); |
193 | | - assert!(encoded.patches().is_none()); |
194 | | - assert_eq!( |
195 | | - encoded.encoded().to_primitive().unwrap().as_slice::<i32>(), |
196 | | - vec![605; 1] |
197 | | - ); |
198 | | - |
199 | | - assert!(between_test( |
200 | | - &encoded, |
201 | | - 0.0605_f32, |
202 | | - 0.0605, |
203 | | - &BetweenOptions { |
204 | | - lower_strict: StrictComparison::NonStrict, |
205 | | - upper_strict: StrictComparison::NonStrict, |
206 | | - }, |
207 | | - )); |
208 | | - |
209 | | - assert!(!between_test( |
210 | | - &encoded, |
211 | | - 0.0605_f32, |
212 | | - 0.0605, |
213 | | - &BetweenOptions { |
214 | | - lower_strict: StrictComparison::Strict, |
215 | | - upper_strict: StrictComparison::NonStrict, |
216 | | - }, |
217 | | - )); |
218 | | - |
219 | | - assert!(!between_test( |
220 | | - &encoded, |
221 | | - 0.0605_f32, |
222 | | - 0.0605, |
223 | | - &BetweenOptions { |
224 | | - lower_strict: StrictComparison::NonStrict, |
225 | | - upper_strict: StrictComparison::Strict, |
226 | | - }, |
227 | | - )); |
228 | | - |
229 | | - assert!(between_test( |
230 | | - &encoded, |
231 | | - 0.060499_f32, |
232 | | - 0.06051, |
233 | | - &BetweenOptions { |
234 | | - lower_strict: StrictComparison::NonStrict, |
235 | | - upper_strict: StrictComparison::NonStrict, |
236 | | - }, |
237 | | - )); |
238 | | - |
239 | | - assert!(between_test( |
240 | | - &encoded, |
241 | | - 0.06_f32, |
242 | | - 0.06051, |
243 | | - &BetweenOptions { |
244 | | - lower_strict: StrictComparison::NonStrict, |
245 | | - upper_strict: StrictComparison::Strict, |
246 | | - }, |
247 | | - )) |
248 | | - } |
249 | | -} |
0 commit comments