1 /*-------------------------------------------------------------------------
2  *
3  * parse_target.c
4  *	  handle target lists
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *	  src/backend/parser/parse_target.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "catalog/pg_type.h"
18 #include "commands/dbcommands.h"
19 #include "funcapi.h"
20 #include "miscadmin.h"
21 #include "nodes/makefuncs.h"
22 #include "nodes/nodeFuncs.h"
23 #include "parser/parsetree.h"
24 #include "parser/parse_coerce.h"
25 #include "parser/parse_expr.h"
26 #include "parser/parse_func.h"
27 #include "parser/parse_relation.h"
28 #include "parser/parse_target.h"
29 #include "parser/parse_type.h"
30 #include "utils/builtins.h"
31 #include "utils/lsyscache.h"
32 #include "utils/rel.h"
33 #include "utils/typcache.h"
34 
35 
36 static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle,
37 					 Var *var, int levelsup);
38 static Node *transformAssignmentIndirection(ParseState *pstate,
39 							   Node *basenode,
40 							   const char *targetName,
41 							   bool targetIsArray,
42 							   Oid targetTypeId,
43 							   int32 targetTypMod,
44 							   Oid targetCollation,
45 							   ListCell *indirection,
46 							   Node *rhs,
47 							   int location);
48 static Node *transformAssignmentSubscripts(ParseState *pstate,
49 							  Node *basenode,
50 							  const char *targetName,
51 							  Oid targetTypeId,
52 							  int32 targetTypMod,
53 							  Oid targetCollation,
54 							  List *subscripts,
55 							  bool isSlice,
56 							  ListCell *next_indirection,
57 							  Node *rhs,
58 							  int location);
59 static List *ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref,
60 					bool make_target_entry);
61 static List *ExpandAllTables(ParseState *pstate, int location);
62 static List *ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind,
63 					  bool make_target_entry, ParseExprKind exprKind);
64 static List *ExpandSingleTable(ParseState *pstate, RangeTblEntry *rte,
65 				  int location, bool make_target_entry);
66 static List *ExpandRowReference(ParseState *pstate, Node *expr,
67 				   bool make_target_entry);
68 static int	FigureColnameInternal(Node *node, char **name);
69 
70 
71 /*
72  * transformTargetEntry()
73  *	Transform any ordinary "expression-type" node into a targetlist entry.
74  *	This is exported so that parse_clause.c can generate targetlist entries
75  *	for ORDER/GROUP BY items that are not already in the targetlist.
76  *
77  * node		the (untransformed) parse tree for the value expression.
78  * expr		the transformed expression, or NULL if caller didn't do it yet.
79  * exprKind expression kind (EXPR_KIND_SELECT_TARGET, etc)
80  * colname	the column name to be assigned, or NULL if none yet set.
81  * resjunk	true if the target should be marked resjunk, ie, it is not
82  *			wanted in the final projected tuple.
83  */
84 TargetEntry *
transformTargetEntry(ParseState * pstate,Node * node,Node * expr,ParseExprKind exprKind,char * colname,bool resjunk)85 transformTargetEntry(ParseState *pstate,
86 					 Node *node,
87 					 Node *expr,
88 					 ParseExprKind exprKind,
89 					 char *colname,
90 					 bool resjunk)
91 {
92 	/* Transform the node if caller didn't do it already */
93 	if (expr == NULL)
94 	{
95 		/*
96 		 * If it's a SetToDefault node and we should allow that, pass it
97 		 * through unmodified.  (transformExpr will throw the appropriate
98 		 * error if we're disallowing it.)
99 		 */
100 		if (exprKind == EXPR_KIND_UPDATE_SOURCE && IsA(node, SetToDefault))
101 			expr = node;
102 		else
103 			expr = transformExpr(pstate, node, exprKind);
104 	}
105 
106 	if (colname == NULL && !resjunk)
107 	{
108 		/*
109 		 * Generate a suitable column name for a column without any explicit
110 		 * 'AS ColumnName' clause.
111 		 */
112 		colname = FigureColname(node);
113 	}
114 
115 	return makeTargetEntry((Expr *) expr,
116 						   (AttrNumber) pstate->p_next_resno++,
117 						   colname,
118 						   resjunk);
119 }
120 
121 
122 /*
123  * transformTargetList()
124  * Turns a list of ResTarget's into a list of TargetEntry's.
125  *
126  * This code acts mostly the same for SELECT, UPDATE, or RETURNING lists;
127  * the main thing is to transform the given expressions (the "val" fields).
128  * The exprKind parameter distinguishes these cases when necessary.
129  */
130 List *
transformTargetList(ParseState * pstate,List * targetlist,ParseExprKind exprKind)131 transformTargetList(ParseState *pstate, List *targetlist,
132 					ParseExprKind exprKind)
133 {
134 	List	   *p_target = NIL;
135 	bool		expand_star;
136 	ListCell   *o_target;
137 
138 	/* Shouldn't have any leftover multiassign items at start */
139 	Assert(pstate->p_multiassign_exprs == NIL);
140 
141 	/* Expand "something.*" in SELECT and RETURNING, but not UPDATE */
142 	expand_star = (exprKind != EXPR_KIND_UPDATE_SOURCE);
143 
144 	foreach(o_target, targetlist)
145 	{
146 		ResTarget  *res = (ResTarget *) lfirst(o_target);
147 
148 		/*
149 		 * Check for "something.*".  Depending on the complexity of the
150 		 * "something", the star could appear as the last field in ColumnRef,
151 		 * or as the last indirection item in A_Indirection.
152 		 */
153 		if (expand_star)
154 		{
155 			if (IsA(res->val, ColumnRef))
156 			{
157 				ColumnRef  *cref = (ColumnRef *) res->val;
158 
159 				if (IsA(llast(cref->fields), A_Star))
160 				{
161 					/* It is something.*, expand into multiple items */
162 					p_target = list_concat(p_target,
163 										   ExpandColumnRefStar(pstate,
164 															   cref,
165 															   true));
166 					continue;
167 				}
168 			}
169 			else if (IsA(res->val, A_Indirection))
170 			{
171 				A_Indirection *ind = (A_Indirection *) res->val;
172 
173 				if (IsA(llast(ind->indirection), A_Star))
174 				{
175 					/* It is something.*, expand into multiple items */
176 					p_target = list_concat(p_target,
177 										   ExpandIndirectionStar(pstate,
178 																 ind,
179 																 true,
180 																 exprKind));
181 					continue;
182 				}
183 			}
184 		}
185 
186 		/*
187 		 * Not "something.*", or we want to treat that as a plain whole-row
188 		 * variable, so transform as a single expression
189 		 */
190 		p_target = lappend(p_target,
191 						   transformTargetEntry(pstate,
192 												res->val,
193 												NULL,
194 												exprKind,
195 												res->name,
196 												false));
197 	}
198 
199 	/*
200 	 * If any multiassign resjunk items were created, attach them to the end
201 	 * of the targetlist.  This should only happen in an UPDATE tlist.  We
202 	 * don't need to worry about numbering of these items; transformUpdateStmt
203 	 * will set their resnos.
204 	 */
205 	if (pstate->p_multiassign_exprs)
206 	{
207 		Assert(exprKind == EXPR_KIND_UPDATE_SOURCE);
208 		p_target = list_concat(p_target, pstate->p_multiassign_exprs);
209 		pstate->p_multiassign_exprs = NIL;
210 	}
211 
212 	return p_target;
213 }
214 
215 
216 /*
217  * transformExpressionList()
218  *
219  * This is the identical transformation to transformTargetList, except that
220  * the input list elements are bare expressions without ResTarget decoration,
221  * and the output elements are likewise just expressions without TargetEntry
222  * decoration.  Also, we don't expect any multiassign constructs within the
223  * list, so there's nothing to do for that.  We use this for ROW() and
224  * VALUES() constructs.
225  *
226  * exprKind is not enough to tell us whether to allow SetToDefault, so
227  * an additional flag is needed for that.
228  */
229 List *
transformExpressionList(ParseState * pstate,List * exprlist,ParseExprKind exprKind,bool allowDefault)230 transformExpressionList(ParseState *pstate, List *exprlist,
231 						ParseExprKind exprKind, bool allowDefault)
232 {
233 	List	   *result = NIL;
234 	ListCell   *lc;
235 
236 	foreach(lc, exprlist)
237 	{
238 		Node	   *e = (Node *) lfirst(lc);
239 
240 		/*
241 		 * Check for "something.*".  Depending on the complexity of the
242 		 * "something", the star could appear as the last field in ColumnRef,
243 		 * or as the last indirection item in A_Indirection.
244 		 */
245 		if (IsA(e, ColumnRef))
246 		{
247 			ColumnRef  *cref = (ColumnRef *) e;
248 
249 			if (IsA(llast(cref->fields), A_Star))
250 			{
251 				/* It is something.*, expand into multiple items */
252 				result = list_concat(result,
253 									 ExpandColumnRefStar(pstate, cref,
254 														 false));
255 				continue;
256 			}
257 		}
258 		else if (IsA(e, A_Indirection))
259 		{
260 			A_Indirection *ind = (A_Indirection *) e;
261 
262 			if (IsA(llast(ind->indirection), A_Star))
263 			{
264 				/* It is something.*, expand into multiple items */
265 				result = list_concat(result,
266 									 ExpandIndirectionStar(pstate, ind,
267 														   false, exprKind));
268 				continue;
269 			}
270 		}
271 
272 		/*
273 		 * Not "something.*", so transform as a single expression.  If it's a
274 		 * SetToDefault node and we should allow that, pass it through
275 		 * unmodified.  (transformExpr will throw the appropriate error if
276 		 * we're disallowing it.)
277 		 */
278 		if (allowDefault && IsA(e, SetToDefault))
279 			 /* do nothing */ ;
280 		else
281 			e = transformExpr(pstate, e, exprKind);
282 
283 		result = lappend(result, e);
284 	}
285 
286 	return result;
287 }
288 
289 
290 /*
291  * resolveTargetListUnknowns()
292  *		Convert any unknown-type targetlist entries to type TEXT.
293  *
294  * We do this after we've exhausted all other ways of identifying the output
295  * column types of a query.
296  */
297 void
resolveTargetListUnknowns(ParseState * pstate,List * targetlist)298 resolveTargetListUnknowns(ParseState *pstate, List *targetlist)
299 {
300 	ListCell   *l;
301 
302 	foreach(l, targetlist)
303 	{
304 		TargetEntry *tle = (TargetEntry *) lfirst(l);
305 		Oid			restype = exprType((Node *) tle->expr);
306 
307 		if (restype == UNKNOWNOID)
308 		{
309 			tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
310 											 restype, TEXTOID, -1,
311 											 COERCION_IMPLICIT,
312 											 COERCE_IMPLICIT_CAST,
313 											 -1);
314 		}
315 	}
316 }
317 
318 
319 /*
320  * markTargetListOrigins()
321  *		Mark targetlist columns that are simple Vars with the source
322  *		table's OID and column number.
323  *
324  * Currently, this is done only for SELECT targetlists and RETURNING lists,
325  * since we only need the info if we are going to send it to the frontend.
326  */
327 void
markTargetListOrigins(ParseState * pstate,List * targetlist)328 markTargetListOrigins(ParseState *pstate, List *targetlist)
329 {
330 	ListCell   *l;
331 
332 	foreach(l, targetlist)
333 	{
334 		TargetEntry *tle = (TargetEntry *) lfirst(l);
335 
336 		markTargetListOrigin(pstate, tle, (Var *) tle->expr, 0);
337 	}
338 }
339 
340 /*
341  * markTargetListOrigin()
342  *		If 'var' is a Var of a plain relation, mark 'tle' with its origin
343  *
344  * levelsup is an extra offset to interpret the Var's varlevelsup correctly.
345  *
346  * This is split out so it can recurse for join references.  Note that we
347  * do not drill down into views, but report the view as the column owner.
348  */
349 static void
markTargetListOrigin(ParseState * pstate,TargetEntry * tle,Var * var,int levelsup)350 markTargetListOrigin(ParseState *pstate, TargetEntry *tle,
351 					 Var *var, int levelsup)
352 {
353 	int			netlevelsup;
354 	RangeTblEntry *rte;
355 	AttrNumber	attnum;
356 
357 	if (var == NULL || !IsA(var, Var))
358 		return;
359 	netlevelsup = var->varlevelsup + levelsup;
360 	rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
361 	attnum = var->varattno;
362 
363 	switch (rte->rtekind)
364 	{
365 		case RTE_RELATION:
366 			/* It's a table or view, report it */
367 			tle->resorigtbl = rte->relid;
368 			tle->resorigcol = attnum;
369 			break;
370 		case RTE_SUBQUERY:
371 			/* Subselect-in-FROM: copy up from the subselect */
372 			if (attnum != InvalidAttrNumber)
373 			{
374 				TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
375 													attnum);
376 
377 				if (ste == NULL || ste->resjunk)
378 					elog(ERROR, "subquery %s does not have attribute %d",
379 						 rte->eref->aliasname, attnum);
380 				tle->resorigtbl = ste->resorigtbl;
381 				tle->resorigcol = ste->resorigcol;
382 			}
383 			break;
384 		case RTE_JOIN:
385 			/* Join RTE --- recursively inspect the alias variable */
386 			if (attnum != InvalidAttrNumber)
387 			{
388 				Var		   *aliasvar;
389 
390 				Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
391 				aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
392 				/* We intentionally don't strip implicit coercions here */
393 				markTargetListOrigin(pstate, tle, aliasvar, netlevelsup);
394 			}
395 			break;
396 		case RTE_FUNCTION:
397 		case RTE_VALUES:
398 		case RTE_TABLEFUNC:
399 		case RTE_NAMEDTUPLESTORE:
400 			/* not a simple relation, leave it unmarked */
401 			break;
402 		case RTE_CTE:
403 
404 			/*
405 			 * CTE reference: copy up from the subquery, if possible. If the
406 			 * RTE is a recursive self-reference then we can't do anything
407 			 * because we haven't finished analyzing it yet. However, it's no
408 			 * big loss because we must be down inside the recursive term of a
409 			 * recursive CTE, and so any markings on the current targetlist
410 			 * are not going to affect the results anyway.
411 			 */
412 			if (attnum != InvalidAttrNumber && !rte->self_reference)
413 			{
414 				CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
415 				TargetEntry *ste;
416 
417 				ste = get_tle_by_resno(GetCTETargetList(cte), attnum);
418 				if (ste == NULL || ste->resjunk)
419 					elog(ERROR, "subquery %s does not have attribute %d",
420 						 rte->eref->aliasname, attnum);
421 				tle->resorigtbl = ste->resorigtbl;
422 				tle->resorigcol = ste->resorigcol;
423 			}
424 			break;
425 	}
426 }
427 
428 
429 /*
430  * transformAssignedExpr()
431  *	This is used in INSERT and UPDATE statements only.  It prepares an
432  *	expression for assignment to a column of the target table.
433  *	This includes coercing the given value to the target column's type
434  *	(if necessary), and dealing with any subfield names or subscripts
435  *	attached to the target column itself.  The input expression has
436  *	already been through transformExpr().
437  *
438  * pstate		parse state
439  * expr			expression to be modified
440  * exprKind		indicates which type of statement we're dealing with
441  * colname		target column name (ie, name of attribute to be assigned to)
442  * attrno		target attribute number
443  * indirection	subscripts/field names for target column, if any
444  * location		error cursor position for the target column, or -1
445  *
446  * Returns the modified expression.
447  *
448  * Note: location points at the target column name (SET target or INSERT
449  * column name list entry), and must therefore be -1 in an INSERT that
450  * omits the column name list.  So we should usually prefer to use
451  * exprLocation(expr) for errors that can happen in a default INSERT.
452  */
453 Expr *
transformAssignedExpr(ParseState * pstate,Expr * expr,ParseExprKind exprKind,char * colname,int attrno,List * indirection,int location)454 transformAssignedExpr(ParseState *pstate,
455 					  Expr *expr,
456 					  ParseExprKind exprKind,
457 					  char *colname,
458 					  int attrno,
459 					  List *indirection,
460 					  int location)
461 {
462 	Relation	rd = pstate->p_target_relation;
463 	Oid			type_id;		/* type of value provided */
464 	Oid			attrtype;		/* type of target column */
465 	int32		attrtypmod;
466 	Oid			attrcollation;	/* collation of target column */
467 	ParseExprKind sv_expr_kind;
468 
469 	/*
470 	 * Save and restore identity of expression type we're parsing.  We must
471 	 * set p_expr_kind here because we can parse subscripts without going
472 	 * through transformExpr().
473 	 */
474 	Assert(exprKind != EXPR_KIND_NONE);
475 	sv_expr_kind = pstate->p_expr_kind;
476 	pstate->p_expr_kind = exprKind;
477 
478 	Assert(rd != NULL);
479 	if (attrno <= 0)
480 		ereport(ERROR,
481 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
482 				 errmsg("cannot assign to system column \"%s\"",
483 						colname),
484 				 parser_errposition(pstate, location)));
485 	attrtype = attnumTypeId(rd, attrno);
486 	attrtypmod = rd->rd_att->attrs[attrno - 1]->atttypmod;
487 	attrcollation = rd->rd_att->attrs[attrno - 1]->attcollation;
488 
489 	/*
490 	 * If the expression is a DEFAULT placeholder, insert the attribute's
491 	 * type/typmod/collation into it so that exprType etc will report the
492 	 * right things.  (We expect that the eventually substituted default
493 	 * expression will in fact have this type and typmod.  The collation
494 	 * likely doesn't matter, but let's set it correctly anyway.)  Also,
495 	 * reject trying to update a subfield or array element with DEFAULT, since
496 	 * there can't be any default for portions of a column.
497 	 */
498 	if (expr && IsA(expr, SetToDefault))
499 	{
500 		SetToDefault *def = (SetToDefault *) expr;
501 
502 		def->typeId = attrtype;
503 		def->typeMod = attrtypmod;
504 		def->collation = attrcollation;
505 		if (indirection)
506 		{
507 			if (IsA(linitial(indirection), A_Indices))
508 				ereport(ERROR,
509 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
510 						 errmsg("cannot set an array element to DEFAULT"),
511 						 parser_errposition(pstate, location)));
512 			else
513 				ereport(ERROR,
514 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
515 						 errmsg("cannot set a subfield to DEFAULT"),
516 						 parser_errposition(pstate, location)));
517 		}
518 	}
519 
520 	/* Now we can use exprType() safely. */
521 	type_id = exprType((Node *) expr);
522 
523 	/*
524 	 * If there is indirection on the target column, prepare an array or
525 	 * subfield assignment expression.  This will generate a new column value
526 	 * that the source value has been inserted into, which can then be placed
527 	 * in the new tuple constructed by INSERT or UPDATE.
528 	 */
529 	if (indirection)
530 	{
531 		Node	   *colVar;
532 
533 		if (pstate->p_is_insert)
534 		{
535 			/*
536 			 * The command is INSERT INTO table (col.something) ... so there
537 			 * is not really a source value to work with. Insert a NULL
538 			 * constant as the source value.
539 			 */
540 			colVar = (Node *) makeNullConst(attrtype, attrtypmod,
541 											attrcollation);
542 		}
543 		else
544 		{
545 			/*
546 			 * Build a Var for the column to be updated.
547 			 */
548 			colVar = (Node *) make_var(pstate,
549 									   pstate->p_target_rangetblentry,
550 									   attrno,
551 									   location);
552 		}
553 
554 		expr = (Expr *)
555 			transformAssignmentIndirection(pstate,
556 										   colVar,
557 										   colname,
558 										   false,
559 										   attrtype,
560 										   attrtypmod,
561 										   attrcollation,
562 										   list_head(indirection),
563 										   (Node *) expr,
564 										   location);
565 	}
566 	else
567 	{
568 		/*
569 		 * For normal non-qualified target column, do type checking and
570 		 * coercion.
571 		 */
572 		Node	   *orig_expr = (Node *) expr;
573 
574 		expr = (Expr *)
575 			coerce_to_target_type(pstate,
576 								  orig_expr, type_id,
577 								  attrtype, attrtypmod,
578 								  COERCION_ASSIGNMENT,
579 								  COERCE_IMPLICIT_CAST,
580 								  -1);
581 		if (expr == NULL)
582 			ereport(ERROR,
583 					(errcode(ERRCODE_DATATYPE_MISMATCH),
584 					 errmsg("column \"%s\" is of type %s"
585 							" but expression is of type %s",
586 							colname,
587 							format_type_be(attrtype),
588 							format_type_be(type_id)),
589 					 errhint("You will need to rewrite or cast the expression."),
590 					 parser_errposition(pstate, exprLocation(orig_expr))));
591 	}
592 
593 	pstate->p_expr_kind = sv_expr_kind;
594 
595 	return expr;
596 }
597 
598 
599 /*
600  * updateTargetListEntry()
601  *	This is used in UPDATE statements (and ON CONFLICT DO UPDATE)
602  *	only.  It prepares an UPDATE TargetEntry for assignment to a
603  *	column of the target table.  This includes coercing the given
604  *	value to the target column's type (if necessary), and dealing with
605  *	any subfield names or subscripts attached to the target column
606  *	itself.
607  *
608  * pstate		parse state
609  * tle			target list entry to be modified
610  * colname		target column name (ie, name of attribute to be assigned to)
611  * attrno		target attribute number
612  * indirection	subscripts/field names for target column, if any
613  * location		error cursor position (should point at column name), or -1
614  */
615 void
updateTargetListEntry(ParseState * pstate,TargetEntry * tle,char * colname,int attrno,List * indirection,int location)616 updateTargetListEntry(ParseState *pstate,
617 					  TargetEntry *tle,
618 					  char *colname,
619 					  int attrno,
620 					  List *indirection,
621 					  int location)
622 {
623 	/* Fix up expression as needed */
624 	tle->expr = transformAssignedExpr(pstate,
625 									  tle->expr,
626 									  EXPR_KIND_UPDATE_TARGET,
627 									  colname,
628 									  attrno,
629 									  indirection,
630 									  location);
631 
632 	/*
633 	 * Set the resno to identify the target column --- the rewriter and
634 	 * planner depend on this.  We also set the resname to identify the target
635 	 * column, but this is only for debugging purposes; it should not be
636 	 * relied on.  (In particular, it might be out of date in a stored rule.)
637 	 */
638 	tle->resno = (AttrNumber) attrno;
639 	tle->resname = colname;
640 }
641 
642 
643 /*
644  * Process indirection (field selection or subscripting) of the target
645  * column in INSERT/UPDATE.  This routine recurses for multiple levels
646  * of indirection --- but note that several adjacent A_Indices nodes in
647  * the indirection list are treated as a single multidimensional subscript
648  * operation.
649  *
650  * In the initial call, basenode is a Var for the target column in UPDATE,
651  * or a null Const of the target's type in INSERT.  In recursive calls,
652  * basenode is NULL, indicating that a substitute node should be consed up if
653  * needed.
654  *
655  * targetName is the name of the field or subfield we're assigning to, and
656  * targetIsArray is true if we're subscripting it.  These are just for
657  * error reporting.
658  *
659  * targetTypeId, targetTypMod, targetCollation indicate the datatype and
660  * collation of the object to be assigned to (initially the target column,
661  * later some subobject).
662  *
663  * indirection is the sublist remaining to process.  When it's NULL, we're
664  * done recursing and can just coerce and return the RHS.
665  *
666  * rhs is the already-transformed value to be assigned; note it has not been
667  * coerced to any particular type.
668  *
669  * location is the cursor error position for any errors.  (Note: this points
670  * to the head of the target clause, eg "foo" in "foo.bar[baz]".  Later we
671  * might want to decorate indirection cells with their own location info,
672  * in which case the location argument could probably be dropped.)
673  */
674 static Node *
transformAssignmentIndirection(ParseState * pstate,Node * basenode,const char * targetName,bool targetIsArray,Oid targetTypeId,int32 targetTypMod,Oid targetCollation,ListCell * indirection,Node * rhs,int location)675 transformAssignmentIndirection(ParseState *pstate,
676 							   Node *basenode,
677 							   const char *targetName,
678 							   bool targetIsArray,
679 							   Oid targetTypeId,
680 							   int32 targetTypMod,
681 							   Oid targetCollation,
682 							   ListCell *indirection,
683 							   Node *rhs,
684 							   int location)
685 {
686 	Node	   *result;
687 	List	   *subscripts = NIL;
688 	bool		isSlice = false;
689 	ListCell   *i;
690 
691 	if (indirection && !basenode)
692 	{
693 		/* Set up a substitution.  We reuse CaseTestExpr for this. */
694 		CaseTestExpr *ctest = makeNode(CaseTestExpr);
695 
696 		ctest->typeId = targetTypeId;
697 		ctest->typeMod = targetTypMod;
698 		ctest->collation = targetCollation;
699 		basenode = (Node *) ctest;
700 	}
701 
702 	/*
703 	 * We have to split any field-selection operations apart from
704 	 * subscripting.  Adjacent A_Indices nodes have to be treated as a single
705 	 * multidimensional subscript operation.
706 	 */
707 	for_each_cell(i, indirection)
708 	{
709 		Node	   *n = lfirst(i);
710 
711 		if (IsA(n, A_Indices))
712 		{
713 			subscripts = lappend(subscripts, n);
714 			if (((A_Indices *) n)->is_slice)
715 				isSlice = true;
716 		}
717 		else if (IsA(n, A_Star))
718 		{
719 			ereport(ERROR,
720 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
721 					 errmsg("row expansion via \"*\" is not supported here"),
722 					 parser_errposition(pstate, location)));
723 		}
724 		else
725 		{
726 			FieldStore *fstore;
727 			Oid			typrelid;
728 			AttrNumber	attnum;
729 			Oid			fieldTypeId;
730 			int32		fieldTypMod;
731 			Oid			fieldCollation;
732 
733 			Assert(IsA(n, String));
734 
735 			/* process subscripts before this field selection */
736 			if (subscripts)
737 			{
738 				/* recurse, and then return because we're done */
739 				return transformAssignmentSubscripts(pstate,
740 													 basenode,
741 													 targetName,
742 													 targetTypeId,
743 													 targetTypMod,
744 													 targetCollation,
745 													 subscripts,
746 													 isSlice,
747 													 i,
748 													 rhs,
749 													 location);
750 			}
751 
752 			/* No subscripts, so can process field selection here */
753 
754 			typrelid = typeidTypeRelid(targetTypeId);
755 			if (!typrelid)
756 				ereport(ERROR,
757 						(errcode(ERRCODE_DATATYPE_MISMATCH),
758 						 errmsg("cannot assign to field \"%s\" of column \"%s\" because its type %s is not a composite type",
759 								strVal(n), targetName,
760 								format_type_be(targetTypeId)),
761 						 parser_errposition(pstate, location)));
762 
763 			attnum = get_attnum(typrelid, strVal(n));
764 			if (attnum == InvalidAttrNumber)
765 				ereport(ERROR,
766 						(errcode(ERRCODE_UNDEFINED_COLUMN),
767 						 errmsg("cannot assign to field \"%s\" of column \"%s\" because there is no such column in data type %s",
768 								strVal(n), targetName,
769 								format_type_be(targetTypeId)),
770 						 parser_errposition(pstate, location)));
771 			if (attnum < 0)
772 				ereport(ERROR,
773 						(errcode(ERRCODE_UNDEFINED_COLUMN),
774 						 errmsg("cannot assign to system column \"%s\"",
775 								strVal(n)),
776 						 parser_errposition(pstate, location)));
777 
778 			get_atttypetypmodcoll(typrelid, attnum,
779 								  &fieldTypeId, &fieldTypMod, &fieldCollation);
780 
781 			/* recurse to create appropriate RHS for field assign */
782 			rhs = transformAssignmentIndirection(pstate,
783 												 NULL,
784 												 strVal(n),
785 												 false,
786 												 fieldTypeId,
787 												 fieldTypMod,
788 												 fieldCollation,
789 												 lnext(i),
790 												 rhs,
791 												 location);
792 
793 			/* and build a FieldStore node */
794 			fstore = makeNode(FieldStore);
795 			fstore->arg = (Expr *) basenode;
796 			fstore->newvals = list_make1(rhs);
797 			fstore->fieldnums = list_make1_int(attnum);
798 			fstore->resulttype = targetTypeId;
799 
800 			return (Node *) fstore;
801 		}
802 	}
803 
804 	/* process trailing subscripts, if any */
805 	if (subscripts)
806 	{
807 		/* recurse, and then return because we're done */
808 		return transformAssignmentSubscripts(pstate,
809 											 basenode,
810 											 targetName,
811 											 targetTypeId,
812 											 targetTypMod,
813 											 targetCollation,
814 											 subscripts,
815 											 isSlice,
816 											 NULL,
817 											 rhs,
818 											 location);
819 	}
820 
821 	/* base case: just coerce RHS to match target type ID */
822 
823 	result = coerce_to_target_type(pstate,
824 								   rhs, exprType(rhs),
825 								   targetTypeId, targetTypMod,
826 								   COERCION_ASSIGNMENT,
827 								   COERCE_IMPLICIT_CAST,
828 								   -1);
829 	if (result == NULL)
830 	{
831 		if (targetIsArray)
832 			ereport(ERROR,
833 					(errcode(ERRCODE_DATATYPE_MISMATCH),
834 					 errmsg("array assignment to \"%s\" requires type %s"
835 							" but expression is of type %s",
836 							targetName,
837 							format_type_be(targetTypeId),
838 							format_type_be(exprType(rhs))),
839 					 errhint("You will need to rewrite or cast the expression."),
840 					 parser_errposition(pstate, location)));
841 		else
842 			ereport(ERROR,
843 					(errcode(ERRCODE_DATATYPE_MISMATCH),
844 					 errmsg("subfield \"%s\" is of type %s"
845 							" but expression is of type %s",
846 							targetName,
847 							format_type_be(targetTypeId),
848 							format_type_be(exprType(rhs))),
849 					 errhint("You will need to rewrite or cast the expression."),
850 					 parser_errposition(pstate, location)));
851 	}
852 
853 	return result;
854 }
855 
856 /*
857  * helper for transformAssignmentIndirection: process array assignment
858  */
859 static Node *
transformAssignmentSubscripts(ParseState * pstate,Node * basenode,const char * targetName,Oid targetTypeId,int32 targetTypMod,Oid targetCollation,List * subscripts,bool isSlice,ListCell * next_indirection,Node * rhs,int location)860 transformAssignmentSubscripts(ParseState *pstate,
861 							  Node *basenode,
862 							  const char *targetName,
863 							  Oid targetTypeId,
864 							  int32 targetTypMod,
865 							  Oid targetCollation,
866 							  List *subscripts,
867 							  bool isSlice,
868 							  ListCell *next_indirection,
869 							  Node *rhs,
870 							  int location)
871 {
872 	Node	   *result;
873 	Oid			arrayType;
874 	int32		arrayTypMod;
875 	Oid			elementTypeId;
876 	Oid			typeNeeded;
877 	Oid			collationNeeded;
878 
879 	Assert(subscripts != NIL);
880 
881 	/* Identify the actual array type and element type involved */
882 	arrayType = targetTypeId;
883 	arrayTypMod = targetTypMod;
884 	elementTypeId = transformArrayType(&arrayType, &arrayTypMod);
885 
886 	/* Identify type that RHS must provide */
887 	typeNeeded = isSlice ? arrayType : elementTypeId;
888 
889 	/*
890 	 * Array normally has same collation as elements, but there's an
891 	 * exception: we might be subscripting a domain over an array type. In
892 	 * that case use collation of the base type.
893 	 */
894 	if (arrayType == targetTypeId)
895 		collationNeeded = targetCollation;
896 	else
897 		collationNeeded = get_typcollation(arrayType);
898 
899 	/* recurse to create appropriate RHS for array assign */
900 	rhs = transformAssignmentIndirection(pstate,
901 										 NULL,
902 										 targetName,
903 										 true,
904 										 typeNeeded,
905 										 arrayTypMod,
906 										 collationNeeded,
907 										 next_indirection,
908 										 rhs,
909 										 location);
910 
911 	/* process subscripts */
912 	result = (Node *) transformArraySubscripts(pstate,
913 											   basenode,
914 											   arrayType,
915 											   elementTypeId,
916 											   arrayTypMod,
917 											   subscripts,
918 											   rhs);
919 
920 	/* If target was a domain over array, need to coerce up to the domain */
921 	if (arrayType != targetTypeId)
922 	{
923 		Oid			resulttype = exprType(result);
924 
925 		result = coerce_to_target_type(pstate,
926 									   result, resulttype,
927 									   targetTypeId, targetTypMod,
928 									   COERCION_ASSIGNMENT,
929 									   COERCE_IMPLICIT_CAST,
930 									   -1);
931 		/* can fail if we had int2vector/oidvector, but not for true domains */
932 		if (result == NULL)
933 			ereport(ERROR,
934 					(errcode(ERRCODE_CANNOT_COERCE),
935 					 errmsg("cannot cast type %s to %s",
936 							format_type_be(resulttype),
937 							format_type_be(targetTypeId)),
938 					 parser_errposition(pstate, location)));
939 	}
940 
941 	return result;
942 }
943 
944 
945 /*
946  * checkInsertTargets -
947  *	  generate a list of INSERT column targets if not supplied, or
948  *	  test supplied column names to make sure they are in target table.
949  *	  Also return an integer list of the columns' attribute numbers.
950  */
951 List *
checkInsertTargets(ParseState * pstate,List * cols,List ** attrnos)952 checkInsertTargets(ParseState *pstate, List *cols, List **attrnos)
953 {
954 	*attrnos = NIL;
955 
956 	if (cols == NIL)
957 	{
958 		/*
959 		 * Generate default column list for INSERT.
960 		 */
961 		Form_pg_attribute *attr = pstate->p_target_relation->rd_att->attrs;
962 		int			numcol = pstate->p_target_relation->rd_rel->relnatts;
963 		int			i;
964 
965 		for (i = 0; i < numcol; i++)
966 		{
967 			ResTarget  *col;
968 
969 			if (attr[i]->attisdropped)
970 				continue;
971 
972 			col = makeNode(ResTarget);
973 			col->name = pstrdup(NameStr(attr[i]->attname));
974 			col->indirection = NIL;
975 			col->val = NULL;
976 			col->location = -1;
977 			cols = lappend(cols, col);
978 			*attrnos = lappend_int(*attrnos, i + 1);
979 		}
980 	}
981 	else
982 	{
983 		/*
984 		 * Do initial validation of user-supplied INSERT column list.
985 		 */
986 		Bitmapset  *wholecols = NULL;
987 		Bitmapset  *partialcols = NULL;
988 		ListCell   *tl;
989 
990 		foreach(tl, cols)
991 		{
992 			ResTarget  *col = (ResTarget *) lfirst(tl);
993 			char	   *name = col->name;
994 			int			attrno;
995 
996 			/* Lookup column name, ereport on failure */
997 			attrno = attnameAttNum(pstate->p_target_relation, name, false);
998 			if (attrno == InvalidAttrNumber)
999 				ereport(ERROR,
1000 						(errcode(ERRCODE_UNDEFINED_COLUMN),
1001 						 errmsg("column \"%s\" of relation \"%s\" does not exist",
1002 								name,
1003 								RelationGetRelationName(pstate->p_target_relation)),
1004 						 parser_errposition(pstate, col->location)));
1005 
1006 			/*
1007 			 * Check for duplicates, but only of whole columns --- we allow
1008 			 * INSERT INTO foo (col.subcol1, col.subcol2)
1009 			 */
1010 			if (col->indirection == NIL)
1011 			{
1012 				/* whole column; must not have any other assignment */
1013 				if (bms_is_member(attrno, wholecols) ||
1014 					bms_is_member(attrno, partialcols))
1015 					ereport(ERROR,
1016 							(errcode(ERRCODE_DUPLICATE_COLUMN),
1017 							 errmsg("column \"%s\" specified more than once",
1018 									name),
1019 							 parser_errposition(pstate, col->location)));
1020 				wholecols = bms_add_member(wholecols, attrno);
1021 			}
1022 			else
1023 			{
1024 				/* partial column; must not have any whole assignment */
1025 				if (bms_is_member(attrno, wholecols))
1026 					ereport(ERROR,
1027 							(errcode(ERRCODE_DUPLICATE_COLUMN),
1028 							 errmsg("column \"%s\" specified more than once",
1029 									name),
1030 							 parser_errposition(pstate, col->location)));
1031 				partialcols = bms_add_member(partialcols, attrno);
1032 			}
1033 
1034 			*attrnos = lappend_int(*attrnos, attrno);
1035 		}
1036 	}
1037 
1038 	return cols;
1039 }
1040 
1041 /*
1042  * ExpandColumnRefStar()
1043  *		Transforms foo.* into a list of expressions or targetlist entries.
1044  *
1045  * This handles the case where '*' appears as the last or only item in a
1046  * ColumnRef.  The code is shared between the case of foo.* at the top level
1047  * in a SELECT target list (where we want TargetEntry nodes in the result)
1048  * and foo.* in a ROW() or VALUES() construct (where we want just bare
1049  * expressions).
1050  *
1051  * The referenced columns are marked as requiring SELECT access.
1052  */
1053 static List *
ExpandColumnRefStar(ParseState * pstate,ColumnRef * cref,bool make_target_entry)1054 ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref,
1055 					bool make_target_entry)
1056 {
1057 	List	   *fields = cref->fields;
1058 	int			numnames = list_length(fields);
1059 
1060 	if (numnames == 1)
1061 	{
1062 		/*
1063 		 * Target item is a bare '*', expand all tables
1064 		 *
1065 		 * (e.g., SELECT * FROM emp, dept)
1066 		 *
1067 		 * Since the grammar only accepts bare '*' at top level of SELECT, we
1068 		 * need not handle the make_target_entry==false case here.
1069 		 */
1070 		Assert(make_target_entry);
1071 		return ExpandAllTables(pstate, cref->location);
1072 	}
1073 	else
1074 	{
1075 		/*
1076 		 * Target item is relation.*, expand that table
1077 		 *
1078 		 * (e.g., SELECT emp.*, dname FROM emp, dept)
1079 		 *
1080 		 * Note: this code is a lot like transformColumnRef; it's tempting to
1081 		 * call that instead and then replace the resulting whole-row Var with
1082 		 * a list of Vars.  However, that would leave us with the RTE's
1083 		 * selectedCols bitmap showing the whole row as needing select
1084 		 * permission, as well as the individual columns.  That would be
1085 		 * incorrect (since columns added later shouldn't need select
1086 		 * permissions).  We could try to remove the whole-row permission bit
1087 		 * after the fact, but duplicating code is less messy.
1088 		 */
1089 		char	   *nspname = NULL;
1090 		char	   *relname = NULL;
1091 		RangeTblEntry *rte = NULL;
1092 		int			levels_up;
1093 		enum
1094 		{
1095 			CRSERR_NO_RTE,
1096 			CRSERR_WRONG_DB,
1097 			CRSERR_TOO_MANY
1098 		}			crserr = CRSERR_NO_RTE;
1099 
1100 		/*
1101 		 * Give the PreParseColumnRefHook, if any, first shot.  If it returns
1102 		 * non-null then we should use that expression.
1103 		 */
1104 		if (pstate->p_pre_columnref_hook != NULL)
1105 		{
1106 			Node	   *node;
1107 
1108 			node = (*pstate->p_pre_columnref_hook) (pstate, cref);
1109 			if (node != NULL)
1110 				return ExpandRowReference(pstate, node, make_target_entry);
1111 		}
1112 
1113 		switch (numnames)
1114 		{
1115 			case 2:
1116 				relname = strVal(linitial(fields));
1117 				rte = refnameRangeTblEntry(pstate, nspname, relname,
1118 										   cref->location,
1119 										   &levels_up);
1120 				break;
1121 			case 3:
1122 				nspname = strVal(linitial(fields));
1123 				relname = strVal(lsecond(fields));
1124 				rte = refnameRangeTblEntry(pstate, nspname, relname,
1125 										   cref->location,
1126 										   &levels_up);
1127 				break;
1128 			case 4:
1129 				{
1130 					char	   *catname = strVal(linitial(fields));
1131 
1132 					/*
1133 					 * We check the catalog name and then ignore it.
1134 					 */
1135 					if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
1136 					{
1137 						crserr = CRSERR_WRONG_DB;
1138 						break;
1139 					}
1140 					nspname = strVal(lsecond(fields));
1141 					relname = strVal(lthird(fields));
1142 					rte = refnameRangeTblEntry(pstate, nspname, relname,
1143 											   cref->location,
1144 											   &levels_up);
1145 					break;
1146 				}
1147 			default:
1148 				crserr = CRSERR_TOO_MANY;
1149 				break;
1150 		}
1151 
1152 		/*
1153 		 * Now give the PostParseColumnRefHook, if any, a chance. We cheat a
1154 		 * bit by passing the RangeTblEntry, not a Var, as the planned
1155 		 * translation.  (A single Var wouldn't be strictly correct anyway.
1156 		 * This convention allows hooks that really care to know what is
1157 		 * happening.)
1158 		 */
1159 		if (pstate->p_post_columnref_hook != NULL)
1160 		{
1161 			Node	   *node;
1162 
1163 			node = (*pstate->p_post_columnref_hook) (pstate, cref,
1164 													 (Node *) rte);
1165 			if (node != NULL)
1166 			{
1167 				if (rte != NULL)
1168 					ereport(ERROR,
1169 							(errcode(ERRCODE_AMBIGUOUS_COLUMN),
1170 							 errmsg("column reference \"%s\" is ambiguous",
1171 									NameListToString(cref->fields)),
1172 							 parser_errposition(pstate, cref->location)));
1173 				return ExpandRowReference(pstate, node, make_target_entry);
1174 			}
1175 		}
1176 
1177 		/*
1178 		 * Throw error if no translation found.
1179 		 */
1180 		if (rte == NULL)
1181 		{
1182 			switch (crserr)
1183 			{
1184 				case CRSERR_NO_RTE:
1185 					errorMissingRTE(pstate, makeRangeVar(nspname, relname,
1186 														 cref->location));
1187 					break;
1188 				case CRSERR_WRONG_DB:
1189 					ereport(ERROR,
1190 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1191 							 errmsg("cross-database references are not implemented: %s",
1192 									NameListToString(cref->fields)),
1193 							 parser_errposition(pstate, cref->location)));
1194 					break;
1195 				case CRSERR_TOO_MANY:
1196 					ereport(ERROR,
1197 							(errcode(ERRCODE_SYNTAX_ERROR),
1198 							 errmsg("improper qualified name (too many dotted names): %s",
1199 									NameListToString(cref->fields)),
1200 							 parser_errposition(pstate, cref->location)));
1201 					break;
1202 			}
1203 		}
1204 
1205 		/*
1206 		 * OK, expand the RTE into fields.
1207 		 */
1208 		return ExpandSingleTable(pstate, rte, cref->location, make_target_entry);
1209 	}
1210 }
1211 
1212 /*
1213  * ExpandAllTables()
1214  *		Transforms '*' (in the target list) into a list of targetlist entries.
1215  *
1216  * tlist entries are generated for each relation visible for unqualified
1217  * column name access.  We do not consider qualified-name-only entries because
1218  * that would include input tables of aliasless JOINs, NEW/OLD pseudo-entries,
1219  * etc.
1220  *
1221  * The referenced relations/columns are marked as requiring SELECT access.
1222  */
1223 static List *
ExpandAllTables(ParseState * pstate,int location)1224 ExpandAllTables(ParseState *pstate, int location)
1225 {
1226 	List	   *target = NIL;
1227 	bool		found_table = false;
1228 	ListCell   *l;
1229 
1230 	foreach(l, pstate->p_namespace)
1231 	{
1232 		ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(l);
1233 		RangeTblEntry *rte = nsitem->p_rte;
1234 
1235 		/* Ignore table-only items */
1236 		if (!nsitem->p_cols_visible)
1237 			continue;
1238 		/* Should not have any lateral-only items when parsing targetlist */
1239 		Assert(!nsitem->p_lateral_only);
1240 		/* Remember we found a p_cols_visible item */
1241 		found_table = true;
1242 
1243 		target = list_concat(target,
1244 							 expandRelAttrs(pstate,
1245 											rte,
1246 											RTERangeTablePosn(pstate, rte,
1247 															  NULL),
1248 											0,
1249 											location));
1250 	}
1251 
1252 	/*
1253 	 * Check for "SELECT *;".  We do it this way, rather than checking for
1254 	 * target == NIL, because we want to allow SELECT * FROM a zero_column
1255 	 * table.
1256 	 */
1257 	if (!found_table)
1258 		ereport(ERROR,
1259 				(errcode(ERRCODE_SYNTAX_ERROR),
1260 				 errmsg("SELECT * with no tables specified is not valid"),
1261 				 parser_errposition(pstate, location)));
1262 
1263 	return target;
1264 }
1265 
1266 /*
1267  * ExpandIndirectionStar()
1268  *		Transforms foo.* into a list of expressions or targetlist entries.
1269  *
1270  * This handles the case where '*' appears as the last item in A_Indirection.
1271  * The code is shared between the case of foo.* at the top level in a SELECT
1272  * target list (where we want TargetEntry nodes in the result) and foo.* in
1273  * a ROW() or VALUES() construct (where we want just bare expressions).
1274  * For robustness, we use a separate "make_target_entry" flag to control
1275  * this rather than relying on exprKind.
1276  */
1277 static List *
ExpandIndirectionStar(ParseState * pstate,A_Indirection * ind,bool make_target_entry,ParseExprKind exprKind)1278 ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind,
1279 					  bool make_target_entry, ParseExprKind exprKind)
1280 {
1281 	Node	   *expr;
1282 
1283 	/* Strip off the '*' to create a reference to the rowtype object */
1284 	ind = copyObject(ind);
1285 	ind->indirection = list_truncate(ind->indirection,
1286 									 list_length(ind->indirection) - 1);
1287 
1288 	/* And transform that */
1289 	expr = transformExpr(pstate, (Node *) ind, exprKind);
1290 
1291 	/* Expand the rowtype expression into individual fields */
1292 	return ExpandRowReference(pstate, expr, make_target_entry);
1293 }
1294 
1295 /*
1296  * ExpandSingleTable()
1297  *		Transforms foo.* into a list of expressions or targetlist entries.
1298  *
1299  * This handles the case where foo has been determined to be a simple
1300  * reference to an RTE, so we can just generate Vars for the expressions.
1301  *
1302  * The referenced columns are marked as requiring SELECT access.
1303  */
1304 static List *
ExpandSingleTable(ParseState * pstate,RangeTblEntry * rte,int location,bool make_target_entry)1305 ExpandSingleTable(ParseState *pstate, RangeTblEntry *rte,
1306 				  int location, bool make_target_entry)
1307 {
1308 	int			sublevels_up;
1309 	int			rtindex;
1310 
1311 	rtindex = RTERangeTablePosn(pstate, rte, &sublevels_up);
1312 
1313 	if (make_target_entry)
1314 	{
1315 		/* expandRelAttrs handles permissions marking */
1316 		return expandRelAttrs(pstate, rte, rtindex, sublevels_up,
1317 							  location);
1318 	}
1319 	else
1320 	{
1321 		List	   *vars;
1322 		ListCell   *l;
1323 
1324 		expandRTE(rte, rtindex, sublevels_up, location, false,
1325 				  NULL, &vars);
1326 
1327 		/*
1328 		 * Require read access to the table.  This is normally redundant with
1329 		 * the markVarForSelectPriv calls below, but not if the table has zero
1330 		 * columns.
1331 		 */
1332 		rte->requiredPerms |= ACL_SELECT;
1333 
1334 		/* Require read access to each column */
1335 		foreach(l, vars)
1336 		{
1337 			Var		   *var = (Var *) lfirst(l);
1338 
1339 			markVarForSelectPriv(pstate, var, rte);
1340 		}
1341 
1342 		return vars;
1343 	}
1344 }
1345 
1346 /*
1347  * ExpandRowReference()
1348  *		Transforms foo.* into a list of expressions or targetlist entries.
1349  *
1350  * This handles the case where foo is an arbitrary expression of composite
1351  * type.
1352  */
1353 static List *
ExpandRowReference(ParseState * pstate,Node * expr,bool make_target_entry)1354 ExpandRowReference(ParseState *pstate, Node *expr,
1355 				   bool make_target_entry)
1356 {
1357 	List	   *result = NIL;
1358 	TupleDesc	tupleDesc;
1359 	int			numAttrs;
1360 	int			i;
1361 
1362 	/*
1363 	 * If the rowtype expression is a whole-row Var, we can expand the fields
1364 	 * as simple Vars.  Note: if the RTE is a relation, this case leaves us
1365 	 * with the RTE's selectedCols bitmap showing the whole row as needing
1366 	 * select permission, as well as the individual columns.  However, we can
1367 	 * only get here for weird notations like (table.*).*, so it's not worth
1368 	 * trying to clean up --- arguably, the permissions marking is correct
1369 	 * anyway for such cases.
1370 	 */
1371 	if (IsA(expr, Var) &&
1372 		((Var *) expr)->varattno == InvalidAttrNumber)
1373 	{
1374 		Var		   *var = (Var *) expr;
1375 		RangeTblEntry *rte;
1376 
1377 		rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1378 		return ExpandSingleTable(pstate, rte, var->location, make_target_entry);
1379 	}
1380 
1381 	/*
1382 	 * Otherwise we have to do it the hard way.  Our current implementation is
1383 	 * to generate multiple copies of the expression and do FieldSelects.
1384 	 * (This can be pretty inefficient if the expression involves nontrivial
1385 	 * computation :-(.)
1386 	 *
1387 	 * Verify it's a composite type, and get the tupdesc.  We use
1388 	 * get_expr_result_type() because that can handle references to functions
1389 	 * returning anonymous record types.  If that fails, use
1390 	 * lookup_rowtype_tupdesc(), which will almost certainly fail as well, but
1391 	 * it will give an appropriate error message.
1392 	 *
1393 	 * If it's a Var of type RECORD, we have to work even harder: we have to
1394 	 * find what the Var refers to, and pass that to get_expr_result_type.
1395 	 * That task is handled by expandRecordVariable().
1396 	 */
1397 	if (IsA(expr, Var) &&
1398 		((Var *) expr)->vartype == RECORDOID)
1399 		tupleDesc = expandRecordVariable(pstate, (Var *) expr, 0);
1400 	else if (get_expr_result_type(expr, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
1401 		tupleDesc = lookup_rowtype_tupdesc_copy(exprType(expr),
1402 												exprTypmod(expr));
1403 	Assert(tupleDesc);
1404 
1405 	/* Generate a list of references to the individual fields */
1406 	numAttrs = tupleDesc->natts;
1407 	for (i = 0; i < numAttrs; i++)
1408 	{
1409 		Form_pg_attribute att = tupleDesc->attrs[i];
1410 		FieldSelect *fselect;
1411 
1412 		if (att->attisdropped)
1413 			continue;
1414 
1415 		fselect = makeNode(FieldSelect);
1416 		fselect->arg = (Expr *) copyObject(expr);
1417 		fselect->fieldnum = i + 1;
1418 		fselect->resulttype = att->atttypid;
1419 		fselect->resulttypmod = att->atttypmod;
1420 		/* save attribute's collation for parse_collate.c */
1421 		fselect->resultcollid = att->attcollation;
1422 
1423 		if (make_target_entry)
1424 		{
1425 			/* add TargetEntry decoration */
1426 			TargetEntry *te;
1427 
1428 			te = makeTargetEntry((Expr *) fselect,
1429 								 (AttrNumber) pstate->p_next_resno++,
1430 								 pstrdup(NameStr(att->attname)),
1431 								 false);
1432 			result = lappend(result, te);
1433 		}
1434 		else
1435 			result = lappend(result, fselect);
1436 	}
1437 
1438 	return result;
1439 }
1440 
1441 /*
1442  * expandRecordVariable
1443  *		Get the tuple descriptor for a Var of type RECORD, if possible.
1444  *
1445  * Since no actual table or view column is allowed to have type RECORD, such
1446  * a Var must refer to a JOIN or FUNCTION RTE or to a subquery output.  We
1447  * drill down to find the ultimate defining expression and attempt to infer
1448  * the tupdesc from it.  We ereport if we can't determine the tupdesc.
1449  *
1450  * levelsup is an extra offset to interpret the Var's varlevelsup correctly.
1451  */
1452 TupleDesc
expandRecordVariable(ParseState * pstate,Var * var,int levelsup)1453 expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
1454 {
1455 	TupleDesc	tupleDesc;
1456 	int			netlevelsup;
1457 	RangeTblEntry *rte;
1458 	AttrNumber	attnum;
1459 	Node	   *expr;
1460 
1461 	/* Check my caller didn't mess up */
1462 	Assert(IsA(var, Var));
1463 	Assert(var->vartype == RECORDOID);
1464 
1465 	netlevelsup = var->varlevelsup + levelsup;
1466 	rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
1467 	attnum = var->varattno;
1468 
1469 	if (attnum == InvalidAttrNumber)
1470 	{
1471 		/* Whole-row reference to an RTE, so expand the known fields */
1472 		List	   *names,
1473 				   *vars;
1474 		ListCell   *lname,
1475 				   *lvar;
1476 		int			i;
1477 
1478 		expandRTE(rte, var->varno, 0, var->location, false,
1479 				  &names, &vars);
1480 
1481 		tupleDesc = CreateTemplateTupleDesc(list_length(vars), false);
1482 		i = 1;
1483 		forboth(lname, names, lvar, vars)
1484 		{
1485 			char	   *label = strVal(lfirst(lname));
1486 			Node	   *varnode = (Node *) lfirst(lvar);
1487 
1488 			TupleDescInitEntry(tupleDesc, i,
1489 							   label,
1490 							   exprType(varnode),
1491 							   exprTypmod(varnode),
1492 							   0);
1493 			TupleDescInitEntryCollation(tupleDesc, i,
1494 										exprCollation(varnode));
1495 			i++;
1496 		}
1497 		Assert(lname == NULL && lvar == NULL);	/* lists same length? */
1498 
1499 		return tupleDesc;
1500 	}
1501 
1502 	expr = (Node *) var;		/* default if we can't drill down */
1503 
1504 	switch (rte->rtekind)
1505 	{
1506 		case RTE_RELATION:
1507 		case RTE_VALUES:
1508 		case RTE_NAMEDTUPLESTORE:
1509 
1510 			/*
1511 			 * This case should not occur: a column of a table, values list,
1512 			 * or ENR shouldn't have type RECORD.  Fall through and fail (most
1513 			 * likely) at the bottom.
1514 			 */
1515 			break;
1516 		case RTE_SUBQUERY:
1517 			{
1518 				/* Subselect-in-FROM: examine sub-select's output expr */
1519 				TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
1520 													attnum);
1521 
1522 				if (ste == NULL || ste->resjunk)
1523 					elog(ERROR, "subquery %s does not have attribute %d",
1524 						 rte->eref->aliasname, attnum);
1525 				expr = (Node *) ste->expr;
1526 				if (IsA(expr, Var))
1527 				{
1528 					/*
1529 					 * Recurse into the sub-select to see what its Var refers
1530 					 * to.  We have to build an additional level of ParseState
1531 					 * to keep in step with varlevelsup in the subselect.
1532 					 */
1533 					ParseState	mypstate;
1534 
1535 					MemSet(&mypstate, 0, sizeof(mypstate));
1536 					mypstate.parentParseState = pstate;
1537 					mypstate.p_rtable = rte->subquery->rtable;
1538 					/* don't bother filling the rest of the fake pstate */
1539 
1540 					return expandRecordVariable(&mypstate, (Var *) expr, 0);
1541 				}
1542 				/* else fall through to inspect the expression */
1543 			}
1544 			break;
1545 		case RTE_JOIN:
1546 			/* Join RTE --- recursively inspect the alias variable */
1547 			Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
1548 			expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1549 			Assert(expr != NULL);
1550 			/* We intentionally don't strip implicit coercions here */
1551 			if (IsA(expr, Var))
1552 				return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
1553 			/* else fall through to inspect the expression */
1554 			break;
1555 		case RTE_FUNCTION:
1556 
1557 			/*
1558 			 * We couldn't get here unless a function is declared with one of
1559 			 * its result columns as RECORD, which is not allowed.
1560 			 */
1561 			break;
1562 		case RTE_TABLEFUNC:
1563 
1564 			/*
1565 			 * Table function cannot have columns with RECORD type.
1566 			 */
1567 			break;
1568 		case RTE_CTE:
1569 			/* CTE reference: examine subquery's output expr */
1570 			if (!rte->self_reference)
1571 			{
1572 				CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
1573 				TargetEntry *ste;
1574 
1575 				ste = get_tle_by_resno(GetCTETargetList(cte), attnum);
1576 				if (ste == NULL || ste->resjunk)
1577 					elog(ERROR, "subquery %s does not have attribute %d",
1578 						 rte->eref->aliasname, attnum);
1579 				expr = (Node *) ste->expr;
1580 				if (IsA(expr, Var))
1581 				{
1582 					/*
1583 					 * Recurse into the CTE to see what its Var refers to. We
1584 					 * have to build an additional level of ParseState to keep
1585 					 * in step with varlevelsup in the CTE; furthermore it
1586 					 * could be an outer CTE.
1587 					 */
1588 					ParseState	mypstate;
1589 					Index		levelsup;
1590 
1591 					MemSet(&mypstate, 0, sizeof(mypstate));
1592 					/* this loop must work, since GetCTEForRTE did */
1593 					for (levelsup = 0;
1594 						 levelsup < rte->ctelevelsup + netlevelsup;
1595 						 levelsup++)
1596 						pstate = pstate->parentParseState;
1597 					mypstate.parentParseState = pstate;
1598 					mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
1599 					/* don't bother filling the rest of the fake pstate */
1600 
1601 					return expandRecordVariable(&mypstate, (Var *) expr, 0);
1602 				}
1603 				/* else fall through to inspect the expression */
1604 			}
1605 			break;
1606 	}
1607 
1608 	/*
1609 	 * We now have an expression we can't expand any more, so see if
1610 	 * get_expr_result_type() can do anything with it.  If not, pass to
1611 	 * lookup_rowtype_tupdesc() which will probably fail, but will give an
1612 	 * appropriate error message while failing.
1613 	 */
1614 	if (get_expr_result_type(expr, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
1615 		tupleDesc = lookup_rowtype_tupdesc_copy(exprType(expr),
1616 												exprTypmod(expr));
1617 
1618 	return tupleDesc;
1619 }
1620 
1621 
1622 /*
1623  * FigureColname -
1624  *	  if the name of the resulting column is not specified in the target
1625  *	  list, we have to guess a suitable name.  The SQL spec provides some
1626  *	  guidance, but not much...
1627  *
1628  * Note that the argument is the *untransformed* parse tree for the target
1629  * item.  This is a shade easier to work with than the transformed tree.
1630  */
1631 char *
FigureColname(Node * node)1632 FigureColname(Node *node)
1633 {
1634 	char	   *name = NULL;
1635 
1636 	(void) FigureColnameInternal(node, &name);
1637 	if (name != NULL)
1638 		return name;
1639 	/* default result if we can't guess anything */
1640 	return "?column?";
1641 }
1642 
1643 /*
1644  * FigureIndexColname -
1645  *	  choose the name for an expression column in an index
1646  *
1647  * This is actually just like FigureColname, except we return NULL if
1648  * we can't pick a good name.
1649  */
1650 char *
FigureIndexColname(Node * node)1651 FigureIndexColname(Node *node)
1652 {
1653 	char	   *name = NULL;
1654 
1655 	(void) FigureColnameInternal(node, &name);
1656 	return name;
1657 }
1658 
1659 /*
1660  * FigureColnameInternal -
1661  *	  internal workhorse for FigureColname
1662  *
1663  * Return value indicates strength of confidence in result:
1664  *		0 - no information
1665  *		1 - second-best name choice
1666  *		2 - good name choice
1667  * The return value is actually only used internally.
1668  * If the result isn't zero, *name is set to the chosen name.
1669  */
1670 static int
FigureColnameInternal(Node * node,char ** name)1671 FigureColnameInternal(Node *node, char **name)
1672 {
1673 	int			strength = 0;
1674 
1675 	if (node == NULL)
1676 		return strength;
1677 
1678 	switch (nodeTag(node))
1679 	{
1680 		case T_ColumnRef:
1681 			{
1682 				char	   *fname = NULL;
1683 				ListCell   *l;
1684 
1685 				/* find last field name, if any, ignoring "*" */
1686 				foreach(l, ((ColumnRef *) node)->fields)
1687 				{
1688 					Node	   *i = lfirst(l);
1689 
1690 					if (IsA(i, String))
1691 						fname = strVal(i);
1692 				}
1693 				if (fname)
1694 				{
1695 					*name = fname;
1696 					return 2;
1697 				}
1698 			}
1699 			break;
1700 		case T_A_Indirection:
1701 			{
1702 				A_Indirection *ind = (A_Indirection *) node;
1703 				char	   *fname = NULL;
1704 				ListCell   *l;
1705 
1706 				/* find last field name, if any, ignoring "*" and subscripts */
1707 				foreach(l, ind->indirection)
1708 				{
1709 					Node	   *i = lfirst(l);
1710 
1711 					if (IsA(i, String))
1712 						fname = strVal(i);
1713 				}
1714 				if (fname)
1715 				{
1716 					*name = fname;
1717 					return 2;
1718 				}
1719 				return FigureColnameInternal(ind->arg, name);
1720 			}
1721 			break;
1722 		case T_FuncCall:
1723 			*name = strVal(llast(((FuncCall *) node)->funcname));
1724 			return 2;
1725 		case T_A_Expr:
1726 			if (((A_Expr *) node)->kind == AEXPR_NULLIF)
1727 			{
1728 				/* make nullif() act like a regular function */
1729 				*name = "nullif";
1730 				return 2;
1731 			}
1732 			if (((A_Expr *) node)->kind == AEXPR_PAREN)
1733 			{
1734 				/* look through dummy parenthesis node */
1735 				return FigureColnameInternal(((A_Expr *) node)->lexpr, name);
1736 			}
1737 			break;
1738 		case T_TypeCast:
1739 			strength = FigureColnameInternal(((TypeCast *) node)->arg,
1740 											 name);
1741 			if (strength <= 1)
1742 			{
1743 				if (((TypeCast *) node)->typeName != NULL)
1744 				{
1745 					*name = strVal(llast(((TypeCast *) node)->typeName->names));
1746 					return 1;
1747 				}
1748 			}
1749 			break;
1750 		case T_CollateClause:
1751 			return FigureColnameInternal(((CollateClause *) node)->arg, name);
1752 		case T_GroupingFunc:
1753 			/* make GROUPING() act like a regular function */
1754 			*name = "grouping";
1755 			return 2;
1756 		case T_SubLink:
1757 			switch (((SubLink *) node)->subLinkType)
1758 			{
1759 				case EXISTS_SUBLINK:
1760 					*name = "exists";
1761 					return 2;
1762 				case ARRAY_SUBLINK:
1763 					*name = "array";
1764 					return 2;
1765 				case EXPR_SUBLINK:
1766 					{
1767 						/* Get column name of the subquery's single target */
1768 						SubLink    *sublink = (SubLink *) node;
1769 						Query	   *query = (Query *) sublink->subselect;
1770 
1771 						/*
1772 						 * The subquery has probably already been transformed,
1773 						 * but let's be careful and check that.  (The reason
1774 						 * we can see a transformed subquery here is that
1775 						 * transformSubLink is lazy and modifies the SubLink
1776 						 * node in-place.)
1777 						 */
1778 						if (IsA(query, Query))
1779 						{
1780 							TargetEntry *te = (TargetEntry *) linitial(query->targetList);
1781 
1782 							if (te->resname)
1783 							{
1784 								*name = te->resname;
1785 								return 2;
1786 							}
1787 						}
1788 					}
1789 					break;
1790 					/* As with other operator-like nodes, these have no names */
1791 				case MULTIEXPR_SUBLINK:
1792 				case ALL_SUBLINK:
1793 				case ANY_SUBLINK:
1794 				case ROWCOMPARE_SUBLINK:
1795 				case CTE_SUBLINK:
1796 					break;
1797 			}
1798 			break;
1799 		case T_CaseExpr:
1800 			strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult,
1801 											 name);
1802 			if (strength <= 1)
1803 			{
1804 				*name = "case";
1805 				return 1;
1806 			}
1807 			break;
1808 		case T_A_ArrayExpr:
1809 			/* make ARRAY[] act like a function */
1810 			*name = "array";
1811 			return 2;
1812 		case T_RowExpr:
1813 			/* make ROW() act like a function */
1814 			*name = "row";
1815 			return 2;
1816 		case T_CoalesceExpr:
1817 			/* make coalesce() act like a regular function */
1818 			*name = "coalesce";
1819 			return 2;
1820 		case T_MinMaxExpr:
1821 			/* make greatest/least act like a regular function */
1822 			switch (((MinMaxExpr *) node)->op)
1823 			{
1824 				case IS_GREATEST:
1825 					*name = "greatest";
1826 					return 2;
1827 				case IS_LEAST:
1828 					*name = "least";
1829 					return 2;
1830 			}
1831 			break;
1832 		case T_SQLValueFunction:
1833 			/* make these act like a function or variable */
1834 			switch (((SQLValueFunction *) node)->op)
1835 			{
1836 				case SVFOP_CURRENT_DATE:
1837 					*name = "current_date";
1838 					return 2;
1839 				case SVFOP_CURRENT_TIME:
1840 				case SVFOP_CURRENT_TIME_N:
1841 					*name = "current_time";
1842 					return 2;
1843 				case SVFOP_CURRENT_TIMESTAMP:
1844 				case SVFOP_CURRENT_TIMESTAMP_N:
1845 					*name = "current_timestamp";
1846 					return 2;
1847 				case SVFOP_LOCALTIME:
1848 				case SVFOP_LOCALTIME_N:
1849 					*name = "localtime";
1850 					return 2;
1851 				case SVFOP_LOCALTIMESTAMP:
1852 				case SVFOP_LOCALTIMESTAMP_N:
1853 					*name = "localtimestamp";
1854 					return 2;
1855 				case SVFOP_CURRENT_ROLE:
1856 					*name = "current_role";
1857 					return 2;
1858 				case SVFOP_CURRENT_USER:
1859 					*name = "current_user";
1860 					return 2;
1861 				case SVFOP_USER:
1862 					*name = "user";
1863 					return 2;
1864 				case SVFOP_SESSION_USER:
1865 					*name = "session_user";
1866 					return 2;
1867 				case SVFOP_CURRENT_CATALOG:
1868 					*name = "current_catalog";
1869 					return 2;
1870 				case SVFOP_CURRENT_SCHEMA:
1871 					*name = "current_schema";
1872 					return 2;
1873 			}
1874 			break;
1875 		case T_XmlExpr:
1876 			/* make SQL/XML functions act like a regular function */
1877 			switch (((XmlExpr *) node)->op)
1878 			{
1879 				case IS_XMLCONCAT:
1880 					*name = "xmlconcat";
1881 					return 2;
1882 				case IS_XMLELEMENT:
1883 					*name = "xmlelement";
1884 					return 2;
1885 				case IS_XMLFOREST:
1886 					*name = "xmlforest";
1887 					return 2;
1888 				case IS_XMLPARSE:
1889 					*name = "xmlparse";
1890 					return 2;
1891 				case IS_XMLPI:
1892 					*name = "xmlpi";
1893 					return 2;
1894 				case IS_XMLROOT:
1895 					*name = "xmlroot";
1896 					return 2;
1897 				case IS_XMLSERIALIZE:
1898 					*name = "xmlserialize";
1899 					return 2;
1900 				case IS_DOCUMENT:
1901 					/* nothing */
1902 					break;
1903 			}
1904 			break;
1905 		case T_XmlSerialize:
1906 			*name = "xmlserialize";
1907 			return 2;
1908 		default:
1909 			break;
1910 	}
1911 
1912 	return strength;
1913 }
1914