diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/MaterializedViewAnalyzer.java b/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/MaterializedViewAnalyzer.java index bf06d6783bc6cb..bc9d984bce50c4 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/MaterializedViewAnalyzer.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/MaterializedViewAnalyzer.java @@ -62,6 +62,7 @@ import com.starrocks.mv.analyzer.MVPartitionSlotRefResolver; import com.starrocks.qe.ConnectContext; import com.starrocks.server.GlobalStateMgr; +import com.starrocks.server.RunMode; import com.starrocks.sql.ast.AlterMaterializedViewStmt; import com.starrocks.sql.ast.AstVisitor; import com.starrocks.sql.ast.CancelRefreshMaterializedViewStmt; @@ -1091,17 +1092,17 @@ private void checkDistribution(CreateMaterializedViewStatement statement, private Short autoInferReplicationNum(Map tableNameTableMap) { // For replication_num, we select the maximum value of all tables replication_num - Short defaultReplicationNum = 1; + Optional maxReplicationFromTable = Optional.empty(); for (Table table : tableNameTableMap.values()) { if (table instanceof OlapTable) { OlapTable olapTable = (OlapTable) table; Short replicationNum = olapTable.getDefaultReplicationNum(); - if (replicationNum > defaultReplicationNum) { - defaultReplicationNum = replicationNum; + if (maxReplicationFromTable.isEmpty() || replicationNum > maxReplicationFromTable.get()) { + maxReplicationFromTable = Optional.of(replicationNum); } } } - return defaultReplicationNum; + return maxReplicationFromTable.orElseGet(RunMode::defaultReplicationNum); } @Override diff --git a/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/MaterializedViewAnalyzerTest.java b/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/MaterializedViewAnalyzerTest.java index ce47de07798107..b42bf2891295a5 100644 --- a/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/MaterializedViewAnalyzerTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/MaterializedViewAnalyzerTest.java @@ -26,10 +26,17 @@ import com.starrocks.catalog.ScalarType; import com.starrocks.catalog.Table; import com.starrocks.catalog.Type; +import com.starrocks.common.Config; import com.starrocks.common.DdlException; import com.starrocks.common.Pair; +import com.starrocks.common.util.PropertyAnalyzer; import com.starrocks.qe.ShowExecutor; import com.starrocks.qe.ShowResultSet; +<<<<<<< HEAD +======= +import com.starrocks.server.GlobalStateMgr; +import com.starrocks.sql.ast.CreateMaterializedViewStatement; +>>>>>>> eb8fdb9a41 ([Enhancement] change default_replication_num of MV (#50931)) import com.starrocks.sql.ast.ShowStmt; import com.starrocks.sql.plan.ConnectorPlanTestBase; import com.starrocks.utframe.StarRocksAssert; @@ -39,6 +46,7 @@ import org.apache.hadoop.util.Lists; import org.junit.Assert; import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; @@ -483,4 +491,32 @@ private void checkQueryOutputIndices(List inputs, String expect, boolea Assert.assertEquals(IntStream.range(0, queryOutputIndices.size()).anyMatch(i -> i != queryOutputIndices.get(i)), isChanged); } + + @Test + public void testReplicationNum() throws Exception { + short defaultReplication = Config.default_replication_num; + final String sql = "create materialized view mv1 refresh manual as " + + "SELECT id, data, date FROM `iceberg0`.`partitioned_db`.`t1` as a;"; + + { + Config.default_replication_num = 1; + CreateMaterializedViewStatement statementBase = + (CreateMaterializedViewStatement) UtFrameUtils.parseStmtWithNewParser(sql, + starRocksAssert.getCtx()); + MaterializedViewAnalyzer.analyze(statementBase, starRocksAssert.getCtx()); + Assertions.assertEquals("1", + statementBase.getProperties().get(PropertyAnalyzer.PROPERTIES_REPLICATION_NUM)); + } + { + Config.default_replication_num = 3; + CreateMaterializedViewStatement statementBase = + (CreateMaterializedViewStatement) UtFrameUtils.parseStmtWithNewParser(sql, + starRocksAssert.getCtx()); + MaterializedViewAnalyzer.analyze(statementBase, starRocksAssert.getCtx()); + Assertions.assertEquals("3", + statementBase.getProperties().get(PropertyAnalyzer.PROPERTIES_REPLICATION_NUM)); + } + + Config.default_replication_num = defaultReplication; + } }