PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
wparser.c File Reference
#include "postgres.h"
#include "funcapi.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "tsearch/ts_cache.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
#include "utils/jsonapi.h"
#include "utils/varlena.h"
Include dependency graph for wparser.c:

Go to the source code of this file.

Data Structures

struct  TSTokenTypeStorage
 
struct  HeadlineJsonState
 
struct  LexemeEntry
 
struct  PrsStorage
 

Typedefs

typedef struct HeadlineJsonState HeadlineJsonState
 

Functions

static textheadline_json_value (void *_state, char *elem_value, int elem_len)
 
static void tt_setup_firstcall (FuncCallContext *funcctx, Oid prsid)
 
static Datum tt_process_call (FuncCallContext *funcctx)
 
Datum ts_token_type_byid (PG_FUNCTION_ARGS)
 
Datum ts_token_type_byname (PG_FUNCTION_ARGS)
 
static void prs_setup_firstcall (FuncCallContext *funcctx, Oid prsid, text *txt)
 
static Datum prs_process_call (FuncCallContext *funcctx)
 
Datum ts_parse_byid (PG_FUNCTION_ARGS)
 
Datum ts_parse_byname (PG_FUNCTION_ARGS)
 
Datum ts_headline_byid_opt (PG_FUNCTION_ARGS)
 
Datum ts_headline_byid (PG_FUNCTION_ARGS)
 
Datum ts_headline (PG_FUNCTION_ARGS)
 
Datum ts_headline_opt (PG_FUNCTION_ARGS)
 
Datum ts_headline_jsonb_byid_opt (PG_FUNCTION_ARGS)
 
Datum ts_headline_jsonb (PG_FUNCTION_ARGS)
 
Datum ts_headline_jsonb_byid (PG_FUNCTION_ARGS)
 
Datum ts_headline_jsonb_opt (PG_FUNCTION_ARGS)
 
Datum ts_headline_json_byid_opt (PG_FUNCTION_ARGS)
 
Datum ts_headline_json (PG_FUNCTION_ARGS)
 
Datum ts_headline_json_byid (PG_FUNCTION_ARGS)
 
Datum ts_headline_json_opt (PG_FUNCTION_ARGS)
 

Typedef Documentation

Function Documentation

static text * headline_json_value ( void *  _state,
char *  elem_value,
int  elem_len 
)
static

Definition at line 539 of file wparser.c.

References HeadlineJsonState::cfg, TSConfigCacheEntry::cfgId, HeadlineParsedText::curwords, FunctionCall3, generateHeadline(), hlparsetext(), PointerGetDatum, HeadlineJsonState::prs, TSParserCacheEntry::prsheadline, HeadlineJsonState::prsobj, HeadlineJsonState::prsoptions, HeadlineJsonState::query, and HeadlineJsonState::transformed.

Referenced by ts_headline_json_byid_opt(), and ts_headline_jsonb_byid_opt().

540 {
542 
543  HeadlineParsedText *prs = state->prs;
544  TSConfigCacheEntry *cfg = state->cfg;
545  TSParserCacheEntry *prsobj = state->prsobj;
546  TSQuery query = state->query;
547  List *prsoptions = state->prsoptions;
548 
549  prs->curwords = 0;
550  hlparsetext(cfg->cfgId, prs, query, elem_value, elem_len);
551  FunctionCall3(&(prsobj->prsheadline),
552  PointerGetDatum(prs),
553  PointerGetDatum(prsoptions),
554  PointerGetDatum(query));
555 
556  state->transformed = true;
557  return generateHeadline(prs);
558 }
TSParserCacheEntry * prsobj
Definition: wparser.c:40
#define PointerGetDatum(X)
Definition: postgres.h:562
List * prsoptions
Definition: wparser.c:42
#define FunctionCall3(flinfo, arg1, arg2, arg3)
Definition: fmgr.h:606
bool transformed
Definition: wparser.c:43
text * generateHeadline(HeadlineParsedText *prs)
Definition: ts_parse.c:598
TSQuery query
Definition: wparser.c:41
Definition: regguts.h:298
void hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query, char *buf, int buflen)
Definition: ts_parse.c:533
TSConfigCacheEntry * cfg
Definition: wparser.c:39
Definition: pg_list.h:45
HeadlineParsedText * prs
Definition: wparser.c:38
FmgrInfo prsheadline
Definition: ts_cache.h:48
static Datum prs_process_call ( FuncCallContext funcctx)
static

Definition at line 226 of file wparser.c.

