[fix](Nereids): move parentExpression in moveOwnership() (#15786)

This commit is contained in:
jakevin
2023-01-11 15:47:37 +08:00
committed by GitHub
parent 98d69d1568
commit 4424874237
3 changed files with 19 additions and 45 deletions

View File

@ -46,7 +46,7 @@ import java.util.stream.Collectors;
*/
public class Group {
private final GroupId groupId;
// Save all parent GroupExpression to avoid travsing whole Memo.
// Save all parent GroupExpression to avoid traversing whole Memo.
private final IdentityHashMap<GroupExpression, Void> parentExpressions = new IdentityHashMap<>();
private final List<GroupExpression> logicalExpressions = Lists.newArrayList();
@ -261,47 +261,28 @@ public class Group {
}
/**
* move the ownerGroup of all logical expressions to target group
* move the ownerGroup to target group
* if this.equals(target), do nothing.
*
* @param target the new owner group of expressions
*/
public void moveLogicalExpressionOwnership(Group target) {
public void moveOwnership(Group target) {
if (equals(target)) {
return;
}
for (GroupExpression expression : logicalExpressions) {
target.addGroupExpression(expression);
}
// move parentExpressions Ownership
parentExpressions.keySet().forEach(kv -> target.addParentExpression(kv));
parentExpressions.clear();
// move LogicalExpression PhysicalExpression Ownership
logicalExpressions.forEach(expr -> target.addGroupExpression(expr));
logicalExpressions.clear();
}
/**
* move the ownerGroup of all physical expressions to target group
* if this.equals(target), do nothing.
*
* @param target the new owner group of expressions
*/
public void movePhysicalExpressionOwnership(Group target) {
if (equals(target)) {
return;
}
for (GroupExpression expression : physicalExpressions) {
target.addGroupExpression(expression);
}
// movePhysicalExpressionOwnership
physicalExpressions.forEach(expr -> target.addGroupExpression(expr));
physicalExpressions.clear();
}
/**
* move the ownerGroup of all lowestCostPlans to target group
* if this.equals(target), do nothing.
*
* @param target the new owner group of expressions
*/
public void moveLowestCostPlansOwnership(Group target) {
if (equals(target)) {
return;
}
// moveLowestCostPlansOwnership
lowestCostPlans.forEach((physicalProperties, costAndGroupExpr) -> {
GroupExpression bestGroupExpression = costAndGroupExpr.second;
// change into target group.

View File

@ -33,6 +33,7 @@ import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import org.apache.doris.nereids.util.Utils;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.statistics.StatsDeriveResult;
@ -131,12 +132,11 @@ public class Memo {
return copyOutAll(root);
}
public List<Plan> copyOutAll(Group group) {
private List<Plan> copyOutAll(Group group) {
List<GroupExpression> logicalExpressions = group.getLogicalExpressions();
List<Plan> plans = logicalExpressions.stream()
return logicalExpressions.stream()
.flatMap(groupExpr -> copyOutAll(groupExpr).stream())
.collect(Collectors.toList());
return plans;
}
private List<Plan> copyOutAll(GroupExpression logicalExpression) {
@ -446,12 +446,7 @@ public class Memo {
// After change GroupExpression children, the hashcode will change,
// so need to reinsert into map.
groupExpressions.remove(groupExpression);
List<Group> children = groupExpression.children();
for (int i = 0; i < children.size(); i++) {
if (children.get(i).equals(source)) {
children.set(i, destination);
}
}
Utils.replaceList(groupExpression.children(), source, destination);
GroupExpression that = groupExpressions.get(groupExpression);
if (that != null && that.getOwnerGroup() != null
@ -467,9 +462,7 @@ public class Memo {
}
if (!source.equals(destination)) {
// TODO: stats and other
source.moveLogicalExpressionOwnership(destination);
source.movePhysicalExpressionOwnership(destination);
source.moveLowestCostPlansOwnership(destination);
source.moveOwnership(destination);
groups.remove(source.getGroupId());
}
return destination;

View File

@ -212,7 +212,7 @@ public class Utils {
public static <T> void replaceList(List<T> list, T oldItem, T newItem) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == oldItem) {
if (list.get(i).equals(oldItem)) {
list.set(i, newItem);
}
}