PostgreSQL Source Code  git master
parse_target.h File Reference
Include dependency graph for parse_target.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ListtransformTargetList (ParseState *pstate, List *targetlist, ParseExprKind exprKind)
 
ListtransformExpressionList (ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
 
void resolveTargetListUnknowns (ParseState *pstate, List *targetlist)
 
void markTargetListOrigins (ParseState *pstate, List *targetlist)
 
TargetEntrytransformTargetEntry (ParseState *pstate, Node *node, Node *expr, ParseExprKind exprKind, char *colname, bool resjunk)
 
ExprtransformAssignedExpr (ParseState *pstate, Expr *expr, ParseExprKind exprKind, const char *colname, int attrno, List *indirection, int location)
 
void updateTargetListEntry (ParseState *pstate, TargetEntry *tle, char *colname, int attrno, List *indirection, int location)
 
NodetransformAssignmentIndirection (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)
 
ListcheckInsertTargets (ParseState *pstate, List *cols, List **attrnos)
 
TupleDesc expandRecordVariable (ParseState *pstate, Var *var, int levelsup)
 
char * FigureColname (Node *node)
 
char * FigureIndexColname (Node *node)
 

Function Documentation

◆ checkInsertTargets()

List* checkInsertTargets ( ParseState pstate,
List cols,
List **  attrnos 
)

Definition at line 1015 of file parse_target.c.

1016 {
1017  *attrnos = NIL;
1018 
1019  if (cols == NIL)
1020  {
1021  /*
1022  * Generate default column list for INSERT.
1023  */
1024  int numcol = RelationGetNumberOfAttributes(pstate->p_target_relation);
1025 
1026  int i;
1027 
1028  for (i = 0; i < numcol; i++)
1029  {
1030  ResTarget *col;
1031  Form_pg_attribute attr;
1032 
1033  attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
1034 
1035  if (attr->attisdropped)
1036  continue;
1037 
1038  col = makeNode(ResTarget);
1039  col->name = pstrdup(NameStr(attr->attname));
1040  col->indirection = NIL;
1041  col->val = NULL;
1042  col->location = -1;
1043  cols = lappend(cols, col);
1044  *attrnos = lappend_int(*attrnos, i + 1);
1045  }
1046  }
1047  else
1048  {
1049  /*
1050  * Do initial validation of user-supplied INSERT column list.
1051  */
1052  Bitmapset *wholecols = NULL;
1053  Bitmapset *partialcols = NULL;
1054  ListCell *tl;
1055 
1056  foreach(tl, cols)
1057  {
1058  ResTarget *col = (ResTarget *) lfirst(tl);
1059  char *name = col->name;
1060  int attrno;
1061 
1062  /* Lookup column name, ereport on failure */
1063  attrno = attnameAttNum(pstate->p_target_relation, name, false);
1064  if (attrno == InvalidAttrNumber)
1065  ereport(ERROR,
1066  (errcode(ERRCODE_UNDEFINED_COLUMN),
1067  errmsg("column \"%s\" of relation \"%s\" does not exist",
1068  name,
1070  parser_errposition(pstate, col->location)));
1071 
1072  /*
1073  * Check for duplicates, but only of whole columns --- we allow
1074  * INSERT INTO foo (col.subcol1, col.subcol2)
1075  */
1076  if (col->indirection == NIL)
1077  {
1078  /* whole column; must not have any other assignment */
1079  if (bms_is_member(attrno, wholecols) ||
1080  bms_is_member(attrno, partialcols))
1081  ereport(ERROR,
1082  (errcode(ERRCODE_DUPLICATE_COLUMN),
1083  errmsg("column \"%s\" specified more than once",
1084  name),
1085  parser_errposition(pstate, col->location)));
1086  wholecols = bms_add_member(wholecols, attrno);
1087  }
1088  else
1089  {
1090  /* partial column; must not have any whole assignment */
1091  if (bms_is_member(attrno, wholecols))
1092  ereport(ERROR,
1093  (errcode(ERRCODE_DUPLICATE_COLUMN),
1094  errmsg("column \"%s\" specified more than once",
1095  name),
1096  parser_errposition(pstate, col->location)));
1097  partialcols = bms_add_member(partialcols, attrno);
1098  }
1099 
1100  *attrnos = lappend_int(*attrnos, attrno);
1101  }
1102  }
1103 
1104  return cols;
1105 }
#define InvalidAttrNumber
Definition: attnum.h:23
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
#define NameStr(name)
Definition: c.h:746
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
int i
Definition: isn.c:73
List * lappend(List *list, void *datum)
Definition: list.c:339
List * lappend_int(List *list, int datum)
Definition: list.c:357
char * pstrdup(const char *in)
Definition: mcxt.c:1695
#define makeNode(_type_)
Definition: nodes.h:155
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:511
#define RelationGetRelationName(relation)
Definition: rel.h:539
Relation p_target_relation
Definition: parse_node.h:207
TupleDesc rd_att
Definition: rel.h:112
Node * val
Definition: parsenodes.h:519
ParseLoc location
Definition: parsenodes.h:520
List * indirection
Definition: parsenodes.h:518
char * name
Definition: parsenodes.h:517
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
const char * name

References attnameAttNum(), bms_add_member(), bms_is_member(), ereport, errcode(), errmsg(), ERROR, i, ResTarget::indirection, InvalidAttrNumber, lappend(), lappend_int(), lfirst, ResTarget::location, makeNode, name, ResTarget::name, NameStr, NIL, ParseState::p_target_relation, parser_errposition(), pstrdup(), RelationData::rd_att, RelationGetNumberOfAttributes, RelationGetRelationName, TupleDescAttr, and ResTarget::val.

Referenced by transformInsertStmt(), and transformMergeStmt().

◆ expandRecordVariable()

TupleDesc expandRecordVariable ( ParseState pstate,
Var var,
int  levelsup 
)

Definition at line 1519 of file parse_target.c.

1520 {
1521  TupleDesc tupleDesc;
1522  int netlevelsup;
1523  RangeTblEntry *rte;
1525  Node *expr;
1526 
1527  /* Check my caller didn't mess up */
1528  Assert(IsA(var, Var));
1529  Assert(var->vartype == RECORDOID);
1530 
1531  /*
1532  * Note: it's tempting to use GetNSItemByRangeTablePosn here so that we
1533  * can use expandNSItemVars instead of expandRTE; but that does not work
1534  * for some of the recursion cases below, where we have consed up a
1535  * ParseState that lacks p_namespace data.
1536  */
1537  netlevelsup = var->varlevelsup + levelsup;
1538  rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
1539  attnum = var->varattno;
1540 
1541  if (attnum == InvalidAttrNumber)
1542  {
1543  /* Whole-row reference to an RTE, so expand the known fields */
1544  List *names,
1545  *vars;
1546  ListCell *lname,
1547  *lvar;
1548  int i;
1549 
1550  expandRTE(rte, var->varno, 0, var->location, false,
1551  &names, &vars);
1552 
1554  i = 1;
1555  forboth(lname, names, lvar, vars)
1556  {
1557  char *label = strVal(lfirst(lname));
1558  Node *varnode = (Node *) lfirst(lvar);
1559 
1560  TupleDescInitEntry(tupleDesc, i,
1561  label,
1562  exprType(varnode),
1563  exprTypmod(varnode),
1564  0);
1565  TupleDescInitEntryCollation(tupleDesc, i,
1566  exprCollation(varnode));
1567  i++;
1568  }
1569  Assert(lname == NULL && lvar == NULL); /* lists same length? */
1570 
1571  return tupleDesc;
1572  }
1573 
1574  expr = (Node *) var; /* default if we can't drill down */
1575 
1576  switch (rte->rtekind)
1577  {
1578  case RTE_RELATION:
1579  case RTE_VALUES:
1580  case RTE_NAMEDTUPLESTORE:
1581  case RTE_RESULT:
1582 
1583  /*
1584  * This case should not occur: a column of a table, values list,
1585  * or ENR shouldn't have type RECORD. Fall through and fail (most
1586  * likely) at the bottom.
1587  */
1588  break;
1589  case RTE_SUBQUERY:
1590  {
1591  /* Subselect-in-FROM: examine sub-select's output expr */
1593  attnum);
1594 
1595  if (ste == NULL || ste->resjunk)
1596  elog(ERROR, "subquery %s does not have attribute %d",
1597  rte->eref->aliasname, attnum);
1598  expr = (Node *) ste->expr;
1599  if (IsA(expr, Var))
1600  {
1601  /*
1602  * Recurse into the sub-select to see what its Var refers
1603  * to. We have to build an additional level of ParseState
1604  * to keep in step with varlevelsup in the subselect;
1605  * furthermore, the subquery RTE might be from an outer
1606  * query level, in which case the ParseState for the
1607  * subselect must have that outer level as parent.
1608  */
1609  ParseState mypstate = {0};
1610  Index levelsup;
1611 
1612  /* this loop must work, since GetRTEByRangeTablePosn did */
1613  for (levelsup = 0; levelsup < netlevelsup; levelsup++)
1614  pstate = pstate->parentParseState;
1615  mypstate.parentParseState = pstate;
1616  mypstate.p_rtable = rte->subquery->rtable;
1617  /* don't bother filling the rest of the fake pstate */
1618 
1619  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1620  }
1621  /* else fall through to inspect the expression */
1622  }
1623  break;
1624  case RTE_JOIN:
1625  /* Join RTE --- recursively inspect the alias variable */
1626  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
1627  expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1628  Assert(expr != NULL);
1629  /* We intentionally don't strip implicit coercions here */
1630  if (IsA(expr, Var))
1631  return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
1632  /* else fall through to inspect the expression */
1633  break;
1634  case RTE_FUNCTION:
1635 
1636  /*
1637  * We couldn't get here unless a function is declared with one of
1638  * its result columns as RECORD, which is not allowed.
1639  */
1640  break;
1641  case RTE_TABLEFUNC:
1642 
1643  /*
1644  * Table function cannot have columns with RECORD type.
1645  */
1646  break;
1647  case RTE_CTE:
1648  /* CTE reference: examine subquery's output expr */
1649  if (!rte->self_reference)
1650  {
1651  CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
1652  TargetEntry *ste;
1653 
1655  if (ste == NULL || ste->resjunk)
1656  elog(ERROR, "CTE %s does not have attribute %d",
1657  rte->eref->aliasname, attnum);
1658  expr = (Node *) ste->expr;
1659  if (IsA(expr, Var))
1660  {
1661  /*
1662  * Recurse into the CTE to see what its Var refers to. We
1663  * have to build an additional level of ParseState to keep
1664  * in step with varlevelsup in the CTE; furthermore it
1665  * could be an outer CTE (compare SUBQUERY case above).
1666  */
1667  ParseState mypstate = {0};
1668  Index levelsup;
1669 
1670  /* this loop must work, since GetCTEForRTE did */
1671  for (levelsup = 0;
1672  levelsup < rte->ctelevelsup + netlevelsup;
1673  levelsup++)
1674  pstate = pstate->parentParseState;
1675  mypstate.parentParseState = pstate;
1676  mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
1677  /* don't bother filling the rest of the fake pstate */
1678 
1679  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1680  }
1681  /* else fall through to inspect the expression */
1682  }
1683  break;
1684  }
1685 
1686  /*
1687  * We now have an expression we can't expand any more, so see if
1688  * get_expr_result_tupdesc() can do anything with it.
1689  */
1690  return get_expr_result_tupdesc(expr, false);
1691 }
int16 AttrNumber
Definition: attnum.h:21
#define Assert(condition)
Definition: c.h:858
unsigned int Index
Definition: c.h:614
#define elog(elevel,...)
Definition: elog.h:224
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:551
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:298
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:816
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
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)
TupleDesc expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
#define GetCTETargetList(cte)
Definition: parsenodes.h:1710
@ RTE_JOIN
Definition: parsenodes.h:1030
@ RTE_CTE
Definition: parsenodes.h:1034
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1035
@ RTE_VALUES
Definition: parsenodes.h:1033
@ RTE_SUBQUERY
Definition: parsenodes.h:1029
@ RTE_RESULT
Definition: parsenodes.h:1036
@ RTE_FUNCTION
Definition: parsenodes.h:1031
@ RTE_TABLEFUNC
Definition: parsenodes.h:1032
@ RTE_RELATION
Definition: parsenodes.h:1028
int16 attnum
Definition: pg_attribute.h:74
static char * label
static int list_length(const List *l)
Definition: pg_list.h:152
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
Definition: pg_list.h:54
Definition: nodes.h:129
ParseState * parentParseState
Definition: parse_node.h:192
List * p_rtable
Definition: parse_node.h:194
List * rtable
Definition: parsenodes.h:168
List * targetList
Definition: parsenodes.h:191
Index ctelevelsup
Definition: parsenodes.h:1208
Query * subquery
Definition: parsenodes.h:1114
RTEKind rtekind
Definition: parsenodes.h:1057
Expr * expr
Definition: primnodes.h:2162
Definition: primnodes.h:248
ParseLoc location
Definition: primnodes.h:293
AttrNumber varattno
Definition: primnodes.h:260
int varno
Definition: primnodes.h:255
Index varlevelsup
Definition: primnodes.h:280
Definition: regcomp.c:281
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:67
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:833
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:651
#define strVal(v)
Definition: value.h:82

