Skip to content

Commit e5db49c

Browse files
author
xiaojun
committed
Update Overflow and negative check
1 parent 7b0967e commit e5db49c

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3227,18 +3227,30 @@ public static ByteString bitNot(ByteString b) {
32273227
return new ByteString(result);
32283228
}
32293229
public static int leftShift(int a, int b) {
3230+
if (b < 0) {
3231+
throw new IllegalArgumentException("Shift count must be non-negative");
3232+
}
32303233
return a << b;
32313234
}
32323235

32333236
public static long leftShift(long a, int b) {
3237+
if (b < 0) {
3238+
throw new IllegalArgumentException("Shift count must be non-negative");
3239+
}
32343240
return a << b;
32353241
}
32363242

32373243
public static long leftShift(int a, long b) {
3244+
if (b < 0) {
3245+
throw new IllegalArgumentException("Shift count must be non-negative");
3246+
}
32383247
return (long) a << b;
32393248
}
32403249

32413250
public static long leftShift(Long a, Long b) {
3251+
if (b < 0) {
3252+
throw new IllegalArgumentException("Shift count must be non-negative");
3253+
}
32423254
return a << b;
32433255
}
32443256

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16313,6 +16313,7 @@ private static void checkLogicalOrFunc(SqlOperatorFixture f) {
1631316313
assertEquals(2, call.getOperandList().size());
1631416314
}
1631516315

16316+
1631616317
@Test void testLeftShiftScalarFunc() {
1631716318
final SqlOperatorFixture f = fixture();
1631816319
f.setFor(SqlStdOperatorTable.LEFTSHIFT_OPERATOR, VmName.EXPAND);
@@ -16328,7 +16329,6 @@ private static void checkLogicalOrFunc(SqlOperatorFixture f) {
1632816329
f.checkScalar("-5 << 2", "-20", "INTEGER NOT NULL");
1632916330
f.checkScalar("-5 << 3", "-40", "INTEGER NOT NULL");
1633016331
f.checkScalar("CAST(-5 AS TINYINT) << CAST(2 AS TINYINT)", "-20", "TINYINT NOT NULL");
16331-
1633216332
// Verify return types
1633316333
f.checkType("CAST(2 AS TINYINT) << CAST(3 AS TINYINT)", "TINYINT NOT NULL");
1633416334
f.checkType("CAST(2 AS SMALLINT) << CAST(3 AS SMALLINT)", "SMALLINT NOT NULL");
@@ -16342,6 +16342,11 @@ private static void checkLogicalOrFunc(SqlOperatorFixture f) {
1634216342
f.checkNull("CAST(NULL AS INTEGER) << 5");
1634316343
f.checkNull("10 << CAST(NULL AS INTEGER)");
1634416344
f.checkNull("CAST(NULL AS INTEGER) << CAST(NULL AS INTEGER)");
16345+
f.checkScalar("1 << 64", "1", "INTEGER NOT NULL"); // INTEGER << shift: effective shift is shift % 32. 64 % 32 = 0.
16346+
f.checkScalar("CAST(1 AS BIGINT) << 64", "1", "BIGINT NOT NULL"); // BIGINT << shift: effective shift is shift % 64. 64 % 64 = 0.
16347+
f.checkScalar("CAST(1 AS BIGINT) << 65", "2", "BIGINT NOT NULL"); // BIGINT << shift: effective shift is shift % 64. 65 % 64 = 1.
16348+
f.checkFails("1 << -2 ",
16349+
"Shift count must be non-negative", true);
1634516350
}
1634616351

1634716352
@Test void testBitAndScalarFunc() {

0 commit comments

Comments
 (0)