query.c 16.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * IO definitions for query_txt and mquery_txt. This type
 * are identical, but for parsing mquery_txt used parser for text
 * and also morphology is used.
 * Internal structure:
 * query tree, then string with original value.
 * Query tree with plain view. It's means that in array of nodes
 * right child is always next and left position = item+item->left
 * Teodor Sigaev <teodor@stack.net>
 */
#include "postgres.h"

Tom Lane's avatar
Tom Lane committed
13
#include <ctype.h>
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <float.h>

#include "access/gist.h"
#include "access/itup.h"
#include "access/rtree.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "storage/bufpage.h"

#include "txtidx.h"
#include "crc32.h"
#include "query.h"
#include "morph.h"
#include "rewrite.h"

#include "deflex.h"
#include "parser.h"

PG_FUNCTION_INFO_V1(mqtxt_in);
33 34
Datum		mqtxt_in(PG_FUNCTION_ARGS);

35
PG_FUNCTION_INFO_V1(qtxt_in);
36 37
Datum		qtxt_in(PG_FUNCTION_ARGS);

38
PG_FUNCTION_INFO_V1(qtxt_out);
39
Datum		qtxt_out(PG_FUNCTION_ARGS);
40 41

PG_FUNCTION_INFO_V1(execqtxt);
42 43
Datum		execqtxt(PG_FUNCTION_ARGS);

44
PG_FUNCTION_INFO_V1(rexecqtxt);
45
Datum		rexecqtxt(PG_FUNCTION_ARGS);
46 47

PG_FUNCTION_INFO_V1(querytree);
48 49 50 51 52 53 54 55 56
Datum		querytree(PG_FUNCTION_ARGS);

#define END			0
#define ERR			1
#define VAL			2
#define OPR			3
#define OPEN		4
#define CLOSE		5
#define VALTRUE		6			/* for stop words */
57 58 59
#define VALFALSE	7

/* parser's states */
60
#define WAITOPERAND 1
61 62 63 64 65 66
#define WAITOPERATOR	2

/*
 * node of query tree, also used
 * for storing polish notation in parser
 */
67 68 69 70 71 72 73
typedef struct NODE
{
	int4		type;
	int4		val;
	int2		distance;
	int2		length;
	struct NODE *next;
74
}	NODE;
75 76 77 78 79 80

typedef struct
{
	char	   *buf;
	int4		state;
	int4		count;
81
	/* reverse polish notation in list (for temporary usage) */
82
	NODE	   *str;
83
	/* number in str */
84
	int4		num;
85 86

	/* user-friendly operand */
87 88 89 90
	int4		lenop;
	int4		sumlen;
	char	   *op;
	char	   *curop;
91 92

	/* state for value's parser */
93
	TI_IN_STATE valstate;
94
}	QPRS_STATE;
95 96 97 98

/*
 * get token from query string
 */
99 100 101 102 103 104 105
static int4
gettoken_query(QPRS_STATE * state, int4 *val, int4 *lenval, char **strval)
{
	while (1)
	{
		switch (state->state)
		{
106
			case WAITOPERAND:
107 108
				if (*(state->buf) == '!')
				{
109
					(state->buf)++;
110
					*val = (int4) '!';
111
					return OPR;
112 113 114
				}
				else if (*(state->buf) == '(')
				{
115 116 117
					state->count++;
					(state->buf)++;
					return OPEN;
118 119 120
				}
				else if (*(state->buf) != ' ')
				{
121 122
					state->valstate.prsbuf = state->buf;
					state->state = WAITOPERATOR;
123 124
					if (gettoken_txtidx(&(state->valstate)))
					{
125 126 127 128
						*strval = state->valstate.word;
						*lenval = state->valstate.curpos - state->valstate.word;
						state->buf = state->valstate.prsbuf;
						return VAL;
129 130
					}
					else
131 132 133
						ereport(ERROR,
								(errcode(ERRCODE_SYNTAX_ERROR),
								 errmsg("no operand")));
134 135 136
				}
				break;
			case WAITOPERATOR:
137 138
				if (*(state->buf) == '&' || *(state->buf) == '|')
				{
139 140
					state->state = WAITOPERAND;
					*val = (int4) *(state->buf);
141
					(state->buf)++;
142
					return OPR;
143 144 145
				}
				else if (*(state->buf) == ')')
				{
146 147
					(state->buf)++;
					state->count--;
148 149 150 151 152
					return (state->count < 0) ? ERR : CLOSE;
				}
				else if (*(state->buf) == '\0')
					return (state->count) ? ERR : END;
				else if (*(state->buf) != ' ')
153 154
					return ERR;
				break;
155
			default:
156 157
				return ERR;
				break;
158 159
		}
		(state->buf)++;
160 161 162 163 164 165 166 167
	}
	return END;
}

