You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by vl...@apache.org on 2018/08/28 12:06:56 UTC

[1/2] calcite git commit: [CALCITE-2327] RexSimplify: AND(x, y, NOT(y)) ==> AND(x, null, IS NULL(y))

Repository: calcite
Updated Branches:
  refs/heads/master 9f7e565ff -> c8e91ea1d


[CALCITE-2327] RexSimplify: AND(x, y, NOT(y)) ==> AND(x, null, IS NULL(y))


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

Branch: refs/heads/master
Commit: c8e91ea1db1e519d9d73bbb8b08ee27f710cb1d4
Parents: 6b2b9ff
Author: Vladimir Sitnikov <si...@gmail.com>
Authored: Tue Aug 28 15:04:31 2018 +0300
Committer: Vladimir Sitnikov <si...@gmail.com>
Committed: Tue Aug 28 15:06:33 2018 +0300

----------------------------------------------------------------------
 .../org/apache/calcite/rex/RexSimplify.java     | 22 ++++++++++++--------
 .../org/apache/calcite/test/RexProgramTest.java | 20 ++++++++++++++++++
 2 files changed, 33 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/c8e91ea1/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 1274391..f5a99ef 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
@@ -718,7 +718,7 @@ public class RexSimplify {
     // Example #1. x AND y AND z AND NOT (x AND y)  - not satisfiable
     // Example #2. x AND y AND NOT (x AND y)        - not satisfiable
     // Example #3. x AND y AND NOT (x AND y AND z)  - may be satisfiable
-    RexNode bestNotDisjunction = null;
+    List<RexNode> notSatisfiableNullables = null;
     for (RexNode notDisjunction : notTerms) {
       final List<RexNode> terms2 = RelOptUtil.conjunctions(notDisjunction);
       if (!terms.containsAll(terms2)) {
@@ -732,17 +732,21 @@ public class RexSimplify {
       // x AND NOT(x) is UNKNOWN for NULL input
       // So we search for the shortest notDisjunction then convert
       // original expression to NULL and x IS NULL
-      if (bestNotDisjunction == null
-          || bestNotDisjunction.toString().length() > notDisjunction.toString().length()) {
-        bestNotDisjunction = notDisjunction;
+      if (notSatisfiableNullables == null) {
+        notSatisfiableNullables = new ArrayList<>();
       }
+      notSatisfiableNullables.add(notDisjunction);
     }
-    if (bestNotDisjunction != null) {
+    if (notSatisfiableNullables != null) {
+      terms.removeAll(notSatisfiableNullables);
+      terms.add(rexBuilder.makeNullLiteral(notSatisfiableNullables.get(0).getType()));
+      for (RexNode notSatisfiableNullable : notSatisfiableNullables) {
+        terms.add(
+            simplifyIs((RexCall)
+                rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, notSatisfiableNullable)));
+      }
       // NULL AND (x IS NULL)
-      return rexBuilder.makeCall(SqlStdOperatorTable.AND,
-          rexBuilder.makeNullLiteral(bestNotDisjunction.getType()),
-          simplifyIs(
-              (RexCall) rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, bestNotDisjunction)));
+      return rexBuilder.makeCall(SqlStdOperatorTable.AND, terms);
     }
     // Add the NOT disjunctions back in.
     for (RexNode notDisjunction : notTerms) {

http://git-wip-us.apache.org/repos/asf/calcite/blob/c8e91ea1/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 25c99e5..7d0afee 100644
--- a/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
@@ -581,6 +581,26 @@ public class RexProgramTest extends RexProgramBuilderBase {
 
   }
 
+  @Test public void xAndNotX() {
+    checkSimplify2(
+        and(vBool(), not(vBool()),
+            vBool(1), not(vBool(1))),
+        "AND(null, IS NULL(?0.bool0), IS NULL(?0.bool1))",
+        "false");
+
+    checkSimplify2(
+        and(vBool(),
+            vBool(1), not(vBool(1))),
+        "AND(?0.bool0, null, IS NULL(?0.bool1))",
+        "false");
+
+    checkSimplify2(
+        and(vBool(), not(vBool()),
+            vBoolNotNull(1), not(vBoolNotNull(1))),
+        "false",
+        "false");
+  }
+
   /** Unit test for {@link org.apache.calcite.rex.RexUtil#isLosslessCast(RexNode)}. */
   @Test public void testLosslessCast() {
     final RelDataType tinyIntType = typeFactory.createSqlType(SqlTypeName.TINYINT);


[2/2] calcite git commit: [CALCITE-2494] RexFieldAccess should implement equals/hashCode

Posted by vl...@apache.org.
[CALCITE-2494] RexFieldAccess should implement equals/hashCode


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

Branch: refs/heads/master
Commit: 6b2b9ffd7e620696e7e7cf34e38641d1817f0fd9
Parents: 9f7e565
Author: Vladimir Sitnikov <si...@gmail.com>
Authored: Tue Aug 28 12:29:18 2018 +0300
Committer: Vladimir Sitnikov <si...@gmail.com>
Committed: Tue Aug 28 15:06:33 2018 +0300

----------------------------------------------------------------------
 .../org/apache/calcite/rex/RexFieldAccess.java   | 19 +++++++++++++++++++
 .../org/apache/calcite/test/RexProgramTest.java  | 13 +++++++++++++
 2 files changed, 32 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/6b2b9ffd/core/src/main/java/org/apache/calcite/rex/RexFieldAccess.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rex/RexFieldAccess.java b/core/src/main/java/org/apache/calcite/rex/RexFieldAccess.java
index cf731d2..6de5548 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexFieldAccess.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexFieldAccess.java
@@ -91,6 +91,25 @@ public class RexFieldAccess extends RexNode {
   public RexNode getReferenceExpr() {
     return expr;
   }
+
+  @Override public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    RexFieldAccess that = (RexFieldAccess) o;
+
+    return field.equals(that.field) && expr.equals(that.expr);
+  }
+
+  @Override public int hashCode() {
+    int result = expr.hashCode();
+    result = 31 * result + field.hashCode();
+    return result;
+  }
 }
 
 // End RexFieldAccess.java

http://git-wip-us.apache.org/repos/asf/calcite/blob/6b2b9ffd/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 990afb6..25c99e5 100644
--- a/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
@@ -70,6 +70,8 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertThat;
 
 /**
@@ -1559,6 +1561,17 @@ public class RexProgramTest extends RexProgramBuilderBase {
         "false");
   }
 
+  @Test public void fieldAccessEqualsHashCode() {
+    assertEquals("vBool() instances should be equal", vBool(), vBool());
+    assertEquals("vBool().hashCode()", vBool().hashCode(), vBool().hashCode());
+    assertNotSame("vBool() is expected to produce new RexFieldAccess", vBool(), vBool());
+    assertNotEquals("vBool(0) != vBool(1)", vBool(0), vBool(1));
+  }
+
+  @Test public void testSimplifyDynamicParam() {
+    checkSimplify2(or(vBool(), vBool()), "?0.bool0", "?0.bool0");
+  }
+
   /** Unit test for
    * <a href="https://issues.apache.org/jira/browse/CALCITE-1289">[CALCITE-1289]
    * RexUtil.simplifyCase() should account for nullability</a>. */