2007-08-21 03:11:32 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* tsquery_cleanup.c
|
|
|
|
* Cleanup query from NOT values and/or stopword
|
|
|
|
* Utility functions to correct work.
|
|
|
|
*
|
2022-01-08 01:04:57 +01:00
|
|
|
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
|
2007-08-21 03:11:32 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/adt/tsquery_cleanup.c
|
2007-08-21 03:11:32 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2007-09-07 18:03:40 +02:00
|
|
|
#include "miscadmin.h"
|
2019-11-12 04:00:16 +01:00
|
|
|
#include "tsearch/ts_utils.h"
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
typedef struct NODE
|
|
|
|
{
|
|
|
|
struct NODE *left;
|
|
|
|
struct NODE *right;
|
|
|
|
QueryItem *valnode;
|
|
|
|
} NODE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make query tree from plain view of query
|
|
|
|
*/
|
|
|
|
static NODE *
|
|
|
|
maketree(QueryItem *in)
|
|
|
|
{
|
|
|
|
NODE *node = (NODE *) palloc(sizeof(NODE));
|
|
|
|
|
2015-10-05 16:06:30 +02:00
|
|
|
/* since this function recurses, it could be driven to stack overflow. */
|
|
|
|
check_stack_depth();
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
node->valnode = in;
|
|
|
|
node->right = node->left = NULL;
|
2007-09-07 17:09:56 +02:00
|
|
|
if (in->type == QI_OPR)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
node->right = maketree(in + 1);
|
2009-07-16 08:33:46 +02:00
|
|
|
if (in->qoperator.oper != OP_NOT)
|
|
|
|
node->left = maketree(in + in->qoperator.left);
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2007-09-07 17:09:56 +02:00
|
|
|
/*
|
|
|
|
* Internal state for plaintree and plainnode
|
|
|
|
*/
|
2007-08-21 03:11:32 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
QueryItem *ptr;
|
2007-09-07 17:09:56 +02:00
|
|
|
int len; /* allocated size of ptr */
|
|
|
|
int cur; /* number of elements in ptr */
|
2007-08-21 03:11:32 +02:00
|
|
|
} PLAINTREE;
|
|
|
|
|
|
|
|
static void
|
|
|
|
plainnode(PLAINTREE *state, NODE *node)
|
|
|
|
{
|
2007-09-07 17:35:11 +02:00
|
|
|
/* since this function recurses, it could be driven to stack overflow. */
|
|
|
|
check_stack_depth();
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
if (state->cur == state->len)
|
|
|
|
{
|
|
|
|
state->len *= 2;
|
|
|
|
state->ptr = (QueryItem *) repalloc((void *) state->ptr, state->len * sizeof(QueryItem));
|
|
|
|
}
|
|
|
|
memcpy((void *) &(state->ptr[state->cur]), (void *) node->valnode, sizeof(QueryItem));
|
2007-09-07 17:09:56 +02:00
|
|
|
if (node->valnode->type == QI_VAL)
|
2007-08-21 03:11:32 +02:00
|
|
|
state->cur++;
|
2009-07-16 08:33:46 +02:00
|
|
|
else if (node->valnode->qoperator.oper == OP_NOT)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2009-07-16 08:33:46 +02:00
|
|
|
state->ptr[state->cur].qoperator.left = 1;
|
2007-08-21 03:11:32 +02:00
|
|
|
state->cur++;
|
|
|
|
plainnode(state, node->right);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-09-07 17:09:56 +02:00
|
|
|
int cur = state->cur;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
state->cur++;
|
|
|
|
plainnode(state, node->right);
|
2009-07-16 08:33:46 +02:00
|
|
|
state->ptr[cur].qoperator.left = state->cur - cur;
|
2007-08-21 03:11:32 +02:00
|
|
|
plainnode(state, node->left);
|
|
|
|
}
|
|
|
|
pfree(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-09-07 17:09:56 +02:00
|
|
|
* make plain view of tree from a NODE-tree representation
|
2007-08-21 03:11:32 +02:00
|
|
|
*/
|
|
|
|
static QueryItem *
|
2007-09-07 17:09:56 +02:00
|
|
|
plaintree(NODE *root, int *len)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
PLAINTREE pl;
|
|
|
|
|
|
|
|
pl.cur = 0;
|
|
|
|
pl.len = 16;
|
2007-09-07 17:09:56 +02:00
|
|
|
if (root && (root->valnode->type == QI_VAL || root->valnode->type == QI_OPR))
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
pl.ptr = (QueryItem *) palloc(pl.len * sizeof(QueryItem));
|
|
|
|
plainnode(&pl, root);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pl.ptr = NULL;
|
|
|
|
*len = pl.cur;
|
|
|
|
return pl.ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
freetree(NODE *node)
|
|
|
|
{
|
2007-09-07 17:35:11 +02:00
|
|
|
/* since this function recurses, it could be driven to stack overflow. */
|
|
|
|
check_stack_depth();
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
if (node->left)
|
|
|
|
freetree(node->left);
|
|
|
|
if (node->right)
|
|
|
|
freetree(node->right);
|
|
|
|
pfree(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clean tree for ! operator.
|
2012-04-24 04:43:09 +02:00
|
|
|
* It's useful for debug, but in
|
2007-08-21 03:11:32 +02:00
|
|
|
* other case, such view is used with search in index.
|
|
|
|
* Operator ! always return TRUE
|
|
|
|
*/
|
|
|
|
static NODE *
|
|
|
|
clean_NOT_intree(NODE *node)
|
|
|
|
{
|
2007-09-07 17:35:11 +02:00
|
|
|
/* since this function recurses, it could be driven to stack overflow. */
|
|
|
|
check_stack_depth();
|
|
|
|
|
2007-09-07 17:09:56 +02:00
|
|
|
if (node->valnode->type == QI_VAL)
|
2007-08-21 03:11:32 +02:00
|
|
|
return node;
|
|
|
|
|
2009-07-16 08:33:46 +02:00
|
|
|
if (node->valnode->qoperator.oper == OP_NOT)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
freetree(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* operator & or | */
|
2009-07-16 08:33:46 +02:00
|
|
|
if (node->valnode->qoperator.oper == OP_OR)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
if ((node->left = clean_NOT_intree(node->left)) == NULL ||
|
|
|
|
(node->right = clean_NOT_intree(node->right)) == NULL)
|
|
|
|
{
|
|
|
|
freetree(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NODE *res = node;
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2016-04-07 17:44:18 +02:00
|
|
|
Assert(node->valnode->qoperator.oper == OP_AND ||
|
|
|
|
node->valnode->qoperator.oper == OP_PHRASE);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
node->left = clean_NOT_intree(node->left);
|
|
|
|
node->right = clean_NOT_intree(node->right);
|
|
|
|
if (node->left == NULL && node->right == NULL)
|
|
|
|
{
|
|
|
|
pfree(node);
|
|
|
|
res = NULL;
|
|
|
|
}
|
|
|
|
else if (node->left == NULL)
|
|
|
|
{
|
|
|
|
res = node->right;
|
|
|
|
pfree(node);
|
|
|
|
}
|
|
|
|
else if (node->right == NULL)
|
|
|
|
{
|
|
|
|
res = node->left;
|
|
|
|
pfree(node);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryItem *
|
2007-09-07 17:09:56 +02:00
|
|
|
clean_NOT(QueryItem *ptr, int *len)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
NODE *root = maketree(ptr);
|
|
|
|
|
|
|
|
return plaintree(clean_NOT_intree(root), len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2016-12-19 19:49:45 +01:00
|
|
|
* Remove QI_VALSTOP (stopword) nodes from query tree.
|
|
|
|
*
|
|
|
|
* Returns NULL if the query degenerates to nothing. Input must not be NULL.
|
|
|
|
*
|
|
|
|
* When we remove a phrase operator due to removing one or both of its
|
|
|
|
* arguments, we might need to adjust the distance of a parent phrase
|
|
|
|
* operator. For example, 'a' is a stopword, so:
|
|
|
|
* (b <-> a) <-> c should become b <2> c
|
|
|
|
* b <-> (a <-> c) should become b <2> c
|
|
|
|
* (b <-> (a <-> a)) <-> c should become b <3> c
|
|
|
|
* b <-> ((a <-> a) <-> c) should become b <3> c
|
|
|
|
* To handle that, we define two output parameters:
|
|
|
|
* ladd: amount to add to a phrase distance to the left of this node
|
|
|
|
* radd: amount to add to a phrase distance to the right of this node
|
|
|
|
* We need two outputs because we could need to bubble up adjustments to two
|
|
|
|
* different parent phrase operators. Consider
|
|
|
|
* w <-> (((a <-> x) <2> (y <3> a)) <-> z)
|
|
|
|
* After we've removed the two a's and are considering the <2> node (which is
|
|
|
|
* now just x <2> y), we have an ladd distance of 1 that needs to propagate
|
|
|
|
* up to the topmost (leftmost) <->, and an radd distance of 3 that needs to
|
|
|
|
* propagate to the rightmost <->, so that we'll end up with
|
|
|
|
* w <2> ((x <2> y) <4> z)
|
|
|
|
* Near the bottom of the tree, we may have subtrees consisting only of
|
|
|
|
* stopwords. The distances of any phrase operators within such a subtree are
|
|
|
|
* summed and propagated to both ladd and radd, since we don't know which side
|
|
|
|
* of the lowest surviving phrase operator we are in. The rule is that any
|
|
|
|
* subtree that degenerates to NULL must return equal values of ladd and radd,
|
|
|
|
* and the parent node dealing with it should incorporate only one of those.
|
|
|
|
*
|
|
|
|
* Currently, we only implement this adjustment for adjacent phrase operators.
|
|
|
|
* Thus for example 'x <-> ((a <-> y) | z)' will become 'x <-> (y | z)', which
|
|
|
|
* isn't ideal, but there is no way to represent the really desired semantics
|
|
|
|
* without some redesign of the tsquery structure. Certainly it would not be
|
|
|
|
* any better to convert that to 'x <2> (y | z)'. Since this is such a weird
|
|
|
|
* corner case, let it go for now. But we can fix it in cases where the
|
|
|
|
* intervening non-phrase operator also gets removed, for example
|
|
|
|
* '((x <-> a) | a) <-> y' will become 'x <2> y'.
|
2007-08-21 03:11:32 +02:00
|
|
|
*/
|
|
|
|
static NODE *
|
2016-12-19 19:49:45 +01:00
|
|
|
clean_stopword_intree(NODE *node, int *ladd, int *radd)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2007-09-07 17:35:11 +02:00
|
|
|
/* since this function recurses, it could be driven to stack overflow. */
|
|
|
|
check_stack_depth();
|
|
|
|
|
2016-12-19 19:49:45 +01:00
|
|
|
/* default output parameters indicate no change in parent distance */
|
|
|
|
*ladd = *radd = 0;
|
2016-04-07 17:44:18 +02:00
|
|
|
|
2007-09-07 17:09:56 +02:00
|
|
|
if (node->valnode->type == QI_VAL)
|
2007-08-21 03:11:32 +02:00
|
|
|
return node;
|
2007-09-07 17:09:56 +02:00
|
|
|
else if (node->valnode->type == QI_VALSTOP)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
pfree(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-07 17:09:56 +02:00
|
|
|
Assert(node->valnode->type == QI_OPR);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2009-07-16 08:33:46 +02:00
|
|
|
if (node->valnode->qoperator.oper == OP_NOT)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2016-12-19 19:49:45 +01:00
|
|
|
/* NOT doesn't change pattern width, so just report child distances */
|
|
|
|
node->right = clean_stopword_intree(node->right, ladd, radd);
|
2007-08-21 03:11:32 +02:00
|
|
|
if (!node->right)
|
|
|
|
{
|
|
|
|
freetree(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-07 17:44:18 +02:00
|
|
|
NODE *res = node;
|
2016-12-19 19:49:45 +01:00
|
|
|
bool isphrase;
|
2016-04-07 17:44:18 +02:00
|
|
|
int ndistance,
|
2016-12-19 19:49:45 +01:00
|
|
|
lladd,
|
|
|
|
lradd,
|
|
|
|
rladd,
|
|
|
|
rradd;
|
2016-04-07 17:44:18 +02:00
|
|
|
|
2016-12-19 19:49:45 +01:00
|
|
|
/* First, recurse */
|
|
|
|
node->left = clean_stopword_intree(node->left, &lladd, &lradd);
|
|
|
|
node->right = clean_stopword_intree(node->right, &rladd, &rradd);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2016-12-19 19:49:45 +01:00
|
|
|
/* Check if current node is OP_PHRASE, get its distance */
|
|
|
|
isphrase = (node->valnode->qoperator.oper == OP_PHRASE);
|
|
|
|
ndistance = isphrase ? node->valnode->qoperator.distance : 0;
|
2016-04-07 17:44:18 +02:00
|
|
|
|
2016-12-19 19:49:45 +01:00
|
|
|
if (node->left == NULL && node->right == NULL)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2016-12-19 19:49:45 +01:00
|
|
|
/*
|
|
|
|
* When we collapse out a phrase node entirely, propagate its own
|
|
|
|
* distance into both *ladd and *radd; it is the responsibility of
|
|
|
|
* the parent node to count it only once. Also, for a phrase
|
|
|
|
* node, distances coming from children are summed and propagated
|
|
|
|
* up to parent (we assume lladd == lradd and rladd == rradd, else
|
|
|
|
* rule was broken at a lower level). But if this isn't a phrase
|
|
|
|
* node, take the larger of the two child distances; that
|
|
|
|
* corresponds to what TS_execute will do in non-stopword cases.
|
|
|
|
*/
|
|
|
|
if (isphrase)
|
|
|
|
*ladd = *radd = lladd + ndistance + rladd;
|
|
|
|
else
|
|
|
|
*ladd = *radd = Max(lladd, rladd);
|
2007-08-21 03:11:32 +02:00
|
|
|
freetree(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-12-19 19:49:45 +01:00
|
|
|
else if (node->left == NULL)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2016-12-19 19:49:45 +01:00
|
|
|
/* Removing this operator and left subnode */
|
|
|
|
/* lladd and lradd are equal/redundant, don't count both */
|
|
|
|
if (isphrase)
|
|
|
|
{
|
|
|
|
/* operator's own distance must propagate to left */
|
|
|
|
*ladd = lladd + ndistance + rladd;
|
|
|
|
*radd = rradd;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* at non-phrase op, just forget the left subnode entirely */
|
|
|
|
*ladd = rladd;
|
|
|
|
*radd = rradd;
|
|
|
|
}
|
2007-08-21 03:11:32 +02:00
|
|
|
res = node->right;
|
|
|
|
pfree(node);
|
|
|
|
}
|
2016-12-19 19:49:45 +01:00
|
|
|
else if (node->right == NULL)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2016-12-19 19:49:45 +01:00
|
|
|
/* Removing this operator and right subnode */
|
|
|
|
/* rladd and rradd are equal/redundant, don't count both */
|
|
|
|
if (isphrase)
|
|
|
|
{
|
|
|
|
/* operator's own distance must propagate to right */
|
|
|
|
*ladd = lladd;
|
|
|
|
*radd = lradd + ndistance + rradd;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* at non-phrase op, just forget the right subnode entirely */
|
|
|
|
*ladd = lladd;
|
|
|
|
*radd = lradd;
|
|
|
|
}
|
2007-08-21 03:11:32 +02:00
|
|
|
res = node->left;
|
|
|
|
pfree(node);
|
|
|
|
}
|
2016-12-19 19:49:45 +01:00
|
|
|
else if (isphrase)
|
2016-04-07 17:44:18 +02:00
|
|
|
{
|
2016-12-19 19:49:45 +01:00
|
|
|
/* Absorb appropriate corrections at this level */
|
|
|
|
node->valnode->qoperator.distance += lradd + rladd;
|
|
|
|
/* Propagate up any unaccounted-for corrections */
|
|
|
|
*ladd = lladd;
|
|
|
|
*radd = rradd;
|
2016-04-07 17:44:18 +02:00
|
|
|
}
|
2016-12-19 19:49:45 +01:00
|
|
|
else
|
2016-04-07 17:44:18 +02:00
|
|
|
{
|
2016-12-19 19:49:45 +01:00
|
|
|
/* We're keeping a non-phrase operator, so ladd/radd remain 0 */
|
2016-04-07 17:44:18 +02:00
|
|
|
}
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2016-04-07 17:44:18 +02:00
|
|
|
/*
|
|
|
|
* Number of elements in query tree
|
|
|
|
*/
|
|
|
|
static int32
|
|
|
|
calcstrlen(NODE *node)
|
|
|
|
{
|
|
|
|
int32 size = 0;
|
|
|
|
|
|
|
|
if (node->valnode->type == QI_VAL)
|
|
|
|
{
|
|
|
|
size = node->valnode->qoperand.length + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Assert(node->valnode->type == QI_OPR);
|
|
|
|
|
|
|
|
size = calcstrlen(node->right);
|
|
|
|
if (node->valnode->qoperator.oper != OP_NOT)
|
|
|
|
size += calcstrlen(node->left);
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
Fix strange behavior (and possible crashes) in full text phrase search.
In an attempt to simplify the tsquery matching engine, the original
phrase search patch invented rewrite rules that would rearrange a
tsquery so that no AND/OR/NOT operator appeared below a PHRASE operator.
But this approach had numerous problems. The rearrangement step was
missed by ts_rewrite (and perhaps other places), allowing tsqueries
to be created that would cause Assert failures or perhaps crashes at
execution, as reported by Andreas Seltenreich. The rewrite rules
effectively defined semantics for operators underneath PHRASE that were
buggy, or at least unintuitive. And because rewriting was done in
tsqueryin() rather than at execution, the rearrangement was user-visible,
which is not very desirable --- for example, it might cause unexpected
matches or failures to match in ts_rewrite.
As a somewhat independent problem, the behavior of nested PHRASE operators
was only sane for left-deep trees; queries like "x <-> (y <-> z)" did not
behave intuitively at all.
To fix, get rid of the rewrite logic altogether, and instead teach the
tsquery execution engine to manage AND/OR/NOT below a PHRASE operator
by explicitly computing the match location(s) and match widths for these
operators.
This requires introducing some additional fields into the publicly visible
ExecPhraseData struct; but since there's no way for third-party code to
pass such a struct to TS_phrase_execute, it shouldn't create an ABI problem
as long as we don't move the offsets of the existing fields.
Another related problem was that index searches supposed that "!x <-> y"
could be lossily approximated as "!x & y", which isn't correct because
the latter will reject, say, "x q y" which the query itself accepts.
This required some tweaking in TS_execute_ternary along with the main
tsquery engine.
Back-patch to 9.6 where phrase operators were introduced. While this
could be argued to change behavior more than we'd like in a stable branch,
we have to do something about the crash hazards and index-vs-seqscan
inconsistency, and it doesn't seem desirable to let the unintuitive
behaviors induced by the rewriting implementation stand as precedent.
Discussion: https://postgr.es/m/28215.1481999808@sss.pgh.pa.us
Discussion: https://postgr.es/m/26706.1482087250@sss.pgh.pa.us
2016-12-21 21:18:25 +01:00
|
|
|
/*
|
|
|
|
* Remove QI_VALSTOP (stopword) nodes from TSQuery.
|
|
|
|
*/
|
2016-04-07 17:44:18 +02:00
|
|
|
TSQuery
|
Fix strange behavior (and possible crashes) in full text phrase search.
In an attempt to simplify the tsquery matching engine, the original
phrase search patch invented rewrite rules that would rearrange a
tsquery so that no AND/OR/NOT operator appeared below a PHRASE operator.
But this approach had numerous problems. The rearrangement step was
missed by ts_rewrite (and perhaps other places), allowing tsqueries
to be created that would cause Assert failures or perhaps crashes at
execution, as reported by Andreas Seltenreich. The rewrite rules
effectively defined semantics for operators underneath PHRASE that were
buggy, or at least unintuitive. And because rewriting was done in
tsqueryin() rather than at execution, the rearrangement was user-visible,
which is not very desirable --- for example, it might cause unexpected
matches or failures to match in ts_rewrite.
As a somewhat independent problem, the behavior of nested PHRASE operators
was only sane for left-deep trees; queries like "x <-> (y <-> z)" did not
behave intuitively at all.
To fix, get rid of the rewrite logic altogether, and instead teach the
tsquery execution engine to manage AND/OR/NOT below a PHRASE operator
by explicitly computing the match location(s) and match widths for these
operators.
This requires introducing some additional fields into the publicly visible
ExecPhraseData struct; but since there's no way for third-party code to
pass such a struct to TS_phrase_execute, it shouldn't create an ABI problem
as long as we don't move the offsets of the existing fields.
Another related problem was that index searches supposed that "!x <-> y"
could be lossily approximated as "!x & y", which isn't correct because
the latter will reject, say, "x q y" which the query itself accepts.
This required some tweaking in TS_execute_ternary along with the main
tsquery engine.
Back-patch to 9.6 where phrase operators were introduced. While this
could be argued to change behavior more than we'd like in a stable branch,
we have to do something about the crash hazards and index-vs-seqscan
inconsistency, and it doesn't seem desirable to let the unintuitive
behaviors induced by the rewriting implementation stand as precedent.
Discussion: https://postgr.es/m/28215.1481999808@sss.pgh.pa.us
Discussion: https://postgr.es/m/26706.1482087250@sss.pgh.pa.us
2016-12-21 21:18:25 +01:00
|
|
|
cleanup_tsquery_stopwords(TSQuery in)
|
2016-04-07 17:44:18 +02:00
|
|
|
{
|
|
|
|
int32 len,
|
|
|
|
lenstr,
|
|
|
|
commonlen,
|
|
|
|
i;
|
|
|
|
NODE *root;
|
2016-12-19 19:49:45 +01:00
|
|
|
int ladd,
|
|
|
|
radd;
|
2016-04-07 17:44:18 +02:00
|
|
|
TSQuery out;
|
|
|
|
QueryItem *items;
|
|
|
|
char *operands;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2016-04-07 17:44:18 +02:00
|
|
|
if (in->size == 0)
|
|
|
|
return in;
|
|
|
|
|
|
|
|
/* eliminate stop words */
|
2016-12-19 19:49:45 +01:00
|
|
|
root = clean_stopword_intree(maketree(GETQUERY(in)), &ladd, &radd);
|
|
|
|
if (root == NULL)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2007-11-28 22:56:30 +01:00
|
|
|
ereport(NOTICE,
|
2007-12-27 14:02:48 +01:00
|
|
|
(errmsg("text-search query contains only stop words or doesn't contain lexemes, ignored")));
|
2016-04-07 17:44:18 +02:00
|
|
|
out = palloc(HDRSIZETQ);
|
|
|
|
out->size = 0;
|
|
|
|
SET_VARSIZE(out, HDRSIZETQ);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build TSQuery from plain view
|
|
|
|
*/
|
|
|
|
|
|
|
|
lenstr = calcstrlen(root);
|
|
|
|
items = plaintree(root, &len);
|
|
|
|
commonlen = COMPUTESIZE(len, lenstr);
|
|
|
|
|
|
|
|
out = palloc(commonlen);
|
|
|
|
SET_VARSIZE(out, commonlen);
|
|
|
|
out->size = len;
|
|
|
|
|
|
|
|
memcpy(GETQUERY(out), items, len * sizeof(QueryItem));
|
|
|
|
|
|
|
|
items = GETQUERY(out);
|
|
|
|
operands = GETOPERAND(out);
|
|
|
|
for (i = 0; i < out->size; i++)
|
|
|
|
{
|
|
|
|
QueryOperand *op = (QueryOperand *) &items[i];
|
|
|
|
|
|
|
|
if (op->type != QI_VAL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
memcpy(operands, GETOPERAND(in) + op->distance, op->length);
|
|
|
|
operands[op->length] = '\0';
|
|
|
|
op->distance = operands - GETOPERAND(out);
|
|
|
|
operands += op->length + 1;
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
|
2016-04-07 17:44:18 +02:00
|
|
|
return out;
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|