1999-01-17 11:04:57 +08:00
|
|
|
#include "postgres.h"
|
2001-02-10 10:31:31 +08:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
1998-07-20 02:26:41 +08:00
|
|
|
#include "executor/spi.h"
|
|
|
|
#include "commands/trigger.h"
|
|
|
|
|
|
|
|
/*
|
2001-10-25 13:50:21 +08:00
|
|
|
* Trigger function accepts variable number of arguments:
|
2001-08-05 03:41:00 +08:00
|
|
|
*
|
|
|
|
* 1. relation in which to store the substrings
|
|
|
|
* 2. fields to extract substrings from
|
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* The relation in which to insert *must* have the following layout:
|
2001-08-05 03:41:00 +08:00
|
|
|
*
|
|
|
|
* string varchar(#)
|
|
|
|
* id oid
|
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* where # is the largest size of the varchar columns being indexed
|
2001-08-05 03:41:00 +08:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* -- Create the SQL function based on the compiled shared object
|
2002-08-22 08:01:51 +08:00
|
|
|
* create function fti() returns trigger as
|
2001-10-25 13:50:21 +08:00
|
|
|
* '/usr/local/pgsql/lib/contrib/fti.so' language 'C';
|
2001-08-05 03:41:00 +08:00
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* -- Create the FTI table
|
2002-09-02 14:27:04 +08:00
|
|
|
* create table product_fti (string varchar(255), id oid) without oids;
|
2001-08-05 03:41:00 +08:00
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* -- Create an index to assist string matches
|
|
|
|
* create index product_fti_string_idx on product_fti (string);
|
2001-08-05 03:41:00 +08:00
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* -- Create an index to assist trigger'd deletes
|
|
|
|
* create index product_fti_id_idx on product_fti (id);
|
2001-08-05 03:41:00 +08:00
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* -- Create an index on the product oid column to assist joins
|
|
|
|
* -- between the fti table and the product table
|
|
|
|
* create index product_oid_idx on product (oid);
|
2001-08-05 03:41:00 +08:00
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* -- Create the trigger to perform incremental changes to the full text index.
|
|
|
|
* create trigger product_fti_trig after update or insert or delete on product
|
|
|
|
* for each row execute procedure fti(product_fti, title, artist);
|
|
|
|
* ^^^^^^^^^^^
|
|
|
|
* table where full text index is stored
|
|
|
|
* ^^^^^^^^^^^^^
|
|
|
|
* columns to index in the base table
|
2001-08-05 03:41:00 +08:00
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* After populating 'product', try something like:
|
2001-08-05 03:41:00 +08:00
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* SELECT DISTINCT(p.*) FROM product p, product_fti f1, product_fti f2 WHERE
|
2001-08-05 03:41:00 +08:00
|
|
|
* f1.string ~ '^slippery' AND f2.string ~ '^wet' AND p.oid=f1.id AND p.oid=f2.id;
|
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* To check that your indicies are being used correctly, make sure you
|
|
|
|
* EXPLAIN SELECT ... your test query above.
|
2001-08-05 03:41:00 +08:00
|
|
|
*
|
|
|
|
* CHANGELOG
|
|
|
|
* ---------
|
|
|
|
*
|
|
|
|
* august 3 2001
|
|
|
|
* Extended fti function to accept more than one column as a
|
|
|
|
* parameter and all specified columns are indexed. Changed
|
|
|
|
* all uses of sprintf to snprintf. Made error messages more
|
|
|
|
* consistent.
|
|
|
|
*
|
|
|
|
* march 4 1998 Changed breakup() to return less substrings. Only breakup
|
|
|
|
* in word parts which are in turn shortened from the start
|
|
|
|
* of the word (ie. word, ord, rd)
|
|
|
|
* Did allocation of substring buffer outside of breakup()
|
|
|
|
*
|
|
|
|
* oct. 5 1997, fixed a bug in string breakup (where there are more nonalpha
|
|
|
|
* characters between words then 1).
|
|
|
|
*
|
|
|
|
* oct 4-5 1997 implemented the thing, at least the basic functionallity
|
|
|
|
* of it all....
|
|
|
|
*
|
|
|
|
* TODO
|
|
|
|
* ----
|
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* prevent generating duplicate words for an oid in the fti table
|
|
|
|
* save a plan for deletes
|
|
|
|
* create a function that will make the index *after* we have populated
|
|
|
|
* the main table (probably first delete all contents to be sure there's
|
|
|
|
* nothing in it, then re-populate the fti-table)
|
2001-08-05 03:41:00 +08:00
|
|
|
*
|
2001-10-25 13:50:21 +08:00
|
|
|
* can we do something with operator overloading or a seperate function
|
2003-03-11 06:28:22 +08:00
|
|
|
* that can build the final query automagically?
|
2001-08-05 03:41:00 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_FTI_QUERY_LENGTH 8192
|
1998-07-20 02:26:41 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
extern Datum fti(PG_FUNCTION_ARGS);
|
|
|
|
static char *breakup(char *, char *);
|
|
|
|
static bool is_stopword(char *);
|
1998-07-20 02:26:41 +08:00
|
|
|
|
2001-03-22 12:01:46 +08:00
|
|
|
static bool new_tuple = false;
|
1998-07-20 02:26:41 +08:00
|
|
|
|
|
|
|
|
2001-10-16 02:56:30 +08:00
|
|
|
#ifdef USE_STOP_WORDS
|
|
|
|
|
1998-07-20 02:26:41 +08:00
|
|
|
/* THIS LIST MUST BE IN SORTED ORDER, A BINARY SEARCH IS USED!!!! */
|
1999-05-26 00:15:34 +08:00
|
|
|
char *StopWords[] = { /* list of words to skip in indexing */
|
2001-10-16 02:56:30 +08:00
|
|
|
"no",
|
1998-07-20 02:26:41 +08:00
|
|
|
"the",
|
2001-10-16 02:56:30 +08:00
|
|
|
"yes"
|
1998-07-20 02:26:41 +08:00
|
|
|
};
|
2001-11-06 01:46:40 +08:00
|
|
|
#endif /* USE_STOP_WORDS */
|
2001-10-16 02:56:30 +08:00
|
|
|
|
1998-07-20 02:26:41 +08:00
|
|
|
/* stuff for caching query-plans, stolen from contrib/spi/\*.c */
|
|
|
|
typedef struct
|
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
char *ident;
|
|
|
|
int nplans;
|
|
|
|
void **splan;
|
2001-11-06 01:46:40 +08:00
|
|
|
} EPlan;
|
1998-07-20 02:26:41 +08:00
|
|
|
|
|
|
|
static EPlan *InsertPlans = NULL;
|
|
|
|
static EPlan *DeletePlans = NULL;
|
|
|
|
static int nInsertPlans = 0;
|
1999-05-26 00:15:34 +08:00
|
|
|
static int nDeletePlans = 0;
|
1998-07-20 02:26:41 +08:00
|
|
|
|
|
|
|
static EPlan *find_plan(char *ident, EPlan ** eplan, int *nplans);
|
|
|
|
|
|
|
|
/***********************************************************************/
|
2000-11-21 04:36:57 +08:00
|
|
|
PG_FUNCTION_INFO_V1(fti);
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
Datum
|
|
|
|
fti(PG_FUNCTION_ARGS)
|
1998-07-20 02:26:41 +08:00
|
|
|
{
|
2001-08-05 03:41:00 +08:00
|
|
|
TriggerData *trigdata;
|
1999-05-26 00:15:34 +08:00
|
|
|
Trigger *trigger; /* to get trigger name */
|
|
|
|
int nargs; /* # of arguments */
|
|
|
|
char **args; /* arguments */
|
|
|
|
char *relname; /* triggered relation name */
|
|
|
|
Relation rel; /* triggered relation */
|
|
|
|
char *indexname; /* name of table for substrings */
|
1998-07-20 02:26:41 +08:00
|
|
|
HeapTuple rettuple = NULL;
|
1999-05-26 00:15:34 +08:00
|
|
|
TupleDesc tupdesc; /* tuple description */
|
|
|
|
bool isinsert = false;
|
|
|
|
bool isdelete = false;
|
1998-07-20 02:26:41 +08:00
|
|
|
int ret;
|
2001-08-05 03:41:00 +08:00
|
|
|
char query[MAX_FTI_QUERY_LENGTH];
|
1998-07-20 02:26:41 +08:00
|
|
|
Oid oid;
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1998-07-20 02:26:41 +08:00
|
|
|
/*
|
1999-05-26 00:15:34 +08:00
|
|
|
* FILE *debug;
|
|
|
|
*/
|
1998-07-20 02:26:41 +08:00
|
|
|
|
|
|
|
/*
|
1999-05-26 00:15:34 +08:00
|
|
|
* debug = fopen("/dev/xconsole", "w"); fprintf(debug, "FTI: entered
|
|
|
|
* function\n"); fflush(debug);
|
|
|
|
*/
|
1998-07-20 02:26:41 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "not fired by trigger manager");
|
2001-08-05 03:41:00 +08:00
|
|
|
|
|
|
|
/* It's safe to cast now that we've checked */
|
|
|
|
trigdata = (TriggerData *) fcinfo->context;
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
|
|
|
|
errmsg("can't process STATEMENT events")));
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
|
|
|
|
errmsg("must be fired AFTER event")));
|
1998-07-20 02:26:41 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
|
1999-05-26 00:15:34 +08:00
|
|
|
isinsert = true;
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
|
1999-05-26 00:15:34 +08:00
|
|
|
{
|
|
|
|
isdelete = true;
|
|
|
|
isinsert = true;
|
|
|
|
}
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
|
1999-05-26 00:15:34 +08:00
|
|
|
isdelete = true;
|
1998-07-20 02:26:41 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
trigger = trigdata->tg_trigger;
|
|
|
|
rel = trigdata->tg_relation;
|
1998-07-20 02:26:41 +08:00
|
|
|
relname = SPI_getrelname(rel);
|
2000-05-29 09:59:17 +08:00
|
|
|
rettuple = trigdata->tg_trigtuple;
|
1999-05-26 00:15:34 +08:00
|
|
|
if (isdelete && isinsert) /* is an UPDATE */
|
2000-05-29 09:59:17 +08:00
|
|
|
rettuple = trigdata->tg_newtuple;
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
if ((ret = SPI_connect()) < 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "SPI_connect failed, returned %d", ret);
|
1998-07-20 02:26:41 +08:00
|
|
|
|
|
|
|
nargs = trigger->tgnargs;
|
2001-08-05 03:41:00 +08:00
|
|
|
if (nargs < 2)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
|
|
|
|
errmsg("fti trigger must have at least 2 arguments")));
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1998-07-20 02:26:41 +08:00
|
|
|
args = trigger->tgargs;
|
|
|
|
indexname = args[0];
|
1999-05-26 00:15:34 +08:00
|
|
|
tupdesc = rel->rd_att; /* what the tuple looks like (?) */
|
1998-07-20 02:26:41 +08:00
|
|
|
|
|
|
|
/* get oid of current tuple, needed by all, so place here */
|
2002-09-02 09:05:06 +08:00
|
|
|
oid = HeapTupleGetOid(rettuple);
|
1998-07-20 02:26:41 +08:00
|
|
|
if (!OidIsValid(oid))
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
|
|
|
errmsg("OID is not present"),
|
|
|
|
errhint("Full Text Index requires indexed tables be created WITH OIDS.")));
|
1998-07-20 02:26:41 +08:00
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
if (isdelete)
|
|
|
|
{
|
|
|
|
void *pplan;
|
|
|
|
Oid *argtypes;
|
|
|
|
Datum values[1];
|
|
|
|
EPlan *plan;
|
2001-10-25 13:50:21 +08:00
|
|
|
int i;
|
2001-08-05 03:41:00 +08:00
|
|
|
|
|
|
|
snprintf(query, MAX_FTI_QUERY_LENGTH, "D%s", indexname);
|
|
|
|
for (i = 1; i < nargs; i++)
|
|
|
|
snprintf(query, MAX_FTI_QUERY_LENGTH, "%s$%s", query, args[i]);
|
1998-07-20 02:26:41 +08:00
|
|
|
|
|
|
|
plan = find_plan(query, &DeletePlans, &nDeletePlans);
|
1999-05-26 00:15:34 +08:00
|
|
|
if (plan->nplans <= 0)
|
|
|
|
{
|
|
|
|
argtypes = (Oid *) palloc(sizeof(Oid));
|
1998-07-20 02:26:41 +08:00
|
|
|
|
|
|
|
argtypes[0] = OIDOID;
|
|
|
|
|
2001-08-05 03:41:00 +08:00
|
|
|
snprintf(query, MAX_FTI_QUERY_LENGTH, "DELETE FROM %s WHERE id = $1", indexname);
|
1998-07-20 02:26:41 +08:00
|
|
|
pplan = SPI_prepare(query, 1, argtypes);
|
|
|
|
if (!pplan)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "SPI_prepare returned NULL in delete");
|
1998-07-20 02:26:41 +08:00
|
|
|
pplan = SPI_saveplan(pplan);
|
|
|
|
if (pplan == NULL)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "SPI_saveplan returned NULL in delete");
|
1998-07-20 02:26:41 +08:00
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
plan->splan = (void **) malloc(sizeof(void *));
|
1998-07-20 02:26:41 +08:00
|
|
|
*(plan->splan) = pplan;
|
|
|
|
plan->nplans = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
values[0] = oid;
|
|
|
|
|
|
|
|
ret = SPI_execp(*(plan->splan), values, NULL, 0);
|
|
|
|
if (ret != SPI_OK_DELETE)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
|
|
|
|
errmsg("error executing delete")));
|
1998-07-20 02:26:41 +08:00
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
if (isinsert)
|
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
char *substring;
|
|
|
|
char *column;
|
|
|
|
void *pplan;
|
|
|
|
Oid *argtypes;
|
1999-05-26 00:15:34 +08:00
|
|
|
Datum values[2];
|
|
|
|
int colnum;
|
2001-10-25 13:50:21 +08:00
|
|
|
struct varlena *data;
|
|
|
|
EPlan *plan;
|
|
|
|
int i;
|
|
|
|
char *buff;
|
|
|
|
char *string;
|
2001-08-05 03:41:00 +08:00
|
|
|
|
|
|
|
snprintf(query, MAX_FTI_QUERY_LENGTH, "I%s", indexname);
|
|
|
|
for (i = 1; i < nargs; i++)
|
|
|
|
snprintf(query, MAX_FTI_QUERY_LENGTH, "%s$%s", query, args[i]);
|
1998-07-20 02:26:41 +08:00
|
|
|
|
|
|
|
plan = find_plan(query, &InsertPlans, &nInsertPlans);
|
|
|
|
|
|
|
|
/* no plan yet, so allocate mem for argtypes */
|
1999-05-26 00:15:34 +08:00
|
|
|
if (plan->nplans <= 0)
|
|
|
|
{
|
|
|
|
argtypes = (Oid *) palloc(2 * sizeof(Oid));
|
1998-07-20 02:26:41 +08:00
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
argtypes[0] = VARCHAROID; /* create table t_name (string
|
|
|
|
* varchar, */
|
|
|
|
argtypes[1] = OIDOID; /* id oid); */
|
1998-07-20 02:26:41 +08:00
|
|
|
|
|
|
|
/* prepare plan to gain speed */
|
2001-08-05 03:41:00 +08:00
|
|
|
snprintf(query, MAX_FTI_QUERY_LENGTH, "INSERT INTO %s (string, id) VALUES ($1, $2)",
|
2001-10-25 13:50:21 +08:00
|
|
|
indexname);
|
1998-07-20 02:26:41 +08:00
|
|
|
pplan = SPI_prepare(query, 2, argtypes);
|
|
|
|
if (!pplan)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "SPI_prepare returned NULL in insert");
|
1998-07-20 02:26:41 +08:00
|
|
|
|
|
|
|
pplan = SPI_saveplan(pplan);
|
|
|
|
if (pplan == NULL)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "SPI_saveplan returned NULL in insert");
|
1998-07-20 02:26:41 +08:00
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
plan->splan = (void **) malloc(sizeof(void *));
|
1998-07-20 02:26:41 +08:00
|
|
|
*(plan->splan) = pplan;
|
|
|
|
plan->nplans = 1;
|
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1998-07-20 02:26:41 +08:00
|
|
|
/* prepare plan for query */
|
2001-08-05 03:41:00 +08:00
|
|
|
for (i = 0; i < nargs - 1; i++)
|
|
|
|
{
|
|
|
|
colnum = SPI_fnumber(tupdesc, args[i + 1]);
|
|
|
|
if (colnum == SPI_ERROR_NOATTRIBUTE)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
|
|
|
errmsg("column \"%s\" of \"%s\" does not exist",
|
2003-08-04 08:43:34 +08:00
|
|
|
args[i + 1], indexname)));
|
1998-07-20 02:26:41 +08:00
|
|
|
|
2001-08-05 03:41:00 +08:00
|
|
|
/* Get the char* representation of the column */
|
|
|
|
column = SPI_getvalue(rettuple, tupdesc, colnum);
|
1998-07-20 02:26:41 +08:00
|
|
|
|
2001-08-05 03:41:00 +08:00
|
|
|
/* make sure we don't try to index NULL's */
|
|
|
|
if (column)
|
1999-05-26 00:15:34 +08:00
|
|
|
{
|
2001-08-05 03:41:00 +08:00
|
|
|
string = column;
|
|
|
|
while (*string != '\0')
|
|
|
|
{
|
|
|
|
*string = tolower((unsigned char) *string);
|
|
|
|
string++;
|
|
|
|
}
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
data = (struct varlena *) palloc(sizeof(int32) + strlen(column) +1);
|
2001-08-05 03:41:00 +08:00
|
|
|
buff = palloc(strlen(column) + 1);
|
|
|
|
/* saves lots of calls in while-loop and in breakup() */
|
|
|
|
|
|
|
|
new_tuple = true;
|
|
|
|
|
|
|
|
while ((substring = breakup(column, buff)))
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
|
|
|
|
l = strlen(substring);
|
|
|
|
|
|
|
|
data->vl_len = l + sizeof(int32);
|
|
|
|
memcpy(VARDATA(data), substring, l);
|
|
|
|
values[0] = PointerGetDatum(data);
|
|
|
|
values[1] = oid;
|
|
|
|
|
|
|
|
ret = SPI_execp(*(plan->splan), values, NULL, 0);
|
|
|
|
if (ret != SPI_OK_INSERT)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
2003-08-04 08:43:34 +08:00
|
|
|
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
|
|
|
|
errmsg("error executing insert")));
|
2001-08-05 03:41:00 +08:00
|
|
|
}
|
|
|
|
pfree(buff);
|
|
|
|
pfree(data);
|
1998-07-20 02:26:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SPI_finish();
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(rettuple);
|
1998-07-20 02:26:41 +08:00
|
|
|
}
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
static char *
|
1999-05-26 00:15:34 +08:00
|
|
|
breakup(char *string, char *substring)
|
1998-07-20 02:26:41 +08:00
|
|
|
{
|
|
|
|
static char *last_start;
|
|
|
|
static char *cur_pos;
|
|
|
|
|
|
|
|
if (new_tuple)
|
|
|
|
{
|
1999-05-26 00:15:34 +08:00
|
|
|
cur_pos = last_start = &string[strlen(string) - 1];
|
|
|
|
new_tuple = false; /* don't initialize this next time */
|
1998-07-20 02:26:41 +08:00
|
|
|
}
|
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
while (cur_pos > string) /* don't read before start of 'string' */
|
1998-07-20 02:26:41 +08:00
|
|
|
{
|
1999-05-26 00:15:34 +08:00
|
|
|
/*
|
|
|
|
* skip pieces at the end of a string that are not alfa-numeric
|
|
|
|
* (ie. 'string$%^&', last_start first points to '&', and after
|
|
|
|
* this to 'g'
|
|
|
|
*/
|
2000-12-04 04:45:40 +08:00
|
|
|
if (!isalnum((unsigned char) *last_start))
|
1999-05-26 00:15:34 +08:00
|
|
|
{
|
2000-12-04 04:45:40 +08:00
|
|
|
while (!isalnum((unsigned char) *last_start) &&
|
1998-07-20 02:26:41 +08:00
|
|
|
last_start > string)
|
|
|
|
last_start--;
|
1999-05-26 00:15:34 +08:00
|
|
|
cur_pos = last_start;
|
1998-07-20 02:26:41 +08:00
|
|
|
}
|
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
cur_pos--; /* substrings are at minimum 2 characters
|
|
|
|
* long */
|
1998-07-20 02:26:41 +08:00
|
|
|
|
2000-12-04 04:45:40 +08:00
|
|
|
if (isalnum((unsigned char) *cur_pos))
|
1998-07-20 02:26:41 +08:00
|
|
|
{
|
|
|
|
/* Houston, we have a substring! :) */
|
|
|
|
memcpy(substring, cur_pos, last_start - cur_pos + 1);
|
1999-05-26 00:15:34 +08:00
|
|
|
substring[last_start - cur_pos + 1] = '\0';
|
|
|
|
if (!is_stopword(substring))
|
|
|
|
return substring;
|
1998-07-20 02:26:41 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-05-26 00:15:34 +08:00
|
|
|
last_start = cur_pos - 1;
|
1998-07-20 02:26:41 +08:00
|
|
|
cur_pos = last_start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
return NULL; /* we've processed all of 'string' */
|
1998-07-20 02:26:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* copied from src/backend/parser/keywords.c and adjusted for our situation*/
|
2000-05-29 09:59:17 +08:00
|
|
|
static bool
|
1998-07-20 02:26:41 +08:00
|
|
|
is_stopword(char *text)
|
|
|
|
{
|
2001-10-16 02:56:30 +08:00
|
|
|
#ifdef USE_STOP_WORDS
|
1999-05-26 00:15:34 +08:00
|
|
|
char **StopLow; /* for list of stop-words */
|
|
|
|
char **StopHigh;
|
|
|
|
char **StopMiddle;
|
2000-07-04 14:20:14 +08:00
|
|
|
int difference;
|
1998-07-20 02:26:41 +08:00
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
StopLow = &StopWords[0]; /* initialize stuff for binary search */
|
1998-07-20 02:26:41 +08:00
|
|
|
StopHigh = endof(StopWords);
|
|
|
|
|
2000-07-04 14:29:27 +08:00
|
|
|
/* Loop invariant: *StopLow <= text < *StopHigh */
|
1999-05-26 00:15:34 +08:00
|
|
|
|
2000-07-04 14:29:27 +08:00
|
|
|
while (StopLow < StopHigh)
|
1999-05-26 00:15:34 +08:00
|
|
|
{
|
|
|
|
StopMiddle = StopLow + (StopHigh - StopLow) / 2;
|
|
|
|
difference = strcmp(*StopMiddle, text);
|
|
|
|
if (difference == 0)
|
|
|
|
return (true);
|
|
|
|
else if (difference < 0)
|
|
|
|
StopLow = StopMiddle + 1;
|
|
|
|
else
|
2000-07-04 14:29:27 +08:00
|
|
|
StopHigh = StopMiddle;
|
1999-05-26 00:15:34 +08:00
|
|
|
}
|
2001-11-06 01:46:40 +08:00
|
|
|
#endif /* USE_STOP_WORDS */
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
return (false);
|
1998-07-20 02:26:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* for caching of query plans, stolen from contrib/spi/\*.c */
|
|
|
|
static EPlan *
|
|
|
|
find_plan(char *ident, EPlan ** eplan, int *nplans)
|
|
|
|
{
|
|
|
|
EPlan *newp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (*nplans > 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < *nplans; i++)
|
|
|
|
{
|
|
|
|
if (strcmp((*eplan)[i].ident, ident) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i != *nplans)
|
|
|
|
return (*eplan + i);
|
|
|
|
*eplan = (EPlan *) realloc(*eplan, (i + 1) * sizeof(EPlan));
|
|
|
|
newp = *eplan + i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newp = *eplan = (EPlan *) malloc(sizeof(EPlan));
|
|
|
|
(*nplans) = i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
newp->ident = (char *) malloc(strlen(ident) + 1);
|
|
|
|
strcpy(newp->ident, ident);
|
|
|
|
newp->nplans = 0;
|
|
|
|
newp->splan = NULL;
|
|
|
|
(*nplans)++;
|
|
|
|
|
|
|
|
return (newp);
|
|
|
|
}
|