summaryrefslogtreecommitdiff
path: root/contrib/tsearch2/tsearch2.c
blob: 030a84c73be7ea4ee8e4044c600950e22d06cb15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*-------------------------------------------------------------------------
 *
 * tsearch2.c
 *		Backwards-compatibility package for old contrib/tsearch2 API
 *
 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
 *
 *
 * IDENTIFICATION
 *	  contrib/tsearch2/tsearch2.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "fmgr.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/syscache.h"

PG_MODULE_MAGIC;

static Oid	current_dictionary_oid = InvalidOid;
static Oid	current_parser_oid = InvalidOid;

/* insert given value at argument position 0 */
#define INSERT_ARGUMENT0(argument, isnull)				\
	do {												\
		int i;											\
		for (i = fcinfo->nargs; i > 0; i--)				\
		{												\
			fcinfo->arg[i] = fcinfo->arg[i-1];			\
			fcinfo->argnull[i] = fcinfo->argnull[i-1];	\
		}												\
		fcinfo->arg[0] = (argument);					\
		fcinfo->argnull[0] = (isnull);					\
		fcinfo->nargs++;								\
	} while (0)

#define TextGetObjectId(infunction, text) \
	DatumGetObjectId(DirectFunctionCall1(infunction, \
					 CStringGetDatum(text_to_cstring(text))))

#define UNSUPPORTED_FUNCTION(name)						\
	Datum name(PG_FUNCTION_ARGS);						\
	Datum												\
	name(PG_FUNCTION_ARGS)								\
	{													\
		ereport(ERROR,									\
				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),\
				 errmsg("function %s is no longer supported", \
						format_procedure(fcinfo->flinfo->fn_oid)), \
				 errhint("Switch to new tsearch functionality."))); \
		/* keep compiler quiet */						\
		PG_RETURN_NULL();								\
	}													\
	PG_FUNCTION_INFO_V1(name)

static Oid	GetCurrentDict(void);
static Oid	GetCurrentParser(void);

Datum		tsa_lexize_byname(PG_FUNCTION_ARGS);
Datum		tsa_lexize_bycurrent(PG_FUNCTION_ARGS);
Datum		tsa_set_curdict(PG_FUNCTION_ARGS);
Datum		tsa_set_curdict_byname(PG_FUNCTION_ARGS);
Datum		tsa_token_type_current(PG_FUNCTION_ARGS);
Datum		tsa_set_curprs(PG_FUNCTION_ARGS);
Datum		tsa_set_curprs_byname(PG_FUNCTION_ARGS);
Datum		tsa_parse_current(PG_FUNCTION_ARGS);
Datum		tsa_set_curcfg(PG_FUNCTION_ARGS);
Datum		tsa_set_curcfg_byname(PG_FUNCTION_ARGS);
Datum		tsa_to_tsvector_name(PG_FUNCTION_ARGS);
Datum		tsa_to_tsquery_name(PG_FUNCTION_ARGS);
Datum		tsa_plainto_tsquery_name(PG_FUNCTION_ARGS);
Datum		tsa_headline_byname(PG_FUNCTION_ARGS);
Datum		tsa_ts_stat(PG_FUNCTION_ARGS);
Datum		tsa_tsearch2(PG_FUNCTION_ARGS);
Datum		tsa_rewrite_accum(PG_FUNCTION_ARGS);
Datum		tsa_rewrite_finish(PG_FUNCTION_ARGS);

