ClickHouse/src/Interpreters/InterpreterUndropQuery.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
2.7 KiB
C++
Raw Normal View History

2023-03-21 12:14:26 +00:00
#include <Interpreters/Context.h>
#include <Interpreters/executeDDLQueryOnCluster.h>
#include <Interpreters/InterpreterUndropQuery.h>
#include <Access/Common/AccessRightsElement.h>
#include <Parsers/ASTUndropQuery.h>
#include "config.h"
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int TABLE_ALREADY_EXISTS;
extern const int SUPPORT_IS_DISABLED;
}
InterpreterUndropQuery::InterpreterUndropQuery(const ASTPtr & query_ptr_, ContextMutablePtr context_) : WithMutableContext(context_), query_ptr(query_ptr_)
{
}
BlockIO InterpreterUndropQuery::execute()
{
if (!getContext()->getSettingsRef().allow_experimental_undrop_table_query)
throw Exception(ErrorCodes::SUPPORT_IS_DISABLED,
"Undrop table is experimental. "
"Set `allow_experimental_undrop_table_query` setting to enable it");
getContext()->checkAccess(AccessType::UNDROP_TABLE);
auto & undrop = query_ptr->as<ASTUndropQuery &>();
if (!undrop.cluster.empty() && !maybeRemoveOnCluster(query_ptr, getContext()))
{
DDLQueryOnClusterParams params;
params.access_to_check = getRequiredAccessForDDLOnCluster();
return executeDDLQueryOnCluster(query_ptr, getContext(), params);
}
if (undrop.table)
return executeToTable(undrop);
else
throw Exception(ErrorCodes::LOGICAL_ERROR, "Nothing to undrop, both names are empty");
}
BlockIO InterpreterUndropQuery::executeToTable(ASTUndropQuery & query)
{
auto table_id = StorageID(query);
auto context = getContext();
if (table_id.database_name.empty())
{
table_id.database_name = context->getCurrentDatabase();
query.setDatabase(table_id.database_name);
}
2023-03-26 05:15:23 +00:00
auto guard = DatabaseCatalog::instance().getDDLGuard(table_id.database_name, table_id.table_name);
2023-03-21 12:14:26 +00:00
auto database = DatabaseCatalog::instance().getDatabase(table_id.database_name);
2023-03-28 21:47:45 +00:00
if (database->getEngineName() == "Replicated")
throw Exception(ErrorCodes::SUPPORT_IS_DISABLED, "Replicated database does not support UNDROP query");
2023-03-21 12:14:26 +00:00
if (database->isTableExist(table_id.table_name, getContext()))
throw Exception(
2023-03-28 21:47:45 +00:00
ErrorCodes::TABLE_ALREADY_EXISTS, "Cannot undrop table, {} already exists", table_id);
2023-03-21 12:14:26 +00:00
database->checkMetadataFilenameAvailability(table_id.table_name);
DatabaseCatalog::instance().dequeueDroppedTableCleanup(table_id);
return {};
}
AccessRightsElements InterpreterUndropQuery::getRequiredAccessForDDLOnCluster() const
{
AccessRightsElements required_access;
const auto & undrop = query_ptr->as<const ASTUndropQuery &>();
required_access.emplace_back(AccessType::UNDROP_TABLE, undrop.getDatabase(), undrop.getTable());
return required_access;
}
}