From 7352a0cba06a46f99af0172bd4cc8611310bd0bc Mon Sep 17 00:00:00 2001 From: nobigo Date: Tue, 29 Oct 2024 08:41:32 +0800 Subject: [PATCH] [CALCITE-6649] Enhance RelMdPredicates pull up predicate from PROJECT --- .../apache/calcite/test/RelMetadataTest.java | 39 +++++++++++ .../apache/calcite/test/RelOptRulesTest.java | 25 +++++++ .../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 bbe599923dd..3392f3fa91e 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 @@ private void checkPredicates(RelOptCluster cluster, RelOptTable empTable, sortsAs("[IS NULL($0), IS NULL($1)]")); } + /** Test case for + * [CALCITE-6649] + * Enhance RelMdPredicates pull up predicate from PROJECT. */ + @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 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 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 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 pulledUpPredicates = inputSet.pulledUpPredicates; + assertThat(pulledUpPredicates, sortsAs("[]")); + } + /** Test case for * [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 b0324902f1d..8fd418224cc 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 @@ 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 * [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 25520355ae7..90f22fee123 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]]) +]]> + + + + + 2, comm = 2 from emp where comm = 2]]> + + + ($6, 2)], EXPR$1=[=($6, 2)]) + LogicalFilter(condition=[=($6, 2)]) + LogicalTableScan(table=[[CATALOG, SALES, EMP]]) +]]> + + + + + + + + 1 from emp where comm = 2 and empno = 1]]> + + + ($0, 1)]) + LogicalFilter(condition=[AND(=($6, 2), =($0, 1))]) + LogicalTableScan(table=[[CATALOG, SALES, EMP]]) +]]> + + + + + + + + + + + + + +