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

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

Referenced by transformInsertStmt().

985 {
986  *attrnos = NIL;
987 
988  if (cols == NIL)
989  {
990  /*
991  * Generate default column list for INSERT.
992  */
993  int numcol = RelationGetNumberOfAttributes(pstate->p_target_relation);
994 
995  int i;
996 
997  for (i = 0; i < numcol; i++)
998  {
999  ResTarget *col;
1000  Form_pg_attribute attr;
1001 
1002  attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
1003 
1004  if (attr->attisdropped)
1005  continue;
1006 
1007  col = makeNode(ResTarget);
1008  col->name = pstrdup(NameStr(attr->attname));
1009  col->indirection = NIL;
1010  col->val = NULL;
1011  col->location = -1;
1012  cols = lappend(cols, col);
1013  *attrnos = lappend_int(*attrnos, i + 1);
1014  }
1015  }
1016  else
1017  {
1018  /*
1019  * Do initial validation of user-supplied INSERT column list.
1020  */
1021  Bitmapset *wholecols = NULL;
1022  Bitmapset *partialcols = NULL;
1023  ListCell *tl;
1024 
1025  foreach(tl, cols)
1026  {
1027  ResTarget *col = (ResTarget *) lfirst(tl);
1028  char *name = col->name;
1029  int attrno;
1030 
1031  /* Lookup column name, ereport on failure */
1032  attrno = attnameAttNum(pstate->p_target_relation, name, false);
1033  if (attrno == InvalidAttrNumber)
1034  ereport(ERROR,
1035  (errcode(ERRCODE_UNDEFINED_COLUMN),
1036  errmsg("column \"%s\" of relation \"%s\" does not exist",
1037  name,
1039  parser_errposition(pstate, col->location)));
1040 
1041  /*
1042  * Check for duplicates, but only of whole columns --- we allow
1043  * INSERT INTO foo (col.subcol1, col.subcol2)
1044  */
1045  if (col->indirection == NIL)
1046  {
1047  /* whole column; must not have any other assignment */
1048  if (bms_is_member(attrno, wholecols) ||
1049  bms_is_member(attrno, partialcols))
1050  ereport(ERROR,
1051  (errcode(ERRCODE_DUPLICATE_COLUMN),
1052  errmsg("column \"%s\" specified more than once",
1053  name),
1054  parser_errposition(pstate, col->location)));
1055  wholecols = bms_add_member(wholecols, attrno);
1056  }
1057  else
1058  {
1059  /* partial column; must not have any whole assignment */
1060  if (bms_is_member(attrno, wholecols))
1061  ereport(ERROR,
1062  (errcode(ERRCODE_DUPLICATE_COLUMN),
1063  errmsg("column \"%s\" specified more than once",
1064  name),
1065  parser_errposition(pstate, col->location)));
1066  partialcols = bms_add_member(partialcols, attrno);
1067  }
1068 
1069  *attrnos = lappend_int(*attrnos, attrno);
1070  }
1071  }
1072 
1073  return cols;
1074 }
List * indirection
Definition: parsenodes.h:441
#define NIL
Definition: pg_list.h:65
Node * val
Definition: parsenodes.h:442
char * name
Definition: parsenodes.h:440
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:434
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1186
int errcode(int sqlerrcode)
Definition: elog.c:608
int location
Definition: parsenodes.h:443
#define ERROR
Definition: elog.h:43
#define RelationGetRelationName(relation)
Definition: rel.h:462
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend_int(List *list, int datum)
Definition: list.c:340
List * lappend(List *list, void *datum)
Definition: list.c:322
TupleDesc rd_att
Definition: rel.h:85
#define makeNode(_type_)
Definition: nodes.h:573
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
#define lfirst(lc)
Definition: pg_list.h:190
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
const char * name
Definition: encode.c:521
#define InvalidAttrNumber
Definition: attnum.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:822
Relation p_target_relation
Definition: parse_node.h:190
int i
#define NameStr(name)
Definition: c.h:615
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ expandRecordVariable()

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

Definition at line 1478 of file parse_target.c.

References Alias::aliasname, Assert, attnum, CreateTemplateTupleDesc(), RangeTblEntry::ctelevelsup, CommonTableExpr::ctequery, elog, RangeTblEntry::eref, ERROR, expandRecordVariable(), expandRTE(), TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), forboth, get_expr_result_tupdesc(), get_tle_by_resno(), GetCTEForRTE(), GetCTETargetList, GetRTEByRangeTablePosn(), i, InvalidAttrNumber, IsA, RangeTblEntry::joinaliasvars, label, lfirst, list_length(), list_nth(), Var::location, MemSet, ParseState::p_rtable, ParseState::parentParseState, TargetEntry::resjunk, 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, Var::varno, and Var::vartype.

