Repository: calcite Updated Branches: refs/heads/master 40503ff65 -> 56def3946
[CALCITE-2581] Avoid errors in simplifying "null and not (null or ...)" (pengzhiwei) closes #849 Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/56def394 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/56def394 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/56def394 Branch: refs/heads/master Commit: 56def3946d7e30706b2845464f7fc575c66e4694 Parents: 40503ff Author: pengzhiwei <[email protected]> Authored: Fri Sep 21 16:15:58 2018 +0800 Committer: Vladimir Sitnikov <[email protected]> Committed: Fri Sep 21 18:12:21 2018 +0300 ---------------------------------------------------------------------- .../java/org/apache/calcite/rex/RexSimplify.java | 7 +++++-- .../org/apache/calcite/test/RexProgramTest.java | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/56def394/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 c5ae228..33bdbeb 100644 --- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java +++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java @@ -782,16 +782,19 @@ public class RexSimplify { } notSatisfiableNullables.add(notDisjunction); } + if (notSatisfiableNullables != null) { + // Remove the intersection of "terms" and "notTerms" terms.removeAll(notSatisfiableNullables); + notTerms.removeAll(notSatisfiableNullables); + + // The intersection simplify to "null and x1 is null and x2 is null..." 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, terms); } // Add the NOT disjunctions back in. for (RexNode notDisjunction : notTerms) { http://git-wip-us.apache.org/repos/asf/calcite/blob/56def394/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 f991872..d7ca9dc 100644 --- a/core/src/test/java/org/apache/calcite/test/RexProgramTest.java +++ b/core/src/test/java/org/apache/calcite/test/RexProgramTest.java @@ -1479,6 +1479,24 @@ public class RexProgramTest extends RexProgramBuilderBase { and(gt(aRef, literal1), gt(aRef, literal10)), ">(?0.a, 10)"); + + // "null AND NOT(null OR x)" => "null AND NOT(x)" + checkSimplify2( + and(nullBool, + not(or(nullBool, vBool()))), + "AND(null, NOT(?0.bool0))", + "false"); + + // "x1 AND x2 AND x3 AND NOT(x1) AND NOT(x2) AND NOT(x0)" => + // "x3 AND null AND x1 IS NULL AND x2 IS NULL AND NOT(x0)" + checkSimplify2( + and(vBool(1), vBool(2), + vBool(3), not(vBool(1)), + not(vBool(2)), not(vBool())), + "AND(?0.bool3, null, IS NULL(?0.bool1)," + + " IS NULL(?0.bool2), NOT(?0.bool0))", + "false" + ); } @Test public void testSimplifyOrTerms() {
