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

Go to the source code of this file.

Data Structures

struct  MorphOpaque
 
struct  TSVectorBuildState
 

Typedefs

typedef struct MorphOpaque MorphOpaque
 
typedef struct TSVectorBuildState TSVectorBuildState
 

Functions

static void add_to_tsvector (void *state, char *elem_value, int elem_len)
 
Datum get_current_ts_config (PG_FUNCTION_ARGS)
 
static int compareWORD (const void *a, const void *b)
 
static int uniqueWORD (ParsedWord *a, int32 l)
 
TSVector make_tsvector (ParsedText *prs)
 
Datum to_tsvector_byid (PG_FUNCTION_ARGS)
 
Datum to_tsvector (PG_FUNCTION_ARGS)
 
Datum jsonb_to_tsvector_byid (PG_FUNCTION_ARGS)
 
Datum jsonb_to_tsvector (PG_FUNCTION_ARGS)
 
Datum json_to_tsvector_byid (PG_FUNCTION_ARGS)
 
Datum json_to_tsvector (PG_FUNCTION_ARGS)
 
static void pushval_morph (Datum opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
 
Datum to_tsquery_byid (PG_FUNCTION_ARGS)
 
Datum to_tsquery (PG_FUNCTION_ARGS)
 
Datum plainto_tsquery_byid (PG_FUNCTION_ARGS)
 
Datum plainto_tsquery (PG_FUNCTION_ARGS)
 
Datum phraseto_tsquery_byid (PG_FUNCTION_ARGS)
 
Datum phraseto_tsquery (PG_FUNCTION_ARGS)
 

Typedef Documentation

Function Documentation

static void add_to_tsvector ( void *  state,
char *  elem_value,
int  elem_len 
)
static

Definition at line 362 of file to_tsany.c.

References TSVectorBuildState::cfgId, ParsedText::curwords, DirectFunctionCall2, i, ParsedText::lenwords, make_tsvector(), NULL, palloc(), parsetext(), PointerGetDatum, ParsedWord::pos, ParsedText::pos, TSVectorBuildState::prs, TSVectorBuildState::result, TS_JUMP, tsvector_concat(), TSVectorGetDatum, and ParsedText::words.

Referenced by json_to_tsvector_byid(), and jsonb_to_tsvector_byid().

363 {
365  ParsedText *prs = state->prs;
366  TSVector item_vector;
367  int i;
368 
369  prs->lenwords = elem_len / 6;
370  if (prs->lenwords == 0)
371  prs->lenwords = 2;
372 
373  prs->words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs->lenwords);
374  prs->curwords = 0;
375  prs->pos = 0;
376 
377  parsetext(state->cfgId, prs, elem_value, elem_len);
378 
379  if (prs->curwords)
380  {
381  if (state->result != NULL)
382  {
383  for (i = 0; i < prs->curwords; i++)
384  prs->words[i].pos.pos = prs->words[i].pos.pos + TS_JUMP;
385 
386  item_vector = make_tsvector(prs);
387 
389  TSVectorGetDatum(state->result),
390  PointerGetDatum(item_vector));
391  }
392  else
393  state->result = make_tsvector(prs);
394  }
395 }
TSVector make_tsvector(ParsedText *prs)
Definition: to_tsany.c:154
#define PointerGetDatum(X)
Definition: postgres.h:562
TSVector result
Definition: to_tsany.c:31
Datum tsvector_concat(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:933
int32 lenwords
Definition: ts_utils.h:86
#define TS_JUMP
Definition: ts_type.h:96
int32 curwords
Definition: ts_utils.h:87
void parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
Definition: ts_parse.c:358
ParsedWord * words
Definition: ts_utils.h:85
#define TSVectorGetDatum(X)
Definition: ts_type.h:128
uint16 pos
Definition: ts_utils.h:69
ParsedText * prs
Definition: to_tsany.c:30
TSVectorData * TSVector
Definition: ts_type.h:107
#define NULL
Definition: c.h:229
Definition: regguts.h:298
int32 pos
Definition: ts_utils.h:88
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
static int compareWORD ( const void *  a,
const void *  b 
)
static

Definition at line 47 of file to_tsany.c.

References tsCompareString(), and word().

Referenced by uniqueWORD().

48 {
49  int res;
50 
51  res = tsCompareString(
52  ((const ParsedWord *) a)->word, ((const ParsedWord *) a)->len,
53  ((const ParsedWord *) b)->word, ((const ParsedWord *) b)->len,
54  false);
55 
56  if (res == 0)
57  {
58  if (((const ParsedWord *) a)->pos.pos == ((const ParsedWord *) b)->pos.pos)
59  return 0;
60 
61  res = (((const ParsedWord *) a)->pos.pos > ((const ParsedWord *) b)->pos.pos) ? 1 : -1;
62  }
63 
64  return res;
65 }
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1160
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1243
Datum get_current_ts_config ( PG_FUNCTION_ARGS  )

Definition at line 38 of file to_tsany.c.

References getTSCurrentConfig(), and PG_RETURN_OID.

39 {
41 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
Datum json_to_tsvector ( PG_FUNCTION_ARGS  )

Definition at line 346 of file to_tsany.c.

References DirectFunctionCall2, getTSCurrentConfig(), json_to_tsvector_byid(), ObjectIdGetDatum, PG_GETARG_TEXT_P, PG_RETURN_DATUM, and PointerGetDatum.

347 {
348  text *json = PG_GETARG_TEXT_P(0);
349  Oid cfgId;
350 
351  cfgId = getTSCurrentConfig(true);
353  ObjectIdGetDatum(cfgId),
354  PointerGetDatum(json)));
355 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PointerGetDatum(X)
Definition: postgres.h:562
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:300
Definition: c.h:439
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum json_to_tsvector_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:314
Datum json_to_tsvector_byid ( PG_FUNCTION_ARGS  )

Definition at line 314 of file to_tsany.c.

References add_to_tsvector(), CALCDATASIZE, TSVectorBuildState::cfgId, iterate_json_string_values(), NULL, palloc(), pfree(), PG_FREE_IF_COPY, PG_GETARG_OID, PG_GETARG_TEXT_P, PG_RETURN_TSVECTOR, TSVectorBuildState::prs, TSVectorBuildState::result, SET_VARSIZE, TSVectorData::size, and ParsedText::words.

Referenced by json_to_tsvector().

315 {
316  Oid cfgId = PG_GETARG_OID(0);
317  text *json = PG_GETARG_TEXT_P(1);
319  ParsedText *prs = (ParsedText *) palloc(sizeof(ParsedText));
320 
321  prs->words = NULL;
322  state.result = NULL;
323  state.cfgId = cfgId;
324  state.prs = prs;
325 
327 
328  PG_FREE_IF_COPY(json, 1);
329  if (state.result == NULL)
330  {
331  /* There weren't any string elements in json,
332  * so wee need to return an empty vector */
333 
334  if (prs->words != NULL)
335  pfree(prs->words);
336 
337  state.result = palloc(CALCDATASIZE(0, 0));
338  SET_VARSIZE(state.result, CALCDATASIZE(0, 0));
339  state.result->size = 0;
340  }
341 
342  PG_RETURN_TSVECTOR(state.result);
343 }
void iterate_json_string_values(text *json, void *action_state, JsonIterateStringValuesAction action)
Definition: jsonfuncs.c:4816
TSVector result
Definition: to_tsany.c:31
#define PG_RETURN_TSVECTOR(x)
Definition: ts_type.h:131
unsigned int Oid
Definition: postgres_ext.h:31
void pfree(void *pointer)
Definition: mcxt.c:950
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
int32 size
Definition: ts_type.h:102
ParsedWord * words
Definition: ts_utils.h:85
void(* JsonIterateStringValuesAction)(void *state, char *elem_value, int elem_len)
Definition: jsonapi.h:136
ParsedText * prs
Definition: to_tsany.c:30
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
#define NULL
Definition: c.h:229
Definition: regguts.h:298
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
static void add_to_tsvector(void *state, char *elem_value, int elem_len)
Definition: to_tsany.c:362
void * palloc(Size size)
Definition: mcxt.c:849
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:300
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
Datum jsonb_to_tsvector ( PG_FUNCTION_ARGS  )

Definition at line 302 of file to_tsany.c.

References DirectFunctionCall2, getTSCurrentConfig(), jsonb_to_tsvector_byid(), JsonbGetDatum, ObjectIdGetDatum, PG_GETARG_JSONB, and PG_RETURN_DATUM.

303 {
304  Jsonb *jb = PG_GETARG_JSONB(0);
305  Oid cfgId;
306 
307  cfgId = getTSCurrentConfig(true);
309  ObjectIdGetDatum(cfgId),
310  JsonbGetDatum(jb)));
311 }
Definition: jsonb.h:215
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define JsonbGetDatum(p)
Definition: jsonb.h:69
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
Datum jsonb_to_tsvector_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:269
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum jsonb_to_tsvector_byid ( PG_FUNCTION_ARGS  )

