PostgreSQL Source Code  git master
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 #include "utils/float.h"
9 
10 typedef struct float8key
11 {
15 
16 /*
17 ** float8 ops
18 */
27 
28 
29 static bool
30 gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo)
31 {
32  return (*((const float8 *) a) > *((const float8 *) b));
33 }
34 static bool
35 gbt_float8ge(const void *a, const void *b, FmgrInfo *flinfo)
36 {
37  return (*((const float8 *) a) >= *((const float8 *) b));
38 }
39 static bool
40 gbt_float8eq(const void *a, const void *b, FmgrInfo *flinfo)
41 {
42  return (*((const float8 *) a) == *((const float8 *) b));
43 }
44 static bool
45 gbt_float8le(const void *a, const void *b, FmgrInfo *flinfo)
46 {
47  return (*((const float8 *) a) <= *((const float8 *) b));
48 }
49 static bool
50 gbt_float8lt(const void *a, const void *b, FmgrInfo *flinfo)
51 {
52  return (*((const float8 *) a) < *((const float8 *) b));
53 }
54 
55 static int
56 gbt_float8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
57 {
58  float8KEY *ia = (float8KEY *) (((const Nsrt *) a)->t);
59  float8KEY *ib = (float8KEY *) (((const Nsrt *) b)->t);
60 
61  if (ia->lower == ib->lower)
62  {
63  if (ia->upper == ib->upper)
64  return 0;
65 
66  return (ia->upper > ib->upper) ? 1 : -1;
67  }
68 
69  return (ia->lower > ib->lower) ? 1 : -1;
70 }
71 
72 static float8
73 gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
74 {
75  float8 arg1 = *(const float8 *) a;
76  float8 arg2 = *(const float8 *) b;
77  float8 r;
78 
79  r = arg1 - arg2;
80  if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2))
82  return fabs(r);
83 }
84 
85 
86 static const gbtree_ninfo tinfo =
87 {
89  sizeof(float8),
90  16, /* sizeof(gbtreekey16) */
98 };
99 
100 
102 Datum
104 {
107  float8 r;
108 
109  r = a - b;
110  if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
112 
113  PG_RETURN_FLOAT8(fabs(r));
114 }
115 
116 /**************************************************
117  * float8 ops
118  **************************************************/
119 
120 
121 Datum
123 {
124  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
125 
127 }
128 
129 Datum
131 {
132  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
133 
135 }
136 
137 Datum
139 {
140  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
141  float8 query = PG_GETARG_FLOAT8(1);
143 
144  /* Oid subtype = PG_GETARG_OID(3); */
145  bool *recheck = (bool *) PG_GETARG_POINTER(4);
146  float8KEY *kkk = (float8KEY *) DatumGetPointer(entry->key);
148 
149  /* All cases served by this function are exact */
150  *recheck = false;
151 
152  key.lower = (GBT_NUMKEY *) &kkk->lower;
153  key.upper = (GBT_NUMKEY *) &kkk->upper;
154 
155  PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
156  GIST_LEAF(entry), &tinfo,
157  fcinfo->flinfo));
158 }
159 
160 
161 Datum
163 {
164  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
165  float8 query = PG_GETARG_FLOAT8(1);
166 
167  /* Oid subtype = PG_GETARG_OID(3); */
168  float8KEY *kkk = (float8KEY *) DatumGetPointer(entry->key);
170 
171  key.lower = (GBT_NUMKEY *) &kkk->lower;
172  key.upper = (GBT_NUMKEY *) &kkk->upper;
173 
175  &tinfo, fcinfo->flinfo));
176 }
177 
178 
179 Datum
181 {
183  void *out = palloc(sizeof(float8KEY));
184 
185  *(int *) PG_GETARG_POINTER(1) = sizeof(float8KEY);
186  PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
187 }
188 
189 
190 Datum
192 {
193  float8KEY *origentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
195  float *result = (float *) PG_GETARG_POINTER(2);
196 
197  penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
198 
199  PG_RETURN_POINTER(result);
200 }
201 
202 Datum
204 {
207  &tinfo, fcinfo->flinfo));
208 }
209 
210 Datum
212 {
215  bool *result = (bool *) PG_GETARG_POINTER(2);
216 
217  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
218  PG_RETURN_POINTER(result);
219 }
Datum gbt_float8_same(PG_FUNCTION_ARGS)
Definition: btree_float8.c:211
struct float8key float8KEY
static bool gbt_float8eq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:40
Datum gbt_float8_picksplit(PG_FUNCTION_ARGS)
Definition: btree_float8.c:203
PG_FUNCTION_INFO_V1(gbt_float8_compress)
static bool gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:30
static float8 gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:73
static const gbtree_ninfo tinfo
Definition: btree_float8.c:86
Datum gbt_float8_compress(PG_FUNCTION_ARGS)
Definition: btree_float8.c:122
Datum gbt_float8_fetch(PG_FUNCTION_ARGS)
Definition: btree_float8.c:130
Datum gbt_float8_distance(PG_FUNCTION_ARGS)
Definition: btree_float8.c:162
static bool gbt_float8le(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:45
static bool gbt_float8ge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:35
static int gbt_float8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:56
Datum float8_dist(PG_FUNCTION_ARGS)
Definition: btree_float8.c:103
Datum gbt_float8_union(PG_FUNCTION_ARGS)
Definition: btree_float8.c:180
static bool gbt_float8lt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_float8.c:50
Datum gbt_float8_penalty(PG_FUNCTION_ARGS)
Definition: btree_float8.c:191
Datum gbt_float8_consistent(PG_FUNCTION_ARGS)
Definition: btree_float8.c:138
@ gbt_t_float8
Definition: btree_gist.h:21
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)
char GBT_NUMKEY
double float8
Definition: c.h:584
#define unlikely(x)
Definition: c.h:330
pg_noinline void float_overflow_error(void)
Definition: float.c:86
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#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_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define GIST_LEAF(entry)
Definition: gist.h:170
int b
Definition: isn.c:69
int a
Definition: isn.c:68
void * palloc(Size size)
Definition: mcxt.c:1317
uintptr_t Datum
Definition: postgres.h:64
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: fmgr.h:57
Datum key
Definition: gist.h:160
float8 upper
Definition: btree_float8.c:13
float8 lower
Definition: btree_float8.c:12