This is an automated email from the ASF dual-hosted git repository.

jakevin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new b044ff0556 [test](Nereids): add ut for PullUpProjectUnderApply (#24331)
b044ff0556 is described below

commit b044ff0556ad67c470dfb3a786251a76cee64a7a
Author: jakevin <[email protected]>
AuthorDate: Thu Sep 14 12:56:27 2023 +0800

    [test](Nereids): add ut for PullUpProjectUnderApply (#24331)
---
 ...CorrelatedFilterUnderApplyAggregateProject.java | 11 +--
 .../rules/rewrite/PullUpProjectUnderApply.java     | 21 +++---
 .../nereids/rules/rewrite/ScalarApplyToJoin.java   |  3 +-
 .../nereids/trees/plans/logical/LogicalApply.java  |  5 +-
 .../rules/rewrite/PullUpProjectUnderApplyTest.java | 86 ++++++++++++++++++++++
 ...t.java => PushdownFilterThroughWindowTest.java} |  7 +-
 6 files changed, 107 insertions(+), 26 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpCorrelatedFilterUnderApplyAggregateProject.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpCorrelatedFilterUnderApplyAggregateProject.java
index 21cf3ea7f8..e56f552f9f 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpCorrelatedFilterUnderApplyAggregateProject.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpCorrelatedFilterUnderApplyAggregateProject.java
@@ -74,13 +74,10 @@ public class 
PullUpCorrelatedFilterUnderApplyAggregateProject extends OneRewrite
                         }
                     });
 
-                    LogicalProject newProject = 
project.withProjectsAndChild(newProjects, filter.child());
-                    LogicalFilter newFilter = new 
LogicalFilter<>(filter.getConjuncts(), newProject);
-                    LogicalAggregate newAgg = 
agg.withChildren(ImmutableList.of(newFilter));
-                    return new LogicalApply<>(apply.getCorrelationSlot(), 
apply.getSubqueryExpr(),
-                            apply.getCorrelationFilter(), 
apply.getMarkJoinSlotReference(),
-                            apply.isNeedAddSubOutputToProjects(),
-                            apply.isInProject(), apply.left(), newAgg);
+                    LogicalProject<Plan> newProject = 
project.withProjectsAndChild(newProjects, filter.child());
+                    LogicalFilter<Plan> newFilter = new 
LogicalFilter<>(filter.getConjuncts(), newProject);
+                    LogicalAggregate<Plan> newAgg = 
agg.withChildren(ImmutableList.of(newFilter));
+                    return apply.withChildren(apply.left(), newAgg);
                 
}).toRule(RuleType.PULL_UP_CORRELATED_FILTER_UNDER_APPLY_AGGREGATE_PROJECT);
     }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpProjectUnderApply.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpProjectUnderApply.java
index fbe4844f74..25c17b8f18 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpProjectUnderApply.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpProjectUnderApply.java
@@ -26,15 +26,17 @@ import 
org.apache.doris.nereids.trees.plans.logical.LogicalApply;
 import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
 import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
 
