2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTRenameQuery.h>
|
|
|
|
#include <Databases/IDatabase.h>
|
2017-05-23 18:24:43 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterRenameQuery.h>
|
|
|
|
#include <Storages/IStorage.h>
|
2017-04-21 12:39:28 +00:00
|
|
|
#include <Interpreters/DDLWorker.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_)
|
2017-04-01 07:20:54 +00:00
|
|
|
: query_ptr(query_ptr_), context(context_)
|
2012-06-18 06:20:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-08 01:06:15 +00:00
|
|
|
struct RenameDescription
|
|
|
|
{
|
2017-12-01 21:13:25 +00:00
|
|
|
RenameDescription(const ASTRenameQuery::Element & elem, const String & current_database) :
|
2017-04-01 07:20:54 +00:00
|
|
|
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-04-08 01:06:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
BlockIO InterpreterRenameQuery::execute()
|
2012-06-18 06:20:23 +00:00
|
|
|
{
|
2017-04-21 12:39:28 +00:00
|
|
|
ASTRenameQuery & rename = typeid_cast<ASTRenameQuery &>(*query_ptr);
|
|
|
|
|
|
|
|
if (!rename.cluster.empty())
|
2018-04-17 19:33:58 +00:00
|
|
|
{
|
|
|
|
NameSet databases;
|
|
|
|
for (const auto & elem : rename.elements)
|
|
|
|
{
|
|
|
|
databases.emplace(elem.from.database);
|
|
|
|
databases.emplace(elem.to.database);
|
|
|
|
}
|
|
|
|
|
|
|
|
return executeDDLQueryOnCluster(query_ptr, context, databases);
|
|
|
|
}
|
2017-04-21 12:39:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String path = context.getPath();
|
|
|
|
String current_database = context.getCurrentDatabase();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** 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
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::vector<RenameDescription> descriptions;
|
|
|
|
descriptions.reserve(rename.elements.size());
|
2015-04-08 01:06:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// To avoid deadlocks, we must acquire locks for tables in same order in any different RENAMES.
|
|
|
|
struct UniqueTableName
|
|
|
|
{
|
|
|
|
String database_name;
|
|
|
|
String table_name;
|
2015-04-08 01:06:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
UniqueTableName(const String & database_name_, const String & table_name_)
|
|
|
|
: database_name(database_name_), table_name(table_name_) {}
|
2015-04-08 01:06:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool operator< (const UniqueTableName & rhs) const
|
|
|
|
{
|
|
|
|
return std::tie(database_name, table_name) < std::tie(rhs.database_name, rhs.table_name);
|
|
|
|
}
|
|
|
|
};
|
2015-04-08 01:06:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::set<UniqueTableName> unique_tables_from;
|
2016-03-22 01:33:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Don't allow to drop tables (that we are renaming); do't allow to create tables in places where tables will be renamed.
|
|
|
|
std::map<UniqueTableName, std::unique_ptr<DDLGuard>> table_guards;
|
2015-04-08 01:06:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & elem : rename.elements)
|
|
|
|
{
|
2017-12-01 21:13:25 +00:00
|
|
|
descriptions.emplace_back(elem, current_database);
|
2016-03-22 01:33:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
UniqueTableName from(descriptions.back().from_database_name, descriptions.back().from_table_name);
|
|
|
|
UniqueTableName to(descriptions.back().to_database_name, descriptions.back().to_table_name);
|
2016-03-22 01:33:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
unique_tables_from.emplace(from);
|
2016-03-22 01:33:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!table_guards.count(from))
|
|
|
|
table_guards.emplace(from,
|
|
|
|
context.getDDLGuard(
|
|
|
|
from.database_name,
|
|
|
|
from.table_name,
|
|
|
|
"Table " + from.database_name + "." + from.table_name + " is being renamed right now"));
|
2016-03-22 01:33:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!table_guards.count(to))
|
|
|
|
table_guards.emplace(to,
|
|
|
|
context.getDDLGuard(
|
|
|
|
to.database_name,
|
|
|
|
to.table_name,
|
|
|
|
"Some table right now is being renamed to " + to.database_name + "." + to.table_name));
|
|
|
|
}
|
2012-06-18 06:20:23 +00:00
|
|
|
|
2017-07-28 17:34:02 +00:00
|
|
|
std::vector<TableFullWriteLock> locks;
|
2017-04-01 07:20:54 +00:00
|
|
|
locks.reserve(unique_tables_from.size());
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & names : unique_tables_from)
|
|
|
|
if (auto table = context.tryGetTable(names.database_name, names.table_name))
|
2017-09-01 15:05:23 +00:00
|
|
|
locks.emplace_back(table->lockForAlter(__PRETTY_FUNCTION__));
|
2014-03-20 10:59:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** All tables are locked. If there are more than one rename in chain,
|
|
|
|
* we need to hold global lock while doing all renames. Order matters to avoid deadlocks.
|
|
|
|
* It provides atomicity of all RENAME chain as a whole, from the point of view of DBMS client,
|
|
|
|
* but only in cases when there was no exceptions during this process and server does not fall.
|
|
|
|
*/
|
2014-03-20 10:59:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
decltype(context.getLock()) lock;
|
2016-03-22 01:33:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (descriptions.size() > 1)
|
|
|
|
lock = context.getLock();
|
2012-06-18 06:20:23 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & elem : descriptions)
|
|
|
|
{
|
|
|
|
context.assertTableDoesntExist(elem.to_database_name, elem.to_table_name);
|
2014-03-20 10:59:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
context.getDatabase(elem.from_database_name)->renameTable(
|
2017-09-11 12:39:01 +00:00
|
|
|
context, elem.from_table_name, *context.getDatabase(elem.to_database_name), elem.to_table_name);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-06-18 02:11:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return {};
|
2012-06-18 06:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|