PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 "btree_gist.h"
7 
8 #include <math.h>
9 #include <float.h>
10 
11 #include "btree_utils_var.h"
12 #include "utils/builtins.h"
13 #include "utils/numeric.h"
14 #include "utils/rel.h"
15 
16 /*
17 ** Bytea ops
18 */
25 
26 
27 /* define for comparison */
28 
29 static bool
30 gbt_numeric_gt(const void *a, const void *b, Oid collation)
31 {
33  PointerGetDatum(a),
34  PointerGetDatum(b)));
35 }
36 
37 static bool
38 gbt_numeric_ge(const void *a, const void *b, Oid collation)
39 {
41  PointerGetDatum(a),
42  PointerGetDatum(b)));
43 }
44 
45 static bool
46 gbt_numeric_eq(const void *a, const void *b, Oid collation)
47 {
49  PointerGetDatum(a),
50  PointerGetDatum(b)));
51 }
52 
53 static bool
54 gbt_numeric_le(const void *a, const void *b, Oid collation)
55 {
57  PointerGetDatum(a),
58  PointerGetDatum(b)));
59 }
60 
61 static bool
62 gbt_numeric_lt(const void *a, const void *b, Oid collation)
63 {
65  PointerGetDatum(a),
66  PointerGetDatum(b)));
67 }
68 
69 static int32
70 gbt_numeric_cmp(const void *a, const void *b, Oid collation)
71 {
73  PointerGetDatum(a),
74  PointerGetDatum(b)));
75 }
76 
77 
78 static const gbtree_vinfo tinfo =
79 {
81  0,
82  FALSE,
89  NULL
90 };
91 
92 
93 /**************************************************
94  * Text ops
95  **************************************************/
96 
97 
98 Datum
100 {
101  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
102 
103  PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
104 }
105 
106 
107 
108 Datum
110 {
111  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
112  void *query = (void *) DatumGetNumeric(PG_GETARG_DATUM(1));
114 
115  /* Oid subtype = PG_GETARG_OID(3); */
116  bool *recheck = (bool *) PG_GETARG_POINTER(4);
117  bool retval;
118  GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
120 
121  /* All cases served by this function are exact */
122  *recheck = false;
123 
124  retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
125  GIST_LEAF(entry), &tinfo);
126  PG_RETURN_BOOL(retval);
127 }
128 
129 
130 
131 Datum
133 {
135  int32 *size = (int *) PG_GETARG_POINTER(1);
136 
138  &tinfo));
139 }
140 
141 
142 Datum
144 {
145  Datum d1 = PG_GETARG_DATUM(0);
146  Datum d2 = PG_GETARG_DATUM(1);
147  bool *result = (bool *) PG_GETARG_POINTER(2);
148 
149  *result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo);
150  PG_RETURN_POINTER(result);
151 }
152 
153 
154 Datum
156 {
159  float *result = (float *) PG_GETARG_POINTER(2);
160 
161  Numeric us,
162  os,
163  ds;
164 
165  GBT_VARKEY *org = (GBT_VARKEY *) DatumGetPointer(o->key);
166  GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
167  Datum uni;
168  GBT_VARKEY_R rk,
169  ok,
170  uk;
171 
172  rk = gbt_var_key_readable(org);
173  uni = PointerGetDatum(gbt_var_key_copy(&rk));
174  gbt_var_bin_union(&uni, newe, PG_GET_COLLATION(), &tinfo);
175  ok = gbt_var_key_readable(org);
177 
179  numeric_sub,
182  ));
183 
185  numeric_sub,
188  ));
189 
191  numeric_sub,
192  NumericGetDatum(us),
193  NumericGetDatum(os)
194  ));
195 
196  if (numeric_is_nan(us))
197  {
198  if (numeric_is_nan(os))
199  *result = 0.0;
200  else
201  *result = 1.0;
202  }
203  else
204  {
206 
207  *result = 0.0;
208 
210  {
211  *result += FLT_MIN;
213  numeric_div,
214  NumericGetDatum(ds),
215  NumericGetDatum(us)
216  ));
218  }
219  }
220 
221  if (*result > 0)
222  *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
223 
224  PG_RETURN_POINTER(result);
225 }
226 
227 
228 
229 Datum
231 {
234 
235  gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
236  &tinfo);
238 }
#define GIST_LEAF(entry)
Definition: gist.h:133
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
static bool gbt_numeric_lt(const void *a, const void *b, Oid collation)
Definition: btree_numeric.c:62
Datum gbt_numeric_union(PG_FUNCTION_ARGS)
#define DatumGetInt32(X)
Definition: postgres.h:480
void gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo)
Datum gbt_numeric_penalty(PG_FUNCTION_ARGS)
#define PointerGetDatum(X)
Definition: postgres.h:564
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
Datum numeric_gt(PG_FUNCTION_ARGS)
Definition: numeric.c:2066
#define NumericGetDatum(X)
Definition: numeric.h:51
static bool gbt_numeric_le(const void *a, const void *b, Oid collation)
Definition: btree_numeric.c:54
Datum numeric_cmp(PG_FUNCTION_ARGS)
Definition: numeric.c:2020
bool gbt_var_same(Datum d1, Datum d2, Oid collation, const gbtree_vinfo *tinfo)
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)
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:555
unsigned int Oid
Definition: postgres_ext.h:31
Datum numeric_div(PG_FUNCTION_ARGS)
Definition: numeric.c:2366
#define PG_GET_COLLATION()
Definition: fmgr.h:155
static bool gbt_numeric_gt(const void *a, const void *b, Oid collation)
Definition: btree_numeric.c:30
signed int int32
Definition: c.h:253
Datum numeric_eq(PG_FUNCTION_ARGS)
Definition: numeric.c:2036
bool numeric_is_nan(Numeric num)
Definition: numeric.c:669
#define FALSE
Definition: c.h:218
Datum key
Definition: gist.h:123
Datum numeric_le(PG_FUNCTION_ARGS)
Definition: numeric.c:2111
Datum gbt_numeric_compress(PG_FUNCTION_ARGS)
Definition: btree_numeric.c:99
#define DatumGetBool(X)
Definition: postgres.h:401
PG_FUNCTION_INFO_V1(gbt_numeric_compress)
Datum gbt_numeric_consistent(PG_FUNCTION_ARGS)
GIST_SPLITVEC * gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, Oid collation, const gbtree_vinfo *tinfo)
float float4
Definition: c.h:377
Datum int4_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2902
#define DatumGetFloat8(X)
Definition: postgres.h:736
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
Datum numeric_sub(PG_FUNCTION_ARGS)
Definition: numeric.c:2286
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
GBT_VARKEY * gbt_var_union(const GistEntryVector *entryvec, int32 *size, Oid collation, const gbtree_vinfo *tinfo)
GISTENTRY * gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo *tinfo)
Datum numeric_lt(PG_FUNCTION_ARGS)
Definition: numeric.c:2096
#define DatumGetNumeric(X)
Definition: numeric.h:49
#define NULL
Definition: c.h:226
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:3118
static int32 gbt_numeric_cmp(const void *a, const void *b, Oid collation)
Definition: btree_numeric.c:70
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:228
#define DatumGetPointer(X)
Definition: postgres.h:557
Datum gbt_numeric_picksplit(PG_FUNCTION_ARGS)
#define Int32GetDatum(X)
Definition: postgres.h:487
Definition: c.h:435
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
static bool gbt_numeric_ge(const void *a, const void *b, Oid collation)
Definition: btree_numeric.c:38
Datum numeric_ge(PG_FUNCTION_ARGS)
Definition: numeric.c:2081
static const gbtree_vinfo tinfo
Definition: btree_numeric.c:78
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
static bool gbt_numeric_eq(const void *a, const void *b, Oid collation)
Definition: btree_numeric.c:46