From 5a21a53d982929dce367f78ad3a6e828ed43ceab Mon Sep 17 00:00:00 2001 From: Rushabh Lathia Date: Thu, 19 Nov 2026 14:07:27 +0530 Subject: [PATCH v2] Add ORDER BY ALL ORDER BY ALL is a form of ORDER BY that sorts by all non-junk columns of the SELECT list, making it exactly equivalent to listing those columns explicitly. The sort direction and NULLS ordering may be given once and apply to every column: ORDER BY ALL [ ASC | DESC ] [ NULLS { FIRST | LAST } ] --- doc/src/sgml/queries.sgml | 19 + doc/src/sgml/ref/select.sgml | 19 +- doc/src/sgml/ref/select_into.sgml | 2 +- src/backend/nodes/nodeFuncs.c | 10 +- src/backend/parser/analyze.c | 3 + src/backend/parser/gram.y | 16 + src/backend/parser/parse_agg.c | 1 + src/backend/parser/parse_clause.c | 84 +++++ src/backend/parser/parse_expr.c | 7 + src/include/parser/parse_clause.h | 2 + src/pl/plpgsql/src/expected/plpgsql_misc.out | 28 ++ src/pl/plpgsql/src/sql/plpgsql_misc.sql | 23 ++ src/test/regress/expected/aggregates.out | 11 + src/test/regress/expected/create_view.out | 55 +++ src/test/regress/expected/select.out | 376 +++++++++++++++++++ src/test/regress/expected/union.out | 93 +++++ src/test/regress/expected/window.out | 19 + src/test/regress/expected/with.out | 6 + src/test/regress/sql/aggregates.sql | 6 + src/test/regress/sql/create_view.sql | 19 + src/test/regress/sql/select.sql | 80 ++++ src/test/regress/sql/union.sql | 29 ++ src/test/regress/sql/window.sql | 7 + src/test/regress/sql/with.sql | 4 + 24 files changed, 914 insertions(+), 5 deletions(-) diff --git a/doc/src/sgml/queries.sgml b/doc/src/sgml/queries.sgml index eadec641674..37067b07b82 100644 --- a/doc/src/sgml/queries.sgml +++ b/doc/src/sgml/queries.sgml @@ -1888,6 +1888,25 @@ SELECT a + b AS sum, c FROM table1 ORDER BY sum + c; -- wrong column to match some other table column's name. + + PostgreSQL also supports the syntax + ORDER BY ALL, which is equivalent to explicitly + writing all select-list columns. The sort direction (ascending or + descending) and null ordering can optionally be specified; these apply + to all columns. For example: + +SELECT a, b, c FROM table1 ORDER BY ALL; +SELECT a, b, c FROM table1 ORDER BY ALL DESC; +SELECT a, b, c FROM table1 ORDER BY ALL DESC NULLS LAST; + + The first example is equivalent to ORDER BY a, b, c. + The second is equivalent to ORDER BY a DESC, b DESC, c DESC. + ORDER BY ALL cannot be combined with other sort + expressions, and it is not allowed inside the ORDER BY + of an aggregate or window function. + This syntax can simplify ad-hoc exploration of data. + + ORDER BY can be applied to the result of a UNION, INTERSECT, or EXCEPT diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml index 18392f7caec..96be66f45fb 100644 --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -41,7 +41,7 @@ SELECT [ ALL | DISTINCT [ ON ( expressioncondition ] [ WINDOW window_name AS ( window_definition ) [, ...] ] [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ] - [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ] + [ ORDER BY { ALL [ ASC | DESC ] [ NULLS { FIRST | LAST } ] | expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] } ] [ LIMIT { count | ALL } ] [ OFFSET start [ ROW | ROWS ] ] [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } { ONLY | WITH TIES } ] @@ -1387,7 +1387,7 @@ SELECT DISTINCT ON (location) location, time, report The optional ORDER BY clause has this general form: -ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] +ORDER BY { ALL [ ASC | DESC ] [ NULLS { FIRST | LAST } ] | expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] } The ORDER BY clause causes the result rows to be sorted according to the specified expression(s). If two rows are @@ -1397,6 +1397,21 @@ ORDER BY expression [ ASC | DESC | an implementation-dependent order. + + The form ORDER BY ALL with no explicit + expression provided is + equivalent to writing ORDER BY with all columns of + the SELECT output list. The sort direction + (ascending or descending) and null ordering can optionally be specified + and apply to all columns. For example, ORDER BY ALL DESC + is equivalent to listing all output columns with DESC + after each. ORDER BY ALL cannot be combined with + other sort expressions or with USING, and it is allowed + only in a query's own ORDER BY clause, not in the + ORDER BY of an aggregate function call, a window + definition, or a WITHIN GROUP clause. + + Each expression can be the name or ordinal number of an output column diff --git a/doc/src/sgml/ref/select_into.sgml b/doc/src/sgml/ref/select_into.sgml index 550ba69d5d1..eddcb5cd016 100644 --- a/doc/src/sgml/ref/select_into.sgml +++ b/doc/src/sgml/ref/select_into.sgml @@ -31,7 +31,7 @@ SELECT [ ALL | DISTINCT [ ON ( expressioncondition ] [ WINDOW window_name AS ( window_definition ) [, ...] ] [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ] - [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ] + [ ORDER BY { ALL [ ASC | DESC ] [ NULLS { FIRST | LAST } ] | expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] } ] [ LIMIT { count | ALL } ] [ OFFSET start [ ROW | ROWS ] ] [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } { ONLY | WITH TIES } ] diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index 678b9a046ce..ad46cddf9d5 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -1722,8 +1722,14 @@ exprLocation(const Node *expr) loc = exprLocation(((const CollateClause *) expr)->arg); break; case T_SortBy: - /* just use argument's location (ignore operator, if any) */ - loc = exprLocation(((const SortBy *) expr)->node); + /* + * Just use argument's location (ignore operator, if any). The + * ORDER BY ALL marker has no argument, so use its own location. + */ + if (((const SortBy *) expr)->node != NULL) + loc = exprLocation(((const SortBy *) expr)->node); + else + loc = ((const SortBy *) expr)->location; break; case T_WindowDef: loc = ((const WindowDef *) expr)->location; diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index c980d65c743..18abcbdd689 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -1490,6 +1490,7 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt, */ qry->sortClause = transformSortClause(pstate, stmt->sortClause, + sortClauseIsAll(stmt->sortClause), &qry->targetList, EXPR_KIND_ORDER_BY, false /* allow SQL92 rules */ ); @@ -1753,6 +1754,7 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt) */ qry->sortClause = transformSortClause(pstate, stmt->sortClause, + sortClauseIsAll(stmt->sortClause), &qry->targetList, EXPR_KIND_ORDER_BY, false /* allow SQL92 rules */ ); @@ -1996,6 +1998,7 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt) qry->sortClause = transformSortClause(pstate, sortClause, + sortClauseIsAll(sortClause), &qry->targetList, EXPR_KIND_ORDER_BY, false /* allow SQL92 rules */ ); diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 0563453fe24..9a09d460d7c 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -13506,6 +13506,21 @@ opt_sort_clause: sort_clause: ORDER BY sortby_list { $$ = $3; } + | ORDER BY ALL opt_asc_desc opt_nulls_order + { + /* + * Create a special marker SortBy node for ORDER BY ALL. + * We use a SortBy with node=NULL as the marker, and store + * the sort direction and nulls ordering to apply to all columns. + */ + SortBy *sortby = makeNode(SortBy); + sortby->node = NULL; /* NULL node indicates ORDER BY ALL */ + sortby->sortby_dir = $4; + sortby->sortby_nulls = $5; + sortby->useOp = NIL; + sortby->location = @2; + $$ = list_make1(sortby); + } ; sortby_list: @@ -19437,6 +19452,7 @@ insertSelectOptions(SelectStmt *stmt, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("multiple ORDER BY clauses not allowed"), parser_errposition(exprLocation((Node *) sortClause)))); + stmt->sortClause = sortClause; } /* We can handle multiple locking clauses, though */ diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index a5424c9d24c..f4bc2bee46f 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -191,6 +191,7 @@ transformAggregateCall(ParseState *pstate, Aggref *agg, torder = transformSortClause(pstate, aggorder, + false, &tlist, EXPR_KIND_ORDER_BY, true /* force SQL99 rules */ ); diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index 7a5179b9053..e062afe2ed6 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -2719,6 +2719,22 @@ transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets, return result; } +/* + * sortClauseIsAll + * Is this raw sort clause the ORDER BY ALL marker? + * + * The grammar represents ORDER BY ALL as a single SortBy with a NULL node, + * which carries the sort direction and NULLS ordering to apply to every + * output column. + */ +bool +sortClauseIsAll(List *sortClause) +{ + return list_length(sortClause) == 1 && + IsA(linitial(sortClause), SortBy) && + ((SortBy *) linitial(sortClause))->node == NULL; +} + /* * transformSortClause - * transform an ORDER BY clause @@ -2732,6 +2748,7 @@ transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets, List * transformSortClause(ParseState *pstate, List *orderlist, + bool orderByAll, List **targetlist, ParseExprKind exprKind, bool useSQL99) @@ -2739,11 +2756,77 @@ transformSortClause(ParseState *pstate, List *sortlist = NIL; ListCell *olitem; + /* + * Handle ORDER BY ALL expansion. + * + * The target list has already been transformed and contains the + * columns we should sort by. We create a SortBy node for each + * non-junk target entry, applying the same sort direction and + * NULLS ordering specified in the marker to all columns. + */ + if (orderByAll) + { + SortBy *sortby; + SortBy *all_marker; + SortByDir sort_dir; + SortByNulls nulls_order; + + /* + * orderlist should contain a single SortBy marker with node=NULL + * that specifies the sort direction and nulls ordering to apply + * to all columns. + */ + Assert(list_length(orderlist) == 1); + all_marker = (SortBy *) linitial(orderlist); + Assert(IsA(all_marker, SortBy) && all_marker->node == NULL); + + /* Extract sort direction and nulls ordering from the marker */ + sort_dir = all_marker->sortby_dir; + nulls_order = all_marker->sortby_nulls; + + /* Iterate over targets, adding all non-junk columns to sort clause */ + foreach_ptr(TargetEntry, tle, *targetlist) + { + /* + * Ignore junk TLEs. These are system columns (like ctid, xmin) + * or internal working columns that shouldn't be part of the + * user-visible ordering. + */ + if (tle->resjunk) + continue; + + /* Create a SortBy for this target entry with specified direction */ + sortby = makeNode(SortBy); + + sortby->node = (Node *) tle->expr; + sortby->sortby_dir = sort_dir; + sortby->sortby_nulls = nulls_order; + sortby->useOp = NIL; + sortby->location = exprLocation((Node *) tle->expr); + + sortlist = addTargetToSortList(pstate, tle, + sortlist, *targetlist, sortby); + } + + return sortlist; + } + foreach(olitem, orderlist) { SortBy *sortby = (SortBy *) lfirst(olitem); TargetEntry *tle; + /* + * A SortBy with a NULL node is the ORDER BY ALL marker. It is + * expanded above, and only for a query's own ORDER BY. Anywhere else + * (aggregate or window ORDER BY, WITHIN GROUP) it is an error. + */ + if (sortby->node == NULL) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("ORDER BY ALL is only allowed in a query's ORDER BY clause"), + parser_errposition(pstate, sortby->location))); + if (useSQL99) tle = findTargetlistEntrySQL99(pstate, sortby->node, targetlist, exprKind); @@ -2814,6 +2897,7 @@ transformWindowDefinitions(ParseState *pstate, */ orderClause = transformSortClause(pstate, windef->orderClause, + false, targetlist, EXPR_KIND_WINDOW_ORDER, true /* force SQL99 rules */ ); diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 728392f436f..bc05da12584 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -1476,6 +1476,13 @@ transformFuncCall(ParseState *pstate, FuncCall *fn) { SortBy *arg = (SortBy *) lfirst(args); + /* ORDER BY ALL marker (NULL node) is not valid here */ + if (arg->node == NULL) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("ORDER BY ALL is only allowed in a query's ORDER BY clause"), + parser_errposition(pstate, arg->location))); + targs = lappend(targs, transformExpr(pstate, arg->node, EXPR_KIND_ORDER_BY)); } diff --git a/src/include/parser/parse_clause.h b/src/include/parser/parse_clause.h index ca815a9d1bb..c95cecae7e8 100644 --- a/src/include/parser/parse_clause.h +++ b/src/include/parser/parse_clause.h @@ -29,7 +29,9 @@ extern List *transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99); +extern bool sortClauseIsAll(List *sortClause); extern List *transformSortClause(ParseState *pstate, List *orderlist, + bool orderByAll, List **targetlist, ParseExprKind exprKind, bool useSQL99); diff --git a/src/pl/plpgsql/src/expected/plpgsql_misc.out b/src/pl/plpgsql/src/expected/plpgsql_misc.out index b3def302821..17d997ed297 100644 --- a/src/pl/plpgsql/src/expected/plpgsql_misc.out +++ b/src/pl/plpgsql/src/expected/plpgsql_misc.out @@ -110,3 +110,31 @@ do $$ declare x int; begin x := 1 $$; ERROR: missing ";" at end of SQL statement LINE 1: do $$ declare x int; begin x := 1 $$; ^ +-- ORDER BY ALL in PL/pgSQL queries (these go through the PLpgSQL_Expr grammar) +create temp table plpgsql_order_all (a int, b text); +insert into plpgsql_order_all values (3, 'foo'), (1, 'bar'), (2, 'baz'), (1, 'qux'); +do $$ +declare + r record; + cur cursor for select * from plpgsql_order_all order by all desc; +begin + for r in select * from plpgsql_order_all order by all loop + raise notice 'for: a=%, b=%', r.a, r.b; + end loop; + open cur; + loop + fetch cur into r; + exit when not found; + raise notice 'cursor: a=%, b=%', r.a, r.b; + end loop; + close cur; +end $$; +NOTICE: for: a=1, b=bar +NOTICE: for: a=1, b=qux +NOTICE: for: a=2, b=baz +NOTICE: for: a=3, b=foo +NOTICE: cursor: a=3, b=foo +NOTICE: cursor: a=2, b=baz +NOTICE: cursor: a=1, b=qux +NOTICE: cursor: a=1, b=bar +drop table plpgsql_order_all; diff --git a/src/pl/plpgsql/src/sql/plpgsql_misc.sql b/src/pl/plpgsql/src/sql/plpgsql_misc.sql index e2c3278a8d3..c5fd208099e 100644 --- a/src/pl/plpgsql/src/sql/plpgsql_misc.sql +++ b/src/pl/plpgsql/src/sql/plpgsql_misc.sql @@ -70,3 +70,26 @@ end $$; -- unterminated constructs should complain about the missing terminator do $$ begin if true $$; do $$ declare x int; begin x := 1 $$; + +-- ORDER BY ALL in PL/pgSQL queries (these go through the PLpgSQL_Expr grammar) +create temp table plpgsql_order_all (a int, b text); +insert into plpgsql_order_all values (3, 'foo'), (1, 'bar'), (2, 'baz'), (1, 'qux'); + +do $$ +declare + r record; + cur cursor for select * from plpgsql_order_all order by all desc; +begin + for r in select * from plpgsql_order_all order by all loop + raise notice 'for: a=%, b=%', r.a, r.b; + end loop; + open cur; + loop + fetch cur into r; + exit when not found; + raise notice 'cursor: a=%, b=%', r.a, r.b; + end loop; + close cur; +end $$; + +drop table plpgsql_order_all; diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out index ce555a31693..59cdb236279 100644 --- a/src/test/regress/expected/aggregates.out +++ b/src/test/regress/expected/aggregates.out @@ -3986,3 +3986,14 @@ drop table agg_hash_1; drop table agg_hash_2; drop table agg_hash_3; drop table agg_hash_4; +-- +-- ORDER BY ALL is not allowed inside aggregate calls +-- +select array_agg(unique1 order by all) from tenk1; +ERROR: ORDER BY ALL is only allowed in a query's ORDER BY clause +LINE 1: select array_agg(unique1 order by all) from tenk1; + ^ +select percentile_disc(0.5) within group (order by all) from tenk1; +ERROR: ORDER BY ALL is only allowed in a query's ORDER BY clause +LINE 1: select percentile_disc(0.5) within group (order by all) from... + ^ diff --git a/src/test/regress/expected/create_view.out b/src/test/regress/expected/create_view.out index c2892dfb6d0..34d66174262 100644 --- a/src/test/regress/expected/create_view.out +++ b/src/test/regress/expected/create_view.out @@ -2425,3 +2425,58 @@ drop cascades to operator ||(xml,xml) drop cascades to view tt261v drop cascades to table tt27v_tbl drop cascades to view tt27v +-- +-- Deparsing of ORDER BY ALL in view definitions +-- +create temp table order_all_vtbl (a int, b text); +create view order_all_v1 as select * from order_all_vtbl order by all; +NOTICE: view "order_all_v1" will be a temporary view +DETAIL: It depends on temporary table order_all_vtbl. +create view order_all_v2 as select * from order_all_vtbl order by all desc; +NOTICE: view "order_all_v2" will be a temporary view +DETAIL: It depends on temporary table order_all_vtbl. +create view order_all_v3 as select * from order_all_vtbl order by all nulls first; +NOTICE: view "order_all_v3" will be a temporary view +DETAIL: It depends on temporary table order_all_vtbl. +create view order_all_v4 as select * from order_all_vtbl order by all asc nulls last; +NOTICE: view "order_all_v4" will be a temporary view +DETAIL: It depends on temporary table order_all_vtbl. +create view order_all_v5 as select * from order_all_vtbl order by all desc nulls first; +NOTICE: view "order_all_v5" will be a temporary view +DETAIL: It depends on temporary table order_all_vtbl. +create view order_all_v6 as select * from order_all_vtbl order by all desc nulls last; +NOTICE: view "order_all_v6" will be a temporary view +DETAIL: It depends on temporary table order_all_vtbl. +select viewname, definition from pg_views + where viewname like 'order\_all\_v%' order by viewname; + viewname | definition +--------------+-------------------------------------------------- + order_all_v1 | SELECT a, + + | b + + | FROM order_all_vtbl + + | ORDER BY a, b; + order_all_v2 | SELECT a, + + | b + + | FROM order_all_vtbl + + | ORDER BY a DESC, b DESC; + order_all_v3 | SELECT a, + + | b + + | FROM order_all_vtbl + + | ORDER BY a NULLS FIRST, b NULLS FIRST; + order_all_v4 | SELECT a, + + | b + + | FROM order_all_vtbl + + | ORDER BY a, b; + order_all_v5 | SELECT a, + + | b + + | FROM order_all_vtbl + + | ORDER BY a DESC, b DESC; + order_all_v6 | SELECT a, + + | b + + | FROM order_all_vtbl + + | ORDER BY a DESC NULLS LAST, b DESC NULLS LAST; +(6 rows) + +drop view order_all_v1, order_all_v2, order_all_v3, + order_all_v4, order_all_v5, order_all_v6; +drop table order_all_vtbl; diff --git a/src/test/regress/expected/select.out b/src/test/regress/expected/select.out index 34f040beecc..d1dc8789830 100644 --- a/src/test/regress/expected/select.out +++ b/src/test/regress/expected/select.out @@ -983,3 +983,379 @@ explain (costs off) select * from list_parted_tbl; (3 rows) drop table list_parted_tbl; +-- +-- ORDER BY ALL +-- +create temp table order_all_tbl (a int, b text, c float8); +insert into order_all_tbl values + (3, 'foo', 1.5), (1, 'bar', null), (2, 'baz', 1.5), (1, 'qux', 1.5), + (null, 'nul', 3.0), (2, null, 2.0), (3, 'foo', null); +-- direction and NULLS variants +select * from order_all_tbl order by all; + a | b | c +---+-----+----- + 1 | bar | + 1 | qux | 1.5 + 2 | baz | 1.5 + 2 | | 2 + 3 | foo | 1.5 + 3 | foo | + | nul | 3 +(7 rows) + +select * from order_all_tbl order by all asc; + a | b | c +---+-----+----- + 1 | bar | + 1 | qux | 1.5 + 2 | baz | 1.5 + 2 | | 2 + 3 | foo | 1.5 + 3 | foo | + | nul | 3 +(7 rows) + +select * from order_all_tbl order by all desc; + a | b | c +---+-----+----- + | nul | 3 + 3 | foo | + 3 | foo | 1.5 + 2 | | 2 + 2 | baz | 1.5 + 1 | qux | 1.5 + 1 | bar | +(7 rows) + +select * from order_all_tbl order by all nulls first; + a | b | c +---+-----+----- + | nul | 3 + 1 | bar | + 1 | qux | 1.5 + 2 | | 2 + 2 | baz | 1.5 + 3 | foo | + 3 | foo | 1.5 +(7 rows) + +select * from order_all_tbl order by all nulls last; + a | b | c +---+-----+----- + 1 | bar | + 1 | qux | 1.5 + 2 | baz | 1.5 + 2 | | 2 + 3 | foo | 1.5 + 3 | foo | + | nul | 3 +(7 rows) + +select * from order_all_tbl order by all asc nulls first; + a | b | c +---+-----+----- + | nul | 3 + 1 | bar | + 1 | qux | 1.5 + 2 | | 2 + 2 | baz | 1.5 + 3 | foo | + 3 | foo | 1.5 +(7 rows) + +select * from order_all_tbl order by all asc nulls last; + a | b | c +---+-----+----- + 1 | bar | + 1 | qux | 1.5 + 2 | baz | 1.5 + 2 | | 2 + 3 | foo | 1.5 + 3 | foo | + | nul | 3 +(7 rows) + +select * from order_all_tbl order by all desc nulls first; + a | b | c +---+-----+----- + | nul | 3 + 3 | foo | + 3 | foo | 1.5 + 2 | | 2 + 2 | baz | 1.5 + 1 | qux | 1.5 + 1 | bar | +(7 rows) + +select * from order_all_tbl order by all desc nulls last; + a | b | c +---+-----+----- + 3 | foo | 1.5 + 3 | foo | + 2 | baz | 1.5 + 2 | | 2 + 1 | qux | 1.5 + 1 | bar | + | nul | 3 +(7 rows) + +-- equivalent to listing every select-list column +select * from order_all_tbl order by a desc, b desc, c desc; + a | b | c +---+-----+----- + | nul | 3 + 3 | foo | + 3 | foo | 1.5 + 2 | | 2 + 2 | baz | 1.5 + 1 | qux | 1.5 + 1 | bar | +(7 rows) + +-- subset of columns, expressions, duplicate columns, aggregates +select a, b from order_all_tbl order by all; + a | b +---+----- + 1 | bar + 1 | qux + 2 | baz + 2 | + 3 | foo + 3 | foo + | nul +(7 rows) + +select a + 1 as x, b || '_t' as y from order_all_tbl order by all desc; + x | y +---+------- + | nul_t + 4 | foo_t + 4 | foo_t + 3 | + 3 | baz_t + 2 | qux_t + 2 | bar_t +(7 rows) + +select a, a from order_all_tbl order by all; + a | a +---+--- + 1 | 1 + 1 | 1 + 2 | 2 + 2 | 2 + 3 | 3 + 3 | 3 + | +(7 rows) + +select a, count(*) from order_all_tbl group by a order by all; + a | count +---+------- + 1 | 2 + 2 | 2 + 3 | 2 + | 1 +(4 rows) + +-- interaction with other clauses +select distinct a, b from order_all_tbl order by all; + a | b +---+----- + 1 | bar + 1 | qux + 2 | baz + 2 | + 3 | foo + | nul +(6 rows) + +select distinct on (a) a, b from order_all_tbl order by all; + a | b +---+----- + 1 | bar + 2 | baz + 3 | foo + | nul +(4 rows) + +select * from order_all_tbl order by all desc limit 3; + a | b | c +---+-----+----- + | nul | 3 + 3 | foo | + 3 | foo | 1.5 +(3 rows) + +select * from order_all_tbl where a > 1 order by all; + a | b | c +---+-----+----- + 2 | baz | 1.5 + 2 | | 2 + 3 | foo | 1.5 + 3 | foo | +(4 rows) + +-- subqueries, CTEs, joins, VALUES +select * from (select a, b from order_all_tbl order by all) sq; + a | b +---+----- + 1 | bar + 1 | qux + 2 | baz + 2 | + 3 | foo + 3 | foo + | nul +(7 rows) + +with cte as (select a, b from order_all_tbl where a > 1) + select * from cte order by all desc; + a | b +---+----- + 3 | foo + 3 | foo + 2 | + 2 | baz +(4 rows) + +select t1.a, t1.b, t2.b as b2 + from order_all_tbl t1 join order_all_tbl t2 on t1.a = t2.a and t1.b < t2.b + order by all; + a | b | b2 +---+-----+----- + 1 | bar | qux +(1 row) + +select * from (values (3, 'foo'), (1, 'bar'), (2, 'baz')) as t(x, y) order by all; + x | y +---+----- + 1 | bar + 2 | baz + 3 | foo +(3 rows) + +values (3, 'foo'), (1, 'bar'), (2, 'baz') order by all desc; + column1 | column2 +---------+--------- + 3 | foo + 2 | baz + 1 | bar +(3 rows) + +-- not allowed: ALL combined with other sort items or a USING operator +select * from order_all_tbl order by all, a; +ERROR: syntax error at or near "," +LINE 1: select * from order_all_tbl order by all, a; + ^ +select * from order_all_tbl order by a, all; +ERROR: syntax error at or near "all" +LINE 1: select * from order_all_tbl order by a, all; + ^ +select * from order_all_tbl order by all, all; +ERROR: syntax error at or near "," +LINE 1: select * from order_all_tbl order by all, all; + ^ +select * from order_all_tbl order by all using <; +ERROR: syntax error at or near "using" +LINE 1: select * from order_all_tbl order by all using <; + ^ +-- ORDER BY ALL together with GROUP BY and DISTINCT must behave exactly like +-- the equivalent explicit ORDER BY (compare each pair of results) +create temp table order_all_num (n numeric, g int); +insert into order_all_num values + (1.5, 1), (1.5, 1), (2.5, 2), (2.5, 2), (1.5, 3), (null, 4), (null, 4); +select n, count(*) from order_all_num group by n order by all desc; + n | count +-----+------- + | 2 + 2.5 | 2 + 1.5 | 3 +(3 rows) + +select n, count(*) from order_all_num group by n order by n desc, count(*) desc; + n | count +-----+------- + | 2 + 2.5 | 2 + 1.5 | 3 +(3 rows) + +select distinct n from order_all_num order by all; + n +----- + 1.5 + 2.5 + +(3 rows) + +select distinct n from order_all_num order by n; + n +----- + 1.5 + 2.5 + +(3 rows) + +select g, max(n), count(*) from order_all_num group by g + order by all desc nulls last; + g | max | count +---+-----+------- + 4 | | 2 + 3 | 1.5 | 1 + 2 | 2.5 | 2 + 1 | 1.5 | 2 +(4 rows) + +select g, max(n), count(*) from order_all_num group by g + order by g desc nulls last, max(n) desc nulls last, count(*) desc nulls last; + g | max | count +---+-----+------- + 4 | | 2 + 3 | 1.5 | 1 + 2 | 2.5 | 2 + 1 | 1.5 | 2 +(4 rows) + +select g, n, count(*) from order_all_num group by rollup (g, n) + order by all nulls last; + g | n | count +---+-----+------- + 1 | 1.5 | 2 + 1 | | 2 + 2 | 2.5 | 2 + 2 | | 2 + 3 | 1.5 | 1 + 3 | | 1 + 4 | | 2 + 4 | | 2 + | | 7 +(9 rows) + +select g, n, count(*) from order_all_num group by rollup (g, n) + order by g nulls last, n nulls last, count(*) nulls last; + g | n | count +---+-----+------- + 1 | 1.5 | 2 + 1 | | 2 + 2 | 2.5 | 2 + 2 | | 2 + 3 | 1.5 | 1 + 3 | | 1 + 4 | | 2 + 4 | | 2 + | | 7 +(9 rows) + +-- an ungrouped select-list column is still an error +select n, g from order_all_num group by n order by all; +ERROR: column "order_all_num.g" must appear in the GROUP BY clause or be used in an aggregate function +LINE 1: select n, g from order_all_num group by n order by all; + ^ +-- a second ORDER BY on a parenthesized query is an error +(select n from order_all_num order by n) order by all; +ERROR: multiple ORDER BY clauses not allowed +LINE 1: (select n from order_all_num order by n) order by all; + ^ +drop table order_all_num; +drop table order_all_tbl; diff --git a/src/test/regress/expected/union.out b/src/test/regress/expected/union.out index 2495351d907..0cf6894954d 100644 --- a/src/test/regress/expected/union.out +++ b/src/test/regress/expected/union.out @@ -1804,3 +1804,96 @@ join (select ten from tenk1 union select ten from onek) s on s.ten = t.unique1; Index Cond: (unique1 = tenk1.ten) (8 rows) +-- +-- ORDER BY ALL on set operations +-- +create temp table union_order_all (a int, b text); +insert into union_order_all values (3, 'foo'), (1, 'bar'), (2, null), (1, 'qux'); +select a, b from union_order_all union select a, b from union_order_all + order by all; + a | b +---+----- + 1 | bar + 1 | qux + 2 | + 3 | foo +(4 rows) + +select a, b from union_order_all union all select a, b from union_order_all + order by all desc nulls last; + a | b +---+----- + 3 | foo + 3 | foo + 2 | + 2 | + 1 | qux + 1 | qux + 1 | bar + 1 | bar +(8 rows) + +select a, b from union_order_all intersect select a, b from union_order_all + order by all desc; + a | b +---+----- + 3 | foo + 2 | + 1 | qux + 1 | bar +(4 rows) + +select a, b from union_order_all except select 1, 'bar' + order by all nulls first; + a | b +---+----- + 1 | qux + 2 | + 3 | foo +(3 rows) + +select * from (select a, b from union_order_all + union all select a, b from union_order_all) s + order by all; + a | b +---+----- + 1 | bar + 1 | bar + 1 | qux + 1 | qux + 2 | + 2 | + 3 | foo + 3 | foo +(8 rows) + +-- a view over a set operation must deparse with ORDER BY ALL +create view union_order_all_v as + select a, b from union_order_all union select a, b from union_order_all + order by all desc nulls last; +NOTICE: view "union_order_all_v" will be a temporary view +DETAIL: It depends on temporary table union_order_all. +select pg_get_viewdef('union_order_all_v'::regclass, true); + pg_get_viewdef +-------------------------------------------------- + SELECT union_order_all.a, + + union_order_all.b + + FROM union_order_all + + UNION + + SELECT union_order_all.a, + + union_order_all.b + + FROM union_order_all + + ORDER BY 1 DESC NULLS LAST, 2 DESC NULLS LAST; +(1 row) + +select * from union_order_all_v; + a | b +---+----- + 3 | foo + 2 | + 1 | qux + 1 | bar +(4 rows) + +drop view union_order_all_v; +drop table union_order_all; diff --git a/src/test/regress/expected/window.out b/src/test/regress/expected/window.out index 112551c350b..42640aec2b5 100644 --- a/src/test/regress/expected/window.out +++ b/src/test/regress/expected/window.out @@ -6319,3 +6319,22 @@ SELECT last_value FROM null_treatment_seq; --cleanup DROP TABLE planets CASCADE; NOTICE: drop cascades to view planets_view +-- ORDER BY ALL is only valid for a query's ORDER BY, not in window definitions +SELECT rank() OVER (ORDER BY ALL) FROM tenk1; +ERROR: ORDER BY ALL is only allowed in a query's ORDER BY clause +LINE 1: SELECT rank() OVER (ORDER BY ALL) FROM tenk1; + ^ +SELECT four FROM tenk1 WINDOW w AS (ORDER BY ALL DESC); +ERROR: ORDER BY ALL is only allowed in a query's ORDER BY clause +LINE 1: SELECT four FROM tenk1 WINDOW w AS (ORDER BY ALL DESC); + ^ +-- ... but it is fine in the query's own ORDER BY, next to window functions +SELECT four, ten, row_number() OVER (PARTITION BY four ORDER BY ten) AS rn + FROM tenk1 ORDER BY ALL LIMIT 3; + four | ten | rn +------+-----+---- + 0 | 0 | 1 + 0 | 0 | 2 + 0 | 0 | 3 +(3 rows) + diff --git a/src/test/regress/expected/with.out b/src/test/regress/expected/with.out index addb24896be..f364dfb4bf1 100644 --- a/src/test/regress/expected/with.out +++ b/src/test/regress/expected/with.out @@ -2163,6 +2163,12 @@ WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x ORDER BY 1) ERROR: ORDER BY in a recursive query is not implemented LINE 1: ...VE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x ORDER BY 1) ^ +-- ORDER BY ALL +WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x ORDER BY ALL) + SELECT * FROM x; +ERROR: ORDER BY in a recursive query is not implemented +LINE 1: ... x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x ORDER BY ALL) + ^ -- LIMIT/OFFSET WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x LIMIT 10 OFFSET 1) SELECT * FROM x; diff --git a/src/test/regress/sql/aggregates.sql b/src/test/regress/sql/aggregates.sql index 0936088b074..13a8595a476 100644 --- a/src/test/regress/sql/aggregates.sql +++ b/src/test/regress/sql/aggregates.sql @@ -1803,3 +1803,9 @@ drop table agg_hash_1; drop table agg_hash_2; drop table agg_hash_3; drop table agg_hash_4; + +-- +-- ORDER BY ALL is not allowed inside aggregate calls +-- +select array_agg(unique1 order by all) from tenk1; +select percentile_disc(0.5) within group (order by all) from tenk1; diff --git a/src/test/regress/sql/create_view.sql b/src/test/regress/sql/create_view.sql index f80b9823f00..65c682673ab 100644 --- a/src/test/regress/sql/create_view.sql +++ b/src/test/regress/sql/create_view.sql @@ -855,3 +855,22 @@ reset restrict_nonsystem_relation_kind; -- clean up all the random objects we made above DROP SCHEMA temp_view_test CASCADE; DROP SCHEMA testviewschm2 CASCADE; + +-- +-- Deparsing of ORDER BY ALL in view definitions +-- +create temp table order_all_vtbl (a int, b text); + +create view order_all_v1 as select * from order_all_vtbl order by all; +create view order_all_v2 as select * from order_all_vtbl order by all desc; +create view order_all_v3 as select * from order_all_vtbl order by all nulls first; +create view order_all_v4 as select * from order_all_vtbl order by all asc nulls last; +create view order_all_v5 as select * from order_all_vtbl order by all desc nulls first; +create view order_all_v6 as select * from order_all_vtbl order by all desc nulls last; + +select viewname, definition from pg_views + where viewname like 'order\_all\_v%' order by viewname; + +drop view order_all_v1, order_all_v2, order_all_v3, + order_all_v4, order_all_v5, order_all_v6; +drop table order_all_vtbl; diff --git a/src/test/regress/sql/select.sql b/src/test/regress/sql/select.sql index 2dfe88d2054..cdb5929ed13 100644 --- a/src/test/regress/sql/select.sql +++ b/src/test/regress/sql/select.sql @@ -271,3 +271,83 @@ create table list_parted_tbl1 partition of list_parted_tbl for values in (1) partition by list(b); explain (costs off) select * from list_parted_tbl; drop table list_parted_tbl; + +-- +-- ORDER BY ALL +-- + +create temp table order_all_tbl (a int, b text, c float8); +insert into order_all_tbl values + (3, 'foo', 1.5), (1, 'bar', null), (2, 'baz', 1.5), (1, 'qux', 1.5), + (null, 'nul', 3.0), (2, null, 2.0), (3, 'foo', null); + +-- direction and NULLS variants +select * from order_all_tbl order by all; +select * from order_all_tbl order by all asc; +select * from order_all_tbl order by all desc; +select * from order_all_tbl order by all nulls first; +select * from order_all_tbl order by all nulls last; +select * from order_all_tbl order by all asc nulls first; +select * from order_all_tbl order by all asc nulls last; +select * from order_all_tbl order by all desc nulls first; +select * from order_all_tbl order by all desc nulls last; + +-- equivalent to listing every select-list column +select * from order_all_tbl order by a desc, b desc, c desc; + +-- subset of columns, expressions, duplicate columns, aggregates +select a, b from order_all_tbl order by all; +select a + 1 as x, b || '_t' as y from order_all_tbl order by all desc; +select a, a from order_all_tbl order by all; +select a, count(*) from order_all_tbl group by a order by all; + +-- interaction with other clauses +select distinct a, b from order_all_tbl order by all; +select distinct on (a) a, b from order_all_tbl order by all; +select * from order_all_tbl order by all desc limit 3; +select * from order_all_tbl where a > 1 order by all; + +-- subqueries, CTEs, joins, VALUES +select * from (select a, b from order_all_tbl order by all) sq; +with cte as (select a, b from order_all_tbl where a > 1) + select * from cte order by all desc; +select t1.a, t1.b, t2.b as b2 + from order_all_tbl t1 join order_all_tbl t2 on t1.a = t2.a and t1.b < t2.b + order by all; +select * from (values (3, 'foo'), (1, 'bar'), (2, 'baz')) as t(x, y) order by all; +values (3, 'foo'), (1, 'bar'), (2, 'baz') order by all desc; + +-- not allowed: ALL combined with other sort items or a USING operator +select * from order_all_tbl order by all, a; +select * from order_all_tbl order by a, all; +select * from order_all_tbl order by all, all; +select * from order_all_tbl order by all using <; + +-- ORDER BY ALL together with GROUP BY and DISTINCT must behave exactly like +-- the equivalent explicit ORDER BY (compare each pair of results) +create temp table order_all_num (n numeric, g int); +insert into order_all_num values + (1.5, 1), (1.5, 1), (2.5, 2), (2.5, 2), (1.5, 3), (null, 4), (null, 4); + +select n, count(*) from order_all_num group by n order by all desc; +select n, count(*) from order_all_num group by n order by n desc, count(*) desc; +select distinct n from order_all_num order by all; +select distinct n from order_all_num order by n; +select g, max(n), count(*) from order_all_num group by g + order by all desc nulls last; +select g, max(n), count(*) from order_all_num group by g + order by g desc nulls last, max(n) desc nulls last, count(*) desc nulls last; +select g, n, count(*) from order_all_num group by rollup (g, n) + order by all nulls last; +select g, n, count(*) from order_all_num group by rollup (g, n) + order by g nulls last, n nulls last, count(*) nulls last; + +-- an ungrouped select-list column is still an error +select n, g from order_all_num group by n order by all; + +-- a second ORDER BY on a parenthesized query is an error +(select n from order_all_num order by n) order by all; + +drop table order_all_num; + +drop table order_all_tbl; diff --git a/src/test/regress/sql/union.sql b/src/test/regress/sql/union.sql index 8fce4528a3f..34a63e0f73c 100644 --- a/src/test/regress/sql/union.sql +++ b/src/test/regress/sql/union.sql @@ -710,3 +710,32 @@ select null::int[] union all select null::int[] union all select null::bigint[]; explain (costs off) select * from tenk1 t join (select ten from tenk1 union select ten from onek) s on s.ten = t.unique1; + +-- +-- ORDER BY ALL on set operations +-- + +create temp table union_order_all (a int, b text); +insert into union_order_all values (3, 'foo'), (1, 'bar'), (2, null), (1, 'qux'); + +select a, b from union_order_all union select a, b from union_order_all + order by all; +select a, b from union_order_all union all select a, b from union_order_all + order by all desc nulls last; +select a, b from union_order_all intersect select a, b from union_order_all + order by all desc; +select a, b from union_order_all except select 1, 'bar' + order by all nulls first; +select * from (select a, b from union_order_all + union all select a, b from union_order_all) s + order by all; + +-- a view over a set operation must deparse with ORDER BY ALL +create view union_order_all_v as + select a, b from union_order_all union select a, b from union_order_all + order by all desc nulls last; +select pg_get_viewdef('union_order_all_v'::regclass, true); +select * from union_order_all_v; + +drop view union_order_all_v; +drop table union_order_all; diff --git a/src/test/regress/sql/window.sql b/src/test/regress/sql/window.sql index e848557174b..c5a786adaec 100644 --- a/src/test/regress/sql/window.sql +++ b/src/test/regress/sql/window.sql @@ -2333,3 +2333,10 @@ SELECT last_value FROM null_treatment_seq; --cleanup DROP TABLE planets CASCADE; + +-- ORDER BY ALL is only valid for a query's ORDER BY, not in window definitions +SELECT rank() OVER (ORDER BY ALL) FROM tenk1; +SELECT four FROM tenk1 WINDOW w AS (ORDER BY ALL DESC); +-- ... but it is fine in the query's own ORDER BY, next to window functions +SELECT four, ten, row_number() OVER (PARTITION BY four ORDER BY ten) AS rn + FROM tenk1 ORDER BY ALL LIMIT 3; diff --git a/src/test/regress/sql/with.sql b/src/test/regress/sql/with.sql index 0cb26312e21..7db77505b05 100644 --- a/src/test/regress/sql/with.sql +++ b/src/test/regress/sql/with.sql @@ -1008,6 +1008,10 @@ WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT sum(n) FROM x) WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x ORDER BY 1) SELECT * FROM x; +-- ORDER BY ALL +WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x ORDER BY ALL) + SELECT * FROM x; + -- LIMIT/OFFSET WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x LIMIT 10 OFFSET 1) SELECT * FROM x; -- 2.43.0