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 bitwise shift left/right #4148

Merged
merged 3 commits into from
Apr 28, 2023
Merged

Conversation

Weijun-H
Copy link
Member

@Weijun-H Weijun-H commented Apr 27, 2023

Which issue does this PR close?

Closes #2741

Rationale for this change

Support bitwise_shift_right and bitwise_shift_left. And use 'as_usize' to ignore truncation.

What changes are included in this PR?

Are there any user-facing changes?

@Weijun-H Weijun-H changed the title feat: support bitwise shift left/right feat: support bitwise shift left/right Apr 27, 2023
@Weijun-H Weijun-H marked this pull request as draft April 27, 2023 17:46
@github-actions github-actions bot added the arrow Changes to the arrow crate label Apr 27, 2023
@Weijun-H Weijun-H marked this pull request as ready for review April 27, 2023 17:49
@@ -143,6 +177,24 @@ mod tests {
Ok(())
}

#[test]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it worth adding a test of what happens if you shift by more than the number of bits?

Copy link
Member Author

Choose a reason for hiding this comment

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

I add a test for it. It should be 0

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

This makes sense, as wrapping_shr would shift the position would be the remainder of the type. So wrapping_shr(3_i32, 32) == 3 and wrapping_shr(3_i32, 33) == 1

Copy link
Member Author

@Weijun-H Weijun-H Apr 28, 2023

Choose a reason for hiding this comment

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

Shift operations (<<, >>) on a value of width N can be passed a shift value >= N. It is unclear what behaviour should result from this, so the shift value is unconditionally masked to be modulo N to ensure that the argument is always in range.

I found the reason here https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md

@tustvold tustvold merged commit 67176f0 into apache:master Apr 28, 2023
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you @Weijun-H -- I can't wait to use this to simplify datafusion

use arrow_schema::ArrowError;
use num::traits::{WrappingShl, WrappingShr};
Copy link
Contributor

Choose a reason for hiding this comment

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

this is great -- when I looked into doing this in DataFusion I noticed these traits weren't in std -- 100% for finding them in num

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bitwise: support left/right shift using the native wrapping api
3 participants