Skip to content

Commit 1050496

Browse files
jackwenerpull[bot]
authored andcommitted
[test](Nereids): add ut for PullUpProjectUnderApply (#24331)
1 parent ae4f918 commit 1050496

6 files changed

Lines changed: 107 additions & 26 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpCorrelatedFilterUnderApplyAggregateProject.java‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,10 @@ public Rule build() {
7474
}
7575
});
7676

77-
LogicalProject newProject = project.withProjectsAndChild(newProjects, filter.child());
78-
LogicalFilter newFilter = new LogicalFilter<>(filter.getConjuncts(), newProject);
79-
LogicalAggregate newAgg = agg.withChildren(ImmutableList.of(newFilter));
80-
return new LogicalApply<>(apply.getCorrelationSlot(), apply.getSubqueryExpr(),
81-
apply.getCorrelationFilter(), apply.getMarkJoinSlotReference(),
82-
apply.isNeedAddSubOutputToProjects(),
83-
apply.isInProject(), apply.left(), newAgg);
77+
LogicalProject<Plan> newProject = project.withProjectsAndChild(newProjects, filter.child());
78+
LogicalFilter<Plan> newFilter = new LogicalFilter<>(filter.getConjuncts(), newProject);
79+
LogicalAggregate<Plan> newAgg = agg.withChildren(ImmutableList.of(newFilter));
80+
return apply.withChildren(apply.left(), newAgg);
8481
}).toRule(RuleType.PULL_UP_CORRELATED_FILTER_UNDER_APPLY_AGGREGATE_PROJECT);
8582
}
8683
}

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpProjectUnderApply.java‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@
2626
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
2727
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
2828

29+
import com.google.common.base.Preconditions;
30+
2931
import java.util.ArrayList;
3032
import java.util.List;
3133

