00001 /*------------------------------------------------------------------------- 00002 * 00003 * sortsupport.h 00004 * Framework for accelerated sorting. 00005 * 00006 * Traditionally, PostgreSQL has implemented sorting by repeatedly invoking 00007 * an SQL-callable comparison function "cmp(x, y) returns int" on pairs of 00008 * values to be compared, where the comparison function is the BTORDER_PROC 00009 * pg_amproc support function of the appropriate btree index opclass. 00010 * 00011 * This file defines alternative APIs that allow sorting to be performed with 00012 * reduced overhead. To support lower-overhead sorting, a btree opclass may 00013 * provide a BTSORTSUPPORT_PROC pg_amproc entry, which must take a single 00014 * argument of type internal and return void. The argument is actually a 00015 * pointer to a SortSupportData struct, which is defined below. 00016 * 00017 * If provided, the BTSORTSUPPORT function will be called during sort setup, 00018 * and it must initialize the provided struct with pointers to function(s) 00019 * that can be called to perform sorting. This API is defined to allow 00020 * multiple acceleration mechanisms to be supported, but no opclass is 00021 * required to provide all of them. The BTSORTSUPPORT function should 00022 * simply not set any function pointers for mechanisms it doesn't support. 00023 * (However, all opclasses that provide BTSORTSUPPORT are required to provide 00024 * the comparator function.) 00025 * 00026 * All sort support functions will be passed the address of the 00027 * SortSupportData struct when called, so they can use it to store 00028 * additional private data as needed. In particular, for collation-aware 00029 * datatypes, the ssup_collation field is set before calling BTSORTSUPPORT 00030 * and is available to all support functions. Additional opclass-dependent 00031 * data can be stored using the ssup_extra field. Any such data 00032 * should be allocated in the ssup_cxt memory context. 00033 * 00034 * Note: since pg_amproc functions are indexed by (lefttype, righttype) 00035 * it is possible to associate a BTSORTSUPPORT function with a cross-type 00036 * comparison. This could sensibly be used to provide a fast comparator 00037 * function for such cases, but probably not any other acceleration method. 00038 * 00039 * 00040 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group 00041 * Portions Copyright (c) 1994, Regents of the University of California 00042 * 00043 * src/include/utils/sortsupport.h 00044 * 00045 *------------------------------------------------------------------------- 00046 */ 00047 #ifndef SORTSUPPORT_H 00048 #define SORTSUPPORT_H 00049 00050 #include "access/attnum.h" 00051 00052 typedef struct SortSupportData *SortSupport; 00053 00054 typedef struct SortSupportData 00055 { 00056 /* 00057 * These fields are initialized before calling the BTSORTSUPPORT function 00058 * and should not be changed later. 00059 */ 00060 MemoryContext ssup_cxt; /* Context containing sort info */ 00061 Oid ssup_collation; /* Collation to use, or InvalidOid */ 00062 00063 /* 00064 * Additional sorting parameters; but unlike ssup_collation, these can be 00065 * changed after BTSORTSUPPORT is called, so don't use them in selecting 00066 * sort support functions. 00067 */ 00068 bool ssup_reverse; /* descending-order sort? */ 00069 bool ssup_nulls_first; /* sort nulls first? */ 00070 00071 /* 00072 * These fields are workspace for callers, and should not be touched by 00073 * opclass-specific functions. 00074 */ 00075 AttrNumber ssup_attno; /* column number to sort */ 00076 00077 /* 00078 * ssup_extra is zeroed before calling the BTSORTSUPPORT function, and is 00079 * not touched subsequently by callers. 00080 */ 00081 void *ssup_extra; /* Workspace for opclass functions */ 00082 00083 /* 00084 * Function pointers are zeroed before calling the BTSORTSUPPORT function, 00085 * and must be set by it for any acceleration methods it wants to supply. 00086 * The comparator pointer must be set, others are optional. 00087 */ 00088 00089 /* 00090 * Comparator function has the same API as the traditional btree 00091 * comparison function, ie, return <0, 0, or >0 according as x is less 00092 * than, equal to, or greater than y. Note that x and y are guaranteed 00093 * not null, and there is no way to return null either. Do not return 00094 * INT_MIN, as callers are allowed to negate the result before using it. 00095 */ 00096 int (*comparator) (Datum x, Datum y, SortSupport ssup); 00097 00098 /* 00099 * Additional sort-acceleration functions might be added here later. 00100 */ 00101 } SortSupportData; 00102 00103 00104 /* 00105 * ApplySortComparator should be inlined if possible. See STATIC_IF_INLINE 00106 * in c.h. 00107 */ 00108 #ifndef PG_USE_INLINE 00109 extern int ApplySortComparator(Datum datum1, bool isNull1, 00110 Datum datum2, bool isNull2, 00111 SortSupport ssup); 00112 #endif /* !PG_USE_INLINE */ 00113 #if defined(PG_USE_INLINE) || defined(SORTSUPPORT_INCLUDE_DEFINITIONS) 00114 /* 00115 * Apply a sort comparator function and return a 3-way comparison result. 00116 * This takes care of handling reverse-sort and NULLs-ordering properly. 00117 */ 00118 STATIC_IF_INLINE int 00119 ApplySortComparator(Datum datum1, bool isNull1, 00120 Datum datum2, bool isNull2, 00121 SortSupport ssup) 00122 { 00123 int compare; 00124 00125 if (isNull1) 00126 { 00127 if (isNull2) 00128 compare = 0; /* NULL "=" NULL */ 00129 else if (ssup->ssup_nulls_first) 00130 compare = -1; /* NULL "<" NOT_NULL */ 00131 else 00132 compare = 1; /* NULL ">" NOT_NULL */ 00133 } 00134 else if (isNull2) 00135 { 00136 if (ssup->ssup_nulls_first) 00137 compare = 1; /* NOT_NULL ">" NULL */ 00138 else 00139 compare = -1; /* NOT_NULL "<" NULL */ 00140 } 00141 else 00142 { 00143 compare = (*ssup->comparator) (datum1, datum2, ssup); 00144 if (ssup->ssup_reverse) 00145 compare = -compare; 00146 } 00147 00148 return compare; 00149 } 00150 #endif /*-- PG_USE_INLINE || SORTSUPPORT_INCLUDE_DEFINITIONS */ 00151 00152 /* Other functions in utils/sort/sortsupport.c */ 00153 extern void PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup); 00154 extern void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup); 00155 00156 #endif /* SORTSUPPORT_H */