OSDN Git Service

Regression tests for new btree_gist "not equals" support.
[pg-rex/syncrep.git] / contrib / btree_gist / btree_float4.c
1 /*
2  * $PostgreSQL: pgsql/contrib/btree_gist/btree_float4.c,v 1.10 2010/02/26 02:00:31 momjian Exp $
3  */
4 #include "btree_gist.h"
5 #include "btree_utils_num.h"
6
7 typedef struct float4key
8 {
9         float4          lower;
10         float4          upper;
11 } float4KEY;
12
13 /*
14 ** float4 ops
15 */
16 PG_FUNCTION_INFO_V1(gbt_float4_compress);
17 PG_FUNCTION_INFO_V1(gbt_float4_union);
18 PG_FUNCTION_INFO_V1(gbt_float4_picksplit);
19 PG_FUNCTION_INFO_V1(gbt_float4_consistent);
20 PG_FUNCTION_INFO_V1(gbt_float4_penalty);
21 PG_FUNCTION_INFO_V1(gbt_float4_same);
22
23 Datum           gbt_float4_compress(PG_FUNCTION_ARGS);
24 Datum           gbt_float4_union(PG_FUNCTION_ARGS);
25 Datum           gbt_float4_picksplit(PG_FUNCTION_ARGS);
26 Datum           gbt_float4_consistent(PG_FUNCTION_ARGS);
27 Datum           gbt_float4_penalty(PG_FUNCTION_ARGS);
28 Datum           gbt_float4_same(PG_FUNCTION_ARGS);
29
30 static bool
31 gbt_float4gt(const void *a, const void *b)
32 {
33         return (*((float4 *) a) > *((float4 *) b));
34 }
35 static bool
36 gbt_float4ge(const void *a, const void *b)
37 {
38         return (*((float4 *) a) >= *((float4 *) b));
39 }
40 static bool
41 gbt_float4eq(const void *a, const void *b)
42 {
43         return (*((float4 *) a) == *((float4 *) b));
44 }
45 static bool
46 gbt_float4le(const void *a, const void *b)
47 {
48         return (*((float4 *) a) <= *((float4 *) b));
49 }
50 static bool
51 gbt_float4lt(const void *a, const void *b)
52 {
53         return (*((float4 *) a) < *((float4 *) b));
54 }
55
56 static int
57 gbt_float4key_cmp(const void *a, const void *b)
58 {
59         float4KEY  *ia = (float4KEY *) (((Nsrt *) a)->t);
60         float4KEY  *ib = (float4KEY *) (((Nsrt *) b)->t);
61
62         if (ia->lower == ib->lower)
63         {
64                 if (ia->upper == ib->upper)
65                         return 0;
66
67                 return (ia->upper > ib->upper) ? 1 : -1;
68         }
69
70         return (ia->lower > ib->lower) ? 1 : -1;
71 }
72
73
74 static const gbtree_ninfo tinfo =
75 {
76         gbt_t_float4,
77         sizeof(float4),
78         gbt_float4gt,
79         gbt_float4ge,
80         gbt_float4eq,
81         gbt_float4le,
82         gbt_float4lt,
83         gbt_float4key_cmp
84 };
85
86
87 /**************************************************
88  * float4 ops
89  **************************************************/
90
91
92 Datum
93 gbt_float4_compress(PG_FUNCTION_ARGS)
94 {
95         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
96         GISTENTRY  *retval = NULL;
97
98         PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
99 }
100
101
102 Datum
103 gbt_float4_consistent(PG_FUNCTION_ARGS)
104 {
105         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
106         float4          query = PG_GETARG_FLOAT4(1);
107         StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
108
109         /* Oid          subtype = PG_GETARG_OID(3); */
110         bool       *recheck = (bool *) PG_GETARG_POINTER(4);
111         float4KEY  *kkk = (float4KEY *) DatumGetPointer(entry->key);
112         GBT_NUMKEY_R key;
113
114         /* All cases served by this function are exact */
115         *recheck = false;
116
117         key.lower = (GBT_NUMKEY *) &kkk->lower;
118         key.upper = (GBT_NUMKEY *) &kkk->upper;
119
120         PG_RETURN_BOOL(
121                                    gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
122                 );
123 }
124
125
126 Datum
127 gbt_float4_union(PG_FUNCTION_ARGS)
128 {
129         GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
130         void       *out = palloc(sizeof(float4KEY));
131
132         *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
133         PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
134 }
135
136
137 Datum
138 gbt_float4_penalty(PG_FUNCTION_ARGS)
139 {
140         float4KEY  *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
141         float4KEY  *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
142         float      *result = (float *) PG_GETARG_POINTER(2);
143
144         penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
145
146         PG_RETURN_POINTER(result);
147
148 }
149
150 Datum
151 gbt_float4_picksplit(PG_FUNCTION_ARGS)
152 {
153         PG_RETURN_POINTER(gbt_num_picksplit(
154                                                                         (GistEntryVector *) PG_GETARG_POINTER(0),
155                                                                           (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
156                                                                                 &tinfo
157                                                                                 ));
158 }
159
160 Datum
161 gbt_float4_same(PG_FUNCTION_ARGS)
162 {
163         float4KEY  *b1 = (float4KEY *) PG_GETARG_POINTER(0);
164         float4KEY  *b2 = (float4KEY *) PG_GETARG_POINTER(1);
165         bool       *result = (bool *) PG_GETARG_POINTER(2);
166
167         *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
168         PG_RETURN_POINTER(result);
169 }