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)
|
|
|
|
{
|
|
|
|
uint32 *nentries = (uint32 *) PG_GETARG_POINTER(1);
|
|
|
|
StrategyNumber strategy = PG_GETARG_UINT16(2);
|
|
|
|
Datum *res = NULL;
|
|
|
|
|
2006-05-04 00:31:07 +08:00
|
|
|
*nentries = 0;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (strategy == BooleanSearchStrategy)
|
|
|
|
{
|
|
|
|
QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM_COPY(PG_GETARG_POINTER(0));
|
|
|
|
ITEM *items = GETQUERY(query);
|
|
|
|
int i;
|
2006-05-04 00:31:07 +08:00
|
|
|
|
|
|
|
if (query->size == 0)
|
|
|
|
PG_RETURN_POINTER(NULL);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (shorterquery(items, query->size) == 0)
|
|
|
|
elog(ERROR, "Query requires full scan, GIN doesn't support it");
|
2006-05-04 00:31:07 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
pfree(query);
|
2006-05-04 00:31:07 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_POINTER(0));
|
2006-05-04 00:31:07 +08:00
|
|
|
items = GETQUERY(query);
|
|
|
|
|
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++)
|
|
|
|
if (items[i].type == VAL)
|
|
|
|
{
|
|
|
|
res[*nentries] = Int32GetDatum(items[i].val);
|
2006-05-04 00:31:07 +08:00
|
|
|
(*nentries)++;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ArrayType *query = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
int4 *arr;
|
|
|
|
uint32 i;
|
2006-05-04 00:31:07 +08:00
|
|
|
|
|
|
|
CHECKARRVALID(query);
|
2006-10-04 08:30:14 +08:00
|
|
|
*nentries = ARRNELEMS(query);
|
|
|
|
if (*nentries > 0)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
int res = FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we can do not check array carefully, it's done by previous
|
|
|
|
* ginarrayextract call
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTOverlapStrategyNumber:
|
|
|
|
case RTContainedByStrategyNumber:
|
|
|
|
case RTOldContainedByStrategyNumber:
|
|
|
|
/* at least one element in check[] is true, so result = true */
|
|
|
|
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
|
|
|
case RTContainsStrategyNumber:
|
|
|
|
case RTOldContainsStrategyNumber:
|
|
|
|
res = TRUE;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ArrayType *query = PG_GETARG_ARRAYTYPE_P(2);
|
|
|
|
int i,
|
|
|
|
nentries = ARRNELEMS(query);
|
|
|
|
|
|
|
|
for (i = 0; i < nentries; i++)
|
|
|
|
if (!check[i])
|
|
|
|
{
|
|
|
|
res = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (0);
|
2006-05-04 00:31:07 +08:00
|
|
|
break;
|
2006-10-04 08:30:14 +08:00
|
|
|
case BooleanSearchStrategy:
|
|
|
|
do
|
|
|
|
{
|
|
|
|
QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_POINTER(2));
|
|
|
|
|
|
|
|
res = ginconsistent(query, check);
|
|
|
|
} while (0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "ginint4_consistent: unknown strategy number: %d", strategy);
|
|
|
|
}
|
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
|
|
|
}
|