Skip to content

Commit 26cf16a

Browse files
committed
Fix YA text phrase search bug.
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.
1 parent 4d21263 commit 26cf16a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/backend/utils/adt/tsvector_op.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,12 +1327,13 @@ checkcondition_str(void *checkval, QueryOperand *val, ExecPhraseData *data)
13271327
WordEntry *StopLow = chkval->arrb;
13281328
WordEntry *StopHigh = chkval->arre;
13291329
WordEntry *StopMiddle = StopHigh;
1330-
int difference = -1;
13311330
bool res = false;
13321331

13331332
/* Loop invariant: StopLow <= val < StopHigh */
13341333
while (StopLow < StopHigh)
13351334
{
1335+
int difference;
1336+
13361337
StopMiddle = StopLow + (StopHigh - StopLow) / 2;
13371338
difference = tsCompareString(chkval->operand + val->distance,
13381339
val->length,
@@ -1398,6 +1399,11 @@ checkcondition_str(void *checkval, QueryOperand *val, ExecPhraseData *data)
13981399
memcpy(allpos + npos, data->pos, sizeof(WordEntryPos) * data->npos);
13991400
npos += data->npos;
14001401
}
1402+
else
1403+
{
1404+
/* at loop exit, res must be true if we found matches */
1405+
res = (npos > 0);
1406+
}
14011407
}
14021408
else
14031409
{

src/test/regress/expected/tstypes.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,24 @@ SELECT 'a b:89 ca:23A,64c cb:80b d:34c'::tsvector @@ 'd:AC & c:*B' as "true";
531531
t
532532
(1 row)
533533

534+
SELECT 'wa:1D wb:2A'::tsvector @@ 'w:*D & w:*A'::tsquery as "true";
535+
true
536+
------
537+
t
538+
(1 row)
539+
540+
SELECT 'wa:1D wb:2A'::tsvector @@ 'w:*D <-> w:*A'::tsquery as "true";
541+
true
542+
------
543+
t
544+
(1 row)
545+
546+
SELECT 'wa:1A wb:2D'::tsvector @@ 'w:*D <-> w:*A'::tsquery as "false";
547+
false
548+
-------
549+
f
550+
(1 row)
551+
534552
SELECT 'supernova'::tsvector @@ 'super'::tsquery AS "false";
535553
false
536554
-------

src/test/regress/sql/tstypes.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ SELECT 'a b:89 ca:23A,64b d:34c'::tsvector @@ 'd:AC & c:*CB' as "true";
9898
SELECT 'a b:89 ca:23A,64b cb:80c d:34c'::tsvector @@ 'd:AC & c:*C' as "true";
9999
SELECT 'a b:89 ca:23A,64c cb:80b d:34c'::tsvector @@ 'd:AC & c:*C' as "true";
100100
SELECT 'a b:89 ca:23A,64c cb:80b d:34c'::tsvector @@ 'd:AC & c:*B' as "true";
101+
SELECT 'wa:1D wb:2A'::tsvector @@ 'w:*D & w:*A'::tsquery as "true";
102+
SELECT 'wa:1D wb:2A'::tsvector @@ 'w:*D <-> w:*A'::tsquery as "true";
103+
SELECT 'wa:1A wb:2D'::tsvector @@ 'w:*D <-> w:*A'::tsquery as "false";
101104

102105
SELECT 'supernova'::tsvector @@ 'super'::tsquery AS "false";
103106
SELECT 'supeanova supernova'::tsvector @@ 'super'::tsquery AS "false";

0 commit comments

Comments
 (0)