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 1002 of file parse_target.c.

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 }
#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
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
int i
Definition: isn.c:73
List * lappend(List *list, void *datum)
Definition: list.c:338
List * lappend_int(List *list, int datum)
Definition: list.c:356
char * pstrdup(const char *in)
Definition: mcxt.c:1644
#define makeNode(_type_)
Definition: nodes.h:176
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
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:510
#define RelationGetRelationName(relation)
Definition: rel.h:538
Relation p_target_relation
Definition: parse_node.h:206
TupleDesc rd_att
Definition: rel.h:112
int location
Definition: parsenodes.h:518
Node * val
Definition: parsenodes.h:517
List * indirection
Definition: parsenodes.h:516
char * name
Definition: parsenodes.h:515
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

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 1505 of file parse_target.c.

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 }
int16 AttrNumber
Definition: attnum.h:21
unsigned int Index
Definition: c.h:598
#define MemSet(start, val, len)
Definition: c.h:1004
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:543
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Assert(fmt[strlen(fmt) - 1] !='\n')
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:284
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:788
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
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:1659
@ 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
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:467
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
char * aliasname
Definition: primnodes.h:42
Definition: pg_list.h:54
Definition: nodes.h:129
ParseState * parentParseState
Definition: parse_node.h:191
List * p_rtable
Definition: parse_node.h:193
List * rtable
Definition: parsenodes.h:175
List * targetList
Definition: parsenodes.h:189
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
Expr * expr
Definition: primnodes.h:1886
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:281
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 strVal(v)
Definition: value.h:82

References Alias::aliasname, Assert(), attnum, CreateTemplateTupleDesc(), RangeTblEntry::ctelevelsup, CommonTableExpr::ctequery, elog(), RangeTblEntry::eref, ERROR, expandRTE(), TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), forboth, get_expr_result_tupdesc(), get_tle_by_resno(), GetCTEForRTE(), GetCTETargetList, GetRTEByRangeTablePosn(), i, if(), InvalidAttrNumber, IsA, RangeTblEntry::joinaliasvars, label, lfirst, list_length(), list_nth(), Var::location, MemSet, 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, RangeTblEntry::self_reference, 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 1684 of file parse_target.c.

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 }
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 1703 of file parse_target.c.

1704 {
1705  char *name = NULL;
1706 
1707  (void) FigureColnameInternal(node, &name);
1708  return name;
1709 }

References FigureColnameInternal(), and name.

Referenced by transformIndexStmt().

◆ markTargetListOrigins()

void markTargetListOrigins ( ParseState pstate,
List targetlist 
)

Definition at line 319 of file parse_target.c.

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 }
static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle, Var *var, int levelsup)
Definition: parse_target.c:344

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

Referenced by transformReturningList(), and transformSelectStmt().

◆ resolveTargetListUnknowns()

void resolveTargetListUnknowns ( ParseState pstate,
List targetlist 
)

Definition at line 289 of file parse_target.c.

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 }
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:663
@ COERCION_IMPLICIT
Definition: primnodes.h:641

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 453 of file parse_target.c.

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 }
signed int int32
Definition: c.h:478
int errhint(const char *fmt,...)
Definition: elog.c:1316
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:340
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1314
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:684
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:642
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:207
ParseExprKind p_expr_kind
Definition: parse_node.h:210
bool p_is_insert
Definition: parse_node.h:208

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 684 of file parse_target.c.

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 }
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:857
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
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:895
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:1087
Expr * arg
Definition: primnodes.h:1086
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 221 of file parse_target.c.

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 }
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:106
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:292

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 76 of file parse_target.c.

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 }
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:241
@ EXPR_KIND_UPDATE_SOURCE
Definition: parse_node.h:56
char * FigureColname(Node *node)
int p_next_resno
Definition: parse_node.h:211

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 122 of file parse_target.c.

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 }
TargetEntry * transformTargetEntry(ParseState *pstate, Node *node, Node *expr, ParseExprKind exprKind, char *colname, bool resjunk)
Definition: parse_target.c:76
List * p_multiassign_exprs
Definition: parse_node.h:212

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 620 of file parse_target.c.

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 }
@ 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:453
AttrNumber resno
Definition: primnodes.h:1888

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

Referenced by transformUpdateTargetList().