PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

Definition at line 58 of file sortsupport.h.

Function Documentation

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

Definition at line 239 of file sortsupport.h.

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

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

242 {
243  int compare;
244 
245  if (isNull1)
246  {
247  if (isNull2)
248  compare = 0; /* NULL "=" NULL */
249  else if (ssup->ssup_nulls_first)
250  compare = -1; /* NULL "<" NOT_NULL */
251  else
252  compare = 1; /* NULL ">" NOT_NULL */
253  }
254  else if (isNull2)
255  {
256  if (ssup->ssup_nulls_first)
257  compare = 1; /* NOT_NULL ">" NULL */
258  else
259  compare = -1; /* NOT_NULL "<" NULL */
260  }
261  else
262  {
263  compare = ssup->abbrev_full_comparator(datum1, datum2, ssup);
264  if (ssup->ssup_reverse)
265  compare = -compare;
266  }
267 
268  return compare;
269 }
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:192
static int ApplySortComparator ( Datum  datum1,
bool  isNull1,
Datum  datum2,
bool  isNull2,
SortSupport  ssup 
)
inlinestatic

Definition at line 201 of file sortsupport.h.

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

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

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

Definition at line 67 of file sortsupport.c.

References FunctionCallInfoData::argnull, SortSupportData::comparator, comparison_shim(), SortShimExtra::fcinfo, SortShimExtra::flinfo, fmgr_info_cxt(), InitFunctionCallInfoData, MemoryContextAlloc(), SortSupportData::ssup_collation, SortSupportData::ssup_cxt, and SortSupportData::ssup_extra.

Referenced by FinishSortSupportFunction(), and MJExamineQuals().

68 {
69  SortShimExtra *extra;
70 
71  extra = (SortShimExtra *) MemoryContextAlloc(ssup->ssup_cxt,
72  sizeof(SortShimExtra));
73 
74  /* Lookup the comparison function */
75  fmgr_info_cxt(cmpFunc, &extra->flinfo, ssup->ssup_cxt);
76 
77  /* We can initialize the callinfo just once and re-use it */
78  InitFunctionCallInfoData(extra->fcinfo, &extra->flinfo, 2,
79  ssup->ssup_collation, NULL, NULL);
80  extra->fcinfo.argnull[0] = false;
81  extra->fcinfo.argnull[1] = false;
82 
83  ssup->ssup_extra = extra;
85 }
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:107
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void * ssup_extra
Definition: sortsupport.h:87
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:86
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:132
static int comparison_shim(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.c:42
FunctionCallInfoData fcinfo
Definition: sortsupport.c:29
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:120
FmgrInfo flinfo
Definition: sortsupport.c:30
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
void PrepareSortSupportFromIndexRel ( Relation  indexRel,
int16  strategy,
SortSupport  ssup 
)

Definition at line 160 of file sortsupport.c.

References Assert, BTGreaterStrategyNumber, BTLessStrategyNumber, BTREE_AM_OID, 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().

162 {
163  Oid opfamily = indexRel->rd_opfamily[ssup->ssup_attno - 1];
164  Oid opcintype = indexRel->rd_opcintype[ssup->ssup_attno - 1];
165 
166  Assert(ssup->comparator == NULL);
167 
168  if (indexRel->rd_rel->relam != BTREE_AM_OID)
169  elog(ERROR, "unexpected non-btree AM: %u", indexRel->rd_rel->relam);
170  if (strategy != BTGreaterStrategyNumber &&
171  strategy != BTLessStrategyNumber)
172  elog(ERROR, "unexpected sort support strategy: %d", strategy);
173  ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
174 
175  FinishSortSupportFunction(opfamily, opcintype, ssup);
176 }
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:107
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTREE_AM_OID
Definition: pg_am.h:70
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
Oid * rd_opfamily
Definition: rel.h:182
static void FinishSortSupportFunction(Oid opfamily, Oid opcintype, SortSupport ssup)
Definition: sortsupport.c:93
AttrNumber ssup_attno
Definition: sortsupport.h:81
#define Assert(condition)
Definition: c.h:681
#define elog
Definition: elog.h:219
Oid * rd_opcintype
Definition: rel.h:183
#define BTLessStrategyNumber
Definition: stratnum.h:29
void PrepareSortSupportFromOrderingOp ( Oid  orderingOp,
SortSupport  ssup 
)

Definition at line 133 of file sortsupport.c.

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(), tuplesort_begin_datum(), and tuplesort_begin_heap().

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