analyze.c 24.0 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * analyze.c--
4
 *	  transform the parse tree into a query tree
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
B
Bruce Momjian 已提交
10
 *	  $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.56 1997/12/24 06:06:18 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14

15 16
#include <stdio.h>
#include <stdlib.h>
17
#include <stdarg.h>
18
#include <string.h>
V
Vadim B. Mikheev 已提交
19

B
Bruce Momjian 已提交
20
#include "postgres.h"
21 22 23 24 25 26
#include "access/heapam.h"
#include "nodes/makefuncs.h"
#include "nodes/memnodes.h"
#include "nodes/pg_list.h"
#include "parser/analyze.h"
#include "parser/parse_agg.h"
B
Bruce Momjian 已提交
27
#include "parser/parse_clause.h"
28 29 30 31 32
#include "parser/parse_node.h"
#include "parser/parse_relation.h"
#include "parser/parse_target.h"
#include "utils/builtins.h"
#include "utils/mcxt.h"
33

34 35 36 37 38 39 40 41 42
static Query *transformStmt(ParseState *pstate, Node *stmt);
static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
static Query *transformInsertStmt(ParseState *pstate, AppendStmt *stmt);
static Query *transformIndexStmt(ParseState *pstate, IndexStmt *stmt);
static Query *transformExtendStmt(ParseState *pstate, ExtendStmt *stmt);
static Query *transformRuleStmt(ParseState *query, RuleStmt *stmt);
static Query *transformSelectStmt(ParseState *pstate, RetrieveStmt *stmt);
static Query *transformUpdateStmt(ParseState *pstate, ReplaceStmt *stmt);
static Query *transformCursorStmt(ParseState *pstate, CursorStmt *stmt);
43
static Query *transformCreateStmt(ParseState *pstate, CreateStmt *stmt);
44

45
List   *extras = NIL;
B
Bruce Momjian 已提交
46

47 48
/*
 * parse_analyze -
49
 *	  analyze a list of parse trees and transform them if necessary.
50 51 52 53 54 55
 *
 * Returns a list of transformed parse trees. Optimizable statements are
 * all transformed to Query while the rest stays the same.
 *
 * CALLER is responsible for freeing the QueryTreeList* returned
 */
56
QueryTreeList *
57
parse_analyze(List *pl)
58
{
59 60 61
	QueryTreeList *result;
	ParseState *pstate;
	int			i = 0;
62 63 64 65 66 67 68

	result = malloc(sizeof(QueryTreeList));
	result->len = length(pl);
	result->qtrees = (Query **) malloc(result->len * sizeof(Query *));

	while (pl != NIL)
	{
69
		pstate = make_parsestate();
70
		result->qtrees[i++] = transformStmt(pstate, lfirst(pl));
71 72 73 74 75 76 77 78 79 80 81
		if (extras != NIL)
		{
			result->len += length(extras);
			result->qtrees = (Query **) realloc(result->qtrees, result->len * sizeof(Query *));
			while (extras != NIL)
			{
				result->qtrees[i++] = transformStmt(pstate, lfirst(extras));
				extras = lnext(extras);
			}
		}
		extras = NIL;
82 83 84 85 86 87 88
		pl = lnext(pl);
		if (pstate->p_target_relation != NULL)
			heap_close(pstate->p_target_relation);
		free(pstate);
	}

	return result;
89 90 91 92
}

/*
 * transformStmt -
93 94
 *	  transform a Parse tree. If it is an optimizable statement, turn it
 *	  into a Query tree.
95
 */
