-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: Support Binary bitwise shift operators (<< and >>) #3037
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,7 +50,10 @@ use arrow::compute::kernels::comparison::{ | |
|
|
||
| use adapter::{eq_dyn, gt_dyn, gt_eq_dyn, lt_dyn, lt_eq_dyn, neq_dyn}; | ||
| use arrow::compute::kernels::concat_elements::concat_elements_utf8; | ||
| use kernels::{bitwise_and, bitwise_and_scalar, bitwise_or, bitwise_or_scalar}; | ||
| use kernels::{ | ||
| bitwise_and, bitwise_and_scalar, bitwise_or, bitwise_or_scalar, bitwise_shift_left, | ||
| bitwise_shift_left_scalar, bitwise_shift_right, bitwise_shift_right_scalar, | ||
| }; | ||
| use kernels_arrow::{ | ||
| add_decimal, add_decimal_scalar, divide_decimal, divide_decimal_scalar, | ||
| eq_decimal_scalar, gt_decimal_scalar, gt_eq_decimal_scalar, is_distinct_from, | ||
|
|
@@ -740,6 +743,12 @@ impl BinaryExpr { | |
| ), | ||
| Operator::BitwiseAnd => bitwise_and_scalar(array, scalar.clone()), | ||
| Operator::BitwiseOr => bitwise_or_scalar(array, scalar.clone()), | ||
| Operator::BitwiseShiftRight => { | ||
| bitwise_shift_right_scalar(array, scalar.clone()) | ||
| } | ||
| Operator::BitwiseShiftLeft => { | ||
| bitwise_shift_left_scalar(array, scalar.clone()) | ||
| } | ||
| // if scalar operation is not supported - fallback to array implementation | ||
| _ => None, | ||
| }; | ||
|
|
@@ -850,6 +859,8 @@ impl BinaryExpr { | |
| } | ||
| Operator::BitwiseAnd => bitwise_and(left, right), | ||
| Operator::BitwiseOr => bitwise_or(left, right), | ||
| Operator::BitwiseShiftRight => bitwise_shift_right(left, right), | ||
| Operator::BitwiseShiftLeft => bitwise_shift_left(left, right), | ||
| Operator::StringConcat => { | ||
| binary_string_array_op!(left, right, concat_elements) | ||
| } | ||
|
|
@@ -2481,6 +2492,34 @@ mod tests { | |
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn bitwise_shift_array_test() -> Result<()> { | ||
| let input = Arc::new(Int32Array::from(vec![Some(2), None, Some(10)])) as ArrayRef; | ||
| let modules = | ||
|
Contributor
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. I suggest a test for when the |
||
| Arc::new(Int32Array::from(vec![Some(2), Some(4), Some(8)])) as ArrayRef; | ||
| let mut result = bitwise_shift_left(input.clone(), modules.clone())?; | ||
|
|
||
| let expected = Int32Array::from(vec![Some(8), None, Some(2560)]); | ||
| assert_eq!(result.as_ref(), &expected); | ||
|
|
||
| result = bitwise_shift_right(result.clone(), modules.clone())?; | ||
| assert_eq!(result.as_ref(), &input); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn bitwise_shift_array_overflow_test() -> Result<()> { | ||
| let input = Arc::new(Int32Array::from(vec![Some(2)])) as ArrayRef; | ||
| let modules = Arc::new(Int32Array::from(vec![Some(100)])) as ArrayRef; | ||
| let result = bitwise_shift_left(input.clone(), modules.clone())?; | ||
|
|
||
| let expected = Int32Array::from(vec![Some(32)]); | ||
|
Contributor
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.
Contributor
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.
Member
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. i am surprised that this is rotational shifting
Contributor
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.
me too... |
||
| 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; | ||
|
|
@@ -2494,4 +2533,19 @@ mod tests { | |
| assert_eq!(result.as_ref(), &expected); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn bitwise_shift_scalar_test() -> Result<()> { | ||
| let input = Arc::new(Int32Array::from(vec![Some(2), None, Some(4)])) as ArrayRef; | ||
| let module = ScalarValue::from(10i32); | ||
|
Contributor
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. Likewise, here a test for null handling might be good
Contributor
Author
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. Instead of doing unit test here, I did a simple SQL test which test the whole pipeline, and I found that DF doesn't handle nulls for bitwise operators. Fixed in: |
||
| let mut result = bitwise_shift_left_scalar(&input, module.clone()).unwrap()?; | ||
|
|
||
| let expected = Int32Array::from(vec![Some(2048), None, Some(4096)]); | ||
| assert_eq!(result.as_ref(), &expected); | ||
|
|
||
| result = bitwise_shift_right_scalar(&result, module).unwrap()?; | ||
| assert_eq!(result.as_ref(), &input); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
To be similar with default SQL behaviour for null.