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

mbudiu 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 df3de965ac [CALCITE-7296] RexSimplify should not simplify IS 
NULL(CAST(10/0 AS BIGINT))
df3de965ac is described below

commit df3de965acd358993911f6abb13c89bfd6eb3d77
Author: Thomas Rebele <[email protected]>
AuthorDate: Thu Nov 20 14:07:43 2025 +0100

    [CALCITE-7296] RexSimplify should not simplify IS NULL(CAST(10/0 AS BIGINT))
---
 .../java/org/apache/calcite/rex/RexSimplify.java   | 30 ++++++++++++----------
 .../org/apache/calcite/rex/RexProgramTest.java     |  5 ++++
 2 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java 
b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
index 3b0b04043b..bb7ccf7050 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
@@ -1153,20 +1153,21 @@ private RexNode simplifyIs(RexCall call, RexUnknownAs 
unknownAs) {
     if (!a.getType().isNullable() && isSafe) {
       return rexBuilder.makeLiteral(true);
     }
+    RexNode simplifiedResult = null;
     if (RexUtil.isLosslessCast(a)) {
-      if (!a.getType().isNullable()) {
-        return rexBuilder.makeLiteral(true);
-      }
-      return rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, 
RexUtil.removeCast(a));
+      a = RexUtil.removeCast(a);
+      // to keep this simplification, we must return IS NOT NULL(a),
+      // even if we cannot do anything else
+      simplifiedResult = rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, 
a);
     }
     if (predicates.pulledUpPredicates.contains(a)) {
       return rexBuilder.makeLiteral(true);
     }
     if (hasCustomNullabilityRules(a.getKind())) {
-      return null;
+      return simplifiedResult;
     }
     if (!isSafe) {
-      return rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, a);
+      return simplifiedResult;
     }
     switch (Strong.policy(a)) {
     case NOT_NULL:
@@ -1197,7 +1198,7 @@ private RexNode simplifyIs(RexCall call, RexUnknownAs 
unknownAs) {
       }
     case AS_IS:
     default:
-      return null;
+      return simplifiedResult;
     }
   }
 
@@ -1212,20 +1213,21 @@ private RexNode simplifyIs(RexCall call, RexUnknownAs 
unknownAs) {
     if (!a.getType().isNullable() && isSafe) {
       return rexBuilder.makeLiteral(false);
     }
+    RexNode simplifiedResult = null;
     if (RexUtil.isLosslessCast(a)) {
-      if (!a.getType().isNullable()) {
-        return rexBuilder.makeLiteral(false);
-      }
-      return rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, 
RexUtil.removeCast(a));
+      a = RexUtil.removeCast(a);
+      // to keep this simplification, we must return IS NULL(a),
+      // even if we cannot do anything else
+      simplifiedResult = rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, a);
     }
     if (RexUtil.isNull(a)) {
       return rexBuilder.makeLiteral(true);
     }
     if (hasCustomNullabilityRules(a.getKind())) {
-      return null;
+      return simplifiedResult;
     }
     if (!isSafe) {
-      return rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, a);
+      return simplifiedResult;
     }
     switch (Strong.policy(a)) {
     case NOT_NULL:
@@ -1246,7 +1248,7 @@ private RexNode simplifyIs(RexCall call, RexUnknownAs 
unknownAs) {
       return RexUtil.composeDisjunction(rexBuilder, operands, false);
     case AS_IS:
     default:
-      return null;
+      return simplifiedResult;
     }
   }
 
diff --git a/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java 
b/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
index 5e727d3005..877878aede 100644
--- a/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
+++ b/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
@@ -2776,6 +2776,11 @@ trueLiteral, literal(1),
     checkSimplify(
         isNotNull(div(vDecimalNotNull(), 
cast(literal(BigDecimal.valueOf(2.5)), intType))),
         "true");
+
+    checkSimplify(isNull(cast(div(vIntNotNull(), literal(0)), tBigInt())),
+        "IS NULL(/(?0.notNullInt0, 0))");
+    checkSimplify(isNotNull(cast(div(vIntNotNull(), literal(0)), tBigInt())),
+        "IS NOT NULL(/(?0.notNullInt0, 0))");
   }
 
   /**

Reply via email to