96
static Query *
97
transformStmt(ParseState *pstate, Node *parseTree)
98
{
99
	Query	   *result = NULL;
100 101 102

	switch (nodeTag(parseTree))
	{
103 104 105 106
			/*------------------------
			 *	Non-optimizable statements
			 *------------------------
			 */
107 108 109 110
		case T_CreateStmt:
			result = transformCreateStmt(pstate, (CreateStmt *) parseTree);
			break;

111 112 113
		case T_IndexStmt:
			result = transformIndexStmt(pstate, (IndexStmt *) parseTree);
			break;
114

115 116 117
		case T_ExtendStmt:
			result = transformExtendStmt(pstate, (ExtendStmt *) parseTree);
			break;
118

119 120 121
		case T_RuleStmt:
			result = transformRuleStmt(pstate, (RuleStmt *) parseTree);
			break;
122

123 124 125
		case T_ViewStmt:
			{
				ViewStmt   *n = (ViewStmt *) parseTree;
126

127 128 129 130 131 132
				n->query = (Query *) transformStmt(pstate, (Node *) n->query);
				result = makeNode(Query);
				result->commandType = CMD_UTILITY;
				result->utilityStmt = (Node *) n;
			}
			break;
133

134 135 136
		case T_VacuumStmt:
			{
				MemoryContext oldcontext;
137

138 139 140 141 142 143 144 145 146 147 148 149
				/*
				 * make sure that this Query is allocated in TopMemory
				 * context because vacuum spans transactions and we don't
				 * want to lose the vacuum Query due to end-of-transaction
				 * free'ing
				 */
				oldcontext = MemoryContextSwitchTo(TopMemoryContext);
				result = makeNode(Query);
				result->commandType = CMD_UTILITY;
				result->utilityStmt = (Node *) parseTree;
				MemoryContextSwitchTo(oldcontext);
				break;
150

151 152 153 154
			}
		case T_ExplainStmt:
			{
				ExplainStmt *n = (ExplainStmt *) parseTree;
155

156 157 158 159 160 161
				result = makeNode(Query);
				result->commandType = CMD_UTILITY;
				n->query = transformStmt(pstate, (Node *) n->query);
				result->utilityStmt = (Node *) parseTree;
			}
			break;
162

163 164 165 166 167 168 169
			/*------------------------
			 *	Optimizable statements
			 *------------------------
			 */
		case T_AppendStmt:
			result = transformInsertStmt(pstate, (AppendStmt *) parseTree);
			break;
170

171 172 173
		case T_DeleteStmt:
			result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
			break;
174

175 176 177
		case T_ReplaceStmt:
			result = transformUpdateStmt(pstate, (ReplaceStmt *) parseTree);
			break;
178

179 180 181
		case T_CursorStmt:
			result = transformCursorStmt(pstate, (CursorStmt *) parseTree);
			break;
182

183 184 185
		case T_RetrieveStmt:
			result = transformSelectStmt(pstate, (RetrieveStmt *) parseTree);
			break;
186

187
		default:
188

189 190 191 192 193 194 195 196
			/*
			 * other statments don't require any transformation-- just
			 * return the original parsetree
			 */
			result = makeNode(Query);
			result->commandType = CMD_UTILITY;
			result->utilityStmt = (Node *) parseTree;
			break;
197 198
	}
	return result;
199 200 201 202
}

/*
 * transformDeleteStmt -
203
 *	  transforms a Delete Statement
204
 */
205
static Query *
206
transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
207
{
208
	Query	   *qry = makeNode(Query);
209

210
	qry->commandType = CMD_DELETE;
211

212 213
	/* set up a range table */
	makeRangeTable(pstate, stmt->relname, NULL);
214

215
	qry->uniqueFlag = NULL;
216

217 218
	/* fix where clause */
	qry->qual = transformWhereClause(pstate, stmt->whereClause);
219

220 221 222 223 224 225
	qry->rtable = pstate->p_rtable;
	qry->resultRelation = refnameRangeTablePosn(pstate->p_rtable, stmt->relname);

	/* make sure we don't have aggregates in the where clause */
	if (pstate->p_numAgg > 0)
		parseCheckAggregates(pstate, qry);
226

227
	return (Query *) qry;
228 229 230 231
}