References FuncCallContext::attinmeta, BuildTupleFromCStrings(), PrsStorage::cur, HeapTupleGetDatum, PrsStorage::len, LexemeEntry::lexeme, PrsStorage::list, pfree(), result, LexemeEntry::type, FuncCallContext::user_fctx, and values.

Referenced by ts_parse_byid(), and ts_parse_byname().

227 {
228  PrsStorage *st;
229 
230  st = (PrsStorage *) funcctx->user_fctx;
231  if (st->cur < st->len)
232  {
233  Datum result;
234  char *values[2];
235  char tid[16];
236  HeapTuple tuple;
237 
238  values[0] = tid;
239  sprintf(tid, "%d", st->list[st->cur].type);
240  values[1] = st->list[st->cur].lexeme;
241  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
242  result = HeapTupleGetDatum(tuple);
243 
244  pfree(values[1]);
245  st->cur++;
246  return result;
247  }
248  else
249  {
250  if (st->list)
251  pfree(st->list);
252  pfree(st);
253  }
254  return (Datum) 0;
255 }
int type
Definition: wparser.c:157
LexemeEntry * list
Definition: wparser.c:165
int len
Definition: wparser.c:164
return result
Definition: formatting.c:1633
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1118
void pfree(void *pointer)
Definition: mcxt.c:950
AttInMetadata * attinmeta
Definition: funcapi.h:99
char * lexeme
Definition: wparser.c:158
int cur
Definition: wparser.c:163
uintptr_t Datum
Definition: postgres.h:372
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
static Datum values[MAXATTR]
Definition: bootstrap.c:163
void * user_fctx
Definition: funcapi.h:90
static void prs_setup_firstcall ( FuncCallContext funcctx,
Oid  prsid,
text txt 
)
static

Definition at line 170 of file wparser.c.

References FuncCallContext::attinmeta, CreateTemplateTupleDesc(), PrsStorage::cur, DatumGetInt32, DatumGetPointer, FunctionCall1, FunctionCall2, FunctionCall3, Int32GetDatum, INT4OID, PrsStorage::len, LexemeEntry::lexeme, PrsStorage::list, lookup_ts_parser_cache(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, NULL, palloc(), PointerGetDatum, TSParserCacheEntry::prsend, TSParserCacheEntry::prsstart, TSParserCacheEntry::prstoken, repalloc(), TEXTOID, TupleDescGetAttInMetadata(), TupleDescInitEntry(), LexemeEntry::type, FuncCallContext::user_fctx, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by ts_parse_byid(), and ts_parse_byname().

171 {
172  TupleDesc tupdesc;
173  MemoryContext oldcontext;
174  PrsStorage *st;
176  char *lex = NULL;
177  int llen = 0,
178  type = 0;
179  void *prsdata;
180 
181  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
182 
183  st = (PrsStorage *) palloc(sizeof(PrsStorage));
184  st->cur = 0;
185  st->len = 16;
186  st->list = (LexemeEntry *) palloc(sizeof(LexemeEntry) * st->len);
187 
188  prsdata = (void *) DatumGetPointer(FunctionCall2(&prs->prsstart,
191 
192  while ((type = DatumGetInt32(FunctionCall3(&prs->prstoken,
193  PointerGetDatum(prsdata),
194  PointerGetDatum(&lex),
195  PointerGetDatum(&llen)))) != 0)
196  {
197  if (st->cur >= st->len)
198  {
199  st->len = 2 * st->len;
200  st->list = (LexemeEntry *) repalloc(st->list, sizeof(LexemeEntry) * st->len);
201  }
202  st->list[st->cur].lexeme = palloc(llen + 1);
203  memcpy(st->list[st->cur].lexeme, lex, llen);
204  st->list[st->cur].lexeme[llen] = '\0';
205  st->list[st->cur].type = type;
206  st->cur++;
207  }
208 
209  FunctionCall1(&prs->prsend, PointerGetDatum(prsdata));
210 
211  st->len = st->cur;
212  st->cur = 0;
213 
214  funcctx->user_fctx = (void *) st;
215  tupdesc = CreateTemplateTupleDesc(2, false);
216  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "tokid",
217  INT4OID, -1, 0);
218  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "token",
219  TEXTOID, -1, 0);
220 
221  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
222  MemoryContextSwitchTo(oldcontext);
223 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
int type
Definition: wparser.c:157
#define DatumGetInt32(X)
Definition: postgres.h:478
LexemeEntry * list
Definition: wparser.c:165
#define TEXTOID
Definition: pg_type.h:324
#define PointerGetDatum(X)
Definition: postgres.h:562
int len
Definition: wparser.c:164
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define FunctionCall2(flinfo, arg1, arg2)
Definition: fmgr.h:604
#define INT4OID
Definition: pg_type.h:316
TSParserCacheEntry * lookup_ts_parser_cache(Oid prsId)
Definition: ts_cache.c:112
#define FunctionCall3(flinfo, arg1, arg2, arg3)
Definition: fmgr.h:606
FmgrInfo prsend
Definition: ts_cache.h:47
AttInMetadata * attinmeta
Definition: funcapi.h:99
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:462
char * lexeme
Definition: wparser.c:158
int cur
Definition: wparser.c:163
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1069
#define NULL
Definition: c.h:229
FmgrInfo prstoken
Definition: ts_cache.h:46
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963
#define DatumGetPointer(X)
Definition: postgres.h:555
FmgrInfo prsstart
Definition: ts_cache.h:45
#define Int32GetDatum(X)
Definition: postgres.h:485
void * user_fctx
Definition: funcapi.h:90
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:602
int16 AttrNumber
Definition: attnum.h:21
Datum ts_headline ( PG_FUNCTION_ARGS  )

Definition at line 364 of file wparser.c.

References DirectFunctionCall3, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_DATUM, PG_RETURN_DATUM, and ts_headline_byid_opt().

365 {
368  PG_GETARG_DATUM(0),
369  PG_GETARG_DATUM(1)));
370 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
Datum ts_headline_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:304
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:588
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum ts_headline_byid ( PG_FUNCTION_ARGS  )

Definition at line 355 of file wparser.c.

References DirectFunctionCall3, PG_GETARG_DATUM, PG_RETURN_DATUM, and ts_headline_byid_opt().

356 {
358  PG_GETARG_DATUM(0),
359  PG_GETARG_DATUM(1),
360  PG_GETARG_DATUM(2)));
361 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
Datum ts_headline_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:304
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:588
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum ts_headline_byid_opt ( PG_FUNCTION_ARGS  )

