From 9e087622abe477ca1eb5b812745907da1a26ca46 Mon Sep 17 00:00:00 2001 From: jakevin Date: Sat, 1 Apr 2023 16:51:27 +0800 Subject: [PATCH] [fix](Nereids): fix JoinReorderContext in withXXX() of LogicalJoin. (#18299) --- .../org/apache/doris/nereids/memo/Memo.java | 2 +- .../join/LogicalJoinSemiJoinTranspose.java | 8 +-- .../LogicalJoinSemiJoinTransposeProject.java | 8 +-- .../exploration/join/OuterJoinAssoc.java | 4 +- .../join/OuterJoinAssocProject.java | 4 +- .../exploration/join/OuterJoinLAsscom.java | 4 +- .../join/OuterJoinLAsscomProject.java | 4 +- .../join/PushdownProjectThroughInnerJoin.java | 4 +- .../join/PushdownProjectThroughSemiJoin.java | 2 +- .../join/SemiJoinSemiJoinTranspose.java | 4 +- .../SemiJoinSemiJoinTransposeProject.java | 4 +- .../trees/plans/logical/LogicalJoin.java | 59 ++++++++----------- 12 files changed, 48 insertions(+), 59 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java index 7db2f86a68..7a8d62338d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java @@ -393,7 +393,7 @@ public class Memo { validateRewriteChildGroup(childGroup, targetGroup); childrenGroups.add(childGroup); } else { - childrenGroups.add(copyIn(child, null, true).correspondingExpression.getOwnerGroup()); + childrenGroups.add(doRewrite(child, null).correspondingExpression.getOwnerGroup()); } } return childrenGroups; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/LogicalJoinSemiJoinTranspose.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/LogicalJoinSemiJoinTranspose.java index c0b012fd12..5cfcb3c0b3 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/LogicalJoinSemiJoinTranspose.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/LogicalJoinSemiJoinTranspose.java @@ -51,8 +51,8 @@ public class LogicalJoinSemiJoinTranspose implements ExplorationRuleFactory { GroupPlan b = bottomJoin.right(); GroupPlan c = topJoin.right(); - Plan newBottomJoin = topJoin.withChildren(a, c); - return bottomJoin.withChildren(newBottomJoin, b); + Plan newBottomJoin = topJoin.withChildrenNoContext(a, c); + return bottomJoin.withChildrenNoContext(newBottomJoin, b); }).toRule(RuleType.LOGICAL_JOIN_LOGICAL_SEMI_JOIN_TRANSPOSE), logicalJoin(group(), logicalJoin()) @@ -67,8 +67,8 @@ public class LogicalJoinSemiJoinTranspose implements ExplorationRuleFactory { GroupPlan b = bottomJoin.left(); GroupPlan c = bottomJoin.right(); - Plan newBottomJoin = topJoin.withChildren(a, b); - return bottomJoin.withChildren(newBottomJoin, c); + Plan newBottomJoin = topJoin.withChildrenNoContext(a, b); + return bottomJoin.withChildrenNoContext(newBottomJoin, c); }).toRule(RuleType.LOGICAL_JOIN_LOGICAL_SEMI_JOIN_TRANSPOSE) ); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/LogicalJoinSemiJoinTransposeProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/LogicalJoinSemiJoinTransposeProject.java index 95064c7838..064b79dbb6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/LogicalJoinSemiJoinTransposeProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/LogicalJoinSemiJoinTransposeProject.java @@ -53,8 +53,8 @@ public class LogicalJoinSemiJoinTransposeProject implements ExplorationRuleFacto GroupPlan c = topJoin.right(); // Discard this project, because it is useless. - Plan newBottomJoin = topJoin.withChildren(a, c); - Plan newTopJoin = bottomJoin.withChildren(newBottomJoin, b); + Plan newBottomJoin = topJoin.withChildrenNoContext(a, c); + Plan newTopJoin = bottomJoin.withChildrenNoContext(newBottomJoin, b); return JoinReorderUtils.projectOrSelf(new ArrayList<>(topJoin.getOutput()), newTopJoin); }).toRule(RuleType.LOGICAL_JOIN_LOGICAL_SEMI_JOIN_TRANSPOSE_PROJECT), @@ -71,8 +71,8 @@ public class LogicalJoinSemiJoinTransposeProject implements ExplorationRuleFacto GroupPlan c = bottomJoin.right(); // Discard this project, because it is useless. - Plan newBottomJoin = topJoin.withChildren(a, b); - Plan newTopJoin = bottomJoin.withChildren(newBottomJoin, c); + Plan newBottomJoin = topJoin.withChildrenNoContext(a, b); + Plan newTopJoin = bottomJoin.withChildrenNoContext(newBottomJoin, c); return JoinReorderUtils.projectOrSelf(new ArrayList<>(topJoin.getOutput()), newTopJoin); }).toRule(RuleType.LOGICAL_JOIN_LOGICAL_SEMI_JOIN_TRANSPOSE_PROJECT) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinAssoc.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinAssoc.java index 370e92220e..6bb35baa88 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinAssoc.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinAssoc.java @@ -70,9 +70,9 @@ public class OuterJoinAssoc extends OneExplorationRuleFactory { * But because we have added eliminate_outer_rule, we don't need to consider this. */ - LogicalJoin newBottomJoin = (LogicalJoin) topJoin.withChildren(b, c); + LogicalJoin newBottomJoin = topJoin.withChildrenNoContext(b, c); newBottomJoin.getJoinReorderContext().copyFrom(bottomJoin.getJoinReorderContext()); - LogicalJoin newTopJoin = (LogicalJoin) bottomJoin.withChildren(a, newBottomJoin); + LogicalJoin newTopJoin = bottomJoin.withChildrenNoContext(a, newBottomJoin); newTopJoin.getJoinReorderContext().copyFrom(topJoin.getJoinReorderContext()); setReorderContext(newTopJoin, newBottomJoin); return newTopJoin; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinAssocProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinAssocProject.java index c86361f231..561f03998e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinAssocProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinAssocProject.java @@ -96,13 +96,13 @@ public class OuterJoinAssocProject extends OneExplorationRuleFactory { bProjects.addAll(OuterJoinLAsscomProject.forceToNullable(c.getOutputSet())); /* ********** new Plan ********** */ - LogicalJoin newBottomJoin = (LogicalJoin) topJoin.withChildren(b, c); + LogicalJoin newBottomJoin = topJoin.withChildrenNoContext(b, c); newBottomJoin.getJoinReorderContext().copyFrom(bottomJoin.getJoinReorderContext()); Plan left = JoinReorderUtils.projectOrSelf(aProjects, a); Plan right = JoinReorderUtils.projectOrSelf(bProjects, newBottomJoin); - LogicalJoin newTopJoin = (LogicalJoin) bottomJoin.withChildren(left, right); + LogicalJoin newTopJoin = bottomJoin.withChildrenNoContext(left, right); newTopJoin.getJoinReorderContext().copyFrom(topJoin.getJoinReorderContext()); OuterJoinAssoc.setReorderContext(newTopJoin, newBottomJoin); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinLAsscom.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinLAsscom.java index f2671958a0..51d7db4227 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinLAsscom.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinLAsscom.java @@ -71,12 +71,12 @@ public class OuterJoinLAsscom extends OneExplorationRuleFactory { GroupPlan b = bottomJoin.right(); GroupPlan c = topJoin.right(); - LogicalJoin newBottomJoin = (LogicalJoin) topJoin.withChildren(a, c); + LogicalJoin newBottomJoin = topJoin.withChildrenNoContext(a, c); newBottomJoin.getJoinReorderContext().copyFrom(bottomJoin.getJoinReorderContext()); newBottomJoin.getJoinReorderContext().setHasLAsscom(false); newBottomJoin.getJoinReorderContext().setHasCommute(false); - LogicalJoin newTopJoin = (LogicalJoin) bottomJoin.withChildren(newBottomJoin, b); + LogicalJoin newTopJoin = bottomJoin.withChildrenNoContext(newBottomJoin, b); newTopJoin.getJoinReorderContext().copyFrom(topJoin.getJoinReorderContext()); newTopJoin.getJoinReorderContext().setHasLAsscom(true); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinLAsscomProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinLAsscomProject.java index 96e77d7ab9..2b52cd9157 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinLAsscomProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/OuterJoinLAsscomProject.java @@ -99,7 +99,7 @@ public class OuterJoinLAsscomProject extends OneExplorationRuleFactory { aProjects.addAll(forceToNullable(c.getOutputSet())); /* ********** new Plan ********** */ - LogicalJoin newBottomJoin = (LogicalJoin) topJoin.withChildren(a, c); + LogicalJoin newBottomJoin = topJoin.withChildrenNoContext(a, c); newBottomJoin.getJoinReorderContext().copyFrom(bottomJoin.getJoinReorderContext()); newBottomJoin.getJoinReorderContext().setHasLAsscom(false); newBottomJoin.getJoinReorderContext().setHasCommute(false); @@ -107,7 +107,7 @@ public class OuterJoinLAsscomProject extends OneExplorationRuleFactory { Plan left = JoinReorderUtils.projectOrSelf(aProjects, newBottomJoin); Plan right = JoinReorderUtils.projectOrSelf(bProjects, b); - LogicalJoin newTopJoin = (LogicalJoin) bottomJoin.withChildren(left, right); + LogicalJoin newTopJoin = bottomJoin.withChildrenNoContext(left, right); newTopJoin.getJoinReorderContext().copyFrom(topJoin.getJoinReorderContext()); newTopJoin.getJoinReorderContext().setHasLAsscom(true); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/PushdownProjectThroughInnerJoin.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/PushdownProjectThroughInnerJoin.java index 6e9fa8f130..fb98df8408 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/PushdownProjectThroughInnerJoin.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/PushdownProjectThroughInnerJoin.java @@ -93,7 +93,7 @@ public class PushdownProjectThroughInnerJoin extends OneExplorationRuleFactory { Plan newLeft = JoinReorderUtils.projectOrSelf(newAProject.build(), join.left()); if (!rightContains) { - Plan newJoin = join.withChildren(newLeft, join.right()); + Plan newJoin = join.withChildrenNoContext(newLeft, join.right()); return JoinReorderUtils.projectOrSelf(new ArrayList<>(project.getOutput()), newJoin); } @@ -103,7 +103,7 @@ public class PushdownProjectThroughInnerJoin extends OneExplorationRuleFactory { bConditionSlots.stream().filter(slot -> !bProjectSlots.contains(slot)).forEach(newBProject::add); Plan newRight = JoinReorderUtils.projectOrSelf(newBProject.build(), join.right()); - Plan newJoin = join.withChildren(newLeft, newRight); + Plan newJoin = join.withChildrenNoContext(newLeft, newRight); return JoinReorderUtils.projectOrSelfInOrder(new ArrayList<>(project.getOutput()), newJoin); }).toRule(RuleType.PUSH_DOWN_PROJECT_THROUGH_INNER_JOIN); } 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 fa29bc46c3..b6316511da 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 @@ -63,7 +63,7 @@ public class PushdownProjectThroughSemiJoin extends OneExplorationRuleFactory { conditionLeftSlots.stream().filter(slot -> !projectUsedSlots.contains(slot)).forEach(newProject::add); Plan newLeft = JoinReorderUtils.projectOrSelf(newProject, join.left()); - Plan newJoin = join.withChildren(newLeft, join.right()); + Plan newJoin = join.withChildrenNoContext(newLeft, join.right()); return JoinReorderUtils.projectOrSelfInOrder(new ArrayList<>(project.getOutput()), newJoin); }).toRule(RuleType.PUSH_DOWN_PROJECT_THROUGH_SEMI_JOIN); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/SemiJoinSemiJoinTranspose.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/SemiJoinSemiJoinTranspose.java index 67c97e5788..7a58bbd6ae 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/SemiJoinSemiJoinTranspose.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/SemiJoinSemiJoinTranspose.java @@ -70,8 +70,8 @@ public class SemiJoinSemiJoinTranspose extends OneExplorationRuleFactory { GroupPlan b = bottomJoin.right(); GroupPlan c = topJoin.right(); - Plan newBottomJoin = topJoin.withChildren(a, c); - Plan newTopJoin = bottomJoin.withChildren(newBottomJoin, b); + Plan newBottomJoin = topJoin.withChildrenNoContext(a, c); + Plan newTopJoin = bottomJoin.withChildrenNoContext(newBottomJoin, b); return newTopJoin; }).toRule(RuleType.LOGICAL_SEMI_JOIN_SEMI_JOIN_TRANSPOSE); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/SemiJoinSemiJoinTransposeProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/SemiJoinSemiJoinTransposeProject.java index ecd255e01a..bc24162dec 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/SemiJoinSemiJoinTransposeProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/SemiJoinSemiJoinTransposeProject.java @@ -71,13 +71,13 @@ public class SemiJoinSemiJoinTransposeProject extends OneExplorationRuleFactory acProjects.add(slot); } }); - LogicalJoin newBottomSemi = (LogicalJoin) topSemi.withChildren(a, c); + LogicalJoin newBottomSemi = topSemi.withChildrenNoContext(a, c); newBottomSemi.getJoinReorderContext().copyFrom(bottomSemi.getJoinReorderContext()); newBottomSemi.getJoinReorderContext().setHasCommute(false); newBottomSemi.getJoinReorderContext().setHasLAsscom(false); LogicalProject acProject = new LogicalProject<>(Lists.newArrayList(acProjects), newBottomSemi); - LogicalJoin newTopSemi = (LogicalJoin) bottomSemi.withChildren(acProject, b); + LogicalJoin newTopSemi = bottomSemi.withChildrenNoContext(acProject, b); newTopSemi.getJoinReorderContext().copyFrom(topSemi.getJoinReorderContext()); newTopSemi.getJoinReorderContext().setHasLAsscom(true); return JoinReorderUtils.projectOrSelf(new ArrayList<>(topSemi.getOutput()), newTopSemi); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalJoin.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalJoin.java index 5d0112f23a..81f1e08b8e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalJoin.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalJoin.java @@ -98,9 +98,9 @@ public class LogicalJoin hashJoinConjuncts, List otherJoinConjuncts, JoinHint hint, Optional markJoinSlotReference, - LEFT_CHILD_TYPE leftChild, RIGHT_CHILD_TYPE rightChild, - JoinReorderContext joinReorderContext) { - super(PlanType.LOGICAL_JOIN, Optional.empty(), Optional.empty(), leftChild, rightChild); + Optional groupExpression, Optional logicalProperties, + LEFT_CHILD_TYPE leftChild, RIGHT_CHILD_TYPE rightChild, JoinReorderContext joinReorderContext) { + super(PlanType.LOGICAL_JOIN, groupExpression, logicalProperties, leftChild, rightChild); this.joinType = Objects.requireNonNull(joinType, "joinType can not be null"); this.hashJoinConjuncts = ImmutableList.copyOf(hashJoinConjuncts); this.otherJoinConjuncts = ImmutableList.copyOf(otherJoinConjuncts); @@ -168,10 +168,10 @@ public class LogicalJoin computeOutput() { return ImmutableList.builder() - .addAll(JoinUtils.getJoinOutput(joinType, left(), right())) - .addAll(isMarkJoin() + .addAll(JoinUtils.getJoinOutput(joinType, left(), right())) + .addAll(isMarkJoin() ? ImmutableList.of(markJoinSlotReference.get()) : ImmutableList.of()) - .build(); + .build(); } @Override @@ -242,74 +242,63 @@ public class LogicalJoin withChildren(List children) { Preconditions.checkArgument(children.size() == 2); - return new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, - markJoinSlotReference, children.get(0), - children.get(1), joinReorderContext); + return new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, markJoinSlotReference, + Optional.empty(), Optional.empty(), children.get(0), children.get(1), joinReorderContext); } @Override public LogicalJoin withGroupExpression(Optional groupExpression) { - LogicalJoin newJoin = new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, - markJoinSlotReference, - groupExpression, Optional.of(getLogicalProperties()), left(), right()); - newJoin.getJoinReorderContext().copyFrom(this.getJoinReorderContext()); - return newJoin; + return new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, markJoinSlotReference, + groupExpression, Optional.of(getLogicalProperties()), left(), right(), joinReorderContext); } @Override public LogicalJoin withLogicalProperties(Optional logicalProperties) { - LogicalJoin newJoin = new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, - markJoinSlotReference, - Optional.empty(), logicalProperties, left(), right()); - newJoin.getJoinReorderContext().copyFrom(this.getJoinReorderContext()); - return newJoin; + return new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, markJoinSlotReference, + Optional.empty(), logicalProperties, left(), right(), joinReorderContext); + } + + public LogicalJoin withChildrenNoContext(Plan left, Plan right) { + return new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, + markJoinSlotReference, left, right); } public LogicalJoin withHashJoinConjuncts(List hashJoinConjuncts) { return new LogicalJoin<>(joinType, hashJoinConjuncts, this.otherJoinConjuncts, hint, markJoinSlotReference, - left(), right(), joinReorderContext); + left(), right()); } public LogicalJoin withJoinConjuncts( List hashJoinConjuncts, List otherJoinConjuncts) { return new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, - hint, markJoinSlotReference, left(), right(), joinReorderContext); - } - - public LogicalJoin withHashJoinConjunctsAndChildren( - List hashJoinConjuncts, List children) { - Preconditions.checkArgument(children.size() == 2); - return new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, - markJoinSlotReference, children.get(0), - children.get(1), joinReorderContext); + hint, markJoinSlotReference, left(), right()); } public LogicalJoin withHashJoinConjunctsAndChildren( List hashJoinConjuncts, Plan left, Plan right) { Preconditions.checkArgument(children.size() == 2); return new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, - markJoinSlotReference, left, right, joinReorderContext); + markJoinSlotReference, left, right); } public LogicalJoin withConjunctsChildren(List hashJoinConjuncts, List otherJoinConjuncts, Plan left, Plan right) { return new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, markJoinSlotReference, left, - right, joinReorderContext); + right); } public LogicalJoin withJoinType(JoinType joinType) { return new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, - markJoinSlotReference, left(), right(), joinReorderContext); + markJoinSlotReference, left(), right()); } public LogicalJoin withTypeChildren(JoinType joinType, Plan left, Plan right) { return new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, - markJoinSlotReference, left, right, joinReorderContext); + markJoinSlotReference, left, right); } public LogicalJoin withOtherJoinConjuncts(List otherJoinConjuncts) { return new LogicalJoin<>(joinType, hashJoinConjuncts, otherJoinConjuncts, hint, - markJoinSlotReference, left(), right(), - joinReorderContext); + markJoinSlotReference, left(), right()); } }