7
7
*
8
8
*
9
9
* 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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
14
14
#include "postgres.h"
15
15
16
16
#include "catalog/namespace.h"
17
+ #include "catalog/pg_type.h"
17
18
#include "commands/trigger.h"
18
19
#include "fmgr.h"
19
20
#include "tsearch/ts_utils.h"
@@ -77,13 +78,14 @@ Datum tsa_set_curprs_byname(PG_FUNCTION_ARGS);
77
78
Datum tsa_parse_current (PG_FUNCTION_ARGS );
78
79
Datum tsa_set_curcfg (PG_FUNCTION_ARGS );
79
80
Datum tsa_set_curcfg_byname (PG_FUNCTION_ARGS );
80
- Datum tsa_show_curcfg (PG_FUNCTION_ARGS );
81
81
Datum tsa_to_tsvector_name (PG_FUNCTION_ARGS );
82
82
Datum tsa_to_tsquery_name (PG_FUNCTION_ARGS );
83
83
Datum tsa_plainto_tsquery_name (PG_FUNCTION_ARGS );
84
84
Datum tsa_headline_byname (PG_FUNCTION_ARGS );
85
85
Datum tsa_ts_stat (PG_FUNCTION_ARGS );
86
86
Datum tsa_tsearch2 (PG_FUNCTION_ARGS );
87
+ Datum tsa_rewrite_accum (PG_FUNCTION_ARGS );
88
+ Datum tsa_rewrite_finish (PG_FUNCTION_ARGS );
87
89
88
90
PG_FUNCTION_INFO_V1 (tsa_lexize_byname );
89
91
PG_FUNCTION_INFO_V1 (tsa_lexize_bycurrent );
@@ -95,13 +97,14 @@ PG_FUNCTION_INFO_V1(tsa_set_curprs_byname);
95
97
PG_FUNCTION_INFO_V1 (tsa_parse_current );
96
98
PG_FUNCTION_INFO_V1 (tsa_set_curcfg );
97
99
PG_FUNCTION_INFO_V1 (tsa_set_curcfg_byname );
98
- PG_FUNCTION_INFO_V1 (tsa_show_curcfg );
99
100
PG_FUNCTION_INFO_V1 (tsa_to_tsvector_name );
100
101
PG_FUNCTION_INFO_V1 (tsa_to_tsquery_name );
101
102
PG_FUNCTION_INFO_V1 (tsa_plainto_tsquery_name );
102
103
PG_FUNCTION_INFO_V1 (tsa_headline_byname );
103
104
PG_FUNCTION_INFO_V1 (tsa_ts_stat );
104
105
PG_FUNCTION_INFO_V1 (tsa_tsearch2 );
106
+ PG_FUNCTION_INFO_V1 (tsa_rewrite_accum );
107
+ PG_FUNCTION_INFO_V1 (tsa_rewrite_finish );
105
108
106
109
107
110
/*
@@ -138,9 +141,6 @@ UNSUPPORTED_FUNCTION(tsa_prsd_headline);
138
141
UNSUPPORTED_FUNCTION (tsa_reset_tsearch );
139
142
UNSUPPORTED_FUNCTION (tsa_get_covers );
140
143
141
- UNSUPPORTED_FUNCTION (tsa_rewrite_accum );
142
- UNSUPPORTED_FUNCTION (tsa_rewrite_finish );
143
-
144
144
145
145
/*
146
146
* list of redefined functions
@@ -275,6 +275,7 @@ tsa_set_curcfg_byname(PG_FUNCTION_ARGS)
275
275
char * name ;
276
276
277
277
name = TextPGetCString (arg0 );
278
+
278
279
set_config_option ("default_text_search_config" , name ,
279
280
PGC_USERSET ,
280
281
PGC_S_SESSION ,
@@ -284,20 +285,6 @@ tsa_set_curcfg_byname(PG_FUNCTION_ARGS)
284
285
PG_RETURN_VOID ();
285
286
}
286
287
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
-
301
288
/* to_tsvector(text, text) */
302
289
Datum
303
290
tsa_to_tsvector_name (PG_FUNCTION_ARGS )
@@ -411,6 +398,136 @@ tsa_tsearch2(PG_FUNCTION_ARGS)
411
398
return tsvector_update_trigger_byid (fcinfo );
412
399
}
413
400
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
+
414
531
/*
415
532
* Get Oid of current dictionary
416
533
*/
0 commit comments