diff --git a/src/Storages/StorageMaterializedView.cpp b/src/Storages/StorageMaterializedView.cpp index 27cd649aae4..67bd6b21c3f 100644 --- a/src/Storages/StorageMaterializedView.cpp +++ b/src/Storages/StorageMaterializedView.cpp @@ -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::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()); diff --git a/tests/queries/0_stateless/01155_rename_move_materialized_view.reference b/tests/queries/0_stateless/01155_rename_move_materialized_view.reference new file mode 100644 index 00000000000..942cedf8696 --- /dev/null +++ b/tests/queries/0_stateless/01155_rename_move_materialized_view.reference @@ -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: diff --git a/tests/queries/0_stateless/01155_rename_move_materialized_view.sql b/tests/queries/0_stateless/01155_rename_move_materialized_view.sql new file mode 100644 index 00000000000..2ede0fbcedf --- /dev/null +++ b/tests/queries/0_stateless/01155_rename_move_materialized_view.sql @@ -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;