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 2018/10/28 17:30:45 UTC
[3/3] calcite git commit: [CALCITE-2617] Add a variant of
FilterProjectTransposeRule that can push down a Filter that contains
correlated variables (Stamatis Zampetakis)
[CALCITE-2617] Add a variant of FilterProjectTransposeRule that can push down a Filter that contains correlated variables (Stamatis Zampetakis)
The default instance of FilterProjectTransposeRule does not push down
a Filter that contains correlated variables, as before.
1. Add a new constructor accepting predicates for the Filter and
Project operands.
2. Remove the correlating condition check from the body of the rule
and make it an optional predicate.
3. Pass the new correlating condition predicate to the existing
constructors (retaining the same functionality for the generic
INSTANCE rule)
Note: Classes using the protected constructor may break if they rely
on the fact that the Filter is not pushed when it contains correlating
conditions.
Add tests for each variant (Julian Hyde)
Close apache/calcite#887
Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/6284d3c8
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/6284d3c8
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/6284d3c8
Branch: refs/heads/master
Commit: 6284d3c88d9c659e0a8ef3dd5cf4819133de7522
Parents: 4b51b5a
Author: Stamatis Zampetakis <za...@gmail.com>
Authored: Thu Oct 11 19:25:49 2018 +0200
Committer: Julian Hyde <jh...@apache.org>
Committed: Sun Oct 28 02:29:04 2018 -0700
----------------------------------------------------------------------
.../rel/rules/FilterProjectTransposeRule.java | 66 +++++++++++++++-----
.../apache/calcite/test/RelOptRulesTest.java | 49 +++++++++++++++
.../org/apache/calcite/test/RelOptTestBase.java | 2 +-
.../org/apache/calcite/test/RelOptRulesTest.xml | 66 ++++++++++++++++++++
4 files changed, 165 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/calcite/blob/6284d3c8/core/src/main/java/org/apache/calcite/rel/rules/FilterProjectTransposeRule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/FilterProjectTransposeRule.java b/core/src/main/java/org/apache/calcite/rel/rules/FilterProjectTransposeRule.java
index 3bdb5f5..ac0ad83 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/FilterProjectTransposeRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/FilterProjectTransposeRule.java
@@ -30,17 +30,26 @@ import org.apache.calcite.rex.RexUtil;
import org.apache.calcite.tools.RelBuilder;
import org.apache.calcite.tools.RelBuilderFactory;
+import java.util.function.Predicate;
+
/**
* Planner rule that pushes
- * a {@link org.apache.calcite.rel.logical.LogicalFilter}
- * past a {@link org.apache.calcite.rel.logical.LogicalProject}.
+ * a {@link org.apache.calcite.rel.core.Filter}
+ * past a {@link org.apache.calcite.rel.core.Project}.
*/
public class FilterProjectTransposeRule extends RelOptRule {
/** The default instance of
* {@link org.apache.calcite.rel.rules.FilterProjectTransposeRule}.
*
- * <p>It matches any kind of join or filter, and generates the same kind of
- * join and filter. */
+ * <p>It matches any kind of {@link org.apache.calcite.rel.core.Join} or
+ * {@link org.apache.calcite.rel.core.Filter}, and generates the same kind of
+ * Join and Filter.
+ *
+ * <p>It does not allow a Filter to be pushed past the Project if
+ * {@link RexUtil#containsCorrelation there is a correlation condition})
+ * anywhere in the Filter, since in some cases it can prevent a
+ * {@link org.apache.calcite.rel.core.Correlate} from being de-correlated.
+ */
public static final FilterProjectTransposeRule INSTANCE =
new FilterProjectTransposeRule(Filter.class, Project.class, true, true,
RelFactories.LOGICAL_BUILDER);
@@ -53,17 +62,46 @@ public class FilterProjectTransposeRule extends RelOptRule {
/**
* Creates a FilterProjectTransposeRule.
*
- * <p>If {@code filterFactory} is null, creates the same kind of filter as
- * matched in the rule. Similarly {@code projectFactory}.</p>
+ * <p>Equivalent to the rule created by
+ * {@link #FilterProjectTransposeRule(Class, Predicate, Class, Predicate, boolean, boolean, RelBuilderFactory)}
+ * with some default predicates that do not allow a filter to be pushed
+ * past the project if there is a correlation condition anywhere in the
+ * filter (since in some cases it can prevent a
+ * {@link org.apache.calcite.rel.core.Correlate} from being de-correlated).
*/
public FilterProjectTransposeRule(
Class<? extends Filter> filterClass,
Class<? extends Project> projectClass,
boolean copyFilter, boolean copyProject,
RelBuilderFactory relBuilderFactory) {
+ this(filterClass,
+ filter -> !RexUtil.containsCorrelation(filter.getCondition()),
+ projectClass, project -> true,
+ copyFilter, copyProject, relBuilderFactory);
+ }
+
+ /**
+ * Creates a FilterProjectTransposeRule.
+ *
+ * <p>If {@code copyFilter} is true, creates the same kind of Filter as
+ * matched in the rule, otherwise it creates a Filter using the RelBuilder
+ * obtained by the {@code relBuilderFactory}.
+ * Similarly for {@code copyProject}.
+ *
+ * <p>Defining predicates for the Filter (using {@code filterPredicate})
+ * and/or the Project (using {@code projectPredicate} allows making the rule
+ * more restrictive.
+ */
+ public <F extends Filter, P extends Project> FilterProjectTransposeRule(
+ Class<F> filterClass,
+ Predicate<? super F> filterPredicate,
+ Class<P> projectClass,
+ Predicate<? super P> projectPredicate,
+ boolean copyFilter, boolean copyProject,
+ RelBuilderFactory relBuilderFactory) {
this(
- operand(filterClass,
- operand(projectClass, any())),
+ operandJ(filterClass, null, filterPredicate,
+ operandJ(projectClass, null, projectPredicate, any())),
copyFilter, copyProject, relBuilderFactory);
}
@@ -73,7 +111,9 @@ public class FilterProjectTransposeRule extends RelOptRule {
RelFactories.FilterFactory filterFactory,
Class<? extends Project> projectClass,
RelFactories.ProjectFactory projectFactory) {
- this(filterClass, projectClass, filterFactory == null,
+ this(filterClass, filter -> !RexUtil.containsCorrelation(filter.getCondition()),
+ projectClass, project -> true,
+ filterFactory == null,
projectFactory == null,
RelBuilder.proto(filterFactory, projectFactory));
}
@@ -103,14 +143,6 @@ public class FilterProjectTransposeRule extends RelOptRule {
// it can be pushed down. For now we don't support this.
return;
}
-
- if (RexUtil.containsCorrelation(filter.getCondition())) {
- // If there is a correlation condition anywhere in the filter, don't
- // push this filter past project since in some cases it can prevent a
- // Correlate from being de-correlated.
- return;
- }
-
// convert the filter to one that references the child of the project
RexNode newCondition =
RelOptUtil.pushPastProject(filter.getCondition(), project);
http://git-wip-us.apache.org/repos/asf/calcite/blob/6284d3c8/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 a4475fb..e3a943a 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -34,6 +34,7 @@ import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.RelRoot;
import org.apache.calcite.rel.core.Aggregate;
import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.Filter;
import org.apache.calcite.rel.core.Intersect;
import org.apache.calcite.rel.core.JoinRelType;
import org.apache.calcite.rel.core.Minus;
@@ -1193,6 +1194,54 @@ public class RelOptRulesTest extends RelOptTestBase {
+ "unnest(t1.employees) as t2");
}
+ /** Tests that the default instance of {@link FilterProjectTransposeRule}
+ * does not push a Filter that contains a correlating variable.
+ *
+ * @see #testFilterProjectTranspose() */
+ @Test public void testFilterProjectTransposePreventedByCorrelation() {
+ final String sql = "SELECT e.empno\n"
+ + "FROM emp as e\n"
+ + "WHERE exists (\n"
+ + " SELECT *\n"
+ + " FROM (\n"
+ + " SELECT deptno * 2 AS twiceDeptno\n"
+ + " FROM dept) AS d\n"
+ + " WHERE e.deptno = d.twiceDeptno)";
+ HepProgram program = new HepProgramBuilder()
+ .addRuleInstance(FilterProjectTransposeRule.INSTANCE)
+ .build();
+ sql(sql)
+ .withDecorrelation(false)
+ .expand(true)
+ .with(program)
+ .checkUnchanged();
+ }
+
+ /** Tests a variant of {@link FilterProjectTransposeRule}
+ * that pushes a Filter that contains a correlating variable. */
+ @Test public void testFilterProjectTranspose() {
+ final String sql = "SELECT e.empno\n"
+ + "FROM emp as e\n"
+ + "WHERE exists (\n"
+ + " SELECT *\n"
+ + " FROM (\n"
+ + " SELECT deptno * 2 AS twiceDeptno\n"
+ + " FROM dept) AS d\n"
+ + " WHERE e.deptno = d.twiceDeptno)";
+ final FilterProjectTransposeRule filterProjectTransposeRule =
+ new FilterProjectTransposeRule(Filter.class, filter -> true,
+ Project.class, project -> true, true, true,
+ RelFactories.LOGICAL_BUILDER);
+ HepProgram program = new HepProgramBuilder()
+ .addRuleInstance(filterProjectTransposeRule)
+ .build();
+ sql(sql)
+ .withDecorrelation(false)
+ .expand(true)
+ .with(program)
+ .check();
+ }
+
private static final String NOT_STRONG_EXPR =
"case when e.sal < 11 then 11 else -1 * e.sal end";
http://git-wip-us.apache.org/repos/asf/calcite/blob/6284d3c8/core/src/test/java/org/apache/calcite/test/RelOptTestBase.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/RelOptTestBase.java b/core/src/test/java/org/apache/calcite/test/RelOptTestBase.java
index 1fb2e57..507edd2 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptTestBase.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptTestBase.java
@@ -219,7 +219,7 @@ abstract class RelOptTestBase extends SqlToRelTestBase {
diffRepos.assertEquals("planAfter", "${planAfter}", planAfter);
if (planBefore.equals(planAfter)) {
throw new AssertionError("Expected plan before and after is the same.\n"
- + "You must use unchanged=true or call checkPlanUnchanged");
+ + "You must use unchanged=true or call checkUnchanged");
}
}
SqlToRelTestBase.assertValid(r);
http://git-wip-us.apache.org/repos/asf/calcite/blob/6284d3c8/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 d99152e..a702384 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -2518,6 +2518,69 @@ LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$
]]>
</Resource>
</TestCase>
+ <TestCase name="testFilterProjectTranspose">
+ <Resource name="sql">
+ <![CDATA[SELECT e.empno
+FROM emp as e
+WHERE exists (
+ SELECT *
+ FROM (
+ SELECT deptno * 2 AS twiceDeptno
+ FROM dept) AS d
+ WHERE e.deptno = d.twiceDeptno)]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(EMPNO=[$0])
+ LogicalFilter(condition=[IS NOT NULL($9)])
+ LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{7}])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ LogicalAggregate(group=[{}], agg#0=[MIN($0)])
+ LogicalProject($f0=[true])
+ LogicalFilter(condition=[=($cor0.DEPTNO, $0)])
+ LogicalProject(TWICEDEPTNO=[*($0, 2)])
+ LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(EMPNO=[$0])
+ LogicalFilter(condition=[IS NOT NULL($9)])
+ LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{7}])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ LogicalAggregate(group=[{}], agg#0=[MIN($0)])
+ LogicalProject($f0=[true])
+ LogicalProject(TWICEDEPTNO=[*($0, 2)])
+ LogicalFilter(condition=[=($cor0.DEPTNO, *($0, 2))])
+ LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testFilterProjectTransposePreventedByCorrelation">
+ <Resource name="sql">
+ <![CDATA[SELECT e.empno
+FROM emp as e
+WHERE exists (
+ SELECT *
+ FROM (
+ SELECT deptno * 2 AS twiceDeptno
+ FROM dept) AS d
+ WHERE e.deptno = d.twiceDeptno)]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(EMPNO=[$0])
+ LogicalFilter(condition=[IS NOT NULL($9)])
+ LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{7}])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ LogicalAggregate(group=[{}], agg#0=[MIN($0)])
+ LogicalProject($f0=[true])
+ LogicalFilter(condition=[=($cor0.DEPTNO, $0)])
+ LogicalProject(TWICEDEPTNO=[*($0, 2)])
+ LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+]]>
+ </Resource>
+ </TestCase>
<TestCase name="testFilterRemoveIsNotDistinctFromRule">
<Resource name="planBefore">
<![CDATA[
@@ -8607,5 +8670,8 @@ LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
+ <Resource name="sql">
+ <![CDATA[select * from emp where MGR > 0 and case when MGR > 0 then deptno / MGR else null end > 1]]>
+ </Resource>
</TestCase>
</Root>