2007-08-21 01:11:32 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* tsquery.c
|
|
|
|
* I/O functions for tsquery
|
|
|
|
*
|
|
|
|
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2007-09-07 15:35:11 +00:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/tsquery.c,v 1.4 2007/09/07 15:35:10 teodor Exp $
|
2007-08-21 01:11:32 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "libpq/pqformat.h"
|
2007-08-31 02:26:29 +00:00
|
|
|
#include "miscadmin.h"
|
2007-08-21 01:11:32 +00:00
|
|
|
#include "tsearch/ts_locale.h"
|
|
|
|
#include "tsearch/ts_type.h"
|
|
|
|
#include "tsearch/ts_utils.h"
|
|
|
|
#include "utils/memutils.h"
|
|
|
|
#include "utils/pg_crc.h"
|
2007-09-07 15:35:11 +00:00
|
|
|
#include "nodes/bitmapset.h"
|
2007-08-21 01:11:32 +00:00
|
|
|
|
2007-08-31 02:26:29 +00:00
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
struct TSQueryParserStateData
|
|
|
|
{
|
|
|
|
/* State for gettoken_query */
|
|
|
|
char *buffer; /* entire string we are scanning */
|
|
|
|
char *buf; /* current scan point */
|
|
|
|
int state;
|
|
|
|
int count; /* nesting count, incremented by (,
|
|
|
|
decremented by ) */
|
|
|
|
|
|
|
|
/* polish (prefix) notation in list, filled in by push* functions */
|
|
|
|
List *polstr;
|
|
|
|
|
|
|
|
/* Strings from operands are collected in op. curop is a pointer to
|
|
|
|
* the end of used space of op. */
|
|
|
|
char *op;
|
|
|
|
char *curop;
|
|
|
|
int lenop; /* allocated size of op */
|
|
|
|
int sumlen; /* used size of op */
|
|
|
|
|
|
|
|
/* state for value's parser */
|
|
|
|
TSVectorParseState valstate;
|
|
|
|
};
|
|
|
|
|
2007-08-21 01:11:32 +00:00
|
|
|
/* parser's states */
|
|
|
|
#define WAITOPERAND 1
|
|
|
|
#define WAITOPERATOR 2
|
|
|
|
#define WAITFIRSTOPERAND 3
|
|
|
|
#define WAITSINGLEOPERAND 4
|
|
|
|
|
|
|
|
/*
|
2007-09-07 15:09:56 +00:00
|
|
|
* subroutine to parse the weight part, like ':1AB' of a query.
|
2007-08-21 01:11:32 +00:00
|
|
|
*/
|
|
|
|
static char *
|
2007-09-07 15:09:56 +00:00
|
|
|
get_weight(char *buf, int16 *weight)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
*weight = 0;
|
|
|
|
|
|
|
|
if (!t_iseq(buf, ':'))
|
|
|
|
return buf;
|
|
|
|
|
|
|
|
buf++;
|
|
|
|
while (*buf && pg_mblen(buf) == 1)
|
|
|
|
{
|
|
|
|
switch (*buf)
|
|
|
|
{
|
|
|
|
case 'a':
|
|
|
|
case 'A':
|
|
|
|
*weight |= 1 << 3;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
case 'B':
|
|
|
|
*weight |= 1 << 2;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
case 'C':
|
|
|
|
*weight |= 1 << 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
case 'D':
|
|
|
|
*weight |= 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
/*
|
|
|
|
* token types for parsing
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
PT_END = 0,
|
|
|
|
PT_ERR = 1,
|
|
|
|
PT_VAL = 2,
|
|
|
|
PT_OPR = 3,
|
|
|
|
PT_OPEN = 4,
|
|
|
|
PT_CLOSE = 5,
|
|
|
|
} ts_tokentype;
|
|
|
|
|
2007-08-21 01:11:32 +00:00
|
|
|
/*
|
|
|
|
* get token from query string
|
2007-09-07 15:09:56 +00:00
|
|
|
*
|
|
|
|
* *operator is filled in with OP_* when return values is PT_OPR
|
|
|
|
* *strval, *lenval and *weight are filled in when return value is PT_VAL
|
2007-08-21 01:11:32 +00:00
|
|
|
*/
|
2007-09-07 15:09:56 +00:00
|
|
|
static ts_tokentype
|
|
|
|
gettoken_query(TSQueryParserState state,
|
|
|
|
int8 *operator,
|
|
|
|
int *lenval, char **strval, int16 *weight)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
switch (state->state)
|
|
|
|
{
|
|
|
|
case WAITFIRSTOPERAND:
|
|
|
|
case WAITOPERAND:
|
|
|
|
if (t_iseq(state->buf, '!'))
|
|
|
|
{
|
|
|
|
(state->buf)++; /* can safely ++, t_iseq guarantee
|
|
|
|
* that pg_mblen()==1 */
|
2007-09-07 15:09:56 +00:00
|
|
|
*operator = OP_NOT;
|
2007-08-21 01:11:32 +00:00
|
|
|
state->state = WAITOPERAND;
|
2007-09-07 15:09:56 +00:00
|
|
|
return PT_OPR;
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
else if (t_iseq(state->buf, '('))
|
|
|
|
{
|
|
|
|
state->count++;
|
|
|
|
(state->buf)++;
|
|
|
|
state->state = WAITOPERAND;
|
2007-09-07 15:09:56 +00:00
|
|
|
return PT_OPEN;
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
else if (t_iseq(state->buf, ':'))
|
|
|
|
{
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("syntax error at start of operand in tsearch query: \"%s\"",
|
|
|
|
state->buffer)));
|
|
|
|
}
|
|
|
|
else if (!t_isspace(state->buf))
|
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
/* We rely on the tsvector parser to parse the value for us */
|
|
|
|
reset_tsvector_parser(state->valstate, state->buf);
|
|
|
|
if (gettoken_tsvector(state->valstate, strval, lenval, NULL, NULL, &state->buf))
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
state->buf = get_weight(state->buf, weight);
|
2007-08-21 01:11:32 +00:00
|
|
|
state->state = WAITOPERATOR;
|
2007-09-07 15:09:56 +00:00
|
|
|
return PT_VAL;
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
else if (state->state == WAITFIRSTOPERAND)
|
2007-09-07 15:09:56 +00:00
|
|
|
return PT_END;
|
2007-08-21 01:11:32 +00:00
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("no operand in tsearch query: \"%s\"",
|
|
|
|
state->buffer)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WAITOPERATOR:
|
2007-09-07 15:09:56 +00:00
|
|
|
if (t_iseq(state->buf, '&'))
|
|
|
|
{
|
|
|
|
state->state = WAITOPERAND;
|
|
|
|
*operator = OP_AND;
|
|
|
|
(state->buf)++;
|
|
|
|
return PT_OPR;
|
|
|
|
}
|
|
|
|
if (t_iseq(state->buf, '|'))
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
state->state = WAITOPERAND;
|
2007-09-07 15:09:56 +00:00
|
|
|
*operator = OP_OR;
|
2007-08-21 01:11:32 +00:00
|
|
|
(state->buf)++;
|
2007-09-07 15:09:56 +00:00
|
|
|
return PT_OPR;
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
else if (t_iseq(state->buf, ')'))
|
|
|
|
{
|
|
|
|
(state->buf)++;
|
|
|
|
state->count--;
|
2007-09-07 15:09:56 +00:00
|
|
|
return (state->count < 0) ? PT_ERR : PT_CLOSE;
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
else if (*(state->buf) == '\0')
|
2007-09-07 15:09:56 +00:00
|
|
|
return (state->count) ? PT_ERR : PT_END;
|
2007-08-21 01:11:32 +00:00
|
|
|
else if (!t_isspace(state->buf))
|
2007-09-07 15:09:56 +00:00
|
|
|
return PT_ERR;
|
2007-08-21 01:11:32 +00:00
|
|
|
break;
|
|
|
|
case WAITSINGLEOPERAND:
|
|
|
|
if (*(state->buf) == '\0')
|
2007-09-07 15:09:56 +00:00
|
|
|
return PT_END;
|
2007-08-21 01:11:32 +00:00
|
|
|
*strval = state->buf;
|
|
|
|
*lenval = strlen(state->buf);
|
|
|
|
state->buf += strlen(state->buf);
|
|
|
|
state->count++;
|
2007-09-07 15:09:56 +00:00
|
|
|
return PT_VAL;
|
2007-08-21 01:11:32 +00:00
|
|
|
default:
|
2007-09-07 15:09:56 +00:00
|
|
|
return PT_ERR;
|
2007-08-21 01:11:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
state->buf += pg_mblen(state->buf);
|
|
|
|
}
|
2007-09-07 15:09:56 +00:00
|
|
|
return PT_END;
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-09-07 15:09:56 +00:00
|
|
|
* Push an operator to state->polstr
|
2007-08-21 01:11:32 +00:00
|
|
|
*/
|
|
|
|
void
|
2007-09-07 15:09:56 +00:00
|
|
|
pushOperator(TSQueryParserState state, int8 oper)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
QueryOperator *tmp;
|
|
|
|
|
|
|
|
Assert(oper == OP_NOT || oper == OP_AND || oper == OP_OR);
|
|
|
|
|
|
|
|
tmp = (QueryOperator *) palloc(sizeof(QueryOperator));
|
|
|
|
tmp->type = QI_OPR;
|
|
|
|
tmp->oper = oper;
|
|
|
|
/* left is filled in later with findoprnd */
|
|
|
|
|
|
|
|
state->polstr = lcons(tmp, state->polstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pushValue_internal(TSQueryParserState state, pg_crc32 valcrc, int distance, int lenval, int weight)
|
|
|
|
{
|
|
|
|
QueryOperand *tmp;
|
2007-08-21 01:11:32 +00:00
|
|
|
|
|
|
|
if (distance >= MAXSTRPOS)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("value is too big in tsearch query: \"%s\"",
|
|
|
|
state->buffer)));
|
|
|
|
if (lenval >= MAXSTRLEN)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("operand is too long in tsearch query: \"%s\"",
|
|
|
|
state->buffer)));
|
2007-09-07 15:09:56 +00:00
|
|
|
|
|
|
|
tmp = (QueryOperand *) palloc(sizeof(QueryOperand));
|
|
|
|
tmp->type = QI_VAL;
|
|
|
|
tmp->weight = weight;
|
|
|
|
tmp->valcrc = (int32) valcrc;
|
2007-08-21 01:11:32 +00:00
|
|
|
tmp->length = lenval;
|
2007-09-07 15:09:56 +00:00
|
|
|
tmp->distance = distance;
|
|
|
|
|
|
|
|
state->polstr = lcons(tmp, state->polstr);
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-09-07 15:09:56 +00:00
|
|
|
* Push an operand to state->polstr.
|
|
|
|
*
|
|
|
|
* strval must point to a string equal to state->curop. lenval is the length
|
|
|
|
* of the string.
|
2007-08-21 01:11:32 +00:00
|
|
|
*/
|
|
|
|
void
|
2007-09-07 15:09:56 +00:00
|
|
|
pushValue(TSQueryParserState state, char *strval, int lenval, int2 weight)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
pg_crc32 valcrc;
|
2007-08-21 01:11:32 +00:00
|
|
|
|
|
|
|
if (lenval >= MAXSTRLEN)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("word is too long in tsearch query: \"%s\"",
|
|
|
|
state->buffer)));
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
INIT_CRC32(valcrc);
|
|
|
|
COMP_CRC32(valcrc, strval, lenval);
|
|
|
|
FIN_CRC32(valcrc);
|
|
|
|
pushValue_internal(state, valcrc, state->curop - state->op, lenval, weight);
|
2007-08-21 01:11:32 +00:00
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
/* append the value string to state.op, enlarging buffer if needed first */
|
2007-08-21 01:11:32 +00:00
|
|
|
while (state->curop - state->op + lenval + 1 >= state->lenop)
|
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
int used = state->curop - state->op;
|
2007-08-21 01:11:32 +00:00
|
|
|
|
|
|
|
state->lenop *= 2;
|
|
|
|
state->op = (char *) repalloc((void *) state->op, state->lenop);
|
2007-09-07 15:09:56 +00:00
|
|
|
state->curop = state->op + used;
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
memcpy((void *) state->curop, (void *) strval, lenval);
|
|
|
|
state->curop += lenval;
|
|
|
|
*(state->curop) = '\0';
|
|
|
|
state->curop++;
|
|
|
|
state->sumlen += lenval + 1 /* \0 */ ;
|
|
|
|
}
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Push a stopword placeholder to state->polstr
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pushStop(TSQueryParserState state)
|
|
|
|
{
|
|
|
|
QueryOperand *tmp;
|
|
|
|
|
|
|
|
tmp = (QueryOperand *) palloc(sizeof(QueryOperand));
|
|
|
|
tmp->type = QI_VALSTOP;
|
|
|
|
|
|
|
|
state->polstr = lcons(tmp, state->polstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-21 01:11:32 +00:00
|
|
|
#define STACKDEPTH 32
|
2007-08-31 02:26:29 +00:00
|
|
|
|
2007-08-21 01:11:32 +00:00
|
|
|
/*
|
2007-09-07 15:09:56 +00:00
|
|
|
* Make polish (prefix) notation of query.
|
|
|
|
*
|
|
|
|
* See parse_tsquery for explanation of pushval.
|
2007-08-21 01:11:32 +00:00
|
|
|
*/
|
2007-09-07 15:09:56 +00:00
|
|
|
static void
|
|
|
|
makepol(TSQueryParserState state,
|
|
|
|
PushFunction pushval,
|
|
|
|
void *opaque)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
int8 operator = 0;
|
|
|
|
ts_tokentype type;
|
|
|
|
int lenval = 0;
|
2007-08-21 01:11:32 +00:00
|
|
|
char *strval = NULL;
|
2007-09-07 15:09:56 +00:00
|
|
|
int8 opstack[STACKDEPTH];
|
|
|
|
int lenstack = 0;
|
|
|
|
int16 weight = 0;
|
2007-08-21 01:11:32 +00:00
|
|
|
|
2007-08-31 02:26:29 +00:00
|
|
|
/* since this function recurses, it could be driven to stack overflow */
|
|
|
|
check_stack_depth();
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
while ((type = gettoken_query(state, &operator, &lenval, &strval, &weight)) != PT_END)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
case PT_VAL:
|
|
|
|
pushval(opaque, state, strval, lenval, weight);
|
|
|
|
while (lenstack && (opstack[lenstack - 1] == OP_AND ||
|
|
|
|
opstack[lenstack - 1] == OP_NOT))
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
lenstack--;
|
2007-09-07 15:09:56 +00:00
|
|
|
pushOperator(state, opstack[lenstack]);
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-09-07 15:09:56 +00:00
|
|
|
case PT_OPR:
|
|
|
|
if (lenstack && operator == OP_OR)
|
|
|
|
pushOperator(state, OP_OR);
|
2007-08-21 01:11:32 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (lenstack == STACKDEPTH) /* internal error */
|
|
|
|
elog(ERROR, "tsquery stack too small");
|
2007-09-07 15:09:56 +00:00
|
|
|
opstack[lenstack] = operator;
|
2007-08-21 01:11:32 +00:00
|
|
|
lenstack++;
|
|
|
|
}
|
|
|
|
break;
|
2007-09-07 15:09:56 +00:00
|
|
|
case PT_OPEN:
|
|
|
|
makepol(state, pushval, opaque);
|
|
|
|
|
|
|
|
if (lenstack && (opstack[lenstack - 1] == OP_AND ||
|
|
|
|
opstack[lenstack - 1] == OP_NOT))
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
lenstack--;
|
2007-09-07 15:09:56 +00:00
|
|
|
pushOperator(state, opstack[lenstack]);
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-09-07 15:09:56 +00:00
|
|
|
case PT_CLOSE:
|
2007-08-21 01:11:32 +00:00
|
|
|
while (lenstack)
|
|
|
|
{
|
|
|
|
lenstack--;
|
2007-09-07 15:09:56 +00:00
|
|
|
pushOperator(state, opstack[lenstack]);
|
2007-08-21 01:11:32 +00:00
|
|
|
};
|
2007-09-07 15:09:56 +00:00
|
|
|
return;
|
|
|
|
case PT_ERR:
|
2007-08-21 01:11:32 +00:00
|
|
|
default:
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("syntax error in tsearch query: \"%s\"",
|
|
|
|
state->buffer)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (lenstack)
|
|
|
|
{
|
|
|
|
lenstack--;
|
2007-09-07 15:09:56 +00:00
|
|
|
pushOperator(state, opstack[lenstack]);
|
|
|
|
}
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
/*
|
|
|
|
* Fills in the left-fields previously left unfilled. The input
|
|
|
|
* QueryItems must be in polish (prefix) notation.
|
|
|
|
*/
|
2007-08-21 01:11:32 +00:00
|
|
|
static void
|
2007-09-07 15:35:11 +00:00
|
|
|
findoprnd(QueryItem *ptr, uint32 *pos)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
/* since this function recurses, it could be driven to stack overflow. */
|
|
|
|
check_stack_depth();
|
|
|
|
|
|
|
|
if (ptr[*pos].type == QI_VAL ||
|
|
|
|
ptr[*pos].type == QI_VALSTOP) /* need to handle VALSTOP here,
|
2007-09-07 15:35:11 +00:00
|
|
|
* they haven't been cleaned
|
2007-09-07 15:09:56 +00:00
|
|
|
* away yet.
|
|
|
|
*/
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
(*pos)++;
|
|
|
|
}
|
2007-09-07 15:09:56 +00:00
|
|
|
else
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
Assert(ptr[*pos].type == QI_OPR);
|
2007-08-21 01:11:32 +00:00
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
if (ptr[*pos].operator.oper == OP_NOT)
|
|
|
|
{
|
|
|
|
ptr[*pos].operator.left = 1;
|
|
|
|
(*pos)++;
|
|
|
|
findoprnd(ptr, pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QueryOperator *curitem = &ptr[*pos].operator;
|
|
|
|
int tmp = *pos;
|
|
|
|
|
|
|
|
Assert(curitem->oper == OP_AND || curitem->oper == OP_OR);
|
|
|
|
|
|
|
|
(*pos)++;
|
|
|
|
findoprnd(ptr, pos);
|
|
|
|
curitem->left = *pos - tmp;
|
|
|
|
findoprnd(ptr, pos);
|
|
|
|
}
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-09-07 15:09:56 +00:00
|
|
|
* Each value (operand) in the query is be passed to pushval. pushval can
|
|
|
|
* transform the simple value to an arbitrarily complex expression using
|
|
|
|
* pushValue and pushOperator. It must push a single value with pushValue,
|
|
|
|
* a complete expression with all operands, or a a stopword placeholder
|
|
|
|
* with pushStop, otherwise the prefix notation representation will be broken,
|
|
|
|
* having an operator with no operand.
|
|
|
|
*
|
|
|
|
* opaque is passed on to pushval as is, pushval can use it to store its
|
|
|
|
* private state.
|
|
|
|
*
|
|
|
|
* The returned query might contain QI_STOPVAL nodes. The caller is responsible
|
|
|
|
* for cleaning them up (with clean_fakeval)
|
2007-08-21 01:11:32 +00:00
|
|
|
*/
|
|
|
|
TSQuery
|
2007-09-07 15:09:56 +00:00
|
|
|
parse_tsquery(char *buf,
|
|
|
|
PushFunction pushval,
|
|
|
|
void *opaque,
|
|
|
|
bool isplain)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
struct TSQueryParserStateData state;
|
|
|
|
int i;
|
2007-08-21 01:11:32 +00:00
|
|
|
TSQuery query;
|
2007-09-07 15:09:56 +00:00
|
|
|
int commonlen;
|
2007-08-21 01:11:32 +00:00
|
|
|
QueryItem *ptr;
|
2007-09-07 15:35:11 +00:00
|
|
|
uint32 pos = 0;
|
2007-09-07 15:09:56 +00:00
|
|
|
ListCell *cell;
|
2007-08-21 01:11:32 +00:00
|
|
|
|
|
|
|
/* init state */
|
|
|
|
state.buffer = buf;
|
|
|
|
state.buf = buf;
|
|
|
|
state.state = (isplain) ? WAITSINGLEOPERAND : WAITFIRSTOPERAND;
|
|
|
|
state.count = 0;
|
2007-09-07 15:09:56 +00:00
|
|
|
state.polstr = NIL;
|
2007-08-21 01:11:32 +00:00
|
|
|
|
|
|
|
/* init value parser's state */
|
2007-09-07 15:09:56 +00:00
|
|
|
state.valstate = init_tsvector_parser(NULL, true);
|
2007-08-21 01:11:32 +00:00
|
|
|
|
|
|
|
/* init list of operand */
|
|
|
|
state.sumlen = 0;
|
|
|
|
state.lenop = 64;
|
|
|
|
state.curop = state.op = (char *) palloc(state.lenop);
|
|
|
|
*(state.curop) = '\0';
|
|
|
|
|
|
|
|
/* parse query & make polish notation (postfix, but in reverse order) */
|
2007-09-07 15:09:56 +00:00
|
|
|
makepol(&state, pushval, opaque);
|
|
|
|
|
|
|
|
close_tsvector_parser(state.valstate);
|
|
|
|
|
|
|
|
if (list_length(state.polstr) == 0)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
ereport(NOTICE,
|
|
|
|
(errmsg("tsearch query doesn't contain lexeme(s): \"%s\"",
|
|
|
|
state.buffer)));
|
|
|
|
query = (TSQuery) palloc(HDRSIZETQ);
|
|
|
|
SET_VARSIZE(query, HDRSIZETQ);
|
|
|
|
query->size = 0;
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
/* Pack the QueryItems in the final TSQuery struct to return to caller */
|
|
|
|
commonlen = COMPUTESIZE(list_length(state.polstr), state.sumlen);
|
|
|
|
query = (TSQuery) palloc0(commonlen);
|
2007-08-21 01:11:32 +00:00
|
|
|
SET_VARSIZE(query, commonlen);
|
2007-09-07 15:09:56 +00:00
|
|
|
query->size = list_length(state.polstr);
|
2007-08-21 01:11:32 +00:00
|
|
|
ptr = GETQUERY(query);
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
/* Copy QueryItems to TSQuery */
|
|
|
|
i = 0;
|
|
|
|
foreach(cell, state.polstr)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
QueryItem *item = (QueryItem *) lfirst(cell);
|
|
|
|
|
|
|
|
switch(item->type)
|
|
|
|
{
|
|
|
|
case QI_VAL:
|
|
|
|
memcpy(&ptr[i], item, sizeof(QueryOperand));
|
|
|
|
break;
|
|
|
|
case QI_VALSTOP:
|
|
|
|
ptr[i].type = QI_VALSTOP;
|
|
|
|
break;
|
|
|
|
case QI_OPR:
|
|
|
|
memcpy(&ptr[i], item, sizeof(QueryOperator));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "unknown QueryItem type %d", item->type);
|
|
|
|
}
|
|
|
|
i++;
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
/* Copy all the operand strings to TSQuery */
|
2007-08-21 01:11:32 +00:00
|
|
|
memcpy((void *) GETOPERAND(query), (void *) state.op, state.sumlen);
|
|
|
|
pfree(state.op);
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
/* Set left operand pointers for every operator. */
|
2007-08-21 01:11:32 +00:00
|
|
|
pos = 0;
|
|
|
|
findoprnd(ptr, &pos);
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
static void
|
|
|
|
pushval_asis(void *opaque, TSQueryParserState state, char *strval, int lenval,
|
|
|
|
int16 weight)
|
|
|
|
{
|
|
|
|
pushValue(state, strval, lenval, weight);
|
|
|
|
}
|
|
|
|
|
2007-08-21 01:11:32 +00:00
|
|
|
/*
|
|
|
|
* in without morphology
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
tsqueryin(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
char *in = PG_GETARG_CSTRING(0);
|
|
|
|
|
|
|
|
pg_verifymbstr(in, strlen(in), false);
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
PG_RETURN_TSQUERY(parse_tsquery(in, pushval_asis, NULL, false));
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* out function
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
QueryItem *curpol;
|
|
|
|
char *buf;
|
|
|
|
char *cur;
|
|
|
|
char *op;
|
2007-09-07 15:09:56 +00:00
|
|
|
int buflen;
|
2007-08-21 01:11:32 +00:00
|
|
|
} INFIX;
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
/* Makes sure inf->buf is large enough for adding 'addsize' bytes */
|
|
|
|
#define RESIZEBUF(inf, addsize) \
|
2007-08-21 01:11:32 +00:00
|
|
|
while( ( (inf)->cur - (inf)->buf ) + (addsize) + 1 >= (inf)->buflen ) \
|
|
|
|
{ \
|
2007-09-07 15:09:56 +00:00
|
|
|
int len = (inf)->cur - (inf)->buf; \
|
2007-08-21 01:11:32 +00:00
|
|
|
(inf)->buflen *= 2; \
|
|
|
|
(inf)->buf = (char*) repalloc( (void*)(inf)->buf, (inf)->buflen ); \
|
|
|
|
(inf)->cur = (inf)->buf + len; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* recursive walk on tree and print it in
|
|
|
|
* infix (human-readable) view
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
infix(INFIX * in, bool first)
|
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
/* since this function recurses, it could be driven to stack overflow. */
|
|
|
|
check_stack_depth();
|
|
|
|
|
|
|
|
if (in->curpol->type == QI_VAL)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
QueryOperand *curpol = &in->curpol->operand;
|
|
|
|
char *op = in->op + curpol->distance;
|
2007-08-21 01:11:32 +00:00
|
|
|
int clen;
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
RESIZEBUF(in, curpol->length * (pg_database_encoding_max_length() + 1) + 2 + 5);
|
2007-08-21 01:11:32 +00:00
|
|
|
*(in->cur) = '\'';
|
|
|
|
in->cur++;
|
|
|
|
while (*op)
|
|
|
|
{
|
|
|
|
if (t_iseq(op, '\''))
|
|
|
|
{
|
|
|
|
*(in->cur) = '\'';
|
|
|
|
in->cur++;
|
|
|
|
}
|
|
|
|
COPYCHAR(in->cur, op);
|
|
|
|
|
|
|
|
clen = pg_mblen(op);
|
|
|
|
op += clen;
|
|
|
|
in->cur += clen;
|
|
|
|
}
|
|
|
|
*(in->cur) = '\'';
|
|
|
|
in->cur++;
|
2007-09-07 15:09:56 +00:00
|
|
|
if (curpol->weight)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
*(in->cur) = ':';
|
|
|
|
in->cur++;
|
2007-09-07 15:09:56 +00:00
|
|
|
if (curpol->weight & (1 << 3))
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
*(in->cur) = 'A';
|
|
|
|
in->cur++;
|
|
|
|
}
|
2007-09-07 15:09:56 +00:00
|
|
|
if (curpol->weight & (1 << 2))
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
*(in->cur) = 'B';
|
|
|
|
in->cur++;
|
|
|
|
}
|
2007-09-07 15:09:56 +00:00
|
|
|
if (curpol->weight & (1 << 1))
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
*(in->cur) = 'C';
|
|
|
|
in->cur++;
|
|
|
|
}
|
2007-09-07 15:09:56 +00:00
|
|
|
if (curpol->weight & 1)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
*(in->cur) = 'D';
|
|
|
|
in->cur++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*(in->cur) = '\0';
|
|
|
|
in->curpol++;
|
|
|
|
}
|
2007-09-07 15:09:56 +00:00
|
|
|
else if (in->curpol->operator.oper == OP_NOT)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
bool isopr = false;
|
|
|
|
|
|
|
|
RESIZEBUF(in, 1);
|
|
|
|
*(in->cur) = '!';
|
|
|
|
in->cur++;
|
|
|
|
*(in->cur) = '\0';
|
|
|
|
in->curpol++;
|
2007-09-07 15:09:56 +00:00
|
|
|
|
|
|
|
if (in->curpol->type == QI_OPR)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
isopr = true;
|
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, "( ");
|
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
}
|
2007-09-07 15:09:56 +00:00
|
|
|
|
2007-08-21 01:11:32 +00:00
|
|
|
infix(in, isopr);
|
|
|
|
if (isopr)
|
|
|
|
{
|
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, " )");
|
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
int8 op = in->curpol->operator.oper;
|
2007-08-21 01:11:32 +00:00
|
|
|
INFIX nrm;
|
|
|
|
|
|
|
|
in->curpol++;
|
2007-09-07 15:09:56 +00:00
|
|
|
if (op == OP_OR && !first)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, "( ");
|
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
nrm.curpol = in->curpol;
|
|
|
|
nrm.op = in->op;
|
|
|
|
nrm.buflen = 16;
|
|
|
|
nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
|
|
|
|
|
|
|
|
/* get right operand */
|
|
|
|
infix(&nrm, false);
|
|
|
|
|
|
|
|
/* get & print left operand */
|
|
|
|
in->curpol = nrm.curpol;
|
|
|
|
infix(in, false);
|
|
|
|
|
|
|
|
/* print operator & right operand */
|
|
|
|
RESIZEBUF(in, 3 + (nrm.cur - nrm.buf));
|
2007-09-07 15:09:56 +00:00
|
|
|
switch(op)
|
|
|
|
{
|
|
|
|
case OP_OR:
|
|
|
|
sprintf(in->cur, " | %s", nrm.buf);
|
|
|
|
break;
|
|
|
|
case OP_AND:
|
|
|
|
sprintf(in->cur, " & %s", nrm.buf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* OP_NOT is handled in above if-branch*/
|
|
|
|
elog(ERROR, "unexpected operator type %d", op);
|
|
|
|
}
|
2007-08-21 01:11:32 +00:00
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
pfree(nrm.buf);
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
if (op == OP_OR && !first)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, " )");
|
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
tsqueryout(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(0);
|
|
|
|
INFIX nrm;
|
|
|
|
|
|
|
|
if (query->size == 0)
|
|
|
|
{
|
|
|
|
char *b = palloc(1);
|
|
|
|
|
|
|
|
*b = '\0';
|
|
|
|
PG_RETURN_POINTER(b);
|
|
|
|
}
|
|
|
|
nrm.curpol = GETQUERY(query);
|
|
|
|
nrm.buflen = 32;
|
|
|
|
nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
|
|
|
|
*(nrm.cur) = '\0';
|
|
|
|
nrm.op = GETOPERAND(query);
|
|
|
|
infix(&nrm, true);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(query, 0);
|
|
|
|
PG_RETURN_CSTRING(nrm.buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
tsquerysend(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(0);
|
|
|
|
StringInfoData buf;
|
|
|
|
int i;
|
|
|
|
QueryItem *item = GETQUERY(query);
|
|
|
|
|
|
|
|
pq_begintypsend(&buf);
|
|
|
|
|
|
|
|
pq_sendint(&buf, query->size, sizeof(int32));
|
|
|
|
for (i = 0; i < query->size; i++)
|
|
|
|
{
|
|
|
|
pq_sendint(&buf, item->type, sizeof(item->type));
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
switch(item->type)
|
|
|
|
{
|
|
|
|
case QI_VAL:
|
|
|
|
pq_sendint(&buf, item->operand.weight, sizeof(item->operand.weight));
|
|
|
|
pq_sendint(&buf, item->operand.valcrc, sizeof(item->operand.valcrc));
|
|
|
|
pq_sendint(&buf, item->operand.length, sizeof(int16));
|
|
|
|
/* istrue flag is just for temporary use in tsrank.c/Cover,
|
|
|
|
* so we don't need to transfer that */
|
|
|
|
break;
|
|
|
|
case QI_OPR:
|
|
|
|
pq_sendint(&buf, item->operator.oper, sizeof(item->operator.oper));
|
|
|
|
if (item->operator.oper != OP_NOT)
|
|
|
|
pq_sendint(&buf, item->operator.left, sizeof(item->operator.left));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "unknown tsquery node type %d", item->type);
|
|
|
|
}
|
2007-08-21 01:11:32 +00:00
|
|
|
item++;
|
|
|
|
}
|
|
|
|
|
|
|
|
item = GETQUERY(query);
|
|
|
|
for (i = 0; i < query->size; i++)
|
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
if (item->type == QI_VAL)
|
|
|
|
pq_sendbytes(&buf, GETOPERAND(query) + item->operand.distance, item->operand.length);
|
2007-08-21 01:11:32 +00:00
|
|
|
item++;
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(query, 0);
|
|
|
|
|
|
|
|
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
tsqueryrecv(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
|
|
|
|
TSQuery query;
|
|
|
|
int i,
|
|
|
|
size,
|
2007-09-07 15:09:56 +00:00
|
|
|
len;
|
2007-08-21 01:11:32 +00:00
|
|
|
QueryItem *item;
|
|
|
|
int datalen = 0;
|
|
|
|
char *ptr;
|
2007-09-07 15:35:11 +00:00
|
|
|
Bitmapset *parentset = NULL;
|
2007-08-21 01:11:32 +00:00
|
|
|
|
|
|
|
size = pq_getmsgint(buf, sizeof(uint32));
|
|
|
|
if (size < 0 || size > (MaxAllocSize / sizeof(QueryItem)))
|
|
|
|
elog(ERROR, "invalid size of tsquery");
|
2007-09-07 15:09:56 +00:00
|
|
|
|
|
|
|
len = HDRSIZETQ + sizeof(QueryItem) * size;
|
2007-08-21 01:11:32 +00:00
|
|
|
|
2007-09-07 15:35:11 +00:00
|
|
|
query = (TSQuery) palloc0(len);
|
2007-08-21 01:11:32 +00:00
|
|
|
query->size = size;
|
|
|
|
item = GETQUERY(query);
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
item->type = (int8) pq_getmsgint(buf, sizeof(int8));
|
2007-09-07 15:09:56 +00:00
|
|
|
|
|
|
|
switch(item->type)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
case QI_VAL:
|
|
|
|
item->operand.weight = (int8) pq_getmsgint(buf, sizeof(int8));
|
|
|
|
item->operand.valcrc = (int32) pq_getmsgint(buf, sizeof(int32));
|
|
|
|
item->operand.length = pq_getmsgint(buf, sizeof(int16));
|
|
|
|
|
2007-09-07 15:35:11 +00:00
|
|
|
/* Check that the weight bitmap is valid */
|
|
|
|
if (item->operand.weight < 0 || item->operand.weight > 0xF)
|
|
|
|
elog(ERROR, "invalid weight bitmap");
|
|
|
|
|
|
|
|
/* XXX: We don't check that the CRC is valid. Actually, if we
|
|
|
|
* bothered to calculate it to verify, there would be no need
|
|
|
|
* to transfer it.
|
|
|
|
*/
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
/*
|
|
|
|
* Check that datalen doesn't grow too large. Without the
|
|
|
|
* check, a malicious client could induce a buffer overflow
|
|
|
|
* by sending a tsquery whose size exceeds 2GB. datalen
|
|
|
|
* would overflow, we would allocate a too small buffer below,
|
|
|
|
* and overflow the buffer. Because operand.length is a 20-bit
|
|
|
|
* field, adding one such value to datalen must exceed
|
|
|
|
* MaxAllocSize before wrapping over the 32-bit datalen field,
|
|
|
|
* so this check will protect from it.
|
|
|
|
*/
|
|
|
|
if (datalen > MAXSTRLEN)
|
|
|
|
elog(ERROR, "invalid tsquery; total operand length exceeded");
|
|
|
|
|
|
|
|
/* We can calculate distance from datalen, no need to send it
|
2007-09-07 15:35:11 +00:00
|
|
|
* across the wire. If we did, we would have to check that
|
2007-09-07 15:09:56 +00:00
|
|
|
* it's valid anyway.
|
|
|
|
*/
|
|
|
|
item->operand.distance = datalen;
|
|
|
|
|
|
|
|
datalen += item->operand.length + 1; /* \0 */
|
2007-08-21 01:11:32 +00:00
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
break;
|
|
|
|
case QI_OPR:
|
|
|
|
item->operator.oper = (int8) pq_getmsgint(buf, sizeof(int8));
|
|
|
|
if (item->operator.oper != OP_NOT &&
|
|
|
|
item->operator.oper != OP_OR &&
|
|
|
|
item->operator.oper != OP_AND)
|
|
|
|
elog(ERROR, "unknown operator type %d", (int) item->operator.oper);
|
2007-09-07 15:35:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that no previous operator node points to the right
|
|
|
|
* operand. That would mean that the operand node
|
|
|
|
* has two parents.
|
|
|
|
*/
|
|
|
|
if (bms_is_member(i + 1, parentset))
|
|
|
|
elog(ERROR, "malformed query tree");
|
|
|
|
|
|
|
|
parentset = bms_add_member(parentset, i + 1);
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
if(item->operator.oper != OP_NOT)
|
|
|
|
{
|
2007-09-07 15:35:11 +00:00
|
|
|
uint32 left = (uint32) pq_getmsgint(buf, sizeof(uint32));
|
|
|
|
|
2007-09-07 15:09:56 +00:00
|
|
|
/*
|
2007-09-07 15:35:11 +00:00
|
|
|
* Right operand is implicitly at "this+1". Don't allow
|
|
|
|
* left to point to the right operand, or to self.
|
2007-09-07 15:09:56 +00:00
|
|
|
*/
|
2007-09-07 15:35:11 +00:00
|
|
|
if (left <= 1 || i + left >= size)
|
2007-09-07 15:09:56 +00:00
|
|
|
elog(ERROR, "invalid pointer to left operand");
|
|
|
|
|
2007-09-07 15:35:11 +00:00
|
|
|
/*
|
|
|
|
* Check that no previous operator node points to the left
|
|
|
|
* operand.
|
2007-09-07 15:09:56 +00:00
|
|
|
*/
|
2007-09-07 15:35:11 +00:00
|
|
|
if (bms_is_member(i + left, parentset))
|
|
|
|
elog(ERROR, "malformed query tree");
|
|
|
|
|
|
|
|
parentset = bms_add_member(parentset, i + left);
|
|
|
|
|
|
|
|
item->operator.left = left;
|
2007-09-07 15:09:56 +00:00
|
|
|
}
|
2007-09-07 15:35:11 +00:00
|
|
|
else
|
|
|
|
item->operator.left = 1; /* do not leave uninitialized fields */
|
2007-09-07 15:09:56 +00:00
|
|
|
|
|
|
|
if (i == size - 1)
|
|
|
|
elog(ERROR, "invalid pointer to right operand");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "unknown tsquery node type %d", item->type);
|
2007-08-21 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
item++;
|
|
|
|
}
|
|
|
|
|
2007-09-07 15:35:11 +00:00
|
|
|
/* Now check that each node, except the root, has a parent. We
|
|
|
|
* already checked above that no node has more than one parent. */
|
|
|
|
if (bms_num_members(parentset) != size - 1 && size != 0)
|
|
|
|
elog(ERROR, "malformed query tree");
|
|
|
|
|
|
|
|
bms_free( parentset );
|
|
|
|
|
2007-08-21 01:11:32 +00:00
|
|
|
query = (TSQuery) repalloc(query, len + datalen);
|
|
|
|
|
|
|
|
item = GETQUERY(query);
|
|
|
|
ptr = GETOPERAND(query);
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
2007-09-07 15:09:56 +00:00
|
|
|
if (item->type == QI_VAL)
|
2007-08-21 01:11:32 +00:00
|
|
|
{
|
|
|
|
memcpy(ptr,
|
2007-09-07 15:09:56 +00:00
|
|
|
pq_getmsgbytes(buf, item->operand.length),
|
|
|
|
item->operand.length);
|
|
|
|
ptr += item->operand.length;
|
2007-08-21 01:11:32 +00:00
|
|
|
*ptr++ = '\0';
|
|
|
|
}
|
|
|
|
item++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert(ptr - GETOPERAND(query) == datalen);
|
|
|
|
|
|
|
|
SET_VARSIZE(query, len + datalen);
|
|
|
|
|
|
|
|
PG_RETURN_TSVECTOR(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* debug function, used only for view query
|
|
|
|
* which will be executed in non-leaf pages in index
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
tsquerytree(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(0);
|
|
|
|
INFIX nrm;
|
|
|
|
text *res;
|
|
|
|
QueryItem *q;
|
2007-09-07 15:09:56 +00:00
|
|
|
int len;
|
2007-08-21 01:11:32 +00:00
|
|
|
|
|
|
|
if (query->size == 0)
|
|
|
|
{
|
|
|
|
res = (text *) palloc(VARHDRSZ);
|
|
|
|
SET_VARSIZE(res, VARHDRSZ);
|
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
q = clean_NOT(GETQUERY(query), &len);
|
|
|
|
|
|
|
|
if (!q)
|
|
|
|
{
|
|
|
|
res = (text *) palloc(1 + VARHDRSZ);
|
|
|
|
SET_VARSIZE(res, 1 + VARHDRSZ);
|
|
|
|
*((char *) VARDATA(res)) = 'T';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nrm.curpol = q;
|
|
|
|
nrm.buflen = 32;
|
|
|
|
nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
|
|
|
|
*(nrm.cur) = '\0';
|
|
|
|
nrm.op = GETOPERAND(query);
|
|
|
|
infix(&nrm, true);
|
|
|
|
|
|
|
|
res = (text *) palloc(nrm.cur - nrm.buf + VARHDRSZ);
|
|
|
|
SET_VARSIZE(res, nrm.cur - nrm.buf + VARHDRSZ);
|
|
|
|
strncpy(VARDATA(res), nrm.buf, nrm.cur - nrm.buf);
|
|
|
|
pfree(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(query, 0);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
}
|