diff --git a/src/common/backend/parser/analyze.cpp b/src/common/backend/parser/analyze.cpp index b81407d2d..77e9df43b 100644 --- a/src/common/backend/parser/analyze.cpp +++ b/src/common/backend/parser/analyze.cpp @@ -4681,7 +4681,7 @@ static void transformGroupConstToColumn(ParseState* pstate, Node* groupClause, L } long target_pos = intVal(val); - if (target_pos <= list_length(targetList)) { + if (target_pos > 0 && target_pos <= list_length(targetList)) { TargetEntry* tle = (TargetEntry*)list_nth(targetList, target_pos - 1); lfirst(lc) = copyObject(tle->expr); diff --git a/src/test/regress/expected/aggregates_part2.out b/src/test/regress/expected/aggregates_part2.out index cf9c6156c..65b24a782 100644 --- a/src/test/regress/expected/aggregates_part2.out +++ b/src/test/regress/expected/aggregates_part2.out @@ -457,6 +457,11 @@ select count(*) from (select distinct(c*d) from t_agg1); 343 (1 row) +-- group by with negative value +SELECT ( 'x' , 'x' ) x GROUP BY -128 , ( ); +ERROR: GROUP BY position -128 is not in select list +LINE 1: SELECT ( 'x' , 'x' ) x GROUP BY -128 , ( ); + ^ reset current_schema; drop schema if exists distribute_aggregates_part2 cascade; NOTICE: drop cascades to 3 other objects diff --git a/src/test/regress/sql/aggregates_part2.sql b/src/test/regress/sql/aggregates_part2.sql index 846d3bb3a..d40843361 100644 --- a/src/test/regress/sql/aggregates_part2.sql +++ b/src/test/regress/sql/aggregates_part2.sql @@ -76,5 +76,8 @@ select count(*) from (select distinct(c) from t_agg1); explain (costs off) select count(*) from (select distinct(c*d) from t_agg1); select count(*) from (select distinct(c*d) from t_agg1); +-- group by with negative value +SELECT ( 'x' , 'x' ) x GROUP BY -128 , ( ); + reset current_schema; drop schema if exists distribute_aggregates_part2 cascade;