PG_FUNCTION_INFO_V1(tsa_lexize_byname);
PG_FUNCTION_INFO_V1(tsa_lexize_bycurrent);
PG_FUNCTION_INFO_V1(tsa_set_curdict);
PG_FUNCTION_INFO_V1(tsa_set_curdict_byname);
PG_FUNCTION_INFO_V1(tsa_token_type_current);
PG_FUNCTION_INFO_V1(tsa_set_curprs);
PG_FUNCTION_INFO_V1(tsa_set_curprs_byname);
PG_FUNCTION_INFO_V1(tsa_parse_current);
PG_FUNCTION_INFO_V1(tsa_set_curcfg);
PG_FUNCTION_INFO_V1(tsa_set_curcfg_byname);
PG_FUNCTION_INFO_V1(tsa_to_tsvector_name);
PG_FUNCTION_INFO_V1(tsa_to_tsquery_name);
PG_FUNCTION_INFO_V1(tsa_plainto_tsquery_name);
PG_FUNCTION_INFO_V1(tsa_headline_byname);
PG_FUNCTION_INFO_V1(tsa_ts_stat);
PG_FUNCTION_INFO_V1(tsa_tsearch2);
PG_FUNCTION_INFO_V1(tsa_rewrite_accum);
PG_FUNCTION_INFO_V1(tsa_rewrite_finish);


/*
 * List of unsupported functions
 *
 * The parser and dictionary functions are defined only so that the former
 * contents of pg_ts_parser and pg_ts_dict can be loaded into the system,
 * for ease of reference while creating the new tsearch configuration.
 */

UNSUPPORTED_FUNCTION(tsa_dex_init);
UNSUPPORTED_FUNCTION(tsa_dex_lexize);

UNSUPPORTED_FUNCTION(tsa_snb_en_init);
UNSUPPORTED_FUNCTION(tsa_snb_lexize);
UNSUPPORTED_FUNCTION(tsa_snb_ru_init_koi8);
UNSUPPORTED_FUNCTION(tsa_snb_ru_init_utf8);
UNSUPPORTED_FUNCTION(tsa_snb_ru_init);

UNSUPPORTED_FUNCTION(tsa_spell_init);
UNSUPPORTED_FUNCTION(tsa_spell_lexize);

UNSUPPORTED_FUNCTION(tsa_syn_init);
UNSUPPORTED_FUNCTION(tsa_syn_lexize);

UNSUPPORTED_FUNCTION(tsa_thesaurus_init);
UNSUPPORTED_FUNCTION(tsa_thesaurus_lexize);

UNSUPPORTED_FUNCTION(tsa_prsd_start);
UNSUPPORTED_FUNCTION(tsa_prsd_getlexeme);
UNSUPPORTED_FUNCTION(tsa_prsd_end);
UNSUPPORTED_FUNCTION(tsa_prsd_lextype);
UNSUPPORTED_FUNCTION(tsa_prsd_headline);

UNSUPPORTED_FUNCTION(tsa_reset_tsearch);
UNSUPPORTED_FUNCTION(tsa_get_covers);


/*
 * list of redefined functions
 */

/* lexize(text, text) */
Datum
tsa_lexize_byname(PG_FUNCTION_ARGS)
{
	text	   *dictname = PG_GETARG_TEXT_PP(0);
	Datum		arg1 = PG_GETARG_DATUM(1);

	return DirectFunctionCall2(ts_lexize,
				ObjectIdGetDatum(TextGetObjectId(regdictionaryin, dictname)),
							   arg1);
}

/* lexize(text) */
Datum
tsa_lexize_bycurrent(PG_FUNCTION_ARGS)
{
	Datum		arg0 = PG_GETARG_DATUM(0);
	Oid			id = GetCurrentDict();

	return DirectFunctionCall2(ts_lexize,
							   ObjectIdGetDatum(id),
							   arg0);
}

/* set_curdict(int) */
Datum
tsa_set_curdict(PG_FUNCTION_ARGS)
{
	Oid			dict_oid = PG_GETARG_OID(0);

	if (!SearchSysCacheExists(TSDICTOID,
							  ObjectIdGetDatum(dict_oid),
							  0, 0, 0))
		elog(ERROR, "cache lookup failed for text search dictionary %u",
			 dict_oid);

	current_dictionary_oid = dict_oid;

	PG_RETURN_VOID();
}

