fix GROUPING SETS formating

This commit is contained in:
Dmitry Novik 2021-12-07 16:44:09 +03:00
parent 3bcf915541
commit f138122259
3 changed files with 33 additions and 69 deletions

View File

@ -17,9 +17,6 @@ void ASTExpressionList::formatImpl(const FormatSettings & settings, FormatState
if (frame.expression_list_prepend_whitespace) if (frame.expression_list_prepend_whitespace)
settings.ostr << ' '; settings.ostr << ' ';
if (frame.need_parens)
{
settings.ostr << "(";
for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it) for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it)
{ {
if (it != children.begin()) if (it != children.begin())
@ -29,27 +26,16 @@ void ASTExpressionList::formatImpl(const FormatSettings & settings, FormatState
settings.ostr << ' '; settings.ostr << ' ';
} }
if (frame.surround_each_list_element_with_parens)
settings.ostr << "("; settings.ostr << "(";
FormatStateStacked frame_nested = frame; FormatStateStacked frame_nested = frame;
frame_nested.need_parens = false; frame_nested.surround_each_list_element_with_parens = false;
(*it)->formatImpl(settings, state, frame_nested); (*it)->formatImpl(settings, state, frame_nested);
if (frame.surround_each_list_element_with_parens)
settings.ostr << ")"; settings.ostr << ")";
} }
settings.ostr << ")";
}
else
{
for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it)
{
if (it != children.begin())
{
if (separator)
settings.ostr << separator;
settings.ostr << ' ';
}
(*it)->formatImpl(settings, state, frame);
}
}
} }
void ASTExpressionList::formatImplMultiline(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const void ASTExpressionList::formatImplMultiline(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
@ -64,36 +50,6 @@ void ASTExpressionList::formatImplMultiline(const FormatSettings & settings, For
++frame.indent; ++frame.indent;
if (frame.need_parens)
{
for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it)
{
if (it != children.begin())
{
if (separator)
settings.ostr << separator;
}
if (children.size() > 1 || frame.expression_list_always_start_on_new_line)
settings.ostr << indent_str;
if (it == children.begin())
{
settings.ostr << "(";
}
FormatStateStacked frame_nested = frame;
frame_nested.expression_list_always_start_on_new_line = false;
frame_nested.expression_list_prepend_whitespace = false;
frame_nested.need_parens = false;
settings.ostr << "(";
(*it)->formatImpl(settings, state, frame_nested);
settings.ostr << ")";
}
settings.ostr << ")";
}
else
{
for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it) for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it)
{ {
if (it != children.begin()) if (it != children.begin())
@ -107,9 +63,15 @@ void ASTExpressionList::formatImplMultiline(const FormatSettings & settings, For
FormatStateStacked frame_nested = frame; FormatStateStacked frame_nested = frame;
frame_nested.expression_list_always_start_on_new_line = false; frame_nested.expression_list_always_start_on_new_line = false;
frame_nested.surround_each_list_element_with_parens = false;
if (frame.surround_each_list_element_with_parens)
settings.ostr << "(";
(*it)->formatImpl(settings, state, frame_nested); (*it)->formatImpl(settings, state, frame_nested);
}
if (frame.surround_each_list_element_with_parens)
settings.ostr << ")";
} }
} }

View File

@ -130,13 +130,14 @@ void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, F
if (group_by_with_grouping_sets) if (group_by_with_grouping_sets)
{ {
bool tmp_need_parens = frame.need_parens; frame.surround_each_list_element_with_parens = true;
frame.need_parens = true;
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << (s.one_line ? "" : " ") << "GROUPING SETS" << (s.hilite ? hilite_none : ""); s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << (s.one_line ? "" : " ") << "GROUPING SETS" << (s.hilite ? hilite_none : "");
s.ostr << " (";
s.one_line s.one_line
? groupBy()->formatImpl(s, state, frame) ? groupBy()->formatImpl(s, state, frame)
: groupBy()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame); : groupBy()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame);
frame.need_parens = tmp_need_parens; s.ostr << ")";
frame.surround_each_list_element_with_parens = false;
} }
if (group_by_with_totals) if (group_by_with_totals)

View File

@ -208,6 +208,7 @@ public:
bool need_parens = false; bool need_parens = false;
bool expression_list_always_start_on_new_line = false; /// Line feed and indent before expression list even if it's of single element. bool expression_list_always_start_on_new_line = false; /// Line feed and indent before expression list even if it's of single element.
bool expression_list_prepend_whitespace = false; /// Prepend whitespace (if it is required) bool expression_list_prepend_whitespace = false; /// Prepend whitespace (if it is required)
bool surround_each_list_element_with_parens = false;
const IAST * current_select = nullptr; const IAST * current_select = nullptr;
}; };