PostgreSQL Source Code  git master
wparser.c File Reference
#include "postgres.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "common/jsonapi.h"
#include "funcapi.h"
#include "tsearch/ts_cache.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
#include "utils/jsonfuncs.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

◆ HeadlineJsonState

Function Documentation

◆ headline_json_value()

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

Definition at line 530 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().

531 {
533 
534  HeadlineParsedText *prs = state->prs;
535  TSConfigCacheEntry *cfg = state->cfg;
536  TSParserCacheEntry *prsobj = state->prsobj;
537  TSQuery query = state->query;
538  List *prsoptions = state->prsoptions;
539 
540  prs->curwords = 0;
541  hlparsetext(cfg->cfgId, prs, query, elem_value, elem_len);
542  FunctionCall3(&(prsobj->prsheadline),
543  PointerGetDatum(prs),
544  PointerGetDatum(prsoptions),
545  PointerGetDatum(query));
546 
547  state->transformed = true;
548  return generateHeadline(prs);
549 }
TSParserCacheEntry * prsobj
Definition: wparser.c:40
#define PointerGetDatum(X)
Definition: postgres.h:556
List * prsoptions
Definition: wparser.c:42
#define FunctionCall3(flinfo, arg1, arg2, arg3)
Definition: fmgr.h:646
bool transformed
Definition: wparser.c:43
text * generateHeadline(HeadlineParsedText *prs)
Definition: ts_parse.c:594
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:529
TSConfigCacheEntry * cfg
Definition: wparser.c:39
Definition: pg_list.h:50
HeadlineParsedText * prs
Definition: wparser.c:38
FmgrInfo prsheadline
Definition: ts_cache.h:48

◆ prs_process_call()

static Datum prs_process_call ( FuncCallContext funcctx)
static

Definition at line 223 of file wparser.c.

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

Referenced by ts_parse_byid(), and ts_parse_byname().

224 {
225  PrsStorage *st;
226 
227  st = (PrsStorage *) funcctx->user_fctx;
228  if (st->cur < st->len)
229  {
230  Datum result;
231  char *values[2];
232  char tid[16];
233  HeapTuple tuple;
234 
235  values[0] = tid;
236  sprintf(tid, "%d", st->list[st->cur].type);
237  values[1] = st->list[st->cur].lexeme;
238  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
239  result = HeapTupleGetDatum(tuple);
240 
241  pfree(values[1]);
242  st->cur++;
243  return result;
244  }
245  return (Datum) 0;
246 }
int type
Definition: wparser.c:154
LexemeEntry * list
Definition: wparser.c:162
int len
Definition: wparser.c:161
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2116
#define sprintf
Definition: port.h:195
void pfree(void *pointer)
Definition: mcxt.c:1056
AttInMetadata * attinmeta
Definition: funcapi.h:91
char * lexeme
Definition: wparser.c:155
int cur
Definition: wparser.c:160
uintptr_t Datum
Definition: postgres.h:367
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void * user_fctx
Definition: funcapi.h:82

◆ prs_setup_firstcall()

static void prs_setup_firstcall ( FuncCallContext funcctx,
Oid  prsid,
text txt 
)
static

Definition at line 167 of file wparser.c.

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

Referenced by ts_parse_byid(), and ts_parse_byname().