/* set_curdict(text) */
Datum
tsa_set_curdict_byname(PG_FUNCTION_ARGS)
{
	text	   *name = PG_GETARG_TEXT_PP(0);
	Oid			dict_oid;

	dict_oid = get_ts_dict_oid(stringToQualifiedNameList(text_to_cstring(name)), false);

	current_dictionary_oid = dict_oid;

	PG_RETURN_VOID();
}

/* token_type() */
Datum
tsa_token_type_current(PG_FUNCTION_ARGS)
{
	INSERT_ARGUMENT0(ObjectIdGetDatum(GetCurrentParser()), false);
	return ts_token_type_byid(fcinfo);
}

/* set_curprs(int) */
Datum
tsa_set_curprs(PG_FUNCTION_ARGS)
{
	Oid			parser_oid = PG_GETARG_OID(0);

	if (!SearchSysCacheExists(TSPARSEROID,
							  ObjectIdGetDatum(parser_oid),
							  0, 0, 0))
		elog(ERROR, "cache lookup failed for text search parser %u",
			 parser_oid);

	current_parser_oid = parser_oid;

	PG_RETURN_VOID();
}

/* set_curprs(text) */
Datum
tsa_set_curprs_byname(PG_FUNCTION_ARGS)
{
	text	   *name = PG_GETARG_TEXT_PP(0);
	Oid			parser_oid;

	parser_oid = get_ts_parser_oid(stringToQualifiedNameList(text_to_cstring(name)), false);

	current_parser_oid = parser_oid;

	PG_RETURN_VOID();
}

/* parse(text) */
Datum
tsa_parse_current(PG_FUNCTION_ARGS)
{
	INSERT_ARGUMENT0(ObjectIdGetDatum(GetCurrentParser()), false);
	return ts_parse_byid(fcinfo);
}

/* set_curcfg(int) */
Datum
tsa_set_curcfg(PG_FUNCTION_ARGS)
{
	Oid			arg0 = PG_GETARG_OID(0);
	char	   *name;

	name = DatumGetCString(DirectFunctionCall1(regconfigout,
											   ObjectIdGetDatum(arg0)));

	set_config_option("default_text_search_config", name,
					  PGC_USERSET,
					  PGC_S_SESSION,
					  GUC_ACTION_SET,
					  true);

	PG_RETURN_VOID();
}

/* set_curcfg(text) */
Datum
tsa_set_curcfg_byname(PG_FUNCTION_ARGS)
{
	text	   *arg0 = PG_GETARG_TEXT_PP(0);
	char	   *name;

	name = text_to_cstring(arg0);

	set_config_option("default_text_search_config", name,
					  PGC_USERSET,
					  PGC_S_SESSION,
					  GUC_ACTION_SET,
					  true);

	PG_RETURN_VOID();
}

/* to_tsvector(text, text) */
Datum
tsa_to_tsvector_name(PG_FUNCTION_ARGS)
{
	text	   *cfgname = PG_GETARG_TEXT_PP(0);
	Datum		arg1 = PG_GETARG_DATUM(1);
	Oid			config_oid;

	config_oid = TextGetObjectId(regconfigin, cfgname);

	return DirectFunctionCall2(to_tsvector_byid,
							   ObjectIdGetDatum(config_oid), arg1);
}

/* to_tsquery(text, text) */
Datum
tsa_to_tsquery_name(PG_FUNCTION_ARGS)
{
	text	   *cfgname = PG_GETARG_TEXT_PP(0);
	Datum		arg1 = PG_GETARG_DATUM(1);
	Oid			config_oid;

	config_oid = TextGetObjectId(regconfigin, cfgname);

	return DirectFunctionCall2(to_tsquery_byid,
							   ObjectIdGetDatum(config_oid), arg1);
}


/* plainto_tsquery(text, text) */
Datum
tsa_plainto_tsquery_name(PG_FUNCTION_ARGS)
{
	text	   *cfgname = PG_GETARG_TEXT_PP(0);
	Datum		arg1 = PG_GETARG_DATUM(1);
	Oid			config_oid;

	config_oid = TextGetObjectId(regconfigin, cfgname);

	return DirectFunctionCall2(plainto_tsquery_byid,
							   ObjectIdGetDatum(config_oid), arg1);
}

