PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
to_tsany.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * to_tsany.c
4  * to_ts* function definitions
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  *
8  *
9  * IDENTIFICATION
10  * src/backend/tsearch/to_tsany.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include "tsearch/ts_cache.h"
17 #include "tsearch/ts_utils.h"
18 #include "utils/builtins.h"
19 #include "utils/jsonapi.h"
20 
21 
22 typedef struct MorphOpaque
23 {
25  int qoperator; /* query operator */
26 } MorphOpaque;
27 
28 typedef struct TSVectorBuildState
29 {
34 
35 static void add_to_tsvector(void *state, char *elem_value, int elem_len);
36 
37 Datum
39 {
41 }
42 
43 /*
44  * to_tsvector
45  */
46 static int
47 compareWORD(const void *a, const void *b)
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 }
66 
67 static int
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 }
149 
150 /*
151  * make value of tsvector, given parsed text
152  */
153 TSVector
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 }
223 
224 Datum
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 }
255 
256 Datum
258 {
259  text *in = PG_GETARG_TEXT_PP(0);
260  Oid cfgId;
261 
262  cfgId = getTSCurrentConfig(true);
264  ObjectIdGetDatum(cfgId),
265  PointerGetDatum(in)));
266 }
267 
268 Datum
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 }
302 
303 Datum
305 {
306  Jsonb *jb = PG_GETARG_JSONB(0);
307  Oid cfgId;
308 
309  cfgId = getTSCurrentConfig(true);
311  ObjectIdGetDatum(cfgId),
312  JsonbGetDatum(jb)));
313 }
314 
315 Datum
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 }
348 
349 Datum
351 {
352  text *json = PG_GETARG_TEXT_P(0);
353  Oid cfgId;
354 
355  cfgId = getTSCurrentConfig(true);
357  ObjectIdGetDatum(cfgId),
358  PointerGetDatum(json)));
359 }
360 
361 /*
362  * Extend current TSVector from _state with a new one,
363  * build over a json(b) element.
364  */
365 static void
366 add_to_tsvector(void *_state, char *elem_value, int elem_len)
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 }
400 
401 /*
402  * to_tsquery
403  */
404 
405 
406 /*
407  * This function is used for morph parsing.
408  *
409  * The value is passed to parsetext which will call the right dictionary to
410  * lexize the word. If it turns out to be a stopword, we push a QI_VALSTOP
411  * to the stack.
412  *
413  * All words belonging to the same variant are pushed as an ANDed list,
414  * and different variants are ORed together.
415  */
416 static void
417 pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
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 }
503 
504 Datum
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 }
521 
522 Datum
524 {
525  text *in = PG_GETARG_TEXT_PP(0);
526  Oid cfgId;
527 
528  cfgId = getTSCurrentConfig(true);
530  ObjectIdGetDatum(cfgId),
531  PointerGetDatum(in)));
532 }
533 
534 Datum
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 }
551 
552 Datum
554 {
555  text *in = PG_GETARG_TEXT_PP(0);
556  Oid cfgId;
557 
558  cfgId = getTSCurrentConfig(true);
560  ObjectIdGetDatum(cfgId),
561  PointerGetDatum(in)));
562 }
563 
564 
565 Datum
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 }
582 
583 Datum
585 {
586  text *in = PG_GETARG_TEXT_PP(0);
587  Oid cfgId;
588 
589  cfgId = getTSCurrentConfig(true);
591  ObjectIdGetDatum(cfgId),
592  PointerGetDatum(in)));
593 }
Datum plainto_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:535
uint16 WordEntryPos
Definition: ts_type.h:63
signed short int16
Definition: c.h:255
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
void iterate_json_string_values(text *json, void *action_state, JsonIterateStringValuesAction action)
Definition: jsonfuncs.c:4837
struct TSVectorBuildState TSVectorBuildState
#define TSL_PREFIX
Definition: ts_public.h:116
TSVector make_tsvector(ParsedText *prs)
Definition: to_tsany.c:154
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
Definition: jsonb.h:215
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PointerGetDatum(X)
Definition: postgres.h:562
TSVector result
Definition: to_tsany.c:31
struct MorphOpaque MorphOpaque
uint16 nvariant
Definition: ts_utils.h:65
Datum tsvector_concat(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:933
uint32 len
Definition: ts_type.h:44
#define PG_RETURN_TSVECTOR(x)
Definition: ts_type.h:131
Datum plainto_tsquery(PG_FUNCTION_ARGS)
Definition: to_tsany.c:553
int errcode(int sqlerrcode)
Definition: elog.c:575
int32 lenwords
Definition: ts_utils.h:85
#define MAXSTRPOS
Definition: ts_type.h:50
unsigned int Oid
Definition: postgres_ext.h:31
#define OP_OR
Definition: ts_type.h:177
#define WEP_SETPOS(x, v)
Definition: ts_type.h:83
#define TS_JUMP
Definition: ts_type.h:96
int32 curwords
Definition: ts_utils.h:86
signed int int32
Definition: c.h:256
Datum get_current_ts_config(PG_FUNCTION_ARGS)
Definition: to_tsany.c:38
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define POSDATAPTR(x, e)
Definition: ts_type.h:120
Datum to_tsvector_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:225
#define PG_RETURN_TSQUERY(x)
Definition: ts_type.h:249
Datum phraseto_tsquery(PG_FUNCTION_ARGS)
Definition: to_tsany.c:584
#define OP_AND
Definition: ts_type.h:176
unsigned short uint16
Definition: c.h:267
void pfree(void *pointer)
Definition: mcxt.c:950
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
void parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
Definition: ts_parse.c:358
Datum json_to_tsvector(PG_FUNCTION_ARGS)
Definition: to_tsany.c:350
#define MAXNUMPOS
Definition: ts_type.h:86
int qoperator
Definition: to_tsany.c:25
void pushStop(TSQueryParserState state)
Definition: tsquery.c:420
#define WEP_SETWEIGHT(x, v)
Definition: ts_type.h:82
TSQuery parse_tsquery(char *buf, PushFunction pushval, Datum opaque, bool isplain)
Definition: tsquery.c:605
#define JsonbGetDatum(p)
Definition: jsonb.h:69
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
int32 size
Definition: ts_type.h:102
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
#define TSVectorGetDatum(X)
Definition: ts_type.h:128
uint16 pos
Definition: ts_utils.h:68
static int compareWORD(const void *a, const void *b)
Definition: to_tsany.c:47
void(* JsonIterateStringValuesAction)(void *state, char *elem_value, int elem_len)
Definition: jsonapi.h:136
uint32 haspos
Definition: ts_type.h:44
#define ereport(elevel, rest)
Definition: elog.h:122
ParsedText * prs
Definition: to_tsany.c:30
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
void * palloc0(Size size)
Definition: mcxt.c:878
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
TSVectorData * TSVector
Definition: ts_type.h:107
void pushOperator(TSQueryParserState state, int8 oper, int16 distance)
Definition: tsquery.c:335
#define NULL
Definition: c.h:229
Definition: regguts.h:298
#define OP_PHRASE
Definition: ts_type.h:178
uint16 len
Definition: ts_utils.h:64
Datum to_tsvector(PG_FUNCTION_ARGS)
Definition: to_tsany.c:257
int32 pos
Definition: ts_utils.h:87
Oid cfg_id
Definition: to_tsany.c:24
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1160
uint32 pos
Definition: ts_type.h:44
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963
#define DatumGetPointer(X)
Definition: postgres.h:555
uint16 flags
Definition: ts_utils.h:77
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1243
Datum to_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:505
uint32 alen
Definition: ts_utils.h:79
char * text_to_cstring(const text *t)
Definition: varlena.c:182
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
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:300
#define STRPTR(x)
Definition: hstore.h:76
int i
Datum jsonb_to_tsvector_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:269
Definition: c.h:439
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define LIMITPOS(x)
Definition: ts_type.h:87
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
Datum phraseto_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:566
#define qsort(a, b, c, d)
Definition: port.h:440
#define SHORTALIGN(LEN)
Definition: c.h:584
#define MAXENTRYPOS
Definition: ts_type.h:85
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
void iterate_jsonb_string_values(Jsonb *jb, void *state, JsonIterateStringValuesAction action)
Definition: jsonfuncs.c:4815
Datum jsonb_to_tsvector(PG_FUNCTION_ARGS)
Definition: to_tsany.c:304
static void pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
Definition: to_tsany.c:417
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum to_tsquery(PG_FUNCTION_ARGS)
Definition: to_tsany.c:523
char * word
Definition: ts_utils.h:78
Datum json_to_tsvector_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:316