Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1d41739

Browse files
committed
Don't require sort support functions to provide a comparator.
This could be useful for datatypes like text, where we might want to optimize for some collations but not others. However, this patch doesn't introduce any new sortsupport functions that work this way; it merely revises the code so that future patches may do so. Patch by me. Review by Peter Geoghegan.
1 parent 873de34 commit 1d41739

File tree

4 files changed

+35
-73
lines changed

4 files changed

+35
-73
lines changed

src/backend/executor/nodeMergejoin.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -230,19 +230,19 @@ MJExamineQuals(List *mergeclauses,
230230
qual->opno);
231231

232232
/* And get the matching support or comparison function */
233+
Assert(clause->ssup.comparator == NULL);
233234
sortfunc = get_opfamily_proc(opfamily,
234235
op_lefttype,
235236
op_righttype,
236237
BTSORTSUPPORT_PROC);
237238
if (OidIsValid(sortfunc))
238239
{
239-
/* The sort support function should provide a comparator */
240+
/* The sort support function can provide a comparator */
240241
OidFunctionCall1(sortfunc, PointerGetDatum(&clause->ssup));
241-
Assert(clause->ssup.comparator != NULL);
242242
}
243-
else
243+
if (clause->ssup.comparator == NULL)
244244
{
245-
/* opfamily doesn't provide sort support, get comparison func */
245+
/* support not available, get comparison func */
246246
sortfunc = get_opfamily_proc(opfamily,
247247
op_lefttype,
248248
op_righttype,

src/backend/utils/cache/lsyscache.c

-56
Original file line numberDiff line numberDiff line change
@@ -245,62 +245,6 @@ get_ordering_op_properties(Oid opno,
245245
return result;
246246
}
247247

248-
/*
249-
* get_sort_function_for_ordering_op
250-
* Get the OID of the datatype-specific btree sort support function,
251-
* or if there is none, the btree comparison function,
252-
* associated with an ordering operator (a "<" or ">" operator).
253-
*
254-
* *sortfunc receives the support or comparison function OID.
255-
* *issupport is set TRUE if it's a support func, FALSE if a comparison func.
256-
* *reverse is set FALSE if the operator is "<", TRUE if it's ">"
257-
* (indicating that comparison results must be negated before use).
258-
*
259-
* Returns TRUE if successful, FALSE if no btree function can be found.
260-
* (This indicates that the operator is not a valid ordering operator.)
261-
*/
262-
bool
263-
get_sort_function_for_ordering_op(Oid opno, Oid *sortfunc,
264-
bool *issupport, bool *reverse)
265-
{
266-
Oid opfamily;
267-
Oid opcintype;
268-
int16 strategy;
269-
270-
/* Find the operator in pg_amop */
271-
if (get_ordering_op_properties(opno,
272-
&opfamily, &opcintype, &strategy))
273-
{
274-
/* Found a suitable opfamily, get matching support function */
275-
*sortfunc = get_opfamily_proc(opfamily,
276-
opcintype,
277-
opcintype,
278-
BTSORTSUPPORT_PROC);
279-
if (OidIsValid(*sortfunc))
280-
*issupport = true;
281-
else
282-
{
283-
/* opfamily doesn't provide sort support, get comparison func */
284-
*sortfunc = get_opfamily_proc(opfamily,
285-
opcintype,
286-
opcintype,
287-
BTORDER_PROC);
288-
if (!OidIsValid(*sortfunc)) /* should not happen */
289-
elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
290-
BTORDER_PROC, opcintype, opcintype, opfamily);
291-
*issupport = false;
292-
}
293-
*reverse = (strategy == BTGreaterStrategyNumber);
294-
return true;
295-
}
296-
297-
/* ensure outputs are set on failure */
298-
*sortfunc = InvalidOid;
299-
*issupport = false;
300-
*reverse = false;
301-
return false;
302-
}
303-
304248
/*
305249
* get_equality_op_for_ordering_op
306250
* Get the OID of the datatype-specific btree equality operator

src/backend/utils/sort/sortsupport.c

+31-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/* See sortsupport.h */
1919
#define SORTSUPPORT_INCLUDE_DEFINITIONS
2020

21+
#include "access/nbtree.h"
2122
#include "fmgr.h"
2223
#include "utils/lsyscache.h"
2324
#include "utils/sortsupport.h"
@@ -94,24 +95,43 @@ PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup)
9495
void
9596
PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
9697
{
97-
Oid sortFunction;
98-
bool issupport;
98+
Oid sortSupportFunction;
99+
Oid opfamily;
100+
Oid opcintype;
101+
int16 strategy;
99102

100-
if (!get_sort_function_for_ordering_op(orderingOp,
101-
&sortFunction,
102-
&issupport,
103-
&ssup->ssup_reverse))
103+
Assert(ssup->comparator == NULL);
104+
105+
/* Find the operator in pg_amop */
106+
if (!get_ordering_op_properties(orderingOp, &opfamily, &opcintype,
107+
&strategy))
104108
elog(ERROR, "operator %u is not a valid ordering operator",
105109
orderingOp);
110+
ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
106111

107-
if (issupport)
112+
/* Look for a sort support function */
113+
sortSupportFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
114+
BTSORTSUPPORT_PROC);
115+
if (OidIsValid(sortSupportFunction))
108116
{
109-
/* The sort support function should provide a comparator */
110-
OidFunctionCall1(sortFunction, PointerGetDatum(ssup));
111-
Assert(ssup->comparator != NULL);
117+
/*
118+
* The sort support function can provide a comparator, but it can
119+
* also choose not to so (e.g. based on the selected collation).
120+
*/
121+
OidFunctionCall1(sortSupportFunction, PointerGetDatum(ssup));
112122
}
113-
else
123+
124+
if (ssup->comparator == NULL)
114125
{
126+
Oid sortFunction;
127+
128+
sortFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
129+
BTORDER_PROC);
130+
131+
if (!OidIsValid(sortFunction))
132+
elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
133+
BTORDER_PROC, opcintype, opcintype, opfamily);
134+
115135
/* We'll use a shim to call the old-style btree comparator */
116136
PrepareSortSupportComparisonShim(sortFunction, ssup);
117137
}

src/include/utils/lsyscache.h

-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ extern Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype,
5050
int16 strategy);
5151
extern bool get_ordering_op_properties(Oid opno,
5252
Oid *opfamily, Oid *opcintype, int16 *strategy);
53-
extern bool get_sort_function_for_ordering_op(Oid opno, Oid *sortfunc,
54-
bool *issupport, bool *reverse);
5553
extern Oid get_equality_op_for_ordering_op(Oid opno, bool *reverse);
5654
extern Oid get_ordering_op_for_equality_op(Oid opno, bool use_lhs_type);
5755
extern List *get_mergejoin_opfamilies(Oid opno);

0 commit comments

Comments
 (0)