168 {
169  TupleDesc tupdesc;
170  MemoryContext oldcontext;
171  PrsStorage *st;
173  char *lex = NULL;
174  int llen = 0,
175  type = 0;
176  void *prsdata;
177 
178  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
179 
180  st = (PrsStorage *) palloc(sizeof(PrsStorage));
181  st->cur = 0;
182  st->len = 16;
183  st->list = (LexemeEntry *) palloc(sizeof(LexemeEntry) * st->len);
184 
185  prsdata = (void *) DatumGetPointer(FunctionCall2(&prs->prsstart,
188 
189  while ((type = DatumGetInt32(FunctionCall3(&prs->prstoken,
190  PointerGetDatum(prsdata),
191  PointerGetDatum(&lex),
192  PointerGetDatum(&llen)))) != 0)
193  {
194  if (st->cur >= st->len)
195  {
196  st->len = 2 * st->len;
197  st->list = (LexemeEntry *) repalloc(st->list, sizeof(LexemeEntry) * st->len);
198  }
199  st->list[st->cur].lexeme = palloc(llen + 1);
200  memcpy(st->list[st->cur].lexeme, lex, llen);
201  st->list[st->cur].lexeme[llen] = '\0';
202  st->list[st->cur].type = type;
203  st->cur++;
204  }
205 
206  FunctionCall1(&prs->prsend, PointerGetDatum(prsdata));
207 
208  st->len = st->cur;
209  st->cur = 0;
210 
211  funcctx->user_fctx = (void *) st;
212  tupdesc = CreateTemplateTupleDesc(2);
213  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "tokid",
214  INT4OID, -1, 0);
215  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "token",
216  TEXTOID, -1, 0);
217 
218  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
219  MemoryContextSwitchTo(oldcontext);
220 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int type
Definition: wparser.c:154
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
#define DatumGetInt32(X)
Definition: postgres.h:472
LexemeEntry * list
Definition: wparser.c:162
#define PointerGetDatum(X)
Definition: postgres.h:556
int len
Definition: wparser.c:161
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define FunctionCall2(flinfo, arg1, arg2)
Definition: fmgr.h:644
TSParserCacheEntry * lookup_ts_parser_cache(Oid prsId)
Definition: ts_cache.c:112
#define FunctionCall3(flinfo, arg1, arg2, arg3)
Definition: fmgr.h:646
FmgrInfo prsend
Definition: ts_cache.h:47
AttInMetadata * attinmeta
Definition: funcapi.h:91
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
char * lexeme
Definition: wparser.c:155
int cur
Definition: wparser.c:160
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2067
FmgrInfo prstoken
Definition: ts_cache.h:46
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
#define DatumGetPointer(X)
Definition: postgres.h:549
FmgrInfo prsstart
Definition: ts_cache.h:45
#define Int32GetDatum(X)
Definition: postgres.h:479
void * user_fctx
Definition: funcapi.h:82
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:642
int16 AttrNumber
Definition: attnum.h:21

◆ ts_headline()

Datum ts_headline ( PG_FUNCTION_ARGS  )

Definition at line 355 of file wparser.c.

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

356 {
359  PG_GETARG_DATUM(0),
360  PG_GETARG_DATUM(1)));
361 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:560
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
Datum ts_headline_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:295
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:628
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352

◆ ts_headline_byid()

Datum ts_headline_byid ( PG_FUNCTION_ARGS  )

Definition at line 346 of file wparser.c.

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

347 {
349  PG_GETARG_DATUM(0),
350  PG_GETARG_DATUM(1),
351  PG_GETARG_DATUM(2)));
352 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum ts_headline_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:295
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:628
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352

◆ ts_headline_byid_opt()

Datum ts_headline_byid_opt ( PG_FUNCTION_ARGS  )

Definition at line 295 of file wparser.c.

References HeadlineJsonState::cfg, TSConfigCacheEntry::cfgId, deserialize_deflist(), ereport, errcode(), errmsg(), ERROR, FunctionCall3, generateHeadline(), TSParserCacheEntry::headlineOid, hlparsetext(), HeadlineParsedText::lenwords, lookup_ts_config_cache(), lookup_ts_parser_cache(), NIL, 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, HeadlineJsonState::prs, TSParserCacheEntry::prsheadline, TSConfigCacheEntry::prsId, HeadlineJsonState::prsobj, HeadlineJsonState::prsoptions, HeadlineJsonState::query, HeadlineParsedText::startsel, HeadlineParsedText::stopsel, VARDATA_ANY, VARSIZE_ANY_EXHDR, and HeadlineParsedText::words.

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