/* headline(text, text, tsquery [,text]) */
Datum
tsa_headline_byname(PG_FUNCTION_ARGS)
{
	Datum		arg0 = PG_GETARG_DATUM(0);
	Datum		arg1 = PG_GETARG_DATUM(1);
	Datum		arg2 = PG_GETARG_DATUM(2);
	Datum		result;
	Oid			config_oid;

	/* first parameter has to be converted to oid */
	config_oid = DatumGetObjectId(DirectFunctionCall1(regconfigin,
								CStringGetDatum(TextDatumGetCString(arg0))));

	if (PG_NARGS() == 3)
		result = DirectFunctionCall3(ts_headline_byid,
								   ObjectIdGetDatum(config_oid), arg1, arg2);
	else
	{
		Datum		arg3 = PG_GETARG_DATUM(3);

		result = DirectFunctionCall4(ts_headline_byid_opt,
									 ObjectIdGetDatum(config_oid),
									 arg1, arg2, arg3);
	}

	return result;
}

/*
 * tsearch2 version of update trigger
 *
 * We pass this on to the core trigger after inserting the default text
 * search configuration name as the second argument.  Note that this isn't
 * a complete implementation of the original functionality; tsearch2 allowed
 * transformation function names to be included in the list.  However, that
 * is deliberately removed as being a security risk.
 */
Datum
tsa_tsearch2(PG_FUNCTION_ARGS)
{
	TriggerData *trigdata;
	Trigger    *trigger;
	char	  **tgargs,
			  **tgargs_old;
	int			i;
	Datum		res;

	/* Check call context */
	if (!CALLED_AS_TRIGGER(fcinfo))		/* internal error */
		elog(ERROR, "tsvector_update_trigger: not fired by trigger manager");

	trigdata = (TriggerData *) fcinfo->context;
	trigger = trigdata->tg_trigger;

	if (trigger->tgnargs < 2)
		elog(ERROR, "TSearch: format tsearch2(tsvector_field, text_field1,...)");

	/* create space for configuration name */
	tgargs = (char **) palloc((trigger->tgnargs + 1) * sizeof(char *));
	tgargs[0] = trigger->tgargs[0];
	for (i = 1; i < trigger->tgnargs; i++)
		tgargs[i + 1] = trigger->tgargs[i];

	tgargs[1] = pstrdup(GetConfigOptionByName("default_text_search_config",
											  NULL));
	tgargs_old = trigger->tgargs;
	trigger->tgargs = tgargs;
	trigger->tgnargs++;

	res = tsvector_update_trigger_byid(fcinfo);

	/* restore old trigger data */
	trigger->tgargs = tgargs_old;
	trigger->tgnargs--;

	pfree(tgargs[1]);
	pfree(tgargs);

	return res;
}