Definition at line 269 of file to_tsany.c.

References add_to_tsvector(), CALCDATASIZE, TSVectorBuildState::cfgId, iterate_jsonb_string_values(), NULL, palloc(), pfree(), PG_FREE_IF_COPY, PG_GETARG_JSONB, PG_GETARG_OID, PG_RETURN_TSVECTOR, TSVectorBuildState::prs, TSVectorBuildState::result, SET_VARSIZE, TSVectorData::size, and ParsedText::words.

Referenced by jsonb_to_tsvector().

270 {
271  Oid cfgId = PG_GETARG_OID(0);
272  Jsonb *jb = PG_GETARG_JSONB(1);
274  ParsedText *prs = (ParsedText *) palloc(sizeof(ParsedText));
275 
276  prs->words = NULL;
277  state.result = NULL;
278  state.cfgId = cfgId;
279  state.prs = prs;
280 
282 
283  PG_FREE_IF_COPY(jb, 1);
284 
285  if (state.result == NULL)
286  {
287  /* There weren't any string elements in jsonb,
288  * so wee need to return an empty vector */
289 
290  if (prs->words != NULL)
291  pfree(prs->words);
292 
293  state.result = palloc(CALCDATASIZE(0, 0));
294  SET_VARSIZE(state.result, CALCDATASIZE(0, 0));
295  state.result->size = 0;
296  }
297 
298  PG_RETURN_TSVECTOR(state.result);
299 }
Definition: jsonb.h:215
TSVector result
Definition: to_tsany.c:31
#define PG_RETURN_TSVECTOR(x)
Definition: ts_type.h:131
unsigned int Oid
Definition: postgres_ext.h:31
void pfree(void *pointer)
Definition: mcxt.c:950
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
int32 size
Definition: ts_type.h:102
ParsedWord * words
Definition: ts_utils.h:85
void(* JsonIterateStringValuesAction)(void *state, char *elem_value, int elem_len)
Definition: jsonapi.h:136
ParsedText * prs
Definition: to_tsany.c:30
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
#define NULL
Definition: c.h:229
Definition: regguts.h:298
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
static void add_to_tsvector(void *state, char *elem_value, int elem_len)
Definition: to_tsany.c:362
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
void * palloc(Size size)
Definition: mcxt.c:849
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
void iterate_jsonb_string_values(Jsonb *jb, void *state, JsonIterateStringValuesAction action)
Definition: jsonfuncs.c:4794
TSVector make_tsvector ( ParsedText prs)

