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

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

Definition at line 1454 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_NAMEDTUPLESTORE, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, 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().

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

Definition at line 1633 of file parse_target.c.

References FigureColnameInternal(), name, and NULL.

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

1634 {
1635  char *name = NULL;
1636 
1637  (void) FigureColnameInternal(node, &name);
1638  if (name != NULL)
1639  return name;
1640  /* default result if we can't guess anything */
1641  return "?column?";
1642 }
#define NULL
Definition: c.h:229
static int FigureColnameInternal(Node *node, char **name)
const char * name
Definition: encode.c:521
char* FigureIndexColname ( Node node)

Definition at line 1652 of file parse_target.c.

References FigureColnameInternal(), name, and NULL.

Referenced by transformIndexStmt().

1653 {
1654  char *name = NULL;
1655 
1656  (void) FigureColnameInternal(node, &name);
1657  return name;
1658 }
#define NULL
Definition: c.h:229
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:163
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1367
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:509
unsigned int Oid
Definition: postgres_ext.h:31
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1367
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define UNKNOWNOID
Definition: pg_type.h:431
Expr* transformAssignedExpr ( ParseState pstate,
Expr expr,
ParseExprKind  exprKind,
char *  colname,
int  attrno,
List indirection,
int  location 
)

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

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 = rd->rd_att->attrs[attrno - 1]->atttypmod;
488  attrcollation = rd->rd_att->attrs[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:560
int errhint(const char *fmt,...)
Definition: elog.c:987
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1213
Form_pg_attribute * attrs
Definition: tupdesc.h:74
Definition: nodes.h:509
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:256
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:334
#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:77
int32 typeMod
Definition: primnodes.h:1256
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:180
ParseExprKind p_expr_kind
Definition: parse_node.h:183
TupleDesc rd_att
Definition: rel.h:115
Var * make_var(ParseState *pstate, RangeTblEntry *rte, int attrno, int location)
Definition: parse_node.c:189
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
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:179
bool p_is_insert
Definition: parse_node.h:181
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
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, result, 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:560
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:147
#define llast(l)
Definition: pg_list.h:131
Definition: nodes.h:509
List * p_multiassign_exprs
Definition: parse_node.h:185
List * list_concat(List *list1, List *list2)
Definition: list.c:321
return result
Definition: formatting.c:1618
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:675
#define lfirst(lc)
Definition: pg_list.h:106
List * indirection
Definition: parsenodes.h:406
e
Definition: preproc-init.c:82
Definition: pg_list.h:45
List * fields
Definition: parsenodes.h:235
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:560
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:184
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:235
#define NULL
Definition: c.h:229
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:560
Node * val
Definition: parsenodes.h:442
char * name
Definition: parsenodes.h:440
#define llast(l)
Definition: pg_list.h:131
List * p_multiassign_exprs
Definition: parse_node.h:185
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:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
List * indirection
Definition: parsenodes.h:406
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:235
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, char *colname, int attrno, List *indirection, int location)
Definition: parse_target.c:455
char * resname
Definition: primnodes.h:1369
AttrNumber resno
Definition: primnodes.h:1368
Expr * expr
Definition: primnodes.h:1367
int16 AttrNumber
Definition: attnum.h:21