Definition at line 304 of file wparser.c.

References TSConfigCacheEntry::cfgId, deserialize_deflist(), ereport, errcode(), errmsg(), ERROR, FunctionCall3, generateHeadline(), TSParserCacheEntry::headlineOid, hlparsetext(), HeadlineParsedText::lenwords, lookup_ts_config_cache(), lookup_ts_parser_cache(), NIL, NULL, OidIsValid, palloc(), pfree(), PG_FREE_IF_COPY, PG_GETARG_OID, PG_GETARG_POINTER, PG_GETARG_TEXT_PP, PG_GETARG_TSQUERY, PG_NARGS, PG_RETURN_POINTER, PointerGetDatum, TSParserCacheEntry::prsheadline, TSConfigCacheEntry::prsId, HeadlineParsedText::startsel, HeadlineParsedText::stopsel, VARDATA_ANY, VARSIZE_ANY_EXHDR, and HeadlineParsedText::words.

Referenced by ts_headline(), ts_headline_byid(), and ts_headline_opt().

305 {
306  Oid tsconfig = PG_GETARG_OID(0);
307  text *in = PG_GETARG_TEXT_PP(1);
308  TSQuery query = PG_GETARG_TSQUERY(2);
309  text *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_PP(3) : NULL;
310  HeadlineParsedText prs;
311  List *prsoptions;
312  text *out;
313  TSConfigCacheEntry *cfg;
314  TSParserCacheEntry *prsobj;
315 
316  cfg = lookup_ts_config_cache(tsconfig);
317  prsobj = lookup_ts_parser_cache(cfg->prsId);
318 
319  if (!OidIsValid(prsobj->headlineOid))
320  ereport(ERROR,
321  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
322  errmsg("text search parser does not support headline creation")));
323 
324  memset(&prs, 0, sizeof(HeadlineParsedText));
325  prs.lenwords = 32;
326  prs.words = (HeadlineWordEntry *) palloc(sizeof(HeadlineWordEntry) * prs.lenwords);
327 
328  hlparsetext(cfg->cfgId, &prs, query,
329  VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
330 
331  if (opt)
332  prsoptions = deserialize_deflist(PointerGetDatum(opt));
333  else
334  prsoptions = NIL;
335 
336  FunctionCall3(&(prsobj->prsheadline),
337  PointerGetDatum(&prs),
338  PointerGetDatum(prsoptions),
339  PointerGetDatum(query));
340 
341  out = generateHeadline(&prs);
342 
343  PG_FREE_IF_COPY(in, 1);
344  PG_FREE_IF_COPY(query, 2);
345  if (opt)
346  PG_FREE_IF_COPY(opt, 3);
347  pfree(prs.words);
348  pfree(prs.startsel);
349  pfree(prs.stopsel);
350 
351  PG_RETURN_POINTER(out);
352 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define NIL
Definition: pg_list.h:69
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
List * deserialize_deflist(Datum txt)
Definition: tsearchcmds.c:1562
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
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:538
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define FunctionCall3(flinfo, arg1, arg2, arg3)
Definition: fmgr.h:606
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
TSConfigCacheEntry * lookup_ts_config_cache(Oid cfgId)
Definition: ts_cache.c:385
HeadlineWordEntry * words
Definition: ts_public.h:52
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define ereport(elevel, rest)
Definition: elog.h:122
text * generateHeadline(HeadlineParsedText *prs)
Definition: ts_parse.c:598
#define NULL
Definition: c.h:229
void hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query, char *buf, int buflen)
Definition: ts_parse.c:533
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
Definition: pg_list.h:45
FmgrInfo prsheadline
Definition: ts_cache.h:48
Datum ts_headline_json ( PG_FUNCTION_ARGS  )

