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

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

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

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

References FigureColnameInternal(), name, and NULL.

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

1632 {
1633  char *name = NULL;
1634 
1635  (void) FigureColnameInternal(node, &name);
1636  if (name != NULL)
1637  return name;
1638  /* default result if we can't guess anything */
1639  return "?column?";
1640 }
#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 1650 of file parse_target.c.

References FigureColnameInternal(), name, and NULL.

Referenced by transformIndexStmt().

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

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

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

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

Referenced by transformUpdateTargetList().

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