From f413061e0d3d2ee5fafe4c7ab323e487d5eb16a5 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Thu, 23 Apr 2020 19:51:48 +0300 Subject: [PATCH] remove Context from some IDatabase methods --- programs/copier/ClusterCopierApp.cpp | 2 +- programs/local/LocalServer.cpp | 10 +++--- src/Databases/DatabaseAtomic.cpp | 2 +- src/Databases/DatabaseDictionary.cpp | 21 +++++------ src/Databases/DatabaseDictionary.h | 14 +++----- src/Databases/DatabaseFactory.cpp | 4 +-- src/Databases/DatabaseLazy.cpp | 14 +++----- src/Databases/DatabaseLazy.h | 10 ++---- src/Databases/DatabaseMemory.cpp | 9 +++-- src/Databases/DatabaseMemory.h | 7 ++-- src/Databases/DatabaseMySQL.cpp | 16 ++++----- src/Databases/DatabaseMySQL.h | 10 +++--- src/Databases/DatabaseOnDisk.cpp | 22 ++++++------ src/Databases/DatabaseOnDisk.h | 5 ++- src/Databases/DatabaseWithDictionaries.cpp | 11 +++--- src/Databases/DatabaseWithDictionaries.h | 7 ++-- src/Databases/DatabasesCommon.cpp | 10 ++---- src/Databases/DatabasesCommon.h | 10 ++---- src/Databases/IDatabase.h | 36 ++++++++----------- src/Interpreters/DatabaseCatalog.cpp | 21 ++++++----- src/Interpreters/DatabaseCatalog.h | 2 +- ...ExternalLoaderDatabaseConfigRepository.cpp | 2 +- src/Interpreters/InterpreterCreateQuery.cpp | 10 +++--- src/Interpreters/InterpreterDropQuery.cpp | 2 +- .../InterpreterShowCreateQuery.cpp | 6 ++-- src/Interpreters/InterpreterSystemQuery.cpp | 2 +- .../tests/expression_analyzer.cpp | 2 +- src/Storages/System/StorageSystemTables.cpp | 2 +- ..._transform_query_for_external_database.cpp | 2 +- 29 files changed, 117 insertions(+), 154 deletions(-) diff --git a/programs/copier/ClusterCopierApp.cpp b/programs/copier/ClusterCopierApp.cpp index 689e5a935ad..52a37c75c72 100644 --- a/programs/copier/ClusterCopierApp.cpp +++ b/programs/copier/ClusterCopierApp.cpp @@ -118,7 +118,7 @@ void ClusterCopierApp::mainImpl() registerDisks(); static const std::string default_database = "_local"; - DatabaseCatalog::instance().attachDatabase(default_database, std::make_shared(default_database, *context)); + DatabaseCatalog::instance().attachDatabase(default_database, std::make_shared(default_database)); context->setCurrentDatabase(default_database); /// Initialize query scope just in case. diff --git a/programs/local/LocalServer.cpp b/programs/local/LocalServer.cpp index 2628781206f..d2d19a395bb 100644 --- a/programs/local/LocalServer.cpp +++ b/programs/local/LocalServer.cpp @@ -119,13 +119,13 @@ void LocalServer::tryInitPath() } -static void attachSystemTables(const Context & context) +static void attachSystemTables() { DatabasePtr system_database = DatabaseCatalog::instance().tryGetDatabase(DatabaseCatalog::SYSTEM_DATABASE); if (!system_database) { /// TODO: add attachTableDelayed into DatabaseMemory to speedup loading - system_database = std::make_shared(DatabaseCatalog::SYSTEM_DATABASE, context); + system_database = std::make_shared(DatabaseCatalog::SYSTEM_DATABASE); DatabaseCatalog::instance().attachDatabase(DatabaseCatalog::SYSTEM_DATABASE, system_database); } @@ -203,7 +203,7 @@ try * if such tables will not be dropped, clickhouse-server will not be able to load them due to security reasons. */ std::string default_database = config().getString("default_database", "_local"); - DatabaseCatalog::instance().attachDatabase(default_database, std::make_shared(default_database, *context)); + DatabaseCatalog::instance().attachDatabase(default_database, std::make_shared(default_database)); context->setCurrentDatabase(default_database); applyCmdOptions(); @@ -214,14 +214,14 @@ try LOG_DEBUG(log, "Loading metadata from " << context->getPath()); loadMetadataSystem(*context); - attachSystemTables(*context); + attachSystemTables(); loadMetadata(*context); DatabaseCatalog::instance().loadDatabases(); LOG_DEBUG(log, "Loaded metadata."); } else { - attachSystemTables(*context); + attachSystemTables(); } processQueries(); diff --git a/src/Databases/DatabaseAtomic.cpp b/src/Databases/DatabaseAtomic.cpp index b31e8d87d04..f247405eed5 100644 --- a/src/Databases/DatabaseAtomic.cpp +++ b/src/Databases/DatabaseAtomic.cpp @@ -293,7 +293,7 @@ DatabaseTablesIteratorPtr DatabaseAtomic::getTablesIterator(const IDatabase::Fil UUID DatabaseAtomic::tryGetTableUUID(const String & table_name) const { - if (auto table = tryGetTable(global_context, table_name)) + if (auto table = tryGetTable(table_name)) return table->getStorageID().uuid; return UUIDHelpers::Nil; } diff --git a/src/Databases/DatabaseDictionary.cpp b/src/Databases/DatabaseDictionary.cpp index 55f00baa4c6..81bcbf7bc6b 100644 --- a/src/Databases/DatabaseDictionary.cpp +++ b/src/Databases/DatabaseDictionary.cpp @@ -50,16 +50,12 @@ Tables DatabaseDictionary::listTables(const FilterByNameFunction & filter_by_nam return tables; } -bool DatabaseDictionary::isTableExist( - const Context & context, - const String & table_name) const +bool DatabaseDictionary::isTableExist(const String & table_name) const { - return context.getExternalDictionariesLoader().getCurrentStatus(table_name) != ExternalLoader::Status::NOT_EXIST; + return global_context.getExternalDictionariesLoader().getCurrentStatus(table_name) != ExternalLoader::Status::NOT_EXIST; } -StoragePtr DatabaseDictionary::tryGetTable( - const Context & /*context*/, - const String & table_name) const +StoragePtr DatabaseDictionary::tryGetTable(const String & table_name) const { auto load_result = global_context.getExternalDictionariesLoader().getLoadResult(table_name); return createStorageDictionary(getDatabaseName(), load_result); @@ -70,13 +66,12 @@ DatabaseTablesIteratorPtr DatabaseDictionary::getTablesIterator(const FilterByNa return std::make_unique(listTables(filter_by_table_name)); } -bool DatabaseDictionary::empty(const Context & /*context*/) const +bool DatabaseDictionary::empty() const { return !global_context.getExternalDictionariesLoader().hasObjects(); } -ASTPtr DatabaseDictionary::getCreateTableQueryImpl(const Context & context, - const String & table_name, bool throw_on_error) const +ASTPtr DatabaseDictionary::getCreateTableQueryImpl(const String & table_name, bool throw_on_error) const { String query; { @@ -96,7 +91,7 @@ ASTPtr DatabaseDictionary::getCreateTableQueryImpl(const Context & context, buffer << ") Engine = Dictionary(" << backQuoteIfNeed(table_name) << ")"; } - auto settings = context.getSettingsRef(); + auto settings = global_context.getSettingsRef(); ParserCreateQuery parser; const char * pos = query.data(); std::string error_message; @@ -109,14 +104,14 @@ ASTPtr DatabaseDictionary::getCreateTableQueryImpl(const Context & context, return ast; } -ASTPtr DatabaseDictionary::getCreateDatabaseQuery(const Context & context) const +ASTPtr DatabaseDictionary::getCreateDatabaseQuery() const { String query; { WriteBufferFromString buffer(query); buffer << "CREATE DATABASE " << backQuoteIfNeed(database_name) << " ENGINE = Dictionary"; } - auto settings = context.getSettingsRef(); + auto settings = global_context.getSettingsRef(); ParserCreateQuery parser; return parseQuery(parser, query.data(), query.data() + query.size(), "", 0, settings.max_parser_depth); } diff --git a/src/Databases/DatabaseDictionary.h b/src/Databases/DatabaseDictionary.h index 4d6bdf8703f..bf10dc134bd 100644 --- a/src/Databases/DatabaseDictionary.h +++ b/src/Databases/DatabaseDictionary.h @@ -29,26 +29,22 @@ public: return "Dictionary"; } - bool isTableExist( - const Context & context, - const String & table_name) const override; + bool isTableExist(const String & table_name) const override; - StoragePtr tryGetTable( - const Context & context, - const String & table_name) const override; + StoragePtr tryGetTable(const String & table_name) const override; DatabaseTablesIteratorPtr getTablesIterator(const FilterByNameFunction & filter_by_table_name) override; - bool empty(const Context & context) const override; + bool empty() const override; - ASTPtr getCreateDatabaseQuery(const Context & context) const override; + ASTPtr getCreateDatabaseQuery() const override; bool shouldBeEmptyOnDetach() const override { return false; } void shutdown() override; protected: - ASTPtr getCreateTableQueryImpl(const Context & context, const String & table_name, bool throw_on_error) const override; + ASTPtr getCreateTableQueryImpl(const String & table_name, bool throw_on_error) const override; private: mutable std::mutex mutex; diff --git a/src/Databases/DatabaseFactory.cpp b/src/Databases/DatabaseFactory.cpp index 3541c397c83..21feb122da1 100644 --- a/src/Databases/DatabaseFactory.cpp +++ b/src/Databases/DatabaseFactory.cpp @@ -82,7 +82,7 @@ DatabasePtr DatabaseFactory::getImpl( else if (engine_name == "Atomic") return std::make_shared(database_name, metadata_path, context); else if (engine_name == "Memory") - return std::make_shared(database_name, context); + return std::make_shared(database_name); else if (engine_name == "Dictionary") return std::make_shared(database_name, context); @@ -113,7 +113,7 @@ DatabasePtr DatabaseFactory::getImpl( auto mysql_database = std::make_shared( context, database_name, metadata_path, engine_define, database_name_in_mysql, std::move(mysql_pool)); - mysql_database->empty(context); /// test database is works fine. + mysql_database->empty(); /// test database is works fine. return mysql_database; } catch (...) diff --git a/src/Databases/DatabaseLazy.cpp b/src/Databases/DatabaseLazy.cpp index 3e737f04563..d287ece0fde 100644 --- a/src/Databases/DatabaseLazy.cpp +++ b/src/Databases/DatabaseLazy.cpp @@ -103,18 +103,14 @@ void DatabaseLazy::alterTable( throw Exception("ALTER query is not supported for Lazy database.", ErrorCodes::UNSUPPORTED_METHOD); } -bool DatabaseLazy::isTableExist( - const Context & /* context */, - const String & table_name) const +bool DatabaseLazy::isTableExist(const String & table_name) const { SCOPE_EXIT({ clearExpiredTables(); }); std::lock_guard lock(mutex); return tables_cache.find(table_name) != tables_cache.end(); } -StoragePtr DatabaseLazy::tryGetTable( - const Context & /*context*/, - const String & table_name) const +StoragePtr DatabaseLazy::tryGetTable(const String & table_name) const { SCOPE_EXIT({ clearExpiredTables(); }); { @@ -149,7 +145,7 @@ DatabaseTablesIteratorPtr DatabaseLazy::getTablesIterator(const FilterByNameFunc return std::make_unique(*this, std::move(filtered_tables)); } -bool DatabaseLazy::empty(const Context & /* context */) const +bool DatabaseLazy::empty() const { return tables_cache.empty(); } @@ -309,7 +305,7 @@ void DatabaseLazyIterator::next() { current_storage.reset(); ++iterator; - while (isValid() && !database.isTableExist(database.global_context, *iterator)) + while (isValid() && !database.isTableExist(*iterator)) ++iterator; } @@ -326,7 +322,7 @@ const String & DatabaseLazyIterator::name() const const StoragePtr & DatabaseLazyIterator::table() const { if (!current_storage) - current_storage = database.tryGetTable(database.global_context, *iterator); + current_storage = database.tryGetTable(*iterator); return current_storage; } diff --git a/src/Databases/DatabaseLazy.h b/src/Databases/DatabaseLazy.h index 153157ce17a..77ad1a3ab20 100644 --- a/src/Databases/DatabaseLazy.h +++ b/src/Databases/DatabaseLazy.h @@ -51,15 +51,11 @@ public: time_t getObjectMetadataModificationTime(const String & table_name) const override; - bool isTableExist( - const Context & context, - const String & table_name) const override; + bool isTableExist(const String & table_name) const override; - StoragePtr tryGetTable( - const Context & context, - const String & table_name) const override; + StoragePtr tryGetTable(const String & table_name) const override; - bool empty(const Context & context) const override; + bool empty() const override; DatabaseTablesIteratorPtr getTablesIterator(const FilterByNameFunction & filter_by_table_name) override; diff --git a/src/Databases/DatabaseMemory.cpp b/src/Databases/DatabaseMemory.cpp index e37a9ca9b56..417761f81e7 100644 --- a/src/Databases/DatabaseMemory.cpp +++ b/src/Databases/DatabaseMemory.cpp @@ -15,9 +15,8 @@ namespace ErrorCodes extern const int UNKNOWN_TABLE; } -DatabaseMemory::DatabaseMemory(const String & name_, const Context & global_context_) +DatabaseMemory::DatabaseMemory(const String & name_) : DatabaseWithOwnTablesBase(name_, "DatabaseMemory(" + name_ + ")") - , global_context(global_context_.getGlobalContext()) , data_path("data/" + escapeForFileName(database_name) + "/") {} @@ -55,7 +54,7 @@ void DatabaseMemory::dropTable( create_queries.erase(table_name); } -ASTPtr DatabaseMemory::getCreateDatabaseQuery(const Context & /*context*/) const +ASTPtr DatabaseMemory::getCreateDatabaseQuery() const { auto create_query = std::make_shared(); create_query->database = database_name; @@ -64,7 +63,7 @@ ASTPtr DatabaseMemory::getCreateDatabaseQuery(const Context & /*context*/) const return create_query; } -ASTPtr DatabaseMemory::getCreateTableQueryImpl(const Context &, const String & table_name, bool throw_on_error) const +ASTPtr DatabaseMemory::getCreateTableQueryImpl(const String & table_name, bool throw_on_error) const { std::lock_guard lock{mutex}; auto it = create_queries.find(table_name); @@ -80,7 +79,7 @@ ASTPtr DatabaseMemory::getCreateTableQueryImpl(const Context &, const String & t UUID DatabaseMemory::tryGetTableUUID(const String & table_name) const { - if (auto table = tryGetTable(global_context, table_name)) + if (auto table = tryGetTable(table_name)) return table->getStorageID().uuid; return UUIDHelpers::Nil; } diff --git a/src/Databases/DatabaseMemory.h b/src/Databases/DatabaseMemory.h index 2740867878c..29a9abc9d75 100644 --- a/src/Databases/DatabaseMemory.h +++ b/src/Databases/DatabaseMemory.h @@ -19,7 +19,7 @@ namespace DB class DatabaseMemory final : public DatabaseWithOwnTablesBase { public: - DatabaseMemory(const String & name_, const Context & global_context_); + DatabaseMemory(const String & name_); String getEngineName() const override { return "Memory"; } @@ -34,8 +34,8 @@ public: const String & table_name, bool no_delay) override; - ASTPtr getCreateTableQueryImpl(const Context & /*context*/, const String & name, bool throw_on_error) const override; - ASTPtr getCreateDatabaseQuery(const Context & /*context*/) const override; + ASTPtr getCreateTableQueryImpl(const String & name, bool throw_on_error) const override; + ASTPtr getCreateDatabaseQuery() const override; /// DatabaseMemory allows to create tables, which store data on disk. /// It's needed to create such tables in default database of clickhouse-local. @@ -47,7 +47,6 @@ public: UUID tryGetTableUUID(const String & table_name) const override; private: - const Context & global_context; String data_path; using NameToASTCreate = std::unordered_map; NameToASTCreate create_queries; diff --git a/src/Databases/DatabaseMySQL.cpp b/src/Databases/DatabaseMySQL.cpp index 8402aa6ca41..cbd3261975a 100644 --- a/src/Databases/DatabaseMySQL.cpp +++ b/src/Databases/DatabaseMySQL.cpp @@ -73,7 +73,7 @@ DatabaseMySQL::DatabaseMySQL( { } -bool DatabaseMySQL::empty(const Context &) const +bool DatabaseMySQL::empty() const { std::lock_guard lock(mutex); @@ -103,12 +103,12 @@ DatabaseTablesIteratorPtr DatabaseMySQL::getTablesIterator(const FilterByNameFun return std::make_unique(tables); } -bool DatabaseMySQL::isTableExist(const Context & context, const String & name) const +bool DatabaseMySQL::isTableExist(const String & name) const { - return bool(tryGetTable(context, name)); + return bool(tryGetTable(name)); } -StoragePtr DatabaseMySQL::tryGetTable(const Context &, const String & mysql_table_name) const +StoragePtr DatabaseMySQL::tryGetTable(const String & mysql_table_name) const { std::lock_guard lock(mutex); @@ -155,7 +155,7 @@ static ASTPtr getCreateQueryFromStorage(const StoragePtr & storage, const ASTPtr return create_table_query; } -ASTPtr DatabaseMySQL::getCreateTableQueryImpl(const Context &, const String & table_name, bool throw_on_error) const +ASTPtr DatabaseMySQL::getCreateTableQueryImpl(const String & table_name, bool throw_on_error) const { std::lock_guard lock(mutex); @@ -184,7 +184,7 @@ time_t DatabaseMySQL::getObjectMetadataModificationTime(const String & table_nam return time_t(local_tables_cache[table_name].first); } -ASTPtr DatabaseMySQL::getCreateDatabaseQuery(const Context & /*context*/) const +ASTPtr DatabaseMySQL::getCreateDatabaseQuery() const { const auto & create_query = std::make_shared(); create_query->database = database_name; @@ -490,7 +490,7 @@ DatabaseMySQL::~DatabaseMySQL() } } -void DatabaseMySQL::createTable(const Context & context, const String & table_name, const StoragePtr & storage, const ASTPtr & create_query) +void DatabaseMySQL::createTable(const Context &, const String & table_name, const StoragePtr & storage, const ASTPtr & create_query) { const auto & create = create_query->as(); @@ -501,7 +501,7 @@ void DatabaseMySQL::createTable(const Context & context, const String & table_na /// XXX: hack /// In order to prevent users from broken the table structure by executing attach table database_name.table_name (...) /// we should compare the old and new create_query to make them completely consistent - const auto & origin_create_query = getCreateTableQuery(context, table_name); + const auto & origin_create_query = getCreateTableQuery(table_name); origin_create_query->as()->attach = true; if (queryToString(origin_create_query) != queryToString(create_query)) diff --git a/src/Databases/DatabaseMySQL.h b/src/Databases/DatabaseMySQL.h index 6d7f1a29e55..d729c62a8bb 100644 --- a/src/Databases/DatabaseMySQL.h +++ b/src/Databases/DatabaseMySQL.h @@ -28,15 +28,15 @@ public: String getEngineName() const override { return "MySQL"; } - bool empty(const Context & context) const override; + bool empty() const override; DatabaseTablesIteratorPtr getTablesIterator(const FilterByNameFunction & filter_by_table_name) override; - ASTPtr getCreateDatabaseQuery(const Context & /*context*/) const override; + ASTPtr getCreateDatabaseQuery() const override; - bool isTableExist(const Context & context, const String & name) const override; + bool isTableExist(const String & name) const override; - StoragePtr tryGetTable(const Context & context, const String & name) const override; + StoragePtr tryGetTable(const String & name) const override; time_t getObjectMetadataModificationTime(const String & name) const override; @@ -57,7 +57,7 @@ public: void attachTable(const String & table_name, const StoragePtr & storage, const String & relative_table_path) override; protected: - ASTPtr getCreateTableQueryImpl(const Context & context, const String & name, bool throw_on_error) const override; + ASTPtr getCreateTableQueryImpl(const String & name, bool throw_on_error) const override; private: const Context & global_context; diff --git a/src/Databases/DatabaseOnDisk.cpp b/src/Databases/DatabaseOnDisk.cpp index c523be81011..da38dc3b8f7 100644 --- a/src/Databases/DatabaseOnDisk.cpp +++ b/src/Databases/DatabaseOnDisk.cpp @@ -156,11 +156,11 @@ void DatabaseOnDisk::createTable( /// But there is protection from it - see using DDLGuard in InterpreterCreateQuery. - if (isDictionaryExist(context, table_name)) + if (isDictionaryExist(table_name)) throw Exception("Dictionary " + backQuote(getDatabaseName()) + "." + backQuote(table_name) + " already exists.", ErrorCodes::DICTIONARY_ALREADY_EXISTS); - if (isTableExist(context, table_name)) + if (isTableExist(table_name)) throw Exception("Table " + backQuote(getDatabaseName()) + "." + backQuote(table_name) + " already exists.", ErrorCodes::TABLE_ALREADY_EXISTS); if (create.attach_short_syntax) @@ -267,7 +267,7 @@ void DatabaseOnDisk::renameTable( String table_metadata_path; ASTPtr attach_query; /// DatabaseLazy::detachTable may return nullptr even if table exists, so we need tryGetTable for this case. - StoragePtr table = tryGetTable(context, table_name); + StoragePtr table = tryGetTable(table_name); detachTable(table_name); try { @@ -304,14 +304,14 @@ void DatabaseOnDisk::renameTable( Poco::File(table_metadata_path).remove(); } -ASTPtr DatabaseOnDisk::getCreateTableQueryImpl(const Context & context, const String & table_name, bool throw_on_error) const +ASTPtr DatabaseOnDisk::getCreateTableQueryImpl(const String & table_name, bool throw_on_error) const { ASTPtr ast; - bool has_table = tryGetTable(context, table_name) != nullptr; + bool has_table = tryGetTable(table_name) != nullptr; auto table_metadata_path = getObjectMetadataPath(table_name); try { - ast = getCreateQueryFromMetadata(context, table_metadata_path, throw_on_error); + ast = getCreateQueryFromMetadata(table_metadata_path, throw_on_error); } catch (const Exception & e) { @@ -324,14 +324,14 @@ ASTPtr DatabaseOnDisk::getCreateTableQueryImpl(const Context & context, const St return ast; } -ASTPtr DatabaseOnDisk::getCreateDatabaseQuery(const Context & context) const +ASTPtr DatabaseOnDisk::getCreateDatabaseQuery() const { ASTPtr ast; - auto settings = context.getSettingsRef(); + auto settings = global_context.getSettingsRef(); auto metadata_dir_path = getMetadataPath(); auto database_metadata_path = metadata_dir_path.substr(0, metadata_dir_path.size() - 1) + ".sql"; - ast = getCreateQueryFromMetadata(context, database_metadata_path, true); + ast = getCreateQueryFromMetadata(database_metadata_path, true); if (!ast) { /// Handle databases (such as default) for which there are no database.sql files. @@ -474,9 +474,9 @@ ASTPtr DatabaseOnDisk::parseQueryFromMetadata(Poco::Logger * loger, const Contex return ast; } -ASTPtr DatabaseOnDisk::getCreateQueryFromMetadata(const Context & context, const String & database_metadata_path, bool throw_on_error) const +ASTPtr DatabaseOnDisk::getCreateQueryFromMetadata(const String & database_metadata_path, bool throw_on_error) const { - ASTPtr ast = parseQueryFromMetadata(log, context, database_metadata_path, throw_on_error); + ASTPtr ast = parseQueryFromMetadata(log, global_context, database_metadata_path, throw_on_error); if (ast) { diff --git a/src/Databases/DatabaseOnDisk.h b/src/Databases/DatabaseOnDisk.h index 09f222818b0..a07a143ea92 100644 --- a/src/Databases/DatabaseOnDisk.h +++ b/src/Databases/DatabaseOnDisk.h @@ -50,7 +50,7 @@ public: const String & to_table_name, bool exchange) override; - ASTPtr getCreateDatabaseQuery(const Context & context) const override; + ASTPtr getCreateDatabaseQuery() const override; void drop(const Context & context) override; @@ -74,11 +74,10 @@ protected: void iterateMetadataFiles(const Context & context, const IteratingFunction & process_metadata_file) const; ASTPtr getCreateTableQueryImpl( - const Context & context, const String & table_name, bool throw_on_error) const override; - ASTPtr getCreateQueryFromMetadata(const Context & context, const String & metadata_path, bool throw_on_error) const; + ASTPtr getCreateQueryFromMetadata(const String & metadata_path, bool throw_on_error) const; virtual void commitCreateTable(const ASTCreateQuery & query, const StoragePtr & table, const String & table_metadata_tmp_path, const String & table_metadata_path); diff --git a/src/Databases/DatabaseWithDictionaries.cpp b/src/Databases/DatabaseWithDictionaries.cpp index 25240b8d5f2..2ca4a7b3395 100644 --- a/src/Databases/DatabaseWithDictionaries.cpp +++ b/src/Databases/DatabaseWithDictionaries.cpp @@ -117,7 +117,7 @@ void DatabaseWithDictionaries::createDictionary(const Context & context, const S /// A race condition would be possible if a dictionary with the same name is simultaneously created using CREATE and using ATTACH. /// But there is protection from it - see using DDLGuard in InterpreterCreateQuery. - if (isDictionaryExist(context, dictionary_name)) + if (isDictionaryExist(dictionary_name)) throw Exception("Dictionary " + backQuote(getDatabaseName()) + "." + backQuote(dictionary_name) + " already exists.", ErrorCodes::DICTIONARY_ALREADY_EXISTS); /// A dictionary with the same full name could be defined in *.xml config files. @@ -127,7 +127,7 @@ void DatabaseWithDictionaries::createDictionary(const Context & context, const S "Dictionary " + backQuote(getDatabaseName()) + "." + backQuote(dictionary_name) + " already exists.", ErrorCodes::DICTIONARY_ALREADY_EXISTS); - if (isTableExist(context, dictionary_name)) + if (isTableExist(dictionary_name)) throw Exception("Table " + backQuote(getDatabaseName()) + "." + backQuote(dictionary_name) + " already exists.", ErrorCodes::TABLE_ALREADY_EXISTS); @@ -215,14 +215,13 @@ DatabaseDictionariesIteratorPtr DatabaseWithDictionaries::getDictionariesIterato return std::make_unique(std::move(filtered_dictionaries)); } -bool DatabaseWithDictionaries::isDictionaryExist(const Context & /*context*/, const String & dictionary_name) const +bool DatabaseWithDictionaries::isDictionaryExist(const String & dictionary_name) const { std::lock_guard lock(mutex); return dictionaries.find(dictionary_name) != dictionaries.end(); } ASTPtr DatabaseWithDictionaries::getCreateDictionaryQueryImpl( - const Context & context, const String & dictionary_name, bool throw_on_error) const { @@ -245,7 +244,7 @@ ASTPtr DatabaseWithDictionaries::getCreateDictionaryQueryImpl( try { auto dictionary_metadata_path = getObjectMetadataPath(dictionary_name); - ast = getCreateQueryFromMetadata(context, dictionary_metadata_path, throw_on_error); + ast = getCreateQueryFromMetadata(dictionary_metadata_path, throw_on_error); } catch (const Exception & e) { @@ -286,7 +285,7 @@ time_t DatabaseWithDictionaries::getObjectMetadataModificationTime(const String } -bool DatabaseWithDictionaries::empty(const Context &) const +bool DatabaseWithDictionaries::empty() const { std::lock_guard lock{mutex}; return tables.empty() && dictionaries.empty(); diff --git a/src/Databases/DatabaseWithDictionaries.h b/src/Databases/DatabaseWithDictionaries.h index 27e95c7fb67..48ec92aa3d0 100644 --- a/src/Databases/DatabaseWithDictionaries.h +++ b/src/Databases/DatabaseWithDictionaries.h @@ -18,7 +18,7 @@ public: void removeDictionary(const Context & context, const String & dictionary_name) override; - bool isDictionaryExist(const Context & context, const String & dictionary_name) const override; + bool isDictionaryExist(const String & dictionary_name) const override; DatabaseDictionariesIteratorPtr getDictionariesIterator(const FilterByNameFunction & filter_by_dictionary_name) override; @@ -26,7 +26,7 @@ public: time_t getObjectMetadataModificationTime(const String & object_name) const override; - bool empty(const Context & context) const override; + bool empty() const override; void shutdown() override; @@ -41,8 +41,7 @@ protected: void detachDictionaryImpl(const String & dictionary_name, DictionaryAttachInfo & attach_info); - ASTPtr getCreateDictionaryQueryImpl(const Context & context, - const String & dictionary_name, + ASTPtr getCreateDictionaryQueryImpl(const String & dictionary_name, bool throw_on_error) const override; std::unordered_map dictionaries; diff --git a/src/Databases/DatabasesCommon.cpp b/src/Databases/DatabasesCommon.cpp index 81d02584712..5ad4fa20690 100644 --- a/src/Databases/DatabasesCommon.cpp +++ b/src/Databases/DatabasesCommon.cpp @@ -23,17 +23,13 @@ DatabaseWithOwnTablesBase::DatabaseWithOwnTablesBase(const String & name_, const { } -bool DatabaseWithOwnTablesBase::isTableExist( - const Context & /*context*/, - const String & table_name) const +bool DatabaseWithOwnTablesBase::isTableExist(const String & table_name) const { std::lock_guard lock(mutex); return tables.find(table_name) != tables.end(); } -StoragePtr DatabaseWithOwnTablesBase::tryGetTable( - const Context & /*context*/, - const String & table_name) const +StoragePtr DatabaseWithOwnTablesBase::tryGetTable(const String & table_name) const { std::lock_guard lock(mutex); auto it = tables.find(table_name); @@ -56,7 +52,7 @@ DatabaseTablesIteratorPtr DatabaseWithOwnTablesBase::getTablesIterator(const Fil return std::make_unique(std::move(filtered_tables)); } -bool DatabaseWithOwnTablesBase::empty(const Context & /*context*/) const +bool DatabaseWithOwnTablesBase::empty() const { std::lock_guard lock(mutex); return tables.empty(); diff --git a/src/Databases/DatabasesCommon.h b/src/Databases/DatabasesCommon.h index 366c46d33ef..e195fc59ce9 100644 --- a/src/Databases/DatabasesCommon.h +++ b/src/Databases/DatabasesCommon.h @@ -19,15 +19,11 @@ class Context; class DatabaseWithOwnTablesBase : public IDatabase { public: - bool isTableExist( - const Context & context, - const String & table_name) const override; + bool isTableExist(const String & table_name) const override; - StoragePtr tryGetTable( - const Context & context, - const String & table_name) const override; + StoragePtr tryGetTable(const String & table_name) const override; - bool empty(const Context & context) const override; + bool empty() const override; void attachTable(const String & table_name, const StoragePtr & table, const String & relative_table_path) override; diff --git a/src/Databases/IDatabase.h b/src/Databases/IDatabase.h index f0add23baab..24147a0e7c5 100644 --- a/src/Databases/IDatabase.h +++ b/src/Databases/IDatabase.h @@ -130,22 +130,16 @@ public: virtual void loadStoredObjects(Context & /*context*/, bool /*has_force_restore_data_flag*/) {} /// Check the existence of the table. - virtual bool isTableExist( - const Context & context, - const String & name) const = 0; + virtual bool isTableExist(const String & name) const = 0; /// Check the existence of the dictionary - virtual bool isDictionaryExist( - const Context & /*context*/, - const String & /*name*/) const + virtual bool isDictionaryExist(const String & /*name*/) const { return false; } /// Get the table for work. Return nullptr if there is no table. - virtual StoragePtr tryGetTable( - const Context & context, - const String & name) const = 0; + virtual StoragePtr tryGetTable(const String & name) const = 0; virtual UUID tryGetTableUUID(const String & /*table_name*/) const { return UUIDHelpers::Nil; } @@ -162,7 +156,7 @@ public: } /// Is the database empty. - virtual bool empty(const Context & context) const = 0; + virtual bool empty() const = 0; /// Add the table to the database. Record its presence in the metadata. virtual void createTable( @@ -255,25 +249,25 @@ public: } /// Get the CREATE TABLE query for the table. It can also provide information for detached tables for which there is metadata. - ASTPtr tryGetCreateTableQuery(const Context & context, const String & name) const noexcept + ASTPtr tryGetCreateTableQuery(const String & name) const noexcept { - return getCreateTableQueryImpl(context, name, false); + return getCreateTableQueryImpl(name, false); } - ASTPtr getCreateTableQuery(const Context & context, const String & name) const + ASTPtr getCreateTableQuery(const String & name) const { - return getCreateTableQueryImpl(context, name, true); + return getCreateTableQueryImpl(name, true); } /// Get the CREATE DICTIONARY query for the dictionary. Returns nullptr if dictionary doesn't exists. - ASTPtr tryGetCreateDictionaryQuery(const Context & context, const String & name) const noexcept + ASTPtr tryGetCreateDictionaryQuery(const String & name) const noexcept { - return getCreateDictionaryQueryImpl(context, name, false); + return getCreateDictionaryQueryImpl(name, false); } - ASTPtr getCreateDictionaryQuery(const Context & context, const String & name) const + ASTPtr getCreateDictionaryQuery(const String & name) const { - return getCreateDictionaryQueryImpl(context, name, true); + return getCreateDictionaryQueryImpl(name, true); } virtual Poco::AutoPtr getDictionaryConfiguration(const String & /*name*/) const @@ -282,7 +276,7 @@ public: } /// Get the CREATE DATABASE query for current database. - virtual ASTPtr getCreateDatabaseQuery(const Context & /*context*/) const = 0; + virtual ASTPtr getCreateDatabaseQuery() const = 0; /// Get name of database. String getDatabaseName() const { return database_name; } @@ -310,14 +304,14 @@ public: virtual ~IDatabase() {} protected: - virtual ASTPtr getCreateTableQueryImpl(const Context & /*context*/, const String & /*name*/, bool throw_on_error) const + virtual ASTPtr getCreateTableQueryImpl(const String & /*name*/, bool throw_on_error) const { if (throw_on_error) throw Exception("There is no SHOW CREATE TABLE query for Database" + getEngineName(), ErrorCodes::CANNOT_GET_CREATE_TABLE_QUERY); return nullptr; } - virtual ASTPtr getCreateDictionaryQueryImpl(const Context & /*context*/, const String & /*name*/, bool throw_on_error) const + virtual ASTPtr getCreateDictionaryQueryImpl(const String & /*name*/, bool throw_on_error) const { if (throw_on_error) throw Exception("There is no SHOW CREATE DICTIONARY query for Database" + getEngineName(), ErrorCodes::CANNOT_GET_CREATE_DICTIONARY_QUERY); diff --git a/src/Interpreters/DatabaseCatalog.cpp b/src/Interpreters/DatabaseCatalog.cpp index 8bc5cae023a..8f7d8272e5e 100644 --- a/src/Interpreters/DatabaseCatalog.cpp +++ b/src/Interpreters/DatabaseCatalog.cpp @@ -97,7 +97,7 @@ StorageID TemporaryTableHolder::getGlobalTableID() const StoragePtr TemporaryTableHolder::getTable() const { - auto table = temporary_tables->tryGetTable(*global_context, "_tmp_" + toString(id)); + auto table = temporary_tables->tryGetTable("_tmp_" + toString(id)); if (!table) throw Exception("Temporary table " + getGlobalTableID().getNameForLogs() + " not found", ErrorCodes::LOGICAL_ERROR); return table; @@ -108,7 +108,7 @@ void DatabaseCatalog::loadDatabases() { drop_delay_sec = global_context->getConfigRef().getInt("database_atomic_delay_before_drop_table_sec", 60); - auto db_for_temporary_and_external_tables = std::make_shared(TEMPORARY_DATABASE, *global_context); + auto db_for_temporary_and_external_tables = std::make_shared(TEMPORARY_DATABASE); attachDatabase(TEMPORARY_DATABASE, db_for_temporary_and_external_tables); loadMarkedAsDroppedTables(); @@ -159,7 +159,6 @@ DatabaseAndTable DatabaseCatalog::tryGetByUUID(const UUID & uuid) const DatabaseAndTable DatabaseCatalog::getTableImpl( const StorageID & table_id, - const Context & local_context, std::optional * exception) const { if (!table_id) @@ -207,7 +206,7 @@ DatabaseAndTable DatabaseCatalog::getTableImpl( database = it->second; } - auto table = database->tryGetTable(local_context, table_id.table_name); + auto table = database->tryGetTable(table_id.table_name); if (!table && exception) exception->emplace("Table " + table_id.getNameForLogs() + " doesn't exist.", ErrorCodes::UNKNOWN_TABLE); @@ -262,7 +261,7 @@ DatabasePtr DatabaseCatalog::detachDatabase(const String & database_name, bool d if (check_empty) { - if (!db->empty(*global_context)) + if (!db->empty()) throw Exception("New table appeared in database being dropped or detached. Try again.", ErrorCodes::DATABASE_NOT_EMPTY); auto database_atomic = typeid_cast(db.get()); @@ -332,7 +331,7 @@ bool DatabaseCatalog::isTableExist(const DB::StorageID & table_id) const if (iter != databases.end()) db = iter->second; } - return db && db->isTableExist(*global_context, table_id.table_name); + return db && db->isTableExist(table_id.table_name); } void DatabaseCatalog::assertTableDoesntExist(const StorageID & table_id) const @@ -466,13 +465,13 @@ std::unique_ptr DatabaseCatalog::getDDLGuard(const String & database, bool DatabaseCatalog::isDictionaryExist(const StorageID & table_id) const { auto db = tryGetDatabase(table_id.getDatabaseName()); - return db && db->isDictionaryExist(*global_context, table_id.getTableName()); + return db && db->isDictionaryExist(table_id.getTableName()); } StoragePtr DatabaseCatalog::getTable(const StorageID & table_id) const { std::optional exc; - auto res = getTableImpl(table_id, *global_context, &exc); + auto res = getTableImpl(table_id, &exc); if (!res.second) throw Exception(*exc); return res.second; @@ -480,13 +479,13 @@ StoragePtr DatabaseCatalog::getTable(const StorageID & table_id) const StoragePtr DatabaseCatalog::tryGetTable(const StorageID & table_id) const { - return getTableImpl(table_id, *global_context, nullptr).second; + return getTableImpl(table_id, nullptr).second; } DatabaseAndTable DatabaseCatalog::getDatabaseAndTable(const StorageID & table_id) const { std::optional exc; - auto res = getTableImpl(table_id, *global_context, &exc); + auto res = getTableImpl(table_id, &exc); if (!res.second) throw Exception(*exc); return res; @@ -494,7 +493,7 @@ DatabaseAndTable DatabaseCatalog::getDatabaseAndTable(const StorageID & table_id DatabaseAndTable DatabaseCatalog::tryGetDatabaseAndTable(const StorageID & table_id) const { - return getTableImpl(table_id, *global_context, nullptr); + return getTableImpl(table_id, nullptr); } void DatabaseCatalog::loadMarkedAsDroppedTables() diff --git a/src/Interpreters/DatabaseCatalog.h b/src/Interpreters/DatabaseCatalog.h index 9b419e1f624..9b9b55cfd49 100644 --- a/src/Interpreters/DatabaseCatalog.h +++ b/src/Interpreters/DatabaseCatalog.h @@ -137,7 +137,7 @@ public: StoragePtr tryGetTable(const StorageID & table_id) const; DatabaseAndTable getDatabaseAndTable(const StorageID & table_id) const; DatabaseAndTable tryGetDatabaseAndTable(const StorageID & table_id) const; - DatabaseAndTable getTableImpl(const StorageID & table_id, const Context & local_context, std::optional * exception = nullptr) const; + DatabaseAndTable getTableImpl(const StorageID & table_id, std::optional * exception = nullptr) const; void addDependency(const StorageID & from, const StorageID & where); void removeDependency(const StorageID & from, const StorageID & where); diff --git a/src/Interpreters/ExternalLoaderDatabaseConfigRepository.cpp b/src/Interpreters/ExternalLoaderDatabaseConfigRepository.cpp index 30d22a4ac55..f850762e962 100644 --- a/src/Interpreters/ExternalLoaderDatabaseConfigRepository.cpp +++ b/src/Interpreters/ExternalLoaderDatabaseConfigRepository.cpp @@ -39,7 +39,7 @@ LoadablesConfigurationPtr ExternalLoaderDatabaseConfigRepository::load(const std bool ExternalLoaderDatabaseConfigRepository::exists(const std::string & loadable_definition_name) { - return database.isDictionaryExist(context, trimDatabaseName(loadable_definition_name, database)); + return database.isDictionaryExist(trimDatabaseName(loadable_definition_name, database)); } Poco::Timestamp ExternalLoaderDatabaseConfigRepository::getUpdateTime(const std::string & loadable_definition_name) diff --git a/src/Interpreters/InterpreterCreateQuery.cpp b/src/Interpreters/InterpreterCreateQuery.cpp index ca561ac1485..20c9dc839f4 100644 --- a/src/Interpreters/InterpreterCreateQuery.cpp +++ b/src/Interpreters/InterpreterCreateQuery.cpp @@ -504,7 +504,7 @@ void InterpreterCreateQuery::setEngine(ASTCreateQuery & create) const String as_database_name = context.resolveDatabase(create.as_database); String as_table_name = create.as_table; - ASTPtr as_create_ptr = DatabaseCatalog::instance().getDatabase(as_database_name)->getCreateTableQuery(context, as_table_name); + ASTPtr as_create_ptr = DatabaseCatalog::instance().getDatabase(as_database_name)->getCreateTableQuery(as_table_name); const auto & as_create = as_create_ptr->as(); const String qualified_name = backQuoteIfNeed(as_database_name) + "." + backQuoteIfNeed(as_table_name); @@ -546,7 +546,7 @@ BlockIO InterpreterCreateQuery::createTable(ASTCreateQuery & create) bool if_not_exists = create.if_not_exists; // Table SQL definition is available even if the table is detached - auto query = database->getCreateTableQuery(context, create.table); + auto query = database->getCreateTableQuery(create.table); create = query->as(); // Copy the saved create query, but use ATTACH instead of CREATE create.attach = true; create.attach_short_syntax = true; @@ -608,7 +608,7 @@ bool InterpreterCreateQuery::doCreateTable(ASTCreateQuery & create, guard = DatabaseCatalog::instance().getDDLGuard(create.database, table_name); /// Table can be created before or it can be created concurrently in another thread, while we were waiting in DDLGuard. - if (database->isTableExist(context, table_name)) + if (database->isTableExist(table_name)) { /// TODO Check structure of table if (create.if_not_exists) @@ -708,7 +708,7 @@ BlockIO InterpreterCreateQuery::createDictionary(ASTCreateQuery & create) auto guard = DatabaseCatalog::instance().getDDLGuard(database_name, dictionary_name); DatabasePtr database = DatabaseCatalog::instance().getDatabase(database_name); - if (database->isDictionaryExist(context, dictionary_name)) + if (database->isDictionaryExist(dictionary_name)) { /// TODO Check structure of dictionary if (create.if_not_exists) @@ -720,7 +720,7 @@ BlockIO InterpreterCreateQuery::createDictionary(ASTCreateQuery & create) if (create.attach) { - auto query = DatabaseCatalog::instance().getDatabase(database_name)->getCreateDictionaryQuery(context, dictionary_name); + auto query = DatabaseCatalog::instance().getDatabase(database_name)->getCreateDictionaryQuery(dictionary_name); create = query->as(); create.attach = true; auto config = getDictionaryConfigurationFromAST(create); diff --git a/src/Interpreters/InterpreterDropQuery.cpp b/src/Interpreters/InterpreterDropQuery.cpp index 37010971c42..533baadf348 100644 --- a/src/Interpreters/InterpreterDropQuery.cpp +++ b/src/Interpreters/InterpreterDropQuery.cpp @@ -144,7 +144,7 @@ BlockIO InterpreterDropQuery::executeToDictionary( DatabasePtr database = tryGetDatabase(database_name, if_exists); - if (!database || !database->isDictionaryExist(context, dictionary_name)) + if (!database || !database->isDictionaryExist(dictionary_name)) { if (!if_exists) throw Exception( diff --git a/src/Interpreters/InterpreterShowCreateQuery.cpp b/src/Interpreters/InterpreterShowCreateQuery.cpp index 7f26666a1ae..a7f808bf8cf 100644 --- a/src/Interpreters/InterpreterShowCreateQuery.cpp +++ b/src/Interpreters/InterpreterShowCreateQuery.cpp @@ -49,7 +49,7 @@ BlockInputStreamPtr InterpreterShowCreateQuery::executeImpl() auto resolve_table_type = show_query->temporary ? Context::ResolveExternal : Context::ResolveOrdinary; auto table_id = context.resolveStorageID(*show_query, resolve_table_type); context.checkAccess(AccessType::SHOW_COLUMNS, table_id); - create_query = DatabaseCatalog::instance().getDatabase(table_id.database_name)->getCreateTableQuery(context, table_id.table_name); + create_query = DatabaseCatalog::instance().getDatabase(table_id.database_name)->getCreateTableQuery(table_id.table_name); } else if ((show_query = query_ptr->as())) { @@ -57,7 +57,7 @@ BlockInputStreamPtr InterpreterShowCreateQuery::executeImpl() throw Exception("Temporary databases are not possible.", ErrorCodes::SYNTAX_ERROR); show_query->database = context.resolveDatabase(show_query->database); context.checkAccess(AccessType::SHOW_DATABASES, show_query->database); - create_query = DatabaseCatalog::instance().getDatabase(show_query->database)->getCreateDatabaseQuery(context); + create_query = DatabaseCatalog::instance().getDatabase(show_query->database)->getCreateDatabaseQuery(); } else if ((show_query = query_ptr->as())) { @@ -65,7 +65,7 @@ BlockInputStreamPtr InterpreterShowCreateQuery::executeImpl() throw Exception("Temporary dictionaries are not possible.", ErrorCodes::SYNTAX_ERROR); show_query->database = context.resolveDatabase(show_query->database); context.checkAccess(AccessType::SHOW_DICTIONARIES, show_query->database, show_query->table); - create_query = DatabaseCatalog::instance().getDatabase(show_query->database)->getCreateDictionaryQuery(context, show_query->table); + create_query = DatabaseCatalog::instance().getDatabase(show_query->database)->getCreateDictionaryQuery(show_query->table); } if (!create_query && show_query && show_query->temporary) diff --git a/src/Interpreters/InterpreterSystemQuery.cpp b/src/Interpreters/InterpreterSystemQuery.cpp index 5e46b28c079..7e757e60033 100644 --- a/src/Interpreters/InterpreterSystemQuery.cpp +++ b/src/Interpreters/InterpreterSystemQuery.cpp @@ -330,7 +330,7 @@ StoragePtr InterpreterSystemQuery::tryRestartReplica(const StorageID & replica, { /// If table was already dropped by anyone, an exception will be thrown auto table_lock = table->lockExclusively(context.getCurrentQueryId(), context.getSettingsRef().lock_acquire_timeout); - create_ast = database->getCreateTableQuery(system_context, replica.table_name); + create_ast = database->getCreateTableQuery(replica.table_name); database->detachTable(replica.table_name); } diff --git a/src/Interpreters/tests/expression_analyzer.cpp b/src/Interpreters/tests/expression_analyzer.cpp index e3f7483e515..987b5c763e6 100644 --- a/src/Interpreters/tests/expression_analyzer.cpp +++ b/src/Interpreters/tests/expression_analyzer.cpp @@ -99,7 +99,7 @@ int main() Context context = Context::createGlobal(shared_context.get()); context.makeGlobalContext(); - auto system_database = std::make_shared("system", context); + auto system_database = std::make_shared("system"); DatabaseCatalog::instance().attachDatabase("system", system_database); //context.setCurrentDatabase("system"); system_database->attachTable("one", StorageSystemOne::create("one"), {}); diff --git a/src/Storages/System/StorageSystemTables.cpp b/src/Storages/System/StorageSystemTables.cpp index 3b9382c6f22..9ffd350c4bf 100644 --- a/src/Storages/System/StorageSystemTables.cpp +++ b/src/Storages/System/StorageSystemTables.cpp @@ -331,7 +331,7 @@ protected: if (columns_mask[src_index] || columns_mask[src_index + 1]) { - ASTPtr ast = database->tryGetCreateTableQuery(context, table_name); + ASTPtr ast = database->tryGetCreateTableQuery(table_name); if (columns_mask[src_index++]) res_columns[res_index++]->insert(ast ? queryToString(ast) : ""); diff --git a/src/Storages/tests/gtest_transform_query_for_external_database.cpp b/src/Storages/tests/gtest_transform_query_for_external_database.cpp index 480852f9b6b..df6ee2bd0a5 100644 --- a/src/Storages/tests/gtest_transform_query_for_external_database.cpp +++ b/src/Storages/tests/gtest_transform_query_for_external_database.cpp @@ -30,7 +30,7 @@ struct State explicit State(Context & context_) : context(context_) { registerFunctions(); - DatabasePtr database = std::make_shared("test", context); + DatabasePtr database = std::make_shared("test"); database->attachTable("table", StorageMemory::create(StorageID("test", "table"), ColumnsDescription{columns}, ConstraintsDescription{})); context.makeGlobalContext(); DatabaseCatalog::instance().attachDatabase("test", database);