Age Owner Branch data TLA 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 */
7771 teodor@sigaev.ru 19 :CBC 4 : PG_FUNCTION_INFO_V1(gbt_float4_compress);
3816 heikki.linnakangas@i 20 : 4 : PG_FUNCTION_INFO_V1(gbt_float4_fetch);
7771 teodor@sigaev.ru 21 : 4 : PG_FUNCTION_INFO_V1(gbt_float4_union);
22 : 4 : PG_FUNCTION_INFO_V1(gbt_float4_picksplit);
23 : 4 : PG_FUNCTION_INFO_V1(gbt_float4_consistent);
5302 tgl@sss.pgh.pa.us 24 : 4 : PG_FUNCTION_INFO_V1(gbt_float4_distance);
7771 teodor@sigaev.ru 25 : 4 : PG_FUNCTION_INFO_V1(gbt_float4_penalty);
26 : 4 : PG_FUNCTION_INFO_V1(gbt_float4_same);
156 heikki.linnakangas@i 27 : 4 : PG_FUNCTION_INFO_V1(gbt_float4_sortsupport);
28 : :
29 : : static bool
3091 andrew@dunslane.net 30 : 1364 : gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
31 : : {
5109 peter_e@gmx.net 32 : 1364 : return (*((const float4 *) a) > *((const float4 *) b));
33 : : }
34 : : static bool
3091 andrew@dunslane.net 35 : 522 : gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
36 : : {
5109 peter_e@gmx.net 37 : 522 : return (*((const float4 *) a) >= *((const float4 *) b));
38 : : }
39 : : static bool
3091 andrew@dunslane.net 40 : 273 : gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
41 : : {
5109 peter_e@gmx.net 42 : 273 : return (*((const float4 *) a) == *((const float4 *) b));
43 : : }
44 : : static bool
3091 andrew@dunslane.net 45 : 829 : gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
46 : : {
5109 peter_e@gmx.net 47 : 829 : return (*((const float4 *) a) <= *((const float4 *) b));
48 : : }
49 : : static bool
3091 andrew@dunslane.net 50 : 1638 : gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
51 : : {
5109 peter_e@gmx.net 52 : 1638 : return (*((const float4 *) a) < *((const float4 *) b));
53 : : }
54 : :
55 : : static int
3091 andrew@dunslane.net 56 : 546 : gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
57 : : {
5109 peter_e@gmx.net 58 : 546 : float4KEY *ia = (float4KEY *) (((const Nsrt *) a)->t);
59 : 546 : float4KEY *ib = (float4KEY *) (((const Nsrt *) b)->t);
60 : :
5757 teodor@sigaev.ru 61 [ - + ]: 546 : if (ia->lower == ib->lower)
62 : : {
5757 teodor@sigaev.ru 63 [ # # ]:UBC 0 : if (ia->upper == ib->upper)
64 : 0 : return 0;
65 : :
66 [ # # ]: 0 : return (ia->upper > ib->upper) ? 1 : -1;
67 : : }
68 : :
5757 teodor@sigaev.ru 69 [ - + ]:CBC 546 : return (ia->lower > ib->lower) ? 1 : -1;
70 : : }
71 : :
72 : : static float8
3091 andrew@dunslane.net 73 : 274 : gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
74 : : {
5302 tgl@sss.pgh.pa.us 75 : 274 : 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 : 4 : PG_FUNCTION_INFO_V1(float4_dist);
95 : : Datum
96 : 550 : float4_dist(PG_FUNCTION_ARGS)
97 : : {
5263 bruce@momjian.us 98 : 550 : float4 a = PG_GETARG_FLOAT4(0);
5302 tgl@sss.pgh.pa.us 99 : 550 : float4 b = PG_GETARG_FLOAT4(1);
100 : : float4 r;
101 : :
102 : 550 : r = a - b;
1479 michael@paquier.xyz 103 [ - + - - : 550 : if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
- - ]
1479 michael@paquier.xyz 104 :UBC 0 : float_overflow_error();
105 : :
1064 peter@eisentraut.org 106 :CBC 550 : PG_RETURN_FLOAT4(fabsf(r));
107 : : }
108 : :
109 : :
110 : : /**************************************************
111 : : * GiST support functions
112 : : **************************************************/
113 : :
114 : : Datum
7771 teodor@sigaev.ru 115 : 549 : gbt_float4_compress(PG_FUNCTION_ARGS)
116 : : {
7678 bruce@momjian.us 117 : 549 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
118 : :
3817 heikki.linnakangas@i 119 : 549 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
120 : : }
121 : :
122 : : Datum
3816 123 : 273 : gbt_float4_fetch(PG_FUNCTION_ARGS)
124 : : {
125 : 273 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
126 : :
127 : 273 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
128 : : }
129 : :
130 : : Datum
7771 teodor@sigaev.ru 131 : 1923 : gbt_float4_consistent(PG_FUNCTION_ARGS)
132 : : {
7678 bruce@momjian.us 133 : 1923 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
134 : 1923 : float4 query = PG_GETARG_FLOAT4(1);
6354 tgl@sss.pgh.pa.us 135 : 1923 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
136 : :
137 : : /* Oid subtype = PG_GETARG_OID(3); */
138 : 1923 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
7678 bruce@momjian.us 139 : 1923 : float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
140 : : GBT_NUMKEY_R key;
141 : :
142 : : /* All cases served by this function are exact */
6354 tgl@sss.pgh.pa.us 143 : 1923 : *recheck = false;
144 : :
5931 bruce@momjian.us 145 : 1923 : key.lower = (GBT_NUMKEY *) &kkk->lower;
146 : 1923 : key.upper = (GBT_NUMKEY *) &kkk->upper;
147 : :
282 peter@eisentraut.org 148 : 1923 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
149 : : GIST_LEAF(entry), &tinfo,
150 : : fcinfo->flinfo));
151 : : }
152 : :
153 : : Datum
5302 tgl@sss.pgh.pa.us 154 : 275 : gbt_float4_distance(PG_FUNCTION_ARGS)
155 : : {
156 : 275 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
157 : 275 : float4 query = PG_GETARG_FLOAT4(1);
158 : :
159 : : /* Oid subtype = PG_GETARG_OID(3); */
160 : 275 : float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
161 : : GBT_NUMKEY_R key;
162 : :
163 : 275 : key.lower = (GBT_NUMKEY *) &kkk->lower;
164 : 275 : key.upper = (GBT_NUMKEY *) &kkk->upper;
165 : :
282 peter@eisentraut.org 166 : 275 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
167 : : &tinfo, fcinfo->flinfo));
168 : : }
169 : :
170 : : Datum
7771 teodor@sigaev.ru 171 : 1 : gbt_float4_union(PG_FUNCTION_ARGS)
172 : : {
7678 bruce@momjian.us 173 : 1 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
174 : 1 : void *out = palloc(sizeof(float4KEY));
175 : :
176 : 1 : *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
282 peter@eisentraut.org 177 : 1 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
178 : : }
179 : :
180 : : Datum
7771 teodor@sigaev.ru 181 :UBC 0 : gbt_float4_penalty(PG_FUNCTION_ARGS)
182 : : {
7678 bruce@momjian.us 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 : :
7266 187 [ # # # # : 0 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
# # ]
188 : :
7678 189 : 0 : PG_RETURN_POINTER(result);
190 : : }
191 : :
192 : : Datum
7771 teodor@sigaev.ru 193 :CBC 1 : gbt_float4_picksplit(PG_FUNCTION_ARGS)
194 : : {
2046 alvherre@alvh.no-ip. 195 : 1 : 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
7771 teodor@sigaev.ru 201 :UBC 0 : gbt_float4_same(PG_FUNCTION_ARGS)
202 : : {
7678 bruce@momjian.us 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 : :
3091 andrew@dunslane.net 207 : 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
7678 bruce@momjian.us 208 : 0 : PG_RETURN_POINTER(result);
209 : : }
210 : :
211 : : static int
156 heikki.linnakangas@i 212 :CBC 5031 : gbt_float4_ssup_cmp(Datum x, Datum y, SortSupport ssup)
213 : : {
214 : 5031 : float4KEY *arg1 = (float4KEY *) DatumGetPointer(x);
215 : 5031 : float4KEY *arg2 = (float4KEY *) DatumGetPointer(y);
216 : :
217 : : /* for leaf items we expect lower == upper, so only compare lower */
218 : 5031 : return float4_cmp_internal(arg1->lower, arg2->lower);
219 : : }
220 : :
221 : : Datum
222 : 1 : gbt_float4_sortsupport(PG_FUNCTION_ARGS)
223 : : {
224 : 1 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
225 : :
226 : 1 : ssup->comparator = gbt_float4_ssup_cmp;
227 : 1 : ssup->ssup_extra = NULL;
228 : :
229 : 1 : PG_RETURN_VOID();
230 : : }
|