Definition at line 154 of file to_tsany.c.

References ParsedWord::alen, ARRPTR, CALCDATASIZE, ParsedText::curwords, elog, ereport, errcode(), errmsg(), ERROR, WordEntry::haspos, i, WordEntry::len, ParsedWord::len, MAXSTRPOS, palloc0(), pfree(), WordEntry::pos, ParsedWord::pos, POSDATAPTR, SET_VARSIZE, SHORTALIGN, TSVectorData::size, STRPTR, uniqueWORD(), WEP_SETPOS, WEP_SETWEIGHT, ParsedWord::word, and ParsedText::words.

Referenced by add_to_tsvector(), to_tsvector_byid(), and tsvector_update_trigger().

155 {
156  int i,
157  j,
158  lenstr = 0,
159  totallen;
160  TSVector in;
161  WordEntry *ptr;
162  char *str;
163  int stroff;
164 
165  prs->curwords = uniqueWORD(prs->words, prs->curwords);
166  for (i = 0; i < prs->curwords; i++)
167  {
168  lenstr += prs->words[i].len;
169  if (prs->words[i].alen)
170  {
171  lenstr = SHORTALIGN(lenstr);
172  lenstr += sizeof(uint16) + prs->words[i].pos.apos[0] * sizeof(WordEntryPos);
173  }
174  }
175 
176  if (lenstr > MAXSTRPOS)
177  ereport(ERROR,
178  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
179  errmsg("string is too long for tsvector (%d bytes, max %d bytes)", lenstr, MAXSTRPOS)));
180 
181  totallen = CALCDATASIZE(prs->curwords, lenstr);
182  in = (TSVector) palloc0(totallen);
183  SET_VARSIZE(in, totallen);
184  in->size = prs->curwords;
185 
186  ptr = ARRPTR(in);
187  str = STRPTR(in);
188  stroff = 0;
189  for (i = 0; i < prs->curwords; i++)
190  {
191  ptr->len = prs->words[i].len;
192  ptr->pos = stroff;
193  memcpy(str + stroff, prs->words[i].word, prs->words[i].len);
194  stroff += prs->words[i].len;
195  pfree(prs->words[i].word);
196  if (prs->words[i].alen)
197  {
198  int k = prs->words[i].pos.apos[0];
199  WordEntryPos *wptr;
200 
201  if (k > 0xFFFF)
202  elog(ERROR, "positions array too long");
203 
204  ptr->haspos = 1;
205  stroff = SHORTALIGN(stroff);
206  *(uint16 *) (str + stroff) = (uint16) k;
207  wptr = POSDATAPTR(in, ptr);
208  for (j = 0; j < k; j++)
209  {
210  WEP_SETWEIGHT(wptr[j], 0);
211  WEP_SETPOS(wptr[j], prs->words[i].pos.apos[j + 1]);
212  }
213  stroff += sizeof(uint16) + k * sizeof(WordEntryPos);
214  pfree(prs->words[i].pos.apos);
215  }
216  else
217  ptr->haspos = 0;
218  ptr++;
219  }
220  pfree(prs->words);
221  return in;
222 }
uint16 WordEntryPos
Definition: ts_type.h:63
uint32 len
Definition: ts_type.h:44
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MAXSTRPOS
Definition: ts_type.h:50
#define WEP_SETPOS(x, v)
Definition: ts_type.h:83
int32 curwords
Definition: ts_utils.h:87
#define POSDATAPTR(x, e)
Definition: ts_type.h:120
unsigned short uint16
Definition: c.h:267
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
#define WEP_SETWEIGHT(x, v)
Definition: ts_type.h:82
int32 size
Definition: ts_type.h:102
ParsedWord * words
Definition: ts_utils.h:85
uint16 pos
Definition: ts_utils.h:69
uint32 haspos
Definition: ts_type.h:44
#define ereport(elevel, rest)
Definition: elog.h:122
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
void * palloc0(Size size)
Definition: mcxt.c:878
TSVectorData * TSVector
Definition: ts_type.h:107
uint16 len
Definition: ts_utils.h:65
uint32 pos
Definition: ts_type.h:44
uint32 alen
Definition: ts_utils.h:80
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define STRPTR(x)
Definition: hstore.h:76
int i
static int uniqueWORD(ParsedWord *a, int32 l)
Definition: to_tsany.c:68
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define ARRPTR(x)
Definition: cube.c:26
#define elog
Definition: elog.h:219
#define SHORTALIGN(LEN)
Definition: c.h:584
char * word
Definition: ts_utils.h:79
Datum phraseto_tsquery ( PG_FUNCTION_ARGS  )