Definition at line 507 of file wparser.c.

References DirectFunctionCall3, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_DATUM, PG_RETURN_DATUM, and ts_headline_json_byid_opt().

508 {
511  PG_GETARG_DATUM(0),
512  PG_GETARG_DATUM(1)));
513 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
Datum ts_headline_json_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:459
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:588
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum ts_headline_json_byid ( PG_FUNCTION_ARGS  )

Definition at line 516 of file wparser.c.

References DirectFunctionCall3, PG_GETARG_DATUM, PG_RETURN_DATUM, and ts_headline_json_byid_opt().

517 {
519  PG_GETARG_DATUM(0),
520  PG_GETARG_DATUM(1),
521  PG_GETARG_DATUM(2)));
522 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
Datum ts_headline_json_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:459
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:588
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum ts_headline_json_byid_opt ( PG_FUNCTION_ARGS  )

Definition at line 459 of file wparser.c.

References HeadlineJsonState::cfg, deserialize_deflist(), ereport, errcode(), errmsg(), ERROR, headline_json_value(), TSParserCacheEntry::headlineOid, HeadlineParsedText::lenwords, lookup_ts_config_cache(), lookup_ts_parser_cache(), NIL, NULL, OidIsValid, palloc(), palloc0(), pfree(), PG_FREE_IF_COPY, PG_GETARG_OID, PG_GETARG_POINTER, PG_GETARG_TEXT_P, PG_GETARG_TSQUERY, PG_NARGS, PG_RETURN_TEXT_P, PointerGetDatum, HeadlineJsonState::prs, TSConfigCacheEntry::prsId, HeadlineJsonState::prsobj, HeadlineJsonState::prsoptions, HeadlineJsonState::query, HeadlineParsedText::startsel, HeadlineParsedText::stopsel, transform_json_string_values(), HeadlineJsonState::transformed, and HeadlineParsedText::words.

Referenced by ts_headline_json(), ts_headline_json_byid(), and ts_headline_json_opt().

