2007-08-21 03:11:32 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* tsginidx.c
|
|
|
|
* GIN support functions for tsvector_ops
|
|
|
|
*
|
2017-01-03 19:48:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2017, 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/tsginidx.c
|
2007-08-21 03:11:32 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2011-01-09 20:34:50 +01:00
|
|
|
#include "access/gin.h"
|
2015-05-15 22:03:16 +02:00
|
|
|
#include "access/stratnum.h"
|
2014-03-12 16:13:22 +01:00
|
|
|
#include "miscadmin.h"
|
2007-08-21 03:11:32 +02:00
|
|
|
#include "tsearch/ts_type.h"
|
|
|
|
#include "tsearch/ts_utils.h"
|
2008-03-25 23:42:46 +01:00
|
|
|
#include "utils/builtins.h"
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
|
2008-05-16 18:31:02 +02:00
|
|
|
Datum
|
|
|
|
gin_cmp_tslexeme(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
text *a = PG_GETARG_TEXT_PP(0);
|
|
|
|
text *b = PG_GETARG_TEXT_PP(1);
|
|
|
|
int cmp;
|
2008-05-16 18:31:02 +02:00
|
|
|
|
2011-01-09 20:34:50 +01:00
|
|
|
cmp = tsCompareString(VARDATA_ANY(a), VARSIZE_ANY_EXHDR(a),
|
2009-06-11 16:49:15 +02:00
|
|
|
VARDATA_ANY(b), VARSIZE_ANY_EXHDR(b),
|
|
|
|
false);
|
2008-05-16 18:31:02 +02:00
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_FREE_IF_COPY(b, 1);
|
|
|
|
PG_RETURN_INT32(cmp);
|
2008-05-16 18:31:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gin_cmp_prefix(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
text *a = PG_GETARG_TEXT_PP(0);
|
|
|
|
text *b = PG_GETARG_TEXT_PP(1);
|
|
|
|
|
2008-05-16 18:31:02 +02:00
|
|
|
#ifdef NOT_USED
|
|
|
|
StrategyNumber strategy = PG_GETARG_UINT16(2);
|
2009-06-11 16:49:15 +02:00
|
|
|
Pointer extra_data = PG_GETARG_POINTER(3);
|
2008-05-16 18:31:02 +02:00
|
|
|
#endif
|
2009-06-11 16:49:15 +02:00
|
|
|
int cmp;
|
2008-05-16 18:31:02 +02:00
|
|
|
|
2011-01-09 20:34:50 +01:00
|
|
|
cmp = tsCompareString(VARDATA_ANY(a), VARSIZE_ANY_EXHDR(a),
|
2009-06-11 16:49:15 +02:00
|
|
|
VARDATA_ANY(b), VARSIZE_ANY_EXHDR(b),
|
|
|
|
true);
|
2008-05-16 18:31:02 +02:00
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
if (cmp < 0)
|
|
|
|
cmp = 1; /* prevent continue scan */
|
2008-05-16 18:31:02 +02:00
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_FREE_IF_COPY(b, 1);
|
|
|
|
PG_RETURN_INT32(cmp);
|
2008-05-16 18:31:02 +02:00
|
|
|
}
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
Datum
|
|
|
|
gin_extract_tsvector(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TSVector vector = PG_GETARG_TSVECTOR(0);
|
2007-09-07 18:03:40 +02:00
|
|
|
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
|
2007-08-21 03:11:32 +02:00
|
|
|
Datum *entries = NULL;
|
|
|
|
|
2007-09-11 10:46:29 +02:00
|
|
|
*nentries = vector->size;
|
2007-08-21 03:11:32 +02:00
|
|
|
if (vector->size > 0)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
WordEntry *we = ARRPTR(vector);
|
|
|
|
|
|
|
|
entries = (Datum *) palloc(sizeof(Datum) * vector->size);
|
|
|
|
|
|
|
|
for (i = 0; i < vector->size; i++)
|
|
|
|
{
|
2008-03-25 23:42:46 +01:00
|
|
|
text *txt;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
txt = cstring_to_text_with_len(STRPTR(vector) + we->pos, we->len);
|
2007-08-21 03:11:32 +02:00
|
|
|
entries[i] = PointerGetDatum(txt);
|
|
|
|
|
|
|
|
we++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(vector, 0);
|
|
|
|
PG_RETURN_POINTER(entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2007-11-28 20:33:05 +01:00
|
|
|
gin_extract_tsquery(PG_FUNCTION_ARGS)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(0);
|
2007-09-07 18:03:40 +02:00
|
|
|
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
|
2011-04-10 17:42:00 +02:00
|
|
|
|
2008-04-14 19:05:34 +02:00
|
|
|
/* StrategyNumber strategy = PG_GETARG_UINT16(2); */
|
2009-06-11 16:49:15 +02:00
|
|
|
bool **ptr_partialmatch = (bool **) PG_GETARG_POINTER(3);
|
|
|
|
Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4);
|
2011-04-10 17:42:00 +02:00
|
|
|
|
|
|
|
/* bool **nullFlags = (bool **) PG_GETARG_POINTER(5); */
|
2011-01-09 20:34:50 +01:00
|
|
|
int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
|
2007-08-21 03:11:32 +02:00
|
|
|
Datum *entries = NULL;
|
|
|
|
|
|
|
|
*nentries = 0;
|
|
|
|
|
|
|
|
if (query->size > 0)
|
|
|
|
{
|
2011-01-09 20:34:50 +01:00
|
|
|
QueryItem *item = GETQUERY(query);
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 i,
|
2011-01-09 20:34:50 +01:00
|
|
|
j;
|
|
|
|
bool *partialmatch;
|
2009-03-25 23:19:02 +01:00
|
|
|
int *map_item_operand;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2011-01-09 20:34:50 +01:00
|
|
|
/*
|
|
|
|
* If the query doesn't have any required positive matches (for
|
2011-04-10 17:42:00 +02:00
|
|
|
* instance, it's something like '! foo'), we have to do a full index
|
|
|
|
* scan.
|
2011-01-09 20:34:50 +01:00
|
|
|
*/
|
|
|
|
if (tsquery_requires_match(item))
|
|
|
|
*searchMode = GIN_SEARCH_MODE_DEFAULT;
|
|
|
|
else
|
|
|
|
*searchMode = GIN_SEARCH_MODE_ALL;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2011-01-09 20:34:50 +01:00
|
|
|
/* count number of VAL items */
|
|
|
|
j = 0;
|
2007-08-21 03:11:32 +02:00
|
|
|
for (i = 0; i < query->size; i++)
|
2011-01-09 20:34:50 +01:00
|
|
|
{
|
2007-09-07 17:09:56 +02:00
|
|
|
if (item[i].type == QI_VAL)
|
2011-01-09 20:34:50 +01:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
*nentries = j;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2011-01-09 20:34:50 +01:00
|
|
|
entries = (Datum *) palloc(sizeof(Datum) * j);
|
|
|
|
partialmatch = *ptr_partialmatch = (bool *) palloc(sizeof(bool) * j);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2009-03-25 23:19:02 +01:00
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Make map to convert item's number to corresponding operand's (the
|
|
|
|
* same, entry's) number. Entry's number is used in check array in
|
|
|
|
* consistent method. We use the same map for each entry.
|
2009-03-25 23:19:02 +01:00
|
|
|
*/
|
2011-01-09 20:34:50 +01:00
|
|
|
*extra_data = (Pointer *) palloc(sizeof(Pointer) * j);
|
|
|
|
map_item_operand = (int *) palloc0(sizeof(int) * query->size);
|
2009-03-25 23:19:02 +01:00
|
|
|
|
2011-01-09 20:34:50 +01:00
|
|
|
/* Now rescan the VAL items and fill in the arrays */
|
|
|
|
j = 0;
|
2007-08-21 03:11:32 +02:00
|
|
|
for (i = 0; i < query->size; i++)
|
2011-01-09 20:34:50 +01:00
|
|
|
{
|
2007-09-07 17:09:56 +02:00
|
|
|
if (item[i].type == QI_VAL)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2009-07-16 08:33:46 +02:00
|
|
|
QueryOperand *val = &item[i].qoperand;
|
2011-01-09 20:34:50 +01:00
|
|
|
text *txt;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
txt = cstring_to_text_with_len(GETOPERAND(query) + val->distance,
|
|
|
|
val->length);
|
2011-01-09 20:34:50 +01:00
|
|
|
entries[j] = PointerGetDatum(txt);
|
|
|
|
partialmatch[j] = val->prefix;
|
2009-06-11 16:49:15 +02:00
|
|
|
(*extra_data)[j] = (Pointer) map_item_operand;
|
2009-03-25 23:19:02 +01:00
|
|
|
map_item_operand[i] = j;
|
2011-01-09 20:34:50 +01:00
|
|
|
j++;
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
2009-03-25 23:19:02 +01:00
|
|
|
}
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(query, 0);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2009-03-25 23:19:02 +01:00
|
|
|
QueryItem *first_item;
|
2014-03-31 09:26:38 +02:00
|
|
|
GinTernaryValue *check;
|
2009-03-25 23:19:02 +01:00
|
|
|
int *map_item_operand;
|
2008-04-14 19:05:34 +02:00
|
|
|
bool *need_recheck;
|
2007-11-15 23:25:18 +01:00
|
|
|
} GinChkVal;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2014-03-31 09:26:38 +02:00
|
|
|
static GinTernaryValue
|
2016-04-07 17:44:18 +02:00
|
|
|
checkcondition_gin_internal(GinChkVal *gcv, QueryOperand *val, ExecPhraseData *data)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2009-03-25 23:19:02 +01:00
|
|
|
int j;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2016-04-07 17:44:18 +02:00
|
|
|
/*
|
2016-06-10 00:02:36 +02:00
|
|
|
* if any val requiring a weight is used or caller needs position
|
|
|
|
* information then set recheck flag
|
2016-04-07 17:44:18 +02:00
|
|
|
*/
|
|
|
|
if (val->weight != 0 || data != NULL)
|
2016-12-16 17:50:07 +01:00
|
|
|
*(gcv->need_recheck) = true;
|
2008-04-14 19:05:34 +02:00
|
|
|
|
2009-03-25 23:19:02 +01:00
|
|
|
/* convert item's number to corresponding entry's (operand's) number */
|
2009-06-11 16:49:15 +02:00
|
|
|
j = gcv->map_item_operand[((QueryItem *) val) - gcv->first_item];
|
2009-03-25 23:19:02 +01:00
|
|
|
|
|
|
|
/* return presence of current entry in indexed value */
|
|
|
|
return gcv->check[j];
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
|
2016-04-07 17:44:18 +02:00
|
|
|
/*
|
|
|
|
* Wrapper of check condition function for TS_execute.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
checkcondition_gin(void *checkval, QueryOperand *val, ExecPhraseData *data)
|
|
|
|
{
|
|
|
|
return checkcondition_gin_internal((GinChkVal *) checkval,
|
|
|
|
val,
|
|
|
|
data) != GIN_FALSE;
|
|
|
|
}
|
|
|
|
|
2014-03-12 16:13:22 +01:00
|
|
|
/*
|
|
|
|
* Evaluate tsquery boolean expression using ternary logic.
|
|
|
|
*/
|
2014-03-31 09:26:38 +02:00
|
|
|
static GinTernaryValue
|
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
|
|
|
TS_execute_ternary(GinChkVal *gcv, QueryItem *curitem, bool in_phrase)
|
2014-03-12 16:13:22 +01:00
|
|
|
{
|
2014-05-06 18:12:18 +02:00
|
|
|
GinTernaryValue val1,
|
|
|
|
val2,
|
|
|
|
result;
|
|
|
|
|
2014-03-12 16:13:22 +01:00
|
|
|
/* since this function recurses, it could be driven to stack overflow */
|
|
|
|
check_stack_depth();
|
|
|
|
|
|
|
|
if (curitem->type == QI_VAL)
|
2016-05-03 16:52:25 +02:00
|
|
|
return
|
|
|
|
checkcondition_gin_internal(gcv,
|
|
|
|
(QueryOperand *) curitem,
|
|
|
|
NULL /* don't have position info */ );
|
2014-03-12 16:13:22 +01:00
|
|
|
|
|
|
|
switch (curitem->qoperator.oper)
|
|
|
|
{
|
|
|
|
case OP_NOT:
|
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
|
|
|
/* In phrase search, always return MAYBE since we lack positions */
|
|
|
|
if (in_phrase)
|
|
|
|
return GIN_MAYBE;
|
|
|
|
result = TS_execute_ternary(gcv, curitem + 1, in_phrase);
|
2014-03-12 16:13:22 +01:00
|
|
|
if (result == GIN_MAYBE)
|
|
|
|
return result;
|
|
|
|
return !result;
|
|
|
|
|
2016-04-07 17:44:18 +02:00
|
|
|
case OP_PHRASE:
|
2016-06-10 00:02:36 +02:00
|
|
|
|
2016-04-07 17:44:18 +02:00
|
|
|
/*
|
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
|
|
|
* GIN doesn't contain any information about positions, so treat
|
2016-06-10 00:02:36 +02:00
|
|
|
* OP_PHRASE as OP_AND with recheck requirement
|
2016-04-07 17:44:18 +02:00
|
|
|
*/
|
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
|
|
|
*(gcv->need_recheck) = true;
|
|
|
|
/* Pass down in_phrase == true in case there's a NOT below */
|
|
|
|
in_phrase = true;
|
|
|
|
|
2016-04-11 05:52:34 +02:00
|
|
|
/* FALL THRU */
|
2016-04-07 17:44:18 +02:00
|
|
|
|
2014-03-12 16:13:22 +01:00
|
|
|
case OP_AND:
|
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
|
|
|
val1 = TS_execute_ternary(gcv, curitem + curitem->qoperator.left,
|
|
|
|
in_phrase);
|
2014-03-12 16:13:22 +01:00
|
|
|
if (val1 == GIN_FALSE)
|
|
|
|
return GIN_FALSE;
|
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
|
|
|
val2 = TS_execute_ternary(gcv, curitem + 1, in_phrase);
|
2014-03-12 16:13:22 +01:00
|
|
|
if (val2 == GIN_FALSE)
|
|
|
|
return GIN_FALSE;
|
|
|
|
if (val1 == GIN_TRUE && val2 == GIN_TRUE)
|
|
|
|
return GIN_TRUE;
|
|
|
|
else
|
|
|
|
return GIN_MAYBE;
|
|
|
|
|
|
|
|
case OP_OR:
|
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
|
|
|
val1 = TS_execute_ternary(gcv, curitem + curitem->qoperator.left,
|
|
|
|
in_phrase);
|
2014-03-12 16:13:22 +01:00
|
|
|
if (val1 == GIN_TRUE)
|
|
|
|
return GIN_TRUE;
|
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
|
|
|
val2 = TS_execute_ternary(gcv, curitem + 1, in_phrase);
|
2014-03-12 16:13:22 +01:00
|
|
|
if (val2 == GIN_TRUE)
|
|
|
|
return GIN_TRUE;
|
|
|
|
if (val1 == GIN_FALSE && val2 == GIN_FALSE)
|
|
|
|
return GIN_FALSE;
|
|
|
|
else
|
|
|
|
return GIN_MAYBE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not reachable, but keep compiler quiet */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
Datum
|
2007-11-28 20:33:05 +01:00
|
|
|
gin_tsquery_consistent(PG_FUNCTION_ARGS)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
bool *check = (bool *) PG_GETARG_POINTER(0);
|
2009-06-11 16:49:15 +02:00
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
/* StrategyNumber strategy = PG_GETARG_UINT16(1); */
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(2);
|
2009-06-11 16:49:15 +02:00
|
|
|
|
2009-03-25 23:19:02 +01:00
|
|
|
/* int32 nkeys = PG_GETARG_INT32(3); */
|
2009-06-11 16:49:15 +02:00
|
|
|
Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
|
2009-03-25 23:19:02 +01:00
|
|
|
bool *recheck = (bool *) PG_GETARG_POINTER(5);
|
2017-08-16 06:22:32 +02:00
|
|
|
bool res = false;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2016-12-16 17:50:07 +01:00
|
|
|
/* Initially assume query doesn't require recheck */
|
2008-04-14 19:05:34 +02:00
|
|
|
*recheck = false;
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
if (query->size > 0)
|
|
|
|
{
|
|
|
|
GinChkVal gcv;
|
|
|
|
|
2007-09-11 10:46:29 +02:00
|
|
|
/*
|
|
|
|
* check-parameter array has one entry for each value (operand) in the
|
2009-03-25 23:19:02 +01:00
|
|
|
* query.
|
2007-09-11 10:46:29 +02:00
|
|
|
*/
|
2016-12-16 17:50:07 +01:00
|
|
|
gcv.first_item = GETQUERY(query);
|
2009-03-25 23:19:02 +01:00
|
|
|
gcv.check = check;
|
2009-06-11 16:49:15 +02:00
|
|
|
gcv.map_item_operand = (int *) (extra_data[0]);
|
2008-04-14 19:05:34 +02:00
|
|
|
gcv.need_recheck = recheck;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2011-01-09 20:34:50 +01:00
|
|
|
res = TS_execute(GETQUERY(query),
|
2007-08-21 03:11:32 +02:00
|
|
|
&gcv,
|
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
|
|
|
TS_EXEC_CALC_NOT | TS_EXEC_PHRASE_NO_POS,
|
2011-01-09 20:34:50 +01:00
|
|
|
checkcondition_gin);
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
2011-02-16 23:24:06 +01:00
|
|
|
|
2014-03-12 16:13:22 +01:00
|
|
|
Datum
|
|
|
|
gin_tsquery_triconsistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2014-03-31 09:26:38 +02:00
|
|
|
GinTernaryValue *check = (GinTernaryValue *) PG_GETARG_POINTER(0);
|
2014-03-12 16:13:22 +01:00
|
|
|
|
|
|
|
/* StrategyNumber strategy = PG_GETARG_UINT16(1); */
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(2);
|
|
|
|
|
|
|
|
/* int32 nkeys = PG_GETARG_INT32(3); */
|
|
|
|
Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
|
2014-03-31 09:26:38 +02:00
|
|
|
GinTernaryValue res = GIN_FALSE;
|
2014-03-12 16:13:22 +01:00
|
|
|
bool recheck;
|
|
|
|
|
2016-12-16 17:50:07 +01:00
|
|
|
/* Initially assume query doesn't require recheck */
|
2014-03-26 09:13:44 +01:00
|
|
|
recheck = false;
|
|
|
|
|
2014-03-12 16:13:22 +01:00
|
|
|
if (query->size > 0)
|
|
|
|
{
|
|
|
|
GinChkVal gcv;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check-parameter array has one entry for each value (operand) in the
|
|
|
|
* query.
|
|
|
|
*/
|
2016-12-16 17:50:07 +01:00
|
|
|
gcv.first_item = GETQUERY(query);
|
2014-03-12 16:13:22 +01:00
|
|
|
gcv.check = check;
|
|
|
|
gcv.map_item_operand = (int *) (extra_data[0]);
|
|
|
|
gcv.need_recheck = &recheck;
|
|
|
|
|
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
|
|
|
res = TS_execute_ternary(&gcv, GETQUERY(query), false);
|
2014-03-12 16:13:22 +01:00
|
|
|
|
|
|
|
if (res == GIN_TRUE && recheck)
|
|
|
|
res = GIN_MAYBE;
|
|
|
|
}
|
|
|
|
|
2014-03-31 09:26:38 +02:00
|
|
|
PG_RETURN_GIN_TERNARY_VALUE(res);
|
2014-03-12 16:13:22 +01:00
|
|
|
}
|
|
|
|
|
2011-02-16 23:24:06 +01:00
|
|
|
/*
|
|
|
|
* Formerly, gin_extract_tsvector had only two arguments. Now it has three,
|
|
|
|
* but we still need a pg_proc entry with two args to support reloading
|
|
|
|
* pre-9.1 contrib/tsearch2 opclass declarations. This compatibility
|
2014-05-06 18:12:18 +02:00
|
|
|
* function should go away eventually. (Note: you might say "hey, but the
|
2011-02-16 23:24:06 +01:00
|
|
|
* code above is only *using* two args, so let's just declare it that way".
|
|
|
|
* If you try that you'll find the opr_sanity regression test complains.)
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
gin_extract_tsvector_2args(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
if (PG_NARGS() < 3) /* should not happen */
|
|
|
|
elog(ERROR, "gin_extract_tsvector requires three arguments");
|
|
|
|
return gin_extract_tsvector(fcinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Likewise, we need a stub version of gin_extract_tsquery declared with
|
|
|
|
* only five arguments.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
gin_extract_tsquery_5args(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
if (PG_NARGS() < 7) /* should not happen */
|
|
|
|
elog(ERROR, "gin_extract_tsquery requires seven arguments");
|
|
|
|
return gin_extract_tsquery(fcinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Likewise, we need a stub version of gin_tsquery_consistent declared with
|
|
|
|
* only six arguments.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
gin_tsquery_consistent_6args(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
if (PG_NARGS() < 8) /* should not happen */
|
|
|
|
elog(ERROR, "gin_tsquery_consistent requires eight arguments");
|
|
|
|
return gin_tsquery_consistent(fcinfo);
|
|
|
|
}
|
2016-03-02 23:37:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Likewise, a stub version of gin_extract_tsquery declared with argument
|
|
|
|
* types that are no longer considered appropriate.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
gin_extract_tsquery_oldsig(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return gin_extract_tsquery(fcinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Likewise, a stub version of gin_tsquery_consistent declared with argument
|
|
|
|
* types that are no longer considered appropriate.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
gin_tsquery_consistent_oldsig(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return gin_tsquery_consistent(fcinfo);
|
|
|
|
}
|