Referenced by expandRecordVariable(), ExpandRowReference(), and ParseComplexProjection().

1479 {
1480  TupleDesc tupleDesc;
1481  int netlevelsup;
1482  RangeTblEntry *rte;
1484  Node *expr;
1485 
1486  /* Check my caller didn't mess up */
1487  Assert(IsA(var, Var));
1488  Assert(var->vartype == RECORDOID);
1489 
1490  /*
1491  * Note: it's tempting to use GetNSItemByRangeTablePosn here so that we
1492  * can use expandNSItemVars instead of expandRTE; but that does not work
1493  * for some of the recursion cases below, where we have consed up a
1494  * ParseState that lacks p_namespace data.
1495  */
1496  netlevelsup = var->varlevelsup + levelsup;
1497  rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
1498  attnum = var->varattno;
1499 
1500  if (attnum == InvalidAttrNumber)
1501  {
1502  /* Whole-row reference to an RTE, so expand the known fields */
1503  List *names,
1504  *vars;
1505  ListCell *lname,
1506  *lvar;
1507  int i;
1508 
1509  expandRTE(rte, var->varno, 0, var->location, false,
1510  &names, &vars);
1511 
1512  tupleDesc = CreateTemplateTupleDesc(list_length(vars));
1513  i = 1;
1514  forboth(lname, names, lvar, vars)
1515  {
1516  char *label = strVal(lfirst(lname));
1517  Node *varnode = (Node *) lfirst(lvar);
1518 
1519  TupleDescInitEntry(tupleDesc, i,
1520  label,
1521  exprType(varnode),
1522  exprTypmod(varnode),
1523  0);
1524  TupleDescInitEntryCollation(tupleDesc, i,
1525  exprCollation(varnode));
1526  i++;
1527  }
1528  Assert(lname == NULL && lvar == NULL); /* lists same length? */
1529 
1530  return tupleDesc;
1531  }
1532 
1533  expr = (Node *) var; /* default if we can't drill down */
1534 
1535  switch (rte->rtekind)
1536  {
1537  case RTE_RELATION:
1538  case RTE_VALUES:
1539  case RTE_NAMEDTUPLESTORE:
1540  case RTE_RESULT:
1541 
1542  /*
1543  * This case should not occur: a column of a table, values list,
1544  * or ENR shouldn't have type RECORD. Fall through and fail (most
1545  * likely) at the bottom.
1546  */
1547  break;
1548  case RTE_SUBQUERY:
1549  {
1550  /* Subselect-in-FROM: examine sub-select's output expr */
1552  attnum);
1553 
1554  if (ste == NULL || ste->resjunk)
1555  elog(ERROR, "subquery %s does not have attribute %d",
1556  rte->eref->aliasname, attnum);
1557  expr = (Node *) ste->expr;
1558  if (IsA(expr, Var))
1559  {
1560  /*
1561  * Recurse into the sub-select to see what its Var refers
1562  * to. We have to build an additional level of ParseState
1563  * to keep in step with varlevelsup in the subselect.
1564  */
1565  ParseState mypstate;
1566 
1567  MemSet(&mypstate, 0, sizeof(mypstate));
1568  mypstate.parentParseState = pstate;
1569  mypstate.p_rtable = rte->subquery->rtable;
1570  /* don't bother filling the rest of the fake pstate */
1571 
1572  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1573  }
1574  /* else fall through to inspect the expression */
1575  }
1576  break;
1577  case RTE_JOIN:
1578  /* Join RTE --- recursively inspect the alias variable */
1579  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
1580  expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1581  Assert(expr != NULL);
1582  /* We intentionally don't strip implicit coercions here */
1583  if (IsA(expr, Var))
1584  return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
1585  /* else fall through to inspect the expression */
1586  break;
1587  case RTE_FUNCTION:
1588 
1589  /*
1590  * We couldn't get here unless a function is declared with one of
1591  * its result columns as RECORD, which is not allowed.
1592  */
1593  break;
1594  case RTE_TABLEFUNC:
1595 
1596  /*
1597  * Table function cannot have columns with RECORD type.
1598  */
1599  break;
1600  case RTE_CTE:
1601  /* CTE reference: examine subquery's output expr */
1602  if (!rte->self_reference)
1603  {
1604  CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
1605  TargetEntry *ste;
1606 
1607  ste = get_tle_by_resno(GetCTETargetList(cte), attnum);
1608  if (ste == NULL || ste->resjunk)
1609  elog(ERROR, "subquery %s does not have attribute %d",
1610  rte->eref->aliasname, attnum);
1611  expr = (Node *) ste->expr;
1612  if (IsA(expr, Var))
1613  {
1614  /*
1615  * Recurse into the CTE to see what its Var refers to. We
1616  * have to build an additional level of ParseState to keep
1617  * in step with varlevelsup in the CTE; furthermore it
1618  * could be an outer CTE.
1619  */
1620  ParseState mypstate;
1621  Index levelsup;
1622 
1623  MemSet(&mypstate, 0, sizeof(mypstate));
1624  /* this loop must work, since GetCTEForRTE did */
1625  for (levelsup = 0;
1626  levelsup < rte->ctelevelsup + netlevelsup;
1627  levelsup++)
1628  pstate = pstate->parentParseState;
1629  mypstate.parentParseState = pstate;
1630  mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
1631  /* don't bother filling the rest of the fake pstate */
1632 
1633  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1634  }
1635  /* else fall through to inspect the expression */
1636  }
1637  break;
1638  }
1639 
1640  /*
1641  * We now have an expression we can't expand any more, so see if
1642  * get_expr_result_tupdesc() can do anything with it.
1643  */
1644  return get_expr_result_tupdesc(expr, false);
1645 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
List * joinaliasvars
Definition: parsenodes.h:1049
Index varlevelsup
Definition: primnodes.h:191
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
ParseState * parentParseState
Definition: parse_node.h:178
CommonTableExpr * GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
Definition: nodes.h:525
#define strVal(v)
Definition: value.h:54
#define MemSet(start, val, len)
Definition: c.h:971
AttrNumber varattno
Definition: primnodes.h:186
Definition: primnodes.h:181
List * targetList
Definition: parsenodes.h:140
bool resjunk
Definition: primnodes.h:1414
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:43
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:429
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
Oid vartype
Definition: primnodes.h:188
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:769
int location
Definition: primnodes.h:196
TupleDesc expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
Index varno
Definition: primnodes.h:184
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
bool self_reference
Definition: parsenodes.h:1079
static char * label
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
unsigned int Index
Definition: c.h:475
int16 attnum
Definition: pg_attribute.h:79
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
Expr * expr
Definition: primnodes.h:1407
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
Index ctelevelsup
Definition: parsenodes.h:1078
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
#define elog(elevel,...)
Definition: elog.h:228
int i
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
Alias * eref
Definition: parsenodes.h:1113
Definition: regcomp.c:224
Definition: pg_list.h:50
#define GetCTETargetList(cte)
Definition: parsenodes.h:1465
int16 AttrNumber
Definition: attnum.h:21
List * p_rtable
Definition: parse_node.h:180