296 {
297  Oid tsconfig = PG_GETARG_OID(0);
298  text *in = PG_GETARG_TEXT_PP(1);
299  TSQuery query = PG_GETARG_TSQUERY(2);
300  text *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_PP(3) : NULL;
301  HeadlineParsedText prs;
302  List *prsoptions;
303  text *out;
304  TSConfigCacheEntry *cfg;
305  TSParserCacheEntry *prsobj;
306 
307  cfg = lookup_ts_config_cache(tsconfig);
308  prsobj = lookup_ts_parser_cache(cfg->prsId);
309 
310  if (!OidIsValid(prsobj->headlineOid))
311  ereport(ERROR,
312  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
313  errmsg("text search parser does not support headline creation")));
314 
315  memset(&prs, 0, sizeof(HeadlineParsedText));
316  prs.lenwords = 32;
317  prs.words = (HeadlineWordEntry *) palloc(sizeof(HeadlineWordEntry) * prs.lenwords);
318 
319  hlparsetext(cfg->cfgId, &prs, query,
320  VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
321 
322  if (opt)
323  prsoptions = deserialize_deflist(PointerGetDatum(opt));
324  else
325  prsoptions = NIL;
326 
327  FunctionCall3(&(prsobj->prsheadline),
328  PointerGetDatum(&prs),
329  PointerGetDatum(prsoptions),
330  PointerGetDatum(query));
331 
332  out = generateHeadline(&prs);
333 
334  PG_FREE_IF_COPY(in, 1);
335  PG_FREE_IF_COPY(query, 2);
336  if (opt)
337  PG_FREE_IF_COPY(opt, 3);
338  pfree(prs.words);
339  pfree(prs.startsel);
340  pfree(prs.stopsel);
341 
342  PG_RETURN_POINTER(out);
343 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define NIL
Definition: pg_list.h:65
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
List * deserialize_deflist(Datum txt)
Definition: tsearchcmds.c:1512
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
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:651
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define FunctionCall3(flinfo, arg1, arg2, arg3)
Definition: fmgr.h:646
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
TSConfigCacheEntry * lookup_ts_config_cache(Oid cfgId)
Definition: ts_cache.c:389
HeadlineWordEntry * words
Definition: ts_public.h:52
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define ereport(elevel,...)
Definition: elog.h:144
text * generateHeadline(HeadlineParsedText *prs)
Definition: ts_parse.c:594
void hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query, char *buf, int buflen)
Definition: ts_parse.c:529
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_NARGS()
Definition: fmgr.h:203
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
Definition: c.h:562
Definition: pg_list.h:50
FmgrInfo prsheadline
Definition: ts_cache.h:48

◆ ts_headline_json()

Datum ts_headline_json ( PG_FUNCTION_ARGS  )

Definition at line 498 of file wparser.c.

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

499 {
502  PG_GETARG_DATUM(0),
503  PG_GETARG_DATUM(1)));
504 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:560
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
Datum ts_headline_json_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:450
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:628
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352

◆ ts_headline_json_byid()

Datum ts_headline_json_byid ( PG_FUNCTION_ARGS  )

Definition at line 507 of file wparser.c.

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

508 {
510  PG_GETARG_DATUM(0),
511  PG_GETARG_DATUM(1),
512  PG_GETARG_DATUM(2)));
513 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum ts_headline_json_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:450
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:628
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352

◆ ts_headline_json_byid_opt()

Datum ts_headline_json_byid_opt ( PG_FUNCTION_ARGS  )

Definition at line 450 of file wparser.c.

References generate_unaccent_rules::action, HeadlineJsonState::cfg, deserialize_deflist(), ereport, errcode(), errmsg(), ERROR, headline_json_value(), TSParserCacheEntry::headlineOid, HeadlineParsedText::lenwords, lookup_ts_config_cache(), lookup_ts_parser_cache(), NIL, 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().

451 {
452  Oid tsconfig = PG_GETARG_OID(0);
453  text *json = PG_GETARG_TEXT_P(1);
454  TSQuery query = PG_GETARG_TSQUERY(2);
455  text *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_P(3) : NULL;
456  text *out;
458 
459  HeadlineParsedText prs;
461 
462  memset(&prs, 0, sizeof(HeadlineParsedText));
463  prs.lenwords = 32;
464  prs.words = (HeadlineWordEntry *) palloc(sizeof(HeadlineWordEntry) * prs.lenwords);
465 
466  state->prs = &prs;
467  state->cfg = lookup_ts_config_cache(tsconfig);
468  state->prsobj = lookup_ts_parser_cache(state->cfg->prsId);
469  state->query = query;
470  if (opt)
472  else
473  state->prsoptions = NIL;
474 
475  if (!OidIsValid(state->prsobj->headlineOid))
476  ereport(ERROR,
477  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
478  errmsg("text search parser does not support headline creation")));
479 
480  out = transform_json_string_values(json, state, action);
481 
482  PG_FREE_IF_COPY(json, 1);
483  PG_FREE_IF_COPY(query, 2);
484  if (opt)
485  PG_FREE_IF_COPY(opt, 3);
486  pfree(prs.words);
487 
488  if (state->transformed)
489  {
490  pfree(prs.startsel);
491  pfree(prs.stopsel);
492  }
493 
494  PG_RETURN_TEXT_P(out);
495 }
#define NIL
Definition: pg_list.h:65
TSParserCacheEntry * prsobj
Definition: wparser.c:40
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
List * deserialize_deflist(Datum txt)
Definition: tsearchcmds.c:1512
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
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:651
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
text * transform_json_string_values(text *json, void *action_state, JsonTransformStringValuesAction transform_action)
Definition: jsonfuncs.c:5330
TSConfigCacheEntry * lookup_ts_config_cache(Oid cfgId)
Definition: ts_cache.c:389
HeadlineWordEntry * words
Definition: ts_public.h:52
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
bool transformed
Definition: wparser.c:43
void * palloc0(Size size)
Definition: mcxt.c:980
#define ereport(elevel,...)
Definition: elog.h:144
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
TSQuery query
Definition: wparser.c:41
Definition: regguts.h:298
static text * headline_json_value(void *_state, char *elem_value, int elem_len)
Definition: wparser.c:530
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_NARGS()
Definition: fmgr.h:203
TSConfigCacheEntry * cfg
Definition: wparser.c:39
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:335
Definition: c.h:562
text *(* JsonTransformStringValuesAction)(void *state, char *elem_value, int elem_len)
Definition: jsonfuncs.h:37
HeadlineParsedText * prs
Definition: wparser.c:38