References Assert, attnum, CreateTemplateTupleDesc(), RangeTblEntry::ctelevelsup, CommonTableExpr::ctequery, elog, ERROR, expandRTE(), TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), forboth, get_expr_result_tupdesc(), get_tle_by_resno(), GetCTEForRTE(), GetCTETargetList, GetRTEByRangeTablePosn(), i, if(), InvalidAttrNumber, IsA, label, lfirst, list_length(), list_nth(), Var::location, ParseState::p_rtable, ParseState::parentParseState, Query::rtable, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, strVal, RangeTblEntry::subquery, Query::targetList, TupleDescInitEntry(), TupleDescInitEntryCollation(), Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by ExpandRowReference(), and ParseComplexProjection().

◆ FigureColname()

char* FigureColname ( Node node)

Definition at line 1704 of file parse_target.c.

1705 {
1706  char *name = NULL;
1707 
1708  (void) FigureColnameInternal(node, &name);
1709  if (name != NULL)
1710  return name;
1711  /* default result if we can't guess anything */
1712  return "?column?";
1713 }
static int FigureColnameInternal(Node *node, char **name)

References FigureColnameInternal(), and name.

Referenced by transformRangeFunction(), transformTargetEntry(), and transformXmlExpr().

◆ FigureIndexColname()

char* FigureIndexColname ( Node node)

