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 972 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, RelationData::rd_rel, RelationGetRelationName, TupleDescAttr, and ResTarget::val.

Referenced by transformInsertStmt().

973 {
974  *attrnos = NIL;
975 
976  if (cols == NIL)
977  {
978  /*
979  * Generate default column list for INSERT.
980  */
981  int numcol = pstate->p_target_relation->rd_rel->relnatts;
982  int i;
983 
984  for (i = 0; i < numcol; i++)
985  {
986  ResTarget *col;
987  Form_pg_attribute attr;
988 
989  attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
990 
991  if (attr->attisdropped)
992  continue;
993 
994  col = makeNode(ResTarget);
995  col->name = pstrdup(NameStr(attr->attname));
996  col->indirection = NIL;
997  col->val = NULL;
998  col->location = -1;
999  cols = lappend(cols, col);
1000  *attrnos = lappend_int(*attrnos, i + 1);
1001  }
1002  }
1003  else
1004  {
1005  /*
1006  * Do initial validation of user-supplied INSERT column list.
1007  */
1008  Bitmapset *wholecols = NULL;
1009  Bitmapset *partialcols = NULL;
1010  ListCell *tl;
1011 
1012  foreach(tl, cols)
1013  {
1014  ResTarget *col = (ResTarget *) lfirst(tl);
1015  char *name = col->name;
1016  int attrno;
1017 
1018  /* Lookup column name, ereport on failure */
1019  attrno = attnameAttNum(pstate->p_target_relation, name, false);
1020  if (attrno == InvalidAttrNumber)
1021  ereport(ERROR,
1022  (errcode(ERRCODE_UNDEFINED_COLUMN),
1023  errmsg("column \"%s\" of relation \"%s\" does not exist",
1024  name,
1026  parser_errposition(pstate, col->location)));
1027 
1028  /*
1029  * Check for duplicates, but only of whole columns --- we allow
1030  * INSERT INTO foo (col.subcol1, col.subcol2)
1031  */
1032  if (col->indirection == NIL)
1033  {
1034  /* whole column; must not have any other assignment */
1035  if (bms_is_member(attrno, wholecols) ||
1036  bms_is_member(attrno, partialcols))
1037  ereport(ERROR,
1038  (errcode(ERRCODE_DUPLICATE_COLUMN),
1039  errmsg("column \"%s\" specified more than once",
1040  name),
1041  parser_errposition(pstate, col->location)));
1042  wholecols = bms_add_member(wholecols, attrno);
1043  }
1044  else
1045  {
1046  /* partial column; must not have any whole assignment */
1047  if (bms_is_member(attrno, wholecols))
1048  ereport(ERROR,
1049  (errcode(ERRCODE_DUPLICATE_COLUMN),
1050  errmsg("column \"%s\" specified more than once",
1051  name),
1052  parser_errposition(pstate, col->location)));
1053  partialcols = bms_add_member(partialcols, attrno);
1054  }
1055 
1056  *attrnos = lappend_int(*attrnos, attrno);
1057  }
1058  }
1059 
1060  return cols;
1061 }
List * indirection
Definition: parsenodes.h:440
#define NIL
Definition: pg_list.h:69
Node * val
Definition: parsenodes.h:441
char * name
Definition: parsenodes.h:439
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
char * pstrdup(const char *in)
Definition: mcxt.c:1063
int errcode(int sqlerrcode)
Definition: elog.c:575
Form_pg_class rd_rel
Definition: rel.h:114
int location
Definition: parsenodes.h:442
#define ERROR
Definition: elog.h:43
#define RelationGetRelationName(relation)
Definition: rel.h:445
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
TupleDesc rd_att
Definition: rel.h:115
#define makeNode(_type_)
Definition: nodes.h:560
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
const char * name
Definition: encode.c:521
#define InvalidAttrNumber
Definition: attnum.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation p_target_relation
Definition: parse_node.h:183
int i
#define NameStr(name)
Definition: c.h:557
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420

◆ expandRecordVariable()

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

Definition at line 1471 of file parse_target.c.

References Alias::aliasname, Assert, 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, RECORDOID, TargetEntry::resjunk, Query::rtable, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, 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().

1472 {
1474  int netlevelsup;
1475  RangeTblEntry *rte;
1476  AttrNumber attnum;
1477  Node *expr;
1478 
1479  /* Check my caller didn't mess up */
1480  Assert(IsA(var, Var));
1481  Assert(var->vartype == RECORDOID);
1482 
1483  netlevelsup = var->varlevelsup + levelsup;
1484  rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
1485  attnum = var->varattno;
1486 
1487  if (attnum == InvalidAttrNumber)
1488  {
1489  /* Whole-row reference to an RTE, so expand the known fields */
1490  List *names,
1491  *vars;
1492  ListCell *lname,
1493  *lvar;
1494  int i;
1495 
1496  expandRTE(rte, var->varno, 0, var->location, false,
1497  &names, &vars);
1498 
1499  tupleDesc = CreateTemplateTupleDesc(list_length(vars), false);
1500  i = 1;
1501  forboth(lname, names, lvar, vars)
1502  {
1503  char *label = strVal(lfirst(lname));
1504  Node *varnode = (Node *) lfirst(lvar);
1505 
1506  TupleDescInitEntry(tupleDesc, i,
1507  label,
1508  exprType(varnode),
1509  exprTypmod(varnode),
1510  0);
1511  TupleDescInitEntryCollation(tupleDesc, i,
1512  exprCollation(varnode));
1513  i++;
1514  }
1515  Assert(lname == NULL && lvar == NULL); /* lists same length? */
1516 
1517  return tupleDesc;
1518  }
1519 
1520  expr = (Node *) var; /* default if we can't drill down */
1521 
1522  switch (rte->rtekind)
1523  {
1524  case RTE_RELATION:
1525  case RTE_VALUES:
1526  case RTE_NAMEDTUPLESTORE:
1527 
1528  /*
1529  * This case should not occur: a column of a table, values list,
1530  * or ENR shouldn't have type RECORD. Fall through and fail (most
1531  * likely) at the bottom.
1532  */
1533  break;
1534  case RTE_SUBQUERY:
1535  {
1536  /* Subselect-in-FROM: examine sub-select's output expr */
1538  attnum);
1539 
1540  if (ste == NULL || ste->resjunk)
1541  elog(ERROR, "subquery %s does not have attribute %d",
1542  rte->eref->aliasname, attnum);
1543  expr = (Node *) ste->expr;
1544  if (IsA(expr, Var))
1545  {
1546  /*
1547  * Recurse into the sub-select to see what its Var refers
1548  * to. We have to build an additional level of ParseState
1549  * to keep in step with varlevelsup in the subselect.
1550  */
1551  ParseState mypstate;
1552 
1553  MemSet(&mypstate, 0, sizeof(mypstate));
1554  mypstate.parentParseState = pstate;
1555  mypstate.p_rtable = rte->subquery->rtable;
1556  /* don't bother filling the rest of the fake pstate */
1557 
1558  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1559  }
1560  /* else fall through to inspect the expression */
1561  }
1562  break;
1563  case RTE_JOIN:
1564  /* Join RTE --- recursively inspect the alias variable */
1565  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
1566  expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1567  Assert(expr != NULL);
1568  /* We intentionally don't strip implicit coercions here */
1569  if (IsA(expr, Var))
1570  return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
1571  /* else fall through to inspect the expression */
1572  break;
1573  case RTE_FUNCTION:
1574 
1575  /*
1576  * We couldn't get here unless a function is declared with one of
1577  * its result columns as RECORD, which is not allowed.
1578  */
1579  break;
1580  case RTE_TABLEFUNC:
1581 
1582  /*
1583  * Table function cannot have columns with RECORD type.
1584  */
1585  break;
1586  case RTE_CTE:
1587  /* CTE reference: examine subquery's output expr */
1588  if (!rte->self_reference)
1589  {
1590  CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
1591  TargetEntry *ste;
1592 
1593  ste = get_tle_by_resno(GetCTETargetList(cte), attnum);
1594  if (ste == NULL || ste->resjunk)
1595  elog(ERROR, "subquery %s does not have attribute %d",
1596  rte->eref->aliasname, attnum);
1597  expr = (Node *) ste->expr;
1598  if (IsA(expr, Var))
1599  {
1600  /*
1601  * Recurse into the CTE to see what its Var refers to. We
1602  * have to build an additional level of ParseState to keep
1603  * in step with varlevelsup in the CTE; furthermore it
1604  * could be an outer CTE.
1605  */
1606  ParseState mypstate;
1607  Index levelsup;
1608 
1609  MemSet(&mypstate, 0, sizeof(mypstate));
1610  /* this loop must work, since GetCTEForRTE did */
1611  for (levelsup = 0;
1612  levelsup < rte->ctelevelsup + netlevelsup;
1613  levelsup++)
1614  pstate = pstate->parentParseState;
1615  mypstate.parentParseState = pstate;
1616  mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
1617  /* don't bother filling the rest of the fake pstate */
1618 
1619  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1620  }
1621  /* else fall through to inspect the expression */
1622  }
1623  break;
1624  }
1625 
1626  /*
1627  * We now have an expression we can't expand any more, so see if
1628  * get_expr_result_tupdesc() can do anything with it.
1629  */
1630  return get_expr_result_tupdesc(expr, false);
1631 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
List * joinaliasvars
Definition: parsenodes.h:995
Index varlevelsup
Definition: primnodes.h:173
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
CommonTableExpr * GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
Definition: nodes.h:512
#define strVal(v)
Definition: value.h:54
#define MemSet(start, val, len)
Definition: c.h:863
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
List * targetList
Definition: parsenodes.h:138
bool resjunk
Definition: primnodes.h:1382
List * rtable
Definition: parsenodes.h:135
#define ERROR
Definition: elog.h:43
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:412
Oid vartype
Definition: primnodes.h:170
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:664
int location
Definition: primnodes.h:178
void * list_nth(const List *list, int n)
Definition: list.c:410
#define RECORDOID
Definition: pg_type.h:680
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:505
Index varno
Definition: primnodes.h:166
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:171
bool self_reference
Definition: parsenodes.h:1023
static char * label
Definition: pg_basebackup.c:82
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
unsigned int Index
Definition: c.h:423
#define Assert(condition)
Definition: c.h:680
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
Expr * expr
Definition: primnodes.h:1375
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
Index ctelevelsup
Definition: parsenodes.h:1022
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:43
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:951
Query * subquery
Definition: parsenodes.h:974
int i
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1055
Definition: regcomp.c:224
Definition: pg_list.h:45
#define GetCTETargetList(cte)
Definition: parsenodes.h:1390
int16 AttrNumber
Definition: attnum.h:21
List * p_rtable
Definition: parse_node.h:173

◆ FigureColname()

char* FigureColname ( Node node)

Definition at line 1644 of file parse_target.c.

References FigureColnameInternal(), and name.

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

1645 {
1646  char *name = NULL;
1647 
1648  (void) FigureColnameInternal(node, &name);
1649  if (name != NULL)
1650  return name;
1651  /* default result if we can't guess anything */
1652  return "?column?";
1653 }
static int FigureColnameInternal(Node *node, char **name)
const char * name
Definition: encode.c:521

◆ FigureIndexColname()

char* FigureIndexColname ( Node node)

Definition at line 1663 of file parse_target.c.

References FigureColnameInternal(), and name.

Referenced by transformIndexStmt().

1664 {
1665  char *name = NULL;
1666 
1667  (void) FigureColnameInternal(node, &name);
1668  return name;
1669 }
static int FigureColnameInternal(Node *node, char **name)
const char * name
Definition: encode.c:521

◆ markTargetListOrigins()

void markTargetListOrigins ( ParseState pstate,
List targetlist 
)

Definition at line 329 of file parse_target.c.

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

Referenced by transformReturningList(), and transformSelectStmt().

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

◆ resolveTargetListUnknowns()

void resolveTargetListUnknowns ( ParseState pstate,
List targetlist 
)

Definition at line 299 of file parse_target.c.

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

Referenced by transformReturningList(), and transformSelectStmt().

300 {
301  ListCell *l;
302 
303  foreach(l, targetlist)
304  {
305  TargetEntry *tle = (TargetEntry *) lfirst(l);
306  Oid restype = exprType((Node *) tle->expr);
307 
308  if (restype == UNKNOWNOID)
309  {
310  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
311  restype, TEXTOID, -1,
314  -1);
315  }
316  }
317 }
#define TEXTOID
Definition: pg_type.h:324
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:512
unsigned int Oid
Definition: postgres_ext.h:31
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1375
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define UNKNOWNOID
Definition: pg_type.h:431

◆ transformAssignedExpr()

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

Definition at line 455 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().

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

◆ transformExpressionList()

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

Definition at line 228 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().

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

◆ transformTargetEntry()

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

Definition at line 85 of file parse_target.c.

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

Referenced by findTargetlistEntrySQL99(), and transformTargetList().

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

◆ transformTargetList()

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

Definition at line 131 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().

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

◆ updateTargetListEntry()

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

Definition at line 617 of file parse_target.c.

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

Referenced by transformUpdateTargetList().

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