ClickHouse/src/Interpreters/InterpreterDropQuery.cpp

312 lines
13 KiB
C++
Raw Normal View History

2011-11-05 23:31:19 +00:00
#include <Poco/File.h>
#include <Databases/IDatabase.h>
2017-05-23 18:01:50 +00:00
#include <Interpreters/Context.h>
#include <Interpreters/DDLWorker.h>
#include <Interpreters/InterpreterDropQuery.h>
2019-10-15 18:04:17 +00:00
#include <Interpreters/ExternalDictionariesLoader.h>
#include <Access/AccessRightsElement.h>
#include <Parsers/ASTDropQuery.h>
#include <Storages/IStorage.h>
#include <Common/escapeForFileName.h>
#include <Common/quoteString.h>
#include <Common/typeid_cast.h>
2020-04-10 23:02:15 +00:00
#include <Databases/DatabaseAtomic.h>
#include <Databases/DatabaseReplicated.h>
2011-11-05 23:31:19 +00:00
namespace DB
{
2017-12-22 19:20:18 +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;
extern const int UNKNOWN_DICTIONARY;
}
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);
}
InterpreterDropQuery::InterpreterDropQuery(const ASTPtr & query_ptr_, Context & context_) : query_ptr(query_ptr_), context(context_) {}
2011-11-05 23:31:19 +00:00
2015-06-18 02:11:05 +00:00
BlockIO InterpreterDropQuery::execute()
{
auto & drop = query_ptr->as<ASTDropQuery &>();
if (!drop.cluster.empty())
2020-01-24 16:20:36 +00:00
return executeDDLQueryOnCluster(query_ptr, context, getRequiredAccessForDDLOnCluster());
if (!drop.table.empty())
{
if (!drop.is_dictionary)
2020-03-20 00:07:52 +00:00
return executeToTable({drop.database, drop.table, drop.uuid}, drop);
else
return executeToDictionary(drop.database, drop.table, drop.kind, drop.if_exists, drop.temporary, drop.no_ddl_lock);
}
else if (!drop.database.empty())
return executeToDatabase(drop.database, drop.kind, drop.if_exists);
2018-04-21 00:35:20 +00:00
else
2020-03-23 22:28:30 +00:00
throw Exception("Nothing to drop, both names are empty", ErrorCodes::LOGICAL_ERROR);
2018-04-21 00:35:20 +00:00
}
BlockIO InterpreterDropQuery::executeToTable(
2020-03-20 00:07:52 +00:00
const StorageID & table_id_,
const ASTDropQuery & query)
2018-04-21 00:35:20 +00:00
{
2020-03-20 00:07:52 +00:00
if (query.temporary || table_id_.database_name.empty())
{
2020-03-20 00:07:52 +00:00
if (context.tryResolveStorageID(table_id_, Context::ResolveExternal))
return executeToTemporaryTable(table_id_.getTableName(), query.kind);
}
2020-03-20 00:07:52 +00:00
if (query.temporary)
{
2020-03-20 00:07:52 +00:00
if (query.if_exists)
2020-01-24 16:20:36 +00:00
return {};
throw Exception("Temporary table " + backQuoteIfNeed(table_id_.table_name) + " doesn't exist",
ErrorCodes::UNKNOWN_TABLE);
}
2020-04-03 23:37:58 +00:00
auto table_id = query.if_exists ? context.tryResolveStorageID(table_id_, Context::ResolveOrdinary)
: context.resolveStorageID(table_id_, Context::ResolveOrdinary);
if (!table_id)
return {};
2018-04-21 00:35:20 +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
2020-05-28 23:01:18 +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)
{
2020-03-23 22:28:30 +00:00
if (query_ptr->as<ASTDropQuery &>().is_view && !table->isView())
throw Exception("Table " + table_id.getNameForLogs() + " is not a View", ErrorCodes::LOGICAL_ERROR);
2020-03-23 22:28:30 +00:00
2020-03-20 00:07:52 +00:00
if (query.kind == ASTDropQuery::Kind::Detach)
{
2020-03-13 15:41:36 +00:00
context.checkAccess(table->isView() ? AccessType::DROP_VIEW : AccessType::DROP_TABLE, table_id);
2020-01-24 16:20:36 +00:00
table->shutdown();
2020-06-27 13:39:41 +00:00
TableExclusiveLockHolder table_lock;
if (database->getEngineName() != "Atomic" && database->getEngineName() != "Replicated")
table_lock = table->lockExclusively(context.getCurrentQueryId(), context.getSettingsRef().lock_acquire_timeout);
2018-04-21 00:35:20 +00:00
/// Drop table from memory, don't touch data and metadata
if (database->getEngineName() == "Replicated" && context.getClientInfo().query_kind != ClientInfo::QueryKind::REPLICATED_LOG_QUERY) {
2020-05-11 12:55:17 +00:00
database->propose(query_ptr);
} else {
database->detachTable(table_id.table_name);
2020-05-11 12:55:17 +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
{
context.checkAccess(AccessType::TRUNCATE, table_id);
2020-01-24 16:20:36 +00:00
table->checkTableCanBeDropped();
2018-06-09 15:48:22 +00:00
auto table_lock = table->lockExclusively(context.getCurrentQueryId(), context.getSettingsRef().lock_acquire_timeout);
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
if (database->getEngineName() == "Replicated" && context.getClientInfo().query_kind != ClientInfo::QueryKind::REPLICATED_LOG_QUERY) {
database->propose(query_ptr);
} else {
table->truncate(query_ptr, metadata_snapshot, context, table_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
{
2020-03-13 15:41:36 +00:00
context.checkAccess(table->isView() ? AccessType::DROP_VIEW : AccessType::DROP_TABLE, table_id);
2020-01-24 16:20:36 +00:00
table->checkTableCanBeDropped();
2018-04-21 00:35:20 +00:00
2020-03-30 14:53:05 +00:00
table->shutdown();
2020-06-18 16:10:47 +00:00
TableExclusiveLockHolder table_lock;
if (database->getEngineName() != "Atomic" && database->getEngineName() != "Replicated")
table_lock = table->lockExclusively(context.getCurrentQueryId(), context.getSettingsRef().lock_acquire_timeout);
// Prevents recursive drop from drop database query. The original query must specify a table.
if (!query_ptr->as<ASTDropQuery &>().table.empty() && database->getEngineName() == "Replicated" && context.getClientInfo().query_kind != ClientInfo::QueryKind::REPLICATED_LOG_QUERY) {
2020-05-11 12:55:17 +00:00
database->propose(query_ptr);
} else {
database->dropTable(context, table_id.table_name, query.no_delay);
2020-05-11 12:55:17 +00:00
}
2018-04-21 00:35:20 +00:00
}
}
if (database->getEngineName() == "Replicated" && context.getClientInfo().query_kind != ClientInfo::QueryKind::REPLICATED_LOG_QUERY) {
auto * database_replicated = typeid_cast<DatabaseReplicated *>(database.get());
return database_replicated->getFeedback();
}
2018-04-21 00:35:20 +00:00
return {};
}
BlockIO InterpreterDropQuery::executeToDictionary(
2020-01-24 16:20:36 +00:00
const String & database_name_,
const String & dictionary_name,
ASTDropQuery::Kind kind,
bool if_exists,
bool is_temporary,
bool no_ddl_lock)
{
if (is_temporary)
throw Exception("Temporary dictionaries are not possible.", ErrorCodes::SYNTAX_ERROR);
String database_name = context.resolveDatabase(database_name_);
2020-02-10 18:31:52 +00:00
auto ddl_guard = (!no_ddl_lock ? DatabaseCatalog::instance().getDDLGuard(database_name, dictionary_name) : nullptr);
DatabasePtr database = tryGetDatabase(database_name, if_exists);
if (!database || !database->isDictionaryExist(dictionary_name))
{
if (!if_exists)
throw Exception(
"Dictionary " + backQuoteIfNeed(database_name) + "." + backQuoteIfNeed(dictionary_name) + " doesn't exist.",
ErrorCodes::UNKNOWN_DICTIONARY);
else
return {};
}
if (kind == ASTDropQuery::Kind::Detach)
{
/// Drop dictionary from memory, don't touch data and metadata
context.checkAccess(AccessType::DROP_DICTIONARY, database_name, dictionary_name);
database->detachDictionary(dictionary_name);
}
else if (kind == ASTDropQuery::Kind::Truncate)
{
throw Exception("Cannot TRUNCATE dictionary", ErrorCodes::SYNTAX_ERROR);
}
else if (kind == ASTDropQuery::Kind::Drop)
{
2020-01-24 16:20:36 +00:00
context.checkAccess(AccessType::DROP_DICTIONARY, database_name, dictionary_name);
database->removeDictionary(context, dictionary_name);
}
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)
throw Exception("Unable to detach temporary table.", ErrorCodes::SYNTAX_ERROR);
else
2018-04-21 00:35:20 +00:00
{
auto & context_handle = context.hasSessionContext() ? context.getSessionContext() : context;
2020-02-12 18:14:12 +00:00
auto resolved_id = context_handle.tryResolveStorageID(StorageID("", table_name), Context::ResolveExternal);
if (resolved_id)
2018-04-21 00:35:20 +00:00
{
2020-05-28 23:01:18 +00:00
StoragePtr table = DatabaseCatalog::instance().getTable(resolved_id, context);
if (kind == ASTDropQuery::Kind::Truncate)
{
auto table_lock = table->lockExclusively(context.getCurrentQueryId(), context.getSettingsRef().lock_acquire_timeout);
/// Drop table data, don't touch metadata
2020-06-18 10:29:13 +00:00
auto metadata_snapshot = table->getInMemoryMetadataPtr();
table->truncate(query_ptr, metadata_snapshot, context, table_lock);
}
else if (kind == ASTDropQuery::Kind::Drop)
{
context_handle.removeExternalTable(table_name);
table->shutdown();
auto table_lock = table->lockExclusively(context.getCurrentQueryId(), context.getSettingsRef().lock_acquire_timeout);
/// Delete table data
2020-01-22 11:30:11 +00:00
table->drop();
table->is_dropped = true;
}
}
}
2018-04-21 00:35:20 +00:00
return {};
}
2020-03-18 00:57:00 +00:00
2020-01-24 16:20:36 +00:00
BlockIO InterpreterDropQuery::executeToDatabase(const String & database_name, ASTDropQuery::Kind kind, bool if_exists)
2018-04-21 00:35:20 +00:00
{
2020-02-10 18:31:52 +00:00
auto ddl_guard = DatabaseCatalog::instance().getDDLGuard(database_name, "");
2018-09-18 18:33:15 +00:00
2018-04-21 00:35:20 +00:00
if (auto database = tryGetDatabase(database_name, if_exists))
{
2018-04-21 00:35:20 +00:00
if (kind == ASTDropQuery::Kind::Truncate)
{
2020-03-23 22:28:30 +00:00
throw Exception("Unable to truncate database", ErrorCodes::SYNTAX_ERROR);
}
2020-02-13 21:00:03 +00:00
else if (kind == ASTDropQuery::Kind::Detach || kind == ASTDropQuery::Kind::Drop)
2018-04-21 00:35:20 +00:00
{
2020-02-13 21:00:03 +00:00
bool drop = kind == ASTDropQuery::Kind::Drop;
2020-01-24 16:20:36 +00:00
context.checkAccess(AccessType::DROP_DATABASE, database_name);
if (database->shouldBeEmptyOnDetach())
2018-04-21 00:35:20 +00:00
{
/// DETACH or DROP all tables and dictionaries inside database.
/// First we should DETACH or DROP dictionaries because StorageDictionary
/// must be detached only by detaching corresponding dictionary.
for (auto iterator = database->getDictionariesIterator(); iterator->isValid(); iterator->next())
{
String current_dictionary = iterator->name();
executeToDictionary(database_name, current_dictionary, kind, false, false, false);
}
ASTDropQuery query;
query.kind = kind;
query.database = database_name;
2020-05-28 20:10:45 +00:00
for (auto iterator = database->getTablesIterator(context); iterator->isValid(); iterator->next())
{
query.table = iterator->name();
executeToTable({query.database, query.table}, query);
}
2019-10-11 15:22:30 +00:00
}
2020-04-23 19:50:05 +00:00
auto * database_atomic = typeid_cast<DatabaseAtomic *>(database.get());
2020-04-10 23:02:15 +00:00
if (!drop && database_atomic)
database_atomic->assertCanBeDetached(true);
2020-02-13 21:00:03 +00:00
/// DETACH or DROP database itself
DatabaseCatalog::instance().detachDatabase(database_name, drop, database->shouldBeEmptyOnDetach());
}
}
2018-04-21 00:35:20 +00:00
return {};
}
2020-01-24 16:20:36 +00:00
AccessRightsElements InterpreterDropQuery::getRequiredAccessForDDLOnCluster() const
{
AccessRightsElements required_access;
const auto & drop = query_ptr->as<const ASTDropQuery &>();
2020-01-24 16:20:36 +00:00
if (drop.table.empty())
{
if (drop.kind == ASTDropQuery::Kind::Detach)
required_access.emplace_back(AccessType::DROP_DATABASE, drop.database);
2020-01-24 16:20:36 +00:00
else if (drop.kind == ASTDropQuery::Kind::Drop)
required_access.emplace_back(AccessType::DROP_DATABASE, drop.database);
}
else if (drop.is_dictionary)
{
if (drop.kind == ASTDropQuery::Kind::Detach)
required_access.emplace_back(AccessType::DROP_DICTIONARY, drop.database, drop.table);
2020-01-24 16:20:36 +00:00
else if (drop.kind == ASTDropQuery::Kind::Drop)
required_access.emplace_back(AccessType::DROP_DICTIONARY, drop.database, drop.table);
}
else if (!drop.temporary)
{
/// It can be view or table.
if (drop.kind == ASTDropQuery::Kind::Drop)
required_access.emplace_back(AccessType::DROP_TABLE | AccessType::DROP_VIEW, drop.database, drop.table);
else if (drop.kind == ASTDropQuery::Kind::Truncate)
required_access.emplace_back(AccessType::TRUNCATE, drop.database, drop.table);
2020-01-24 16:20:36 +00:00
else if (drop.kind == ASTDropQuery::Kind::Detach)
required_access.emplace_back(AccessType::DROP_TABLE | AccessType::DROP_VIEW, drop.database, drop.table);
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-22 19:20:18 +00:00
2011-11-05 23:31:19 +00:00
}