PostgreSQL Source Code  git master
sortsupport.h File Reference
#include "access/attnum.h"
#include "utils/relcache.h"
Include dependency graph for sortsupport.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SortSupportData
 

Typedefs

typedef struct SortSupportDataSortSupport
 
typedef struct SortSupportData SortSupportData
 

Functions

static int ApplySortComparator (Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
 
static int ApplySortAbbrevFullComparator (Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
 
void PrepareSortSupportComparisonShim (Oid cmpFunc, SortSupport ssup)
 
void PrepareSortSupportFromOrderingOp (Oid orderingOp, SortSupport ssup)
 
void PrepareSortSupportFromIndexRel (Relation indexRel, int16 strategy, SortSupport ssup)
 

Typedef Documentation

◆ SortSupport

Definition at line 58 of file sortsupport.h.

◆ SortSupportData

Function Documentation

◆ ApplySortAbbrevFullComparator()

static int ApplySortAbbrevFullComparator ( Datum  datum1,
bool  isNull1,
Datum  datum2,
bool  isNull2,
SortSupport  ssup 
)
inlinestatic

Definition at line 238 of file sortsupport.h.

References SortSupportData::abbrev_full_comparator, compare(), INVERT_COMPARE_RESULT, PrepareSortSupportComparisonShim(), PrepareSortSupportFromIndexRel(), PrepareSortSupportFromOrderingOp(), SortSupportData::ssup_nulls_first, and SortSupportData::ssup_reverse.

Referenced by comparetup_cluster(), comparetup_datum(), comparetup_heap(), and comparetup_index_btree().

241 {
242  int compare;
243 
244  if (isNull1)
245  {
246  if (isNull2)
247  compare = 0; /* NULL "=" NULL */
248  else if (ssup->ssup_nulls_first)
249  compare = -1; /* NULL "<" NOT_NULL */
250  else
251  compare = 1; /* NULL ">" NOT_NULL */
252  }
253  else if (isNull2)
254  {
255  if (ssup->ssup_nulls_first)
256  compare = 1; /* NOT_NULL ">" NULL */
257  else
258  compare = -1; /* NOT_NULL "<" NULL */
259  }
260  else
261  {
262  compare = ssup->abbrev_full_comparator(datum1, datum2, ssup);
263  if (ssup->ssup_reverse)
264  INVERT_COMPARE_RESULT(compare);
265  }
266 
267  return compare;
268 }
bool ssup_nulls_first
Definition: sortsupport.h:75
static int compare(const void *arg1, const void *arg2)
Definition: geqo_pool.c:145
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:191
#define INVERT_COMPARE_RESULT(var)
Definition: c.h:1044

◆ ApplySortComparator()

static int ApplySortComparator ( Datum  datum1,
bool  isNull1,
Datum  datum2,
bool  isNull2,
SortSupport  ssup 
)
inlinestatic

Definition at line 200 of file sortsupport.h.

References SortSupportData::comparator, compare(), INVERT_COMPARE_RESULT, SortSupportData::ssup_nulls_first, and SortSupportData::ssup_reverse.

Referenced by _bt_load(), compare_datums_simple(), compare_scalars(), comparetup_cluster(), comparetup_datum(), comparetup_heap(), comparetup_index_btree(), heap_compare_slots(), MJCompare(), multi_sort_compare(), multi_sort_compare_dim(), multi_sort_compare_dims(), and sort_item_compare().

203 {
204  int compare;
205 
206  if (isNull1)
207  {
208  if (isNull2)
209  compare = 0; /* NULL "=" NULL */
210  else if (ssup->ssup_nulls_first)
211  compare = -1; /* NULL "<" NOT_NULL */
212  else
213  compare = 1; /* NULL ">" NOT_NULL */
214  }
215  else if (isNull2)
216  {
217  if (ssup->ssup_nulls_first)
218  compare = 1; /* NOT_NULL ">" NULL */
219  else
220  compare = -1; /* NOT_NULL "<" NULL */
221  }
222  else
223  {
224  compare = ssup->comparator(datum1, datum2, ssup);
225  if (ssup->ssup_reverse)
226  INVERT_COMPARE_RESULT(compare);
227  }
228 
229  return compare;
230 }
bool ssup_nulls_first
Definition: sortsupport.h:75
static int compare(const void *arg1, const void *arg2)
Definition: geqo_pool.c:145
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
#define INVERT_COMPARE_RESULT(var)
Definition: c.h:1044

◆ PrepareSortSupportComparisonShim()

void PrepareSortSupportComparisonShim ( Oid  cmpFunc,
SortSupport  ssup 
)

Definition at line 68 of file sortsupport.c.

References FunctionCallInfoBaseData::args, SortSupportData::comparator, comparison_shim(), SortShimExtra::fcinfo, SortShimExtra::flinfo, fmgr_info_cxt(), InitFunctionCallInfoData, NullableDatum::isnull, MemoryContextAlloc(), SizeForSortShimExtra, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, and SortSupportData::ssup_extra.

Referenced by ApplySortAbbrevFullComparator(), FinishSortSupportFunction(), and MJExamineQuals().

69 {
70  SortShimExtra *extra;
71 
72  extra = (SortShimExtra *) MemoryContextAlloc(ssup->ssup_cxt,
74 
75  /* Lookup the comparison function */
76  fmgr_info_cxt(cmpFunc, &extra->flinfo, ssup->ssup_cxt);
77 
78  /* We can initialize the callinfo just once and re-use it */
79  InitFunctionCallInfoData(extra->fcinfo, &extra->flinfo, 2,
80  ssup->ssup_collation, NULL, NULL);
81  extra->fcinfo.args[0].isnull = false;
82  extra->fcinfo.args[1].isnull = false;
83 
84  ssup->ssup_extra = extra;
86 }
FunctionCallInfoBaseData fcinfo
Definition: sortsupport.c:30
#define SizeForSortShimExtra(nargs)
Definition: sortsupport.c:33
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
MemoryContext ssup_cxt
Definition: sortsupport.h:66
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
void * ssup_extra
Definition: sortsupport.h:87
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:134
static int comparison_shim(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.c:43
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
FmgrInfo flinfo
Definition: sortsupport.c:29
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:771

◆ PrepareSortSupportFromIndexRel()

void PrepareSortSupportFromIndexRel ( Relation  indexRel,
int16  strategy,
SortSupport  ssup 
)

Definition at line 161 of file sortsupport.c.

References Assert, BTGreaterStrategyNumber, BTLessStrategyNumber, SortSupportData::comparator, elog, ERROR, FinishSortSupportFunction(), RelationData::rd_opcintype, RelationData::rd_opfamily, RelationData::rd_rel, SortSupportData::ssup_attno, and SortSupportData::ssup_reverse.

Referenced by _bt_load(), ApplySortAbbrevFullComparator(), tuplesort_begin_cluster(), and tuplesort_begin_index_btree().

163 {
164  Oid opfamily = indexRel->rd_opfamily[ssup->ssup_attno - 1];
165  Oid opcintype = indexRel->rd_opcintype[ssup->ssup_attno - 1];
166 
167  Assert(ssup->comparator == NULL);
168 
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);
175 
176  FinishSortSupportFunction(opfamily, opcintype, ssup);
177 }
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
Form_pg_class rd_rel
Definition: rel.h:83
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
Oid * rd_opfamily
Definition: rel.h:158
static void FinishSortSupportFunction(Oid opfamily, Oid opcintype, SortSupport ssup)
Definition: sortsupport.c:94
AttrNumber ssup_attno
Definition: sortsupport.h:81
#define Assert(condition)
Definition: c.h:732
#define elog(elevel,...)
Definition: elog.h:226
Oid * rd_opcintype
Definition: rel.h:159
#define BTLessStrategyNumber
Definition: stratnum.h:29

◆ PrepareSortSupportFromOrderingOp()

void PrepareSortSupportFromOrderingOp ( Oid  orderingOp,
SortSupport  ssup 
)

Definition at line 134 of file sortsupport.c.

References Assert, BTGreaterStrategyNumber, SortSupportData::comparator, elog, ERROR, FinishSortSupportFunction(), get_ordering_op_properties(), and SortSupportData::ssup_reverse.

Referenced by ApplySortAbbrevFullComparator(), compute_scalar_stats(), ExecInitGatherMerge(), ExecInitIndexScan(), ExecInitMergeAppend(), multi_sort_add_dimension(), statext_mcv_serialize(), tuplesort_begin_datum(), and tuplesort_begin_heap().

135 {
136  Oid opfamily;
137  Oid opcintype;
138  int16 strategy;
139 
140  Assert(ssup->comparator == NULL);
141 
142  /* Find the operator in pg_amop */
143  if (!get_ordering_op_properties(orderingOp, &opfamily, &opcintype,
144  &strategy))
145  elog(ERROR, "operator %u is not a valid ordering operator",
146  orderingOp);
147  ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
148 
149  FinishSortSupportFunction(opfamily, opcintype, ssup);
150 }
signed short int16
Definition: c.h:345
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
static void FinishSortSupportFunction(Oid opfamily, Oid opcintype, SortSupport ssup)
Definition: sortsupport.c:94
#define Assert(condition)
Definition: c.h:732
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, int16 *strategy)
Definition: lsyscache.c:204
#define elog(elevel,...)
Definition: elog.h:226