From 014c7c02bdc454a4b53c44d1525c9932906d07ca Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 8 Sep 2020 23:34:10 +0300 Subject: [PATCH] Fix some trailing whitespaces in query format The following statements still has the trailing whitespace: - WITH - SELECT - SELECT DISTINCT - ARRAY JOIN - GROUP BY - ORDER BY - LIMIT BY --- src/Parsers/ASTExpressionList.cpp | 9 +++++++++ src/Parsers/ASTFunction.cpp | 1 + src/Parsers/ASTSelectQuery.cpp | 12 +++++++----- src/Parsers/ASTTablesInSelectQuery.cpp | 5 ++++- src/Parsers/IAST.h | 1 + 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Parsers/ASTExpressionList.cpp b/src/Parsers/ASTExpressionList.cpp index abab1e895cf..de38e1fd7ea 100644 --- a/src/Parsers/ASTExpressionList.cpp +++ b/src/Parsers/ASTExpressionList.cpp @@ -13,6 +13,9 @@ ASTPtr ASTExpressionList::clone() const void ASTExpressionList::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const { + if (frame.expression_list_prepend_whitespace) + settings.ostr << ' '; + for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it) { if (it != children.begin()) @@ -30,6 +33,12 @@ void ASTExpressionList::formatImplMultiline(const FormatSettings & settings, For { std::string indent_str = "\n" + std::string(4 * (frame.indent + 1), ' '); + if (frame.expression_list_prepend_whitespace) + { + if (!(children.size() > 1 || frame.expression_list_always_start_on_new_line)) + settings.ostr << ' '; + } + ++frame.indent; for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it) { diff --git a/src/Parsers/ASTFunction.cpp b/src/Parsers/ASTFunction.cpp index 07429c8104f..ebef4261d01 100644 --- a/src/Parsers/ASTFunction.cpp +++ b/src/Parsers/ASTFunction.cpp @@ -114,6 +114,7 @@ static bool highlightStringLiteralWithMetacharacters(const ASTPtr & node, const void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const { + frame.expression_list_prepend_whitespace = false; FormatStateStacked nested_need_parens = frame; FormatStateStacked nested_dont_need_parens = frame; nested_need_parens.need_parens = true; diff --git a/src/Parsers/ASTSelectQuery.cpp b/src/Parsers/ASTSelectQuery.cpp index fdc7bd47e4d..499761c4634 100644 --- a/src/Parsers/ASTSelectQuery.cpp +++ b/src/Parsers/ASTSelectQuery.cpp @@ -72,18 +72,20 @@ void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, F { frame.current_select = this; frame.need_parens = false; + frame.expression_list_prepend_whitespace = true; + std::string indent_str = s.one_line ? "" : std::string(4 * frame.indent, ' '); if (with()) { - s.ostr << (s.hilite ? hilite_keyword : "") << indent_str << "WITH " << (s.hilite ? hilite_none : ""); + s.ostr << (s.hilite ? hilite_keyword : "") << indent_str << "WITH" << (s.hilite ? hilite_none : ""); s.one_line ? with()->formatImpl(s, state, frame) : with()->as().formatImplMultiline(s, state, frame); s.ostr << s.nl_or_ws; } - s.ostr << (s.hilite ? hilite_keyword : "") << indent_str << "SELECT " << (distinct ? "DISTINCT " : "") << (s.hilite ? hilite_none : ""); + s.ostr << (s.hilite ? hilite_keyword : "") << indent_str << "SELECT" << (distinct ? " DISTINCT" : "") << (s.hilite ? hilite_none : ""); s.one_line ? select()->formatImpl(s, state, frame) @@ -109,7 +111,7 @@ void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, F if (groupBy()) { - s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "GROUP BY " << (s.hilite ? hilite_none : ""); + s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "GROUP BY" << (s.hilite ? hilite_none : ""); s.one_line ? groupBy()->formatImpl(s, state, frame) : groupBy()->as().formatImplMultiline(s, state, frame); @@ -132,7 +134,7 @@ void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, F if (orderBy()) { - s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "ORDER BY " << (s.hilite ? hilite_none : ""); + s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "ORDER BY" << (s.hilite ? hilite_none : ""); s.one_line ? orderBy()->formatImpl(s, state, frame) : orderBy()->as().formatImplMultiline(s, state, frame); @@ -147,7 +149,7 @@ void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, F s.ostr << ", "; } limitByLength()->formatImpl(s, state, frame); - s.ostr << (s.hilite ? hilite_keyword : "") << " BY " << (s.hilite ? hilite_none : ""); + s.ostr << (s.hilite ? hilite_keyword : "") << " BY" << (s.hilite ? hilite_none : ""); s.one_line ? limitBy()->formatImpl(s, state, frame) : limitBy()->as().formatImplMultiline(s, state, frame); diff --git a/src/Parsers/ASTTablesInSelectQuery.cpp b/src/Parsers/ASTTablesInSelectQuery.cpp index 0fd93bbd04d..eb3446ca1c4 100644 --- a/src/Parsers/ASTTablesInSelectQuery.cpp +++ b/src/Parsers/ASTTablesInSelectQuery.cpp @@ -210,6 +210,7 @@ void ASTTableJoin::formatImplBeforeTable(const FormatSettings & settings, Format void ASTTableJoin::formatImplAfterTable(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const { frame.need_parens = false; + frame.expression_list_prepend_whitespace = false; if (using_expression_list) { @@ -236,8 +237,10 @@ void ASTTableJoin::formatImpl(const FormatSettings & settings, FormatState & sta void ASTArrayJoin::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const { + frame.expression_list_prepend_whitespace = true; + settings.ostr << (settings.hilite ? hilite_keyword : "") - << (kind == Kind::Left ? "LEFT " : "") << "ARRAY JOIN " << (settings.hilite ? hilite_none : ""); + << (kind == Kind::Left ? "LEFT " : "") << "ARRAY JOIN" << (settings.hilite ? hilite_none : ""); settings.one_line ? expression_list->formatImpl(settings, state, frame) diff --git a/src/Parsers/IAST.h b/src/Parsers/IAST.h index c0c286ac0d2..cf6a7efc102 100644 --- a/src/Parsers/IAST.h +++ b/src/Parsers/IAST.h @@ -203,6 +203,7 @@ public: UInt8 indent = 0; 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_prepend_whitespace = false; /// Prepend whitespace (if it is required) const IAST * current_select = nullptr; };