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 ApplyUnsignedSortComparator (Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
 
static int ApplyInt32SortComparator (Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
 
static int ApplySortAbbrevFullComparator (Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
 
int ssup_datum_unsigned_cmp (Datum x, Datum y, SortSupport ssup)
 
int ssup_datum_int32_cmp (Datum x, Datum y, SortSupport ssup)
 
void PrepareSortSupportComparisonShim (Oid cmpFunc, SortSupport ssup)
 
void PrepareSortSupportFromOrderingOp (Oid orderingOp, SortSupport ssup)
 
void PrepareSortSupportFromIndexRel (Relation indexRel, int16 strategy, SortSupport ssup)
 
void PrepareSortSupportFromGistIndexRel (Relation indexRel, SortSupport ssup)
 

Typedef Documentation

◆ SortSupport

typedef struct SortSupportData* SortSupport

Definition at line 58 of file sortsupport.h.

◆ SortSupportData

Function Documentation

◆ ApplyInt32SortComparator()

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

Definition at line 302 of file sortsupport.h.

305 {
306  int compare;
307 
308  if (isNull1)
309  {
310  if (isNull2)
311  compare = 0; /* NULL "=" NULL */
312  else if (ssup->ssup_nulls_first)
313  compare = -1; /* NULL "<" NOT_NULL */
314  else
315  compare = 1; /* NULL ">" NOT_NULL */
316  }
317  else if (isNull2)
318  {
319  if (ssup->ssup_nulls_first)
320  compare = 1; /* NOT_NULL ">" NULL */
321  else
322  compare = -1; /* NOT_NULL "<" NULL */
323  }
324  else
325  {
326  compare = DatumGetInt32(datum1) < DatumGetInt32(datum2) ? -1 :
327  DatumGetInt32(datum1) > DatumGetInt32(datum2) ? 1 : 0;
328  if (ssup->ssup_reverse)
330  }
331 
332  return compare;
333 }
#define INVERT_COMPARE_RESULT(var)
Definition: c.h:1063
static int compare(const void *arg1, const void *arg2)
Definition: geqo_pool.c:145
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:550
bool ssup_nulls_first
Definition: sortsupport.h:75

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

Referenced by qsort_tuple_int32_compare().

◆ ApplySortAbbrevFullComparator()

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

Definition at line 341 of file sortsupport.h.

344 {
345  int compare;
346 
347  if (isNull1)
348  {
349  if (isNull2)
350  compare = 0; /* NULL "=" NULL */
351  else if (ssup->ssup_nulls_first)
352  compare = -1; /* NULL "<" NOT_NULL */
353  else
354  compare = 1; /* NULL ">" NOT_NULL */
355  }
356  else if (isNull2)
357  {
358  if (ssup->ssup_nulls_first)
359  compare = 1; /* NOT_NULL ">" NULL */
360  else
361  compare = -1; /* NOT_NULL "<" NULL */
362  }
363  else
364  {
365  compare = ssup->abbrev_full_comparator(datum1, datum2, ssup);
366  if (ssup->ssup_reverse)
368  }
369 
370  return compare;
371 }
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:191

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

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

◆ ApplySortComparator()

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

Definition at line 200 of file sortsupport.h.

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)
227  }
228 
229  return compare;
230 }
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106

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().

◆ ApplyUnsignedSortComparator()

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

Definition at line 233 of file sortsupport.h.

236 {
237  int compare;
238 
239  if (isNull1)
240  {
241  if (isNull2)
242  compare = 0; /* NULL "=" NULL */
243  else if (ssup->ssup_nulls_first)
244  compare = -1; /* NULL "<" NOT_NULL */
245  else
246  compare = 1; /* NULL ">" NOT_NULL */
247  }
248  else if (isNull2)
249  {
250  if (ssup->ssup_nulls_first)
251  compare = 1; /* NOT_NULL ">" NULL */
252  else
253  compare = -1; /* NOT_NULL "<" NULL */
254  }
255  else
256  {
257  compare = datum1 < datum2 ? -1 : datum1 > datum2 ? 1 : 0;
258  if (ssup->ssup_reverse)
260  }
261 
262  return compare;
263 }

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

Referenced by qsort_tuple_unsigned_compare().

◆ PrepareSortSupportComparisonShim()

void PrepareSortSupportComparisonShim ( Oid  cmpFunc,
SortSupport  ssup 
)

Definition at line 69 of file sortsupport.c.

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

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 FinishSortSupportFunction(), and MJExamineQuals().

◆ PrepareSortSupportFromGistIndexRel()

void PrepareSortSupportFromGistIndexRel ( Relation  indexRel,
SortSupport  ssup 
)

Definition at line 189 of file sortsupport.c.