/*
 * transformInsertStmt -
232
 *	  transform an Insert Statement
233
 */
234
static Query *
235
transformInsertStmt(ParseState *pstate, AppendStmt *stmt)
236
{
237
	Query	   *qry = makeNode(Query);	/* make a new query tree */
238
	List	   *icolumns;
239

240 241
	qry->commandType = CMD_INSERT;
	pstate->p_is_insert = true;
242

243 244
	/* set up a range table */
	makeRangeTable(pstate, stmt->relname, stmt->fromClause);
245

246
	qry->uniqueFlag = NULL;
247

248
	/* fix the target list */
249 250
	icolumns = pstate->p_insert_columns = makeTargetNames(pstate, stmt->cols);
	
251
	qry->targetList = transformTargetList(pstate, stmt->targetList);
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	
	/* DEFAULT handling */
	if (length(qry->targetList) < pstate->p_target_relation->rd_att->natts &&
		pstate->p_target_relation->rd_att->constr &&
		pstate->p_target_relation->rd_att->constr->num_defval > 0)
	{
		AttributeTupleForm	   *att = pstate->p_target_relation->rd_att->attrs;
		AttrDefault			   *defval = pstate->p_target_relation->rd_att->constr->defval;
		int						ndef = pstate->p_target_relation->rd_att->constr->num_defval;
		
		/* 
		 * if stmt->cols == NIL then makeTargetNames returns list of all 
		 * attrs: have to shorter icolumns list...
		 */
		if (stmt->cols == NIL)
		{
			List   *extrl;
			int		i = length(qry->targetList);
			
			foreach (extrl, icolumns)
			{
				if (--i <= 0)
					break;
			}
			freeList (lnext(extrl));
			lnext(extrl) = NIL;
		}
		
		while (ndef-- > 0)
		{
			List		   *tl;
			Ident		   *id;
			TargetEntry	   *te;
			
			foreach (tl, icolumns)
			{
				id = (Ident *) lfirst(tl);
				if (!namestrcmp(&(att[defval[ndef].adnum - 1]->attname), id->name))
					break;
			}
			if (tl != NIL)		/* something given for this attr */
				continue;
			/* 
			 * Nothing given for this attr with DEFAULT expr, so
			 * add new TargetEntry to qry->targetList. 
			 * Note, that we set resno to defval[ndef].adnum:
			 * it's what transformTargetList()->make_targetlist_expr()
			 * does for INSERT ... SELECT. But for INSERT ... VALUES
			 * pstate->p_last_resno is used. It doesn't matter for 
			 * "normal" using (planner creates proper target list
			 * in preptlist.c), but may break RULEs in some way.
			 * It seems better to create proper target list here...
			 */
			te = makeNode(TargetEntry);
			te->resdom = makeResdom(defval[ndef].adnum,
									att[defval[ndef].adnum - 1]->atttypid,
									att[defval[ndef].adnum - 1]->attlen,
									pstrdup(nameout(&(att[defval[ndef].adnum - 1]->attname))),
									0, 0, 0);
			te->fjoin = NULL;
			te->expr = (Node *) stringToNode(defval[ndef].adbin);
			qry->targetList = lappend (qry->targetList, te);
		}
	}
	
317 318
	/* fix where clause */
	qry->qual = transformWhereClause(pstate, stmt->whereClause);
319

320 321 322
	/* now the range table will not change */
	qry->rtable = pstate->p_rtable;
	qry->resultRelation = refnameRangeTablePosn(pstate->p_rtable, stmt->relname);
323

324 325
	if (pstate->p_numAgg > 0)
		finalizeAggregates(pstate, qry);
326

327
	return (Query *) qry;
328 329
}

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/* makeTableName()
 * Create a table name from a list of fields.
 */
static char *
makeTableName(void *elem,...);

