-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
suppport bitwise and as an example #1653
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d6b2b2e
suppport bitwise and as an example
liukun4515 f81bffb
Use $OP in macro rather than `&`
alamb 3b86d31
Merge remote-tracking branch 'apache/master' into bitwise_op_and
alamb 74598f2
fix: change signature to &dyn Array
alamb ec5a2db
fmt
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -334,6 +334,100 @@ fn modulus_decimal(left: &DecimalArray, right: &DecimalArray) -> Result<DecimalA | |||||
Ok(decimal_builder.finish()) | ||||||
} | ||||||
|
||||||
/// The binary_bitwise_array_op macro only evaluates for integer types | ||||||
/// like int64, int32. | ||||||
/// It is used to do bitwise operation. | ||||||
macro_rules! binary_bitwise_array_op { | ||||||
($LEFT:expr, $RIGHT:expr, $OP:tt, $ARRAY_TYPE:ident, $TYPE:ty) => {{ | ||||||
let len = $LEFT.len(); | ||||||
let left = $LEFT.as_any().downcast_ref::<$ARRAY_TYPE>().unwrap(); | ||||||
let right = $RIGHT.as_any().downcast_ref::<$ARRAY_TYPE>().unwrap(); | ||||||
let result = (0..len) | ||||||
.into_iter() | ||||||
.map(|i| { | ||||||
if left.is_null(i) || right.is_null(i) { | ||||||
None | ||||||
} else { | ||||||
Some(left.value(i) & right.value(i)) | ||||||
} | ||||||
}) | ||||||
.collect::<$ARRAY_TYPE>(); | ||||||
Ok(Arc::new(result)) | ||||||
}}; | ||||||
} | ||||||
|
||||||
/// The binary_bitwise_array_op macro only evaluates for integer types | ||||||
/// like int64, int32. | ||||||
/// It is used to do bitwise operation on an array with a scalar. | ||||||
macro_rules! binary_bitwise_array_scalar { | ||||||
($LEFT:expr, $RIGHT:expr, $OP:tt, $ARRAY_TYPE:ident, $TYPE:ty) => {{ | ||||||
let len = $LEFT.len(); | ||||||
let array = $LEFT.as_any().downcast_ref::<$ARRAY_TYPE>().unwrap(); | ||||||
let scalar = $RIGHT; | ||||||
if scalar.is_null() { | ||||||
Ok(new_null_array(array.data_type(), len)) | ||||||
} else { | ||||||
let right: $TYPE = scalar.try_into().unwrap(); | ||||||
let result = (0..len) | ||||||
.into_iter() | ||||||
.map(|i| { | ||||||
if array.is_null(i) { | ||||||
None | ||||||
} else { | ||||||
Some(array.value(i) & right) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this supposed to be
Suggested change
I don't think it matters for correctness but it may be confusing in the future |
||||||
} | ||||||
}) | ||||||
.collect::<$ARRAY_TYPE>(); | ||||||
Ok(Arc::new(result) as ArrayRef) | ||||||
} | ||||||
}}; | ||||||
} | ||||||
|
||||||
fn bitwise_and(left: ArrayRef, right: ArrayRef) -> Result<ArrayRef> { | ||||||
match &left.data_type() { | ||||||
DataType::Int8 => { | ||||||
binary_bitwise_array_op!(left, right, &, Int8Array, i8) | ||||||
} | ||||||
DataType::Int16 => { | ||||||
binary_bitwise_array_op!(left, right, &, Int16Array, i16) | ||||||
} | ||||||
DataType::Int32 => { | ||||||
binary_bitwise_array_op!(left, right, &, Int32Array, i32) | ||||||
} | ||||||
DataType::Int64 => { | ||||||
binary_bitwise_array_op!(left, right, &, Int64Array, i64) | ||||||
} | ||||||
other => Err(DataFusionError::Internal(format!( | ||||||
"Data type {:?} not supported for binary operation '{}' on dyn arrays", | ||||||
other, | ||||||
Operator::BitwiseAnd | ||||||
))), | ||||||
} | ||||||
} | ||||||
|
||||||
fn bitwise_and_scalar(array: &ArrayRef, scalar: ScalarValue) -> Option<Result<ArrayRef>> { | ||||||
let result = match array.data_type() { | ||||||
DataType::Int8 => { | ||||||
binary_bitwise_array_scalar!(array, scalar, &, Int8Array, i8) | ||||||
} | ||||||
DataType::Int16 => { | ||||||
binary_bitwise_array_scalar!(array, scalar, &, Int16Array, i16) | ||||||
} | ||||||
DataType::Int32 => { | ||||||
binary_bitwise_array_scalar!(array, scalar, &, Int32Array, i32) | ||||||
} | ||||||
DataType::Int64 => { | ||||||
binary_bitwise_array_scalar!(array, scalar, &, Int64Array, i64) | ||||||
} | ||||||
other => Err(DataFusionError::Internal(format!( | ||||||
"Data type {:?} not supported for binary operation '{}' on dyn arrays", | ||||||
other, | ||||||
Operator::BitwiseAnd | ||||||
))), | ||||||
}; | ||||||
Some(result) | ||||||
} | ||||||
|
||||||
/// Binary expression | ||||||
#[derive(Debug)] | ||||||
pub struct BinaryExpr { | ||||||
|
@@ -811,6 +905,8 @@ pub fn binary_operator_data_type( | |||||
| Operator::RegexNotIMatch | ||||||
| Operator::IsDistinctFrom | ||||||
| Operator::IsNotDistinctFrom => Ok(DataType::Boolean), | ||||||
// bitwise operations return the common coerced type | ||||||
Operator::BitwiseAnd => Ok(result_type), | ||||||
// math operations return the same value as the common coerced type | ||||||
Operator::Plus | ||||||
| Operator::Minus | ||||||
|
@@ -939,6 +1035,7 @@ impl BinaryExpr { | |||||
true, | ||||||
true | ||||||
), | ||||||
Operator::BitwiseAnd => bitwise_and_scalar(array, scalar.clone()), | ||||||
// if scalar operation is not supported - fallback to array implementation | ||||||
_ => None, | ||||||
}; | ||||||
|
@@ -1027,6 +1124,7 @@ impl BinaryExpr { | |||||
Operator::RegexNotIMatch => { | ||||||
binary_string_array_flag_op!(left, right, regexp_is_match, true, true) | ||||||
} | ||||||
Operator::BitwiseAnd => bitwise_and(left, right), | ||||||
} | ||||||
} | ||||||
} | ||||||
|
@@ -1464,6 +1562,18 @@ mod tests { | |||||
DataType::Boolean, | ||||||
vec![false, false, false, false, true] | ||||||
); | ||||||
test_coercion!( | ||||||
Int16Array, | ||||||
DataType::Int16, | ||||||
vec![1i16, 2i16, 3i16], | ||||||
Int64Array, | ||||||
DataType::Int64, | ||||||
vec![10i64, 4i64, 5i64], | ||||||
Operator::BitwiseAnd, | ||||||
Int64Array, | ||||||
DataType::Int64, | ||||||
vec![0i64, 0i64, 1i64] | ||||||
); | ||||||
Ok(()) | ||||||
} | ||||||
|
||||||
|
@@ -2838,4 +2948,25 @@ mod tests { | |||||
|
||||||
Ok(()) | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn bitwise_array_test() -> Result<()> { | ||||||
let left = Arc::new(Int32Array::from(vec![Some(12), None, Some(11)])) as ArrayRef; | ||||||
let right = | ||||||
Arc::new(Int32Array::from(vec![Some(1), Some(3), Some(7)])) as ArrayRef; | ||||||
let result = bitwise_and(left, right)?; | ||||||
let expected = Int32Array::from(vec![Some(0), None, Some(3)]); | ||||||
assert_eq!(result.as_ref(), &expected); | ||||||
Ok(()) | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn bitwise_scalar_test() -> Result<()> { | ||||||
let left = Arc::new(Int32Array::from(vec![Some(12), None, Some(11)])) as ArrayRef; | ||||||
let right = ScalarValue::from(3i32); | ||||||
let result = bitwise_and_scalar(&left, right).unwrap()?; | ||||||
let expected = Int32Array::from(vec![Some(0), None, Some(3)]); | ||||||
assert_eq!(result.as_ref(), &expected); | ||||||
Ok(()) | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As below, I think this is meant to be
$OP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in f81bffb (I was trying to make a follow on PR but then I found the logical conflict so I figured I would fix it directly on this PR).
I hope that is OK with you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is from my mistake.