190 {
191  Oid opfamily = indexRel->rd_opfamily[ssup->ssup_attno - 1];
192  Oid opcintype = indexRel->rd_opcintype[ssup->ssup_attno - 1];
193  Oid sortSupportFunction;
194 
195  Assert(ssup->comparator == NULL);
196 
197  if (indexRel->rd_rel->relam != GIST_AM_OID)
198  elog(ERROR, "unexpected non-gist AM: %u", indexRel->rd_rel->relam);
199  ssup->ssup_reverse = false;
200 
201  /*
202  * Look up the sort support function. This is simpler than for B-tree
203  * indexes because we don't support the old-style btree comparators.
204  */
205  sortSupportFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
207  if (!OidIsValid(sortSupportFunction))
208  elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
209  GIST_SORTSUPPORT_PROC, opcintype, opcintype, opfamily);
210  OidFunctionCall1(sortSupportFunction, PointerGetDatum(ssup));
211 }
#define OidIsValid(objectId)
Definition: c.h:711
#define ERROR
Definition: elog.h:35
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:680
#define GIST_SORTSUPPORT_PROC
Definition: gist.h:40
Assert(fmt[strlen(fmt) - 1] !='\n')
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:795
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
unsigned int Oid
Definition: postgres_ext.h:31
Oid * rd_opcintype
Definition: rel.h:204
Oid * rd_opfamily
Definition: rel.h:203
Form_pg_class rd_rel
Definition: rel.h:110
AttrNumber ssup_attno
Definition: sortsupport.h:81

References Assert(), SortSupportData::comparator, elog(), ERROR, get_opfamily_proc(), GIST_SORTSUPPORT_PROC, OidFunctionCall1, OidIsValid, PointerGetDatum(), RelationData::rd_opcintype, RelationData::rd_opfamily, RelationData::rd_rel, SortSupportData::ssup_attno, and SortSupportData::ssup_reverse.

Referenced by tuplesort_begin_index_gist().

◆ PrepareSortSupportFromIndexRel()

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

Definition at line 162 of file sortsupport.c.

164 {
165  Oid opfamily = indexRel->rd_opfamily[ssup->ssup_attno - 1];
166  Oid opcintype = indexRel->rd_opcintype[ssup->ssup_attno - 1];
167 
168  Assert(ssup->comparator == NULL);
169 
170  if (indexRel->rd_rel->relam != BTREE_AM_OID)
171  elog(ERROR, "unexpected non-btree AM: %u", indexRel->rd_rel->relam);
172  if (strategy != BTGreaterStrategyNumber &&
173  strategy != BTLessStrategyNumber)
174  elog(ERROR, "unexpected sort support strategy: %d", strategy);
175  ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
176 
177  FinishSortSupportFunction(opfamily, opcintype, ssup);
178 }
static void FinishSortSupportFunction(Oid opfamily, Oid opcintype, SortSupport ssup)
Definition: sortsupport.c:95
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessStrategyNumber
Definition: stratnum.h:29

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(), tuplesort_begin_cluster(), and tuplesort_begin_index_btree().

◆ PrepareSortSupportFromOrderingOp()

void PrepareSortSupportFromOrderingOp ( Oid  orderingOp,
SortSupport  ssup 
)

Definition at line 135 of file sortsupport.c.

136 {
137  Oid opfamily;
138  Oid opcintype;
139  int16 strategy;
140 
141  Assert(ssup->comparator == NULL);
142 
143  /* Find the operator in pg_amop */
144  if (!get_ordering_op_properties(orderingOp, &opfamily, &opcintype,
145  &strategy))
146  elog(ERROR, "operator %u is not a valid ordering operator",
147  orderingOp);
148  ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
149 
150  FinishSortSupportFunction(opfamily, opcintype, ssup);
151 }
signed short int16
Definition: c.h:429
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, int16 *strategy)
Definition: lsyscache.c:206

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

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

◆ ssup_datum_int32_cmp()

int ssup_datum_int32_cmp ( Datum  x,
Datum  y,
SortSupport  ssup 
)

Definition at line 3204 of file tuplesort.c.

3205 {
3206  int32 xx = DatumGetInt32(x);
3207  int32 yy = DatumGetInt32(y);
3208 
3209  if (xx < yy)
3210  return -1;
3211  else if (xx > yy)
3212  return 1;
3213  else
3214  return 0;
3215 }
signed int int32
Definition: c.h:430
int y
Definition: isn.c:72
int x
Definition: isn.c:71

References DatumGetInt32(), x, and y.

Referenced by btint4sortsupport(), date_sortsupport(), and tuplesort_sort_memtuples().

◆ ssup_datum_unsigned_cmp()

int ssup_datum_unsigned_cmp ( Datum  x,
Datum  y,
SortSupport  ssup 
)

Definition at line 3177 of file tuplesort.c.

3178 {
3179  if (x < y)
3180  return -1;
3181  else if (x > y)
3182  return 1;
3183  else
3184  return 0;
3185 }

References x, and y.

Referenced by gist_point_sortsupport(), macaddr_sortsupport(), network_sortsupport(), tuplesort_sort_memtuples(), uuid_sortsupport(), and varstr_sortsupport().