-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[improvement](mtmv) Support to get tables in materialized view when collecting table in plan #32797
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 3 commits into
apache:master
from
seawinde:support_get_base_table_recursion
Mar 28, 2024
Merged
Changes from all commits
Commits
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
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
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 |
|---|---|---|
|
|
@@ -17,53 +17,100 @@ | |
|
|
||
| package org.apache.doris.nereids.trees.plans.visitor; | ||
|
|
||
| import org.apache.doris.catalog.MTMV; | ||
| import org.apache.doris.catalog.TableIf; | ||
| import org.apache.doris.catalog.TableIf.TableType; | ||
| import org.apache.doris.common.AnalysisException; | ||
| import org.apache.doris.mtmv.MTMVCache; | ||
| import org.apache.doris.mtmv.MTMVPlanUtil; | ||
| import org.apache.doris.nereids.trees.plans.Plan; | ||
| import org.apache.doris.nereids.trees.plans.algebra.CatalogRelation; | ||
| import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation; | ||
| import org.apache.doris.nereids.trees.plans.physical.PhysicalCatalogRelation; | ||
| import org.apache.doris.nereids.trees.plans.visitor.TableCollector.TableCollectorContext; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Collect the table in plan | ||
| * Note: will not get table if table is eliminated by EmptyRelation in rewrite. | ||
| * View expand is in RBO, if call this method with the plan after RBO, this will get base tables in view, or will not. | ||
| * Materialized view is extended or not can be controlled by the field expand | ||
| */ | ||
| public class TableCollector extends DefaultPlanVisitor<Void, TableCollectorContext> { | ||
| public class TableCollector extends DefaultPlanVisitor<Plan, TableCollectorContext> { | ||
|
|
||
| public static final TableCollector INSTANCE = new TableCollector(); | ||
| private static final Logger LOG = LogManager.getLogger(TableCollector.class); | ||
|
|
||
| @Override | ||
| public Void visit(Plan plan, TableCollectorContext context) { | ||
| if (plan instanceof CatalogRelation) { | ||
| TableIf table = ((CatalogRelation) plan).getTable(); | ||
| if (context.getTargetTableTypes().isEmpty() || context.getTargetTableTypes().contains(table.getType())) { | ||
| context.getCollectedTables().add(table); | ||
| } | ||
| public Plan visitLogicalCatalogRelation(LogicalCatalogRelation catalogRelation, TableCollectorContext context) { | ||
| TableIf table = catalogRelation.getTable(); | ||
| if (context.getTargetTableTypes().isEmpty() || context.getTargetTableTypes().contains(table.getType())) { | ||
| context.getCollectedTables().add(table); | ||
| } | ||
| if (table instanceof MTMV) { | ||
| expandMvAndCollect((MTMV) table, context); | ||
| } | ||
| return catalogRelation; | ||
| } | ||
|
|
||
| @Override | ||
| public Plan visitPhysicalCatalogRelation(PhysicalCatalogRelation catalogRelation, TableCollectorContext context) { | ||
| TableIf table = catalogRelation.getTable(); | ||
| if (context.getTargetTableTypes().isEmpty() || context.getTargetTableTypes().contains(table.getType())) { | ||
| context.getCollectedTables().add(table); | ||
| } | ||
| if (table instanceof MTMV) { | ||
| expandMvAndCollect((MTMV) table, context); | ||
| } | ||
| return catalogRelation; | ||
| } | ||
|
|
||
| private void expandMvAndCollect(MTMV mtmv, TableCollectorContext context) { | ||
| if (!context.isExpand()) { | ||
| return; | ||
| } | ||
| try { | ||
| MTMVCache expandedMv = MTMVCache.from(mtmv, MTMVPlanUtil.createMTMVContext(mtmv)); | ||
| expandedMv.getLogicalPlan().accept(this, context); | ||
| } catch (AnalysisException e) { | ||
| LOG.error(String.format( | ||
| "table collector expand fail, mtmv name is %s, targetTableTypes is %s", | ||
| mtmv.getName(), context.targetTableTypes), e); | ||
| throw new org.apache.doris.nereids.exceptions.AnalysisException( | ||
| String.format("expand mv and collect table fail, mv name is %s, mv sql is %s", | ||
| mtmv.getName(), mtmv.getQuerySql()), e); | ||
| } | ||
| return super.visit(plan, context); | ||
| } | ||
|
|
||
| /** | ||
| * The context for table collecting, it contains the target collect table types | ||
| * and the result of collect. | ||
| */ | ||
| public static final class TableCollectorContext { | ||
| private final List<TableIf> collectedTables = new ArrayList<>(); | ||
| private final Set<TableIf> collectedTables = new HashSet<>(); | ||
| private final Set<TableType> targetTableTypes; | ||
| // if expand the mv or not | ||
| private final boolean expand; | ||
|
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. always true now?
Contributor
Author
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. this method provides an ability which can set expland true or false. in the scene, it's should always be true |
||
|
|
||
| public TableCollectorContext(Set<TableType> targetTableTypes) { | ||
| public TableCollectorContext(Set<TableType> targetTableTypes, boolean expand) { | ||
| this.targetTableTypes = targetTableTypes; | ||
| this.expand = expand; | ||
| } | ||
|
|
||
| public List<TableIf> getCollectedTables() { | ||
| public Set<TableIf> getCollectedTables() { | ||
| return collectedTables; | ||
| } | ||
|
|
||
| public Set<TableType> getTargetTableTypes() { | ||
| return targetTableTypes; | ||
| } | ||
|
|
||
| public boolean isExpand() { | ||
| return expand; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
do not throw exception again?
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, should throw exception