Skip to content

Commit 3a31fe3

Browse files
authored
[improvement](mtmv) Support more join types for query rewriting by materialized view (#32685)
This pattern of rewriting is supported for multi-table joins and supported join types is as following: INNER JOIN LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN LEFT SEMI JOIN RIGHT SEMI JOIN LEFT ANTI JOIN RIGHT ANTI JOIN
1 parent 22b8038 commit 3a31fe3

14 files changed

Lines changed: 2270 additions & 148 deletions

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858

5959
import java.util.ArrayList;
6060
import java.util.Collection;
61-
import java.util.HashSet;
6261
import java.util.List;
6362
import java.util.Map;
6463
import java.util.Objects;
@@ -69,8 +68,15 @@
6968
* The abstract class for all materialized view rules
7069
*/
7170
public abstract class AbstractMaterializedViewRule implements ExplorationRuleFactory {
72-
public static final HashSet<JoinType> SUPPORTED_JOIN_TYPE_SET = Sets.newHashSet(JoinType.INNER_JOIN,
73-
JoinType.LEFT_OUTER_JOIN);
71+
public static final Set<JoinType> SUPPORTED_JOIN_TYPE_SET = ImmutableSet.of(
72+
JoinType.INNER_JOIN,
73+
JoinType.LEFT_OUTER_JOIN,
74+
JoinType.RIGHT_OUTER_JOIN,
75+
JoinType.FULL_OUTER_JOIN,
76+
JoinType.LEFT_SEMI_JOIN,
77+
JoinType.RIGHT_SEMI_JOIN,
78+
JoinType.LEFT_ANTI_JOIN,
79+
JoinType.RIGHT_ANTI_JOIN);
7480

7581
/**
7682
* The abstract template method for query rewrite, it contains the main logic, try to rewrite query by

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/ComparisonResult.java‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.doris.nereids.rules.exploration.mv;
1919

20+
import org.apache.doris.common.Pair;
2021
import org.apache.doris.nereids.trees.expressions.Expression;
2122
import org.apache.doris.nereids.trees.expressions.Slot;
2223

@@ -27,6 +28,7 @@
2728
import java.util.Collection;
2829
import java.util.List;
2930
import java.util.Set;
31+
import java.util.stream.Collectors;
3032

3133
/**
3234
* comparison result of view and query
@@ -111,8 +113,14 @@ public Builder addViewExpressions(Collection<? extends Expression> expressions)
111113
return this;
112114
}
113115

114-
public Builder addViewNoNullableSlot(Set<Slot> viewNoNullableSlot) {
115-
viewNoNullableSlotBuilder.add(ImmutableSet.copyOf(viewNoNullableSlot));
116+
/**Add slots which should reject null slots in view*/
117+
public Builder addViewNoNullableSlot(Pair<Set<Slot>, Set<Slot>> viewNoNullableSlotsPair) {
118+
if (!viewNoNullableSlotsPair.first.isEmpty()) {
119+
viewNoNullableSlotBuilder.add(viewNoNullableSlotsPair.first);
120+
}
121+
if (!viewNoNullableSlotsPair.second.isEmpty()) {
122+
viewNoNullableSlotBuilder.add(viewNoNullableSlotsPair.second);
123+
}
116124
return this;
117125
}
118126

@@ -127,7 +135,9 @@ public boolean isInvalid() {
127135

128136
public ComparisonResult build() {
129137
Preconditions.checkArgument(valid, "Comparison result must be valid");
130-
return new ComparisonResult(queryBuilder.build(), queryAllPulledUpExpressionsBuilder.build(),
138+
return new ComparisonResult(queryBuilder.build(),
139+
queryAllPulledUpExpressionsBuilder.build().stream()
140+
.filter(expr -> !expr.isInferred()).collect(Collectors.toList()),
131141
viewBuilder.build(), viewNoNullableSlotBuilder.build(), valid, "");
132142
}
133143
}

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/HyperGraphComparator.java‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ public class HyperGraphComparator {
7373
private final Map<Edge, List<? extends Expression>> pullUpQueryExprWithEdge = new HashMap<>();
7474
private final Map<Edge, List<? extends Expression>> pullUpViewExprWithEdge = new HashMap<>();
7575
private final LogicalCompatibilityContext logicalCompatibilityContext;
76-
private final Map<JoinEdge, Pair<JoinType, Set<Slot>>> inferredViewEdgeWithCond = new HashMap<>();
76+
// this records the slots which needs to reject null
77+
// the key is the target join which should reject null, the value is a pair, the first value of the pair is the
78+
// join type, the second value is also a pair which left represents the slots in the left of join that should
79+
// reject null, right represents the slots in the right of join that should reject null.
80+
private final Map<JoinEdge, Pair<JoinType, Pair<Set<Slot>, Set<Slot>>>> inferredViewEdgeWithCond = new HashMap<>();
7781
private List<JoinEdge> viewJoinEdgesAfterInferring;
7882
private List<FilterEdge> viewFilterEdgesAfterInferring;
7983
private final long eliminateViewNodesMap;
@@ -263,7 +267,7 @@ private ComparisonResult buildComparisonRes() {
263267
}
264268
builder.addViewExpressions(rawFilter);
265269
}
266-
for (Pair<JoinType, Set<Slot>> inferredCond : inferredViewEdgeWithCond.values()) {
270+
for (Pair<JoinType, Pair<Set<Slot>, Set<Slot>>> inferredCond : inferredViewEdgeWithCond.values()) {
267271
builder.addViewNoNullableSlot(inferredCond.second);
268272
}
269273
builder.addQueryAllPulledUpExpressions(
@@ -485,11 +489,14 @@ private boolean tryInferEdge(JoinEdge query, JoinEdge view) {
485489
if (noNullableChild == null) {
486490
return false;
487491
}
488-
Set<Slot> noNullableSlot = Sets.union(
489-
noNullableChild.first ? view.getJoin().left().getOutputSet() : ImmutableSet.of(),
490-
noNullableChild.second ? view.getJoin().right().getOutputSet() : ImmutableSet.of()
491-
);
492-
inferredViewEdgeWithCond.put(view, Pair.of(query.getJoinType(), noNullableSlot));
492+
Pair<Set<Slot>, Set<Slot>> noNullableSlotSetPair = Pair.of(ImmutableSet.of(), ImmutableSet.of());
493+
if (noNullableChild.first) {
494+
noNullableSlotSetPair.first = view.getJoin().left().getOutputSet();
495+
}
496+
if (noNullableChild.second) {
497+
noNullableSlotSetPair.second = view.getJoin().right().getOutputSet();
498+
}
499+
inferredViewEdgeWithCond.put(view, Pair.of(query.getJoinType(), noNullableSlotSetPair));
493500
}
494501
return true;
495502
}

0 commit comments

Comments
 (0)