|
30 | 30 | import org.apache.doris.catalog.Env; |
31 | 31 | import org.apache.doris.catalog.KeysType; |
32 | 32 | import org.apache.doris.catalog.MTMV; |
| 33 | +import org.apache.doris.catalog.OlapTable; |
33 | 34 | import org.apache.doris.catalog.ScalarType; |
34 | 35 | import org.apache.doris.catalog.TableIf; |
35 | 36 | import org.apache.doris.catalog.TableIf.TableType; |
|
44 | 45 | import org.apache.doris.common.util.Util; |
45 | 46 | import org.apache.doris.datasource.CatalogIf; |
46 | 47 | import org.apache.doris.datasource.ExternalTable; |
| 48 | +import org.apache.doris.info.TableNameInfo; |
47 | 49 | import org.apache.doris.job.exception.JobException; |
48 | 50 | import org.apache.doris.job.task.AbstractTask; |
49 | 51 | import org.apache.doris.mtmv.MTMVPartitionInfo.MTMVPartitionType; |
@@ -496,7 +498,26 @@ public static MTMVAnalyzeQueryInfo analyzeQuery(ConnectContext ctx, Map<String, |
496 | 498 | MTMVPartitionDefinition mvPartitionDefinition, DistributionDescriptor distribution, |
497 | 499 | List<SimpleColumnDefinition> simpleColumnDefinitions, Map<String, String> properties, List<String> keys, |
498 | 500 | LogicalPlan logicalQuery, boolean isIvm) throws UserException { |
499 | | - try (StatementContext statementContext = ctx.getStatementContext()) { |
| 501 | + if (!isIvm) { |
| 502 | + return analyzeQueryInternal(ctx, mvProperties, mvPartitionDefinition, distribution, |
| 503 | + simpleColumnDefinitions, properties, keys, logicalQuery, false, Sets.newHashSet()); |
| 504 | + } |
| 505 | + MTMVAnalyzeQueryInfo queryInfo = analyzeQueryInternal(ctx, mvProperties, mvPartitionDefinition, distribution, |
| 506 | + simpleColumnDefinitions, properties, keys, logicalQuery, true, |
| 507 | + getExcludedTriggerTables(mvProperties)); |
| 508 | + validateIncrementalBaseTableModels(queryInfo.getRelation(), mvProperties); |
| 509 | + return queryInfo; |
| 510 | + } |
| 511 | + |
| 512 | + private static MTMVAnalyzeQueryInfo analyzeQueryInternal(ConnectContext ctx, Map<String, String> mvProperties, |
| 513 | + MTMVPartitionDefinition mvPartitionDefinition, DistributionDescriptor distribution, |
| 514 | + List<SimpleColumnDefinition> simpleColumnDefinitions, Map<String, String> properties, List<String> keys, |
| 515 | + LogicalPlan logicalQuery, boolean isIvm, Set<TableNameInfo> excludedTriggerTables) |
| 516 | + throws UserException { |
| 517 | + StatementContext originalStatementContext = ctx.getStatementContext(); |
| 518 | + try (StatementContext statementContext = new StatementContext(ctx, null)) { |
| 519 | + statementContext.setIvmExcludedTriggerTables(excludedTriggerTables); |
| 520 | + ctx.setStatementContext(statementContext); |
500 | 521 | NereidsPlanner planner = new NereidsPlanner(statementContext); |
501 | 522 | // this is for expression column name infer when not use alias |
502 | 523 | LogicalSink<Plan> logicalSink = new UnboundResultSink<>(logicalQuery); |
@@ -566,6 +587,60 @@ public static MTMVAnalyzeQueryInfo analyzeQuery(ConnectContext ctx, Map<String, |
566 | 587 | queryInfo::setIvmNormalizeResult); |
567 | 588 | } |
568 | 589 | return queryInfo; |
| 590 | + } finally { |
| 591 | + ctx.setStatementContext(originalStatementContext); |
| 592 | + } |
| 593 | + } |
| 594 | + |
| 595 | + private static Set<TableNameInfo> getExcludedTriggerTables(Map<String, String> mvProperties) { |
| 596 | + if (mvProperties == null) { |
| 597 | + return Sets.newHashSet(); |
| 598 | + } |
| 599 | + return MTMVPropertyUtil.parseTableNameInfos( |
| 600 | + mvProperties.get(PropertyAnalyzer.PROPERTIES_EXCLUDED_TRIGGER_TABLES)); |
| 601 | + } |
| 602 | + |
| 603 | + public static void validateIncrementalBaseTableModels(MTMVRelation relation, Map<String, String> mvProperties) { |
| 604 | + Set<TableNameInfo> excludedTriggerTables = getExcludedTriggerTables(mvProperties); |
| 605 | + if (relation == null || relation.getBaseTablesOneLevelAndFromView() == null) { |
| 606 | + return; |
| 607 | + } |
| 608 | + for (BaseTableInfo baseTableInfo : relation.getBaseTablesOneLevelAndFromView()) { |
| 609 | + final TableIf table; |
| 610 | + try { |
| 611 | + table = MTMVUtil.getTable(baseTableInfo); |
| 612 | + } catch (org.apache.doris.common.AnalysisException e) { |
| 613 | + throw new AnalysisException(e.getMessage(), e); |
| 614 | + } |
| 615 | + TableNameInfo tableNameInfo = new TableNameInfo(baseTableInfo.getCtlName(), |
| 616 | + baseTableInfo.getDbName(), baseTableInfo.getTableName()); |
| 617 | + if (MTMVPartitionUtil.isTableExcluded(excludedTriggerTables, tableNameInfo)) { |
| 618 | + continue; |
| 619 | + } |
| 620 | + if (!(table instanceof OlapTable)) { |
| 621 | + throw new AnalysisException( |
| 622 | + "INCREMENTAL materialized view only supports OlapTable base tables. " |
| 623 | + + "Table '" + table.getName() + "' is " + table.getType() + "."); |
| 624 | + } |
| 625 | + OlapTable olapTable = (OlapTable) table; |
| 626 | + if (olapTable.getKeysType() != KeysType.UNIQUE_KEYS |
| 627 | + && olapTable.getKeysType() != KeysType.DUP_KEYS) { |
| 628 | + throw new AnalysisException( |
| 629 | + "INCREMENTAL materialized view requires base tables to be " |
| 630 | + + "UNIQUE_KEYS with Merge-On-Write or DUP_KEYS. Table '" |
| 631 | + + olapTable.getName() + "' is " + olapTable.getKeysType() |
| 632 | + + ". If this table does not participate in incremental refresh, " |
| 633 | + + "add it to 'excluded_trigger_tables'."); |
| 634 | + } |
| 635 | + if (olapTable.getKeysType() == KeysType.UNIQUE_KEYS |
| 636 | + && !olapTable.getEnableUniqueKeyMergeOnWrite()) { |
| 637 | + throw new AnalysisException( |
| 638 | + "INCREMENTAL materialized view requires UNIQUE_KEYS base tables " |
| 639 | + + "to enable Merge-On-Write. Table '" |
| 640 | + + olapTable.getName() + "' has MOW disabled." |
| 641 | + + " If this table does not participate in incremental refresh, " |
| 642 | + + "add it to 'excluded_trigger_tables'."); |
| 643 | + } |
569 | 644 | } |
570 | 645 | } |
571 | 646 |
|
|
0 commit comments