diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 3290d918a8b..9ec6d9e358c 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -2156,19 +2156,31 @@ void QueryAnalyzer::replaceNodesWithPositionalArguments(QueryTreeNodePtr & node_ node_to_replace = &sort_node->getExpression(); auto * constant_node = (*node_to_replace)->as(); - if (!constant_node || constant_node->getValue().getType() != Field::Types::UInt64) + + if (!constant_node + || (constant_node->getValue().getType() != Field::Types::UInt64 && constant_node->getValue().getType() != Field::Types::Int64)) continue; - UInt64 positional_argument_number = constant_node->getValue().get(); - if (positional_argument_number == 0 || positional_argument_number > projection_nodes.size()) - throw Exception(ErrorCodes::BAD_ARGUMENTS, + UInt64 pos; + if (constant_node->getValue().getType() == Field::Types::UInt64) + { + pos = constant_node->getValue().get(); + } + else // Int64 + { + auto value = constant_node->getValue().get(); + pos = value > 0 ? value : projection_nodes.size() + value + 1; + } + + if (!pos || pos > projection_nodes.size()) + throw Exception( + ErrorCodes::BAD_ARGUMENTS, "Positional argument number {} is out of bounds. Expected in range [1, {}]. In scope {}", - positional_argument_number, + pos, projection_nodes.size(), scope.scope_node->formatASTForErrorMessage()); - --positional_argument_number; - *node_to_replace = projection_nodes[positional_argument_number]; + *node_to_replace = projection_nodes[--pos]; } } diff --git a/tests/queries/0_stateless/02943_positional_arguments_bugs.reference b/tests/queries/0_stateless/02943_positional_arguments_bugs.reference index 47e8df9e382..08310b7cf27 100644 --- a/tests/queries/0_stateless/02943_positional_arguments_bugs.reference +++ b/tests/queries/0_stateless/02943_positional_arguments_bugs.reference @@ -1,11 +1,12 @@ 0 0 -4 4 -3 3 -2 2 -5 5 1 1 +2 2 +3 3 +4 4 +5 5 6 6 7 7 -9 9 8 8 +9 9 +45 1 processed 99 0 diff --git a/tests/queries/0_stateless/02943_positional_arguments_bugs.sql b/tests/queries/0_stateless/02943_positional_arguments_bugs.sql index 8cc3fb4b17d..9b1b872ae40 100644 --- a/tests/queries/0_stateless/02943_positional_arguments_bugs.sql +++ b/tests/queries/0_stateless/02943_positional_arguments_bugs.sql @@ -3,7 +3,7 @@ DROP TABLE IF EXISTS t; CREATE TABLE t ( `n` int, - `__unused_group_by_column` int + `__unused_group_by_column` int ) ENGINE = MergeTree ORDER BY n AS @@ -14,7 +14,9 @@ SELECT sum(n), __unused_group_by_column FROM t -GROUP BY __unused_group_by_column; +GROUP BY __unused_group_by_column ORDER BY __unused_group_by_column; + +SELECT sum(n), 1 as x from t group by x; SELECT 'processed' AS type,