2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTAlterQuery.h>
|
2017-04-16 05:40:17 +00:00
|
|
|
#include <iomanip>
|
|
|
|
|
2016-01-28 01:00:27 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int UNEXPECTED_AST_STRUCTURE;
|
2016-01-28 01:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTAlterQuery::Parameters::Parameters() : type(NO_TYPE) {}
|
|
|
|
|
|
|
|
void ASTAlterQuery::Parameters::clone(Parameters & p) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
p = *this;
|
2017-04-21 12:39:28 +00:00
|
|
|
if (col_decl) p.col_decl = col_decl->clone();
|
|
|
|
if (column) p.column = column->clone();
|
|
|
|
if (partition) p.partition = partition->clone();
|
2017-04-01 07:20:54 +00:00
|
|
|
if (weighted_zookeeper_paths) p.weighted_zookeeper_paths = weighted_zookeeper_paths->clone();
|
2017-04-21 12:39:28 +00:00
|
|
|
if (sharding_key_expr) p.sharding_key_expr = sharding_key_expr->clone();
|
|
|
|
if (coordinator) p.coordinator = coordinator->clone();
|
2016-01-28 01:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTAlterQuery::addParameters(const Parameters & params)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
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);
|
|
|
|
if (params.weighted_zookeeper_paths)
|
|
|
|
children.push_back(params.weighted_zookeeper_paths);
|
|
|
|
if (params.sharding_key_expr)
|
|
|
|
children.push_back(params.sharding_key_expr);
|
|
|
|
if (params.coordinator)
|
|
|
|
children.push_back(params.coordinator);
|
|
|
|
if (params.primary_key)
|
|
|
|
children.push_back(params.primary_key);
|
2016-01-28 01:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTAlterQuery::ASTAlterQuery(StringRange range_) : IAST(range_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/** Get the text that identifies this element. */
|
2016-01-28 01:00:27 +00:00
|
|
|
String ASTAlterQuery::getID() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return ("AlterQuery_" + database + "_" + table);
|
2016-01-28 01:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr ASTAlterQuery::clone() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto res = std::make_shared<ASTAlterQuery>(*this);
|
|
|
|
for (ParameterContainer::size_type i = 0; i < parameters.size(); ++i)
|
|
|
|
parameters[i].clone(res->parameters[i]);
|
|
|
|
return res;
|
2016-01-28 01:00:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 12:39:28 +00:00
|
|
|
ASTPtr ASTAlterQuery::getRewrittenASTWithoutOnCluster(const std::string & new_database) const
|
|
|
|
{
|
|
|
|
auto query_ptr = clone();
|
|
|
|
ASTAlterQuery & query = static_cast<ASTAlterQuery &>(*query_ptr);
|
|
|
|
|
|
|
|
query.cluster.clear();
|
|
|
|
if (query.database.empty())
|
|
|
|
query.database = new_database;
|
|
|
|
|
|
|
|
return query_ptr;
|
|
|
|
}
|
|
|
|
|
2016-01-28 01:00:27 +00:00
|
|
|
void ASTAlterQuery::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
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 << backQuoteIfNeed(database);
|
|
|
|
settings.ostr << ".";
|
|
|
|
}
|
|
|
|
settings.ostr << indent_str << backQuoteIfNeed(table);
|
|
|
|
}
|
2017-04-21 12:39:28 +00:00
|
|
|
formatOnCluster(settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
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)
|
|
|
|
{
|
2017-06-22 11:01:30 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str
|
|
|
|
<< (p.clear_column ? "CLEAR " : "DROP ") << "COLUMN " << (settings.hilite ? hilite_none : "");
|
2017-04-01 07:20:54 +00:00
|
|
|
p.column->formatImpl(settings, state, frame);
|
2017-04-14 12:40:48 +00:00
|
|
|
if (p.partition)
|
|
|
|
{
|
2017-06-22 11:01:30 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str<< " IN PARTITION " << (settings.hilite ? hilite_none : "");
|
2017-04-14 12:40:48 +00:00
|
|
|
p.partition->formatImpl(settings, state, frame);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
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::MODIFY_PRIMARY_KEY)
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "MODIFY PRIMARY KEY " << (settings.hilite ? hilite_none : "");
|
|
|
|
settings.ostr << "(";
|
|
|
|
p.primary_key->formatImpl(settings, state, frame);
|
|
|
|
settings.ostr << ")";
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2017-05-24 21:38:56 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "ATTACH "
|
|
|
|
<< (p.part ? "PART " : "PARTITION ") << (settings.hilite ? hilite_none : "");
|
2017-04-01 07:20:54 +00:00
|
|
|
p.partition->formatImpl(settings, state, frame);
|
|
|
|
}
|
|
|
|
else if (p.type == ASTAlterQuery::FETCH_PARTITION)
|
|
|
|
{
|
2017-05-24 21:38:56 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "FETCH "
|
|
|
|
<< "PARTITION " << (settings.hilite ? hilite_none : "");
|
2017-04-01 07:20:54 +00:00
|
|
|
p.partition->formatImpl(settings, state, frame);
|
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "")
|
2017-04-16 05:40:17 +00:00
|
|
|
<< " FROM " << (settings.hilite ? hilite_none : "") << std::quoted(p.from, '\'');
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (!p.with_name.empty())
|
|
|
|
{
|
|
|
|
settings.ostr << " " << (settings.hilite ? hilite_keyword : "") << "WITH NAME" << (settings.hilite ? hilite_none : "")
|
2017-04-16 05:40:17 +00:00
|
|
|
<< " " << std::quoted(p.with_name, '\'');
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (p.type == ASTAlterQuery::RESHARD_PARTITION)
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "RESHARD ";
|
|
|
|
|
|
|
|
if (p.do_copy)
|
|
|
|
settings.ostr << "COPY ";
|
|
|
|
|
|
|
|
if (p.partition)
|
|
|
|
settings.ostr << "PARTITION ";
|
|
|
|
|
|
|
|
settings.ostr << (settings.hilite ? hilite_none : "");
|
|
|
|
|
|
|
|
if (p.partition)
|
|
|
|
p.partition->formatImpl(settings, state, frame);
|
|
|
|
|
|
|
|
std::string ws = p.partition ? " " : "";
|
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << ws
|
|
|
|
<< "TO " << (settings.hilite ? hilite_none : "");
|
|
|
|
|
|
|
|
FormatStateStacked frame_with_indent = frame;
|
|
|
|
++frame_with_indent.indent;
|
|
|
|
p.weighted_zookeeper_paths->formatImpl(settings, state, frame_with_indent);
|
|
|
|
|
|
|
|
settings.ostr << settings.nl_or_ws;
|
|
|
|
|
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str
|
|
|
|
<< "USING " << (settings.hilite ? hilite_none : "");
|
|
|
|
|
|
|
|
p.sharding_key_expr->formatImpl(settings, state, frame);
|
|
|
|
|
|
|
|
if (p.coordinator)
|
|
|
|
{
|
|
|
|
settings.ostr << settings.nl_or_ws;
|
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str
|
|
|
|
<< "COORDINATE WITH " << (settings.hilite ? hilite_none : "");
|
|
|
|
|
|
|
|
p.coordinator->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;
|
|
|
|
}
|
2016-01-28 01:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|