1use std::fmt::Debug;
25
26use itertools::Itertools;
27use num_traits::Bounded;
28use num_traits::CheckedAdd;
29use num_traits::CheckedDiv;
30use num_traits::CheckedMul;
31use num_traits::CheckedSub;
32use num_traits::Float;
33use num_traits::Num;
34use num_traits::Signed;
35use vortex_error::VortexExpect;
36use vortex_error::vortex_panic;
37
38use crate::ArrayRef;
39use crate::Canonical;
40use crate::ExecutionCtx;
41use crate::IntoArray;
42use crate::RecursiveCanonical;
43use crate::arrays::ConstantArray;
44use crate::builtins::ArrayBuiltins;
45use crate::dtype::DType;
46use crate::dtype::DecimalDType;
47use crate::dtype::NativeDecimalType;
48use crate::dtype::NativePType;
49use crate::dtype::PType;
50use crate::dtype::i256;
51use crate::scalar::DecimalValue;
52use crate::scalar::NumericOperator;
53use crate::scalar::PrimitiveScalar;
54use crate::scalar::Scalar;
55use crate::scalar_fn::fns::binary::numeric_op_result_decimal_dtype;
56
57fn to_vec_of_scalar(array: &ArrayRef, ctx: &mut ExecutionCtx) -> Vec<Scalar> {
58 (0..array.len())
60 .map(|index| {
61 array
62 .execute_scalar(index, ctx)
63 .vortex_expect("scalar_at should succeed in conformance test")
64 })
65 .collect_vec()
66}
67
68fn test_binary_numeric_conformance<T: NativePType + Num + Copy>(
93 array: &ArrayRef,
94 ctx: &mut ExecutionCtx,
95) where
96 Scalar: From<T>,
97{
98 test_binary_numeric_with_scalar(array, T::one(), &[true, false], ctx);
100
101 test_binary_numeric_edge_cases(array, ctx);
103}
104
105pub fn test_binary_numeric_array(array: &ArrayRef, ctx: &mut ExecutionCtx) {
122 match array.dtype() {
123 DType::Primitive(ptype, _) => match ptype {
124 PType::I8 => test_binary_numeric_conformance::<i8>(array, ctx),
125 PType::I16 => test_binary_numeric_conformance::<i16>(array, ctx),
126 PType::I32 => test_binary_numeric_conformance::<i32>(array, ctx),
127 PType::I64 => test_binary_numeric_conformance::<i64>(array, ctx),
128 PType::U8 => test_binary_numeric_conformance::<u8>(array, ctx),
129 PType::U16 => test_binary_numeric_conformance::<u16>(array, ctx),
130 PType::U32 => test_binary_numeric_conformance::<u32>(array, ctx),
131 PType::U64 => test_binary_numeric_conformance::<u64>(array, ctx),
132 PType::F16 => {
133 eprintln!("Skipping f16 binary numeric tests (not supported)");
135 }
136 PType::F32 => test_binary_numeric_conformance::<f32>(array, ctx),
137 PType::F64 => test_binary_numeric_conformance::<f64>(array, ctx),
138 },
139 DType::Decimal(decimal_dtype, _) => {
140 test_binary_numeric_conformance_decimal(array, *decimal_dtype, ctx)
141 }
142 dtype => vortex_panic!(
143 "Binary numeric tests are only supported for primitive and decimal types, got {dtype}",
144 ),
145 }
146}
147
148fn test_binary_numeric_conformance_decimal(
152 array: &ArrayRef,
153 decimal_dtype: DecimalDType,
154 ctx: &mut ExecutionCtx,
155) {
156 let precision = decimal_dtype.precision() as usize;
157 let prec_max = <i256 as NativeDecimalType>::MAX_BY_PRECISION[precision];
158
159 let mut constants = vec![
160 i256::from_i128(0),
161 i256::from_i128(1),
162 i256::from_i128(-1),
163 prec_max,
164 ];
165 if decimal_dtype.scale() >= 0
167 && let Some(one) = i256::from_i128(10).checked_pow(decimal_dtype.scale() as u32)
168 && one <= prec_max
169 {
170 constants.push(one);
171 }
172
173 for constant in constants {
174 let value = DecimalValue::try_from_i256(constant, decimal_dtype)
175 .vortex_expect("conformance constants fit the precision");
176 test_decimal_binary_numeric_with_scalar(array, value, decimal_dtype, ctx);
177 }
178}
179
180fn test_decimal_binary_numeric_with_scalar(
181 array: &ArrayRef,
182 value: DecimalValue,
183 decimal_dtype: DecimalDType,
184 ctx: &mut ExecutionCtx,
185) {
186 let canonicalized_array = array
187 .clone()
188 .execute::<Canonical>(ctx)
189 .vortex_expect("Must be able to canonicalise")
190 .into_array();
191 let original_values = to_vec_of_scalar(&canonicalized_array, ctx);
192
193 let scalar = Scalar::decimal(value, decimal_dtype, array.dtype().nullability());
194
195 let mut operators = vec![
196 NumericOperator::Add,
197 NumericOperator::Sub,
198 NumericOperator::Mul,
199 ];
200 if !value.is_zero() {
201 operators.push(NumericOperator::Div);
202 }
203
204 for operator in operators {
205 for lhs_is_array in [true, false] {
206 test_decimal_binary_numeric_direction(
207 array,
208 &original_values,
209 &scalar,
210 decimal_dtype,
211 operator,
212 lhs_is_array,
213 ctx,
214 );
215 }
216 }
217}
218
219fn test_decimal_binary_numeric_direction(
220 array: &ArrayRef,
221 original_values: &[Scalar],
222 scalar: &Scalar,
223 decimal_dtype: DecimalDType,
224 operator: NumericOperator,
225 lhs_is_array: bool,
226 ctx: &mut ExecutionCtx,
227) {
228 let result_decimal_dtype = numeric_op_result_decimal_dtype(decimal_dtype, operator)
229 .vortex_expect("decimal arithmetic must have a result dtype");
230 let result_dtype = DType::Decimal(result_decimal_dtype, array.dtype().nullability());
231 let expected_results = expected_decimal_results(
232 original_values,
233 scalar,
234 operator,
235 result_decimal_dtype,
236 &result_dtype,
237 lhs_is_array,
238 );
239
240 let constant = ConstantArray::new(scalar.clone(), array.len()).into_array();
241 let (lhs, rhs) = if lhs_is_array {
242 (array.clone(), constant)
243 } else {
244 (constant, array.clone())
245 };
246 let result = lhs
247 .binary(rhs, operator.into())
248 .vortex_expect("binary decimal op shouldn't fail")
249 .execute::<RecursiveCanonical>(ctx)
250 .map(|c| c.0.into_array());
251
252 assert_decimal_results(
253 result,
254 &expected_results,
255 array,
256 scalar,
257 operator,
258 lhs_is_array,
259 ctx,
260 );
261}
262
263fn expected_decimal_results(
264 original_values: &[Scalar],
265 scalar: &Scalar,
266 operator: NumericOperator,
267 result_decimal_dtype: DecimalDType,
268 result_dtype: &DType,
269 lhs_is_array: bool,
270) -> Vec<Option<Scalar>> {
271 original_values
272 .iter()
273 .map(|value| {
274 let (lhs, rhs) = if lhs_is_array {
275 (value.as_decimal(), scalar.as_decimal())
276 } else {
277 (scalar.as_decimal(), value.as_decimal())
278 };
279 let (Some(lhs), Some(rhs)) = (lhs.decimal_value(), rhs.decimal_value()) else {
280 return Some(Scalar::null(result_dtype.clone()));
281 };
282 let lhs = lhs.as_i256();
283 let rhs = rhs.as_i256();
284 let value = match operator {
285 NumericOperator::Add => lhs.checked_add(&rhs),
286 NumericOperator::Sub => lhs.checked_sub(&rhs),
287 NumericOperator::Mul => lhs.checked_mul(&rhs),
288 NumericOperator::Div => {
289 let scale_power = result_decimal_dtype.scale();
290 let factor =
291 i256::from_i128(10).checked_pow(scale_power.unsigned_abs() as u32)?;
292 if scale_power >= 0 {
293 lhs.checked_mul(&factor)?.checked_div(&rhs)
294 } else {
295 lhs.checked_div(&rhs.checked_mul(&factor)?)
296 }
297 }
298 }?;
299 let value = DecimalValue::try_from_i256(value, result_decimal_dtype).ok()?;
300 Some(Scalar::decimal(
301 value,
302 result_decimal_dtype,
303 result_dtype.nullability(),
304 ))
305 })
306 .collect()
307}
308
309fn assert_decimal_results(
310 result: vortex_error::VortexResult<ArrayRef>,
311 expected_results: &[Option<Scalar>],
312 array: &ArrayRef,
313 scalar: &Scalar,
314 operator: NumericOperator,
315 lhs_is_array: bool,
316 ctx: &mut ExecutionCtx,
317) {
318 if expected_results.iter().any(Option::is_none) {
319 assert!(
320 result.is_err(),
321 "Decimal binary numeric operation should overflow for encoding {}: \
322 {operator:?} {scalar} (lhs_is_array: {lhs_is_array})",
323 array.encoding_id(),
324 );
325 return;
326 }
327
328 let result = result.unwrap_or_else(|err| {
329 vortex_panic!(
330 err,
331 "Decimal binary numeric operation unexpectedly failed for encoding {}: \
332 {operator:?} {scalar} (lhs_is_array: {lhs_is_array})",
333 array.encoding_id(),
334 )
335 });
336
337 assert_eq!(result.len(), expected_results.len());
338 let actual_values = to_vec_of_scalar(&result, ctx);
339 for (idx, (actual, expected)) in actual_values.iter().zip(expected_results).enumerate() {
340 let expected_value = expected
341 .as_ref()
342 .vortex_expect("non-overflowing decimal lane must have an expected value");
343 assert_eq!(
344 actual,
345 expected_value,
346 "Decimal binary numeric operation failed for encoding {} at index {}: \
347 ({array:?})[{idx}] {operator:?} {scalar} (lhs_is_array: {lhs_is_array}) \
348 expected {expected_value:?}, got {actual:?}",
349 array.encoding_id(),
350 idx,
351 );
352 }
353}
354
355fn test_binary_numeric_edge_cases(array: &ArrayRef, ctx: &mut ExecutionCtx) {
363 match array.dtype() {
364 DType::Primitive(ptype, _) => match ptype {
365 PType::I8 => test_binary_numeric_edge_cases_signed::<i8>(array, ctx),
366 PType::I16 => test_binary_numeric_edge_cases_signed::<i16>(array, ctx),
367 PType::I32 => test_binary_numeric_edge_cases_signed::<i32>(array, ctx),
368 PType::I64 => test_binary_numeric_edge_cases_signed::<i64>(array, ctx),
369 PType::U8 => test_binary_numeric_edge_cases_unsigned::<u8>(array, ctx),
370 PType::U16 => test_binary_numeric_edge_cases_unsigned::<u16>(array, ctx),
371 PType::U32 => test_binary_numeric_edge_cases_unsigned::<u32>(array, ctx),
372 PType::U64 => test_binary_numeric_edge_cases_unsigned::<u64>(array, ctx),
373 PType::F16 => {
374 eprintln!("Skipping f16 edge case tests (not supported)");
375 }
376 PType::F32 => test_binary_numeric_edge_cases_float::<f32>(array, ctx),
377 PType::F64 => test_binary_numeric_edge_cases_float::<f64>(array, ctx),
378 },
379 dtype => vortex_panic!(
380 "Binary numeric edge case tests are only supported for primitive numeric types, got {dtype}"
381 ),
382 }
383}
384
385fn test_binary_numeric_edge_cases_signed<T>(array: &ArrayRef, ctx: &mut ExecutionCtx)
386where
387 T: NativePType + Num + Copy + Debug + Bounded + Signed,
388 Scalar: From<T>,
389{
390 test_binary_numeric_with_scalar(array, T::zero(), &[true], ctx);
392
393 test_binary_numeric_with_scalar(array, -T::one(), &[true], ctx);
395
396 test_binary_numeric_with_scalar(array, T::max_value(), &[true], ctx);
398
399 test_binary_numeric_with_scalar(array, T::min_value(), &[true], ctx);
401}
402
403fn test_binary_numeric_edge_cases_unsigned<T>(array: &ArrayRef, ctx: &mut ExecutionCtx)
404where
405 T: NativePType + Num + Copy + Debug + Bounded,
406 Scalar: From<T>,
407{
408 test_binary_numeric_with_scalar(array, T::zero(), &[true], ctx);
410
411 test_binary_numeric_with_scalar(array, T::max_value(), &[true], ctx);
413}
414
415fn test_binary_numeric_edge_cases_float<T>(array: &ArrayRef, ctx: &mut ExecutionCtx)
416where
417 T: NativePType + Num + Copy + Debug + Float,
418 Scalar: From<T>,
419{
420 test_binary_numeric_with_scalar(array, T::zero(), &[true], ctx);
422
423 test_binary_numeric_with_scalar(array, -T::one(), &[true], ctx);
425
426 test_binary_numeric_with_scalar(array, T::max_value(), &[true], ctx);
428
429 test_binary_numeric_with_scalar(array, T::min_value(), &[true], ctx);
431
432 test_binary_numeric_with_scalar(array, T::epsilon(), &[true], ctx);
434
435 test_binary_numeric_with_scalar(array, T::min_positive_value(), &[true], ctx);
437
438 test_binary_numeric_with_scalar(array, T::nan(), &[true], ctx);
440 test_binary_numeric_with_scalar(array, T::infinity(), &[true], ctx);
441 test_binary_numeric_with_scalar(array, T::neg_infinity(), &[true], ctx);
442}
443
444fn test_binary_numeric_with_scalar<T>(
445 array: &ArrayRef,
446 scalar_value: T,
447 directions: &[bool],
448 ctx: &mut ExecutionCtx,
449) where
450 T: NativePType + Num + Copy + Debug,
451 Scalar: From<T>,
452{
453 let canonicalized_array = array
454 .clone()
455 .execute::<Canonical>(ctx)
456 .vortex_expect("Must be able to canonicalise")
457 .into_array();
458 let original_values = to_vec_of_scalar(&canonicalized_array, ctx);
459 let scalar = Scalar::from(scalar_value)
460 .cast(array.dtype())
461 .vortex_expect("constant must fit the array dtype");
462
463 for operator in [
464 NumericOperator::Add,
465 NumericOperator::Sub,
466 NumericOperator::Mul,
467 NumericOperator::Div,
468 ] {
469 if operator == NumericOperator::Div && scalar_value == T::zero() {
470 continue;
471 }
472 for &lhs_is_array in directions {
473 let expected_results: Vec<Option<Scalar>> = original_values
474 .iter()
475 .map(|value| {
476 let (lhs, rhs) = if lhs_is_array {
477 (value.as_primitive(), scalar.as_primitive())
478 } else {
479 (scalar.as_primitive(), value.as_primitive())
480 };
481 lhs.checked_binary_numeric(&rhs, operator)
482 .map(<Scalar as From<PrimitiveScalar<'_>>>::from)
483 })
484 .collect();
485 let constant = ConstantArray::new(scalar.clone(), array.len()).into_array();
486 let (lhs, rhs) = if lhs_is_array {
487 (array.clone(), constant)
488 } else {
489 (constant, array.clone())
490 };
491 let result = lhs
492 .binary(rhs, operator.into())
493 .vortex_expect("binary numeric construction must succeed")
494 .execute::<RecursiveCanonical>(ctx);
495 let result = match result {
496 Ok(result) => result.0.into_array(),
497 Err(err) => {
498 assert!(
499 expected_results.iter().any(Option::is_none),
500 "Binary numeric operation unexpectedly failed for encoding {}: \
501 {operator:?} {scalar} (lhs_is_array: {lhs_is_array}): {err}",
502 array.encoding_id(),
503 );
504 continue;
505 }
506 };
507 assert_eq!(result.len(), expected_results.len());
508 assert_eq!(result.dtype(), array.dtype());
509 let actual_values = to_vec_of_scalar(&result, ctx);
510 for (idx, (actual, expected)) in actual_values.iter().zip(&expected_results).enumerate()
511 {
512 if let Some(expected) = expected {
513 assert_eq!(
514 actual,
515 expected,
516 "Binary numeric operation failed for encoding {} at index {idx}: \
517 {operator:?} {scalar} (lhs_is_array: {lhs_is_array})",
518 array.encoding_id(),
519 );
520 }
521 }
522 }
523 }
524}