fix rename matview

This commit is contained in:
Alexander Tokmakov 2021-05-19 21:53:31 +03:00
parent 87eedfded8
commit 2996e7c072
3 changed files with 126 additions and 2 deletions

View File

@ -366,17 +366,18 @@ void StorageMaterializedView::renameInMemory(const StorageID & new_table_id)
auto metadata_snapshot = getInMemoryMetadataPtr();
bool from_atomic_to_atomic_database = old_table_id.hasUUID() && new_table_id.hasUUID();
if (has_inner_table && tryGetTargetTable() && !from_atomic_to_atomic_database)
if (!from_atomic_to_atomic_database && has_inner_table && tryGetTargetTable())
{
auto new_target_table_name = generateInnerTableName(new_table_id);
auto rename = std::make_shared<ASTRenameQuery>();
ASTRenameQuery::Table from;
assert(target_table_id.database_name == old_table_id.database_name);
from.database = target_table_id.database_name;
from.table = target_table_id.table_name;
ASTRenameQuery::Table to;
to.database = target_table_id.database_name;
to.database = new_table_id.database_name;
to.table = new_target_table_name;
ASTRenameQuery::Element elem;
@ -385,10 +386,16 @@ void StorageMaterializedView::renameInMemory(const StorageID & new_table_id)
rename->elements.emplace_back(elem);
InterpreterRenameQuery(rename, getContext()).execute();
target_table_id.database_name = new_table_id.database_name;
target_table_id.table_name = new_target_table_name;
}
IStorage::renameInMemory(new_table_id);
if (from_atomic_to_atomic_database && has_inner_table)
{
assert(target_table_id.database_name == old_table_id.database_name);
target_table_id.database_name = new_table_id.database_name;
}
const auto & select_query = metadata_snapshot->getSelectQuery();
// TODO Actually we don't need to update dependency if MV has UUID, but then db and table name will be outdated
DatabaseCatalog::instance().updateDependency(select_query.select_table_id, old_table_id, select_query.select_table_id, getStorageID());

View File

@ -0,0 +1,58 @@
1 .inner.mv1 before moving tablesmv1
1 dst before moving tablesmv2
1 mv1 before moving tablesmv1
1 mv2 before moving tablesmv2
1 src before moving tables
ordinary:
.inner.mv1
dst
mv1
mv2
src
ordinary after rename:
atomic after rename:
.inner_id.
dst
mv1
mv2
src
3 .inner_id. after renaming databasemv1
3 .inner_id. before moving tablesmv1
3 dst after renaming databasemv2
3 dst before moving tablesmv2
3 mv1 after renaming databasemv1
3 mv1 before moving tablesmv1
3 mv2 after renaming databasemv2
3 mv2 before moving tablesmv2
3 src after moving tables
3 src after renaming database
3 src before moving tables
.inner_id.
dst
mv1
mv2
src
CREATE DATABASE test_01155_atomic\nENGINE = Atomic
4 .inner.mv1 after renaming databasemv1
4 .inner.mv1 after renaming tablesmv1
4 .inner.mv1 before moving tablesmv1
4 dst after renaming databasemv2
4 dst after renaming tablesmv2
4 dst before moving tablesmv2
4 mv1 after renaming databasemv1
4 mv1 after renaming tablesmv1
4 mv1 before moving tablesmv1
4 mv2 after renaming databasemv2
4 mv2 after renaming tablesmv2
4 mv2 before moving tablesmv2
4 src after moving tables
4 src after renaming database
4 src after renaming tables
4 src before moving tables
test_01155_ordinary:
.inner.mv1
dst
mv1
mv2
src
test_01155_atomic:

View File

@ -0,0 +1,59 @@
DROP DATABASE IF EXISTS test_01155_ordinary;
DROP DATABASE IF EXISTS test_01155_atomic;
CREATE DATABASE test_01155_ordinary ENGINE=Ordinary;
CREATE DATABASE test_01155_atomic ENGINE=Atomic;
USE test_01155_ordinary;
CREATE TABLE src (s String) ENGINE=MergeTree() PARTITION BY tuple() ORDER BY s;
CREATE MATERIALIZED VIEW mv1 (s String) ENGINE=MergeTree() PARTITION BY tuple() ORDER BY s AS SELECT (*,).1 || 'mv1' as s FROM src;
CREATE TABLE dst (s String) ENGINE=MergeTree() PARTITION BY tuple() ORDER BY s;
CREATE MATERIALIZED VIEW mv2 TO dst (s String) AS SELECT (*,).1 || 'mv2' as s FROM src;
INSERT INTO src VALUES ('before moving tables');
SELECT 1, substr(_table, 1, 10), s FROM merge('test_01155_ordinary', '') ORDER BY _table, s;
-- Move tables with materialized views from Ordinary to Atomic
SELECT 'ordinary:';
SHOW TABLES FROM test_01155_ordinary;
RENAME TABLE test_01155_ordinary.mv1 TO test_01155_atomic.mv1;
RENAME TABLE test_01155_ordinary.mv2 TO test_01155_atomic.mv2;
RENAME TABLE test_01155_ordinary.dst TO test_01155_atomic.dst;
RENAME TABLE test_01155_ordinary.src TO test_01155_atomic.src;
SELECT 'ordinary after rename:';
SELECT substr(name, 1, 10) FROM system.tables WHERE database='test_01155_ordinary';
SELECT 'atomic after rename:';
SELECT substr(name, 1, 10) FROM system.tables WHERE database='test_01155_atomic';
DROP DATABASE test_01155_ordinary;
USE default;
INSERT INTO test_01155_atomic.src VALUES ('after moving tables');
SELECT 2, substr(_table, 1, 10), s FROM merge('test_01155_atomic', '') ORDER BY _table, s; -- { serverError 81 }
RENAME DATABASE test_01155_atomic TO test_01155_ordinary;
USE test_01155_ordinary;
INSERT INTO src VALUES ('after renaming database');
SELECT 3, substr(_table, 1, 10), s FROM merge('test_01155_ordinary', '') ORDER BY _table, s;
SELECT substr(name, 1, 10) FROM system.tables WHERE database='test_01155_ordinary';
-- Move tables back
RENAME DATABASE test_01155_ordinary TO test_01155_atomic;
CREATE DATABASE test_01155_ordinary ENGINE=Ordinary;
SHOW CREATE DATABASE test_01155_atomic;
RENAME TABLE test_01155_atomic.mv1 TO test_01155_ordinary.mv1;
RENAME TABLE test_01155_atomic.mv2 TO test_01155_ordinary.mv2;
RENAME TABLE test_01155_atomic.dst TO test_01155_ordinary.dst;
RENAME TABLE test_01155_atomic.src TO test_01155_ordinary.src;
INSERT INTO src VALUES ('after renaming tables');
SELECT 4, substr(_table, 1, 10), s FROM merge('test_01155_ordinary', '') ORDER BY _table, s;
SELECT 'test_01155_ordinary:';
SHOW TABLES FROM test_01155_ordinary;
SELECT 'test_01155_atomic:';
SHOW TABLES FROM test_01155_atomic;
DROP DATABASE IF EXISTS test_01155_atomic;
DROP DATABASE IF EXISTS test_01155_ordinary;