Skip to content

Commit

Permalink
fix drop partition in different db
Browse files Browse the repository at this point in the history
Signed-off-by: crossoverJie <[email protected]>
  • Loading branch information
crossoverJie committed Nov 5, 2024
1 parent 19c386f commit 43848e6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1473,10 +1473,11 @@ public void dropPartition(Database db, Table table, DropPartitionClause clause)
if (!isTempPartition) {
try {
for (MvId mvId : olapTable.getRelatedMaterializedViews()) {
MaterializedView materializedView = (MaterializedView) getTable(db.getId(), mvId.getId());
MaterializedView materializedView = (MaterializedView) getTable(mvId.getDbId(), mvId.getId());
if (materializedView != null && materializedView.isLoadTriggeredRefresh()) {
Database mvDb = getDb(mvId.getDbId());
GlobalStateMgr.getCurrentState().getLocalMetastore().refreshMaterializedView(
db.getFullName(), materializedView.getName(), false, null,
mvDb.getFullName(), materializedView.getName(), false, null,
Constants.TaskRunPriority.NORMAL.value(), true, false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.starrocks.server.GlobalStateMgr;
import com.starrocks.sql.ast.AlterMaterializedViewStmt;
import com.starrocks.sql.ast.DmlStmt;
import com.starrocks.sql.ast.DropPartitionClause;
import com.starrocks.sql.ast.InsertStmt;
import com.starrocks.sql.ast.RefreshMaterializedViewStatement;
import com.starrocks.sql.ast.TruncateTableStmt;
Expand Down Expand Up @@ -1245,4 +1246,52 @@ public void testTruncateTableInDiffDb() throws Exception {
count = mvEntity.histRefreshJobDuration.getCount();
Assert.assertEquals(1, count);
}

@Test
public void testDropPartitionTableInDiffDb() throws Exception {
starRocksAssert
.createDatabaseIfNotExists("drop_db")
.useDatabase("drop_db")
.withTable("CREATE TABLE tbl_with_mv\n" +
"(\n" +
" k1 date,\n" +
" k2 int,\n" +
" v1 int sum\n" +
")\n" +
"PARTITION BY RANGE(k1)\n" +
"(\n" +
" PARTITION p1 values [('2022-02-01'),('2022-02-16')),\n" +
" PARTITION p2 values [('2022-02-16'),('2022-03-01'))\n" +
")\n" +
"DISTRIBUTED BY HASH(k2) BUCKETS 3\n" +
"PROPERTIES('replication_num' = '1');");

starRocksAssert.createDatabaseIfNotExists("mv_db")
.useDatabase("mv_db")
.withMaterializedView("CREATE MATERIALIZED VIEW test_mv\n"
+ "DISTRIBUTED BY HASH(`k2`)\n"
+ "REFRESH ASYNC\n"
+ "AS select k1, k2, v1 from drop_db.tbl_with_mv;");

executeInsertSql(connectContext, "insert into drop_db.tbl_with_mv partition(p2) values(\"2022-02-20\", 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);

OlapTable table = (OlapTable) getTable("drop_db", "tbl_with_mv");
Partition p1 = table.getPartition("p1");
DropPartitionClause dropPartitionClause = new DropPartitionClause(false, p1.getName(), false, true);
dropPartitionClause.setResolvedPartitionNames(ImmutableList.of(p1.getName()));
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb("drop_db");
GlobalStateMgr.getCurrentState().getLocalMetastore().dropPartition(db, table, dropPartitionClause);
// sleep 3s, wait for the refresh job to complete
Thread.sleep(3000);

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

0 comments on commit 43848e6

Please sign in to comment.