/*
 * push new one in polish notation reverse view
 */
static void
168 169 170 171 172 173 174
pushquery(QPRS_STATE * state, int4 type, int4 val, int4 distance, int4 lenval)
{
	NODE	   *tmp = (NODE *) palloc(sizeof(NODE));

	tmp->type = type;
	tmp->val = val;
	if (distance > 0xffff)
175 176 177
		ereport(ERROR,
				(errcode(ERRCODE_SYNTAX_ERROR),
				 errmsg("value is too big")));
178
	if (lenval > 0xffff)
179 180 181
		ereport(ERROR,
				(errcode(ERRCODE_SYNTAX_ERROR),
				 errmsg("operand is too long")));
182 183
	tmp->distance = distance;
	tmp->length = lenval;
184 185 186 187 188 189 190 191 192
	tmp->next = state->str;
	state->str = tmp;
	state->num++;
}

/*
 * This function is used for query_txt parsing
 */
static void
193 194 195
pushval_asis(QPRS_STATE * state, int type, char *strval, int lenval)
{
	if (lenval > 0xffff)
196 197 198
		ereport(ERROR,
				(errcode(ERRCODE_SYNTAX_ERROR),
				 errmsg("word is too long")));
199

200 201 202 203 204 205
	pushquery(state, type, crc32_sz((uint8 *) strval, lenval),
			  state->curop - state->op, lenval);

	while (state->curop - state->op + lenval + 1 >= state->lenop)
	{
		int4		tmp = state->curop - state->op;
206 207

		state->lenop *= 2;
208
		state->op = (char *) repalloc((void *) state->op, state->lenop);
209 210
		state->curop = state->op + tmp;
	}
211
	memcpy((void *) state->curop, (void *) strval, lenval);
212 213 214 215 216 217 218 219 220 221 222
	state->curop += lenval;
	*(state->curop) = '\0';
	state->curop++;
	state->sumlen += lenval + 1;
	return;
}

/*
 * This function is used for mquery_txt parsing
 */
static void
223 224 225 226 227 228 229 230 231 232 233 234
pushval_morph(QPRS_STATE * state, int typeval, char *strval, int lenval)
{
	int4		type,
				lenlemm;
	int4		count = 0;
	char	   *lemm;

	start_parse_str(strval, lenval);
	while ((type = tsearch_yylex()) != 0)
	{
		if (tokenlen > 0xffff)
		{
235
			end_parse();
236 237 238
			ereport(ERROR,
					(errcode(ERRCODE_SYNTAX_ERROR),
					 errmsg("word is too long")));
239 240
		}
		lenlemm = tokenlen;
241 242 243
		lemm = lemmatize(token, &lenlemm, type);
		if (lemm)
		{
Bruce Momjian's avatar
Bruce Momjian committed
244 245 246 247 248 249 250 251
			if (lemm == token)
			{
				char	   *ptrs = token,
						   *ptrd;

				ptrd = lemm = palloc(lenlemm + 1);
				while (ptrs - token < lenlemm)
				{
252 253 254 255
					*ptrd = tolower((unsigned char) *ptrs);
					ptrs++;
					ptrd++;
				}
Bruce Momjian's avatar
Bruce Momjian committed
256 257
				*ptrd = '\0';
			}
258
			pushval_asis(state, VAL, lemm, lenlemm);
259
			pfree(lemm);
260
		}
261
		else
262
			pushval_asis(state, VALTRUE, NULL, 0);
263 264 265
		if (count)
			pushquery(state, OPR, (int4) '&', 0, 0);
		count++;
266 267 268 269 270 271
	}
	end_parse();
}

#define STACKDEPTH	32
/*
272
 * make polish notation of query
273
 */
