| 1 |
/* |
| 2 |
* contrib/btree_gist/btree_float8.c |
| 3 |
*/ |
| 4 |
#include "postgres.h" |
| 5 |
|
| 6 |
#include "btree_gist.h" |
| 7 |
#include "btree_utils_num.h" |
| 8 |
#include "utils/float.h" |
| 9 |
#include "utils/sortsupport.h" |
| 10 |
|
| 11 |
typedef struct float8key |
| 12 |
{ |
| 13 |
float8 lower; |
| 14 |
float8 upper; |
| 15 |
} float8KEY; |
| 16 |
|
| 17 |
/* GiST support functions */ |
| 18 |
PG_FUNCTION_INFO_V1(gbt_float8_compress); |
| 19 |
PG_FUNCTION_INFO_V1(gbt_float8_fetch); |
| 20 |
PG_FUNCTION_INFO_V1(gbt_float8_union); |
| 21 |
PG_FUNCTION_INFO_V1(gbt_float8_picksplit); |
| 22 |
PG_FUNCTION_INFO_V1(gbt_float8_consistent); |
| 23 |
PG_FUNCTION_INFO_V1(gbt_float8_distance); |
| 24 |
PG_FUNCTION_INFO_V1(gbt_float8_penalty); |
| 25 |
PG_FUNCTION_INFO_V1(gbt_float8_same); |
| 26 |
PG_FUNCTION_INFO_V1(gbt_float8_sortsupport); |
| 27 |
|
| 28 |
|
| 29 |
/* |
| 30 |
* Use the NaN-aware comparators from utils/float.h, so that our results |
| 31 |
* will agree with standard btree indexes. Note that penalty and distance |
| 32 |
* functions below must also cope with NaNs, in particular with the policy |
| 33 |
* that all NaNs are equal. |
| 34 |
*/ |
| 35 |
static bool |
| 36 |
gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo) |
| 37 |
{ |
| 38 |
return float8_gt(*((const float8 *) a), *((const float8 *) b)); |
| 39 |
} |
| 40 |
static bool |
| 41 |
gbt_float8ge(const void *a, const void *b, FmgrInfo *flinfo) |
| 42 |
{ |
| 43 |
return float8_ge(*((const float8 *) a), *((const float8 *) b)); |
| 44 |
} |
| 45 |
static bool |
| 46 |
gbt_float8eq(const void *a, const void *b, FmgrInfo *flinfo) |
| 47 |
{ |
| 48 |
return float8_eq(*((const float8 *) a), *((const float8 *) b)); |
| 49 |
} |
| 50 |
static bool |
| 51 |
gbt_float8le(const void *a, const void *b, FmgrInfo *flinfo) |
| 52 |
{ |
| 53 |
return float8_le(*((const float8 *) a), *((const float8 *) b)); |
| 54 |
} |
| 55 |
static bool |
| 56 |
gbt_float8lt(const void *a, const void *b, FmgrInfo *flinfo) |
| 57 |
{ |
| 58 |
return float8_lt(*((const float8 *) a), *((const float8 *) b)); |
| 59 |
} |
| 60 |
|
| 61 |
static int |
| 62 |
gbt_float8key_cmp(const void *a, const void *b, FmgrInfo *flinfo) |
| 63 |
{ |
| 64 |
float8KEY *ia = (float8KEY *) (((const Nsrt *) a)->t); |
| 65 |
float8KEY *ib = (float8KEY *) (((const Nsrt *) b)->t); |
| 66 |
int res; |
| 67 |
|
| 68 |
res = float8_cmp_internal(ia->lower, ib->lower); |
| 69 |
if (res != 0) |
| 70 |
return res; |
| 71 |
return float8_cmp_internal(ia->upper, ib->upper); |
| 72 |
} |
| 73 |
|
| 74 |
static float8 |
| 75 |
gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo) |
| 76 |
{ |
| 77 |
float8 arg1 = *(const float8 *) a; |
| 78 |
float8 arg2 = *(const float8 *) b; |
| 79 |
float8 r; |
| 80 |
|
| 81 |
r = arg1 - arg2; |
| 82 |
if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2)) |
| 83 |
float_overflow_error(); |
| 84 |
if (unlikely(isnan(r))) |
| 85 |
{ |
| 86 |
if (isnan(arg1) && isnan(arg2)) |
| 87 |
r = 0.0; /* treat NaNs as equal */ |
| 88 |
else if (isnan(arg1) || isnan(arg2)) |
| 89 |
r = get_float8_infinity(); /* max dist for NaN vs non-NaN */ |
| 90 |
else |
| 91 |
r = 0.0; /* must be Inf - Inf case */ |
| 92 |
} |
| 93 |
return fabs(r); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
static const gbtree_ninfo tinfo = |
| 98 |
{ |
| 99 |
gbt_t_float8, |
| 100 |
sizeof(float8), |
| 101 |
16, /* sizeof(gbtreekey16) */ |
| 102 |
gbt_float8gt, |
| 103 |
gbt_float8ge, |
| 104 |
gbt_float8eq, |
| 105 |
gbt_float8le, |
| 106 |
gbt_float8lt, |
| 107 |
gbt_float8key_cmp, |
| 108 |
gbt_float8_dist |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
PG_FUNCTION_INFO_V1(float8_dist); |
| 113 |
Datum |
| 114 |
float8_dist(PG_FUNCTION_ARGS) |
| 115 |
{ |
| 116 |
float8 a = PG_GETARG_FLOAT8(0); |
| 117 |
float8 b = PG_GETARG_FLOAT8(1); |
| 118 |
float8 r; |
| 119 |
|
| 120 |
r = a - b; |
| 121 |
if (unlikely(isinf(r)) && !isinf(a) && !isinf(b)) |
| 122 |
float_overflow_error(); |
| 123 |
if (unlikely(isnan(r))) |
| 124 |
{ |
| 125 |
if (isnan(a) && isnan(b)) |
| 126 |
r = 0.0; /* treat NaNs as equal */ |
| 127 |
else if (isnan(a) || isnan(b)) |
| 128 |
r = get_float8_infinity(); /* max dist for NaN vs non-NaN */ |
| 129 |
else |
| 130 |
r = 0.0; /* must be Inf - Inf case */ |
| 131 |
} |
| 132 |
PG_RETURN_FLOAT8(fabs(r)); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
/************************************************** |
| 137 |
* GiST support functions |
| 138 |
**************************************************/ |
| 139 |
|
| 140 |
Datum |
| 141 |
gbt_float8_compress(PG_FUNCTION_ARGS) |
| 142 |
{ |
| 143 |
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); |
| 144 |
|
| 145 |
PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo)); |
| 146 |
} |
| 147 |
|
| 148 |
Datum |
| 149 |
gbt_float8_fetch(PG_FUNCTION_ARGS) |
| 150 |
{ |
| 151 |
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); |
| 152 |
|
| 153 |
PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo)); |
| 154 |
} |
| 155 |
|
| 156 |
Datum |
| 157 |
gbt_float8_consistent(PG_FUNCTION_ARGS) |
| 158 |
{ |
| 159 |
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); |
| 160 |
float8 query = PG_GETARG_FLOAT8(1); |
| 161 |
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); |
| 162 |
|
| 163 |
/* Oid subtype = PG_GETARG_OID(3); */ |
| 164 |
bool *recheck = (bool *) PG_GETARG_POINTER(4); |
| 165 |
float8KEY *kkk = (float8KEY *) DatumGetPointer(entry->key); |
| 166 |
GBT_NUMKEY_R key; |
| 167 |
|
| 168 |
/* All cases served by this function are exact */ |
| 169 |
*recheck = false; |
| 170 |
|
| 171 |
key.lower = (GBT_NUMKEY *) &kkk->lower; |
| 172 |
key.upper = (GBT_NUMKEY *) &kkk->upper; |
| 173 |
|
| 174 |
PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy, |
| 175 |
GIST_LEAF(entry), &tinfo, |
| 176 |
fcinfo->flinfo)); |
| 177 |
} |
| 178 |
|
| 179 |
Datum |
| 180 |
gbt_float8_distance(PG_FUNCTION_ARGS) |
| 181 |
{ |
| 182 |
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); |
| 183 |
float8 query = PG_GETARG_FLOAT8(1); |
| 184 |
|
| 185 |
/* Oid subtype = PG_GETARG_OID(3); */ |
| 186 |
float8KEY *kkk = (float8KEY *) DatumGetPointer(entry->key); |
| 187 |
GBT_NUMKEY_R key; |
| 188 |
|
| 189 |
key.lower = (GBT_NUMKEY *) &kkk->lower; |
| 190 |
key.upper = (GBT_NUMKEY *) &kkk->upper; |
| 191 |
|
| 192 |
PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry), |
| 193 |
&tinfo, fcinfo->flinfo)); |
| 194 |
} |
| 195 |
|
| 196 |
Datum |
| 197 |
gbt_float8_union(PG_FUNCTION_ARGS) |
| 198 |
{ |
| 199 |
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0); |
| 200 |
void *out = palloc(sizeof(float8KEY)); |
| 201 |
|
| 202 |
*(int *) PG_GETARG_POINTER(1) = sizeof(float8KEY); |
| 203 |
PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo)); |
| 204 |
} |
| 205 |
|
| 206 |
Datum |
| 207 |
gbt_float8_penalty(PG_FUNCTION_ARGS) |
| 208 |
{ |
| 209 |
float8KEY *origentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key); |
| 210 |
float8KEY *newentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key); |
| 211 |
float *result = (float *) PG_GETARG_POINTER(2); |
| 212 |
|
| 213 |
float_penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper); |
| 214 |
|
| 215 |
PG_RETURN_POINTER(result); |
| 216 |
} |
| 217 |
|
| 218 |
Datum |
| 219 |
gbt_float8_picksplit(PG_FUNCTION_ARGS) |
| 220 |
{ |
| 221 |
PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0), |
| 222 |
(GIST_SPLITVEC *) PG_GETARG_POINTER(1), |
| 223 |
&tinfo, fcinfo->flinfo)); |
| 224 |
} |
| 225 |
|
| 226 |
Datum |
| 227 |
gbt_float8_same(PG_FUNCTION_ARGS) |
| 228 |
{ |
| 229 |
float8KEY *b1 = (float8KEY *) PG_GETARG_POINTER(0); |
| 230 |
float8KEY *b2 = (float8KEY *) PG_GETARG_POINTER(1); |
| 231 |
bool *result = (bool *) PG_GETARG_POINTER(2); |
| 232 |
|
| 233 |
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo); |
| 234 |
PG_RETURN_POINTER(result); |
| 235 |
} |
| 236 |
|
| 237 |
static int |
| 238 |
gbt_float8_ssup_cmp(Datum x, Datum y, SortSupport ssup) |
| 239 |
{ |
| 240 |
float8KEY *arg1 = (float8KEY *) DatumGetPointer(x); |
| 241 |
float8KEY *arg2 = (float8KEY *) DatumGetPointer(y); |
| 242 |
|
| 243 |
/* for leaf items we expect lower == upper, so only compare lower */ |
| 244 |
return float8_cmp_internal(arg1->lower, arg2->lower); |
| 245 |
} |
| 246 |
|
| 247 |
Datum |
| 248 |
gbt_float8_sortsupport(PG_FUNCTION_ARGS) |
| 249 |
{ |
| 250 |
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0); |
| 251 |
|
| 252 |
ssup->comparator = gbt_float8_ssup_cmp; |
| 253 |
ssup->ssup_extra = NULL; |
| 254 |
|
| 255 |
PG_RETURN_VOID(); |
| 256 |
} |