ClickHouse/dbms/include/DB/Parsers/ASTAlterQuery.h

185 lines
5.9 KiB
C
Raw Normal View History

2013-08-07 13:07:42 +00:00
#pragma once
#include <DB/Parsers/IAST.h>
#include <mysqlxx/Manip.h>
2013-08-07 13:07:42 +00:00
namespace DB
{
/** ALTER запрос
* ALTER TABLE [db.]name_type
* ADD COLUMN col_name type [AFTER col_after],
* DROP COLUMN col_drop,
* MODIFY COLUMN col_name type,
* DROP PARTITION partition
2013-08-07 13:07:42 +00:00
* ...
*/
class ASTAlterQuery : public IAST
{
public:
enum ParameterType
{
ADD_COLUMN,
DROP_COLUMN,
MODIFY_COLUMN,
DROP_PARTITION,
2014-08-07 11:46:01 +00:00
ATTACH_PARTITION,
FETCH_PARTITION,
FREEZE_PARTITION,
2013-08-07 13:07:42 +00:00
NO_TYPE
};
struct Parameters
{
Parameters() : type(NO_TYPE) {}
2014-08-07 09:23:55 +00:00
int type = NO_TYPE;
2013-08-07 13:07:42 +00:00
/** В запросе ADD COLUMN здесь хранится имя и тип добавляемого столбца
2014-07-10 10:16:50 +00:00
* В запросе DROP это поле не используется
* В запросе MODIFY здесь хранится имя столбца и новый тип
*/
ASTPtr col_decl;
2014-07-10 10:16:50 +00:00
/** В запросе ADD COLUMN здесь опционально хранится имя столбца, следующее после AFTER
2014-07-10 10:16:50 +00:00
* В запросе DROP здесь хранится имя столбца для удаления
*/
2013-08-07 13:07:42 +00:00
ASTPtr column;
/** В запросе DROP PARTITION здесь хранится имя partition'а.
*/
ASTPtr partition;
2014-08-07 09:23:55 +00:00
bool detach = false; /// true для DETACH PARTITION.
2014-08-07 11:46:01 +00:00
bool part = false; /// true для ATTACH [UNREPLICATED] PART
2015-04-21 13:10:08 +00:00
bool unreplicated = false; /// true для ATTACH UNREPLICATED, DROP UNREPLICATED ...
2014-08-07 11:46:01 +00:00
/** Для FETCH PARTITION - путь в ZK к шарду, с которого скачивать партицию.
*/
String from;
2013-08-07 13:07:42 +00:00
/// deep copy
2013-08-09 00:12:59 +00:00
void clone(Parameters & p) const
2013-08-07 13:07:42 +00:00
{
2014-08-07 11:46:01 +00:00
p = *this;
if (col_decl) p.col_decl = col_decl->clone();
if (column) p.column = column->clone();
if (partition) p.partition = partition->clone();
2013-08-07 13:07:42 +00:00
}
};
typedef std::vector<Parameters> ParameterContainer;
ParameterContainer parameters;
String database;
String table;
void addParameters(const Parameters & params)
{
parameters.push_back(params);
if (params.col_decl)
children.push_back(params.col_decl);
if (params.column)
children.push_back(params.column);
if (params.partition)
children.push_back(params.partition);
}
2014-04-08 07:47:51 +00:00
ASTAlterQuery(StringRange range_ = StringRange()) : IAST(range_) {};
2013-08-07 13:07:42 +00:00
/** Получить текст, который идентифицирует этот элемент. */
2014-12-17 15:26:24 +00:00
String getID() const override { return ("AlterQuery_" + database + "_" + table); };
2013-08-07 13:07:42 +00:00
2014-12-17 15:26:24 +00:00
ASTPtr clone() const override
2013-08-07 13:07:42 +00:00
{
ASTAlterQuery * res = new ASTAlterQuery(*this);
2013-08-09 00:12:59 +00:00
for (ParameterContainer::size_type i = 0; i < parameters.size(); ++i)
2013-08-07 13:07:42 +00:00
parameters[i].clone(res->parameters[i]);
return res;
}
protected:
void formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override
{
frame.need_parens = false;
std::string indent_str = settings.one_line ? "" : std::string(4 * frame.indent, ' ');
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "ALTER TABLE " << (settings.hilite ? hilite_none : "");
if (!table.empty())
{
if (!database.empty())
{
settings.ostr << indent_str << database;
settings.ostr << ".";
}
settings.ostr << indent_str << table;
}
settings.ostr << settings.nl_or_ws;
for (size_t i = 0; i < parameters.size(); ++i)
{
const ASTAlterQuery::Parameters & p = parameters[i];
if (p.type == ASTAlterQuery::ADD_COLUMN)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "ADD COLUMN " << (settings.hilite ? hilite_none : "");
p.col_decl->formatImpl(settings, state, frame);
/// AFTER
if (p.column)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << " AFTER " << (settings.hilite ? hilite_none : "");
p.column->formatImpl(settings, state, frame);
}
}
else if (p.type == ASTAlterQuery::DROP_COLUMN)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "DROP COLUMN " << (settings.hilite ? hilite_none : "");
p.column->formatImpl(settings, state, frame);
}
else if (p.type == ASTAlterQuery::MODIFY_COLUMN)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "MODIFY COLUMN " << (settings.hilite ? hilite_none : "");
p.col_decl->formatImpl(settings, state, frame);
}
else if (p.type == ASTAlterQuery::DROP_PARTITION)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << (p.detach ? "DETACH" : "DROP") << " PARTITION "
<< (settings.hilite ? hilite_none : "");
p.partition->formatImpl(settings, state, frame);
}
else if (p.type == ASTAlterQuery::ATTACH_PARTITION)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "ATTACH " << (p.unreplicated ? "UNREPLICATED " : "")
<< (p.part ? "PART " : "PARTITION ") << (settings.hilite ? hilite_none : "");
p.partition->formatImpl(settings, state, frame);
}
else if (p.type == ASTAlterQuery::FETCH_PARTITION)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "FETCH " << (p.unreplicated ? "UNREPLICATED " : "")
<< "PARTITION " << (settings.hilite ? hilite_none : "");
p.partition->formatImpl(settings, state, frame);
settings.ostr << (settings.hilite ? hilite_keyword : "")
<< " FROM " << (settings.hilite ? hilite_none : "") << mysqlxx::quote << p.from;
}
else if (p.type == ASTAlterQuery::FREEZE_PARTITION)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "FREEZE PARTITION " << (settings.hilite ? hilite_none : "");
p.partition->formatImpl(settings, state, frame);
}
else
throw Exception("Unexpected type of ALTER", ErrorCodes::UNEXPECTED_AST_STRUCTURE);
std::string comma = (i < (parameters.size() -1) ) ? "," : "";
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << comma << (settings.hilite ? hilite_none : "");
settings.ostr << settings.nl_or_ws;
}
}
2013-08-07 13:07:42 +00:00
};
2013-08-07 13:07:42 +00:00
}