diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/cascades/ApplyRuleJob.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/cascades/ApplyRuleJob.java index df58b4ed18..3e73850b01 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/cascades/ApplyRuleJob.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/cascades/ApplyRuleJob.java @@ -73,6 +73,9 @@ public class ApplyRuleJob extends Job { for (Plan plan : groupExpressionMatching) { List newPlans = rule.transform(plan, context.getCascadesContext()); for (Plan newPlan : newPlans) { + if (newPlan == plan) { + continue; + } CopyInResult result = context.getCascadesContext() .getMemo() .copyIn(newPlan, groupExpression.getOwnerGroup(), false); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLAsscom.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLAsscom.java index fa90adf630..d92aede5bb 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLAsscom.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLAsscom.java @@ -75,9 +75,6 @@ public class InnerJoinLAsscom extends OneExplorationRuleFactory { LogicalJoin newBottomJoin = topJoin.withConjunctsChildren(newBottomHashConjuncts, newBottomOtherConjuncts, a, c); - newBottomJoin.getJoinReorderContext().copyFrom(bottomJoin.getJoinReorderContext()); - newBottomJoin.getJoinReorderContext().setHasLAsscom(false); - newBottomJoin.getJoinReorderContext().setHasCommute(false); LogicalJoin newTopJoin = bottomJoin.withConjunctsChildren(newTopHashConjuncts, newTopOtherConjuncts, newBottomJoin, b); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLAsscomProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLAsscomProject.java index 1c73a41a94..2ca9c81591 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLAsscomProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLAsscomProject.java @@ -83,9 +83,6 @@ public class InnerJoinLAsscomProject extends OneExplorationRuleFactory { /* ********** new Plan ********** */ LogicalJoin newBottomJoin = topJoin.withConjunctsChildren(newBottomHashConjuncts, newBottomOtherConjuncts, a, c); - newBottomJoin.getJoinReorderContext().copyFrom(bottomJoin.getJoinReorderContext()); - newBottomJoin.getJoinReorderContext().setHasLAsscom(false); - newBottomJoin.getJoinReorderContext().setHasCommute(false); // merge newTopHashConjuncts newTopOtherConjuncts topJoin.getOutputExprIdSet() Set topUsedExprIds = new HashSet<>(topJoin.getOutputExprIdSet()); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLeftAssociate.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLeftAssociate.java index 13d4311198..d552f19b16 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLeftAssociate.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLeftAssociate.java @@ -88,8 +88,7 @@ public class InnerJoinLeftAssociate extends OneExplorationRuleFactory { newBottomHashJoinConjuncts, newBottomOtherJoinConjuncts, a, b); LogicalJoin newTopJoin = bottomJoin.withConjunctsChildren( newTopHashJoinConjuncts, newTopOtherJoinConjuncts, newBottomJoin, c); - setNewBottomJoinReorder(newBottomJoin, bottomJoin); - setNewTopJoinReorder(newTopJoin, topJoin); + newTopJoin.getJoinReorderContext().setHasLeftAssociate(true); return newTopJoin; }).toRule(RuleType.LOGICAL_INNER_JOIN_LEFT_ASSOCIATIVE); @@ -102,20 +101,4 @@ public class InnerJoinLeftAssociate extends OneExplorationRuleFactory { && !topJoin.getJoinReorderContext().hasRightAssociate() && !topJoin.getJoinReorderContext().hasExchange(); } - - /** Set JoinReorderContext */ - public static void setNewTopJoinReorder(LogicalJoin newTopJoin, LogicalJoin topJoin) { - newTopJoin.getJoinReorderContext().copyFrom(topJoin.getJoinReorderContext()); - newTopJoin.getJoinReorderContext().setHasLeftAssociate(true); - newTopJoin.getJoinReorderContext().setHasCommute(false); - } - - /** Set JoinReorderContext */ - public static void setNewBottomJoinReorder(LogicalJoin newBottomJoin, LogicalJoin bottomJoin) { - newBottomJoin.getJoinReorderContext().copyFrom(bottomJoin.getJoinReorderContext()); - newBottomJoin.getJoinReorderContext().setHasCommute(false); - newBottomJoin.getJoinReorderContext().setHasRightAssociate(false); - newBottomJoin.getJoinReorderContext().setHasLeftAssociate(false); - newBottomJoin.getJoinReorderContext().setHasExchange(false); - } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLeftAssociateProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLeftAssociateProject.java index 35effb34c6..bb395df201 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLeftAssociateProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLeftAssociateProject.java @@ -88,8 +88,7 @@ public class InnerJoinLeftAssociateProject extends OneExplorationRuleFactory { LogicalJoin newTopJoin = bottomJoin.withConjunctsChildren( newTopHashConjuncts, newTopOtherConjuncts, left, right); - InnerJoinLeftAssociate.setNewBottomJoinReorder(newBottomJoin, bottomJoin); - InnerJoinLeftAssociate.setNewTopJoinReorder(newTopJoin, topJoin); + newTopJoin.getJoinReorderContext().setHasLeftAssociate(true); return CBOUtils.projectOrSelf(ImmutableList.copyOf(topJoin.getOutput()), newTopJoin); }).toRule(RuleType.LOGICAL_INNER_JOIN_LEFT_ASSOCIATIVE_PROJECT); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinRightAssociate.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinRightAssociate.java index 5f0c88848f..760c4bc7b7 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinRightAssociate.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinRightAssociate.java @@ -86,8 +86,7 @@ public class InnerJoinRightAssociate extends OneExplorationRuleFactory { newBottomHashJoinConjuncts, newBottomOtherJoinConjuncts, b, c); LogicalJoin newTopJoin = bottomJoin.withConjunctsChildren(newTopHashJoinConjuncts, newTopOtherJoinConjuncts, a, newBottomJoin); - setNewBottomJoinReorder(newBottomJoin, bottomJoin); - setNewTopJoinReorder(newTopJoin, topJoin); + newTopJoin.getJoinReorderContext().setHasRightAssociate(true); return newTopJoin; }).toRule(RuleType.LOGICAL_INNER_JOIN_RIGHT_ASSOCIATIVE); @@ -100,20 +99,4 @@ public class InnerJoinRightAssociate extends OneExplorationRuleFactory { && !topJoin.getJoinReorderContext().hasLeftAssociate() && !topJoin.getJoinReorderContext().hasExchange(); } - - /** Set JoinReorderContext */ - public static void setNewTopJoinReorder(LogicalJoin newTopJoin, LogicalJoin topJoin) { - newTopJoin.getJoinReorderContext().copyFrom(topJoin.getJoinReorderContext()); - newTopJoin.getJoinReorderContext().setHasRightAssociate(true); - newTopJoin.getJoinReorderContext().setHasCommute(false); - } - - /** Set JoinReorderContext */ - public static void setNewBottomJoinReorder(LogicalJoin newBottomJoin, LogicalJoin bottomJoin) { - newBottomJoin.getJoinReorderContext().copyFrom(bottomJoin.getJoinReorderContext()); - newBottomJoin.getJoinReorderContext().setHasCommute(false); - newBottomJoin.getJoinReorderContext().setHasRightAssociate(false); - newBottomJoin.getJoinReorderContext().setHasLeftAssociate(false); - newBottomJoin.getJoinReorderContext().setHasExchange(false); - } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinRightAssociateProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinRightAssociateProject.java index 6b8d918af6..9e9dfcb100 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinRightAssociateProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinRightAssociateProject.java @@ -85,8 +85,7 @@ public class InnerJoinRightAssociateProject extends OneExplorationRuleFactory { LogicalJoin newTopJoin = bottomJoin.withConjunctsChildren( newTopHashConjuncts, newTopOtherConjuncts, left, right); - setNewBottomJoinReorder(newBottomJoin, bottomJoin); - setNewTopJoinReorder(newTopJoin, topJoin); + newTopJoin.getJoinReorderContext().setHasRightAssociate(true); return CBOUtils.projectOrSelf(ImmutableList.copyOf(topJoin.getOutput()), newTopJoin); }).toRule(RuleType.LOGICAL_INNER_JOIN_RIGHT_ASSOCIATIVE_PROJECT); @@ -101,24 +100,4 @@ public class InnerJoinRightAssociateProject extends OneExplorationRuleFactory { && !topJoin.getJoinReorderContext().hasLeftAssociate() && !topJoin.getJoinReorderContext().hasExchange(); } - - /** - * Set JoinReorderContext - */ - public static void setNewTopJoinReorder(LogicalJoin newTopJoin, LogicalJoin topJoin) { - newTopJoin.getJoinReorderContext().copyFrom(topJoin.getJoinReorderContext()); - newTopJoin.getJoinReorderContext().setHasRightAssociate(true); - newTopJoin.getJoinReorderContext().setHasCommute(false); - } - - /** - * Set JoinReorderContext - */ - public static void setNewBottomJoinReorder(LogicalJoin newBottomJoin, LogicalJoin bottomJoin) { - newBottomJoin.getJoinReorderContext().copyFrom(bottomJoin.getJoinReorderContext()); - newBottomJoin.getJoinReorderContext().setHasCommute(false); - newBottomJoin.getJoinReorderContext().setHasRightAssociate(false); - newBottomJoin.getJoinReorderContext().setHasLeftAssociate(false); - newBottomJoin.getJoinReorderContext().setHasExchange(false); - } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinCommute.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinCommute.java index c231640a79..f4c56fabd5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinCommute.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinCommute.java @@ -54,6 +54,7 @@ public class JoinCommute extends OneExplorationRuleFactory { public Rule build() { return logicalJoin() .when(join -> !justNonInner || !join.getJoinType().isInnerJoin()) + .when(join -> checkReorder(join)) .when(join -> check(swapType, join)) .whenNot(LogicalJoin::hasJoinHint) .whenNot(join -> joinOrderMatchBitmapRuntimeFilterOrder(join)) @@ -87,7 +88,12 @@ public class JoinCommute extends OneExplorationRuleFactory { return false; } - return !join.getJoinReorderContext().hasCommute() && !join.getJoinReorderContext().hasExchange(); + return true; + } + + private boolean checkReorder(LogicalJoin join) { + return !join.getJoinReorderContext().hasCommute() + && !join.getJoinReorderContext().hasExchange(); } public static boolean isNotBottomJoin(LogicalJoin join) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchange.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchange.java index fe57194463..08c01da35c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchange.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchange.java @@ -91,9 +91,7 @@ public class JoinExchange extends OneExplorationRuleFactory { LogicalJoin newTopJoin = new LogicalJoin<>(JoinType.INNER_JOIN, newTopJoinHashJoinConjuncts, newTopJoinOtherJoinConjuncts, JoinHint.NONE, newLeftJoin, newRightJoin); - setNewLeftJoinReorder(newLeftJoin, leftJoin); - setNewRightJoinReorder(newRightJoin, leftJoin); - setNewTopJoinReorder(newTopJoin, topJoin); + newTopJoin.getJoinReorderContext().setHasExchange(true); return newTopJoin; }).toRule(RuleType.LOGICAL_JOIN_EXCHANGE); @@ -113,27 +111,6 @@ public class JoinExchange extends OneExplorationRuleFactory { } } - public static void setNewTopJoinReorder(LogicalJoin newTopJoin, LogicalJoin topJoin) { - newTopJoin.getJoinReorderContext().copyFrom(topJoin.getJoinReorderContext()); - newTopJoin.getJoinReorderContext().setHasExchange(true); - } - - public static void setNewLeftJoinReorder(LogicalJoin newLeftJoin, LogicalJoin leftJoin) { - newLeftJoin.getJoinReorderContext().copyFrom(leftJoin.getJoinReorderContext()); - newLeftJoin.getJoinReorderContext().setHasCommute(false); - newLeftJoin.getJoinReorderContext().setHasLeftAssociate(false); - newLeftJoin.getJoinReorderContext().setHasRightAssociate(false); - newLeftJoin.getJoinReorderContext().setHasExchange(false); - } - - public static void setNewRightJoinReorder(LogicalJoin newRightJoin, LogicalJoin rightJoin) { - newRightJoin.getJoinReorderContext().copyFrom(rightJoin.getJoinReorderContext()); - newRightJoin.getJoinReorderContext().setHasCommute(false); - newRightJoin.getJoinReorderContext().setHasLeftAssociate(false); - newRightJoin.getJoinReorderContext().setHasRightAssociate(false); - newRightJoin.getJoinReorderContext().setHasExchange(false); - } - /** * split condition. */ diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchangeBothProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchangeBothProject.java index 0e5a010b1b..ebac66c31c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchangeBothProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchangeBothProject.java @@ -101,9 +101,7 @@ public class JoinExchangeBothProject extends OneExplorationRuleFactory { LogicalJoin newTopJoin = new LogicalJoin<>(JoinType.INNER_JOIN, newTopJoinHashJoinConjuncts, newTopJoinOtherJoinConjuncts, JoinHint.NONE, left, right); - JoinExchange.setNewLeftJoinReorder(newLeftJoin, leftJoin); - JoinExchange.setNewRightJoinReorder(newRightJoin, leftJoin); - JoinExchange.setNewTopJoinReorder(newTopJoin, topJoin); + newTopJoin.getJoinReorderContext().setHasExchange(true); return CBOUtils.projectOrSelf(ImmutableList.copyOf(topJoin.getOutput()), newTopJoin); }).toRule(RuleType.LOGICAL_JOIN_EXCHANGE_BOTH_PROJECT); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchangeLeftProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchangeLeftProject.java index 52ecf6338c..d3e291f13b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchangeLeftProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchangeLeftProject.java @@ -101,9 +101,7 @@ public class JoinExchangeLeftProject extends OneExplorationRuleFactory { LogicalJoin newTopJoin = new LogicalJoin<>(JoinType.INNER_JOIN, newTopJoinHashJoinConjuncts, newTopJoinOtherJoinConjuncts, JoinHint.NONE, left, right); - JoinExchange.setNewLeftJoinReorder(newLeftJoin, leftJoin); - JoinExchange.setNewRightJoinReorder(newRightJoin, leftJoin); - JoinExchange.setNewTopJoinReorder(newTopJoin, topJoin); + newTopJoin.getJoinReorderContext().setHasExchange(true); return CBOUtils.projectOrSelf(ImmutableList.copyOf(topJoin.getOutput()), newTopJoin); }).toRule(RuleType.LOGICAL_JOIN_EXCHANGE_LEFT_PROJECT); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchangeRightProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchangeRightProject.java index 60be54b820..ff44c8ea10 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchangeRightProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchangeRightProject.java @@ -101,9 +101,7 @@ public class JoinExchangeRightProject extends OneExplorationRuleFactory { LogicalJoin newTopJoin = new LogicalJoin<>(JoinType.INNER_JOIN, newTopJoinHashJoinConjuncts, newTopJoinOtherJoinConjuncts, JoinHint.NONE, left, right); - JoinExchange.setNewLeftJoinReorder(newLeftJoin, leftJoin); - JoinExchange.setNewRightJoinReorder(newRightJoin, rightJoin); - JoinExchange.setNewTopJoinReorder(newTopJoin, topJoin); + newTopJoin.getJoinReorderContext().setHasExchange(true); return CBOUtils.projectOrSelf(ImmutableList.copyOf(topJoin.getOutput()), newTopJoin); }).toRule(RuleType.LOGICAL_JOIN_EXCHANGE_RIGHT_PROJECT); diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/sqltest/CascadesJoinReorderTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/sqltest/CascadesJoinReorderTest.java index fa919c3b19..baf6625ac2 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/sqltest/CascadesJoinReorderTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/sqltest/CascadesJoinReorderTest.java @@ -33,7 +33,7 @@ import org.junit.jupiter.api.Test; * bushy: star graph can't be a bushy, it can only form a zig-zag (because the center must be joined first) * */ -public class CascadesJoinReorderTest extends SqlTestBase { +class CascadesJoinReorderTest extends SqlTestBase { @Test void testStartThreeJoin() { // Three join