[enhancement](Nereids): when rule return original plan, skip copyIn() (#25730)

This commit is contained in:
jakevin
2023-10-23 12:38:43 +08:00
committed by GitHub
parent 6714966df2
commit 09b2593035
13 changed files with 19 additions and 101 deletions

View File

@ -73,6 +73,9 @@ public class ApplyRuleJob extends Job {
for (Plan plan : groupExpressionMatching) {
List<Plan> newPlans = rule.transform(plan, context.getCascadesContext());
for (Plan newPlan : newPlans) {
if (newPlan == plan) {
continue;
}
CopyInResult result = context.getCascadesContext()
.getMemo()
.copyIn(newPlan, groupExpression.getOwnerGroup(), false);

View File

@ -75,9 +75,6 @@ public class InnerJoinLAsscom extends OneExplorationRuleFactory {
LogicalJoin<Plan, Plan> newBottomJoin = topJoin.withConjunctsChildren(newBottomHashConjuncts,
newBottomOtherConjuncts, a, c);
newBottomJoin.getJoinReorderContext().copyFrom(bottomJoin.getJoinReorderContext());
newBottomJoin.getJoinReorderContext().setHasLAsscom(false);
newBottomJoin.getJoinReorderContext().setHasCommute(false);
LogicalJoin<Plan, Plan> newTopJoin = bottomJoin.withConjunctsChildren(newTopHashConjuncts,
newTopOtherConjuncts, newBottomJoin, b);

View File

@ -83,9 +83,6 @@ public class InnerJoinLAsscomProject extends OneExplorationRuleFactory {
/* ********** new Plan ********** */
LogicalJoin<Plan, Plan> 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<ExprId> topUsedExprIds = new HashSet<>(topJoin.getOutputExprIdSet());

View File

@ -88,8 +88,7 @@ public class InnerJoinLeftAssociate extends OneExplorationRuleFactory {
newBottomHashJoinConjuncts, newBottomOtherJoinConjuncts, a, b);
LogicalJoin<Plan, Plan> 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);
}
}

View File

@ -88,8 +88,7 @@ public class InnerJoinLeftAssociateProject extends OneExplorationRuleFactory {
LogicalJoin<Plan, Plan> 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);

View File

@ -86,8 +86,7 @@ public class InnerJoinRightAssociate extends OneExplorationRuleFactory {
newBottomHashJoinConjuncts, newBottomOtherJoinConjuncts, b, c);
LogicalJoin<Plan, Plan> 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);
}
}

View File

@ -85,8 +85,7 @@ public class InnerJoinRightAssociateProject extends OneExplorationRuleFactory {
LogicalJoin<Plan, Plan> 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);
}
}

View File

@ -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<GroupPlan, GroupPlan> join) {
return !join.getJoinReorderContext().hasCommute()
&& !join.getJoinReorderContext().hasExchange();
}
public static boolean isNotBottomJoin(LogicalJoin<GroupPlan, GroupPlan> join) {

View File

@ -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.
*/

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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)
* </pre>
*/
public class CascadesJoinReorderTest extends SqlTestBase {
class CascadesJoinReorderTest extends SqlTestBase {
@Test
void testStartThreeJoin() {
// Three join