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 
9 typedef struct float4key
10 {
13 } float4KEY;
14 
15 /*
16 ** float4 ops
17 */
26 
27 static bool
28 gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
29 {
30  return (*((const float4 *) a) > *((const float4 *) b));
31 }
32 static bool
33 gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
34 {
35  return (*((const float4 *) a) >= *((const float4 *) b));
36 }
37 static bool
38 gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
39 {
40  return (*((const float4 *) a) == *((const float4 *) b));
41 }
42 static bool
43 gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
44 {
45  return (*((const float4 *) a) <= *((const float4 *) b));
46 }
47 static bool
48 gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
49 {
50  return (*((const float4 *) a) < *((const float4 *) b));
51 }
52 
53 static int
54 gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
55 {
56  float4KEY *ia = (float4KEY *) (((const Nsrt *) a)->t);
57  float4KEY *ib = (float4KEY *) (((const Nsrt *) b)->t);
58 
59  if (ia->lower == ib->lower)
60  {
61  if (ia->upper == ib->upper)
62  return 0;
63 
64  return (ia->upper > ib->upper) ? 1 : -1;
65  }
66 
67  return (ia->lower > ib->lower) ? 1 : -1;
68 }
69 
70 static float8
71 gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
72 {
73  return GET_FLOAT_DISTANCE(float4, a, b);
74 }
75 
76 
77 static const gbtree_ninfo tinfo =
78 {
80  sizeof(float4),
81  8, /* sizeof(gbtreekey8) */
89 };
90 
91 
93 Datum
95 {
96  float4 a = PG_GETARG_FLOAT4(0);
97  float4 b = PG_GETARG_FLOAT4(1);
98  float4 r;
99 
100  r = a - b;
101  CHECKFLOATVAL(r, isinf(a) || isinf(b), true);
102 
103  PG_RETURN_FLOAT4(Abs(r));
104 }
105 
106 
107 /**************************************************
108  * float4 ops
109  **************************************************/
110 
111 
112 Datum
114 {
115  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
116 
117  PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
118 }
119 
120 Datum
122 {
123  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
124 
125  PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
126 }
127 
128 Datum
130 {
131  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
132  float4 query = PG_GETARG_FLOAT4(1);
134 
135  /* Oid subtype = PG_GETARG_OID(3); */
136  bool *recheck = (bool *) PG_GETARG_POINTER(4);
137  float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
138  GBT_NUMKEY_R key;
139 
140  /* All cases served by this function are exact */
141  *recheck = false;
142 
143  key.lower = (GBT_NUMKEY *) &kkk->lower;
144  key.upper = (GBT_NUMKEY *) &kkk->upper;
145 
147  gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
148  );
149 }
150 
151 
152 Datum
154 {
155  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
156  float4 query = PG_GETARG_FLOAT4(1);
157 
158  /* Oid subtype = PG_GETARG_OID(3); */
159  float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
160  GBT_NUMKEY_R key;
161 
162  key.lower = (GBT_NUMKEY *) &kkk->lower;
163  key.upper = (GBT_NUMKEY *) &kkk->upper;
164 
166  gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
167  );
168 }
169 
170 
171 Datum
173 {
175  void *out = palloc(sizeof(float4KEY));
176 
177  *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
178  PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
179 }
180 
181 
182 Datum
184 {
185  float4KEY *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
186  float4KEY *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
187  float *result = (float *) PG_GETARG_POINTER(2);
188 
189  penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
190 
191  PG_RETURN_POINTER(result);
192 
193 }
194 
195 Datum
197 {
201  &tinfo, fcinfo->flinfo
202  ));
203 }
204 
205 Datum
207 {
210  bool *result = (bool *) PG_GETARG_POINTER(2);
211 
212  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
213  PG_RETURN_POINTER(result);
214 }
#define GIST_LEAF(entry)
Definition: gist.h:133
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
Datum gbt_float4_union(PG_FUNCTION_ARGS)
Definition: btree_float4.c:172
Definition: fmgr.h:56
static bool gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:38
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:325
float4 lower
Definition: btree_float4.c:11
struct float4key float4KEY
const GBT_NUMKEY * lower
static bool gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:43
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
Datum gbt_float4_picksplit(PG_FUNCTION_ARGS)
Definition: btree_float4.c:196
Datum gbt_float4_fetch(PG_FUNCTION_ARGS)
Definition: btree_float4.c:121
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
uint16 StrategyNumber
Definition: stratnum.h:22
PG_FUNCTION_INFO_V1(gbt_float4_compress)
const GBT_NUMKEY * upper
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define Abs(x)
Definition: c.h:808
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
Datum gbt_float4_compress(PG_FUNCTION_ARGS)
Definition: btree_float4.c:113
double float8
Definition: c.h:429
#define penalty_num(result, olower, oupper, nlower, nupper)
int isinf(double x)
Datum key
Definition: gist.h:123
static bool gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:48
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
char GBT_NUMKEY
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:245
static bool gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:28
float4 upper
Definition: btree_float4.c:12
static int gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:54
float float4
Definition: c.h:428
static bool gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:33
Datum gbt_float4_distance(PG_FUNCTION_ARGS)
Definition: btree_float4.c:153
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uintptr_t Datum
Definition: postgres.h:372
#define GET_FLOAT_DISTANCE(t, arg1, arg2)
Datum gbt_float4_consistent(PG_FUNCTION_ARGS)
Definition: btree_float4.c:129
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:237
#define DatumGetPointer(X)
Definition: postgres.h:555
Datum float4_dist(PG_FUNCTION_ARGS)
Definition: btree_float4.c:94
static const gbtree_ninfo tinfo
Definition: btree_float4.c:77
void * palloc(Size size)
Definition: mcxt.c:848
Datum gbt_float4_penalty(PG_FUNCTION_ARGS)
Definition: btree_float4.c:183
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
static float8 gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float4.c:71
Datum gbt_float4_same(PG_FUNCTION_ARGS)
Definition: btree_float4.c:206
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)