Skip to content

[CALCITE-7639] Support bitwise right shift (>>) operator and RIGHTSHIFT function#5073

Open
nielspardon wants to merge 1 commit into
apache:mainfrom
nielspardon:CALCITE-7639-bitwise-right-shift
Open

[CALCITE-7639] Support bitwise right shift (>>) operator and RIGHTSHIFT function#5073
nielspardon wants to merge 1 commit into
apache:mainfrom
nielspardon:CALCITE-7639-bitwise-right-shift

Conversation

@nielspardon

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7109

Changes Proposed

Mirrors the left-shift work in CALCITE-7109 to close a gap in the umbrella issue CALCITE-5087.

Adds:

  • >> (SqlStdOperatorTable.BIT_RIGHT_SHIFT), a signed/arithmetic right shift (Java >>), symmetric to << (precedence 32, left-assoc, ReturnTypes.ARG0_NULLABLE, InferTypes.FIRST_KNOWN, and the same INTEGER / BINARY / UNSIGNED_NUMERIC operand families).
  • RIGHTSHIFT(x, n) scalar function, mirroring LEFTSHIFT.
  • SqlFunctions.rightShift(...) runtime overloads (int, long, byte[], ByteString, and the joou unsigned types), plus BuiltInMethod.RIGHT_SHIFT and the RexImpTable registrations for both operator and function.

Unlike <<, a greedy >> token cannot be added: the lexer would also match the two > that close nested angle-bracket types (e.g. MAP<INT, MAP<INT, INT>>), breaking type parsing. >> is therefore recognized in expression context as two adjacent > tokens via LOOKAHEAD(2) in BinaryRowOperator, leaving type parsing unchanged. Because there is no dedicated token, the SQL advisor advertises > rather than >>, so SqlAdvisorTest is not modified.

Scope is limited to >> (arithmetic). The logical/fill-zero >>> (RIGHT_SHIFT_FILL_ZERO) remains a possible follow-up.

The code in this PR was generated with the help of AI.

@nielspardon nielspardon force-pushed the CALCITE-7639-bitwise-right-shift branch from bb53cb6 to a6d5390 Compare July 3, 2026 15:28
@nielspardon nielspardon changed the title [CALCITE-7639] support bitwise right shift (>>) operator and RIGHTSHIFT function [CALCITE-7639] Support bitwise right shift (>>) operator and RIGHTSHIFT function Jul 3, 2026
…FT function

Mirrors the left-shift work in CALCITE-7109 to close a gap in the umbrella
issue CALCITE-5087. Adds:

  * `>>` (SqlStdOperatorTable.BIT_RIGHT_SHIFT), a signed/arithmetic right
    shift (Java `>>`), symmetric to `<<` (precedence 32, left-assoc,
    ReturnTypes.ARG0_NULLABLE, InferTypes.FIRST_KNOWN, and the same
    INTEGER / BINARY / UNSIGNED_NUMERIC operand families).
  * `RIGHTSHIFT(x, n)` scalar function, mirroring `LEFTSHIFT`.
  * SqlFunctions.rightShift(...) runtime overloads (int, long, byte[],
    ByteString, and the joou unsigned types), plus BuiltInMethod.RIGHT_SHIFT
    and the RexImpTable registrations for both operator and function.

Unlike `<<`, a greedy `>>` token cannot be added: the lexer would also match
the two `>` that close nested angle-bracket types (e.g. MAP<INT, MAP<INT,
INT>>), breaking type parsing. `>>` is therefore recognized in expression
context as two adjacent `>` tokens via LOOKAHEAD(2) in BinaryRowOperator,
leaving type parsing unchanged. Because there is no dedicated token, the SQL
advisor advertises `>` rather than `>>`, so SqlAdvisorTest is not modified.

Scope is limited to `>>` (arithmetic). The logical/fill-zero `>>>`
(RIGHT_SHIFT_FILL_ZERO) remains a possible follow-up.

Tests: SqlOperatorTest (operator + function forms), SqlFunctionsTest,
operator.iq, and the operator-precedence dump in SqlValidatorTest; docs in
site/_docs/reference.md.
@nielspardon nielspardon force-pushed the CALCITE-7639-bitwise-right-shift branch from a6d5390 to d765854 Compare July 3, 2026 15:44
@sonarqubecloud

sonarqubecloud Bot commented Jul 3, 2026

Copy link
Copy Markdown

@Dwrite Dwrite left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM. This is consistent with the LEFTSHIFT operator introduced in #4478 (parser-level resolution with precedence 32). Nice symmetric completion of the feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants