Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support dyn_compare_scalar for Decimal256 #4084

Merged
merged 2 commits into from
Apr 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions arrow-ord/src/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use arrow_array::cast::*;
use arrow_array::types::*;
use arrow_array::*;
use arrow_buffer::i256;
use arrow_buffer::{bit_util, BooleanBuffer, Buffer, MutableBuffer, NullBuffer};
use arrow_data::ArrayData;
use arrow_schema::{ArrowError, DataType, IntervalUnit, TimeUnit};
Expand Down Expand Up @@ -517,6 +518,11 @@ macro_rules! dyn_compare_scalar {
let left = as_primitive_array::<Decimal128Type>($LEFT);
$OP::<Decimal128Type>(left, right)
}
DataType::Decimal256(_, _) => {
let right = try_to_type!($RIGHT, to_i128)?;
let left = as_primitive_array::<Decimal256Type>($LEFT);
$OP::<Decimal256Type>(left, i256::from_i128(right))
}
DataType::Date32 => {
let right = try_to_type!($RIGHT, to_i32)?;
let left = as_primitive_array::<Date32Type>($LEFT);
Expand Down Expand Up @@ -6165,6 +6171,128 @@ mod tests {
assert_eq!(e, r);
}

#[test]
fn test_decimal256_scalar_i128() {
let a = Decimal256Array::from_iter_values(
[1, 2, 3, 4, 5].into_iter().map(i256::from_i128),
);
let b = i256::from_i128(3);
// array eq scalar
let e = BooleanArray::from(
vec![Some(false), Some(false), Some(true), Some(false), Some(false)],
);
let r = eq_scalar(&a, b).unwrap();
assert_eq!(e, r);
let r = eq_dyn_scalar(&a, b).unwrap();
assert_eq!(e, r);

// array neq scalar
let e = BooleanArray::from(
vec![Some(true), Some(true), Some(false), Some(true), Some(true)],
);
let r = neq_scalar(&a, b).unwrap();
assert_eq!(e, r);
let r = neq_dyn_scalar(&a, b).unwrap();
assert_eq!(e, r);

// array lt scalar
let e = BooleanArray::from(
vec![Some(true), Some(true), Some(false), Some(false), Some(false)],
);
let r = lt_scalar(&a, b).unwrap();
assert_eq!(e, r);
let r = lt_dyn_scalar(&a, b).unwrap();
assert_eq!(e, r);

// array lt_eq scalar
let e = BooleanArray::from(
vec![Some(true), Some(true), Some(true), Some(false), Some(false)],
);
let r = lt_eq_scalar(&a, b).unwrap();
assert_eq!(e, r);
let r = lt_eq_dyn_scalar(&a, b).unwrap();
assert_eq!(e, r);

// array gt scalar
let e = BooleanArray::from(
vec![Some(false), Some(false), Some(false), Some(true), Some(true)],
);
let r = gt_scalar(&a, b).unwrap();
assert_eq!(e, r);
let r = gt_dyn_scalar(&a, b).unwrap();
assert_eq!(e, r);

// array gt_eq scalar
let e = BooleanArray::from(
vec![Some(false), Some(false), Some(true), Some(true), Some(true)],
);
let r = gt_eq_scalar(&a, b).unwrap();
assert_eq!(e, r);
let r = gt_eq_dyn_scalar(&a, b).unwrap();
assert_eq!(e, r);
}

#[test]
fn test_decimal256_scalar_i256() {
let a = Decimal256Array::from_iter_values(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let a = Decimal256Array::from_iter_values(
// TODO: Support i256 scalars (#2837)
let a = Decimal256Array::from_iter_values(

[1, 2, 3, 4, 5].into_iter().map(i256::from_i128),
);
let b = i256::MAX;
// array eq scalar
let e = BooleanArray::from(
vec![Some(false), Some(false), Some(false), Some(false), Some(false)],
);
let r = eq_scalar(&a, b).unwrap();
assert_eq!(e, r);
let r = eq_dyn_scalar(&a, b).is_err();
assert!(r);

// array neq scalar
let e = BooleanArray::from(
vec![Some(true), Some(true), Some(true), Some(true), Some(true)],
);
let r = neq_scalar(&a, b).unwrap();
assert_eq!(e, r);
let r = neq_dyn_scalar(&a, b).is_err();
assert!(r);

// array lt scalar
let e = BooleanArray::from(
vec![Some(true), Some(true), Some(true), Some(true), Some(true)],
);
let r = lt_scalar(&a, b).unwrap();
assert_eq!(e, r);
let r = lt_dyn_scalar(&a, b).is_err();
assert!(r);

// array lt_eq scalar
let e = BooleanArray::from(
vec![Some(true), Some(true), Some(true), Some(true), Some(true)],
);
let r = lt_eq_scalar(&a, b).unwrap();
assert_eq!(e, r);
let r = lt_eq_dyn_scalar(&a, b).is_err();
assert!(r);

// array gt scalar
let e = BooleanArray::from(
vec![Some(false), Some(false), Some(false), Some(false), Some(false)],
);
let r = gt_scalar(&a, b).unwrap();
assert_eq!(e, r);
let r = gt_dyn_scalar(&a, b).is_err();
assert!(r);

// array gt_eq scalar
let e = BooleanArray::from(
vec![Some(false), Some(false), Some(false), Some(false), Some(false)],
);
let r = gt_eq_scalar(&a, b).unwrap();
assert_eq!(e, r);
let r = gt_eq_dyn_scalar(&a, b).is_err();
assert!(r);
}

#[test]
#[cfg(not(feature = "simd"))]
fn test_floating_zeros() {
Expand Down