Skip to content

Commit 0d2aa4d

Browse files
committed
Track sort direction in SortGroupClause
Functions make_pathkey_from_sortop() and transformWindowDefinitions(), which receive a SortGroupClause, were determining the sort order (ascending vs. descending) by comparing that structure's operator strategy to BTLessStrategyNumber, but could just as easily have gotten it from the SortGroupClause object, if it had such a field, so add one. This reduces the number of places that hardcode the assumption that the strategy refers specifically to a btree strategy, rather than some other index AM's operators. Author: Mark Dilger <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
1 parent e7d0cf4 commit 0d2aa4d

File tree

8 files changed

+18
-8
lines changed

8 files changed

+18
-8
lines changed

src/backend/optimizer/path/pathkeys.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ static PathKey *
256256
make_pathkey_from_sortop(PlannerInfo *root,
257257
Expr *expr,
258258
Oid ordering_op,
259+
bool reverse_sort,
259260
bool nulls_first,
260261
Index sortref,
261262
bool create_it)
@@ -279,7 +280,7 @@ make_pathkey_from_sortop(PlannerInfo *root,
279280
opfamily,
280281
opcintype,
281282
collation,
282-
(strategy == BTGreaterStrategyNumber),
283+
reverse_sort,
283284
nulls_first,
284285
sortref,
285286
NULL,
@@ -1411,6 +1412,7 @@ make_pathkeys_for_sortclauses_extended(PlannerInfo *root,
14111412
pathkey = make_pathkey_from_sortop(root,
14121413
sortkey,
14131414
sortcl->sortop,
1415+
sortcl->reverse_sort,
14141416
sortcl->nulls_first,
14151417
sortcl->tleSortGroupRef,
14161418
true);

src/backend/optimizer/plan/createplan.c

+1
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,7 @@ create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
18961896
subplan->targetlist);
18971897
sortcl->eqop = eqop;
18981898
sortcl->sortop = sortop;
1899+
sortcl->reverse_sort = false;
18991900
sortcl->nulls_first = false;
19001901
sortcl->hashable = false; /* no need to make this accurate */
19011902
sortList = lappend(sortList, sortcl);

src/backend/optimizer/plan/planagg.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848

4949
static bool can_minmax_aggs(PlannerInfo *root, List **context);
5050
static bool build_minmax_path(PlannerInfo *root, MinMaxAggInfo *mminfo,
51-
Oid eqop, Oid sortop, bool nulls_first);
51+
Oid eqop, Oid sortop, bool reverse_sort,
52+
bool nulls_first);
5253
static void minmax_qp_callback(PlannerInfo *root, void *extra);
5354
static Oid fetch_agg_sort_op(Oid aggfnoid);
5455

@@ -172,9 +173,9 @@ preprocess_minmax_aggregates(PlannerInfo *root)
172173
* FIRST is more likely to be available if the operator is a
173174
* reverse-sort operator, so try that first if reverse.
174175
*/
175-
if (build_minmax_path(root, mminfo, eqop, mminfo->aggsortop, reverse))
176+
if (build_minmax_path(root, mminfo, eqop, mminfo->aggsortop, reverse, reverse))
176177
continue;
177-
if (build_minmax_path(root, mminfo, eqop, mminfo->aggsortop, !reverse))
178+
if (build_minmax_path(root, mminfo, eqop, mminfo->aggsortop, reverse, !reverse))
178179
continue;
179180

180181
/* No indexable path for this aggregate, so fail */
@@ -314,7 +315,7 @@ can_minmax_aggs(PlannerInfo *root, List **context)
314315
*/
315316
static bool
316317
build_minmax_path(PlannerInfo *root, MinMaxAggInfo *mminfo,
317-
Oid eqop, Oid sortop, bool nulls_first)
318+
Oid eqop, Oid sortop, bool reverse_sort, bool nulls_first)
318319
{
319320
PlannerInfo *subroot;
320321
Query *parse;
@@ -399,6 +400,7 @@ build_minmax_path(PlannerInfo *root, MinMaxAggInfo *mminfo,
399400
sortcl->tleSortGroupRef = assignSortGroupRef(tle, subroot->processed_tlist);
400401
sortcl->eqop = eqop;
401402
sortcl->sortop = sortop;
403+
sortcl->reverse_sort = reverse_sort;
402404
sortcl->nulls_first = nulls_first;
403405
sortcl->hashable = false; /* no need to make this accurate */
404406
parse->sortClause = list_make1(sortcl);

src/backend/parser/analyze.c

+1
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,7 @@ makeSortGroupClauseForSetOp(Oid rescoltype, bool require_hash)
19851985
grpcl->tleSortGroupRef = 0;
19861986
grpcl->eqop = eqop;
19871987
grpcl->sortop = sortop;
1988+
grpcl->reverse_sort = false; /* Sort-op is "less than", or InvalidOid */
19881989
grpcl->nulls_first = false; /* OK with or without sortop */
19891990
grpcl->hashable = hashable;
19901991

src/backend/parser/parse_clause.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -2933,7 +2933,7 @@ transformWindowDefinitions(ParseState *pstate,
29332933
sortcl->sortop);
29342934
/* Record properties of sort ordering */
29352935
wc->inRangeColl = exprCollation(sortkey);
2936-
wc->inRangeAsc = (rangestrategy == BTLessStrategyNumber);
2936+
wc->inRangeAsc = !sortcl->reverse_sort;
29372937
wc->inRangeNullsFirst = sortcl->nulls_first;
29382938
}
29392939

@@ -3489,6 +3489,7 @@ addTargetToSortList(ParseState *pstate, TargetEntry *tle,
34893489
sortcl->eqop = eqop;
34903490
sortcl->sortop = sortop;
34913491
sortcl->hashable = hashable;
3492+
sortcl->reverse_sort = reverse;
34923493

34933494
switch (sortby->sortby_nulls)
34943495
{
@@ -3571,6 +3572,8 @@ addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
35713572
grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
35723573
grpcl->eqop = eqop;
35733574
grpcl->sortop = sortop;
3575+
grpcl->reverse_sort = false; /* sortop is "less than", or
3576+
* InvalidOid */
35743577
grpcl->nulls_first = false; /* OK with or without sortop */
35753578
grpcl->hashable = hashable;
35763579

src/backend/utils/cache/lsyscache.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ get_equality_op_for_ordering_op(Oid opno, bool *reverse)
289289

290290
/*
291291
* get_ordering_op_for_equality_op
292-
* Get the OID of a datatype-specific btree ordering operator
292+
* Get the OID of a datatype-specific btree "less than" ordering operator
293293
* associated with an equality operator. (If there are multiple
294294
* possibilities, assume any one will do.)
295295
*

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202410114
60+
#define CATALOG_VERSION_NO 202410141
6161

6262
#endif

src/include/nodes/parsenodes.h

+1
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,7 @@ typedef struct SortGroupClause
14381438
Index tleSortGroupRef; /* reference into targetlist */
14391439
Oid eqop; /* the equality operator ('=' op) */
14401440
Oid sortop; /* the ordering operator ('<' op), or 0 */
1441+
bool reverse_sort; /* is sortop a "greater than" operator? */
14411442
bool nulls_first; /* do NULLs come before normal values? */
14421443
/* can eqop be implemented by hashing? */
14431444
bool hashable pg_node_attr(query_jumble_ignore);

0 commit comments

Comments
 (0)