Repository: calcite
Updated Branches:
  refs/heads/master fb0703681 -> 47c49c9c6


[CALCITE-2189] RelMdAllPredicates fast bail out creates mismatch with 
RelMdTableReferences


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

Branch: refs/heads/master
Commit: 47c49c9c64e4ec9e6ff6d33f1c7917c7927e673a
Parents: fb07036
Author: Jesus Camacho Rodriguez <jcama...@apache.org>
Authored: Thu Feb 22 15:40:38 2018 -0800
Committer: Jesus Camacho Rodriguez <jcama...@apache.org>
Committed: Thu Feb 22 15:40:38 2018 -0800

----------------------------------------------------------------------
 .../rel/metadata/RelMdAllPredicates.java        | 10 ------
 .../apache/calcite/test/RelMetadataTest.java    | 37 ++++++++++++++++++++
 2 files changed, 37 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/47c49c9c/core/src/main/java/org/apache/calcite/rel/metadata/RelMdAllPredicates.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdAllPredicates.java 
b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdAllPredicates.java
index f5520cb..4430db4 100644
--- a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdAllPredicates.java
+++ b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdAllPredicates.java
@@ -172,11 +172,6 @@ public class RelMdAllPredicates
         // Bail out
         return null;
       }
-      // If it does not contain table references, nothing needs to be done
-      if (!RexUtil.containsTableInputRef(inputPreds.pulledUpPredicates)) {
-        newPreds = newPreds.union(rexBuilder, inputPreds);
-        continue;
-      }
       // Gather table references
       final Set<RelTableRef> tableRefs = mq.getTableReferences(input);
       if (input == join.getLeft()) {
@@ -260,11 +255,6 @@ public class RelMdAllPredicates
         // Bail out
         return null;
       }
-      // If it does not contain table references, nothing needs to be done
-      if (!RexUtil.containsTableInputRef(inputPreds.pulledUpPredicates)) {
-        newPreds = newPreds.union(rexBuilder, inputPreds);
-        continue;
-      }
       // Gather table references
       final Set<RelTableRef> tableRefs = mq.getTableReferences(input);
       if (i == 0) {

http://git-wip-us.apache.org/repos/asf/calcite/blob/47c49c9c/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java 
b/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
index b341159..580a196 100644
--- a/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
@@ -2063,6 +2063,43 @@ public class RelMetadataTest extends SqlToRelTestBase {
             + "[CATALOG, SALES, EMP].#2, [CATALOG, SALES, EMP].#3]"));
   }
 
+  @Test public void testAllPredicatesCrossJoinMultiTable() {
+    final String sql = "select x.sal from\n"
+        + "(select a.deptno, c.sal from (select * from emp limit 7) as a\n"
+        + "cross join (select * from dept limit 1) as b\n"
+        + "cross join (select * from emp where empno = 5 limit 2) as c) as x";
+    final RelNode rel = convertSql(sql);
+    final RelMetadataQuery mq = RelMetadataQuery.instance();
+    final Set<RelTableRef> tableReferences = 
Sets.newTreeSet(mq.getTableReferences(rel));
+    assertThat(tableReferences.toString(),
+        equalTo("[[CATALOG, SALES, DEPT].#0, "
+            + "[CATALOG, SALES, EMP].#0, "
+            + "[CATALOG, SALES, EMP].#1]"));
+    final RelOptPredicateList inputSet = mq.getAllPredicates(rel);
+    // Note that we reference [CATALOG, SALES, EMP].#1 rather than [CATALOG, 
SALES, EMP].#0
+    assertThat(inputSet.pulledUpPredicates.toString(),
+        equalTo("[true, =([CATALOG, SALES, EMP].#1.$0, 5), true]"));
+  }
+
+  @Test public void testAllPredicatesUnionMultiTable() {
+    final String sql = "select x.sal from\n"
+        + "(select a.deptno, a.sal from (select * from emp) as a\n"
+        + "union all select emp.deptno, emp.sal from emp\n"
+        + "union all select emp.deptno, emp.sal from emp where empno = 5) as 
x";
+    final RelNode rel = convertSql(sql);
+    final RelMetadataQuery mq = RelMetadataQuery.instance();
+    final Set<RelTableRef> tableReferences = 
Sets.newTreeSet(mq.getTableReferences(rel));
+    assertThat(tableReferences.toString(),
+        equalTo("[[CATALOG, SALES, EMP].#0, "
+            + "[CATALOG, SALES, EMP].#1, "
+            + "[CATALOG, SALES, EMP].#2]"));
+    // Note that we reference [CATALOG, SALES, EMP].#2 rather than
+    // [CATALOG, SALES, EMP].#0 or [CATALOG, SALES, EMP].#1
+    final RelOptPredicateList inputSet = mq.getAllPredicates(rel);
+    assertThat(inputSet.pulledUpPredicates.toString(),
+        equalTo("[=([CATALOG, SALES, EMP].#2.$0, 5)]"));
+  }
+
   private void checkNodeTypeCount(String sql, Map<Class<? extends RelNode>, 
Integer> expected) {
     final RelNode rel = convertSql(sql);
     final RelMetadataQuery mq = RelMetadataQuery.instance();

Reply via email to