Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * tsquery.c
4 : : * I/O functions for tsquery
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : *
8 : : *
9 : : * IDENTIFICATION
10 : : * src/backend/utils/adt/tsquery.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : :
15 : : #include "postgres.h"
16 : :
17 : : #include "libpq/pqformat.h"
18 : : #include "miscadmin.h"
19 : : #include "nodes/miscnodes.h"
20 : : #include "tsearch/ts_locale.h"
21 : : #include "tsearch/ts_type.h"
22 : : #include "tsearch/ts_utils.h"
23 : : #include "utils/builtins.h"
24 : : #include "utils/memutils.h"
25 : : #include "utils/pg_crc.h"
26 : : #include "varatt.h"
27 : :
28 : : /* FTS operator priorities, see ts_type.h */
29 : : const int tsearch_op_priority[OP_COUNT] =
30 : : {
31 : : 4, /* OP_NOT */
32 : : 2, /* OP_AND */
33 : : 1, /* OP_OR */
34 : : 3 /* OP_PHRASE */
35 : : };
36 : :
37 : : /*
38 : : * parser's states
39 : : */
40 : : typedef enum
41 : : {
42 : : WAITOPERAND = 1,
43 : : WAITOPERATOR = 2,
44 : : WAITFIRSTOPERAND = 3,
45 : : } ts_parserstate;
46 : :
47 : : /*
48 : : * token types for parsing
49 : : */
50 : : typedef enum
51 : : {
52 : : PT_END = 0,
53 : : PT_ERR = 1,
54 : : PT_VAL = 2,
55 : : PT_OPR = 3,
56 : : PT_OPEN = 4,
57 : : PT_CLOSE = 5,
58 : : } ts_tokentype;
59 : :
60 : : /*
61 : : * get token from query string
62 : : *
63 : : * All arguments except "state" are output arguments.
64 : : *
65 : : * If return value is PT_OPR, then *operator is filled with an OP_* code
66 : : * and *weight will contain a distance value in case of phrase operator.
67 : : *
68 : : * If return value is PT_VAL, then *lenval, *strval, *weight, and *prefix
69 : : * are filled.
70 : : *
71 : : * If PT_ERR is returned then a soft error has occurred. If state->escontext
72 : : * isn't already filled then this should be reported as a generic parse error.
73 : : */
74 : : typedef ts_tokentype (*ts_tokenizer) (TSQueryParserState state, int8 *operator,
75 : : int *lenval, char **strval,
76 : : int16 *weight, bool *prefix);
77 : :
78 : : struct TSQueryParserStateData
79 : : {
80 : : /* Tokenizer used for parsing tsquery */
81 : : ts_tokenizer gettoken;
82 : :
83 : : /* State of tokenizer function */
84 : : char *buffer; /* entire string we are scanning */
85 : : char *buf; /* current scan point */
86 : : int count; /* nesting count, incremented by (,
87 : : * decremented by ) */
88 : : ts_parserstate state;
89 : :
90 : : /* polish (prefix) notation in list, filled in by push* functions */
91 : : List *polstr;
92 : :
93 : : /*
94 : : * Strings from operands are collected in op. curop is a pointer to the
95 : : * end of used space of op.
96 : : */
97 : : char *op;
98 : : char *curop;
99 : : int lenop; /* allocated size of op */
100 : : int sumlen; /* used size of op */
101 : :
102 : : /* state for value's parser */
103 : : TSVectorParseState valstate;
104 : :
105 : : /* context object for soft errors - must match valstate's escontext */
106 : : Node *escontext;
107 : : };
108 : :
109 : : /*
110 : : * subroutine to parse the modifiers (weight and prefix flag currently)
111 : : * part, like ':AB*' of a query.
112 : : */
113 : : static char *
6723 tgl@sss.pgh.pa.us 114 :CBC 5076 : get_modifiers(char *buf, int16 *weight, bool *prefix)
115 : : {
6992 116 : 5076 : *weight = 0;
6723 117 : 5076 : *prefix = false;
118 : :
6992 119 [ + + ]: 5076 : if (!t_iseq(buf, ':'))
120 : 4641 : return buf;
121 : :
122 : 435 : buf++;
278 tmunro@postgresql.or 123 [ + + + - ]: 1016 : while (*buf && pg_mblen_cstr(buf) == 1)
124 : : {
6992 tgl@sss.pgh.pa.us 125 [ + + + + : 736 : switch (*buf)
+ + ]
126 : : {
127 : 160 : case 'a':
128 : : case 'A':
129 : 160 : *weight |= 1 << 3;
130 : 160 : break;
131 : 50 : case 'b':
132 : : case 'B':
133 : 50 : *weight |= 1 << 2;
134 : 50 : break;
135 : 78 : case 'c':
136 : : case 'C':
137 : 78 : *weight |= 1 << 1;
138 : 78 : break;
139 : 81 : case 'd':
140 : : case 'D':
141 : 81 : *weight |= 1;
142 : 81 : break;
6723 143 : 212 : case '*':
144 : 212 : *prefix = true;
145 : 212 : break;
6992 146 : 155 : default:
147 : 155 : return buf;
148 : : }
149 : 581 : buf++;
150 : : }
151 : :
152 : 280 : return buf;
153 : : }
154 : :
155 : : /*
156 : : * Parse phrase operator. The operator
157 : : * may take the following forms:
158 : : *
159 : : * a <N> b (distance is exactly N lexemes)
160 : : * a <-> b (default distance = 1)
161 : : *
162 : : * The buffer should begin with '<' char
163 : : */
164 : : static bool
3112 teodor@sigaev.ru 165 : 6469 : parse_phrase_operator(TSQueryParserState pstate, int16 *distance)
166 : : {
167 : : enum
168 : : {
169 : : PHRASE_OPEN = 0,
170 : : PHRASE_DIST,
171 : : PHRASE_CLOSE,
172 : : PHRASE_FINISH
3777 rhaas@postgresql.org 173 : 6469 : } state = PHRASE_OPEN;
3112 teodor@sigaev.ru 174 : 6469 : char *ptr = pstate->buf;
175 : : char *endptr;
3395 tgl@sss.pgh.pa.us 176 : 6469 : long l = 1; /* default distance */
177 : :
3840 teodor@sigaev.ru 178 [ + + ]: 16716 : while (*ptr)
179 : : {
3777 rhaas@postgresql.org 180 [ + + + + : 7939 : switch (state)
- ]
181 : : {
3840 teodor@sigaev.ru 182 : 4161 : case PHRASE_OPEN:
3112 183 [ + + ]: 4161 : if (t_iseq(ptr, '<'))
184 : : {
185 : 1262 : state = PHRASE_DIST;
186 : 1262 : ptr++;
187 : : }
188 : : else
189 : 2899 : return false;
3840 190 : 1262 : break;
191 : :
192 : 1262 : case PHRASE_DIST:
193 [ + + ]: 1262 : if (t_iseq(ptr, '-'))
194 : : {
195 : 1054 : state = PHRASE_CLOSE;
196 : 1054 : ptr++;
3112 197 : 1054 : continue;
198 : : }
199 : :
664 peter@eisentraut.org 200 [ - + ]: 208 : if (!isdigit((unsigned char) *ptr))
3112 teodor@sigaev.ru 201 :UBC 0 : return false;
202 : :
3395 tgl@sss.pgh.pa.us 203 :CBC 208 : errno = 0;
3840 teodor@sigaev.ru 204 : 208 : l = strtol(ptr, &endptr, 10);
205 [ - + ]: 208 : if (ptr == endptr)
3112 teodor@sigaev.ru 206 :UBC 0 : return false;
3395 tgl@sss.pgh.pa.us 207 [ + - + - :CBC 208 : else if (errno == ERANGE || l < 0 || l > MAXENTRYPOS)
+ + ]
1385 208 [ + - ]: 4 : ereturn(pstate->escontext, false,
209 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
210 : : errmsg("distance in phrase operator must be an integer value between zero and %d inclusive",
211 : : MAXENTRYPOS)));
212 : : else
213 : : {
3840 teodor@sigaev.ru 214 : 204 : state = PHRASE_CLOSE;
215 : 204 : ptr = endptr;
216 : : }
217 : 204 : break;
218 : :
219 : 1258 : case PHRASE_CLOSE:
220 [ + - ]: 1258 : if (t_iseq(ptr, '>'))
221 : : {
222 : 1258 : state = PHRASE_FINISH;
223 : 1258 : ptr++;
224 : : }
225 : : else
3112 teodor@sigaev.ru 226 :UBC 0 : return false;
3840 teodor@sigaev.ru 227 :CBC 1258 : break;
228 : :
229 : 1258 : case PHRASE_FINISH:
230 : 1258 : *distance = (int16) l;
3112 231 : 1258 : pstate->buf = ptr;
232 : 1258 : return true;
233 : : }
234 : : }
235 : :
236 : 2308 : return false;
237 : : }
238 : :
239 : : /*
240 : : * Parse OR operator used in websearch_to_tsquery(), returns true if we
241 : : * believe that "OR" literal could be an operator OR
242 : : */
243 : : static bool
244 : 1165 : parse_or_operator(TSQueryParserState pstate)
245 : : {
3091 tgl@sss.pgh.pa.us 246 : 1165 : char *ptr = pstate->buf;
247 : :
248 : : /* it should begin with "OR" literal */
3112 teodor@sigaev.ru 249 [ + + ]: 1165 : if (pg_strncasecmp(ptr, "or", 2) != 0)
250 : 1040 : return false;
251 : :
252 : 125 : ptr += 2;
253 : :
254 : : /*
255 : : * it shouldn't be a part of any word but somewhere later it should be
256 : : * some operand
257 : : */
3091 tgl@sss.pgh.pa.us 258 [ + + ]: 125 : if (*ptr == '\0') /* no operand */
3112 teodor@sigaev.ru 259 : 5 : return false;
260 : :
261 : : /* it shouldn't be a part of any word */
278 tmunro@postgresql.or 262 [ + + + + : 120 : if (t_iseq(ptr, '-') || t_iseq(ptr, '_') || t_isalnum_cstr(ptr))
+ + ]
3112 teodor@sigaev.ru 263 : 20 : return false;
264 : :
265 : : for (;;)
266 : : {
278 tmunro@postgresql.or 267 : 100 : ptr += pg_mblen_cstr(ptr);
268 : :
3091 tgl@sss.pgh.pa.us 269 [ + + ]: 100 : if (*ptr == '\0') /* got end of string without operand */
3112 teodor@sigaev.ru 270 : 10 : return false;
271 : :
272 : : /*
273 : : * Suppose, we found an operand, but could be a not correct operand.
274 : : * So we still treat OR literal as operation with possibly incorrect
275 : : * operand and will not search it as lexeme
276 : : */
664 peter@eisentraut.org 277 [ + - ]: 90 : if (!isspace((unsigned char) *ptr))
3112 teodor@sigaev.ru 278 : 90 : break;
279 : : }
280 : :
281 : 90 : pstate->buf += 2;
282 : 90 : return true;
283 : : }
284 : :
285 : : static ts_tokentype
286 : 12343 : gettoken_query_standard(TSQueryParserState state, int8 *operator,
287 : : int *lenval, char **strval,
288 : : int16 *weight, bool *prefix)
289 : : {
6723 tgl@sss.pgh.pa.us 290 : 12343 : *weight = 0;
291 : 12343 : *prefix = false;
292 : :
293 : : while (true)
294 : : {
6992 295 [ + + - ]: 16556 : switch (state->state)
296 : : {
297 : 8589 : case WAITFIRSTOPERAND:
298 : : case WAITOPERAND:
299 [ + + ]: 8589 : if (t_iseq(state->buf, '!'))
300 : : {
3112 teodor@sigaev.ru 301 : 627 : state->buf++;
6992 tgl@sss.pgh.pa.us 302 : 627 : state->state = WAITOPERAND;
3112 teodor@sigaev.ru 303 : 627 : *operator = OP_NOT;
6975 304 : 627 : return PT_OPR;
305 : : }
6992 tgl@sss.pgh.pa.us 306 [ + + ]: 7962 : else if (t_iseq(state->buf, '('))
307 : : {
3112 teodor@sigaev.ru 308 : 773 : state->buf++;
6992 tgl@sss.pgh.pa.us 309 : 773 : state->state = WAITOPERAND;
3112 teodor@sigaev.ru 310 : 773 : state->count++;
6975 311 : 773 : return PT_OPEN;
312 : : }
6992 tgl@sss.pgh.pa.us 313 [ - + ]: 7189 : else if (t_iseq(state->buf, ':'))
314 : : {
315 : : /* generic syntax error message is fine */
1385 tgl@sss.pgh.pa.us 316 :UBC 0 : return PT_ERR;
317 : : }
664 peter@eisentraut.org 318 [ + + ]:CBC 7189 : else if (!isspace((unsigned char) *state->buf))
319 : : {
320 : : /*
321 : : * We rely on the tsvector parser to parse the value for
322 : : * us
323 : : */
6975 teodor@sigaev.ru 324 : 5094 : reset_tsvector_parser(state->valstate, state->buf);
3112 325 [ + + ]: 5094 : if (gettoken_tsvector(state->valstate, strval, lenval,
326 : : NULL, NULL, &state->buf))
327 : : {
6723 tgl@sss.pgh.pa.us 328 : 5076 : state->buf = get_modifiers(state->buf, weight, prefix);
6992 329 : 5076 : state->state = WAITOPERATOR;
6975 teodor@sigaev.ru 330 : 5076 : return PT_VAL;
331 : : }
1385 tgl@sss.pgh.pa.us 332 [ - + - - : 18 : else if (SOFT_ERROR_OCCURRED(state->escontext))
- - ]
333 : : {
334 : : /* gettoken_tsvector reported a soft error */
1385 tgl@sss.pgh.pa.us 335 :UBC 0 : return PT_ERR;
336 : : }
6992 tgl@sss.pgh.pa.us 337 [ + - ]:CBC 18 : else if (state->state == WAITFIRSTOPERAND)
338 : : {
6975 teodor@sigaev.ru 339 : 18 : return PT_END;
340 : : }
341 : : else
1385 tgl@sss.pgh.pa.us 342 [ # # ]:UBC 0 : ereturn(state->escontext, PT_ERR,
343 : : (errcode(ERRCODE_SYNTAX_ERROR),
344 : : errmsg("no operand in tsquery: \"%s\"",
345 : : state->buffer)));
346 : : }
6992 tgl@sss.pgh.pa.us 347 :CBC 2095 : break;
348 : :
349 : 7967 : case WAITOPERATOR:
6975 teodor@sigaev.ru 350 [ + + ]: 7967 : if (t_iseq(state->buf, '&'))
351 : : {
3112 352 : 942 : state->buf++;
6975 353 : 942 : state->state = WAITOPERAND;
354 : 942 : *operator = OP_AND;
355 : 942 : return PT_OPR;
356 : : }
3840 357 [ + + ]: 7025 : else if (t_iseq(state->buf, '|'))
358 : : {
3112 359 : 556 : state->buf++;
6992 tgl@sss.pgh.pa.us 360 : 556 : state->state = WAITOPERAND;
6975 teodor@sigaev.ru 361 : 556 : *operator = OP_OR;
362 : 556 : return PT_OPR;
363 : : }
3112 364 [ + + ]: 6469 : else if (parse_phrase_operator(state, weight))
365 : : {
366 : : /* weight var is used as storage for distance */
3840 367 : 1258 : state->state = WAITOPERAND;
368 : 1258 : *operator = OP_PHRASE;
369 : 1258 : return PT_OPR;
370 : : }
1385 tgl@sss.pgh.pa.us 371 [ + + + - : 5211 : else if (SOFT_ERROR_OCCURRED(state->escontext))
+ + ]
372 : : {
373 : : /* parse_phrase_operator reported a soft error */
374 : 4 : return PT_ERR;
375 : : }
6992 376 [ + + ]: 5207 : else if (t_iseq(state->buf, ')'))
377 : : {
3112 teodor@sigaev.ru 378 : 773 : state->buf++;
6992 tgl@sss.pgh.pa.us 379 : 773 : state->count--;
6975 teodor@sigaev.ru 380 [ - + ]: 773 : return (state->count < 0) ? PT_ERR : PT_CLOSE;
381 : : }
3112 382 [ + + ]: 4434 : else if (*state->buf == '\0')
383 : : {
6975 384 : 2308 : return (state->count) ? PT_ERR : PT_END;
385 : : }
664 peter@eisentraut.org 386 [ + + ]: 2126 : else if (!isspace((unsigned char) *state->buf))
387 : : {
6975 teodor@sigaev.ru 388 : 8 : return PT_ERR;
389 : : }
3112 390 : 2118 : break;
391 : : }
392 : :
278 tmunro@postgresql.or 393 : 4213 : state->buf += pg_mblen_cstr(state->buf);
394 : : }
395 : : }
396 : :
397 : : static ts_tokentype
3112 teodor@sigaev.ru 398 : 1877 : gettoken_query_websearch(TSQueryParserState state, int8 *operator,
399 : : int *lenval, char **strval,
400 : : int16 *weight, bool *prefix)
401 : : {
402 : 1877 : *weight = 0;
403 : 1877 : *prefix = false;
404 : :
405 : : while (true)
406 : : {
407 [ + + - ]: 2622 : switch (state->state)
408 : : {
409 : 1116 : case WAITFIRSTOPERAND:
410 : : case WAITOPERAND:
411 [ + + ]: 1116 : if (t_iseq(state->buf, '-'))
412 : : {
413 : 55 : state->buf++;
414 : 55 : state->state = WAITOPERAND;
415 : :
416 : 55 : *operator = OP_NOT;
417 : 55 : return PT_OPR;
418 : : }
419 [ + + ]: 1061 : else if (t_iseq(state->buf, '"'))
420 : : {
421 : : /* Everything in quotes is processed as a single token */
422 : :
423 : : /* skip opening quote */
424 : 160 : state->buf++;
1988 akorotkov@postgresql 425 : 160 : *strval = state->buf;
426 : :
427 : : /* iterate to the closing quote or end of the string */
428 [ + + + + ]: 1450 : while (*state->buf != '\0' && !t_iseq(state->buf, '"'))
429 : 1290 : state->buf++;
430 : 160 : *lenval = state->buf - *strval;
431 : :
432 : : /* skip closing quote if not end of the string */
433 [ + + ]: 160 : if (*state->buf != '\0')
434 : 140 : state->buf++;
435 : :
436 : 160 : state->state = WAITOPERATOR;
437 : 160 : state->count++;
438 : 160 : return PT_VAL;
439 : : }
3112 teodor@sigaev.ru 440 [ + + + + : 901 : else if (ISOPERATOR(state->buf))
+ + + + +
+ + + ]
441 : : {
442 : : /* ignore, else gettoken_tsvector() will raise an error */
443 : 85 : state->buf++;
444 : 85 : state->state = WAITOPERAND;
445 : 85 : continue;
446 : : }
664 peter@eisentraut.org 447 [ + + ]: 816 : else if (!isspace((unsigned char) *state->buf))
448 : : {
449 : : /*
450 : : * We rely on the tsvector parser to parse the value for
451 : : * us
452 : : */
3112 teodor@sigaev.ru 453 : 751 : reset_tsvector_parser(state->valstate, state->buf);
454 [ + - ]: 751 : if (gettoken_tsvector(state->valstate, strval, lenval,
455 : : NULL, NULL, &state->buf))
456 : : {
457 : 751 : state->state = WAITOPERATOR;
458 : 751 : return PT_VAL;
459 : : }
1385 tgl@sss.pgh.pa.us 460 [ # # # # :UBC 0 : else if (SOFT_ERROR_OCCURRED(state->escontext))
# # ]
461 : : {
462 : : /* gettoken_tsvector reported a soft error */
463 : 0 : return PT_ERR;
464 : : }
3112 teodor@sigaev.ru 465 [ # # ]: 0 : else if (state->state == WAITFIRSTOPERAND)
466 : : {
467 : 0 : return PT_END;
468 : : }
469 : : else
470 : : {
471 : : /* finally, we have to provide an operand */
472 : 0 : pushStop(state);
473 : 0 : return PT_END;
474 : : }
475 : : }
6992 tgl@sss.pgh.pa.us 476 :CBC 65 : break;
477 : :
3112 teodor@sigaev.ru 478 : 1506 : case WAITOPERATOR:
851 tgl@sss.pgh.pa.us 479 [ + + ]: 1506 : if (*state->buf == '\0')
480 : : {
481 : 341 : return PT_END;
482 : : }
3112 teodor@sigaev.ru 483 [ + + ]: 1165 : else if (parse_or_operator(state))
484 : : {
485 : 90 : state->state = WAITOPERAND;
486 : 90 : *operator = OP_OR;
487 : 90 : return PT_OPR;
488 : : }
851 tgl@sss.pgh.pa.us 489 [ + + + + : 1075 : else if (ISOPERATOR(state->buf))
+ + + + +
+ + + ]
490 : : {
491 : : /* ignore other operators in this state too */
492 : 95 : state->buf++;
493 : 95 : continue;
494 : : }
664 peter@eisentraut.org 495 [ + + ]: 980 : else if (!isspace((unsigned char) *state->buf))
496 : : {
497 : : /* insert implicit AND between operands */
3112 teodor@sigaev.ru 498 : 480 : state->state = WAITOPERAND;
851 tgl@sss.pgh.pa.us 499 : 480 : *operator = OP_AND;
3112 teodor@sigaev.ru 500 : 480 : return PT_OPR;
501 : : }
6992 tgl@sss.pgh.pa.us 502 : 500 : break;
503 : : }
504 : :
278 tmunro@postgresql.or 505 : 565 : state->buf += pg_mblen_cstr(state->buf);
506 : : }
507 : : }
508 : :
509 : : static ts_tokentype
3112 teodor@sigaev.ru 510 : 176 : gettoken_query_plain(TSQueryParserState state, int8 *operator,
511 : : int *lenval, char **strval,
512 : : int16 *weight, bool *prefix)
513 : : {
514 : 176 : *weight = 0;
515 : 176 : *prefix = false;
516 : :
517 [ + + ]: 176 : if (*state->buf == '\0')
518 : 88 : return PT_END;
519 : :
520 : 88 : *strval = state->buf;
521 : 88 : *lenval = strlen(state->buf);
522 : 88 : state->buf += *lenval;
523 : 88 : state->count++;
524 : 88 : return PT_VAL;
525 : : }
526 : :
527 : : /*
528 : : * Push an operator to state->polstr
529 : : */
530 : : void
3840 531 : 4560 : pushOperator(TSQueryParserState state, int8 oper, int16 distance)
532 : : {
533 : : QueryOperator *tmp;
534 : :
535 [ + + + + : 4560 : Assert(oper == OP_NOT || oper == OP_AND || oper == OP_OR || oper == OP_PHRASE);
+ + - + ]
536 : :
306 michael@paquier.xyz 537 : 4560 : tmp = palloc0_object(QueryOperator);
6975 teodor@sigaev.ru 538 : 4560 : tmp->type = QI_OPR;
539 : 4560 : tmp->oper = oper;
3840 540 [ + + ]: 4560 : tmp->distance = (oper == OP_PHRASE) ? distance : 0;
541 : : /* left is filled in later with findoprnd */
542 : :
6975 543 : 4560 : state->polstr = lcons(tmp, state->polstr);
544 : 4560 : }
545 : :
546 : : static void
6723 tgl@sss.pgh.pa.us 547 : 6077 : pushValue_internal(TSQueryParserState state, pg_crc32 valcrc, int distance, int lenval, int weight, bool prefix)
548 : : {
549 : : QueryOperand *tmp;
550 : :
60 551 [ - + ]: 6077 : if (distance > MAXSTRPOS)
1385 tgl@sss.pgh.pa.us 552 [ # # ]:UBC 0 : ereturn(state->escontext,,
553 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
554 : : errmsg("value is too big in tsquery: \"%s\"",
555 : : state->buffer)));
60 tgl@sss.pgh.pa.us 556 [ - + ]:CBC 6077 : if (lenval > MAXSTRLEN)
1385 tgl@sss.pgh.pa.us 557 [ # # ]:UBC 0 : ereturn(state->escontext,,
558 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
559 : : errmsg("operand is too long in tsquery: \"%s\"",
560 : : state->buffer)));
561 : :
306 michael@paquier.xyz 562 :CBC 6077 : tmp = palloc0_object(QueryOperand);
6975 teodor@sigaev.ru 563 : 6077 : tmp->type = QI_VAL;
564 : 6077 : tmp->weight = weight;
6723 tgl@sss.pgh.pa.us 565 : 6077 : tmp->prefix = prefix;
6975 teodor@sigaev.ru 566 : 6077 : tmp->valcrc = (int32) valcrc;
6992 tgl@sss.pgh.pa.us 567 : 6077 : tmp->length = lenval;
6975 teodor@sigaev.ru 568 : 6077 : tmp->distance = distance;
569 : :
570 : 6077 : state->polstr = lcons(tmp, state->polstr);
571 : : }
572 : :
573 : : /*
574 : : * Push an operand to state->polstr.
575 : : *
576 : : * strval must point to a string equal to state->curop. lenval is the length
577 : : * of the string.
578 : : */
579 : : void
5222 peter_e@gmx.net 580 : 6077 : pushValue(TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
581 : : {
582 : : pg_crc32 valcrc;
583 : :
60 tgl@sss.pgh.pa.us 584 [ - + ]: 6077 : if (lenval > MAXSTRLEN)
1385 tgl@sss.pgh.pa.us 585 [ # # ]:UBC 0 : ereturn(state->escontext,,
586 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
587 : : errmsg("word is too long in tsquery: \"%s\"",
588 : : state->buffer)));
589 : :
4360 heikki.linnakangas@i 590 :CBC 6077 : INIT_LEGACY_CRC32(valcrc);
591 [ + + ]: 21903 : COMP_LEGACY_CRC32(valcrc, strval, lenval);
592 : 6077 : FIN_LEGACY_CRC32(valcrc);
6723 tgl@sss.pgh.pa.us 593 : 6077 : pushValue_internal(state, valcrc, state->curop - state->op, lenval, weight, prefix);
594 : :
595 : : /* append the value string to state.op, enlarging buffer if needed first */
6992 596 [ - + ]: 6077 : while (state->curop - state->op + lenval + 1 >= state->lenop)
597 : : {
6906 bruce@momjian.us 598 :UBC 0 : int used = state->curop - state->op;
599 : :
6992 tgl@sss.pgh.pa.us 600 : 0 : state->lenop *= 2;
1343 peter@eisentraut.org 601 : 0 : state->op = (char *) repalloc(state->op, state->lenop);
6975 teodor@sigaev.ru 602 : 0 : state->curop = state->op + used;
603 : : }
1343 peter@eisentraut.org 604 :CBC 6077 : memcpy(state->curop, strval, lenval);
6992 tgl@sss.pgh.pa.us 605 : 6077 : state->curop += lenval;
606 : 6077 : *(state->curop) = '\0';
607 : 6077 : state->curop++;
608 : 6077 : state->sumlen += lenval + 1 /* \0 */ ;
609 : : }
610 : :
611 : :
612 : : /*
613 : : * Push a stopword placeholder to state->polstr
614 : : */
615 : : void
6975 teodor@sigaev.ru 616 : 550 : pushStop(TSQueryParserState state)
617 : : {
618 : : QueryOperand *tmp;
619 : :
306 michael@paquier.xyz 620 : 550 : tmp = palloc0_object(QueryOperand);
6975 teodor@sigaev.ru 621 : 550 : tmp->type = QI_VALSTOP;
622 : :
623 : 550 : state->polstr = lcons(tmp, state->polstr);
624 : 550 : }
625 : :
626 : :
627 : : #define STACKDEPTH 32
628 : :
629 : : typedef struct OperatorElement
630 : : {
631 : : int8 op;
632 : : int16 distance;
633 : : } OperatorElement;
634 : :
635 : : static void
7 tgl@sss.pgh.pa.us 636 : 4008 : pushOpStack(TSQueryParserState state,
637 : : OperatorElement *stack, int *lenstack,
638 : : int8 op, int16 distance)
639 : : {
640 [ - + ]: 4008 : if (*lenstack >= STACKDEPTH)
7 tgl@sss.pgh.pa.us 641 [ # # ]:UBC 0 : ereturn(state->escontext,,
642 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
643 : : errmsg("tsquery is too complex")));
644 : :
3759 teodor@sigaev.ru 645 :CBC 4008 : stack[*lenstack].op = op;
646 : 4008 : stack[*lenstack].distance = distance;
647 : :
648 : 4008 : (*lenstack)++;
649 : : }
650 : :
651 : : static void
652 : 7536 : cleanOpStack(TSQueryParserState state,
653 : : OperatorElement *stack, int *lenstack, int8 op)
654 : : {
3710 tgl@sss.pgh.pa.us 655 : 7536 : int opPriority = OP_PRIORITY(op);
656 : :
657 [ + + ]: 11544 : while (*lenstack)
658 : : {
659 : : /* NOT is right associative unlike to others */
3741 teodor@sigaev.ru 660 [ + + + + : 4351 : if ((op != OP_NOT && opPriority > OP_PRIORITY(stack[*lenstack - 1].op)) ||
+ + ]
3400 tgl@sss.pgh.pa.us 661 [ - + ]: 228 : (op == OP_NOT && opPriority >= OP_PRIORITY(stack[*lenstack - 1].op)))
662 : : break;
663 : :
3759 teodor@sigaev.ru 664 : 4008 : (*lenstack)--;
665 : 4008 : pushOperator(state, stack[*lenstack].op,
3710 tgl@sss.pgh.pa.us 666 : 4008 : stack[*lenstack].distance);
667 : : }
3759 teodor@sigaev.ru 668 : 7536 : }
669 : :
670 : : /*
671 : : * Make polish (prefix) notation of query.
672 : : *
673 : : * See parse_tsquery for explanation of pushval.
674 : : */
675 : : static void
6906 bruce@momjian.us 676 : 3540 : makepol(TSQueryParserState state,
677 : : PushFunction pushval,
678 : : void *opaque)
679 : : {
3777 rhaas@postgresql.org 680 : 3540 : int8 operator = 0;
681 : : ts_tokentype type;
682 : 3540 : int lenval = 0;
683 : 3540 : char *strval = NULL;
684 : : OperatorElement opstack[STACKDEPTH];
685 : 3540 : int lenstack = 0;
686 : 3540 : int16 weight = 0;
687 : : bool prefix;
688 : :
689 : : /* since this function recurses, it could be driven to stack overflow */
6982 tgl@sss.pgh.pa.us 690 : 3540 : check_stack_depth();
691 : :
3112 teodor@sigaev.ru 692 : 14396 : while ((type = state->gettoken(state, &operator,
693 : : &lenval, &strval,
694 [ + + ]: 14396 : &weight, &prefix)) != PT_END)
695 : : {
6992 tgl@sss.pgh.pa.us 696 [ + + + + : 11641 : switch (type)
+ ]
697 : : {
6975 teodor@sigaev.ru 698 : 6075 : case PT_VAL:
6723 tgl@sss.pgh.pa.us 699 : 6075 : pushval(opaque, state, strval, lenval, weight, prefix);
6992 700 : 6075 : break;
6975 teodor@sigaev.ru 701 : 4008 : case PT_OPR:
3759 702 : 4008 : cleanOpStack(state, opstack, &lenstack, operator);
7 tgl@sss.pgh.pa.us 703 : 4008 : pushOpStack(state, opstack, &lenstack, operator, weight);
6992 704 : 4008 : break;
6975 teodor@sigaev.ru 705 : 773 : case PT_OPEN:
706 : 773 : makepol(state, pushval, opaque);
6992 tgl@sss.pgh.pa.us 707 : 773 : break;
6975 teodor@sigaev.ru 708 : 773 : case PT_CLOSE:
3710 tgl@sss.pgh.pa.us 709 : 773 : cleanOpStack(state, opstack, &lenstack, OP_OR /* lowest */ );
6975 teodor@sigaev.ru 710 : 785 : return;
711 : 12 : case PT_ERR:
712 : : default:
713 : : /* don't overwrite a soft error saved by gettoken function */
1385 tgl@sss.pgh.pa.us 714 [ + - + - : 12 : if (!SOFT_ERROR_OCCURRED(state->escontext))
+ + ]
715 [ + + ]: 8 : errsave(state->escontext,
716 : : (errcode(ERRCODE_SYNTAX_ERROR),
717 : : errmsg("syntax error in tsquery: \"%s\"",
718 : : state->buffer)));
719 : 12 : return;
720 : : }
721 : : /* detect soft error in pushval, pushOpStack, or recursion */
722 [ + + + - : 10856 : if (SOFT_ERROR_OCCURRED(state->escontext))
- + ]
1385 tgl@sss.pgh.pa.us 723 :UBC 0 : return;
724 : : }
725 : :
3710 tgl@sss.pgh.pa.us 726 :CBC 2755 : cleanOpStack(state, opstack, &lenstack, OP_OR /* lowest */ );
727 : : }
728 : :
729 : : static void
3840 teodor@sigaev.ru 730 : 11175 : findoprnd_recurse(QueryItem *ptr, uint32 *pos, int nnodes, bool *needcleanup)
731 : : {
732 : : /* since this function recurses, it could be driven to stack overflow. */
6975 733 : 11175 : check_stack_depth();
734 : :
735 [ - + ]: 11175 : if (*pos >= nnodes)
6893 tgl@sss.pgh.pa.us 736 [ # # ]:UBC 0 : elog(ERROR, "malformed tsquery: operand not found");
737 : :
3840 teodor@sigaev.ru 738 [ + + ]:CBC 11175 : if (ptr[*pos].type == QI_VAL)
739 : : {
740 : 6065 : (*pos)++;
741 : : }
742 [ + + ]: 5110 : else if (ptr[*pos].type == QI_VALSTOP)
743 : : {
3777 rhaas@postgresql.org 744 : 550 : *needcleanup = true; /* we'll have to remove stop words */
6992 tgl@sss.pgh.pa.us 745 : 550 : (*pos)++;
746 : : }
747 : : else
748 : : {
6975 teodor@sigaev.ru 749 [ - + ]: 4560 : Assert(ptr[*pos].type == QI_OPR);
750 : :
6297 peter_e@gmx.net 751 [ + + ]: 4560 : if (ptr[*pos].qoperator.oper == OP_NOT)
752 : : {
3400 tgl@sss.pgh.pa.us 753 : 682 : ptr[*pos].qoperator.left = 1; /* fixed offset */
6975 teodor@sigaev.ru 754 : 682 : (*pos)++;
755 : :
756 : : /* process the only argument */
3840 757 : 682 : findoprnd_recurse(ptr, pos, nnodes, needcleanup);
758 : : }
759 : : else
760 : : {
3777 rhaas@postgresql.org 761 : 3878 : QueryOperator *curitem = &ptr[*pos].qoperator;
3400 tgl@sss.pgh.pa.us 762 : 3878 : int tmp = *pos; /* save current position */
763 : :
3840 teodor@sigaev.ru 764 [ + + + + : 3878 : Assert(curitem->oper == OP_AND ||
- + ]
765 : : curitem->oper == OP_OR ||
766 : : curitem->oper == OP_PHRASE);
767 : :
6975 768 : 3878 : (*pos)++;
769 : :
770 : : /* process RIGHT argument */
3840 771 : 3878 : findoprnd_recurse(ptr, pos, nnodes, needcleanup);
772 : :
773 : 3878 : curitem->left = *pos - tmp; /* set LEFT arg's offset */
774 : :
775 : : /* process LEFT argument */
776 : 3878 : findoprnd_recurse(ptr, pos, nnodes, needcleanup);
777 : : }
778 : : }
6992 tgl@sss.pgh.pa.us 779 : 11175 : }
780 : :
781 : :
782 : : /*
783 : : * Fill in the left-fields previously left unfilled.
784 : : * The input QueryItems must be in polish (prefix) notation.
785 : : * Also, set *needcleanup to true if there are any QI_VALSTOP nodes.
786 : : */
787 : : static void
3840 teodor@sigaev.ru 788 : 2737 : findoprnd(QueryItem *ptr, int size, bool *needcleanup)
789 : : {
790 : : uint32 pos;
791 : :
792 : 2737 : *needcleanup = false;
6975 793 : 2737 : pos = 0;
3840 794 : 2737 : findoprnd_recurse(ptr, &pos, size, needcleanup);
795 : :
6975 796 [ - + ]: 2737 : if (pos != size)
6893 tgl@sss.pgh.pa.us 797 [ # # ]:UBC 0 : elog(ERROR, "malformed tsquery: extra nodes");
6975 teodor@sigaev.ru 798 :CBC 2737 : }
799 : :
800 : :
801 : : /*
802 : : * Parse the tsquery stored in "buf".
803 : : *
804 : : * Each value (operand) in the query is passed to pushval. pushval can
805 : : * transform the simple value to an arbitrarily complex expression using
806 : : * pushValue and pushOperator. It must push a single value with pushValue,
807 : : * a complete expression with all operands, or a stopword placeholder
808 : : * with pushStop, otherwise the prefix notation representation will be broken,
809 : : * having an operator with no operand.
810 : : *
811 : : * opaque is passed on to pushval as is, pushval can use it to store its
812 : : * private state.
813 : : *
814 : : * The pushval function can record soft errors via escontext.
815 : : * Callers must check SOFT_ERROR_OCCURRED to detect that.
816 : : *
817 : : * A bitmask of flags (see ts_utils.h) and an error context object
818 : : * can be provided as well. If a soft error occurs, NULL is returned.
819 : : */
820 : : TSQuery
6906 bruce@momjian.us 821 : 2767 : parse_tsquery(char *buf,
822 : : PushFunction pushval,
823 : : void *opaque,
824 : : int flags,
825 : : Node *escontext)
826 : : {
827 : : struct TSQueryParserStateData state;
828 : : int i;
829 : : TSQuery query;
830 : : int commonlen;
831 : : QueryItem *ptr;
832 : : ListCell *cell;
833 : : bool noisy;
834 : : bool needcleanup;
3112 teodor@sigaev.ru 835 : 2767 : int tsv_flags = P_TSV_OPR_IS_DELIM | P_TSV_IS_TSQUERY;
836 : :
837 : : /* plain should not be used with web */
838 [ - + ]: 2767 : Assert((flags & (P_TSQ_PLAIN | P_TSQ_WEB)) != (P_TSQ_PLAIN | P_TSQ_WEB));
839 : :
840 : : /* select suitable tokenizer */
841 [ + + ]: 2767 : if (flags & P_TSQ_PLAIN)
842 : 88 : state.gettoken = gettoken_query_plain;
843 [ + + ]: 2679 : else if (flags & P_TSQ_WEB)
844 : : {
845 : 341 : state.gettoken = gettoken_query_websearch;
846 : 341 : tsv_flags |= P_TSV_IS_WEB;
847 : : }
848 : : else
849 : 2338 : state.gettoken = gettoken_query_standard;
850 : :
851 : : /* emit nuisance NOTICEs only if not doing soft errors */
1385 tgl@sss.pgh.pa.us 852 [ + + - + ]: 2767 : noisy = !(escontext && IsA(escontext, ErrorSaveContext));
853 : :
854 : : /* init state */
6992 855 : 2767 : state.buffer = buf;
856 : 2767 : state.buf = buf;
857 : 2767 : state.count = 0;
3112 teodor@sigaev.ru 858 : 2767 : state.state = WAITFIRSTOPERAND;
6975 859 : 2767 : state.polstr = NIL;
1385 tgl@sss.pgh.pa.us 860 : 2767 : state.escontext = escontext;
861 : :
862 : : /* init value parser's state */
863 : 2767 : state.valstate = init_tsvector_parser(state.buffer, tsv_flags, escontext);
864 : :
865 : : /* init list of operand */
6992 866 : 2767 : state.sumlen = 0;
867 : 2767 : state.lenop = 64;
868 : 2767 : state.curop = state.op = (char *) palloc(state.lenop);
869 : 2767 : *(state.curop) = '\0';
870 : :
871 : : /* parse query & make polish notation (postfix, but in reverse order) */
6975 teodor@sigaev.ru 872 : 2767 : makepol(&state, pushval, opaque);
873 : :
874 : 2767 : close_tsvector_parser(state.valstate);
875 : :
1385 tgl@sss.pgh.pa.us 876 [ + + + - : 2767 : if (SOFT_ERROR_OCCURRED(escontext))
+ + ]
877 : 12 : return NULL;
878 : :
1517 879 [ + + ]: 2755 : if (state.polstr == NIL)
880 : : {
1385 881 [ + - ]: 18 : if (noisy)
882 [ + + ]: 18 : ereport(NOTICE,
883 : : (errmsg("text-search query doesn't contain lexemes: \"%s\"",
884 : : state.buffer)));
6992 885 : 18 : query = (TSQuery) palloc(HDRSIZETQ);
886 : 18 : SET_VARSIZE(query, HDRSIZETQ);
887 : 18 : query->size = 0;
888 : 18 : return query;
889 : : }
890 : :
4620 noah@leadboat.com 891 [ + - - + ]: 2737 : if (TSQUERY_TOO_BIG(list_length(state.polstr), state.sumlen))
1385 tgl@sss.pgh.pa.us 892 [ # # ]:UBC 0 : ereturn(escontext, NULL,
893 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
894 : : errmsg("tsquery is too large")));
6975 teodor@sigaev.ru 895 :CBC 2737 : commonlen = COMPUTESIZE(list_length(state.polstr), state.sumlen);
896 : :
897 : : /* Pack the QueryItems in the final TSQuery struct to return to caller */
898 : 2737 : query = (TSQuery) palloc0(commonlen);
6992 tgl@sss.pgh.pa.us 899 : 2737 : SET_VARSIZE(query, commonlen);
6975 teodor@sigaev.ru 900 : 2737 : query->size = list_length(state.polstr);
6992 tgl@sss.pgh.pa.us 901 : 2737 : ptr = GETQUERY(query);
902 : :
903 : : /* Copy QueryItems to TSQuery */
6975 teodor@sigaev.ru 904 : 2737 : i = 0;
905 [ + - + + : 13912 : foreach(cell, state.polstr)
+ + ]
906 : : {
3777 rhaas@postgresql.org 907 : 11175 : QueryItem *item = (QueryItem *) lfirst(cell);
908 : :
6906 bruce@momjian.us 909 [ + + + - ]: 11175 : switch (item->type)
910 : : {
6975 teodor@sigaev.ru 911 : 6065 : case QI_VAL:
912 : 6065 : memcpy(&ptr[i], item, sizeof(QueryOperand));
913 : 6065 : break;
914 : 550 : case QI_VALSTOP:
915 : 550 : ptr[i].type = QI_VALSTOP;
916 : 550 : break;
917 : 4560 : case QI_OPR:
918 : 4560 : memcpy(&ptr[i], item, sizeof(QueryOperator));
919 : 4560 : break;
6975 teodor@sigaev.ru 920 :UBC 0 : default:
6893 tgl@sss.pgh.pa.us 921 [ # # ]: 0 : elog(ERROR, "unrecognized QueryItem type: %d", item->type);
922 : : }
6975 teodor@sigaev.ru 923 :CBC 11175 : i++;
924 : : }
925 : :
926 : : /* Copy all the operand strings to TSQuery */
1343 peter@eisentraut.org 927 : 2737 : memcpy(GETOPERAND(query), state.op, state.sumlen);
6992 tgl@sss.pgh.pa.us 928 : 2737 : pfree(state.op);
929 : :
930 : : /*
931 : : * Set left operand pointers for every operator. While we're at it,
932 : : * detect whether there are any QI_VALSTOP nodes.
933 : : */
3840 teodor@sigaev.ru 934 : 2737 : findoprnd(ptr, query->size, &needcleanup);
935 : :
936 : : /*
937 : : * If there are QI_VALSTOP nodes, delete them and simplify the tree.
938 : : */
939 [ + + ]: 2737 : if (needcleanup)
1385 tgl@sss.pgh.pa.us 940 : 355 : query = cleanup_tsquery_stopwords(query, noisy);
941 : :
6992 942 : 2737 : return query;
943 : : }
944 : :
945 : : static void
288 peter@eisentraut.org 946 : 3530 : pushval_asis(void *opaque, TSQueryParserState state, char *strval, int lenval,
947 : : int16 weight, bool prefix)
948 : : {
6723 tgl@sss.pgh.pa.us 949 : 3530 : pushValue(state, strval, lenval, weight, prefix);
6975 teodor@sigaev.ru 950 : 3530 : }
951 : :
952 : : /*
953 : : * in without morphology
954 : : */
955 : : Datum
6992 tgl@sss.pgh.pa.us 956 : 1725 : tsqueryin(PG_FUNCTION_ARGS)
957 : : {
958 : 1725 : char *in = PG_GETARG_CSTRING(0);
1385 959 : 1725 : Node *escontext = fcinfo->context;
960 : :
961 : 1725 : PG_RETURN_TSQUERY(parse_tsquery(in,
962 : : pushval_asis,
963 : : NULL,
964 : : 0,
965 : : escontext));
966 : : }
967 : :
968 : : /*
969 : : * out function
970 : : */
971 : : typedef struct
972 : : {
973 : : QueryItem *curpol; /* current query item */
974 : : char *op; /* start of tsquery's operand strings */
975 : : StringInfoData buf; /* output is accumulated here */
976 : : } INFIX;
977 : :
978 : : /*
979 : : * recursively traverse the tree and
980 : : * print it in infix (human-readable) form
981 : : */
982 : : static void
3759 teodor@sigaev.ru 983 : 4782 : infix(INFIX *in, int parentPriority, bool rightPhraseOp)
984 : : {
985 : : /* since this function recurses, it could be driven to stack overflow. */
6975 986 : 4782 : check_stack_depth();
987 : :
988 [ + + ]: 4782 : if (in->curpol->type == QI_VAL)
989 : : {
6297 peter_e@gmx.net 990 : 2763 : QueryOperand *curpol = &in->curpol->qoperand;
6975 teodor@sigaev.ru 991 : 2763 : char *op = in->op + curpol->distance;
992 : :
53 tgl@sss.pgh.pa.us 993 :GNC 2763 : appendStringInfoChar(&in->buf, '\'');
6992 tgl@sss.pgh.pa.us 994 [ + + ]:CBC 10701 : while (*op)
995 : : {
53 tgl@sss.pgh.pa.us 996 :GNC 7938 : int clen = pg_mblen_cstr(op);
997 : :
6992 tgl@sss.pgh.pa.us 998 [ + + ]:CBC 7938 : if (t_iseq(op, '\''))
53 tgl@sss.pgh.pa.us 999 :GNC 8 : appendStringInfoChar(&in->buf, '\'');
6905 teodor@sigaev.ru 1000 [ + + ]:CBC 7930 : else if (t_iseq(op, '\\'))
53 tgl@sss.pgh.pa.us 1001 :GNC 4 : appendStringInfoChar(&in->buf, '\\');
1002 : 7938 : appendBinaryStringInfo(&in->buf, op, clen);
6992 tgl@sss.pgh.pa.us 1003 :CBC 7938 : op += clen;
1004 : : }
53 tgl@sss.pgh.pa.us 1005 :GNC 2763 : appendStringInfoChar(&in->buf, '\'');
6723 tgl@sss.pgh.pa.us 1006 [ + + + + ]:CBC 2763 : if (curpol->weight || curpol->prefix)
1007 : : {
53 tgl@sss.pgh.pa.us 1008 :GNC 116 : appendStringInfoChar(&in->buf, ':');
6332 bruce@momjian.us 1009 [ + + ]:CBC 116 : if (curpol->prefix)
53 tgl@sss.pgh.pa.us 1010 :GNC 16 : appendStringInfoChar(&in->buf, '*');
6975 teodor@sigaev.ru 1011 [ + + ]:CBC 116 : if (curpol->weight & (1 << 3))
53 tgl@sss.pgh.pa.us 1012 :GNC 40 : appendStringInfoChar(&in->buf, 'A');
6975 teodor@sigaev.ru 1013 [ + + ]:CBC 116 : if (curpol->weight & (1 << 2))
53 tgl@sss.pgh.pa.us 1014 :GNC 64 : appendStringInfoChar(&in->buf, 'B');
6975 teodor@sigaev.ru 1015 [ + + ]:CBC 116 : if (curpol->weight & (1 << 1))
53 tgl@sss.pgh.pa.us 1016 :GNC 12 : appendStringInfoChar(&in->buf, 'C');
6975 teodor@sigaev.ru 1017 [ + + ]:CBC 116 : if (curpol->weight & 1)
53 tgl@sss.pgh.pa.us 1018 :GNC 4 : appendStringInfoChar(&in->buf, 'D');
1019 : : }
6992 tgl@sss.pgh.pa.us 1020 :CBC 2763 : in->curpol++;
1021 : : }
6297 peter_e@gmx.net 1022 [ + + ]: 2019 : else if (in->curpol->qoperator.oper == OP_NOT)
1023 : : {
3759 teodor@sigaev.ru 1024 : 248 : int priority = QO_PRIORITY(in->curpol);
1025 : :
3840 1026 [ - + ]: 248 : if (priority < parentPriority)
53 tgl@sss.pgh.pa.us 1027 :UNC 0 : appendStringInfoString(&in->buf, "( ");
53 tgl@sss.pgh.pa.us 1028 :GNC 248 : appendStringInfoChar(&in->buf, '!');
3840 teodor@sigaev.ru 1029 :CBC 248 : in->curpol++;
3759 1030 : 248 : infix(in, priority, false);
3840 1031 [ - + ]: 248 : if (priority < parentPriority)
53 tgl@sss.pgh.pa.us 1032 :UNC 0 : appendStringInfoString(&in->buf, " )");
1033 : : }
1034 : : else
1035 : : {
6297 peter_e@gmx.net 1036 :CBC 1771 : int8 op = in->curpol->qoperator.oper;
3759 teodor@sigaev.ru 1037 : 1771 : int priority = QO_PRIORITY(in->curpol);
3840 1038 : 1771 : int16 distance = in->curpol->qoperator.distance;
53 tgl@sss.pgh.pa.us 1039 :GNC 1771 : QueryItem *leftop = in->curpol + in->curpol->qoperator.left;
1040 : 1771 : QueryItem *rightop = in->curpol + 1;
1041 : : QueryItem *leftend;
3840 teodor@sigaev.ru 1042 :CBC 1771 : bool needParenthesis = false;
1043 : :
1044 [ + + + + ]: 1771 : if (priority < parentPriority ||
1045 : : /* phrase operator depends on order */
3710 tgl@sss.pgh.pa.us 1046 [ + + ]: 468 : (op == OP_PHRASE && rightPhraseOp))
1047 : : {
3840 teodor@sigaev.ru 1048 : 220 : needParenthesis = true;
53 tgl@sss.pgh.pa.us 1049 :GNC 220 : appendStringInfoString(&in->buf, "( ");
1050 : : }
1051 : :
1052 : : /* print left operand */
1053 : 1771 : in->curpol = leftop;
3759 teodor@sigaev.ru 1054 :CBC 1771 : infix(in, priority, false);
1055 : : /* remember end+1 of left operand */
53 tgl@sss.pgh.pa.us 1056 :GNC 1771 : leftend = in->curpol;
1057 : :
1058 : : /* print operator */
6906 bruce@momjian.us 1059 [ + + + - ]:CBC 1771 : switch (op)
1060 : : {
6975 teodor@sigaev.ru 1061 : 480 : case OP_OR:
53 tgl@sss.pgh.pa.us 1062 :GNC 480 : appendStringInfoString(&in->buf, " | ");
6975 teodor@sigaev.ru 1063 :CBC 480 : break;
1064 : 815 : case OP_AND:
53 tgl@sss.pgh.pa.us 1065 :GNC 815 : appendStringInfoString(&in->buf, " & ");
6975 teodor@sigaev.ru 1066 :CBC 815 : break;
3840 1067 : 476 : case OP_PHRASE:
1068 [ + + ]: 476 : if (distance != 1)
53 tgl@sss.pgh.pa.us 1069 :GNC 116 : appendStringInfo(&in->buf, " <%d> ", distance);
1070 : : else
1071 : 360 : appendStringInfoString(&in->buf, " <-> ");
3840 teodor@sigaev.ru 1072 :CBC 476 : break;
6975 teodor@sigaev.ru 1073 :UBC 0 : default:
1074 : : /* OP_NOT is handled in above if-branch */
6893 tgl@sss.pgh.pa.us 1075 [ # # ]: 0 : elog(ERROR, "unrecognized operator type: %d", op);
1076 : : }
1077 : :
1078 : : /* print right operand */
53 tgl@sss.pgh.pa.us 1079 :GNC 1771 : in->curpol = rightop;
1080 : 1771 : infix(in, priority, (op == OP_PHRASE));
1081 : :
1082 : : /* re-advance over left operand */
1083 [ - + ]: 1771 : Assert(in->curpol == leftop);
1084 : 1771 : in->curpol = leftend;
1085 : :
3840 teodor@sigaev.ru 1086 [ + + ]:CBC 1771 : if (needParenthesis)
53 tgl@sss.pgh.pa.us 1087 :GNC 220 : appendStringInfoString(&in->buf, " )");
1088 : : }
6992 tgl@sss.pgh.pa.us 1089 :CBC 4782 : }
1090 : :
1091 : : Datum
1092 : 1012 : tsqueryout(PG_FUNCTION_ARGS)
1093 : : {
1094 : 1012 : TSQuery query = PG_GETARG_TSQUERY(0);
1095 : : INFIX nrm;
1096 : :
1097 [ + + ]: 1012 : if (query->size == 0)
1098 : : {
1099 : 20 : char *b = palloc(1);
1100 : :
1101 : 20 : *b = '\0';
1102 : 20 : PG_RETURN_POINTER(b);
1103 : : }
1104 : 992 : nrm.curpol = GETQUERY(query);
1105 : 992 : nrm.op = GETOPERAND(query);
53 tgl@sss.pgh.pa.us 1106 :GNC 992 : initStringInfo(&nrm.buf);
3710 tgl@sss.pgh.pa.us 1107 :CBC 992 : infix(&nrm, -1 /* lowest priority */ , false);
1108 : :
6992 1109 [ - + ]: 992 : PG_FREE_IF_COPY(query, 0);
53 tgl@sss.pgh.pa.us 1110 :GNC 992 : PG_RETURN_CSTRING(nrm.buf.data);
1111 : : }
1112 : :
1113 : : /*
1114 : : * Binary Input / Output functions. The binary format is as follows:
1115 : : *
1116 : : * uint32 number of operators/operands in the query
1117 : : *
1118 : : * Followed by the operators and operands, in prefix notation. For each
1119 : : * operand:
1120 : : *
1121 : : * uint8 type, QI_VAL
1122 : : * uint8 weight
1123 : : * uint8 prefix
1124 : : * operand text in client encoding, null-terminated
1125 : : *
1126 : : * For each operator:
1127 : : *
1128 : : * uint8 type, QI_OPR
1129 : : * uint8 operator, one of OP_AND, OP_PHRASE OP_OR, OP_NOT.
1130 : : * uint16 distance (only for OP_PHRASE)
1131 : : */
1132 : : Datum
6992 tgl@sss.pgh.pa.us 1133 :UBC 0 : tsquerysend(PG_FUNCTION_ARGS)
1134 : : {
1135 : 0 : TSQuery query = PG_GETARG_TSQUERY(0);
1136 : : StringInfoData buf;
1137 : : int i;
1138 : 0 : QueryItem *item = GETQUERY(query);
1139 : :
1140 : 0 : pq_begintypsend(&buf);
1141 : :
3288 andres@anarazel.de 1142 : 0 : pq_sendint32(&buf, query->size);
6992 tgl@sss.pgh.pa.us 1143 [ # # ]: 0 : for (i = 0; i < query->size; i++)
1144 : : {
3288 andres@anarazel.de 1145 : 0 : pq_sendint8(&buf, item->type);
1146 : :
6906 bruce@momjian.us 1147 [ # # # ]: 0 : switch (item->type)
1148 : : {
6975 teodor@sigaev.ru 1149 : 0 : case QI_VAL:
3288 andres@anarazel.de 1150 : 0 : pq_sendint8(&buf, item->qoperand.weight);
1151 : 0 : pq_sendint8(&buf, item->qoperand.prefix);
6297 peter_e@gmx.net 1152 : 0 : pq_sendstring(&buf, GETOPERAND(query) + item->qoperand.distance);
6975 teodor@sigaev.ru 1153 : 0 : break;
1154 : 0 : case QI_OPR:
3288 andres@anarazel.de 1155 : 0 : pq_sendint8(&buf, item->qoperator.oper);
3840 teodor@sigaev.ru 1156 [ # # ]: 0 : if (item->qoperator.oper == OP_PHRASE)
3288 andres@anarazel.de 1157 : 0 : pq_sendint16(&buf, item->qoperator.distance);
6975 teodor@sigaev.ru 1158 : 0 : break;
1159 : 0 : default:
6893 tgl@sss.pgh.pa.us 1160 [ # # ]: 0 : elog(ERROR, "unrecognized tsquery node type: %d", item->type);
1161 : : }
6992 1162 : 0 : item++;
1163 : : }
1164 : :
1165 [ # # ]: 0 : PG_FREE_IF_COPY(query, 0);
1166 : :
1167 : 0 : PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
1168 : : }
1169 : :
1170 : : Datum
1171 : 0 : tsqueryrecv(PG_FUNCTION_ARGS)
1172 : : {
3777 rhaas@postgresql.org 1173 : 0 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
1174 : : TSQuery query;
1175 : : int len;
1176 : : QueryItem *item;
1177 : : int datalen;
1178 : : char *ptr;
1179 : : uint32 size;
1180 : : const char **operands;
1181 : : bool needcleanup;
1182 : :
6992 tgl@sss.pgh.pa.us 1183 : 0 : size = pq_getmsgint(buf, sizeof(uint32));
6975 teodor@sigaev.ru 1184 [ # # ]: 0 : if (size > (MaxAllocSize / sizeof(QueryItem)))
6992 tgl@sss.pgh.pa.us 1185 [ # # ]: 0 : elog(ERROR, "invalid size of tsquery");
1186 : :
1187 : : /* Allocate space to temporarily hold operand strings */
6975 teodor@sigaev.ru 1188 : 0 : operands = palloc(size * sizeof(char *));
1189 : :
1190 : : /* Allocate space for all the QueryItems. */
1191 : 0 : len = HDRSIZETQ + sizeof(QueryItem) * size;
1192 : 0 : query = (TSQuery) palloc0(len);
6992 tgl@sss.pgh.pa.us 1193 : 0 : query->size = size;
1194 : 0 : item = GETQUERY(query);
1195 : :
6975 teodor@sigaev.ru 1196 : 0 : datalen = 0;
93 peter@eisentraut.org 1197 [ # # ]:UNC 0 : for (uint32 i = 0; i < size; i++)
1198 : : {
6992 tgl@sss.pgh.pa.us 1199 :UBC 0 : item->type = (int8) pq_getmsgint(buf, sizeof(int8));
1200 : :
6975 teodor@sigaev.ru 1201 [ # # ]: 0 : if (item->type == QI_VAL)
1202 : : {
1203 : : size_t val_len; /* length after recoding to server
1204 : : * encoding */
1205 : : uint8 weight;
1206 : : uint8 prefix;
1207 : : const char *val;
1208 : : pg_crc32 valcrc;
1209 : :
6906 bruce@momjian.us 1210 : 0 : weight = (uint8) pq_getmsgint(buf, sizeof(uint8));
6723 tgl@sss.pgh.pa.us 1211 : 0 : prefix = (uint8) pq_getmsgint(buf, sizeof(uint8));
6975 teodor@sigaev.ru 1212 : 0 : val = pq_getmsgstring(buf);
1213 : 0 : val_len = strlen(val);
1214 : :
1215 : : /* Sanity checks */
1216 : :
1217 [ # # ]: 0 : if (weight > 0xF)
6893 tgl@sss.pgh.pa.us 1218 [ # # ]: 0 : elog(ERROR, "invalid tsquery: invalid weight bitmap");
1219 : :
56 1220 [ # # ]: 0 : if (val_len == 0)
1221 [ # # ]: 0 : elog(ERROR, "invalid tsquery: empty operand");
1222 : :
6975 teodor@sigaev.ru 1223 [ # # ]: 0 : if (val_len > MAXSTRLEN)
6893 tgl@sss.pgh.pa.us 1224 [ # # ]: 0 : elog(ERROR, "invalid tsquery: operand too long");
1225 : :
6975 teodor@sigaev.ru 1226 [ # # ]: 0 : if (datalen > MAXSTRPOS)
6893 tgl@sss.pgh.pa.us 1227 [ # # ]: 0 : elog(ERROR, "invalid tsquery: total operand length exceeded");
1228 : :
1229 : : /* Looks valid. */
1230 : :
4360 heikki.linnakangas@i 1231 : 0 : INIT_LEGACY_CRC32(valcrc);
1232 [ # # ]: 0 : COMP_LEGACY_CRC32(valcrc, val, val_len);
1233 : 0 : FIN_LEGACY_CRC32(valcrc);
1234 : :
6297 peter_e@gmx.net 1235 : 0 : item->qoperand.weight = weight;
1236 : 0 : item->qoperand.prefix = (prefix) ? true : false;
1237 : 0 : item->qoperand.valcrc = (int32) valcrc;
1238 : 0 : item->qoperand.length = val_len;
1239 : 0 : item->qoperand.distance = datalen;
1240 : :
1241 : : /*
1242 : : * Operand strings are copied to the final struct after this loop;
1243 : : * here we just collect them to an array
1244 : : */
6975 teodor@sigaev.ru 1245 : 0 : operands[i] = val;
1246 : :
3400 tgl@sss.pgh.pa.us 1247 : 0 : datalen += val_len + 1; /* + 1 for the '\0' terminator */
1248 : : }
6975 teodor@sigaev.ru 1249 [ # # ]: 0 : else if (item->type == QI_OPR)
1250 : : {
1251 : : int8 oper;
1252 : :
1253 : 0 : oper = (int8) pq_getmsgint(buf, sizeof(int8));
3840 1254 [ # # # # : 0 : if (oper != OP_NOT && oper != OP_OR && oper != OP_AND && oper != OP_PHRASE)
# # # # ]
6893 tgl@sss.pgh.pa.us 1255 [ # # ]: 0 : elog(ERROR, "invalid tsquery: unrecognized operator type %d",
1256 : : (int) oper);
6975 teodor@sigaev.ru 1257 [ # # ]: 0 : if (i == size - 1)
1258 [ # # ]: 0 : elog(ERROR, "invalid pointer to right operand");
1259 : :
6297 peter_e@gmx.net 1260 : 0 : item->qoperator.oper = oper;
3840 teodor@sigaev.ru 1261 [ # # ]: 0 : if (oper == OP_PHRASE)
1262 : : {
56 tgl@sss.pgh.pa.us 1263 : 0 : unsigned int dist = pq_getmsgint(buf, sizeof(int16));
1264 : :
1265 [ # # ]: 0 : if (dist > MAXENTRYPOS)
1266 [ # # ]: 0 : elog(ERROR, "invalid tsquery: invalid phrase distance %u",
1267 : : dist);
1268 : 0 : item->qoperator.distance = (int16) dist;
1269 : : }
1270 : : }
1271 : : else
6893 1272 [ # # ]: 0 : elog(ERROR, "unrecognized tsquery node type: %d", item->type);
1273 : :
6992 1274 : 0 : item++;
1275 : : }
1276 : :
1277 : : /* Enlarge buffer to make room for the operand values. */
1278 : 0 : query = (TSQuery) repalloc(query, len + datalen);
1279 : 0 : item = GETQUERY(query);
1280 : 0 : ptr = GETOPERAND(query);
1281 : :
1282 : : /*
1283 : : * Fill in the left-pointers. Checks that the tree is well-formed as a
1284 : : * side-effect.
1285 : : */
3840 teodor@sigaev.ru 1286 : 0 : findoprnd(item, size, &needcleanup);
1287 : :
1288 : : /* Can't have found any QI_VALSTOP nodes */
3582 tgl@sss.pgh.pa.us 1289 [ # # ]: 0 : Assert(!needcleanup);
1290 : :
1291 : : /* Copy operands to output struct */
93 peter@eisentraut.org 1292 [ # # ]:UNC 0 : for (uint32 i = 0; i < size; i++)
1293 : : {
6975 teodor@sigaev.ru 1294 [ # # ]:UBC 0 : if (item->type == QI_VAL)
1295 : : {
6297 peter_e@gmx.net 1296 : 0 : memcpy(ptr, operands[i], item->qoperand.length + 1);
1297 : 0 : ptr += item->qoperand.length + 1;
1298 : : }
6992 tgl@sss.pgh.pa.us 1299 : 0 : item++;
1300 : : }
1301 : :
6975 teodor@sigaev.ru 1302 : 0 : pfree(operands);
1303 : :
6992 tgl@sss.pgh.pa.us 1304 [ # # ]: 0 : Assert(ptr - GETOPERAND(query) == datalen);
1305 : :
1306 : 0 : SET_VARSIZE(query, len + datalen);
1307 : :
3840 teodor@sigaev.ru 1308 : 0 : PG_RETURN_TSQUERY(query);
1309 : : }
1310 : :
1311 : : /*
1312 : : * debug function, used only for view query
1313 : : * which will be executed in non-leaf pages in index
1314 : : */
1315 : : Datum
6992 tgl@sss.pgh.pa.us 1316 : 0 : tsquerytree(PG_FUNCTION_ARGS)
1317 : : {
1318 : 0 : TSQuery query = PG_GETARG_TSQUERY(0);
1319 : : INFIX nrm;
1320 : : text *res;
1321 : : QueryItem *q;
1322 : : int len;
1323 : :
1324 [ # # ]: 0 : if (query->size == 0)
1325 : : {
1326 : 0 : res = (text *) palloc(VARHDRSZ);
1327 : 0 : SET_VARSIZE(res, VARHDRSZ);
1328 : 0 : PG_RETURN_POINTER(res);
1329 : : }
1330 : :
1331 : 0 : q = clean_NOT(GETQUERY(query), &len);
1332 : :
1333 [ # # ]: 0 : if (!q)
1334 : : {
6775 1335 : 0 : res = cstring_to_text("T");
1336 : : }
1337 : : else
1338 : : {
6992 1339 : 0 : nrm.curpol = q;
1340 : 0 : nrm.op = GETOPERAND(query);
53 tgl@sss.pgh.pa.us 1341 :UNC 0 : initStringInfo(&nrm.buf);
3759 teodor@sigaev.ru 1342 :UBC 0 : infix(&nrm, -1, false);
53 tgl@sss.pgh.pa.us 1343 :UNC 0 : res = cstring_to_text_with_len(nrm.buf.data, nrm.buf.len);
6992 tgl@sss.pgh.pa.us 1344 :UBC 0 : pfree(q);
1345 : : }
1346 : :
1347 [ # # ]: 0 : PG_FREE_IF_COPY(query, 0);
1348 : :
6775 1349 : 0 : PG_RETURN_TEXT_P(res);
1350 : : }
|