From 6ecf3a5b31f4dae08ef1c07aa7ded30341371b1f Mon Sep 17 00:00:00 2001 From: Mihai Budiu Date: Mon, 18 Nov 2024 12:02:55 -0800 Subject: [PATCH] Fix bug for short-to-int unary minus Signed-off-by: Mihai Budiu --- .../main/java/org/apache/calcite/runtime/SqlFunctions.java | 2 +- core/src/test/resources/sql/cast.iq | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java index 0021cabf659..a5f03669dac 100644 --- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java +++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java @@ -2516,7 +2516,7 @@ public static byte checkedUnaryMinus(byte b) { } public static short checkedUnaryMinus(short b) { - return intToShort(b); + return intToShort(-b); } public static int checkedUnaryMinus(int b) { diff --git a/core/src/test/resources/sql/cast.iq b/core/src/test/resources/sql/cast.iq index 4e25fcb2eb3..4ab5e9a5e3f 100644 --- a/core/src/test/resources/sql/cast.iq +++ b/core/src/test/resources/sql/cast.iq @@ -53,6 +53,10 @@ select -2147483648 / -1; Caused by: java.lang.ArithmeticException !error +select -CAST(-32768 AS SMALLINT); +Caused by: java.lang.ArithmeticException: integer overflow: Value 32768 does not fit in a SMALLINT +!error + select CAST(32767 AS SMALLINT) + CAST(2 AS SMALLINT); Caused by: java.lang.ArithmeticException: integer overflow: Value 32769 does not fit in a SMALLINT !error