2012-06-18 06:19:13 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/IInterpreter.h>
|
2020-07-07 12:11:58 +00:00
|
|
|
#include <Parsers/ASTRenameQuery.h>
|
2012-06-18 06:19:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-05-23 18:24:43 +00:00
|
|
|
class Context;
|
2020-01-24 16:20:36 +00:00
|
|
|
class AccessRightsElements;
|
2020-12-10 23:56:57 +00:00
|
|
|
class DDLGuard;
|
2017-05-23 18:24:43 +00:00
|
|
|
|
2020-03-30 23:36:23 +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;
|
|
|
|
|
|
|
|
UniqueTableName(const String & database_name_, const String & table_name_)
|
|
|
|
: database_name(database_name_), table_name(table_name_) {}
|
|
|
|
|
|
|
|
bool operator< (const UniqueTableName & rhs) const
|
|
|
|
{
|
|
|
|
return std::tie(database_name, table_name) < std::tie(rhs.database_name, rhs.table_name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-07 12:11:58 +00:00
|
|
|
struct RenameDescription
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
using RenameDescriptions = std::vector<RenameDescription>;
|
|
|
|
|
2020-03-30 23:36:23 +00:00
|
|
|
using TableGuards = std::map<UniqueTableName, std::unique_ptr<DDLGuard>>;
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-01-23 18:05:07 +00:00
|
|
|
/** Rename one table
|
|
|
|
* or rename many tables at once.
|
2012-06-18 06:19:13 +00:00
|
|
|
*/
|
2015-06-18 02:11:05 +00:00
|
|
|
class InterpreterRenameQuery : public IInterpreter
|
2012-06-18 06:19:13 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-05-23 18:24:43 +00:00
|
|
|
InterpreterRenameQuery(const ASTPtr & query_ptr_, Context & context_);
|
2017-04-01 07:20:54 +00:00
|
|
|
BlockIO execute() override;
|
2020-12-14 03:30:39 +00:00
|
|
|
void extendQueryLogElemImpl(QueryLogElement & elem, const ASTPtr & ast, const Context &) const override;
|
2012-06-18 06:19:13 +00:00
|
|
|
|
|
|
|
private:
|
2020-07-07 12:11:58 +00:00
|
|
|
BlockIO executeToTables(const ASTRenameQuery & rename, const RenameDescriptions & descriptions);
|
2020-07-17 13:11:44 +00:00
|
|
|
static BlockIO executeToDatabase(const ASTRenameQuery & rename, const RenameDescriptions & descriptions);
|
2020-07-07 12:11:58 +00:00
|
|
|
|
2020-01-24 16:20:36 +00:00
|
|
|
AccessRightsElements getRequiredAccess() const;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr query_ptr;
|
2017-05-23 18:24:43 +00:00
|
|
|
Context & context;
|
2021-02-01 19:29:47 +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;
|
2012-06-18 06:19:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|