◆ ts_headline_json_opt()

Datum ts_headline_json_opt ( PG_FUNCTION_ARGS  )

Definition at line 516 of file wparser.c.

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

517 {
520  PG_GETARG_DATUM(0),
521  PG_GETARG_DATUM(1),
522  PG_GETARG_DATUM(2)));
523 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:560
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define DirectFunctionCall4(func, arg1, arg2, arg3, arg4)
Definition: fmgr.h:630
Datum ts_headline_json_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:450
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352

◆ ts_headline_jsonb()

Datum ts_headline_jsonb ( PG_FUNCTION_ARGS  )

Definition at line 422 of file wparser.c.

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

423 {
426  PG_GETARG_DATUM(0),
427  PG_GETARG_DATUM(1)));
428 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:560
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:628
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352
Datum ts_headline_jsonb_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:374

◆ ts_headline_jsonb_byid()

Datum ts_headline_jsonb_byid ( PG_FUNCTION_ARGS  )

Definition at line 431 of file wparser.c.

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

432 {
434  PG_GETARG_DATUM(0),
435  PG_GETARG_DATUM(1),
436  PG_GETARG_DATUM(2)));
437 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:628
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352
Datum ts_headline_jsonb_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:374

◆ ts_headline_jsonb_byid_opt()

Datum ts_headline_jsonb_byid_opt ( PG_FUNCTION_ARGS  )

Definition at line 374 of file wparser.c.

References generate_unaccent_rules::action, HeadlineJsonState::cfg, deserialize_deflist(), ereport, errcode(), errmsg(), ERROR, headline_json_value(), TSParserCacheEntry::headlineOid, HeadlineParsedText::lenwords, lookup_ts_config_cache(), lookup_ts_parser_cache(), NIL, OidIsValid, palloc(), palloc0(), pfree(), PG_FREE_IF_COPY, PG_GETARG_JSONB_P, PG_GETARG_OID, PG_GETARG_POINTER, PG_GETARG_TEXT_P, PG_GETARG_TSQUERY, PG_NARGS, PG_RETURN_JSONB_P, 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().