static char *
makeTableName(void *elem,...)
{
	va_list	args;

	char   *name;
	char	buf[NAMEDATALEN+1];

	strcpy(buf,"");

	va_start(args,elem);

	name = elem;
	while (name != NULL)
	{
		/* not enough room for next part? then return nothing */
		if ((strlen(buf)+strlen(name)) >= (sizeof(buf)-1))
			return (NULL);

		if (strlen(buf) > 0) strcat(buf,"_");
		strcat(buf,name);

		name = va_arg(args,void *);
	}

	va_end(args);

	name = palloc(strlen(buf)+1);
	strcpy(name,buf);

	return (name);
} /* makeTableName() */

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
char *
CreateIndexName(char *tname, char *cname, char *label, List *indices);

char *
CreateIndexName(char *tname, char *cname, char *label, List *indices)
{
	int			pass = 0;
	char	   *iname = NULL;
	List	   *ilist;
	IndexStmt  *index;
	char		name2[NAMEDATALEN+1];

	/* use working storage, since we might be trying several possibilities */
	strcpy(name2,cname);
	while (iname == NULL)
	{
		iname = makeTableName(tname, name2, label, NULL);
		/* unable to make a name at all? then quit */
		if (iname == NULL)
			break;

#if PARSEDEBUG
printf("CreateNameIndex- check %s against indices\n",iname);
#endif

		ilist = indices;
		while (ilist != NIL)
		{
			index = lfirst(ilist);
#if PARSEDEBUG
printf("CreateNameIndex- compare %s with existing index %s\n",iname,index->idxname);
#endif
			if (strcasecmp(iname,index->idxname) == 0)
				break;

			ilist = lnext(ilist);
		}
		/* ran through entire list? then no name conflict found so done */
		if (ilist == NIL)
			break;

		/* the last one conflicted, so try a new name component */
		pfree(iname);
		iname = NULL;
		pass++;
		sprintf(name2, "%s_%d", cname, (pass+1));
	}

	return (iname);
} /* CreateIndexName() */

420 421 422 423 424 425 426 427 428 429 430 431 432
/*
 * transformCreateStmt -
 *	  transforms the "create table" statement
 *	  SQL92 allows constraints to be scattered all over, so thumb through
 *	   the columns and collect all constraints into one place.
 *	  If there are any implied indices (e.g. UNIQUE or PRIMARY KEY)
 *	   then expand those into multiple IndexStmt blocks.
 *	  - thomas 1997-12-02
 */
