Skip to content

Commit 4add3af

Browse files
committed
[CALCITE-7639] Use a single long shift-amount overload for the shift functions
Address review feedback on the inconsistent shift-amount overloads: the int-valued shift methods had both an int and a long shift-amount variant, while the long and unsigned variants only had an int shift amount, and no (long, long) overload existed at all (so an executed BIGINT >> BIGINT would fail to resolve at code generation). Make every leftShift/rightShift overload take a long shift amount. An int shift amount widens to long automatically during enumerable code generation, so a single amount type suffices for every value type; the redundant int-valued long-amount overloads are removed. Behaviour is unchanged at the SQL level (the result type still follows the first argument). * SqlFunctions: change the shift amount of every leftShift/rightShift overload from int to long, and drop the now-redundant leftShift(int, long) / rightShift(int, long) overloads. * BuiltInMethod: point LEFT_SHIFT/RIGHT_SHIFT at the (int, long) overloads. * SqlOperatorTest: add executed BIGINT-by-BIGINT cases for both the operator and function forms of left and right shift.
1 parent 1bec017 commit 4add3af

3 files changed

Lines changed: 22 additions & 42 deletions

File tree

core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3779,7 +3779,7 @@ private static ByteString binaryOperator(
37793779
* @param y the shift amount (positive: left shift, negative: right shift)
37803780
* @return the shifted integer
37813781
*/
3782-
public static int leftShift(int x, int y) {
3782+
public static int leftShift(int x, long y) {
37833783
int shift = Math.floorMod(y, 32); // normalize to 0~31
37843784
return y >= 0 ? x << shift : x >> shift; // arithmetic right shift
37853785
}
@@ -3793,23 +3793,11 @@ public static int leftShift(int x, int y) {
37933793
* @param y the shift amount
37943794
* @return the shifted long value
37953795
*/
3796-
public static long leftShift(long x, int y) {
3796+
public static long leftShift(long x, long y) {
37973797
int shift = Math.floorMod(y, 64); // normalize to 0~63
37983798
return y >= 0 ? x << shift : x >> shift;
37993799
}
38003800

3801-
/**
3802-
* Performs PostgresSQL-style bitwise shift on an int value with a long shift amount.
3803-
*
3804-
* @param x the int value to shift
3805-
* @param y the long shift amount
3806-
* @return the shifted value as long
3807-
*/
3808-
public static long leftShift(int x, long y) {
3809-
int shift = Math.floorMod(y, 32); // normalize to 0~31
3810-
return y >= 0 ? (long) x << shift : (long) x >> shift;
3811-
}
3812-
38133801
/**
38143802
* Performs PostgresSQL-style bitwise shift on a byte array.
38153803
* Positive shift: left shift.
@@ -3819,7 +3807,7 @@ public static long leftShift(int x, long y) {
38193807
* @param y the shift amount in bits
38203808
* @return the shifted byte array
38213809
*/
3822-
public static byte[] leftShift(byte[] bytes, int y) {
3810+
public static byte[] leftShift(byte[] bytes, long y) {
38233811
if (bytes.length == 0) {
38243812
return new byte[0];
38253813
}
@@ -3866,15 +3854,15 @@ public static byte[] leftShift(byte[] bytes, int y) {
38663854
* @param y the shift amount in bits
38673855
* @return shifted ByteString
38683856
*/
3869-
public static ByteString leftShift(ByteString bytes, int y) {
3857+
public static ByteString leftShift(ByteString bytes, long y) {
38703858
return new ByteString(leftShift(bytes.getBytes(), y));
38713859
}
38723860

38733861
/**
38743862
* Performs PostgresSQL-style bitwise shift on UByte.
38753863
* Overflow bits are masked to 8 bits.
38763864
*/
3877-
public static UByte leftShift(UByte x, int y) {
3865+
public static UByte leftShift(UByte x, long y) {
38783866
int shift = Math.floorMod(y, 8);
38793867
int val = x.byteValue() & 0xFF;
38803868
val = (y >= 0) ? (val << shift) & 0xFF : (val >> shift) & 0xFF;
@@ -3885,7 +3873,7 @@ public static UByte leftShift(UByte x, int y) {
38853873
* Performs PostgresSQL-style bitwise shift on UShort.
38863874
* Overflow bits are masked to 16 bits.
38873875
*/
3888-
public static UShort leftShift(UShort x, int y) {
3876+
public static UShort leftShift(UShort x, long y) {
38893877
int shift = Math.floorMod(y, 16);
38903878
int val = x.shortValue() & 0xFFFF;
38913879
val = (y >= 0) ? (val << shift) & 0xFFFF : (val >> shift) & 0xFFFF;
@@ -3896,7 +3884,7 @@ public static UShort leftShift(UShort x, int y) {
38963884
* Performs PostgresSQL-style bitwise shift on UInteger.
38973885
* Overflow bits are masked to 32 bits.
38983886
*/
3899-
public static UInteger leftShift(UInteger x, int y) {
3887+
public static UInteger leftShift(UInteger x, long y) {
39003888
int shift = Math.floorMod(y, 32);
39013889
long val = x.longValue() & 0xFFFFFFFFL;
39023890
val = (y >= 0) ? (val << shift) & 0xFFFFFFFFL : (val >> shift) & 0xFFFFFFFFL;
@@ -3907,7 +3895,7 @@ public static UInteger leftShift(UInteger x, int y) {
39073895
* Performs PostgresSQL-style bitwise shift on ULong.
39083896
* Overflow bits are masked to 64 bits (long shifts naturally truncate).
39093897
*/
3910-
public static ULong leftShift(ULong x, int y) {
3898+
public static ULong leftShift(ULong x, long y) {
39113899
int shift = Math.floorMod(y, 64);
39123900
long val = x.longValue();
39133901
val = (y >= 0) ? val << shift : val >> shift;
@@ -3921,7 +3909,7 @@ public static ULong leftShift(ULong x, int y) {
39213909
* @param y the shift amount (positive: right shift, negative: left shift)
39223910
* @return the shifted integer
39233911
*/
3924-
public static int rightShift(int x, int y) {
3912+
public static int rightShift(int x, long y) {
39253913
int shift = Math.floorMod(y, 32); // normalize to 0~31
39263914
return y >= 0 ? x >> shift : x << shift; // arithmetic right shift
39273915
}
@@ -3935,23 +3923,11 @@ public static int rightShift(int x, int y) {
39353923
* @param y the shift amount
39363924
* @return the shifted long value
39373925
*/
3938-
public static long rightShift(long x, int y) {
3926+
public static long rightShift(long x, long y) {
39393927
int shift = Math.floorMod(y, 64); // normalize to 0~63
39403928
return y >= 0 ? x >> shift : x << shift;
39413929
}
39423930

3943-
/**
3944-
* Performs PostgresSQL-style bitwise shift on an int value with a long shift amount.
3945-
*
3946-
* @param x the int value to shift
3947-
* @param y the long shift amount
3948-
* @return the shifted value as long
3949-
*/
3950-
public static long rightShift(int x, long y) {
3951-
int shift = Math.floorMod(y, 32); // normalize to 0~31
3952-
return y >= 0 ? (long) x >> shift : (long) x << shift;
3953-
}
3954-
39553931
/**
39563932
* Performs PostgresSQL-style bitwise shift on a byte array.
39573933
* Positive shift: right shift.
@@ -3961,7 +3937,7 @@ public static long rightShift(int x, long y) {
39613937
* @param y the shift amount in bits
39623938
* @return the shifted byte array
39633939
*/
3964-
public static byte[] rightShift(byte[] bytes, int y) {
3940+
public static byte[] rightShift(byte[] bytes, long y) {
39653941
if (bytes.length == 0) {
39663942
return new byte[0];
39673943
}
@@ -4008,15 +3984,15 @@ public static byte[] rightShift(byte[] bytes, int y) {
40083984
* @param y the shift amount in bits
40093985
* @return shifted ByteString
40103986
*/
4011-
public static ByteString rightShift(ByteString bytes, int y) {
3987+
public static ByteString rightShift(ByteString bytes, long y) {
40123988
return new ByteString(rightShift(bytes.getBytes(), y));
40133989
}
40143990

40153991
/**
40163992
* Performs PostgresSQL-style bitwise shift on UByte.
40173993
* Overflow bits are masked to 8 bits.
40183994
*/
4019-
public static UByte rightShift(UByte x, int y) {
3995+
public static UByte rightShift(UByte x, long y) {
40203996
int shift = Math.floorMod(y, 8);
40213997
int val = x.byteValue() & 0xFF;
40223998
val = (y >= 0) ? (val >> shift) & 0xFF : (val << shift) & 0xFF;
@@ -4027,7 +4003,7 @@ public static UByte rightShift(UByte x, int y) {
40274003
* Performs PostgresSQL-style bitwise shift on UShort.
40284004
* Overflow bits are masked to 16 bits.
40294005
*/
4030-
public static UShort rightShift(UShort x, int y) {
4006+
public static UShort rightShift(UShort x, long y) {
40314007
int shift = Math.floorMod(y, 16);
40324008
int val = x.shortValue() & 0xFFFF;
40334009
val = (y >= 0) ? (val >> shift) & 0xFFFF : (val << shift) & 0xFFFF;
@@ -4038,7 +4014,7 @@ public static UShort rightShift(UShort x, int y) {
40384014
* Performs PostgresSQL-style bitwise shift on UInteger.
40394015
* Overflow bits are masked to 32 bits.
40404016
*/
4041-
public static UInteger rightShift(UInteger x, int y) {
4017+
public static UInteger rightShift(UInteger x, long y) {
40424018
int shift = Math.floorMod(y, 32);
40434019
long val = x.longValue() & 0xFFFFFFFFL;
40444020
val = (y >= 0) ? (val >> shift) & 0xFFFFFFFFL : (val << shift) & 0xFFFFFFFFL;
@@ -4049,7 +4025,7 @@ public static UInteger rightShift(UInteger x, int y) {
40494025
* Performs PostgresSQL-style bitwise shift on ULong.
40504026
* Overflow bits are masked to 64 bits (long shifts naturally truncate).
40514027
*/
4052-
public static ULong rightShift(ULong x, int y) {
4028+
public static ULong rightShift(ULong x, long y) {
40534029
int shift = Math.floorMod(y, 64);
40544030
long val = x.longValue();
40554031
val = (y >= 0) ? val >> shift : val << shift;

core/src/main/java/org/apache/calcite/util/BuiltInMethod.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,8 @@ public enum BuiltInMethod {
705705
BIT_OR(SqlFunctions.class, "bitOr", long.class, long.class),
706706
BIT_XOR(SqlFunctions.class, "bitXor", long.class, long.class),
707707
BIT_NOT(SqlFunctions.class, "bitNot", long.class),
708-
LEFT_SHIFT(SqlFunctions.class, "leftShift", int.class, int.class),
709-
RIGHT_SHIFT(SqlFunctions.class, "rightShift", int.class, int.class),
708+
LEFT_SHIFT(SqlFunctions.class, "leftShift", int.class, long.class),
709+
RIGHT_SHIFT(SqlFunctions.class, "rightShift", int.class, long.class),
710710
MODIFIABLE_TABLE_GET_MODIFIABLE_COLLECTION(ModifiableTable.class,
711711
"getModifiableCollection"),
712712
SCANNABLE_TABLE_SCAN(ScannableTable.class, "scan", DataContext.class),

testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16958,6 +16958,7 @@ private static void checkLogicalOrFunc(SqlOperatorFixture f) {
1695816958
f.checkType("CAST(2 AS SMALLINT) << CAST(3 AS SMALLINT)", "SMALLINT NOT NULL");
1695916959
f.checkType("CAST(2 AS INTEGER) << CAST(3 AS INTEGER)", "INTEGER NOT NULL");
1696016960
f.checkType("CAST(2 AS BIGINT) << CAST(3 AS BIGINT)", "BIGINT NOT NULL");
16961+
f.checkScalar("CAST(2 AS BIGINT) << CAST(3 AS BIGINT)", "16", "BIGINT NOT NULL");
1696116962

1696216963
// === BigInt shifts with explicit BIGINT inputs ===
1696316964
f.checkScalar("CAST(1 AS BIGINT) << 62", BigInteger.ONE.shiftLeft(62).toString(),
@@ -17078,6 +17079,7 @@ private static void checkLogicalOrFunc(SqlOperatorFixture f) {
1707817079
f.checkType("LEFTSHIFT(CAST(2 AS SMALLINT), CAST(3 AS SMALLINT))", "SMALLINT NOT NULL");
1707917080
f.checkType("LEFTSHIFT(CAST(2 AS INTEGER), CAST(3 AS INTEGER))", "INTEGER NOT NULL");
1708017081
f.checkType("LEFTSHIFT(CAST(2 AS BIGINT), CAST(3 AS BIGINT))", "BIGINT NOT NULL");
17082+
f.checkScalar("LEFTSHIFT(CAST(2 AS BIGINT), CAST(3 AS BIGINT))", "16", "BIGINT NOT NULL");
1708117083

1708217084
// === BigInt shifts with explicit BIGINT inputs ===
1708317085
f.checkScalar("LEFTSHIFT(CAST(1 AS BIGINT), 62)",
@@ -17183,6 +17185,7 @@ private static void checkLogicalOrFunc(SqlOperatorFixture f) {
1718317185
f.checkType("CAST(8 AS SMALLINT) >> CAST(2 AS SMALLINT)", "SMALLINT NOT NULL");
1718417186
f.checkType("CAST(8 AS INTEGER) >> CAST(2 AS INTEGER)", "INTEGER NOT NULL");
1718517187
f.checkType("CAST(8 AS BIGINT) >> CAST(2 AS BIGINT)", "BIGINT NOT NULL");
17188+
f.checkScalar("CAST(8 AS BIGINT) >> CAST(2 AS BIGINT)", "2", "BIGINT NOT NULL");
1718617189

1718717190
// === BigInt shifts with explicit BIGINT inputs (arithmetic/sign-preserving) ===
1718817191
f.checkScalar("CAST(4611686018427387904 AS BIGINT) >> 62", "1", "BIGINT NOT NULL"); // 2^62
@@ -17276,6 +17279,7 @@ private static void checkLogicalOrFunc(SqlOperatorFixture f) {
1727617279
f.checkType("RIGHTSHIFT(CAST(8 AS SMALLINT), CAST(2 AS SMALLINT))", "SMALLINT NOT NULL");
1727717280
f.checkType("RIGHTSHIFT(CAST(8 AS INTEGER), CAST(2 AS INTEGER))", "INTEGER NOT NULL");
1727817281
f.checkType("RIGHTSHIFT(CAST(8 AS BIGINT), CAST(2 AS BIGINT))", "BIGINT NOT NULL");
17282+
f.checkScalar("RIGHTSHIFT(CAST(8 AS BIGINT), CAST(2 AS BIGINT))", "2", "BIGINT NOT NULL");
1727917283

1728017284
// === BigInt shifts with explicit BIGINT inputs ===
1728117285
f.checkScalar("RIGHTSHIFT(CAST(4611686018427387904 AS BIGINT), 62)", "1", "BIGINT NOT NULL");

0 commit comments

Comments
 (0)