2017-12-20 07:39:52 +00:00
|
|
|
#include <Databases/IDatabase.h>
|
2017-05-23 18:01:50 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2020-11-03 13:47:26 +00:00
|
|
|
#include <Interpreters/executeDDLQueryOnCluster.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterDropQuery.h>
|
2019-10-15 18:04:17 +00:00
|
|
|
#include <Interpreters/ExternalDictionariesLoader.h>
|
2020-12-14 03:30:39 +00:00
|
|
|
#include <Interpreters/QueryLog.h>
|
2021-10-31 08:51:20 +00:00
|
|
|
#include <Access/Common/AccessRightsElement.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Parsers/ASTDropQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/IStorage.h>
|
2022-09-09 14:03:05 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeData.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
2019-10-08 18:42:22 +00:00
|
|
|
#include <Common/quoteString.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2020-07-04 16:32:23 +00:00
|
|
|
#include <Databases/DatabaseReplicated.h>
|
2020-09-15 14:22:20 +00:00
|
|
|
|
2022-09-28 13:29:29 +00:00
|
|
|
#include "config.h"
|
2020-09-15 14:22:20 +00:00
|
|
|
|
|
|
|
#if USE_MYSQL
|
2021-07-26 18:17:28 +00:00
|
|
|
# include <Databases/MySQL/DatabaseMaterializedMySQL.h>
|
2020-09-15 14:22:20 +00:00
|
|
|
#endif
|
2011-11-05 23:31:19 +00:00
|
|
|
|
2021-01-30 12:05:14 +00:00
|
|
|
#if USE_LIBPQXX
|
2021-06-27 19:09:17 +00:00
|
|
|
# include <Databases/PostgreSQL/DatabaseMaterializedPostgreSQL.h>
|
2021-01-30 12:05:14 +00:00
|
|
|
#endif
|
2011-11-05 23:31:19 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2017-12-22 19:20:18 +00:00
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2018-04-21 00:35:20 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2018-06-09 16:03:07 +00:00
|
|
|
extern const int SYNTAX_ERROR;
|
2018-04-21 00:35:20 +00:00
|
|
|
extern const int UNKNOWN_TABLE;
|
2020-12-07 13:55:24 +00:00
|
|
|
extern const int NOT_IMPLEMENTED;
|
2021-02-01 19:29:47 +00:00
|
|
|
extern const int INCORRECT_QUERY;
|
2021-08-23 11:26:54 +00:00
|
|
|
extern const int TABLE_IS_READ_ONLY;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 20:44:53 +00:00
|
|
|
namespace ActionLocks
|
|
|
|
{
|
|
|
|
extern const StorageActionBlockType PartsMerge;
|
|
|
|
}
|
2011-11-05 23:31:19 +00:00
|
|
|
|
2020-03-18 00:57:00 +00:00
|
|
|
static DatabasePtr tryGetDatabase(const String & database_name, bool if_exists)
|
|
|
|
{
|
|
|
|
return if_exists ? DatabaseCatalog::instance().tryGetDatabase(database_name) : DatabaseCatalog::instance().getDatabase(database_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-31 14:49:02 +00:00
|
|
|
InterpreterDropQuery::InterpreterDropQuery(const ASTPtr & query_ptr_, ContextMutablePtr context_) : WithMutableContext(context_), query_ptr(query_ptr_)
|
2021-04-10 23:33:54 +00:00
|
|
|
{
|
|
|
|
}
|
2011-11-05 23:31:19 +00:00
|
|
|
|
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
BlockIO InterpreterDropQuery::execute()
|
2017-04-13 16:12:56 +00:00
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
auto & drop = query_ptr->as<ASTDropQuery &>();
|
2022-07-06 17:54:05 +00:00
|
|
|
if (!drop.cluster.empty() && !maybeRemoveOnCluster(query_ptr, getContext()))
|
2022-04-22 12:15:29 +00:00
|
|
|
{
|
|
|
|
DDLQueryOnClusterParams params;
|
|
|
|
params.access_to_check = getRequiredAccessForDDLOnCluster();
|
|
|
|
return executeDDLQueryOnCluster(query_ptr, getContext(), params);
|
|
|
|
}
|
2017-04-13 16:12:56 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
if (getContext()->getSettingsRef().database_atomic_wait_for_drop_and_detach_synchronously)
|
2022-06-23 07:59:13 +00:00
|
|
|
drop.sync = true;
|
2020-09-29 22:30:34 +00:00
|
|
|
|
2021-11-11 13:28:18 +00:00
|
|
|
if (drop.table)
|
2021-04-21 13:45:13 +00:00
|
|
|
return executeToTable(drop);
|
2021-11-11 13:28:18 +00:00
|
|
|
else if (drop.database)
|
2020-10-27 19:56:54 +00:00
|
|
|
return executeToDatabase(drop);
|
2018-04-21 00:35:20 +00:00
|
|
|
else
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Nothing to drop, both names are empty");
|
2018-04-21 00:35:20 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
2020-10-27 19:56:54 +00:00
|
|
|
void InterpreterDropQuery::waitForTableToBeActuallyDroppedOrDetached(const ASTDropQuery & query, const DatabasePtr & db, const UUID & uuid_to_wait)
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2020-10-27 19:56:54 +00:00
|
|
|
if (uuid_to_wait == UUIDHelpers::Nil)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (query.kind == ASTDropQuery::Kind::Drop)
|
|
|
|
DatabaseCatalog::instance().waitTableFinallyDropped(uuid_to_wait);
|
|
|
|
else if (query.kind == ASTDropQuery::Kind::Detach)
|
2020-11-24 12:28:31 +00:00
|
|
|
db->waitDetachedTableNotInUse(uuid_to_wait);
|
2020-10-27 19:56:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 22:01:48 +00:00
|
|
|
BlockIO InterpreterDropQuery::executeToTable(ASTDropQuery & query)
|
2020-10-27 19:56:54 +00:00
|
|
|
{
|
|
|
|
DatabasePtr database;
|
|
|
|
UUID table_to_wait_on = UUIDHelpers::Nil;
|
2021-11-10 13:13:27 +00:00
|
|
|
auto res = executeToTableImpl(getContext(), query, database, table_to_wait_on);
|
2022-06-23 07:59:13 +00:00
|
|
|
if (query.sync)
|
2020-10-27 19:56:54 +00:00
|
|
|
waitForTableToBeActuallyDroppedOrDetached(query, database, table_to_wait_on);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-11-10 13:13:27 +00:00
|
|
|
BlockIO InterpreterDropQuery::executeToTableImpl(ContextPtr context_, ASTDropQuery & query, DatabasePtr & db, UUID & uuid_to_wait)
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2020-10-23 12:53:12 +00:00
|
|
|
/// NOTE: it does not contain UUID, we will resolve it with locked DDLGuard
|
2020-10-13 13:31:02 +00:00
|
|
|
auto table_id = StorageID(query);
|
|
|
|
if (query.temporary || table_id.database_name.empty())
|
2016-03-19 01:18:49 +00:00
|
|
|
{
|
2021-11-10 13:13:27 +00:00
|
|
|
if (context_->tryResolveStorageID(table_id, Context::ResolveExternal))
|
2020-10-13 13:31:02 +00:00
|
|
|
return executeToTemporaryTable(table_id.getTableName(), query.kind);
|
|
|
|
else
|
2021-11-10 13:13:27 +00:00
|
|
|
query.setDatabase(table_id.database_name = context_->getCurrentDatabase());
|
2016-03-19 01:18:49 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-03-20 00:07:52 +00:00
|
|
|
if (query.temporary)
|
2020-01-29 19:54:40 +00:00
|
|
|
{
|
2020-03-20 00:07:52 +00:00
|
|
|
if (query.if_exists)
|
2020-01-24 16:20:36 +00:00
|
|
|
return {};
|
2023-01-17 00:19:44 +00:00
|
|
|
throw Exception(ErrorCodes::UNKNOWN_TABLE, "Temporary table {} doesn't exist", backQuoteIfNeed(table_id.table_name));
|
2020-01-29 19:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 14:53:05 +00:00
|
|
|
auto ddl_guard = (!query.no_ddl_lock ? DatabaseCatalog::instance().getDDLGuard(table_id.database_name, table_id.table_name) : nullptr);
|
2018-09-18 18:33:15 +00:00
|
|
|
|
2020-04-13 14:09:56 +00:00
|
|
|
/// If table was already dropped by anyone, an exception will be thrown
|
2021-11-10 13:13:27 +00:00
|
|
|
auto [database, table] = query.if_exists ? DatabaseCatalog::instance().tryGetDatabaseAndTable(table_id, context_)
|
|
|
|
: DatabaseCatalog::instance().getDatabaseAndTable(table_id, context_);
|
2018-04-21 00:35:20 +00:00
|
|
|
|
2020-01-24 16:20:36 +00:00
|
|
|
if (database && table)
|
2017-08-15 12:34:28 +00:00
|
|
|
{
|
2023-02-21 12:36:23 +00:00
|
|
|
checkStorageSupportsTransactionsIfNeeded(table, context_);
|
|
|
|
|
2021-04-22 14:09:30 +00:00
|
|
|
auto & ast_drop_query = query.as<ASTDropQuery &>();
|
|
|
|
|
|
|
|
if (ast_drop_query.is_view && !table->isView())
|
|
|
|
throw Exception(ErrorCodes::INCORRECT_QUERY,
|
|
|
|
"Table {} is not a View",
|
|
|
|
table_id.getNameForLogs());
|
|
|
|
|
|
|
|
if (ast_drop_query.is_dictionary && !table->isDictionary())
|
|
|
|
throw Exception(ErrorCodes::INCORRECT_QUERY,
|
|
|
|
"Table {} is not a Dictionary",
|
|
|
|
table_id.getNameForLogs());
|
2020-03-23 22:28:30 +00:00
|
|
|
|
2020-10-23 12:53:12 +00:00
|
|
|
/// Now get UUID, so we can wait for table data to be finally dropped
|
2020-10-23 14:19:02 +00:00
|
|
|
table_id.uuid = database->tryGetTableUUID(table_id.table_name);
|
2020-10-13 17:45:59 +00:00
|
|
|
|
2021-02-01 19:29:47 +00:00
|
|
|
/// Prevents recursive drop from drop database query. The original query must specify a table.
|
2021-11-11 13:28:18 +00:00
|
|
|
bool is_drop_or_detach_database = !query_ptr->as<ASTDropQuery>()->table;
|
2021-04-21 13:45:13 +00:00
|
|
|
|
|
|
|
AccessFlags drop_storage;
|
|
|
|
|
|
|
|
if (table->isView())
|
|
|
|
drop_storage = AccessType::DROP_VIEW;
|
|
|
|
else if (table->isDictionary())
|
|
|
|
drop_storage = AccessType::DROP_DICTIONARY;
|
|
|
|
else
|
|
|
|
drop_storage = AccessType::DROP_TABLE;
|
|
|
|
|
2022-09-15 19:15:57 +00:00
|
|
|
if (database->shouldReplicateQuery(getContext(), query_ptr))
|
2021-02-01 19:29:47 +00:00
|
|
|
{
|
2021-02-08 19:36:17 +00:00
|
|
|
if (query.kind == ASTDropQuery::Kind::Detach)
|
2021-11-10 13:13:27 +00:00
|
|
|
context_->checkAccess(drop_storage, table_id);
|
2021-02-08 19:36:17 +00:00
|
|
|
else if (query.kind == ASTDropQuery::Kind::Truncate)
|
2021-11-10 13:13:27 +00:00
|
|
|
context_->checkAccess(AccessType::TRUNCATE, table_id);
|
2021-02-08 19:36:17 +00:00
|
|
|
else if (query.kind == ASTDropQuery::Kind::Drop)
|
2021-11-10 13:13:27 +00:00
|
|
|
context_->checkAccess(drop_storage, table_id);
|
2021-02-08 19:36:17 +00:00
|
|
|
|
2021-02-02 19:39:04 +00:00
|
|
|
ddl_guard->releaseTableLock();
|
2021-02-01 19:29:47 +00:00
|
|
|
table.reset();
|
2022-09-16 14:25:32 +00:00
|
|
|
return database->tryEnqueueReplicatedDDL(query.clone(), context_);
|
2021-02-01 19:29:47 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 00:07:52 +00:00
|
|
|
if (query.kind == ASTDropQuery::Kind::Detach)
|
2017-08-15 17:00:18 +00:00
|
|
|
{
|
2021-11-10 13:13:27 +00:00
|
|
|
context_->checkAccess(drop_storage, table_id);
|
2021-04-21 13:45:13 +00:00
|
|
|
|
2021-04-22 09:19:59 +00:00
|
|
|
if (table->isDictionary())
|
|
|
|
{
|
2021-04-26 10:20:57 +00:00
|
|
|
/// If DROP DICTIONARY query is not used, check if Dictionary can be dropped with DROP TABLE query
|
2021-04-22 09:19:59 +00:00
|
|
|
if (!query.is_dictionary)
|
2021-04-27 19:09:03 +00:00
|
|
|
table->checkTableCanBeDetached();
|
2021-04-22 09:19:59 +00:00
|
|
|
}
|
|
|
|
else
|
2021-04-27 19:09:03 +00:00
|
|
|
table->checkTableCanBeDetached();
|
2021-04-21 13:45:13 +00:00
|
|
|
|
2021-05-13 07:03:00 +00:00
|
|
|
table->flushAndShutdown();
|
2020-06-18 16:10:47 +00:00
|
|
|
TableExclusiveLockHolder table_lock;
|
2021-01-18 14:09:39 +00:00
|
|
|
|
2020-09-14 19:25:02 +00:00
|
|
|
if (database->getUUID() == UUIDHelpers::Nil)
|
2021-11-10 13:13:27 +00:00
|
|
|
table_lock = table->lockExclusively(context_->getCurrentQueryId(), context_->getSettingsRef().lock_acquire_timeout);
|
2020-11-30 17:52:32 +00:00
|
|
|
|
|
|
|
if (query.permanently)
|
|
|
|
{
|
2021-11-01 18:53:07 +00:00
|
|
|
/// Server may fail to restart of DETACH PERMANENTLY if table has dependent ones
|
2023-02-01 23:30:49 +00:00
|
|
|
bool check_ref_deps = getContext()->getSettingsRef().check_referential_table_dependencies;
|
|
|
|
bool check_loading_deps = !check_ref_deps && getContext()->getSettingsRef().check_table_dependencies;
|
|
|
|
DatabaseCatalog::instance().removeDependencies(table_id, check_ref_deps, check_loading_deps, is_drop_or_detach_database);
|
2020-11-30 17:52:32 +00:00
|
|
|
/// Drop table from memory, don't touch data, metadata file renamed and will be skipped during server restart
|
2021-11-10 13:13:27 +00:00
|
|
|
database->detachTablePermanently(context_, table_id.table_name);
|
2020-11-30 17:52:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Drop table from memory, don't touch data and metadata
|
2021-11-10 13:13:27 +00:00
|
|
|
database->detachTable(context_, table_id.table_name);
|
2020-11-30 17:52:32 +00:00
|
|
|
}
|
2018-04-21 00:35:20 +00:00
|
|
|
}
|
2020-03-20 00:07:52 +00:00
|
|
|
else if (query.kind == ASTDropQuery::Kind::Truncate)
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2021-04-27 19:13:21 +00:00
|
|
|
if (table->isDictionary())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SYNTAX_ERROR, "Cannot TRUNCATE dictionary");
|
2021-04-27 19:13:21 +00:00
|
|
|
|
2021-11-10 13:13:27 +00:00
|
|
|
context_->checkAccess(AccessType::TRUNCATE, table_id);
|
2021-09-04 09:02:07 +00:00
|
|
|
if (table->isStaticStorage())
|
2021-08-23 11:26:54 +00:00
|
|
|
throw Exception(ErrorCodes::TABLE_IS_READ_ONLY, "Table is read-only");
|
2021-04-21 13:45:13 +00:00
|
|
|
|
2020-01-24 16:20:36 +00:00
|
|
|
table->checkTableCanBeDropped();
|
2018-06-09 15:48:22 +00:00
|
|
|
|
2022-09-09 14:03:05 +00:00
|
|
|
TableExclusiveLockHolder table_excl_lock;
|
2022-09-14 13:04:24 +00:00
|
|
|
/// We don't need any lock for ReplicatedMergeTree and for simple MergeTree
|
|
|
|
/// For the rest of tables types exclusive lock is needed
|
2022-09-22 22:51:13 +00:00
|
|
|
if (!std::dynamic_pointer_cast<MergeTreeData>(table))
|
2022-09-14 13:04:24 +00:00
|
|
|
table_excl_lock = table->lockExclusively(context_->getCurrentQueryId(), context_->getSettingsRef().lock_acquire_timeout);
|
2022-02-03 20:44:53 +00:00
|
|
|
|
2020-06-18 10:29:13 +00:00
|
|
|
auto metadata_snapshot = table->getInMemoryMetadataPtr();
|
2018-04-21 00:35:20 +00:00
|
|
|
/// Drop table data, don't touch metadata
|
2022-09-09 14:03:05 +00:00
|
|
|
table->truncate(query_ptr, metadata_snapshot, context_, table_excl_lock);
|
2018-04-21 00:35:20 +00:00
|
|
|
}
|
2020-03-20 00:07:52 +00:00
|
|
|
else if (query.kind == ASTDropQuery::Kind::Drop)
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2021-11-10 13:13:27 +00:00
|
|
|
context_->checkAccess(drop_storage, table_id);
|
2021-04-21 13:45:13 +00:00
|
|
|
|
2021-04-22 09:19:59 +00:00
|
|
|
if (table->isDictionary())
|
|
|
|
{
|
2021-04-26 10:20:57 +00:00
|
|
|
/// If DROP DICTIONARY query is not used, check if Dictionary can be dropped with DROP TABLE query
|
2021-04-22 09:19:59 +00:00
|
|
|
if (!query.is_dictionary)
|
|
|
|
table->checkTableCanBeDropped();
|
|
|
|
}
|
|
|
|
else
|
2021-04-21 13:45:13 +00:00
|
|
|
table->checkTableCanBeDropped();
|
2018-04-21 00:35:20 +00:00
|
|
|
|
2022-10-05 18:57:55 +00:00
|
|
|
/// Check dependencies before shutting table down
|
2023-02-01 23:30:49 +00:00
|
|
|
bool check_ref_deps = getContext()->getSettingsRef().check_referential_table_dependencies;
|
|
|
|
bool check_loading_deps = !check_ref_deps && getContext()->getSettingsRef().check_table_dependencies;
|
|
|
|
DatabaseCatalog::instance().checkTableCanBeRemovedOrRenamed(table_id, check_ref_deps, check_loading_deps, is_drop_or_detach_database);
|
2022-10-05 18:57:55 +00:00
|
|
|
|
2021-05-13 07:03:00 +00:00
|
|
|
table->flushAndShutdown();
|
2020-03-30 14:53:05 +00:00
|
|
|
|
2020-06-18 16:10:47 +00:00
|
|
|
TableExclusiveLockHolder table_lock;
|
2020-09-14 19:25:02 +00:00
|
|
|
if (database->getUUID() == UUIDHelpers::Nil)
|
2021-11-10 13:13:27 +00:00
|
|
|
table_lock = table->lockExclusively(context_->getCurrentQueryId(), context_->getSettingsRef().lock_acquire_timeout);
|
2018-11-28 15:59:57 +00:00
|
|
|
|
2023-02-01 23:30:49 +00:00
|
|
|
DatabaseCatalog::instance().removeDependencies(table_id, check_ref_deps, check_loading_deps, is_drop_or_detach_database);
|
2022-06-23 07:59:13 +00:00
|
|
|
database->dropTable(context_, table_id.table_name, query.sync);
|
2022-07-29 12:26:41 +00:00
|
|
|
|
|
|
|
/// We have to drop mmapio cache when dropping table from Ordinary database
|
|
|
|
/// to avoid reading old data if new table with the same name is created
|
|
|
|
if (database->getUUID() == UUIDHelpers::Nil)
|
|
|
|
context_->dropMMappedFileCache();
|
2018-04-21 00:35:20 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 19:56:54 +00:00
|
|
|
db = database;
|
|
|
|
uuid_to_wait = table_id.uuid;
|
2020-09-29 13:42:58 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 00:35:20 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-01-24 16:20:36 +00:00
|
|
|
BlockIO InterpreterDropQuery::executeToTemporaryTable(const String & table_name, ASTDropQuery::Kind kind)
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
|
|
|
if (kind == ASTDropQuery::Kind::Detach)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SYNTAX_ERROR, "Unable to detach temporary table.");
|
2018-07-10 14:24:19 +00:00
|
|
|
else
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
auto context_handle = getContext()->hasSessionContext() ? getContext()->getSessionContext() : getContext();
|
|
|
|
auto resolved_id = context_handle->tryResolveStorageID(StorageID("", table_name), Context::ResolveExternal);
|
2020-02-12 18:14:12 +00:00
|
|
|
if (resolved_id)
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
StoragePtr table = DatabaseCatalog::instance().getTable(resolved_id, getContext());
|
2018-07-10 14:24:19 +00:00
|
|
|
if (kind == ASTDropQuery::Kind::Truncate)
|
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
auto table_lock
|
|
|
|
= table->lockExclusively(getContext()->getCurrentQueryId(), getContext()->getSettingsRef().lock_acquire_timeout);
|
2018-07-10 14:24:19 +00:00
|
|
|
/// Drop table data, don't touch metadata
|
2020-06-18 10:29:13 +00:00
|
|
|
auto metadata_snapshot = table->getInMemoryMetadataPtr();
|
2021-04-10 23:33:54 +00:00
|
|
|
table->truncate(query_ptr, metadata_snapshot, getContext(), table_lock);
|
2018-07-10 14:24:19 +00:00
|
|
|
}
|
|
|
|
else if (kind == ASTDropQuery::Kind::Drop)
|
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
context_handle->removeExternalTable(table_name);
|
2018-07-10 14:24:19 +00:00
|
|
|
}
|
2023-01-24 13:15:51 +00:00
|
|
|
else if (kind == ASTDropQuery::Kind::Detach)
|
|
|
|
{
|
|
|
|
table->is_detached = true;
|
|
|
|
}
|
2017-08-15 17:00:18 +00:00
|
|
|
}
|
2017-08-15 12:34:28 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 00:35:20 +00:00
|
|
|
return {};
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-03-18 00:57:00 +00:00
|
|
|
|
2020-10-27 19:56:54 +00:00
|
|
|
BlockIO InterpreterDropQuery::executeToDatabase(const ASTDropQuery & query)
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2020-10-27 19:56:54 +00:00
|
|
|
DatabasePtr database;
|
|
|
|
std::vector<UUID> tables_to_wait;
|
|
|
|
BlockIO res;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
res = executeToDatabaseImpl(query, database, tables_to_wait);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2022-06-23 07:59:13 +00:00
|
|
|
if (query.sync)
|
2020-10-27 19:56:54 +00:00
|
|
|
{
|
2020-10-29 13:37:59 +00:00
|
|
|
for (const auto & table_uuid : tables_to_wait)
|
2020-10-27 19:56:54 +00:00
|
|
|
waitForTableToBeActuallyDroppedOrDetached(query, database, table_uuid);
|
|
|
|
}
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2022-06-23 07:59:13 +00:00
|
|
|
if (query.sync)
|
2020-10-27 19:56:54 +00:00
|
|
|
{
|
2020-10-29 13:37:59 +00:00
|
|
|
for (const auto & table_uuid : tables_to_wait)
|
2020-10-27 19:56:54 +00:00
|
|
|
waitForTableToBeActuallyDroppedOrDetached(query, database, table_uuid);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockIO InterpreterDropQuery::executeToDatabaseImpl(const ASTDropQuery & query, DatabasePtr & database, std::vector<UUID> & uuids_to_wait)
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2021-09-06 22:13:54 +00:00
|
|
|
const auto & database_name = query.getDatabase();
|
2020-02-10 18:31:52 +00:00
|
|
|
auto ddl_guard = DatabaseCatalog::instance().getDDLGuard(database_name, "");
|
2018-09-18 18:33:15 +00:00
|
|
|
|
2020-10-27 19:56:54 +00:00
|
|
|
database = tryGetDatabase(database_name, query.if_exists);
|
|
|
|
if (database)
|
2014-03-20 10:59:45 +00:00
|
|
|
{
|
2020-10-27 19:56:54 +00:00
|
|
|
if (query.kind == ASTDropQuery::Kind::Truncate)
|
2011-11-05 23:31:19 +00:00
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SYNTAX_ERROR, "Unable to truncate database");
|
2011-11-05 23:31:19 +00:00
|
|
|
}
|
2020-10-27 19:56:54 +00:00
|
|
|
else if (query.kind == ASTDropQuery::Kind::Detach || query.kind == ASTDropQuery::Kind::Drop)
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2020-10-27 19:56:54 +00:00
|
|
|
bool drop = query.kind == ASTDropQuery::Kind::Drop;
|
2021-04-10 23:33:54 +00:00
|
|
|
getContext()->checkAccess(AccessType::DROP_DATABASE, database_name);
|
2020-01-24 16:20:36 +00:00
|
|
|
|
2020-12-07 13:55:24 +00:00
|
|
|
if (query.kind == ASTDropQuery::Kind::Detach && query.permanently)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "DETACH PERMANENTLY is not implemented for databases");
|
2020-12-07 13:55:24 +00:00
|
|
|
|
2021-11-11 20:35:26 +00:00
|
|
|
if (database->hasReplicationThread())
|
|
|
|
database->stopReplication();
|
2020-09-14 19:25:02 +00:00
|
|
|
|
2020-04-12 20:50:32 +00:00
|
|
|
if (database->shouldBeEmptyOnDetach())
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2020-10-27 19:56:54 +00:00
|
|
|
ASTDropQuery query_for_table;
|
|
|
|
query_for_table.kind = query.kind;
|
|
|
|
query_for_table.if_exists = true;
|
2021-09-06 22:13:54 +00:00
|
|
|
query_for_table.setDatabase(database_name);
|
2022-06-23 07:59:13 +00:00
|
|
|
query_for_table.sync = query.sync;
|
2020-10-18 20:18:02 +00:00
|
|
|
|
2021-05-13 19:24:20 +00:00
|
|
|
/// Flush should not be done if shouldBeEmptyOnDetach() == false,
|
|
|
|
/// since in this case getTablesIterator() may do some additional work,
|
2021-11-10 13:13:27 +00:00
|
|
|
/// see DatabaseMaterializedMySQL::getTablesIterator()
|
2023-04-01 12:26:00 +00:00
|
|
|
auto table_context = Context::createCopy(getContext());
|
|
|
|
table_context->setInternalQuery(true);
|
|
|
|
/// Do not hold extra shared pointers to tables
|
|
|
|
std::vector<std::pair<String, bool>> tables_to_drop;
|
|
|
|
for (auto iterator = database->getTablesIterator(table_context); iterator->isValid(); iterator->next())
|
2021-05-13 19:24:20 +00:00
|
|
|
{
|
|
|
|
iterator->table()->flush();
|
2023-04-01 12:26:00 +00:00
|
|
|
tables_to_drop.push_back({iterator->name(), iterator->table()->isDictionary()});
|
2021-05-13 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-01 12:26:00 +00:00
|
|
|
for (const auto & table : tables_to_drop)
|
2020-04-12 20:50:32 +00:00
|
|
|
{
|
2023-04-01 12:26:00 +00:00
|
|
|
query_for_table.setTable(table.first);
|
|
|
|
query_for_table.is_dictionary = table.second;
|
2020-10-27 19:56:54 +00:00
|
|
|
DatabasePtr db;
|
|
|
|
UUID table_to_wait = UUIDHelpers::Nil;
|
2021-11-10 13:13:27 +00:00
|
|
|
executeToTableImpl(table_context, query_for_table, db, table_to_wait);
|
2020-10-27 19:56:54 +00:00
|
|
|
uuids_to_wait.push_back(table_to_wait);
|
2020-04-12 20:50:32 +00:00
|
|
|
}
|
2019-10-11 15:22:30 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 07:59:13 +00:00
|
|
|
if (!drop && query.sync)
|
2021-09-10 10:11:40 +00:00
|
|
|
{
|
|
|
|
/// Avoid "some tables are still in use" when sync mode is enabled
|
|
|
|
for (const auto & table_uuid : uuids_to_wait)
|
|
|
|
database->waitDetachedTableNotInUse(table_uuid);
|
|
|
|
}
|
|
|
|
|
2020-08-18 15:15:27 +00:00
|
|
|
/// Protects from concurrent CREATE TABLE queries
|
|
|
|
auto db_guard = DatabaseCatalog::instance().getExclusiveDDLGuardForDatabase(database_name);
|
|
|
|
|
2020-09-14 19:25:02 +00:00
|
|
|
if (!drop)
|
|
|
|
database->assertCanBeDetached(true);
|
2020-04-10 23:02:15 +00:00
|
|
|
|
2020-02-13 21:00:03 +00:00
|
|
|
/// DETACH or DROP database itself
|
2021-10-13 10:34:18 +00:00
|
|
|
DatabaseCatalog::instance().detachDatabase(getContext(), database_name, drop, database->shouldBeEmptyOnDetach());
|
2011-11-05 23:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-04-21 00:35:20 +00:00
|
|
|
return {};
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
2020-01-24 16:20:36 +00:00
|
|
|
AccessRightsElements InterpreterDropQuery::getRequiredAccessForDDLOnCluster() const
|
|
|
|
{
|
|
|
|
AccessRightsElements required_access;
|
|
|
|
const auto & drop = query_ptr->as<const ASTDropQuery &>();
|
2017-12-20 07:39:52 +00:00
|
|
|
|
2021-11-11 13:28:18 +00:00
|
|
|
if (!drop.table)
|
2020-01-24 16:20:36 +00:00
|
|
|
{
|
|
|
|
if (drop.kind == ASTDropQuery::Kind::Detach)
|
2021-09-06 22:13:54 +00:00
|
|
|
required_access.emplace_back(AccessType::DROP_DATABASE, drop.getDatabase());
|
2020-01-24 16:20:36 +00:00
|
|
|
else if (drop.kind == ASTDropQuery::Kind::Drop)
|
2021-09-06 22:13:54 +00:00
|
|
|
required_access.emplace_back(AccessType::DROP_DATABASE, drop.getDatabase());
|
2020-01-24 16:20:36 +00:00
|
|
|
}
|
|
|
|
else if (drop.is_dictionary)
|
|
|
|
{
|
|
|
|
if (drop.kind == ASTDropQuery::Kind::Detach)
|
2021-09-06 22:13:54 +00:00
|
|
|
required_access.emplace_back(AccessType::DROP_DICTIONARY, drop.getDatabase(), drop.getTable());
|
2020-01-24 16:20:36 +00:00
|
|
|
else if (drop.kind == ASTDropQuery::Kind::Drop)
|
2021-09-06 22:13:54 +00:00
|
|
|
required_access.emplace_back(AccessType::DROP_DICTIONARY, drop.getDatabase(), drop.getTable());
|
2020-01-24 16:20:36 +00:00
|
|
|
}
|
|
|
|
else if (!drop.temporary)
|
|
|
|
{
|
|
|
|
/// It can be view or table.
|
|
|
|
if (drop.kind == ASTDropQuery::Kind::Drop)
|
2021-09-06 22:13:54 +00:00
|
|
|
required_access.emplace_back(AccessType::DROP_TABLE | AccessType::DROP_VIEW, drop.getDatabase(), drop.getTable());
|
2020-01-24 16:20:36 +00:00
|
|
|
else if (drop.kind == ASTDropQuery::Kind::Truncate)
|
2021-09-06 22:13:54 +00:00
|
|
|
required_access.emplace_back(AccessType::TRUNCATE, drop.getDatabase(), drop.getTable());
|
2020-01-24 16:20:36 +00:00
|
|
|
else if (drop.kind == ASTDropQuery::Kind::Detach)
|
2021-09-06 22:13:54 +00:00
|
|
|
required_access.emplace_back(AccessType::DROP_TABLE | AccessType::DROP_VIEW, drop.getDatabase(), drop.getTable());
|
2020-01-24 16:20:36 +00:00
|
|
|
}
|
2018-09-11 18:37:19 +00:00
|
|
|
|
2020-01-24 16:20:36 +00:00
|
|
|
return required_access;
|
2017-12-20 07:39:52 +00:00
|
|
|
}
|
2017-12-22 19:20:18 +00:00
|
|
|
|
2023-04-01 12:26:00 +00:00
|
|
|
void InterpreterDropQuery::executeDropQuery(ASTDropQuery::Kind kind, ContextPtr global_context, ContextPtr current_context,
|
|
|
|
const StorageID & target_table_id, bool sync, bool ignore_sync_setting)
|
2021-04-10 17:58:09 +00:00
|
|
|
{
|
|
|
|
if (DatabaseCatalog::instance().tryGetTable(target_table_id, current_context))
|
|
|
|
{
|
|
|
|
/// We create and execute `drop` query for internal table.
|
|
|
|
auto drop_query = std::make_shared<ASTDropQuery>();
|
2021-09-06 22:13:54 +00:00
|
|
|
drop_query->setDatabase(target_table_id.database_name);
|
|
|
|
drop_query->setTable(target_table_id.table_name);
|
2021-04-10 17:58:09 +00:00
|
|
|
drop_query->kind = kind;
|
2022-06-23 07:59:13 +00:00
|
|
|
drop_query->sync = sync;
|
2021-04-10 17:58:09 +00:00
|
|
|
drop_query->if_exists = true;
|
|
|
|
ASTPtr ast_drop_query = drop_query;
|
|
|
|
/// FIXME We have to use global context to execute DROP query for inner table
|
|
|
|
/// to avoid "Not enough privileges" error if current user has only DROP VIEW ON mat_view_name privilege
|
|
|
|
/// and not allowed to drop inner table explicitly. Allowing to drop inner table without explicit grant
|
|
|
|
/// looks like expected behaviour and we have tests for it.
|
2021-04-11 07:44:40 +00:00
|
|
|
auto drop_context = Context::createCopy(global_context);
|
2023-04-01 12:26:00 +00:00
|
|
|
if (ignore_sync_setting)
|
|
|
|
drop_context->setSetting("database_atomic_wait_for_drop_and_detach_synchronously", false);
|
2023-07-07 10:49:50 +00:00
|
|
|
drop_context->setQueryKind(ClientInfo::QueryKind::SECONDARY_QUERY);
|
2021-04-11 07:44:40 +00:00
|
|
|
if (auto txn = current_context->getZooKeeperMetadataTransaction())
|
2021-04-10 17:58:09 +00:00
|
|
|
{
|
|
|
|
/// For Replicated database
|
2023-07-07 10:49:50 +00:00
|
|
|
drop_context->setQueryKindReplicatedDatabaseInternal();
|
2021-06-01 17:43:15 +00:00
|
|
|
drop_context->setQueryContext(std::const_pointer_cast<Context>(current_context));
|
2021-04-11 07:44:40 +00:00
|
|
|
drop_context->initZooKeeperMetadataTransaction(txn, true);
|
2021-04-10 17:58:09 +00:00
|
|
|
}
|
|
|
|
InterpreterDropQuery drop_interpreter(ast_drop_query, drop_context);
|
|
|
|
drop_interpreter.execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 14:03:05 +00:00
|
|
|
bool InterpreterDropQuery::supportsTransactions() const
|
|
|
|
{
|
|
|
|
/// Enable only for truncate table with MergeTreeData engine
|
|
|
|
|
|
|
|
auto & drop = query_ptr->as<ASTDropQuery &>();
|
|
|
|
|
2022-09-14 13:04:24 +00:00
|
|
|
return drop.cluster.empty()
|
2022-09-09 14:03:05 +00:00
|
|
|
&& !drop.temporary
|
|
|
|
&& drop.kind == ASTDropQuery::Kind::Truncate
|
2022-09-14 13:04:24 +00:00
|
|
|
&& drop.table;
|
2022-09-09 14:03:05 +00:00
|
|
|
}
|
|
|
|
|
2011-11-05 23:31:19 +00:00
|
|
|
}
|