Definition at line 1723 of file parse_target.c.

1724 {
1725  char *name = NULL;
1726 
1727  (void) FigureColnameInternal(node, &name);
1728  return name;
1729 }

References FigureColnameInternal(), and name.

Referenced by transformIndexStmt().

◆ markTargetListOrigins()

void markTargetListOrigins ( ParseState pstate,
List targetlist 
)

Definition at line 318 of file parse_target.c.

319 {
320  ListCell *l;
321 
322  foreach(l, targetlist)
323  {
324  TargetEntry *tle = (TargetEntry *) lfirst(l);
325 
326  markTargetListOrigin(pstate, tle, (Var *) tle->expr, 0);
327  }
328 }
static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle, Var *var, int levelsup)
Definition: parse_target.c:343

References TargetEntry::expr, lfirst, and markTargetListOrigin().

Referenced by transformReturningList(), and transformSelectStmt().

◆ resolveTargetListUnknowns()

void resolveTargetListUnknowns ( ParseState pstate,
List targetlist 
)

Definition at line 288 of file parse_target.c.

289 {
290  ListCell *l;
291 
292  foreach(l, targetlist)
293  {
294  TargetEntry *tle = (TargetEntry *) lfirst(l);
295  Oid restype = exprType((Node *) tle->expr);
296 
297  if (restype == UNKNOWNOID)
298  {
299  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
300  restype, TEXTOID, -1,
303  -1);
304  }
305  }
306 }
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:157
unsigned int Oid
Definition: postgres_ext.h:31
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:706
@ COERCION_IMPLICIT
Definition: primnodes.h:684