static Query *
transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
{
	Query	   *q;
433
	int			have_pkey = FALSE;
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	List	   *elements;
	Node	   *element;
	List	   *columns;
	List	   *dlist;
	ColumnDef  *column;
	List	   *constraints, *clist;
	Constraint *constraint;
	List	   *keys;
	Ident	   *key;
	List	   *ilist;
	IndexStmt  *index;
	IndexElem  *iparam;

	q = makeNode(Query);
	q->commandType = CMD_UTILITY;

	elements = stmt->tableElts;
	constraints = stmt->constraints;
	columns = NIL;
	dlist = NIL;

	while (elements != NIL)
	{
		element = lfirst(elements);
		switch (nodeTag(element))
		{
			case T_ColumnDef:
				column = (ColumnDef *) element;
#if PARSEDEBUG
printf("transformCreateStmt- found column %s\n",column->colname);
#endif
				columns = lappend(columns,column);
				if (column->constraints != NIL)
				{
#if PARSEDEBUG
printf("transformCreateStmt- found constraint(s) on column %s\n",column->colname);
#endif
					clist = column->constraints;
					while (clist != NIL)
					{
						constraint = lfirst(clist);
						switch (constraint->contype)
						{
							case CONSTR_NOTNULL:
#if PARSEDEBUG
printf("transformCreateStmt- found NOT NULL constraint on column %s\n",column->colname);
#endif
								if (column->is_not_null)
									elog(WARN,"CREATE TABLE/NOT NULL already specified"
										" for %s.%s", stmt->relname, column->colname);
								column->is_not_null = TRUE;
								break;

							case CONSTR_DEFAULT:
#if PARSEDEBUG
printf("transformCreateStmt- found DEFAULT clause on column %s\n",column->colname);
#endif
								if (column->defval != NULL)
									elog(WARN,"CREATE TABLE/DEFAULT multiple values specified"
										" for %s.%s", stmt->relname, column->colname);
								column->defval = constraint->def;
								break;

							case CONSTR_PRIMARY:
#if PARSEDEBUG
printf("transformCreateStmt- found PRIMARY KEY clause on column %s\n",column->colname);
#endif
								if (constraint->name == NULL)
									constraint->name = makeTableName(stmt->relname, "pkey", NULL);
								if (constraint->keys == NIL)
									constraint->keys = lappend(constraint->keys, column);
								dlist = lappend(dlist, constraint);
								break;

							case CONSTR_UNIQUE:
#if PARSEDEBUG
printf("transformCreateStmt- found UNIQUE clause on column %s\n",column->colname);
#endif
								if (constraint->name == NULL)
									constraint->name = makeTableName(stmt->relname, column->colname, "key", NULL);
								if (constraint->keys == NIL)
									constraint->keys = lappend(constraint->keys, column);
								dlist = lappend(dlist, constraint);
								break;

							case CONSTR_CHECK:
#if PARSEDEBUG
printf("transformCreateStmt- found CHECK clause on column %s\n",column->colname);
#endif
								constraints = lappend(constraints, constraint);
								if (constraint->name == NULL)
525
									constraint->name = makeTableName(stmt->relname, column->colname, NULL);
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
								break;

							default:
								elog(WARN,"parser: internal error; unrecognized constraint",NULL);
								break;
						}
						clist = lnext(clist);
					}
				}
				break;

			case T_Constraint:
				constraint = (Constraint *) element;
#if PARSEDEBUG
printf("transformCreateStmt- found constraint %s\n", ((constraint->name != NULL)? constraint->name: "(unknown)"));
#endif
				switch (constraint->contype)
				{
					case CONSTR_PRIMARY:
#if PARSEDEBUG
printf("transformCreateStmt- found PRIMARY KEY clause\n");
#endif
						if (constraint->name == NULL)
							constraint->name = makeTableName(stmt->relname, "pkey", NULL);
						dlist = lappend(dlist, constraint);
						break;

					case CONSTR_UNIQUE:
#if PARSEDEBUG
printf("transformCreateStmt- found UNIQUE clause\n");
#endif
#if FALSE
						if (constraint->name == NULL)
							constraint->name = makeTableName(stmt->relname, "key", NULL);
#endif
						dlist = lappend(dlist, constraint);
						break;

					case CONSTR_CHECK:
#if PARSEDEBUG
printf("transformCreateStmt- found CHECK clause\n");
#endif
						constraints = lappend(constraints, constraint);
						break;

					case CONSTR_NOTNULL:
					case CONSTR_DEFAULT:
						elog(WARN,"parser: internal error; illegal context for constraint",NULL);
						break;
					default:
						elog(WARN,"parser: internal error; unrecognized constraint",NULL);
						break;
				}
				break;

			default:
				elog(WARN,"parser: internal error; unrecognized node",NULL);
		}

		elements = lnext(elements);
	}

	stmt->tableElts = columns;
	stmt->constraints = constraints;

/* Now run through the "deferred list" to complete the query transformation.
 * For PRIMARY KEYs, mark each column as NOT NULL and create an index.
 * For UNIQUE, create an index as for PRIMARY KEYS, but do not insist on NOT NULL.
 *
 * Note that this code does not currently look for all possible redundant cases
596 597 598
 *  and either ignore or stop with warning. The create might fail later when
 *  names for indices turn out to be redundant, or a user might have specified
 *  extra useless indices which might hurt performance. - thomas 1997-12-08
599 600 601 602 603 604 605 606 607 608 609 610 611
 */
	ilist = NIL;
	while (dlist != NIL)
	{
		constraint = lfirst(dlist);
		if (nodeTag(constraint) != T_Constraint)
			elog(WARN,"parser: internal error; unrecognized deferred node",NULL);

#if PARSEDEBUG
printf("transformCreateStmt- found deferred constraint %s\n",
 ((constraint->name != NULL)? constraint->name: "(unknown)"));
#endif

612 613 614 615 616 617 618 619 620
		if (constraint->contype == CONSTR_PRIMARY)
			if (have_pkey)
				elog(WARN,"CREATE TABLE/PRIMARY KEY multiple primary keys"
					" for table %s are not legal", stmt->relname);
			else 
				have_pkey = TRUE;
		else if (constraint->contype != CONSTR_UNIQUE)
			elog(WARN,"parser: internal error; unrecognized deferred constraint",NULL);

621 622 623 624
#if PARSEDEBUG
printf("transformCreateStmt- found deferred %s clause\n",
 (constraint->contype == CONSTR_PRIMARY? "PRIMARY KEY": "UNIQUE"));
#endif
625

626 627 628 629 630 631
		index = makeNode(IndexStmt);

		index->unique = TRUE;
		if (constraint->name != NULL)
			index->idxname = constraint->name;
		else if (constraint->contype == CONSTR_PRIMARY)
632 633 634 635 636
		{
			if (have_pkey)
				elog(WARN,"CREATE TABLE/PRIMARY KEY multiple keys for table %s are not legal", stmt->relname);

			have_pkey = TRUE;
637
			index->idxname = makeTableName(stmt->relname, "pkey", NULL);
638
		}
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
		else
			index->idxname = NULL;

		index->relname = stmt->relname;
		index->accessMethod = "btree";
		index->indexParams = NIL;
		index->withClause = NIL;
		index->whereClause = NULL;
 
		keys = constraint->keys;
		while (keys != NIL)
		{
			key = lfirst(keys);
#if PARSEDEBUG
printf("transformCreateStmt- check key %s for column match\n", key->name);
#endif
			columns = stmt->tableElts;
			column = NULL;
			while (columns != NIL)
			{
				column = lfirst(columns);
#if PARSEDEBUG
printf("transformCreateStmt- check column %s for key match\n", column->colname);
#endif
				if (strcasecmp(column->colname,key->name) == 0) break;
				else column = NULL;
				columns = lnext(columns);
			}
			if (column == NULL)
				elog(WARN,"parser: column '%s' in key does not exist",key->name);

			if (constraint->contype == CONSTR_PRIMARY)
			{
#if PARSEDEBUG
printf("transformCreateStmt- mark column %s as NOT NULL\n", column->colname);
#endif
				column->is_not_null = TRUE;
			}
			iparam = makeNode(IndexElem);
			iparam->name = strcpy(palloc(strlen(column->colname)+1), column->colname);
			iparam->args = NIL;
			iparam->class = NULL;
			iparam->tname = NULL;
			index->indexParams = lappend(index->indexParams, iparam);

			if (index->idxname == NULL)
685
				index->idxname = CreateIndexName(stmt->relname, iparam->name, "key", ilist);
686 687 688 689 690 691 692

			keys = lnext(keys);
		}

		if (index->idxname == NULL)
			elog(WARN,"parser: unable to construct implicit index for table %s"
				"; name too long", stmt->relname);
693 694 695 696
		else
			elog(NOTICE,"CREATE TABLE/%s will create implicit index %s for table %s",
				((constraint->contype == CONSTR_PRIMARY)? "PRIMARY KEY": "UNIQUE"),
				index->idxname, stmt->relname);
697

698
		ilist = lappend(ilist, index);
699 700 701 702 703 704 705 706 707
		dlist = lnext(dlist);
	}

	q->utilityStmt = (Node *) stmt;
	extras = ilist;

	return q;
} /* transformCreateStmt() */

