diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Catalog.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Catalog.java index 8016f65270..5e4005a453 100755 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Catalog.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Catalog.java @@ -5336,6 +5336,14 @@ public class Catalog { GroupId groupId = null; if (!Strings.isNullOrEmpty(colocateGroup)) { String fullGroupName = db.getId() + "_" + colocateGroup; + //When the new name is the same as the old name, we return it to prevent npe + if (!Strings.isNullOrEmpty(oldGroup)) { + String oldFullGroupName = db.getId() + "_" + oldGroup; + if (oldFullGroupName.equals(fullGroupName)) { + LOG.warn("modify table[{}] group name same as old group name,skip.", table.getName()); + return; + } + } ColocateGroupSchema groupSchema = colocateTableIndex.getGroupSchema(fullGroupName); if (groupSchema == null) { // user set a new colocate group, diff --git a/fe/fe-core/src/test/java/org/apache/doris/catalog/ColocateTableTest.java b/fe/fe-core/src/test/java/org/apache/doris/catalog/ColocateTableTest.java index 1b5d632dbf..7a54231139 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/catalog/ColocateTableTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/catalog/ColocateTableTest.java @@ -17,6 +17,7 @@ package org.apache.doris.catalog; +import org.apache.doris.analysis.AlterTableStmt; import org.apache.doris.analysis.CreateDbStmt; import org.apache.doris.analysis.CreateTableStmt; import org.apache.doris.analysis.DropDbStmt; @@ -90,6 +91,11 @@ public class ColocateTableTest { Catalog.getCurrentCatalog().createTable(createTableStmt); } + private static void alterTable(String sql) throws Exception { + AlterTableStmt alterTableStmt = (AlterTableStmt) UtFrameUtils.parseAndAnalyzeStmt(sql, connectContext); + Catalog.getCurrentCatalog().alterTable(alterTableStmt); + } + @Test public void testCreateOneTable() throws Exception { createTable("create table " + dbName + "." + tableName1 + " (\n" + @@ -318,4 +324,38 @@ public class ColocateTableTest { " \"colocate_with\" = \"" + groupName + "\"\n" + ");"); } + + + @Test + public void testModifyGroupNameForBucketSeqInconsistent() throws Exception { + createTable("create table " + dbName + "." + tableName1 + " (\n" + + " `k1` int NULL COMMENT \"\",\n" + + " `k2` varchar(10) NULL COMMENT \"\"\n" + + ") ENGINE=OLAP\n" + + "DUPLICATE KEY(`k1`, `k2`)\n" + + "COMMENT \"OLAP\"\n" + + "DISTRIBUTED BY HASH(`k1`, `k2`) BUCKETS 1\n" + + "PROPERTIES (\n" + + " \"replication_num\" = \"1\",\n" + + " \"colocate_with\" = \"" + groupName + "\"\n" + + ");"); + + ColocateTableIndex index = Catalog.getCurrentColocateIndex(); + Database db = Catalog.getCurrentCatalog().getDbOrMetaException(fullDbName); + long tableId = db.getTableOrMetaException(tableName1).getId(); + GroupId groupId1 = index.getGroup(tableId); + + Map>> backendIds1 = index.getBackendsPerBucketSeq(groupId1); + Assert.assertEquals(1, backendIds1.get(Tag.DEFAULT_BACKEND_TAG).get(0).size()); + + // set same group name + alterTable("ALTER TABLE "+ dbName + "." + tableName1 + " SET (" + "\"colocate_with\" = \"" + groupName + "\")"); + GroupId groupId2 = index.getGroup(tableId); + + // verify groupId group2BackendsPerBucketSeq + Map>> backendIds2 = index.getBackendsPerBucketSeq(groupId2); + Assert.assertEquals(1, backendIds2.get(Tag.DEFAULT_BACKEND_TAG).get(0).size()); + Assert.assertEquals(groupId1, groupId2); + Assert.assertEquals(backendIds1, backendIds2); + } }