PostgreSQL Source Code  git master
btree_numeric.c
Go to the documentation of this file.
1 /*
2  * contrib/btree_gist/btree_numeric.c
3  */
4 #include "postgres.h"
5 
6 #include <math.h>
7 #include <float.h>
8 
9 #include "btree_gist.h"
10 #include "btree_utils_var.h"
11 #include "utils/builtins.h"
12 #include "utils/numeric.h"
13 #include "utils/rel.h"
14 
15 /*
16 ** Bytea ops
17 */
24 
25 
26 /* define for comparison */
27 
28 static bool
29 gbt_numeric_gt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
30 {
32  PointerGetDatum(a),
33  PointerGetDatum(b)));
34 }
35 
36 static bool
37 gbt_numeric_ge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
38 {
40  PointerGetDatum(a),
41  PointerGetDatum(b)));
42 }
43 
44 static bool
45 gbt_numeric_eq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
46 {
48  PointerGetDatum(a),
49  PointerGetDatum(b)));
50 }
51 
52 static bool
53 gbt_numeric_le(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
54 {
56  PointerGetDatum(a),
57  PointerGetDatum(b)));
58 }
59 
60 static bool
61 gbt_numeric_lt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
62 {
64  PointerGetDatum(a),
65  PointerGetDatum(b)));
66 }
67 
68 static int32
69 gbt_numeric_cmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
70 {
72  PointerGetDatum(a),
73  PointerGetDatum(b)));
74 }
75 
76 
77 static const gbtree_vinfo tinfo =
78 {
80  0,
81  false,
88  NULL
89 };
90 
91 
92 /**************************************************
93  * Text ops
94  **************************************************/
95 
96 
97 Datum
99 {
100  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
101 
102  PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
103 }
104 
105 
106 
107 Datum
109 {
110  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
111  void *query = (void *) DatumGetNumeric(PG_GETARG_DATUM(1));
113 
114  /* Oid subtype = PG_GETARG_OID(3); */
115  bool *recheck = (bool *) PG_GETARG_POINTER(4);
116  bool retval;
119 
120  /* All cases served by this function are exact */
121  *recheck = false;
122 
123  retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
124  GIST_LEAF(entry), &tinfo, fcinfo->flinfo);
125  PG_RETURN_BOOL(retval);
126 }
127 
128 
129 
130 Datum
132 {
134  int32 *size = (int *) PG_GETARG_POINTER(1);
135 
137  &tinfo, fcinfo->flinfo));
138 }
139 
140 
141 Datum
143 {
144  Datum d1 = PG_GETARG_DATUM(0);
145  Datum d2 = PG_GETARG_DATUM(1);
146  bool *result = (bool *) PG_GETARG_POINTER(2);
147 
148  *result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
149  PG_RETURN_POINTER(result);
150 }
151 
152 
153 Datum
155 {
158  float *result = (float *) PG_GETARG_POINTER(2);
159 
160  Numeric us,
161  os,
162  ds;
163 
164  GBT_VARKEY *org = (GBT_VARKEY *) DatumGetPointer(o->key);
165  GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
166  Datum uni;
167  GBT_VARKEY_R rk,
168  ok,
169  uk;
170 
171  rk = gbt_var_key_readable(org);
172  uni = PointerGetDatum(gbt_var_key_copy(&rk));
173  gbt_var_bin_union(&uni, newe, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
174  ok = gbt_var_key_readable(org);
176 
178  numeric_sub,
181  ));
182 
184  numeric_sub,
187  ));
188 
190  numeric_sub,
191  NumericGetDatum(us),
192  NumericGetDatum(os)
193  ));
194 
195  if (numeric_is_nan(us))
196  {
197  if (numeric_is_nan(os))
198  *result = 0.0;
199  else
200  *result = 1.0;
201  }
202  else
203  {
205 
206  *result = 0.0;
207 
209  {
210  *result += FLT_MIN;
212  numeric_div,
213  NumericGetDatum(ds),
214  NumericGetDatum(us)
215  ));
217  }
218  }
219 
220  if (*result > 0)
221  *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
222 
223  PG_RETURN_POINTER(result);
224 }
225 
226 
227 
228 Datum
230 {
233 
234  gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
235  &tinfo, fcinfo->flinfo);
237 }
#define GIST_LEAF(entry)
Definition: gist.h:141
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
Definition: fmgr.h:56
Datum gbt_numeric_union(PG_FUNCTION_ARGS)
GIST_SPLITVEC * gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
static int32 gbt_numeric_cmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_numeric.c:69
#define DatumGetInt32(X)
Definition: postgres.h:472
Datum gbt_numeric_penalty(PG_FUNCTION_ARGS)
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
void gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
Datum numeric_gt(PG_FUNCTION_ARGS)
Definition: numeric.c:2092
#define NumericGetDatum(X)
Definition: numeric.h:51
Datum numeric_cmp(PG_FUNCTION_ARGS)
Definition: numeric.c:2046
uint16 StrategyNumber
Definition: stratnum.h:22
bool gbt_var_consistent(GBT_VARKEY_R *key, const void *query, StrategyNumber strategy, Oid collation, bool is_leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
bool gbt_var_same(Datum d1, Datum d2, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
unsigned int Oid
Definition: postgres_ext.h:31
Datum numeric_div(PG_FUNCTION_ARGS)
Definition: numeric.c:2580
#define PG_GET_COLLATION()
Definition: fmgr.h:193
signed int int32
Definition: c.h:347
Datum numeric_eq(PG_FUNCTION_ARGS)
Definition: numeric.c:2062
bool numeric_is_nan(Numeric num)
Definition: numeric.c:683
static bool gbt_numeric_gt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_numeric.c:29
static bool gbt_numeric_le(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_numeric.c:53
static bool gbt_numeric_eq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_numeric.c:45
Datum key
Definition: gist.h:131
Datum numeric_le(PG_FUNCTION_ARGS)
Definition: numeric.c:2137
Datum gbt_numeric_compress(PG_FUNCTION_ARGS)
Definition: btree_numeric.c:98
#define DatumGetBool(X)
Definition: postgres.h:393
PG_FUNCTION_INFO_V1(gbt_numeric_compress)
Datum gbt_numeric_consistent(PG_FUNCTION_ARGS)
float float4
Definition: c.h:491
static bool gbt_numeric_ge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_numeric.c:37
Datum int4_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3193
#define DatumGetFloat8(X)
Definition: postgres.h:728
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
uintptr_t Datum
Definition: postgres.h:367
Datum numeric_sub(PG_FUNCTION_ARGS)
Definition: numeric.c:2464
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
GISTENTRY * gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo *tinfo)
Datum numeric_lt(PG_FUNCTION_ARGS)
Definition: numeric.c:2122
static bool gbt_numeric_lt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_numeric.c:61
#define DatumGetNumeric(X)
Definition: numeric.h:49
Datum gbt_numeric_same(PG_FUNCTION_ARGS)
GBT_VARKEY * gbt_var_key_copy(const GBT_VARKEY_R *u)
Datum numeric_float8_no_overflow(PG_FUNCTION_ARGS)
Definition: numeric.c:3440
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:267
#define DatumGetPointer(X)
Definition: postgres.h:549
Datum gbt_numeric_picksplit(PG_FUNCTION_ARGS)
#define Int32GetDatum(X)
Definition: postgres.h:479
Definition: c.h:550
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
Datum numeric_ge(PG_FUNCTION_ARGS)
Definition: numeric.c:2107
static const gbtree_vinfo tinfo
Definition: btree_numeric.c:77
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:619
GBT_VARKEY * gbt_var_union(const GistEntryVector *entryvec, int32 *size, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)