| 1 |
/* |
| 2 |
* contrib/btree_gist/btree_int4.c |
| 3 |
*/ |
| 4 |
#include "postgres.h" |
| 5 |
#include "btree_gist.h" |
| 6 |
#include "btree_utils_num.h" |
| 7 |
#include "common/int.h" |
| 8 |
#include "utils/sortsupport.h" |
| 9 |
|
| 10 |
typedef struct int32key |
| 11 |
{ |
| 12 |
int32 lower; |
| 13 |
int32 upper; |
| 14 |
} int32KEY; |
| 15 |
|
| 16 |
/* GiST support functions */ |
| 17 |
PG_FUNCTION_INFO_V1(gbt_int4_compress); |
| 18 |
PG_FUNCTION_INFO_V1(gbt_int4_fetch); |
| 19 |
PG_FUNCTION_INFO_V1(gbt_int4_union); |
| 20 |
PG_FUNCTION_INFO_V1(gbt_int4_picksplit); |
| 21 |
PG_FUNCTION_INFO_V1(gbt_int4_consistent); |
| 22 |
PG_FUNCTION_INFO_V1(gbt_int4_distance); |
| 23 |
PG_FUNCTION_INFO_V1(gbt_int4_penalty); |
| 24 |
PG_FUNCTION_INFO_V1(gbt_int4_same); |
| 25 |
PG_FUNCTION_INFO_V1(gbt_int4_sortsupport); |
| 26 |
|
| 27 |
static bool |
| 28 |
gbt_int4gt(const void *a, const void *b, FmgrInfo *flinfo) |
| 29 |
{ |
| 30 |
return (*((const int32 *) a) > *((const int32 *) b)); |
| 31 |
} |
| 32 |
static bool |
| 33 |
gbt_int4ge(const void *a, const void *b, FmgrInfo *flinfo) |
| 34 |
{ |
| 35 |
return (*((const int32 *) a) >= *((const int32 *) b)); |
| 36 |
} |
| 37 |
static bool |
| 38 |
gbt_int4eq(const void *a, const void *b, FmgrInfo *flinfo) |
| 39 |
{ |
| 40 |
return (*((const int32 *) a) == *((const int32 *) b)); |
| 41 |
} |
| 42 |
static bool |
| 43 |
gbt_int4le(const void *a, const void *b, FmgrInfo *flinfo) |
| 44 |
{ |
| 45 |
return (*((const int32 *) a) <= *((const int32 *) b)); |
| 46 |
} |
| 47 |
static bool |
| 48 |
gbt_int4lt(const void *a, const void *b, FmgrInfo *flinfo) |
| 49 |
{ |
| 50 |
return (*((const int32 *) a) < *((const int32 *) b)); |
| 51 |
} |
| 52 |
|
| 53 |
static int |
| 54 |
gbt_int4key_cmp(const void *a, const void *b, FmgrInfo *flinfo) |
| 55 |
{ |
| 56 |
int32KEY *ia = (int32KEY *) (((const Nsrt *) a)->t); |
| 57 |
int32KEY *ib = (int32KEY *) (((const Nsrt *) b)->t); |
| 58 |
|
| 59 |
if (ia->lower == ib->lower) |
| 60 |
{ |
| 61 |
if (ia->upper == ib->upper) |
| 62 |
return 0; |
| 63 |
|
| 64 |
return (ia->upper > ib->upper) ? 1 : -1; |
| 65 |
} |
| 66 |
|
| 67 |
return (ia->lower > ib->lower) ? 1 : -1; |
| 68 |
} |
| 69 |
|
| 70 |
static float8 |
| 71 |
gbt_int4_dist(const void *a, const void *b, FmgrInfo *flinfo) |
| 72 |
{ |
| 73 |
return GET_FLOAT_DISTANCE(int32, a, b); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
static const gbtree_ninfo tinfo = |
| 78 |
{ |
| 79 |
gbt_t_int4, |
| 80 |
sizeof(int32), |
| 81 |
8, /* sizeof(gbtreekey8) */ |
| 82 |
gbt_int4gt, |
| 83 |
gbt_int4ge, |
| 84 |
gbt_int4eq, |
| 85 |
gbt_int4le, |
| 86 |
gbt_int4lt, |
| 87 |
gbt_int4key_cmp, |
| 88 |
gbt_int4_dist |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
PG_FUNCTION_INFO_V1(int4_dist); |
| 93 |
Datum |
| 94 |
int4_dist(PG_FUNCTION_ARGS) |
| 95 |
{ |
| 96 |
int32 a = PG_GETARG_INT32(0); |
| 97 |
int32 b = PG_GETARG_INT32(1); |
| 98 |
int32 r; |
| 99 |
int32 ra; |
| 100 |
|
| 101 |
if (pg_sub_s32_overflow(a, b, &r) || |
| 102 |
r == PG_INT32_MIN) |
| 103 |
ereport(ERROR, |
| 104 |
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), |
| 105 |
errmsg("integer out of range"))); |
| 106 |
|
| 107 |
ra = abs(r); |
| 108 |
|
| 109 |
PG_RETURN_INT32(ra); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
/************************************************** |
| 114 |
* GiST support functions |
| 115 |
**************************************************/ |
| 116 |
|
| 117 |
Datum |
| 118 |
gbt_int4_compress(PG_FUNCTION_ARGS) |
| 119 |
{ |
| 120 |
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); |
| 121 |
|
| 122 |
PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo)); |
| 123 |
} |
| 124 |
|
| 125 |
Datum |
| 126 |
gbt_int4_fetch(PG_FUNCTION_ARGS) |
| 127 |
{ |
| 128 |
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); |
| 129 |
|
| 130 |
PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo)); |
| 131 |
} |
| 132 |
|
| 133 |
Datum |
| 134 |
gbt_int4_consistent(PG_FUNCTION_ARGS) |
| 135 |
{ |
| 136 |
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); |
| 137 |
int32 query = PG_GETARG_INT32(1); |
| 138 |
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); |
| 139 |
|
| 140 |
/* Oid subtype = PG_GETARG_OID(3); */ |
| 141 |
bool *recheck = (bool *) PG_GETARG_POINTER(4); |
| 142 |
int32KEY *kkk = (int32KEY *) DatumGetPointer(entry->key); |
| 143 |
GBT_NUMKEY_R key; |
| 144 |
|
| 145 |
/* All cases served by this function are exact */ |
| 146 |
*recheck = false; |
| 147 |
|
| 148 |
key.lower = (GBT_NUMKEY *) &kkk->lower; |
| 149 |
key.upper = (GBT_NUMKEY *) &kkk->upper; |
| 150 |
|
| 151 |
PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy, |
| 152 |
GIST_LEAF(entry), &tinfo, fcinfo->flinfo)); |
| 153 |
} |
| 154 |
|
| 155 |
Datum |
| 156 |
gbt_int4_distance(PG_FUNCTION_ARGS) |
| 157 |
{ |
| 158 |
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); |
| 159 |
int32 query = PG_GETARG_INT32(1); |
| 160 |
|
| 161 |
/* Oid subtype = PG_GETARG_OID(3); */ |
| 162 |
int32KEY *kkk = (int32KEY *) DatumGetPointer(entry->key); |
| 163 |
GBT_NUMKEY_R key; |
| 164 |
|
| 165 |
key.lower = (GBT_NUMKEY *) &kkk->lower; |
| 166 |
key.upper = (GBT_NUMKEY *) &kkk->upper; |
| 167 |
|
| 168 |
PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry), |
| 169 |
&tinfo, fcinfo->flinfo)); |
| 170 |
} |
| 171 |
|
| 172 |
Datum |
| 173 |
gbt_int4_union(PG_FUNCTION_ARGS) |
| 174 |
{ |
| 175 |
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0); |
| 176 |
void *out = palloc(sizeof(int32KEY)); |
| 177 |
|
| 178 |
*(int *) PG_GETARG_POINTER(1) = sizeof(int32KEY); |
| 179 |
PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo)); |
| 180 |
} |
| 181 |
|
| 182 |
Datum |
| 183 |
gbt_int4_penalty(PG_FUNCTION_ARGS) |
| 184 |
{ |
| 185 |
int32KEY *origentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key); |
| 186 |
int32KEY *newentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key); |
| 187 |
float *result = (float *) PG_GETARG_POINTER(2); |
| 188 |
|
| 189 |
penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper); |
| 190 |
|
| 191 |
PG_RETURN_POINTER(result); |
| 192 |
} |
| 193 |
|
| 194 |
Datum |
| 195 |
gbt_int4_picksplit(PG_FUNCTION_ARGS) |
| 196 |
{ |
| 197 |
PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0), |
| 198 |
(GIST_SPLITVEC *) PG_GETARG_POINTER(1), |
| 199 |
&tinfo, fcinfo->flinfo)); |
| 200 |
} |
| 201 |
|
| 202 |
Datum |
| 203 |
gbt_int4_same(PG_FUNCTION_ARGS) |
| 204 |
{ |
| 205 |
int32KEY *b1 = (int32KEY *) PG_GETARG_POINTER(0); |
| 206 |
int32KEY *b2 = (int32KEY *) PG_GETARG_POINTER(1); |
| 207 |
bool *result = (bool *) PG_GETARG_POINTER(2); |
| 208 |
|
| 209 |
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo); |
| 210 |
PG_RETURN_POINTER(result); |
| 211 |
} |
| 212 |
|
| 213 |
static int |
| 214 |
gbt_int4_ssup_cmp(Datum a, Datum b, SortSupport ssup) |
| 215 |
{ |
| 216 |
int32KEY *ia = (int32KEY *) DatumGetPointer(a); |
| 217 |
int32KEY *ib = (int32KEY *) DatumGetPointer(b); |
| 218 |
|
| 219 |
/* for leaf items we expect lower == upper, so only compare lower */ |
| 220 |
if (ia->lower < ib->lower) |
| 221 |
return -1; |
| 222 |
else if (ia->lower > ib->lower) |
| 223 |
return 1; |
| 224 |
else |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
Datum |
| 229 |
gbt_int4_sortsupport(PG_FUNCTION_ARGS) |
| 230 |
{ |
| 231 |
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0); |
| 232 |
|
| 233 |
ssup->comparator = gbt_int4_ssup_cmp; |
| 234 |
PG_RETURN_VOID(); |
| 235 |
} |