2007-08-21 03:11:32 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* tsginidx.c
|
|
|
|
* GIN support functions for tsvector_ops
|
|
|
|
*
|
2008-01-01 20:46:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
2007-08-21 03:11:32 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2008-05-16 18:31:02 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/tsginidx.c,v 1.12 2008/05/16 16:31:01 tgl Exp $
|
2007-08-21 03:11:32 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/skey.h"
|
|
|
|
#include "tsearch/ts_type.h"
|
|
|
|
#include "tsearch/ts_utils.h"
|
2008-03-25 23:42:46 +01:00
|
|
|
#include "utils/builtins.h"
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
|
2008-05-16 18:31:02 +02:00
|
|
|
Datum
|
|
|
|
gin_cmp_tslexeme(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *a = PG_GETARG_TEXT_P(0);
|
|
|
|
text *b = PG_GETARG_TEXT_P(1);
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
cmp = tsCompareString(
|
|
|
|
VARDATA(a), VARSIZE(a) - VARHDRSZ,
|
|
|
|
VARDATA(b), VARSIZE(b) - VARHDRSZ,
|
|
|
|
false );
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(a,0);
|
|
|
|
PG_FREE_IF_COPY(b,1);
|
|
|
|
PG_RETURN_INT32( cmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gin_cmp_prefix(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *a = PG_GETARG_TEXT_P(0);
|
|
|
|
text *b = PG_GETARG_TEXT_P(1);
|
|
|
|
#ifdef NOT_USED
|
|
|
|
StrategyNumber strategy = PG_GETARG_UINT16(2);
|
|
|
|
#endif
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
cmp = tsCompareString(
|
|
|
|
VARDATA(a), VARSIZE(a) - VARHDRSZ,
|
|
|
|
VARDATA(b), VARSIZE(b) - VARHDRSZ,
|
|
|
|
true );
|
|
|
|
|
|
|
|
if ( cmp < 0 )
|
|
|
|
cmp = 1; /* prevent continue scan */
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(a,0);
|
|
|
|
PG_FREE_IF_COPY(b,1);
|
|
|
|
PG_RETURN_INT32( cmp );
|
|
|
|
}
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
Datum
|
|
|
|
gin_extract_tsvector(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TSVector vector = PG_GETARG_TSVECTOR(0);
|
2007-09-07 18:03:40 +02:00
|
|
|
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
|
2007-08-21 03:11:32 +02:00
|
|
|
Datum *entries = NULL;
|
|
|
|
|
2007-09-11 10:46:29 +02:00
|
|
|
*nentries = vector->size;
|
2007-08-21 03:11:32 +02:00
|
|
|
if (vector->size > 0)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
WordEntry *we = ARRPTR(vector);
|
|
|
|
|
|
|
|
entries = (Datum *) palloc(sizeof(Datum) * vector->size);
|
|
|
|
|
|
|
|
for (i = 0; i < vector->size; i++)
|
|
|
|
{
|
2008-03-25 23:42:46 +01:00
|
|
|
text *txt;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
txt = cstring_to_text_with_len(STRPTR(vector) + we->pos, we->len);
|
2007-08-21 03:11:32 +02:00
|
|
|
entries[i] = PointerGetDatum(txt);
|
|
|
|
|
|
|
|
we++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(vector, 0);
|
|
|
|
PG_RETURN_POINTER(entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2007-11-28 20:33:05 +01:00
|
|
|
gin_extract_tsquery(PG_FUNCTION_ARGS)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(0);
|
2007-09-07 18:03:40 +02:00
|
|
|
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
|
2008-04-14 19:05:34 +02:00
|
|
|
/* StrategyNumber strategy = PG_GETARG_UINT16(2); */
|
2008-05-16 18:31:02 +02:00
|
|
|
bool **ptr_partialmatch = (bool**) PG_GETARG_POINTER(3);
|
2007-08-21 03:11:32 +02:00
|
|
|
Datum *entries = NULL;
|
2008-05-16 18:31:02 +02:00
|
|
|
bool *partialmatch;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
*nentries = 0;
|
|
|
|
|
|
|
|
if (query->size > 0)
|
|
|
|
{
|
|
|
|
int4 i,
|
|
|
|
j = 0,
|
|
|
|
len;
|
|
|
|
QueryItem *item;
|
2008-05-16 18:31:02 +02:00
|
|
|
bool use_fullscan=false;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
item = clean_NOT(GETQUERY(query), &len);
|
|
|
|
if (!item)
|
2008-05-16 18:31:02 +02:00
|
|
|
{
|
|
|
|
use_fullscan = true;
|
|
|
|
*nentries = 1;
|
|
|
|
}
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
item = GETQUERY(query);
|
|
|
|
|
|
|
|
for (i = 0; i < query->size; i++)
|
2007-09-07 17:09:56 +02:00
|
|
|
if (item[i].type == QI_VAL)
|
2007-08-21 03:11:32 +02:00
|
|
|
(*nentries)++;
|
|
|
|
|
|
|
|
entries = (Datum *) palloc(sizeof(Datum) * (*nentries));
|
2008-05-16 18:31:02 +02:00
|
|
|
partialmatch = *ptr_partialmatch = (bool*) palloc(sizeof(bool) * (*nentries));
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
for (i = 0; i < query->size; i++)
|
2007-09-07 17:09:56 +02:00
|
|
|
if (item[i].type == QI_VAL)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
text *txt;
|
2007-09-07 17:09:56 +02:00
|
|
|
QueryOperand *val = &item[i].operand;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
txt = cstring_to_text_with_len(GETOPERAND(query) + val->distance,
|
|
|
|
val->length);
|
2008-05-16 18:31:02 +02:00
|
|
|
partialmatch[j] = val->prefix;
|
2007-08-21 03:11:32 +02:00
|
|
|
entries[j++] = PointerGetDatum(txt);
|
|
|
|
}
|
2008-05-16 18:31:02 +02:00
|
|
|
|
|
|
|
if ( use_fullscan )
|
|
|
|
entries[j++] = PointerGetDatum(cstring_to_text_with_len("", 0));
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
*nentries = -1; /* nothing can be found */
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(query, 0);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
QueryItem *frst;
|
|
|
|
bool *mapped_check;
|
2008-04-14 19:05:34 +02:00
|
|
|
bool *need_recheck;
|
2007-11-15 23:25:18 +01:00
|
|
|
} GinChkVal;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
static bool
|
2007-11-15 23:25:18 +01:00
|
|
|
checkcondition_gin(void *checkval, QueryOperand *val)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
GinChkVal *gcv = (GinChkVal *) checkval;
|
|
|
|
|
2008-04-14 19:05:34 +02:00
|
|
|
/* if any val requiring a weight is used, set recheck flag */
|
|
|
|
if (val->weight != 0)
|
|
|
|
*(gcv->need_recheck) = true;
|
|
|
|
|
2007-09-07 17:09:56 +02:00
|
|
|
return gcv->mapped_check[((QueryItem *) val) - gcv->frst];
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2007-11-28 20:33:05 +01:00
|
|
|
gin_tsquery_consistent(PG_FUNCTION_ARGS)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
bool *check = (bool *) PG_GETARG_POINTER(0);
|
|
|
|
/* StrategyNumber strategy = PG_GETARG_UINT16(1); */
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(2);
|
2008-04-14 19:05:34 +02:00
|
|
|
bool *recheck = (bool *) PG_GETARG_POINTER(3);
|
2007-08-21 03:11:32 +02:00
|
|
|
bool res = FALSE;
|
|
|
|
|
2008-04-14 19:05:34 +02:00
|
|
|
/* The query requires recheck only if it involves weights */
|
|
|
|
*recheck = false;
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
if (query->size > 0)
|
|
|
|
{
|
2007-09-11 10:46:29 +02:00
|
|
|
int i,
|
2007-08-21 03:11:32 +02:00
|
|
|
j = 0;
|
|
|
|
QueryItem *item;
|
|
|
|
GinChkVal gcv;
|
|
|
|
|
2007-09-11 10:46:29 +02:00
|
|
|
/*
|
|
|
|
* check-parameter array has one entry for each value (operand) in the
|
|
|
|
* query. We expand that array into mapped_check, so that there's one
|
2007-11-15 22:14:46 +01:00
|
|
|
* entry in mapped_check for every node in the query, including
|
|
|
|
* operators, to allow quick lookups in checkcondition_gin. Only the
|
2007-09-11 10:46:29 +02:00
|
|
|
* entries corresponding operands are actually used.
|
|
|
|
*/
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
gcv.frst = item = GETQUERY(query);
|
|
|
|
gcv.mapped_check = (bool *) palloc(sizeof(bool) * query->size);
|
2008-04-14 19:05:34 +02:00
|
|
|
gcv.need_recheck = recheck;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
for (i = 0; i < query->size; i++)
|
2007-09-07 17:09:56 +02:00
|
|
|
if (item[i].type == QI_VAL)
|
2007-08-21 03:11:32 +02:00
|
|
|
gcv.mapped_check[i] = check[j++];
|
|
|
|
|
|
|
|
res = TS_execute(
|
|
|
|
GETQUERY(query),
|
|
|
|
&gcv,
|
|
|
|
true,
|
|
|
|
checkcondition_gin
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|