Skip to content

Commit a359d37

Browse files
peteremarkdilger
andcommitted
Simplify and generalize PrepareSortSupportFromIndexRel()
PrepareSortSupportFromIndexRel() was accepting btree strategy numbers purely for the purpose of comparing it later against btree strategies to determine if the sort direction was forward or reverse. Change that. Instead, pass a bool directly, to indicate the same without an unfortunate assumption that a strategy number refers specifically to a btree strategy. (This is similar in spirit to commits 0d2aa4d and c594f1a.) (This could arguably be simplfied further by having the callers fill in ssup_reverse directly. But this way, it preserves consistency by having all PrepareSortSupport*() variants be responsible for filling in ssup_reverse.) Moreover, remove the hardcoded check against BTREE_AM_OID, and check against amcanorder instead, which is the actual requirement. Co-authored-by: Mark Dilger <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
1 parent 1548c3a commit a359d37

File tree

4 files changed

+16
-22
lines changed

4 files changed

+16
-22
lines changed

src/backend/access/nbtree/nbtsort.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
11711171
{
11721172
SortSupport sortKey = sortKeys + i;
11731173
ScanKey scanKey = wstate->inskey->scankeys + i;
1174-
int16 strategy;
1174+
bool reverse;
11751175

11761176
sortKey->ssup_cxt = CurrentMemoryContext;
11771177
sortKey->ssup_collation = scanKey->sk_collation;
@@ -1183,10 +1183,9 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
11831183

11841184
Assert(sortKey->ssup_attno != 0);
11851185

1186-
strategy = (scanKey->sk_flags & SK_BT_DESC) != 0 ?
1187-
BTGreaterStrategyNumber : BTLessStrategyNumber;
1186+
reverse = (scanKey->sk_flags & SK_BT_DESC) != 0;
11881187

1189-
PrepareSortSupportFromIndexRel(wstate->index, strategy, sortKey);
1188+
PrepareSortSupportFromIndexRel(wstate->index, reverse, sortKey);
11901189
}
11911190

11921191
for (;;)

src/backend/utils/sort/sortsupport.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,28 +150,25 @@ PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
150150
}
151151

152152
/*
153-
* Fill in SortSupport given an index relation, attribute, and strategy.
153+
* Fill in SortSupport given an index relation and attribute.
154154
*
155155
* Caller must previously have zeroed the SortSupportData structure and then
156156
* filled in ssup_cxt, ssup_attno, ssup_collation, and ssup_nulls_first. This
157-
* will fill in ssup_reverse (based on the supplied strategy), as well as the
157+
* will fill in ssup_reverse (based on the supplied argument), as well as the
158158
* comparator function pointer.
159159
*/
160160
void
161-
PrepareSortSupportFromIndexRel(Relation indexRel, int16 strategy,
161+
PrepareSortSupportFromIndexRel(Relation indexRel, bool reverse,
162162
SortSupport ssup)
163163
{
164164
Oid opfamily = indexRel->rd_opfamily[ssup->ssup_attno - 1];
165165
Oid opcintype = indexRel->rd_opcintype[ssup->ssup_attno - 1];
166166

167167
Assert(ssup->comparator == NULL);
168168

169-
if (indexRel->rd_rel->relam != BTREE_AM_OID)
170-
elog(ERROR, "unexpected non-btree AM: %u", indexRel->rd_rel->relam);
171-
if (strategy != BTGreaterStrategyNumber &&
172-
strategy != BTLessStrategyNumber)
173-
elog(ERROR, "unexpected sort support strategy: %d", strategy);
174-
ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
169+
if (!indexRel->rd_indam->amcanorder)
170+
elog(ERROR, "unexpected non-amcanorder AM: %u", indexRel->rd_rel->relam);
171+
ssup->ssup_reverse = reverse;
175172

176173
FinishSortSupportFunction(opfamily, opcintype, ssup);
177174
}

src/backend/utils/sort/tuplesortvariants.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ tuplesort_begin_cluster(TupleDesc tupDesc,
329329
{
330330
SortSupport sortKey = base->sortKeys + i;
331331
ScanKey scanKey = indexScanKey->scankeys + i;
332-
int16 strategy;
332+
bool reverse;
333333

334334
sortKey->ssup_cxt = CurrentMemoryContext;
335335
sortKey->ssup_collation = scanKey->sk_collation;
@@ -341,10 +341,9 @@ tuplesort_begin_cluster(TupleDesc tupDesc,
341341

342342
Assert(sortKey->ssup_attno != 0);
343343

344-
strategy = (scanKey->sk_flags & SK_BT_DESC) != 0 ?
345-
BTGreaterStrategyNumber : BTLessStrategyNumber;
344+
reverse = (scanKey->sk_flags & SK_BT_DESC) != 0;
346345

347-
PrepareSortSupportFromIndexRel(indexRel, strategy, sortKey);
346+
PrepareSortSupportFromIndexRel(indexRel, reverse, sortKey);
348347
}
349348

350349
pfree(indexScanKey);
@@ -412,7 +411,7 @@ tuplesort_begin_index_btree(Relation heapRel,
412411
{
413412
SortSupport sortKey = base->sortKeys + i;
414413
ScanKey scanKey = indexScanKey->scankeys + i;
415-
int16 strategy;
414+
bool reverse;
416415

417416
sortKey->ssup_cxt = CurrentMemoryContext;
418417
sortKey->ssup_collation = scanKey->sk_collation;
@@ -424,10 +423,9 @@ tuplesort_begin_index_btree(Relation heapRel,
424423

425424
Assert(sortKey->ssup_attno != 0);
426425

427-
strategy = (scanKey->sk_flags & SK_BT_DESC) != 0 ?
428-
BTGreaterStrategyNumber : BTLessStrategyNumber;
426+
reverse = (scanKey->sk_flags & SK_BT_DESC) != 0;
429427

430-
PrepareSortSupportFromIndexRel(indexRel, strategy, sortKey);
428+
PrepareSortSupportFromIndexRel(indexRel, reverse, sortKey);
431429
}
432430

433431
pfree(indexScanKey);

src/include/utils/sortsupport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ extern int ssup_datum_int32_cmp(Datum x, Datum y, SortSupport ssup);
384384
/* Other functions in utils/sort/sortsupport.c */
385385
extern void PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup);
386386
extern void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup);
387-
extern void PrepareSortSupportFromIndexRel(Relation indexRel, int16 strategy,
387+
extern void PrepareSortSupportFromIndexRel(Relation indexRel, bool reverse,
388388
SortSupport ssup);
389389
extern void PrepareSortSupportFromGistIndexRel(Relation indexRel, SortSupport ssup);
390390

0 commit comments

Comments
 (0)