Repository: calcite
Updated Branches:
  refs/heads/master 4f1b5eff9 -> 8f202e918


Avoid simplification of cast(null as integer) to false in filter expressions 
broken by [CALCITE-2555]


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/8f202e91
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/8f202e91
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/8f202e91

Branch: refs/heads/master
Commit: 8f202e9189a6c1d0abbd8da09d7643b9efd4ad40
Parents: 4f1b5ef
Author: Vladimir Sitnikov <[email protected]>
Authored: Thu Sep 20 11:25:13 2018 +0300
Committer: Vladimir Sitnikov <[email protected]>
Committed: Thu Sep 20 11:27:53 2018 +0300

----------------------------------------------------------------------
 .../org/apache/calcite/rex/RexSimplify.java     |  3 +-
 .../org/apache/calcite/test/RexProgramTest.java |  7 +++++
 .../test/fuzzer/RexProgramFuzzyTest.java        | 29 +++++++++++++++++---
 3 files changed, 34 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/8f202e91/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
----------------------------------------------------------------------
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 9da8d4a..1f84dec 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
@@ -180,7 +180,8 @@ public class RexSimplify {
 
   private RexNode simplify_(RexNode e) {
     if (strong.isNull(e)) {
-      if (unknownAsFalse) {
+      // NULL integer must not be converted to FALSE even in unknownAsFalse 
mode
+      if (unknownAsFalse && e.getType().getSqlTypeName() == 
SqlTypeName.BOOLEAN) {
         return rexBuilder.makeLiteral(false);
       }
       return rexBuilder.makeNullLiteral(e.getType());

http://git-wip-us.apache.org/repos/asf/calcite/blob/8f202e91/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/RexProgramTest.java 
b/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
index bf17c9d..d73e533 100644
--- a/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
@@ -2120,6 +2120,13 @@ public class RexProgramTest extends 
RexProgramBuilderBase {
         "...", "...");
   }
 
+  @Test
+  public void simplifyNull() {
+    checkSimplify2(nullBool, "null", "false");
+    // null int must not be simplified to false
+    checkSimplify2(nullInt, "null", "null");
+  }
+
   /** Converts a map to a string, sorting on the string representation of its
    * keys. */
   private static String getString(ImmutableMap<RexNode, RexNode> map) {

http://git-wip-us.apache.org/repos/asf/calcite/blob/8f202e91/core/src/test/java/org/apache/calcite/test/fuzzer/RexProgramFuzzyTest.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/java/org/apache/calcite/test/fuzzer/RexProgramFuzzyTest.java 
b/core/src/test/java/org/apache/calcite/test/fuzzer/RexProgramFuzzyTest.java
index 159e88d..006cb9b 100644
--- a/core/src/test/java/org/apache/calcite/test/fuzzer/RexProgramFuzzyTest.java
+++ b/core/src/test/java/org/apache/calcite/test/fuzzer/RexProgramFuzzyTest.java
@@ -17,10 +17,13 @@
 package org.apache.calcite.test.fuzzer;
 
 import org.apache.calcite.plan.Strong;
+import org.apache.calcite.rex.RexLiteral;
 import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.rex.RexUtil;
 import org.apache.calcite.sql.SqlOperator;
 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
 import org.apache.calcite.test.RexProgramBuilderBase;
 import org.apache.calcite.util.ImmutableBitSet;
 
@@ -218,10 +221,18 @@ public class RexProgramFuzzyTest extends 
RexProgramBuilderBase {
     }
     if (STRONG.isNull(node)) {
       if (unknownAsFalse) {
-        if (!falseLiteral.equals(opt)) {
-          assertEquals(nodeToString(node)
-                  + " is always null, so it should simplify to FALSE " + uaf,
-              falseLiteral, opt);
+        if (node.getType().getSqlTypeName() == SqlTypeName.BOOLEAN) {
+          if (!falseLiteral.equals(opt)) {
+            assertEquals(nodeToString(node)
+                    + " is always null boolean, so it should simplify to FALSE 
" + uaf,
+                falseLiteral, opt);
+          }
+        } else {
+          if (!RexLiteral.isNullLiteral(opt)) {
+            assertEquals(nodeToString(node)
+                    + " is always null (non boolean), so it should simplify to 
NULL " + uaf,
+                rexBuilder.makeNullLiteral(node.getType()), opt);
+          }
         }
       } else {
         if (!RexUtil.isNull(opt)) {
@@ -231,6 +242,16 @@ public class RexProgramFuzzyTest extends 
RexProgramBuilderBase {
         }
       }
     }
+    if (opt.getType().isNullable() && !node.getType().isNullable()) {
+      fail(nodeToString(node) + " had non-nullable type " + opt.getType()
+          + ", and it was optimized to " + nodeToString(opt)
+          + " that has nullable type " + opt.getType()
+          + ", " + uaf);
+    }
+    if (!SqlTypeUtil.equalSansNullability(typeFactory, node.getType(), 
opt.getType())) {
+      assertEquals(nodeToString(node) + " has different type after 
simplification to "
+          + nodeToString(opt), node.getType(), opt.getType());
+    }
   }
 
   private static String nodeToString(RexNode node) {

Reply via email to