References COERCE_IMPLICIT_CAST, coerce_type(), COERCION_IMPLICIT, TargetEntry::expr, exprType(), and lfirst.

Referenced by transformReturningList(), transformReturnStmt(), and transformSelectStmt().

◆ transformAssignedExpr()

Expr* transformAssignedExpr ( ParseState pstate,
Expr expr,
ParseExprKind  exprKind,
const char *  colname,
int  attrno,
List indirection,
int  location 
)

Definition at line 452 of file parse_target.c.

459 {
460  Relation rd = pstate->p_target_relation;
461  Oid type_id; /* type of value provided */
462  Oid attrtype; /* type of target column */
463  int32 attrtypmod;
464  Oid attrcollation; /* collation of target column */
465  ParseExprKind sv_expr_kind;
466 
467  /*
468  * Save and restore identity of expression type we're parsing. We must
469  * set p_expr_kind here because we can parse subscripts without going
470  * through transformExpr().
471  */
472  Assert(exprKind != EXPR_KIND_NONE);
473  sv_expr_kind = pstate->p_expr_kind;
474  pstate->p_expr_kind = exprKind;
475 
476  Assert(rd != NULL);
477  if (attrno <= 0)
478  ereport(ERROR,
479  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
480  errmsg("cannot assign to system column \"%s\"",
481  colname),
482  parser_errposition(pstate, location)));
483  attrtype = attnumTypeId(rd, attrno);
484  attrtypmod = TupleDescAttr(rd->rd_att, attrno - 1)->atttypmod;
485  attrcollation = TupleDescAttr(rd->rd_att, attrno - 1)->attcollation;
486 
487  /*
488  * If the expression is a DEFAULT placeholder, insert the attribute's
489  * type/typmod/collation into it so that exprType etc will report the
490  * right things. (We expect that the eventually substituted default
491  * expression will in fact have this type and typmod. The collation
492  * likely doesn't matter, but let's set it correctly anyway.) Also,
493  * reject trying to update a subfield or array element with DEFAULT, since
494  * there can't be any default for portions of a column.
495  */
496  if (expr && IsA(expr, SetToDefault))
497  {
498  SetToDefault *def = (SetToDefault *) expr;
499 
500  def->typeId = attrtype;
501  def->typeMod = attrtypmod;
502  def->collation = attrcollation;
503  if (indirection)
504  {
505  if (IsA(linitial(indirection), A_Indices))
506  ereport(ERROR,
507  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
508  errmsg("cannot set an array element to DEFAULT"),
509  parser_errposition(pstate, location)));
510  else
511  ereport(ERROR,
512  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
513  errmsg("cannot set a subfield to DEFAULT"),
514  parser_errposition(pstate, location)));
515  }
516  }
517 
518  /* Now we can use exprType() safely. */
519  type_id = exprType((Node *) expr);
520 
521  /*
522  * If there is indirection on the target column, prepare an array or
523  * subfield assignment expression. This will generate a new column value
524  * that the source value has been inserted into, which can then be placed
525  * in the new tuple constructed by INSERT or UPDATE.
526  */
527  if (indirection)
528  {
529  Node *colVar;
530 
531  if (pstate->p_is_insert)
532  {
533  /*
534  * The command is INSERT INTO table (col.something) ... so there
535  * is not really a source value to work with. Insert a NULL
536  * constant as the source value.
537  */
538  colVar = (Node *) makeNullConst(attrtype, attrtypmod,
539  attrcollation);
540  }
541  else
542  {
543  /*
544  * Build a Var for the column to be updated.
545  */
546  Var *var;
547 
548  var = makeVar(pstate->p_target_nsitem->p_rtindex, attrno,
549  attrtype, attrtypmod, attrcollation, 0);
550  var->location = location;
551 
552  colVar = (Node *) var;
553  }
554 
555  expr = (Expr *)
557  colVar,
558  colname,
559  false,
560  attrtype,
561  attrtypmod,
562  attrcollation,
563  indirection,
564  list_head(indirection),
565  (Node *) expr,
567  location);
568  }
569  else
570  {
571  /*
572  * For normal non-qualified target column, do type checking and
573  * coercion.
574  */
575  Node *orig_expr = (Node *) expr;
576 
577  expr = (Expr *)
578  coerce_to_target_type(pstate,
579  orig_expr, type_id,
580  attrtype, attrtypmod,
583  -1);
584  if (expr == NULL)
585  ereport(ERROR,
586  (errcode(ERRCODE_DATATYPE_MISMATCH),
587  errmsg("column \"%s\" is of type %s"
588  " but expression is of type %s",
589  colname,
590  format_type_be(attrtype),
591  format_type_be(type_id)),
592  errhint("You will need to rewrite or cast the expression."),
593  parser_errposition(pstate, exprLocation(orig_expr))));
594  }
595 
596  pstate->p_expr_kind = sv_expr_kind;
597 
598  return expr;
599 }
signed int int32
Definition: c.h:494
int errhint(const char *fmt,...)
Definition: elog.c:1319
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:339
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1386
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
ParseExprKind
Definition: parse_node.h:39
@ EXPR_KIND_NONE
Definition: parse_node.h:40
Oid attnumTypeId(Relation rd, int attid)
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:683
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
#define linitial(l)
Definition: pg_list.h:178
@ COERCION_ASSIGNMENT
Definition: primnodes.h:685
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:208
ParseExprKind p_expr_kind
Definition: parse_node.h:211
bool p_is_insert
Definition: parse_node.h:209

