morrySnow commented on code in PR #32797: URL: https://github.com/apache/doris/pull/32797#discussion_r1540549189
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/TableCollector.java: ########## @@ -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.common.NereidsException; +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 NereidsException(String.format("expand mv and collect table fail, mv name is %s, mv sql is %s", Review Comment: throw Nereids' AnalysisException -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org