460 {
461  Oid tsconfig = PG_GETARG_OID(0);
462  text *json = PG_GETARG_TEXT_P(1);
463  TSQuery query = PG_GETARG_TSQUERY(2);
464  text *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_P(3) : NULL;
465  text *out;
467 
468  HeadlineParsedText prs;
470 
471  memset(&prs, 0, sizeof(HeadlineParsedText));
472  prs.lenwords = 32;
473  prs.words = (HeadlineWordEntry *) palloc(sizeof(HeadlineWordEntry) * prs.lenwords);
474 
475  state->prs = &prs;
476  state->cfg = lookup_ts_config_cache(tsconfig);
477  state->prsobj = lookup_ts_parser_cache(state->cfg->prsId);
478  state->query = query;
479  if (opt)
481  else
482  state->prsoptions = NIL;
483 
484  if (!OidIsValid(state->prsobj->headlineOid))
485  ereport(ERROR,
486  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
487  errmsg("text search parser does not support headline creation")));
488 
489  out = transform_json_string_values(json, state, action);
490 
491  PG_FREE_IF_COPY(json, 1);
492  PG_FREE_IF_COPY(query, 2);
493  if (opt)
494  PG_FREE_IF_COPY(opt, 3);
495  pfree(prs.words);
496 
497  if (state->transformed)
498  {
499  pfree(prs.startsel);
500  pfree(prs.stopsel);
501  }
502 
503  PG_RETURN_TEXT_P(out);
504 }
#define NIL
Definition: pg_list.h:69
TSParserCacheEntry * prsobj
Definition: wparser.c:40
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
List * deserialize_deflist(Datum txt)
Definition: tsearchcmds.c:1562
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
TSParserCacheEntry * lookup_ts_parser_cache(Oid prsId)
Definition: ts_cache.c:112
List * prsoptions
Definition: wparser.c:42
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
text * transform_json_string_values(text *json, void *action_state, JsonTransformStringValuesAction transform_action)
Definition: jsonfuncs.c:4918
TSConfigCacheEntry * lookup_ts_config_cache(Oid cfgId)
Definition: ts_cache.c:385
HeadlineWordEntry * words
Definition: ts_public.h:52
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
bool transformed
Definition: wparser.c:43
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:878
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
TSQuery query
Definition: wparser.c:41
#define NULL
Definition: c.h:229
Definition: regguts.h:298
text *(* JsonTransformStringValuesAction)(void *state, char *elem_value, int elem_len)
Definition: jsonapi.h:139
static text * headline_json_value(void *_state, char *elem_value, int elem_len)
Definition: wparser.c:539
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
TSConfigCacheEntry * cfg
Definition: wparser.c:39
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:300
Definition: c.h:439
HeadlineParsedText * prs
Definition: wparser.c:38
Datum ts_headline_json_opt ( PG_FUNCTION_ARGS  )

Definition at line 525 of file wparser.c.

References DirectFunctionCall4, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_DATUM, PG_RETURN_DATUM, and ts_headline_json_byid_opt().

526 {
529  PG_GETARG_DATUM(0),
530  PG_GETARG_DATUM(1),
531  PG_GETARG_DATUM(2)));
532 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define DirectFunctionCall4(func, arg1, arg2, arg3, arg4)
Definition: fmgr.h:590
Datum ts_headline_json_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:459
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum ts_headline_jsonb ( PG_FUNCTION_ARGS  )

Definition at line 431 of file wparser.c.

References DirectFunctionCall3, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_DATUM, PG_RETURN_DATUM, and ts_headline_jsonb_byid_opt().

432 {
435  PG_GETARG_DATUM(0),
436  PG_GETARG_DATUM(1)));
437 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:588
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum ts_headline_jsonb_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:383
Datum ts_headline_jsonb_byid ( PG_FUNCTION_ARGS  )

Definition at line 440 of file wparser.c.

References DirectFunctionCall3, PG_GETARG_DATUM, PG_RETURN_DATUM, and ts_headline_jsonb_byid_opt().

441 {
443  PG_GETARG_DATUM(0),
444  PG_GETARG_DATUM(1),
445  PG_GETARG_DATUM(2)));
446 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:588
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum ts_headline_jsonb_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:383
Datum ts_headline_jsonb_byid_opt ( PG_FUNCTION_ARGS  )

Definition at line 383 of file wparser.c.

References HeadlineJsonState::cfg, deserialize_deflist(), ereport, errcode(), errmsg(), ERROR, headline_json_value(), TSParserCacheEntry::headlineOid, HeadlineParsedText::lenwords, lookup_ts_config_cache(), lookup_ts_parser_cache(), NIL, NULL, OidIsValid, palloc(), palloc0(), pfree(), PG_FREE_IF_COPY, PG_GETARG_JSONB, PG_GETARG_OID, PG_GETARG_POINTER, PG_GETARG_TEXT_P, PG_GETARG_TSQUERY, PG_NARGS, PG_RETURN_JSONB, PointerGetDatum, HeadlineJsonState::prs, TSConfigCacheEntry::prsId, HeadlineJsonState::prsobj, HeadlineJsonState::prsoptions, HeadlineJsonState::query, HeadlineParsedText::startsel, HeadlineParsedText::stopsel, transform_jsonb_string_values(), HeadlineJsonState::transformed, and HeadlineParsedText::words.

Referenced by ts_headline_jsonb(), ts_headline_jsonb_byid(), and ts_headline_jsonb_opt().

