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

Commit 592c88a

Browse files
committed
Remove the aggregate form of ts_rewrite(), since it doesn't work as desired
if there are zero rows to aggregate over, and the API seems both conceptually and notationally ugly anyway. We should look for something that improves on the tsquery-and-text-SELECT version (which is also pretty ugly but at least it works...), but it seems that will take query infrastructure that doesn't exist today. (Hm, I wonder if there's anything in or near SQL2003 window functions that would help?) Per discussion.
1 parent 07d0a37 commit 592c88a

File tree

8 files changed

+48
-214
lines changed

8 files changed

+48
-214
lines changed

doc/src/sgml/textsearch.sgml

+1-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/textsearch.sgml,v 1.24 2007/10/23 20:46:12 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/textsearch.sgml,v 1.25 2007/10/24 02:24:47 tgl Exp $ -->
22

33
<chapter id="textsearch">
44
<title id="textsearch-title">Full Text Search</title>
@@ -1456,33 +1456,6 @@ SELECT ts_rewrite('a &amp; b'::tsquery, 'a'::tsquery, 'c'::tsquery);
14561456
</listitem>
14571457
</varlistentry>
14581458

1459-
<varlistentry>
1460-
1461-
<term>
1462-
<synopsis>
1463-
ts_rewrite(ARRAY[<replaceable class="PARAMETER">query</replaceable> <type>tsquery</>, <replaceable class="PARAMETER">target</replaceable> <type>tsquery</>, <replaceable class="PARAMETER">substitute</replaceable> <type>tsquery</>]) returns <type>tsquery</>
1464-
</synopsis>
1465-
</term>
1466-
1467-
<listitem>
1468-
<para>
1469-
Aggregate form. XXX if we choose not to remove this, it needs to
1470-
be documented better. Note it is not listed in
1471-
textsearch-functions-table at the moment.
1472-
1473-
<programlisting>
1474-
CREATE TABLE aliases (t tsquery PRIMARY KEY, s tsquery);
1475-
INSERT INTO aliases VALUES('a', 'c');
1476-
1477-
SELECT ts_rewrite(ARRAY['a &amp; b'::tsquery, t,s]) FROM aliases;
1478-
ts_rewrite
1479-
------------
1480-
'b' &amp; 'c'
1481-
</programlisting>
1482-
</para>
1483-
</listitem>
1484-
</varlistentry>
1485-
14861459
<varlistentry>
14871460

14881461
<term>

src/backend/utils/adt/tsquery_rewrite.c

+3-131
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.5 2007/10/23 01:44:39 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_rewrite.c,v 1.6 2007/10/24 02:24:47 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -250,135 +250,7 @@ findsubquery(QTNode *root, QTNode *ex, QTNode *subs, bool *isfind)
250250
}
251251