References Assert, attnumTypeId(), COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_NONE, exprLocation(), exprType(), format_type_be(), IsA, linitial, list_head(), Var::location, makeNullConst(), makeVar(), ParseState::p_expr_kind, ParseState::p_is_insert, ParseNamespaceItem::p_rtindex, ParseState::p_target_nsitem, ParseState::p_target_relation, parser_errposition(), RelationData::rd_att, transformAssignmentIndirection(), TupleDescAttr, and SetToDefault::typeId.

Referenced by transformInsertRow(), and updateTargetListEntry().

◆ transformAssignmentIndirection()

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 at line 683 of file parse_target.c.

695 {
696  Node *result;
697  List *subscripts = NIL;
698  ListCell *i;
699 
700  if (indirection_cell && !basenode)
701  {
702  /*
703  * Set up a substitution. We abuse CaseTestExpr for this. It's safe
704  * to do so because the only nodes that will be above the CaseTestExpr
705  * in the finished expression will be FieldStore and SubscriptingRef
706  * nodes. (There could be other stuff in the tree, but it will be
707  * within other child fields of those node types.)
708  */
710 
711  ctest->typeId = targetTypeId;
712  ctest->typeMod = targetTypMod;
713  ctest->collation = targetCollation;
714  basenode = (Node *) ctest;
715  }
716 
717  /*
718  * We have to split any field-selection operations apart from
719  * subscripting. Adjacent A_Indices nodes have to be treated as a single
720  * multidimensional subscript operation.
721  */
722  for_each_cell(i, indirection, indirection_cell)
723  {
724  Node *n = lfirst(i);
725 
726  if (IsA(n, A_Indices))
727  subscripts = lappend(subscripts, n);
728  else if (IsA(n, A_Star))
729  {
730  ereport(ERROR,
731  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
732  errmsg("row expansion via \"*\" is not supported here"),
733  parser_errposition(pstate, location)));
734  }
735  else
736  {
737  FieldStore *fstore;
738  Oid baseTypeId;
739  int32 baseTypeMod;
740  Oid typrelid;
742  Oid fieldTypeId;
743  int32 fieldTypMod;
744  Oid fieldCollation;
745 
746  Assert(IsA(n, String));
747 
748  /* process subscripts before this field selection */
749  if (subscripts)
750  {
751  /* recurse, and then return because we're done */
752  return transformAssignmentSubscripts(pstate,
753  basenode,
754  targetName,
755  targetTypeId,
756  targetTypMod,
757  targetCollation,
758  subscripts,
759  indirection,
760  i,
761  rhs,
762  ccontext,
763  location);
764  }
765 
766  /* No subscripts, so can process field selection here */
767 
768  /*
769  * Look up the composite type, accounting for possibility that
770  * what we are given is a domain over composite.
771  */
772  baseTypeMod = targetTypMod;
773  baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
774 
775  typrelid = typeidTypeRelid(baseTypeId);
776  if (!typrelid)
777  ereport(ERROR,
778  (errcode(ERRCODE_DATATYPE_MISMATCH),
779  errmsg("cannot assign to field \"%s\" of column \"%s\" because its type %s is not a composite type",
780  strVal(n), targetName,
781  format_type_be(targetTypeId)),
782  parser_errposition(pstate, location)));
783 
784  attnum = get_attnum(typrelid, strVal(n));
785  if (attnum == InvalidAttrNumber)
786  ereport(ERROR,
787  (errcode(ERRCODE_UNDEFINED_COLUMN),
788  errmsg("cannot assign to field \"%s\" of column \"%s\" because there is no such column in data type %s",
789  strVal(n), targetName,
790  format_type_be(targetTypeId)),
791  parser_errposition(pstate, location)));
792  if (attnum < 0)
793  ereport(ERROR,
794  (errcode(ERRCODE_UNDEFINED_COLUMN),
795  errmsg("cannot assign to system column \"%s\"",
796  strVal(n)),
797  parser_errposition(pstate, location)));
798 
799  get_atttypetypmodcoll(typrelid, attnum,
800  &fieldTypeId, &fieldTypMod, &fieldCollation);
801 
802  /* recurse to create appropriate RHS for field assign */
803  rhs = transformAssignmentIndirection(pstate,
804  NULL,
805  strVal(n),
806  false,
807  fieldTypeId,
808  fieldTypMod,
809  fieldCollation,
810  indirection,
811  lnext(indirection, i),
812  rhs,
813  ccontext,
814  location);
815 
816  /* and build a FieldStore node */
817  fstore = makeNode(FieldStore);
818  fstore->arg = (Expr *) basenode;
819  fstore->newvals = list_make1(rhs);
820  fstore->fieldnums = list_make1_int(attnum);
821  fstore->resulttype = baseTypeId;
822 
823  /*
824  * If target is a domain, apply constraints. Notice that this
825  * isn't totally right: the expression tree we build would check
826  * the domain's constraints on a composite value with only this
827  * one field populated or updated, possibly leading to an unwanted
828  * failure. The rewriter will merge together any subfield
829  * assignments to the same table column, resulting in the domain's
830  * constraints being checked only once after we've assigned to all
831  * the fields that the INSERT or UPDATE means to.
832  */
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  indirection,
858  NULL,
859  rhs,
860  ccontext,
861  location);
862  }
863 
864  /* base case: just coerce RHS to match target type ID */
865 
866  result = coerce_to_target_type(pstate,
867  rhs, exprType(rhs),
868  targetTypeId, targetTypMod,
869  ccontext,
871  -1);
872  if (result == NULL)
873  {
874  if (targetIsSubscripting)
875  ereport(ERROR,
876  (errcode(ERRCODE_DATATYPE_MISMATCH),
877  errmsg("subscripted assignment to \"%s\" requires type %s"
878  " but expression is of type %s",
879  targetName,
880  format_type_be(targetTypeId),
881  format_type_be(exprType(rhs))),
882  errhint("You will need to rewrite or cast the expression."),
883  parser_errposition(pstate, location)));
884  else
885  ereport(ERROR,
886  (errcode(ERRCODE_DATATYPE_MISMATCH),
887  errmsg("subfield \"%s\" is of type %s"
888  " but expression is of type %s",
889  targetName,
890  format_type_be(targetTypeId),
891  format_type_be(exprType(rhs))),
892  errhint("You will need to rewrite or cast the expression."),
893  parser_errposition(pstate, location)));
894  }
895 
896  return result;
897 }
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:858
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2538
void get_atttypetypmodcoll(Oid relid, AttrNumber attnum, Oid *typid, int32 *typmod, Oid *collid)
Definition: lsyscache.c:943
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
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:903
Oid typeidTypeRelid(Oid type_id)
Definition: parse_type.c:668
#define list_make1(x1)
Definition: pg_list.h:212
#define for_each_cell(cell, lst, initcell)
Definition: pg_list.h:438
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define list_make1_int(x1)
Definition: pg_list.h:227
List * newvals
Definition: primnodes.h:1130
Expr * arg
Definition: primnodes.h:1129
Definition: value.h:64