◆ FigureColname()

char* FigureColname ( Node node)

Definition at line 1658 of file parse_target.c.

References FigureColnameInternal(), and name.

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

1659 {
1660  char *name = NULL;
1661 
1662  (void) FigureColnameInternal(node, &name);
1663  if (name != NULL)
1664  return name;
1665  /* default result if we can't guess anything */
1666  return "?column?";
1667 }
static int FigureColnameInternal(Node *node, char **name)
const char * name
Definition: encode.c:521

◆ FigureIndexColname()

char* FigureIndexColname ( Node node)

Definition at line 1677 of file parse_target.c.

References FigureColnameInternal(), and name.

Referenced by transformIndexStmt().

1678 {
1679  char *name = NULL;
1680 
1681  (void) FigureColnameInternal(node, &name);
1682  return name;
1683 }
static int FigureColnameInternal(Node *node, char **name)
const char * name
Definition: encode.c:521

◆ markTargetListOrigins()

void markTargetListOrigins ( ParseState pstate,
List targetlist 
)

Definition at line 331 of file parse_target.c.

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

Referenced by transformReturningList(), and transformSelectStmt().

332 {
333  ListCell *l;
334 
335  foreach(l, targetlist)
336  {
337  TargetEntry *tle = (TargetEntry *) lfirst(l);
338 
339  markTargetListOrigin(pstate, tle, (Var *) tle->expr, 0);
340  }
341 }
Definition: primnodes.h:181
#define lfirst(lc)
Definition: pg_list.h:190
Expr * expr
Definition: primnodes.h:1407
static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle, Var *var, int levelsup)
Definition: parse_target.c:356

◆ resolveTargetListUnknowns()

void resolveTargetListUnknowns ( ParseState pstate,
List targetlist 
)

Definition at line 301 of file parse_target.c.

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

Referenced by transformReturningList(), and transformSelectStmt().

302 {
303  ListCell *l;
304 
305  foreach(l, targetlist)
306  {
307  TargetEntry *tle = (TargetEntry *) lfirst(l);
308  Oid restype = exprType((Node *) tle->expr);
309 
310  if (restype == UNKNOWNOID)
311  {
312  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
313  restype, TEXTOID, -1,
316  -1);
317  }
318  }
319 }
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:156
Definition: nodes.h:525
unsigned int Oid
Definition: postgres_ext.h:31
#define lfirst(lc)
Definition: pg_list.h:190
Expr * expr
Definition: primnodes.h:1407
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41

