PostgreSQL Source Code git master
Loading...
Searching...
No Matches
btree_float4.c
Go to the documentation of this file.
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
17
18/* GiST support functions */
28
29/*
30 * Use the NaN-aware comparators from utils/float.h, so that our results
31 * will agree with standard btree indexes. Note that penalty and distance
32 * functions below must also cope with NaNs, in particular with the policy
33 * that all NaNs are equal.
34 */
35static bool
36gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
37{
38 return float4_gt(*((const float4 *) a), *((const float4 *) b));
39}
40static bool
41gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
42{
43 return float4_ge(*((const float4 *) a), *((const float4 *) b));
44}
45static bool
46gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
47{
48 return float4_eq(*((const float4 *) a), *((const float4 *) b));
49}
50static bool
51gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
52{
53 return float4_le(*((const float4 *) a), *((const float4 *) b));
54}
55static bool
56gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
57{
58 return float4_lt(*((const float4 *) a), *((const float4 *) b));
59}
60
61static int
62gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
63{
64 float4KEY *ia = (float4KEY *) (((const Nsrt *) a)->t);
65 float4KEY *ib = (float4KEY *) (((const Nsrt *) b)->t);
66 int res;
67
68 res = float4_cmp_internal(ia->lower, ib->lower);
69 if (res != 0)
70 return res;
71 return float4_cmp_internal(ia->upper, ib->upper);
72}
73
74static float8
75gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
76{
77 float8 arg1 = *(const float4 *) a;
78 float8 arg2 = *(const float4 *) b;
79 float8 r;
80
81 r = arg1 - arg2;
82 /* needn't consider isinf case here, must be due to input infinity */
83 if (unlikely(isnan(r)))
84 {
85 if (isnan(arg1) && isnan(arg2))
86 r = 0.0; /* treat NaNs as equal */
87 else if (isnan(arg1) || isnan(arg2))
88 r = get_float8_infinity(); /* max dist for NaN vs non-NaN */
89 else
90 r = 0.0; /* must be Inf - Inf case */
91 }
92 return fabs(r);
93}
94
95
96static const gbtree_ninfo tinfo =
97{
99 sizeof(float4),
100 8, /* sizeof(gbtreekey8) */
108};
109
110
112Datum
114{
117 float4 r;
118
119 r = a - b;
120 if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
122 if (unlikely(isnan(r)))
123 {
124 if (isnan(a) && isnan(b))
125 r = 0.0; /* treat NaNs as equal */
126 else if (isnan(a) || isnan(b))
127 r = get_float4_infinity(); /* max dist for NaN vs non-NaN */
128 else
129 r = 0.0; /* must be Inf - Inf case */
130 }
132}
133
134
135/**************************************************
136 * GiST support functions
137 **************************************************/
138
139Datum
146
147Datum
154
155Datum
157{
158 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
159 float4 query = PG_GETARG_FLOAT4(1);
161#ifdef NOT_USED
162 Oid subtype = PG_GETARG_OID(3);
163#endif
164 bool *recheck = (bool *) PG_GETARG_POINTER(4);
166 GBT_NUMKEY_R key;
167
168 /* All cases served by this function are exact */
169 *recheck = false;
170
171 key.lower = (GBT_NUMKEY *) &kkk->lower;
172 key.upper = (GBT_NUMKEY *) &kkk->upper;
173
174 PG_RETURN_BOOL(gbt_num_consistent(&key, &query, strategy,
175 GIST_LEAF(entry), &tinfo,
176 fcinfo->flinfo));
177}
178
179Datum
181{
182 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
183 float4 query = PG_GETARG_FLOAT4(1);
184#ifdef NOT_USED
185 Oid subtype = PG_GETARG_OID(3);
186#endif
188 GBT_NUMKEY_R key;
189
190 key.lower = (GBT_NUMKEY *) &kkk->lower;
191 key.upper = (GBT_NUMKEY *) &kkk->upper;
192
193 PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
194 &tinfo, fcinfo->flinfo));
195}
196
197Datum
199{
201 void *out = palloc(sizeof(float4KEY));
202
203 *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
204 PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
205}
206
207Datum
218
219Datum
226
227Datum
229{
232 bool *result = (bool *) PG_GETARG_POINTER(2);
233
234 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
236}
237
238static int
240{
243
244 /* for leaf items we expect lower == upper, so only compare lower */
245 return float4_cmp_internal(arg1->lower, arg2->lower);
246}
247
248Datum
static bool gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
Datum gbt_float4_compress(PG_FUNCTION_ARGS)
Datum gbt_float4_same(PG_FUNCTION_ARGS)
Datum gbt_float4_union(PG_FUNCTION_ARGS)
Datum gbt_float4_picksplit(PG_FUNCTION_ARGS)
static bool gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
Datum gbt_float4_sortsupport(PG_FUNCTION_ARGS)
Datum gbt_float4_penalty(PG_FUNCTION_ARGS)
Datum gbt_float4_fetch(PG_FUNCTION_ARGS)
static float8 gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
static const gbtree_ninfo tinfo
struct float4key float4KEY
Datum gbt_float4_distance(PG_FUNCTION_ARGS)
Datum float4_dist(PG_FUNCTION_ARGS)
static int gbt_float4_ssup_cmp(Datum x, Datum y, SortSupport ssup)
static int gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
Datum gbt_float4_consistent(PG_FUNCTION_ARGS)
static bool gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
@ gbt_t_float4
Definition btree_gist.h:20
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, StrategyNumber strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
char GBT_NUMKEY
#define float_penalty_num(result, olower, oupper, nlower, nupper)
double float8
Definition c.h:773
#define unlikely(x)
Definition c.h:497
float float4
Definition c.h:772
uint32 result
struct SortSupportData * SortSupport
Definition execnodes.h:61
pg_noinline void float_overflow_error(void)
Definition float.c:103
int float4_cmp_internal(float4 a, float4 b)
Definition float.c:857
static float4 get_float4_infinity(void)
Definition float.h:61
static bool float4_lt(const float4 val1, const float4 val2)
Definition float.h:268
static bool float4_ge(const float4 val1, const float4 val2)
Definition float.h:304
static bool float4_eq(const float4 val1, const float4 val2)
Definition float.h:244
static float8 get_float8_infinity(void)
Definition float.h:68
static bool float4_gt(const float4 val1, const float4 val2)
Definition float.h:292
static bool float4_le(const float4 val1, const float4 val2)
Definition float.h:280
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_RETURN_FLOAT8(x)
Definition fmgr.h:369
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_GETARG_UINT16(n)
Definition fmgr.h:272
#define PG_GETARG_FLOAT4(n)
Definition fmgr.h:282
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_RETURN_FLOAT4(x)
Definition fmgr.h:368
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define GIST_LEAF(entry)
Definition gist.h:171
int y
Definition isn.c:76
int b
Definition isn.c:74
int x
Definition isn.c:75
int a
Definition isn.c:73
void * palloc(Size size)
Definition mcxt.c:1390
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
unsigned int Oid
static int fb(int x)
uint16 StrategyNumber
Definition stratnum.h:22
Datum key
Definition gist.h:161
int(* comparator)(Datum x, Datum y, SortSupport ssup)
float4 upper
float4 lower