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