384 {
385  Oid tsconfig = PG_GETARG_OID(0);
386  Jsonb *jb = PG_GETARG_JSONB(1);
387  TSQuery query = PG_GETARG_TSQUERY(2);
388  text *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_P(3) : NULL;
389  Jsonb *out;
391  HeadlineParsedText prs;
393 
394  memset(&prs, 0, sizeof(HeadlineParsedText));
395  prs.lenwords = 32;
396  prs.words = (HeadlineWordEntry *) palloc(sizeof(HeadlineWordEntry) * prs.lenwords);
397 
398  state->prs = &prs;
399  state->cfg = lookup_ts_config_cache(tsconfig);
400  state->prsobj = lookup_ts_parser_cache(state->cfg->prsId);
401  state->query = query;
402  if (opt)
404  else
405  state->prsoptions = NIL;
406 
407  if (!OidIsValid(state->prsobj->headlineOid))
408  ereport(ERROR,
409  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
410  errmsg("text search parser does not support headline creation")));
411 
412  out = transform_jsonb_string_values(jb, state, action);
413 
414  PG_FREE_IF_COPY(jb, 1);
415  PG_FREE_IF_COPY(query, 2);
416  if (opt)
417  PG_FREE_IF_COPY(opt, 3);
418 
419  pfree(prs.words);
420 
421  if (state->transformed)
422  {
423  pfree(prs.startsel);
424  pfree(prs.stopsel);
425  }
426 
427  PG_RETURN_JSONB(out);
428 }
#define NIL
Definition: pg_list.h:69
Definition: jsonb.h:215
TSParserCacheEntry * prsobj
Definition: wparser.c:40
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
List * deserialize_deflist(Datum txt)
Definition: tsearchcmds.c:1562
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
TSParserCacheEntry * lookup_ts_parser_cache(Oid prsId)
Definition: ts_cache.c:112
List * prsoptions
Definition: wparser.c:42
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
#define PG_RETURN_JSONB(x)
Definition: jsonb.h:71
TSConfigCacheEntry * lookup_ts_config_cache(Oid cfgId)
Definition: ts_cache.c:385
Jsonb * transform_jsonb_string_values(Jsonb *jsonb, void *action_state, JsonTransformStringValuesAction transform_action)
Definition: jsonfuncs.c:4873
HeadlineWordEntry * words
Definition: ts_public.h:52
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
bool transformed
Definition: wparser.c:43
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:878
TSQuery query
Definition: wparser.c:41
#define NULL
Definition: c.h:229
Definition: regguts.h:298
text *(* JsonTransformStringValuesAction)(void *state, char *elem_value, int elem_len)
Definition: jsonapi.h:139
static text * headline_json_value(void *_state, char *elem_value, int elem_len)
Definition: wparser.c:539
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
TSConfigCacheEntry * cfg
Definition: wparser.c:39
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:300
Definition: c.h:439
HeadlineParsedText * prs
Definition: wparser.c:38
Datum ts_headline_jsonb_opt ( PG_FUNCTION_ARGS  )

Definition at line 449 of file wparser.c.

References DirectFunctionCall4, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_DATUM, PG_RETURN_DATUM, and ts_headline_jsonb_byid_opt().

450 {
453  PG_GETARG_DATUM(0),
454  PG_GETARG_DATUM(1),
455  PG_GETARG_DATUM(2)));
456 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define DirectFunctionCall4(func, arg1, arg2, arg3, arg4)
Definition: fmgr.h:590
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum ts_headline_jsonb_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:383
Datum ts_headline_opt ( PG_FUNCTION_ARGS  )

Definition at line 373 of file wparser.c.

References DirectFunctionCall4, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_DATUM, PG_RETURN_DATUM, and ts_headline_byid_opt().

374 {
377  PG_GETARG_DATUM(0),
378  PG_GETARG_DATUM(1),
379  PG_GETARG_DATUM(2)));
380 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
Datum ts_headline_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:304
#define DirectFunctionCall4(func, arg1, arg2, arg3, arg4)
Definition: fmgr.h:590
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum ts_parse_byid ( PG_FUNCTION_ARGS  )

Definition at line 258 of file wparser.c.

References PG_FREE_IF_COPY, PG_GETARG_OID, PG_GETARG_TEXT_PP, prs_process_call(), prs_setup_firstcall(), result, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, and SRF_RETURN_NEXT.

