Skip to content

Commit a020aba

Browse files
committed
[opt](memo) reduce memo size
1 parent 38e7ffd commit a020aba

File tree

6 files changed

+54
-6
lines changed

6 files changed

+54
-6
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Group.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.doris.common.Pair;
2121
import org.apache.doris.nereids.cost.Cost;
22+
import org.apache.doris.nereids.properties.DistributionSpec;
2223
import org.apache.doris.nereids.properties.LogicalProperties;
2324
import org.apache.doris.nereids.properties.PhysicalProperties;
2425
import org.apache.doris.nereids.trees.expressions.literal.Literal;
@@ -27,6 +28,7 @@
2728
import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
2829
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
2930
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
31+
import org.apache.doris.nereids.trees.plans.physical.PhysicalDistribute;
3032
import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan;
3133
import org.apache.doris.nereids.util.TreeStringUtils;
3234
import org.apache.doris.nereids.util.Utils;
@@ -37,6 +39,7 @@
3739
import com.google.common.collect.ImmutableMap;
3840
import com.google.common.collect.Lists;
3941
import com.google.common.collect.Maps;
42+
import com.google.common.collect.Sets;
4043

4144
import java.text.DecimalFormat;
4245
import java.util.ArrayList;
@@ -46,6 +49,7 @@
4649
import java.util.Map;
4750
import java.util.Map.Entry;
4851
import java.util.Optional;
52+
import java.util.Set;
4953
import java.util.function.Function;
5054
import java.util.stream.Collectors;
5155