+import com.google.common.base.Preconditions;
+
 import java.util.ArrayList;
 import java.util.List;
 
 /**
  * Adjust the order of Project and apply in correlated subqueries.
- *
+ * <pre>
  * before:
  *              apply
- *         /              \
+ *             /     \
  * Input(output:b)    Project(output:a)
  *                         |
  *                       child
@@ -43,8 +45,9 @@ import java.util.List;
  *          Project(b,(if the Subquery is Scalar add 'a' as the output column))
  *                  |
  *                apply
- *          /               \
- * Input(output:b)          child
+ *               /     \
+ * Input(output:b)      child
+ * </pre>
  */
 public class PullUpProjectUnderApply extends OneRewriteRuleFactory {
     @Override
@@ -55,13 +58,11 @@ public class PullUpProjectUnderApply extends 
OneRewriteRuleFactory {
                 .whenNot(LogicalApply::alreadyExecutedEliminateFilter)
                 .then(apply -> {
                     LogicalProject<Plan> project = apply.right();
-                    LogicalApply newCorrelate = new 
LogicalApply<>(apply.getCorrelationSlot(), apply.getSubqueryExpr(),
-                                apply.getCorrelationFilter(), 
apply.getMarkJoinSlotReference(),
-                                apply.isNeedAddSubOutputToProjects(),
-                                apply.isInProject(), apply.left(), 
project.child());
-                    List<NamedExpression> newProjects = new ArrayList<>();
-                    newProjects.addAll(apply.left().getOutput());
+                    Plan newCorrelate = apply.withChildren(apply.left(), 
project.child());
+                    List<NamedExpression> newProjects = new 
ArrayList<>(apply.left().getOutput());
                     if (apply.getSubqueryExpr() instanceof ScalarSubquery) {
+                        Preconditions.checkState(project.getProjects().size() 
== 1,
+                                "ScalarSubquery should only have one output 
column");
                         newProjects.add(project.getProjects().get(0));
                     }
                     return project.withProjectsAndChild(newProjects, 
newCorrelate);
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ScalarApplyToJoin.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ScalarApplyToJoin.java
index 6c10427215..a9f9cf448f 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ScalarApplyToJoin.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ScalarApplyToJoin.java
@@ -82,8 +82,7 @@ public class ScalarApplyToJoin extends OneRewriteRuleFactory {
         }
 
         return new LogicalJoin<>(
-                apply.isNeedAddSubOutputToProjects() ? JoinType.LEFT_OUTER_JOIN
-                        : JoinType.LEFT_SEMI_JOIN,
+                apply.isNeedAddSubOutputToProjects() ? 
JoinType.LEFT_OUTER_JOIN : JoinType.LEFT_SEMI_JOIN,
                 ExpressionUtils.EMPTY_CONDITION,
                 ExpressionUtils.extractConjunction(correlationFilter.get()),
                 JoinHint.NONE,
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalApply.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalApply.java
index ed408c0c3a..b4f25e9771 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalApply.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalApply.java
@@ -61,10 +61,7 @@ public class LogicalApply<LEFT_CHILD_TYPE extends Plan, 
RIGHT_CHILD_TYPE extends
     // Whether adding the subquery's output to projects
     private final boolean needAddSubOutputToProjects;
 
-    /**
-     * Constructor.
-     */
-    public LogicalApply(Optional<GroupExpression> groupExpression,
+    private LogicalApply(Optional<GroupExpression> groupExpression,
             Optional<LogicalProperties> logicalProperties,
             List<Expression> correlationSlot,
             SubqueryExpr subqueryExpr, Optional<Expression> correlationFilter,
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PullUpProjectUnderApplyTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PullUpProjectUnderApplyTest.java
new file mode 100644
index 0000000000..1c18dbe93f
--- /dev/null
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PullUpProjectUnderApplyTest.java
@@ -0,0 +1,86 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.rules.rewrite;
+
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
+import org.apache.doris.nereids.util.MemoPatternMatchSupported;
+import org.apache.doris.nereids.util.PlanChecker;
+import org.apache.doris.utframe.TestWithFeService;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+class PullUpProjectUnderApplyTest extends TestWithFeService implements 
MemoPatternMatchSupported {
+    @Override
+    protected void runBeforeAll() throws Exception {
+        createDatabase("test");
+        connectContext.setDatabase("default_cluster:test");
+
+        createTables(
+                "CREATE TABLE IF NOT EXISTS T (\n"
+                        + "    id bigint,\n"
+                        + "    score bigint,\n"
+                        + "    score_int int\n"
+                        + ")\n"
+                        + "DUPLICATE KEY(id)\n"
+                        + "DISTRIBUTED BY HASH(id) BUCKETS 1\n"
+                        + "PROPERTIES (\n"
+                        + "  \"replication_num\" = \"1\"\n"
+                        + ")\n"
+        );
+    }
+
+    @Override
+    protected void runBeforeEach() throws Exception {
+        StatementScopeIdGenerator.clear();
+    }
+
+    @Test
+    void testPullUpProjectUnderApply() {
+        List<String> testSql = ImmutableList.of(
+                "select * from T as T1 where id = (select max(id) from T as T2 
where T1.score = T2.score)",
+                "select * from T as T1 where id = (select max(id) + 1 from T 
as T2 where T1.score = T2.score)"
+        );
+
+        testSql.forEach(sql ->
+                PlanChecker.from(connectContext)
+                        .analyze(sql)
+                        .applyTopDown(new PullUpProjectUnderApply())
+                        .printlnTree()
+                        .matchesNotCheck(
+                                logicalApply(
+                                        logicalSubQueryAlias(),
+                                        logicalAggregate()
+                                )
+                        )
+        );
+    }
+
+    @Test
+    void testScalarTwoColumn() {
+        String sql = "select * from T as T1 where id = (select max(id), score 
from T as T2 where T1.score = T2.score)";
+        Assertions.assertThrows(AnalysisException.class, () -> 
PlanChecker.from(connectContext)
+                .analyze(sql)
+                .rewrite()
+        );
+    }
+}
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PushdowFilterThroughWindowTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PushdownFilterThroughWindowTest.java
similarity index 95%
rename from 
fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PushdowFilterThroughWindowTest.java
rename to 
fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PushdownFilterThroughWindowTest.java
index 6c8239c9a9..8d09426ce3 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PushdowFilterThroughWindowTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PushdownFilterThroughWindowTest.java
@@ -42,12 +42,13 @@ import org.junit.jupiter.api.Test;
 
 import java.util.List;
 
-public class PushdowFilterThroughWindowTest implements 
MemoPatternMatchSupported {
-    private final LogicalOlapScan scan = new 
LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), 
PlanConstructor.student,
+class PushdownFilterThroughWindowTest implements MemoPatternMatchSupported {
+    private final LogicalOlapScan scan = new 
LogicalOlapScan(StatementScopeIdGenerator.newRelationId(),
+            PlanConstructor.student,
             ImmutableList.of(""));
 
     @Test
-    public void pushDownFilterThroughWindowTest() {
+    void pushDownFilterThroughWindowTest() {
         ConnectContext context = MemoTestUtils.createConnectContext();
         NamedExpression age = scan.getOutput().get(3).toSlot();
         List<Expression> partitionKeyList = ImmutableList.of(age);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to