Skip to content

Commit

Permalink
[BugFix] Fix materialized view cannot refresh in different database. …
Browse files Browse the repository at this point in the history
…(backport #52295) (#52453)

Co-authored-by: crossoverJie <[email protected]>
  • Loading branch information
mergify[bot] and crossoverJie authored Nov 14, 2024
1 parent c321b6d commit 90f6b73
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4597,13 +4597,14 @@ public void truncateTable(TruncateTableStmt truncateTableStmt) throws DdlExcepti
// refresh mv
Set<MvId> relatedMvs = olapTable.getRelatedMaterializedViews();
for (MvId mvId : relatedMvs) {
MaterializedView materializedView = (MaterializedView) db.getTable(mvId.getId());
MaterializedView materializedView = (MaterializedView) getTable(mvId.getDbId(), mvId.getId());
if (materializedView == null) {
LOG.warn("Table related materialized view {} can not be found", mvId.getId());
LOG.warn("Table related materialized view {}.{} can not be found", mvId.getDbId(), mvId.getId());
continue;
}
if (materializedView.isLoadTriggeredRefresh()) {
refreshMaterializedView(db.getFullName(), db.getTable(mvId.getId()).getName(), false, null,
Database mvDb = getDb(mvId.getDbId());
refreshMaterializedView(mvDb.getFullName(), getTable(mvDb.getId(), mvId.getId()).getName(), false, null,
Constants.TaskRunPriority.NORMAL.value(), true, false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.starrocks.catalog.LocalTablet;
import com.starrocks.catalog.MaterializedIndex;
import com.starrocks.catalog.MaterializedView;
import com.starrocks.catalog.MvId;
import com.starrocks.catalog.OlapTable;
import com.starrocks.catalog.Partition;
import com.starrocks.catalog.Replica;
Expand All @@ -36,6 +37,7 @@
import com.starrocks.sql.ast.DropPartitionClause;
import com.starrocks.sql.ast.InsertStmt;
import com.starrocks.sql.ast.RefreshMaterializedViewStatement;
import com.starrocks.sql.ast.TruncateTableStmt;
import com.starrocks.sql.optimizer.rule.transformation.materialization.MvRewriteTestBase;
import com.starrocks.sql.plan.ExecPlan;
import com.starrocks.utframe.UtFrameUtils;
Expand Down Expand Up @@ -746,6 +748,46 @@ public void testMaterializedViewPartitionTTL() throws Exception {

}

@Test
public void testTruncateTableInDiffDb() throws Exception {
starRocksAssert
.createDatabaseIfNotExists("trunc_db")
.useDatabase("trunc_db")
.withTable("CREATE TABLE t1 \n" +
"(\n" +
" k1 int,\n" +
" v1 int\n" +
")\n" +
"PROPERTIES('replication_num' = '1');");

starRocksAssert.createDatabaseIfNotExists("mv_db")
.useDatabase("mv_db")
.withMaterializedView("CREATE MATERIALIZED VIEW test_mv\n"
+ "DISTRIBUTED BY HASH(`k1`)\n"
+ "REFRESH ASYNC\n"
+ "AS SELECT k1 from trunc_db.t1;");

executeInsertSql(connectContext, "insert into trunc_db.t1 values(2, 10)");
MaterializedView mv1 = getMv("mv_db", "test_mv");
MaterializedViewMetricsEntity mvEntity =
(MaterializedViewMetricsEntity) MaterializedViewMetricsRegistry.getInstance().getMetricsEntity(mv1.getMvId());
long count = mvEntity.histRefreshJobDuration.getCount();
Assert.assertEquals(0, count);

Table table = getTable("trunc_db", "t1");
// Simulate writing to a non-existent MV
table.addRelatedMaterializedView(new MvId(1,1));
String truncateStr = "truncate table trunc_db.t1;";
TruncateTableStmt truncateTableStmt = (TruncateTableStmt) UtFrameUtils.parseStmtWithNewParser(truncateStr, connectContext);
GlobalStateMgr.getCurrentState().getLocalMetastore().truncateTable(truncateTableStmt);
starRocksAssert.waitRefreshFinished(mv1.getId());

mvEntity =
(MaterializedViewMetricsEntity) MaterializedViewMetricsRegistry.getInstance().getMetricsEntity(mv1.getMvId());
count = mvEntity.histRefreshJobDuration.getCount();
Assert.assertEquals(1, count);
}

@Test
public void testDropPartitionTableInDiffDb() throws Exception {
starRocksAssert
Expand Down

0 comments on commit 90f6b73

Please sign in to comment.