Skip to content

Commit

Permalink
Add gt eq dyn scalar kernel (#1117)
Browse files Browse the repository at this point in the history
* Add lt_dyn_scalar and tests

* Add lt_eq_dyn_scalar kernel

* Add gt_dyn_scalar kernel

* Add gt_eq_dyn_scalar kernel

* Add kernel to err message

Co-authored-by: Andrew Lamb <[email protected]>
  • Loading branch information
matthewmturner and alamb authored Jan 2, 2022
1 parent 1cbda7a commit 37b843b
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions arrow/src/compute/kernels/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,42 @@ where
}
}

/// Perform `left >= right` operation on an array and a numeric scalar
/// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values
pub fn gt_eq_dyn_scalar<T>(left: Arc<dyn Array>, right: T) -> Result<BooleanArray>
where
T: TryInto<i128> + Copy + std::fmt::Debug,
{
match left.data_type() {
DataType::Dictionary(key_type, value_type) => match value_type.as_ref() {
DataType::Int8
| DataType::Int16
| DataType::Int32
| DataType::Int64
| DataType::UInt8
| DataType::UInt16
| DataType::UInt32
| DataType::UInt64 => {dyn_compare_scalar!(&left, right, key_type, gt_eq_scalar)}
_ => Err(ArrowError::ComputeError(
"gt_eq_dyn_scalar only supports PrimitiveArray or DictionaryArray with Primitive values".to_string(),
))
}
DataType::Int8
| DataType::Int16
| DataType::Int32
| DataType::Int64
| DataType::UInt8
| DataType::UInt16
| DataType::UInt32
| DataType::UInt64 => {
dyn_compare_scalar!(&left, right, gt_eq_scalar)
}
_ => Err(ArrowError::ComputeError(
"gt_eq_dyn_scalar only supports PrimitiveArray or DictionaryArray with Primitive values".to_string(),
))
}
}

/// Perform `left == right` operation on an array and a numeric scalar
/// value. Supports StringArrays, and DictionaryArrays that have string values
pub fn eq_dyn_utf8_scalar(left: Arc<dyn Array>, right: &str) -> Result<BooleanArray> {
Expand Down Expand Up @@ -3209,6 +3245,35 @@ mod tests {
);
}

#[test]
fn test_gt_eq_dyn_scalar() {
let array = Int32Array::from(vec![6, 7, 8, 8, 10]);
let array = Arc::new(array);
let a_eq = gt_eq_dyn_scalar(array, 8).unwrap();
assert_eq!(
a_eq,
BooleanArray::from(
vec![Some(false), Some(false), Some(true), Some(true), Some(true)]
)
);
}

#[test]
fn test_gt_eq_dyn_scalar_with_dict() {
let key_builder = PrimitiveBuilder::<Int8Type>::new(3);
let value_builder = PrimitiveBuilder::<Int32Type>::new(2);
let mut builder = PrimitiveDictionaryBuilder::new(key_builder, value_builder);
builder.append(22).unwrap();
builder.append_null().unwrap();
builder.append(23).unwrap();
let array = Arc::new(builder.finish());
let a_eq = gt_eq_dyn_scalar(array, 23).unwrap();
assert_eq!(
a_eq,
BooleanArray::from(vec![Some(false), None, Some(true)])
);
}

#[test]
fn test_eq_dyn_utf8_scalar() {
let array = StringArray::from(vec!["abc", "def", "xyz"]);
Expand Down

0 comments on commit 37b843b

Please sign in to comment.