2 * contrib/btree_gin/btree_gin.c
8 #include "access/stratnum.h"
9 #include "utils/builtins.h"
10 #include "utils/date.h"
11 #include "utils/float.h"
12 #include "utils/inet.h"
13 #include "utils/numeric.h"
14 #include "utils/timestamp.h"
15 #include "utils/uuid.h"
19 typedef struct QueryInfo
21 StrategyNumber strategy
;
24 Datum (*typecmp
) (FunctionCallInfo
);
27 /*** GIN support functions shared by all datatypes ***/
30 gin_btree_extract_value(FunctionCallInfo fcinfo
, bool is_varlena
)
32 Datum datum
= PG_GETARG_DATUM(0);
33 int32
*nentries
= (int32
*) PG_GETARG_POINTER(1);
34 Datum
*entries
= (Datum
*) palloc(sizeof(Datum
));
37 datum
= PointerGetDatum(PG_DETOAST_DATUM(datum
));
41 PG_RETURN_POINTER(entries
);
45 * For BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, and
46 * BTEqualStrategyNumber we want to start the index scan at the
47 * supplied query datum, and work forward. For BTLessStrategyNumber
48 * and BTLessEqualStrategyNumber, we need to start at the leftmost
49 * key, and work forward until the supplied query datum (which must be
50 * sent along inside the QueryInfo structure).
53 gin_btree_extract_query(FunctionCallInfo fcinfo
,
55 Datum (*leftmostvalue
) (void),
56 Datum (*typecmp
) (FunctionCallInfo
))
58 Datum datum
= PG_GETARG_DATUM(0);
59 int32
*nentries
= (int32
*) PG_GETARG_POINTER(1);
60 StrategyNumber strategy
= PG_GETARG_UINT16(2);
61 bool **partialmatch
= (bool **) PG_GETARG_POINTER(3);
62 Pointer
**extra_data
= (Pointer
**) PG_GETARG_POINTER(4);
63 Datum
*entries
= (Datum
*) palloc(sizeof(Datum
));
64 QueryInfo
*data
= (QueryInfo
*) palloc(sizeof(QueryInfo
));
65 bool *ptr_partialmatch
;
68 ptr_partialmatch
= *partialmatch
= (bool *) palloc(sizeof(bool));
69 *ptr_partialmatch
= false;
71 datum
= PointerGetDatum(PG_DETOAST_DATUM(datum
));
72 data
->strategy
= strategy
;
74 data
->is_varlena
= is_varlena
;
75 data
->typecmp
= typecmp
;
76 *extra_data
= (Pointer
*) palloc(sizeof(Pointer
));
77 **extra_data
= (Pointer
) data
;
81 case BTLessStrategyNumber
:
82 case BTLessEqualStrategyNumber
:
83 entries
[0] = leftmostvalue();
84 *ptr_partialmatch
= true;
86 case BTGreaterEqualStrategyNumber
:
87 case BTGreaterStrategyNumber
:
88 *ptr_partialmatch
= true;
90 case BTEqualStrategyNumber
:
94 elog(ERROR
, "unrecognized strategy number: %d", strategy
);
97 PG_RETURN_POINTER(entries
);
101 * Datum a is a value from extract_query method and for BTLess*
102 * strategy it is a left-most value. So, use original datum from QueryInfo
103 * to decide to stop scanning or not. Datum b is always from index.
106 gin_btree_compare_prefix(FunctionCallInfo fcinfo
)
108 Datum a
= PG_GETARG_DATUM(0);
109 Datum b
= PG_GETARG_DATUM(1);
110 QueryInfo
*data
= (QueryInfo
*) PG_GETARG_POINTER(3);
114 cmp
= DatumGetInt32(CallerFInfoFunctionCall2(data
->typecmp
,
117 (data
->strategy
== BTLessStrategyNumber
||
118 data
->strategy
== BTLessEqualStrategyNumber
)
122 switch (data
->strategy
)
124 case BTLessStrategyNumber
:
125 /* If original datum > indexed one then return match */
131 case BTLessEqualStrategyNumber
:
132 /* The same except equality */
138 case BTEqualStrategyNumber
:
144 case BTGreaterEqualStrategyNumber
:
145 /* If original datum <= indexed one then return match */
151 case BTGreaterStrategyNumber
:
152 /* If original datum <= indexed one then return match */
153 /* If original datum == indexed one then continue scan */
162 elog(ERROR
, "unrecognized strategy number: %d",
167 PG_RETURN_INT32(res
);
170 PG_FUNCTION_INFO_V1(gin_btree_consistent
);
172 gin_btree_consistent(PG_FUNCTION_ARGS
)
174 bool *recheck
= (bool *) PG_GETARG_POINTER(5);
177 PG_RETURN_BOOL(true);
180 /*** GIN_SUPPORT macro defines the datatype specific functions ***/
182 #define GIN_SUPPORT(type, is_varlena, leftmostvalue, typecmp) \
183 PG_FUNCTION_INFO_V1(gin_extract_value_##type); \
185 gin_extract_value_##type(PG_FUNCTION_ARGS) \
187 return gin_btree_extract_value(fcinfo, is_varlena); \
189 PG_FUNCTION_INFO_V1(gin_extract_query_##type); \
191 gin_extract_query_##type(PG_FUNCTION_ARGS) \
193 return gin_btree_extract_query(fcinfo, \
194 is_varlena, leftmostvalue, typecmp); \
196 PG_FUNCTION_INFO_V1(gin_compare_prefix_##type); \
198 gin_compare_prefix_##type(PG_FUNCTION_ARGS) \
200 return gin_btree_compare_prefix(fcinfo); \
204 /*** Datatype specifications ***/
207 leftmostvalue_int2(void)
209 return Int16GetDatum(SHRT_MIN
);
212 GIN_SUPPORT(int2
, false, leftmostvalue_int2
, btint2cmp
)
215 leftmostvalue_int4(void)
217 return Int32GetDatum(INT_MIN
);
220 GIN_SUPPORT(int4
, false, leftmostvalue_int4
, btint4cmp
)
223 leftmostvalue_int8(void)
225 return Int64GetDatum(PG_INT64_MIN
);
228 GIN_SUPPORT(int8
, false, leftmostvalue_int8
, btint8cmp
)
231 leftmostvalue_float4(void)
233 return Float4GetDatum(-get_float4_infinity());
236 GIN_SUPPORT(float4
, false, leftmostvalue_float4
, btfloat4cmp
)
239 leftmostvalue_float8(void)
241 return Float8GetDatum(-get_float8_infinity());
244 GIN_SUPPORT(float8
, false, leftmostvalue_float8
, btfloat8cmp
)
247 leftmostvalue_money(void)
249 return Int64GetDatum(PG_INT64_MIN
);
252 GIN_SUPPORT(money
, false, leftmostvalue_money
, cash_cmp
)
255 leftmostvalue_oid(void)
257 return ObjectIdGetDatum(0);
260 GIN_SUPPORT(oid
, false, leftmostvalue_oid
, btoidcmp
)
263 leftmostvalue_timestamp(void)
265 return TimestampGetDatum(DT_NOBEGIN
);
268 GIN_SUPPORT(timestamp
, false, leftmostvalue_timestamp
, timestamp_cmp
)
270 GIN_SUPPORT(timestamptz
, false, leftmostvalue_timestamp
, timestamp_cmp
)
273 leftmostvalue_time(void)
275 return TimeADTGetDatum(0);
278 GIN_SUPPORT(time
, false, leftmostvalue_time
, time_cmp
)
281 leftmostvalue_timetz(void)
283 TimeTzADT
*v
= palloc(sizeof(TimeTzADT
));
286 v
->zone
= -24 * 3600; /* XXX is that true? */
288 return TimeTzADTPGetDatum(v
);
291 GIN_SUPPORT(timetz
, false, leftmostvalue_timetz
, timetz_cmp
)
294 leftmostvalue_date(void)
296 return DateADTGetDatum(DATEVAL_NOBEGIN
);
299 GIN_SUPPORT(date
, false, leftmostvalue_date
, date_cmp
)
302 leftmostvalue_interval(void)
304 Interval
*v
= palloc(sizeof(Interval
));
308 return IntervalPGetDatum(v
);
311 GIN_SUPPORT(interval
, false, leftmostvalue_interval
, interval_cmp
)
314 leftmostvalue_macaddr(void)
316 macaddr
*v
= palloc0(sizeof(macaddr
));
318 return MacaddrPGetDatum(v
);
321 GIN_SUPPORT(macaddr
, false, leftmostvalue_macaddr
, macaddr_cmp
)
324 leftmostvalue_macaddr8(void)
326 macaddr8
*v
= palloc0(sizeof(macaddr8
));
328 return Macaddr8PGetDatum(v
);
331 GIN_SUPPORT(macaddr8
, false, leftmostvalue_macaddr8
, macaddr8_cmp
)
334 leftmostvalue_inet(void)
336 return DirectFunctionCall1(inet_in
, CStringGetDatum("0.0.0.0/0"));
339 GIN_SUPPORT(inet
, true, leftmostvalue_inet
, network_cmp
)
341 GIN_SUPPORT(cidr
, true, leftmostvalue_inet
, network_cmp
)
344 leftmostvalue_text(void)
346 return PointerGetDatum(cstring_to_text_with_len("", 0));
349 GIN_SUPPORT(text
, true, leftmostvalue_text
, bttextcmp
)
351 GIN_SUPPORT(bpchar
, true, leftmostvalue_text
, bpcharcmp
)
354 leftmostvalue_char(void)
356 return CharGetDatum(0);
359 GIN_SUPPORT(char, false, leftmostvalue_char
, btcharcmp
)
361 GIN_SUPPORT(bytea
, true, leftmostvalue_text
, byteacmp
)
364 leftmostvalue_bit(void)
366 return DirectFunctionCall3(bit_in
,
372 GIN_SUPPORT(bit
, true, leftmostvalue_bit
, bitcmp
)
375 leftmostvalue_varbit(void)
377 return DirectFunctionCall3(varbit_in
,
383 GIN_SUPPORT(varbit
, true, leftmostvalue_varbit
, bitcmp
)
386 * Numeric type hasn't a real left-most value, so we use PointerGetDatum(NULL)
387 * (*not* a SQL NULL) to represent that. We can get away with that because
388 * the value returned by our leftmostvalue function will never be stored in
389 * the index nor passed to anything except our compare and prefix-comparison
390 * functions. The same trick could be used for other pass-by-reference types.
393 #define NUMERIC_IS_LEFTMOST(x) ((x) == NULL)
395 PG_FUNCTION_INFO_V1(gin_numeric_cmp
);
398 gin_numeric_cmp(PG_FUNCTION_ARGS
)
400 Numeric a
= (Numeric
) PG_GETARG_POINTER(0);
401 Numeric b
= (Numeric
) PG_GETARG_POINTER(1);
404 if (NUMERIC_IS_LEFTMOST(a
))
406 res
= (NUMERIC_IS_LEFTMOST(b
)) ? 0 : -1;
408 else if (NUMERIC_IS_LEFTMOST(b
))
414 res
= DatumGetInt32(DirectFunctionCall2(numeric_cmp
,
416 NumericGetDatum(b
)));
419 PG_RETURN_INT32(res
);
423 leftmostvalue_numeric(void)
425 return PointerGetDatum(NULL
);
428 GIN_SUPPORT(numeric
, true, leftmostvalue_numeric
, gin_numeric_cmp
)
431 * Use a similar trick to that used for numeric for enums, since we don't
432 * actually know the leftmost value of any enum without knowing the concrete
433 * type, so we use a dummy leftmost value of InvalidOid.
435 * Note that we use CallerFInfoFunctionCall2 here so that enum_cmp
436 * gets a valid fn_extra to work with. Unlike most other type comparison
437 * routines it needs it, so we can't use DirectFunctionCall2.
440 #define ENUM_IS_LEFTMOST(x) ((x) == InvalidOid)
442 PG_FUNCTION_INFO_V1(gin_enum_cmp
);
445 gin_enum_cmp(PG_FUNCTION_ARGS
)
447 Oid a
= PG_GETARG_OID(0);
448 Oid b
= PG_GETARG_OID(1);
451 if (ENUM_IS_LEFTMOST(a
))
453 res
= (ENUM_IS_LEFTMOST(b
)) ? 0 : -1;
455 else if (ENUM_IS_LEFTMOST(b
))
461 res
= DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp
,
465 ObjectIdGetDatum(b
)));
468 PG_RETURN_INT32(res
);
472 leftmostvalue_enum(void)
474 return ObjectIdGetDatum(InvalidOid
);
477 GIN_SUPPORT(anyenum
, false, leftmostvalue_enum
, gin_enum_cmp
)
480 leftmostvalue_uuid(void)
483 * palloc0 will create the UUID with all zeroes:
484 * "00000000-0000-0000-0000-000000000000"
486 pg_uuid_t
*retval
= (pg_uuid_t
*) palloc0(sizeof(pg_uuid_t
));
488 return UUIDPGetDatum(retval
);
491 GIN_SUPPORT(uuid
, false, leftmostvalue_uuid
, uuid_cmp
)
494 leftmostvalue_name(void)
496 NameData
*result
= (NameData
*) palloc0(NAMEDATALEN
);
498 return NameGetDatum(result
);
501 GIN_SUPPORT(name
, false, leftmostvalue_name
, btnamecmp
)
504 leftmostvalue_bool(void)
506 return BoolGetDatum(false);
509 GIN_SUPPORT(bool, false, leftmostvalue_bool
, btboolcmp
)