259 {
260  FuncCallContext *funcctx;
261  Datum result;
262 
263  if (SRF_IS_FIRSTCALL())
264  {
265  text *txt = PG_GETARG_TEXT_PP(1);
266 
267  funcctx = SRF_FIRSTCALL_INIT();
268  prs_setup_firstcall(funcctx, PG_GETARG_OID(0), txt);
269  PG_FREE_IF_COPY(txt, 1);
270  }
271 
272  funcctx = SRF_PERCALL_SETUP();
273 
274  if ((result = prs_process_call(funcctx)) != (Datum) 0)
275  SRF_RETURN_NEXT(funcctx, result);
276  SRF_RETURN_DONE(funcctx);
277 }
static Datum prs_process_call(FuncCallContext *funcctx)
Definition: wparser.c:226
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
return result
Definition: formatting.c:1633
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
uintptr_t Datum
Definition: postgres.h:372
static void prs_setup_firstcall(FuncCallContext *funcctx, Oid prsid, text *txt)
Definition: wparser.c:170
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum ts_parse_byname ( PG_FUNCTION_ARGS  )

Definition at line 280 of file wparser.c.

References get_ts_parser_oid(), PG_GETARG_TEXT_PP, prs_process_call(), prs_setup_firstcall(), result, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, and textToQualifiedNameList().

281 {
282  FuncCallContext *funcctx;
283  Datum result;
284 
285  if (SRF_IS_FIRSTCALL())
286  {
287  text *prsname = PG_GETARG_TEXT_PP(0);
288  text *txt = PG_GETARG_TEXT_PP(1);
289  Oid prsId;
290 
291  funcctx = SRF_FIRSTCALL_INIT();
292  prsId = get_ts_parser_oid(textToQualifiedNameList(prsname), false);
293  prs_setup_firstcall(funcctx, prsId, txt);
294  }
295 
296  funcctx = SRF_PERCALL_SETUP();
297 
298  if ((result = prs_process_call(funcctx)) != (Datum) 0)
299  SRF_RETURN_NEXT(funcctx, result);
300  SRF_RETURN_DONE(funcctx);
301 }
static Datum prs_process_call(FuncCallContext *funcctx)
Definition: wparser.c:226
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
return result
Definition: formatting.c:1633
unsigned int Oid
Definition: postgres_ext.h:31
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3202
uintptr_t Datum
Definition: postgres.h:372
Oid get_ts_parser_oid(List *names, bool missing_ok)
Definition: namespace.c:2260
static void prs_setup_firstcall(FuncCallContext *funcctx, Oid prsid, text *txt)
Definition: wparser.c:170
Definition: c.h:439
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum ts_token_type_byid ( PG_FUNCTION_ARGS  )

Definition at line 114 of file wparser.c.

References PG_GETARG_OID, result, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, tt_process_call(), and tt_setup_firstcall().

