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