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

Commit 1ec4c7c

Browse files
committed
Restore original tsquery operation numbering.
As noticed by Tom Lane changing operation's number in commit bb14050 causes on-disk format incompatibility. Revert to previous numbering, that is reason to add special array to store priorities of operation. Also it reverts order of tsquery to previous. Author: Dmitry Ivanov
1 parent 76a1c97 commit 1ec4c7c

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

src/backend/utils/adt/tsquery.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@
1616

1717
#include "libpq/pqformat.h"
1818
#include "miscadmin.h"
19+
#include "tsearch/ts_type.h"
1920
#include "tsearch/ts_locale.h"
2021
#include "tsearch/ts_utils.h"
2122
#include "utils/builtins.h"
2223
#include "utils/memutils.h"
2324
#include "utils/pg_crc.h"
2425

26+
/* FTS operator priorities, see ts_type.h */
27+
const int tsearch_op_priority[OP_COUNT] =
28+
{
29+
3, /* OP_NOT */
30+
2, /* OP_AND */
31+
1, /* OP_OR */
32+
4 /* OP_PHRASE */
33+
};
2534

2635
struct TSQueryParserStateData
2736
{
@@ -736,9 +745,6 @@ while( ( (inf)->cur - (inf)->buf ) + (addsize) + 1 >= (inf)->buflen ) \
736745
(inf)->cur = (inf)->buf + len; \
737746
}
738747

739-
#define PRINT_PRIORITY(x) \
740-
( (QO_PRIORITY(x) == OP_NOT) ? OP_NOT_PHRASE : QO_PRIORITY(x) )
741-
742748
/*
743749
* recursively traverse the tree and
744750
* print it in infix (human-readable) form

src/include/tsearch/ts_type.h

+13-4
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,29 @@ typedef struct
222222
* for query transformation! That's need to simplify
223223
* algorithm of query transformation.
224224
*/
225-
#define OP_OR 1
225+
#define OP_NOT 1
226226
#define OP_AND 2
227-
#define OP_NOT 3
227+
#define OP_OR 3
228228
#define OP_PHRASE 4
229-
#define OP_NOT_PHRASE 5 /*
229+
#define OP_COUNT 4
230+
231+
extern const int tsearch_op_priority[OP_COUNT];
232+
233+
#define NOT_PHRASE_P 5 /*
230234
* OP_PHRASE negation operations must have greater
231235
* priority in order to force infix() to surround
232236
* the whole OP_PHRASE expression with parentheses.
233237
*/
234238

235239
#define TOP_PRIORITY 6 /* highest priority for val nodes */
236240

237-
#define OP_PRIORITY(x) (x)
241+
/* get operation priority by its code*/
242+
#define OP_PRIORITY(x) ( tsearch_op_priority[(x) - 1] )
243+
/* get QueryOperator priority */
238244
#define QO_PRIORITY(x) OP_PRIORITY(((QueryOperator *) (x))->oper)
245+
/* special case: get QueryOperator priority for correct printing !(a <-> b>) */
246+
#define PRINT_PRIORITY(x) \
247+
( (((QueryOperator *) (x))->oper == OP_NOT) ? NOT_PHRASE_P : QO_PRIORITY(x) )
239248

