PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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, 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

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

Definition at line 951 of file parse_target.c.

References attnameAttNum(), tupleDesc::attrs, 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, NULL, ParseState::p_target_relation, parser_errposition(), pstrdup(), RelationData::rd_att, RelationData::rd_rel, RelationGetRelationName, and ResTarget::val.

Referenced by transformInsertStmt().

952 {
953  *attrnos = NIL;
954 
955  if (cols == NIL)
956  {
957  /*
958  * Generate default column list for INSERT.
959  */
961  int numcol = pstate->p_target_relation->rd_rel->relnatts;
962  int i;
963 
964  for (i = 0; i < numcol; i++)
965  {
966  ResTarget *col;
967 
968  if (attr[i]->attisdropped)
969  continue;
970 
971  col = makeNode(ResTarget);
972  col->name = pstrdup(NameStr(attr[i]->attname));
973  col->indirection = NIL;
974  col->val = NULL;
975  col->location = -1;
976  cols = lappend(cols, col);
977  *attrnos = lappend_int(*attrnos, i + 1);
978  }
979  }
980  else
981  {
982  /*
983  * Do initial validation of user-supplied INSERT column list.
984  */
985  Bitmapset *wholecols = NULL;
986  Bitmapset *partialcols = NULL;
987  ListCell *tl;
988 
989  foreach(tl, cols)
990  {
991  ResTarget *col = (ResTarget *) lfirst(tl);
992  char *name = col->name;
993  int attrno;
994 
995  /* Lookup column name, ereport on failure */
996  attrno = attnameAttNum(pstate->p_target_relation, name, false);
997  if (attrno == InvalidAttrNumber)
998  ereport(ERROR,
999  (errcode(ERRCODE_UNDEFINED_COLUMN),
1000  errmsg("column \"%s\" of relation \"%s\" does not exist",
1001  name,
1003  parser_errposition(pstate, col->location)));
1004 
1005  /*
1006  * Check for duplicates, but only of whole columns --- we allow
1007  * INSERT INTO foo (col.subcol1, col.subcol2)
1008  */
1009  if (col->indirection == NIL)
1010  {
1011  /* whole column; must not have any other assignment */
1012  if (bms_is_member(attrno, wholecols) ||
1013  bms_is_member(attrno, partialcols))
1014  ereport(ERROR,
1015  (errcode(ERRCODE_DUPLICATE_COLUMN),
1016  errmsg("column \"%s\" specified more than once",
1017  name),
1018  parser_errposition(pstate, col->location)));
1019  wholecols = bms_add_member(wholecols, attrno);
1020  }
1021  else
1022  {
1023  /* partial column; must not have any whole assignment */
1024  if (bms_is_member(attrno, wholecols))
1025  ereport(ERROR,
1026  (errcode(ERRCODE_DUPLICATE_COLUMN),
1027  errmsg("column \"%s\" specified more than once",
1028  name),
1029  parser_errposition(pstate, col->location)));
1030  partialcols = bms_add_member(partialcols, attrno);
1031  }
1032 
1033  *attrnos = lappend_int(*attrnos, attrno);
1034  }
1035  }
1036 
1037  return cols;
1038 }
List * indirection
Definition: parsenodes.h:432
#define NIL
Definition: pg_list.h:69
Node * val
Definition: parsenodes.h:433
char * name
Definition: parsenodes.h:431
char * pstrdup(const char *in)
Definition: mcxt.c:1165
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int errcode(int sqlerrcode)
Definition: elog.c:575
Form_pg_class rd_rel
Definition: rel.h:113
int location
Definition: parsenodes.h:434
#define ERROR
Definition: elog.h:43
#define RelationGetRelationName(relation)
Definition: rel.h:433
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
#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:114
#define makeNode(_type_)
Definition: nodes.h:556
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
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:178
int i
#define NameStr(name)
Definition: c.h:495
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:419
TupleDesc expandRecordVariable ( ParseState pstate,
Var var,
int  levelsup 
)

Definition at line 1452 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_type(), get_tle_by_resno(), GetCTEForRTE(), GetCTETargetList, GetRTEByRangeTablePosn(), i, InvalidAttrNumber, IsA, RangeTblEntry::joinaliasvars, label, lfirst, list_length(), list_nth(), Var::location, lookup_rowtype_tupdesc_copy(), MemSet, NULL, ParseState::p_rtable, ParseState::parentParseState, RECORDOID, TargetEntry::resjunk, Query::rtable, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_RELATION, RTE_SUBQUERY, RTE_VALUES, RangeTblEntry::rtekind, RangeTblEntry::self_reference, strVal, RangeTblEntry::subquery, Query::targetList, TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, Var::varattno, Var::varlevelsup, Var::varno, and Var::vartype.

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

1453 {
1455  int netlevelsup;
1456  RangeTblEntry *rte;
1457  AttrNumber attnum;
1458  Node *expr;
1459 
1460  /* Check my caller didn't mess up */
1461  Assert(IsA(var, Var));
1462  Assert(var->vartype == RECORDOID);
1463 
1464  netlevelsup = var->varlevelsup + levelsup;
1465  rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
1466  attnum = var->varattno;
1467 
1468  if (attnum == InvalidAttrNumber)
1469  {
1470  /* Whole-row reference to an RTE, so expand the known fields */
1471  List *names,
1472  *vars;
1473  ListCell *lname,
1474  *lvar;
1475  int i;
1476 
1477  expandRTE(rte, var->varno, 0, var->location, false,
1478  &names, &vars);
1479 
1480  tupleDesc = CreateTemplateTupleDesc(list_length(vars), false);
1481  i = 1;
1482  forboth(lname, names, lvar, vars)
1483  {
1484  char *label = strVal(lfirst(lname));
1485  Node *varnode = (Node *) lfirst(lvar);
1486 
1487  TupleDescInitEntry(tupleDesc, i,
1488  label,
1489  exprType(varnode),
1490  exprTypmod(varnode),
1491  0);
1492  TupleDescInitEntryCollation(tupleDesc, i,
1493  exprCollation(varnode));
1494  i++;
1495  }
1496  Assert(lname == NULL && lvar == NULL); /* lists same length? */
1497 
1498  return tupleDesc;
1499  }
1500 
1501  expr = (Node *) var; /* default if we can't drill down */
1502 
1503  switch (rte->rtekind)
1504  {
1505  case RTE_RELATION:
1506  case RTE_VALUES:
1507 
1508  /*
1509  * This case should not occur: a column of a table or values list
1510  * shouldn't have type RECORD. Fall through and fail (most
1511  * likely) at the bottom.
1512  */
1513  break;
1514  case RTE_SUBQUERY:
1515  {
1516  /* Subselect-in-FROM: examine sub-select's output expr */
1518  attnum);
1519 
1520  if (ste == NULL || ste->resjunk)
1521  elog(ERROR, "subquery %s does not have attribute %d",
1522  rte->eref->aliasname, attnum);
1523  expr = (Node *) ste->expr;
1524  if (IsA(expr, Var))
1525  {
1526  /*
1527  * Recurse into the sub-select to see what its Var refers
1528  * to. We have to build an additional level of ParseState
1529  * to keep in step with varlevelsup in the subselect.
1530  */
1531  ParseState mypstate;
1532 
1533  MemSet(&mypstate, 0, sizeof(mypstate));
1534  mypstate.parentParseState = pstate;
1535  mypstate.p_rtable = rte->subquery->rtable;
1536  /* don't bother filling the rest of the fake pstate */
1537 
1538  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1539  }
1540  /* else fall through to inspect the expression */
1541  }
1542  break;
1543  case RTE_JOIN:
1544  /* Join RTE --- recursively inspect the alias variable */
1545  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
1546  expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1547  Assert(expr != NULL);
1548  /* We intentionally don't strip implicit coercions here */
1549  if (IsA(expr, Var))
1550  return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
1551  /* else fall through to inspect the expression */
1552  break;
1553  case RTE_FUNCTION:
1554 
1555  /*
1556  * We couldn't get here unless a function is declared with one of
1557  * its result columns as RECORD, which is not allowed.
1558  */
1559  break;
1560  case RTE_CTE:
1561  /* CTE reference: examine subquery's output expr */
1562  if (!rte->self_reference)
1563  {
1564  CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
1565  TargetEntry *ste;
1566 
1567  ste = get_tle_by_resno(GetCTETargetList(cte), attnum);
1568  if (ste == NULL || ste->resjunk)
1569  elog(ERROR, "subquery %s does not have attribute %d",
1570  rte->eref->aliasname, attnum);
1571  expr = (Node *) ste->expr;
1572  if (IsA(expr, Var))
1573  {
1574  /*
1575  * Recurse into the CTE to see what its Var refers to. We
1576  * have to build an additional level of ParseState to keep
1577  * in step with varlevelsup in the CTE; furthermore it
1578  * could be an outer CTE.
1579  */
1580  ParseState mypstate;
1581  Index levelsup;
1582 
1583  MemSet(&mypstate, 0, sizeof(mypstate));
1584  /* this loop must work, since GetCTEForRTE did */
1585  for (levelsup = 0;
1586  levelsup < rte->ctelevelsup + netlevelsup;
1587  levelsup++)
1588  pstate = pstate->parentParseState;
1589  mypstate.parentParseState = pstate;
1590  mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
1591  /* don't bother filling the rest of the fake pstate */
1592 
1593  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1594  }
1595  /* else fall through to inspect the expression */
1596  }
1597  break;
1598  }
1599 
1600  /*
1601  * We now have an expression we can't expand any more, so see if
1602  * get_expr_result_type() can do anything with it. If not, pass to
1603  * lookup_rowtype_tupdesc() which will probably fail, but will give an
1604  * appropriate error message while failing.
1605  */
1606  if (get_expr_result_type(expr, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
1607  tupleDesc = lookup_rowtype_tupdesc_copy(exprType(expr),
1608  exprTypmod(expr));
1609 
1610  return tupleDesc;
1611 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
List * joinaliasvars
Definition: parsenodes.h:921
Index varlevelsup
Definition: primnodes.h:151
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
CommonTableExpr * GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
Definition: nodes.h:508
#define strVal(v)
Definition: value.h:54
#define MemSet(start, val, len)
Definition: c.h:853
AttrNumber varattno
Definition: primnodes.h:146
Definition: primnodes.h:141
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
List * targetList
Definition: parsenodes.h:131
bool resjunk
Definition: primnodes.h:1337
List * rtable
Definition: parsenodes.h:128
#define ERROR
Definition: elog.h:43
Oid vartype
Definition: primnodes.h:148
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:650
int location
Definition: primnodes.h:156
void * list_nth(const List *list, int n)
Definition: list.c:410
#define RECORDOID
Definition: pg_type.h:668
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:493
Index varno
Definition: primnodes.h:144
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:166
bool self_reference
Definition: parsenodes.h:944
static char * label
Definition: pg_basebackup.c:84
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
unsigned int Index
Definition: c.h:362
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:41
Expr * expr
Definition: primnodes.h:1330
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:745
Index ctelevelsup
Definition: parsenodes.h:943
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:882
Query * subquery
Definition: parsenodes.h:900
int i
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:961
Definition: regcomp.c:226
Definition: pg_list.h:45
#define GetCTETargetList(cte)
Definition: parsenodes.h:1296
int16 AttrNumber
Definition: attnum.h:21
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1279
List * p_rtable
Definition: parse_node.h:168
char* FigureColname ( Node node)

Definition at line 1624 of file parse_target.c.

References FigureColnameInternal(), name, and NULL.

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

1625 {
1626  char *name = NULL;
1627 
1628  (void) FigureColnameInternal(node, &name);
1629  if (name != NULL)
1630  return name;
1631  /* default result if we can't guess anything */
1632  return "?column?";
1633 }
#define NULL
Definition: c.h:226
static int FigureColnameInternal(Node *node, char **name)
const char * name
Definition: encode.c:521
char* FigureIndexColname ( Node node)

Definition at line 1643 of file parse_target.c.

References FigureColnameInternal(), name, and NULL.

Referenced by transformIndexStmt().

1644 {
1645  char *name = NULL;
1646 
1647  (void) FigureColnameInternal(node, &name);
1648  return name;
1649 }
#define NULL
Definition: c.h:226
static int FigureColnameInternal(Node *node, char **name)
const char * name
Definition: encode.c:521
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:141
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1330
static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle, Var *var, int levelsup)
Definition: parse_target.c:351
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:508
unsigned int Oid
Definition: postgres_ext.h:31
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1330
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define UNKNOWNOID
Definition: pg_type.h:423
Expr* transformAssignedExpr ( ParseState pstate,
Expr expr,
ParseExprKind  exprKind,
char *  colname,
int  attrno,
List indirection,
int  location 
)

Definition at line 453 of file parse_target.c.

References Assert, attnumTypeId(), tupleDesc::attrs, 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(), NULL, ParseState::p_expr_kind, ParseState::p_is_insert, ParseState::p_target_rangetblentry, ParseState::p_target_relation, parser_errposition(), RelationData::rd_att, transformAssignmentIndirection(), SetToDefault::typeId, and SetToDefault::typeMod.

Referenced by transformInsertRow(), and updateTargetListEntry().

460 {
461  Relation rd = pstate->p_target_relation;
462  Oid type_id; /* type of value provided */
463  Oid attrtype; /* type of target column */
464  int32 attrtypmod;
465  Oid attrcollation; /* collation of target column */
466  ParseExprKind sv_expr_kind;
467 
468  /*
469  * Save and restore identity of expression type we're parsing. We must
470  * set p_expr_kind here because we can parse subscripts without going
471  * through transformExpr().
472  */
473  Assert(exprKind != EXPR_KIND_NONE);
474  sv_expr_kind = pstate->p_expr_kind;
475  pstate->p_expr_kind = exprKind;
476 
477  Assert(rd != NULL);
478  if (attrno <= 0)
479  ereport(ERROR,
480  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
481  errmsg("cannot assign to system column \"%s\"",
482  colname),
483  parser_errposition(pstate, location)));
484  attrtype = attnumTypeId(rd, attrno);
485  attrtypmod = rd->rd_att->attrs[attrno - 1]->atttypmod;
486  attrcollation = rd->rd_att->attrs[attrno - 1]->attcollation;
487 
488  /*
489  * If the expression is a DEFAULT placeholder, insert the attribute's
490  * type/typmod/collation into it so that exprType etc will report the
491  * right things. (We expect that the eventually substituted default
492  * expression will in fact have this type and typmod. The collation
493  * likely doesn't matter, but let's set it correctly anyway.) Also,
494  * reject trying to update a subfield or array element with DEFAULT, since
495  * there can't be any default for portions of a column.
496  */
497  if (expr && IsA(expr, SetToDefault))
498  {
499  SetToDefault *def = (SetToDefault *) expr;
500 
501  def->typeId = attrtype;
502  def->typeMod = attrtypmod;
503  def->collation = attrcollation;
504  if (indirection)
505  {
506  if (IsA(linitial(indirection), A_Indices))
507  ereport(ERROR,
508  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
509  errmsg("cannot set an array element to DEFAULT"),
510  parser_errposition(pstate, location)));
511  else
512  ereport(ERROR,
513  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
514  errmsg("cannot set a subfield to DEFAULT"),
515  parser_errposition(pstate, location)));
516  }
517  }
518 
519  /* Now we can use exprType() safely. */
520  type_id = exprType((Node *) expr);
521 
522  /*
523  * If there is indirection on the target column, prepare an array or
524  * subfield assignment expression. This will generate a new column value
525  * that the source value has been inserted into, which can then be placed
526  * in the new tuple constructed by INSERT or UPDATE.
527  */
528  if (indirection)
529  {
530  Node *colVar;
531 
532  if (pstate->p_is_insert)
533  {
534  /*
535  * The command is INSERT INTO table (col.something) ... so there
536  * is not really a source value to work with. Insert a NULL
537  * constant as the source value.
538  */
539  colVar = (Node *) makeNullConst(attrtype, attrtypmod,
540  attrcollation);
541  }
542  else
543  {
544  /*
545  * Build a Var for the column to be updated.
546  */
547  colVar = (Node *) make_var(pstate,
548  pstate->p_target_rangetblentry,
549  attrno,
550  location);
551  }
552 
553  expr = (Expr *)
555  colVar,
556  colname,
557  false,
558  attrtype,
559  attrtypmod,
560  attrcollation,
561  list_head(indirection),
562  (Node *) expr,
563  location);
564  }
565  else
566  {
567  /*
568  * For normal non-qualified target column, do type checking and
569  * coercion.
570  */
571  Node *orig_expr = (Node *) expr;
572 
573  expr = (Expr *)
574  coerce_to_target_type(pstate,
575  orig_expr, type_id,
576  attrtype, attrtypmod,
579  -1);
580  if (expr == NULL)
581  ereport(ERROR,
582  (errcode(ERRCODE_DATATYPE_MISMATCH),
583  errmsg("column \"%s\" is of type %s"
584  " but expression is of type %s",
585  colname,
586  format_type_be(attrtype),
587  format_type_be(type_id)),
588  errhint("You will need to rewrite or cast the expression."),
589  parser_errposition(pstate, exprLocation(orig_expr))));
590  }
591 
592  pstate->p_expr_kind = sv_expr_kind;
593 
594  return expr;
595 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
int errhint(const char *fmt,...)
Definition: elog.c:987
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1204
Form_pg_attribute * attrs
Definition: tupdesc.h:74
Definition: nodes.h:508
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:32
signed int int32
Definition: c.h:253
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:334
#define linitial(l)
Definition: pg_list.h:110
#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:77
int32 typeMod
Definition: primnodes.h:1233
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:179
ParseExprKind p_expr_kind
Definition: parse_node.h:182
TupleDesc rd_att
Definition: rel.h:114
Var * make_var(ParseState *pstate, RangeTblEntry *rte, int attrno, int location)
Definition: parse_node.c:187
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation p_target_relation
Definition: parse_node.h:178
bool p_is_insert
Definition: parse_node.h:180
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:674
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 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:559
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:147
#define llast(l)
Definition: pg_list.h:126
Definition: nodes.h:508
List * p_multiassign_exprs
Definition: parse_node.h:184
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:671
#define lfirst(lc)
Definition: pg_list.h:106
List * indirection
Definition: parsenodes.h:397
e
Definition: preproc-init.c:82
Definition: pg_list.h:45
List * fields
Definition: parsenodes.h:226
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(), NULL, 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:559
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:147
char * FigureColname(Node *node)
int p_next_resno
Definition: parse_node.h:183
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:235
#define NULL
Definition: c.h:226
int16 AttrNumber
Definition: attnum.h:21
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, NULL, 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:559
Node * val
Definition: parsenodes.h:433
char * name
Definition: parsenodes.h:431
#define llast(l)
Definition: pg_list.h:126
List * p_multiassign_exprs
Definition: parse_node.h:184
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 NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
List * indirection
Definition: parsenodes.h:397
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:226
void updateTargetListEntry ( ParseState pstate,
TargetEntry tle,
char *  colname,
int  attrno,
List indirection,
int  location 
)

Definition at line 615 of file parse_target.c.

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

Referenced by transformUpdateTargetList().

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