252252
Datum
253-
ts_rewrite_accum(PG_FUNCTION_ARGS)
254-
{
255-
TSQuery acc;
256-
ArrayType *qa;
257-
TSQuery q;
258-
QTNode *qex = NULL,
259-
*subs = NULL,
260-
*acctree = NULL;
261-
bool isfind = false;
262-
Datum *elemsp;
263-
int nelemsp;
264-
MemoryContext aggcontext;
265-
MemoryContext oldcontext;
266-
267-
aggcontext = ((AggState *) fcinfo->context)->aggcontext;
268-
269-
if (PG_ARGISNULL(0) || PG_GETARG_POINTER(0) == NULL)
270-
{
271-
acc = (TSQuery) MemoryContextAlloc(aggcontext, HDRSIZETQ);
272-
SET_VARSIZE(acc, HDRSIZETQ);
273-
acc->size = 0;
274-
}
275-
else
276-
acc = PG_GETARG_TSQUERY(0);
277-
278-
if (PG_ARGISNULL(1) || PG_GETARG_POINTER(1) == NULL)
279-
PG_RETURN_TSQUERY(acc);
280-
else
281-
qa = PG_GETARG_ARRAYTYPE_P_COPY(1);
282-
283-
if (ARR_NDIM(qa) != 1)
284-
elog(ERROR, "array must be one-dimensional, not %d dimensions",
285-
ARR_NDIM(qa));
286-
if (ArrayGetNItems(ARR_NDIM(qa), ARR_DIMS(qa)) != 3)
287-
elog(ERROR, "array must have three elements");
288-
if (ARR_ELEMTYPE(qa) != TSQUERYOID)
289-
elog(ERROR, "array must contain tsquery elements");
290-
291-
deconstruct_array(qa, TSQUERYOID, -1, false, 'i', &elemsp, NULL, &nelemsp);
292-
293-
q = DatumGetTSQuery(elemsp[0]);
294-
if (q->size == 0)
295-
{
296-
pfree(elemsp);
297-
PG_RETURN_POINTER(acc);
298-
}
299-
300-
if (!acc->size)
301-
{
302-
if (VARSIZE(acc) > HDRSIZETQ)
303-
{
304-
pfree(elemsp);
305-
PG_RETURN_POINTER(acc);
306-
}
307-
else
308-
acctree = QT2QTN(GETQUERY(q), GETOPERAND(q));
309-
}
310-
else
311-
acctree = QT2QTN(GETQUERY(acc), GETOPERAND(acc));
312-
313-
QTNTernary(acctree);
314-
QTNSort(acctree);
315-
316-
q = DatumGetTSQuery(elemsp[1]);
317-
if (q->size == 0)
318-
{
319-
pfree(elemsp);
320-
PG_RETURN_POINTER(acc);
321-
}
322-
qex = QT2QTN(GETQUERY(q), GETOPERAND(q));
323-
QTNTernary(qex);
324-
QTNSort(qex);
325-
326-
q = DatumGetTSQuery(elemsp[2]);
327-
if (q->size)
328-
subs = QT2QTN(GETQUERY(q), GETOPERAND(q));
329-
330-
acctree = findsubquery(acctree, qex, subs, &isfind);
331-
332-
if (isfind || !acc->size)
333-
{
334-
/* pfree( acc ); do not pfree(p), because nodeAgg.c will */
335-
if (acctree)
336-
{
337-
QTNBinary(acctree);
338-
oldcontext = MemoryContextSwitchTo(aggcontext);
339-
acc = QTN2QT(acctree);
340-
MemoryContextSwitchTo(oldcontext);
341-
}
342-
else
343-
{
344-
acc = (TSQuery) MemoryContextAlloc(aggcontext, HDRSIZETQ);
345-
SET_VARSIZE(acc, HDRSIZETQ);
346-
acc->size = 0;
347-
}
348-
}
349-
350-
pfree(elemsp);
351-
QTNFree(qex);
352-
QTNFree(subs);
353-
QTNFree(acctree);
354-
355-
PG_RETURN_TSQUERY(acc);
356-
}
357-
358-
Datum
359-
ts_rewrite_finish(PG_FUNCTION_ARGS)
360-
{
361-
TSQuery acc = PG_GETARG_TSQUERY(0);
362-
TSQuery rewrited;
363-
364-
if (acc == NULL || PG_ARGISNULL(0) || acc->size == 0)
365-
{
366-
rewrited = (TSQuery) palloc(HDRSIZETQ);
367-
SET_VARSIZE(rewrited, HDRSIZETQ);
368-
rewrited->size = 0;
369-
}
370-
else
371-
{
372-
rewrited = (TSQuery) palloc(VARSIZE(acc));
373-
memcpy(rewrited, acc, VARSIZE(acc));
374-
pfree(acc);
375-
}
376-
377-
PG_RETURN_POINTER(rewrited);
378-
}
379-
380-
Datum
381-
tsquery_rewrite(PG_FUNCTION_ARGS)
253+
tsquery_rewrite_query(PG_FUNCTION_ARGS)
382254
{
383255
TSQuery query = PG_GETARG_TSQUERY_COPY(0);
384256
text *in = PG_GETARG_TEXT_P(1);
@@ -505,7 +377,7 @@ tsquery_rewrite(PG_FUNCTION_ARGS)
505377
}
506378

507379
Datum
508-
tsquery_rewrite_query(PG_FUNCTION_ARGS)
380+
tsquery_rewrite(PG_FUNCTION_ARGS)
509381
{
510382
TSQuery query = PG_GETARG_TSQUERY_COPY(0);
511383
TSQuery ex = PG_GETARG_TSQUERY(1);

src/include/catalog/catversion.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.436 2007/10/23 20:46:12 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.437 2007/10/24 02:24:47 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200710231
56+
#define CATALOG_VERSION_NO 200710232
5757

5858
#endif

src/include/catalog/pg_aggregate.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/catalog/pg_aggregate.h,v 1.63 2007/08/21 01:11:25 tgl Exp $
11+
* $PostgreSQL: pgsql/src/include/catalog/pg_aggregate.h,v 1.64 2007/10/24 02:24:47 tgl Exp $
1212
*
1313
* NOTES
1414
* the genbki.sh script reads this file and generates .bki
@@ -226,9 +226,6 @@ DATA(insert ( 2243 bitor - 0 1560 _null_ ));
226226
/* xml */
227227
DATA(insert ( 2901 xmlconcat2 - 0 142 _null_ ));
228228

229-
/* text search */
230-
DATA(insert ( 3688 ts_rewrite_accum ts_rewrite_finish 0 3615 _null_ ));
231-
232229
/*
233230
* prototypes for functions in pg_aggregate.c
234231
*/

src/include/catalog/pg_proc.h

+3-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.476 2007/10/19 22:01:45 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.477 2007/10/24 02:24:47 tgl Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -4258,15 +4258,9 @@ DESCR("number of nodes");
42584258
DATA(insert OID = 3673 ( querytree PGNSP PGUID 12 1 0 f f t f i 1 25 "3615" _null_ _null_ _null_ tsquerytree - _null_ _null_ ));
42594259
DESCR("show real useful query for GiST index");
42604260

4261-
DATA(insert OID = 3684 ( ts_rewrite PGNSP PGUID 12 1 0 f f t f i 3 3615 "3615 3615 3615" _null_ _null_ _null_ tsquery_rewrite_query - _null_ _null_ ));
4261+
DATA(insert OID = 3684 ( ts_rewrite PGNSP PGUID 12 1 0 f f t f i 3 3615 "3615 3615 3615" _null_ _null_ _null_ tsquery_rewrite - _null_ _null_ ));
42624262
DESCR("rewrite tsquery");
4263-
DATA(insert OID = 3685 ( ts_rewrite PGNSP PGUID 12 1 0 f f t f v 2 3615 "3615 25" _null_ _null_ _null_ tsquery_rewrite - _null_ _null_ ));
4264-
DESCR("rewrite tsquery");
4265-
DATA(insert OID = 3686 ( ts_rewrite_accum PGNSP PGUID 12 1 0 f f f f i 2 3615 "3615 3645" _null_ _null_ _null_ ts_rewrite_accum - _null_ _null_ ));
4266-
DESCR("rewrite tsquery accumulator");
4267-
DATA(insert OID = 3687 ( ts_rewrite_finish PGNSP PGUID 12 1 0 f f t f i 1 3615 "3615" _null_ _null_ _null_ ts_rewrite_finish - _null_ _null_ ));
4268-
DESCR("rewrite tsquery finish");
4269-
DATA(insert OID = 3688 ( ts_rewrite PGNSP PGUID 12 1 0 t f f f i 1 3615 "3645" _null_ _null_ _null_ aggregate_dummy - _null_ _null_ ));
4263+
DATA(insert OID = 3685 ( ts_rewrite PGNSP PGUID 12 1 0 f f t f v 2 3615 "3615 25" _null_ _null_ _null_ tsquery_rewrite_query - _null_ _null_ ));
42704264
DESCR("rewrite tsquery");
42714265

42724266
DATA(insert OID = 3695 ( gtsquery_compress PGNSP PGUID 12 1 0 f f t f i 1 2281 "2281" _null_ _null_ _null_ gtsquery_compress - _null_ _null_ ));

src/include/tsearch/ts_type.h

+1-3
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_type.h,v 1.6 2007/09/11 16:01:40 teodor Exp $
8+
* $PostgreSQL: pgsql/src/include/tsearch/ts_type.h,v 1.7 2007/10/24 02:24:49 tgl Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -285,8 +285,6 @@ extern Datum tsquery_not(PG_FUNCTION_ARGS);
285285

286286
extern Datum tsquery_rewrite(PG_FUNCTION_ARGS);
287287
extern Datum tsquery_rewrite_query(PG_FUNCTION_ARGS);
288-
extern Datum ts_rewrite_accum(PG_FUNCTION_ARGS);
289-
extern Datum ts_rewrite_finish(PG_FUNCTION_ARGS);
290288

291289
extern Datum tsq_mcontains(PG_FUNCTION_ARGS);
292290
extern Datum tsq_mcontained(PG_FUNCTION_ARGS);

src/test/regress/expected/tsearch.out

+21-21
Original file line numberDiff line numberDiff line change
@@ -676,25 +676,25 @@ SELECT ts_rewrite('moscow & hotel', 'SELECT keyword, sample FROM test_tsquery'::
676676
'hotel' & ( 'moskva' | 'moscow' )
677677
(1 row)
678678

679-
SELECT ts_rewrite('bar & new & qq & foo & york', 'SELECT keyword, sample FROM test_tsquery'::text );
679+
SELECT ts_rewrite('bar & new & qq & foo & york', 'SELECT keyword, sample FROM test_tsquery'::text );
680680
ts_rewrite
681681
-------------------------------------------------------------------------------------
682682
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | ( 'big' & 'appl' | 'new' & 'york' ) )
683683
(1 row)
684684

685-
SELECT ts_rewrite( ARRAY['moscow', keyword, sample] ) FROM test_tsquery;
685+
SELECT ts_rewrite( 'moscow', 'SELECT keyword, sample FROM test_tsquery');
686686
ts_rewrite
687687
---------------------
688688
'moskva' | 'moscow'
689689
(1 row)
690690

691-
SELECT ts_rewrite( ARRAY['moscow & hotel', keyword, sample] ) FROM test_tsquery;
691+
SELECT ts_rewrite( 'moscow & hotel', 'SELECT keyword, sample FROM test_tsquery');
692692
ts_rewrite
693693
-----------------------------------
694-
( 'moskva' | 'moscow' ) & 'hotel'
694+
'hotel' & ( 'moskva' | 'moscow' )
695695
(1 row)
696696

697-
SELECT ts_rewrite( ARRAY['bar & new & qq & foo & york', keyword, sample] ) FROM test_tsquery;
697+
SELECT ts_rewrite( 'bar & new & qq & foo & york', 'SELECT keyword, sample FROM test_tsquery');
698698
ts_rewrite
699699
-------------------------------------------------------------------------------------
700700
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | ( 'big' & 'appl' | 'new' & 'york' ) )
@@ -723,37 +723,37 @@ SELECT keyword FROM test_tsquery WHERE keyword <@ 'moscow';
723723
'moscow'
724724
(1 row)
725725

726-
SELECT ts_rewrite( ARRAY[query, keyword, sample] ) FROM test_tsquery, to_tsquery('english', 'moscow') AS query WHERE keyword <@ query;
726+
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow') AS query;
727727
ts_rewrite
728728
---------------------
729729
'moskva' | 'moscow'
730730
(1 row)
731731

732-
SELECT ts_rewrite( ARRAY[query, keyword, sample] ) FROM test_tsquery, to_tsquery('english', 'moscow & hotel') AS query WHERE keyword <@ query;
732+
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow & hotel') AS query;
733733
ts_rewrite
734734
-----------------------------------
735-
( 'moskva' | 'moscow' ) & 'hotel'
735+
'hotel' & ( 'moskva' | 'moscow' )
736736
(1 row)
737737

738-
SELECT ts_rewrite( ARRAY[query, keyword, sample] ) FROM test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') AS query WHERE keyword <@ query;
738+
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'bar & new & qq & foo & york') AS query;
739739
ts_rewrite
740740
-------------------------------------------------------------------------------------
741741
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | ( 'big' & 'appl' | 'new' & 'york' ) )
742742
(1 row)
743743

744-
SELECT ts_rewrite( ARRAY[query, keyword, sample] ) FROM test_tsquery, to_tsquery('english', 'moscow') AS query WHERE query @> keyword;
744+
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow') AS query;
745745
ts_rewrite
746746
---------------------
747747
'moskva' | 'moscow'
748748
(1 row)
749749

750-
SELECT ts_rewrite( ARRAY[query, keyword, sample] ) FROM test_tsquery, to_tsquery('english', 'moscow & hotel') AS query WHERE query @> keyword;
750+
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow & hotel') AS query;
751751
ts_rewrite
752752
-----------------------------------
753-
( 'moskva' | 'moscow' ) & 'hotel'
753+
'hotel' & ( 'moskva' | 'moscow' )
754754
(1 row)
755755

756-
SELECT ts_rewrite( ARRAY[query, keyword, sample] ) FROM test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') AS query WHERE query @> keyword;
756+
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'bar & new & qq & foo & york') AS query;
757757
ts_rewrite
758758
-------------------------------------------------------------------------------------
759759
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | ( 'big' & 'appl' | 'new' & 'york' ) )
@@ -784,37 +784,37 @@ SELECT keyword FROM test_tsquery WHERE keyword <@ 'moscow';
784784
'moscow'
785785
(1 row)
786786

787-
SELECT ts_rewrite( ARRAY[query, keyword, sample] ) FROM test_tsquery, to_tsquery('english', 'moscow') AS query WHERE keyword <@ query;
787+
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow') AS query;
788788
ts_rewrite
789789
---------------------
790790
'moskva' | 'moscow'
791791
(1 row)
792792

793-
SELECT ts_rewrite( ARRAY[query, keyword, sample] ) FROM test_tsquery, to_tsquery('english', 'moscow & hotel') AS query WHERE keyword <@ query;
793+
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow & hotel') AS query;
794794
ts_rewrite
795795
-----------------------------------
796-
( 'moskva' | 'moscow' ) & 'hotel'
796+
'hotel' & ( 'moskva' | 'moscow' )
797797
(1 row)
798798

799-
SELECT ts_rewrite( ARRAY[query, keyword, sample] ) FROM test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') AS query WHERE keyword <@ query;
799+
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'bar & new & qq & foo & york') AS query;
800800
ts_rewrite
801801
-------------------------------------------------------------------------------------
802802
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | ( 'big' & 'appl' | 'new' & 'york' ) )
803803
(1 row)
804804

805-
SELECT ts_rewrite( ARRAY[query, keyword, sample] ) FROM test_tsquery, to_tsquery('english', 'moscow') AS query WHERE query @> keyword;
805+
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow') AS query;
806806
ts_rewrite
807807
---------------------
808808
'moskva' | 'moscow'
809809
(1 row)
810810

811-
SELECT ts_rewrite( ARRAY[query, keyword, sample] ) FROM test_tsquery, to_tsquery('english', 'moscow & hotel') AS query WHERE query @> keyword;
811+
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow & hotel') AS query;
812812
ts_rewrite
813813
-----------------------------------
814-
( 'moskva' | 'moscow' ) & 'hotel'
814+
'hotel' & ( 'moskva' | 'moscow' )
815815
(1 row)
816816

817-
SELECT ts_rewrite( ARRAY[query, keyword, sample] ) FROM test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') AS query WHERE query @> keyword;
817+
SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'bar & new & qq & foo & york') AS query;
818818
ts_rewrite
819819
-------------------------------------------------------------------------------------
820820
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | ( 'big' & 'appl' | 'new' & 'york' ) )

0 commit comments

Comments
 (0)