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 989 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().

990 {
991  *attrnos = NIL;
992 
993  if (cols == NIL)
994  {
995  /*
996  * Generate default column list for INSERT.
997  */
998  int numcol = RelationGetNumberOfAttributes(pstate->p_target_relation);
999 
1000  int i;
1001 
1002  for (i = 0; i < numcol; i++)
1003  {
1004  ResTarget *col;
1005  Form_pg_attribute attr;
1006 
1007  attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
1008 
1009  if (attr->attisdropped)
1010  continue;
1011 
1012  col = makeNode(ResTarget);
1013  col->name = pstrdup(NameStr(attr->attname));
1014  col->indirection = NIL;
1015  col->val = NULL;
1016  col->location = -1;
1017  cols = lappend(cols, col);
1018  *attrnos = lappend_int(*attrnos, i + 1);
1019  }
1020  }
1021  else
1022  {
1023  /*
1024  * Do initial validation of user-supplied INSERT column list.
1025  */
1026  Bitmapset *wholecols = NULL;
1027  Bitmapset *partialcols = NULL;
1028  ListCell *tl;
1029 
1030  foreach(tl, cols)
1031  {
1032  ResTarget *col = (ResTarget *) lfirst(tl);
1033  char *name = col->name;
1034  int attrno;
1035 
1036  /* Lookup column name, ereport on failure */
1037  attrno = attnameAttNum(pstate->p_target_relation, name, false);
1038  if (attrno == InvalidAttrNumber)
1039  ereport(ERROR,
1040  (errcode(ERRCODE_UNDEFINED_COLUMN),
1041  errmsg("column \"%s\" of relation \"%s\" does not exist",
1042  name,
1044  parser_errposition(pstate, col->location)));
1045 
1046  /*
1047  * Check for duplicates, but only of whole columns --- we allow
1048  * INSERT INTO foo (col.subcol1, col.subcol2)
1049  */
1050  if (col->indirection == NIL)
1051  {
1052  /* whole column; must not have any other assignment */
1053  if (bms_is_member(attrno, wholecols) ||
1054  bms_is_member(attrno, partialcols))
1055  ereport(ERROR,
1056  (errcode(ERRCODE_DUPLICATE_COLUMN),
1057  errmsg("column \"%s\" specified more than once",
1058  name),
1059  parser_errposition(pstate, col->location)));
1060  wholecols = bms_add_member(wholecols, attrno);
1061  }
1062  else
1063  {
1064  /* partial column; must not have any whole assignment */
1065  if (bms_is_member(attrno, wholecols))
1066  ereport(ERROR,
1067  (errcode(ERRCODE_DUPLICATE_COLUMN),
1068  errmsg("column \"%s\" specified more than once",
1069  name),
1070  parser_errposition(pstate, col->location)));
1071  partialcols = bms_add_member(partialcols, attrno);
1072  }
1073 
1074  *attrnos = lappend_int(*attrnos, attrno);
1075  }
1076  }
1077 
1078  return cols;
1079 }
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:422
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1161
int errcode(int sqlerrcode)
Definition: elog.c:570
int location
Definition: parsenodes.h:443
#define ERROR
Definition: elog.h:43
#define RelationGetRelationName(relation)
Definition: rel.h:450
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:339
List * lappend(List *list, void *datum)
Definition: list.c:321
TupleDesc rd_att
Definition: rel.h:84
#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:111
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:784
Relation p_target_relation
Definition: parse_node.h:187
int i
#define NameStr(name)
Definition: c.h:609
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 1489 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().

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

◆ FigureColname()

char* FigureColname ( Node node)

Definition at line 1663 of file parse_target.c.

References FigureColnameInternal(), and name.

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

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

◆ FigureIndexColname()

char* FigureIndexColname ( Node node)

Definition at line 1682 of file parse_target.c.

References FigureColnameInternal(), and name.

Referenced by transformIndexStmt().

1683 {
1684  char *name = NULL;
1685 
1686  (void) FigureColnameInternal(node, &name);
1687  return name;
1688 }
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:167
#define lfirst(lc)
Definition: pg_list.h:190
Expr * expr
Definition: primnodes.h:1393
static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle, Var *var, int levelsup)
Definition: parse_target.c:353

◆ 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:1393
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42

◆ transformAssignedExpr()

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

Definition at line 458 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(), make_var(), makeNullConst(), ParseState::p_expr_kind, ParseState::p_is_insert, ParseState::p_target_rangetblentry, ParseState::p_target_relation, parser_errposition(), RelationData::rd_att, transformAssignmentIndirection(), TupleDescAttr, SetToDefault::typeId, and SetToDefault::typeMod.

Referenced by transformInsertRow(), and updateTargetListEntry().

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

◆ 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:515
List * p_multiassign_exprs
Definition: parse_node.h:193
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:321
#define Assert(condition)
Definition: c.h:732
#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:192
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:515
List * p_multiassign_exprs
Definition: parse_node.h:193
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:321
#define Assert(condition)
Definition: c.h:732
#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 621 of file parse_target.c.

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

Referenced by transformUpdateTargetList().

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