-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[improvement](mtmv) Optimize the nested materialized view rewrite performance #34050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
morrySnow
merged 4 commits into
apache:master
from
seawinde:optimize_nested_mv_performance
Apr 24, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,10 +29,11 @@ | |
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Iterator; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
|
|
@@ -41,6 +42,7 @@ | |
| public class StructInfoMap { | ||
| private final Map<BitSet, Pair<GroupExpression, List<BitSet>>> groupExpressionMap = new HashMap<>(); | ||
| private final Map<BitSet, StructInfo> infoMap = new HashMap<>(); | ||
| private boolean refreshed; | ||
|
|
||
| /** | ||
| * get struct info according to table map | ||
|
|
@@ -79,6 +81,14 @@ public Pair<GroupExpression, List<BitSet>> getGroupExpressionWithChildren(BitSet | |
| return groupExpressionMap.get(tableMap); | ||
| } | ||
|
|
||
| public boolean isRefreshed() { | ||
| return refreshed; | ||
| } | ||
|
|
||
| public void setRefreshed(boolean refreshed) { | ||
| this.refreshed = refreshed; | ||
| } | ||
|
|
||
| private StructInfo constructStructInfo(GroupExpression groupExpression, List<BitSet> children, | ||
| BitSet tableMap, Plan originPlan) { | ||
| // this plan is not origin plan, should record origin plan in struct info | ||
|
|
@@ -111,26 +121,41 @@ public boolean refresh(Group group) { | |
| int originSize = groupExpressionMap.size(); | ||
| for (GroupExpression groupExpression : group.getLogicalExpressions()) { | ||
| List<Set<BitSet>> childrenTableMap = new ArrayList<>(); | ||
| boolean needRefresh = false; | ||
| boolean needRefresh = groupExpressionMap.isEmpty(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove it. It's seems not needed any more |
||
| if (groupExpression.children().isEmpty()) { | ||
| BitSet leaf = constructLeaf(groupExpression); | ||
| groupExpressionMap.put(leaf, Pair.of(groupExpression, new ArrayList<>())); | ||
| continue; | ||
| } | ||
|
|
||
| for (Group child : groupExpression.children()) { | ||
| if (!refreshedGroup.contains(child)) { | ||
| if (!refreshedGroup.contains(child) && !child.getstructInfoMap().isRefreshed()) { | ||
| StructInfoMap childStructInfoMap = child.getstructInfoMap(); | ||
| needRefresh |= childStructInfoMap.refresh(child); | ||
| childStructInfoMap.setRefreshed(true); | ||
| } | ||
| refreshedGroup.add(child); | ||
| childrenTableMap.add(child.getstructInfoMap().getTableMaps()); | ||
| } | ||
| // if one same groupExpression have refreshed, continue | ||
| BitSet oneOfGroupExpressionTableSet = new BitSet(); | ||
| for (Set<BitSet> groupExpressionBitSet : childrenTableMap) { | ||
| Iterator<BitSet> iterator = groupExpressionBitSet.iterator(); | ||
| if (iterator.hasNext()) { | ||
| oneOfGroupExpressionTableSet.or(iterator.next()); | ||
| } | ||
| } | ||
| if (groupExpressionMap.containsKey(oneOfGroupExpressionTableSet)) { | ||
| continue; | ||
| } | ||
| // if cumulative child table map is different from current | ||
| // or current group expression map is empty, should update the groupExpressionMap currently | ||
| Set<Pair<BitSet, List<BitSet>>> bitSetWithChildren = cartesianProduct(childrenTableMap); | ||
| for (Pair<BitSet, List<BitSet>> bitSetWithChild : bitSetWithChildren) { | ||
| groupExpressionMap.putIfAbsent(bitSetWithChild.first, Pair.of(groupExpression, bitSetWithChild.second)); | ||
| Collection<Pair<BitSet, List<BitSet>>> bitSetWithChildren = cartesianProduct(childrenTableMap); | ||
| if (needRefresh) { | ||
| for (Pair<BitSet, List<BitSet>> bitSetWithChild : bitSetWithChildren) { | ||
| groupExpressionMap.putIfAbsent(bitSetWithChild.first, | ||
| Pair.of(groupExpression, bitSetWithChild.second)); | ||
| } | ||
| } | ||
| } | ||
| return originSize != groupExpressionMap.size(); | ||
|
|
@@ -147,17 +172,17 @@ private BitSet constructLeaf(GroupExpression groupExpression) { | |
| return tableMap; | ||
| } | ||
|
|
||
| private Set<Pair<BitSet, List<BitSet>>> cartesianProduct(List<Set<BitSet>> childrenTableMap) { | ||
| return Sets.cartesianProduct(childrenTableMap) | ||
| .stream() | ||
| .map(bitSetList -> { | ||
| BitSet bitSet = new BitSet(); | ||
| for (BitSet b : bitSetList) { | ||
| bitSet.or(b); | ||
| } | ||
| return Pair.of(bitSet, bitSetList); | ||
| }) | ||
| .collect(Collectors.toSet()); | ||
| private Collection<Pair<BitSet, List<BitSet>>> cartesianProduct(List<Set<BitSet>> childrenTableMap) { | ||
| Set<List<BitSet>> cartesianLists = Sets.cartesianProduct(childrenTableMap); | ||
| List<Pair<BitSet, List<BitSet>>> resultPairSet = new LinkedList<>(); | ||
| for (List<BitSet> bitSetList : cartesianLists) { | ||
| BitSet bitSet = new BitSet(); | ||
| for (BitSet b : bitSetList) { | ||
| bitSet.or(b); | ||
| } | ||
| resultPairSet.add(Pair.of(bitSet, bitSetList)); | ||
| } | ||
| return resultPairSet; | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why add it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have fixed it