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 103c6d6763..5bf57cab67 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 @@ -111,12 +111,9 @@ public class InnerJoinLeftAssociate extends OneExplorationRuleFactory { * Check JoinReorderContext. */ public static boolean checkReorder(LogicalJoin topJoin) { - if (topJoin.getJoinReorderContext().hasCommute() - || topJoin.getJoinReorderContext().hasLeftAssociate() - || topJoin.getJoinReorderContext().hasRightAssociate() - || topJoin.getJoinReorderContext().hasExchange()) { - return false; - } - return true; + return !topJoin.getJoinReorderContext().hasCommute() + && !topJoin.getJoinReorderContext().hasLeftAssociate() + && !topJoin.getJoinReorderContext().hasRightAssociate() + && !topJoin.getJoinReorderContext().hasExchange(); } } 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 96d94c3d90..2b163485a2 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 @@ -111,7 +111,7 @@ public class InnerJoinRightAssociate extends OneExplorationRuleFactory { public static boolean checkReorder(LogicalJoin topJoin) { return !topJoin.getJoinReorderContext().hasCommute() && !topJoin.getJoinReorderContext().hasRightAssociate() - && !topJoin.getJoinReorderContext().hasRightAssociate() + && !topJoin.getJoinReorderContext().hasLeftAssociate() && !topJoin.getJoinReorderContext().hasExchange(); } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLeftAssociateTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLeftAssociateTest.java index 7b4b7187b8..9c4215dba0 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLeftAssociateTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinLeftAssociateTest.java @@ -19,6 +19,7 @@ package org.apache.doris.nereids.rules.exploration.join; import org.apache.doris.common.Pair; import org.apache.doris.nereids.trees.plans.JoinType; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan; import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; import org.apache.doris.nereids.util.LogicalPlanBuilder; @@ -27,6 +28,7 @@ import org.apache.doris.nereids.util.MemoTestUtils; import org.apache.doris.nereids.util.PlanChecker; import org.apache.doris.nereids.util.PlanConstructor; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; class InnerJoinLeftAssociateTest implements MemoPatternMatchSupported { @@ -35,7 +37,7 @@ class InnerJoinLeftAssociateTest implements MemoPatternMatchSupported { private final LogicalOlapScan scan3 = PlanConstructor.newLogicalOlapScan(2, "t3", 0); @Test - public void testSimple() { + void testSimple() { /* * LogicalJoin ( type=INNER_JOIN, hashJoinConjuncts=[(id#4 = id#0)], otherJoinConjuncts=[] ) * |--LogicalOlapScan ( qualified=db.t1, output=[id#4, name#5], candidateIndexIds=[], selectedIndexId=-1, preAgg=ON ) @@ -65,4 +67,43 @@ class InnerJoinLeftAssociateTest implements MemoPatternMatchSupported { ) ); } + + @Test + void testCheckReorderFailed() { + JoinReorderContext joinReorderContext = new JoinReorderContext(); + + joinReorderContext.setHasExchange(true); + test(joinReorderContext); + joinReorderContext.setHasExchange(false); + + joinReorderContext.setHasCommute(true); + test(joinReorderContext); + joinReorderContext.setHasCommute(false); + + joinReorderContext.setHasLeftAssociate(true); + test(joinReorderContext); + joinReorderContext.setHasLeftAssociate(false); + + joinReorderContext.setHasRightAssociate(true); + test(joinReorderContext); + joinReorderContext.setHasRightAssociate(false); + } + + void test(JoinReorderContext joinReorderContext) { + LogicalJoin topJoin = (LogicalJoin) new LogicalPlanBuilder(scan1) + .join( + new LogicalPlanBuilder(scan2) + .join(scan3, JoinType.INNER_JOIN, Pair.of(0, 0)) + .build(), + JoinType.INNER_JOIN, Pair.of(0, 0) + ) + .build(); + + JoinReorderContext topJoinReorderContext = topJoin.getJoinReorderContext(); + topJoinReorderContext.copyFrom(joinReorderContext); + + PlanChecker.from(MemoTestUtils.createConnectContext(), topJoin) + .applyExploration(InnerJoinLeftAssociate.INSTANCE.build()) + .checkMemo(memo -> Assertions.assertEquals(1, memo.getRoot().getLogicalExpressions().size())); + } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinRightAssociateTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinRightAssociateTest.java index c0c07513b6..b33f60d25f 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinRightAssociateTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/join/InnerJoinRightAssociateTest.java @@ -19,6 +19,7 @@ package org.apache.doris.nereids.rules.exploration.join; import org.apache.doris.common.Pair; import org.apache.doris.nereids.trees.plans.JoinType; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan; import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; import org.apache.doris.nereids.util.LogicalPlanBuilder; @@ -27,6 +28,7 @@ import org.apache.doris.nereids.util.MemoTestUtils; import org.apache.doris.nereids.util.PlanChecker; import org.apache.doris.nereids.util.PlanConstructor; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; class InnerJoinRightAssociateTest implements MemoPatternMatchSupported { @@ -35,7 +37,7 @@ class InnerJoinRightAssociateTest implements MemoPatternMatchSupported { private final LogicalOlapScan scan3 = PlanConstructor.newLogicalOlapScan(2, "t3", 0); @Test - public void testSimple() { + void testSimple() { /*- * LogicalJoin ( type=INNER_JOIN, hashJoinConjuncts=[(id#2 = id#4)], otherJoinConjuncts=[] ) * |--LogicalJoin ( type=INNER_JOIN, hashJoinConjuncts=[(id#0 = id#2)], otherJoinConjuncts=[] ) @@ -62,4 +64,38 @@ class InnerJoinRightAssociateTest implements MemoPatternMatchSupported { ) ); } + + @Test + void testCheckReorderFailed() { + JoinReorderContext joinReorderContext = new JoinReorderContext(); + + joinReorderContext.setHasExchange(true); + assertApplyFailedWithJoinReorderContext(joinReorderContext); + joinReorderContext.setHasExchange(false); + + joinReorderContext.setHasCommute(true); + assertApplyFailedWithJoinReorderContext(joinReorderContext); + joinReorderContext.setHasCommute(false); + + joinReorderContext.setHasLeftAssociate(true); + assertApplyFailedWithJoinReorderContext(joinReorderContext); + joinReorderContext.setHasLeftAssociate(false); + + joinReorderContext.setHasRightAssociate(true); + assertApplyFailedWithJoinReorderContext(joinReorderContext); + joinReorderContext.setHasRightAssociate(false); + } + + void assertApplyFailedWithJoinReorderContext(JoinReorderContext joinReorderContext) { + LogicalJoin topJoin = (LogicalJoin) new LogicalPlanBuilder(scan1) + .join(scan2, JoinType.INNER_JOIN, Pair.of(0, 0)) + .join(scan3, JoinType.INNER_JOIN, Pair.of(2, 0)) + .build(); + JoinReorderContext topJoinReorderContext = topJoin.getJoinReorderContext(); + topJoinReorderContext.copyFrom(joinReorderContext); + PlanChecker.from(MemoTestUtils.createConnectContext(), topJoin) + .applyExploration(InnerJoinRightAssociate.INSTANCE.build()) + .checkMemo(memo -> Assertions.assertEquals(1, memo.getRoot().getLogicalExpressions().size())); + } + }