1 /*-------------------------------------------------------------------------
2  *
3  * analyze.c
4  *	  transform the raw parse tree into a query tree
5  *
6  * For optimizable statements, we are careful to obtain a suitable lock on
7  * each referenced table, and other modules of the backend preserve or
8  * re-obtain these locks before depending on the results.  It is therefore
9  * okay to do significant semantic analysis of these statements.  For
10  * utility commands, no locks are obtained here (and if they were, we could
11  * not be sure we'd still have them at execution).  Hence the general rule
12  * for utility commands is to just dump them into a Query node untransformed.
13  * DECLARE CURSOR, EXPLAIN, and CREATE TABLE AS are exceptions because they
14  * contain optimizable statements, which we should transform.
15  *
16  *
17  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
18  * Portions Copyright (c) 1994, Regents of the University of California
19  *
20  *	src/backend/parser/analyze.c
21  *
22  *-------------------------------------------------------------------------
23  */
24 
25 #include "postgres.h"
26 
27 #include "access/sysattr.h"
28 #include "catalog/pg_type.h"
29 #include "miscadmin.h"
30 #include "nodes/makefuncs.h"
31 #include "nodes/nodeFuncs.h"
32 #include "optimizer/var.h"
33 #include "parser/analyze.h"
34 #include "parser/parse_agg.h"
35 #include "parser/parse_clause.h"
36 #include "parser/parse_coerce.h"
37 #include "parser/parse_collate.h"
38 #include "parser/parse_cte.h"
39 #include "parser/parse_expr.h"
40 #include "parser/parse_func.h"
41 #include "parser/parse_oper.h"
42 #include "parser/parse_param.h"
43 #include "parser/parse_relation.h"
44 #include "parser/parse_target.h"
45 #include "parser/parsetree.h"
46 #include "rewrite/rewriteManip.h"
47 #include "utils/rel.h"
48 
49 
50 /* Hook for plugins to get control at end of parse analysis */
51 post_parse_analyze_hook_type post_parse_analyze_hook = NULL;
52 
53 static Query *transformOptionalSelectInto(ParseState *pstate, Node *parseTree);
54 static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
55 static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
56 static List *transformInsertRow(ParseState *pstate, List *exprlist,
57 				   List *stmtcols, List *icolumns, List *attrnos,
58 				   bool strip_indirection);
59 static OnConflictExpr *transformOnConflictClause(ParseState *pstate,
60 						  OnConflictClause *onConflictClause);
61 static int	count_rowexpr_columns(ParseState *pstate, Node *expr);
62 static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt);
63 static Query *transformValuesClause(ParseState *pstate, SelectStmt *stmt);
64 static Query *transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt);
65 static Node *transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
66 						  bool isTopLevel, List **targetlist);
67 static void determineRecursiveColTypes(ParseState *pstate,
68 						   Node *larg, List *nrtargetlist);
69 static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
70 static List *transformReturningList(ParseState *pstate, List *returningList);
71 static List *transformUpdateTargetList(ParseState *pstate,
72 						  List *targetList);
73 static Query *transformDeclareCursorStmt(ParseState *pstate,
74 						   DeclareCursorStmt *stmt);
75 static Query *transformExplainStmt(ParseState *pstate,
76 					 ExplainStmt *stmt);
77 static Query *transformCreateTableAsStmt(ParseState *pstate,
78 						   CreateTableAsStmt *stmt);
79 static Query *transformCallStmt(ParseState *pstate,
80 				  CallStmt *stmt);
81 static void transformLockingClause(ParseState *pstate, Query *qry,
82 					   LockingClause *lc, bool pushedDown);
83 #ifdef RAW_EXPRESSION_COVERAGE_TEST
84 static bool test_raw_expression_coverage(Node *node, void *context);
85 #endif
86 
87 
88 /*
89  * parse_analyze
90  *		Analyze a raw parse tree and transform it to Query form.
91  *
92  * Optionally, information about $n parameter types can be supplied.
93  * References to $n indexes not defined by paramTypes[] are disallowed.
94  *
95  * The result is a Query node.  Optimizable statements require considerable
96  * transformation, while utility-type statements are simply hung off
97  * a dummy CMD_UTILITY Query node.
98  */
99 Query *
parse_analyze(RawStmt * parseTree,const char * sourceText,Oid * paramTypes,int numParams,QueryEnvironment * queryEnv)100 parse_analyze(RawStmt *parseTree, const char *sourceText,
101 			  Oid *paramTypes, int numParams,
102 			  QueryEnvironment *queryEnv)
103 {
104 	ParseState *pstate = make_parsestate(NULL);
105 	Query	   *query;
106 
107 	Assert(sourceText != NULL); /* required as of 8.4 */
108 
109 	pstate->p_sourcetext = sourceText;
110 
111 	if (numParams > 0)
112 		parse_fixed_parameters(pstate, paramTypes, numParams);
113 
114 	pstate->p_queryEnv = queryEnv;
115 
116 	query = transformTopLevelStmt(pstate, parseTree);
117 
118 	if (post_parse_analyze_hook)
119 		(*post_parse_analyze_hook) (pstate, query);
120 
121 	free_parsestate(pstate);
122 
123 	return query;
124 }
125 
126 /*
127  * parse_analyze_varparams
128  *
129  * This variant is used when it's okay to deduce information about $n
130  * symbol datatypes from context.  The passed-in paramTypes[] array can
131  * be modified or enlarged (via repalloc).
132  */
133 Query *
parse_analyze_varparams(RawStmt * parseTree,const char * sourceText,Oid ** paramTypes,int * numParams)134 parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
135 						Oid **paramTypes, int *numParams)
136 {
137 	ParseState *pstate = make_parsestate(NULL);
138 	Query	   *query;
139 
140 	Assert(sourceText != NULL); /* required as of 8.4 */
141 
142 	pstate->p_sourcetext = sourceText;
143 
144 	parse_variable_parameters(pstate, paramTypes, numParams);
145 
146 	query = transformTopLevelStmt(pstate, parseTree);
147 
148 	/* make sure all is well with parameter types */
149 	check_variable_parameters(pstate, query);
150 
151 	if (post_parse_analyze_hook)
152 		(*post_parse_analyze_hook) (pstate, query);
153 
154 	free_parsestate(pstate);
155 
156 	return query;
157 }
158 
159 /*
160  * parse_sub_analyze
161  *		Entry point for recursively analyzing a sub-statement.
162  */
163 Query *
parse_sub_analyze(Node * parseTree,ParseState * parentParseState,CommonTableExpr * parentCTE,bool locked_from_parent,bool resolve_unknowns)164 parse_sub_analyze(Node *parseTree, ParseState *parentParseState,
165 				  CommonTableExpr *parentCTE,
166 				  bool locked_from_parent,
167 				  bool resolve_unknowns)
168 {
169 	ParseState *pstate = make_parsestate(parentParseState);
170 	Query	   *query;
171 
172 	pstate->p_parent_cte = parentCTE;
173 	pstate->p_locked_from_parent = locked_from_parent;
174 	pstate->p_resolve_unknowns = resolve_unknowns;
175 
176 	query = transformStmt(pstate, parseTree);
177 
178 	free_parsestate(pstate);
179 
180 	return query;
181 }
182 
183 /*
184  * transformTopLevelStmt -
185  *	  transform a Parse tree into a Query tree.
186  *
187  * This function is just responsible for transferring statement location data
188  * from the RawStmt into the finished Query.
189  */
190 Query *
transformTopLevelStmt(ParseState * pstate,RawStmt * parseTree)191 transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
192 {
193 	Query	   *result;
194 
195 	/* We're at top level, so allow SELECT INTO */
196 	result = transformOptionalSelectInto(pstate, parseTree->stmt);
197 
198 	result->stmt_location = parseTree->stmt_location;
199 	result->stmt_len = parseTree->stmt_len;
200 
201 	return result;
202 }
203 
204 /*
205  * transformOptionalSelectInto -
206  *	  If SELECT has INTO, convert it to CREATE TABLE AS.
207  *
208  * The only thing we do here that we don't do in transformStmt() is to
209  * convert SELECT ... INTO into CREATE TABLE AS.  Since utility statements
210  * aren't allowed within larger statements, this is only allowed at the top
211  * of the parse tree, and so we only try it before entering the recursive
212  * transformStmt() processing.
213  */
214 static Query *
transformOptionalSelectInto(ParseState * pstate,Node * parseTree)215 transformOptionalSelectInto(ParseState *pstate, Node *parseTree)
216 {
217 	if (IsA(parseTree, SelectStmt))
218 	{
219 		SelectStmt *stmt = (SelectStmt *) parseTree;
220 
221 		/* If it's a set-operation tree, drill down to leftmost SelectStmt */
222 		while (stmt && stmt->op != SETOP_NONE)
223 			stmt = stmt->larg;
224 		Assert(stmt && IsA(stmt, SelectStmt) &&stmt->larg == NULL);
225 
226 		if (stmt->intoClause)
227 		{
228 			CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
229 
230 			ctas->query = parseTree;
231 			ctas->into = stmt->intoClause;
232 			ctas->relkind = OBJECT_TABLE;
233 			ctas->is_select_into = true;
234 
235 			/*
236 			 * Remove the intoClause from the SelectStmt.  This makes it safe
237 			 * for transformSelectStmt to complain if it finds intoClause set
238 			 * (implying that the INTO appeared in a disallowed place).
239 			 */
240 			stmt->intoClause = NULL;
241 
242 			parseTree = (Node *) ctas;
243 		}
244 	}
245 
246 	return transformStmt(pstate, parseTree);
247 }
248 
249 /*
250  * transformStmt -
251  *	  recursively transform a Parse tree into a Query tree.
252  */
253 Query *
transformStmt(ParseState * pstate,Node * parseTree)254 transformStmt(ParseState *pstate, Node *parseTree)
255 {
256 	Query	   *result;
257 
258 	/*
259 	 * We apply RAW_EXPRESSION_COVERAGE_TEST testing to basic DML statements;
260 	 * we can't just run it on everything because raw_expression_tree_walker()
261 	 * doesn't claim to handle utility statements.
262 	 */
263 #ifdef RAW_EXPRESSION_COVERAGE_TEST
264 	switch (nodeTag(parseTree))
265 	{
266 		case T_SelectStmt:
267 		case T_InsertStmt:
268 		case T_UpdateStmt:
269 		case T_DeleteStmt:
270 			(void) test_raw_expression_coverage(parseTree, NULL);
271 			break;
272 		default:
273 			break;
274 	}
275 #endif							/* RAW_EXPRESSION_COVERAGE_TEST */
276 
277 	switch (nodeTag(parseTree))
278 	{
279 			/*
280 			 * Optimizable statements
281 			 */
282 		case T_InsertStmt:
283 			result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
284 			break;
285 
286 		case T_DeleteStmt:
287 			result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
288 			break;
289 
290 		case T_UpdateStmt:
291 			result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
292 			break;
293 
294 		case T_SelectStmt:
295 			{
296 				SelectStmt *n = (SelectStmt *) parseTree;
297 
298 				if (n->valuesLists)
299 					result = transformValuesClause(pstate, n);
300 				else if (n->op == SETOP_NONE)
301 					result = transformSelectStmt(pstate, n);
302 				else
303 					result = transformSetOperationStmt(pstate, n);
304 			}
305 			break;
306 
307 			/*
308 			 * Special cases
309 			 */
310 		case T_DeclareCursorStmt:
311 			result = transformDeclareCursorStmt(pstate,
312 												(DeclareCursorStmt *) parseTree);
313 			break;
314 
315 		case T_ExplainStmt:
316 			result = transformExplainStmt(pstate,
317 										  (ExplainStmt *) parseTree);
318 			break;
319 
320 		case T_CreateTableAsStmt:
321 			result = transformCreateTableAsStmt(pstate,
322 												(CreateTableAsStmt *) parseTree);
323 			break;
324 
325 		case T_CallStmt:
326 			result = transformCallStmt(pstate,
327 									   (CallStmt *) parseTree);
328 			break;
329 
330 		default:
331 
332 			/*
333 			 * other statements don't require any transformation; just return
334 			 * the original parsetree with a Query node plastered on top.
335 			 */
336 			result = makeNode(Query);
337 			result->commandType = CMD_UTILITY;
338 			result->utilityStmt = (Node *) parseTree;
339 			break;
340 	}
341 
342 	/* Mark as original query until we learn differently */
343 	result->querySource = QSRC_ORIGINAL;
344 	result->canSetTag = true;
345 
346 	return result;
347 }
348 
349 /*
350  * analyze_requires_snapshot
351  *		Returns true if a snapshot must be set before doing parse analysis
352  *		on the given raw parse tree.
353  *
354  * Classification here should match transformStmt().
355  */
356 bool
analyze_requires_snapshot(RawStmt * parseTree)357 analyze_requires_snapshot(RawStmt *parseTree)
358 {
359 	bool		result;
360 
361 	switch (nodeTag(parseTree->stmt))
362 	{
363 			/*
364 			 * Optimizable statements
365 			 */
366 		case T_InsertStmt:
367 		case T_DeleteStmt:
368 		case T_UpdateStmt:
369 		case T_SelectStmt:
370 			result = true;
371 			break;
372 
373 			/*
374 			 * Special cases
375 			 */
376 		case T_DeclareCursorStmt:
377 		case T_ExplainStmt:
378 		case T_CreateTableAsStmt:
379 			/* yes, because we must analyze the contained statement */
380 			result = true;
381 			break;
382 
383 		default:
384 			/* other utility statements don't have any real parse analysis */
385 			result = false;
386 			break;
387 	}
388 
389 	return result;
390 }
391 
392 /*
393  * transformDeleteStmt -
394  *	  transforms a Delete Statement
395  */
396 static Query *
transformDeleteStmt(ParseState * pstate,DeleteStmt * stmt)397 transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
398 {
399 	Query	   *qry = makeNode(Query);
400 	ParseNamespaceItem *nsitem;
401 	Node	   *qual;
402 
403 	qry->commandType = CMD_DELETE;
404 
405 	/* process the WITH clause independently of all else */
406 	if (stmt->withClause)
407 	{
408 		qry->hasRecursive = stmt->withClause->recursive;
409 		qry->cteList = transformWithClause(pstate, stmt->withClause);
410 		qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
411 	}
412 
413 	/* set up range table with just the result rel */
414 	qry->resultRelation = setTargetTable(pstate, stmt->relation,
415 										 stmt->relation->inh,
416 										 true,
417 										 ACL_DELETE);
418 
419 	/* grab the namespace item made by setTargetTable */
420 	nsitem = (ParseNamespaceItem *) llast(pstate->p_namespace);
421 
422 	/* there's no DISTINCT in DELETE */
423 	qry->distinctClause = NIL;
424 
425 	/* subqueries in USING cannot access the result relation */
426 	nsitem->p_lateral_only = true;
427 	nsitem->p_lateral_ok = false;
428 
429 	/*
430 	 * The USING clause is non-standard SQL syntax, and is equivalent in
431 	 * functionality to the FROM list that can be specified for UPDATE. The
432 	 * USING keyword is used rather than FROM because FROM is already a
433 	 * keyword in the DELETE syntax.
434 	 */
435 	transformFromClause(pstate, stmt->usingClause);
436 
437 	/* remaining clauses can reference the result relation normally */
438 	nsitem->p_lateral_only = false;
439 	nsitem->p_lateral_ok = true;
440 
441 	qual = transformWhereClause(pstate, stmt->whereClause,
442 								EXPR_KIND_WHERE, "WHERE");
443 
444 	qry->returningList = transformReturningList(pstate, stmt->returningList);
445 
446 	/* done building the range table and jointree */
447 	qry->rtable = pstate->p_rtable;
448 	qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
449 
450 	qry->hasSubLinks = pstate->p_hasSubLinks;
451 	qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
452 	qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
453 	qry->hasAggs = pstate->p_hasAggs;
454 
455 	assign_query_collations(pstate, qry);
456 
457 	/* this must be done after collations, for reliable comparison of exprs */
458 	if (pstate->p_hasAggs)
459 		parseCheckAggregates(pstate, qry);
460 
461 	return qry;
462 }
463 
464 /*
465  * transformInsertStmt -
466  *	  transform an Insert Statement
467  */
468 static Query *
transformInsertStmt(ParseState * pstate,InsertStmt * stmt)469 transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
470 {
471 	Query	   *qry = makeNode(Query);
472 	SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
473 	List	   *exprList = NIL;
474 	bool		isGeneralSelect;
475 	List	   *sub_rtable;
476 	List	   *sub_namespace;
477 	List	   *icolumns;
478 	List	   *attrnos;
479 	RangeTblEntry *rte;
480 	RangeTblRef *rtr;
481 	ListCell   *icols;
482 	ListCell   *attnos;
483 	ListCell   *lc;
484 	bool		isOnConflictUpdate;
485 	AclMode		targetPerms;
486 
487 	/* There can't be any outer WITH to worry about */
488 	Assert(pstate->p_ctenamespace == NIL);
489 
490 	qry->commandType = CMD_INSERT;
491 	pstate->p_is_insert = true;
492 
493 	/* process the WITH clause independently of all else */
494 	if (stmt->withClause)
495 	{
496 		qry->hasRecursive = stmt->withClause->recursive;
497 		qry->cteList = transformWithClause(pstate, stmt->withClause);
498 		qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
499 	}
500 
501 	qry->override = stmt->override;
502 
503 	isOnConflictUpdate = (stmt->onConflictClause &&
504 						  stmt->onConflictClause->action == ONCONFLICT_UPDATE);
505 
506 	/*
507 	 * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
508 	 * VALUES list, or general SELECT input.  We special-case VALUES, both for
509 	 * efficiency and so we can handle DEFAULT specifications.
510 	 *
511 	 * The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
512 	 * VALUES clause.  If we have any of those, treat it as a general SELECT;
513 	 * so it will work, but you can't use DEFAULT items together with those.
514 	 */
515 	isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
516 									  selectStmt->sortClause != NIL ||
517 									  selectStmt->limitOffset != NULL ||
518 									  selectStmt->limitCount != NULL ||
519 									  selectStmt->lockingClause != NIL ||
520 									  selectStmt->withClause != NULL));
521 
522 	/*
523 	 * If a non-nil rangetable/namespace was passed in, and we are doing
524 	 * INSERT/SELECT, arrange to pass the rangetable/namespace down to the
525 	 * SELECT.  This can only happen if we are inside a CREATE RULE, and in
526 	 * that case we want the rule's OLD and NEW rtable entries to appear as
527 	 * part of the SELECT's rtable, not as outer references for it.  (Kluge!)
528 	 * The SELECT's joinlist is not affected however.  We must do this before
529 	 * adding the target table to the INSERT's rtable.
530 	 */
531 	if (isGeneralSelect)
532 	{
533 		sub_rtable = pstate->p_rtable;
534 		pstate->p_rtable = NIL;
535 		sub_namespace = pstate->p_namespace;
536 		pstate->p_namespace = NIL;
537 	}
538 	else
539 	{
540 		sub_rtable = NIL;		/* not used, but keep compiler quiet */
541 		sub_namespace = NIL;
542 	}
543 
544 	/*
545 	 * Must get write lock on INSERT target table before scanning SELECT, else
546 	 * we will grab the wrong kind of initial lock if the target table is also
547 	 * mentioned in the SELECT part.  Note that the target table is not added
548 	 * to the joinlist or namespace.
549 	 */
550 	targetPerms = ACL_INSERT;
551 	if (isOnConflictUpdate)
552 		targetPerms |= ACL_UPDATE;
553 	qry->resultRelation = setTargetTable(pstate, stmt->relation,
554 										 false, false, targetPerms);
555 
556 	/* Validate stmt->cols list, or build default list if no list given */
557 	icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
558 	Assert(list_length(icolumns) == list_length(attrnos));
559 
560 	/*
561 	 * Determine which variant of INSERT we have.
562 	 */
563 	if (selectStmt == NULL)
564 	{
565 		/*
566 		 * We have INSERT ... DEFAULT VALUES.  We can handle this case by
567 		 * emitting an empty targetlist --- all columns will be defaulted when
568 		 * the planner expands the targetlist.
569 		 */
570 		exprList = NIL;
571 	}
572 	else if (isGeneralSelect)
573 	{
574 		/*
575 		 * We make the sub-pstate a child of the outer pstate so that it can
576 		 * see any Param definitions supplied from above.  Since the outer
577 		 * pstate's rtable and namespace are presently empty, there are no
578 		 * side-effects of exposing names the sub-SELECT shouldn't be able to
579 		 * see.
580 		 */
581 		ParseState *sub_pstate = make_parsestate(pstate);
582 		Query	   *selectQuery;
583 
584 		/*
585 		 * Process the source SELECT.
586 		 *
587 		 * It is important that this be handled just like a standalone SELECT;
588 		 * otherwise the behavior of SELECT within INSERT might be different
589 		 * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
590 		 * bugs of just that nature...)
591 		 *
592 		 * The sole exception is that we prevent resolving unknown-type
593 		 * outputs as TEXT.  This does not change the semantics since if the
594 		 * column type matters semantically, it would have been resolved to
595 		 * something else anyway.  Doing this lets us resolve such outputs as
596 		 * the target column's type, which we handle below.
597 		 */
598 		sub_pstate->p_rtable = sub_rtable;
599 		sub_pstate->p_joinexprs = NIL;	/* sub_rtable has no joins */
600 		sub_pstate->p_namespace = sub_namespace;
601 		sub_pstate->p_resolve_unknowns = false;
602 
603 		selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
604 
605 		free_parsestate(sub_pstate);
606 
607 		/* The grammar should have produced a SELECT */
608 		if (!IsA(selectQuery, Query) ||
609 			selectQuery->commandType != CMD_SELECT)
610 			elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
611 
612 		/*
613 		 * Make the source be a subquery in the INSERT's rangetable, and add
614 		 * it to the INSERT's joinlist.
615 		 */
616 		rte = addRangeTableEntryForSubquery(pstate,
617 											selectQuery,
618 											makeAlias("*SELECT*", NIL),
619 											false,
620 											false);
621 		rtr = makeNode(RangeTblRef);
622 		/* assume new rte is at end */
623 		rtr->rtindex = list_length(pstate->p_rtable);
624 		Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
625 		pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
626 
627 		/*----------
628 		 * Generate an expression list for the INSERT that selects all the
629 		 * non-resjunk columns from the subquery.  (INSERT's tlist must be
630 		 * separate from the subquery's tlist because we may add columns,
631 		 * insert datatype coercions, etc.)
632 		 *
633 		 * HACK: unknown-type constants and params in the SELECT's targetlist
634 		 * are copied up as-is rather than being referenced as subquery
635 		 * outputs.  This is to ensure that when we try to coerce them to
636 		 * the target column's datatype, the right things happen (see
637 		 * special cases in coerce_type).  Otherwise, this fails:
638 		 *		INSERT INTO foo SELECT 'bar', ... FROM baz
639 		 *----------
640 		 */
641 		exprList = NIL;
642 		foreach(lc, selectQuery->targetList)
643 		{
644 			TargetEntry *tle = (TargetEntry *) lfirst(lc);
645 			Expr	   *expr;
646 
647 			if (tle->resjunk)
648 				continue;
649 			if (tle->expr &&
650 				(IsA(tle->expr, Const) ||IsA(tle->expr, Param)) &&
651 				exprType((Node *) tle->expr) == UNKNOWNOID)
652 				expr = tle->expr;
653 			else
654 			{
655 				Var		   *var = makeVarFromTargetEntry(rtr->rtindex, tle);
656 
657 				var->location = exprLocation((Node *) tle->expr);
658 				expr = (Expr *) var;
659 			}
660 			exprList = lappend(exprList, expr);
661 		}
662 
663 		/* Prepare row for assignment to target table */
664 		exprList = transformInsertRow(pstate, exprList,
665 									  stmt->cols,
666 									  icolumns, attrnos,
667 									  false);
668 	}
669 	else if (list_length(selectStmt->valuesLists) > 1)
670 	{
671 		/*
672 		 * Process INSERT ... VALUES with multiple VALUES sublists. We
673 		 * generate a VALUES RTE holding the transformed expression lists, and
674 		 * build up a targetlist containing Vars that reference the VALUES
675 		 * RTE.
676 		 */
677 		List	   *exprsLists = NIL;
678 		List	   *coltypes = NIL;
679 		List	   *coltypmods = NIL;
680 		List	   *colcollations = NIL;
681 		int			sublist_length = -1;
682 		bool		lateral = false;
683 
684 		Assert(selectStmt->intoClause == NULL);
685 
686 		foreach(lc, selectStmt->valuesLists)
687 		{
688 			List	   *sublist = (List *) lfirst(lc);
689 
690 			/*
691 			 * Do basic expression transformation (same as a ROW() expr, but
692 			 * allow SetToDefault at top level)
693 			 */
694 			sublist = transformExpressionList(pstate, sublist,
695 											  EXPR_KIND_VALUES, true);
696 
697 			/*
698 			 * All the sublists must be the same length, *after*
699 			 * transformation (which might expand '*' into multiple items).
700 			 * The VALUES RTE can't handle anything different.
701 			 */
702 			if (sublist_length < 0)
703 			{
704 				/* Remember post-transformation length of first sublist */
705 				sublist_length = list_length(sublist);
706 			}
707 			else if (sublist_length != list_length(sublist))
708 			{
709 				ereport(ERROR,
710 						(errcode(ERRCODE_SYNTAX_ERROR),
711 						 errmsg("VALUES lists must all be the same length"),
712 						 parser_errposition(pstate,
713 											exprLocation((Node *) sublist))));
714 			}
715 
716 			/*
717 			 * Prepare row for assignment to target table.  We process any
718 			 * indirection on the target column specs normally but then strip
719 			 * off the resulting field/array assignment nodes, since we don't
720 			 * want the parsed statement to contain copies of those in each
721 			 * VALUES row.  (It's annoying to have to transform the
722 			 * indirection specs over and over like this, but avoiding it
723 			 * would take some really messy refactoring of
724 			 * transformAssignmentIndirection.)
725 			 */
726 			sublist = transformInsertRow(pstate, sublist,
727 										 stmt->cols,
728 										 icolumns, attrnos,
729 										 true);
730 
731 			/*
732 			 * We must assign collations now because assign_query_collations
733 			 * doesn't process rangetable entries.  We just assign all the
734 			 * collations independently in each row, and don't worry about
735 			 * whether they are consistent vertically.  The outer INSERT query
736 			 * isn't going to care about the collations of the VALUES columns,
737 			 * so it's not worth the effort to identify a common collation for
738 			 * each one here.  (But note this does have one user-visible
739 			 * consequence: INSERT ... VALUES won't complain about conflicting
740 			 * explicit COLLATEs in a column, whereas the same VALUES
741 			 * construct in another context would complain.)
742 			 */
743 			assign_list_collations(pstate, sublist);
744 
745 			exprsLists = lappend(exprsLists, sublist);
746 		}
747 
748 		/*
749 		 * Construct column type/typmod/collation lists for the VALUES RTE.
750 		 * Every expression in each column has been coerced to the type/typmod
751 		 * of the corresponding target column or subfield, so it's sufficient
752 		 * to look at the exprType/exprTypmod of the first row.  We don't care
753 		 * about the collation labeling, so just fill in InvalidOid for that.
754 		 */
755 		foreach(lc, (List *) linitial(exprsLists))
756 		{
757 			Node	   *val = (Node *) lfirst(lc);
758 
759 			coltypes = lappend_oid(coltypes, exprType(val));
760 			coltypmods = lappend_int(coltypmods, exprTypmod(val));
761 			colcollations = lappend_oid(colcollations, InvalidOid);
762 		}
763 
764 		/*
765 		 * Ordinarily there can't be any current-level Vars in the expression
766 		 * lists, because the namespace was empty ... but if we're inside
767 		 * CREATE RULE, then NEW/OLD references might appear.  In that case we
768 		 * have to mark the VALUES RTE as LATERAL.
769 		 */
770 		if (list_length(pstate->p_rtable) != 1 &&
771 			contain_vars_of_level((Node *) exprsLists, 0))
772 			lateral = true;
773 
774 		/*
775 		 * Generate the VALUES RTE
776 		 */
777 		rte = addRangeTableEntryForValues(pstate, exprsLists,
778 										  coltypes, coltypmods, colcollations,
779 										  NULL, lateral, true);
780 		rtr = makeNode(RangeTblRef);
781 		/* assume new rte is at end */
782 		rtr->rtindex = list_length(pstate->p_rtable);
783 		Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
784 		pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
785 
786 		/*
787 		 * Generate list of Vars referencing the RTE
788 		 */
789 		expandRTE(rte, rtr->rtindex, 0, -1, false, NULL, &exprList);
790 
791 		/*
792 		 * Re-apply any indirection on the target column specs to the Vars
793 		 */
794 		exprList = transformInsertRow(pstate, exprList,
795 									  stmt->cols,
796 									  icolumns, attrnos,
797 									  false);
798 	}
799 	else
800 	{
801 		/*
802 		 * Process INSERT ... VALUES with a single VALUES sublist.  We treat
803 		 * this case separately for efficiency.  The sublist is just computed
804 		 * directly as the Query's targetlist, with no VALUES RTE.  So it
805 		 * works just like a SELECT without any FROM.
806 		 */
807 		List	   *valuesLists = selectStmt->valuesLists;
808 
809 		Assert(list_length(valuesLists) == 1);
810 		Assert(selectStmt->intoClause == NULL);
811 
812 		/*
813 		 * Do basic expression transformation (same as a ROW() expr, but allow
814 		 * SetToDefault at top level)
815 		 */
816 		exprList = transformExpressionList(pstate,
817 										   (List *) linitial(valuesLists),
818 										   EXPR_KIND_VALUES_SINGLE,
819 										   true);
820 
821 		/* Prepare row for assignment to target table */
822 		exprList = transformInsertRow(pstate, exprList,
823 									  stmt->cols,
824 									  icolumns, attrnos,
825 									  false);
826 	}
827 
828 	/*
829 	 * Generate query's target list using the computed list of expressions.
830 	 * Also, mark all the target columns as needing insert permissions.
831 	 */
832 	rte = pstate->p_target_rangetblentry;
833 	qry->targetList = NIL;
834 	icols = list_head(icolumns);
835 	attnos = list_head(attrnos);
836 	foreach(lc, exprList)
837 	{
838 		Expr	   *expr = (Expr *) lfirst(lc);
839 		ResTarget  *col;
840 		AttrNumber	attr_num;
841 		TargetEntry *tle;
842 
843 		col = lfirst_node(ResTarget, icols);
844 		attr_num = (AttrNumber) lfirst_int(attnos);
845 
846 		tle = makeTargetEntry(expr,
847 							  attr_num,
848 							  col->name,
849 							  false);
850 		qry->targetList = lappend(qry->targetList, tle);
851 
852 		rte->insertedCols = bms_add_member(rte->insertedCols,
853 										   attr_num - FirstLowInvalidHeapAttributeNumber);
854 
855 		icols = lnext(icols);
856 		attnos = lnext(attnos);
857 	}
858 
859 	/* Process ON CONFLICT, if any. */
860 	if (stmt->onConflictClause)
861 		qry->onConflict = transformOnConflictClause(pstate,
862 													stmt->onConflictClause);
863 
864 	/*
865 	 * If we have a RETURNING clause, we need to add the target relation to
866 	 * the query namespace before processing it, so that Var references in
867 	 * RETURNING will work.  Also, remove any namespace entries added in a
868 	 * sub-SELECT or VALUES list.
869 	 */
870 	if (stmt->returningList)
871 	{
872 		pstate->p_namespace = NIL;
873 		addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
874 					  false, true, true);
875 		qry->returningList = transformReturningList(pstate,
876 													stmt->returningList);
877 	}
878 
879 	/* done building the range table and jointree */
880 	qry->rtable = pstate->p_rtable;
881 	qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
882 
883 	qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
884 	qry->hasSubLinks = pstate->p_hasSubLinks;
885 
886 	assign_query_collations(pstate, qry);
887 
888 	return qry;
889 }
890 
891 /*
892  * Prepare an INSERT row for assignment to the target table.
893  *
894  * exprlist: transformed expressions for source values; these might come from
895  * a VALUES row, or be Vars referencing a sub-SELECT or VALUES RTE output.
896  * stmtcols: original target-columns spec for INSERT (we just test for NIL)
897  * icolumns: effective target-columns spec (list of ResTarget)
898  * attrnos: integer column numbers (must be same length as icolumns)
899  * strip_indirection: if true, remove any field/array assignment nodes
900  */
901 static List *
transformInsertRow(ParseState * pstate,List * exprlist,List * stmtcols,List * icolumns,List * attrnos,bool strip_indirection)902 transformInsertRow(ParseState *pstate, List *exprlist,
903 				   List *stmtcols, List *icolumns, List *attrnos,
904 				   bool strip_indirection)
905 {
906 	List	   *result;
907 	ListCell   *lc;
908 	ListCell   *icols;
909 	ListCell   *attnos;
910 
911 	/*
912 	 * Check length of expr list.  It must not have more expressions than
913 	 * there are target columns.  We allow fewer, but only if no explicit
914 	 * columns list was given (the remaining columns are implicitly
915 	 * defaulted).  Note we must check this *after* transformation because
916 	 * that could expand '*' into multiple items.
917 	 */
918 	if (list_length(exprlist) > list_length(icolumns))
919 		ereport(ERROR,
920 				(errcode(ERRCODE_SYNTAX_ERROR),
921 				 errmsg("INSERT has more expressions than target columns"),
922 				 parser_errposition(pstate,
923 									exprLocation(list_nth(exprlist,
924 														  list_length(icolumns))))));
925 	if (stmtcols != NIL &&
926 		list_length(exprlist) < list_length(icolumns))
927 	{
928 		/*
929 		 * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
930 		 * where the user accidentally created a RowExpr instead of separate
931 		 * columns.  Add a suitable hint if that seems to be the problem,
932 		 * because the main error message is quite misleading for this case.
933 		 * (If there's no stmtcols, you'll get something about data type
934 		 * mismatch, which is less misleading so we don't worry about giving a
935 		 * hint in that case.)
936 		 */
937 		ereport(ERROR,
938 				(errcode(ERRCODE_SYNTAX_ERROR),
939 				 errmsg("INSERT has more target columns than expressions"),
940 				 ((list_length(exprlist) == 1 &&
941 				   count_rowexpr_columns(pstate, linitial(exprlist)) ==
942 				   list_length(icolumns)) ?
943 				  errhint("The insertion source is a row expression containing the same number of columns expected by the INSERT. Did you accidentally use extra parentheses?") : 0),
944 				 parser_errposition(pstate,
945 									exprLocation(list_nth(icolumns,
946 														  list_length(exprlist))))));
947 	}
948 
949 	/*
950 	 * Prepare columns for assignment to target table.
951 	 */
952 	result = NIL;
953 	icols = list_head(icolumns);
954 	attnos = list_head(attrnos);
955 	foreach(lc, exprlist)
956 	{
957 		Expr	   *expr = (Expr *) lfirst(lc);
958 		ResTarget  *col;
959 
960 		col = lfirst_node(ResTarget, icols);
961 
962 		expr = transformAssignedExpr(pstate, expr,
963 									 EXPR_KIND_INSERT_TARGET,
964 									 col->name,
965 									 lfirst_int(attnos),
966 									 col->indirection,
967 									 col->location);
968 
969 		if (strip_indirection)
970 		{
971 			while (expr)
972 			{
973 				if (IsA(expr, FieldStore))
974 				{
975 					FieldStore *fstore = (FieldStore *) expr;
976 
977 					expr = (Expr *) linitial(fstore->newvals);
978 				}
979 				else if (IsA(expr, ArrayRef))
980 				{
981 					ArrayRef   *aref = (ArrayRef *) expr;
982 
983 					if (aref->refassgnexpr == NULL)
984 						break;
985 					expr = aref->refassgnexpr;
986 				}
987 				else
988 					break;
989 			}
990 		}
991 
992 		result = lappend(result, expr);
993 
994 		icols = lnext(icols);
995 		attnos = lnext(attnos);
996 	}
997 
998 	return result;
999 }
1000 
1001 /*
1002  * transformOnConflictClause -
1003  *	  transforms an OnConflictClause in an INSERT
1004  */
1005 static OnConflictExpr *
transformOnConflictClause(ParseState * pstate,OnConflictClause * onConflictClause)1006 transformOnConflictClause(ParseState *pstate,
1007 						  OnConflictClause *onConflictClause)
1008 {
1009 	List	   *arbiterElems;
1010 	Node	   *arbiterWhere;
1011 	Oid			arbiterConstraint;
1012 	List	   *onConflictSet = NIL;
1013 	Node	   *onConflictWhere = NULL;
1014 	RangeTblEntry *exclRte = NULL;
1015 	int			exclRelIndex = 0;
1016 	List	   *exclRelTlist = NIL;
1017 	OnConflictExpr *result;
1018 
1019 	/* Process the arbiter clause, ON CONFLICT ON (...) */
1020 	transformOnConflictArbiter(pstate, onConflictClause, &arbiterElems,
1021 							   &arbiterWhere, &arbiterConstraint);
1022 
1023 	/* Process DO UPDATE */
1024 	if (onConflictClause->action == ONCONFLICT_UPDATE)
1025 	{
1026 		Relation	targetrel = pstate->p_target_relation;
1027 
1028 		/*
1029 		 * All INSERT expressions have been parsed, get ready for potentially
1030 		 * existing SET statements that need to be processed like an UPDATE.
1031 		 */
1032 		pstate->p_is_insert = false;
1033 
1034 		/*
1035 		 * Add range table entry for the EXCLUDED pseudo relation.  relkind is
1036 		 * set to composite to signal that we're not dealing with an actual
1037 		 * relation, and no permission checks are required on it.  (We'll
1038 		 * check the actual target relation, instead.)
1039 		 */
1040 		exclRte = addRangeTableEntryForRelation(pstate,
1041 												targetrel,
1042 												makeAlias("excluded", NIL),
1043 												false, false);
1044 		exclRte->relkind = RELKIND_COMPOSITE_TYPE;
1045 		exclRte->requiredPerms = 0;
1046 		/* other permissions fields in exclRte are already empty */
1047 
1048 		exclRelIndex = list_length(pstate->p_rtable);
1049 
1050 		/* Create EXCLUDED rel's targetlist for use by EXPLAIN */
1051 		exclRelTlist = BuildOnConflictExcludedTargetlist(targetrel,
1052 														 exclRelIndex);
1053 
1054 		/*
1055 		 * Add EXCLUDED and the target RTE to the namespace, so that they can
1056 		 * be used in the UPDATE subexpressions.
1057 		 */
1058 		addRTEtoQuery(pstate, exclRte, false, true, true);
1059 		addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
1060 					  false, true, true);
1061 
1062 		/*
1063 		 * Now transform the UPDATE subexpressions.
1064 		 */
1065 		onConflictSet =
1066 			transformUpdateTargetList(pstate, onConflictClause->targetList);
1067 
1068 		onConflictWhere = transformWhereClause(pstate,
1069 											   onConflictClause->whereClause,
1070 											   EXPR_KIND_WHERE, "WHERE");
1071 	}
1072 
1073 	/* Finally, build ON CONFLICT DO [NOTHING | UPDATE] expression */
1074 	result = makeNode(OnConflictExpr);
1075 
1076 	result->action = onConflictClause->action;
1077 	result->arbiterElems = arbiterElems;
1078 	result->arbiterWhere = arbiterWhere;
1079 	result->constraint = arbiterConstraint;
1080 	result->onConflictSet = onConflictSet;
1081 	result->onConflictWhere = onConflictWhere;
1082 	result->exclRelIndex = exclRelIndex;
1083 	result->exclRelTlist = exclRelTlist;
1084 
1085 	return result;
1086 }
1087 
1088 
1089 /*
1090  * BuildOnConflictExcludedTargetlist
1091  *		Create target list for the EXCLUDED pseudo-relation of ON CONFLICT,
1092  *		representing the columns of targetrel with varno exclRelIndex.
1093  *
1094  * Note: Exported for use in the rewriter.
1095  */
1096 List *
BuildOnConflictExcludedTargetlist(Relation targetrel,Index exclRelIndex)1097 BuildOnConflictExcludedTargetlist(Relation targetrel,
1098 								  Index exclRelIndex)
1099 {
1100 	List	   *result = NIL;
1101 	int			attno;
1102 	Var		   *var;
1103 	TargetEntry *te;
1104 
1105 	/*
1106 	 * Note that resnos of the tlist must correspond to attnos of the
1107 	 * underlying relation, hence we need entries for dropped columns too.
1108 	 */
1109 	for (attno = 0; attno < RelationGetNumberOfAttributes(targetrel); attno++)
1110 	{
1111 		Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, attno);
1112 		char	   *name;
1113 
1114 		if (attr->attisdropped)
1115 		{
1116 			/*
1117 			 * can't use atttypid here, but it doesn't really matter what type
1118 			 * the Const claims to be.
1119 			 */
1120 			var = (Var *) makeNullConst(INT4OID, -1, InvalidOid);
1121 			name = "";
1122 		}
1123 		else
1124 		{
1125 			var = makeVar(exclRelIndex, attno + 1,
1126 						  attr->atttypid, attr->atttypmod,
1127 						  attr->attcollation,
1128 						  0);
1129 			name = pstrdup(NameStr(attr->attname));
1130 		}
1131 
1132 		te = makeTargetEntry((Expr *) var,
1133 							 attno + 1,
1134 							 name,
1135 							 false);
1136 
1137 		result = lappend(result, te);
1138 	}
1139 
1140 	/*
1141 	 * Add a whole-row-Var entry to support references to "EXCLUDED.*".  Like
1142 	 * the other entries in the EXCLUDED tlist, its resno must match the Var's
1143 	 * varattno, else the wrong things happen while resolving references in
1144 	 * setrefs.c.  This is against normal conventions for targetlists, but
1145 	 * it's okay since we don't use this as a real tlist.
1146 	 */
1147 	var = makeVar(exclRelIndex, InvalidAttrNumber,
1148 				  targetrel->rd_rel->reltype,
1149 				  -1, InvalidOid, 0);
1150 	te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
1151 	result = lappend(result, te);
1152 
1153 	return result;
1154 }
1155 
1156 
1157 /*
1158  * count_rowexpr_columns -
1159  *	  get number of columns contained in a ROW() expression;
1160  *	  return -1 if expression isn't a RowExpr or a Var referencing one.
1161  *
1162  * This is currently used only for hint purposes, so we aren't terribly
1163  * tense about recognizing all possible cases.  The Var case is interesting
1164  * because that's what we'll get in the INSERT ... SELECT (...) case.
1165  */
1166 static int
count_rowexpr_columns(ParseState * pstate,Node * expr)1167 count_rowexpr_columns(ParseState *pstate, Node *expr)
1168 {
1169 	if (expr == NULL)
1170 		return -1;
1171 	if (IsA(expr, RowExpr))
1172 		return list_length(((RowExpr *) expr)->args);
1173 	if (IsA(expr, Var))
1174 	{
1175 		Var		   *var = (Var *) expr;
1176 		AttrNumber	attnum = var->varattno;
1177 
1178 		if (attnum > 0 && var->vartype == RECORDOID)
1179 		{
1180 			RangeTblEntry *rte;
1181 
1182 			rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1183 			if (rte->rtekind == RTE_SUBQUERY)
1184 			{
1185 				/* Subselect-in-FROM: examine sub-select's output expr */
1186 				TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
1187 													attnum);
1188 
1189 				if (ste == NULL || ste->resjunk)
1190 					return -1;
1191 				expr = (Node *) ste->expr;
1192 				if (IsA(expr, RowExpr))
1193 					return list_length(((RowExpr *) expr)->args);
1194 			}
1195 		}
1196 	}
1197 	return -1;
1198 }
1199 
1200 
1201 /*
1202  * transformSelectStmt -
1203  *	  transforms a Select Statement
1204  *
1205  * Note: this covers only cases with no set operations and no VALUES lists;
1206  * see below for the other cases.
1207  */
1208 static Query *
transformSelectStmt(ParseState * pstate,SelectStmt * stmt)1209 transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
1210 {
1211 	Query	   *qry = makeNode(Query);
1212 	Node	   *qual;
1213 	ListCell   *l;
1214 
1215 	qry->commandType = CMD_SELECT;
1216 
1217 	/* process the WITH clause independently of all else */
1218 	if (stmt->withClause)
1219 	{
1220 		qry->hasRecursive = stmt->withClause->recursive;
1221 		qry->cteList = transformWithClause(pstate, stmt->withClause);
1222 		qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1223 	}
1224 
1225 	/* Complain if we get called from someplace where INTO is not allowed */
1226 	if (stmt->intoClause)
1227 		ereport(ERROR,
1228 				(errcode(ERRCODE_SYNTAX_ERROR),
1229 				 errmsg("SELECT ... INTO is not allowed here"),
1230 				 parser_errposition(pstate,
1231 									exprLocation((Node *) stmt->intoClause))));
1232 
1233 	/* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
1234 	pstate->p_locking_clause = stmt->lockingClause;
1235 
1236 	/* make WINDOW info available for window functions, too */
1237 	pstate->p_windowdefs = stmt->windowClause;
1238 
1239 	/* process the FROM clause */
1240 	transformFromClause(pstate, stmt->fromClause);
1241 
1242 	/* transform targetlist */
1243 	qry->targetList = transformTargetList(pstate, stmt->targetList,
1244 										  EXPR_KIND_SELECT_TARGET);
1245 
1246 	/* mark column origins */
1247 	markTargetListOrigins(pstate, qry->targetList);
1248 
1249 	/* transform WHERE */
1250 	qual = transformWhereClause(pstate, stmt->whereClause,
1251 								EXPR_KIND_WHERE, "WHERE");
1252 
1253 	/* initial processing of HAVING clause is much like WHERE clause */
1254 	qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
1255 										   EXPR_KIND_HAVING, "HAVING");
1256 
1257 	/*
1258 	 * Transform sorting/grouping stuff.  Do ORDER BY first because both
1259 	 * transformGroupClause and transformDistinctClause need the results. Note
1260 	 * that these functions can also change the targetList, so it's passed to
1261 	 * them by reference.
1262 	 */
1263 	qry->sortClause = transformSortClause(pstate,
1264 										  stmt->sortClause,
1265 										  &qry->targetList,
1266 										  EXPR_KIND_ORDER_BY,
1267 										  false /* allow SQL92 rules */ );
1268 
1269 	qry->groupClause = transformGroupClause(pstate,
1270 											stmt->groupClause,
1271 											&qry->groupingSets,
1272 											&qry->targetList,
1273 											qry->sortClause,
1274 											EXPR_KIND_GROUP_BY,
1275 											false /* allow SQL92 rules */ );
1276 
1277 	if (stmt->distinctClause == NIL)
1278 	{
1279 		qry->distinctClause = NIL;
1280 		qry->hasDistinctOn = false;
1281 	}
1282 	else if (linitial(stmt->distinctClause) == NULL)
1283 	{
1284 		/* We had SELECT DISTINCT */
1285 		qry->distinctClause = transformDistinctClause(pstate,
1286 													  &qry->targetList,
1287 													  qry->sortClause,
1288 													  false);
1289 		qry->hasDistinctOn = false;
1290 	}
1291 	else
1292 	{
1293 		/* We had SELECT DISTINCT ON */
1294 		qry->distinctClause = transformDistinctOnClause(pstate,
1295 														stmt->distinctClause,
1296 														&qry->targetList,
1297 														qry->sortClause);
1298 		qry->hasDistinctOn = true;
1299 	}
1300 
1301 	/* transform LIMIT */
1302 	qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1303 											EXPR_KIND_OFFSET, "OFFSET");
1304 	qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1305 										   EXPR_KIND_LIMIT, "LIMIT");
1306 
1307 	/* transform window clauses after we have seen all window functions */
1308 	qry->windowClause = transformWindowDefinitions(pstate,
1309 												   pstate->p_windowdefs,
1310 												   &qry->targetList);
1311 
1312 	/* resolve any still-unresolved output columns as being type text */
1313 	if (pstate->p_resolve_unknowns)
1314 		resolveTargetListUnknowns(pstate, qry->targetList);
1315 
1316 	qry->rtable = pstate->p_rtable;
1317 	qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1318 
1319 	qry->hasSubLinks = pstate->p_hasSubLinks;
1320 	qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1321 	qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1322 	qry->hasAggs = pstate->p_hasAggs;
1323 
1324 	foreach(l, stmt->lockingClause)
1325 	{
1326 		transformLockingClause(pstate, qry,
1327 							   (LockingClause *) lfirst(l), false);
1328 	}
1329 
1330 	assign_query_collations(pstate, qry);
1331 
1332 	/* this must be done after collations, for reliable comparison of exprs */
1333 	if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
1334 		parseCheckAggregates(pstate, qry);
1335 
1336 	return qry;
1337 }
1338 
1339 /*
1340  * transformValuesClause -
1341  *	  transforms a VALUES clause that's being used as a standalone SELECT
1342  *
1343  * We build a Query containing a VALUES RTE, rather as if one had written
1344  *			SELECT * FROM (VALUES ...) AS "*VALUES*"
1345  */
1346 static Query *
transformValuesClause(ParseState * pstate,SelectStmt * stmt)1347 transformValuesClause(ParseState *pstate, SelectStmt *stmt)
1348 {
1349 	Query	   *qry = makeNode(Query);
1350 	List	   *exprsLists;
1351 	List	   *coltypes = NIL;
1352 	List	   *coltypmods = NIL;
1353 	List	   *colcollations = NIL;
1354 	List	  **colexprs = NULL;
1355 	int			sublist_length = -1;
1356 	bool		lateral = false;
1357 	RangeTblEntry *rte;
1358 	int			rtindex;
1359 	ListCell   *lc;
1360 	ListCell   *lc2;
1361 	int			i;
1362 
1363 	qry->commandType = CMD_SELECT;
1364 
1365 	/* Most SELECT stuff doesn't apply in a VALUES clause */
1366 	Assert(stmt->distinctClause == NIL);
1367 	Assert(stmt->intoClause == NULL);
1368 	Assert(stmt->targetList == NIL);
1369 	Assert(stmt->fromClause == NIL);
1370 	Assert(stmt->whereClause == NULL);
1371 	Assert(stmt->groupClause == NIL);
1372 	Assert(stmt->havingClause == NULL);
1373 	Assert(stmt->windowClause == NIL);
1374 	Assert(stmt->op == SETOP_NONE);
1375 
1376 	/* process the WITH clause independently of all else */
1377 	if (stmt->withClause)
1378 	{
1379 		qry->hasRecursive = stmt->withClause->recursive;
1380 		qry->cteList = transformWithClause(pstate, stmt->withClause);
1381 		qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1382 	}
1383 
1384 	/*
1385 	 * For each row of VALUES, transform the raw expressions.
1386 	 *
1387 	 * Note that the intermediate representation we build is column-organized
1388 	 * not row-organized.  That simplifies the type and collation processing
1389 	 * below.
1390 	 */
1391 	foreach(lc, stmt->valuesLists)
1392 	{
1393 		List	   *sublist = (List *) lfirst(lc);
1394 
1395 		/*
1396 		 * Do basic expression transformation (same as a ROW() expr, but here
1397 		 * we disallow SetToDefault)
1398 		 */
1399 		sublist = transformExpressionList(pstate, sublist,
1400 										  EXPR_KIND_VALUES, false);
1401 
1402 		/*
1403 		 * All the sublists must be the same length, *after* transformation
1404 		 * (which might expand '*' into multiple items).  The VALUES RTE can't
1405 		 * handle anything different.
1406 		 */
1407 		if (sublist_length < 0)
1408 		{
1409 			/* Remember post-transformation length of first sublist */
1410 			sublist_length = list_length(sublist);
1411 			/* and allocate array for per-column lists */
1412 			colexprs = (List **) palloc0(sublist_length * sizeof(List *));
1413 		}
1414 		else if (sublist_length != list_length(sublist))
1415 		{
1416 			ereport(ERROR,
1417 					(errcode(ERRCODE_SYNTAX_ERROR),
1418 					 errmsg("VALUES lists must all be the same length"),
1419 					 parser_errposition(pstate,
1420 										exprLocation((Node *) sublist))));
1421 		}
1422 
1423 		/* Build per-column expression lists */
1424 		i = 0;
1425 		foreach(lc2, sublist)
1426 		{
1427 			Node	   *col = (Node *) lfirst(lc2);
1428 
1429 			colexprs[i] = lappend(colexprs[i], col);
1430 			i++;
1431 		}
1432 
1433 		/* Release sub-list's cells to save memory */
1434 		list_free(sublist);
1435 	}
1436 
1437 	/*
1438 	 * Now resolve the common types of the columns, and coerce everything to
1439 	 * those types.  Then identify the common typmod and common collation, if
1440 	 * any, of each column.
1441 	 *
1442 	 * We must do collation processing now because (1) assign_query_collations
1443 	 * doesn't process rangetable entries, and (2) we need to label the VALUES
1444 	 * RTE with column collations for use in the outer query.  We don't
1445 	 * consider conflict of implicit collations to be an error here; instead
1446 	 * the column will just show InvalidOid as its collation, and you'll get a
1447 	 * failure later if that results in failure to resolve a collation.
1448 	 *
1449 	 * Note we modify the per-column expression lists in-place.
1450 	 */
1451 	for (i = 0; i < sublist_length; i++)
1452 	{
1453 		Oid			coltype;
1454 		int32		coltypmod = -1;
1455 		Oid			colcoll;
1456 		bool		first = true;
1457 
1458 		coltype = select_common_type(pstate, colexprs[i], "VALUES", NULL);
1459 
1460 		foreach(lc, colexprs[i])
1461 		{
1462 			Node	   *col = (Node *) lfirst(lc);
1463 
1464 			col = coerce_to_common_type(pstate, col, coltype, "VALUES");
1465 			lfirst(lc) = (void *) col;
1466 			if (first)
1467 			{
1468 				coltypmod = exprTypmod(col);
1469 				first = false;
1470 			}
1471 			else
1472 			{
1473 				/* As soon as we see a non-matching typmod, fall back to -1 */
1474 				if (coltypmod >= 0 && coltypmod != exprTypmod(col))
1475 					coltypmod = -1;
1476 			}
1477 		}
1478 
1479 		colcoll = select_common_collation(pstate, colexprs[i], true);
1480 
1481 		coltypes = lappend_oid(coltypes, coltype);
1482 		coltypmods = lappend_int(coltypmods, coltypmod);
1483 		colcollations = lappend_oid(colcollations, colcoll);
1484 	}
1485 
1486 	/*
1487 	 * Finally, rearrange the coerced expressions into row-organized lists.
1488 	 */
1489 	exprsLists = NIL;
1490 	foreach(lc, colexprs[0])
1491 	{
1492 		Node	   *col = (Node *) lfirst(lc);
1493 		List	   *sublist;
1494 
1495 		sublist = list_make1(col);
1496 		exprsLists = lappend(exprsLists, sublist);
1497 	}
1498 	list_free(colexprs[0]);
1499 	for (i = 1; i < sublist_length; i++)
1500 	{
1501 		forboth(lc, colexprs[i], lc2, exprsLists)
1502 		{
1503 			Node	   *col = (Node *) lfirst(lc);
1504 			List	   *sublist = lfirst(lc2);
1505 
1506 			/* sublist pointer in exprsLists won't need adjustment */
1507 			(void) lappend(sublist, col);
1508 		}
1509 		list_free(colexprs[i]);
1510 	}
1511 
1512 	/*
1513 	 * Ordinarily there can't be any current-level Vars in the expression
1514 	 * lists, because the namespace was empty ... but if we're inside CREATE
1515 	 * RULE, then NEW/OLD references might appear.  In that case we have to
1516 	 * mark the VALUES RTE as LATERAL.
1517 	 */
1518 	if (pstate->p_rtable != NIL &&
1519 		contain_vars_of_level((Node *) exprsLists, 0))
1520 		lateral = true;
1521 
1522 	/*
1523 	 * Generate the VALUES RTE
1524 	 */
1525 	rte = addRangeTableEntryForValues(pstate, exprsLists,
1526 									  coltypes, coltypmods, colcollations,
1527 									  NULL, lateral, true);
1528 	addRTEtoQuery(pstate, rte, true, true, true);
1529 
1530 	/* assume new rte is at end */
1531 	rtindex = list_length(pstate->p_rtable);
1532 	Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1533 
1534 	/*
1535 	 * Generate a targetlist as though expanding "*"
1536 	 */
1537 	Assert(pstate->p_next_resno == 1);
1538 	qry->targetList = expandRelAttrs(pstate, rte, rtindex, 0, -1);
1539 
1540 	/*
1541 	 * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
1542 	 * VALUES, so cope.
1543 	 */
1544 	qry->sortClause = transformSortClause(pstate,
1545 										  stmt->sortClause,
1546 										  &qry->targetList,
1547 										  EXPR_KIND_ORDER_BY,
1548 										  false /* allow SQL92 rules */ );
1549 
1550 	qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1551 											EXPR_KIND_OFFSET, "OFFSET");
1552 	qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1553 										   EXPR_KIND_LIMIT, "LIMIT");
1554 
1555 	if (stmt->lockingClause)
1556 		ereport(ERROR,
1557 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1558 		/*------
1559 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
1560 				 errmsg("%s cannot be applied to VALUES",
1561 						LCS_asString(((LockingClause *)
1562 									  linitial(stmt->lockingClause))->strength))));
1563 
1564 	qry->rtable = pstate->p_rtable;
1565 	qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1566 
1567 	qry->hasSubLinks = pstate->p_hasSubLinks;
1568 
1569 	assign_query_collations(pstate, qry);
1570 
1571 	return qry;
1572 }
1573 
1574 /*
1575  * transformSetOperationStmt -
1576  *	  transforms a set-operations tree
1577  *
1578  * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
1579  * structure to it.  We must transform each leaf SELECT and build up a top-
1580  * level Query that contains the leaf SELECTs as subqueries in its rangetable.
1581  * The tree of set operations is converted into the setOperations field of
1582  * the top-level Query.
1583  */
1584 static Query *
transformSetOperationStmt(ParseState * pstate,SelectStmt * stmt)1585 transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
1586 {
1587 	Query	   *qry = makeNode(Query);
1588 	SelectStmt *leftmostSelect;
1589 	int			leftmostRTI;
1590 	Query	   *leftmostQuery;
1591 	SetOperationStmt *sostmt;
1592 	List	   *sortClause;
1593 	Node	   *limitOffset;
1594 	Node	   *limitCount;
1595 	List	   *lockingClause;
1596 	WithClause *withClause;
1597 	Node	   *node;
1598 	ListCell   *left_tlist,
1599 			   *lct,
1600 			   *lcm,
1601 			   *lcc,
1602 			   *l;
1603 	List	   *targetvars,
1604 			   *targetnames,
1605 			   *sv_namespace;
1606 	int			sv_rtable_length;
1607 	RangeTblEntry *jrte;
1608 	int			tllen;
1609 
1610 	qry->commandType = CMD_SELECT;
1611 
1612 	/*
1613 	 * Find leftmost leaf SelectStmt.  We currently only need to do this in
1614 	 * order to deliver a suitable error message if there's an INTO clause
1615 	 * there, implying the set-op tree is in a context that doesn't allow
1616 	 * INTO.  (transformSetOperationTree would throw error anyway, but it
1617 	 * seems worth the trouble to throw a different error for non-leftmost
1618 	 * INTO, so we produce that error in transformSetOperationTree.)
1619 	 */
1620 	leftmostSelect = stmt->larg;
1621 	while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
1622 		leftmostSelect = leftmostSelect->larg;
1623 	Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
1624 		   leftmostSelect->larg == NULL);
1625 	if (leftmostSelect->intoClause)
1626 		ereport(ERROR,
1627 				(errcode(ERRCODE_SYNTAX_ERROR),
1628 				 errmsg("SELECT ... INTO is not allowed here"),
1629 				 parser_errposition(pstate,
1630 									exprLocation((Node *) leftmostSelect->intoClause))));
1631 
1632 	/*
1633 	 * We need to extract ORDER BY and other top-level clauses here and not
1634 	 * let transformSetOperationTree() see them --- else it'll just recurse
1635 	 * right back here!
1636 	 */
1637 	sortClause = stmt->sortClause;
1638 	limitOffset = stmt->limitOffset;
1639 	limitCount = stmt->limitCount;
1640 	lockingClause = stmt->lockingClause;
1641 	withClause = stmt->withClause;
1642 
1643 	stmt->sortClause = NIL;
1644 	stmt->limitOffset = NULL;
1645 	stmt->limitCount = NULL;
1646 	stmt->lockingClause = NIL;
1647 	stmt->withClause = NULL;
1648 
1649 	/* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1650 	if (lockingClause)
1651 		ereport(ERROR,
1652 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1653 		/*------
1654 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
1655 				 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
1656 						LCS_asString(((LockingClause *)
1657 									  linitial(lockingClause))->strength))));
1658 
1659 	/* Process the WITH clause independently of all else */
1660 	if (withClause)
1661 	{
1662 		qry->hasRecursive = withClause->recursive;
1663 		qry->cteList = transformWithClause(pstate, withClause);
1664 		qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1665 	}
1666 
1667 	/*
1668 	 * Recursively transform the components of the tree.
1669 	 */
1670 	sostmt = castNode(SetOperationStmt,
1671 					  transformSetOperationTree(pstate, stmt, true, NULL));
1672 	Assert(sostmt);
1673 	qry->setOperations = (Node *) sostmt;
1674 
1675 	/*
1676 	 * Re-find leftmost SELECT (now it's a sub-query in rangetable)
1677 	 */
1678 	node = sostmt->larg;
1679 	while (node && IsA(node, SetOperationStmt))
1680 		node = ((SetOperationStmt *) node)->larg;
1681 	Assert(node && IsA(node, RangeTblRef));
1682 	leftmostRTI = ((RangeTblRef *) node)->rtindex;
1683 	leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1684 	Assert(leftmostQuery != NULL);
1685 
1686 	/*
1687 	 * Generate dummy targetlist for outer query using column names of
1688 	 * leftmost select and common datatypes/collations of topmost set
1689 	 * operation.  Also make lists of the dummy vars and their names for use
1690 	 * in parsing ORDER BY.
1691 	 *
1692 	 * Note: we use leftmostRTI as the varno of the dummy variables. It
1693 	 * shouldn't matter too much which RT index they have, as long as they
1694 	 * have one that corresponds to a real RT entry; else funny things may
1695 	 * happen when the tree is mashed by rule rewriting.
1696 	 */
1697 	qry->targetList = NIL;
1698 	targetvars = NIL;
1699 	targetnames = NIL;
1700 	left_tlist = list_head(leftmostQuery->targetList);
1701 
1702 	forthree(lct, sostmt->colTypes,
1703 			 lcm, sostmt->colTypmods,
1704 			 lcc, sostmt->colCollations)
1705 	{
1706 		Oid			colType = lfirst_oid(lct);
1707 		int32		colTypmod = lfirst_int(lcm);
1708 		Oid			colCollation = lfirst_oid(lcc);
1709 		TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1710 		char	   *colName;
1711 		TargetEntry *tle;
1712 		Var		   *var;
1713 
1714 		Assert(!lefttle->resjunk);
1715 		colName = pstrdup(lefttle->resname);
1716 		var = makeVar(leftmostRTI,
1717 					  lefttle->resno,
1718 					  colType,
1719 					  colTypmod,
1720 					  colCollation,
1721 					  0);
1722 		var->location = exprLocation((Node *) lefttle->expr);
1723 		tle = makeTargetEntry((Expr *) var,
1724 							  (AttrNumber) pstate->p_next_resno++,
1725 							  colName,
1726 							  false);
1727 		qry->targetList = lappend(qry->targetList, tle);
1728 		targetvars = lappend(targetvars, var);
1729 		targetnames = lappend(targetnames, makeString(colName));
1730 		left_tlist = lnext(left_tlist);
1731 	}
1732 
1733 	/*
1734 	 * As a first step towards supporting sort clauses that are expressions
1735 	 * using the output columns, generate a namespace entry that makes the
1736 	 * output columns visible.  A Join RTE node is handy for this, since we
1737 	 * can easily control the Vars generated upon matches.
1738 	 *
1739 	 * Note: we don't yet do anything useful with such cases, but at least
1740 	 * "ORDER BY upper(foo)" will draw the right error message rather than
1741 	 * "foo not found".
1742 	 */
1743 	sv_rtable_length = list_length(pstate->p_rtable);
1744 
1745 	jrte = addRangeTableEntryForJoin(pstate,
1746 									 targetnames,
1747 									 JOIN_INNER,
1748 									 targetvars,
1749 									 NULL,
1750 									 false);
1751 
1752 	sv_namespace = pstate->p_namespace;
1753 	pstate->p_namespace = NIL;
1754 
1755 	/* add jrte to column namespace only */
1756 	addRTEtoQuery(pstate, jrte, false, false, true);
1757 
1758 	/*
1759 	 * For now, we don't support resjunk sort clauses on the output of a
1760 	 * setOperation tree --- you can only use the SQL92-spec options of
1761 	 * selecting an output column by name or number.  Enforce by checking that
1762 	 * transformSortClause doesn't add any items to tlist.
1763 	 */
1764 	tllen = list_length(qry->targetList);
1765 
1766 	qry->sortClause = transformSortClause(pstate,
1767 										  sortClause,
1768 										  &qry->targetList,
1769 										  EXPR_KIND_ORDER_BY,
1770 										  false /* allow SQL92 rules */ );
1771 
1772 	/* restore namespace, remove jrte from rtable */
1773 	pstate->p_namespace = sv_namespace;
1774 	pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
1775 
1776 	if (tllen != list_length(qry->targetList))
1777 		ereport(ERROR,
1778 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1779 				 errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
1780 				 errdetail("Only result column names can be used, not expressions or functions."),
1781 				 errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
1782 				 parser_errposition(pstate,
1783 									exprLocation(list_nth(qry->targetList, tllen)))));
1784 
1785 	qry->limitOffset = transformLimitClause(pstate, limitOffset,
1786 											EXPR_KIND_OFFSET, "OFFSET");
1787 	qry->limitCount = transformLimitClause(pstate, limitCount,
1788 										   EXPR_KIND_LIMIT, "LIMIT");
1789 
1790 	qry->rtable = pstate->p_rtable;
1791 	qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1792 
1793 	qry->hasSubLinks = pstate->p_hasSubLinks;
1794 	qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1795 	qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1796 	qry->hasAggs = pstate->p_hasAggs;
1797 
1798 	foreach(l, lockingClause)
1799 	{
1800 		transformLockingClause(pstate, qry,
1801 							   (LockingClause *) lfirst(l), false);
1802 	}
1803 
1804 	assign_query_collations(pstate, qry);
1805 
1806 	/* this must be done after collations, for reliable comparison of exprs */
1807 	if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
1808 		parseCheckAggregates(pstate, qry);
1809 
1810 	return qry;
1811 }
1812 
1813 /*
1814  * transformSetOperationTree
1815  *		Recursively transform leaves and internal nodes of a set-op tree
1816  *
1817  * In addition to returning the transformed node, if targetlist isn't NULL
1818  * then we return a list of its non-resjunk TargetEntry nodes.  For a leaf
1819  * set-op node these are the actual targetlist entries; otherwise they are
1820  * dummy entries created to carry the type, typmod, collation, and location
1821  * (for error messages) of each output column of the set-op node.  This info
1822  * is needed only during the internal recursion of this function, so outside
1823  * callers pass NULL for targetlist.  Note: the reason for passing the
1824  * actual targetlist entries of a leaf node is so that upper levels can
1825  * replace UNKNOWN Consts with properly-coerced constants.
1826  */
1827 static Node *
transformSetOperationTree(ParseState * pstate,SelectStmt * stmt,bool isTopLevel,List ** targetlist)1828 transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
1829 						  bool isTopLevel, List **targetlist)
1830 {
1831 	bool		isLeaf;
1832 
1833 	Assert(stmt && IsA(stmt, SelectStmt));
1834 
1835 	/* Guard against stack overflow due to overly complex set-expressions */
1836 	check_stack_depth();
1837 
1838 	/*
1839 	 * Validity-check both leaf and internal SELECTs for disallowed ops.
1840 	 */
1841 	if (stmt->intoClause)
1842 		ereport(ERROR,
1843 				(errcode(ERRCODE_SYNTAX_ERROR),
1844 				 errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
1845 				 parser_errposition(pstate,
1846 									exprLocation((Node *) stmt->intoClause))));
1847 
1848 	/* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1849 	if (stmt->lockingClause)
1850 		ereport(ERROR,
1851 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1852 		/*------
1853 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
1854 				 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
1855 						LCS_asString(((LockingClause *)
1856 									  linitial(stmt->lockingClause))->strength))));
1857 
1858 	/*
1859 	 * If an internal node of a set-op tree has ORDER BY, LIMIT, FOR UPDATE,
1860 	 * or WITH clauses attached, we need to treat it like a leaf node to
1861 	 * generate an independent sub-Query tree.  Otherwise, it can be
1862 	 * represented by a SetOperationStmt node underneath the parent Query.
1863 	 */
1864 	if (stmt->op == SETOP_NONE)
1865 	{
1866 		Assert(stmt->larg == NULL && stmt->rarg == NULL);
1867 		isLeaf = true;
1868 	}
1869 	else
1870 	{
1871 		Assert(stmt->larg != NULL && stmt->rarg != NULL);
1872 		if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
1873 			stmt->lockingClause || stmt->withClause)
1874 			isLeaf = true;
1875 		else
1876 			isLeaf = false;
1877 	}
1878 
1879 	if (isLeaf)
1880 	{
1881 		/* Process leaf SELECT */
1882 		Query	   *selectQuery;
1883 		char		selectName[32];
1884 		RangeTblEntry *rte PG_USED_FOR_ASSERTS_ONLY;
1885 		RangeTblRef *rtr;
1886 		ListCell   *tl;
1887 
1888 		/*
1889 		 * Transform SelectStmt into a Query.
1890 		 *
1891 		 * This works the same as SELECT transformation normally would, except
1892 		 * that we prevent resolving unknown-type outputs as TEXT.  This does
1893 		 * not change the subquery's semantics since if the column type
1894 		 * matters semantically, it would have been resolved to something else
1895 		 * anyway.  Doing this lets us resolve such outputs using
1896 		 * select_common_type(), below.
1897 		 *
1898 		 * Note: previously transformed sub-queries don't affect the parsing
1899 		 * of this sub-query, because they are not in the toplevel pstate's
1900 		 * namespace list.
1901 		 */
1902 		selectQuery = parse_sub_analyze((Node *) stmt, pstate,
1903 										NULL, false, false);
1904 
1905 		/*
1906 		 * Check for bogus references to Vars on the current query level (but
1907 		 * upper-level references are okay). Normally this can't happen
1908 		 * because the namespace will be empty, but it could happen if we are
1909 		 * inside a rule.
1910 		 */
1911 		if (pstate->p_namespace)
1912 		{
1913 			if (contain_vars_of_level((Node *) selectQuery, 1))
1914 				ereport(ERROR,
1915 						(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1916 						 errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
1917 						 parser_errposition(pstate,
1918 											locate_var_of_level((Node *) selectQuery, 1))));
1919 		}
1920 
1921 		/*
1922 		 * Extract a list of the non-junk TLEs for upper-level processing.
1923 		 */
1924 		if (targetlist)
1925 		{
1926 			*targetlist = NIL;
1927 			foreach(tl, selectQuery->targetList)
1928 			{
1929 				TargetEntry *tle = (TargetEntry *) lfirst(tl);
1930 
1931 				if (!tle->resjunk)
1932 					*targetlist = lappend(*targetlist, tle);
1933 			}
1934 		}
1935 
1936 		/*
1937 		 * Make the leaf query be a subquery in the top-level rangetable.
1938 		 */
1939 		snprintf(selectName, sizeof(selectName), "*SELECT* %d",
1940 				 list_length(pstate->p_rtable) + 1);
1941 		rte = addRangeTableEntryForSubquery(pstate,
1942 											selectQuery,
1943 											makeAlias(selectName, NIL),
1944 											false,
1945 											false);
1946 
1947 		/*
1948 		 * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
1949 		 */
1950 		rtr = makeNode(RangeTblRef);
1951 		/* assume new rte is at end */
1952 		rtr->rtindex = list_length(pstate->p_rtable);
1953 		Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
1954 		return (Node *) rtr;
1955 	}
1956 	else
1957 	{
1958 		/* Process an internal node (set operation node) */
1959 		SetOperationStmt *op = makeNode(SetOperationStmt);
1960 		List	   *ltargetlist;
1961 		List	   *rtargetlist;
1962 		ListCell   *ltl;
1963 		ListCell   *rtl;
1964 		const char *context;
1965 
1966 		context = (stmt->op == SETOP_UNION ? "UNION" :
1967 				   (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
1968 					"EXCEPT"));
1969 
1970 		op->op = stmt->op;
1971 		op->all = stmt->all;
1972 
1973 		/*
1974 		 * Recursively transform the left child node.
1975 		 */
1976 		op->larg = transformSetOperationTree(pstate, stmt->larg,
1977 											 false,
1978 											 &ltargetlist);
1979 
1980 		/*
1981 		 * If we are processing a recursive union query, now is the time to
1982 		 * examine the non-recursive term's output columns and mark the
1983 		 * containing CTE as having those result columns.  We should do this
1984 		 * only at the topmost setop of the CTE, of course.
1985 		 */
1986 		if (isTopLevel &&
1987 			pstate->p_parent_cte &&
1988 			pstate->p_parent_cte->cterecursive)
1989 			determineRecursiveColTypes(pstate, op->larg, ltargetlist);
1990 
1991 		/*
1992 		 * Recursively transform the right child node.
1993 		 */
1994 		op->rarg = transformSetOperationTree(pstate, stmt->rarg,
1995 											 false,
1996 											 &rtargetlist);
1997 
1998 		/*
1999 		 * Verify that the two children have the same number of non-junk
2000 		 * columns, and determine the types of the merged output columns.
2001 		 */
2002 		if (list_length(ltargetlist) != list_length(rtargetlist))
2003 			ereport(ERROR,
2004 					(errcode(ERRCODE_SYNTAX_ERROR),
2005 					 errmsg("each %s query must have the same number of columns",
2006 							context),
2007 					 parser_errposition(pstate,
2008 										exprLocation((Node *) rtargetlist))));
2009 
2010 		if (targetlist)
2011 			*targetlist = NIL;
2012 		op->colTypes = NIL;
2013 		op->colTypmods = NIL;
2014 		op->colCollations = NIL;
2015 		op->groupClauses = NIL;
2016 		forboth(ltl, ltargetlist, rtl, rtargetlist)
2017 		{
2018 			TargetEntry *ltle = (TargetEntry *) lfirst(ltl);
2019 			TargetEntry *rtle = (TargetEntry *) lfirst(rtl);
2020 			Node	   *lcolnode = (Node *) ltle->expr;
2021 			Node	   *rcolnode = (Node *) rtle->expr;
2022 			Oid			lcoltype = exprType(lcolnode);
2023 			Oid			rcoltype = exprType(rcolnode);
2024 			int32		lcoltypmod = exprTypmod(lcolnode);
2025 			int32		rcoltypmod = exprTypmod(rcolnode);
2026 			Node	   *bestexpr;
2027 			int			bestlocation;
2028 			Oid			rescoltype;
2029 			int32		rescoltypmod;
2030 			Oid			rescolcoll;
2031 
2032 			/* select common type, same as CASE et al */
2033 			rescoltype = select_common_type(pstate,
2034 											list_make2(lcolnode, rcolnode),
2035 											context,
2036 											&bestexpr);
2037 			bestlocation = exprLocation(bestexpr);
2038 			/* if same type and same typmod, use typmod; else default */
2039 			if (lcoltype == rcoltype && lcoltypmod == rcoltypmod)
2040 				rescoltypmod = lcoltypmod;
2041 			else
2042 				rescoltypmod = -1;
2043 
2044 			/*
2045 			 * Verify the coercions are actually possible.  If not, we'd fail
2046 			 * later anyway, but we want to fail now while we have sufficient
2047 			 * context to produce an error cursor position.
2048 			 *
2049 			 * For all non-UNKNOWN-type cases, we verify coercibility but we
2050 			 * don't modify the child's expression, for fear of changing the
2051 			 * child query's semantics.
2052 			 *
2053 			 * If a child expression is an UNKNOWN-type Const or Param, we
2054 			 * want to replace it with the coerced expression.  This can only
2055 			 * happen when the child is a leaf set-op node.  It's safe to
2056 			 * replace the expression because if the child query's semantics
2057 			 * depended on the type of this output column, it'd have already
2058 			 * coerced the UNKNOWN to something else.  We want to do this
2059 			 * because (a) we want to verify that a Const is valid for the
2060 			 * target type, or resolve the actual type of an UNKNOWN Param,
2061 			 * and (b) we want to avoid unnecessary discrepancies between the
2062 			 * output type of the child query and the resolved target type.
2063 			 * Such a discrepancy would disable optimization in the planner.
2064 			 *
2065 			 * If it's some other UNKNOWN-type node, eg a Var, we do nothing
2066 			 * (knowing that coerce_to_common_type would fail).  The planner
2067 			 * is sometimes able to fold an UNKNOWN Var to a constant before
2068 			 * it has to coerce the type, so failing now would just break
2069 			 * cases that might work.
2070 			 */
2071 			if (lcoltype != UNKNOWNOID)
2072 				lcolnode = coerce_to_common_type(pstate, lcolnode,
2073 												 rescoltype, context);
2074 			else if (IsA(lcolnode, Const) ||
2075 					 IsA(lcolnode, Param))
2076 			{
2077 				lcolnode = coerce_to_common_type(pstate, lcolnode,
2078 												 rescoltype, context);
2079 				ltle->expr = (Expr *) lcolnode;
2080 			}
2081 
2082 			if (rcoltype != UNKNOWNOID)
2083 				rcolnode = coerce_to_common_type(pstate, rcolnode,
2084 												 rescoltype, context);
2085 			else if (IsA(rcolnode, Const) ||
2086 					 IsA(rcolnode, Param))
2087 			{
2088 				rcolnode = coerce_to_common_type(pstate, rcolnode,
2089 												 rescoltype, context);
2090 				rtle->expr = (Expr *) rcolnode;
2091 			}
2092 
2093 			/*
2094 			 * Select common collation.  A common collation is required for
2095 			 * all set operators except UNION ALL; see SQL:2008 7.13 <query
2096 			 * expression> Syntax Rule 15c.  (If we fail to identify a common
2097 			 * collation for a UNION ALL column, the curCollations element
2098 			 * will be set to InvalidOid, which may result in a runtime error
2099 			 * if something at a higher query level wants to use the column's
2100 			 * collation.)
2101 			 */
2102 			rescolcoll = select_common_collation(pstate,
2103 												 list_make2(lcolnode, rcolnode),
2104 												 (op->op == SETOP_UNION && op->all));
2105 
2106 			/* emit results */
2107 			op->colTypes = lappend_oid(op->colTypes, rescoltype);
2108 			op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
2109 			op->colCollations = lappend_oid(op->colCollations, rescolcoll);
2110 
2111 			/*
2112 			 * For all cases except UNION ALL, identify the grouping operators
2113 			 * (and, if available, sorting operators) that will be used to
2114 			 * eliminate duplicates.
2115 			 */
2116 			if (op->op != SETOP_UNION || !op->all)
2117 			{
2118 				SortGroupClause *grpcl = makeNode(SortGroupClause);
2119 				Oid			sortop;
2120 				Oid			eqop;
2121 				bool		hashable;
2122 				ParseCallbackState pcbstate;
2123 
2124 				setup_parser_errposition_callback(&pcbstate, pstate,
2125 												  bestlocation);
2126 
2127 				/* determine the eqop and optional sortop */
2128 				get_sort_group_operators(rescoltype,
2129 										 false, true, false,
2130 										 &sortop, &eqop, NULL,
2131 										 &hashable);
2132 
2133 				cancel_parser_errposition_callback(&pcbstate);
2134 
2135 				/* we don't have a tlist yet, so can't assign sortgrouprefs */
2136 				grpcl->tleSortGroupRef = 0;
2137 				grpcl->eqop = eqop;
2138 				grpcl->sortop = sortop;
2139 				grpcl->nulls_first = false; /* OK with or without sortop */
2140 				grpcl->hashable = hashable;
2141 
2142 				op->groupClauses = lappend(op->groupClauses, grpcl);
2143 			}
2144 
2145 			/*
2146 			 * Construct a dummy tlist entry to return.  We use a SetToDefault
2147 			 * node for the expression, since it carries exactly the fields
2148 			 * needed, but any other expression node type would do as well.
2149 			 */
2150 			if (targetlist)
2151 			{
2152 				SetToDefault *rescolnode = makeNode(SetToDefault);
2153 				TargetEntry *restle;
2154 
2155 				rescolnode->typeId = rescoltype;
2156 				rescolnode->typeMod = rescoltypmod;
2157 				rescolnode->collation = rescolcoll;
2158 				rescolnode->location = bestlocation;
2159 				restle = makeTargetEntry((Expr *) rescolnode,
2160 										 0, /* no need to set resno */
2161 										 NULL,
2162 										 false);
2163 				*targetlist = lappend(*targetlist, restle);
2164 			}
2165 		}
2166 
2167 		return (Node *) op;
2168 	}
2169 }
2170 
2171 /*
2172  * Process the outputs of the non-recursive term of a recursive union
2173  * to set up the parent CTE's columns
2174  */
2175 static void
determineRecursiveColTypes(ParseState * pstate,Node * larg,List * nrtargetlist)2176 determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
2177 {
2178 	Node	   *node;
2179 	int			leftmostRTI;
2180 	Query	   *leftmostQuery;
2181 	List	   *targetList;
2182 	ListCell   *left_tlist;
2183 	ListCell   *nrtl;
2184 	int			next_resno;
2185 
2186 	/*
2187 	 * Find leftmost leaf SELECT
2188 	 */
2189 	node = larg;
2190 	while (node && IsA(node, SetOperationStmt))
2191 		node = ((SetOperationStmt *) node)->larg;
2192 	Assert(node && IsA(node, RangeTblRef));
2193 	leftmostRTI = ((RangeTblRef *) node)->rtindex;
2194 	leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
2195 	Assert(leftmostQuery != NULL);
2196 
2197 	/*
2198 	 * Generate dummy targetlist using column names of leftmost select and
2199 	 * dummy result expressions of the non-recursive term.
2200 	 */
2201 	targetList = NIL;
2202 	left_tlist = list_head(leftmostQuery->targetList);
2203 	next_resno = 1;
2204 
2205 	foreach(nrtl, nrtargetlist)
2206 	{
2207 		TargetEntry *nrtle = (TargetEntry *) lfirst(nrtl);
2208 		TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
2209 		char	   *colName;
2210 		TargetEntry *tle;
2211 
2212 		Assert(!lefttle->resjunk);
2213 		colName = pstrdup(lefttle->resname);
2214 		tle = makeTargetEntry(nrtle->expr,
2215 							  next_resno++,
2216 							  colName,
2217 							  false);
2218 		targetList = lappend(targetList, tle);
2219 		left_tlist = lnext(left_tlist);
2220 	}
2221 
2222 	/* Now build CTE's output column info using dummy targetlist */
2223 	analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
2224 }
2225 
2226 
2227 /*
2228  * transformUpdateStmt -
2229  *	  transforms an update statement
2230  */
2231 static Query *
transformUpdateStmt(ParseState * pstate,UpdateStmt * stmt)2232 transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
2233 {
2234 	Query	   *qry = makeNode(Query);
2235 	ParseNamespaceItem *nsitem;
2236 	Node	   *qual;
2237 
2238 	qry->commandType = CMD_UPDATE;
2239 	pstate->p_is_insert = false;
2240 
2241 	/* process the WITH clause independently of all else */
2242 	if (stmt->withClause)
2243 	{
2244 		qry->hasRecursive = stmt->withClause->recursive;
2245 		qry->cteList = transformWithClause(pstate, stmt->withClause);
2246 		qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
2247 	}
2248 
2249 	qry->resultRelation = setTargetTable(pstate, stmt->relation,
2250 										 stmt->relation->inh,
2251 										 true,
2252 										 ACL_UPDATE);
2253 
2254 	/* grab the namespace item made by setTargetTable */
2255 	nsitem = (ParseNamespaceItem *) llast(pstate->p_namespace);
2256 
2257 	/* subqueries in FROM cannot access the result relation */
2258 	nsitem->p_lateral_only = true;
2259 	nsitem->p_lateral_ok = false;
2260 
2261 	/*
2262 	 * the FROM clause is non-standard SQL syntax. We used to be able to do
2263 	 * this with REPLACE in POSTQUEL so we keep the feature.
2264 	 */
2265 	transformFromClause(pstate, stmt->fromClause);
2266 
2267 	/* remaining clauses can reference the result relation normally */
2268 	nsitem->p_lateral_only = false;
2269 	nsitem->p_lateral_ok = true;
2270 
2271 	qual = transformWhereClause(pstate, stmt->whereClause,
2272 								EXPR_KIND_WHERE, "WHERE");
2273 
2274 	qry->returningList = transformReturningList(pstate, stmt->returningList);
2275 
2276 	/*
2277 	 * Now we are done with SELECT-like processing, and can get on with
2278 	 * transforming the target list to match the UPDATE target columns.
2279 	 */
2280 	qry->targetList = transformUpdateTargetList(pstate, stmt->targetList);
2281 
2282 	qry->rtable = pstate->p_rtable;
2283 	qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
2284 
2285 	qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2286 	qry->hasSubLinks = pstate->p_hasSubLinks;
2287 
2288 	assign_query_collations(pstate, qry);
2289 
2290 	return qry;
2291 }
2292 
2293 /*
2294  * transformUpdateTargetList -
2295  *	handle SET clause in UPDATE/INSERT ... ON CONFLICT UPDATE
2296  */
2297 static List *
transformUpdateTargetList(ParseState * pstate,List * origTlist)2298 transformUpdateTargetList(ParseState *pstate, List *origTlist)
2299 {
2300 	List	   *tlist = NIL;
2301 	RangeTblEntry *target_rte;
2302 	ListCell   *orig_tl;
2303 	ListCell   *tl;
2304 
2305 	tlist = transformTargetList(pstate, origTlist,
2306 								EXPR_KIND_UPDATE_SOURCE);
2307 
2308 	/* Prepare to assign non-conflicting resnos to resjunk attributes */
2309 	if (pstate->p_next_resno <= RelationGetNumberOfAttributes(pstate->p_target_relation))
2310 		pstate->p_next_resno = RelationGetNumberOfAttributes(pstate->p_target_relation) + 1;
2311 
2312 	/* Prepare non-junk columns for assignment to target table */
2313 	target_rte = pstate->p_target_rangetblentry;
2314 	orig_tl = list_head(origTlist);
2315 
2316 	foreach(tl, tlist)
2317 	{
2318 		TargetEntry *tle = (TargetEntry *) lfirst(tl);
2319 		ResTarget  *origTarget;
2320 		int			attrno;
2321 
2322 		if (tle->resjunk)
2323 		{
2324 			/*
2325 			 * Resjunk nodes need no additional processing, but be sure they
2326 			 * have resnos that do not match any target columns; else rewriter
2327 			 * or planner might get confused.  They don't need a resname
2328 			 * either.
2329 			 */
2330 			tle->resno = (AttrNumber) pstate->p_next_resno++;
2331 			tle->resname = NULL;
2332 			continue;
2333 		}
2334 		if (orig_tl == NULL)
2335 			elog(ERROR, "UPDATE target count mismatch --- internal error");
2336 		origTarget = lfirst_node(ResTarget, orig_tl);
2337 
2338 		attrno = attnameAttNum(pstate->p_target_relation,
2339 							   origTarget->name, true);
2340 		if (attrno == InvalidAttrNumber)
2341 			ereport(ERROR,
2342 					(errcode(ERRCODE_UNDEFINED_COLUMN),
2343 					 errmsg("column \"%s\" of relation \"%s\" does not exist",
2344 							origTarget->name,
2345 							RelationGetRelationName(pstate->p_target_relation)),
2346 					 parser_errposition(pstate, origTarget->location)));
2347 
2348 		updateTargetListEntry(pstate, tle, origTarget->name,
2349 							  attrno,
2350 							  origTarget->indirection,
2351 							  origTarget->location);
2352 
2353 		/* Mark the target column as requiring update permissions */
2354 		target_rte->updatedCols = bms_add_member(target_rte->updatedCols,
2355 												 attrno - FirstLowInvalidHeapAttributeNumber);
2356 
2357 		orig_tl = lnext(orig_tl);
2358 	}
2359 	if (orig_tl != NULL)
2360 		elog(ERROR, "UPDATE target count mismatch --- internal error");
2361 
2362 	return tlist;
2363 }
2364 
2365 /*
2366  * transformReturningList -
2367  *	handle a RETURNING clause in INSERT/UPDATE/DELETE
2368  */
2369 static List *
transformReturningList(ParseState * pstate,List * returningList)2370 transformReturningList(ParseState *pstate, List *returningList)
2371 {
2372 	List	   *rlist;
2373 	int			save_next_resno;
2374 
2375 	if (returningList == NIL)
2376 		return NIL;				/* nothing to do */
2377 
2378 	/*
2379 	 * We need to assign resnos starting at one in the RETURNING list. Save
2380 	 * and restore the main tlist's value of p_next_resno, just in case
2381 	 * someone looks at it later (probably won't happen).
2382 	 */
2383 	save_next_resno = pstate->p_next_resno;
2384 	pstate->p_next_resno = 1;
2385 
2386 	/* transform RETURNING identically to a SELECT targetlist */
2387 	rlist = transformTargetList(pstate, returningList, EXPR_KIND_RETURNING);
2388 
2389 	/*
2390 	 * Complain if the nonempty tlist expanded to nothing (which is possible
2391 	 * if it contains only a star-expansion of a zero-column table).  If we
2392 	 * allow this, the parsed Query will look like it didn't have RETURNING,
2393 	 * with results that would probably surprise the user.
2394 	 */
2395 	if (rlist == NIL)
2396 		ereport(ERROR,
2397 				(errcode(ERRCODE_SYNTAX_ERROR),
2398 				 errmsg("RETURNING must have at least one column"),
2399 				 parser_errposition(pstate,
2400 									exprLocation(linitial(returningList)))));
2401 
2402 	/* mark column origins */
2403 	markTargetListOrigins(pstate, rlist);
2404 
2405 	/* resolve any still-unresolved output columns as being type text */
2406 	if (pstate->p_resolve_unknowns)
2407 		resolveTargetListUnknowns(pstate, rlist);
2408 
2409 	/* restore state */
2410 	pstate->p_next_resno = save_next_resno;
2411 
2412 	return rlist;
2413 }
2414 
2415 
2416 /*
2417  * transformDeclareCursorStmt -
2418  *	transform a DECLARE CURSOR Statement
2419  *
2420  * DECLARE CURSOR is like other utility statements in that we emit it as a
2421  * CMD_UTILITY Query node; however, we must first transform the contained
2422  * query.  We used to postpone that until execution, but it's really necessary
2423  * to do it during the normal parse analysis phase to ensure that side effects
2424  * of parser hooks happen at the expected time.
2425  */
2426 static Query *
transformDeclareCursorStmt(ParseState * pstate,DeclareCursorStmt * stmt)2427 transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
2428 {
2429 	Query	   *result;
2430 	Query	   *query;
2431 
2432 	/*
2433 	 * Don't allow both SCROLL and NO SCROLL to be specified
2434 	 */
2435 	if ((stmt->options & CURSOR_OPT_SCROLL) &&
2436 		(stmt->options & CURSOR_OPT_NO_SCROLL))
2437 		ereport(ERROR,
2438 				(errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
2439 				 errmsg("cannot specify both SCROLL and NO SCROLL")));
2440 
2441 	/* Transform contained query, not allowing SELECT INTO */
2442 	query = transformStmt(pstate, stmt->query);
2443 	stmt->query = (Node *) query;
2444 
2445 	/* Grammar should not have allowed anything but SELECT */
2446 	if (!IsA(query, Query) ||
2447 		query->commandType != CMD_SELECT)
2448 		elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
2449 
2450 	/*
2451 	 * We also disallow data-modifying WITH in a cursor.  (This could be
2452 	 * allowed, but the semantics of when the updates occur might be
2453 	 * surprising.)
2454 	 */
2455 	if (query->hasModifyingCTE)
2456 		ereport(ERROR,
2457 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2458 				 errmsg("DECLARE CURSOR must not contain data-modifying statements in WITH")));
2459 
2460 	/* FOR UPDATE and WITH HOLD are not compatible */
2461 	if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
2462 		ereport(ERROR,
2463 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2464 		/*------
2465 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
2466 				 errmsg("DECLARE CURSOR WITH HOLD ... %s is not supported",
2467 						LCS_asString(((RowMarkClause *)
2468 									  linitial(query->rowMarks))->strength)),
2469 				 errdetail("Holdable cursors must be READ ONLY.")));
2470 
2471 	/* FOR UPDATE and SCROLL are not compatible */
2472 	if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
2473 		ereport(ERROR,
2474 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2475 		/*------
2476 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
2477 				 errmsg("DECLARE SCROLL CURSOR ... %s is not supported",
2478 						LCS_asString(((RowMarkClause *)
2479 									  linitial(query->rowMarks))->strength)),
2480 				 errdetail("Scrollable cursors must be READ ONLY.")));
2481 
2482 	/* FOR UPDATE and INSENSITIVE are not compatible */
2483 	if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
2484 		ereport(ERROR,
2485 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2486 		/*------
2487 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
2488 				 errmsg("DECLARE INSENSITIVE CURSOR ... %s is not supported",
2489 						LCS_asString(((RowMarkClause *)
2490 									  linitial(query->rowMarks))->strength)),
2491 				 errdetail("Insensitive cursors must be READ ONLY.")));
2492 
2493 	/* represent the command as a utility Query */
2494 	result = makeNode(Query);
2495 	result->commandType = CMD_UTILITY;
2496 	result->utilityStmt = (Node *) stmt;
2497 
2498 	return result;
2499 }
2500 
2501 
2502 /*
2503  * transformExplainStmt -
2504  *	transform an EXPLAIN Statement
2505  *
2506  * EXPLAIN is like other utility statements in that we emit it as a
2507  * CMD_UTILITY Query node; however, we must first transform the contained
2508  * query.  We used to postpone that until execution, but it's really necessary
2509  * to do it during the normal parse analysis phase to ensure that side effects
2510  * of parser hooks happen at the expected time.
2511  */
2512 static Query *
transformExplainStmt(ParseState * pstate,ExplainStmt * stmt)2513 transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
2514 {
2515 	Query	   *result;
2516 
2517 	/* transform contained query, allowing SELECT INTO */
2518 	stmt->query = (Node *) transformOptionalSelectInto(pstate, stmt->query);
2519 
2520 	/* represent the command as a utility Query */
2521 	result = makeNode(Query);
2522 	result->commandType = CMD_UTILITY;
2523 	result->utilityStmt = (Node *) stmt;
2524 
2525 	return result;
2526 }
2527 
2528 
2529 /*
2530  * transformCreateTableAsStmt -
2531  *	transform a CREATE TABLE AS, SELECT ... INTO, or CREATE MATERIALIZED VIEW
2532  *	Statement
2533  *
2534  * As with DECLARE CURSOR and EXPLAIN, transform the contained statement now.
2535  */
2536 static Query *
transformCreateTableAsStmt(ParseState * pstate,CreateTableAsStmt * stmt)2537 transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
2538 {
2539 	Query	   *result;
2540 	Query	   *query;
2541 
2542 	/* transform contained query, not allowing SELECT INTO */
2543 	query = transformStmt(pstate, stmt->query);
2544 	stmt->query = (Node *) query;
2545 
2546 	/* additional work needed for CREATE MATERIALIZED VIEW */
2547 	if (stmt->relkind == OBJECT_MATVIEW)
2548 	{
2549 		/*
2550 		 * Prohibit a data-modifying CTE in the query used to create a
2551 		 * materialized view. It's not sufficiently clear what the user would
2552 		 * want to happen if the MV is refreshed or incrementally maintained.
2553 		 */
2554 		if (query->hasModifyingCTE)
2555 			ereport(ERROR,
2556 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2557 					 errmsg("materialized views must not use data-modifying statements in WITH")));
2558 
2559 		/*
2560 		 * Check whether any temporary database objects are used in the
2561 		 * creation query. It would be hard to refresh data or incrementally
2562 		 * maintain it if a source disappeared.
2563 		 */
2564 		if (isQueryUsingTempRelation(query))
2565 			ereport(ERROR,
2566 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2567 					 errmsg("materialized views must not use temporary tables or views")));
2568 
2569 		/*
2570 		 * A materialized view would either need to save parameters for use in
2571 		 * maintaining/loading the data or prohibit them entirely.  The latter
2572 		 * seems safer and more sane.
2573 		 */
2574 		if (query_contains_extern_params(query))
2575 			ereport(ERROR,
2576 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2577 					 errmsg("materialized views may not be defined using bound parameters")));
2578 
2579 		/*
2580 		 * For now, we disallow unlogged materialized views, because it seems
2581 		 * like a bad idea for them to just go to empty after a crash. (If we
2582 		 * could mark them as unpopulated, that would be better, but that
2583 		 * requires catalog changes which crash recovery can't presently
2584 		 * handle.)
2585 		 */
2586 		if (stmt->into->rel->relpersistence == RELPERSISTENCE_UNLOGGED)
2587 			ereport(ERROR,
2588 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2589 					 errmsg("materialized views cannot be UNLOGGED")));
2590 
2591 		/*
2592 		 * At runtime, we'll need a copy of the parsed-but-not-rewritten Query
2593 		 * for purposes of creating the view's ON SELECT rule.  We stash that
2594 		 * in the IntoClause because that's where intorel_startup() can
2595 		 * conveniently get it from.
2596 		 */
2597 		stmt->into->viewQuery = (Node *) copyObject(query);
2598 	}
2599 
2600 	/* represent the command as a utility Query */
2601 	result = makeNode(Query);
2602 	result->commandType = CMD_UTILITY;
2603 	result->utilityStmt = (Node *) stmt;
2604 
2605 	return result;
2606 }
2607 
2608 /*
2609  * transform a CallStmt
2610  *
2611  * We need to do parse analysis on the procedure call and its arguments.
2612  */
2613 static Query *
transformCallStmt(ParseState * pstate,CallStmt * stmt)2614 transformCallStmt(ParseState *pstate, CallStmt *stmt)
2615 {
2616 	List	   *targs;
2617 	ListCell   *lc;
2618 	Node	   *node;
2619 	Query	   *result;
2620 
2621 	targs = NIL;
2622 	foreach(lc, stmt->funccall->args)
2623 	{
2624 		targs = lappend(targs, transformExpr(pstate,
2625 											 (Node *) lfirst(lc),
2626 											 EXPR_KIND_CALL_ARGUMENT));
2627 	}
2628 
2629 	node = ParseFuncOrColumn(pstate,
2630 							 stmt->funccall->funcname,
2631 							 targs,
2632 							 pstate->p_last_srf,
2633 							 stmt->funccall,
2634 							 true,
2635 							 stmt->funccall->location);
2636 
2637 	assign_expr_collations(pstate, node);
2638 
2639 	stmt->funcexpr = castNode(FuncExpr, node);
2640 
2641 	result = makeNode(Query);
2642 	result->commandType = CMD_UTILITY;
2643 	result->utilityStmt = (Node *) stmt;
2644 
2645 	return result;
2646 }
2647 
2648 /*
2649  * Produce a string representation of a LockClauseStrength value.
2650  * This should only be applied to valid values (not LCS_NONE).
2651  */
2652 const char *
LCS_asString(LockClauseStrength strength)2653 LCS_asString(LockClauseStrength strength)
2654 {
2655 	switch (strength)
2656 	{
2657 		case LCS_NONE:
2658 			Assert(false);
2659 			break;
2660 		case LCS_FORKEYSHARE:
2661 			return "FOR KEY SHARE";
2662 		case LCS_FORSHARE:
2663 			return "FOR SHARE";
2664 		case LCS_FORNOKEYUPDATE:
2665 			return "FOR NO KEY UPDATE";
2666 		case LCS_FORUPDATE:
2667 			return "FOR UPDATE";
2668 	}
2669 	return "FOR some";			/* shouldn't happen */
2670 }
2671 
2672 /*
2673  * Check for features that are not supported with FOR [KEY] UPDATE/SHARE.
2674  *
2675  * exported so planner can check again after rewriting, query pullup, etc
2676  */
2677 void
CheckSelectLocking(Query * qry,LockClauseStrength strength)2678 CheckSelectLocking(Query *qry, LockClauseStrength strength)
2679 {
2680 	Assert(strength != LCS_NONE);	/* else caller error */
2681 
2682 	if (qry->setOperations)
2683 		ereport(ERROR,
2684 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2685 		/*------
2686 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
2687 				 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
2688 						LCS_asString(strength))));
2689 	if (qry->distinctClause != NIL)
2690 		ereport(ERROR,
2691 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2692 		/*------
2693 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
2694 				 errmsg("%s is not allowed with DISTINCT clause",
2695 						LCS_asString(strength))));
2696 	if (qry->groupClause != NIL || qry->groupingSets != NIL)
2697 		ereport(ERROR,
2698 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2699 		/*------
2700 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
2701 				 errmsg("%s is not allowed with GROUP BY clause",
2702 						LCS_asString(strength))));
2703 	if (qry->havingQual != NULL)
2704 		ereport(ERROR,
2705 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2706 		/*------
2707 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
2708 				 errmsg("%s is not allowed with HAVING clause",
2709 						LCS_asString(strength))));
2710 	if (qry->hasAggs)
2711 		ereport(ERROR,
2712 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2713 		/*------
2714 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
2715 				 errmsg("%s is not allowed with aggregate functions",
2716 						LCS_asString(strength))));
2717 	if (qry->hasWindowFuncs)
2718 		ereport(ERROR,
2719 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2720 		/*------
2721 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
2722 				 errmsg("%s is not allowed with window functions",
2723 						LCS_asString(strength))));
2724 	if (qry->hasTargetSRFs)
2725 		ereport(ERROR,
2726 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2727 		/*------
2728 		  translator: %s is a SQL row locking clause such as FOR UPDATE */
2729 				 errmsg("%s is not allowed with set-returning functions in the target list",
2730 						LCS_asString(strength))));
2731 }
2732 
2733 /*
2734  * Transform a FOR [KEY] UPDATE/SHARE clause
2735  *
2736  * This basically involves replacing names by integer relids.
2737  *
2738  * NB: if you need to change this, see also markQueryForLocking()
2739  * in rewriteHandler.c, and isLockedRefname() in parse_relation.c.
2740  */
2741 static void
transformLockingClause(ParseState * pstate,Query * qry,LockingClause * lc,bool pushedDown)2742 transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc,
2743 					   bool pushedDown)
2744 {
2745 	List	   *lockedRels = lc->lockedRels;
2746 	ListCell   *l;
2747 	ListCell   *rt;
2748 	Index		i;
2749 	LockingClause *allrels;
2750 
2751 	CheckSelectLocking(qry, lc->strength);
2752 
2753 	/* make a clause we can pass down to subqueries to select all rels */
2754 	allrels = makeNode(LockingClause);
2755 	allrels->lockedRels = NIL;	/* indicates all rels */
2756 	allrels->strength = lc->strength;
2757 	allrels->waitPolicy = lc->waitPolicy;
2758 
2759 	if (lockedRels == NIL)
2760 	{
2761 		/*
2762 		 * Lock all regular tables used in query and its subqueries.  We
2763 		 * examine inFromCl to exclude auto-added RTEs, particularly NEW/OLD
2764 		 * in rules.  This is a bit of an abuse of a mostly-obsolete flag, but
2765 		 * it's convenient.  We can't rely on the namespace mechanism that has
2766 		 * largely replaced inFromCl, since for example we need to lock
2767 		 * base-relation RTEs even if they are masked by upper joins.
2768 		 */
2769 		i = 0;
2770 		foreach(rt, qry->rtable)
2771 		{
2772 			RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2773 
2774 			++i;
2775 			if (!rte->inFromCl)
2776 				continue;
2777 			switch (rte->rtekind)
2778 			{
2779 				case RTE_RELATION:
2780 					applyLockingClause(qry, i, lc->strength, lc->waitPolicy,
2781 									   pushedDown);
2782 					rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
2783 					break;
2784 				case RTE_SUBQUERY:
2785 					applyLockingClause(qry, i, lc->strength, lc->waitPolicy,
2786 									   pushedDown);
2787 
2788 					/*
2789 					 * FOR UPDATE/SHARE of subquery is propagated to all of
2790 					 * subquery's rels, too.  We could do this later (based on
2791 					 * the marking of the subquery RTE) but it is convenient
2792 					 * to have local knowledge in each query level about which
2793 					 * rels need to be opened with RowShareLock.
2794 					 */
2795 					transformLockingClause(pstate, rte->subquery,
2796 										   allrels, true);
2797 					break;
2798 				default:
2799 					/* ignore JOIN, SPECIAL, FUNCTION, VALUES, CTE RTEs */
2800 					break;
2801 			}
2802 		}
2803 	}
2804 	else
2805 	{
2806 		/*
2807 		 * Lock just the named tables.  As above, we allow locking any base
2808 		 * relation regardless of alias-visibility rules, so we need to
2809 		 * examine inFromCl to exclude OLD/NEW.
2810 		 */
2811 		foreach(l, lockedRels)
2812 		{
2813 			RangeVar   *thisrel = (RangeVar *) lfirst(l);
2814 
2815 			/* For simplicity we insist on unqualified alias names here */
2816 			if (thisrel->catalogname || thisrel->schemaname)
2817 				ereport(ERROR,
2818 						(errcode(ERRCODE_SYNTAX_ERROR),
2819 				/*------
2820 				  translator: %s is a SQL row locking clause such as FOR UPDATE */
2821 						 errmsg("%s must specify unqualified relation names",
2822 								LCS_asString(lc->strength)),
2823 						 parser_errposition(pstate, thisrel->location)));
2824 
2825 			i = 0;
2826 			foreach(rt, qry->rtable)
2827 			{
2828 				RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2829 
2830 				++i;
2831 				if (!rte->inFromCl)
2832 					continue;
2833 				if (strcmp(rte->eref->aliasname, thisrel->relname) == 0)
2834 				{
2835 					switch (rte->rtekind)
2836 					{
2837 						case RTE_RELATION:
2838 							applyLockingClause(qry, i, lc->strength,
2839 											   lc->waitPolicy, pushedDown);
2840 							rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
2841 							break;
2842 						case RTE_SUBQUERY:
2843 							applyLockingClause(qry, i, lc->strength,
2844 											   lc->waitPolicy, pushedDown);
2845 							/* see comment above */
2846 							transformLockingClause(pstate, rte->subquery,
2847 												   allrels, true);
2848 							break;
2849 						case RTE_JOIN:
2850 							ereport(ERROR,
2851 									(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2852 							/*------
2853 							  translator: %s is a SQL row locking clause such as FOR UPDATE */
2854 									 errmsg("%s cannot be applied to a join",
2855 											LCS_asString(lc->strength)),
2856 									 parser_errposition(pstate, thisrel->location)));
2857 							break;
2858 						case RTE_FUNCTION:
2859 							ereport(ERROR,
2860 									(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2861 							/*------
2862 							  translator: %s is a SQL row locking clause such as FOR UPDATE */
2863 									 errmsg("%s cannot be applied to a function",
2864 											LCS_asString(lc->strength)),
2865 									 parser_errposition(pstate, thisrel->location)));
2866 							break;
2867 						case RTE_TABLEFUNC:
2868 							ereport(ERROR,
2869 									(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2870 							/*------
2871 							  translator: %s is a SQL row locking clause such as FOR UPDATE */
2872 									 errmsg("%s cannot be applied to a table function",
2873 											LCS_asString(lc->strength)),
2874 									 parser_errposition(pstate, thisrel->location)));
2875 							break;
2876 						case RTE_VALUES:
2877 							ereport(ERROR,
2878 									(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2879 							/*------
2880 							  translator: %s is a SQL row locking clause such as FOR UPDATE */
2881 									 errmsg("%s cannot be applied to VALUES",
2882 											LCS_asString(lc->strength)),
2883 									 parser_errposition(pstate, thisrel->location)));
2884 							break;
2885 						case RTE_CTE:
2886 							ereport(ERROR,
2887 									(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2888 							/*------
2889 							  translator: %s is a SQL row locking clause such as FOR UPDATE */
2890 									 errmsg("%s cannot be applied to a WITH query",
2891 											LCS_asString(lc->strength)),
2892 									 parser_errposition(pstate, thisrel->location)));
2893 							break;
2894 						case RTE_NAMEDTUPLESTORE:
2895 							ereport(ERROR,
2896 									(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2897 							/*------
2898 							  translator: %s is a SQL row locking clause such as FOR UPDATE */
2899 									 errmsg("%s cannot be applied to a named tuplestore",
2900 											LCS_asString(lc->strength)),
2901 									 parser_errposition(pstate, thisrel->location)));
2902 							break;
2903 						default:
2904 							elog(ERROR, "unrecognized RTE type: %d",
2905 								 (int) rte->rtekind);
2906 							break;
2907 					}
2908 					break;		/* out of foreach loop */
2909 				}
2910 			}
2911 			if (rt == NULL)
2912 				ereport(ERROR,
2913 						(errcode(ERRCODE_UNDEFINED_TABLE),
2914 				/*------
2915 				  translator: %s is a SQL row locking clause such as FOR UPDATE */
2916 						 errmsg("relation \"%s\" in %s clause not found in FROM clause",
2917 								thisrel->relname,
2918 								LCS_asString(lc->strength)),
2919 						 parser_errposition(pstate, thisrel->location)));
2920 		}
2921 	}
2922 }
2923 
2924 /*
2925  * Record locking info for a single rangetable item
2926  */
2927 void
applyLockingClause(Query * qry,Index rtindex,LockClauseStrength strength,LockWaitPolicy waitPolicy,bool pushedDown)2928 applyLockingClause(Query *qry, Index rtindex,
2929 				   LockClauseStrength strength, LockWaitPolicy waitPolicy,
2930 				   bool pushedDown)
2931 {
2932 	RowMarkClause *rc;
2933 
2934 	Assert(strength != LCS_NONE);	/* else caller error */
2935 
2936 	/* If it's an explicit clause, make sure hasForUpdate gets set */
2937 	if (!pushedDown)
2938 		qry->hasForUpdate = true;
2939 
2940 	/* Check for pre-existing entry for same rtindex */
2941 	if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
2942 	{
2943 		/*
2944 		 * If the same RTE is specified with more than one locking strength,
2945 		 * use the strongest.  (Reasonable, since you can't take both a shared
2946 		 * and exclusive lock at the same time; it'll end up being exclusive
2947 		 * anyway.)
2948 		 *
2949 		 * Similarly, if the same RTE is specified with more than one lock
2950 		 * wait policy, consider that NOWAIT wins over SKIP LOCKED, which in
2951 		 * turn wins over waiting for the lock (the default).  This is a bit
2952 		 * more debatable but raising an error doesn't seem helpful. (Consider
2953 		 * for instance SELECT FOR UPDATE NOWAIT from a view that internally
2954 		 * contains a plain FOR UPDATE spec.)  Having NOWAIT win over SKIP
2955 		 * LOCKED is reasonable since the former throws an error in case of
2956 		 * coming across a locked tuple, which may be undesirable in some
2957 		 * cases but it seems better than silently returning inconsistent
2958 		 * results.
2959 		 *
2960 		 * And of course pushedDown becomes false if any clause is explicit.
2961 		 */
2962 		rc->strength = Max(rc->strength, strength);
2963 		rc->waitPolicy = Max(rc->waitPolicy, waitPolicy);
2964 		rc->pushedDown &= pushedDown;
2965 		return;
2966 	}
2967 
2968 	/* Make a new RowMarkClause */
2969 	rc = makeNode(RowMarkClause);
2970 	rc->rti = rtindex;
2971 	rc->strength = strength;
2972 	rc->waitPolicy = waitPolicy;
2973 	rc->pushedDown = pushedDown;
2974 	qry->rowMarks = lappend(qry->rowMarks, rc);
2975 }
2976 
2977 /*
2978  * Coverage testing for raw_expression_tree_walker().
2979  *
2980  * When enabled, we run raw_expression_tree_walker() over every DML statement
2981  * submitted to parse analysis.  Without this provision, that function is only
2982  * applied in limited cases involving CTEs, and we don't really want to have
2983  * to test everything inside as well as outside a CTE.
2984  */
2985 #ifdef RAW_EXPRESSION_COVERAGE_TEST
2986 
2987 static bool
test_raw_expression_coverage(Node * node,void * context)2988 test_raw_expression_coverage(Node *node, void *context)
2989 {
2990 	if (node == NULL)
2991 		return false;
2992 	return raw_expression_tree_walker(node,
2993 									  test_raw_expression_coverage,
2994 									  context);
2995 }
2996 
2997 #endif							/* RAW_EXPRESSION_COVERAGE_TEST */
2998