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 {
33  PointerGetDatum(b)));
34 }
35 
36 static bool
37 gbt_numeric_ge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
38 {
41  PointerGetDatum(b)));
42 }
43 
44 static bool
45 gbt_numeric_eq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
46 {
49  PointerGetDatum(b)));
50 }
51 
52 static bool
53 gbt_numeric_le(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
54 {
57  PointerGetDatum(b)));
58 }
59 
60 static bool
61 gbt_numeric_lt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
62 {
65  PointerGetDatum(b)));
66 }
67 
68 static int32
69 gbt_numeric_cmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
70 {
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 
103 }
104 
105 
106 
107 Datum
109 {
110  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
111  void *query = 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 
179  PointerGetDatum(uk.lower)));
180 
183  PointerGetDatum(ok.lower)));
184 
186  NumericGetDatum(us),
187  NumericGetDatum(os)));
188 
189  if (numeric_is_nan(us))
190  {
191  if (numeric_is_nan(os))
192  *result = 0.0;
193  else
194  *result = 1.0;
195  }
196  else
197  {
198  Numeric nul = int64_to_numeric(0);
199 
200  *result = 0.0;
201 
203  {
204  *result += FLT_MIN;
206  NumericGetDatum(ds),
207  NumericGetDatum(us)));
209  }
210  }
211 
212  if (*result > 0)
213  *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
214 
215  PG_RETURN_POINTER(result);
216 }
217 
218 
219 
220 Datum
222 {
225 
226  gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
227  &tinfo, fcinfo->flinfo);
229 }
Datum numeric_div(PG_FUNCTION_ARGS)
Definition: numeric.c:3242
Datum numeric_sub(PG_FUNCTION_ARGS)
Definition: numeric.c:3043
Datum numeric_cmp(PG_FUNCTION_ARGS)
Definition: numeric.c:2517
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4401
Datum numeric_ge(PG_FUNCTION_ARGS)
Definition: numeric.c:2578
Datum numeric_le(PG_FUNCTION_ARGS)
Definition: numeric.c:2608
bool numeric_is_nan(Numeric num)
Definition: numeric.c:851
Datum numeric_lt(PG_FUNCTION_ARGS)
Definition: numeric.c:2593
Datum numeric_float8_no_overflow(PG_FUNCTION_ARGS)
Definition: numeric.c:4779
Datum numeric_eq(PG_FUNCTION_ARGS)
Definition: numeric.c:2533
Datum numeric_gt(PG_FUNCTION_ARGS)
Definition: numeric.c:2563
@ gbt_t_numeric
Definition: btree_gist.h:22
Datum gbt_numeric_same(PG_FUNCTION_ARGS)
static bool gbt_numeric_gt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_numeric.c:29
Datum gbt_numeric_picksplit(PG_FUNCTION_ARGS)
static int32 gbt_numeric_cmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_numeric.c:69
static bool gbt_numeric_eq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_numeric.c:45
Datum gbt_numeric_union(PG_FUNCTION_ARGS)
static bool gbt_numeric_ge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_numeric.c:37
static const gbtree_vinfo tinfo
Definition: btree_numeric.c:77
static bool gbt_numeric_lt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_numeric.c:61
Datum gbt_numeric_consistent(PG_FUNCTION_ARGS)
static bool gbt_numeric_le(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_numeric.c:53
Datum gbt_numeric_compress(PG_FUNCTION_ARGS)
Definition: btree_numeric.c:98
PG_FUNCTION_INFO_V1(gbt_numeric_compress)
Datum gbt_numeric_penalty(PG_FUNCTION_ARGS)
GBT_VARKEY * gbt_var_union(const GistEntryVector *entryvec, int32 *size, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
bool gbt_var_consistent(GBT_VARKEY_R *key, const void *query, StrategyNumber strategy, Oid collation, bool is_leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
GIST_SPLITVEC * gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
GBT_VARKEY * gbt_var_key_copy(const GBT_VARKEY_R *u)
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
bool gbt_var_same(Datum d1, Datum d2, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
void gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo *tinfo)
int32_t int32
Definition: c.h:481
float float4
Definition: c.h:583
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#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
static Numeric DatumGetNumeric(Datum X)
Definition: numeric.h:61
static Datum NumericGetDatum(Numeric X)
Definition: numeric.h:73
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static float8 DatumGetFloat8(Datum X)
Definition: postgres.h:494
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
unsigned int Oid
Definition: postgres_ext.h:31
static pg_noinline void Size size
Definition: slab.c:607
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: fmgr.h:57
Datum key
Definition: gist.h:160
Definition: c.h:641