References FieldStore::arg, Assert, attnum, COERCE_IMPLICIT_CAST, coerce_to_domain(), coerce_to_target_type(), COERCION_IMPLICIT, ereport, errcode(), errhint(), errmsg(), ERROR, exprType(), for_each_cell, format_type_be(), get_attnum(), get_atttypetypmodcoll(), getBaseTypeAndTypmod(), i, InvalidAttrNumber, IsA, lappend(), lfirst, list_make1, list_make1_int, lnext(), makeNode, FieldStore::newvals, NIL, parser_errposition(), strVal, transformAssignmentSubscripts(), CaseTestExpr::typeId, and typeidTypeRelid().

Referenced by transformAssignedExpr(), transformAssignmentSubscripts(), and transformPLAssignStmt().

◆ transformExpressionList()

List* transformExpressionList ( ParseState pstate,
List exprlist,
ParseExprKind  exprKind,
bool  allowDefault 
)

Definition at line 220 of file parse_target.c.

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  return result;
277 }
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:121
static List * ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref, bool make_target_entry)
static List * ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind, bool make_target_entry, ParseExprKind exprKind)
struct A_Star A_Star
#define llast(l)
Definition: pg_list.h:198
e
Definition: preproc-init.c:82
List * fields
Definition: parsenodes.h:294

