Skip to content

Commit

Permalink
[BugFix] Fix check sync materialized view exist bug (#30038)
Browse files Browse the repository at this point in the history
* Fix check sync materialized view exist bug

Signed-off-by: shuming.li <ming.moriarty@gmail.com>

* fix bugs

Signed-off-by: shuming.li <ming.moriarty@gmail.com>

---------

Signed-off-by: shuming.li <ming.moriarty@gmail.com>
  • Loading branch information
LiShuMing committed Aug 29, 2023
1 parent d07e28c commit d299ddd
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import com.starrocks.catalog.LocalTablet;
import com.starrocks.catalog.MaterializedIndex;
import com.starrocks.catalog.MaterializedIndex.IndexState;
import com.starrocks.catalog.MaterializedIndexMeta;
import com.starrocks.catalog.OlapTable;
import com.starrocks.catalog.OlapTable.OlapTableState;
import com.starrocks.catalog.Partition;
Expand Down Expand Up @@ -220,8 +219,6 @@ public void processCreateMaterializedView(CreateMaterializedViewStmt addMVClause

olapTable.setState(OlapTableState.ROLLUP);



GlobalStateMgr.getCurrentState().getEditLog().logAlterJob(rollupJobV2);
LOG.info("finished to create materialized view job: {}", rollupJobV2.getJobId());
}
Expand Down Expand Up @@ -446,21 +443,27 @@ private RollupJobV2 createMaterializedViewJob(String mvName, String baseIndexNam
private List<Column> checkAndPrepareMaterializedView(CreateMaterializedViewStmt addMVClause, Database db,
OlapTable olapTable)
throws DdlException {
// check if mv index already exists
if (olapTable.hasMaterializedIndex(addMVClause.getMVName())) {
throw new DdlException("Materialized view[" + addMVClause.getMVName() + "] already exists");
}
String mvName = addMVClause.getMVName();
// ensure mv's name is unique in the db and olap table's materialized indexes
{
// check if mv index already exists in this table
if (olapTable.hasMaterializedIndex(mvName)) {
throw new DdlException("Materialized view[" + mvName + "] already exists in the table "
+ olapTable.getName());
}

for (Table tbl : db.getTables()) {
if (tbl.getType() == Table.TableType.OLAP) {
if (addMVClause.getMVName().equals(tbl.getName())) {
throw new DdlException("Table [" + addMVClause.getMVName() + "] already exists, ");
}
// check if mv index already exists in db
if (db.tryGetTable(mvName).isPresent()) {
throw new DdlException("Table [" + mvName + "] already exists in the db " + db.getFullName());
}

List<MaterializedIndexMeta> visibleMaterializedViews = ((OlapTable) tbl).getVisibleIndexMetas();
for (MaterializedIndexMeta mvMeta : visibleMaterializedViews) {
if (((OlapTable) tbl).getIndexNameById(mvMeta.getIndexId()).equals(addMVClause.getMVName())) {
throw new DdlException("Materialized view[" + addMVClause.getMVName() + "] already exists");
// check if mv index already exists in other table's materialized indexes
for (Table tbl : db.getTables()) {
if (tbl.isOlapTable()) {
OlapTable otherOlapTable = (OlapTable) tbl;
if (otherOlapTable.getIndexNameToId().size() > 1 && otherOlapTable.hasMaterializedIndex(mvName)) {
throw new DdlException("Materialized view[" + mvName + "] already exists in table "
+ tbl.getName());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.starrocks.alter.AlterJobV2;
import com.starrocks.catalog.BaseTableInfo;
import com.starrocks.catalog.ColocateTableIndex;
import com.starrocks.catalog.Column;
Expand Down Expand Up @@ -2647,6 +2646,7 @@ public void testCreateMVWithDifferentDB() {
Assert.assertTrue(olapTable.getIndexIdToMeta().entrySet().stream()
.anyMatch(x -> x.getValue().getKeysType().isAggregationFamily()));
newStarRocksAssert.dropDatabase("test_mv_different_db");
starRocksAssert.dropMaterializedView("test_mv_use_different_tbl");
} catch (Exception e) {
Assert.fail();
}
Expand Down Expand Up @@ -2693,6 +2693,7 @@ public void testCreateAsyncMVWithDifferentDB() {
newStarRocksAssert.dropDatabase("test_mv_different_db");
Table mv1 = testDb.getTable("test_mv_use_different_tbl");
Assert.assertTrue(mv1 instanceof MaterializedView);
starRocksAssert.dropMaterializedView("test_mv_use_different_tbl");
} catch (Exception e) {
Assert.fail();
}
Expand Down Expand Up @@ -2903,6 +2904,75 @@ public void testSelectFromSyncMV() throws Exception {
Assert.assertTrue(explainString.contains("partitions=2/2\n" +
" rollup: sync_mv1\n" +
" tabletRatio=6/6"));
starRocksAssert.dropMaterializedView("sync_mv1");
}

// create sync mv that mv's name already existed in the db
@Test
public void testCreateSyncMV1() throws Exception {
try {
String sql = "create materialized view aggregate_table_with_null as select k1, sum(v1) from tbl1 group by k1;";
CreateMaterializedViewStmt createTableStmt = (CreateMaterializedViewStmt) UtFrameUtils.
parseStmtWithNewParser(sql, connectContext);
// aggregate_table_with_null already existed in the db
GlobalStateMgr.getCurrentState().getMetadata().createMaterializedView(createTableStmt);
Assert.fail();
} catch (Throwable e) {
Assert.assertTrue(e.getMessage().contains("Table [aggregate_table_with_null] already exists in the db test"));
}
}

// create sync mv that mv's name already existed in the same table
@Test
public void testCreateSyncMV2() throws Exception {
String sql = "create materialized view sync_mv1 as select k1, sum(v1) from tbl1 group by k1;";
CreateMaterializedViewStmt createTableStmt = (CreateMaterializedViewStmt) UtFrameUtils.
parseStmtWithNewParser(sql, connectContext);
GlobalStateMgr.getCurrentState().getMetadata().createMaterializedView(createTableStmt);

waitingRollupJobV2Finish();
OlapTable tbl1 = (OlapTable) (getTable("test", "tbl1"));
Assert.assertTrue(tbl1 != null);
Assert.assertTrue(tbl1.hasMaterializedIndex("sync_mv1"));

try {
// sync_mv1 already existed in the tbl1
sql = "create materialized view sync_mv1 as select k1, sum(v1) from tbl1 group by k1;";
createTableStmt = (CreateMaterializedViewStmt) UtFrameUtils.
parseStmtWithNewParser(sql, connectContext);
GlobalStateMgr.getCurrentState().getMetadata().createMaterializedView(createTableStmt);
Assert.fail();
} catch (Throwable e) {
Assert.assertTrue(e.getMessage().contains("Materialized view[sync_mv1] already exists in " +
"the table tbl1"));
}
starRocksAssert.dropMaterializedView("sync_mv1");
}

// create sync mv that mv's name already existed in other table
@Test
public void testCreateSyncMV3() throws Exception {
String sql = "create materialized view sync_mv1 as select k1, sum(v1) from tbl1 group by k1;";
CreateMaterializedViewStmt createTableStmt = (CreateMaterializedViewStmt) UtFrameUtils.
parseStmtWithNewParser(sql, connectContext);
GlobalStateMgr.getCurrentState().getMetadata().createMaterializedView(createTableStmt);

waitingRollupJobV2Finish();
OlapTable tbl1 = (OlapTable) (getTable("test", "tbl1"));
Assert.assertTrue(tbl1 != null);
Assert.assertTrue(tbl1.hasMaterializedIndex("sync_mv1"));
try {
// sync_mv1 already existed in tbl1
sql = "create materialized view sync_mv1 as select k1, sum(v1) from tbl3 group by k1;";
createTableStmt = (CreateMaterializedViewStmt) UtFrameUtils.
parseStmtWithNewParser(sql, connectContext);
GlobalStateMgr.getCurrentState().getMetadata().createMaterializedView(createTableStmt);
Assert.fail();
} catch (Throwable e) {
Assert.assertTrue(e.getMessage().contains("Materialized view[sync_mv1] already exists " +
"in table tbl1"));
}
starRocksAssert.dropMaterializedView("sync_mv1");
}

@Test
Expand Down

0 comments on commit d299ddd

Please sign in to comment.