This is an automated email from the ASF dual-hosted git repository.
rubenql pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/master by this push:
new c84dd53 [CALCITE-4287] AggregateJoinRemoveRule and
ProjectJoinRemoveRule are not fired if the last column of the join's left input
is referenced(Liya Fan)
c84dd53 is described below
commit c84dd53414ddb20fcd0817dd1f064812546e6acc
Author: liyafan82 <[email protected]>
AuthorDate: Sun Sep 27 17:43:13 2020 +0800
[CALCITE-4287] AggregateJoinRemoveRule and ProjectJoinRemoveRule are not
fired if the last column of the join's left input is referenced(Liya Fan)
---
.../calcite/rel/rules/AggregateJoinRemoveRule.java | 2 +-
.../calcite/rel/rules/ProjectJoinRemoveRule.java | 2 +-
.../org/apache/calcite/test/RelOptRulesTest.java | 24 ++++++++++++
.../org/apache/calcite/test/RelOptRulesTest.xml | 44 ++++++++++++++++++++++
4 files changed, 70 insertions(+), 2 deletions(-)
diff --git
a/core/src/main/java/org/apache/calcite/rel/rules/AggregateJoinRemoveRule.java
b/core/src/main/java/org/apache/calcite/rel/rules/AggregateJoinRemoveRule.java
index 41ca66a..c27c773 100644
---
a/core/src/main/java/org/apache/calcite/rel/rules/AggregateJoinRemoveRule.java
+++
b/core/src/main/java/org/apache/calcite/rel/rules/AggregateJoinRemoveRule.java
@@ -82,7 +82,7 @@ public class AggregateJoinRemoveRule
final Join join = call.rel(1);
boolean isLeftJoin = join.getJoinType() == JoinRelType.LEFT;
int lower = isLeftJoin
- ? join.getLeft().getRowType().getFieldCount() - 1 : 0;
+ ? join.getLeft().getRowType().getFieldCount() : 0;
int upper = isLeftJoin ? join.getRowType().getFieldCount()
: join.getLeft().getRowType().getFieldCount();
diff --git
a/core/src/main/java/org/apache/calcite/rel/rules/ProjectJoinRemoveRule.java
b/core/src/main/java/org/apache/calcite/rel/rules/ProjectJoinRemoveRule.java
index a0f81ed..335443a 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/ProjectJoinRemoveRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/ProjectJoinRemoveRule.java
@@ -76,7 +76,7 @@ public class ProjectJoinRemoveRule
final Join join = call.rel(1);
final boolean isLeftJoin = join.getJoinType() == JoinRelType.LEFT;
int lower = isLeftJoin
- ? join.getLeft().getRowType().getFieldCount() - 1 : 0;
+ ? join.getLeft().getRowType().getFieldCount() : 0;
int upper = isLeftJoin
? join.getRowType().getFieldCount()
: join.getLeft().getRowType().getFieldCount();
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 bafe0a0..39f0fc1 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -5234,6 +5234,19 @@ class RelOptRulesTest extends RelOptTestBase {
.check();
}
+ /** Similar to {@link #testAggregateJoinRemove3()} but with agg call
+ * referencing the last column of the left input. */
+ @Test void testAggregateJoinRemove11() {
+ final String sql = "select e.deptno, count(distinct e.slacker)\n"
+ + "from sales.emp e\n"
+ + "left outer join sales.dept d on e.deptno = d.deptno\n"
+ + "group by e.deptno";
+ sql(sql)
+ .withRule(CoreRules.AGGREGATE_PROJECT_MERGE,
+ CoreRules.AGGREGATE_JOIN_REMOVE)
+ .check();
+ }
+
/** Similar to {@link #testAggregateJoinRemove1()};
* Should remove the bottom join since the project uses column in the
* right input of bottom join. */
@@ -5336,6 +5349,17 @@ class RelOptRulesTest extends RelOptTestBase {
.checkUnchanged();
}
+ /** Similar to {@link #testAggregateJoinRemove4()};
+ * The project references the last column of the left input.
+ * The rule should be fired.*/
+ @Test void testProjectJoinRemove10() {
+ final String sql = "SELECT e.deptno, e.slacker\n"
+ + "FROM sales.emp e\n"
+ + "LEFT JOIN sales.dept d ON e.deptno = d.deptno";
+ sql(sql).withRule(CoreRules.PROJECT_JOIN_REMOVE)
+ .check();
+ }
+
@Test void testSwapOuterJoin() {
final HepProgram program = new HepProgramBuilder()
.addMatchLimit(1)
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 8d3a70a..1a6acdd 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -9887,6 +9887,29 @@ LogicalAggregate(group=[{7}], EXPR$1=[COUNT(DISTINCT
$10, $12)])
]]>
</Resource>
</TestCase>
+ <TestCase name="testAggregateJoinRemove11">
+ <Resource name="sql">
+ <![CDATA[select e.deptno, count(distinct e.slacker)
+ from sales.emp e
+ left outer join sales.dept d on e.deptno = d.deptno
+ group by e.deptno]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalAggregate(group=[{0}], EXPR$1=[COUNT(DISTINCT $1)])
+ LogicalProject(DEPTNO=[$7], SLACKER=[$8])
+ LogicalJoin(condition=[=($7, $9)], joinType=[left])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalAggregate(group=[{7}], EXPR$1=[COUNT(DISTINCT $8)])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ </TestCase>
<TestCase name="testProjectJoinRemove1">
<Resource name="sql">
<![CDATA[SELECT e.deptno, d2.deptno
@@ -10103,6 +10126,27 @@ LogicalProject(DEPTNO=[$9], NAME=[$1])
]]>
</Resource>
</TestCase>
+ <TestCase name="testProjectJoinRemove10">
+ <Resource name="sql">
+ <![CDATA[SELECT e.deptno, e.slacker
+FROM sales.emp e
+LEFT JOIN sales.dept d ON e.deptno = d.deptno]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(DEPTNO=[$7], SLACKER=[$8])
+ LogicalJoin(condition=[=($7, $9)], joinType=[left])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(DEPTNO=[$7], SLACKER=[$8])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ </TestCase>
<TestCase name="testReduceNullableCase2">
<Resource name="sql">
<![CDATA[SELECT deptno, ename, CASE WHEN 1=2 THEN substring(ename,
1, cast(2 as int)) ELSE NULL end from emp group by deptno, ename, case when 1=2
then substring(ename,1, cast(2 as int)) else null end]]>