diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/PushDownProjectThroughSemiJoin.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/PushDownProjectThroughSemiJoin.java index 0629092f45..66735d39b9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/PushDownProjectThroughSemiJoin.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/PushDownProjectThroughSemiJoin.java @@ -38,6 +38,7 @@ import java.util.stream.Collectors; /** * Rule for pushdown project through left-semi/anti join * Just push down project inside join to avoid to push the top of Join-Cluster. + * Note this rule is only used to push down project between join for join ordering. *
* Join Join
* | |
@@ -61,6 +62,9 @@ public class PushDownProjectThroughSemiJoin implements ExplorationRuleFactory {
.whenNot(j -> j.left().child().hasDistributeHint())
.then(topJoin -> {
LogicalProject> project = topJoin.left();
+ if (projectBothJoinSide(project)) {
+ return null;
+ }
Plan newLeft = pushdownProject(project);
return topJoin.withChildren(newLeft, topJoin.right());
}).toRule(RuleType.PUSH_DOWN_PROJECT_THROUGH_SEMI_JOIN_LEFT),
@@ -72,12 +76,27 @@ public class PushDownProjectThroughSemiJoin implements ExplorationRuleFactory {
.whenNot(j -> j.right().child().hasDistributeHint())
.then(topJoin -> {
LogicalProject> project = topJoin.right();
+ if (projectBothJoinSide(project)) {
+ return null;
+ }
Plan newRight = pushdownProject(project);
return topJoin.withChildren(topJoin.left(), newRight);
}).toRule(RuleType.PUSH_DOWN_PROJECT_THROUGH_SEMI_JOIN_RIGHT)
);
}
+ private boolean projectBothJoinSide(LogicalProject> project) {
+ // if project contains both side of join, it can't be pushed.
+ // such as:
+ // Project(l, null as r)
+ // ------ L(l) left anti join R(r)
+ LogicalJoin, ?> join = project.child();
+ Set projectOutput = project.getOutputSet();
+ boolean containLeft = join.left().getOutput().stream().anyMatch(projectOutput::contains);
+ boolean containRight = join.right().getOutput().stream().anyMatch(projectOutput::contains);
+ return containRight && containLeft;
+ }
+
private Plan pushdownProject(LogicalProject> project) {
LogicalJoin join = project.child();
Set conditionLeftSlots = CBOUtils.joinChildConditionSlots(join, true);
diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/join/PushDownProjectThroughSemiJoinTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/join/PushDownProjectThroughSemiJoinTest.java
index 8c5a29f0d5..bccd3056d3 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/join/PushDownProjectThroughSemiJoinTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/join/PushDownProjectThroughSemiJoinTest.java
@@ -22,6 +22,7 @@ import org.apache.doris.nereids.trees.expressions.Add;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
import org.apache.doris.nereids.trees.plans.JoinType;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
@@ -133,4 +134,35 @@ class PushDownProjectThroughSemiJoinTest implements MemoPatternMatchSupported {
)
);
}
+
+ @Test
+ void testProjectLiteral() {
+ List projectExprs = ImmutableList.of(
+ new Alias(new Add(scan1.getOutput().get(0), Literal.of(1)), "alias"),
+ new Alias(scan2.getOutput().get(0).getExprId(), new NullLiteral(), scan2.getOutput().get(0).getName())
+ );
+ // complex projection contain ti.id, which isn't in Join Condition
+ LogicalPlan plan = new LogicalPlanBuilder(scan1)
+ .join(scan2, JoinType.LEFT_SEMI_JOIN, Pair.of(1, 1))
+ .projectExprs(projectExprs)
+ .join(scan3, JoinType.INNER_JOIN, Pair.of(1, 1))
+ .build();
+ PlanChecker.from(MemoTestUtils.createConnectContext(), plan)
+ .applyExploration(PushDownProjectThroughSemiJoin.INSTANCE.buildRules())
+ .nonMatch(logicalJoin(logicalJoin(logicalProject(), group()), group()));
+
+ projectExprs = ImmutableList.of(
+ new Alias(new Add(scan2.getOutput().get(0), Literal.of(1)), "alias"),
+ new Alias(scan1.getOutput().get(0).getExprId(), new NullLiteral(), scan2.getOutput().get(0).getName())
+ );
+ // complex projection contain ti.id, which isn't in Join Condition
+ plan = new LogicalPlanBuilder(scan1)
+ .join(scan2, JoinType.RIGHT_SEMI_JOIN, Pair.of(1, 1))
+ .projectExprs(projectExprs)
+ .join(scan3, JoinType.INNER_JOIN, Pair.of(1, 1))
+ .build();
+ PlanChecker.from(MemoTestUtils.createConnectContext(), plan)
+ .applyExploration(PushDownProjectThroughSemiJoin.INSTANCE.buildRules())
+ .nonMatch(logicalJoin(logicalJoin(logicalProject(), group()), group()));
+ }
}