PostgreSQL Source Code  git master
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 
10 typedef struct float4key
11 {
15 
16 /*
17 ** float4 ops
18 */
27 
28 static bool
29 gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
30 {
31  return (*((const float4 *) a) > *((const float4 *) b));
32 }
33 static bool
34 gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
35 {
36  return (*((const float4 *) a) >= *((const float4 *) b));
37 }
38 static bool
39 gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
40 {
41  return (*((const float4 *) a) == *((const float4 *) b));
42 }
43 static bool
44 gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
45 {
46  return (*((const float4 *) a) <= *((const float4 *) b));
47 }
48 static bool
49 gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
50 {
51  return (*((const float4 *) a) < *((const float4 *) b));
52 }
53 
54 static int
55 gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
56 {
57  float4KEY *ia = (float4KEY *) (((const Nsrt *) a)->t);
58  float4KEY *ib = (float4KEY *) (((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
72 gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
73 {
74  return GET_FLOAT_DISTANCE(float4, a, b);
75 }
76 
77 
78 static const gbtree_ninfo tinfo =
79 {
81  sizeof(float4),
82  8, /* sizeof(gbtreekey8) */
90 };
91 
92 
94 Datum
96 {
99  float4 r;
100 
101  r = a - b;
102  if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
104 
105  PG_RETURN_FLOAT4(Abs(r));
106 }
107 
108 
109 /**************************************************
110  * float4 ops
111  **************************************************/
112 
113 
114 Datum
116 {
117  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
118 
120 }
121 
122 Datum
124 {
125  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
126 
128 }
129 
130 Datum
132 {
133  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
134  float4 query = PG_GETARG_FLOAT4(1);
136 
137  /* Oid subtype = PG_GETARG_OID(3); */
138  bool *recheck = (bool *) PG_GETARG_POINTER(4);
139  float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
141 
142  /* All cases served by this function are exact */
143  *recheck = false;
144 
145  key.lower = (GBT_NUMKEY *) &kkk->lower;
146  key.upper = (GBT_NUMKEY *) &kkk->upper;
147 
148  PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &query, &strategy,
149  GIST_LEAF(entry), &tinfo,
150  fcinfo->flinfo));
151 }
152 
153 
154 Datum
156 {
157  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
158  float4 query = PG_GETARG_FLOAT4(1);
159 
160  /* Oid subtype = PG_GETARG_OID(3); */
161  float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
163 
164  key.lower = (GBT_NUMKEY *) &kkk->lower;
165  key.upper = (GBT_NUMKEY *) &kkk->upper;
166 
167  PG_RETURN_FLOAT8(gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry),
168  &tinfo, fcinfo->flinfo));
169 }
170 
171 
172 Datum
174 {
176  void *out = palloc(sizeof(float4KEY));
177 
178  *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
179  PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
180 }
181 
182 
183 Datum
185 {
186  float4KEY *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
188  float *result = (float *) PG_GETARG_POINTER(2);
189 
190  penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
191 
192  PG_RETURN_POINTER(result);
193 
194 }
195 
196 Datum
198 {
201  &tinfo, fcinfo->flinfo));
202 }
203 
204 Datum
206 {
209  bool *result = (bool *) PG_GETARG_POINTER(2);
210 
211  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
212  PG_RETURN_POINTER(result);
213 }
static bool gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:39
Datum gbt_float4_compress(PG_FUNCTION_ARGS)
Definition: btree_float4.c:115
Datum gbt_float4_same(PG_FUNCTION_ARGS)
Definition: btree_float4.c:205
Datum gbt_float4_union(PG_FUNCTION_ARGS)
Definition: btree_float4.c:173
Datum gbt_float4_picksplit(PG_FUNCTION_ARGS)
Definition: btree_float4.c:197
static bool gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:29
Datum gbt_float4_penalty(PG_FUNCTION_ARGS)
Definition: btree_float4.c:184
Datum gbt_float4_fetch(PG_FUNCTION_ARGS)
Definition: btree_float4.c:123
static float8 gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:72
PG_FUNCTION_INFO_V1(gbt_float4_compress)
static bool gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:44
static const gbtree_ninfo tinfo
Definition: btree_float4.c:78
struct float4key float4KEY
Datum gbt_float4_distance(PG_FUNCTION_ARGS)
Definition: btree_float4.c:155
Datum float4_dist(PG_FUNCTION_ARGS)
Definition: btree_float4.c:95
static int gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:55
static bool gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:49
Datum gbt_float4_consistent(PG_FUNCTION_ARGS)
Definition: btree_float4.c:131
static bool gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:34
@ gbt_t_float4
Definition: btree_gist.h:20
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const 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_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define penalty_num(result, olower, oupper, nlower, nupper)
#define GET_FLOAT_DISTANCE(t, arg1, arg2)
char GBT_NUMKEY
double float8
Definition: c.h:565
#define Abs(x)
Definition: c.h:992
#define unlikely(x)
Definition: c.h:273
float float4
Definition: c.h:564
pg_noinline void float_overflow_error(void)
Definition: float.c:85
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:281
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:366
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define GIST_LEAF(entry)
Definition: gist.h:168
int b
Definition: isn.c:70
int a
Definition: isn.c:69
void * palloc(Size size)
Definition: mcxt.c:1062
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetPointer(X)
Definition: postgres.h:593
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: fmgr.h:57
Datum key
Definition: gist.h:158
float4 upper
Definition: btree_float4.c:13
float4 lower
Definition: btree_float4.c:12