@@ -60,6 +64,7 @@ public class Group {
6064
private final List<GroupExpression> logicalExpressions = Lists.newArrayList();
6165
private final List<GroupExpression> physicalExpressions = Lists.newArrayList();
6266
private final Map<GroupExpression, GroupExpression> enforcers = Maps.newHashMap();
67+
private final Map<DistributionSpec, GroupExpression> enforcerSpecs = Maps.newHashMap();
6368
private boolean isStatsReliable = true;
6469
private LogicalProperties logicalProperties;
6570

@@ -243,15 +248,28 @@ public GroupExpression getBestPlan(PhysicalProperties properties) {
243248
return null;
244249
}
245250

251+
/**
252+
* add a new enforcer to this group.
253+
*/
246254
public void addEnforcer(GroupExpression enforcer) {
247255
enforcer.setOwnerGroup(this);
256+
if (enforcer.getPlan() instanceof PhysicalDistribute) {
257+
DistributionSpec distributionSpec = ((PhysicalDistribute) enforcer.getPlan()).getDistributionSpec();
258+
if (null != enforcerSpecs.put(distributionSpec, enforcer)) {
259+
return;
260+
}
261+
}
248262
enforcers.put(enforcer, enforcer);
249263
}
250264

251265
public Map<GroupExpression, GroupExpression> getEnforcers() {
252266
return enforcers;
253267
}
254268

269+
public Map<DistributionSpec, GroupExpression> getEnforcerSpecs() {
270+
return enforcerSpecs;
271+
}
272+
255273
/**
256274
* Set or update lowestCostPlans: properties --> Pair.of(cost, expression)
257275
*/
@@ -356,6 +374,7 @@ public void mergeTo(Group target) {
356374
// TODO: dedup?
357375
enforcers.forEach((k, v) -> target.addEnforcer(k));
358376
enforcers.clear();
377+
enforcerSpecs.clear();
359378

360379
// move LogicalExpression PhysicalExpression Ownership
361380
Map<GroupExpression, GroupExpression> logicalSet = target.getLogicalExpressions().stream()

fe/fe-core/src/main/java/org/apache/doris/nereids/memo/GroupExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class GroupExpression {
5454
private static final EventProducer COST_STATE_TRACER = new EventProducer(CostStateUpdateEvent.class,
5555
EventChannel.getDefaultChannel().addConsumers(new LogConsumer(CostStateUpdateEvent.class,
5656
EventChannel.LOG)));
57-
private Cost cost;
57+
private Cost cost = null;
5858
private Group ownerGroup;
5959
private final List<Group> children;
6060
private final Plan plan;

fe/fe-core/src/main/java/org/apache/doris/nereids/properties/ChildrenPropertiesRegulator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,9 @@ private void updateChildEnforceAndCost(GroupExpression child, PhysicalProperties
867867
currentCost = newChildAndCost.first;
868868
}
869869

870+
if (child.getOwnerGroup().getEnforcerSpecs().containsKey(target)) {
871+
return;
872+
}
870873
PhysicalProperties newOutputProperty = new PhysicalProperties(target);
871874
GroupExpression enforcer = target.addEnforcer(child.getOwnerGroup());
872875
child.getOwnerGroup().addEnforcer(enforcer);

fe/fe-core/src/main/java/org/apache/doris/nereids/properties/DistributionSpecHiveTableSinkHashPartitioned.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.doris.nereids.trees.expressions.ExprId;
2121

2222
import java.util.List;
23+
import java.util.Objects;
2324

2425
/**
2526
* use for shuffle data by partition keys before sink.
@@ -44,4 +45,21 @@ public void setOutputColExprIds(List<ExprId> outputColExprIds) {
4445
public boolean satisfy(DistributionSpec other) {
4546
return other instanceof DistributionSpecHiveTableSinkHashPartitioned;
4647
}
48+
49+
@Override
50+
public boolean equals(Object o) {
51+
if (o == null || getClass() != o.getClass()) {
52+
return false;
53+
}
54+
if (!super.equals(o)) {
55+
return false;
56+
}
57+
DistributionSpecHiveTableSinkHashPartitioned that = (DistributionSpecHiveTableSinkHashPartitioned) o;
58+
return Objects.equals(outputColExprIds, that.outputColExprIds);
59+
}
60+
61+
@Override
62+
public int hashCode() {
63+
return Objects.hash(super.hashCode(), outputColExprIds);
64+
}
4765
}

fe/fe-core/src/main/java/org/apache/doris/nereids/properties/EnforceMissingPropertiesHelper.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ private PhysicalProperties enforceDistribution(PhysicalProperties oldOutputPrope
123123
}
124124

125125
PhysicalProperties newOutputProperty = new PhysicalProperties(outputDistributionSpec);
126-
GroupExpression enforcer = outputDistributionSpec.addEnforcer(groupExpression.getOwnerGroup());
126+
GroupExpression enforcer = groupExpression.getOwnerGroup().getEnforcerSpecs().get(outputDistributionSpec);
127+
128+
if (enforcer == null) {
129+
enforcer = outputDistributionSpec.addEnforcer(groupExpression.getOwnerGroup());
130+
}
127131
addEnforcerUpdateCost(enforcer, oldOutputProperty, newOutputProperty);
128132
return newOutputProperty;
129133
}
@@ -160,10 +164,13 @@ private void addEnforcerUpdateCost(GroupExpression enforcer,
160164
oldOutputProperty, newOutputProperty);
161165
ENFORCER_TRACER.log(EnforcerEvent.of(groupExpression, ((PhysicalPlan) enforcer.getPlan()),
162166
oldOutputProperty, newOutputProperty));
163-
enforcer.setEstOutputRowCount(enforcer.getOwnerGroup().getStatistics().getRowCount());
164-
Cost enforcerCost = CostCalculator.calculateCost(connectContext, enforcer,
165-
Lists.newArrayList(oldOutputProperty));
166-
enforcer.setCost(enforcerCost);
167+
Cost enforcerCost = enforcer.getCost();
168+
if (enforcerCost == null) {
169+
enforcer.setEstOutputRowCount(enforcer.getOwnerGroup().getStatistics().getRowCount());
170+
enforcerCost = CostCalculator.calculateCost(connectContext, enforcer,
171+
Lists.newArrayList(oldOutputProperty));
172+
enforcer.setCost(enforcerCost);
173+
}
167174
curTotalCost = CostCalculator.addChildCost(
168175
connectContext,
169176
enforcer.getPlan(),

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalDistribute.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.json.JSONObject;
3636

3737
import java.util.List;
38+
import java.util.Objects;
3839
import java.util.Optional;
3940

4041
/**

0 commit comments

Comments
 (0)