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