Skip to content

Commit

Permalink
[CALCITE-6649] Enhance RelMdPredicates pull up predicate from PROJECT
Browse files Browse the repository at this point in the history
  • Loading branch information
NobiGo committed Dec 26, 2024
1 parent 3e02a4a commit 7352a0c
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
39 changes: 39 additions & 0 deletions core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2934,6 +2934,45 @@ private void checkPredicates(RelOptCluster cluster, RelOptTable empTable,
sortsAs("[IS NULL($0), IS NULL($1)]"));
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6649">[CALCITE-6649]
* Enhance RelMdPredicates pull up predicate from PROJECT</a>. */
@Test void testPullUpPredicatesFromProject2() {
final String sql = "select comm <> 2, comm = 2 from emp where comm = 2";
final Project rel = (Project) sql(sql).toRel();
final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
RelOptPredicateList inputSet = mq.getPulledUpPredicates(rel);
ImmutableList<RexNode> pulledUpPredicates = inputSet.pulledUpPredicates;
assertThat(pulledUpPredicates, sortsAs("[]"));
}

@Test void testPullUpPredicatesFromProject3() {
final String sql = "select comm is null, comm is not null from emp where comm = 2";
final Project rel = (Project) sql(sql).toRel();
final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
RelOptPredicateList inputSet = mq.getPulledUpPredicates(rel);
ImmutableList<RexNode> pulledUpPredicates = inputSet.pulledUpPredicates;
assertThat(pulledUpPredicates, sortsAs("[=($0, false), =($1, true)]"));
}

@Test void testPullUpPredicatesFromProject4() {
final String sql = "select comm = 2, empno <> 1 from emp where comm = 2 and empno = 1";
final Project rel = (Project) sql(sql).toRel();
final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
RelOptPredicateList inputSet = mq.getPulledUpPredicates(rel);
ImmutableList<RexNode> pulledUpPredicates = inputSet.pulledUpPredicates;
assertThat(pulledUpPredicates, sortsAs("[]"));
}

@Test void testPullUpPredicatesFromProject5() {
final String sql = "select mgr=2, comm=2 from emp where mgr is null and empno = 1";
final Project rel = (Project) sql(sql).toRel();
final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
RelOptPredicateList inputSet = mq.getPulledUpPredicates(rel);
ImmutableList<RexNode> pulledUpPredicates = inputSet.pulledUpPredicates;
assertThat(pulledUpPredicates, sortsAs("[]"));
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6599">[CALCITE-6599]
* RelMdPredicates should pull up more predicates from VALUES
Expand Down
25 changes: 25 additions & 0 deletions core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5910,6 +5910,31 @@ private HepProgram getTransitiveProgram() {
.check();
}

@Test void testPullUpPredicatesFromUnionWithProject() {
final String sql = "select empno = null from emp where comm = 2\n"
+ "union all\n"
+ "select comm = 2 from emp where comm = 2";
sql(sql).withPre(getTransitiveProgram())
.withRule(CoreRules.FILTER_REDUCE_EXPRESSIONS,
CoreRules.PROJECT_REDUCE_EXPRESSIONS)
.check();
}

@Test void testPullUpPredicatesFromProject1() {
final String sql = "select comm <> 2, comm = 2 from emp where comm = 2";
sql(sql).withPre(getTransitiveProgram())
.withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS)
.check();
}

@Test void testPullUpPredicatesFromProject2() {
final String sql = "select comm = 2, empno <> 1 from emp where comm = 2 and empno = 1";
sql(sql).withPre(getTransitiveProgram())
.withRule(CoreRules.FILTER_REDUCE_EXPRESSIONS,
CoreRules.PROJECT_REDUCE_EXPRESSIONS)
.check();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-1995">[CALCITE-1995]
* Remove predicates from Filter if they can be proved to be always true or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9353,6 +9353,73 @@ LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$
LogicalProject(EMPNO=[10], ENAME=[$1], JOB=[$2], MGR=[null:INTEGER], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[7], SLACKER=[$8])
LogicalFilter(condition=[AND(=($7, 7), =($0, 10), IS NULL($3))])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testPullUpPredicatesFromProject1">
<Resource name="sql">
<![CDATA[select comm <> 2, comm = 2 from emp where comm = 2]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalProject(EXPR$0=[<>($6, 2)], EXPR$1=[=($6, 2)])
LogicalFilter(condition=[=($6, 2)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalProject(EXPR$0=[false], EXPR$1=[true])
LogicalFilter(condition=[=($6, 2)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testPullUpPredicatesFromProject2">
<Resource name="sql">
<![CDATA[select comm = 2, empno <> 1 from emp where comm = 2 and empno = 1]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalProject(EXPR$0=[=($6, 2)], EXPR$1=[<>($0, 1)])
LogicalFilter(condition=[AND(=($6, 2), =($0, 1))])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalProject(EXPR$0=[true], EXPR$1=[false])
LogicalFilter(condition=[AND(=($6, 2), =($0, 1))])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testPullUpPredicatesFromUnionWithProject">
<Resource name="sql">
<![CDATA[select empno = null from emp where comm = 2
union all
select comm = 2 from emp where comm = 2]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalUnion(all=[true])
LogicalProject(EXPR$0=[null:BOOLEAN])
LogicalFilter(condition=[=($6, 2)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalProject(EXPR$0=[=($6, 2)])
LogicalFilter(condition=[=($6, 2)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalUnion(all=[true])
LogicalProject(EXPR$0=[null:BOOLEAN])
LogicalFilter(condition=[=($6, 2)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalProject(EXPR$0=[true])
LogicalFilter(condition=[=($6, 2)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit 7352a0c

Please sign in to comment.