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