Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 56a8296

Browse files
committed
Fix parsing of ignored operators in websearch_to_tsquery().
The manual says clearly that punctuation in the input of websearch_to_tsquery() is ignored, except for the special cases of dashes and quotes. However, this failed for cases like "(foo bar) or something", or in general an ISOPERATOR character in front of the "or". We'd switch back to WAITOPERAND state, then ignore the operator character while remaining in that state, and then reach the "or" in WAITOPERAND state which (intentionally) makes us treat it as data. The fix is simple enough: if we see an ISOPERATOR character while in WAITOPERATOR state, we have to skip it while staying in that state. (We don't need to worry about other punctuation characters: those will be consumed as though they were words, but then rejected by lexizing.) In v14 and up (since commit eb08605) we can simplify the code a bit more too, because there is no longer a reason for the WAITOPERAND state to distinguish between quoted and unquoted operands. Per bug #18479 from Manos Emmanouilidis. Back-patch to all supported branches. Discussion: https://postgr.es/m/18479-d9b46e2fc242c33e@postgresql.org
1 parent d872e1b commit 56a8296

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/backend/utils/adt/tsquery.c

+9-13
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ gettoken_query_websearch(TSQueryParserState state, int8 *operator,
439439
}
440440
else if (ISOPERATOR(state->buf))
441441
{
442-
/* or else gettoken_tsvector() will raise an error */
442+
/* ignore, else gettoken_tsvector() will raise an error */
443443
state->buf++;
444444
state->state = WAITOPERAND;
445445
continue;
@@ -476,31 +476,27 @@ gettoken_query_websearch(TSQueryParserState state, int8 *operator,
476476
break;
477477

478478
case WAITOPERATOR:
479-
if (t_iseq(state->buf, '"'))
479+
if (*state->buf == '\0')
480480
{
481-
/*
482-
* put implicit AND after an operand and handle this quote
483-
* in WAITOPERAND
484-
*/
485-
state->state = WAITOPERAND;
486-
*operator = OP_AND;
487-
return PT_OPR;
481+
return PT_END;
488482
}
489483
else if (parse_or_operator(state))
490484
{
491485
state->state = WAITOPERAND;
492486
*operator = OP_OR;
493487
return PT_OPR;
494488
}
495-
else if (*state->buf == '\0')
489+
else if (ISOPERATOR(state->buf))
496490
{
497-
return PT_END;
491+
/* ignore other operators in this state too */
492+
state->buf++;
493+
continue;
498494
}
499495
else if (!t_isspace(state->buf))
500496
{
501-
/* put implicit AND after an operand */
502-
*operator = OP_AND;
497+
/* insert implicit AND between operands */
503498
state->state = WAITOPERAND;
499+
*operator = OP_AND;
504500
return PT_OPR;
505501
}
506502
break;

src/test/regress/expected/tsearch.out

+7
Original file line numberDiff line numberDiff line change
@@ -2676,12 +2676,19 @@ select websearch_to_tsquery('simple', 'abc <-> def');
26762676
'abc' & 'def'
26772677
(1 row)
26782678

2679+
-- parens are ignored, too
26792680
select websearch_to_tsquery('simple', 'abc (pg or class)');
26802681
websearch_to_tsquery
26812682
------------------------
26822683
'abc' & 'pg' | 'class'
26832684
(1 row)
26842685

2686+
select websearch_to_tsquery('simple', '(foo bar) or (ding dong)');
2687+
websearch_to_tsquery
2688+
---------------------------------
2689+
'foo' & 'bar' | 'ding' & 'dong'
2690+
(1 row)
2691+
26852692
-- NOT is ignored in quotes
26862693
select websearch_to_tsquery('english', 'My brand new smartphone');
26872694
websearch_to_tsquery

src/test/regress/sql/tsearch.sql

+3
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,10 @@ select websearch_to_tsquery('simple', ':');
818818
select websearch_to_tsquery('simple', 'abc & def');
819819
select websearch_to_tsquery('simple', 'abc | def');
820820
select websearch_to_tsquery('simple', 'abc <-> def');
821+
822+
-- parens are ignored, too
821823
select websearch_to_tsquery('simple', 'abc (pg or class)');
824+
select websearch_to_tsquery('simple', '(foo bar) or (ding dong)');
822825

823826
-- NOT is ignored in quotes
824827
select websearch_to_tsquery('english', 'My brand new smartphone');

0 commit comments

Comments
 (0)