274 275 276 277 278 279 280 281 282 283 284 285 286 287
static int4
makepol(QPRS_STATE * state, void (*pushval) (QPRS_STATE *, int, char *, int))
{
	int4		val,
				type;
	int4		lenval;
	char	   *strval;
	int4		stack[STACKDEPTH];
	int4		lenstack = 0;

	while ((type = gettoken_query(state, &val, &lenval, &strval)) != END)
	{
		switch (type)
		{
288
			case VAL:
289 290 291 292
				(*pushval) (state, VAL, strval, lenval);
				while (lenstack && (stack[lenstack - 1] == (int4) '&' ||
									stack[lenstack - 1] == (int4) '!'))
				{
293
					lenstack--;
294
					pushquery(state, OPR, stack[lenstack], 0, 0);
295 296 297
				}
				break;
			case OPR:
298 299 300 301 302
				if (lenstack && val == (int4) '|')
					pushquery(state, OPR, val, 0, 0);
				else
				{
					if (lenstack == STACKDEPTH)
303 304
						/* internal error */
						elog(ERROR, "stack too short");
305
					stack[lenstack] = val;
306 307 308 309
					lenstack++;
				}
				break;
			case OPEN:
310 311 312 313 314
				if (makepol(state, pushval) == ERR)
					return ERR;
				if (lenstack && (stack[lenstack - 1] == (int4) '&' ||
								 stack[lenstack - 1] == (int4) '!'))
				{
315
					lenstack--;
316
					pushquery(state, OPR, stack[lenstack], 0, 0);
317 318 319
				}
				break;
			case CLOSE:
320 321
				while (lenstack)
				{
322
					lenstack--;
323
					pushquery(state, OPR, stack[lenstack], 0, 0);
324 325 326 327 328
				};
				return END;
				break;
			case ERR:
			default:
329 330 331 332
				ereport(ERROR,
						(errcode(ERRCODE_SYNTAX_ERROR),
						 errmsg("syntax error")));

333
				return ERR;
334

335 336
		}
	}
337 338
	while (lenstack)
	{
339
		lenstack--;
340
		pushquery(state, OPR, stack[lenstack], 0, 0);
341 342 343 344
	};
	return END;
}

345 346 347 348 349 350
typedef struct
{
	WordEntry  *arrb;
	WordEntry  *arre;
	char	   *values;
	char	   *operand;
351
}	CHKVAL;
352 353 354 355 356

/*
 * compare 2 string values
 */
static int4
357 358 359
ValCompare(CHKVAL * chkval, WordEntry * ptr, ITEM * item)
{
	if (ptr->len == item->length)
360
		return strncmp(
361 362 363
					   &(chkval->values[ptr->pos]),
					   &(chkval->operand[item->distance]),
					   item->length);
364

365 366
	return (ptr->len > item->length) ? 1 : -1;
}
367 368 369 370 371

/*
 * is there value 'val' in array or not ?
 */
static bool
372 373 374 375 376 377 378 379 380 381 382
checkcondition_str(void *checkval, ITEM * val)
{
	WordEntry  *StopLow = ((CHKVAL *) checkval)->arrb;
	WordEntry  *StopHigh = ((CHKVAL *) checkval)->arre;
	WordEntry  *StopMiddle;
	int			difference;

	/* Loop invariant: StopLow <= val < StopHigh */

	while (StopLow < StopHigh)
	{
383
		StopMiddle = StopLow + (StopHigh - StopLow) / 2;
384 385
		difference = ValCompare((CHKVAL *) checkval, StopMiddle, val);
		if (difference == 0)
386 387 388 389 390 391 392
			return (true);
		else if (difference < 0)
			StopLow = StopMiddle + 1;
		else
			StopHigh = StopMiddle;
	}

393
	return (false);
394 395 396 397 398 399
}

/*
 * check for boolean condition
 */
bool
400 401 402 403 404 405 406 407 408 409 410 411 412
execute(ITEM * curitem, void *checkval, bool calcnot, bool (*chkcond) (void *checkval, ITEM * val))
{
	if (curitem->type == VAL)
		return (*chkcond) (checkval, curitem);
	else if (curitem->val == (int4) '!')
	{
		return (calcnot) ?
			((execute(curitem + 1, checkval, calcnot, chkcond)) ? false : true)
			: true;
	}
	else if (curitem->val == (int4) '&')
	{
		if (execute(curitem + curitem->left, checkval, calcnot, chkcond))
413 414 415
			return execute(curitem + 1, checkval, calcnot, chkcond);
		else
			return false;
416 417 418 419
	}
	else
	{							/* |-operator */
		if (execute(curitem + curitem->left, checkval, calcnot, chkcond))
420 421 422 423 424 425 426 427
			return true;
		else
			return execute(curitem + 1, checkval, calcnot, chkcond);
	}
	return false;
}