◆ transformAssignedExpr()

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

Definition at line 450 of file parse_target.c.

References Assert, attnumTypeId(), COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, SetToDefault::collation, 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, SetToDefault::typeId, and SetToDefault::typeMod.

Referenced by transformInsertRow(), and updateTargetListEntry().

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

◆ transformExpressionList()

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

Definition at line 230 of file parse_target.c.

References Assert, ExpandColumnRefStar(), ExpandIndirectionStar(), ColumnRef::fields, A_Indirection::indirection, IsA, lappend(), lfirst, list_concat(), llast, NIL, ParseState::p_multiassign_exprs, and transformExpr().

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

232 {
233  List *result = NIL;
234  ListCell *lc;
235 
236  foreach(lc, exprlist)
237  {
238  Node *e = (Node *) lfirst(lc);
239 
240  /*
241  * Check for "something.*". Depending on the complexity of the
242  * "something", the star could appear as the last field in ColumnRef,
243  * or as the last indirection item in A_Indirection.
244  */
245  if (IsA(e, ColumnRef))
246  {
247  ColumnRef *cref = (ColumnRef *) e;
248 
249  if (IsA(llast(cref->fields), A_Star))
250  {
251  /* It is something.*, expand into multiple items */
252  result = list_concat(result,
253  ExpandColumnRefStar(pstate, cref,
254  false));
255  continue;
256  }
257  }
258  else if (IsA(e, A_Indirection))
259  {
261 
262  if (IsA(llast(ind->indirection), A_Star))
263  {
264  /* It is something.*, expand into multiple items */
265  result = list_concat(result,
266  ExpandIndirectionStar(pstate, ind,
267  false, exprKind));
268  continue;
269  }
270  }
271 
272  /*
273  * Not "something.*", so transform as a single expression. If it's a
274  * SetToDefault node and we should allow that, pass it through
275  * unmodified. (transformExpr will throw the appropriate error if
276  * we're disallowing it.)
277  */
278  if (allowDefault && IsA(e, SetToDefault))
279  /* do nothing */ ;
280  else
281  e = transformExpr(pstate, e, exprKind);
282 
283  result = lappend(result, e);
284  }
285 
286  /* Shouldn't have any multiassign items here */
287  Assert(pstate->p_multiassign_exprs == NIL);
288 
289  return result;
290 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:145
#define llast(l)
Definition: pg_list.h:215
Definition: nodes.h:525
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
List * p_multiassign_exprs
Definition: parse_node.h:196
static List * ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref, bool make_target_entry)
struct A_Star A_Star
static List * ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind, bool make_target_entry, ParseExprKind exprKind)
List * lappend(List *list, void *datum)
Definition: list.c:322
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
List * indirection
Definition: parsenodes.h:406
e
Definition: preproc-init.c:82
Definition: pg_list.h:50
List * fields
Definition: parsenodes.h:235

◆ transformTargetEntry()

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

Definition at line 87 of file parse_target.c.

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

Referenced by findTargetlistEntrySQL99(), and transformTargetList().

93 {
94  /* Transform the node if caller didn't do it already */
95  if (expr == NULL)
96  {
97  /*
98  * If it's a SetToDefault node and we should allow that, pass it
99  * through unmodified. (transformExpr will throw the appropriate
100  * error if we're disallowing it.)
101  */
102  if (exprKind == EXPR_KIND_UPDATE_SOURCE && IsA(node, SetToDefault))
103  expr = node;
104  else
105  expr = transformExpr(pstate, node, exprKind);
106  }
107 
108  if (colname == NULL && !resjunk)
109  {
110  /*
111  * Generate a suitable column name for a column without any explicit
112  * 'AS ColumnName' clause.
113  */
114  colname = FigureColname(node);
115  }
116 
117  return makeTargetEntry((Expr *) expr,
118  (AttrNumber) pstate->p_next_resno++,
119  colname,
120  resjunk);
121 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:145
char * FigureColname(Node *node)
int p_next_resno
Definition: parse_node.h:195
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:236
int16 AttrNumber
Definition: attnum.h:21

◆ transformTargetList()

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

Definition at line 133 of file parse_target.c.

References Assert, ExpandColumnRefStar(), ExpandIndirectionStar(), EXPR_KIND_UPDATE_SOURCE, ColumnRef::fields, A_Indirection::indirection, IsA, lappend(), lfirst, list_concat(), llast, ResTarget::name, NIL, ParseState::p_multiassign_exprs, transformTargetEntry(), and ResTarget::val.

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

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

◆ updateTargetListEntry()

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

Definition at line 616 of file parse_target.c.

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

Referenced by transformUpdateTargetList().

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