diff --git a/src/common/backend/parser/parse_clause.cpp b/src/common/backend/parser/parse_clause.cpp index 27bdcaa2f14f1016137bd9db90793bf48bf59fa7..492359ef98af098961285e7dd8fafb2e8b99ad53 100644 --- a/src/common/backend/parser/parse_clause.cpp +++ b/src/common/backend/parser/parse_clause.cpp @@ -2426,7 +2426,9 @@ static void CheckOrderbyColumns(ParseState* pstate, List* targetList, bool isAgg break; } } - } else { + } + + if (!isFound) { Node* refExpr = transformExpr(pstate, (Node*)colRef, EXPR_KIND_ORDER_BY); ListCell* tcell = nullptr; foreach(tcell, targetList) { diff --git a/src/test/regress/expected/distinct.out b/src/test/regress/expected/distinct.out index dd0c454f35ef493e116d684bc736980c613eada0..f9f43ba74f94b85c6d74a10f9022e2e46e2b815b 100644 --- a/src/test/regress/expected/distinct.out +++ b/src/test/regress/expected/distinct.out @@ -1093,5 +1093,39 @@ LINE 1: ...elect a, min(b), group_concat(distinct c order by d) as orde... ^ CONTEXT: referenced column: order_not_in_distinct reset group_concat_max_len; +-- 12. test alias default +create table dob_alias_only( + c1 int, + c2 int +); +insert into dob_alias_only values(1, 2); +insert into dob_alias_only values(2, 1); +insert into dob_alias_only values(2, 1); +reset behavior_compat_options; +select distinct c1 as a1 from dob_alias_only order by c1; + a1 +---- + 1 + 2 +(2 rows) + +select distinct c1 as c2, c2 as c1 from dob_alias_only order by c1; + c2 | c1 +----+---- + 2 | 1 + 1 | 2 +(2 rows) + +select distinct c1 as c2, c2 as c1 from dob_alias_only order by c2; + c2 | c1 +----+---- + 1 | 2 + 2 | 1 +(2 rows) + +select distinct c1 + 1 as a1, c2 + 2 as a2 from dob_alias_only order by c1; -- should error +ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list +LINE 1: ... c1 + 1 as a1, c2 + 2 as a2 from dob_alias_only order by c1; + ^ \c postgres drop database dob_bdb; diff --git a/src/test/regress/sql/distinct.sql b/src/test/regress/sql/distinct.sql index 879c9e3c0bb4601d74f6e4e494b78385752d5a1e..0f17b67d7b6558772e5e05489f7b46d6139b54bb 100644 --- a/src/test/regress/sql/distinct.sql +++ b/src/test/regress/sql/distinct.sql @@ -303,5 +303,20 @@ reset behavior_compat_options; select a, min(b), group_concat(distinct c order by d) as order_not_in_distinct from dob_func_t group by a order by a; reset group_concat_max_len; +-- 12. test alias default +create table dob_alias_only( + c1 int, + c2 int +); +insert into dob_alias_only values(1, 2); +insert into dob_alias_only values(2, 1); +insert into dob_alias_only values(2, 1); + +reset behavior_compat_options; +select distinct c1 as a1 from dob_alias_only order by c1; +select distinct c1 as c2, c2 as c1 from dob_alias_only order by c1; +select distinct c1 as c2, c2 as c1 from dob_alias_only order by c2; +select distinct c1 + 1 as a1, c2 + 2 as a2 from dob_alias_only order by c1; -- should error + \c postgres drop database dob_bdb;