Skip to content

Commit

Permalink
[fix](mtmv) cannot refresh mtmv when base table recreating (#41762) (#…
Browse files Browse the repository at this point in the history
…42340)

pick from master #41762

fix in the scenario of recreating a table,
the materialized view may assume that the data has not changed
  • Loading branch information
zddr authored Nov 1, 2024
1 parent 8034ab1 commit 959bd31
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3163,7 +3163,7 @@ public MTMVSnapshotIf getPartitionSnapshot(String partitionName, MTMVRefreshCont
public MTMVSnapshotIf getTableSnapshot(MTMVRefreshContext context) {
Map<Long, Long> tableVersions = context.getBaseVersions().getTableVersions();
long visibleVersion = tableVersions.containsKey(id) ? tableVersions.get(id) : getVisibleVersion();
return new MTMVVersionSnapshot(visibleVersion);
return new MTMVVersionSnapshot(visibleVersion, id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.doris.catalog.MTMV;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.Partition;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.common.AnalysisException;

import com.google.common.collect.Maps;
Expand Down Expand Up @@ -89,6 +90,13 @@ public void compatible(MTMV mtmv) {
} catch (Throwable e) {
LOG.warn("MTMV compatibleTables failed, mtmv: {}", mtmv.getName(), e);
}

try {
// snapshot add tableId resolve problem of recreate table
compatibleTablesSnapshot();
} catch (Throwable e) {
LOG.warn("MTMV compatibleTables failed, mtmv: {}", mtmv.getName(), e);
}
}

private void compatiblePartitions(MTMV mtmv) throws AnalysisException {
Expand Down Expand Up @@ -116,6 +124,32 @@ private boolean checkHasDataWithoutPartitionId() {
return false;
}

private void compatibleTablesSnapshot() {
if (!checkHasDataWithoutTableId()) {
return;
}
for (Entry<BaseTableInfo, MTMVSnapshotIf> entry : tablesInfo.entrySet()) {
MTMVVersionSnapshot versionSnapshot = (MTMVVersionSnapshot) entry.getValue();
if (versionSnapshot.getId() == 0) {
try {
TableIf table = MTMVUtil.getTable(entry.getKey());
versionSnapshot.setId(table.getId());
} catch (AnalysisException e) {
LOG.warn("MTMV compatibleTablesSnapshot failed, can not get table by: {}", entry.getKey());
}
}
}
}

private boolean checkHasDataWithoutTableId() {
for (MTMVSnapshotIf snapshot : tablesInfo.values()) {
if (snapshot instanceof MTMVVersionSnapshot && ((MTMVVersionSnapshot) snapshot).getId() == 0) {
return true;
}
}
return false;
}

private void compatibleTables(MTMV mtmv) {
if (tables.size() == tablesInfo.size()) {
return;
Expand Down
13 changes: 13 additions & 0 deletions regression-test/data/mtmv_p0/test_recreate_table_mtmv.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !mv1 --
21 mysql test1

-- !mv2 --
mysql 1

-- !mv1_recreate --
21 doris test1

-- !mv2_recreate --
doris 1

91 changes: 91 additions & 0 deletions regression-test/suites/mtmv_p0/test_recreate_table_mtmv.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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.

import org.junit.Assert;

suite("test_recreate_table_mtmv","mtmv") {
String suiteName = "test_recreate_table_mtmv"
String tableName1 = "${suiteName}_table1"
String tableName2 = "${suiteName}_table2"
String mvName1 = "${suiteName}_mv1"
String mvName2 = "${suiteName}_mv2"
sql """drop table if exists `${tableName1}`"""
sql """drop table if exists `${tableName2}`"""
sql """drop materialized view if exists ${mvName1};"""
sql """drop materialized view if exists ${mvName2};"""

sql """
CREATE TABLE `${tableName1}` (
`id` int NULL,
`first_name` varchar(255) NULL,
`last_name` varchar(255) NULL
) ENGINE=OLAP
AGGREGATE KEY(`id`, `first_name`, `last_name`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`id`) BUCKETS 2
PROPERTIES ('replication_num' = '1') ;
"""
sql """
INSERT INTO `${tableName1}` (`id`, `first_name`, `last_name`) VALUES (21, 'mysql', 'test1');
"""
sql """
CREATE TABLE `${tableName2}` (
`id` int NULL,
`first_name` varchar(255) NULL,
`last_name` varchar(255) NULL
) ENGINE=OLAP
AGGREGATE KEY(`id`, `first_name`, `last_name`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`id`) BUCKETS 2
PROPERTIES ('replication_num' = '1') ;
"""
sql """
INSERT INTO `${tableName2}` (`id`, `first_name`, `last_name`) VALUES (21, 'doris', 'test1');
"""
sql """
CREATE MATERIALIZED VIEW IF NOT EXISTS ${mvName1} BUILD IMMEDIATE REFRESH AUTO ON COMMIT DISTRIBUTED BY RANDOM BUCKETS 2
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
) AS (SELECT * FROM ${tableName1} );
"""
waitingMTMVTaskFinishedByMvName(mvName1);
order_qt_mv1 "SELECT * FROM ${mvName1}"
sql """
CREATE MATERIALIZED VIEW IF NOT EXISTS ${mvName2} BUILD IMMEDIATE REFRESH AUTO ON COMMIT DISTRIBUTED BY RANDOM BUCKETS 2
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
) AS (SELECT first_name,count(last_name) FROM ${mvName1} GROUP BY first_name);
"""
waitingMTMVTaskFinishedByMvName(mvName2);
order_qt_mv2 "SELECT * FROM ${mvName2}"
sql """drop materialized view if exists ${mvName1};"""
sql """
CREATE MATERIALIZED VIEW IF NOT EXISTS ${mvName1} BUILD IMMEDIATE REFRESH AUTO ON COMMIT DISTRIBUTED BY RANDOM BUCKETS 2
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
) AS (SELECT * FROM ${tableName2} );
"""
waitingMTMVTaskFinishedByMvName(mvName1);
order_qt_mv1_recreate "SELECT * FROM ${mvName1}"
waitingMTMVTaskFinishedByMvName(mvName2);
order_qt_mv2_recreate "SELECT * FROM ${mvName2}"

sql """drop table if exists `${tableName1}`"""
sql """drop table if exists `${tableName2}`"""
sql """drop materialized view if exists ${mvName1};"""
sql """drop materialized view if exists ${mvName2};"""
}

0 comments on commit 959bd31

Please sign in to comment.