Skip to content

Commit

Permalink
[SPARK-48168][SQL][FOLLOWUP] Fix bitwise shifting operator's precedence
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

After referencing both `C`, `MySQL`'s doc,
https://en.cppreference.com/w/c/language/operator_precedence
https://dev.mysql.com/doc/refman/8.0/en/operator-precedence.html

And doing some experiments on scala-shell

```scala
scala> 1 & 2 >> 1
val res0: Int = 1

scala> 2 >> 1 << 1
val res1: Int = 2

scala> 1 << 1 + 2
val res2: Int = 8
```

The suitable precedence for `<< >> >>>` is between '+/-' and '&' with a left-to-right associativity.
### Why are the changes needed?

bugfix

### Does this PR introduce _any_ user-facing change?

now, unreleased yet

### How was this patch tested?

new tests

### Was this patch authored or co-authored using generative AI tooling?
no

Closes apache#46753 from yaooqinn/SPARK-48168-F.

Authored-by: Kent Yao <[email protected]>
Signed-off-by: Kent Yao <[email protected]>
  • Loading branch information
yaooqinn committed May 27, 2024
1 parent 48a4bdb commit b526456
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -986,11 +986,11 @@ valueExpression
| operator=(MINUS | PLUS | TILDE) valueExpression #arithmeticUnary
| left=valueExpression operator=(ASTERISK | SLASH | PERCENT | DIV) right=valueExpression #arithmeticBinary
| left=valueExpression operator=(PLUS | MINUS | CONCAT_PIPE) right=valueExpression #arithmeticBinary
| left=valueExpression shiftOperator right=valueExpression #shiftExpression
| left=valueExpression operator=AMPERSAND right=valueExpression #arithmeticBinary
| left=valueExpression operator=HAT right=valueExpression #arithmeticBinary
| left=valueExpression operator=PIPE right=valueExpression #arithmeticBinary
| left=valueExpression comparisonOperator right=valueExpression #comparison
| left=valueExpression shiftOperator right=valueExpression #shiftExpression
;

shiftOperator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,24 @@ select cast(null as map<int, array<int>>), 20181117 >> 2
-- !query analysis
Project [cast(null as map<int,array<int>>) AS NULL#x, (20181117 >> 2) AS (20181117 >> 2)#x]
+- OneRowRelation


-- !query
select 1 << 1 + 2 as plus_over_shift
-- !query analysis
Project [(1 << (1 + 2)) AS plus_over_shift#x]
+- OneRowRelation


-- !query
select 2 >> 1 << 1 as left_to_right
-- !query analysis
Project [((2 >> 1) << 1) AS left_to_right#x]
+- OneRowRelation


-- !query
select 1 & 2 >> 1 as shift_over_ampersand
-- !query analysis
Project [(1 & (2 >> 1)) AS shift_over_ampersand#x]
+- OneRowRelation
6 changes: 5 additions & 1 deletion sql/core/src/test/resources/sql-tests/inputs/bitwise.sql
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,8 @@ SELECT 20181117 <<< 2;
SELECT 20181117 >>>> 2;
select cast(null as array<array<int>>), 20181117 >> 2;
select cast(null as array<array<int>>), 20181117 >>> 2;
select cast(null as map<int, array<int>>), 20181117 >> 2;
select cast(null as map<int, array<int>>), 20181117 >> 2;

select 1 << 1 + 2 as plus_over_shift; -- if correct, the result is 8. otherwise, 4
select 2 >> 1 << 1 as left_to_right; -- if correct, the result is 2. otherwise, 0
select 1 & 2 >> 1 as shift_over_ampersand; -- if correct, the result is 1. otherwise, 0
24 changes: 24 additions & 0 deletions sql/core/src/test/resources/sql-tests/results/bitwise.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,27 @@ select cast(null as map<int, array<int>>), 20181117 >> 2
struct<NULL:map<int,array<int>>,(20181117 >> 2):int>
-- !query output
NULL 5045279


-- !query
select 1 << 1 + 2 as plus_over_shift
-- !query schema
struct<plus_over_shift:int>
-- !query output
8


-- !query
select 2 >> 1 << 1 as left_to_right
-- !query schema
struct<left_to_right:int>
-- !query output
2


-- !query
select 1 & 2 >> 1 as shift_over_ampersand
-- !query schema
struct<shift_over_ampersand:int>
-- !query output
1

0 comments on commit b526456

Please sign in to comment.