Definition at line 580 of file to_tsany.c.

References DirectFunctionCall2, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_TEXT_PP, PG_RETURN_DATUM, phraseto_tsquery_byid(), and PointerGetDatum.

581 {
582  text *in = PG_GETARG_TEXT_PP(0);
583  Oid cfgId;
584 
585  cfgId = getTSCurrentConfig(true);
587  ObjectIdGetDatum(cfgId),
588  PointerGetDatum(in)));
589 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PointerGetDatum(X)
Definition: postgres.h:562
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Definition: c.h:439
Datum phraseto_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:562
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum phraseto_tsquery_byid ( PG_FUNCTION_ARGS  )

Definition at line 562 of file to_tsany.c.

References MorphOpaque::cfg_id, OP_PHRASE, parse_tsquery(), PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_TSQUERY, PointerGetDatum, pushval_morph(), MorphOpaque::qoperator, and text_to_cstring().

Referenced by phraseto_tsquery().

563 {
564  text *in = PG_GETARG_TEXT_PP(1);
565  TSQuery query;
566  MorphOpaque data;
567 
568  data.cfg_id = PG_GETARG_OID(0);
569  data.qoperator = OP_PHRASE;
570 
571  query = parse_tsquery(text_to_cstring(in),
573  PointerGetDatum(&data),
574  true);
575 
576  PG_RETURN_TSQUERY(query);
577 }
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_TSQUERY(x)
Definition: ts_type.h:249
int qoperator
Definition: to_tsany.c:25
TSQuery parse_tsquery(char *buf, PushFunction pushval, Datum opaque, bool isplain)
Definition: tsquery.c:605
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define OP_PHRASE
Definition: ts_type.h:178
Oid cfg_id
Definition: to_tsany.c:24
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:439
static void pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
Definition: to_tsany.c:413
Datum plainto_tsquery ( PG_FUNCTION_ARGS  )

