PostgreSQL Source Code  git master
btree_utils_num.h
Go to the documentation of this file.
1 /*
2  * contrib/btree_gist/btree_utils_num.h
3  */
4 #ifndef __BTREE_UTILS_NUM_H__
5 #define __BTREE_UTILS_NUM_H__
6 
7 #include "btree_gist.h"
8 #include "access/gist.h"
9 #include "utils/rel.h"
10 
11 #include <math.h>
12 #include <float.h>
13 
14 typedef char GBT_NUMKEY;
15 
16 /* Better readable key */
17 typedef struct
18 {
19  const GBT_NUMKEY *lower,
20  *upper;
21 } GBT_NUMKEY_R;
22 
23 
24 /* for sorting */
25 typedef struct
26 {
27  int i;
29 } Nsrt;
30 
31 
32 /* type description */
33 
34 typedef struct
35 {
36 
37  /* Attribs */
38 
39  enum gbtree_type t; /* data type */
40  int32 size; /* size of type, 0 means variable */
41  int32 indexsize; /* size of datums stored in index */
42 
43  /* Methods */
44 
45  bool (*f_gt) (const void *, const void *, FmgrInfo *); /* greater than */
46  bool (*f_ge) (const void *, const void *, FmgrInfo *); /* greater or equal */
47  bool (*f_eq) (const void *, const void *, FmgrInfo *); /* equal */
48  bool (*f_le) (const void *, const void *, FmgrInfo *); /* less or equal */
49  bool (*f_lt) (const void *, const void *, FmgrInfo *); /* less than */
50  int (*f_cmp) (const void *, const void *, FmgrInfo *); /* key compare function */
51  float8 (*f_dist) (const void *, const void *, FmgrInfo *); /* key distance function */
52 } gbtree_ninfo;
53 
54 
55 /*
56  * Numeric btree functions
57  */
58 
59 
60 
61 /*
62  * Note: The factor 0.49 in following macro avoids floating point overflows
63  */
64 #define penalty_num(result,olower,oupper,nlower,nupper) do { \
65  double tmp = 0.0F; \
66  (*(result)) = 0.0F; \
67  if ( (nupper) > (oupper) ) \
68  tmp += ( ((double)nupper)*0.49F - ((double)oupper)*0.49F ); \
69  if ( (olower) > (nlower) ) \
70  tmp += ( ((double)olower)*0.49F - ((double)nlower)*0.49F ); \
71  if (tmp > 0.0F) \
72  { \
73  (*(result)) += FLT_MIN; \
74  (*(result)) += (float) ( ((double)(tmp)) / ( (double)(tmp) + ( ((double)(oupper))*0.49F - ((double)(olower))*0.49F ) ) ); \
75  (*(result)) *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1)); \
76  } \
77 } while (0);
78 
79 
80 /*
81  * Convert an Interval to an approximate equivalent number of seconds
82  * (as a double). Here because we need it for time/timetz as well as
83  * interval. See interval_cmp_internal for comparison.
84  */
85 #define INTERVAL_TO_SEC(ivp) \
86  (((double) (ivp)->time) / ((double) USECS_PER_SEC) + \
87  (ivp)->day * (24.0 * SECS_PER_HOUR) + \
88  (ivp)->month * (30.0 * SECS_PER_DAY))
89 
90 #define GET_FLOAT_DISTANCE(t, arg1, arg2) Abs( ((float8) *((const t *) (arg1))) - ((float8) *((const t *) (arg2))) )
91 
92 #define SAMESIGN(a,b) (((a) < 0) == ((b) < 0))
93 
94 /*
95  * check to see if a float4/8 val has underflowed or overflowed
96  * borrowed from src/backend/utils/adt/float.c
97  */
98 #define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid) \
99 do { \
100  if (isinf(val) && !(inf_is_valid)) \
101  ereport(ERROR, \
102  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), \
103  errmsg("value out of range: overflow"))); \
104  \
105  if ((val) == 0.0 && !(zero_is_valid)) \
106  ereport(ERROR, \
107  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), \
108  errmsg("value out of range: underflow"))); \
109 } while(0)
110 
111 
112 extern Interval *abs_interval(Interval *a);
113 
114 extern bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query,
115  const StrategyNumber *strategy, bool is_leaf,
116  const gbtree_ninfo *tinfo, FmgrInfo *flinfo);
117 
118 extern float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query,
119  bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo);
120 
122  const gbtree_ninfo *tinfo, FmgrInfo *flinfo);
123 
124 extern GISTENTRY *gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo);
125 
126 extern GISTENTRY *gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo);
127 
128 extern void *gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec,
129  const gbtree_ninfo *tinfo, FmgrInfo *flinfo);
130 
131 extern bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b,
132  const gbtree_ninfo *tinfo, FmgrInfo *flinfo);
133 
134 extern void gbt_num_bin_union(Datum *u, GBT_NUMKEY *e,
135  const gbtree_ninfo *tinfo, FmgrInfo *flinfo);
136 
137 #endif
Definition: fmgr.h:56
GBT_NUMKEY * t
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
gbtree_type
Definition: btree_gist.h:14
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:43
uint16 StrategyNumber
Definition: stratnum.h:22
const GBT_NUMKEY * upper
void gbt_num_bin_union(Datum *u, GBT_NUMKEY *e, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
char bool
Definition: c.h:247
signed int int32
Definition: c.h:284
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
double float8
Definition: c.h:429
char GBT_NUMKEY
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
uintptr_t Datum
Definition: postgres.h:372
static const gbtree_vinfo tinfo
Definition: btree_bit.c:110
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
Interval * abs_interval(Interval *a)
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
e
Definition: preproc-init.c:82