This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.1 by this push:
     new 70079feaa87 branch-2.1: [Bug](mtmv) update mapping relation when mtmv 
occur alter #46983 (#47063)
70079feaa87 is described below

commit 70079feaa87549e8e620e625fee182473a1942b0
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Thu Jan 16 17:07:15 2025 +0800

    branch-2.1: [Bug](mtmv) update mapping relation when mtmv occur alter 
#46983 (#47063)
    
    Cherry-picked from #46983
    
    Co-authored-by: shee <13843187+qz...@users.noreply.github.com>
    Co-authored-by: garenshi <garen...@tencent.com>
---
 .../main/java/org/apache/doris/alter/Alter.java    |  4 ++
 .../org/apache/doris/mtmv/MTMVRelationManager.java |  4 ++
 .../java/org/apache/doris/mtmv/AlterMTMVTest.java  | 79 ++++++++++++++++++++++
 .../apache/doris/utframe/TestWithFeService.java    | 19 ++++++
 4 files changed, 106 insertions(+)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java 
b/fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java
index 5c0390e84e0..d503cf754a8 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java
@@ -550,6 +550,10 @@ public class Alter {
             throws UserException {
         ReplaceTableClause clause = (ReplaceTableClause) alterClauses.get(0);
         String newTblName = clause.getTblName();
+        Table newTable = db.getTableOrMetaException(newTblName);
+        if (newTable.getType() == TableType.MATERIALIZED_VIEW) {
+            throw new DdlException("replace table[" + newTblName + "] cannot 
be a materialized view");
+        }
         boolean swapTable = clause.isSwapTable();
         processReplaceTable(db, origTable, newTblName, swapTable);
     }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVRelationManager.java 
b/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVRelationManager.java
index f3939cb47ea..125b8ce38de 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVRelationManager.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVRelationManager.java
@@ -261,6 +261,10 @@ public class MTMVRelationManager implements 
MTMVHookService {
     public void alterTable(Table table, String oldTableName) {
         BaseTableInfo baseTableInfo = new BaseTableInfo(table);
         baseTableInfo.setTableName(oldTableName);
+        if (table instanceof MTMV) {
+            removeMTMV(baseTableInfo);
+            refreshMTMVCache(((MTMV) table).getRelation(), new 
BaseTableInfo(table));
+        }
         processBaseTableChange(baseTableInfo, "The base table has been 
updated:");
     }
 
diff --git a/fe/fe-core/src/test/java/org/apache/doris/mtmv/AlterMTMVTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/mtmv/AlterMTMVTest.java
new file mode 100644
index 00000000000..17ec145f583
--- /dev/null
+++ b/fe/fe-core/src/test/java/org/apache/doris/mtmv/AlterMTMVTest.java
@@ -0,0 +1,79 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.mtmv;
+
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.MTMV;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.utframe.TestWithFeService;
+
+import com.google.common.collect.Lists;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.Set;
+
+
+public class AlterMTMVTest extends TestWithFeService {
+
+    @Test
+    public void testAlterMTMV() throws Exception {
+        createDatabaseAndUse("test");
+
+        createTable("CREATE TABLE `stu` (`sid` int(32) NULL, `sname` 
varchar(32) NULL)\n"
+                + "ENGINE=OLAP\n"
+                + "DUPLICATE KEY(`sid`)\n"
+                + "DISTRIBUTED BY HASH(`sid`) BUCKETS 1\n"
+                + "PROPERTIES ('replication_allocation' = 
'tag.location.default: 1')");
+
+        createMvByNereids("CREATE MATERIALIZED VIEW mv_a BUILD IMMEDIATE 
REFRESH COMPLETE ON COMMIT\n"
+                + "DISTRIBUTED BY HASH(`sid`) BUCKETS 1\n"
+                + "PROPERTIES ('replication_allocation' = 
'tag.location.default: 1') "
+                + "AS select * from stu limit 1");
+
+        alterMv("ALTER MATERIALIZED VIEW mv_a RENAME mv_b");
+
+        MTMVRelationManager relationManager = 
Env.getCurrentEnv().getMtmvService().getRelationManager();
+        Table table = 
Env.getCurrentInternalCatalog().getDb("test").get().getTableOrMetaException("stu");
+        Set<MTMV> allMTMVs = 
relationManager.getAllMTMVs(Lists.newArrayList(new BaseTableInfo(table)));
+        boolean hasMvA = false;
+        boolean hasMvB = false;
+        for (MTMV mtmv : allMTMVs) {
+            if ("mv_a".equals(mtmv.getName())) {
+                hasMvA = true;
+            }
+            if ("mv_b".equals(mtmv.getName())) {
+                hasMvB = true;
+            }
+        }
+        Assertions.assertFalse(hasMvA);
+        Assertions.assertTrue(hasMvB);
+
+
+        createTable("CREATE TABLE `stu1` (`sid` int(32) NULL, `sname` 
varchar(32) NULL)\n"
+                + "ENGINE=OLAP\n"
+                + "DUPLICATE KEY(`sid`)\n"
+                + "DISTRIBUTED BY HASH(`sid`) BUCKETS 1\n"
+                + "PROPERTIES ('replication_allocation' = 
'tag.location.default: 1')");
+
+        DdlException exception = Assertions.assertThrows(DdlException.class, 
() ->
+                alterTableSync("ALTER TABLE stu1 REPLACE WITH TABLE mv_b 
PROPERTIES('swap' = 'true')"));
+        Assertions.assertEquals("errCode = 2, detailMessage = replace 
table[mv_b] cannot be a materialized view", exception.getMessage());
+    }
+}
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/utframe/TestWithFeService.java 
b/fe/fe-core/src/test/java/org/apache/doris/utframe/TestWithFeService.java
index 76eb0fbb88a..fd3db68e071 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/utframe/TestWithFeService.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/utframe/TestWithFeService.java
@@ -66,6 +66,7 @@ import org.apache.doris.nereids.parser.NereidsParser;
 import org.apache.doris.nereids.rules.RuleType;
 import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
 import org.apache.doris.nereids.trees.plans.commands.AddConstraintCommand;
+import org.apache.doris.nereids.trees.plans.commands.AlterMTMVCommand;
 import org.apache.doris.nereids.trees.plans.commands.CreateMTMVCommand;
 import org.apache.doris.nereids.trees.plans.commands.CreateTableCommand;
 import org.apache.doris.nereids.trees.plans.commands.DropConstraintCommand;
@@ -616,6 +617,11 @@ public abstract class TestWithFeService {
         Env.getCurrentEnv().createDb(createDbStmt);
     }
 
+    public void createDatabaseAndUse(String db) throws Exception {
+        createDatabase(db);
+        useDatabase(db);
+    }
+
     public void createDatabaseWithSql(String createDbSql) throws Exception {
         CreateDbStmt createDbStmt = (CreateDbStmt) 
parseAndAnalyzeStmt(createDbSql);
         Env.getCurrentEnv().createDb(createDbStmt);
@@ -840,6 +846,19 @@ public abstract class TestWithFeService {
         Thread.sleep(100);
     }
 
+    protected void alterMv(String sql) throws Exception {
+        NereidsParser nereidsParser = new NereidsParser();
+        LogicalPlan parsed = nereidsParser.parseSingle(sql);
+        StmtExecutor stmtExecutor = new StmtExecutor(connectContext, sql);
+        if (parsed instanceof AlterMTMVCommand) {
+            ((AlterMTMVCommand) parsed).run(connectContext, stmtExecutor);
+        }
+        checkAlterJob();
+        // waiting table state to normal
+        Thread.sleep(1000);
+    }
+
+
     protected void createMvByNereids(String sql) throws Exception {
         new MockUp<EditLog>() {
             @Mock


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to