2019-10-31 10:40:11 +00:00
|
|
|
|
2019-10-09 13:02:05 +00:00
|
|
|
#include <Columns/Collator.h>
|
2019-10-16 07:32:37 +00:00
|
|
|
#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
|
2020-04-27 14:47:59 +00:00
|
|
|
{
|
|
|
|
auto clone = std::make_shared<ASTTTLElement>(*this);
|
|
|
|
clone->children.clear();
|
2020-05-12 14:27:00 +00:00
|
|
|
clone->ttl_expr_pos = -1;
|
|
|
|
clone->where_expr_pos = -1;
|
2020-05-12 20:44:48 +00:00
|
|
|
|
2020-05-12 14:27:00 +00:00
|
|
|
clone->setExpression(clone->ttl_expr_pos, getExpression(ttl_expr_pos, true));
|
|
|
|
clone->setExpression(clone->where_expr_pos, getExpression(where_expr_pos, true));
|
2020-04-27 14:47:59 +00:00
|
|
|
|
2020-05-24 20:21:23 +00:00
|
|
|
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)
|
2020-04-27 14:47:59 +00:00
|
|
|
expr = expr->clone();
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2019-10-09 13:02:05 +00:00
|
|
|
void ASTTTLElement::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
|
|
|
|
{
|
2020-04-27 14:47:59 +00:00
|
|
|
ttl()->formatImpl(settings, state, frame);
|
2020-05-28 15:33:44 +00:00
|
|
|
if (mode == TTLMode::MOVE && destination_type == DataDestinationType::DISK)
|
2019-10-16 07:32:37 +00:00
|
|
|
{
|
|
|
|
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)
|
2019-10-16 07:32:37 +00:00
|
|
|
{
|
|
|
|
settings.ostr << " TO VOLUME " << quoteString(destination_name);
|
|
|
|
}
|
2020-05-12 14:27:00 +00:00
|
|
|
else if (mode == TTLMode::GROUP_BY)
|
2020-04-27 14:47:59 +00:00
|
|
|
{
|
|
|
|
settings.ostr << " GROUP BY ";
|
2020-05-24 20:21:23 +00:00
|
|
|
for (auto it = group_by_key.begin(); it != group_by_key.end(); ++it)
|
2020-04-27 14:47:59 +00:00
|
|
|
{
|
2020-05-24 20:21:23 +00:00
|
|
|
if (it != group_by_key.begin())
|
2020-04-27 14:47:59 +00:00
|
|
|
settings.ostr << ", ";
|
2020-05-24 20:21:23 +00:00
|
|
|
(*it)->formatImpl(settings, state, frame);
|
2020-04-27 14:47:59 +00:00
|
|
|
}
|
2021-01-12 00:40:07 +00:00
|
|
|
|
|
|
|
if (!group_by_assignments.empty())
|
2020-04-27 14:47:59 +00:00
|
|
|
{
|
2020-05-24 20:21:23 +00:00
|
|
|
settings.ostr << " SET ";
|
2021-01-12 00:40:07 +00:00
|
|
|
for (auto it = group_by_assignments.begin(); it != group_by_assignments.end(); ++it)
|
2020-05-24 20:21:23 +00:00
|
|
|
{
|
2021-01-12 00:40:07 +00:00
|
|
|
if (it != group_by_assignments.begin())
|
2020-05-24 20:21:23 +00:00
|
|
|
settings.ostr << ", ";
|
2021-01-12 00:40:07 +00:00
|
|
|
(*it)->formatImpl(settings, state, frame);
|
2020-05-24 20:21:23 +00:00
|
|
|
}
|
2020-04-27 14:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-31 11:35:53 +00:00
|
|
|
else if (mode == TTLMode::RECOMPRESS)
|
|
|
|
{
|
|
|
|
settings.ostr << " RECOMPRESS ";
|
|
|
|
recompression_codec->formatImpl(settings, state, frame);
|
|
|
|
}
|
2020-05-12 14:27:00 +00:00
|
|
|
else if (mode == TTLMode::DELETE)
|
2019-10-16 07:32:37 +00:00
|
|
|
{
|
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-04-27 14:47:59 +00:00
|
|
|
|
2020-05-12 20:44:48 +00:00
|
|
|
if (where())
|
2020-04-27 14:47:59 +00:00
|
|
|
{
|
|
|
|
settings.ostr << " WHERE ";
|
|
|
|
where()->formatImpl(settings, state, frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 20:44:48 +00:00
|
|
|
void ASTTTLElement::setExpression(int & pos, ASTPtr && ast)
|
2020-04-27 14:47:59 +00:00
|
|
|
{
|
2020-05-12 14:27:00 +00:00
|
|
|
if (ast)
|
2020-04-27 14:47:59 +00:00
|
|
|
{
|
2020-05-12 14:27:00 +00:00
|
|
|
if (pos == -1)
|
|
|
|
{
|
|
|
|
pos = children.size();
|
|
|
|
children.emplace_back(ast);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
children[pos] = ast;
|
|
|
|
}
|
|
|
|
else if (pos != -1)
|
|
|
|
{
|
|
|
|
children[pos] = ASTPtr{};
|
|
|
|
pos = -1;
|
2020-04-27 14:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 20:44:48 +00:00
|
|
|
ASTPtr ASTTTLElement::getExpression(int pos, bool clone) const
|
2020-04-27 14:47:59 +00:00
|
|
|
{
|
2020-05-12 14:27:00 +00:00
|
|
|
return pos != -1 ? (clone ? children[pos]->clone() : children[pos]) : ASTPtr{};
|
2019-10-09 13:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|