Skip to content

feat: Support bitwise operations for unsigned integer types #5476

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

Merged
merged 7 commits into from
Mar 6, 2023
Merged

feat: Support bitwise operations for unsigned integer types #5476

merged 7 commits into from
Mar 6, 2023

Conversation

izveigor
Copy link
Contributor

@izveigor izveigor commented Mar 4, 2023

Which issue does this PR close?

Closes #5475

@github-actions github-actions bot added logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates labels Mar 4, 2023
@alamb alamb changed the title feat: add uint types for bitwise operations feat: Support bitwise operations for unsigned integer types Mar 4, 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.

This looks great @izveigor -- thank you. This PR looks really nice.

cc @liukun4515

I had a question about using unwrap

Also could you please add some "end to end" sql tests for this feature using sqllogictest ?

https://github.com/apache/arrow-datafusion/tree/main/datafusion/core/tests/sqllogictests

Specifically, added to https://github.com/apache/arrow-datafusion/blob/main/datafusion/core/tests/sqllogictests/test_files/scalar.slt?

(Int64, _) | (_, Int64) => Some(Int64),
(Int32, _) | (_, Int32) => Some(Int32),
(Int16, _) | (_, Int16) => Some(Int16),
(UInt64, _) | (_, UInt64) => Some(UInt64),
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 these rules look reasonable to me.

@@ -125,10 +140,42 @@ pub(crate) fn bitwise_shift_right(left: ArrayRef, right: ArrayRef) -> Result<Arr
binary_bitwise_array_op!(
left,
right,
|a: i64, b: i64| a.wrapping_shr(b as u32),
|a: i64, b: i64| a.wrapping_shr((b as u64).try_into().unwrap()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this function returns a result, I think it would be better if this function returned an error on overflow, rather than panicing (aka can we avoid calling the unwrap?)

binary_bitwise_array_op!(
left,
right,
|a: u8, b: u8| a.wrapping_shr(b as u32),
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL that wrapping_shr for i8 does in fact take a `u32 input 🤷 It looked strange to me but I double checked https://doc.rust-lang.org/std/primitive.i8.html#method.wrapping_shr

@github-actions github-actions bot added core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Mar 5, 2023
binary_bitwise_array_op!(
left,
right,
|a: u64, b: u64| a.wrapping_shr(b.try_into().unwrap()),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe, I don't know some aspects of programming in Rust, but I think it is impossible to return normal error through macros. Anyway, by the behavior of this function: https://github.com/apache/arrow-datafusion/blob/main/datafusion/physical-expr/src/expressions/binary/kernels.rs#L60, I consider that unwrap() in this situation is not a big problem.

Copy link
Contributor

Choose a reason for hiding this comment

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

but I think it is impossible to return normal error through macros.

I think it is possible (the function the macro is used in has to return a result)

Anyway, by the behavior of this function: https://github.com/apache/arrow-datafusion/blob/main/datafusion/physical-expr/src/expressions/binary/kernels.rs#L60, I consider that unwrap() in this situation is not a big problem.

I agree the pre-existing pattern is a reasonable justification for using unwrap.

Thank you

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.

Thanks again @izveigor

@alamb alamb merged commit a6f4a77 into apache:main Mar 6, 2023
@ursabot
Copy link

ursabot commented Mar 6, 2023

Benchmark runs are scheduled for baseline = 99ef989 and contender = a6f4a77. a6f4a77 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate enhancement New feature or request logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: add unsigned integer types for bitwise operations
4 participants