Skip to content
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

[BugFix] Fix materialized view cannot refresh in different database. (backport #52295) #52453

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading