Skip to content

Commit 4394c1b

Browse files
committed
Resurrect the code for the rewrite(ARRAY[...]) aggregate function,
and put it into contrib/tsearch2 compatibility module.
1 parent abd183e commit 4394c1b

File tree

6 files changed

+253
-28
lines changed

6 files changed

+253
-28
lines changed

contrib/tsearch2/expected/tsearch2.out

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,24 @@ select rewrite('bar & new & qq & foo & york', 'select keyword, sample from test
472472
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | ( 'big' & 'appl' | 'new' & 'york' ) )
473473
(1 row)
474474

475+
select rewrite( ARRAY['moscow', keyword, sample] ) from test_tsquery;
476+
rewrite
477+
---------------------
478+
'moskva' | 'moscow'
479+
(1 row)
480+
481+
select rewrite( ARRAY['moscow & hotel', keyword, sample] ) from test_tsquery;
482+
rewrite
483+
-----------------------------------
484+
( 'moskva' | 'moscow' ) & 'hotel'
485+
(1 row)
486+
487+
select rewrite( ARRAY['bar & new & qq & foo & york', keyword, sample] ) from test_tsquery;
488+
rewrite
489+
-------------------------------------------------------------------------------------
490+
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | ( 'big' & 'appl' | 'new' & 'york' ) )
491+
(1 row)
492+
475493
select keyword from test_tsquery where keyword @> 'new';
476494
keyword
477495
----------------
@@ -495,6 +513,42 @@ select keyword from test_tsquery where keyword <@ 'moscow';
495513
'moscow'
496514
(1 row)
497515

516+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow') as query where keyword <@ query;
517+
rewrite
518+
---------------------
519+
'moskva' | 'moscow'
520+
(1 row)
521+
522+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow & hotel') as query where keyword <@ query;
523+
rewrite
524+
-----------------------------------
525+
( 'moskva' | 'moscow' ) & 'hotel'
526+
(1 row)
527+
528+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') as query where keyword <@ query;
529+
rewrite
530+
-------------------------------------------------------------------------------------
531+
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | ( 'big' & 'appl' | 'new' & 'york' ) )
532+
(1 row)
533+
534+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow') as query where query @> keyword;
535+
rewrite
536+
---------------------
537+
'moskva' | 'moscow'
538+
(1 row)
539+
540+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow & hotel') as query where query @> keyword;
541+
rewrite
542+
-----------------------------------
543+
( 'moskva' | 'moscow' ) & 'hotel'
544+
(1 row)
545+
546+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') as query where query @> keyword;
547+
rewrite
548+
-------------------------------------------------------------------------------------
549+
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | ( 'big' & 'appl' | 'new' & 'york' ) )
550+
(1 row)
551+
498552
create index qq on test_tsquery using gist (keyword gist_tp_tsquery_ops);
499553
set enable_seqscan='off';
500554
select keyword from test_tsquery where keyword @> 'new';
@@ -520,6 +574,42 @@ select keyword from test_tsquery where keyword <@ 'moscow';
520574
'moscow'
521575
(1 row)
522576

577+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow') as query where keyword <@ query;
578+
rewrite
579+
---------------------
580+
'moskva' | 'moscow'
581+
(1 row)
582+
583+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow & hotel') as query where keyword <@ query;
584+
rewrite
585+
-----------------------------------
586+
( 'moskva' | 'moscow' ) & 'hotel'
587+
(1 row)
588+
589+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') as query where keyword <@ query;
590+
rewrite
591+
-------------------------------------------------------------------------------------
592+
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | ( 'big' & 'appl' | 'new' & 'york' ) )
593+
(1 row)
594+
595+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow') as query where query @> keyword;
596+
rewrite
597+
---------------------
598+
'moskva' | 'moscow'
599+
(1 row)
600+
601+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow & hotel') as query where query @> keyword;
602+
rewrite
603+
-----------------------------------
604+
( 'moskva' | 'moscow' ) & 'hotel'
605+
(1 row)
606+
607+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') as query where query @> keyword;
608+
rewrite
609+
-------------------------------------------------------------------------------------
610+
'citi' & 'foo' & ( 'bar' | 'qq' ) & ( 'nyc' | ( 'big' & 'appl' | 'new' & 'york' ) )
611+
(1 row)
612+
523613
set enable_seqscan='on';
524614
select lexize('simple', 'ASD56 hsdkf');
525615
lexize

contrib/tsearch2/sql/tsearch2.sql

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,21 @@ select rewrite('moscow', 'select keyword, sample from test_tsquery'::text );
116116
select rewrite('moscow & hotel', 'select keyword, sample from test_tsquery'::text );
117117
select rewrite('bar & new & qq & foo & york', 'select keyword, sample from test_tsquery'::text );
118118

119+
select rewrite( ARRAY['moscow', keyword, sample] ) from test_tsquery;
120+
select rewrite( ARRAY['moscow & hotel', keyword, sample] ) from test_tsquery;
121+
select rewrite( ARRAY['bar & new & qq & foo & york', keyword, sample] ) from test_tsquery;
122+
123+
119124
select keyword from test_tsquery where keyword @> 'new';
120125
select keyword from test_tsquery where keyword @> 'moscow';
121126
select keyword from test_tsquery where keyword <@ 'new';
122127
select keyword from test_tsquery where keyword <@ 'moscow';
128+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow') as query where keyword <@ query;
129+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow & hotel') as query where keyword <@ query;
130+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') as query where keyword <@ query;
131+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow') as query where query @> keyword;
132+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow & hotel') as query where query @> keyword;
133+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') as query where query @> keyword;
123134

124135
create index qq on test_tsquery using gist (keyword gist_tp_tsquery_ops);
125136
set enable_seqscan='off';
@@ -128,7 +139,12 @@ select keyword from test_tsquery where keyword @> 'new';
128139
select keyword from test_tsquery where keyword @> 'moscow';
129140
select keyword from test_tsquery where keyword <@ 'new';
130141
select keyword from test_tsquery where keyword <@ 'moscow';
131-
142+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow') as query where keyword <@ query;
143+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow & hotel') as query where keyword <@ query;
144+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') as query where keyword <@ query;
145+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow') as query where query @> keyword;
146+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow & hotel') as query where query @> keyword;
147+
select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') as query where query @> keyword;
132148
set enable_seqscan='on';
133149

134150

contrib/tsearch2/tsearch2.c

Lines changed: 137 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/contrib/tsearch2/tsearch2.c,v 1.1 2007/11/13 21:02:29 tgl Exp $
10+
* $PostgreSQL: pgsql/contrib/tsearch2/tsearch2.c,v 1.2 2007/11/13 22:14:50 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#include "postgres.h"
1515

1616
#include "catalog/namespace.h"
17+
#include "catalog/pg_type.h"
1718
#include "commands/trigger.h"
1819
#include "fmgr.h"
1920
#include "tsearch/ts_utils.h"
@@ -77,13 +78,14 @@ Datum tsa_set_curprs_byname(PG_FUNCTION_ARGS);
7778
Datum tsa_parse_current(PG_FUNCTION_ARGS);
7879
Datum tsa_set_curcfg(PG_FUNCTION_ARGS);
7980
Datum tsa_set_curcfg_byname(PG_FUNCTION_ARGS);
80-
Datum tsa_show_curcfg(PG_FUNCTION_ARGS);
8181
Datum tsa_to_tsvector_name(PG_FUNCTION_ARGS);
8282
Datum tsa_to_tsquery_name(PG_FUNCTION_ARGS);
8383
Datum tsa_plainto_tsquery_name(PG_FUNCTION_ARGS);
8484
Datum tsa_headline_byname(PG_FUNCTION_ARGS);
8585
Datum tsa_ts_stat(PG_FUNCTION_ARGS);
8686
Datum tsa_tsearch2(PG_FUNCTION_ARGS);
87+
Datum tsa_rewrite_accum(PG_FUNCTION_ARGS);
88+
Datum tsa_rewrite_finish(PG_FUNCTION_ARGS);
8789

8890
PG_FUNCTION_INFO_V1(tsa_lexize_byname);
8991
PG_FUNCTION_INFO_V1(tsa_lexize_bycurrent);
@@ -95,13 +97,14 @@ PG_FUNCTION_INFO_V1(tsa_set_curprs_byname);
9597
PG_FUNCTION_INFO_V1(tsa_parse_current);
9698
PG_FUNCTION_INFO_V1(tsa_set_curcfg);
9799
PG_FUNCTION_INFO_V1(tsa_set_curcfg_byname);
98-
PG_FUNCTION_INFO_V1(tsa_show_curcfg);
99100
PG_FUNCTION_INFO_V1(tsa_to_tsvector_name);
100101
PG_FUNCTION_INFO_V1(tsa_to_tsquery_name);
101102
PG_FUNCTION_INFO_V1(tsa_plainto_tsquery_name);
102103
PG_FUNCTION_INFO_V1(tsa_headline_byname);
103104
PG_FUNCTION_INFO_V1(tsa_ts_stat);
104105
PG_FUNCTION_INFO_V1(tsa_tsearch2);
106+
PG_FUNCTION_INFO_V1(tsa_rewrite_accum);
107+
PG_FUNCTION_INFO_V1(tsa_rewrite_finish);
105108

106109

107110
/*
@@ -138,9 +141,6 @@ UNSUPPORTED_FUNCTION(tsa_prsd_headline);
138141
UNSUPPORTED_FUNCTION(tsa_reset_tsearch);
139142
UNSUPPORTED_FUNCTION(tsa_get_covers);
140143

141-
UNSUPPORTED_FUNCTION(tsa_rewrite_accum);
142-
UNSUPPORTED_FUNCTION(tsa_rewrite_finish);
143-
144144

145145
/*
146146
* list of redefined functions
@@ -275,6 +275,7 @@ tsa_set_curcfg_byname(PG_FUNCTION_ARGS)
275275
char *name;
276276

277277
name = TextPGetCString(arg0);
278+
278279
set_config_option("default_text_search_config", name,
279280
PGC_USERSET,
280281
PGC_S_SESSION,
@@ -284,20 +285,6 @@ tsa_set_curcfg_byname(PG_FUNCTION_ARGS)
284285
PG_RETURN_VOID();
285286
}
286287

287-
/* show_curcfg() */
288-
Datum
289-
tsa_show_curcfg(PG_FUNCTION_ARGS)
290-
{
291-
char *cfgname;
292-
Oid config_oid;
293-
294-
cfgname = GetConfigOptionByName("default_text_search_config", NULL);
295-
config_oid = DatumGetObjectId(DirectFunctionCall1(regconfigin,
296-
CStringGetDatum(cfgname)));
297-
298-
PG_RETURN_OID(config_oid);
299-
}
300-
301288
/* to_tsvector(text, text) */
302289
Datum
303290
tsa_to_tsvector_name(PG_FUNCTION_ARGS)
@@ -411,6 +398,136 @@ tsa_tsearch2(PG_FUNCTION_ARGS)
411398
return tsvector_update_trigger_byid(fcinfo);
412399
}
413400

401+
402+
Datum
403+
tsa_rewrite_accum(PG_FUNCTION_ARGS)
404+
{
405+
TSQuery acc;
406+
ArrayType *qa;
407+
TSQuery q;
408+
QTNode *qex = NULL,
409+
*subs = NULL,
410+
*acctree = NULL;
411+
bool isfind = false;
412+
Datum *elemsp;
413+
int nelemsp;
414+
MemoryContext aggcontext;
415+
MemoryContext oldcontext;
416+
417+
aggcontext = ((AggState *) fcinfo->context)->aggcontext;
418+
419+
if (PG_ARGISNULL(0) || PG_GETARG_POINTER(0) == NULL)
420+
{
421+
acc = (TSQuery) MemoryContextAlloc(aggcontext, HDRSIZETQ);
422+
SET_VARSIZE(acc, HDRSIZETQ);
423+
acc->size = 0;
424+
}
425+
else
426+
acc = PG_GETARG_TSQUERY(0);
427+
428+
if (PG_ARGISNULL(1) || PG_GETARG_POINTER(1) == NULL)
429+
PG_RETURN_TSQUERY(acc);
430+
else
431+
qa = PG_GETARG_ARRAYTYPE_P_COPY(1);
432+
433+
if (ARR_NDIM(qa) != 1)
434+
elog(ERROR, "array must be one-dimensional, not %d dimensions",
435+
ARR_NDIM(qa));
436+
if (ArrayGetNItems(ARR_NDIM(qa), ARR_DIMS(qa)) != 3)
437+
elog(ERROR, "array must have three elements");
438+
if (ARR_ELEMTYPE(qa) != TSQUERYOID)
439+
elog(ERROR, "array must contain tsquery elements");
440+
441+
deconstruct_array(qa, TSQUERYOID, -1, false, 'i', &elemsp, NULL, &nelemsp);
442+
443+
q = DatumGetTSQuery(elemsp[0]);
444+
if (q->size == 0)
445+
{
446+
pfree(elemsp);
447+
PG_RETURN_POINTER(acc);
448+
}
449+
450+
if (!acc->size)
451+
{
452+
if (VARSIZE(acc) > HDRSIZETQ)
453+
{
454+
pfree(elemsp);
455+
PG_RETURN_POINTER(acc);
456+
}
457+
else
458+
acctree = QT2QTN(GETQUERY(q), GETOPERAND(q));
459+
}
460+
else
461+
acctree = QT2QTN(GETQUERY(acc), GETOPERAND(acc));
462+
463+
QTNTernary(acctree);
464+
QTNSort(acctree);
465+
466+
q = DatumGetTSQuery(elemsp[1]);
467+
if (q->size == 0)
468+
{
469+
pfree(elemsp);
470+
PG_RETURN_POINTER(acc);
471+
}
472+
qex = QT2QTN(GETQUERY(q), GETOPERAND(q));
473+
QTNTernary(qex);
474+
QTNSort(qex);
475+
476+
q = DatumGetTSQuery(elemsp[2]);
477+
if (q->size)
478+
subs = QT2QTN(GETQUERY(q), GETOPERAND(q));
479+
480+
acctree = findsubquery(acctree, qex, subs, &isfind);
481+
482+
if (isfind || !acc->size)
483+
{
484+
/* pfree( acc ); do not pfree(p), because nodeAgg.c will */
485+
if (acctree)
486+
{
487+
QTNBinary(acctree);
488+
oldcontext = MemoryContextSwitchTo(aggcontext);
489+
acc = QTN2QT(acctree);
490+
MemoryContextSwitchTo(oldcontext);
491+
}
492+
else
493+
{
494+
acc = (TSQuery) MemoryContextAlloc(aggcontext, HDRSIZETQ);
495+
SET_VARSIZE(acc, HDRSIZETQ);
496+
acc->size = 0;
497+
}
498+
}
499+
500+
pfree(elemsp);
501+
QTNFree(qex);
502+
QTNFree(subs);
503+
QTNFree(acctree);
504+
505+
PG_RETURN_TSQUERY(acc);
506+
}
507+
508+
Datum
509+
tsa_rewrite_finish(PG_FUNCTION_ARGS)
510+
{
511+
TSQuery acc = PG_GETARG_TSQUERY(0);
512+
TSQuery rewrited;
513+
514+
if (acc == NULL || PG_ARGISNULL(0) || acc->size == 0)
515+
{
516+
rewrited = (TSQuery) palloc(HDRSIZETQ);
517+
SET_VARSIZE(rewrited, HDRSIZETQ);
518+
rewrited->size = 0;
519+
}
520+
else
521+
{
522+
rewrited = (TSQuery) palloc(VARSIZE(acc));
523+
memcpy(rewrited, acc, VARSIZE(acc));
524+
pfree(acc);
525+
}
526+
527+
PG_RETURN_POINTER(rewrited);
528+
}
529+
530+
414531
/*
415532
* Get Oid of current dictionary
416533
*/

contrib/tsearch2/tsearch2.sql.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/contrib/tsearch2/tsearch2.sql.in,v 1.1 2007/11/13 21:02:29 tgl Exp $ */
1+
/* $PostgreSQL: pgsql/contrib/tsearch2/tsearch2.sql.in,v 1.2 2007/11/13 22:14:50 tgl Exp $ */
22

33
-- Adjust this setting to control where the objects get created.
44
SET search_path = public;
@@ -206,9 +206,9 @@ CREATE FUNCTION set_curcfg(text)
206206

207207
CREATE FUNCTION show_curcfg()
208208
RETURNS oid
209-
as 'MODULE_PATHNAME', 'tsa_show_curcfg'
210-
LANGUAGE C
211-
RETURNS NULL ON NULL INPUT;
209+
AS 'get_current_ts_config'
210+
LANGUAGE INTERNAL
211+
RETURNS NULL ON NULL INPUT STABLE;
212212

213213
CREATE FUNCTION length(tsvector)
214214
RETURNS int4

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