ClickHouse/src/Interpreters/InterpreterRenameQuery.h

79 lines
2.2 KiB
C++
Raw Normal View History

2012-06-18 06:19:13 +00:00
#pragma once
#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
{
2020-01-24 16:20:36 +00:00
class AccessRightsElements;
class DDLGuard;
2017-05-23 18:24:43 +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),
if_exists(elem.if_exists)
2020-07-07 12:11:58 +00:00
{}
String from_database_name;
String from_table_name;
String to_database_name;
String to_table_name;
bool if_exists;
2020-07-07 12:11:58 +00:00
};
using RenameDescriptions = std::vector<RenameDescription>;
using TableGuards = std::map<UniqueTableName, std::unique_ptr<DDLGuard>>;
2012-06-18 06:19:13 +00:00
/** Rename one table
* or rename many tables at once.
2012-06-18 06:19:13 +00:00
*/
class InterpreterRenameQuery : public IInterpreter, WithContext
2012-06-18 06:19:13 +00:00
{
public:
InterpreterRenameQuery(const ASTPtr & query_ptr_, ContextPtr context_);
BlockIO execute() override;
void extendQueryLogElemImpl(QueryLogElement & elem, const ASTPtr & ast, ContextPtr) const override;
2012-06-18 06:19:13 +00:00
2021-07-01 13:21:38 +00:00
bool renamedInsteadOfExchange() const { return renamed_instead_of_exchange; }
2012-06-18 06:19:13 +00:00
private:
2021-02-02 19:39:04 +00:00
BlockIO executeToTables(const ASTRenameQuery & rename, const RenameDescriptions & descriptions, TableGuards & ddl_guards);
2021-11-02 12:58:45 +00:00
BlockIO executeToDatabase(const ASTRenameQuery & rename, const RenameDescriptions & descriptions);
2020-07-07 12:11:58 +00:00
enum class RenameType
{
RenameTable,
RenameDatabase
};
AccessRightsElements getRequiredAccess(RenameType type) const;
2020-01-24 16:20:36 +00:00
ASTPtr query_ptr;
2021-07-01 13:21:38 +00:00
bool renamed_instead_of_exchange{false};
2012-06-18 06:19:13 +00:00
};
}