PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
wparser.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * wparser.c
4  * Standard interface to word parser
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  *
8  *
9  * IDENTIFICATION
10  * src/backend/tsearch/wparser.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include "funcapi.h"
17 #include "catalog/namespace.h"
18 #include "catalog/pg_type.h"
19 #include "commands/defrem.h"
20 #include "tsearch/ts_cache.h"
21 #include "tsearch/ts_utils.h"
22 #include "utils/builtins.h"
23 #include "utils/varlena.h"
24 
25 
26 /******sql-level interface******/
27 
28 typedef struct
29 {
30  int cur;
33 
34 static void
36 {
37  TupleDesc tupdesc;
38  MemoryContext oldcontext;
41 
42  if (!OidIsValid(prs->lextypeOid))
43  elog(ERROR, "method lextype isn't defined for text search parser %u",
44  prsid);
45 
46  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
47 
49  st->cur = 0;
50  /* lextype takes one dummy argument */
52  (Datum) 0));
53  funcctx->user_fctx = (void *) st;
54 
55  tupdesc = CreateTemplateTupleDesc(3, false);
56  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "tokid",
57  INT4OID, -1, 0);
58  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "alias",
59  TEXTOID, -1, 0);
60  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "description",
61  TEXTOID, -1, 0);
62 
63  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
64  MemoryContextSwitchTo(oldcontext);
65 }
66 
67 static Datum
69 {
71 
72  st = (TSTokenTypeStorage *) funcctx->user_fctx;
73  if (st->list && st->list[st->cur].lexid)
74  {
75  Datum result;
76  char *values[3];
77  char txtid[16];
78  HeapTuple tuple;
79 
80  sprintf(txtid, "%d", st->list[st->cur].lexid);
81  values[0] = txtid;
82  values[1] = st->list[st->cur].alias;
83  values[2] = st->list[st->cur].descr;
84 
85  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
86  result = HeapTupleGetDatum(tuple);
87 
88  pfree(values[1]);
89  pfree(values[2]);
90  st->cur++;
91  return result;
92  }
93  if (st->list)
94  pfree(st->list);
95  pfree(st);
96  return (Datum) 0;
97 }
98 
99 Datum
101 {
102  FuncCallContext *funcctx;
103  Datum result;
104 
105  if (SRF_IS_FIRSTCALL())
106  {
107  funcctx = SRF_FIRSTCALL_INIT();
108  tt_setup_firstcall(funcctx, PG_GETARG_OID(0));
109  }
110 
111  funcctx = SRF_PERCALL_SETUP();
112 
113  if ((result = tt_process_call(funcctx)) != (Datum) 0)
114  SRF_RETURN_NEXT(funcctx, result);
115  SRF_RETURN_DONE(funcctx);
116 }
117 
118 Datum
120 {
121  FuncCallContext *funcctx;
122  Datum result;
123 
124  if (SRF_IS_FIRSTCALL())
125  {
126  text *prsname = PG_GETARG_TEXT_P(0);
127  Oid prsId;
128 
129  funcctx = SRF_FIRSTCALL_INIT();
130  prsId = get_ts_parser_oid(textToQualifiedNameList(prsname), false);
131  tt_setup_firstcall(funcctx, prsId);
132  }
133 
134  funcctx = SRF_PERCALL_SETUP();
135 
136  if ((result = tt_process_call(funcctx)) != (Datum) 0)
137  SRF_RETURN_NEXT(funcctx, result);
138  SRF_RETURN_DONE(funcctx);
139 }
140 
141 typedef struct
142 {
143  int type;
144  char *lexeme;
145 } LexemeEntry;
146 
147 typedef struct
148 {
149  int cur;
150  int len;
152 } PrsStorage;
153 
154 
155 static void
157 {
158  TupleDesc tupdesc;
159  MemoryContext oldcontext;
160  PrsStorage *st;
162  char *lex = NULL;
163  int llen = 0,
164  type = 0;
165  void *prsdata;
166 
167  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
168 
169  st = (PrsStorage *) palloc(sizeof(PrsStorage));
170  st->cur = 0;
171  st->len = 16;
172  st->list = (LexemeEntry *) palloc(sizeof(LexemeEntry) * st->len);
173 
174  prsdata = (void *) DatumGetPointer(FunctionCall2(&prs->prsstart,
175  PointerGetDatum(VARDATA(txt)),
176  Int32GetDatum(VARSIZE(txt) - VARHDRSZ)));
177 
178  while ((type = DatumGetInt32(FunctionCall3(&prs->prstoken,
179  PointerGetDatum(prsdata),
180  PointerGetDatum(&lex),
181  PointerGetDatum(&llen)))) != 0)
182  {
183  if (st->cur >= st->len)
184  {
185  st->len = 2 * st->len;
186  st->list = (LexemeEntry *) repalloc(st->list, sizeof(LexemeEntry) * st->len);
187  }
188  st->list[st->cur].lexeme = palloc(llen + 1);
189  memcpy(st->list[st->cur].lexeme, lex, llen);
190  st->list[st->cur].lexeme[llen] = '\0';
191  st->list[st->cur].type = type;
192  st->cur++;
193  }
194 
195  FunctionCall1(&prs->prsend, PointerGetDatum(prsdata));
196 
197  st->len = st->cur;
198  st->cur = 0;
199 
200  funcctx->user_fctx = (void *) st;
201  tupdesc = CreateTemplateTupleDesc(2, false);
202  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "tokid",
203  INT4OID, -1, 0);
204  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "token",
205  TEXTOID, -1, 0);
206 
207  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
208  MemoryContextSwitchTo(oldcontext);
209 }
210 
211 static Datum
213 {
214  PrsStorage *st;
215 
216  st = (PrsStorage *) funcctx->user_fctx;
217  if (st->cur < st->len)
218  {
219  Datum result;
220  char *values[2];
221  char tid[16];
222  HeapTuple tuple;
223 
224  values[0] = tid;
225  sprintf(tid, "%d", st->list[st->cur].type);
226  values[1] = st->list[st->cur].lexeme;
227  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
228  result = HeapTupleGetDatum(tuple);
229 
230  pfree(values[1]);
231  st->cur++;
232  return result;
233  }
234  else
235  {
236  if (st->list)
237  pfree(st->list);
238  pfree(st);
239  }
240  return (Datum) 0;
241 }
242 
243 Datum
245 {
246  FuncCallContext *funcctx;
247  Datum result;
248 
249  if (SRF_IS_FIRSTCALL())
250  {
251  text *txt = PG_GETARG_TEXT_P(1);
252 
253  funcctx = SRF_FIRSTCALL_INIT();
254  prs_setup_firstcall(funcctx, PG_GETARG_OID(0), txt);
255  PG_FREE_IF_COPY(txt, 1);
256  }
257 
258  funcctx = SRF_PERCALL_SETUP();
259 
260  if ((result = prs_process_call(funcctx)) != (Datum) 0)
261  SRF_RETURN_NEXT(funcctx, result);
262  SRF_RETURN_DONE(funcctx);
263 }
264 
265 Datum
267 {
268  FuncCallContext *funcctx;
269  Datum result;
270 
271  if (SRF_IS_FIRSTCALL())
272  {
273  text *prsname = PG_GETARG_TEXT_P(0);
274  text *txt = PG_GETARG_TEXT_P(1);
275  Oid prsId;
276 
277  funcctx = SRF_FIRSTCALL_INIT();
278  prsId = get_ts_parser_oid(textToQualifiedNameList(prsname), false);
279  prs_setup_firstcall(funcctx, prsId, txt);
280  }
281 
282  funcctx = SRF_PERCALL_SETUP();
283 
284  if ((result = prs_process_call(funcctx)) != (Datum) 0)
285  SRF_RETURN_NEXT(funcctx, result);
286  SRF_RETURN_DONE(funcctx);
287 }
288 
289 Datum
291 {
292  text *in = PG_GETARG_TEXT_P(1);
293  TSQuery query = PG_GETARG_TSQUERY(2);
294  text *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_P(3) : NULL;
295  HeadlineParsedText prs;
296  List *prsoptions;
297  text *out;
298  TSConfigCacheEntry *cfg;
299  TSParserCacheEntry *prsobj;
300 
302  prsobj = lookup_ts_parser_cache(cfg->prsId);
303 
304  if (!OidIsValid(prsobj->headlineOid))
305  ereport(ERROR,
306  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
307  errmsg("text search parser does not support headline creation")));
308 
309  memset(&prs, 0, sizeof(HeadlineParsedText));
310  prs.lenwords = 32;
311  prs.words = (HeadlineWordEntry *) palloc(sizeof(HeadlineWordEntry) * prs.lenwords);
312 
313  hlparsetext(cfg->cfgId, &prs, query, VARDATA(in), VARSIZE(in) - VARHDRSZ);
314 
315  if (opt)
316  prsoptions = deserialize_deflist(PointerGetDatum(opt));
317  else
318  prsoptions = NIL;
319 
320  FunctionCall3(&(prsobj->prsheadline),
321  PointerGetDatum(&prs),
322  PointerGetDatum(prsoptions),
323  PointerGetDatum(query));
324 
325  out = generateHeadline(&prs);
326 
327  PG_FREE_IF_COPY(in, 1);
328  PG_FREE_IF_COPY(query, 2);
329  if (opt)
330  PG_FREE_IF_COPY(opt, 3);
331  pfree(prs.words);
332  pfree(prs.startsel);
333  pfree(prs.stopsel);
334 
335  PG_RETURN_POINTER(out);
336 }
337 
338 Datum
340 {
342  PG_GETARG_DATUM(0),
343  PG_GETARG_DATUM(1),
344  PG_GETARG_DATUM(2)));
345 }
346 
347 Datum
349 {
352  PG_GETARG_DATUM(0),
353  PG_GETARG_DATUM(1)));
354 }
355 
356 Datum
358 {
361  PG_GETARG_DATUM(0),
362  PG_GETARG_DATUM(1),
363  PG_GETARG_DATUM(2)));
364 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define NIL
Definition: pg_list.h:69
static Datum prs_process_call(FuncCallContext *funcctx)
Definition: wparser.c:212
#define VARDATA(PTR)
Definition: postgres.h:305
int type
Definition: wparser.c:143
Datum ts_headline_byid(PG_FUNCTION_ARGS)
Definition: wparser.c:339
LexDescr * list
Definition: wparser.c:31
#define DatumGetInt32(X)
Definition: postgres.h:480
char * alias
Definition: ts_public.h:28
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
LexemeEntry * list
Definition: wparser.c:151
#define TEXTOID
Definition: pg_type.h:324
#define VARSIZE(PTR)
Definition: postgres.h:306
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:564
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define VARHDRSZ
Definition: c.h:441
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
List * deserialize_deflist(Datum txt)
Definition: tsearchcmds.c:1562
int len
Definition: wparser.c:150
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define FunctionCall2(flinfo, arg1, arg2)
Definition: fmgr.h:575
#define INT4OID
Definition: pg_type.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
Datum ts_token_type_byid(PG_FUNCTION_ARGS)
Definition: wparser.c:100
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
TSParserCacheEntry * lookup_ts_parser_cache(Oid prsId)
Definition: ts_cache.c:112
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1115
#define FunctionCall3(flinfo, arg1, arg2, arg3)
Definition: fmgr.h:577
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
Datum ts_parse_byid(PG_FUNCTION_ARGS)
Definition: wparser.c:244
void pfree(void *pointer)
Definition: mcxt.c:992
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
FmgrInfo prsend
Definition: ts_cache.h:47
Datum ts_headline_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:290
Datum ts_headline_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:357
TSConfigCacheEntry * lookup_ts_config_cache(Oid cfgId)
Definition: ts_cache.c:385
Datum ts_parse_byname(PG_FUNCTION_ARGS)
Definition: wparser.c:266
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:593
Datum ts_token_type_byname(PG_FUNCTION_ARGS)
Definition: wparser.c:119
static Datum tt_process_call(FuncCallContext *funcctx)
Definition: wparser.c:68
Datum ts_headline(PG_FUNCTION_ARGS)
Definition: wparser.c:348
#define DirectFunctionCall4(func, arg1, arg2, arg3, arg4)
Definition: fmgr.h:561
HeadlineWordEntry * words
Definition: ts_public.h:52
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
AttInMetadata * attinmeta
Definition: funcapi.h:99
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:493
#define ereport(elevel, rest)
Definition: elog.h:122
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3071
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:559
char * lexeme
Definition: wparser.c:144
char * descr
Definition: ts_public.h:29
int lexid
Definition: ts_public.h:27
int cur
Definition: wparser.c:149
static void tt_setup_firstcall(FuncCallContext *funcctx, Oid prsid)
Definition: wparser.c:35
uintptr_t Datum
Definition: postgres.h:374
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1068
text * generateHeadline(HeadlineParsedText *prs)
Definition: ts_parse.c:598
#define NULL
Definition: c.h:226
Oid get_ts_parser_oid(List *names, bool missing_ok)
Definition: namespace.c:2094
FmgrInfo prstoken
Definition: ts_cache.h:46
static void prs_setup_firstcall(FuncCallContext *funcctx, Oid prsid, text *txt)
Definition: wparser.c:156
void hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query, char *buf, int buflen)
Definition: ts_parse.c:533
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define PG_NARGS()
Definition: fmgr.h:160
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
#define DatumGetPointer(X)
Definition: postgres.h:557
static Datum values[MAXATTR]
Definition: bootstrap.c:162
FmgrInfo prsstart
Definition: ts_cache.h:45
#define Int32GetDatum(X)
Definition: postgres.h:487
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:573
Definition: c.h:435
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
FmgrInfo prsheadline
Definition: ts_cache.h:48