From 038bdcf9b90c31ca88b74e31589826bc10233626 Mon Sep 17 00:00:00 2001 From: kssenii Date: Wed, 24 Nov 2021 23:54:49 +0300 Subject: [PATCH] Fix --- src/Interpreters/ExpressionAnalyzer.cpp | 22 ++++++++++++++----- .../02006_test_positional_arguments.reference | 8 +++++-- .../02006_test_positional_arguments.sql | 5 ++++- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/Interpreters/ExpressionAnalyzer.cpp b/src/Interpreters/ExpressionAnalyzer.cpp index d40a92972b2..fb796eea8fd 100644 --- a/src/Interpreters/ExpressionAnalyzer.cpp +++ b/src/Interpreters/ExpressionAnalyzer.cpp @@ -100,6 +100,21 @@ bool checkPositionalArguments(ASTPtr & argument, const ASTSelectQuery * select_q { auto columns = select_query->select()->children; + const auto * group_by_expr_with_alias = dynamic_cast(argument.get()); + if (group_by_expr_with_alias && !group_by_expr_with_alias->alias.empty()) + { + for (const auto & column : columns) + { + const auto * col_with_alias = dynamic_cast(column.get()); + if (col_with_alias) + { + const auto & alias = col_with_alias->alias; + if (!alias.empty() && alias == group_by_expr_with_alias->alias) + return false; + } + } + } + /// In case of expression/function (order by 1+2 and 2*x1, greatest(1, 2)) replace /// positions only if all literals are numbers, otherwise it is not positional. bool positional = true; @@ -155,12 +170,7 @@ bool checkPositionalArguments(ASTPtr & argument, const ASTSelectQuery * select_q } } else if (pos > columns.size() || !pos) - { - throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "Positional argument out of bounds: {} (exprected in range [1, {}]", - pos, columns.size()); - } - /// Do not throw if pos < 0, because of TreeOptimizer::appendUnusedColumn() + positional = false; } else positional = false; diff --git a/tests/queries/0_stateless/02006_test_positional_arguments.reference b/tests/queries/0_stateless/02006_test_positional_arguments.reference index bf2ea3333e4..7b75ab43430 100644 --- a/tests/queries/0_stateless/02006_test_positional_arguments.reference +++ b/tests/queries/0_stateless/02006_test_positional_arguments.reference @@ -145,6 +145,10 @@ select x1, x1 * 2, max(x2), max(x3) from test2 group by 2, 1, x1 order by 1, 2, 1 2 10 100 10 20 1 10 100 200 100 1 -select a, b, c, d, e, f from (select 44 a, 88 b, 13 c, 14 d, 15 e, 16 f) t group by 1,2,3,4,5,6 - +select a, b, c, d, e, f from (select 44 a, 88 b, 13 c, 14 d, 15 e, 16 f) t group by 1,2,3,4,5,6; 44 88 13 14 15 16 +explain syntax select plus(1, 1) as a group by a; +SELECT 1 + 1 AS a +GROUP BY a +select substr('aaaaaaaaaaaaaa', 8) as a group by a; +aaaaaaa diff --git a/tests/queries/0_stateless/02006_test_positional_arguments.sql b/tests/queries/0_stateless/02006_test_positional_arguments.sql index 6bbdc9ceaff..3ba01b47efa 100644 --- a/tests/queries/0_stateless/02006_test_positional_arguments.sql +++ b/tests/queries/0_stateless/02006_test_positional_arguments.sql @@ -48,4 +48,7 @@ create table test2(x1 Int, x2 Int, x3 Int) engine=Memory; insert into test2 values (1, 10, 100), (10, 1, 10), (100, 100, 1); select x1, x1 * 2, max(x2), max(x3) from test2 group by 2, 1, x1 order by 1, 2, 4 desc, 3 asc; -select a, b, c, d, e, f from (select 44 a, 88 b, 13 c, 14 d, 15 e, 16 f) t group by 1,2,3,4,5,6 +select a, b, c, d, e, f from (select 44 a, 88 b, 13 c, 14 d, 15 e, 16 f) t group by 1,2,3,4,5,6; + +explain syntax select plus(1, 1) as a group by a; +select substr('aaaaaaaaaaaaaa', 8) as a group by a;