From 849a980644a9e6e7e9649e7dbd86a9ad3a934390 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sat, 20 Mar 2021 18:02:09 +0300 Subject: [PATCH] Updated ExternalDictionariesLoader interface with context parameter --- src/Functions/FunctionsExternalDictionaries.h | 4 +-- src/Interpreters/ExpressionAnalyzer.cpp | 2 +- .../ExternalDictionariesLoader.cpp | 28 +++++++++---------- src/Interpreters/ExternalDictionariesLoader.h | 15 +++++----- src/Interpreters/InterpreterSystemQuery.cpp | 2 +- src/Interpreters/TreeOptimizer.cpp | 2 +- src/Storages/StorageDictionary.cpp | 5 ++-- .../TableFunctionDictionary.cpp | 2 +- 8 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/Functions/FunctionsExternalDictionaries.h b/src/Functions/FunctionsExternalDictionaries.h index 6a98e5ef8c2..2c322698327 100644 --- a/src/Functions/FunctionsExternalDictionaries.h +++ b/src/Functions/FunctionsExternalDictionaries.h @@ -79,7 +79,7 @@ public: std::shared_ptr getDictionary(const String & dictionary_name) { - auto dict = context.getExternalDictionariesLoader().getDictionary(dictionary_name); + auto dict = context.getExternalDictionariesLoader().getDictionary(dictionary_name, context); if (!access_checked) { @@ -118,7 +118,7 @@ public: DictionaryStructure getDictionaryStructure(const String & dictionary_name) const { - return context.getExternalDictionariesLoader().getDictionaryStructure(dictionary_name); + return context.getExternalDictionariesLoader().getDictionaryStructure(dictionary_name, context); } private: diff --git a/src/Interpreters/ExpressionAnalyzer.cpp b/src/Interpreters/ExpressionAnalyzer.cpp index de3b7e30171..150f177b2b2 100644 --- a/src/Interpreters/ExpressionAnalyzer.cpp +++ b/src/Interpreters/ExpressionAnalyzer.cpp @@ -762,7 +762,7 @@ static bool allowDictJoin(StoragePtr joined_storage, const Context & context, St return false; dict_name = dict->dictionaryName(); - auto dictionary = context.getExternalDictionariesLoader().getDictionary(dict_name); + auto dictionary = context.getExternalDictionariesLoader().getDictionary(dict_name, context); if (!dictionary) return false; diff --git a/src/Interpreters/ExternalDictionariesLoader.cpp b/src/Interpreters/ExternalDictionariesLoader.cpp index 2671af5b461..1632b7cbf78 100644 --- a/src/Interpreters/ExternalDictionariesLoader.cpp +++ b/src/Interpreters/ExternalDictionariesLoader.cpp @@ -23,9 +23,9 @@ namespace ErrorCodes } /// Must not acquire Context lock in constructor to avoid possibility of deadlocks. -ExternalDictionariesLoader::ExternalDictionariesLoader(Context & context_) +ExternalDictionariesLoader::ExternalDictionariesLoader(Context & global_context_) : ExternalLoader("external dictionary", &Poco::Logger::get("ExternalDictionariesLoader")) - , context(context_) + , global_context(global_context_) { setConfigSettings({"dictionary", "name", "database", "uuid"}); enableAsyncLoading(true); @@ -40,31 +40,31 @@ ExternalLoader::LoadablePtr ExternalDictionariesLoader::create( /// For dictionaries from databases (created with DDL queries) we have to perform /// additional checks, so we identify them here. bool dictionary_from_database = !repository_name.empty(); - return DictionaryFactory::instance().create(name, config, key_in_config, context, dictionary_from_database); + return DictionaryFactory::instance().create(name, config, key_in_config, global_context, dictionary_from_database); } -ExternalDictionariesLoader::DictPtr ExternalDictionariesLoader::getDictionary(const std::string & dictionary_name) const +ExternalDictionariesLoader::DictPtr ExternalDictionariesLoader::getDictionary(const std::string & dictionary_name, const Context & context) const { - std::string resolved_dictionary_name = resolveDictionaryName(dictionary_name); + std::string resolved_dictionary_name = resolveDictionaryName(dictionary_name, context.getCurrentDatabase()); return std::static_pointer_cast(load(resolved_dictionary_name)); } -ExternalDictionariesLoader::DictPtr ExternalDictionariesLoader::tryGetDictionary(const std::string & dictionary_name) const +ExternalDictionariesLoader::DictPtr ExternalDictionariesLoader::tryGetDictionary(const std::string & dictionary_name, const Context & context) const { - std::string resolved_dictionary_name = resolveDictionaryName(dictionary_name); + std::string resolved_dictionary_name = resolveDictionaryName(dictionary_name, context.getCurrentDatabase()); return std::static_pointer_cast(tryLoad(resolved_dictionary_name)); } -void ExternalDictionariesLoader::reloadDictionary(const std::string & dictionary_name) const +void ExternalDictionariesLoader::reloadDictionary(const std::string & dictionary_name, const Context & context) const { - std::string resolved_dictionary_name = resolveDictionaryName(dictionary_name); + std::string resolved_dictionary_name = resolveDictionaryName(dictionary_name, context.getCurrentDatabase()); loadOrReload(resolved_dictionary_name); } -DictionaryStructure ExternalDictionariesLoader::getDictionaryStructure(const std::string & dictionary_name) const +DictionaryStructure ExternalDictionariesLoader::getDictionaryStructure(const std::string & dictionary_name, const Context & query_context) const { - std::string resolved_name = resolveDictionaryName(dictionary_name); + std::string resolved_name = resolveDictionaryName(dictionary_name, query_context.getCurrentDatabase()); auto load_result = getLoadResult(resolved_name); if (!load_result.config) @@ -73,7 +73,7 @@ DictionaryStructure ExternalDictionariesLoader::getDictionaryStructure(const std return ExternalDictionariesLoader::getDictionaryStructure(*load_result.config); } -std::string ExternalDictionariesLoader::resolveDictionaryName(const std::string & dictionary_name) const +std::string ExternalDictionariesLoader::resolveDictionaryName(const std::string & dictionary_name, const std::string & current_database_name) const { std::string resolved_name = resolveDictionaryNameFromDatabaseCatalog(dictionary_name); bool has_dictionary = has(resolved_name); @@ -85,7 +85,7 @@ std::string ExternalDictionariesLoader::resolveDictionaryName(const std::string /// It will help if dictionary is created with DDL and is in current database. if (dictionary_name.find('.') == std::string::npos) { - String dictionary_name_with_database = context.getCurrentDatabase() + '.' + dictionary_name; + String dictionary_name_with_database = current_database_name + '.' + dictionary_name; resolved_name = resolveDictionaryNameFromDatabaseCatalog(dictionary_name_with_database); has_dictionary = has(resolved_name); } @@ -110,7 +110,7 @@ std::string ExternalDictionariesLoader::resolveDictionaryNameFromDatabaseCatalog std::string maybe_database_name = name.substr(0, pos); std::string maybe_table_name = name.substr(pos + 1); - auto [db, table] = DatabaseCatalog::instance().tryGetDatabaseAndTable({maybe_database_name, maybe_table_name}, context); + auto [db, table] = DatabaseCatalog::instance().tryGetDatabaseAndTable({maybe_database_name, maybe_table_name}, global_context); if (!db) return name; assert(table); diff --git a/src/Interpreters/ExternalDictionariesLoader.h b/src/Interpreters/ExternalDictionariesLoader.h index cae19c3d620..0f64715b243 100644 --- a/src/Interpreters/ExternalDictionariesLoader.h +++ b/src/Interpreters/ExternalDictionariesLoader.h @@ -18,17 +18,18 @@ public: using DictPtr = std::shared_ptr; /// Dictionaries will be loaded immediately and then will be updated in separate thread, each 'reload_period' seconds. - explicit ExternalDictionariesLoader(Context & context_); + explicit ExternalDictionariesLoader(Context & global_context_); - DictPtr getDictionary(const std::string & dictionary_name) const; + DictPtr getDictionary(const std::string & dictionary_name, const Context & context) const; - DictPtr tryGetDictionary(const std::string & dictionary_name) const; + DictPtr tryGetDictionary(const std::string & dictionary_name, const Context & context) const; - void reloadDictionary(const std::string & dictionary_name) const; + void reloadDictionary(const std::string & dictionary_name, const Context & context) const; - DictionaryStructure getDictionaryStructure(const std::string & dictionary_name) const; + DictionaryStructure getDictionaryStructure(const std::string & dictionary_name, const Context & context) const; static DictionaryStructure getDictionaryStructure(const Poco::Util::AbstractConfiguration & config, const std::string & key_in_config = "dictionary"); + static DictionaryStructure getDictionaryStructure(const ObjectConfig & config); static void resetAll(); @@ -37,7 +38,7 @@ protected: LoadablePtr create(const std::string & name, const Poco::Util::AbstractConfiguration & config, const std::string & key_in_config, const std::string & repository_name) const override; - std::string resolveDictionaryName(const std::string & dictionary_name) const; + std::string resolveDictionaryName(const std::string & dictionary_name, const std::string & current_database_name) const; /// Try convert qualified dictionary name to persistent UUID std::string resolveDictionaryNameFromDatabaseCatalog(const std::string & name) const; @@ -46,7 +47,7 @@ protected: friend class DatabaseDictionary; private: - Context & context; + Context & global_context; }; } diff --git a/src/Interpreters/InterpreterSystemQuery.cpp b/src/Interpreters/InterpreterSystemQuery.cpp index 56e3ff18a53..32a8f6d1767 100644 --- a/src/Interpreters/InterpreterSystemQuery.cpp +++ b/src/Interpreters/InterpreterSystemQuery.cpp @@ -278,7 +278,7 @@ BlockIO InterpreterSystemQuery::execute() context.checkAccess(AccessType::SYSTEM_RELOAD_DICTIONARY); auto & external_dictionaries_loader = system_context.getExternalDictionariesLoader(); - external_dictionaries_loader.reloadDictionary(query.target_dictionary); + external_dictionaries_loader.reloadDictionary(query.target_dictionary, context); ExternalDictionariesLoader::resetAll(); break; diff --git a/src/Interpreters/TreeOptimizer.cpp b/src/Interpreters/TreeOptimizer.cpp index b1d38450c95..3f4c2e74e23 100644 --- a/src/Interpreters/TreeOptimizer.cpp +++ b/src/Interpreters/TreeOptimizer.cpp @@ -135,7 +135,7 @@ void optimizeGroupBy(ASTSelectQuery * select_query, const NameSet & source_colum const auto & dict_name = dict_name_ast->value.safeGet(); const auto & attr_name = attr_name_ast->value.safeGet(); - const auto & dict_ptr = context.getExternalDictionariesLoader().getDictionary(dict_name); + const auto & dict_ptr = context.getExternalDictionariesLoader().getDictionary(dict_name, context); if (!dict_ptr->isInjective(attr_name)) { ++i; diff --git a/src/Storages/StorageDictionary.cpp b/src/Storages/StorageDictionary.cpp index e102ace4639..36241cd5582 100644 --- a/src/Storages/StorageDictionary.cpp +++ b/src/Storages/StorageDictionary.cpp @@ -133,7 +133,7 @@ Pipe StorageDictionary::read( const size_t max_block_size, const unsigned /*threads*/) { - auto dictionary = context.getExternalDictionariesLoader().getDictionary(dictionary_name); + auto dictionary = context.getExternalDictionariesLoader().getDictionary(dictionary_name, context); auto stream = dictionary->getBlockInputStream(column_names, max_block_size); /// TODO: update dictionary interface for processors. return Pipe(std::make_shared(stream)); @@ -153,7 +153,8 @@ void registerStorageDictionary(StorageFactory & factory) if (!args.attach) { - const auto & dictionary = args.context.getExternalDictionariesLoader().getDictionary(dictionary_name); + const auto & context = args.context; + const auto & dictionary = context.getExternalDictionariesLoader().getDictionary(dictionary_name, context); const DictionaryStructure & dictionary_structure = dictionary->getStructure(); checkNamesAndTypesCompatibleWithDictionary(dictionary_name, args.columns, dictionary_structure); } diff --git a/src/TableFunctions/TableFunctionDictionary.cpp b/src/TableFunctions/TableFunctionDictionary.cpp index f9c08fe044f..722ffccc07d 100644 --- a/src/TableFunctions/TableFunctionDictionary.cpp +++ b/src/TableFunctions/TableFunctionDictionary.cpp @@ -41,7 +41,7 @@ void TableFunctionDictionary::parseArguments(const ASTPtr & ast_function, const ColumnsDescription TableFunctionDictionary::getActualTableStructure(const Context & context) const { const ExternalDictionariesLoader & external_loader = context.getExternalDictionariesLoader(); - auto dictionary_structure = external_loader.getDictionaryStructure(dictionary_name); + auto dictionary_structure = external_loader.getDictionaryStructure(dictionary_name, context); auto result = ColumnsDescription(StorageDictionary::getNamesAndTypes(dictionary_structure)); return result;