708 709
/*
 * transformIndexStmt -
710
 *	  transforms the qualification of the index statement
711
 */
712
static Query *
713
transformIndexStmt(ParseState *pstate, IndexStmt *stmt)
714
{
715
	Query	   *q;
716

717 718
	q = makeNode(Query);
	q->commandType = CMD_UTILITY;
719

720 721 722
	/* take care of the where clause */
	stmt->whereClause = transformWhereClause(pstate, stmt->whereClause);
	stmt->rangetable = pstate->p_rtable;
723

724 725 726
	q->utilityStmt = (Node *) stmt;

	return q;
727 728 729 730
}

/*
 * transformExtendStmt -
731
 *	  transform the qualifications of the Extend Index Statement
732 733
 *
 */
734
static Query *
735
transformExtendStmt(ParseState *pstate, ExtendStmt *stmt)
736
{
737
	Query	   *q;
738

739 740
	q = makeNode(Query);
	q->commandType = CMD_UTILITY;
741

742 743 744
	/* take care of the where clause */
	stmt->whereClause = transformWhereClause(pstate, stmt->whereClause);
	stmt->rangetable = pstate->p_rtable;
745

746 747
	q->utilityStmt = (Node *) stmt;
	return q;
748 749 750 751
}

/*
 * transformRuleStmt -
752 753
 *	  transform a Create Rule Statement. The actions is a list of parse
 *	  trees which is transformed into a list of query trees.
754
 */
