ClickHouse/src/Parsers/ASTWindowDefinition.cpp

170 lines
4.1 KiB
C++
Raw Normal View History

2021-01-12 18:34:35 +00:00
#include <Parsers/ASTWindowDefinition.h>
2021-01-13 19:29:52 +00:00
#include <Common/quoteString.h>
#include <IO/Operators.h>
2021-05-03 22:46:51 +00:00
2021-01-12 18:34:35 +00:00
namespace DB
{
ASTPtr ASTWindowDefinition::clone() const
{
auto result = std::make_shared<ASTWindowDefinition>();
2021-05-27 14:45:40 +00:00
result->parent_window_name = parent_window_name;
2021-01-12 18:34:35 +00:00
if (partition_by)
{
result->partition_by = partition_by->clone();
result->children.push_back(result->partition_by);
}
if (order_by)
{
result->order_by = order_by->clone();
result->children.push_back(result->order_by);
}
2021-05-29 13:12:18 +00:00
result->frame_is_default = frame_is_default;
result->frame_type = frame_type;
result->frame_begin_type = frame_begin_type;
result->frame_begin_preceding = frame_begin_preceding;
result->frame_end_type = frame_end_type;
result->frame_end_preceding = frame_end_preceding;
2021-01-19 01:00:39 +00:00
if (frame_begin_offset)
{
result->frame_begin_offset = frame_begin_offset->clone();
result->children.push_back(result->frame_begin_offset);
}
if (frame_end_offset)
{
result->frame_end_offset = frame_end_offset->clone();
result->children.push_back(result->frame_end_offset);
}
2021-01-12 18:34:35 +00:00
return result;
}
String ASTWindowDefinition::getID(char) const
{
return "WindowDefinition";
}
2021-01-13 19:29:52 +00:00
void ASTWindowDefinition::formatImpl(const FormatSettings & settings,
2021-01-19 01:00:39 +00:00
FormatState & state, FormatStateStacked format_frame) const
2021-01-13 19:29:52 +00:00
{
2021-03-24 12:36:39 +00:00
format_frame.expression_list_prepend_whitespace = false;
2021-05-27 12:58:50 +00:00
bool need_space = false;
if (!parent_window_name.empty())
{
settings.ostr << backQuoteIfNeed(parent_window_name);
need_space = true;
}
2021-03-24 12:36:39 +00:00
2021-01-13 19:29:52 +00:00
if (partition_by)
{
2021-05-27 12:58:50 +00:00
if (need_space)
{
settings.ostr << " ";
}
2021-03-24 12:36:39 +00:00
settings.ostr << "PARTITION BY ";
2021-01-19 01:00:39 +00:00
partition_by->formatImpl(settings, state, format_frame);
2021-01-13 19:29:52 +00:00
2021-05-27 12:58:50 +00:00
need_space = true;
2021-01-13 19:29:52 +00:00
}
if (order_by)
{
2021-05-27 12:58:50 +00:00
if (need_space)
{
settings.ostr << " ";
}
2021-03-24 12:36:39 +00:00
settings.ostr << "ORDER BY ";
2021-01-19 01:00:39 +00:00
order_by->formatImpl(settings, state, format_frame);
2021-05-27 12:58:50 +00:00
need_space = true;
2021-01-27 00:08:15 +00:00
}
2021-05-29 13:12:18 +00:00
if (!frame_is_default)
2021-01-19 01:00:39 +00:00
{
2021-05-27 12:58:50 +00:00
if (need_space)
{
settings.ostr << " ";
}
settings.ostr << frame_type << " BETWEEN ";
2021-05-29 13:12:18 +00:00
if (frame_begin_type == WindowFrame::BoundaryType::Current)
2021-01-30 01:16:44 +00:00
{
settings.ostr << "CURRENT ROW";
}
2021-05-29 13:12:18 +00:00
else if (frame_begin_type == WindowFrame::BoundaryType::Unbounded)
2021-01-30 01:16:44 +00:00
{
settings.ostr << "UNBOUNDED PRECEDING";
}
else
{
frame_begin_offset->formatImpl(settings, state, format_frame);
2021-01-30 01:16:44 +00:00
settings.ostr << " "
2021-05-29 13:12:18 +00:00
<< (!frame_begin_preceding ? "FOLLOWING" : "PRECEDING");
2021-01-30 01:16:44 +00:00
}
settings.ostr << " AND ";
2021-05-29 13:12:18 +00:00
if (frame_end_type == WindowFrame::BoundaryType::Current)
2021-01-30 01:16:44 +00:00
{
settings.ostr << "CURRENT ROW";
}
2021-05-29 13:12:18 +00:00
else if (frame_end_type == WindowFrame::BoundaryType::Unbounded)
2021-01-30 01:16:44 +00:00
{
2021-03-05 03:15:20 +00:00
settings.ostr << "UNBOUNDED FOLLOWING";
2021-01-30 01:16:44 +00:00
}
else
{
frame_end_offset->formatImpl(settings, state, format_frame);
2021-01-30 01:16:44 +00:00
settings.ostr << " "
2021-05-29 13:12:18 +00:00
<< (!frame_end_preceding ? "FOLLOWING" : "PRECEDING");
2021-01-30 01:16:44 +00:00
}
2021-01-13 19:29:52 +00:00
}
}
std::string ASTWindowDefinition::getDefaultWindowName() const
{
WriteBufferFromOwnString ostr;
FormatSettings settings{ostr, true /* one_line */};
FormatState state;
2021-01-19 01:00:39 +00:00
FormatStateStacked format_frame;
formatImpl(settings, state, format_frame);
2021-01-13 19:29:52 +00:00
return ostr.str();
}
2021-01-12 18:34:35 +00:00
ASTPtr ASTWindowListElement::clone() const
{
auto result = std::make_shared<ASTWindowListElement>();
result->name = name;
result->definition = definition->clone();
result->children.push_back(result->definition);
return result;
}
String ASTWindowListElement::getID(char) const
{
return "WindowListElement";
}
2021-01-13 19:29:52 +00:00
void ASTWindowListElement::formatImpl(const FormatSettings & settings,
FormatState & state, FormatStateStacked frame) const
{
settings.ostr << backQuoteIfNeed(name);
settings.ostr << " AS (";
definition->formatImpl(settings, state, frame);
settings.ostr << ")";
}
2021-01-12 18:34:35 +00:00
}