Datum
tsa_rewrite_accum(PG_FUNCTION_ARGS)
{
	TSQuery		acc;
	ArrayType  *qa;
	TSQuery		q;
	QTNode	   *qex = NULL,
			   *subs = NULL,
			   *acctree = NULL;
	bool		isfind = false;
	Datum	   *elemsp;
	int			nelemsp;
	MemoryContext aggcontext;
	MemoryContext oldcontext;

	if (!AggCheckCallContext(fcinfo, &aggcontext))
		elog(ERROR, "tsa_rewrite_accum called in non-aggregate context");

	if (PG_ARGISNULL(0) || PG_GETARG_POINTER(0) == NULL)
	{
		acc = (TSQuery) MemoryContextAlloc(aggcontext, HDRSIZETQ);
		SET_VARSIZE(acc, HDRSIZETQ);
		acc->size = 0;
	}
	else
		acc = PG_GETARG_TSQUERY(0);

	if (PG_ARGISNULL(1) || PG_GETARG_POINTER(1) == NULL)
		PG_RETURN_TSQUERY(acc);
	else
		qa = PG_GETARG_ARRAYTYPE_P_COPY(1);

	if (ARR_NDIM(qa) != 1)
		elog(ERROR, "array must be one-dimensional, not %d dimensions",
			 ARR_NDIM(qa));
	if (ArrayGetNItems(ARR_NDIM(qa), ARR_DIMS(qa)) != 3)
		elog(ERROR, "array must have three elements");
	if (ARR_ELEMTYPE(qa) != TSQUERYOID)
		elog(ERROR, "array must contain tsquery elements");

	deconstruct_array(qa, TSQUERYOID, -1, false, 'i', &elemsp, NULL, &nelemsp);

	q = DatumGetTSQuery(elemsp[0]);
	if (q->size == 0)
	{
		pfree(elemsp);
		PG_RETURN_POINTER(acc);
	}

	if (!acc->size)
	{
		if (VARSIZE(acc) > HDRSIZETQ)
		{
			pfree(elemsp);
			PG_RETURN_POINTER(acc);
		}
		else
			acctree = QT2QTN(GETQUERY(q), GETOPERAND(q));
	}
	else
		acctree = QT2QTN(GETQUERY(acc), GETOPERAND(acc));

	QTNTernary(acctree);
	QTNSort(acctree);

	q = DatumGetTSQuery(elemsp[1]);
	if (q->size == 0)
	{
		pfree(elemsp);
		PG_RETURN_POINTER(acc);
	}
	qex = QT2QTN(GETQUERY(q), GETOPERAND(q));
	QTNTernary(qex);
	QTNSort(qex);

	q = DatumGetTSQuery(elemsp[2]);
	if (q->size)
		subs = QT2QTN(GETQUERY(q), GETOPERAND(q));

	acctree = findsubquery(acctree, qex, subs, &isfind);

	if (isfind || !acc->size)
	{
		/* pfree( acc ); do not pfree(p), because nodeAgg.c will */
		if (acctree)
		{
			QTNBinary(acctree);
			oldcontext = MemoryContextSwitchTo(aggcontext);
			acc = QTN2QT(acctree);
			MemoryContextSwitchTo(oldcontext);
		}
		else
		{
			acc = (TSQuery) MemoryContextAlloc(aggcontext, HDRSIZETQ);
			SET_VARSIZE(acc, HDRSIZETQ);
			acc->size = 0;
		}
	}

	pfree(elemsp);
	QTNFree(qex);
	QTNFree(subs);
	QTNFree(acctree);

	PG_RETURN_TSQUERY(acc);
}

Datum
tsa_rewrite_finish(PG_FUNCTION_ARGS)
{
	TSQuery		acc = PG_GETARG_TSQUERY(0);
	TSQuery		rewrited;

	if (acc == NULL || PG_ARGISNULL(0) || acc->size == 0)
	{
		rewrited = (TSQuery) palloc(HDRSIZETQ);
		SET_VARSIZE(rewrited, HDRSIZETQ);
		rewrited->size = 0;
	}
	else
	{
		rewrited = (TSQuery) palloc(VARSIZE(acc));
		memcpy(rewrited, acc, VARSIZE(acc));
		pfree(acc);
	}

	PG_RETURN_POINTER(rewrited);
}


/*
 * Get Oid of current dictionary
 */
static Oid
GetCurrentDict(void)
{
	if (current_dictionary_oid == InvalidOid)
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
				 errmsg("no current dictionary"),
				 errhint("Execute SELECT set_curdict(...).")));

	return current_dictionary_oid;
}

/*
 * Get Oid of current parser
 *
 * Here, it seems reasonable to select the "default" parser if none has been
 * set.
 */
static Oid
GetCurrentParser(void)
{
	if (current_parser_oid == InvalidOid)
		current_parser_oid = get_ts_parser_oid(stringToQualifiedNameList("pg_catalog.default"), false);
	return current_parser_oid;
}