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

Add neq dyn scalar kernel #1118

Merged
merged 7 commits into from
Jan 2, 2022
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
65 changes: 65 additions & 0 deletions arrow/src/compute/kernels/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,42 @@ where
}
}

/// Perform `left != right` operation on an array and a numeric scalar
/// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values
pub fn neq_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, neq_scalar)}
_ => Err(ArrowError::ComputeError(
"neq_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, neq_scalar)
}
_ => Err(ArrowError::ComputeError(
"neq_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 @@ -3274,6 +3310,35 @@ mod tests {
);
}

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

#[test]
fn test_neq_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 = neq_dyn_scalar(array, 23).unwrap();
assert_eq!(
a_eq,
BooleanArray::from(vec![Some(true), None, Some(false)])
);
}

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