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

190 lines
6.0 KiB
C
Raw Normal View History

2013-08-07 13:07:42 +00:00
#pragma once
#include <DB/Parsers/IAST.h>
2016-01-26 02:00:25 +00:00
#include <mysqlxx/Manip.h>
2013-08-07 13:07:42 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int UNEXPECTED_AST_STRUCTURE;
}
2013-08-07 13:07:42 +00:00
/** 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
{
2016-01-26 02:00:25 +00:00
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;
2016-01-26 02:00:25 +00:00
/** В запросе 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
2016-01-26 02:00:25 +00:00
void clone(Parameters & p) const
{
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;
2016-01-26 02:00:25 +00:00
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);
}
2016-01-26 02:00:25 +00:00
ASTAlterQuery(StringRange range_ = StringRange()) : IAST(range_) {};
2013-08-07 13:07:42 +00:00
/** Получить текст, который идентифицирует этот элемент. */
2016-01-26 02:00:25 +00:00
String getID() const override { return ("AlterQuery_" + database + "_" + table); };
2013-08-07 13:07:42 +00:00
2016-01-26 02:00:25 +00:00
ASTPtr clone() const override
{
ASTAlterQuery * res = new ASTAlterQuery(*this);
for (ParameterContainer::size_type i = 0; i < parameters.size(); ++i)
parameters[i].clone(res->parameters[i]);
return res;
}
protected:
2016-01-26 02:00:25 +00:00
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
}