2011-12-07 06:18:38 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* sortsupport.c
|
|
|
|
* Support routines for accelerated sorting.
|
|
|
|
*
|
|
|
|
*
|
2014-01-07 22:05:30 +01:00
|
|
|
* Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
|
2011-12-07 06:18:38 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/backend/utils/sort/sortsupport.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2012-10-08 21:12:27 +02:00
|
|
|
/* See sortsupport.h */
|
|
|
|
#define SORTSUPPORT_INCLUDE_DEFINITIONS
|
|
|
|
|
2014-08-06 22:06:06 +02:00
|
|
|
#include "access/nbtree.h"
|
2011-12-07 06:18:38 +01:00
|
|
|
#include "fmgr.h"
|
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
#include "utils/sortsupport.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Info needed to use an old-style comparison function as a sort comparator */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
FunctionCallInfoData fcinfo; /* reusable callinfo structure */
|
2012-06-10 21:20:04 +02:00
|
|
|
FmgrInfo flinfo; /* lookup data for comparison function */
|
2011-12-07 06:18:38 +01:00
|
|
|
} SortShimExtra;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shim function for calling an old-style comparator
|
|
|
|
*
|
|
|
|
* This is essentially an inlined version of FunctionCall2Coll(), except
|
|
|
|
* we assume that the FunctionCallInfoData was already mostly set up by
|
|
|
|
* PrepareSortSupportComparisonShim.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
comparison_shim(Datum x, Datum y, SortSupport ssup)
|
|
|
|
{
|
|
|
|
SortShimExtra *extra = (SortShimExtra *) ssup->ssup_extra;
|
|
|
|
Datum result;
|
|
|
|
|
|
|
|
extra->fcinfo.arg[0] = x;
|
|
|
|
extra->fcinfo.arg[1] = y;
|
|
|
|
|
|
|
|
/* just for paranoia's sake, we reset isnull each time */
|
|
|
|
extra->fcinfo.isnull = false;
|
|
|
|
|
|
|
|
result = FunctionCallInvoke(&extra->fcinfo);
|
|
|
|
|
|
|
|
/* Check for null result, since caller is clearly not expecting one */
|
|
|
|
if (extra->fcinfo.isnull)
|
|
|
|
elog(ERROR, "function %u returned NULL", extra->flinfo.fn_oid);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up a shim function to allow use of an old-style btree comparison
|
|
|
|
* function as if it were a sort support comparator.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup)
|
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
SortShimExtra *extra;
|
2011-12-07 06:18:38 +01:00
|
|
|
|
|
|
|
extra = (SortShimExtra *) MemoryContextAlloc(ssup->ssup_cxt,
|
|
|
|
sizeof(SortShimExtra));
|
|
|
|
|
|
|
|
/* Lookup the comparison function */
|
|
|
|
fmgr_info_cxt(cmpFunc, &extra->flinfo, ssup->ssup_cxt);
|
|
|
|
|
|
|
|
/* We can initialize the callinfo just once and re-use it */
|
|
|
|
InitFunctionCallInfoData(extra->fcinfo, &extra->flinfo, 2,
|
|
|
|
ssup->ssup_collation, NULL, NULL);
|
|
|
|
extra->fcinfo.argnull[0] = false;
|
|
|
|
extra->fcinfo.argnull[1] = false;
|
|
|
|
|
|
|
|
ssup->ssup_extra = extra;
|
|
|
|
ssup->comparator = comparison_shim;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill in SortSupport given an ordering operator (btree "<" or ">" operator).
|
|
|
|
*
|
|
|
|
* Caller must previously have zeroed the SortSupportData structure and then
|
|
|
|
* filled in ssup_cxt, ssup_collation, and ssup_nulls_first. This will fill
|
|
|
|
* in ssup_reverse as well as the comparator function pointer.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
|
|
|
|
{
|
2014-08-06 22:06:06 +02:00
|
|
|
Oid sortSupportFunction;
|
|
|
|
Oid opfamily;
|
|
|
|
Oid opcintype;
|
|
|
|
int16 strategy;
|
2011-12-07 06:18:38 +01:00
|
|
|
|
2014-08-06 22:06:06 +02:00
|
|
|
Assert(ssup->comparator == NULL);
|
|
|
|
|
|
|
|
/* Find the operator in pg_amop */
|
|
|
|
if (!get_ordering_op_properties(orderingOp, &opfamily, &opcintype,
|
|
|
|
&strategy))
|
2011-12-07 06:18:38 +01:00
|
|
|
elog(ERROR, "operator %u is not a valid ordering operator",
|
|
|
|
orderingOp);
|
2014-08-06 22:06:06 +02:00
|
|
|
ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
|
2011-12-07 06:18:38 +01:00
|
|
|
|
2014-08-06 22:06:06 +02:00
|
|
|
/* Look for a sort support function */
|
|
|
|
sortSupportFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
|
|
|
|
BTSORTSUPPORT_PROC);
|
|
|
|
if (OidIsValid(sortSupportFunction))
|
2011-12-07 06:18:38 +01:00
|
|
|
{
|
2014-08-06 22:06:06 +02:00
|
|
|
/*
|
|
|
|
* The sort support function can provide a comparator, but it can
|
|
|
|
* also choose not to so (e.g. based on the selected collation).
|
|
|
|
*/
|
|
|
|
OidFunctionCall1(sortSupportFunction, PointerGetDatum(ssup));
|
2011-12-07 06:18:38 +01:00
|
|
|
}
|
2014-08-06 22:06:06 +02:00
|
|
|
|
|
|
|
if (ssup->comparator == NULL)
|
2011-12-07 06:18:38 +01:00
|
|
|
{
|
2014-08-06 22:06:06 +02:00
|
|
|
Oid sortFunction;
|
|
|
|
|
|
|
|
sortFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
|
|
|
|
BTORDER_PROC);
|
|
|
|
|
|
|
|
if (!OidIsValid(sortFunction))
|
|
|
|
elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
|
|
|
|
BTORDER_PROC, opcintype, opcintype, opfamily);
|
|
|
|
|
2011-12-07 06:18:38 +01:00
|
|
|
/* We'll use a shim to call the old-style btree comparator */
|
|
|
|
PrepareSortSupportComparisonShim(sortFunction, ssup);
|
|
|
|
}
|
|
|
|
}
|