From 5f962015caf6e48f3ec5ea115a8f424e88741c8b Mon Sep 17 00:00:00 2001 From: feng lv Date: Sun, 10 Jan 2021 14:51:06 +0000 Subject: [PATCH] fix fix style fix build --- src/Parsers/ExpressionElementParsers.cpp | 45 +++++++++++++++---- src/Parsers/ParserSelectQuery.cpp | 2 +- .../01632_select_all_syntax.reference | 25 +++++++++++ .../0_stateless/01632_select_all_syntax.sql | 14 ++++++ 4 files changed, 77 insertions(+), 9 deletions(-) diff --git a/src/Parsers/ExpressionElementParsers.cpp b/src/Parsers/ExpressionElementParsers.cpp index d55ce3d62b7..501d3329593 100644 --- a/src/Parsers/ExpressionElementParsers.cpp +++ b/src/Parsers/ExpressionElementParsers.cpp @@ -267,7 +267,7 @@ bool ParserFunction::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) ParserKeyword over("OVER"); bool has_all = false; - bool has_distinct_modifier = false; + bool has_distinct = false; ASTPtr identifier; ASTPtr query; @@ -281,19 +281,34 @@ bool ParserFunction::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) return false; ++pos; + auto pos_after_bracket = pos; + auto old_expected = expected; + if (all.ignore(pos, expected)) has_all = true; if (distinct.ignore(pos, expected)) - has_distinct_modifier = true; + has_distinct = true; if (!has_all && all.ignore(pos, expected)) has_all = true; - if (has_all && has_distinct_modifier) + if (has_all && has_distinct) return false; - if (!has_distinct_modifier) + if (has_all || has_distinct) + { + /// case f(ALL), f(ALL, x), f(DISTINCT), f(DISTINCT, x), ALL and DISTINCT should be treat as identifier + if (pos->type == TokenType::Comma || pos->type == TokenType::ClosingRoundBracket) + { + pos = pos_after_bracket; + expected = old_expected; + has_all = false; + has_distinct = false; + } + } + + if (!has_distinct && !has_all) { auto old_pos = pos; auto maybe_an_subquery = pos->type == TokenType::OpeningRoundBracket; @@ -381,24 +396,38 @@ bool ParserFunction::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) ++pos; /// Parametric aggregate functions cannot have DISTINCT in parameters list. - if (has_distinct_modifier) + if (has_distinct) return false; expr_list_params = expr_list_args; expr_list_args = nullptr; + pos_after_bracket = pos; + old_expected = expected; + if (all.ignore(pos, expected)) has_all = true; if (distinct.ignore(pos, expected)) - has_distinct_modifier = true; + has_distinct = true; if (!has_all && all.ignore(pos, expected)) has_all = true; - if (has_all && has_distinct_modifier) + if (has_all && has_distinct) return false; + if (has_all || has_distinct) + { + /// case f(ALL), f(ALL, x), f(DISTINCT), f(DISTINCT, x), ALL and DISTINCT should be treat as identifier + if (pos->type == TokenType::Comma || pos->type == TokenType::ClosingRoundBracket) + { + pos = pos_after_bracket; + expected = old_expected; + has_distinct = false; + } + } + if (!contents.parse(pos, expr_list_args, expected)) return false; @@ -411,7 +440,7 @@ bool ParserFunction::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) tryGetIdentifierNameInto(identifier, function_node->name); /// func(DISTINCT ...) is equivalent to funcDistinct(...) - if (has_distinct_modifier) + if (has_distinct) function_node->name += "Distinct"; function_node->arguments = expr_list_args; diff --git a/src/Parsers/ParserSelectQuery.cpp b/src/Parsers/ParserSelectQuery.cpp index 12fc1619705..f515901edd4 100644 --- a/src/Parsers/ParserSelectQuery.cpp +++ b/src/Parsers/ParserSelectQuery.cpp @@ -92,7 +92,7 @@ bool ParserSelectQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) } } - /// SELECT [DISTINCT] [TOP N [WITH TIES]] expr list + /// SELECT [ALL/DISTINCT] [TOP N [WITH TIES]] expr list { bool has_all = false; if (!s_select.ignore(pos, expected)) diff --git a/tests/queries/0_stateless/01632_select_all_syntax.reference b/tests/queries/0_stateless/01632_select_all_syntax.reference index 3fad8c33238..c836beb205d 100644 --- a/tests/queries/0_stateless/01632_select_all_syntax.reference +++ b/tests/queries/0_stateless/01632_select_all_syntax.reference @@ -8,3 +8,28 @@ a 45 2 1 +1 +1 + +a +aa +aaa +aaaa +aaaaa +aaaaaa +aaaaaaa +aaaaaaaa +aaaaaaaaa + +a +aa +aaa +aaaa +aaaaa +aaaaaa +aaaaaaa +aaaaaaaa +aaaaaaaaa +aaaaa +aaaaa +aaaaa diff --git a/tests/queries/0_stateless/01632_select_all_syntax.sql b/tests/queries/0_stateless/01632_select_all_syntax.sql index 03af91d9e33..f5e96a5cb4e 100644 --- a/tests/queries/0_stateless/01632_select_all_syntax.sql +++ b/tests/queries/0_stateless/01632_select_all_syntax.sql @@ -9,3 +9,17 @@ SELECT sum(DISTINCT number) FROM numbers(10); SELECT sum(ALL x) FROM (SELECT 1 x UNION ALL SELECT 1); SELECT sum(DISTINCT x) FROM (SELECT 1 x UNION ALL SELECT 1); + +SELECT sum(ALL) FROM (SELECT 1 AS ALL); + +SELECT sum(DISTINCT) FROM (SELECT 1 AS DISTINCT); + +SELECT repeat('a', ALL) FROM (SELECT number AS ALL FROM numbers(10)); + +SELECT repeat('a', DISTINCT) FROM (SELECT number AS DISTINCT FROM numbers(10)); + +SELECT repeat(ALL, 5) FROM (SELECT 'a' AS ALL); + +SELECT repeat(DISTINCT, 5) FROM (SELECT 'a' AS DISTINCT); + +SELECT repeat(ALL, DISTINCT) FROM (SELECT 'a' AS ALL, 5 AS DISTINCT);