This is an automated email from the ASF dual-hosted git repository.

mihaibudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new e9185e93a0 [CALCITE-7839] CAST(CAST(0 AS TINYINT) AS BOOLEAN) should 
not evaluate to TRUE
e9185e93a0 is described below

commit e9185e93a05a146321eb5a49492fa80ee157aff6
Author: Mihai Budiu <[email protected]>
AuthorDate: Mon Oct 5 14:43:54 2026 -0700

    [CALCITE-7839] CAST(CAST(0 AS TINYINT) AS BOOLEAN) should not evaluate to 
TRUE
    
    Signed-off-by: Mihai Budiu <[email protected]>
---
 .../main/java/org/apache/calcite/runtime/SqlFunctions.java   |  5 ++++-
 core/src/test/resources/sql/cast.iq                          | 12 ++++++++++++
 2 files changed, 16 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 36dc21358a..9a522661bb 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -5308,7 +5308,10 @@ public static boolean toBoolean(Number number) {
     if (number instanceof Float) {
       return ((Float) number).floatValue() != 0f;
     }
-    return !number.equals(0);
+    if (number instanceof BigInteger) {
+      return ((BigInteger) number).signum() != 0;
+    }
+    return number.longValue() != 0;
   }
 
   public static boolean toBoolean(Object o) {
diff --git a/core/src/test/resources/sql/cast.iq 
b/core/src/test/resources/sql/cast.iq
index a02f89188a..9bfa71ed5f 100644
--- a/core/src/test/resources/sql/cast.iq
+++ b/core/src/test/resources/sql/cast.iq
@@ -2024,6 +2024,18 @@ select cast(mod(deptno, 3) as BOOLEAN), mod(deptno, 3) 
from emp;
 
 !ok
 
+# Test case for [CALCITE-7839] CAST(CAST(0 AS TINYINT) AS BOOLEAN) should not 
evaluate to TRUE
+values (cast(cast(0 as tinyint) as boolean), cast(cast(0 as smallint) as 
boolean),
+  cast(cast(0 as bigint) as boolean), cast(cast(5 as bigint) as boolean));
++--------+--------+--------+--------+
+| EXPR$0 | EXPR$1 | EXPR$2 | EXPR$3 |
++--------+--------+--------+--------+
+| false  | false  | false  | true   |
++--------+--------+--------+--------+
+(1 row)
+
+!ok
+
 # Test case for [CALCITE-7653] ARRAY[NULL] produces uncompilable Java code
 values (cast(array[null] as integer array));
 +--------+

Reply via email to