ClickHouse/src/Parsers/ASTTTLElement.cpp

103 lines
2.8 KiB
C++
Raw Normal View History

2019-10-09 13:02:05 +00:00
#include <Columns/Collator.h>
#include <Common/quoteString.h>
2019-10-09 13:02:05 +00:00
#include <Parsers/ASTTTLElement.h>
2020-11-09 16:05:40 +00:00
#include <IO/Operators.h>
2019-10-09 13:02:05 +00:00
namespace DB
{
2020-05-12 20:44:48 +00:00
ASTPtr ASTTTLElement::clone() const
{
auto clone = std::make_shared<ASTTTLElement>(*this);
clone->children.clear();
clone->ttl_expr_pos = -1;
clone->where_expr_pos = -1;
2020-05-12 20:44:48 +00:00
clone->setExpression(clone->ttl_expr_pos, getExpression(ttl_expr_pos, true));
clone->setExpression(clone->where_expr_pos, getExpression(where_expr_pos, true));
for (auto & expr : clone->group_by_key)
expr = expr->clone();
2021-01-12 00:40:07 +00:00
for (auto & expr : clone->group_by_assignments)
expr = expr->clone();
return clone;
}
2019-10-09 13:02:05 +00:00
void ASTTTLElement::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
{
ttl()->formatImpl(settings, state, frame);
2020-05-28 15:33:44 +00:00
if (mode == TTLMode::MOVE && destination_type == DataDestinationType::DISK)
{
settings.ostr << " TO DISK " << quoteString(destination_name);
2019-10-09 13:02:05 +00:00
}
2020-05-28 15:33:44 +00:00
else if (mode == TTLMode::MOVE && destination_type == DataDestinationType::VOLUME)
{
settings.ostr << " TO VOLUME " << quoteString(destination_name);
}
else if (mode == TTLMode::GROUP_BY)
{
settings.ostr << " GROUP BY ";
for (auto it = group_by_key.begin(); it != group_by_key.end(); ++it)
{
if (it != group_by_key.begin())
settings.ostr << ", ";
(*it)->formatImpl(settings, state, frame);
}
2021-01-12 00:40:07 +00:00
if (!group_by_assignments.empty())
{
settings.ostr << " SET ";
2021-01-12 00:40:07 +00:00
for (auto it = group_by_assignments.begin(); it != group_by_assignments.end(); ++it)
{
2021-01-12 00:40:07 +00:00
if (it != group_by_assignments.begin())
settings.ostr << ", ";
2021-01-12 00:40:07 +00:00
(*it)->formatImpl(settings, state, frame);
}
}
}
2020-08-31 11:35:53 +00:00
else if (mode == TTLMode::RECOMPRESS)
{
settings.ostr << " RECOMPRESS ";
recompression_codec->formatImpl(settings, state, frame);
}
else if (mode == TTLMode::DELETE)
{
2019-11-28 06:44:26 +00:00
/// It would be better to output "DELETE" here but that will break compatibility with earlier versions.
2019-10-09 13:02:05 +00:00
}
2020-05-12 20:44:48 +00:00
if (where())
{
settings.ostr << " WHERE ";
where()->formatImpl(settings, state, frame);
}
}
2020-05-12 20:44:48 +00:00
void ASTTTLElement::setExpression(int & pos, ASTPtr && ast)
{
if (ast)
{
if (pos == -1)
{
pos = children.size();
children.emplace_back(ast);
}
else
children[pos] = ast;
}
else if (pos != -1)
{
children[pos] = ASTPtr{};
pos = -1;
}
}
2020-05-12 20:44:48 +00:00
ASTPtr ASTTTLElement::getExpression(int pos, bool clone) const
{
return pos != -1 ? (clone ? children[pos]->clone() : children[pos]) : ASTPtr{};
2019-10-09 13:02:05 +00:00
}
}