Definition at line 549 of file to_tsany.c.

References DirectFunctionCall2, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_TEXT_PP, PG_RETURN_DATUM, plainto_tsquery_byid(), and PointerGetDatum.

Referenced by ts_match_tt().

550 {
551  text *in = PG_GETARG_TEXT_PP(0);
552  Oid cfgId;
553 
554  cfgId = getTSCurrentConfig(true);
556  ObjectIdGetDatum(cfgId),
557  PointerGetDatum(in)));
558 }
Datum plainto_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:531
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PointerGetDatum(X)
Definition: postgres.h:562
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Definition: c.h:439
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum plainto_tsquery_byid ( PG_FUNCTION_ARGS  )

Definition at line 531 of file to_tsany.c.

References MorphOpaque::cfg_id, OP_AND, parse_tsquery(), PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_POINTER, PointerGetDatum, pushval_morph(), MorphOpaque::qoperator, and text_to_cstring().

Referenced by plainto_tsquery().

532 {
533  text *in = PG_GETARG_TEXT_PP(1);
534  TSQuery query;
535  MorphOpaque data;
536 
537  data.cfg_id = PG_GETARG_OID(0);
538  data.qoperator = OP_AND;
539 
540  query = parse_tsquery(text_to_cstring(in),
542  PointerGetDatum(&data),
543  true);
544 
545  PG_RETURN_POINTER(query);
546 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define OP_AND
Definition: ts_type.h:176
int qoperator
Definition: to_tsany.c:25
TSQuery parse_tsquery(char *buf, PushFunction pushval, Datum opaque, bool isplain)
Definition: tsquery.c:605
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
Oid cfg_id
Definition: to_tsany.c:24
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:439
static void pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
Definition: to_tsany.c:413
static void pushval_morph ( Datum  opaque,
TSQueryParserState  state,
char *  strval,
int  lenval,
int16  weight,
bool  prefix 
)
static

Definition at line 413 of file to_tsany.c.

References MorphOpaque::cfg_id, ParsedText::curwords, DatumGetPointer, ParsedWord::flags, ParsedWord::len, ParsedText::lenwords, ParsedWord::nvariant, OP_AND, OP_OR, palloc(), parsetext(), pfree(), ParsedWord::pos, ParsedText::pos, pushOperator(), pushStop(), pushValue(), MorphOpaque::qoperator, TSL_PREFIX, ParsedWord::word, and ParsedText::words.

Referenced by phraseto_tsquery_byid(), plainto_tsquery_byid(), and to_tsquery_byid().

414 {
415  int32 count = 0;
416  ParsedText prs;
417  uint32 variant,
418  pos = 0,
419  cntvar = 0,
420  cntpos = 0,
421  cnt = 0;
422  MorphOpaque *data = (MorphOpaque *) DatumGetPointer(opaque);
423 
424  prs.lenwords = 4;
425  prs.curwords = 0;
426  prs.pos = 0;
427  prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);
428 
429  parsetext(data->cfg_id, &prs, strval, lenval);
430 
431  if (prs.curwords > 0)
432  {
433  while (count < prs.curwords)
434  {
435  /*
436  * Were any stop words removed? If so, fill empty positions with
437  * placeholders linked by an appropriate operator.
438  */
439  if (pos > 0 && pos + 1 < prs.words[count].pos.pos)
440  {
441  while (pos + 1 < prs.words[count].pos.pos)
442  {
443  /* put placeholders for each missing stop word */
444  pushStop(state);
445  if (cntpos)
446  pushOperator(state, data->qoperator, 1);
447  cntpos++;
448  pos++;
449  }
450  }
451 
452  /* save current word's position */
453  pos = prs.words[count].pos.pos;
454 
455  /* Go through all variants obtained from this token */
456  cntvar = 0;
457  while (count < prs.curwords && pos == prs.words[count].pos.pos)
458  {
459  variant = prs.words[count].nvariant;
460 
461  /* Push all words belonging to the same variant */
462  cnt = 0;
463  while (count < prs.curwords &&
464  pos == prs.words[count].pos.pos &&
465  variant == prs.words[count].nvariant)
466  {
467  pushValue(state,
468  prs.words[count].word,
469  prs.words[count].len,
470  weight,
471  ((prs.words[count].flags & TSL_PREFIX) || prefix));
472  pfree(prs.words[count].word);
473  if (cnt)
474  pushOperator(state, OP_AND, 0);
475  cnt++;
476  count++;
477  }
478 
479  if (cntvar)
480  pushOperator(state, OP_OR, 0);
481  cntvar++;
482  }
483 
484  if (cntpos)
485  {
486  /* distance may be useful */
487  pushOperator(state, data->qoperator, 1);
488  }
489 
490  cntpos++;
491  }
492 
493  pfree(prs.words);
494 
495  }
496  else
497  pushStop(state);
498 }
#define TSL_PREFIX
Definition: ts_public.h:116
uint16 nvariant
Definition: ts_utils.h:66
int32 lenwords
Definition: ts_utils.h:86
#define OP_OR
Definition: ts_type.h:177
int32 curwords
Definition: ts_utils.h:87
signed int int32
Definition: c.h:256
#define OP_AND
Definition: ts_type.h:176
void pfree(void *pointer)
Definition: mcxt.c:950
void parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
Definition: ts_parse.c:358
int qoperator
Definition: to_tsany.c:25
void pushStop(TSQueryParserState state)
Definition: tsquery.c:420
void pushValue(TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
Definition: tsquery.c:384
unsigned int uint32
Definition: c.h:268
ParsedWord * words
Definition: ts_utils.h:85
uint16 pos
Definition: ts_utils.h:69
void pushOperator(TSQueryParserState state, int8 oper, int16 distance)
Definition: tsquery.c:335
uint16 len
Definition: ts_utils.h:65
int32 pos
Definition: ts_utils.h:88
Oid cfg_id
Definition: to_tsany.c:24
#define DatumGetPointer(X)
Definition: postgres.h:555
uint16 flags
Definition: ts_utils.h:78
void * palloc(Size size)
Definition: mcxt.c:849
char * word
Definition: ts_utils.h:79
Datum to_tsquery ( PG_FUNCTION_ARGS  )

Definition at line 519 of file to_tsany.c.

References DirectFunctionCall2, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_TEXT_PP, PG_RETURN_DATUM, PointerGetDatum, and to_tsquery_byid().

520 {
521  text *in = PG_GETARG_TEXT_PP(0);
522  Oid cfgId;
523 
524  cfgId = getTSCurrentConfig(true);
526  ObjectIdGetDatum(cfgId),
527  PointerGetDatum(in)));
528 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PointerGetDatum(X)
Definition: postgres.h:562
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum to_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:501
Definition: c.h:439
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum to_tsquery_byid ( PG_FUNCTION_ARGS  )