240249
typedef struct
241250
{

src/test/regress/expected/tsearch.out

+7-7
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ RESET enable_seqscan;
11691169
SELECT ts_rewrite('foo & bar & qq & new & york', 'new & york'::tsquery, 'big & apple | nyc | new & york & city');
11701170
ts_rewrite
11711171
------------------------------------------------------------------------------
1172-
'foo' & 'bar' & 'qq' & ( 'nyc' | 'big' & 'apple' | 'city' & 'new' & 'york' )
1172+
'foo' & 'bar' & 'qq' & ( 'city' & 'new' & 'york' | 'nyc' | 'big' & 'apple' )
11731173
(1 row)
11741174

11751175
SELECT ts_rewrite('moscow', 'SELECT keyword, sample FROM test_tsquery'::text );
@@ -1187,7 +1187,7 @@ SELECT ts_rewrite('moscow & hotel', 'SELECT keyword, sample FROM test_tsquery'::
11871187
SELECT ts_rewrite('bar & new & qq & foo & york', 'SELECT keyword, sample FROM test_tsquery'::text );
11881188
ts_rewrite
11891189
---------------------------------------------------------------------------------
1190-
( 'nyc' | 'big' & 'appl' | 'new' & 'york' ) & 'citi' & 'foo' & ( 'bar' | 'qq' )
1190+
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | 'big' & 'appl' | 'new' & 'york' )
11911191
(1 row)
11921192

11931193
SELECT ts_rewrite( 'moscow', 'SELECT keyword, sample FROM test_tsquery');
@@ -1205,7 +1205,7 @@ SELECT ts_rewrite( 'moscow & hotel', 'SELECT keyword, sample FROM test_tsquery')
12051205
SELECT ts_rewrite( 'bar & new & qq & foo & york', 'SELECT keyword, sample FROM test_tsquery');
12061206
ts_rewrite
12071207
---------------------------------------------------------------------------------
1208-
( 'nyc' | 'big' & 'appl' | 'new' & 'york' ) & 'citi' & 'foo' & ( 'bar' | 'qq' )
1208+
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | 'big' & 'appl' | 'new' & 'york' )
12091209
(1 row)
12101210

12111211
SELECT ts_rewrite('1 & (2 <-> 3)', 'SELECT keyword, sample FROM test_tsquery'::text );
@@ -1270,7 +1270,7 @@ SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_t
12701270
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'bar & new & qq & foo & york') AS query;
12711271
ts_rewrite
12721272
---------------------------------------------------------------------------------
1273-
( 'nyc' | 'big' & 'appl' | 'new' & 'york' ) & 'citi' & 'foo' & ( 'bar' | 'qq' )
1273+
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | 'big' & 'appl' | 'new' & 'york' )
12741274
(1 row)
12751275

12761276
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow') AS query;
@@ -1288,7 +1288,7 @@ SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_t
12881288
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'bar & new & qq & foo & york') AS query;
12891289
ts_rewrite
12901290
---------------------------------------------------------------------------------
1291-
( 'nyc' | 'big' & 'appl' | 'new' & 'york' ) & 'citi' & 'foo' & ( 'bar' | 'qq' )
1291+
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | 'big' & 'appl' | 'new' & 'york' )
12921292
(1 row)
12931293

12941294
CREATE INDEX qq ON test_tsquery USING gist (keyword tsquery_ops);
@@ -1331,7 +1331,7 @@ SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_t
13311331
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'bar & new & qq & foo & york') AS query;
13321332
ts_rewrite
13331333
---------------------------------------------------------------------------------
1334-
( 'nyc' | 'big' & 'appl' | 'new' & 'york' ) & 'citi' & 'foo' & ( 'bar' | 'qq' )
1334+
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | 'big' & 'appl' | 'new' & 'york' )
13351335
(1 row)
13361336

13371337
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow') AS query;
@@ -1349,7 +1349,7 @@ SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_t
13491349
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'bar & new & qq & foo & york') AS query;
13501350
ts_rewrite
13511351
---------------------------------------------------------------------------------
1352-
( 'nyc' | 'big' & 'appl' | 'new' & 'york' ) & 'citi' & 'foo' & ( 'bar' | 'qq' )
1352+
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | 'big' & 'appl' | 'new' & 'york' )
13531353
(1 row)
13541354

13551355
RESET enable_seqscan;

src/test/regress/expected/tstypes.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ SELECT 'a' > 'b & c'::tsquery as "false";
473473
SELECT 'a | f' < 'b & c'::tsquery as "false";
474474
false
475475
-------
476-
f
476+
t
477477
(1 row)
478478

479479
SELECT 'a | ff' < 'b & c'::tsquery as "false";

0 commit comments

Comments
 (0)