2006-05-02 19:28:56 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/itup.h"
|
2006-07-14 00:57:31 +08:00
|
|
|
#include "access/skey.h"
|
2006-05-02 19:28:56 +08:00
|
|
|
#include "access/tuptoaster.h"
|
|
|
|
#include "storage/bufpage.h"
|
|
|
|
#include "utils/array.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
|
|
|
#include "tsvector.h"
|
|
|
|
#include "query.h"
|
|
|
|
#include "query_cleanup.h"
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gin_extract_tsvector);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum gin_extract_tsvector(PG_FUNCTION_ARGS);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
gin_extract_tsvector(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
tsvector *vector = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
2007-01-31 23:09:45 +08:00
|
|
|
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum *entries = NULL;
|
2006-05-02 19:28:56 +08:00
|
|
|
|
|
|
|
*nentries = 0;
|
2006-10-04 08:30:14 +08:00
|
|
|
if (vector->size > 0)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
WordEntry *we = ARRPTR(vector);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
2007-01-31 23:09:45 +08:00
|
|
|
*nentries = (int32) vector->size;
|
2006-10-04 08:30:14 +08:00
|
|
|
entries = (Datum *) palloc(sizeof(Datum) * vector->size);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = 0; i < vector->size; i++)
|
|
|
|
{
|
|
|
|
text *txt = (text *) palloc(VARHDRSZ + we->len);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
VARATT_SIZEP(txt) = VARHDRSZ + we->len;
|
|
|
|
memcpy(VARDATA(txt), STRPTR(vector) + we->pos, we->len);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
entries[i] = PointerGetDatum(txt);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
|
|
|
we++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(vector, 0);
|
|
|
|
PG_RETURN_POINTER(entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gin_extract_tsquery);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum gin_extract_tsquery(PG_FUNCTION_ARGS);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
gin_extract_tsquery(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
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 = DatumGetUInt16(PG_GETARG_DATUM(2));
|
|
|
|
Datum *entries = NULL;
|
2006-05-02 19:28:56 +08:00
|
|
|
|
|
|
|
*nentries = 0;
|
2006-10-04 08:30:14 +08:00
|
|
|
if (query->size > 0)
|
|
|
|
{
|
|
|
|
int4 i,
|
|
|
|
j = 0,
|
|
|
|
len;
|
|
|
|
ITEM *item;
|
2006-05-02 19:28:56 +08:00
|
|
|
|
|
|
|
item = clean_NOT_v2(GETQUERY(query), &len);
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!item)
|
|
|
|
elog(ERROR, "Query requires full scan, GIN doesn't support it");
|
2006-05-02 19:28:56 +08:00
|
|
|
|
|
|
|
item = GETQUERY(query);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = 0; i < query->size; i++)
|
|
|
|
if (item[i].type == VAL)
|
2006-05-02 19:28:56 +08:00
|
|
|
(*nentries)++;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
entries = (Datum *) palloc(sizeof(Datum) * (*nentries));
|
2006-05-02 19:28:56 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = 0; i < query->size; i++)
|
|
|
|
if (item[i].type == VAL)
|
|
|
|
{
|
|
|
|
text *txt;
|
2006-05-02 19:28:56 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
txt = (text *) palloc(VARHDRSZ + item[i].length);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
VARATT_SIZEP(txt) = VARHDRSZ + item[i].length;
|
|
|
|
memcpy(VARDATA(txt), GETOPERAND(query) + item[i].distance, item[i].length);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
entries[j++] = PointerGetDatum(txt);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (strategy == 1 && item[i].weight != 0)
|
|
|
|
elog(ERROR, "With class of lexeme restrictions use @@@ operation");
|
2006-05-02 19:28:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2007-01-31 23:09:45 +08:00
|
|
|
else
|
|
|
|
*nentries = -1; /* nothing can be found */
|
2006-05-02 19:28:56 +08:00
|
|
|
|
|
|
|
PG_FREE_IF_COPY(query, 0);
|
|
|
|
PG_RETURN_POINTER(entries);
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ITEM *frst;
|
|
|
|
bool *mapped_check;
|
|
|
|
} GinChkVal;
|
2006-05-02 19:28:56 +08:00
|
|
|
|
|
|
|
static bool
|
2006-10-04 08:30:14 +08:00
|
|
|
checkcondition_gin(void *checkval, ITEM * val)
|
|
|
|
{
|
|
|
|
GinChkVal *gcv = (GinChkVal *) checkval;
|
2006-05-02 19:28:56 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return gcv->mapped_check[val - gcv->frst];
|
2006-05-02 19:28:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gin_ts_consistent);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum gin_ts_consistent(PG_FUNCTION_ARGS);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
gin_ts_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
bool *check = (bool *) PG_GETARG_POINTER(0);
|
|
|
|
QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(2));
|
|
|
|
bool res = FALSE;
|
|
|
|
|
|
|
|
if (query->size > 0)
|
|
|
|
{
|
|
|
|
int4 i,
|
|
|
|
j = 0;
|
|
|
|
ITEM *item;
|
2006-05-02 19:28:56 +08:00
|
|
|
GinChkVal gcv;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
gcv.frst = item = GETQUERY(query);
|
|
|
|
gcv.mapped_check = (bool *) palloc(sizeof(bool) * query->size);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = 0; i < query->size; i++)
|
|
|
|
if (item[i].type == VAL)
|
|
|
|
gcv.mapped_check[i] = check[j++];
|
2006-05-02 19:28:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
res = TS_execute(
|
2006-10-04 08:30:14 +08:00
|
|
|
GETQUERY(query),
|
|
|
|
&gcv,
|
|
|
|
true,
|
|
|
|
checkcondition_gin
|
|
|
|
);
|
2006-05-02 19:28:56 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(query, 2);
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|