diff --git a/src/Databases/DatabaseAtomic.cpp b/src/Databases/DatabaseAtomic.cpp index 6b8c470861d..7a529187c84 100644 --- a/src/Databases/DatabaseAtomic.cpp +++ b/src/Databases/DatabaseAtomic.cpp @@ -210,7 +210,7 @@ void DatabaseAtomic::renameTable(ContextPtr local_context, const String & table_ std::unique_lock other_db_lock; if (inside_database) db_lock = std::unique_lock{mutex}; - else if (this < &other_db) + else if (this < &other_db) { db_lock = std::unique_lock{mutex}; other_db_lock = std::unique_lock{other_db.mutex}; diff --git a/src/Storages/StorageDictionary.cpp b/src/Storages/StorageDictionary.cpp index 79c6247e358..4c31f62b21f 100644 --- a/src/Storages/StorageDictionary.cpp +++ b/src/Storages/StorageDictionary.cpp @@ -215,29 +215,25 @@ LoadablesConfigurationPtr StorageDictionary::getConfiguration() const void StorageDictionary::renameInMemory(const StorageID & new_table_id) { - auto previous_table_id = getStorageID(); - auto previous_dictionary_name = getStorageID().getInternalDictionaryName(); - auto new_dictionary_name = new_table_id.getInternalDictionaryName(); - + auto old_table_id = getStorageID(); IStorage::renameInMemory(new_table_id); - dictionary_name = new_dictionary_name; - if (configuration) { configuration->setString("dictionary.database", new_table_id.database_name); configuration->setString("dictionary.name", new_table_id.table_name); const auto & external_dictionaries_loader = getContext()->getExternalDictionariesLoader(); - external_dictionaries_loader.reloadConfig(previous_dictionary_name); + auto result = external_dictionaries_loader.getLoadResult(old_table_id.getInternalDictionaryName()); - auto result = external_dictionaries_loader.getLoadResult(new_dictionary_name); + if (result.object) + { + const auto dictionary = std::static_pointer_cast(result.object); + dictionary->updateDictionaryName(new_table_id); + } - if (!result.object) - return; - - const auto dictionary = std::static_pointer_cast(result.object); - dictionary->updateDictionaryName(new_table_id); + external_dictionaries_loader.reloadConfig(old_table_id.getInternalDictionaryName()); + dictionary_name = new_table_id.getFullNameNotQuoted(); } } diff --git a/tests/queries/0_stateless/01914_exchange_dictionaries.reference b/tests/queries/0_stateless/01914_exchange_dictionaries.reference new file mode 100644 index 00000000000..9278d0abeed --- /dev/null +++ b/tests/queries/0_stateless/01914_exchange_dictionaries.reference @@ -0,0 +1,4 @@ +1 Table1 +2 Table2 +2 Table2 +1 Table1 diff --git a/tests/queries/0_stateless/01914_exchange_dictionaries.sql b/tests/queries/0_stateless/01914_exchange_dictionaries.sql new file mode 100644 index 00000000000..ba0c70d13be --- /dev/null +++ b/tests/queries/0_stateless/01914_exchange_dictionaries.sql @@ -0,0 +1,34 @@ +DROP TABLE IF EXISTS table_1; +CREATE TABLE table_1 (id UInt64, value String) ENGINE=TinyLog; + +DROP TABLE IF EXISTS table_2; +CREATE TABLE table_2 (id UInt64, value String) ENGINE=TinyLog; + +INSERT INTO table_1 VALUES (1, 'Table1'); +INSERT INTO table_2 VALUES (2, 'Table2'); + +DROP DICTIONARY IF EXISTS dictionary_1; +CREATE DICTIONARY dictionary_1 (id UInt64, value String) +PRIMARY KEY id +LAYOUT(DIRECT()) +SOURCE(CLICKHOUSE(TABLE 'table_1')); + +DROP DICTIONARY IF EXISTS dictionary_2; +CREATE DICTIONARY dictionary_2 (id UInt64, value String) +PRIMARY KEY id +LAYOUT(DIRECT()) +SOURCE(CLICKHOUSE(TABLE 'table_2')); + +SELECT * FROM dictionary_1; +SELECT * FROM dictionary_2; + +EXCHANGE DICTIONARIES dictionary_1 AND dictionary_2; + +SELECT * FROM dictionary_1; +SELECT * FROM dictionary_2; + +DROP DICTIONARY dictionary_1; +DROP DICTIONARY dictionary_2; + +DROP TABLE table_1; +DROP TABLE table_2; diff --git a/tests/queries/0_stateless/01915_create_or_replace_dictionary.reference b/tests/queries/0_stateless/01915_create_or_replace_dictionary.reference new file mode 100644 index 00000000000..2d33c16ccc2 --- /dev/null +++ b/tests/queries/0_stateless/01915_create_or_replace_dictionary.reference @@ -0,0 +1,2 @@ +0 Value0 +0 Value1 diff --git a/tests/queries/0_stateless/01915_create_or_replace_dictionary.sql b/tests/queries/0_stateless/01915_create_or_replace_dictionary.sql new file mode 100644 index 00000000000..5d5515f4f8a --- /dev/null +++ b/tests/queries/0_stateless/01915_create_or_replace_dictionary.sql @@ -0,0 +1,50 @@ +DROP DATABASE IF EXISTS 01915_db; +CREATE DATABASE 01915_db ENGINE=Atomic; + +DROP TABLE IF EXISTS 01915_db.test_source_table_1; +CREATE TABLE 01915_db.test_source_table_1 +( + id UInt64, + value String +) ENGINE=TinyLog; + +INSERT INTO 01915_db.test_source_table_1 VALUES (0, 'Value0'); + +DROP DICTIONARY IF EXISTS 01915_db.test_dictionary; +CREATE OR REPLACE DICTIONARY 01915_db.test_dictionary +( + id UInt64, + value String +) +PRIMARY KEY id +LAYOUT(DIRECT()) +SOURCE(CLICKHOUSE(DB '01915_db' TABLE 'test_source_table_1')); + +SELECT * FROM 01915_db.test_dictionary; + +DROP TABLE IF EXISTS 01915_db.test_source_table_2; +CREATE TABLE 01915_db.test_source_table_2 +( + id UInt64, + value_1 String +) ENGINE=TinyLog; + +INSERT INTO 01915_db.test_source_table_2 VALUES (0, 'Value1'); + +CREATE OR REPLACE DICTIONARY 01915_db.test_dictionary +( + id UInt64, + value_1 String +) +PRIMARY KEY id +LAYOUT(HASHED()) +SOURCE(CLICKHOUSE(DB '01915_db' TABLE 'test_source_table_2')) +LIFETIME(0); + +SELECT * FROM 01915_db.test_dictionary; + +DROP DICTIONARY 01915_db.test_dictionary; +DROP TABLE 01915_db.test_source_table_1; +DROP TABLE 01915_db.test_source_table_2; + +DROP DATABASE 01915_db;