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