Definition at line 501 of file to_tsany.c.

References MorphOpaque::cfg_id, OP_AND, parse_tsquery(), PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_TSQUERY, PointerGetDatum, pushval_morph(), MorphOpaque::qoperator, and text_to_cstring().

Referenced by to_tsquery().

502 {
503  text *in = PG_GETARG_TEXT_PP(1);
504  TSQuery query;
505  MorphOpaque data;
506 
507  data.cfg_id = PG_GETARG_OID(0);
508  data.qoperator = OP_AND;
509 
510  query = parse_tsquery(text_to_cstring(in),
512  PointerGetDatum(&data),
513  false);
514 
515  PG_RETURN_TSQUERY(query);
516 }
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_TSQUERY(x)
Definition: ts_type.h:249
#define OP_AND
Definition: ts_type.h:176
int qoperator
Definition: to_tsany.c:25
TSQuery parse_tsquery(char *buf, PushFunction pushval, Datum opaque, bool isplain)
Definition: tsquery.c:605
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
Oid cfg_id
Definition: to_tsany.c:24
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:439
static void pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
Definition: to_tsany.c:413
Datum to_tsvector ( PG_FUNCTION_ARGS  )

Definition at line 257 of file to_tsany.c.

References DirectFunctionCall2, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_TEXT_PP, PG_RETURN_DATUM, PointerGetDatum, and to_tsvector_byid().