755
static Query *
756
transformRuleStmt(ParseState *pstate, RuleStmt *stmt)
757
{
758 759
	Query	   *q;
	List	   *actions;
760 761 762 763 764 765

	q = makeNode(Query);
	q->commandType = CMD_UTILITY;

	actions = stmt->actions;

766
	/*
767
	 * transform each statment, like parse_analyze()
768
	 */
769 770
	while (actions != NIL)
	{
771

772 773 774 775 776
		/*
		 * NOTE: 'CURRENT' must always have a varno equal to 1 and 'NEW'
		 * equal to 2.
		 */
		addRangeTableEntry(pstate, stmt->object->relname, "*CURRENT*",
777
						   FALSE, FALSE);
778
		addRangeTableEntry(pstate, stmt->object->relname, "*NEW*",
779
						   FALSE, FALSE);
780 781 782 783 784 785 786 787 788

		pstate->p_last_resno = 1;
		pstate->p_is_rule = true;		/* for expand all */
		pstate->p_numAgg = 0;
		pstate->p_aggs = NULL;

		lfirst(actions) = transformStmt(pstate, lfirst(actions));
		actions = lnext(actions);
	}
789

790 791
	/* take care of the where clause */
	stmt->whereClause = transformWhereClause(pstate, stmt->whereClause);
792

793 794
	q->utilityStmt = (Node *) stmt;
	return q;
795 796 797 798 799
}


/*
 * transformSelectStmt -
800
 *	  transforms a Select Statement
801 802
 *
 */
