]> granicus.if.org Git - postgresql/blob - contrib/btree_gin/btree_gin.c
c3f45e1afa7add95f3051a1b93654f581bfe27f7
[postgresql] / contrib / btree_gin / btree_gin.c
1 /*
2  * $PostgreSQL: pgsql/contrib/btree_gin/btree_gin.c,v 1.3 2009/08/04 18:49:50 tgl Exp $
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(DirectFunctionCall2(                                                                \
124                                 TypeInfo_##type.typecmp,                                                                        \
125                                 (data->strategy == BTLessStrategyNumber ||                                      \
126                                  data->strategy == BTLessEqualStrategyNumber)                           \
127                                  ? data->datum : a,                                                                                     \
128                                 b));                                                                                                            \
129                                                                                                                                                         \
130         switch (data->strategy)                                                                                                 \
131         {                                                                                                                                               \
132                 case BTLessStrategyNumber:                                                                                      \
133                         /* If original datum > indexed one then return match */                 \
134                         if (cmp > 0)                                                                                                    \
135                                 res = 0;                                                                                                        \
136                         else                                                                                                                    \
137                                 res = 1;                                                                                                        \
138                         break;                                                                                                                  \
139                 case BTLessEqualStrategyNumber:                                                                         \
140                         /* The same except equality */                                                                  \
141                         if (cmp >= 0)                                                                                                   \
142                                 res = 0;                                                                                                        \
143                         else                                                                                                                    \
144                                 res = 1;                                                                                                        \
145                         break;                                                                                                                  \
146                 case BTEqualStrategyNumber:                                                                                     \
147                         if (cmp != 0)                                                                                                   \
148                                 res = 1;                                                                                                        \
149                         else                                                                                                                    \
150                                 res = 0;                                                                                                        \
151                         break;                                                                                                                  \
152                 case BTGreaterEqualStrategyNumber:                                                                      \
153                         /* If original datum <= indexed one then return match */                \
154                         if (cmp <= 0)                                                                                                   \
155                                 res = 0;                                                                                                        \
156                         else                                                                                                                    \
157                                 res = 1;                                                                                                        \
158                         break;                                                                                                                  \
159                 case BTGreaterStrategyNumber:                                                                           \
160                         /* If original datum <= indexed one then return match */                \
161                         /* If original datum == indexed one then continue scan */               \
162                         if (cmp < 0)                                                                                                    \
163                                 res = 0;                                                                                                        \
164                         else if (cmp == 0)                                                                                              \
165                                 res = -1;                                                                                                       \
166                         else                                                                                                                    \
167                                 res = 1;                                                                                                        \
168                         break;                                                                                                                  \
169                 default:                                                                                                                        \
170                         elog(ERROR, "unrecognized strategy number: %d",                                 \
171                                  data->strategy);                                                                                       \
172                         res = 0;                                                                                                                \
173         }                                                                                                                                               \
174                                                                                                                                                         \
175         PG_RETURN_INT32(res);                                                                                                   \
176 }
177
178 #define GIN_SUPPORT(type)                       \
179         GIN_EXTRACT_VALUE(type)                 \
180         GIN_EXTRACT_QUERY(type)                 \
181         GIN_COMPARE_PREFIX(type)
182
183
184 PG_FUNCTION_INFO_V1(gin_btree_consistent);
185 Datum           gin_btree_consistent(PG_FUNCTION_ARGS);
186 Datum
187 gin_btree_consistent(PG_FUNCTION_ARGS)
188 {
189         bool       *recheck = (bool *) PG_GETARG_POINTER(5);
190
191         *recheck = false;
192         PG_RETURN_BOOL(true);
193 }
194
195 static Datum
196 leftmostvalue_int2(void)
197 {
198         return Int16GetDatum(SHRT_MIN);
199 }
200 static TypeInfo TypeInfo_int2 = {false, leftmostvalue_int2, btint2cmp};
201
202 GIN_SUPPORT(int2)
203
204 static Datum
205 leftmostvalue_int4(void)
206 {
207         return Int32GetDatum(INT_MIN);
208 }
209 static TypeInfo TypeInfo_int4 = {false, leftmostvalue_int4, btint4cmp};
210
211 GIN_SUPPORT(int4)
212
213 static Datum
214 leftmostvalue_int8(void)
215 {
216         /*
217          * Use sequence's definition to keep compatibility. Another way may make a
218          * problem with INT64_IS_BUSTED
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. Another way may make a
249          * problem with INT64_IS_BUSTED
250          */
251         return Int64GetDatum(SEQ_MINVALUE);
252 }
253 static TypeInfo TypeInfo_money = {false, leftmostvalue_money, cash_cmp};
254
255 GIN_SUPPORT(money)
256
257 static Datum
258 leftmostvalue_oid(void)
259 {
260         return ObjectIdGetDatum(0);
261 }
262 static TypeInfo TypeInfo_oid = {false, leftmostvalue_oid, btoidcmp};
263
264 GIN_SUPPORT(oid)
265
266 static Datum
267 leftmostvalue_timestamp(void)
268 {
269         return TimestampGetDatum(DT_NOBEGIN);
270 }
271 static TypeInfo TypeInfo_timestamp = {false, leftmostvalue_timestamp, timestamp_cmp};
272
273 GIN_SUPPORT(timestamp)
274
275 static TypeInfo TypeInfo_timestamptz = {false, leftmostvalue_timestamp, timestamp_cmp};
276
277 GIN_SUPPORT(timestamptz)
278
279 static Datum
280 leftmostvalue_time(void)
281 {
282         return TimeADTGetDatum(0);
283 }
284 static TypeInfo TypeInfo_time = {false, leftmostvalue_time, time_cmp};
285
286 GIN_SUPPORT(time)
287
288 static Datum
289 leftmostvalue_timetz(void)
290 {
291         TimeTzADT  *v = palloc(sizeof(TimeTzADT));
292
293         v->time = 0;
294         v->zone = -24 * 3600;           /* XXX is that true? */
295
296         return TimeTzADTPGetDatum(v);
297 }
298 static TypeInfo TypeInfo_timetz = {false, leftmostvalue_timetz, timetz_cmp};
299
300 GIN_SUPPORT(timetz)
301
302 static Datum
303 leftmostvalue_date(void)
304 {
305         return DateADTGetDatum(DATEVAL_NOBEGIN);
306 }
307 static TypeInfo TypeInfo_date = {false, leftmostvalue_date, date_cmp};
308
309 GIN_SUPPORT(date)
310
311 static Datum
312 leftmostvalue_interval(void)
313 {
314         Interval   *v = palloc(sizeof(Interval));
315
316         v->time = DT_NOBEGIN;
317         v->day = 0;
318         v->month = 0;
319         return IntervalPGetDatum(v);
320 }
321 static TypeInfo TypeInfo_interval = {false, leftmostvalue_interval, interval_cmp};
322
323 GIN_SUPPORT(interval)
324
325 static Datum
326 leftmostvalue_macaddr(void)
327 {
328         macaddr    *v = palloc0(sizeof(macaddr));
329
330         return MacaddrPGetDatum(v);
331 }
332 static TypeInfo TypeInfo_macaddr = {false, leftmostvalue_macaddr, macaddr_cmp};
333
334 GIN_SUPPORT(macaddr)
335
336 static Datum
337 leftmostvalue_inet(void)
338 {
339         return DirectFunctionCall3(inet_in,
340                                                            CStringGetDatum("0.0.0.0/0"),
341                                                            ObjectIdGetDatum(0),
342                                                            Int32GetDatum(-1));
343 }
344 static TypeInfo TypeInfo_inet = {true, leftmostvalue_inet, network_cmp};
345
346 GIN_SUPPORT(inet)
347
348 static TypeInfo TypeInfo_cidr = {true, leftmostvalue_inet, network_cmp};
349
350 GIN_SUPPORT(cidr)
351
352 static Datum
353 leftmostvalue_text(void)
354 {
355         return PointerGetDatum(cstring_to_text_with_len("", 0));
356 }
357 static TypeInfo TypeInfo_text = {true, leftmostvalue_text, bttextcmp};
358
359 GIN_SUPPORT(text)
360
361 static Datum
362 leftmostvalue_char(void)
363 {
364         return CharGetDatum(SCHAR_MIN);
365 }
366 static TypeInfo TypeInfo_char = {false, leftmostvalue_char, btcharcmp};
367
368 GIN_SUPPORT(char)
369
370 static TypeInfo TypeInfo_bytea = {true, leftmostvalue_text, byteacmp};
371
372 GIN_SUPPORT(bytea)
373
374 static Datum
375 leftmostvalue_bit(void)
376 {
377         return DirectFunctionCall3(bit_in,
378                                                            CStringGetDatum(""),
379                                                            ObjectIdGetDatum(0),
380                                                            Int32GetDatum(-1));
381 }
382 static TypeInfo TypeInfo_bit = {true, leftmostvalue_bit, bitcmp};
383
384 GIN_SUPPORT(bit)
385
386 static Datum
387 leftmostvalue_varbit(void)
388 {
389         return DirectFunctionCall3(varbit_in,
390                                                            CStringGetDatum(""),
391                                                            ObjectIdGetDatum(0),
392                                                            Int32GetDatum(-1));
393 }
394 static TypeInfo TypeInfo_varbit = {true, leftmostvalue_varbit, bitcmp};
395
396 GIN_SUPPORT(varbit)
397
398 /*
399  * Numeric type hasn't applicable left-most value, so NULL
400  * is used for that. NULL will never be an argument for a C-level
401  * numeric function except gin_numeric_cmp and it will not be stored
402  * somewhere and it could not be returned in any user SQL query.
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)