Referenced by ts_match_tq(), and ts_match_tt().

258 {
259  text *in = PG_GETARG_TEXT_PP(0);
260  Oid cfgId;
261 
262  cfgId = getTSCurrentConfig(true);
264  ObjectIdGetDatum(cfgId),
265  PointerGetDatum(in)));
266 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PointerGetDatum(X)
Definition: postgres.h:562
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
Datum to_tsvector_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:225
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Definition: c.h:439
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum to_tsvector_byid ( PG_FUNCTION_ARGS  )

Definition at line 225 of file to_tsany.c.

References CALCDATASIZE, ParsedText::curwords, ParsedText::lenwords, make_tsvector(), palloc(), parsetext(), pfree(), PG_FREE_IF_COPY, PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_POINTER, ParsedText::pos, SET_VARSIZE, TSVectorData::size, VARDATA_ANY, VARSIZE_ANY_EXHDR, and ParsedText::words.

Referenced by to_tsvector().

226 {
227  Oid cfgId = PG_GETARG_OID(0);
228  text *in = PG_GETARG_TEXT_PP(1);
229  ParsedText prs;
230  TSVector out;
231 
232  prs.lenwords = VARSIZE_ANY_EXHDR(in) / 6; /* just estimation of word's
233  * number */
234  if (prs.lenwords == 0)
235  prs.lenwords = 2;
236  prs.curwords = 0;
237  prs.pos = 0;
238  prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);
239 
240  parsetext(cfgId, &prs, VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
241  PG_FREE_IF_COPY(in, 1);
242 
243  if (prs.curwords)
244  out = make_tsvector(&prs);
245  else
246  {
247  pfree(prs.words);
248  out = palloc(CALCDATASIZE(0, 0));
249  SET_VARSIZE(out, CALCDATASIZE(0, 0));
250  out->size = 0;
251  }
252 
253  PG_RETURN_POINTER(out);
254 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
TSVector make_tsvector(ParsedText *prs)
Definition: to_tsany.c:154
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
int32 lenwords
Definition: ts_utils.h:86
unsigned int Oid
Definition: postgres_ext.h:31
int32 curwords
Definition: ts_utils.h:87
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void pfree(void *pointer)
Definition: mcxt.c:950
void parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
Definition: ts_parse.c:358
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
int32 size
Definition: ts_type.h:102
ParsedWord * words
Definition: ts_utils.h:85
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
int32 pos
Definition: ts_utils.h:88
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
static int uniqueWORD ( ParsedWord a,
int32  l 
)
static

Definition at line 68 of file to_tsany.c.

References ParsedWord::alen, compareWORD(), ParsedWord::len, LIMITPOS, MAXENTRYPOS, MAXNUMPOS, palloc(), pfree(), ParsedWord::pos, qsort, repalloc(), and ParsedWord::word.

Referenced by make_tsvector().

69 {
70  ParsedWord *ptr,
71  *res;
72  int tmppos;
73 
74  if (l == 1)
75  {
76  tmppos = LIMITPOS(a->pos.pos);
77  a->alen = 2;
78  a->pos.apos = (uint16 *) palloc(sizeof(uint16) * a->alen);
79  a->pos.apos[0] = 1;
80  a->pos.apos[1] = tmppos;
81  return l;
82  }
83 
84  res = a;
85  ptr = a + 1;
86 
87  /*
88  * Sort words with its positions
89  */
90  qsort((void *) a, l, sizeof(ParsedWord), compareWORD);
91 
92  /*
93  * Initialize first word and its first position
94  */
95  tmppos = LIMITPOS(a->pos.pos);
96  a->alen = 2;
97  a->pos.apos = (uint16 *) palloc(sizeof(uint16) * a->alen);
98  a->pos.apos[0] = 1;
99  a->pos.apos[1] = tmppos;
100 
101  /*
102  * Summarize position information for each word
103  */
104  while (ptr - a < l)
105  {
106  if (!(ptr->len == res->len &&
107  strncmp(ptr->word, res->word, res->len) == 0))
108  {
109  /*
110  * Got a new word, so put it in result
111  */
112  res++;
113  res->len = ptr->len;
114  res->word = ptr->word;
115  tmppos = LIMITPOS(ptr->pos.pos);
116  res->alen = 2;
117  res->pos.apos = (uint16 *) palloc(sizeof(uint16) * res->alen);
118  res->pos.apos[0] = 1;
119  res->pos.apos[1] = tmppos;
120  }
121  else
122  {
123  /*
124  * The word already exists, so adjust position information. But
125  * before we should check size of position's array, max allowed
126  * value for position and uniqueness of position
127  */
128  pfree(ptr->word);
129  if (res->pos.apos[0] < MAXNUMPOS - 1 && res->pos.apos[res->pos.apos[0]] != MAXENTRYPOS - 1 &&
130  res->pos.apos[res->pos.apos[0]] != LIMITPOS(ptr->pos.pos))
131  {
132  if (res->pos.apos[0] + 1 >= res->alen)
133  {
134  res->alen *= 2;
135  res->pos.apos = (uint16 *) repalloc(res->pos.apos, sizeof(uint16) * res->alen);
136  }
137  if (res->pos.apos[0] == 0 || res->pos.apos[res->pos.apos[0]] != LIMITPOS(ptr->pos.pos))
138  {
139  res->pos.apos[res->pos.apos[0] + 1] = LIMITPOS(ptr->pos.pos);
140  res->pos.apos[0]++;
141  }
142  }
143  }
144  ptr++;
145  }
146 
147  return res + 1 - a;
148 }
unsigned short uint16
Definition: c.h:267
void pfree(void *pointer)
Definition: mcxt.c:950
#define MAXNUMPOS
Definition: ts_type.h:86
uint16 pos
Definition: ts_utils.h:69
static int compareWORD(const void *a, const void *b)
Definition: to_tsany.c:47
uint16 len
Definition: ts_utils.h:65
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963
uint32 alen
Definition: ts_utils.h:80
void * palloc(Size size)
Definition: mcxt.c:849
#define LIMITPOS(x)
Definition: ts_type.h:87
#define qsort(a, b, c, d)
Definition: port.h:440
#define MAXENTRYPOS
Definition: ts_type.h:85
char * word
Definition: ts_utils.h:79