This is an automated email from the ASF dual-hosted git repository.
xiong pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new 7352a0cba0 [CALCITE-6649] Enhance RelMdPredicates pull up predicate
from PROJECT
7352a0cba0 is described below
commit 7352a0cba06a46f99af0172bd4cc8611310bd0bc
Author: Xiong Duan <[email protected]>
AuthorDate: Tue Oct 29 08:41:32 2024 +0800
[CALCITE-6649] Enhance RelMdPredicates pull up predicate from PROJECT
---
.../org/apache/calcite/test/RelMetadataTest.java | 39 +++++++++++++
.../org/apache/calcite/test/RelOptRulesTest.java | 25 ++++++++
.../org/apache/calcite/test/RelOptRulesTest.xml | 67 ++++++++++++++++++++++
3 files changed, 131 insertions(+)
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 bbe599923d..3392f3fa91 100644
--- a/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
@@ -2934,6 +2934,45 @@ public class RelMetadataTest {
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
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 b0324902f1..8fd418224c 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -5910,6 +5910,31 @@ class RelOptRulesTest extends RelOptTestBase {
.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
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 25520355ae..90f22fee12 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -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>