115 {
116  FuncCallContext *funcctx;
117  Datum result;
118 
119  if (SRF_IS_FIRSTCALL())
120  {
121  funcctx = SRF_FIRSTCALL_INIT();
122  tt_setup_firstcall(funcctx, PG_GETARG_OID(0));
123  }
124 
125  funcctx = SRF_PERCALL_SETUP();
126 
127  if ((result = tt_process_call(funcctx)) != (Datum) 0)
128  SRF_RETURN_NEXT(funcctx, result);
129  SRF_RETURN_DONE(funcctx);
130 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
return result
Definition: formatting.c:1633
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
static Datum tt_process_call(FuncCallContext *funcctx)
Definition: wparser.c:82
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
static void tt_setup_firstcall(FuncCallContext *funcctx, Oid prsid)
Definition: wparser.c:49
uintptr_t Datum
Definition: postgres.h:372
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum ts_token_type_byname ( PG_FUNCTION_ARGS  )

Definition at line 133 of file wparser.c.

References get_ts_parser_oid(), PG_GETARG_TEXT_PP, result, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, textToQualifiedNameList(), tt_process_call(), and tt_setup_firstcall().

134 {
135  FuncCallContext *funcctx;
136  Datum result;
137 
138  if (SRF_IS_FIRSTCALL())
139  {
140  text *prsname = PG_GETARG_TEXT_PP(0);
141  Oid prsId;
142 
143  funcctx = SRF_FIRSTCALL_INIT();
144  prsId = get_ts_parser_oid(textToQualifiedNameList(prsname), false);
145  tt_setup_firstcall(funcctx, prsId);
146  }
147 
148  funcctx = SRF_PERCALL_SETUP();
149 
150  if ((result = tt_process_call(funcctx)) != (Datum) 0)
151  SRF_RETURN_NEXT(funcctx, result);
152  SRF_RETURN_DONE(funcctx);
153 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
return result
Definition: formatting.c:1633
unsigned int Oid
Definition: postgres_ext.h:31
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
static Datum tt_process_call(FuncCallContext *funcctx)
Definition: wparser.c:82
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3202
static void tt_setup_firstcall(FuncCallContext *funcctx, Oid prsid)
Definition: wparser.c:49
uintptr_t Datum
Definition: postgres.h:372
Oid get_ts_parser_oid(List *names, bool missing_ok)
Definition: namespace.c:2260
Definition: c.h:439
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
static Datum tt_process_call ( FuncCallContext funcctx)
static

Definition at line 82 of file wparser.c.

References LexDescr::alias, FuncCallContext::attinmeta, BuildTupleFromCStrings(), TSTokenTypeStorage::cur, LexDescr::descr, HeapTupleGetDatum, LexDescr::lexid, TSTokenTypeStorage::list, pfree(), result, FuncCallContext::user_fctx, and values.

Referenced by ts_token_type_byid(), and ts_token_type_byname().

83 {
85 
86  st = (TSTokenTypeStorage *) funcctx->user_fctx;
87  if (st->list && st->list[st->cur].lexid)
88  {
89  Datum result;
90  char *values[3];
91  char txtid[16];
92  HeapTuple tuple;
93 
94  sprintf(txtid, "%d", st->list[st->cur].lexid);
95  values[0] = txtid;
96  values[1] = st->list[st->cur].alias;
97  values[2] = st->list[st->cur].descr;
98 
99  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
100  result = HeapTupleGetDatum(tuple);
101 
102  pfree(values[1]);
103  pfree(values[2]);
104  st->cur++;
105  return result;
106  }
107  if (st->list)
108  pfree(st->list);
109  pfree(st);
110  return (Datum) 0;
111 }
LexDescr * list
Definition: wparser.c:32
char * alias
Definition: ts_public.h:28
return result
Definition: formatting.c:1633
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1118
void pfree(void *pointer)
Definition: mcxt.c:950
AttInMetadata * attinmeta
Definition: funcapi.h:99
char * descr
Definition: ts_public.h:29
int lexid
Definition: ts_public.h:27
uintptr_t Datum
Definition: postgres.h:372
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
static Datum values[MAXATTR]
Definition: bootstrap.c:163
void * user_fctx
Definition: funcapi.h:90
static void tt_setup_firstcall ( FuncCallContext funcctx,
Oid  prsid 
)
static

Definition at line 49 of file wparser.c.

References FuncCallContext::attinmeta, CreateTemplateTupleDesc(), TSTokenTypeStorage::cur, DatumGetPointer, elog, ERROR, INT4OID, TSParserCacheEntry::lextypeOid, TSTokenTypeStorage::list, lookup_ts_parser_cache(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, OidFunctionCall1, OidIsValid, palloc(), TEXTOID, TupleDescGetAttInMetadata(), TupleDescInitEntry(), and FuncCallContext::user_fctx.

Referenced by ts_token_type_byid(), and ts_token_type_byname().

50 {
51  TupleDesc tupdesc;
52  MemoryContext oldcontext;
55 
56  if (!OidIsValid(prs->lextypeOid))
57  elog(ERROR, "method lextype isn't defined for text search parser %u",
58  prsid);
59 
60  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
61 
63  st->cur = 0;
64  /* lextype takes one dummy argument */
66  (Datum) 0));
67  funcctx->user_fctx = (void *) st;
68 
69  tupdesc = CreateTemplateTupleDesc(3, false);
70  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "tokid",
71  INT4OID, -1, 0);
72  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "alias",
73  TEXTOID, -1, 0);
74  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "description",
75  TEXTOID, -1, 0);
76 
77  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
78  MemoryContextSwitchTo(oldcontext);
79 }
LexDescr * list
Definition: wparser.c:32
#define TEXTOID
Definition: pg_type.h:324
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define INT4OID
Definition: pg_type.h:316
TSParserCacheEntry * lookup_ts_parser_cache(Oid prsId)
Definition: ts_cache.c:112
#define OidIsValid(objectId)
Definition: c.h:538
#define ERROR
Definition: elog.h:43
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:622
AttInMetadata * attinmeta
Definition: funcapi.h:99
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:462
uintptr_t Datum
Definition: postgres.h:372
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1069
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
#define DatumGetPointer(X)
Definition: postgres.h:555
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:849
#define elog
Definition: elog.h:219
int16 AttrNumber
Definition: attnum.h:21