3234
/**
3335
* Adjust the order of Project and apply in correlated subqueries.
34-
*
36+
* <pre>
3537
* before:
3638
* apply
37-
* / \
39+
* / \
3840
* Input(output:b) Project(output:a)
3941
* |
4042
* child
@@ -43,8 +45,9 @@
4345
* Project(b,(if the Subquery is Scalar add 'a' as the output column))
4446
* |
4547
* apply
46-
* / \
47-
* Input(output:b) child
48+
* / \
49+
* Input(output:b) child
50+
* </pre>
4851
*/
4952
public class PullUpProjectUnderApply extends OneRewriteRuleFactory {
5053
@Override
@@ -55,13 +58,11 @@ public Rule build() {
5558
.whenNot(LogicalApply::alreadyExecutedEliminateFilter)
5659
.then(apply -> {
5760
LogicalProject<Plan> project = apply.right();
58-
LogicalApply newCorrelate = new LogicalApply<>(apply.getCorrelationSlot(), apply.getSubqueryExpr(),
59-
apply.getCorrelationFilter(), apply.getMarkJoinSlotReference(),
60-
apply.isNeedAddSubOutputToProjects(),
61-
apply.isInProject(), apply.left(), project.child());
62-
List<NamedExpression> newProjects = new ArrayList<>();
63-
newProjects.addAll(apply.left().getOutput());
61+
Plan newCorrelate = apply.withChildren(apply.left(), project.child());
62+
List<NamedExpression> newProjects = new ArrayList<>(apply.left().getOutput());
6463
if (apply.getSubqueryExpr() instanceof ScalarSubquery) {
64+
Preconditions.checkState(project.getProjects().size() == 1,
65+
"ScalarSubquery should only have one output column");
6566
newProjects.add(project.getProjects().get(0));
6667
}
6768
return project.withProjectsAndChild(newProjects, newCorrelate);

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ScalarApplyToJoin.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ private Plan correlatedToJoin(LogicalApply apply) {
8282
}
8383

8484
return new LogicalJoin<>(
85-
apply.isNeedAddSubOutputToProjects() ? JoinType.LEFT_OUTER_JOIN
86-
: JoinType.LEFT_SEMI_JOIN,
85+
apply.isNeedAddSubOutputToProjects() ? JoinType.LEFT_OUTER_JOIN : JoinType.LEFT_SEMI_JOIN,
8786
ExpressionUtils.EMPTY_CONDITION,
8887
ExpressionUtils.extractConjunction(correlationFilter.get()),
8988
JoinHint.NONE,

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalApply.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ public class LogicalApply<LEFT_CHILD_TYPE extends Plan, RIGHT_CHILD_TYPE extends
6161
// Whether adding the subquery's output to projects
6262
private final boolean needAddSubOutputToProjects;
6363

64-
/**
65-
* Constructor.
66-
*/
67-
public LogicalApply(Optional<GroupExpression> groupExpression,
64+
private LogicalApply(Optional<GroupExpression> groupExpression,
6865
Optional<LogicalProperties> logicalProperties,
6966
List<Expression> correlationSlot,
7067
SubqueryExpr subqueryExpr, Optional<Expression> correlationFilter,
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.rules.rewrite;
19+
20+
import org.apache.doris.nereids.exceptions.AnalysisException;
21+
import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
22+
import org.apache.doris.nereids.util.MemoPatternMatchSupported;
23+
import org.apache.doris.nereids.util.PlanChecker;
24+
import org.apache.doris.utframe.TestWithFeService;
25+
26+
import com.google.common.collect.ImmutableList;
27+
import org.junit.jupiter.api.Assertions;
28+
import org.junit.jupiter.api.Test;
29+
30+
import java.util.List;
31+
32+
class PullUpProjectUnderApplyTest extends TestWithFeService implements MemoPatternMatchSupported {
33+
@Override
34+
protected void runBeforeAll() throws Exception {
35+
createDatabase("test");
36+
connectContext.setDatabase("default_cluster:test");
37+
38+
createTables(
39+
"CREATE TABLE IF NOT EXISTS T (\n"
40+
+ " id bigint,\n"
41+
+ " score bigint,\n"
42+
+ " score_int int\n"
43+
+ ")\n"
44+
+ "DUPLICATE KEY(id)\n"
45+
+ "DISTRIBUTED BY HASH(id) BUCKETS 1\n"
46+
+ "PROPERTIES (\n"
47+
+ " \"replication_num\" = \"1\"\n"
48+
+ ")\n"
49+
);
50+
}
51+
52+
@Override
53+
protected void runBeforeEach() throws Exception {
54+
StatementScopeIdGenerator.clear();
55+
}
56+
57+
@Test
58+
void testPullUpProjectUnderApply() {
59+
List<String> testSql = ImmutableList.of(
60+
"select * from T as T1 where id = (select max(id) from T as T2 where T1.score = T2.score)",
61+
"select * from T as T1 where id = (select max(id) + 1 from T as T2 where T1.score = T2.score)"
62+
);
63+
64+
testSql.forEach(sql ->
65+
PlanChecker.from(connectContext)
66+
.analyze(sql)
67+
.applyTopDown(new PullUpProjectUnderApply())
68+
.printlnTree()
69+
.matchesNotCheck(
70+
logicalApply(
71+
logicalSubQueryAlias(),
72+
logicalAggregate()
73+
)
74+
)
75+
);
76+
}
77+
78+
@Test
79+
void testScalarTwoColumn() {
80+
String sql = "select * from T as T1 where id = (select max(id), score from T as T2 where T1.score = T2.score)";
81+
Assertions.assertThrows(AnalysisException.class, () -> PlanChecker.from(connectContext)
82+
.analyze(sql)
83+
.rewrite()
84+
);
85+
}
86+
}

fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PushdowFilterThroughWindowTest.java renamed to fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PushdownFilterThroughWindowTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@
4242

4343
import java.util.List;
4444

45-
public class PushdowFilterThroughWindowTest implements MemoPatternMatchSupported {
46-
private final LogicalOlapScan scan = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student,
45+
class PushdownFilterThroughWindowTest implements MemoPatternMatchSupported {
46+
private final LogicalOlapScan scan = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(),
47+
PlanConstructor.student,
4748
ImmutableList.of(""));
4849

4950
@Test
50-
public void pushDownFilterThroughWindowTest() {
51+
void pushDownFilterThroughWindowTest() {
5152
ConnectContext context = MemoTestUtils.createConnectContext();
5253
NamedExpression age = scan.getOutput().get(3).toSlot();
5354
List<Expression> partitionKeyList = ImmutableList.of(age);

0 commit comments

Comments
 (0)