References ExpandColumnRefStar(), ExpandIndirectionStar(), ColumnRef::fields, IsA, lappend(), lfirst, list_concat(), llast, NIL, and transformExpr().

Referenced by transformMergeStmt(), transformRowExpr(), and transformValuesClause().

◆ transformTargetEntry()

TargetEntry* transformTargetEntry ( ParseState pstate,
Node node,
Node expr,
ParseExprKind  exprKind,
char *  colname,
bool  resjunk 
)

Definition at line 75 of file parse_target.c.

81 {
82  /* Transform the node if caller didn't do it already */
83  if (expr == NULL)
84  {
85  /*
86  * If it's a SetToDefault node and we should allow that, pass it
87  * through unmodified. (transformExpr will throw the appropriate
88  * error if we're disallowing it.)
89  */
90  if (exprKind == EXPR_KIND_UPDATE_SOURCE && IsA(node, SetToDefault))
91  expr = node;
92  else
93  expr = transformExpr(pstate, node, exprKind);
94  }
95 
96  if (colname == NULL && !resjunk)
97  {
98  /*
99  * Generate a suitable column name for a column without any explicit
100  * 'AS ColumnName' clause.
101  */
102  colname = FigureColname(node);
103  }
104 
105  return makeTargetEntry((Expr *) expr,
106  (AttrNumber) pstate->p_next_resno++,
107  colname,
108  resjunk);
109 }
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:240
@ EXPR_KIND_UPDATE_SOURCE
Definition: parse_node.h:56
char * FigureColname(Node *node)
int p_next_resno
Definition: parse_node.h:212

References EXPR_KIND_UPDATE_SOURCE, FigureColname(), IsA, makeTargetEntry(), ParseState::p_next_resno, and transformExpr().

Referenced by findTargetlistEntrySQL99(), and transformTargetList().

◆ transformTargetList()

