Skip to content

Commit

Permalink
branch-3.0: [Bug](mtmv) update mapping relation when mtmv occur alter #…
Browse files Browse the repository at this point in the history
…46983 (#47062)

Cherry-picked from #46983

Co-authored-by: shee <[email protected]>
Co-authored-by: garenshi <[email protected]>
  • Loading branch information
3 people authored Feb 22, 2025
1 parent 75c97e6 commit e4367e2
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
4 changes: 4 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ private void processReplaceTable(Database db, OlapTable origTable, List<AlterCla
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ public void dropTable(Table table) {
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:");
}

Expand Down
79 changes: 79 additions & 0 deletions fe/fe-core/src/test/java/org/apache/doris/mtmv/AlterMTMVTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
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;
Expand Down Expand Up @@ -630,6 +631,11 @@ public void createDatabase(String db) throws Exception {
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);
Expand Down Expand Up @@ -854,6 +860,19 @@ protected void createMv(String sql) throws Exception {
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
Expand Down

0 comments on commit e4367e2

Please sign in to comment.