2001-10-13 07:19:09 +08:00
|
|
|
/*
|
|
|
|
* IO definitions for query_txt and mquery_txt. This type
|
|
|
|
* are identical, but for parsing mquery_txt used parser for text
|
|
|
|
* and also morphology is used.
|
|
|
|
* Internal structure:
|
|
|
|
* query tree, then string with original value.
|
|
|
|
* Query tree with plain view. It's means that in array of nodes
|
|
|
|
* right child is always next and left position = item+item->left
|
|
|
|
* Teodor Sigaev <teodor@stack.net>
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2004-06-13 04:31:43 +08:00
|
|
|
#include <ctype.h>
|
2001-10-13 07:19:09 +08:00
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/itup.h"
|
|
|
|
#include "access/rtree.h"
|
|
|
|
#include "utils/array.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "storage/bufpage.h"
|
|
|
|
|
|
|
|
#include "txtidx.h"
|
|
|
|
#include "crc32.h"
|
|
|
|
#include "query.h"
|
|
|
|
#include "morph.h"
|
|
|
|
#include "rewrite.h"
|
|
|
|
|
|
|
|
#include "deflex.h"
|
|
|
|
#include "parser.h"
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(mqtxt_in);
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum mqtxt_in(PG_FUNCTION_ARGS);
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
PG_FUNCTION_INFO_V1(qtxt_in);
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum qtxt_in(PG_FUNCTION_ARGS);
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
PG_FUNCTION_INFO_V1(qtxt_out);
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum qtxt_out(PG_FUNCTION_ARGS);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(execqtxt);
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum execqtxt(PG_FUNCTION_ARGS);
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
PG_FUNCTION_INFO_V1(rexecqtxt);
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum rexecqtxt(PG_FUNCTION_ARGS);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(querytree);
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum querytree(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
#define END 0
|
|
|
|
#define ERR 1
|
|
|
|
#define VAL 2
|
|
|
|
#define OPR 3
|
|
|
|
#define OPEN 4
|
|
|
|
#define CLOSE 5
|
|
|
|
#define VALTRUE 6 /* for stop words */
|
2001-10-13 07:19:09 +08:00
|
|
|
#define VALFALSE 7
|
|
|
|
|
|
|
|
/* parser's states */
|
2001-10-25 13:50:21 +08:00
|
|
|
#define WAITOPERAND 1
|
2001-10-13 07:19:09 +08:00
|
|
|
#define WAITOPERATOR 2
|
|
|
|
|
|
|
|
/*
|
|
|
|
* node of query tree, also used
|
|
|
|
* for storing polish notation in parser
|
|
|
|
*/
|
2001-10-25 13:50:21 +08:00
|
|
|
typedef struct NODE
|
|
|
|
{
|
|
|
|
int4 type;
|
|
|
|
int4 val;
|
|
|
|
int2 distance;
|
|
|
|
int2 length;
|
|
|
|
struct NODE *next;
|
2001-11-06 01:46:40 +08:00
|
|
|
} NODE;
|
2001-10-25 13:50:21 +08:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
int4 state;
|
|
|
|
int4 count;
|
2003-03-11 06:28:22 +08:00
|
|
|
/* reverse polish notation in list (for temporary usage) */
|
2001-10-25 13:50:21 +08:00
|
|
|
NODE *str;
|
2001-10-13 07:19:09 +08:00
|
|
|
/* number in str */
|
2001-10-25 13:50:21 +08:00
|
|
|
int4 num;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
/* user-friendly operand */
|
2001-10-25 13:50:21 +08:00
|
|
|
int4 lenop;
|
|
|
|
int4 sumlen;
|
|
|
|
char *op;
|
|
|
|
char *curop;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
/* state for value's parser */
|
2001-10-25 13:50:21 +08:00
|
|
|
TI_IN_STATE valstate;
|
2001-11-06 01:46:40 +08:00
|
|
|
} QPRS_STATE;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* get token from query string
|
|
|
|
*/
|
2001-10-25 13:50:21 +08:00
|
|
|
static int4
|
|
|
|
gettoken_query(QPRS_STATE * state, int4 *val, int4 *lenval, char **strval)
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
switch (state->state)
|
|
|
|
{
|
2001-10-28 14:26:15 +08:00
|
|
|
case WAITOPERAND:
|
2001-10-25 13:50:21 +08:00
|
|
|
if (*(state->buf) == '!')
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
(state->buf)++;
|
2001-10-25 13:50:21 +08:00
|
|
|
*val = (int4) '!';
|
2001-10-13 07:19:09 +08:00
|
|
|
return OPR;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else if (*(state->buf) == '(')
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
state->count++;
|
|
|
|
(state->buf)++;
|
|
|
|
return OPEN;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else if (*(state->buf) != ' ')
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
state->valstate.prsbuf = state->buf;
|
|
|
|
state->state = WAITOPERATOR;
|
2001-10-25 13:50:21 +08:00
|
|
|
if (gettoken_txtidx(&(state->valstate)))
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
*strval = state->valstate.word;
|
|
|
|
*lenval = state->valstate.curpos - state->valstate.word;
|
|
|
|
state->buf = state->valstate.prsbuf;
|
|
|
|
return VAL;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("no operand")));
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WAITOPERATOR:
|
2001-10-25 13:50:21 +08:00
|
|
|
if (*(state->buf) == '&' || *(state->buf) == '|')
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
state->state = WAITOPERAND;
|
|
|
|
*val = (int4) *(state->buf);
|
2001-10-25 13:50:21 +08:00
|
|
|
(state->buf)++;
|
2001-10-13 07:19:09 +08:00
|
|
|
return OPR;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else if (*(state->buf) == ')')
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
(state->buf)++;
|
|
|
|
state->count--;
|
2001-10-25 13:50:21 +08:00
|
|
|
return (state->count < 0) ? ERR : CLOSE;
|
|
|
|
}
|
|
|
|
else if (*(state->buf) == '\0')
|
|
|
|
return (state->count) ? ERR : END;
|
|
|
|
else if (*(state->buf) != ' ')
|
2001-10-13 07:19:09 +08:00
|
|
|
return ERR;
|
|
|
|
break;
|
2001-10-25 13:50:21 +08:00
|
|
|
default:
|
2001-10-13 07:19:09 +08:00
|
|
|
return ERR;
|
|
|
|
break;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
(state->buf)++;
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
return END;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* push new one in polish notation reverse view
|
|
|
|
*/
|
|
|
|
static void
|
2001-10-25 13:50:21 +08:00
|
|
|
pushquery(QPRS_STATE * state, int4 type, int4 val, int4 distance, int4 lenval)
|
|
|
|
{
|
|
|
|
NODE *tmp = (NODE *) palloc(sizeof(NODE));
|
|
|
|
|
|
|
|
tmp->type = type;
|
|
|
|
tmp->val = val;
|
|
|
|
if (distance > 0xffff)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("value is too big")));
|
2001-10-25 13:50:21 +08:00
|
|
|
if (lenval > 0xffff)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("operand is too long")));
|
2001-10-25 13:50:21 +08:00
|
|
|
tmp->distance = distance;
|
|
|
|
tmp->length = lenval;
|
2001-10-13 07:19:09 +08:00
|
|
|
tmp->next = state->str;
|
|
|
|
state->str = tmp;
|
|
|
|
state->num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is used for query_txt parsing
|
|
|
|
*/
|
|
|
|
static void
|
2001-10-25 13:50:21 +08:00
|
|
|
pushval_asis(QPRS_STATE * state, int type, char *strval, int lenval)
|
|
|
|
{
|
|
|
|
if (lenval > 0xffff)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("word is too long")));
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
pushquery(state, type, crc32_sz((uint8 *) strval, lenval),
|
|
|
|
state->curop - state->op, lenval);
|
|
|
|
|
|
|
|
while (state->curop - state->op + lenval + 1 >= state->lenop)
|
|
|
|
{
|
|
|
|
int4 tmp = state->curop - state->op;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
state->lenop *= 2;
|
2001-10-25 13:50:21 +08:00
|
|
|
state->op = (char *) repalloc((void *) state->op, state->lenop);
|
2001-10-13 07:19:09 +08:00
|
|
|
state->curop = state->op + tmp;
|
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
memcpy((void *) state->curop, (void *) strval, lenval);
|
2001-10-13 07:19:09 +08:00
|
|
|
state->curop += lenval;
|
|
|
|
*(state->curop) = '\0';
|
|
|
|
state->curop++;
|
|
|
|
state->sumlen += lenval + 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is used for mquery_txt parsing
|
|
|
|
*/
|
|
|
|
static void
|
2001-10-25 13:50:21 +08:00
|
|
|
pushval_morph(QPRS_STATE * state, int typeval, char *strval, int lenval)
|
|
|
|
{
|
|
|
|
int4 type,
|
|
|
|
lenlemm;
|
|
|
|
int4 count = 0;
|
|
|
|
char *lemm;
|
|
|
|
|
|
|
|
start_parse_str(strval, lenval);
|
|
|
|
while ((type = tsearch_yylex()) != 0)
|
|
|
|
{
|
|
|
|
if (tokenlen > 0xffff)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
end_parse();
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("word is too long")));
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
lenlemm = tokenlen;
|
2001-10-25 13:50:21 +08:00
|
|
|
lemm = lemmatize(token, &lenlemm, type);
|
|
|
|
if (lemm)
|
|
|
|
{
|
2004-06-08 00:48:18 +08:00
|
|
|
if ( lemm==token ) {
|
|
|
|
char *ptrs=token,*ptrd;
|
|
|
|
ptrd = lemm = palloc(lenlemm+1);
|
|
|
|
while(ptrs-token<lenlemm) {
|
|
|
|
*ptrd = tolower((unsigned char) *ptrs);
|
|
|
|
ptrs++;
|
|
|
|
ptrd++;
|
|
|
|
}
|
|
|
|
*ptrd='\0';
|
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
pushval_asis(state, VAL, lemm, lenlemm);
|
2004-06-08 00:48:18 +08:00
|
|
|
pfree(lemm);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
else
|
|
|
|
pushval_asis(state, VALTRUE, 0, 0);
|
|
|
|
if (count)
|
|
|
|
pushquery(state, OPR, (int4) '&', 0, 0);
|
|
|
|
count++;
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
end_parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define STACKDEPTH 32
|
|
|
|
/*
|
2003-03-11 06:28:22 +08:00
|
|
|
* make polish notation of query
|
2001-10-13 07:19:09 +08:00
|
|
|
*/
|
2001-10-25 13:50:21 +08:00
|
|
|
static int4
|
|
|
|
makepol(QPRS_STATE * state, void (*pushval) (QPRS_STATE *, int, char *, int))
|
|
|
|
{
|
|
|
|
int4 val,
|
|
|
|
type;
|
|
|
|
int4 lenval;
|
|
|
|
char *strval;
|
|
|
|
int4 stack[STACKDEPTH];
|
|
|
|
int4 lenstack = 0;
|
|
|
|
|
|
|
|
while ((type = gettoken_query(state, &val, &lenval, &strval)) != END)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
case VAL:
|
2001-10-25 13:50:21 +08:00
|
|
|
(*pushval) (state, VAL, strval, lenval);
|
|
|
|
while (lenstack && (stack[lenstack - 1] == (int4) '&' ||
|
|
|
|
stack[lenstack - 1] == (int4) '!'))
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
lenstack--;
|
2001-10-25 13:50:21 +08:00
|
|
|
pushquery(state, OPR, stack[lenstack], 0, 0);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OPR:
|
2001-10-25 13:50:21 +08:00
|
|
|
if (lenstack && val == (int4) '|')
|
|
|
|
pushquery(state, OPR, val, 0, 0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (lenstack == STACKDEPTH)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "stack too short");
|
2001-10-25 13:50:21 +08:00
|
|
|
stack[lenstack] = val;
|
2001-10-13 07:19:09 +08:00
|
|
|
lenstack++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OPEN:
|
2001-10-25 13:50:21 +08:00
|
|
|
if (makepol(state, pushval) == ERR)
|
|
|
|
return ERR;
|
|
|
|
if (lenstack && (stack[lenstack - 1] == (int4) '&' ||
|
|
|
|
stack[lenstack - 1] == (int4) '!'))
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
lenstack--;
|
2001-10-25 13:50:21 +08:00
|
|
|
pushquery(state, OPR, stack[lenstack], 0, 0);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CLOSE:
|
2001-10-25 13:50:21 +08:00
|
|
|
while (lenstack)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
lenstack--;
|
2001-10-25 13:50:21 +08:00
|
|
|
pushquery(state, OPR, stack[lenstack], 0, 0);
|
2001-10-13 07:19:09 +08:00
|
|
|
};
|
|
|
|
return END;
|
|
|
|
break;
|
|
|
|
case ERR:
|
|
|
|
default:
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("syntax error")));
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
return ERR;
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
while (lenstack)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
lenstack--;
|
2001-10-25 13:50:21 +08:00
|
|
|
pushquery(state, OPR, stack[lenstack], 0, 0);
|
2001-10-13 07:19:09 +08:00
|
|
|
};
|
|
|
|
return END;
|
|
|
|
}
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
WordEntry *arrb;
|
|
|
|
WordEntry *arre;
|
|
|
|
char *values;
|
|
|
|
char *operand;
|
2001-11-06 01:46:40 +08:00
|
|
|
} CHKVAL;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* compare 2 string values
|
|
|
|
*/
|
|
|
|
static int4
|
2001-10-25 13:50:21 +08:00
|
|
|
ValCompare(CHKVAL * chkval, WordEntry * ptr, ITEM * item)
|
|
|
|
{
|
|
|
|
if (ptr->len == item->length)
|
2001-10-13 07:19:09 +08:00
|
|
|
return strncmp(
|
2001-10-25 13:50:21 +08:00
|
|
|
&(chkval->values[ptr->pos]),
|
|
|
|
&(chkval->operand[item->distance]),
|
|
|
|
item->length);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
return (ptr->len > item->length) ? 1 : -1;
|
|
|
|
}
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* is there value 'val' in array or not ?
|
|
|
|
*/
|
|
|
|
static bool
|
2001-10-25 13:50:21 +08:00
|
|
|
checkcondition_str(void *checkval, ITEM * val)
|
|
|
|
{
|
|
|
|
WordEntry *StopLow = ((CHKVAL *) checkval)->arrb;
|
|
|
|
WordEntry *StopHigh = ((CHKVAL *) checkval)->arre;
|
|
|
|
WordEntry *StopMiddle;
|
|
|
|
int difference;
|
|
|
|
|
|
|
|
/* Loop invariant: StopLow <= val < StopHigh */
|
|
|
|
|
|
|
|
while (StopLow < StopHigh)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
StopMiddle = StopLow + (StopHigh - StopLow) / 2;
|
2001-10-25 13:50:21 +08:00
|
|
|
difference = ValCompare((CHKVAL *) checkval, StopMiddle, val);
|
|
|
|
if (difference == 0)
|
2001-10-13 07:19:09 +08:00
|
|
|
return (true);
|
|
|
|
else if (difference < 0)
|
|
|
|
StopLow = StopMiddle + 1;
|
|
|
|
else
|
|
|
|
StopHigh = StopMiddle;
|
|
|
|
}
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
return (false);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check for boolean condition
|
|
|
|
*/
|
|
|
|
bool
|
2001-10-25 13:50:21 +08:00
|
|
|
execute(ITEM * curitem, void *checkval, bool calcnot, bool (*chkcond) (void *checkval, ITEM * val))
|
|
|
|
{
|
|
|
|
if (curitem->type == VAL)
|
|
|
|
return (*chkcond) (checkval, curitem);
|
|
|
|
else if (curitem->val == (int4) '!')
|
|
|
|
{
|
|
|
|
return (calcnot) ?
|
|
|
|
((execute(curitem + 1, checkval, calcnot, chkcond)) ? false : true)
|
|
|
|
: true;
|
|
|
|
}
|
|
|
|
else if (curitem->val == (int4) '&')
|
|
|
|
{
|
|
|
|
if (execute(curitem + curitem->left, checkval, calcnot, chkcond))
|
2001-10-13 07:19:09 +08:00
|
|
|
return execute(curitem + 1, checkval, calcnot, chkcond);
|
|
|
|
else
|
|
|
|
return false;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* |-operator */
|
|
|
|
if (execute(curitem + curitem->left, checkval, calcnot, chkcond))
|
2001-10-13 07:19:09 +08:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return execute(curitem + 1, checkval, calcnot, chkcond);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-10-25 13:50:21 +08:00
|
|
|
* boolean operations
|
2001-10-13 07:19:09 +08:00
|
|
|
*/
|
|
|
|
Datum
|
2001-10-25 13:50:21 +08:00
|
|
|
rexecqtxt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
return DirectFunctionCall2(
|
2001-10-25 13:50:21 +08:00
|
|
|
execqtxt,
|
|
|
|
PG_GETARG_DATUM(1),
|
|
|
|
PG_GETARG_DATUM(0)
|
2001-10-28 14:26:15 +08:00
|
|
|
);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2001-10-25 13:50:21 +08:00
|
|
|
execqtxt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
txtidx *val = (txtidx *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
|
|
|
|
QUERYTYPE *query = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
|
|
|
|
CHKVAL chkval;
|
|
|
|
bool result;
|
|
|
|
|
2002-08-23 10:56:36 +08:00
|
|
|
if (!val->size || !query->size)
|
2001-10-25 13:50:21 +08:00
|
|
|
{
|
|
|
|
PG_FREE_IF_COPY(val, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
|
|
|
PG_RETURN_BOOL(false);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
chkval.arrb = ARRPTR(val);
|
|
|
|
chkval.arre = chkval.arrb + val->size;
|
|
|
|
chkval.values = STRPTR(val);
|
2001-10-25 13:50:21 +08:00
|
|
|
chkval.operand = GETOPERAND(query);
|
|
|
|
result = execute(
|
|
|
|
GETQUERY(query),
|
|
|
|
&chkval,
|
|
|
|
true,
|
|
|
|
checkcondition_str
|
|
|
|
);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(val, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
|
|
|
PG_RETURN_BOOL(result);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find left operand in polish notation view
|
|
|
|
*/
|
|
|
|
static void
|
2001-10-25 13:50:21 +08:00
|
|
|
findoprnd(ITEM * ptr, int4 *pos)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
#ifdef BS_DEBUG
|
2003-05-28 01:49:47 +08:00
|
|
|
elog(DEBUG4, (ptr[*pos].type == OPR) ?
|
2003-07-25 01:52:50 +08:00
|
|
|
"%d %c" : "%d %d", *pos, ptr[*pos].val);
|
2001-10-13 07:19:09 +08:00
|
|
|
#endif
|
2001-10-25 13:50:21 +08:00
|
|
|
if (ptr[*pos].type == VAL || ptr[*pos].type == VALTRUE)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
ptr[*pos].left = 0;
|
|
|
|
(*pos)++;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else if (ptr[*pos].val == (int4) '!')
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
ptr[*pos].left = 1;
|
|
|
|
(*pos)++;
|
2001-10-25 13:50:21 +08:00
|
|
|
findoprnd(ptr, pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ITEM *curitem = &ptr[*pos];
|
|
|
|
int4 tmp = *pos;
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
(*pos)++;
|
2001-10-25 13:50:21 +08:00
|
|
|
findoprnd(ptr, pos);
|
2001-10-13 07:19:09 +08:00
|
|
|
curitem->left = *pos - tmp;
|
2001-10-25 13:50:21 +08:00
|
|
|
findoprnd(ptr, pos);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* input
|
|
|
|
*/
|
2001-10-25 13:50:21 +08:00
|
|
|
static QUERYTYPE *
|
|
|
|
queryin(char *buf, void (*pushval) (QPRS_STATE *, int, char *, int))
|
|
|
|
{
|
|
|
|
QPRS_STATE state;
|
|
|
|
int4 i;
|
|
|
|
QUERYTYPE *query;
|
|
|
|
int4 commonlen;
|
|
|
|
ITEM *ptr;
|
|
|
|
NODE *tmp;
|
|
|
|
int4 pos = 0;
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
#ifdef BS_DEBUG
|
2001-10-25 13:50:21 +08:00
|
|
|
char pbuf[16384],
|
|
|
|
*cur;
|
2001-10-13 07:19:09 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* init state */
|
|
|
|
state.buf = buf;
|
|
|
|
state.state = WAITOPERAND;
|
|
|
|
state.count = 0;
|
|
|
|
state.num = 0;
|
2001-10-25 13:50:21 +08:00
|
|
|
state.str = NULL;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
/* init value parser's state */
|
|
|
|
state.valstate.oprisdelim = true;
|
2001-10-25 13:50:21 +08:00
|
|
|
state.valstate.len = 32;
|
|
|
|
state.valstate.word = (char *) palloc(state.valstate.len);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
/* init list of operand */
|
2001-10-25 13:50:21 +08:00
|
|
|
state.sumlen = 0;
|
|
|
|
state.lenop = 64;
|
|
|
|
state.curop = state.op = (char *) palloc(state.lenop);
|
2001-10-13 07:19:09 +08:00
|
|
|
*(state.curop) = '\0';
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
/* parse query & make polish notation (postfix, but in reverse order) */
|
2001-10-25 13:50:21 +08:00
|
|
|
makepol(&state, pushval);
|
|
|
|
pfree(state.valstate.word);
|
|
|
|
if (!state.num)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("empty query")));
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
/* make finish struct */
|
|
|
|
commonlen = COMPUTESIZE(state.num, state.sumlen);
|
2001-10-25 13:50:21 +08:00
|
|
|
query = (QUERYTYPE *) palloc(commonlen);
|
2001-10-13 07:19:09 +08:00
|
|
|
query->len = commonlen;
|
|
|
|
query->size = state.num;
|
|
|
|
ptr = GETQUERY(query);
|
|
|
|
|
|
|
|
/* set item in polish notation */
|
2001-10-25 13:50:21 +08:00
|
|
|
for (i = 0; i < state.num; i++)
|
|
|
|
{
|
|
|
|
ptr[i].type = state.str->type;
|
2001-10-13 07:19:09 +08:00
|
|
|
ptr[i].val = state.str->val;
|
2001-10-25 13:50:21 +08:00
|
|
|
ptr[i].distance = state.str->distance;
|
2001-10-13 07:19:09 +08:00
|
|
|
ptr[i].length = state.str->length;
|
|
|
|
tmp = state.str->next;
|
2001-10-25 13:50:21 +08:00
|
|
|
pfree(state.str);
|
2001-10-13 07:19:09 +08:00
|
|
|
state.str = tmp;
|
|
|
|
}
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
/* set user friendly-operand view */
|
|
|
|
memcpy((void *) GETOPERAND(query), (void *) state.op, state.sumlen);
|
|
|
|
pfree(state.op);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
/* set left operand's position for every operator */
|
2001-10-13 07:19:09 +08:00
|
|
|
pos = 0;
|
2001-10-25 13:50:21 +08:00
|
|
|
findoprnd(ptr, &pos);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
#ifdef BS_DEBUG
|
|
|
|
cur = pbuf;
|
|
|
|
*cur = '\0';
|
2001-10-25 13:50:21 +08:00
|
|
|
for (i = 0; i < query->size; i++)
|
|
|
|
{
|
|
|
|
if (ptr[i].type == OPR)
|
2001-10-13 07:19:09 +08:00
|
|
|
sprintf(cur, "%c(%d) ", ptr[i].val, ptr[i].left);
|
2001-10-25 13:50:21 +08:00
|
|
|
else
|
|
|
|
sprintf(cur, "%d(%s) ", ptr[i].val, GETOPERAND(query) + ptr[i].distance);
|
|
|
|
cur = strchr(cur, '\0');
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
2003-05-28 01:49:47 +08:00
|
|
|
elog(DEBUG4, "POR: %s", pbuf);
|
2001-10-13 07:19:09 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* in without morphology
|
|
|
|
*/
|
|
|
|
Datum
|
2001-10-25 13:50:21 +08:00
|
|
|
qtxt_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_POINTER(queryin((char *) PG_GETARG_POINTER(0), pushval_asis));
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* in with morphology
|
|
|
|
*/
|
|
|
|
Datum
|
2001-10-25 13:50:21 +08:00
|
|
|
mqtxt_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
QUERYTYPE *query;
|
|
|
|
ITEM *res;
|
|
|
|
int4 len;
|
|
|
|
|
|
|
|
#ifdef BS_DEBUG
|
|
|
|
ITEM *ptr;
|
|
|
|
int4 i;
|
|
|
|
char pbuf[16384],
|
|
|
|
*cur;
|
2001-10-13 07:19:09 +08:00
|
|
|
#endif
|
|
|
|
initmorph();
|
2001-10-25 13:50:21 +08:00
|
|
|
query = queryin((char *) PG_GETARG_POINTER(0), pushval_morph);
|
|
|
|
res = clean_fakeval(GETQUERY(query), &len);
|
|
|
|
if (!res)
|
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
query->len = HDRSIZEQT;
|
|
|
|
query->size = 0;
|
2002-08-23 10:56:36 +08:00
|
|
|
PG_RETURN_POINTER(query);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
memcpy((void *) GETQUERY(query), (void *) res, len * sizeof(ITEM));
|
2001-10-13 07:19:09 +08:00
|
|
|
#ifdef BS_DEBUG
|
|
|
|
cur = pbuf;
|
|
|
|
*cur = '\0';
|
|
|
|
ptr = GETQUERY(query);
|
2001-10-25 13:50:21 +08:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (ptr[i].type == OPR)
|
2001-10-13 07:19:09 +08:00
|
|
|
sprintf(cur, "%c(%d) ", ptr[i].val, ptr[i].left);
|
2001-10-25 13:50:21 +08:00
|
|
|
else
|
|
|
|
sprintf(cur, "%d(%s) ", ptr[i].val, GETOPERAND(query) + ptr[i].distance);
|
|
|
|
cur = strchr(cur, '\0');
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
2003-05-28 01:49:47 +08:00
|
|
|
elog(DEBUG4, "POR: %s", pbuf);
|
2001-10-13 07:19:09 +08:00
|
|
|
#endif
|
|
|
|
pfree(res);
|
2001-10-25 13:50:21 +08:00
|
|
|
PG_RETURN_POINTER(query);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* out function
|
|
|
|
*/
|
2001-10-25 13:50:21 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ITEM *curpol;
|
|
|
|
char *buf;
|
|
|
|
char *cur;
|
|
|
|
char *op;
|
|
|
|
int4 buflen;
|
2001-11-06 01:46:40 +08:00
|
|
|
} INFIX;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 09:29:37 +08:00
|
|
|
#define RESIZEBUF(inf,addsize) \
|
|
|
|
while( ( inf->cur - inf->buf ) + addsize + 1 >= inf->buflen ) \
|
|
|
|
{ \
|
2001-10-13 07:19:09 +08:00
|
|
|
int4 len = inf->cur - inf->buf; \
|
|
|
|
inf->buflen *= 2; \
|
|
|
|
inf->buf = (char*) repalloc( (void*)inf->buf, inf->buflen ); \
|
|
|
|
inf->cur = inf->buf + len; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-10-25 13:50:21 +08:00
|
|
|
* recursive walk on tree and print it in
|
2001-10-13 07:19:09 +08:00
|
|
|
* infix (human-readable) view
|
|
|
|
*/
|
|
|
|
static void
|
2001-10-25 13:50:21 +08:00
|
|
|
infix(INFIX * in, bool first)
|
|
|
|
{
|
|
|
|
if (in->curpol->type == VAL)
|
|
|
|
{
|
|
|
|
char *op = in->op + in->curpol->distance;
|
|
|
|
|
|
|
|
RESIZEBUF(in, in->curpol->length * 2 + 2);
|
|
|
|
*(in->cur) = '\'';
|
|
|
|
in->cur++;
|
|
|
|
while (*op)
|
|
|
|
{
|
|
|
|
if (*op == '\'')
|
|
|
|
{
|
|
|
|
*(in->cur) = '\\';
|
|
|
|
in->cur++;
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
*(in->cur) = *op;
|
2001-10-25 13:50:21 +08:00
|
|
|
op++;
|
|
|
|
in->cur++;
|
|
|
|
}
|
|
|
|
*(in->cur) = '\'';
|
|
|
|
in->cur++;
|
2001-10-13 07:19:09 +08:00
|
|
|
*(in->cur) = '\0';
|
|
|
|
in->curpol++;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else if (in->curpol->val == (int4) '!')
|
|
|
|
{
|
|
|
|
bool isopr = false;
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
RESIZEBUF(in, 1);
|
|
|
|
*(in->cur) = '!';
|
|
|
|
in->cur++;
|
|
|
|
*(in->cur) = '\0';
|
|
|
|
in->curpol++;
|
2001-10-25 13:50:21 +08:00
|
|
|
if (in->curpol->type == OPR)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
isopr = true;
|
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, "( ");
|
2001-10-25 13:50:21 +08:00
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
}
|
|
|
|
infix(in, isopr);
|
|
|
|
if (isopr)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, " )");
|
2001-10-25 13:50:21 +08:00
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int4 op = in->curpol->val;
|
|
|
|
INFIX nrm;
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
in->curpol++;
|
2001-10-25 13:50:21 +08:00
|
|
|
if (op == (int4) '|' && !first)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, "( ");
|
2001-10-25 13:50:21 +08:00
|
|
|
in->cur = strchr(in->cur, '\0');
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
nrm.curpol = in->curpol;
|
|
|
|
nrm.op = in->op;
|
|
|
|
nrm.buflen = 16;
|
2001-10-25 13:50:21 +08:00
|
|
|
nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
/* get right operand */
|
2001-10-25 13:50:21 +08:00
|
|
|
infix(&nrm, false);
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
/* get & print left operand */
|
|
|
|
in->curpol = nrm.curpol;
|
2001-10-25 13:50:21 +08:00
|
|
|
infix(in, false);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
/* print operator & right operand */
|
|
|
|
RESIZEBUF(in, 3 + (nrm.cur - nrm.buf));
|
2001-10-13 07:19:09 +08:00
|
|
|
sprintf(in->cur, " %c %s", op, nrm.buf);
|
2001-10-25 13:50:21 +08:00
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
pfree(nrm.buf);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
if (op == (int4) '|' && !first)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, " )");
|
2001-10-25 13:50:21 +08:00
|
|
|
in->cur = strchr(in->cur, '\0');
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
2001-10-25 13:50:21 +08:00
|
|
|
qtxt_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
QUERYTYPE *query = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
|
|
|
|
INFIX nrm;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (query->size == 0)
|
|
|
|
{
|
|
|
|
char *b = palloc(1);
|
|
|
|
|
|
|
|
*b = '\0';
|
2002-08-23 10:56:36 +08:00
|
|
|
PG_RETURN_POINTER(b);
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
2001-10-13 07:19:09 +08:00
|
|
|
nrm.curpol = GETQUERY(query);
|
|
|
|
nrm.buflen = 32;
|
2001-10-25 13:50:21 +08:00
|
|
|
nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
|
2001-10-13 07:19:09 +08:00
|
|
|
*(nrm.cur) = '\0';
|
|
|
|
nrm.op = GETOPERAND(query);
|
2001-10-25 13:50:21 +08:00
|
|
|
infix(&nrm, true);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(query, 0);
|
|
|
|
PG_RETURN_POINTER(nrm.buf);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* debug function, used only for view query
|
|
|
|
* which will be executed in non-leaf pages in index
|
|
|
|
*/
|
|
|
|
Datum
|
2001-10-25 13:50:21 +08:00
|
|
|
querytree(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
QUERYTYPE *query = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
|
|
|
|
INFIX nrm;
|
|
|
|
text *res;
|
|
|
|
ITEM *q;
|
|
|
|
int4 len;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (query->size == 0)
|
|
|
|
{
|
2002-08-23 10:56:36 +08:00
|
|
|
res = (text *) palloc(VARHDRSZ);
|
|
|
|
VARATT_SIZEP(res) = VARHDRSZ;
|
|
|
|
PG_RETURN_POINTER(res);
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
q = clean_NOT(GETQUERY(query), &len);
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
if (!q)
|
|
|
|
{
|
|
|
|
res = (text *) palloc(1 + VARHDRSZ);
|
2001-10-13 07:19:09 +08:00
|
|
|
VARATT_SIZEP(res) = 1 + VARHDRSZ;
|
2001-10-25 13:50:21 +08:00
|
|
|
*((char *) VARDATA(res)) = 'T';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
nrm.curpol = q;
|
|
|
|
nrm.buflen = 32;
|
2001-10-25 13:50:21 +08:00
|
|
|
nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
|
2001-10-13 07:19:09 +08:00
|
|
|
*(nrm.cur) = '\0';
|
|
|
|
nrm.op = GETOPERAND(query);
|
2001-10-25 13:50:21 +08:00
|
|
|
infix(&nrm, true);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
res = (text *) palloc(nrm.cur - nrm.buf + VARHDRSZ);
|
|
|
|
VARATT_SIZEP(res) = nrm.cur - nrm.buf + VARHDRSZ;
|
|
|
|
strncpy(VARDATA(res), nrm.buf, nrm.cur - nrm.buf);
|
2001-10-13 07:19:09 +08:00
|
|
|
pfree(q);
|
|
|
|
}
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
PG_FREE_IF_COPY(query, 0);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
PG_RETURN_POINTER(res);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
2002-11-02 06:52:34 +08:00
|
|
|
|
|
|
|
#include "parser.c"
|