/*
428
 * boolean operations
429 430
 */
Datum
431 432
rexecqtxt(PG_FUNCTION_ARGS)
{
433
	return DirectFunctionCall2(
434 435 436
							   execqtxt,
							   PG_GETARG_DATUM(1),
							   PG_GETARG_DATUM(0)
437
		);
438 439 440
}

Datum
441 442 443 444 445 446 447
execqtxt(PG_FUNCTION_ARGS)
{
	txtidx	   *val = (txtidx *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
	QUERYTYPE  *query = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
	CHKVAL		chkval;
	bool		result;

448
	if (!val->size || !query->size)
449 450 451 452
	{
		PG_FREE_IF_COPY(val, 0);
		PG_FREE_IF_COPY(query, 1);
		PG_RETURN_BOOL(false);
453 454 455 456 457
	}

	chkval.arrb = ARRPTR(val);
	chkval.arre = chkval.arrb + val->size;
	chkval.values = STRPTR(val);
458 459 460 461 462 463 464 465 466 467 468
	chkval.operand = GETOPERAND(query);
	result = execute(
					 GETQUERY(query),
					 &chkval,
					 true,
					 checkcondition_str
		);

	PG_FREE_IF_COPY(val, 0);
	PG_FREE_IF_COPY(query, 1);
	PG_RETURN_BOOL(result);
469 470 471 472 473 474
}

/*
 * find left operand in polish notation view
 */
static void
475 476
findoprnd(ITEM * ptr, int4 *pos)
{
477
#ifdef BS_DEBUG
478
	elog(DEBUG4, (ptr[*pos].type == OPR) ?
479
		 "%d  %c" : "%d  %d", *pos, ptr[*pos].val);
480
#endif
481 482
	if (ptr[*pos].type == VAL || ptr[*pos].type == VALTRUE)
	{
483 484
		ptr[*pos].left = 0;
		(*pos)++;
485 486 487
	}
	else if (ptr[*pos].val == (int4) '!')
	{
488 489
		ptr[*pos].left = 1;
		(*pos)++;
490 491 492 493 494 495 496
		findoprnd(ptr, pos);
	}
	else
	{
		ITEM	   *curitem = &ptr[*pos];
		int4		tmp = *pos;

497
		(*pos)++;
498
		findoprnd(ptr, pos);
499
		curitem->left = *pos - tmp;
500
		findoprnd(ptr, pos);
501 502 503 504 505 506 507
	}
}


/*
 * input
 */
508 509 510 511 512 513 514 515 516 517 518
static QUERYTYPE *
queryin(char *buf, void (*pushval) (QPRS_STATE *, int, char *, int))
{
	QPRS_STATE	state;
	int4		i;
	QUERYTYPE  *query;
	int4		commonlen;
	ITEM	   *ptr;
	NODE	   *tmp;
	int4		pos = 0;

519
#ifdef BS_DEBUG
520 521
	char		pbuf[16384],
			   *cur;
522 523 524 525 526 527 528
#endif

	/* init state */
	state.buf = buf;
	state.state = WAITOPERAND;
	state.count = 0;
	state.num = 0;
529
	state.str = NULL;
530 531 532

	/* init value parser's state */
	state.valstate.oprisdelim = true;
533 534
	state.valstate.len = 32;
	state.valstate.word = (char *) palloc(state.valstate.len);
535 536

	/* init list of operand */
537 538 539
	state.sumlen = 0;
	state.lenop = 64;
	state.curop = state.op = (char *) palloc(state.lenop);
540
	*(state.curop) = '\0';
541

542
	/* parse query & make polish notation (postfix, but in reverse order) */
543 544 545
	makepol(&state, pushval);
	pfree(state.valstate.word);
	if (!state.num)
546 547 548
		ereport(ERROR,
				(errcode(ERRCODE_SYNTAX_ERROR),
				 errmsg("empty query")));
549 550 551

	/* make finish struct */
	commonlen = COMPUTESIZE(state.num, state.sumlen);
552
	query = (QUERYTYPE *) palloc(commonlen);
553 554 555 556 557
	query->len = commonlen;
	query->size = state.num;
	ptr = GETQUERY(query);

	/* set item in polish notation */
558 559 560
	for (i = 0; i < state.num; i++)
	{
		ptr[i].type = state.str->type;
561
		ptr[i].val = state.str->val;
562
		ptr[i].distance = state.str->distance;
563 564
		ptr[i].length = state.str->length;
		tmp = state.str->next;
565
		pfree(state.str);
566 567 568
		state.str = tmp;
	}

569 570 571
	/* set user friendly-operand view */
	memcpy((void *) GETOPERAND(query), (void *) state.op, state.sumlen);
	pfree(state.op);
572

573
	/* set left operand's position for every operator */
574
	pos = 0;
575
	findoprnd(ptr, &pos);
576 577 578 579

#ifdef BS_DEBUG
	cur = pbuf;
	*cur = '\0';
580 581 582
	for (i = 0; i < query->size; i++)
	{
		if (ptr[i].type == OPR)
583
			sprintf(cur, "%c(%d) ", ptr[i].val, ptr[i].left);
584 585 586
		else
			sprintf(cur, "%d(%s) ", ptr[i].val, GETOPERAND(query) + ptr[i].distance);
		cur = strchr(cur, '\0');
587
	}
588
	elog(DEBUG4, "POR: %s", pbuf);
589 590 591 592 593 594 595 596 597
#endif

	return query;
}

/*
 * in without morphology
 */
Datum
598 599 600
qtxt_in(PG_FUNCTION_ARGS)
{
	PG_RETURN_POINTER(queryin((char *) PG_GETARG_POINTER(0), pushval_asis));
601 602 603 604 605 606
}

/*
 * in with morphology
 */
Datum
607 608 609 610 611 612 613 614 615 616 617
mqtxt_in(PG_FUNCTION_ARGS)
{
	QUERYTYPE  *query;
	ITEM	   *res;
	int4		len;

#ifdef BS_DEBUG
	ITEM	   *ptr;
	int4		i;
	char		pbuf[16384],
			   *cur;
618 619
#endif
	initmorph();
620 621 622 623
	query = queryin((char *) PG_GETARG_POINTER(0), pushval_morph);
	res = clean_fakeval(GETQUERY(query), &len);
	if (!res)
	{
Bruce Momjian's avatar
Bruce Momjian committed
624 625
		query->len = HDRSIZEQT;
		query->size = 0;
626
		PG_RETURN_POINTER(query);
627
	}
628
	memcpy((void *) GETQUERY(query), (void *) res, len * sizeof(ITEM));
629 630 631 632
#ifdef BS_DEBUG
	cur = pbuf;
	*cur = '\0';
	ptr = GETQUERY(query);
633 634 635
	for (i = 0; i < len; i++)
	{
		if (ptr[i].type == OPR)
636
			sprintf(cur, "%c(%d) ", ptr[i].val, ptr[i].left);
637 638 639
		else
			sprintf(cur, "%d(%s) ", ptr[i].val, GETOPERAND(query) + ptr[i].distance);
		cur = strchr(cur, '\0');
640
	}
641
	elog(DEBUG4, "POR: %s", pbuf);
642 643
#endif
	pfree(res);
644
	PG_RETURN_POINTER(query);
645 646 647 648 649 650
}


/*
 * out function
 */
651 652 653 654 655 656 657
typedef struct
{
	ITEM	   *curpol;
	char	   *buf;
	char	   *cur;
	char	   *op;
	int4		buflen;
658
}	INFIX;
659

660 661 662
#define RESIZEBUF(inf,addsize) \
while( ( inf->cur - inf->buf ) + addsize + 1 >= inf->buflen ) \
{ \
663 664 665 666 667 668 669
	int4 len = inf->cur - inf->buf; \
	inf->buflen *= 2; \
	inf->buf = (char*) repalloc( (void*)inf->buf, inf->buflen ); \
	inf->cur = inf->buf + len; \
}

/*
670
 * recursive walk on tree and print it in
671 672 673
 * infix (human-readable) view
 */
static void
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
infix(INFIX * in, bool first)
{
	if (in->curpol->type == VAL)
	{
		char	   *op = in->op + in->curpol->distance;

		RESIZEBUF(in, in->curpol->length * 2 + 2);
		*(in->cur) = '\'';
		in->cur++;
		while (*op)
		{
			if (*op == '\'')
			{
				*(in->cur) = '\\';
				in->cur++;
689 690
			}
			*(in->cur) = *op;
691 692 693 694 695
			op++;
			in->cur++;
		}
		*(in->cur) = '\'';
		in->cur++;
696 697
		*(in->cur) = '\0';
		in->curpol++;
698 699 700 701 702
	}
	else if (in->curpol->val == (int4) '!')
	{
		bool		isopr = false;

703 704 705 706 707
		RESIZEBUF(in, 1);
		*(in->cur) = '!';
		in->cur++;
		*(in->cur) = '\0';
		in->curpol++;
708 709
		if (in->curpol->type == OPR)
		{
710 711 712
			isopr = true;
			RESIZEBUF(in, 2);
			sprintf(in->cur, "( ");
713 714 715 716 717
			in->cur = strchr(in->cur, '\0');
		}
		infix(in, isopr);
		if (isopr)
		{
718 719
			RESIZEBUF(in, 2);
			sprintf(in->cur, " )");
720 721 722 723 724 725 726 727
			in->cur = strchr(in->cur, '\0');
		}
	}
	else
	{
		int4		op = in->curpol->val;
		INFIX		nrm;

728
		in->curpol++;
729 730
		if (op == (int4) '|' && !first)
		{
731 732
			RESIZEBUF(in, 2);
			sprintf(in->cur, "( ");
733
			in->cur = strchr(in->cur, '\0');
734 735 736 737 738
		}

		nrm.curpol = in->curpol;
		nrm.op = in->op;
		nrm.buflen = 16;
739 740
		nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);

741
		/* get right operand */
742 743
		infix(&nrm, false);

744 745
		/* get & print left operand */
		in->curpol = nrm.curpol;
746
		infix(in, false);
747

748 749
		/* print operator & right operand */
		RESIZEBUF(in, 3 + (nrm.cur - nrm.buf));
750
		sprintf(in->cur, " %c %s", op, nrm.buf);
751 752
		in->cur = strchr(in->cur, '\0');
		pfree(nrm.buf);
753

754 755
		if (op == (int4) '|' && !first)
		{
756 757
			RESIZEBUF(in, 2);
			sprintf(in->cur, " )");
758
			in->cur = strchr(in->cur, '\0');
759 760 761 762 763 764
		}
	}
}


