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