Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.9k
[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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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? MemberAuthor 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; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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