Age Owner Branch data TLA Line data Source code
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/rel.h"
10 : : #include "utils/sortsupport.h"
11 : :
12 : : typedef struct float8key
13 : : {
14 : : float8 lower;
15 : : float8 upper;
16 : : } float8KEY;
17 : :
18 : : /* GiST support functions */
8219 teodor@sigaev.ru 19 :CBC 4 : PG_FUNCTION_INFO_V1(gbt_float8_compress);
4264 heikki.linnakangas@i 20 : 4 : PG_FUNCTION_INFO_V1(gbt_float8_fetch);
8219 teodor@sigaev.ru 21 : 4 : PG_FUNCTION_INFO_V1(gbt_float8_union);
22 : 4 : PG_FUNCTION_INFO_V1(gbt_float8_picksplit);
23 : 4 : PG_FUNCTION_INFO_V1(gbt_float8_consistent);
5750 tgl@sss.pgh.pa.us 24 : 4 : PG_FUNCTION_INFO_V1(gbt_float8_distance);
8219 teodor@sigaev.ru 25 : 4 : PG_FUNCTION_INFO_V1(gbt_float8_penalty);
26 : 4 : PG_FUNCTION_INFO_V1(gbt_float8_same);
604 heikki.linnakangas@i 27 : 4 : PG_FUNCTION_INFO_V1(gbt_float8_sortsupport);
28 : :
29 : :
30 : : /*
31 : : * Use the NaN-aware comparators from utils/float.h, so that our results
32 : : * will agree with standard btree indexes. Note that penalty and distance
33 : : * functions below must also cope with NaNs, in particular with the policy
34 : : * that all NaNs are equal.
35 : : */
36 : : static bool
3539 andrew@dunslane.net 37 : 5994 : gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo)
38 : : {
150 tgl@sss.pgh.pa.us 39 : 5994 : return float8_gt(*((const float8 *) a), *((const float8 *) b));
40 : : }
41 : : static bool
3539 andrew@dunslane.net 42 : 793 : gbt_float8ge(const void *a, const void *b, FmgrInfo *flinfo)
43 : : {
150 tgl@sss.pgh.pa.us 44 : 793 : return float8_ge(*((const float8 *) a), *((const float8 *) b));
45 : : }
46 : : static bool
3539 andrew@dunslane.net 47 : 416 : gbt_float8eq(const void *a, const void *b, FmgrInfo *flinfo)
48 : : {
150 tgl@sss.pgh.pa.us 49 : 416 : return float8_eq(*((const float8 *) a), *((const float8 *) b));
50 : : }
51 : : static bool
3539 andrew@dunslane.net 52 : 560 : gbt_float8le(const void *a, const void *b, FmgrInfo *flinfo)
53 : : {
150 tgl@sss.pgh.pa.us 54 : 560 : return float8_le(*((const float8 *) a), *((const float8 *) b));
55 : : }
56 : : static bool
3539 andrew@dunslane.net 57 : 5720 : gbt_float8lt(const void *a, const void *b, FmgrInfo *flinfo)
58 : : {
150 tgl@sss.pgh.pa.us 59 : 5720 : return float8_lt(*((const float8 *) a), *((const float8 *) b));
60 : : }
61 : :
62 : : static int
3539 andrew@dunslane.net 63 : 1637 : gbt_float8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
64 : : {
5557 peter_e@gmx.net 65 : 1637 : float8KEY *ia = (float8KEY *) (((const Nsrt *) a)->t);
66 : 1637 : float8KEY *ib = (float8KEY *) (((const Nsrt *) b)->t);
67 : : int res;
68 : :
150 tgl@sss.pgh.pa.us 69 : 1637 : res = float8_cmp_internal(ia->lower, ib->lower);
70 [ + - ]: 1637 : if (res != 0)
71 : 1637 : return res;
150 tgl@sss.pgh.pa.us 72 :UBC 0 : return float8_cmp_internal(ia->upper, ib->upper);
73 : : }
74 : :
75 : : static float8
3539 andrew@dunslane.net 76 :CBC 276 : gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
77 : : {
5711 bruce@momjian.us 78 : 276 : float8 arg1 = *(const float8 *) a;
79 : 276 : float8 arg2 = *(const float8 *) b;
80 : : float8 r;
81 : :
82 : : /*
83 : : * If the difference of two finite values overflows, this subtraction
84 : : * yields an infinite distance. Returning that is fine, while throwing an
85 : : * error is not. However, Inf - Inf will yield NaN, while we need to
86 : : * report zero distance for that case.
87 : : */
5750 tgl@sss.pgh.pa.us 88 : 276 : r = arg1 - arg2;
150 89 [ - + ]: 276 : if (unlikely(isnan(r)))
90 : : {
150 tgl@sss.pgh.pa.us 91 [ # # # # ]:UBC 0 : if (isnan(arg1) && isnan(arg2))
92 : 0 : r = 0.0; /* treat NaNs as equal */
93 [ # # # # ]: 0 : else if (isnan(arg1) || isnan(arg2))
94 : 0 : r = get_float8_infinity(); /* max dist for NaN vs non-NaN */
95 : : else
96 : 0 : r = 0.0; /* must be Inf - Inf case */
97 : : }
1513 peter@eisentraut.org 98 :CBC 276 : return fabs(r);
99 : : }
100 : :
101 : :
102 : : static const gbtree_ninfo tinfo =
103 : : {
104 : : gbt_t_float8,
105 : : sizeof(float8),
106 : : 16, /* sizeof(gbtreekey16) */
107 : : gbt_float8gt,
108 : : gbt_float8ge,
109 : : gbt_float8eq,
110 : : gbt_float8le,
111 : : gbt_float8lt,
112 : : gbt_float8key_cmp,
113 : : gbt_float8_dist
114 : : };
115 : :
116 : :
5750 tgl@sss.pgh.pa.us 117 : 4 : PG_FUNCTION_INFO_V1(float8_dist);
118 : : Datum
119 : 551 : float8_dist(PG_FUNCTION_ARGS)
120 : : {
121 : 551 : float8 a = PG_GETARG_FLOAT8(0);
122 : 551 : float8 b = PG_GETARG_FLOAT8(1);
123 : : float8 r;
124 : :
125 : 551 : r = a - b;
1927 michael@paquier.xyz 126 [ + + - + : 551 : if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
- - ]
1927 michael@paquier.xyz 127 :UBC 0 : float_overflow_error();
150 tgl@sss.pgh.pa.us 128 [ + + ]:CBC 551 : if (unlikely(isnan(r)))
129 : : {
130 [ + - - + ]: 1 : if (isnan(a) && isnan(b))
150 tgl@sss.pgh.pa.us 131 :UBC 0 : r = 0.0; /* treat NaNs as equal */
150 tgl@sss.pgh.pa.us 132 [ - + - - ]:CBC 1 : else if (isnan(a) || isnan(b))
133 : 1 : r = get_float8_infinity(); /* max dist for NaN vs non-NaN */
134 : : else
150 tgl@sss.pgh.pa.us 135 :UBC 0 : r = 0.0; /* must be Inf - Inf case */
136 : : }
1513 peter@eisentraut.org 137 :CBC 551 : PG_RETURN_FLOAT8(fabs(r));
138 : : }
139 : :
140 : :
141 : : /**************************************************
142 : : * GiST support functions
143 : : **************************************************/
144 : :
145 : : Datum
8219 teodor@sigaev.ru 146 : 1655 : gbt_float8_compress(PG_FUNCTION_ARGS)
147 : : {
8126 bruce@momjian.us 148 : 1655 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
149 : :
4265 heikki.linnakangas@i 150 : 1655 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
151 : : }
152 : :
153 : : Datum
4264 154 : 275 : gbt_float8_fetch(PG_FUNCTION_ARGS)
155 : : {
156 : 275 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
157 : :
158 : 275 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
159 : : }
160 : :
161 : : Datum
8219 teodor@sigaev.ru 162 : 2067 : gbt_float8_consistent(PG_FUNCTION_ARGS)
163 : : {
8126 bruce@momjian.us 164 : 2067 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
165 : 2067 : float8 query = PG_GETARG_FLOAT8(1);
6802 tgl@sss.pgh.pa.us 166 : 2067 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
167 : : #ifdef NOT_USED
168 : : Oid subtype = PG_GETARG_OID(3);
169 : : #endif
170 : 2067 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
8126 bruce@momjian.us 171 : 2067 : float8KEY *kkk = (float8KEY *) DatumGetPointer(entry->key);
172 : : GBT_NUMKEY_R key;
173 : :
174 : : /* All cases served by this function are exact */
6802 tgl@sss.pgh.pa.us 175 : 2067 : *recheck = false;
176 : :
6379 bruce@momjian.us 177 : 2067 : key.lower = (GBT_NUMKEY *) &kkk->lower;
178 : 2067 : key.upper = (GBT_NUMKEY *) &kkk->upper;
179 : :
148 tgl@sss.pgh.pa.us 180 :GNC 2067 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, strategy,
181 : : GIST_LEAF(entry), &tinfo,
182 : : fcinfo->flinfo));
183 : : }
184 : :
185 : : Datum
5750 tgl@sss.pgh.pa.us 186 :CBC 277 : gbt_float8_distance(PG_FUNCTION_ARGS)
187 : : {
188 : 277 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
189 : 277 : float8 query = PG_GETARG_FLOAT8(1);
190 : : #ifdef NOT_USED
191 : : Oid subtype = PG_GETARG_OID(3);
192 : : #endif
193 : 277 : float8KEY *kkk = (float8KEY *) DatumGetPointer(entry->key);
194 : : GBT_NUMKEY_R key;
195 : :
196 : 277 : key.lower = (GBT_NUMKEY *) &kkk->lower;
197 : 277 : key.upper = (GBT_NUMKEY *) &kkk->upper;
198 : :
730 peter@eisentraut.org 199 : 277 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
200 : : &tinfo, fcinfo->flinfo));
201 : : }
202 : :
203 : : Datum
8219 teodor@sigaev.ru 204 : 15 : gbt_float8_union(PG_FUNCTION_ARGS)
205 : : {
8126 bruce@momjian.us 206 : 15 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
103 michael@paquier.xyz 207 :GNC 15 : void *out = palloc_object(float8KEY);
208 : :
8126 bruce@momjian.us 209 :CBC 15 : *(int *) PG_GETARG_POINTER(1) = sizeof(float8KEY);
730 peter@eisentraut.org 210 : 15 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
211 : : }
212 : :
213 : : Datum
8219 teodor@sigaev.ru 214 : 1094 : gbt_float8_penalty(PG_FUNCTION_ARGS)
215 : : {
8126 bruce@momjian.us 216 : 1094 : float8KEY *origentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
217 : 1094 : float8KEY *newentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
218 : 1094 : float *result = (float *) PG_GETARG_POINTER(2);
219 : :
150 tgl@sss.pgh.pa.us 220 : 1094 : float_penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
221 : :
8126 bruce@momjian.us 222 : 1094 : PG_RETURN_POINTER(result);
223 : : }
224 : :
225 : : Datum
8219 teodor@sigaev.ru 226 : 4 : gbt_float8_picksplit(PG_FUNCTION_ARGS)
227 : : {
2494 alvherre@alvh.no-ip. 228 : 4 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
229 : : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
230 : : &tinfo, fcinfo->flinfo));
231 : : }
232 : :
233 : : Datum
8219 teodor@sigaev.ru 234 : 3 : gbt_float8_same(PG_FUNCTION_ARGS)
235 : : {
8126 bruce@momjian.us 236 : 3 : float8KEY *b1 = (float8KEY *) PG_GETARG_POINTER(0);
237 : 3 : float8KEY *b2 = (float8KEY *) PG_GETARG_POINTER(1);
238 : 3 : bool *result = (bool *) PG_GETARG_POINTER(2);
239 : :
3539 andrew@dunslane.net 240 : 3 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
8126 bruce@momjian.us 241 : 3 : PG_RETURN_POINTER(result);
242 : : }
243 : :
244 : : static int
604 heikki.linnakangas@i 245 : 10432 : gbt_float8_ssup_cmp(Datum x, Datum y, SortSupport ssup)
246 : : {
247 : 10432 : float8KEY *arg1 = (float8KEY *) DatumGetPointer(x);
248 : 10432 : float8KEY *arg2 = (float8KEY *) DatumGetPointer(y);
249 : :
250 : : /* for leaf items we expect lower == upper, so only compare lower */
251 : 10432 : return float8_cmp_internal(arg1->lower, arg2->lower);
252 : : }
253 : :
254 : : Datum
255 : 5 : gbt_float8_sortsupport(PG_FUNCTION_ARGS)
256 : : {
257 : 5 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
258 : :
259 : 5 : ssup->comparator = gbt_float8_ssup_cmp;
260 : 5 : ssup->ssup_extra = NULL;
261 : :
262 : 5 : PG_RETURN_VOID();
263 : : }
|