Skip to content

Commit 12f25e7

Browse files
committed
Fix two-argument form of ts_rewrite() so it actually works for cases where
a later rewrite rule should change a subtree modified by an earlier one. Per my gripe of a few days ago.
1 parent bb36c51 commit 12f25e7

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/backend/utils/adt/tsquery_rewrite.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_rewrite.c,v 1.4 2007/09/07 16:03:40 teodor Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_rewrite.c,v 1.5 2007/10/23 01:44:39 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -467,6 +467,13 @@ tsquery_rewrite(PG_FUNCTION_ARGS)
467467
QTNFree(qsubs);
468468
if (qtsubs != (TSQuery) DatumGetPointer(sdata))
469469
pfree(qtsubs);
470+
471+
if (tree)
472+
{
473+
/* ready the tree for another pass */
474+
QTNClearFlags(tree, QTN_NOCHANGE);
475+
QTNSort(tree);
476+
}
470477
}
471478
}
472479

src/backend/utils/adt/tsquery_util.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_util.c,v 1.4 2007/09/07 16:03:40 teodor Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_util.c,v 1.5 2007/10/23 01:44:39 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -381,3 +381,20 @@ QTNCopy(QTNode *in)
381381

382382
return out;
383383
}
384+
385+
void
386+
QTNClearFlags(QTNode *in, uint32 flags)
387+
{
388+
/* since this function recurses, it could be driven to stack overflow. */
389+
check_stack_depth();
390+
391+
in->flags &= ~flags;
392+
393+
if (in->valnode->type != QI_VAL)
394+
{
395+
int i;
396+
397+
for (i = 0; i < in->nchild; i++)
398+
QTNClearFlags(in->child[i], flags);
399+
}
400+
}

src/include/tsearch/ts_utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 1998-2007, PostgreSQL Global Development Group
77
*
8-
* $PostgreSQL: pgsql/src/include/tsearch/ts_utils.h,v 1.6 2007/10/21 22:29:56 tgl Exp $
8+
* $PostgreSQL: pgsql/src/include/tsearch/ts_utils.h,v 1.7 2007/10/23 01:44:39 tgl Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -172,6 +172,7 @@ typedef struct QTNode
172172
struct QTNode **child;
173173
} QTNode;
174174

175+
/* bits in QTNode.flags */
175176
#define QTN_NEEDFREE 0x01
176177
#define QTN_NOCHANGE 0x02
177178
#define QTN_WORDFREE 0x04
@@ -189,6 +190,7 @@ extern void QTNTernary(QTNode * in);
189190
extern void QTNBinary(QTNode * in);
190191
extern int QTNodeCompare(QTNode * an, QTNode * bn);
191192
extern QTNode *QTNCopy(QTNode *in);
193+
extern void QTNClearFlags(QTNode *in, uint32 flags);
192194
extern bool QTNEq(QTNode * a, QTNode * b);
193195
extern TSQuerySign makeTSQuerySign(TSQuery a);
194196

src/test/regress/expected/tsearch.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ SELECT ts_rewrite('moscow', 'SELECT keyword, sample FROM test_tsquery'::text );
673673
SELECT ts_rewrite('moscow & hotel', 'SELECT keyword, sample FROM test_tsquery'::text );
674674
ts_rewrite
675675
-----------------------------------
676-
( 'moskva' | 'moscow' ) & 'hotel'
676+
'hotel' & ( 'moskva' | 'moscow' )
677677
(1 row)
678678

679679
SELECT ts_rewrite('bar & new & qq & foo & york', 'SELECT keyword, sample FROM test_tsquery'::text );

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy