You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2016/10/20 23:25:46 UTC

calcite git commit: Further to [CALCITE-1448], flatten set-operators if the top is DISTINCT and bottom is ALL

Repository: calcite
Updated Branches:
  refs/heads/master 9f44aea5b -> ab25d36c7


Further to [CALCITE-1448], flatten set-operators if the top is DISTINCT and bottom is ALL


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

Branch: refs/heads/master
Commit: ab25d36c799ed915852d9f51230905bac26870f4
Parents: 9f44aea
Author: Julian Hyde <jh...@apache.org>
Authored: Thu Oct 20 15:15:47 2016 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Thu Oct 20 15:15:47 2016 -0700

----------------------------------------------------------------------
 .../calcite/rel/rules/UnionMergeRule.java       |  6 ++-
 .../apache/calcite/test/RelOptRulesTest.java    | 18 ++++++++-
 .../org/apache/calcite/test/RelOptRulesTest.xml | 39 ++++++++++++++++++++
 3 files changed, 60 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/ab25d36c/core/src/main/java/org/apache/calcite/rel/rules/UnionMergeRule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/UnionMergeRule.java b/core/src/main/java/org/apache/calcite/rel/rules/UnionMergeRule.java
index e1fc0ac..71865b3 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/UnionMergeRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/UnionMergeRule.java
@@ -105,8 +105,10 @@ public class UnionMergeRule extends RelOptRule {
       return;
     }
 
-    // Can only combine if both are ALL or both are DISTINCT (i.e. not ALL).
-    if (topOp.all != bottomOp.all) {
+    // Can only combine (1) if all operators are ALL,
+    // or (2) top operator is DISTINCT (i.e. not ALL).
+    // In case (2), all operators become DISTINCT.
+    if (topOp.all && !bottomOp.all) {
       return;
     }
 

http://git-wip-us.apache.org/repos/asf/calcite/blob/ab25d36c/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
index 2fd89f0..e0d4a46 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -805,11 +805,27 @@ public class RelOptRulesTest extends RelOptTestBase {
         .build();
 
     final String sql = "select * from emp where deptno = 10\n"
+        + "union\n"
+        + "select * from emp where deptno = 20\n"
+        + "union all\n"
+        + "select * from emp where deptno = 30\n";
+    sql(sql).with(program).checkUnchanged();
+  }
+
+  /** Tests that {@link UnionMergeRule} converts all inputs to DISTINCT
+   * if the top one is DISTINCT.
+   * (Since UNION is left-associative, the "top one" is the rightmost.) */
+  @Test public void testMergeUnionMixed2() throws Exception {
+    HepProgram program = new HepProgramBuilder()
+        .addRuleInstance(UnionMergeRule.INSTANCE)
+        .build();
+
+    final String sql = "select * from emp where deptno = 10\n"
         + "union all\n"
         + "select * from emp where deptno = 20\n"
         + "union\n"
         + "select * from emp where deptno = 30\n";
-    sql(sql).with(program).checkUnchanged();
+    sql(sql).with(program).check();
   }
 
   /** Tests that {@link UnionMergeRule} does nothing if its arguments have

http://git-wip-us.apache.org/repos/asf/calcite/blob/ab25d36c/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
----------------------------------------------------------------------
diff --git a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
index d3ccca6..2885f12 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -1631,6 +1631,31 @@ LogicalUnion(all=[false])
     <TestCase name="testMergeUnionMixed">
         <Resource name="sql">
             <![CDATA[select * from emp where deptno = 10
+union
+select * from emp where deptno = 20
+union all
+select * from emp where deptno = 30
+]]>
+        </Resource>
+        <Resource name="planBefore">
+            <![CDATA[
+LogicalUnion(all=[true])
+  LogicalUnion(all=[false])
+    LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+      LogicalFilter(condition=[=($7, 10)])
+        LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+    LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+      LogicalFilter(condition=[=($7, 20)])
+        LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+    LogicalFilter(condition=[=($7, 30)])
+      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+        </Resource>
+    </TestCase>
+    <TestCase name="testMergeUnionMixed2">
+        <Resource name="sql">
+            <![CDATA[select * from emp where deptno = 10
 union all
 select * from emp where deptno = 20
 union
@@ -1652,6 +1677,20 @@ LogicalUnion(all=[false])
       LogicalTableScan(table=[[CATALOG, SALES, EMP]])
 ]]>
         </Resource>
+        <Resource name="planAfter">
+            <![CDATA[
+LogicalUnion(all=[false])
+  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+    LogicalFilter(condition=[=($7, 10)])
+      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+    LogicalFilter(condition=[=($7, 20)])
+      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+    LogicalFilter(condition=[=($7, 30)])
+      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+        </Resource>
     </TestCase>
     <TestCase name="testNestedAggregates">
         <Resource name="sql">