Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / contrib / btree_gin / btree_gin.c
blob4019f59e49ff39b44a3674fe24e90d28a60ffc3f
1 /*
2 * $PostgreSQL$
3 */
4 #include "postgres.h"
6 #include <limits.h>
8 #include "fmgr.h"
9 #include "access/skey.h"
10 #include "utils/builtins.h"
11 #include "utils/cash.h"
12 #include "utils/date.h"
13 #include "utils/inet.h"
14 #include "utils/numeric.h"
15 #include "utils/timestamp.h"
16 #include "utils/varbit.h"
18 PG_MODULE_MAGIC;
20 typedef struct TypeInfo
22 bool is_varlena;
23 Datum (*leftmostvalue) (void);
24 Datum (*typecmp) (FunctionCallInfo);
25 } TypeInfo;
27 typedef struct QueryInfo
29 StrategyNumber strategy;
30 Datum datum;
31 } QueryInfo;
33 #define GIN_EXTRACT_VALUE(type) \
34 PG_FUNCTION_INFO_V1(gin_extract_value_##type); \
35 Datum gin_extract_value_##type(PG_FUNCTION_ARGS); \
36 Datum \
37 gin_extract_value_##type(PG_FUNCTION_ARGS) \
38 { \
39 Datum datum = PG_GETARG_DATUM(0); \
40 int32 *nentries = (int32 *) PG_GETARG_POINTER(1); \
41 Datum *entries = (Datum *) palloc(sizeof(Datum)); \
43 if ( TypeInfo_##type.is_varlena ) \
44 datum = PointerGetDatum(PG_DETOAST_DATUM(datum)); \
45 entries[0] = datum; \
46 *nentries = 1; \
48 PG_RETURN_POINTER(entries); \
52 * For BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, and
53 * BTEqualStrategyNumber we want to start the index scan at the
54 * supplied query datum, and work forward. For BTLessStrategyNumber
55 * and BTLessEqualStrategyNumber, we need to start at the leftmost
56 * key, and work forward until the supplied query datum (which must be
57 * sent along inside the QueryInfo structure).
60 #define GIN_EXTRACT_QUERY(type) \
61 PG_FUNCTION_INFO_V1(gin_extract_query_##type); \
62 Datum gin_extract_query_##type(PG_FUNCTION_ARGS); \
63 Datum \
64 gin_extract_query_##type(PG_FUNCTION_ARGS) \
65 { \
66 Datum datum = PG_GETARG_DATUM(0); \
67 int32 *nentries = (int32 *) PG_GETARG_POINTER(1); \
68 StrategyNumber strategy = PG_GETARG_UINT16(2); \
69 bool **partialmatch = (bool **) PG_GETARG_POINTER(3); \
70 Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4); \
71 Datum *entries = (Datum *) palloc(sizeof(Datum)); \
72 QueryInfo *data = (QueryInfo *) palloc(sizeof(QueryInfo)); \
73 bool *ptr_partialmatch; \
75 *nentries = 1; \
76 ptr_partialmatch = *partialmatch = (bool *) palloc(sizeof(bool)); \
77 *ptr_partialmatch = false; \
78 if ( TypeInfo_##type.is_varlena ) \
79 datum = PointerGetDatum(PG_DETOAST_DATUM(datum)); \
80 data->strategy = strategy; \
81 data->datum = datum; \
82 *extra_data = (Pointer *) palloc(sizeof(Pointer)); \
83 **extra_data = (Pointer) data; \
85 switch (strategy) \
86 { \
87 case BTLessStrategyNumber: \
88 case BTLessEqualStrategyNumber: \
89 entries[0] = TypeInfo_##type.leftmostvalue(); \
90 *ptr_partialmatch = true; \
91 break; \
92 case BTGreaterEqualStrategyNumber: \
93 case BTGreaterStrategyNumber: \
94 *ptr_partialmatch = true; \
95 case BTEqualStrategyNumber: \
96 entries[0] = datum; \
97 break; \
98 default: \
99 elog(ERROR, "unrecognized strategy number: %d", strategy); \
102 PG_RETURN_POINTER(entries); \
106 * Datum a is a value from extract_query method and for BTLess*
107 * strategy it is a left-most value. So, use original datum from QueryInfo
108 * to decide to stop scanning or not. Datum b is always from index.
110 #define GIN_COMPARE_PREFIX(type) \
111 PG_FUNCTION_INFO_V1(gin_compare_prefix_##type); \
112 Datum gin_compare_prefix_##type(PG_FUNCTION_ARGS); \
113 Datum \
114 gin_compare_prefix_##type(PG_FUNCTION_ARGS) \
116 Datum a = PG_GETARG_DATUM(0); \
117 Datum b = PG_GETARG_DATUM(1); \
118 QueryInfo *data = (QueryInfo *) PG_GETARG_POINTER(3); \
119 int32 res, \
120 cmp; \
122 cmp = DatumGetInt32(DirectFunctionCall2( \
123 TypeInfo_##type.typecmp, \
124 (data->strategy == BTLessStrategyNumber || \
125 data->strategy == BTLessEqualStrategyNumber) \
126 ? data->datum : a, \
127 b)); \
129 switch (data->strategy) \
131 case BTLessStrategyNumber: \
132 /* If original datum > indexed one then return match */ \
133 if (cmp > 0) \
134 res = 0; \
135 else \
136 res = 1; \
137 break; \
138 case BTLessEqualStrategyNumber: \
139 /* The same except equality */ \
140 if (cmp >= 0) \
141 res = 0; \
142 else \
143 res = 1; \
144 break; \
145 case BTEqualStrategyNumber: \
146 if (cmp != 0) \
147 res = 1; \
148 else \
149 res = 0; \
150 break; \
151 case BTGreaterEqualStrategyNumber: \
152 /* If original datum <= indexed one then return match */ \
153 if (cmp <= 0) \
154 res = 0; \
155 else \
156 res = 1; \
157 break; \
158 case BTGreaterStrategyNumber: \
159 /* If original datum <= indexed one then return match */ \
160 /* If original datum == indexed one then continue scan */ \
161 if (cmp < 0) \
162 res = 0; \
163 else if (cmp == 0) \
164 res = -1; \
165 else \
166 res = 1; \
167 break; \
168 default: \
169 elog(ERROR, "unrecognized strategy number: %d", \
170 data->strategy); \
171 res = 0; \
174 PG_RETURN_INT32(res); \
177 #define GIN_SUPPORT(type) \
178 GIN_EXTRACT_VALUE(type) \
179 GIN_EXTRACT_QUERY(type) \
180 GIN_COMPARE_PREFIX(type)
183 PG_FUNCTION_INFO_V1(gin_btree_consistent);
184 Datum gin_btree_consistent(PG_FUNCTION_ARGS);
185 Datum
186 gin_btree_consistent(PG_FUNCTION_ARGS)
188 bool *recheck = (bool *) PG_GETARG_POINTER(5);
190 *recheck = false;
191 PG_RETURN_BOOL(true);
194 static Datum
195 leftmostvalue_int2(void)
197 return Int16GetDatum(SHRT_MIN);
199 static TypeInfo TypeInfo_int2 = {false, leftmostvalue_int2, btint2cmp};
201 GIN_SUPPORT(int2)
203 static Datum
204 leftmostvalue_int4(void)
206 return Int32GetDatum(INT_MIN);
208 static TypeInfo TypeInfo_int4 = {false, leftmostvalue_int4, btint4cmp};
210 GIN_SUPPORT(int4)
212 static Datum
213 leftmostvalue_int8(void)
216 * Use sequence's definition to keep compatibility. Another way may make a
217 * problem with INT64_IS_BUSTED
219 return Int64GetDatum(SEQ_MINVALUE);
221 static TypeInfo TypeInfo_int8 = {false, leftmostvalue_int8, btint8cmp};
223 GIN_SUPPORT(int8)
225 static Datum
226 leftmostvalue_float4(void)
228 return Float4GetDatum(-get_float4_infinity());
230 static TypeInfo TypeInfo_float4 = {false, leftmostvalue_float4, btfloat4cmp};
232 GIN_SUPPORT(float4)
234 static Datum
235 leftmostvalue_float8(void)
237 return Float8GetDatum(-get_float8_infinity());
239 static TypeInfo TypeInfo_float8 = {false, leftmostvalue_float8, btfloat8cmp};
241 GIN_SUPPORT(float8)
243 static Datum
244 leftmostvalue_money(void)
247 * Use sequence's definition to keep compatibility. Another way may make a
248 * problem with INT64_IS_BUSTED
250 return Int64GetDatum(SEQ_MINVALUE);
252 static TypeInfo TypeInfo_money = {false, leftmostvalue_money, cash_cmp};
254 GIN_SUPPORT(money)
256 static Datum
257 leftmostvalue_oid(void)
259 return ObjectIdGetDatum(0);
261 static TypeInfo TypeInfo_oid = {false, leftmostvalue_oid, btoidcmp};
263 GIN_SUPPORT(oid)
265 static Datum
266 leftmostvalue_timestamp(void)
268 return TimestampGetDatum(DT_NOBEGIN);
270 static TypeInfo TypeInfo_timestamp = {false, leftmostvalue_timestamp, timestamp_cmp};
272 GIN_SUPPORT(timestamp)
274 static TypeInfo TypeInfo_timestamptz = {false, leftmostvalue_timestamp, timestamp_cmp};
276 GIN_SUPPORT(timestamptz)
278 static Datum
279 leftmostvalue_time(void)
281 return TimeADTGetDatum(0);
283 static TypeInfo TypeInfo_time = {false, leftmostvalue_time, time_cmp};
285 GIN_SUPPORT(time)
287 static Datum
288 leftmostvalue_timetz(void)
290 TimeTzADT *v = palloc(sizeof(TimeTzADT));
292 v->time = 0;
293 v->zone = -24 * 3600; /* XXX is that true? */
295 return TimeTzADTPGetDatum(v);
297 static TypeInfo TypeInfo_timetz = {false, leftmostvalue_timetz, timetz_cmp};
299 GIN_SUPPORT(timetz)
301 static Datum
302 leftmostvalue_date(void)
304 return DateADTGetDatum(DATEVAL_NOBEGIN);
306 static TypeInfo TypeInfo_date = {false, leftmostvalue_date, date_cmp};
308 GIN_SUPPORT(date)
310 static Datum
311 leftmostvalue_interval(void)
313 Interval *v = palloc(sizeof(Interval));
315 v->time = DT_NOBEGIN;
316 v->day = 0;
317 v->month = 0;
318 return IntervalPGetDatum(v);
320 static TypeInfo TypeInfo_interval = {false, leftmostvalue_interval, interval_cmp};
322 GIN_SUPPORT(interval)
324 static Datum
325 leftmostvalue_macaddr(void)
327 macaddr *v = palloc0(sizeof(macaddr));
329 return MacaddrPGetDatum(v);
331 static TypeInfo TypeInfo_macaddr = {false, leftmostvalue_macaddr, macaddr_cmp};
333 GIN_SUPPORT(macaddr)
335 static Datum
336 leftmostvalue_inet(void)
338 return DirectFunctionCall3(inet_in,
339 CStringGetDatum("0.0.0.0/0"),
340 ObjectIdGetDatum(0),
341 Int32GetDatum(-1));
343 static TypeInfo TypeInfo_inet = {true, leftmostvalue_inet, network_cmp};
345 GIN_SUPPORT(inet)
347 static TypeInfo TypeInfo_cidr = {true, leftmostvalue_inet, network_cmp};
349 GIN_SUPPORT(cidr)
351 static Datum
352 leftmostvalue_text(void)
354 return PointerGetDatum(cstring_to_text_with_len("", 0));
356 static TypeInfo TypeInfo_text = {true, leftmostvalue_text, bttextcmp};
358 GIN_SUPPORT(text)
360 static Datum
361 leftmostvalue_char(void)
363 return CharGetDatum(SCHAR_MIN);
365 static TypeInfo TypeInfo_char = {false, leftmostvalue_char, btcharcmp};
367 GIN_SUPPORT(char)
369 static TypeInfo TypeInfo_bytea = {true, leftmostvalue_text, byteacmp};
371 GIN_SUPPORT(bytea)
373 static Datum
374 leftmostvalue_bit(void)
376 return DirectFunctionCall3(bit_in,
377 CStringGetDatum(""),
378 ObjectIdGetDatum(0),
379 Int32GetDatum(-1));
381 static TypeInfo TypeInfo_bit = {true, leftmostvalue_bit, bitcmp};
383 GIN_SUPPORT(bit)
385 static Datum
386 leftmostvalue_varbit(void)
388 return DirectFunctionCall3(varbit_in,
389 CStringGetDatum(""),
390 ObjectIdGetDatum(0),
391 Int32GetDatum(-1));
393 static TypeInfo TypeInfo_varbit = {true, leftmostvalue_varbit, bitcmp};
395 GIN_SUPPORT(varbit)
398 * Numeric type hasn't applicable left-most value, so NULL
399 * is used for that. NULL will never be an argument for a C-level
400 * numeric function except gin_numeric_cmp and it will not be stored
401 * somewhere and it could not be returned in any user SQL query.
404 #define NUMERIC_IS_LEFTMOST(x) ((x) == NULL)
406 PG_FUNCTION_INFO_V1(gin_numeric_cmp);
407 Datum gin_numeric_cmp(PG_FUNCTION_ARGS);
409 Datum
410 gin_numeric_cmp(PG_FUNCTION_ARGS)
412 Numeric a = (Numeric) PG_GETARG_POINTER(0);
413 Numeric b = (Numeric) PG_GETARG_POINTER(1);
414 int res = 0;
416 if (NUMERIC_IS_LEFTMOST(a))
418 res = (NUMERIC_IS_LEFTMOST(b)) ? 0 : -1;
420 else if (NUMERIC_IS_LEFTMOST(b))
422 res = 1;
424 else
426 res = DatumGetInt32(DirectFunctionCall2(numeric_cmp,
427 NumericGetDatum(a),
428 NumericGetDatum(b)));
431 PG_RETURN_INT32(res);
434 static Datum
435 leftmostvalue_numeric(void)
437 return PointerGetDatum(NULL);
440 static TypeInfo TypeInfo_numeric = {true, leftmostvalue_numeric, gin_numeric_cmp};
442 GIN_SUPPORT(numeric)