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 */
19 0 : PG_FUNCTION_INFO_V1(gbt_float8_compress);
20 0 : PG_FUNCTION_INFO_V1(gbt_float8_fetch);
21 0 : PG_FUNCTION_INFO_V1(gbt_float8_union);
22 0 : PG_FUNCTION_INFO_V1(gbt_float8_picksplit);
23 0 : PG_FUNCTION_INFO_V1(gbt_float8_consistent);
24 0 : PG_FUNCTION_INFO_V1(gbt_float8_distance);
25 0 : PG_FUNCTION_INFO_V1(gbt_float8_penalty);
26 0 : PG_FUNCTION_INFO_V1(gbt_float8_same);
27 0 : PG_FUNCTION_INFO_V1(gbt_float8_sortsupport);
28 :
29 :
30 : static bool
31 0 : gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo)
32 : {
33 0 : return (*((const float8 *) a) > *((const float8 *) b));
34 : }
35 : static bool
36 0 : gbt_float8ge(const void *a, const void *b, FmgrInfo *flinfo)
37 : {
38 0 : return (*((const float8 *) a) >= *((const float8 *) b));
39 : }
40 : static bool
41 0 : gbt_float8eq(const void *a, const void *b, FmgrInfo *flinfo)
42 : {
43 0 : return (*((const float8 *) a) == *((const float8 *) b));
44 : }
45 : static bool
46 0 : gbt_float8le(const void *a, const void *b, FmgrInfo *flinfo)
47 : {
48 0 : return (*((const float8 *) a) <= *((const float8 *) b));
49 : }
50 : static bool
51 0 : gbt_float8lt(const void *a, const void *b, FmgrInfo *flinfo)
52 : {
53 0 : return (*((const float8 *) a) < *((const float8 *) b));
54 : }
55 :
56 : static int
57 0 : gbt_float8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
58 : {
59 0 : float8KEY *ia = (float8KEY *) (((const Nsrt *) a)->t);
60 0 : float8KEY *ib = (float8KEY *) (((const Nsrt *) b)->t);
61 :
62 0 : if (ia->lower == ib->lower)
63 : {
64 0 : if (ia->upper == ib->upper)
65 0 : return 0;
66 :
67 0 : return (ia->upper > ib->upper) ? 1 : -1;
68 : }
69 :
70 0 : return (ia->lower > ib->lower) ? 1 : -1;
71 0 : }
72 :
73 : static float8
74 0 : gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
75 : {
76 0 : float8 arg1 = *(const float8 *) a;
77 0 : float8 arg2 = *(const float8 *) b;
78 0 : float8 r;
79 :
80 0 : r = arg1 - arg2;
81 0 : if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2))
82 0 : float_overflow_error();
83 0 : return fabs(r);
84 0 : }
85 :
86 :
87 : static const gbtree_ninfo tinfo =
88 : {
89 : gbt_t_float8,
90 : sizeof(float8),
91 : 16, /* sizeof(gbtreekey16) */
92 : gbt_float8gt,
93 : gbt_float8ge,
94 : gbt_float8eq,
95 : gbt_float8le,
96 : gbt_float8lt,
97 : gbt_float8key_cmp,
98 : gbt_float8_dist
99 : };
100 :
101 :
102 0 : PG_FUNCTION_INFO_V1(float8_dist);
103 : Datum
104 0 : float8_dist(PG_FUNCTION_ARGS)
105 : {
106 0 : float8 a = PG_GETARG_FLOAT8(0);
107 0 : float8 b = PG_GETARG_FLOAT8(1);
108 0 : float8 r;
109 :
110 0 : r = a - b;
111 0 : if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
112 0 : float_overflow_error();
113 :
114 0 : PG_RETURN_FLOAT8(fabs(r));
115 0 : }
116 :
117 :
118 : /**************************************************
119 : * GiST support functions
120 : **************************************************/
121 :
122 : Datum
123 0 : gbt_float8_compress(PG_FUNCTION_ARGS)
124 : {
125 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
126 :
127 0 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
128 0 : }
129 :
130 : Datum
131 0 : gbt_float8_fetch(PG_FUNCTION_ARGS)
132 : {
133 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
134 :
135 0 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
136 0 : }
137 :
138 : Datum
139 0 : gbt_float8_consistent(PG_FUNCTION_ARGS)
140 : {
141 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
142 0 : float8 query = PG_GETARG_FLOAT8(1);
143 0 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
144 : #ifdef NOT_USED
145 : Oid subtype = PG_GETARG_OID(3);
146 : #endif
147 0 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
148 0 : float8KEY *kkk = (float8KEY *) DatumGetPointer(entry->key);
149 0 : GBT_NUMKEY_R key;
150 :
151 : /* All cases served by this function are exact */
152 0 : *recheck = false;
153 :
154 0 : key.lower = (GBT_NUMKEY *) &kkk->lower;
155 0 : key.upper = (GBT_NUMKEY *) &kkk->upper;
156 :
157 0 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
158 : GIST_LEAF(entry), &tinfo,
159 : fcinfo->flinfo));
160 0 : }
161 :
162 : Datum
163 0 : gbt_float8_distance(PG_FUNCTION_ARGS)
164 : {
165 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
166 0 : float8 query = PG_GETARG_FLOAT8(1);
167 : #ifdef NOT_USED
168 : Oid subtype = PG_GETARG_OID(3);
169 : #endif
170 0 : float8KEY *kkk = (float8KEY *) DatumGetPointer(entry->key);
171 0 : GBT_NUMKEY_R key;
172 :
173 0 : key.lower = (GBT_NUMKEY *) &kkk->lower;
174 0 : key.upper = (GBT_NUMKEY *) &kkk->upper;
175 :
176 0 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
177 : &tinfo, fcinfo->flinfo));
178 0 : }
179 :
180 : Datum
181 0 : gbt_float8_union(PG_FUNCTION_ARGS)
182 : {
183 0 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
184 0 : void *out = palloc(sizeof(float8KEY));
185 :
186 0 : *(int *) PG_GETARG_POINTER(1) = sizeof(float8KEY);
187 0 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
188 0 : }
189 :
190 : Datum
191 0 : gbt_float8_penalty(PG_FUNCTION_ARGS)
192 : {
193 0 : float8KEY *origentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
194 0 : float8KEY *newentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
195 0 : float *result = (float *) PG_GETARG_POINTER(2);
196 :
197 0 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
198 :
199 0 : PG_RETURN_POINTER(result);
200 0 : }
201 :
202 : Datum
203 0 : gbt_float8_picksplit(PG_FUNCTION_ARGS)
204 : {
205 0 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
206 : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
207 : &tinfo, fcinfo->flinfo));
208 : }
209 :
210 : Datum
211 0 : gbt_float8_same(PG_FUNCTION_ARGS)
212 : {
213 0 : float8KEY *b1 = (float8KEY *) PG_GETARG_POINTER(0);
214 0 : float8KEY *b2 = (float8KEY *) PG_GETARG_POINTER(1);
215 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
216 :
217 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
218 0 : PG_RETURN_POINTER(result);
219 0 : }
220 :
221 : static int
222 0 : gbt_float8_ssup_cmp(Datum x, Datum y, SortSupport ssup)
223 : {
224 0 : float8KEY *arg1 = (float8KEY *) DatumGetPointer(x);
225 0 : float8KEY *arg2 = (float8KEY *) DatumGetPointer(y);
226 :
227 : /* for leaf items we expect lower == upper, so only compare lower */
228 0 : return float8_cmp_internal(arg1->lower, arg2->lower);
229 0 : }
230 :
231 : Datum
232 0 : gbt_float8_sortsupport(PG_FUNCTION_ARGS)
233 : {
234 0 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
235 :
236 0 : ssup->comparator = gbt_float8_ssup_cmp;
237 0 : ssup->ssup_extra = NULL;
238 :
239 0 : PG_RETURN_VOID();
240 0 : }
|