Line data Source code
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/rel.h"
10 : #include "utils/sortsupport.h"
11 :
12 : typedef struct float4key
13 : {
14 : float4 lower;
15 : float4 upper;
16 : } float4KEY;
17 :
18 : /* GiST support functions */
19 0 : PG_FUNCTION_INFO_V1(gbt_float4_compress);
20 0 : PG_FUNCTION_INFO_V1(gbt_float4_fetch);
21 0 : PG_FUNCTION_INFO_V1(gbt_float4_union);
22 0 : PG_FUNCTION_INFO_V1(gbt_float4_picksplit);
23 0 : PG_FUNCTION_INFO_V1(gbt_float4_consistent);
24 0 : PG_FUNCTION_INFO_V1(gbt_float4_distance);
25 0 : PG_FUNCTION_INFO_V1(gbt_float4_penalty);
26 0 : PG_FUNCTION_INFO_V1(gbt_float4_same);
27 0 : PG_FUNCTION_INFO_V1(gbt_float4_sortsupport);
28 :
29 : static bool
30 0 : gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
31 : {
32 0 : return (*((const float4 *) a) > *((const float4 *) b));
33 : }
34 : static bool
35 0 : gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
36 : {
37 0 : return (*((const float4 *) a) >= *((const float4 *) b));
38 : }
39 : static bool
40 0 : gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
41 : {
42 0 : return (*((const float4 *) a) == *((const float4 *) b));
43 : }
44 : static bool
45 0 : gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
46 : {
47 0 : return (*((const float4 *) a) <= *((const float4 *) b));
48 : }
49 : static bool
50 0 : gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
51 : {
52 0 : return (*((const float4 *) a) < *((const float4 *) b));
53 : }
54 :
55 : static int
56 0 : gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
57 : {
58 0 : float4KEY *ia = (float4KEY *) (((const Nsrt *) a)->t);
59 0 : float4KEY *ib = (float4KEY *) (((const Nsrt *) b)->t);
60 :
61 0 : 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 0 : return (ia->lower > ib->lower) ? 1 : -1;
70 0 : }
71 :
72 : static float8
73 0 : gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
74 : {
75 0 : return GET_FLOAT_DISTANCE(float4, a, b);
76 : }
77 :
78 :
79 : static const gbtree_ninfo tinfo =
80 : {
81 : gbt_t_float4,
82 : sizeof(float4),
83 : 8, /* sizeof(gbtreekey8) */
84 : gbt_float4gt,
85 : gbt_float4ge,
86 : gbt_float4eq,
87 : gbt_float4le,
88 : gbt_float4lt,
89 : gbt_float4key_cmp,
90 : gbt_float4_dist
91 : };
92 :
93 :
94 0 : PG_FUNCTION_INFO_V1(float4_dist);
95 : Datum
96 0 : float4_dist(PG_FUNCTION_ARGS)
97 : {
98 0 : float4 a = PG_GETARG_FLOAT4(0);
99 0 : float4 b = PG_GETARG_FLOAT4(1);
100 0 : float4 r;
101 :
102 0 : r = a - b;
103 0 : if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
104 0 : float_overflow_error();
105 :
106 0 : PG_RETURN_FLOAT4(fabsf(r));
107 0 : }
108 :
109 :
110 : /**************************************************
111 : * GiST support functions
112 : **************************************************/
113 :
114 : Datum
115 0 : gbt_float4_compress(PG_FUNCTION_ARGS)
116 : {
117 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
118 :
119 0 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
120 0 : }
121 :
122 : Datum
123 0 : gbt_float4_fetch(PG_FUNCTION_ARGS)
124 : {
125 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
126 :
127 0 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
128 0 : }
129 :
130 : Datum
131 0 : gbt_float4_consistent(PG_FUNCTION_ARGS)
132 : {
133 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
134 0 : float4 query = PG_GETARG_FLOAT4(1);
135 0 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
136 : #ifdef NOT_USED
137 : Oid subtype = PG_GETARG_OID(3);
138 : #endif
139 0 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
140 0 : float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
141 0 : GBT_NUMKEY_R key;
142 :
143 : /* All cases served by this function are exact */
144 0 : *recheck = false;
145 :
146 0 : key.lower = (GBT_NUMKEY *) &kkk->lower;
147 0 : key.upper = (GBT_NUMKEY *) &kkk->upper;
148 :
149 0 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
150 : GIST_LEAF(entry), &tinfo,
151 : fcinfo->flinfo));
152 0 : }
153 :
154 : Datum
155 0 : gbt_float4_distance(PG_FUNCTION_ARGS)
156 : {
157 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
158 0 : float4 query = PG_GETARG_FLOAT4(1);
159 : #ifdef NOT_USED
160 : Oid subtype = PG_GETARG_OID(3);
161 : #endif
162 0 : float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
163 0 : GBT_NUMKEY_R key;
164 :
165 0 : key.lower = (GBT_NUMKEY *) &kkk->lower;
166 0 : key.upper = (GBT_NUMKEY *) &kkk->upper;
167 :
168 0 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
169 : &tinfo, fcinfo->flinfo));
170 0 : }
171 :
172 : Datum
173 0 : gbt_float4_union(PG_FUNCTION_ARGS)
174 : {
175 0 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
176 0 : void *out = palloc(sizeof(float4KEY));
177 :
178 0 : *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
179 0 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
180 0 : }
181 :
182 : Datum
183 0 : gbt_float4_penalty(PG_FUNCTION_ARGS)
184 : {
185 0 : float4KEY *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
186 0 : float4KEY *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
187 0 : float *result = (float *) PG_GETARG_POINTER(2);
188 :
189 0 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
190 :
191 0 : PG_RETURN_POINTER(result);
192 0 : }
193 :
194 : Datum
195 0 : gbt_float4_picksplit(PG_FUNCTION_ARGS)
196 : {
197 0 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
198 : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
199 : &tinfo, fcinfo->flinfo));
200 : }
201 :
202 : Datum
203 0 : gbt_float4_same(PG_FUNCTION_ARGS)
204 : {
205 0 : float4KEY *b1 = (float4KEY *) PG_GETARG_POINTER(0);
206 0 : float4KEY *b2 = (float4KEY *) PG_GETARG_POINTER(1);
207 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
208 :
209 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
210 0 : PG_RETURN_POINTER(result);
211 0 : }
212 :
213 : static int
214 0 : gbt_float4_ssup_cmp(Datum x, Datum y, SortSupport ssup)
215 : {
216 0 : float4KEY *arg1 = (float4KEY *) DatumGetPointer(x);
217 0 : float4KEY *arg2 = (float4KEY *) DatumGetPointer(y);
218 :
219 : /* for leaf items we expect lower == upper, so only compare lower */
220 0 : return float4_cmp_internal(arg1->lower, arg2->lower);
221 0 : }
222 :
223 : Datum
224 0 : gbt_float4_sortsupport(PG_FUNCTION_ARGS)
225 : {
226 0 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
227 :
228 0 : ssup->comparator = gbt_float4_ssup_cmp;
229 0 : ssup->ssup_extra = NULL;
230 :
231 0 : PG_RETURN_VOID();
232 0 : }
|