Debian Codegraph @

contrib/btree_gist/btree_float4.c

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