|
35 | 35 | import org.apache.doris.catalog.Tablet; |
36 | 36 | import org.apache.doris.catalog.View; |
37 | 37 | import org.apache.doris.catalog.info.TableNameInfo; |
| 38 | +import org.apache.doris.catalog.stream.BaseTableStream; |
38 | 39 | import org.apache.doris.cloud.alter.CloudSchemaChangeHandler; |
39 | 40 | import org.apache.doris.common.AnalysisException; |
40 | 41 | import org.apache.doris.common.Config; |
|
49 | 50 | import org.apache.doris.datasource.ExternalTable; |
50 | 51 | import org.apache.doris.info.TableNameInfoUtils; |
51 | 52 | import org.apache.doris.mtmv.BaseTableInfo; |
| 53 | +import org.apache.doris.mtmv.MTMVPartitionUtil; |
| 54 | +import org.apache.doris.mtmv.MTMVPropertyUtil; |
| 55 | +import org.apache.doris.mtmv.MTMVRelation; |
| 56 | +import org.apache.doris.mtmv.MTMVUtil; |
| 57 | +import org.apache.doris.mtmv.ivm.IvmUtil; |
52 | 58 | import org.apache.doris.nereids.trees.plans.commands.AlterSystemCommand; |
53 | 59 | import org.apache.doris.nereids.trees.plans.commands.AlterTableCommand; |
54 | 60 | import org.apache.doris.nereids.trees.plans.commands.AlterViewCommand; |
| 61 | +import org.apache.doris.nereids.trees.plans.commands.CreateMTMVCommand; |
55 | 62 | import org.apache.doris.nereids.trees.plans.commands.CreateMaterializedViewCommand; |
56 | 63 | import org.apache.doris.nereids.trees.plans.commands.DropMaterializedViewCommand; |
57 | 64 | import org.apache.doris.nereids.trees.plans.commands.info.AddColumnOp; |
@@ -359,6 +366,8 @@ private boolean processAlterOlapTableInternal(List<AlterOp> alterOps, OlapTable |
359 | 366 | Env.getCurrentEnv().getMtmvService() |
360 | 367 | .alterTable(oldBaseTableInfo, newBaseTableInfo, currentAlterOps.hasReplaceTableOp()); |
361 | 368 | } |
| 369 | + markIvmBinlogBrokenIfNeeded(oldBaseTableInfo, newBaseTableInfo, |
| 370 | + currentAlterOps.hasReplaceTableOp(), alterOps); |
362 | 371 |
|
363 | 372 | olapTable.writeLock(); |
364 | 373 | try { |
@@ -455,6 +464,38 @@ private boolean needChangeMTMVState(List<AlterOp> alterOps) { |
455 | 464 | return false; |
456 | 465 | } |
457 | 466 |
|
| 467 | + private void markIvmBinlogBrokenIfNeeded(BaseTableInfo oldBaseTableInfo, |
| 468 | + Optional<BaseTableInfo> newBaseTableInfo, boolean isReplaceTable, List<AlterOp> alterOps) { |
| 469 | + if (!needMarkIvmBinlogBroken(alterOps)) { |
| 470 | + return; |
| 471 | + } |
| 472 | + if (isReplaceTable) { |
| 473 | + // REPLACE TABLE A WITH TABLE B affects dependencies of both logical names: |
| 474 | + // swap=true: A points to old B, and B points to old A. |
| 475 | + // swap=false: A points to old B, and B no longer exists. |
| 476 | + // Mark both dependency sets broken. Stream recovery is handled after full refresh. |
| 477 | + Env.getCurrentEnv().getMtmvService().getRelationManager() |
| 478 | + .markIvmBinlogBroken(oldBaseTableInfo, "Base table replace changed IVM baseline"); |
| 479 | + if (newBaseTableInfo.isPresent()) { |
| 480 | + Env.getCurrentEnv().getMtmvService().getRelationManager() |
| 481 | + .markIvmBinlogBroken(newBaseTableInfo.get(), "Base table replace changed IVM baseline"); |
| 482 | + } |
| 483 | + } else { |
| 484 | + Env.getCurrentEnv().getMtmvService().getRelationManager().markIvmBinlogBroken( |
| 485 | + oldBaseTableInfo, "Base table alter changed IVM baseline"); |
| 486 | + } |
| 487 | + } |
| 488 | + |
| 489 | + private boolean needMarkIvmBinlogBroken(List<AlterOp> alterOps) { |
| 490 | + for (AlterOp alterOp : alterOps) { |
| 491 | + if (alterOp instanceof ReplaceTableOp |
| 492 | + || alterOp instanceof DropColumnOp) { |
| 493 | + return true; |
| 494 | + } |
| 495 | + } |
| 496 | + return false; |
| 497 | + } |
| 498 | + |
458 | 499 | private void processModifyTableComment(Database db, OlapTable tbl, AlterOp alterOp) |
459 | 500 | throws DdlException { |
460 | 501 | tbl.writeLockOrDdlException(); |
@@ -1286,6 +1327,15 @@ public void processAlterMTMV(AlterMTMV alterMTMV, boolean isReplay) { |
1286 | 1327 | mtmv.alterStatus(alterMTMV.getStatus()); |
1287 | 1328 | break; |
1288 | 1329 | case ALTER_PROPERTY: |
| 1330 | + if (mtmv.isIvm() && alterMTMV.getMvProperties().containsKey( |
| 1331 | + PropertyAnalyzer.PROPERTIES_EXCLUDED_TRIGGER_TABLES)) { |
| 1332 | + Set<TableNameInfo> oldExcludedTriggerTables = mtmv.getExcludedTriggerTables(); |
| 1333 | + Set<TableNameInfo> newExcludedTriggerTables = MTMVPropertyUtil.parseTableNameInfos( |
| 1334 | + alterMTMV.getMvProperties().get( |
| 1335 | + PropertyAnalyzer.PROPERTIES_EXCLUDED_TRIGGER_TABLES)); |
| 1336 | + updateIvmStreamsForExcludedTables(db, mtmv, oldExcludedTriggerTables, |
| 1337 | + newExcludedTriggerTables, isReplay); |
| 1338 | + } |
1289 | 1339 | mtmv.alterMvProperties(alterMTMV.getMvProperties()); |
1290 | 1340 | break; |
1291 | 1341 | case ADD_TASK: |
@@ -1316,4 +1366,63 @@ public void processAlterMTMV(AlterMTMV alterMTMV, boolean isReplay) { |
1316 | 1366 | LOG.warn(e); |
1317 | 1367 | } |
1318 | 1368 | } |
| 1369 | + |
| 1370 | + private void updateIvmStreamsForExcludedTables(Database db, MTMV mtmv, |
| 1371 | + Set<TableNameInfo> oldExcludedTriggerTables, Set<TableNameInfo> newExcludedTriggerTables, |
| 1372 | + boolean isReplay) throws UserException { |
| 1373 | + MTMVRelation relation = mtmv.getRelation(); |
| 1374 | + if (relation == null || relation.getBaseTables() == null) { |
| 1375 | + return; |
| 1376 | + } |
| 1377 | + Set<BaseTableInfo> baseTables = relation.getBaseTables(); |
| 1378 | + boolean hasNewlyIncludedBaseTable = false; |
| 1379 | + for (BaseTableInfo baseTableInfo : baseTables) { |
| 1380 | + TableNameInfo baseTableName = new TableNameInfo(baseTableInfo.getCtlName(), |
| 1381 | + baseTableInfo.getDbName(), baseTableInfo.getTableName()); |
| 1382 | + boolean wasExcluded = MTMVPartitionUtil.isTableExcluded( |
| 1383 | + oldExcludedTriggerTables, baseTableName); |
| 1384 | + boolean isExcluded = MTMVPartitionUtil.isTableExcluded( |
| 1385 | + newExcludedTriggerTables, baseTableName); |
| 1386 | + if (wasExcluded && !isExcluded) { |
| 1387 | + hasNewlyIncludedBaseTable = true; |
| 1388 | + if (!isReplay) { |
| 1389 | + TableIf baseTable = MTMVUtil.getTable(baseTableInfo); |
| 1390 | + CreateMTMVCommand.createTableStream(ConnectContext.get(), db, mtmv, baseTable, |
| 1391 | + baseTableInfo.getDbName()); |
| 1392 | + } |
| 1393 | + } |
| 1394 | + } |
| 1395 | + if (hasNewlyIncludedBaseTable) { |
| 1396 | + mtmv.markIvmBinlogBroken(); |
| 1397 | + } |
| 1398 | + db.writeLockOrDdlException(); |
| 1399 | + try { |
| 1400 | + for (BaseTableInfo baseTableInfo : baseTables) { |
| 1401 | + TableNameInfo baseTableName = new TableNameInfo(baseTableInfo.getCtlName(), |
| 1402 | + baseTableInfo.getDbName(), baseTableInfo.getTableName()); |
| 1403 | + if (!MTMVPartitionUtil.isTableExcluded(newExcludedTriggerTables, baseTableName)) { |
| 1404 | + continue; |
| 1405 | + } |
| 1406 | + String streamName = IvmUtil.streamName(mtmv.getId(), baseTableInfo.getTableName()); |
| 1407 | + TableIf streamTable = db.getTableNullable(streamName); |
| 1408 | + if (streamTable == null) { |
| 1409 | + continue; |
| 1410 | + } |
| 1411 | + if (!(streamTable instanceof BaseTableStream)) { |
| 1412 | + continue; |
| 1413 | + } |
| 1414 | + Table table = (Table) streamTable; |
| 1415 | + table.writeLock(); |
| 1416 | + try { |
| 1417 | + Env.getCurrentEnv().unprotectDropTable(db, table, true, isReplay, 0L); |
| 1418 | + } finally { |
| 1419 | + table.writeUnlock(); |
| 1420 | + } |
| 1421 | + LOG.info("dropped IVM stream {} because its base table is excluded from MTMV", |
| 1422 | + streamName); |
| 1423 | + } |
| 1424 | + } finally { |
| 1425 | + db.writeUnlock(); |
| 1426 | + } |
| 1427 | + } |
1319 | 1428 | } |
0 commit comments