[enhance](nereids): polish code for mergeGroup(). (#16057)

This commit is contained in:
jakevin
2023-01-18 21:03:46 +08:00
committed by GitHub
parent feeb69438b
commit cbcd5228b7
5 changed files with 24 additions and 8 deletions

View File

@ -70,13 +70,8 @@ public class Group {
*/
public Group(GroupId groupId, GroupExpression groupExpression, LogicalProperties logicalProperties) {
this.groupId = groupId;
if (groupExpression.getPlan() instanceof LogicalPlan) {
this.logicalExpressions.add(groupExpression);
} else {
this.physicalExpressions.add(groupExpression);
}
addGroupExpression(groupExpression);
this.logicalProperties = logicalProperties;
groupExpression.setOwnerGroup(this);
}
/**
@ -111,6 +106,11 @@ public class Group {
logicalExpressions.add(groupExpression);
}
public void addPhysicalExpression(GroupExpression groupExpression) {
groupExpression.setOwnerGroup(this);
physicalExpressions.add(groupExpression);
}
public List<GroupExpression> getLogicalExpressions() {
return logicalExpressions;
}
@ -211,7 +211,7 @@ public class Group {
/**
* replace best plan with new properties
*/
public void replaceBestPlan(PhysicalProperties oldProperty, PhysicalProperties newProperty, double cost) {
public void replaceBestPlanProperty(PhysicalProperties oldProperty, PhysicalProperties newProperty, double cost) {
Pair<Double, GroupExpression> pair = lowestCostPlans.get(oldProperty);
GroupExpression lowestGroupExpr = pair.second;
lowestGroupExpr.updateLowestCostTable(newProperty,
@ -298,6 +298,11 @@ public class Group {
}
});
lowestCostPlans.clear();
// If statistics is null, use other statistics
if (target.statistics == null) {
target.statistics = this.statistics;
}
}
public boolean isJoinGroup() {

View File

@ -114,6 +114,7 @@ public class GroupExpression {
}
public void setChild(int i, Group group) {
child(i).removeParentExpression(this);
children.set(i, group);
group.addParentExpression(this);
}
@ -194,6 +195,7 @@ public class GroupExpression {
/**
* get the lowest cost when satisfy property
*
* @param property property that needs to be satisfied
* @return Lowest cost to satisfy that property
*/

View File

@ -95,6 +95,10 @@ public class Memo {
return ImmutableList.copyOf(groups.values());
}
public Group getGroup(GroupId groupId) {
return groups.get(groupId);
}
public Map<GroupExpression, GroupExpression> getGroupExpressions() {
return groupExpressions;
}
@ -472,7 +476,9 @@ public class Memo {
* Add enforcer expression into the target group.
*/
public void addEnforcerPlan(GroupExpression groupExpression, Group group) {
Preconditions.checkArgument(groupExpression != null);
groupExpression.setOwnerGroup(group);
// Don't add groupExpression into group's physicalExpressions, it will cause dead loop;
}
private CopyInResult rewriteByExistedPlan(Group targetGroup, Plan existedPlan) {

View File

@ -83,7 +83,7 @@ public class EnforceMissingPropertiesHelper {
*/
private PhysicalProperties enforceDistributionButMeetSort(PhysicalProperties output, PhysicalProperties request) {
groupExpression.getOwnerGroup()
.replaceBestPlan(output, PhysicalProperties.ANY, groupExpression.getCostByProperties(output));
.replaceBestPlanProperty(output, PhysicalProperties.ANY, groupExpression.getCostByProperties(output));
return enforceSortAndDistribution(output, request);
}

View File

@ -214,11 +214,14 @@ public class Utils {
* Replace one item in a list with another item.
*/
public static <T> void replaceList(List<T> list, T oldItem, T newItem) {
boolean result = false;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(oldItem)) {
list.set(i, newItem);
result = true;
}
}
Preconditions.checkState(result);
}
/**