OSDN Git Service

De-kludge contrib/btree_gin for collations.
[pg-rex/syncrep.git] / contrib / btree_gin / btree_gin.c
1 /*
2  * contrib/btree_gin/btree_gin.c
3  */
4 #include "postgres.h"
5
6 #include <limits.h>
7
8 #include "fmgr.h"
9 #include "access/skey.h"
10 #include "utils/builtins.h"
11 #include "utils/bytea.h"
12 #include "utils/cash.h"
13 #include "utils/date.h"
14 #include "utils/inet.h"
15 #include "utils/numeric.h"
16 #include "utils/timestamp.h"
17 #include "utils/varbit.h"
18
19 PG_MODULE_MAGIC;
20
21 typedef struct TypeInfo
22 {
23         bool            is_varlena;
24         Datum           (*leftmostvalue) (void);
25         Datum           (*typecmp) (FunctionCallInfo);
26 } TypeInfo;
27
28 typedef struct QueryInfo
29 {
30         StrategyNumber strategy;
31         Datum           datum;
32 } QueryInfo;
33
34 #define  GIN_EXTRACT_VALUE(type)                                                                                        \
35 PG_FUNCTION_INFO_V1(gin_extract_value_##type);                                                          \
36 Datum           gin_extract_value_##type(PG_FUNCTION_ARGS);                                             \
37 Datum                                                                                                                                           \
38 gin_extract_value_##type(PG_FUNCTION_ARGS)                                                                      \
39 {                                                                                                                                                       \
40         Datum           datum = PG_GETARG_DATUM(0);                                                                     \
41         int32      *nentries = (int32 *) PG_GETARG_POINTER(1);                                  \
42         Datum      *entries = (Datum *) palloc(sizeof(Datum));                                  \
43                                                                                                                                                         \
44         if ( TypeInfo_##type.is_varlena )                                                                               \
45                 datum = PointerGetDatum(PG_DETOAST_DATUM(datum));                                       \
46         entries[0] = datum;                                                                                                             \
47         *nentries = 1;                                                                                                                  \
48                                                                                                                                                         \
49         PG_RETURN_POINTER(entries);                                                                                             \
50 }
51
52 /*
53  * For BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, and
54  * BTEqualStrategyNumber we want to start the index scan at the
55  * supplied query datum, and work forward. For BTLessStrategyNumber
56  * and BTLessEqualStrategyNumber, we need to start at the leftmost
57  * key, and work forward until the supplied query datum (which must be
58  * sent along inside the QueryInfo structure).
59  */
60
61 #define GIN_EXTRACT_QUERY(type)                                                                                         \
62 PG_FUNCTION_INFO_V1(gin_extract_query_##type);                                                          \
63 Datum           gin_extract_query_##type(PG_FUNCTION_ARGS);                                             \
64 Datum                                                                                                                                           \
65 gin_extract_query_##type(PG_FUNCTION_ARGS)                                                                      \
66 {                                                                                                                                                       \
67         Datum           datum = PG_GETARG_DATUM(0);                                                                     \
68         int32      *nentries = (int32 *) PG_GETARG_POINTER(1);                                  \
69         StrategyNumber strategy = PG_GETARG_UINT16(2);                                                  \
70         bool      **partialmatch = (bool **) PG_GETARG_POINTER(3);                              \
71         Pointer   **extra_data = (Pointer **) PG_GETARG_POINTER(4);                             \
72         Datum      *entries = (Datum *) palloc(sizeof(Datum));                                  \
73         QueryInfo  *data = (QueryInfo *) palloc(sizeof(QueryInfo));                             \
74         bool       *ptr_partialmatch;                                                                                   \
75                                                                                                                                                         \
76         *nentries = 1;                                                                                                                  \
77         ptr_partialmatch = *partialmatch = (bool *) palloc(sizeof(bool));               \
78         *ptr_partialmatch = false;                                                                                              \
79         if ( TypeInfo_##type.is_varlena )                                                                               \
80                 datum = PointerGetDatum(PG_DETOAST_DATUM(datum));                                       \
81         data->strategy = strategy;                                                                                              \
82         data->datum = datum;                                                                                                    \
83         *extra_data = (Pointer *) palloc(sizeof(Pointer));                                              \
84         **extra_data = (Pointer) data;                                                                                  \
85                                                                                                                                                         \
86         switch (strategy)                                                                                                               \
87         {                                                                                                                                               \
88                 case BTLessStrategyNumber:                                                                                      \
89                 case BTLessEqualStrategyNumber:                                                                         \
90                         entries[0] = TypeInfo_##type.leftmostvalue();                                   \
91                         *ptr_partialmatch = true;                                                                               \
92                         break;                                                                                                                  \
93                 case BTGreaterEqualStrategyNumber:                                                                      \
94                 case BTGreaterStrategyNumber:                                                                           \
95                         *ptr_partialmatch = true;                                                                               \
96                 case BTEqualStrategyNumber:                                                                                     \
97                         entries[0] = datum;                                                                                             \
98                         break;                                                                                                                  \
99                 default:                                                                                                                        \
100                         elog(ERROR, "unrecognized strategy number: %d", strategy);              \
101         }                                                                                                                                               \
102                                                                                                                                                         \
103         PG_RETURN_POINTER(entries);                                                                                             \
104 }
105
106 /*
107  * Datum a is a value from extract_query method and for BTLess*
108  * strategy it is a left-most value.  So, use original datum from QueryInfo
109  * to decide to stop scanning or not.  Datum b is always from index.
110  */
111 #define GIN_COMPARE_PREFIX(type)                                                                                        \
112 PG_FUNCTION_INFO_V1(gin_compare_prefix_##type);                                                         \
113 Datum           gin_compare_prefix_##type(PG_FUNCTION_ARGS);                                    \
114 Datum                                                                                                                                           \
115 gin_compare_prefix_##type(PG_FUNCTION_ARGS)                                                                     \
116 {                                                                                                                                                       \
117         Datum           a = PG_GETARG_DATUM(0);                                                                         \
118         Datum           b = PG_GETARG_DATUM(1);                                                                         \
119         QueryInfo  *data = (QueryInfo *) PG_GETARG_POINTER(3);                                  \
120         int32           res,                                                                                                            \
121                                 cmp;                                                                                                            \
122                                                                                                                                                         \
123         cmp = DatumGetInt32(DirectFunctionCall2Coll(                                                    \
124                                 TypeInfo_##type.typecmp,                                                                        \
125                                 PG_GET_COLLATION(),                                                                                     \
126                                 (data->strategy == BTLessStrategyNumber ||                                      \
127                                  data->strategy == BTLessEqualStrategyNumber)                           \
128                                  ? data->datum : a,                                                                                     \
129                                 b));                                                                                                            \
130                                                                                                                                                         \
131         switch (data->strategy)                                                                                                 \
132         {                                                                                                                                               \
133                 case BTLessStrategyNumber:                                                                                      \
134                         /* If original datum > indexed one then return match */                 \
135                         if (cmp > 0)                                                                                                    \
136                                 res = 0;                                                                                                        \
137                         else                                                                                                                    \
138                                 res = 1;                                                                                                        \
139                         break;                                                                                                                  \
140                 case BTLessEqualStrategyNumber:                                                                         \
141                         /* The same except equality */                                                                  \
142                         if (cmp >= 0)                                                                                                   \
143                                 res = 0;                                                                                                        \
144                         else                                                                                                                    \
145                                 res = 1;                                                                                                        \
146                         break;                                                                                                                  \
147                 case BTEqualStrategyNumber:                                                                                     \
148                         if (cmp != 0)                                                                                                   \
149                                 res = 1;                                                                                                        \
150                         else                                                                                                                    \
151                                 res = 0;                                                                                                        \
152                         break;                                                                                                                  \
153                 case BTGreaterEqualStrategyNumber:                                                                      \
154                         /* If original datum <= indexed one then return match */                \
155                         if (cmp <= 0)                                                                                                   \
156                                 res = 0;                                                                                                        \
157                         else                                                                                                                    \
158                                 res = 1;                                                                                                        \
159                         break;                                                                                                                  \
160                 case BTGreaterStrategyNumber:                                                                           \
161                         /* If original datum <= indexed one then return match */                \
162                         /* If original datum == indexed one then continue scan */               \
163                         if (cmp < 0)                                                                                                    \
164                                 res = 0;                                                                                                        \
165                         else if (cmp == 0)                                                                                              \
166                                 res = -1;                                                                                                       \
167                         else                                                                                                                    \
168                                 res = 1;                                                                                                        \
169                         break;                                                                                                                  \
170                 default:                                                                                                                        \
171                         elog(ERROR, "unrecognized strategy number: %d",                                 \
172                                  data->strategy);                                                                                       \
173                         res = 0;                                                                                                                \
174         }                                                                                                                                               \
175                                                                                                                                                         \
176         PG_RETURN_INT32(res);                                                                                                   \
177 }
178
179 #define GIN_SUPPORT(type)                       \
180         GIN_EXTRACT_VALUE(type)                 \
181         GIN_EXTRACT_QUERY(type)                 \
182         GIN_COMPARE_PREFIX(type)
183
184
185 PG_FUNCTION_INFO_V1(gin_btree_consistent);
186 Datum           gin_btree_consistent(PG_FUNCTION_ARGS);
187 Datum
188 gin_btree_consistent(PG_FUNCTION_ARGS)
189 {
190         bool       *recheck = (bool *) PG_GETARG_POINTER(5);
191
192         *recheck = false;
193         PG_RETURN_BOOL(true);
194 }
195
196 static Datum
197 leftmostvalue_int2(void)
198 {
199         return Int16GetDatum(SHRT_MIN);
200 }
201 static TypeInfo TypeInfo_int2 = {false, leftmostvalue_int2, btint2cmp};
202
203 GIN_SUPPORT(int2)
204
205 static Datum
206 leftmostvalue_int4(void)
207 {
208         return Int32GetDatum(INT_MIN);
209 }
210 static TypeInfo TypeInfo_int4 = {false, leftmostvalue_int4, btint4cmp};
211
212 GIN_SUPPORT(int4)
213
214 static Datum
215 leftmostvalue_int8(void)
216 {
217         /*
218          * Use sequence's definition to keep compatibility.
219          */
220         return Int64GetDatum(SEQ_MINVALUE);
221 }
222 static TypeInfo TypeInfo_int8 = {false, leftmostvalue_int8, btint8cmp};
223
224 GIN_SUPPORT(int8)
225
226 static Datum
227 leftmostvalue_float4(void)
228 {
229         return Float4GetDatum(-get_float4_infinity());
230 }
231 static TypeInfo TypeInfo_float4 = {false, leftmostvalue_float4, btfloat4cmp};
232
233 GIN_SUPPORT(float4)
234
235 static Datum
236 leftmostvalue_float8(void)
237 {
238         return Float8GetDatum(-get_float8_infinity());
239 }
240 static TypeInfo TypeInfo_float8 = {false, leftmostvalue_float8, btfloat8cmp};
241
242 GIN_SUPPORT(float8)
243
244 static Datum
245 leftmostvalue_money(void)
246 {
247         /*
248          * Use sequence's definition to keep compatibility.
249          */
250         return Int64GetDatum(SEQ_MINVALUE);
251 }
252 static TypeInfo TypeInfo_money = {false, leftmostvalue_money, cash_cmp};
253
254 GIN_SUPPORT(money)
255
256 static Datum
257 leftmostvalue_oid(void)
258 {
259         return ObjectIdGetDatum(0);
260 }
261 static TypeInfo TypeInfo_oid = {false, leftmostvalue_oid, btoidcmp};
262
263 GIN_SUPPORT(oid)
264
265 static Datum
266 leftmostvalue_timestamp(void)
267 {
268         return TimestampGetDatum(DT_NOBEGIN);
269 }
270 static TypeInfo TypeInfo_timestamp = {false, leftmostvalue_timestamp, timestamp_cmp};
271
272 GIN_SUPPORT(timestamp)
273
274 static TypeInfo TypeInfo_timestamptz = {false, leftmostvalue_timestamp, timestamp_cmp};
275
276 GIN_SUPPORT(timestamptz)
277
278 static Datum
279 leftmostvalue_time(void)
280 {
281         return TimeADTGetDatum(0);
282 }
283 static TypeInfo TypeInfo_time = {false, leftmostvalue_time, time_cmp};
284
285 GIN_SUPPORT(time)
286
287 static Datum
288 leftmostvalue_timetz(void)
289 {
290         TimeTzADT  *v = palloc(sizeof(TimeTzADT));
291
292         v->time = 0;
293         v->zone = -24 * 3600;           /* XXX is that true? */
294
295         return TimeTzADTPGetDatum(v);
296 }
297 static TypeInfo TypeInfo_timetz = {false, leftmostvalue_timetz, timetz_cmp};
298
299 GIN_SUPPORT(timetz)
300
301 static Datum
302 leftmostvalue_date(void)
303 {
304         return DateADTGetDatum(DATEVAL_NOBEGIN);
305 }
306 static TypeInfo TypeInfo_date = {false, leftmostvalue_date, date_cmp};
307
308 GIN_SUPPORT(date)
309
310 static Datum
311 leftmostvalue_interval(void)
312 {
313         Interval   *v = palloc(sizeof(Interval));
314
315         v->time = DT_NOBEGIN;
316         v->day = 0;
317         v->month = 0;
318         return IntervalPGetDatum(v);
319 }
320 static TypeInfo TypeInfo_interval = {false, leftmostvalue_interval, interval_cmp};
321
322 GIN_SUPPORT(interval)
323
324 static Datum
325 leftmostvalue_macaddr(void)
326 {
327         macaddr    *v = palloc0(sizeof(macaddr));
328
329         return MacaddrPGetDatum(v);
330 }
331 static TypeInfo TypeInfo_macaddr = {false, leftmostvalue_macaddr, macaddr_cmp};
332
333 GIN_SUPPORT(macaddr)
334
335 static Datum
336 leftmostvalue_inet(void)
337 {
338         return DirectFunctionCall3(inet_in,
339                                                            CStringGetDatum("0.0.0.0/0"),
340                                                            ObjectIdGetDatum(0),
341                                                            Int32GetDatum(-1));
342 }
343 static TypeInfo TypeInfo_inet = {true, leftmostvalue_inet, network_cmp};
344
345 GIN_SUPPORT(inet)
346
347 static TypeInfo TypeInfo_cidr = {true, leftmostvalue_inet, network_cmp};
348
349 GIN_SUPPORT(cidr)
350
351 static Datum
352 leftmostvalue_text(void)
353 {
354         return PointerGetDatum(cstring_to_text_with_len("", 0));
355 }
356 static TypeInfo TypeInfo_text = {true, leftmostvalue_text, bttextcmp};
357
358 GIN_SUPPORT(text)
359
360 static Datum
361 leftmostvalue_char(void)
362 {
363         return CharGetDatum(SCHAR_MIN);
364 }
365 static TypeInfo TypeInfo_char = {false, leftmostvalue_char, btcharcmp};
366
367 GIN_SUPPORT(char)
368
369 static TypeInfo TypeInfo_bytea = {true, leftmostvalue_text, byteacmp};
370
371 GIN_SUPPORT(bytea)
372
373 static Datum
374 leftmostvalue_bit(void)
375 {
376         return DirectFunctionCall3(bit_in,
377                                                            CStringGetDatum(""),
378                                                            ObjectIdGetDatum(0),
379                                                            Int32GetDatum(-1));
380 }
381 static TypeInfo TypeInfo_bit = {true, leftmostvalue_bit, bitcmp};
382
383 GIN_SUPPORT(bit)
384
385 static Datum
386 leftmostvalue_varbit(void)
387 {
388         return DirectFunctionCall3(varbit_in,
389                                                            CStringGetDatum(""),
390                                                            ObjectIdGetDatum(0),
391                                                            Int32GetDatum(-1));
392 }
393 static TypeInfo TypeInfo_varbit = {true, leftmostvalue_varbit, bitcmp};
394
395 GIN_SUPPORT(varbit)
396
397 /*
398  * Numeric type hasn't a real left-most value, so we use PointerGetDatum(NULL)
399  * (*not* a SQL NULL) to represent that.  We can get away with that because
400  * the value returned by our leftmostvalue function will never be stored in
401  * the index nor passed to anything except our compare and prefix-comparison
402  * functions.  The same trick could be used for other pass-by-reference types.
403  */
404
405 #define NUMERIC_IS_LEFTMOST(x)  ((x) == NULL)
406
407 PG_FUNCTION_INFO_V1(gin_numeric_cmp);
408 Datum           gin_numeric_cmp(PG_FUNCTION_ARGS);
409
410 Datum
411 gin_numeric_cmp(PG_FUNCTION_ARGS)
412 {
413         Numeric         a = (Numeric) PG_GETARG_POINTER(0);
414         Numeric         b = (Numeric) PG_GETARG_POINTER(1);
415         int                     res = 0;
416
417         if (NUMERIC_IS_LEFTMOST(a))
418         {
419                 res = (NUMERIC_IS_LEFTMOST(b)) ? 0 : -1;
420         }
421         else if (NUMERIC_IS_LEFTMOST(b))
422         {
423                 res = 1;
424         }
425         else
426         {
427                 res = DatumGetInt32(DirectFunctionCall2(numeric_cmp,
428                                                                                                 NumericGetDatum(a),
429                                                                                                 NumericGetDatum(b)));
430         }
431
432         PG_RETURN_INT32(res);
433 }
434
435 static Datum
436 leftmostvalue_numeric(void)
437 {
438         return PointerGetDatum(NULL);
439 }
440
441 static TypeInfo TypeInfo_numeric = {true, leftmostvalue_numeric, gin_numeric_cmp};
442
443 GIN_SUPPORT(numeric)