Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Fix YA text phrase search bug.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 7 May 2020 19:59:52 +0000 (15:59 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 7 May 2020 19:59:52 +0000 (15:59 -0400)
checkcondition_str() failed to report multiple matches for a prefix
pattern correctly: it would dutifully merge the match positions, but
then after exiting that loop, if the last prefix-matching word had
had no suitable positions, it would report there were no matches.
The upshot would be failing to recognize a match that the query
should match.

It looks like you need all of these conditions to see the bug:
* a phrase search (else we don't ask for match position details)
* a prefix search item (else we don't get to this code)
* a weight restriction (else checkclass_str won't fail)

Noted while investigating a problem report from Pavel Borisov,
though this is distinct from the issue he was on about.

Back-patch to 9.6 where phrase search was added.

src/backend/utils/adt/tsvector_op.c
src/test/regress/expected/tstypes.out
src/test/regress/sql/tstypes.sql

index 3cd1dfbb6cf1935226a62f7b69c0531205a4931f..4e4119af2c53b9bacc96f2344626ecd965a16f97 100644 (file)
@@ -1327,12 +1327,13 @@ checkcondition_str(void *checkval, QueryOperand *val, ExecPhraseData *data)
    WordEntry  *StopLow = chkval->arrb;
    WordEntry  *StopHigh = chkval->arre;
    WordEntry  *StopMiddle = StopHigh;
-   int         difference = -1;
    bool        res = false;
 
    /* Loop invariant: StopLow <= val < StopHigh */
    while (StopLow < StopHigh)
    {
+       int         difference;
+
        StopMiddle = StopLow + (StopHigh - StopLow) / 2;
        difference = tsCompareString(chkval->operand + val->distance,
                                     val->length,
@@ -1398,6 +1399,11 @@ checkcondition_str(void *checkval, QueryOperand *val, ExecPhraseData *data)
                    memcpy(allpos + npos, data->pos, sizeof(WordEntryPos) * data->npos);
                    npos += data->npos;
                }
+               else
+               {
+                   /* at loop exit, res must be true if we found matches */
+                   res = (npos > 0);
+               }
            }
            else
            {
index 8ff0d0496e96394401236ae8adf732e4384c515c..fa829d61e49d9e4e66b84ce0bc600d3f80fffaae 100644 (file)
@@ -531,6 +531,24 @@ SELECT 'a b:89  ca:23A,64c cb:80b d:34c'::tsvector @@ 'd:AC & c:*B' as "true";
  t
 (1 row)
 
+SELECT 'wa:1D wb:2A'::tsvector @@ 'w:*D & w:*A'::tsquery as "true";
+ true 
+------
+ t
+(1 row)
+
+SELECT 'wa:1D wb:2A'::tsvector @@ 'w:*D <-> w:*A'::tsquery as "true";
+ true 
+------
+ t
+(1 row)
+
+SELECT 'wa:1A wb:2D'::tsvector @@ 'w:*D <-> w:*A'::tsquery as "false";
+ false 
+-------
+ f
+(1 row)
+
 SELECT 'supernova'::tsvector @@ 'super'::tsquery AS "false";
  false 
 -------
index 4c60f602e5d5d55330f3842ea3a943a79a09f515..55710f1c04065c969834c63a85d87d753d56a9af 100644 (file)
@@ -98,6 +98,9 @@ SELECT 'a b:89  ca:23A,64b d:34c'::tsvector @@ 'd:AC & c:*CB' as "true";
 SELECT 'a b:89  ca:23A,64b cb:80c d:34c'::tsvector @@ 'd:AC & c:*C' as "true";
 SELECT 'a b:89  ca:23A,64c cb:80b d:34c'::tsvector @@ 'd:AC & c:*C' as "true";
 SELECT 'a b:89  ca:23A,64c cb:80b d:34c'::tsvector @@ 'd:AC & c:*B' as "true";
+SELECT 'wa:1D wb:2A'::tsvector @@ 'w:*D & w:*A'::tsquery as "true";
+SELECT 'wa:1D wb:2A'::tsvector @@ 'w:*D <-> w:*A'::tsquery as "true";
+SELECT 'wa:1A wb:2D'::tsvector @@ 'w:*D <-> w:*A'::tsquery as "false";
 
 SELECT 'supernova'::tsvector @@ 'super'::tsquery AS "false";
 SELECT 'supeanova supernova'::tsvector @@ 'super'::tsquery AS "false";