-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-7639] Support bitwise right shift (>>) operator and RIGHTSHIFT function #5073
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
Open
nielspardon
wants to merge
13
commits into
apache:main
Choose a base branch
from
nielspardon:CALCITE-7639-bitwise-right-shift
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+544
−41
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d765854
[CALCITE-7639] Support bitwise right shift (>>) operator and RIGHTSHI…
nielspardon 479d691
[CALCITE-7639] Require adjacent '>' tokens for right shift and factor…
nielspardon 3fd6f5e
[CALCITE-7639] Extract SqlParserPos.adjacent helper for right-shift t…
nielspardon 1bec017
[CALCITE-7639] Simplify shift helpers: endsImmediatelyBefore, Math.fl…
nielspardon 4add3af
[CALCITE-7639] Use a single long shift-amount overload for the shift …
nielspardon 0d85f00
[CALCITE-7639] Normalize shift amounts with a JDK 8 compatible positi…
nielspardon 3191430
[CALCITE-7639] Right shift of BIGINT UNSIGNED sign-extends instead of…
nielspardon 0095417
[CALCITE-7639] Re-trigger CI after a transient build-website failure
nielspardon 7633feb
[CALCITE-7639] Clarify binary shift semantics in the reference docs
nielspardon 1c58b2e
[CALCITE-7639] Assert computed byte values in the shift unit tests
nielspardon d7fc15d
[CALCITE-7639] Drop redundant BINARY casts and add VARBINARY shift tests
nielspardon 0095b5a
[CALCITE-7639] Descope binary right shift pending endianness design (…
nielspardon dc399d6
[CALCITE-7639] Right-shift comment and shift-modulo docs are inaccurate
nielspardon 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Since LEFTSHIFT supports negative amounts, isn't RIGHTSHIFT(a, b) the same as LEFTSHIFT(a, -b)?
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.
Good question, but no — they aren't equivalent under the current (released) semantics. Counterexample, both values from the existing tests:
RIGHTSHIFT(8, 1)=4, whereasLEFTSHIFT(8, -1)=0.The reason is how the runtime handles the shift amount: it normalizes the magnitude with
((amount % w) + w) % w(wherewis the type's bit width, e.g. 32 forINTEGER) and uses only the amount's sign to pick the direction. So forLEFTSHIFT(a, -b)the amount is-b, and its normalized magnitude isw - (b mod w)— notb. In other wordsLEFTSHIFT(a, -b)shifts byw - b, not byb. Concretely forINTEGER,-1becomes a shift of31:LEFTSHIFT(8, -1)=8 >> 31=0, whileRIGHTSHIFT(8, 1)=8 >> 1=4. (BINARY differs again: thebyte[]path ignores the sign entirely and always shifts in its native direction.)That negative-amount behavior of
LEFTSHIFTshipped in 1.41.0/1.42.0 (CALCITE-7109), so making the equivalence hold would mean changing released semantics — a separate change, out of scope here.That said, your instinct points at real duplication:
leftShift/rightShiftdiffer only in which way the ternary points. Happy to factor the int/long/unsigned bodies through a shared private helper in a follow-up (or here) to cut that down, without touching behavior.