PostgreSQL Source Code git master
_int_bool.c
Go to the documentation of this file.
1/*
2 * contrib/intarray/_int_bool.c
3 */
4#include "postgres.h"
5
6#include "_int.h"
7#include "miscadmin.h"
8
14
15
16/* parser's states */
17#define WAITOPERAND 1
18#define WAITENDOPERAND 2
19#define WAITOPERATOR 3
20
21/*
22 * node of query tree, also used
23 * for storing polish notation in parser
24 */
25typedef struct NODE
26{
29 struct NODE *next;
31
32typedef struct
33{
34 char *buf;
37 struct Node *escontext;
38 /* reverse polish notation in list (for temporary usage) */
40 /* number in str */
42} WORKSTATE;
43
44/*
45 * get token from query string
46 */
47static int32
49{
50 char nnn[16];
51 int innn;
52
53 *val = 0; /* default result */
54
55 innn = 0;
56 while (1)
57 {
58 if (innn >= sizeof(nnn))
59 return ERR; /* buffer overrun => syntax error */
60 switch (state->state)
61 {
62 case WAITOPERAND:
63 innn = 0;
64 if ((*(state->buf) >= '0' && *(state->buf) <= '9') ||
65 *(state->buf) == '-')
66 {
67 state->state = WAITENDOPERAND;
68 nnn[innn++] = *(state->buf);
69 }
70 else if (*(state->buf) == '!')
71 {
72 (state->buf)++;
73 *val = (int32) '!';
74 return OPR;
75 }
76 else if (*(state->buf) == '(')
77 {
78 state->count++;
79 (state->buf)++;
80 return OPEN;
81 }
82 else if (*(state->buf) != ' ')
83 return ERR;
84 break;
85 case WAITENDOPERAND:
86 if (*(state->buf) >= '0' && *(state->buf) <= '9')
87 {
88 nnn[innn++] = *(state->buf);
89 }
90 else
91 {
92 long lval;
93
94 nnn[innn] = '\0';
95 errno = 0;
96 lval = strtol(nnn, NULL, 0);
97 *val = (int32) lval;
98 if (errno != 0 || (long) *val != lval)
99 return ERR;
100 state->state = WAITOPERATOR;
101 return (state->count && *(state->buf) == '\0')
102 ? ERR : VAL;
103 }
104 break;
105 case WAITOPERATOR:
106 if (*(state->buf) == '&' || *(state->buf) == '|')
107 {
108 state->state = WAITOPERAND;
109 *val = (int32) *(state->buf);
110 (state->buf)++;
111 return OPR;
112 }
113 else if (*(state->buf) == ')')
114 {
115 (state->buf)++;
116 state->count--;
117 return (state->count < 0) ? ERR : CLOSE;
118 }
119 else if (*(state->buf) == '\0')
120 return (state->count) ? ERR : END;
121 else if (*(state->buf) != ' ')
122 return ERR;
123 break;
124 default:
125 return ERR;
126 break;
127 }
128 (state->buf)++;
129 }
130}
131
132/*
133 * push new one in polish notation reverse view
134 */
135static void
137{
138 NODE *tmp = (NODE *) palloc(sizeof(NODE));
139
140 tmp->type = type;
141 tmp->val = val;
142 tmp->next = state->str;
143 state->str = tmp;
144 state->num++;
145}
146
147#define STACKDEPTH 16
148
149/*
150 * make polish notation of query
151 */
152static int32
154{
155 int32 val,
156 type;
157 int32 stack[STACKDEPTH];
158 int32 lenstack = 0;
159
160 /* since this function recurses, it could be driven to stack overflow */
162
163 while ((type = gettoken(state, &val)) != END)
164 {
165 switch (type)
166 {
167 case VAL:
169 while (lenstack && (stack[lenstack - 1] == (int32) '&' ||
170 stack[lenstack - 1] == (int32) '!'))
171 {
172 lenstack--;
173 pushquery(state, OPR, stack[lenstack]);
174 }
175 break;
176 case OPR:
177 if (lenstack && val == (int32) '|')
179 else
180 {
181 if (lenstack == STACKDEPTH)
182 ereturn(state->escontext, ERR,
183 (errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
184 errmsg("statement too complex")));
185 stack[lenstack] = val;
186 lenstack++;
187 }
188 break;
189 case OPEN:
190 if (makepol(state) == ERR)
191 return ERR;
192 while (lenstack && (stack[lenstack - 1] == (int32) '&' ||
193 stack[lenstack - 1] == (int32) '!'))
194 {
195 lenstack--;
196 pushquery(state, OPR, stack[lenstack]);
197 }
198 break;
199 case CLOSE:
200 while (lenstack)
201 {
202 lenstack--;
203 pushquery(state, OPR, stack[lenstack]);
204 };
205 return END;
206 break;
207 case ERR:
208 default:
209 ereturn(state->escontext, ERR,
210 (errcode(ERRCODE_SYNTAX_ERROR),
211 errmsg("syntax error")));
212 }
213 }
214
215 while (lenstack)
216 {
217 lenstack--;
218 pushquery(state, OPR, stack[lenstack]);
219 };
220 return END;
221}
222
223typedef struct
224{
227} CHKVAL;
228
229/*
230 * is there value 'val' in (sorted) array or not ?
231 */
232static bool
233checkcondition_arr(void *checkval, ITEM *item, void *options)
234{
235 int32 *StopLow = ((CHKVAL *) checkval)->arrb;
236 int32 *StopHigh = ((CHKVAL *) checkval)->arre;
237 int32 *StopMiddle;
238
239 /* Loop invariant: StopLow <= val < StopHigh */
240
241 while (StopLow < StopHigh)
242 {
243 StopMiddle = StopLow + (StopHigh - StopLow) / 2;
244 if (*StopMiddle == item->val)
245 return true;
246 else if (*StopMiddle < item->val)
247 StopLow = StopMiddle + 1;
248 else
249 StopHigh = StopMiddle;
250 }
251 return false;
252}
253
254static bool
255checkcondition_bit(void *checkval, ITEM *item, void *siglen)
256{
257 return GETBIT(checkval, HASHVAL(item->val, (int) (intptr_t) siglen));
258}
259
260/*
261 * evaluate boolean expression, using chkcond() to test the primitive cases
262 */
263static bool
264execute(ITEM *curitem, void *checkval, void *options, bool calcnot,
265 bool (*chkcond) (void *checkval, ITEM *item, void *options))
266{
267 /* since this function recurses, it could be driven to stack overflow */
269
270 if (curitem->type == VAL)
271 return (*chkcond) (checkval, curitem, options);
272 else if (curitem->val == (int32) '!')
273 {
274 return calcnot ?
275 ((execute(curitem - 1, checkval, options, calcnot, chkcond)) ? false : true)
276 : true;
277 }
278 else if (curitem->val == (int32) '&')
279 {
280 if (execute(curitem + curitem->left, checkval, options, calcnot, chkcond))
281 return execute(curitem - 1, checkval, options, calcnot, chkcond);
282 else
283 return false;
284 }
285 else
286 { /* |-operator */
287 if (execute(curitem + curitem->left, checkval, options, calcnot, chkcond))
288 return true;
289 else
290 return execute(curitem - 1, checkval, options, calcnot, chkcond);
291 }
292}
293
294/*
295 * signconsistent & execconsistent called by *_consistent
296 */
297bool
298signconsistent(QUERYTYPE *query, BITVECP sign, int siglen, bool calcnot)
299{
300 return execute(GETQUERY(query) + query->size - 1,
301 sign, (void *) (intptr_t) siglen, calcnot,
303}
304
305/* Array must be sorted! */
306bool
307execconsistent(QUERYTYPE *query, ArrayType *array, bool calcnot)
308{
309 CHKVAL chkval;
310
311 CHECKARRVALID(array);
312 chkval.arrb = ARRPTR(array);
313 chkval.arre = chkval.arrb + ARRNELEMS(array);
314 return execute(GETQUERY(query) + query->size - 1,
315 &chkval, NULL, calcnot,
317}
318
319typedef struct
320{
323} GinChkVal;
324
325static bool
326checkcondition_gin(void *checkval, ITEM *item, void *options)
327{
328 GinChkVal *gcv = (GinChkVal *) checkval;
329
330 return gcv->mapped_check[item - gcv->first];
331}
332
333bool
334gin_bool_consistent(QUERYTYPE *query, bool *check)
335{
336 GinChkVal gcv;
337 ITEM *items = GETQUERY(query);
338 int i,
339 j = 0;
340
341 if (query->size <= 0)
342 return false;
343
344 /*
345 * Set up data for checkcondition_gin. This must agree with the query
346 * extraction code in ginint4_queryextract.
347 */
348 gcv.first = items;
349 gcv.mapped_check = (bool *) palloc(sizeof(bool) * query->size);
350 for (i = 0; i < query->size; i++)
351 {
352 if (items[i].type == VAL)
353 gcv.mapped_check[i] = check[j++];
354 }
355
356 return execute(GETQUERY(query) + query->size - 1,
357 &gcv, NULL, true,
359}
360
361static bool
363{
364 /* since this function recurses, it could be driven to stack overflow */
366
367 if (curitem->type == VAL)
368 return true;
369 else if (curitem->val == (int32) '!')
370 {
371 /*
372 * Assume anything under a NOT is non-required. For some cases with
373 * nested NOTs, we could prove there's a required value, but it seems
374 * unlikely to be worth the trouble.
375 */
376 return false;
377 }
378 else if (curitem->val == (int32) '&')
379 {
380 /* If either side has a required value, we're good */
381 if (contains_required_value(curitem + curitem->left))
382 return true;
383 else
384 return contains_required_value(curitem - 1);
385 }
386 else
387 { /* |-operator */
388 /* Both sides must have required values */
389 if (contains_required_value(curitem + curitem->left))
390 return contains_required_value(curitem - 1);
391 else
392 return false;
393 }
394}
395
396bool
398{
399 if (query->size <= 0)
400 return false;
401 return contains_required_value(GETQUERY(query) + query->size - 1);
402}
403
404/*
405 * boolean operations
406 */
407Datum
409{
410 /* just reverse the operands */
413 PG_GETARG_DATUM(0));
414}
415
416Datum
418{
421 CHKVAL chkval;
422 bool result;
423
426 chkval.arrb = ARRPTR(val);
427 chkval.arre = chkval.arrb + ARRNELEMS(val);
428 result = execute(GETQUERY(query) + query->size - 1,
429 &chkval, NULL, true,
431 pfree(val);
432
433 PG_FREE_IF_COPY(query, 1);
434 PG_RETURN_BOOL(result);
435}
436
437static void
439{
440 /* since this function recurses, it could be driven to stack overflow. */
442
443#ifdef BS_DEBUG
444 elog(DEBUG3, (ptr[*pos].type == OPR) ?
445 "%d %c" : "%d %d", *pos, ptr[*pos].val);
446#endif
447 if (ptr[*pos].type == VAL)
448 {
449 ptr[*pos].left = 0;
450 (*pos)--;
451 }
452 else if (ptr[*pos].val == (int32) '!')
453 {
454 ptr[*pos].left = -1;
455 (*pos)--;
456 findoprnd(ptr, pos);
457 }
458 else
459 {
460 ITEM *curitem = &ptr[*pos];
461 int32 tmp = *pos;
462
463 (*pos)--;
464 findoprnd(ptr, pos);
465 curitem->left = *pos - tmp;
466 findoprnd(ptr, pos);
467 }
468}
469
470
471/*
472 * input
473 */
474Datum
476{
477 char *buf = (char *) PG_GETARG_POINTER(0);
479 int32 i;
480 QUERYTYPE *query;
481 int32 commonlen;
482 ITEM *ptr;
483 NODE *tmp;
484 int32 pos = 0;
485 struct Node *escontext = fcinfo->context;
486
487#ifdef BS_DEBUG
488 StringInfoData pbuf;
489#endif
490
491 state.buf = buf;
492 state.state = WAITOPERAND;
493 state.count = 0;
494 state.num = 0;
495 state.str = NULL;
496 state.escontext = escontext;
497
498 /* make polish notation (postfix, but in reverse order) */
499 if (makepol(&state) == ERR)
501 if (!state.num)
502 ereturn(escontext, (Datum) 0,
503 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
504 errmsg("empty query")));
505
506 if (state.num > QUERYTYPEMAXITEMS)
507 ereturn(escontext, (Datum) 0,
508 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
509 errmsg("number of query items (%d) exceeds the maximum allowed (%d)",
510 state.num, (int) QUERYTYPEMAXITEMS)));
511 commonlen = COMPUTESIZE(state.num);
512
513 query = (QUERYTYPE *) palloc(commonlen);
514 SET_VARSIZE(query, commonlen);
515 query->size = state.num;
516 ptr = GETQUERY(query);
517
518 for (i = state.num - 1; i >= 0; i--)
519 {
520 ptr[i].type = state.str->type;
521 ptr[i].val = state.str->val;
522 tmp = state.str->next;
523 pfree(state.str);
524 state.str = tmp;
525 }
526
527 pos = query->size - 1;
528 findoprnd(ptr, &pos);
529#ifdef BS_DEBUG
530 initStringInfo(&pbuf);
531 for (i = 0; i < query->size; i++)
532 {
533 if (ptr[i].type == OPR)
534 appendStringInfo(&pbuf, "%c(%d) ", ptr[i].val, ptr[i].left);
535 else
536 appendStringInfo(&pbuf, "%d ", ptr[i].val);
537 }
538 elog(DEBUG3, "POR: %s", pbuf.data);
539 pfree(pbuf.data);
540#endif
541
542 PG_RETURN_POINTER(query);
543}
544
545
546/*
547 * out function
548 */
549typedef struct
550{
552 char *buf;
553 char *cur;
555} INFIX;
556
557#define RESIZEBUF(inf,addsize) while( ( (inf)->cur - (inf)->buf ) + (addsize) + 1 >= (inf)->buflen ) { \
558 int32 len = inf->cur - inf->buf; \
559 inf->buflen *= 2; \
560 inf->buf = (char*) repalloc( (void*)inf->buf, inf->buflen ); \
561 inf->cur = inf->buf + len; \
562}
563
564static void
565infix(INFIX *in, bool first)
566{
567 /* since this function recurses, it could be driven to stack overflow. */
569
570 if (in->curpol->type == VAL)
571 {
572 RESIZEBUF(in, 11);
573 sprintf(in->cur, "%d", in->curpol->val);
574 in->cur = strchr(in->cur, '\0');
575 in->curpol--;
576 }
577 else if (in->curpol->val == (int32) '!')
578 {
579 bool isopr = false;
580
581 RESIZEBUF(in, 1);
582 *(in->cur) = '!';
583 in->cur++;
584 *(in->cur) = '\0';
585 in->curpol--;
586 if (in->curpol->type == OPR)
587 {
588 isopr = true;
589 RESIZEBUF(in, 2);
590 sprintf(in->cur, "( ");
591 in->cur = strchr(in->cur, '\0');
592 }
593 infix(in, isopr);
594 if (isopr)
595 {
596 RESIZEBUF(in, 2);
597 sprintf(in->cur, " )");
598 in->cur = strchr(in->cur, '\0');
599 }
600 }
601 else
602 {
603 int32 op = in->curpol->val;
604 INFIX nrm;
605
606 in->curpol--;
607 if (op == (int32) '|' && !first)
608 {
609 RESIZEBUF(in, 2);
610 sprintf(in->cur, "( ");
611 in->cur = strchr(in->cur, '\0');
612 }
613
614 nrm.curpol = in->curpol;
615 nrm.buflen = 16;
616 nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
617
618 /* get right operand */
619 infix(&nrm, false);
620
621 /* get & print left operand */
622 in->curpol = nrm.curpol;
623 infix(in, false);
624
625 /* print operator & right operand */
626 RESIZEBUF(in, 3 + (nrm.cur - nrm.buf));
627 sprintf(in->cur, " %c %s", op, nrm.buf);
628 in->cur = strchr(in->cur, '\0');
629 pfree(nrm.buf);
630
631 if (op == (int32) '|' && !first)
632 {
633 RESIZEBUF(in, 2);
634 sprintf(in->cur, " )");
635 in->cur = strchr(in->cur, '\0');
636 }
637 }
638}
639
640
641Datum
643{
645 INFIX nrm;
646
647 if (query->size == 0)
649 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
650 errmsg("empty query")));
651
652 nrm.curpol = GETQUERY(query) + query->size - 1;
653 nrm.buflen = 32;
654 nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
655 *(nrm.cur) = '\0';
656 infix(&nrm, true);
657
658 PG_FREE_IF_COPY(query, 0);
660}
661
662
663/* Useless old "debugging" function for a fundamentally wrong algorithm */
664Datum
666{
667 elog(ERROR, "querytree is no longer implemented");
669}
#define CLOSE
Definition: _int.h:165
#define OPEN
Definition: _int.h:164
#define END
Definition: _int.h:160
#define PREPAREARR(x)
Definition: _int.h:49
#define COMPUTESIZE(size)
Definition: _int.h:155
#define CHECKARRVALID(x)
Definition: _int.h:30
#define OPR
Definition: _int.h:163
#define VAL
Definition: _int.h:162
#define ERR
Definition: _int.h:161
#define GETQUERY(x)
Definition: _int.h:157
#define QUERYTYPEMAXITEMS
Definition: _int.h:156
#define PG_GETARG_QUERYTYPE_P(n)
Definition: _int.h:170
PG_FUNCTION_INFO_V1(bqarr_in)
Datum querytree(PG_FUNCTION_ARGS)
Definition: _int_bool.c:665
Datum rboolop(PG_FUNCTION_ARGS)
Definition: _int_bool.c:408
static bool checkcondition_arr(void *checkval, ITEM *item, void *options)
Definition: _int_bool.c:233
bool signconsistent(QUERYTYPE *query, BITVECP sign, int siglen, bool calcnot)
Definition: _int_bool.c:298
static void pushquery(WORKSTATE *state, int32 type, int32 val)
Definition: _int_bool.c:136
static bool contains_required_value(ITEM *curitem)
Definition: _int_bool.c:362
static bool checkcondition_bit(void *checkval, ITEM *item, void *siglen)
Definition: _int_bool.c:255
#define RESIZEBUF(inf, addsize)
Definition: _int_bool.c:557
static void findoprnd(ITEM *ptr, int32 *pos)
Definition: _int_bool.c:438
static int32 makepol(WORKSTATE *state)
Definition: _int_bool.c:153
bool query_has_required_values(QUERYTYPE *query)
Definition: _int_bool.c:397
#define WAITOPERAND
Definition: _int_bool.c:17
static int32 gettoken(WORKSTATE *state, int32 *val)
Definition: _int_bool.c:48
Datum bqarr_out(PG_FUNCTION_ARGS)
Definition: _int_bool.c:642
#define WAITENDOPERAND
Definition: _int_bool.c:18
static void infix(INFIX *in, bool first)
Definition: _int_bool.c:565
bool execconsistent(QUERYTYPE *query, ArrayType *array, bool calcnot)
Definition: _int_bool.c:307
#define STACKDEPTH
Definition: _int_bool.c:147
Datum boolop(PG_FUNCTION_ARGS)
Definition: _int_bool.c:417
Datum bqarr_in(PG_FUNCTION_ARGS)
Definition: _int_bool.c:475
static bool checkcondition_gin(void *checkval, ITEM *item, void *options)
Definition: _int_bool.c:326
#define WAITOPERATOR
Definition: _int_bool.c:19
static bool execute(ITEM *curitem, void *checkval, void *options, bool calcnot, bool(*chkcond)(void *checkval, ITEM *item, void *options))
Definition: _int_bool.c:264
struct NODE NODE
bool gin_bool_consistent(QUERYTYPE *query, bool *check)
Definition: _int_bool.c:334
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:264
#define GETBIT(x, i)
Definition: blutils.c:30
int32_t int32
Definition: c.h:484
#define ARRNELEMS(x)
Definition: cube.c:26
#define ARRPTR(x)
Definition: cube.c:25
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereturn(context, dummy_value,...)
Definition: elog.h:277
#define DEBUG3
Definition: elog.h:28
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define HASHVAL(val, siglen)
Definition: hstore_gist.c:45
char * BITVECP
Definition: hstore_gist.c:31
long val
Definition: informix.c:689
char sign
Definition: informix.c:693
int j
Definition: isn.c:73
int i
Definition: isn.c:72
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
static char ** options
static char * buf
Definition: pg_test_fsync.c:72
#define sprintf
Definition: port.h:241
uintptr_t Datum
Definition: postgres.h:69
void check_stack_depth(void)
Definition: stack_depth.c:95
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
int32 * arrb
Definition: _int_bool.c:225
int32 * arre
Definition: _int_bool.c:226
ITEM * first
Definition: _int_bool.c:321
bool * mapped_check
Definition: _int_bool.c:322
char * buf
Definition: _int_bool.c:552
char * cur
Definition: _int_bool.c:553
int32 buflen
Definition: _int_bool.c:554
ITEM * curpol
Definition: _int_bool.c:551
Definition: _int.h:141
int16 left
Definition: _int.h:143
int32 val
Definition: _int.h:144
int16 type
Definition: _int.h:142
Definition: _int_bool.c:26
struct NODE * next
Definition: _int_bool.c:29
int32 val
Definition: _int_bool.c:28
int32 type
Definition: _int_bool.c:27
Definition: nodes.h:129
int32 size
Definition: _int.h:150
char * buf
Definition: _int_bool.c:34
struct Node * escontext
Definition: _int_bool.c:37
int32 state
Definition: _int_bool.c:35
int32 num
Definition: _int_bool.c:41
NODE * str
Definition: _int_bool.c:39
int32 count
Definition: _int_bool.c:36
Definition: regguts.h:323
struct state * next
Definition: regguts.h:332
static ItemArray items
Definition: test_tidstore.c:48
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305
const char * type