2 * contrib/btree_gin/btree_gin.c
8 #include "access/skey.h"
9 #include "utils/builtins.h"
10 #include "utils/bytea.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"
20 typedef struct QueryInfo
22 StrategyNumber strategy;
25 Datum (*typecmp) (FunctionCallInfo);
29 /*** GIN support functions shared by all datatypes ***/
32 gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
34 Datum datum = PG_GETARG_DATUM(0);
35 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
36 Datum *entries = (Datum *) palloc(sizeof(Datum));
39 datum = PointerGetDatum(PG_DETOAST_DATUM(datum));
43 PG_RETURN_POINTER(entries);
47 * For BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, and
48 * BTEqualStrategyNumber we want to start the index scan at the
49 * supplied query datum, and work forward. For BTLessStrategyNumber
50 * and BTLessEqualStrategyNumber, we need to start at the leftmost
51 * key, and work forward until the supplied query datum (which must be
52 * sent along inside the QueryInfo structure).
55 gin_btree_extract_query(FunctionCallInfo fcinfo,
57 Datum (*leftmostvalue) (void),
58 Datum (*typecmp) (FunctionCallInfo))
60 Datum datum = PG_GETARG_DATUM(0);
61 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
62 StrategyNumber strategy = PG_GETARG_UINT16(2);
63 bool **partialmatch = (bool **) PG_GETARG_POINTER(3);
64 Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4);
65 Datum *entries = (Datum *) palloc(sizeof(Datum));
66 QueryInfo *data = (QueryInfo *) palloc(sizeof(QueryInfo));
67 bool *ptr_partialmatch;
70 ptr_partialmatch = *partialmatch = (bool *) palloc(sizeof(bool));
71 *ptr_partialmatch = false;
73 datum = PointerGetDatum(PG_DETOAST_DATUM(datum));
74 data->strategy = strategy;
76 data->is_varlena = is_varlena;
77 data->typecmp = typecmp;
78 *extra_data = (Pointer *) palloc(sizeof(Pointer));
79 **extra_data = (Pointer) data;
83 case BTLessStrategyNumber:
84 case BTLessEqualStrategyNumber:
85 entries[0] = leftmostvalue();
86 *ptr_partialmatch = true;
88 case BTGreaterEqualStrategyNumber:
89 case BTGreaterStrategyNumber:
90 *ptr_partialmatch = true;
91 case BTEqualStrategyNumber:
95 elog(ERROR, "unrecognized strategy number: %d", strategy);
98 PG_RETURN_POINTER(entries);
102 * Datum a is a value from extract_query method and for BTLess*
103 * strategy it is a left-most value. So, use original datum from QueryInfo
104 * to decide to stop scanning or not. Datum b is always from index.
107 gin_btree_compare_prefix(FunctionCallInfo fcinfo)
109 Datum a = PG_GETARG_DATUM(0);
110 Datum b = PG_GETARG_DATUM(1);
111 QueryInfo *data = (QueryInfo *) PG_GETARG_POINTER(3);
115 cmp = DatumGetInt32(DirectFunctionCall2Coll(
118 (data->strategy == BTLessStrategyNumber ||
119 data->strategy == BTLessEqualStrategyNumber)
123 switch (data->strategy)
125 case BTLessStrategyNumber:
126 /* If original datum > indexed one then return match */
132 case BTLessEqualStrategyNumber:
133 /* The same except equality */
139 case BTEqualStrategyNumber:
145 case BTGreaterEqualStrategyNumber:
146 /* If original datum <= indexed one then return match */
152 case BTGreaterStrategyNumber:
153 /* If original datum <= indexed one then return match */
154 /* If original datum == indexed one then continue scan */
163 elog(ERROR, "unrecognized strategy number: %d",
168 PG_RETURN_INT32(res);
171 PG_FUNCTION_INFO_V1(gin_btree_consistent);
173 gin_btree_consistent(PG_FUNCTION_ARGS)
175 bool *recheck = (bool *) PG_GETARG_POINTER(5);
178 PG_RETURN_BOOL(true);
181 /*** GIN_SUPPORT macro defines the datatype specific functions ***/
183 #define GIN_SUPPORT(type, is_varlena, leftmostvalue, typecmp) \
184 PG_FUNCTION_INFO_V1(gin_extract_value_##type); \
186 gin_extract_value_##type(PG_FUNCTION_ARGS) \
188 return gin_btree_extract_value(fcinfo, is_varlena); \
190 PG_FUNCTION_INFO_V1(gin_extract_query_##type); \
192 gin_extract_query_##type(PG_FUNCTION_ARGS) \
194 return gin_btree_extract_query(fcinfo, \
195 is_varlena, leftmostvalue, typecmp); \
197 PG_FUNCTION_INFO_V1(gin_compare_prefix_##type); \
199 gin_compare_prefix_##type(PG_FUNCTION_ARGS) \
201 return gin_btree_compare_prefix(fcinfo); \
205 /*** Datatype specifications ***/
208 leftmostvalue_int2(void)
210 return Int16GetDatum(SHRT_MIN);
212 GIN_SUPPORT(int2, false, leftmostvalue_int2, btint2cmp)
215 leftmostvalue_int4(void)
217 return Int32GetDatum(INT_MIN);
219 GIN_SUPPORT(int4, false, leftmostvalue_int4, btint4cmp)
222 leftmostvalue_int8(void)
225 * Use sequence's definition to keep compatibility.
227 return Int64GetDatum(SEQ_MINVALUE);
229 GIN_SUPPORT(int8, false, leftmostvalue_int8, btint8cmp)
232 leftmostvalue_float4(void)
234 return Float4GetDatum(-get_float4_infinity());
236 GIN_SUPPORT(float4, false, leftmostvalue_float4, btfloat4cmp)
239 leftmostvalue_float8(void)
241 return Float8GetDatum(-get_float8_infinity());
243 GIN_SUPPORT(float8, false, leftmostvalue_float8, btfloat8cmp)
246 leftmostvalue_money(void)
249 * Use sequence's definition to keep compatibility.
251 return Int64GetDatum(SEQ_MINVALUE);
253 GIN_SUPPORT(money, false, leftmostvalue_money, cash_cmp)
256 leftmostvalue_oid(void)
258 return ObjectIdGetDatum(0);
260 GIN_SUPPORT(oid, false, leftmostvalue_oid, btoidcmp)
263 leftmostvalue_timestamp(void)
265 return TimestampGetDatum(DT_NOBEGIN);
267 GIN_SUPPORT(timestamp, false, leftmostvalue_timestamp, timestamp_cmp)
269 GIN_SUPPORT(timestamptz, false, leftmostvalue_timestamp, timestamp_cmp)
272 leftmostvalue_time(void)
274 return TimeADTGetDatum(0);
276 GIN_SUPPORT(time, false, leftmostvalue_time, time_cmp)
279 leftmostvalue_timetz(void)
281 TimeTzADT *v = palloc(sizeof(TimeTzADT));
284 v->zone = -24 * 3600; /* XXX is that true? */
286 return TimeTzADTPGetDatum(v);
288 GIN_SUPPORT(timetz, false, leftmostvalue_timetz, timetz_cmp)
291 leftmostvalue_date(void)
293 return DateADTGetDatum(DATEVAL_NOBEGIN);
295 GIN_SUPPORT(date, false, leftmostvalue_date, date_cmp)
298 leftmostvalue_interval(void)
300 Interval *v = palloc(sizeof(Interval));
302 v->time = DT_NOBEGIN;
305 return IntervalPGetDatum(v);
307 GIN_SUPPORT(interval, false, leftmostvalue_interval, interval_cmp)
310 leftmostvalue_macaddr(void)
312 macaddr *v = palloc0(sizeof(macaddr));
314 return MacaddrPGetDatum(v);
316 GIN_SUPPORT(macaddr, false, leftmostvalue_macaddr, macaddr_cmp)
319 leftmostvalue_inet(void)
321 return DirectFunctionCall1(inet_in, CStringGetDatum("0.0.0.0/0"));
323 GIN_SUPPORT(inet, true, leftmostvalue_inet, network_cmp)
325 GIN_SUPPORT(cidr, true, leftmostvalue_inet, network_cmp)
328 leftmostvalue_text(void)
330 return PointerGetDatum(cstring_to_text_with_len("", 0));
332 GIN_SUPPORT(text, true, leftmostvalue_text, bttextcmp)
335 leftmostvalue_char(void)
337 return CharGetDatum(SCHAR_MIN);
339 GIN_SUPPORT(char, false, leftmostvalue_char, btcharcmp)
341 GIN_SUPPORT(bytea, true, leftmostvalue_text, byteacmp)
344 leftmostvalue_bit(void)
346 return DirectFunctionCall1(bit_in, CStringGetDatum(""));
348 GIN_SUPPORT(bit, true, leftmostvalue_bit, bitcmp)
351 leftmostvalue_varbit(void)
353 return DirectFunctionCall1(varbit_in, CStringGetDatum(""));
355 GIN_SUPPORT(varbit, true, leftmostvalue_varbit, bitcmp)
358 * Numeric type hasn't a real left-most value, so we use PointerGetDatum(NULL)
359 * (*not* a SQL NULL) to represent that. We can get away with that because
360 * the value returned by our leftmostvalue function will never be stored in
361 * the index nor passed to anything except our compare and prefix-comparison
362 * functions. The same trick could be used for other pass-by-reference types.
365 #define NUMERIC_IS_LEFTMOST(x) ((x) == NULL)
367 PG_FUNCTION_INFO_V1(gin_numeric_cmp);
370 gin_numeric_cmp(PG_FUNCTION_ARGS)
372 Numeric a = (Numeric) PG_GETARG_POINTER(0);
373 Numeric b = (Numeric) PG_GETARG_POINTER(1);
376 if (NUMERIC_IS_LEFTMOST(a))
378 res = (NUMERIC_IS_LEFTMOST(b)) ? 0 : -1;
380 else if (NUMERIC_IS_LEFTMOST(b))
386 res = DatumGetInt32(DirectFunctionCall2(numeric_cmp,
388 NumericGetDatum(b)));
391 PG_RETURN_INT32(res);
395 leftmostvalue_numeric(void)
397 return PointerGetDatum(NULL);
399 GIN_SUPPORT(numeric, true, leftmostvalue_numeric, gin_numeric_cmp)