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