PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
btree_float8.c
Go to the documentation of this file.
1 /*
2  * contrib/btree_gist/btree_float8.c
3  */
4 #include "postgres.h"
5 
6 #include "btree_gist.h"
7 #include "btree_utils_num.h"
8 
9 typedef struct float8key
10 {
13 } float8KEY;
14 
15 /*
16 ** float8 ops
17 */
26 
27 
28 static bool
29 gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo)
30 {
31  return (*((const float8 *) a) > *((const float8 *) b));
32 }
33 static bool
34 gbt_float8ge(const void *a, const void *b, FmgrInfo *flinfo)
35 {
36  return (*((const float8 *) a) >= *((const float8 *) b));
37 }
38 static bool
39 gbt_float8eq(const void *a, const void *b, FmgrInfo *flinfo)
40 {
41  return (*((const float8 *) a) == *((const float8 *) b));
42 }
43 static bool
44 gbt_float8le(const void *a, const void *b, FmgrInfo *flinfo)
45 {
46  return (*((const float8 *) a) <= *((const float8 *) b));
47 }
48 static bool
49 gbt_float8lt(const void *a, const void *b, FmgrInfo *flinfo)
50 {
51  return (*((const float8 *) a) < *((const float8 *) b));
52 }
53 
54 static int
55 gbt_float8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
56 {
57  float8KEY *ia = (float8KEY *) (((const Nsrt *) a)->t);
58  float8KEY *ib = (float8KEY *) (((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_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
73 {
74  float8 arg1 = *(const float8 *) a;
75  float8 arg2 = *(const float8 *) b;
76  float8 r;
77 
78  r = arg1 - arg2;
79  CHECKFLOATVAL(r, isinf(arg1) || isinf(arg2), true);
80 
81  return Abs(r);
82 }
83 
84 
85 static const gbtree_ninfo tinfo =
86 {
88  sizeof(float8),
89  16, /* sizeof(gbtreekey16) */
97 };
98 
99 
101 Datum
103 {
104  float8 a = PG_GETARG_FLOAT8(0);
105  float8 b = PG_GETARG_FLOAT8(1);
106  float8 r;
107 
108  r = a - b;
109  CHECKFLOATVAL(r, isinf(a) || isinf(b), true);
110 
111  PG_RETURN_FLOAT8(Abs(r));
112 }
113 
114 /**************************************************
115  * float8 ops
116  **************************************************/
117 
118 
119 Datum
121 {
122  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
123 
124  PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
125 }
126 
127 Datum
129 {
130  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
131 
132  PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
133 }
134 
135 Datum
137 {
138  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
139  float8 query = PG_GETARG_FLOAT8(1);
141 
142  /* Oid subtype = PG_GETARG_OID(3); */
143  bool *recheck = (bool *) PG_GETARG_POINTER(4);
144  float8KEY *kkk = (float8KEY *) DatumGetPointer(entry->key);
145  GBT_NUMKEY_R key;
146 
147  /* All cases served by this function are exact */
148  *recheck = false;
149 
150  key.lower = (GBT_NUMKEY *) &kkk->lower;
151  key.upper = (GBT_NUMKEY *) &kkk->upper;
152 
154  gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
155  );
156 }
157 
158 
159 Datum
161 {
162  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
163  float8 query = PG_GETARG_FLOAT8(1);
164 
165  /* Oid subtype = PG_GETARG_OID(3); */
166  float8KEY *kkk = (float8KEY *) DatumGetPointer(entry->key);
167  GBT_NUMKEY_R key;
168 
169  key.lower = (GBT_NUMKEY *) &kkk->lower;
170  key.upper = (GBT_NUMKEY *) &kkk->upper;
171 
173  gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
174  );
175 }
176 
177 
178 Datum
180 {
182  void *out = palloc(sizeof(float8KEY));
183 
184  *(int *) PG_GETARG_POINTER(1) = sizeof(float8KEY);
185  PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
186 }
187 
188 
189 Datum
191 {
192  float8KEY *origentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
193  float8KEY *newentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
194  float *result = (float *) PG_GETARG_POINTER(2);
195 
196  penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
197 
198  PG_RETURN_POINTER(result);
199 
200 }
201 
202 Datum
204 {
208  &tinfo, fcinfo->flinfo
209  ));
210 }
211 
212 Datum
214 {
217  bool *result = (bool *) PG_GETARG_POINTER(2);
218 
219  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
220  PG_RETURN_POINTER(result);
221 }
#define GIST_LEAF(entry)
Definition: gist.h:133
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
static const gbtree_ninfo tinfo
Definition: btree_float8.c:85
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
Definition: fmgr.h:56
const GBT_NUMKEY * lower
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
Datum gbt_float8_fetch(PG_FUNCTION_ARGS)
Definition: btree_float8.c:128
Datum gbt_float8_picksplit(PG_FUNCTION_ARGS)
Definition: btree_float8.c:203
Datum gbt_float8_penalty(PG_FUNCTION_ARGS)
Definition: btree_float8.c:190
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
Datum float8_dist(PG_FUNCTION_ARGS)
Definition: btree_float8.c:102
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
uint16 StrategyNumber
Definition: stratnum.h:22
return result
Definition: formatting.c:1618
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)
static float8 gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:72
#define Abs(x)
Definition: c.h:812
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
struct float8key float8KEY
static bool gbt_float8lt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:49
double float8
Definition: c.h:381
Datum gbt_float8_distance(PG_FUNCTION_ARGS)
Definition: btree_float8.c:160
Datum gbt_float8_union(PG_FUNCTION_ARGS)
Definition: btree_float8.c:179
#define penalty_num(result, olower, oupper, nlower, nupper)
int isinf(double x)
Datum key
Definition: gist.h:123
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
char GBT_NUMKEY
float8 upper
Definition: btree_float8.c:12
Datum gbt_float8_same(PG_FUNCTION_ARGS)
Definition: btree_float8.c:213
static int gbt_float8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:55
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uintptr_t Datum
Definition: postgres.h:372
static bool gbt_float8eq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:39
PG_FUNCTION_INFO_V1(gbt_float8_compress)
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
Datum gbt_float8_compress(PG_FUNCTION_ARGS)
Definition: btree_float8.c:120
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:237
#define DatumGetPointer(X)
Definition: postgres.h:555
void * palloc(Size size)
Definition: mcxt.c:849
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
static bool gbt_float8le(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:44
Datum gbt_float8_consistent(PG_FUNCTION_ARGS)
Definition: btree_float8.c:136
float8 lower
Definition: btree_float8.c:11
static bool gbt_float8ge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:34
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
static bool gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:29