Datum
765 766 767 768
qtxt_out(PG_FUNCTION_ARGS)
{
	QUERYTYPE  *query = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
	INFIX		nrm;
769

Bruce Momjian's avatar
Bruce Momjian committed
770 771 772 773 774
	if (query->size == 0)
	{
		char	   *b = palloc(1);

		*b = '\0';
775
		PG_RETURN_POINTER(b);
Bruce Momjian's avatar
Bruce Momjian committed
776
	}
777 778
	nrm.curpol = GETQUERY(query);
	nrm.buflen = 32;
779
	nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
780 781
	*(nrm.cur) = '\0';
	nrm.op = GETOPERAND(query);
782 783 784 785
	infix(&nrm, true);

	PG_FREE_IF_COPY(query, 0);
	PG_RETURN_POINTER(nrm.buf);
786 787 788 789 790 791 792
}

/*
 * debug function, used only for view query
 * which will be executed in non-leaf pages in index
 */
Datum
793 794 795 796 797 798 799
querytree(PG_FUNCTION_ARGS)
{
	QUERYTYPE  *query = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
	INFIX		nrm;
	text	   *res;
	ITEM	   *q;
	int4		len;
800 801


Bruce Momjian's avatar
Bruce Momjian committed
802 803
	if (query->size == 0)
	{
804 805 806
		res = (text *) palloc(VARHDRSZ);
		VARATT_SIZEP(res) = VARHDRSZ;
		PG_RETURN_POINTER(res);
Bruce Momjian's avatar
Bruce Momjian committed
807
	}
808 809 810

	q = clean_NOT(GETQUERY(query), &len);

811 812 813
	if (!q)
	{
		res = (text *) palloc(1 + VARHDRSZ);
814
		VARATT_SIZEP(res) = 1 + VARHDRSZ;
815 816 817 818
		*((char *) VARDATA(res)) = 'T';
	}
	else
	{
819 820
		nrm.curpol = q;
		nrm.buflen = 32;
821
		nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
822 823
		*(nrm.cur) = '\0';
		nrm.op = GETOPERAND(query);
824
		infix(&nrm, true);
825

826 827 828
		res = (text *) palloc(nrm.cur - nrm.buf + VARHDRSZ);
		VARATT_SIZEP(res) = nrm.cur - nrm.buf + VARHDRSZ;
		strncpy(VARDATA(res), nrm.buf, nrm.cur - nrm.buf);
829 830 831
		pfree(q);
	}

832
	PG_FREE_IF_COPY(query, 0);
833

834
	PG_RETURN_POINTER(res);
835
}
836 837

#include "parser.c"