2008-05-17 09:28:26 +08:00
|
|
|
/*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/intarray/_int_gin.c
|
2008-05-17 09:28:26 +08:00
|
|
|
*/
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
#include "access/gin.h"
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/skey.h"
|
|
|
|
|
2006-05-04 00:31:07 +08:00
|
|
|
#include "_int.h"
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(ginint4_queryextract);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum ginint4_queryextract(PG_FUNCTION_ARGS);
|
2006-05-04 00:31:07 +08:00
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
ginint4_queryextract(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-01-31 23:09:45 +08:00
|
|
|
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
|
2006-10-04 08:30:14 +08:00
|
|
|
StrategyNumber strategy = PG_GETARG_UINT16(2);
|
2011-01-09 13:39:21 +08:00
|
|
|
int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum *res = NULL;
|
|
|
|
|
2006-05-04 00:31:07 +08:00
|
|
|
*nentries = 0;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (strategy == BooleanSearchStrategy)
|
|
|
|
{
|
2011-01-09 13:39:21 +08:00
|
|
|
QUERYTYPE *query = PG_GETARG_QUERYTYPE_P(0);
|
2006-10-04 08:30:14 +08:00
|
|
|
ITEM *items = GETQUERY(query);
|
|
|
|
int i;
|
2006-05-04 00:31:07 +08:00
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
/* empty query must fail */
|
|
|
|
if (query->size <= 0)
|
2006-05-04 00:31:07 +08:00
|
|
|
PG_RETURN_POINTER(NULL);
|
|
|
|
|
2011-01-09 13:39:21 +08:00
|
|
|
/*
|
|
|
|
* If the query doesn't have any required primitive values (for
|
2011-04-10 23:42:00 +08:00
|
|
|
* instance, it's something like '! 42'), we have to do a full index
|
|
|
|
* scan.
|
2011-01-09 13:39:21 +08:00
|
|
|
*/
|
|
|
|
if (query_has_required_values(query))
|
|
|
|
*searchMode = GIN_SEARCH_MODE_DEFAULT;
|
|
|
|
else
|
|
|
|
*searchMode = GIN_SEARCH_MODE_ALL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract all the VAL items as things we want GIN to check for.
|
|
|
|
*/
|
2006-10-04 08:30:14 +08:00
|
|
|
res = (Datum *) palloc(sizeof(Datum) * query->size);
|
2006-05-04 00:31:07 +08:00
|
|
|
*nentries = 0;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = 0; i < query->size; i++)
|
2011-01-09 13:39:21 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
if (items[i].type == VAL)
|
|
|
|
{
|
|
|
|
res[*nentries] = Int32GetDatum(items[i].val);
|
2006-05-04 00:31:07 +08:00
|
|
|
(*nentries)++;
|
|
|
|
}
|
2011-01-09 13:39:21 +08:00
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ArrayType *query = PG_GETARG_ARRAYTYPE_P(0);
|
2006-05-04 00:31:07 +08:00
|
|
|
|
|
|
|
CHECKARRVALID(query);
|
2006-10-04 08:30:14 +08:00
|
|
|
*nentries = ARRNELEMS(query);
|
|
|
|
if (*nentries > 0)
|
|
|
|
{
|
2012-06-25 06:51:46 +08:00
|
|
|
int32 *arr;
|
2011-01-09 13:39:21 +08:00
|
|
|
int32 i;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
res = (Datum *) palloc(sizeof(Datum) * (*nentries));
|
|
|
|
|
|
|
|
arr = ARRPTR(query);
|
|
|
|
for (i = 0; i < *nentries; i++)
|
|
|
|
res[i] = Int32GetDatum(arr[i]);
|
2006-05-04 00:31:07 +08:00
|
|
|
}
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
switch (strategy)
|
2007-01-31 23:09:45 +08:00
|
|
|
{
|
|
|
|
case RTOverlapStrategyNumber:
|
2011-01-09 13:39:21 +08:00
|
|
|
*searchMode = GIN_SEARCH_MODE_DEFAULT;
|
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
|
|
|
case RTOldContainedByStrategyNumber:
|
|
|
|
/* empty set is contained in everything */
|
|
|
|
*searchMode = GIN_SEARCH_MODE_INCLUDE_EMPTY;
|
2007-11-16 05:14:46 +08:00
|
|
|
break;
|
2011-01-09 13:39:21 +08:00
|
|
|
case RTSameStrategyNumber:
|
|
|
|
if (*nentries > 0)
|
|
|
|
*searchMode = GIN_SEARCH_MODE_DEFAULT;
|
|
|
|
else
|
|
|
|
*searchMode = GIN_SEARCH_MODE_INCLUDE_EMPTY;
|
2007-11-16 05:14:46 +08:00
|
|
|
break;
|
2011-01-09 13:39:21 +08:00
|
|
|
case RTContainsStrategyNumber:
|
|
|
|
case RTOldContainsStrategyNumber:
|
|
|
|
if (*nentries > 0)
|
|
|
|
*searchMode = GIN_SEARCH_MODE_DEFAULT;
|
2011-04-10 23:42:00 +08:00
|
|
|
else /* everything contains the empty set */
|
2011-01-09 13:39:21 +08:00
|
|
|
*searchMode = GIN_SEARCH_MODE_ALL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "ginint4_queryextract: unknown strategy number: %d",
|
|
|
|
strategy);
|
2007-01-31 23:09:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_RETURN_POINTER(res);
|
2006-05-04 00:31:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(ginint4_consistent);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum ginint4_consistent(PG_FUNCTION_ARGS);
|
2006-05-04 00:31:07 +08:00
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
ginint4_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
bool *check = (bool *) PG_GETARG_POINTER(0);
|
|
|
|
StrategyNumber strategy = PG_GETARG_UINT16(1);
|
2011-01-09 13:39:21 +08:00
|
|
|
int32 nkeys = PG_GETARG_INT32(3);
|
2011-04-10 23:42:00 +08:00
|
|
|
|
2009-03-26 06:19:02 +08:00
|
|
|
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
|
|
|
|
bool *recheck = (bool *) PG_GETARG_POINTER(5);
|
2008-04-15 01:05:34 +08:00
|
|
|
bool res = FALSE;
|
2011-01-09 13:39:21 +08:00
|
|
|
int32 i;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTOverlapStrategyNumber:
|
2008-04-15 01:05:34 +08:00
|
|
|
/* result is not lossy */
|
|
|
|
*recheck = false;
|
|
|
|
/* at least one element in check[] is true, so result = true */
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
2006-10-04 08:30:14 +08:00
|
|
|
case RTContainedByStrategyNumber:
|
|
|
|
case RTOldContainedByStrategyNumber:
|
2008-04-15 01:05:34 +08:00
|
|
|
/* we will need recheck */
|
|
|
|
*recheck = true;
|
2006-10-04 08:30:14 +08:00
|
|
|
/* at least one element in check[] is true, so result = true */
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
2011-01-09 13:39:21 +08:00
|
|
|
/* we will need recheck */
|
|
|
|
*recheck = true;
|
|
|
|
/* Must have all elements in check[] true */
|
|
|
|
res = TRUE;
|
|
|
|
for (i = 0; i < nkeys; i++)
|
2008-04-15 01:05:34 +08:00
|
|
|
{
|
2011-01-09 13:39:21 +08:00
|
|
|
if (!check[i])
|
|
|
|
{
|
|
|
|
res = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2008-04-15 01:05:34 +08:00
|
|
|
}
|
|
|
|
break;
|
2006-10-04 08:30:14 +08:00
|
|
|
case RTContainsStrategyNumber:
|
|
|
|
case RTOldContainsStrategyNumber:
|
2011-01-09 13:39:21 +08:00
|
|
|
/* result is not lossy */
|
|
|
|
*recheck = false;
|
|
|
|
/* Must have all elements in check[] true */
|
|
|
|
res = TRUE;
|
|
|
|
for (i = 0; i < nkeys; i++)
|
2006-10-04 08:30:14 +08:00
|
|
|
{
|
2011-01-09 13:39:21 +08:00
|
|
|
if (!check[i])
|
|
|
|
{
|
|
|
|
res = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2008-04-15 01:05:34 +08:00
|
|
|
}
|
2006-05-04 00:31:07 +08:00
|
|
|
break;
|
2006-10-04 08:30:14 +08:00
|
|
|
case BooleanSearchStrategy:
|
|
|
|
{
|
2011-01-09 13:39:21 +08:00
|
|
|
QUERYTYPE *query = PG_GETARG_QUERYTYPE_P(2);
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2008-04-15 01:05:34 +08:00
|
|
|
/* result is not lossy */
|
|
|
|
*recheck = false;
|
2011-01-09 13:39:21 +08:00
|
|
|
res = gin_bool_consistent(query, check);
|
2008-04-15 01:05:34 +08:00
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
break;
|
|
|
|
default:
|
2008-04-15 01:05:34 +08:00
|
|
|
elog(ERROR, "ginint4_consistent: unknown strategy number: %d",
|
|
|
|
strategy);
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
2006-05-04 00:31:07 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_RETURN_BOOL(res);
|
2006-05-04 00:31:07 +08:00
|
|
|
}
|