diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java index febad5ea76..a408663b27 100755 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java @@ -61,6 +61,7 @@ import org.apache.doris.analysis.DropFunctionStmt; import org.apache.doris.analysis.DropMaterializedViewStmt; import org.apache.doris.analysis.DropPartitionClause; import org.apache.doris.analysis.DropTableStmt; +import org.apache.doris.analysis.Expr; import org.apache.doris.analysis.FunctionName; import org.apache.doris.analysis.InstallPluginStmt; import org.apache.doris.analysis.LinkDbStmt; @@ -4052,9 +4053,26 @@ public class Env { if (entry.getValue().getColumnByName(newColName) != null) { throw new DdlException("Column name[" + newColName + "] is already used"); } + + // check if have materialized view on rename column + for (Column column : entry.getValue().getSchema()) { + Expr expr = column.getDefineExpr(); + if (expr == null) { + continue; + } + List slots = new ArrayList<>(); + expr.collect(SlotRef.class, slots); + for (SlotRef slot : slots) { + String name = MaterializedIndexMeta + .normalizeName(CreateMaterializedViewStmt.mvColumnBreaker(slot.toSqlWithoutTbl())); + if (!isReplay && name.equals(colName)) { + throw new DdlException("Column[" + colName + "] have materialized view index"); + } + } + } } - // 1. modify MaterializedIndexMeta + // 1. modify old MaterializedIndexMeta boolean hasColumn = false; for (Map.Entry entry : indexIdToMeta.entrySet()) { Column column = entry.getValue().getColumnByName(colName); @@ -4062,16 +4080,6 @@ public class Env { column.setName(newColName); hasColumn = true; } - - // todo: need change complex expr name also - Column mvColumn = entry.getValue().getColumnByName(CreateMaterializedViewStmt.mvColumnBuilder(colName)); - if (mvColumn != null) { - mvColumn.setName(CreateMaterializedViewStmt.mvColumnBuilder(newColName)); - SlotRef slot = (SlotRef) mvColumn.getDefineExpr(); - slot.setCol(newColName); - slot.setLabel('`' + newColName + '`'); - hasColumn = true; - } } if (!hasColumn) { throw new DdlException("Column[" + colName + "] does not exists"); @@ -4084,12 +4092,6 @@ public class Env { if (column.getName().equalsIgnoreCase(colName)) { column.setName(newColName); } - if (column.getName().equalsIgnoreCase(CreateMaterializedViewStmt.mvColumnBuilder(colName))) { - column.setName(CreateMaterializedViewStmt.mvColumnBuilder(newColName)); - SlotRef slot = (SlotRef) column.getDefineExpr(); - slot.setCol(newColName); - slot.setLabel('`' + newColName + '`'); - } } // 3. modify index @@ -4100,9 +4102,6 @@ public class Env { if (columns.get(i).equalsIgnoreCase(colName)) { columns.set(i, newColName); } - if (columns.get(i).equalsIgnoreCase(CreateMaterializedViewStmt.mvColumnBuilder(colName))) { - columns.set(i, CreateMaterializedViewStmt.mvColumnBuilder(newColName)); - } } } @@ -4114,12 +4113,6 @@ public class Env { if (column.getName().equalsIgnoreCase(colName)) { column.setName(newColName); } - if (column.getName().equalsIgnoreCase(CreateMaterializedViewStmt.mvColumnBuilder(colName))) { - column.setName(CreateMaterializedViewStmt.mvColumnBuilder(newColName)); - SlotRef slot = (SlotRef) column.getDefineExpr(); - slot.setCol(newColName); - slot.setLabel('`' + newColName + '`'); - } } } diff --git a/regression-test/data/schema_change_p0/test_rename_column.out b/regression-test/data/schema_change_p0/test_rename_column.out index 5bfcba17d8..3e8d56d043 100644 Binary files a/regression-test/data/schema_change_p0/test_rename_column.out and b/regression-test/data/schema_change_p0/test_rename_column.out differ diff --git a/regression-test/suites/schema_change_p0/test_rename_column.groovy b/regression-test/suites/schema_change_p0/test_rename_column.groovy index 309573a8d8..af95037427 100644 --- a/regression-test/suites/schema_change_p0/test_rename_column.groovy +++ b/regression-test/suites/schema_change_p0/test_rename_column.groovy @@ -240,7 +240,10 @@ suite ("test_rename_column") { qt_select """ select user_id, sum(cost) from ${tableName} group by user_id order by user_id """ - sql """ ALTER TABLE ${tableName} RENAME COLUMN user_id new_user_id """ + test { + sql """ ALTER TABLE ${tableName} RENAME COLUMN user_id new_user_id """ + exception "errCode = 2," + } sql """ INSERT INTO ${tableName} VALUES (2, '2017-10-01', 'Beijing', 10, 1, 1, 31, 21, hll_hash(2), to_bitmap(2)) @@ -250,9 +253,9 @@ suite ("test_rename_column") { """ qt_desc """ desc ${tableName} """ - qt_select""" select * from ${tableName} order by new_user_id """ + qt_select""" select * from ${tableName} order by user_id """ - qt_select """ select new_user_id, sum(cost) from ${tableName} group by new_user_id order by new_user_id """ + qt_select """ select user_id, sum(cost) from ${tableName} group by user_id order by user_id """ sql """ DROP TABLE ${tableName} """