375 {
376  Oid tsconfig = PG_GETARG_OID(0);
377  Jsonb *jb = PG_GETARG_JSONB_P(1);
378  TSQuery query = PG_GETARG_TSQUERY(2);
379  text *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_P(3) : NULL;
380  Jsonb *out;
382  HeadlineParsedText prs;
384 
385  memset(&prs, 0, sizeof(HeadlineParsedText));
386  prs.lenwords = 32;
387  prs.words = (HeadlineWordEntry *) palloc(sizeof(HeadlineWordEntry) * prs.lenwords);
388 
389  state->prs = &prs;
390  state->cfg = lookup_ts_config_cache(tsconfig);
391  state->prsobj = lookup_ts_parser_cache(state->cfg->prsId);
392  state->query = query;
393  if (opt)
395  else
396  state->prsoptions = NIL;
397 
398  if (!OidIsValid(state->prsobj->headlineOid))
399  ereport(ERROR,
400  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
401  errmsg("text search parser does not support headline creation")));
402 
403  out = transform_jsonb_string_values(jb, state, action);
404 
405  PG_FREE_IF_COPY(jb, 1);
406  PG_FREE_IF_COPY(query, 2);
407  if (opt)
408  PG_FREE_IF_COPY(opt, 3);
409 
410  pfree(prs.words);
411 
412  if (state->transformed)
413  {
414  pfree(prs.startsel);
415  pfree(prs.stopsel);
416  }
417 
418  PG_RETURN_JSONB_P(out);
419 }
#define NIL
Definition: pg_list.h:65
Definition: jsonb.h:220
TSParserCacheEntry * prsobj
Definition: wparser.c:40
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
List * deserialize_deflist(Datum txt)
Definition: tsearchcmds.c:1512
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
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:651
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
TSConfigCacheEntry * lookup_ts_config_cache(Oid cfgId)
Definition: ts_cache.c:389
Jsonb * transform_jsonb_string_values(Jsonb *jsonb, void *action_state, JsonTransformStringValuesAction transform_action)
Definition: jsonfuncs.c:5285
HeadlineWordEntry * words
Definition: ts_public.h:52
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
bool transformed
Definition: wparser.c:43
void * palloc0(Size size)
Definition: mcxt.c:980
#define ereport(elevel,...)
Definition: elog.h:144
TSQuery query
Definition: wparser.c:41
Definition: regguts.h:298
static text * headline_json_value(void *_state, char *elem_value, int elem_len)
Definition: wparser.c:530
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_NARGS()
Definition: fmgr.h:203
TSConfigCacheEntry * cfg
Definition: wparser.c:39
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:335
Definition: c.h:562
#define PG_RETURN_JSONB_P(x)
Definition: jsonb.h:76
text *(* JsonTransformStringValuesAction)(void *state, char *elem_value, int elem_len)
Definition: jsonfuncs.h:37
HeadlineParsedText * prs
Definition: wparser.c:38
#define PG_GETARG_JSONB_P(x)
Definition: jsonb.h:74

◆ ts_headline_jsonb_opt()

Datum ts_headline_jsonb_opt ( PG_FUNCTION_ARGS  )

Definition at line 440 of file wparser.c.

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

441 {
444  PG_GETARG_DATUM(0),
445  PG_GETARG_DATUM(1),
446  PG_GETARG_DATUM(2)));
447 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:560
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define DirectFunctionCall4(func, arg1, arg2, arg3, arg4)
Definition: fmgr.h:630
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352
Datum ts_headline_jsonb_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:374

◆ ts_headline_opt()

Datum ts_headline_opt ( PG_FUNCTION_ARGS  )

Definition at line 364 of file wparser.c.

References DirectFunctionCall4, 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  PG_GETARG_DATUM(2)));
371 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:560
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
Datum ts_headline_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:295
#define DirectFunctionCall4(func, arg1, arg2, arg3, arg4)
Definition: fmgr.h:630
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352

◆ ts_parse_byid()

Datum ts_parse_byid ( PG_FUNCTION_ARGS  )

Definition at line 249 of file wparser.c.

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

250 {
251  FuncCallContext *funcctx;
252  Datum result;
253 
254  if (SRF_IS_FIRSTCALL())
255  {
256  text *txt = PG_GETARG_TEXT_PP(1);
257 
258  funcctx = SRF_FIRSTCALL_INIT();
259  prs_setup_firstcall(funcctx, PG_GETARG_OID(0), txt);
260  PG_FREE_IF_COPY(txt, 1);
261  }
262 
263  funcctx = SRF_PERCALL_SETUP();
264 
265  if ((result = prs_process_call(funcctx)) != (Datum) 0)
266  SRF_RETURN_NEXT(funcctx, result);
267  SRF_RETURN_DONE(funcctx);
268 }
static Datum prs_process_call(FuncCallContext *funcctx)
Definition: wparser.c:223
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
uintptr_t Datum
Definition: postgres.h:367
static void prs_setup_firstcall(FuncCallContext *funcctx, Oid prsid, text *txt)
Definition: wparser.c:167
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:562
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ ts_parse_byname()

Datum ts_parse_byname ( PG_FUNCTION_ARGS  )

Definition at line 271 of file wparser.c.

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