803
static Query *
804
transformSelectStmt(ParseState *pstate, RetrieveStmt *stmt)
805
{
806
	Query	   *qry = makeNode(Query);
807 808

	qry->commandType = CMD_SELECT;
809

810 811
	/* set up a range table */
	makeRangeTable(pstate, NULL, stmt->fromClause);
812

813
	qry->uniqueFlag = stmt->unique;
814

815 816
	qry->into = stmt->into;
	qry->isPortal = FALSE;
817

818 819
	/* fix the target list */
	qry->targetList = transformTargetList(pstate, stmt->targetList);
820

821 822
	/* fix where clause */
	qry->qual = transformWhereClause(pstate, stmt->whereClause);
823

B
Bruce Momjian 已提交
824
	/* check having clause */
825 826
	if (stmt->havingClause)
		elog(NOTICE, "HAVING not yet supported; ignore clause", NULL);
827

828 829 830 831 832
	/* fix order clause */
	qry->sortClause = transformSortClause(pstate,
										  stmt->sortClause,
										  qry->targetList,
										  qry->uniqueFlag);
833

834 835 836 837
	qry->groupClause = transformGroupClause(pstate,
											stmt->groupClause,
											qry->targetList);
	qry->rtable = pstate->p_rtable;
838

839 840
	if (pstate->p_numAgg > 0)
		finalizeAggregates(pstate, qry);
841

B
Bruce Momjian 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 855
	if (stmt->unionClause)
	{
		List *ulist = NIL;
		QueryTreeList *qlist;
		int i;
		
		qlist = parse_analyze(stmt->unionClause);
		for (i=0; i < qlist->len; i++)
			ulist = lappend(ulist, qlist->qtrees[i]);
		qry->unionClause = ulist;
	}
	else
	    qry->unionClause = NULL;

856
	return (Query *) qry;
857 858 859 860
}

/*
 * transformUpdateStmt -
861
 *	  transforms an update statement
862 863
 *
 */
864
static Query *
865
transformUpdateStmt(ParseState *pstate, ReplaceStmt *stmt)
866
{
867
	Query	   *qry = makeNode(Query);
868 869 870

	qry->commandType = CMD_UPDATE;
	pstate->p_is_update = true;
871

872 873 874 875 876
	/*
	 * the FROM clause is non-standard SQL syntax. We used to be able to
	 * do this with REPLACE in POSTQUEL so we keep the feature.
	 */
	makeRangeTable(pstate, stmt->relname, stmt->fromClause);
877

878 879
	/* fix the target list */
	qry->targetList = transformTargetList(pstate, stmt->targetList);
880

881 882
	/* fix where clause */
	qry->qual = transformWhereClause(pstate, stmt->whereClause);
883

884 885
	qry->rtable = pstate->p_rtable;
	qry->resultRelation = refnameRangeTablePosn(pstate->p_rtable, stmt->relname);
886

887 888 889
	/* make sure we don't have aggregates in the where clause */
	if (pstate->p_numAgg > 0)
		parseCheckAggregates(pstate, qry);
890

891
	return (Query *) qry;
892 893 894 895
}

/*
 * transformCursorStmt -
896
 *	  transform a Create Cursor Statement
897 898
 *
 */
899
static Query *
900
transformCursorStmt(ParseState *pstate, CursorStmt *stmt)
901
{
902
	Query	   *qry = makeNode(Query);
903

904 905 906 907 908 909
	/*
	 * in the old days, a cursor statement is a 'retrieve into portal'; If
	 * you change the following, make sure you also go through the code in
	 * various places that tests the kind of operation.
	 */
	qry->commandType = CMD_SELECT;
910

911 912
	/* set up a range table */
	makeRangeTable(pstate, NULL, stmt->fromClause);
913

914
	qry->uniqueFlag = stmt->unique;
915

916 917 918
	qry->into = stmt->portalname;
	qry->isPortal = TRUE;
	qry->isBinary = stmt->binary;		/* internal portal */
919

920 921
	/* fix the target list */
	qry->targetList = transformTargetList(pstate, stmt->targetList);
922

923 924
	/* fix where clause */
	qry->qual = transformWhereClause(pstate, stmt->whereClause);
925

926 927 928 929 930 931 932 933 934
	/* fix order clause */
	qry->sortClause = transformSortClause(pstate,
										  stmt->sortClause,
										  qry->targetList,
										  qry->uniqueFlag);
	/* fix group by clause */
	qry->groupClause = transformGroupClause(pstate,
											stmt->groupClause,
											qry->targetList);
M
Fixes:  
Marc G. Fournier 已提交
935

936
	qry->rtable = pstate->p_rtable;
937

938 939
	if (pstate->p_numAgg > 0)
		finalizeAggregates(pstate, qry);
940

941
	return (Query *) qry;
942
}