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