272 {
273  FuncCallContext *funcctx;
274  Datum result;
275 
276  if (SRF_IS_FIRSTCALL())
277  {
278  text *prsname = PG_GETARG_TEXT_PP(0);
279  text *txt = PG_GETARG_TEXT_PP(1);
280  Oid prsId;
281 
282  funcctx = SRF_FIRSTCALL_INIT();
283  prsId = get_ts_parser_oid(textToQualifiedNameList(prsname), false);
284  prs_setup_firstcall(funcctx, prsId, txt);
285  }
286 
287  funcctx = SRF_PERCALL_SETUP();
288 
289  if ((result = prs_process_call(funcctx)) != (Datum) 0)
290  SRF_RETURN_NEXT(funcctx, result);
291  SRF_RETURN_DONE(funcctx);
292 }
static Datum prs_process_call(FuncCallContext *funcctx)
Definition: wparser.c:223
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
unsigned int Oid
Definition: postgres_ext.h:31
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3628
uintptr_t Datum
Definition: postgres.h:367
Oid get_ts_parser_oid(List *names, bool missing_ok)
Definition: namespace.c:2300
static void prs_setup_firstcall(FuncCallContext *funcctx, Oid prsid, text *txt)
Definition: wparser.c:167
Definition: c.h:562
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ ts_token_type_byid()

Datum ts_token_type_byid ( PG_FUNCTION_ARGS  )

Definition at line 111 of file wparser.c.

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

112 {
113  FuncCallContext *funcctx;
114  Datum result;
115 
116  if (SRF_IS_FIRSTCALL())
117  {
118  funcctx = SRF_FIRSTCALL_INIT();
119  tt_setup_firstcall(funcctx, PG_GETARG_OID(0));
120  }
121 
122  funcctx = SRF_PERCALL_SETUP();
123 
124  if ((result = tt_process_call(funcctx)) != (Datum) 0)
125  SRF_RETURN_NEXT(funcctx, result);
126  SRF_RETURN_DONE(funcctx);
127 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
static Datum tt_process_call(FuncCallContext *funcctx)
Definition: wparser.c:82
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
static void tt_setup_firstcall(FuncCallContext *funcctx, Oid prsid)
Definition: wparser.c:49
uintptr_t Datum
Definition: postgres.h:367
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ ts_token_type_byname()

Datum ts_token_type_byname ( PG_FUNCTION_ARGS  )

Definition at line 130 of file wparser.c.

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

131 {
132  FuncCallContext *funcctx;
133  Datum result;
134 
135  if (SRF_IS_FIRSTCALL())
136  {
137  text *prsname = PG_GETARG_TEXT_PP(0);
138  Oid prsId;
139 
140  funcctx = SRF_FIRSTCALL_INIT();
141  prsId = get_ts_parser_oid(textToQualifiedNameList(prsname), false);
142  tt_setup_firstcall(funcctx, prsId);
143  }
144 
145  funcctx = SRF_PERCALL_SETUP();
146 
147  if ((result = tt_process_call(funcctx)) != (Datum) 0)
148  SRF_RETURN_NEXT(funcctx, result);
149  SRF_RETURN_DONE(funcctx);
150 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
unsigned int Oid
Definition: postgres_ext.h:31
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
static Datum tt_process_call(FuncCallContext *funcctx)
Definition: wparser.c:82
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3628
static void tt_setup_firstcall(FuncCallContext *funcctx, Oid prsid)
Definition: wparser.c:49
uintptr_t Datum
Definition: postgres.h:367
Oid get_ts_parser_oid(List *names, bool missing_ok)
Definition: namespace.c:2300
Definition: c.h:562
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ tt_process_call()

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(), sprintf, 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  return (Datum) 0;
108 }
LexDescr * list
Definition: wparser.c:32
char * alias
Definition: ts_public.h:28
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2116
#define sprintf
Definition: port.h:195
void pfree(void *pointer)
Definition: mcxt.c:1056
AttInMetadata * attinmeta
Definition: funcapi.h:91
char * descr
Definition: ts_public.h:29
int lexid
Definition: ts_public.h:27
uintptr_t Datum
Definition: postgres.h:367
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void * user_fctx
Definition: funcapi.h:82

◆ tt_setup_firstcall()

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, TSParserCacheEntry::lextypeOid, TSTokenTypeStorage::list, lookup_ts_parser_cache(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, OidFunctionCall1, OidIsValid, palloc(), HeadlineJsonState::prs, 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);
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 }
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
LexDescr * list
Definition: wparser.c:32
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
TSParserCacheEntry * lookup_ts_parser_cache(Oid prsId)
Definition: ts_cache.c:112
#define OidIsValid(objectId)
Definition: c.h:651
#define ERROR
Definition: elog.h:43
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:662
AttInMetadata * attinmeta
Definition: funcapi.h:91
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
uintptr_t Datum
Definition: postgres.h:367
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2067
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
#define DatumGetPointer(X)
Definition: postgres.h:549
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:214
int16 AttrNumber
Definition: attnum.h:21