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 366 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().

367 {
369  ParsedText *prs = state->prs;
370  TSVector item_vector;
371  int i;
372 
373  prs->lenwords = elem_len / 6;
374  if (prs->lenwords == 0)
375  prs->lenwords = 2;
376 
377  prs->words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs->lenwords);
378  prs->curwords = 0;
379  prs->pos = 0;
380 
381  parsetext(state->cfgId, prs, elem_value, elem_len);
382 
383  if (prs->curwords)
384  {
385  if (state->result != NULL)
386  {
387  for (i = 0; i < prs->curwords; i++)
388  prs->words[i].pos.pos = prs->words[i].pos.pos + TS_JUMP;
389 
390  item_vector = make_tsvector(prs);
391 
393  TSVectorGetDatum(state->result),
394  PointerGetDatum(item_vector));
395  }
396  else
397  state->result = make_tsvector(prs);
398  }
399 }
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:85
#define TS_JUMP
Definition: ts_type.h:96
int32 curwords
Definition: ts_utils.h:86
void parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
Definition: ts_parse.c:358
ParsedWord * words
Definition: ts_utils.h:84
#define TSVectorGetDatum(X)
Definition: ts_type.h:128
uint16 pos
Definition: ts_utils.h:68
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:87
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 350 of file to_tsany.c.

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

351 {
352  text *json = PG_GETARG_TEXT_P(0);
353  Oid cfgId;
354 
355  cfgId = getTSCurrentConfig(true);
357  ObjectIdGetDatum(cfgId),
358  PointerGetDatum(json)));
359 }
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:316
Datum json_to_tsvector_byid ( PG_FUNCTION_ARGS  )

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

317 {
318  Oid cfgId = PG_GETARG_OID(0);
319  text *json = PG_GETARG_TEXT_P(1);
321  ParsedText *prs = (ParsedText *) palloc(sizeof(ParsedText));
322 
323  prs->words = NULL;
324  state.result = NULL;
325  state.cfgId = cfgId;
326  state.prs = prs;
327 
329 
330  PG_FREE_IF_COPY(json, 1);
331  if (state.result == NULL)
332  {
333  /*
334  * There weren't any string elements in json, so wee need to return an
335  * empty vector
336  */
337 
338  if (prs->words != NULL)
339  pfree(prs->words);
340 
341  state.result = palloc(CALCDATASIZE(0, 0));
342  SET_VARSIZE(state.result, CALCDATASIZE(0, 0));
343  state.result->size = 0;
344  }
345 
346  PG_RETURN_TSVECTOR(state.result);
347 }
void iterate_json_string_values(text *json, void *action_state, JsonIterateStringValuesAction action)
Definition: jsonfuncs.c:4837
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:84
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:366
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 304 of file to_tsany.c.

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

305 {
306  Jsonb *jb = PG_GETARG_JSONB(0);
307  Oid cfgId;
308 
309  cfgId = getTSCurrentConfig(true);
311  ObjectIdGetDatum(cfgId),
312  JsonbGetDatum(jb)));
313 }
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  /*
288  * There weren't any string elements in jsonb, so wee need to return
289  * an empty vector
290  */
291 
292  if (prs->words != NULL)
293  pfree(prs->words);
294 
295  state.result = palloc(CALCDATASIZE(0, 0));
296  SET_VARSIZE(state.result, CALCDATASIZE(0, 0));
297  state.result->size = 0;
298  }
299 
300  PG_RETURN_TSVECTOR(state.result);
301 }
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:84
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:366
#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:4815
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:86
#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:84
uint16 pos
Definition: ts_utils.h:68
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:64
uint32 pos
Definition: ts_type.h:44
uint32 alen
Definition: ts_utils.h:79
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:78
Datum phraseto_tsquery ( PG_FUNCTION_ARGS  )

Definition at line 584 of file to_tsany.c.

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

585 {
586  text *in = PG_GETARG_TEXT_PP(0);
587  Oid cfgId;
588 
589  cfgId = getTSCurrentConfig(true);
591  ObjectIdGetDatum(cfgId),
592  PointerGetDatum(in)));
593 }
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:566
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum phraseto_tsquery_byid ( PG_FUNCTION_ARGS  )

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

567 {
568  text *in = PG_GETARG_TEXT_PP(1);
569  TSQuery query;
570  MorphOpaque data;
571 
572  data.cfg_id = PG_GETARG_OID(0);
573  data.qoperator = OP_PHRASE;
574 
575  query = parse_tsquery(text_to_cstring(in),
577  PointerGetDatum(&data),
578  true);
579 
580  PG_RETURN_TSQUERY(query);
581 }
#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:417
Datum plainto_tsquery ( PG_FUNCTION_ARGS  )

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

554 {
555  text *in = PG_GETARG_TEXT_PP(0);
556  Oid cfgId;
557 
558  cfgId = getTSCurrentConfig(true);
560  ObjectIdGetDatum(cfgId),
561  PointerGetDatum(in)));
562 }
Datum plainto_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:535
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 535 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().

536 {
537  text *in = PG_GETARG_TEXT_PP(1);
538  TSQuery query;
539  MorphOpaque data;
540 
541  data.cfg_id = PG_GETARG_OID(0);
542  data.qoperator = OP_AND;
543 
544  query = parse_tsquery(text_to_cstring(in),
546  PointerGetDatum(&data),
547  true);
548 
549  PG_RETURN_POINTER(query);
550 }
#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:417
static void pushval_morph ( Datum  opaque,
TSQueryParserState  state,
char *  strval,
int  lenval,
int16  weight,
bool  prefix 
)
static

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

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

Definition at line 523 of file to_tsany.c.

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

524 {
525  text *in = PG_GETARG_TEXT_PP(0);
526  Oid cfgId;
527 
528  cfgId = getTSCurrentConfig(true);
530  ObjectIdGetDatum(cfgId),
531  PointerGetDatum(in)));
532 }
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:505
Definition: c.h:439
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum to_tsquery_byid ( PG_FUNCTION_ARGS  )

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

506 {
507  text *in = PG_GETARG_TEXT_PP(1);
508  TSQuery query;
509  MorphOpaque data;
510 
511  data.cfg_id = PG_GETARG_OID(0);
512  data.qoperator = OP_AND;
513 
514  query = parse_tsquery(text_to_cstring(in),
516  PointerGetDatum(&data),
517  false);
518 
519  PG_RETURN_TSQUERY(query);
520 }
#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:417
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:85
unsigned int Oid
Definition: postgres_ext.h:31
int32 curwords
Definition: ts_utils.h:86
#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:84
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
int32 pos
Definition: ts_utils.h:87
#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:68
static int compareWORD(const void *a, const void *b)
Definition: to_tsany.c:47
uint16 len
Definition: ts_utils.h:64
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963
uint32 alen
Definition: ts_utils.h:79
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:78