List* transformTargetList ( ParseState pstate,
List targetlist,
ParseExprKind  exprKind 
)

Definition at line 121 of file parse_target.c.

123 {
124  List *p_target = NIL;
125  bool expand_star;
126  ListCell *o_target;
127 
128  /* Shouldn't have any leftover multiassign items at start */
129  Assert(pstate->p_multiassign_exprs == NIL);
130 
131  /* Expand "something.*" in SELECT and RETURNING, but not UPDATE */
132  expand_star = (exprKind != EXPR_KIND_UPDATE_SOURCE);
133 
134  foreach(o_target, targetlist)
135  {
136  ResTarget *res = (ResTarget *) lfirst(o_target);
137 
138  /*
139  * Check for "something.*". Depending on the complexity of the
140  * "something", the star could appear as the last field in ColumnRef,
141  * or as the last indirection item in A_Indirection.
142  */
143  if (expand_star)
144  {
145  if (IsA(res->val, ColumnRef))
146  {
147  ColumnRef *cref = (ColumnRef *) res->val;
148 
149  if (IsA(llast(cref->fields), A_Star))
150  {
151  /* It is something.*, expand into multiple items */
152  p_target = list_concat(p_target,
153  ExpandColumnRefStar(pstate,
154  cref,
155  true));
156  continue;
157  }
158  }
159  else if (IsA(res->val, A_Indirection))
160  {
161  A_Indirection *ind = (A_Indirection *) res->val;
162 
163  if (IsA(llast(ind->indirection), A_Star))
164  {
165  /* It is something.*, expand into multiple items */
166  p_target = list_concat(p_target,
167  ExpandIndirectionStar(pstate,
168  ind,
169  true,
170  exprKind));
171  continue;
172  }
173  }
174  }
175 
176  /*
177  * Not "something.*", or we want to treat that as a plain whole-row
178  * variable, so transform as a single expression
179  */
180  p_target = lappend(p_target,
181  transformTargetEntry(pstate,
182  res->val,
183  NULL,
184  exprKind,
185  res->name,
186  false));
187  }
188 
189  /*
190  * If any multiassign resjunk items were created, attach them to the end
191  * of the targetlist. This should only happen in an UPDATE tlist. We
192  * don't need to worry about numbering of these items; transformUpdateStmt
193  * will set their resnos.
194  */
195  if (pstate->p_multiassign_exprs)
196  {
197  Assert(exprKind == EXPR_KIND_UPDATE_SOURCE);
198  p_target = list_concat(p_target, pstate->p_multiassign_exprs);
199  pstate->p_multiassign_exprs = NIL;
200  }
201 
202  return p_target;
203 }
TargetEntry * transformTargetEntry(ParseState *pstate, Node *node, Node *expr, ParseExprKind exprKind, char *colname, bool resjunk)
Definition: parse_target.c:75
List * p_multiassign_exprs
Definition: parse_node.h:213

References Assert, ExpandColumnRefStar(), ExpandIndirectionStar(), EXPR_KIND_UPDATE_SOURCE, ColumnRef::fields, if(), IsA, lappend(), lfirst, list_concat(), llast, NIL, ParseState::p_multiassign_exprs, res, and transformTargetEntry().

Referenced by transformPLAssignStmt(), transformReturningList(), transformSelectStmt(), and transformUpdateTargetList().

◆ updateTargetListEntry()

void updateTargetListEntry ( ParseState pstate,
TargetEntry tle,
char *  colname,
int  attrno,
List indirection,
int  location 
)

Definition at line 619 of file parse_target.c.

625 {
626  /* Fix up expression as needed */
627  tle->expr = transformAssignedExpr(pstate,
628  tle->expr,
630  colname,
631  attrno,
632  indirection,
633  location);
634 
635  /*
636  * Set the resno to identify the target column --- the rewriter and
637  * planner depend on this. We also set the resname to identify the target
638  * column, but this is only for debugging purposes; it should not be
639  * relied on. (In particular, it might be out of date in a stored rule.)
640  */
641  tle->resno = (AttrNumber) attrno;
642  tle->resname = colname;
643 }
@ EXPR_KIND_UPDATE_TARGET
Definition: parse_node.h:57
Expr * transformAssignedExpr(ParseState *pstate, Expr *expr, ParseExprKind exprKind, const char *colname, int attrno, List *indirection, int location)
Definition: parse_target.c:452
AttrNumber resno
Definition: primnodes.h:2164

References TargetEntry::expr, EXPR_KIND_UPDATE_TARGET, TargetEntry::resno, and transformAssignedExpr().

Referenced by transformUpdateTargetList().