Skip to content

Commit d1b5a73

Browse files
author
yangtao555
committed
[refactor](fe) Refactor MV rewrite StructInfo lookup by relation ids
1 parent 9368a15 commit d1b5a73

30 files changed

Lines changed: 822 additions & 854 deletions

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,9 @@ public enum TableFrom {
274274
// Record used table and it's used partitions
275275
private final Multimap<List<String>, Pair<RelationId, Set<String>>> tableUsedPartitionNameMap =
276276
HashMultimap.create();
277-
// Record query common table id to relation id mapping, this is used for mv rewrite
278-
private final Multimap<Integer, Integer> commonTableIdToRelationIdToMap = HashMultimap.create();
277+
// Record statement-scope table ids to relation ids for MV rewrite.
278+
// One table id may map to multiple relation ids because of aliases and nested MV scan alternatives.
279+
private final Multimap<Integer, Integer> tableIdToRelationIds = HashMultimap.create();
279280

280281
// Record mtmv and valid partitions map because this is time-consuming behavior
281282
private final Map<BaseTableInfo, Collection<Partition>> mvCanRewritePartitionsMap = new HashMap<>();
@@ -304,8 +305,6 @@ public enum TableFrom {
304305
// mark is rewritten in RBO phase, if rewritten in RBO phase should set true
305306
private boolean preMvRewritten = false;
306307

307-
private final Set<List<String>> materializationRewrittenSuccessSet = new HashSet<>();
308-
309308
private boolean isInsert = false;
310309
private Optional<Map<TableIf, Set<Expression>>> mvRefreshPredicates = Optional.empty();
311310

@@ -1109,20 +1108,12 @@ public void setPreMvRewritten(boolean preMvRewritten) {
11091108
this.preMvRewritten = preMvRewritten;
11101109
}
11111110

1112-
public Set<List<String>> getMaterializationRewrittenSuccessSet() {
1113-
return materializationRewrittenSuccessSet;
1114-
}
1115-
1116-
public void addMaterializationRewrittenSuccess(List<String> materializationQualifier) {
1117-
this.materializationRewrittenSuccessSet.add(materializationQualifier);
1118-
}
1119-
11201111
public Multimap<List<String>, Pair<RelationId, Set<String>>> getTableUsedPartitionNameMap() {
11211112
return tableUsedPartitionNameMap;
11221113
}
11231114

1124-
public Multimap<Integer, Integer> getCommonTableIdToRelationIdMap() {
1125-
return commonTableIdToRelationIdToMap;
1115+
public Multimap<Integer, Integer> getTableIdToRelationIds() {
1116+
return tableIdToRelationIds;
11261117
}
11271118

11281119
public Map<BaseTableInfo, Collection<Partition>> getMvCanRewritePartitionsMap() {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public class Group {
8383
private List<PhysicalProperties> chosenEnforcerPropertiesList = new ArrayList<>();
8484
private List<Integer> chosenEnforcerIdList = new ArrayList<>();
8585

86-
private StructInfoMap structInfoMap = new StructInfoMap();
86+
private final StructInfoMap structInfoMap;
8787

8888
/**
8989
* Constructor for Group.
@@ -92,6 +92,7 @@ public class Group {
9292
*/
9393
public Group(GroupId groupId, GroupExpression groupExpression, LogicalProperties logicalProperties) {
9494
this.groupId = groupId;
95+
this.structInfoMap = new StructInfoMap(this);
9596
addGroupExpression(groupExpression);
9697
this.logicalProperties = logicalProperties;
9798
this.groupPlan = new GroupPlan(this);
@@ -104,6 +105,7 @@ public Group(GroupId groupId, GroupExpression groupExpression, LogicalProperties
104105
*/
105106
public Group(GroupId groupId, LogicalProperties logicalProperties) {
106107
this.groupId = groupId;
108+
this.structInfoMap = new StructInfoMap(this);
107109
this.logicalProperties = logicalProperties;
108110
this.groupPlan = new GroupPlan(this);
109111
}

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

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
package org.apache.doris.nereids.memo;
1919

20-
import org.apache.doris.catalog.MTMV;
2120
import org.apache.doris.common.IdGenerator;
2221
import org.apache.doris.common.Pair;
22+
import org.apache.doris.nereids.StatementContext;
2323
import org.apache.doris.nereids.cost.Cost;
2424
import org.apache.doris.nereids.cost.CostCalculator;
2525
import org.apache.doris.nereids.metrics.EventChannel;
@@ -52,7 +52,6 @@
5252
import org.apache.logging.log4j.Logger;
5353

5454
import java.util.ArrayList;
55-
import java.util.BitSet;
5655
import java.util.HashMap;
5756
import java.util.HashSet;
5857
import java.util.LinkedHashMap;
@@ -61,7 +60,6 @@
6160
import java.util.Optional;
6261
import java.util.PriorityQueue;
6362
import java.util.Set;
64-
import java.util.concurrent.atomic.AtomicInteger;
6563
import java.util.stream.Collectors;
6664
import java.util.stream.Stream;
6765
import javax.annotation.Nullable;
@@ -76,9 +74,7 @@ public class Memo {
7674
EventChannel.getDefaultChannel().addConsumers(new LogConsumer(GroupMergeEvent.class, EventChannel.LOG)));
7775
private static long stateId = 0;
7876
private final ConnectContext connectContext;
79-
// The key is the query tableId, the value is the refresh version when last refresh, this is needed
80-
// because struct info refresh base on target tableId.
81-
private final Map<Integer, AtomicInteger> refreshVersion = new HashMap<>();
77+
private final StatementContext statementContext;
8278
private final Map<Class<? extends AbstractMaterializedViewRule>, Set<Long>> materializationCheckSuccessMap =
8379
new LinkedHashMap<>();
8480
private final Map<Class<? extends AbstractMaterializedViewRule>, Set<Long>> materializationCheckFailMap =
@@ -93,11 +89,17 @@ public class Memo {
9389
public Memo() {
9490
this.root = null;
9591
this.connectContext = null;
92+
this.statementContext = null;
9693
}
9794

9895
public Memo(ConnectContext connectContext, Plan plan) {
96+
this(connectContext == null ? null : connectContext.getStatementContext(), plan);
97+
}
98+
99+
private Memo(StatementContext statementContext, Plan plan) {
100+
this.statementContext = statementContext;
101+
this.connectContext = statementContext == null ? null : statementContext.getConnectContext();
99102
this.root = init(plan);
100-
this.connectContext = connectContext;
101103
}
102104

103105
public static long getStateId() {
@@ -132,30 +134,6 @@ public int getGroupExpressionsSize() {
132134
return groupExpressions.size();
133135
}
134136

135-
/** get the refresh version map*/
136-
public Map<Integer, AtomicInteger> getRefreshVersion() {
137-
return refreshVersion;
138-
}
139-
140-
/** return the incremented refresh version for the given commonTableId*/
141-
public long incrementAndGetRefreshVersion(int commonTableId) {
142-
return refreshVersion.compute(commonTableId, (k, v) -> {
143-
if (v == null) {
144-
return new AtomicInteger(1);
145-
}
146-
v.incrementAndGet();
147-
return v;
148-
}).get();
149-
}
150-
151-
/** return the incremented refresh version for the given relationId set*/
152-
public void incrementAndGetRefreshVersion(BitSet commonTableIdSet) {
153-
for (int i = commonTableIdSet.nextSetBit(0); i >= 0;
154-
i = commonTableIdSet.nextSetBit(i + 1)) {
155-
incrementAndGetRefreshVersion(i);
156-
}
157-
}
158-
159137
/**
160138
* Record materialization check result for performance
161139
*/
@@ -379,6 +357,7 @@ public Plan copyOut(GroupExpression logicalExpression, boolean includeGroupExpre
379357
*/
380358
private Group init(Plan plan) {
381359
Preconditions.checkArgument(!(plan instanceof GroupPlan), "Cannot init memo by a GroupPlan");
360+
registerRelationIdentity(plan);
382361

383362
// initialize children recursively
384363
List<Group> childrenGroups = new ArrayList<>(plan.arity());
@@ -481,15 +460,7 @@ private CopyInResult doCopyIn(Plan plan, @Nullable Group targetGroup, @Nullable
481460
plan.getLogicalProperties(), targetGroup.getLogicalProperties());
482461
throw new IllegalStateException("Insert a plan into targetGroup but differ in logicalproperties");
483462
}
484-
if (connectContext != null
485-
&& connectContext.getSessionVariable().isEnableMaterializedViewNestRewrite()
486-
&& plan instanceof LogicalCatalogRelation
487-
&& ((CatalogRelation) plan).getTable() instanceof MTMV
488-
&& !plan.getGroupExpression().isPresent()) {
489-
TableId mvCommonTableId
490-
= this.connectContext.getStatementContext().getTableId(((CatalogRelation) plan).getTable());
491-
incrementAndGetRefreshVersion(mvCommonTableId.asInt());
492-
}
463+
registerRelationIdentity(plan);
493464
Optional<GroupExpression> groupExpr = plan.getGroupExpression();
494465
if (groupExpr.isPresent()) {
495466
Preconditions.checkState(groupExpressions.containsKey(groupExpr.get()));
@@ -513,6 +484,24 @@ private CopyInResult doCopyIn(Plan plan, @Nullable Group targetGroup, @Nullable
513484
// TODO: need to derive logical property if generate new group. currently we not copy logical plan into
514485
}
515486

487+
private void registerRelationIdentity(Plan plan) {
488+
if (statementContext == null) {
489+
return;
490+
}
491+
if (plan instanceof LogicalCatalogRelation) {
492+
// StructInfoMap searches query alternatives by relation id, but each MV context starts from the
493+
// table ids in the MV definition. Register both original scans and nested MV scans copied into memo
494+
// so those table ids can be expanded back to the currently available relation ids.
495+
CatalogRelation catalogRelation = (CatalogRelation) plan;
496+
TableId tableId = statementContext.getTableId(catalogRelation.getTable());
497+
boolean relationIdentityChanged = statementContext.getTableIdToRelationIds()
498+
.put(tableId.asInt(), catalogRelation.getRelationId().asInt());
499+
if (relationIdentityChanged) {
500+
groups.values().forEach(group -> group.getStructInfoMap().clearCandidateCache());
501+
}
502+
}
503+
}
504+
516505
private List<Group> rewriteChildrenPlansToGroups(Plan plan, Group targetGroup) {
517506
List<Group> childrenGroups = Lists.newArrayList();
518507
for (int i = 0; i < plan.children().size(); i++) {

0 commit comments

Comments
 (0)