ClickHouse/dbms/Interpreters/InterpreterRenameQuery.cpp

108 lines
3.5 KiB
C++
Raw Normal View History

#include <Parsers/ASTRenameQuery.h>
#include <Databases/IDatabase.h>
2017-05-23 18:24:43 +00:00
#include <Interpreters/Context.h>
#include <Interpreters/InterpreterRenameQuery.h>
#include <Storages/IStorage.h>
2017-04-21 12:39:28 +00:00
#include <Interpreters/DDLWorker.h>
#include <Access/AccessRightsElement.h>
2017-07-13 20:58:19 +00:00
#include <Common/typeid_cast.h>
2012-06-18 06:20:23 +00:00
namespace DB
{
2017-05-23 18:24:43 +00:00
InterpreterRenameQuery::InterpreterRenameQuery(const ASTPtr & query_ptr_, Context & context_)
: query_ptr(query_ptr_), context(context_)
2012-06-18 06:20:23 +00:00
{
}
struct RenameDescription
{
2017-12-01 21:13:25 +00:00
RenameDescription(const ASTRenameQuery::Element & elem, const String & current_database) :
from_database_name(elem.from.database.empty() ? current_database : elem.from.database),
from_table_name(elem.from.table),
to_database_name(elem.to.database.empty() ? current_database : elem.to.database),
to_table_name(elem.to.table)
{}
String from_database_name;
String from_table_name;
String to_database_name;
String to_table_name;
};
2015-06-18 02:11:05 +00:00
BlockIO InterpreterRenameQuery::execute()
2012-06-18 06:20:23 +00:00
{
2020-01-24 16:20:36 +00:00
const auto & rename = query_ptr->as<const ASTRenameQuery &>();
2017-04-21 12:39:28 +00:00
if (!rename.cluster.empty())
2020-01-24 16:20:36 +00:00
return executeDDLQueryOnCluster(query_ptr, context, getRequiredAccess());
2020-01-24 16:20:36 +00:00
context.checkAccess(getRequiredAccess());
2017-04-21 12:39:28 +00:00
String path = context.getPath();
String current_database = context.getCurrentDatabase();
/** In case of error while renaming, it is possible that only part of tables was renamed
* or we will be in inconsistent state. (It is worth to be fixed.)
*/
2012-06-18 06:20:23 +00:00
std::vector<RenameDescription> descriptions;
descriptions.reserve(rename.elements.size());
2019-01-22 19:56:53 +00:00
/// Don't allow to drop tables (that we are renaming); don't allow to create tables in places where tables will be renamed.
TableGuards table_guards;
for (const auto & elem : rename.elements)
{
2017-12-01 21:13:25 +00:00
descriptions.emplace_back(elem, current_database);
2020-01-24 16:20:36 +00:00
const auto & description = descriptions.back();
2020-01-24 16:20:36 +00:00
UniqueTableName from(description.from_database_name, description.from_table_name);
UniqueTableName to(description.to_database_name, description.to_table_name);
table_guards[from];
table_guards[to];
}
2012-06-18 06:20:23 +00:00
2020-02-10 18:31:52 +00:00
auto & database_catalog = DatabaseCatalog::instance();
/// Must do it in consistent order.
for (auto & table_guard : table_guards)
2020-02-10 18:31:52 +00:00
table_guard.second = database_catalog.getDDLGuard(table_guard.first.database_name, table_guard.first.table_name);
2012-06-18 06:20:23 +00:00
2019-08-27 23:47:30 +00:00
for (auto & elem : descriptions)
{
2020-03-13 15:41:36 +00:00
database_catalog.assertTableDoesntExist(StorageID(elem.to_database_name, elem.to_table_name));
2020-02-17 19:28:25 +00:00
auto from_table = database_catalog.getTable({elem.from_database_name, elem.from_table_name});
auto from_table_lock = from_table->lockExclusively(context.getCurrentQueryId());
database_catalog.getDatabase(elem.from_database_name)->renameTable(
context,
elem.from_table_name,
*database_catalog.getDatabase(elem.to_database_name),
elem.to_table_name,
from_table_lock);
}
2015-06-18 02:11:05 +00:00
return {};
2012-06-18 06:20:23 +00:00
}
2020-01-24 16:20:36 +00:00
AccessRightsElements InterpreterRenameQuery::getRequiredAccess() const
{
AccessRightsElements required_access;
const auto & rename = query_ptr->as<const ASTRenameQuery &>();
for (const auto & elem : rename.elements)
{
required_access.emplace_back(AccessType::SELECT | AccessType::DROP_TABLE, elem.from.database, elem.from.table);
required_access.emplace_back(AccessType::CREATE_TABLE | AccessType::INSERT, elem.to.database, elem.to.table);
}
return required_access;
}
2012-06-18 06:20:23 +00:00
}