PostgreSQL Source Code  git master
parse_target.c File Reference
#include "postgres.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/parse_coerce.h"
#include "parser/parse_expr.h"
#include "parser/parse_relation.h"
#include "parser/parse_target.h"
#include "parser/parse_type.h"
#include "parser/parsetree.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
Include dependency graph for parse_target.c:

Go to the source code of this file.

Functions

static void markTargetListOrigin (ParseState *pstate, TargetEntry *tle, Var *var, int levelsup)
 
static NodetransformAssignmentSubscripts (ParseState *pstate, Node *basenode, const char *targetName, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, List *subscripts, List *indirection, ListCell *next_indirection, Node *rhs, CoercionContext ccontext, int location)
 
static ListExpandColumnRefStar (ParseState *pstate, ColumnRef *cref, bool make_target_entry)
 
static ListExpandAllTables (ParseState *pstate, int location)
 
static ListExpandIndirectionStar (ParseState *pstate, A_Indirection *ind, bool make_target_entry, ParseExprKind exprKind)
 
static ListExpandSingleTable (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, bool make_target_entry)
 
static ListExpandRowReference (ParseState *pstate, Node *expr, bool make_target_entry)
 
static int FigureColnameInternal (Node *node, char **name)
 
TargetEntrytransformTargetEntry (ParseState *pstate, Node *node, Node *expr, ParseExprKind exprKind, char *colname, bool resjunk)
 
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)
 
ExprtransformAssignedExpr (ParseState *pstate, Expr *expr, ParseExprKind exprKind, const char *colname, int attrno, List *indirection, int location)
 
void updateTargetListEntry (ParseState *pstate, TargetEntry *tle, char *colname, int attrno, List *indirection, int location)
 
NodetransformAssignmentIndirection (ParseState *pstate, Node *basenode, const char *targetName, bool targetIsSubscripting, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, List *indirection, ListCell *indirection_cell, Node *rhs, CoercionContext ccontext, int location)
 
ListcheckInsertTargets (ParseState *pstate, List *cols, List **attrnos)
 
TupleDesc expandRecordVariable (ParseState *pstate, Var *var, int levelsup)
 
char * FigureColname (Node *node)
 
char * FigureIndexColname (Node *node)
 

Function Documentation

◆ checkInsertTargets()

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

Definition at line 1015 of file parse_target.c.

1016 {
1017  *attrnos = NIL;
1018 
1019  if (cols == NIL)
1020  {
1021  /*
1022  * Generate default column list for INSERT.
1023  */
1024  int numcol = RelationGetNumberOfAttributes(pstate->p_target_relation);
1025 
1026  int i;
1027 
1028  for (i = 0; i < numcol; i++)
1029  {
1030  ResTarget *col;
1031  Form_pg_attribute attr;
1032 
1033  attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
1034 
1035  if (attr->attisdropped)
1036  continue;
1037 
1038  col = makeNode(ResTarget);
1039  col->name = pstrdup(NameStr(attr->attname));
1040  col->indirection = NIL;
1041  col->val = NULL;
1042  col->location = -1;
1043  cols = lappend(cols, col);
1044  *attrnos = lappend_int(*attrnos, i + 1);
1045  }
1046  }
1047  else
1048  {
1049  /*
1050  * Do initial validation of user-supplied INSERT column list.
1051  */
1052  Bitmapset *wholecols = NULL;
1053  Bitmapset *partialcols = NULL;
1054  ListCell *tl;
1055 
1056  foreach(tl, cols)
1057  {
1058  ResTarget *col = (ResTarget *) lfirst(tl);
1059  char *name = col->name;
1060  int attrno;
1061 
1062  /* Lookup column name, ereport on failure */
1063  attrno = attnameAttNum(pstate->p_target_relation, name, false);
1064  if (attrno == InvalidAttrNumber)
1065  ereport(ERROR,
1066  (errcode(ERRCODE_UNDEFINED_COLUMN),
1067  errmsg("column \"%s\" of relation \"%s\" does not exist",
1068  name,
1070  parser_errposition(pstate, col->location)));
1071 
1072  /*
1073  * Check for duplicates, but only of whole columns --- we allow
1074  * INSERT INTO foo (col.subcol1, col.subcol2)
1075  */
1076  if (col->indirection == NIL)
1077  {
1078  /* whole column; must not have any other assignment */
1079  if (bms_is_member(attrno, wholecols) ||
1080  bms_is_member(attrno, partialcols))
1081  ereport(ERROR,
1082  (errcode(ERRCODE_DUPLICATE_COLUMN),
1083  errmsg("column \"%s\" specified more than once",
1084  name),
1085  parser_errposition(pstate, col->location)));
1086  wholecols = bms_add_member(wholecols, attrno);
1087  }
1088  else
1089  {
1090  /* partial column; must not have any whole assignment */
1091  if (bms_is_member(attrno, wholecols))
1092  ereport(ERROR,
1093  (errcode(ERRCODE_DUPLICATE_COLUMN),
1094  errmsg("column \"%s\" specified more than once",
1095  name),
1096  parser_errposition(pstate, col->location)));
1097  partialcols = bms_add_member(partialcols, attrno);
1098  }
1099 
1100  *attrnos = lappend_int(*attrnos, attrno);
1101  }
1102  }
1103 
1104  return cols;
1105 }
#define InvalidAttrNumber
Definition: attnum.h:23
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
#define NameStr(name)
Definition: c.h:746
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
int i
Definition: isn.c:73
List * lappend(List *list, void *datum)
Definition: list.c:339
List * lappend_int(List *list, int datum)
Definition: list.c:357
char * pstrdup(const char *in)
Definition: mcxt.c:1695
#define makeNode(_type_)
Definition: nodes.h:155
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:511
#define RelationGetRelationName(relation)
Definition: rel.h:539
Relation p_target_relation
Definition: parse_node.h:207
TupleDesc rd_att
Definition: rel.h:112
Node * val
Definition: parsenodes.h:519
ParseLoc location
Definition: parsenodes.h:520
List * indirection
Definition: parsenodes.h:518
char * name
Definition: parsenodes.h:517
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
const char * name

References attnameAttNum(), bms_add_member(), bms_is_member(), ereport, errcode(), errmsg(), ERROR, i, ResTarget::indirection, InvalidAttrNumber, lappend(), lappend_int(), lfirst, ResTarget::location, makeNode, name, ResTarget::name, NameStr, NIL, ParseState::p_target_relation, parser_errposition(), pstrdup(), RelationData::rd_att, RelationGetNumberOfAttributes, RelationGetRelationName, TupleDescAttr, and ResTarget::val.

Referenced by transformInsertStmt(), and transformMergeStmt().

◆ ExpandAllTables()

static List * ExpandAllTables ( ParseState pstate,
int  location 
)
static

Definition at line 1293 of file parse_target.c.

1294 {
1295  List *target = NIL;
1296  bool found_table = false;
1297  ListCell *l;
1298 
1299  foreach(l, pstate->p_namespace)
1300  {
1301  ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(l);
1302 
1303  /* Ignore table-only items */
1304  if (!nsitem->p_cols_visible)
1305  continue;
1306  /* Should not have any lateral-only items when parsing targetlist */
1307  Assert(!nsitem->p_lateral_only);
1308  /* Remember we found a p_cols_visible item */
1309  found_table = true;
1310 
1311  target = list_concat(target,
1312  expandNSItemAttrs(pstate,
1313  nsitem,
1314  0,
1315  true,
1316  location));
1317  }
1318 
1319  /*
1320  * Check for "SELECT *;". We do it this way, rather than checking for
1321  * target == NIL, because we want to allow SELECT * FROM a zero_column
1322  * table.
1323  */
1324  if (!found_table)
1325  ereport(ERROR,
1326  (errcode(ERRCODE_SYNTAX_ERROR),
1327  errmsg("SELECT * with no tables specified is not valid"),
1328  parser_errposition(pstate, location)));
1329 
1330  return target;
1331 }
#define Assert(condition)
Definition: c.h:858
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
List * expandNSItemAttrs(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, bool require_col_privs, int location)
Definition: pg_list.h:54
List * p_namespace
Definition: parse_node.h:201

References Assert, ereport, errcode(), errmsg(), ERROR, expandNSItemAttrs(), lfirst, list_concat(), NIL, ParseNamespaceItem::p_cols_visible, ParseNamespaceItem::p_lateral_only, ParseState::p_namespace, and parser_errposition().

Referenced by ExpandColumnRefStar().

◆ ExpandColumnRefStar()

static List * ExpandColumnRefStar ( ParseState pstate,
ColumnRef cref,
bool  make_target_entry 
)
static

Definition at line 1120 of file parse_target.c.

1122 {
1123  List *fields = cref->fields;
1124  int numnames = list_length(fields);
1125 
1126  if (numnames == 1)
1127  {
1128  /*
1129  * Target item is a bare '*', expand all tables
1130  *
1131  * (e.g., SELECT * FROM emp, dept)
1132  *
1133  * Since the grammar only accepts bare '*' at top level of SELECT, we
1134  * need not handle the make_target_entry==false case here.
1135  */
1136  Assert(make_target_entry);
1137  return ExpandAllTables(pstate, cref->location);
1138  }
1139  else
1140  {
1141  /*
1142  * Target item is relation.*, expand that table
1143  *
1144  * (e.g., SELECT emp.*, dname FROM emp, dept)
1145  *
1146  * Note: this code is a lot like transformColumnRef; it's tempting to
1147  * call that instead and then replace the resulting whole-row Var with
1148  * a list of Vars. However, that would leave us with the relation's
1149  * selectedCols bitmap showing the whole row as needing select
1150  * permission, as well as the individual columns. That would be
1151  * incorrect (since columns added later shouldn't need select
1152  * permissions). We could try to remove the whole-row permission bit
1153  * after the fact, but duplicating code is less messy.
1154  */
1155  char *nspname = NULL;
1156  char *relname = NULL;
1157  ParseNamespaceItem *nsitem = NULL;
1158  int levels_up;
1159  enum
1160  {
1161  CRSERR_NO_RTE,
1162  CRSERR_WRONG_DB,
1163  CRSERR_TOO_MANY
1164  } crserr = CRSERR_NO_RTE;
1165 
1166  /*
1167  * Give the PreParseColumnRefHook, if any, first shot. If it returns
1168  * non-null then we should use that expression.
1169  */
1170  if (pstate->p_pre_columnref_hook != NULL)
1171  {
1172  Node *node;
1173 
1174  node = pstate->p_pre_columnref_hook(pstate, cref);
1175  if (node != NULL)
1176  return ExpandRowReference(pstate, node, make_target_entry);
1177  }
1178 
1179  switch (numnames)
1180  {
1181  case 2:
1182  relname = strVal(linitial(fields));
1183  nsitem = refnameNamespaceItem(pstate, nspname, relname,
1184  cref->location,
1185  &levels_up);
1186  break;
1187  case 3:
1188  nspname = strVal(linitial(fields));
1189  relname = strVal(lsecond(fields));
1190  nsitem = refnameNamespaceItem(pstate, nspname, relname,
1191  cref->location,
1192  &levels_up);
1193  break;
1194  case 4:
1195  {
1196  char *catname = strVal(linitial(fields));
1197 
1198  /*
1199  * We check the catalog name and then ignore it.
1200  */
1201  if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
1202  {
1203  crserr = CRSERR_WRONG_DB;
1204  break;
1205  }
1206  nspname = strVal(lsecond(fields));
1207  relname = strVal(lthird(fields));
1208  nsitem = refnameNamespaceItem(pstate, nspname, relname,
1209  cref->location,
1210  &levels_up);
1211  break;
1212  }
1213  default:
1214  crserr = CRSERR_TOO_MANY;
1215  break;
1216  }
1217 
1218  /*
1219  * Now give the PostParseColumnRefHook, if any, a chance. We cheat a
1220  * bit by passing the RangeTblEntry, not a Var, as the planned
1221  * translation. (A single Var wouldn't be strictly correct anyway.
1222  * This convention allows hooks that really care to know what is
1223  * happening. It might be better to pass the nsitem, but we'd have to
1224  * promote that struct to a full-fledged Node type so that callees
1225  * could identify its type.)
1226  */
1227  if (pstate->p_post_columnref_hook != NULL)
1228  {
1229  Node *node;
1230 
1231  node = pstate->p_post_columnref_hook(pstate, cref,
1232  (Node *) (nsitem ? nsitem->p_rte : NULL));
1233  if (node != NULL)
1234  {
1235  if (nsitem != NULL)
1236  ereport(ERROR,
1237  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1238  errmsg("column reference \"%s\" is ambiguous",
1239  NameListToString(cref->fields)),
1240  parser_errposition(pstate, cref->location)));
1241  return ExpandRowReference(pstate, node, make_target_entry);
1242  }
1243  }
1244 
1245  /*
1246  * Throw error if no translation found.
1247  */
1248  if (nsitem == NULL)
1249  {
1250  switch (crserr)
1251  {
1252  case CRSERR_NO_RTE:
1253  errorMissingRTE(pstate, makeRangeVar(nspname, relname,
1254  cref->location));
1255  break;
1256  case CRSERR_WRONG_DB:
1257  ereport(ERROR,
1258  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1259  errmsg("cross-database references are not implemented: %s",
1260  NameListToString(cref->fields)),
1261  parser_errposition(pstate, cref->location)));
1262  break;
1263  case CRSERR_TOO_MANY:
1264  ereport(ERROR,
1265  (errcode(ERRCODE_SYNTAX_ERROR),
1266  errmsg("improper qualified name (too many dotted names): %s",
1267  NameListToString(cref->fields)),
1268  parser_errposition(pstate, cref->location)));
1269  break;
1270  }
1271  }
1272 
1273  /*
1274  * OK, expand the nsitem into fields.
1275  */
1276  return ExpandSingleTable(pstate, nsitem, levels_up, cref->location,
1277  make_target_entry);
1278  }
1279 }
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3154
Oid MyDatabaseId
Definition: globals.c:91
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:424
char * NameListToString(const List *names)
Definition: namespace.c:3579
void errorMissingRTE(ParseState *pstate, RangeVar *relation)
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
static List * ExpandSingleTable(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, bool make_target_entry)
static List * ExpandRowReference(ParseState *pstate, Node *expr, bool make_target_entry)
static List * ExpandAllTables(ParseState *pstate, int location)
NameData relname
Definition: pg_class.h:38
static int list_length(const List *l)
Definition: pg_list.h:152
#define lthird(l)
Definition: pg_list.h:188
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
ParseLoc location
Definition: parsenodes.h:295
List * fields
Definition: parsenodes.h:294
Definition: nodes.h:129
RangeTblEntry * p_rte
Definition: parse_node.h:287
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:235
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:236
#define strVal(v)
Definition: value.h:82

References Assert, ereport, errcode(), errmsg(), ERROR, errorMissingRTE(), ExpandAllTables(), ExpandRowReference(), ExpandSingleTable(), ColumnRef::fields, get_database_name(), linitial, list_length(), ColumnRef::location, lsecond, lthird, makeRangeVar(), MyDatabaseId, NameListToString(), ParseState::p_post_columnref_hook, ParseState::p_pre_columnref_hook, ParseNamespaceItem::p_rte, parser_errposition(), refnameNamespaceItem(), relname, and strVal.

Referenced by transformExpressionList(), and transformTargetList().

◆ ExpandIndirectionStar()

static List * ExpandIndirectionStar ( ParseState pstate,
A_Indirection ind,
bool  make_target_entry,
ParseExprKind  exprKind 
)
static

Definition at line 1345 of file parse_target.c.

1347 {
1348  Node *expr;
1349 
1350  /* Strip off the '*' to create a reference to the rowtype object */
1351  ind = copyObject(ind);
1352  ind->indirection = list_truncate(ind->indirection,
1353  list_length(ind->indirection) - 1);
1354 
1355  /* And transform that */
1356  expr = transformExpr(pstate, (Node *) ind, exprKind);
1357 
1358  /* Expand the rowtype expression into individual fields */
1359  return ExpandRowReference(pstate, expr, make_target_entry);
1360 }
List * list_truncate(List *list, int new_size)
Definition: list.c:631
#define copyObject(obj)
Definition: nodes.h:224
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:121

References copyObject, ExpandRowReference(), list_length(), list_truncate(), and transformExpr().

Referenced by transformExpressionList(), and transformTargetList().

◆ expandRecordVariable()

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

Definition at line 1519 of file parse_target.c.

1520 {
1521  TupleDesc tupleDesc;
1522  int netlevelsup;
1523  RangeTblEntry *rte;
1525  Node *expr;
1526 
1527  /* Check my caller didn't mess up */
1528  Assert(IsA(var, Var));
1529  Assert(var->vartype == RECORDOID);
1530 
1531  /*
1532  * Note: it's tempting to use GetNSItemByRangeTablePosn here so that we
1533  * can use expandNSItemVars instead of expandRTE; but that does not work
1534  * for some of the recursion cases below, where we have consed up a
1535  * ParseState that lacks p_namespace data.
1536  */
1537  netlevelsup = var->varlevelsup + levelsup;
1538  rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
1539  attnum = var->varattno;
1540 
1541  if (attnum == InvalidAttrNumber)
1542  {
1543  /* Whole-row reference to an RTE, so expand the known fields */
1544  List *names,
1545  *vars;
1546  ListCell *lname,
1547  *lvar;
1548  int i;
1549 
1550  expandRTE(rte, var->varno, 0, var->location, false,
1551  &names, &vars);
1552 
1554  i = 1;
1555  forboth(lname, names, lvar, vars)
1556  {
1557  char *label = strVal(lfirst(lname));
1558  Node *varnode = (Node *) lfirst(lvar);
1559 
1560  TupleDescInitEntry(tupleDesc, i,
1561  label,
1562  exprType(varnode),
1563  exprTypmod(varnode),
1564  0);
1565  TupleDescInitEntryCollation(tupleDesc, i,
1566  exprCollation(varnode));
1567  i++;
1568  }
1569  Assert(lname == NULL && lvar == NULL); /* lists same length? */
1570 
1571  return tupleDesc;
1572  }
1573 
1574  expr = (Node *) var; /* default if we can't drill down */
1575 
1576  switch (rte->rtekind)
1577  {
1578  case RTE_RELATION:
1579  case RTE_VALUES:
1580  case RTE_NAMEDTUPLESTORE:
1581  case RTE_RESULT:
1582 
1583  /*
1584  * This case should not occur: a column of a table, values list,
1585  * or ENR shouldn't have type RECORD. Fall through and fail (most
1586  * likely) at the bottom.
1587  */
1588  break;
1589  case RTE_SUBQUERY:
1590  {
1591  /* Subselect-in-FROM: examine sub-select's output expr */
1593  attnum);
1594 
1595  if (ste == NULL || ste->resjunk)
1596  elog(ERROR, "subquery %s does not have attribute %d",
1597  rte->eref->aliasname, attnum);
1598  expr = (Node *) ste->expr;
1599  if (IsA(expr, Var))
1600  {
1601  /*
1602  * Recurse into the sub-select to see what its Var refers
1603  * to. We have to build an additional level of ParseState
1604  * to keep in step with varlevelsup in the subselect;
1605  * furthermore, the subquery RTE might be from an outer
1606  * query level, in which case the ParseState for the
1607  * subselect must have that outer level as parent.
1608  */
1609  ParseState mypstate = {0};
1610  Index levelsup;
1611 
1612  /* this loop must work, since GetRTEByRangeTablePosn did */
1613  for (levelsup = 0; levelsup < netlevelsup; levelsup++)
1614  pstate = pstate->parentParseState;
1615  mypstate.parentParseState = pstate;
1616  mypstate.p_rtable = rte->subquery->rtable;
1617  /* don't bother filling the rest of the fake pstate */
1618 
1619  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1620  }
1621  /* else fall through to inspect the expression */
1622  }
1623  break;
1624  case RTE_JOIN:
1625  /* Join RTE --- recursively inspect the alias variable */
1626  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
1627  expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1628  Assert(expr != NULL);
1629  /* We intentionally don't strip implicit coercions here */
1630  if (IsA(expr, Var))
1631  return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
1632  /* else fall through to inspect the expression */
1633  break;
1634  case RTE_FUNCTION:
1635 
1636  /*
1637  * We couldn't get here unless a function is declared with one of
1638  * its result columns as RECORD, which is not allowed.
1639  */
1640  break;
1641  case RTE_TABLEFUNC:
1642 
1643  /*
1644  * Table function cannot have columns with RECORD type.
1645  */
1646  break;
1647  case RTE_CTE:
1648  /* CTE reference: examine subquery's output expr */
1649  if (!rte->self_reference)
1650  {
1651  CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
1652  TargetEntry *ste;
1653 
1655  if (ste == NULL || ste->resjunk)
1656  elog(ERROR, "CTE %s does not have attribute %d",
1657  rte->eref->aliasname, attnum);
1658  expr = (Node *) ste->expr;
1659  if (IsA(expr, Var))
1660  {
1661  /*
1662  * Recurse into the CTE to see what its Var refers to. We
1663  * have to build an additional level of ParseState to keep
1664  * in step with varlevelsup in the CTE; furthermore it
1665  * could be an outer CTE (compare SUBQUERY case above).
1666  */
1667  ParseState mypstate = {0};
1668  Index levelsup;
1669 
1670  /* this loop must work, since GetCTEForRTE did */
1671  for (levelsup = 0;
1672  levelsup < rte->ctelevelsup + netlevelsup;
1673  levelsup++)
1674  pstate = pstate->parentParseState;
1675  mypstate.parentParseState = pstate;
1676  mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
1677  /* don't bother filling the rest of the fake pstate */
1678 
1679  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1680  }
1681  /* else fall through to inspect the expression */
1682  }
1683  break;
1684  }
1685 
1686  /*
1687  * We now have an expression we can't expand any more, so see if
1688  * get_expr_result_tupdesc() can do anything with it.
1689  */
1690  return get_expr_result_tupdesc(expr, false);
1691 }
int16 AttrNumber
Definition: attnum.h:21
unsigned int Index
Definition: c.h:614
#define elog(elevel,...)
Definition: elog.h:224
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:551
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:298
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:816
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
CommonTableExpr * GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
TupleDesc expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
#define GetCTETargetList(cte)
Definition: parsenodes.h:1708
@ RTE_JOIN
Definition: parsenodes.h:1030
@ RTE_CTE
Definition: parsenodes.h:1034
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1035
@ RTE_VALUES
Definition: parsenodes.h:1033
@ RTE_SUBQUERY
Definition: parsenodes.h:1029
@ RTE_RESULT
Definition: parsenodes.h:1036
@ RTE_FUNCTION
Definition: parsenodes.h:1031
@ RTE_TABLEFUNC
Definition: parsenodes.h:1032
@ RTE_RELATION
Definition: parsenodes.h:1028
int16 attnum
Definition: pg_attribute.h:74
static char * label
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
ParseState * parentParseState
Definition: parse_node.h:192
List * p_rtable
Definition: parse_node.h:194
List * rtable
Definition: parsenodes.h:168
List * targetList
Definition: parsenodes.h:191
Index ctelevelsup
Definition: parsenodes.h:1208
Query * subquery
Definition: parsenodes.h:1114
RTEKind rtekind
Definition: parsenodes.h:1057
Expr * expr
Definition: primnodes.h:2192
Definition: primnodes.h:248
ParseLoc location
Definition: primnodes.h:293
AttrNumber varattno
Definition: primnodes.h:260
int varno
Definition: primnodes.h:255
Index varlevelsup
Definition: primnodes.h:280
Definition: regcomp.c:281
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:67
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:833
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:651

References Assert, attnum, CreateTemplateTupleDesc(), RangeTblEntry::ctelevelsup, CommonTableExpr::ctequery, elog, ERROR, expandRTE(), TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), forboth, get_expr_result_tupdesc(), get_tle_by_resno(), GetCTEForRTE(), GetCTETargetList, GetRTEByRangeTablePosn(), i, if(), InvalidAttrNumber, IsA, label, lfirst, list_length(), list_nth(), Var::location, ParseState::p_rtable, ParseState::parentParseState, Query::rtable, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, strVal, RangeTblEntry::subquery, Query::targetList, TupleDescInitEntry(), TupleDescInitEntryCollation(), Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by ExpandRowReference(), and ParseComplexProjection().

◆ ExpandRowReference()

static List * ExpandRowReference ( ParseState pstate,
Node expr,
bool  make_target_entry 
)
static

Definition at line 1423 of file parse_target.c.

1425 {
1426  List *result = NIL;
1427  TupleDesc tupleDesc;
1428  int numAttrs;
1429  int i;
1430 
1431  /*
1432  * If the rowtype expression is a whole-row Var, we can expand the fields
1433  * as simple Vars. Note: if the RTE is a relation, this case leaves us
1434  * with its RTEPermissionInfo's selectedCols bitmap showing the whole row
1435  * as needing select permission, as well as the individual columns.
1436  * However, we can only get here for weird notations like (table.*).*, so
1437  * it's not worth trying to clean up --- arguably, the permissions marking
1438  * is correct anyway for such cases.
1439  */
1440  if (IsA(expr, Var) &&
1441  ((Var *) expr)->varattno == InvalidAttrNumber)
1442  {
1443  Var *var = (Var *) expr;
1444  ParseNamespaceItem *nsitem;
1445 
1446  nsitem = GetNSItemByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1447  return ExpandSingleTable(pstate, nsitem, var->varlevelsup, var->location, make_target_entry);
1448  }
1449 
1450  /*
1451  * Otherwise we have to do it the hard way. Our current implementation is
1452  * to generate multiple copies of the expression and do FieldSelects.
1453  * (This can be pretty inefficient if the expression involves nontrivial
1454  * computation :-(.)
1455  *
1456  * Verify it's a composite type, and get the tupdesc.
1457  * get_expr_result_tupdesc() handles this conveniently.
1458  *
1459  * If it's a Var of type RECORD, we have to work even harder: we have to
1460  * find what the Var refers to, and pass that to get_expr_result_tupdesc.
1461  * That task is handled by expandRecordVariable().
1462  */
1463  if (IsA(expr, Var) &&
1464  ((Var *) expr)->vartype == RECORDOID)
1465  tupleDesc = expandRecordVariable(pstate, (Var *) expr, 0);
1466  else
1467  tupleDesc = get_expr_result_tupdesc(expr, false);
1468  Assert(tupleDesc);
1469 
1470  /* Generate a list of references to the individual fields */
1471  numAttrs = tupleDesc->natts;
1472  for (i = 0; i < numAttrs; i++)
1473  {
1474  Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
1475  FieldSelect *fselect;
1476 
1477  if (att->attisdropped)
1478  continue;
1479 
1480  fselect = makeNode(FieldSelect);
1481  fselect->arg = (Expr *) copyObject(expr);
1482  fselect->fieldnum = i + 1;
1483  fselect->resulttype = att->atttypid;
1484  fselect->resulttypmod = att->atttypmod;
1485  /* save attribute's collation for parse_collate.c */
1486  fselect->resultcollid = att->attcollation;
1487 
1488  if (make_target_entry)
1489  {
1490  /* add TargetEntry decoration */
1491  TargetEntry *te;
1492 
1493  te = makeTargetEntry((Expr *) fselect,
1494  (AttrNumber) pstate->p_next_resno++,
1495  pstrdup(NameStr(att->attname)),
1496  false);
1497  result = lappend(result, te);
1498  }
1499  else
1500  result = lappend(result, fselect);
1501  }
1502 
1503  return result;
1504 }
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:240
ParseNamespaceItem * GetNSItemByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
AttrNumber fieldnum
Definition: primnodes.h:1129
Expr * arg
Definition: primnodes.h:1128
int p_next_resno
Definition: parse_node.h:212

References FieldSelect::arg, Assert, copyObject, expandRecordVariable(), ExpandSingleTable(), FieldSelect::fieldnum, get_expr_result_tupdesc(), GetNSItemByRangeTablePosn(), i, InvalidAttrNumber, IsA, lappend(), Var::location, makeNode, makeTargetEntry(), NameStr, TupleDescData::natts, NIL, ParseState::p_next_resno, pstrdup(), TupleDescAttr, Var::varlevelsup, and Var::varno.

Referenced by ExpandColumnRefStar(), and ExpandIndirectionStar().

◆ ExpandSingleTable()

static List * ExpandSingleTable ( ParseState pstate,
ParseNamespaceItem nsitem,
int  sublevels_up,
int  location,
bool  make_target_entry 
)
static

Definition at line 1372 of file parse_target.c.

1374 {
1375  if (make_target_entry)
1376  {
1377  /* expandNSItemAttrs handles permissions marking */
1378  return expandNSItemAttrs(pstate, nsitem, sublevels_up, true, location);
1379  }
1380  else
1381  {
1382  RangeTblEntry *rte = nsitem->p_rte;
1383  RTEPermissionInfo *perminfo = nsitem->p_perminfo;
1384  List *vars;
1385  ListCell *l;
1386 
1387  vars = expandNSItemVars(pstate, nsitem, sublevels_up, location, NULL);
1388 
1389  /*
1390  * Require read access to the table. This is normally redundant with
1391  * the markVarForSelectPriv calls below, but not if the table has zero
1392  * columns. We need not do anything if the nsitem is for a join: its
1393  * component tables will have been marked ACL_SELECT when they were
1394  * added to the rangetable. (This step changes things only for the
1395  * target relation of UPDATE/DELETE, which cannot be under a join.)
1396  */
1397  if (rte->rtekind == RTE_RELATION)
1398  {
1399  Assert(perminfo != NULL);
1400  perminfo->requiredPerms |= ACL_SELECT;
1401  }
1402 
1403  /* Require read access to each column */
1404  foreach(l, vars)
1405  {
1406  Var *var = (Var *) lfirst(l);
1407 
1408  markVarForSelectPriv(pstate, var);
1409  }
1410 
1411  return vars;
1412  }
1413 }
void markVarForSelectPriv(ParseState *pstate, Var *var)
List * expandNSItemVars(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
#define ACL_SELECT
Definition: parsenodes.h:77
RTEPermissionInfo * p_perminfo
Definition: parse_node.h:289
AclMode requiredPerms
Definition: parsenodes.h:1295

References ACL_SELECT, Assert, expandNSItemAttrs(), expandNSItemVars(), lfirst, markVarForSelectPriv(), ParseNamespaceItem::p_perminfo, ParseNamespaceItem::p_rte, RTEPermissionInfo::requiredPerms, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by ExpandColumnRefStar(), and ExpandRowReference().

◆ FigureColname()

char* FigureColname ( Node node)

Definition at line 1704 of file parse_target.c.

1705 {
1706  char *name = NULL;
1707 
1708  (void) FigureColnameInternal(node, &name);
1709  if (name != NULL)
1710  return name;
1711  /* default result if we can't guess anything */
1712  return "?column?";
1713 }
static int FigureColnameInternal(Node *node, char **name)

References FigureColnameInternal(), and name.

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

◆ FigureColnameInternal()

static int FigureColnameInternal ( Node node,
char **  name 
)
static

Definition at line 1743 of file parse_target.c.

1744 {
1745  int strength = 0;
1746 
1747  if (node == NULL)
1748  return strength;
1749 
1750  switch (nodeTag(node))
1751  {
1752  case T_ColumnRef:
1753  {
1754  char *fname = NULL;
1755  ListCell *l;
1756 
1757  /* find last field name, if any, ignoring "*" */
1758  foreach(l, ((ColumnRef *) node)->fields)
1759  {
1760  Node *i = lfirst(l);
1761 
1762  if (IsA(i, String))
1763  fname = strVal(i);
1764  }
1765  if (fname)
1766  {
1767  *name = fname;
1768  return 2;
1769  }
1770  }
1771  break;
1772  case T_A_Indirection:
1773  {
1774  A_Indirection *ind = (A_Indirection *) node;
1775  char *fname = NULL;
1776  ListCell *l;
1777 
1778  /* find last field name, if any, ignoring "*" and subscripts */
1779  foreach(l, ind->indirection)
1780  {
1781  Node *i = lfirst(l);
1782 
1783  if (IsA(i, String))
1784  fname = strVal(i);
1785  }
1786  if (fname)
1787  {
1788  *name = fname;
1789  return 2;
1790  }
1791  return FigureColnameInternal(ind->arg, name);
1792  }
1793  break;
1794  case T_FuncCall:
1795  *name = strVal(llast(((FuncCall *) node)->funcname));
1796  return 2;
1797  case T_A_Expr:
1798  if (((A_Expr *) node)->kind == AEXPR_NULLIF)
1799  {
1800  /* make nullif() act like a regular function */
1801  *name = "nullif";
1802  return 2;
1803  }
1804  break;
1805  case T_TypeCast:
1806  strength = FigureColnameInternal(((TypeCast *) node)->arg,
1807  name);
1808  if (strength <= 1)
1809  {
1810  if (((TypeCast *) node)->typeName != NULL)
1811  {
1812  *name = strVal(llast(((TypeCast *) node)->typeName->names));
1813  return 1;
1814  }
1815  }
1816  break;
1817  case T_CollateClause:
1818  return FigureColnameInternal(((CollateClause *) node)->arg, name);
1819  case T_GroupingFunc:
1820  /* make GROUPING() act like a regular function */
1821  *name = "grouping";
1822  return 2;
1823  case T_MergeSupportFunc:
1824  /* make MERGE_ACTION() act like a regular function */
1825  *name = "merge_action";
1826  return 2;
1827  case T_SubLink:
1828  switch (((SubLink *) node)->subLinkType)
1829  {
1830  case EXISTS_SUBLINK:
1831  *name = "exists";
1832  return 2;
1833  case ARRAY_SUBLINK:
1834  *name = "array";
1835  return 2;
1836  case EXPR_SUBLINK:
1837  {
1838  /* Get column name of the subquery's single target */
1839  SubLink *sublink = (SubLink *) node;
1840  Query *query = (Query *) sublink->subselect;
1841 
1842  /*
1843  * The subquery has probably already been transformed,
1844  * but let's be careful and check that. (The reason
1845  * we can see a transformed subquery here is that
1846  * transformSubLink is lazy and modifies the SubLink
1847  * node in-place.)
1848  */
1849  if (IsA(query, Query))
1850  {
1851  TargetEntry *te = (TargetEntry *) linitial(query->targetList);
1852 
1853  if (te->resname)
1854  {
1855  *name = te->resname;
1856  return 2;
1857  }
1858  }
1859  }
1860  break;
1861  /* As with other operator-like nodes, these have no names */
1862  case MULTIEXPR_SUBLINK:
1863  case ALL_SUBLINK:
1864  case ANY_SUBLINK:
1865  case ROWCOMPARE_SUBLINK:
1866  case CTE_SUBLINK:
1867  break;
1868  }
1869  break;
1870  case T_CaseExpr:
1871  strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult,
1872  name);
1873  if (strength <= 1)
1874  {
1875  *name = "case";
1876  return 1;
1877  }
1878  break;
1879  case T_A_ArrayExpr:
1880  /* make ARRAY[] act like a function */
1881  *name = "array";
1882  return 2;
1883  case T_RowExpr:
1884  /* make ROW() act like a function */
1885  *name = "row";
1886  return 2;
1887  case T_CoalesceExpr:
1888  /* make coalesce() act like a regular function */
1889  *name = "coalesce";
1890  return 2;
1891  case T_MinMaxExpr:
1892  /* make greatest/least act like a regular function */
1893  switch (((MinMaxExpr *) node)->op)
1894  {
1895  case IS_GREATEST:
1896  *name = "greatest";
1897  return 2;
1898  case IS_LEAST:
1899  *name = "least";
1900  return 2;
1901  }
1902  break;
1903  case T_SQLValueFunction:
1904  /* make these act like a function or variable */
1905  switch (((SQLValueFunction *) node)->op)
1906  {
1907  case SVFOP_CURRENT_DATE:
1908  *name = "current_date";
1909  return 2;
1910  case SVFOP_CURRENT_TIME:
1911  case SVFOP_CURRENT_TIME_N:
1912  *name = "current_time";
1913  return 2;
1916  *name = "current_timestamp";
1917  return 2;
1918  case SVFOP_LOCALTIME:
1919  case SVFOP_LOCALTIME_N:
1920  *name = "localtime";
1921  return 2;
1922  case SVFOP_LOCALTIMESTAMP:
1924  *name = "localtimestamp";
1925  return 2;
1926  case SVFOP_CURRENT_ROLE:
1927  *name = "current_role";
1928  return 2;
1929  case SVFOP_CURRENT_USER:
1930  *name = "current_user";
1931  return 2;
1932  case SVFOP_USER:
1933  *name = "user";
1934  return 2;
1935  case SVFOP_SESSION_USER:
1936  *name = "session_user";
1937  return 2;
1938  case SVFOP_CURRENT_CATALOG:
1939  *name = "current_catalog";
1940  return 2;
1941  case SVFOP_CURRENT_SCHEMA:
1942  *name = "current_schema";
1943  return 2;
1944  }
1945  break;
1946  case T_XmlExpr:
1947  /* make SQL/XML functions act like a regular function */
1948  switch (((XmlExpr *) node)->op)
1949  {
1950  case IS_XMLCONCAT:
1951  *name = "xmlconcat";
1952  return 2;
1953  case IS_XMLELEMENT:
1954  *name = "xmlelement";
1955  return 2;
1956  case IS_XMLFOREST:
1957  *name = "xmlforest";
1958  return 2;
1959  case IS_XMLPARSE:
1960  *name = "xmlparse";
1961  return 2;
1962  case IS_XMLPI:
1963  *name = "xmlpi";
1964  return 2;
1965  case IS_XMLROOT:
1966  *name = "xmlroot";
1967  return 2;
1968  case IS_XMLSERIALIZE:
1969  *name = "xmlserialize";
1970  return 2;
1971  case IS_DOCUMENT:
1972  /* nothing */
1973  break;
1974  }
1975  break;
1976  case T_XmlSerialize:
1977  /* make XMLSERIALIZE act like a regular function */
1978  *name = "xmlserialize";
1979  return 2;
1980  case T_JsonParseExpr:
1981  /* make JSON act like a regular function */
1982  *name = "json";
1983  return 2;
1984  case T_JsonScalarExpr:
1985  /* make JSON_SCALAR act like a regular function */
1986  *name = "json_scalar";
1987  return 2;
1988  case T_JsonSerializeExpr:
1989  /* make JSON_SERIALIZE act like a regular function */
1990  *name = "json_serialize";
1991  return 2;
1992  case T_JsonObjectConstructor:
1993  /* make JSON_OBJECT act like a regular function */
1994  *name = "json_object";
1995  return 2;
1996  case T_JsonArrayConstructor:
1997  case T_JsonArrayQueryConstructor:
1998  /* make JSON_ARRAY act like a regular function */
1999  *name = "json_array";
2000  return 2;
2001  case T_JsonObjectAgg:
2002  /* make JSON_OBJECTAGG act like a regular function */
2003  *name = "json_objectagg";
2004  return 2;
2005  case T_JsonArrayAgg:
2006  /* make JSON_ARRAYAGG act like a regular function */
2007  *name = "json_arrayagg";
2008  return 2;
2009  case T_JsonFuncExpr:
2010  /* make SQL/JSON functions act like a regular function */
2011  switch (((JsonFuncExpr *) node)->op)
2012  {
2013  case JSON_EXISTS_OP:
2014  *name = "json_exists";
2015  return 2;
2016  case JSON_QUERY_OP:
2017  *name = "json_query";
2018  return 2;
2019  case JSON_VALUE_OP:
2020  *name = "json_value";
2021  return 2;
2022  /* JSON_TABLE_OP can't happen here. */
2023  default:
2024  elog(ERROR, "unrecognized JsonExpr op: %d",
2025  (int) ((JsonFuncExpr *) node)->op);
2026  }
2027  break;
2028  default:
2029  break;
2030  }
2031 
2032  return strength;
2033 }
#define funcname
Definition: indent_codes.h:69
#define nodeTag(nodeptr)
Definition: nodes.h:133
@ AEXPR_NULLIF
Definition: parsenodes.h:318
void * arg
#define llast(l)
Definition: pg_list.h:198
@ ARRAY_SUBLINK
Definition: primnodes.h:1003
@ ANY_SUBLINK
Definition: primnodes.h:999
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:1002
@ CTE_SUBLINK
Definition: primnodes.h:1004
@ EXPR_SUBLINK
Definition: primnodes.h:1001
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:1000
@ ALL_SUBLINK
Definition: primnodes.h:998
@ EXISTS_SUBLINK
Definition: primnodes.h:997
@ IS_LEAST
Definition: primnodes.h:1503
@ IS_GREATEST
Definition: primnodes.h:1502
@ SVFOP_CURRENT_CATALOG
Definition: primnodes.h:1549
@ SVFOP_LOCALTIME_N
Definition: primnodes.h:1542
@ SVFOP_CURRENT_TIMESTAMP
Definition: primnodes.h:1539
@ SVFOP_LOCALTIME
Definition: primnodes.h:1541
@ SVFOP_CURRENT_TIMESTAMP_N
Definition: primnodes.h:1540
@ SVFOP_CURRENT_ROLE
Definition: primnodes.h:1545
@ SVFOP_USER
Definition: primnodes.h:1547
@ SVFOP_CURRENT_SCHEMA
Definition: primnodes.h:1550
@ SVFOP_LOCALTIMESTAMP_N
Definition: primnodes.h:1544
@ SVFOP_CURRENT_DATE
Definition: primnodes.h:1536
@ SVFOP_CURRENT_TIME_N
Definition: primnodes.h:1538
@ SVFOP_CURRENT_TIME
Definition: primnodes.h:1537
@ SVFOP_LOCALTIMESTAMP
Definition: primnodes.h:1543
@ SVFOP_CURRENT_USER
Definition: primnodes.h:1546
@ SVFOP_SESSION_USER
Definition: primnodes.h:1548
@ IS_DOCUMENT
Definition: primnodes.h:1587
@ IS_XMLFOREST
Definition: primnodes.h:1582
@ IS_XMLCONCAT
Definition: primnodes.h:1580
@ IS_XMLPI
Definition: primnodes.h:1584
@ IS_XMLPARSE
Definition: primnodes.h:1583
@ IS_XMLSERIALIZE
Definition: primnodes.h:1586
@ IS_XMLROOT
Definition: primnodes.h:1585
@ IS_XMLELEMENT
Definition: primnodes.h:1581
@ JSON_QUERY_OP
Definition: primnodes.h:1799
@ JSON_EXISTS_OP
Definition: primnodes.h:1798
@ JSON_VALUE_OP
Definition: primnodes.h:1800
Definition: value.h:64

References AEXPR_NULLIF, ALL_SUBLINK, ANY_SUBLINK, arg, ARRAY_SUBLINK, CTE_SUBLINK, elog, ERROR, EXISTS_SUBLINK, EXPR_SUBLINK, funcname, i, if(), IS_DOCUMENT, IS_GREATEST, IS_LEAST, IS_XMLCONCAT, IS_XMLELEMENT, IS_XMLFOREST, IS_XMLPARSE, IS_XMLPI, IS_XMLROOT, IS_XMLSERIALIZE, IsA, JSON_EXISTS_OP, JSON_QUERY_OP, JSON_VALUE_OP, lfirst, linitial, llast, MULTIEXPR_SUBLINK, name, nodeTag, ROWCOMPARE_SUBLINK, strVal, SubLink::subselect, SVFOP_CURRENT_CATALOG, SVFOP_CURRENT_DATE, SVFOP_CURRENT_ROLE, SVFOP_CURRENT_SCHEMA, SVFOP_CURRENT_TIME, SVFOP_CURRENT_TIME_N, SVFOP_CURRENT_TIMESTAMP, SVFOP_CURRENT_TIMESTAMP_N, SVFOP_CURRENT_USER, SVFOP_LOCALTIME, SVFOP_LOCALTIME_N, SVFOP_LOCALTIMESTAMP, SVFOP_LOCALTIMESTAMP_N, SVFOP_SESSION_USER, SVFOP_USER, and Query::targetList.

Referenced by FigureColname(), and FigureIndexColname().

◆ FigureIndexColname()

char* FigureIndexColname ( Node node)

Definition at line 1723 of file parse_target.c.

1724 {
1725  char *name = NULL;
1726 
1727  (void) FigureColnameInternal(node, &name);
1728  return name;
1729 }

References FigureColnameInternal(), and name.

Referenced by transformIndexStmt().

◆ markTargetListOrigin()

static void markTargetListOrigin ( ParseState pstate,
TargetEntry tle,
Var var,
int  levelsup 
)
static

Definition at line 343 of file parse_target.c.

345 {
346  int netlevelsup;
347  RangeTblEntry *rte;
349 
350  if (var == NULL || !IsA(var, Var))
351  return;
352  netlevelsup = var->varlevelsup + levelsup;
353  rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
354  attnum = var->varattno;
355 
356  switch (rte->rtekind)
357  {
358  case RTE_RELATION:
359  /* It's a table or view, report it */
360  tle->resorigtbl = rte->relid;
361  tle->resorigcol = attnum;
362  break;
363  case RTE_SUBQUERY:
364  /* Subselect-in-FROM: copy up from the subselect */
365  if (attnum != InvalidAttrNumber)
366  {
368  attnum);
369 
370  if (ste == NULL || ste->resjunk)
371  elog(ERROR, "subquery %s does not have attribute %d",
372  rte->eref->aliasname, attnum);
373  tle->resorigtbl = ste->resorigtbl;
374  tle->resorigcol = ste->resorigcol;
375  }
376  break;
377  case RTE_JOIN:
378  case RTE_FUNCTION:
379  case RTE_VALUES:
380  case RTE_TABLEFUNC:
381  case RTE_NAMEDTUPLESTORE:
382  case RTE_RESULT:
383  /* not a simple relation, leave it unmarked */
384  break;
385  case RTE_CTE:
386 
387  /*
388  * CTE reference: copy up from the subquery, if possible. If the
389  * RTE is a recursive self-reference then we can't do anything
390  * because we haven't finished analyzing it yet. However, it's no
391  * big loss because we must be down inside the recursive term of a
392  * recursive CTE, and so any markings on the current targetlist
393  * are not going to affect the results anyway.
394  */
395  if (attnum != InvalidAttrNumber && !rte->self_reference)
396  {
397  CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
398  TargetEntry *ste;
399  List *tl = GetCTETargetList(cte);
400  int extra_cols = 0;
401 
402  /*
403  * RTE for CTE will already have the search and cycle columns
404  * added, but the subquery won't, so skip looking those up.
405  */
406  if (cte->search_clause)
407  extra_cols += 1;
408  if (cte->cycle_clause)
409  extra_cols += 2;
410  if (extra_cols &&
411  attnum > list_length(tl) &&
412  attnum <= list_length(tl) + extra_cols)
413  break;
414 
415  ste = get_tle_by_resno(tl, attnum);
416  if (ste == NULL || ste->resjunk)
417  elog(ERROR, "CTE %s does not have attribute %d",
418  rte->eref->aliasname, attnum);
419  tle->resorigtbl = ste->resorigtbl;
420  tle->resorigcol = ste->resorigcol;
421  }
422  break;
423  }
424 }

References attnum, elog, ERROR, get_tle_by_resno(), GetCTEForRTE(), GetCTETargetList, GetRTEByRangeTablePosn(), InvalidAttrNumber, IsA, list_length(), RangeTblEntry::relid, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, RangeTblEntry::subquery, Query::targetList, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by markTargetListOrigins().

◆ markTargetListOrigins()

void markTargetListOrigins ( ParseState pstate,
List targetlist 
)

Definition at line 318 of file parse_target.c.

319 {
320  ListCell *l;
321 
322  foreach(l, targetlist)
323  {
324  TargetEntry *tle = (TargetEntry *) lfirst(l);
325 
326  markTargetListOrigin(pstate, tle, (Var *) tle->expr, 0);
327  }
328 }
static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle, Var *var, int levelsup)
Definition: parse_target.c:343

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

Referenced by transformReturningList(), and transformSelectStmt().

◆ resolveTargetListUnknowns()

void resolveTargetListUnknowns ( ParseState pstate,
List targetlist 
)

Definition at line 288 of file parse_target.c.

289 {
290  ListCell *l;
291 
292  foreach(l, targetlist)
293  {
294  TargetEntry *tle = (TargetEntry *) lfirst(l);
295  Oid restype = exprType((Node *) tle->expr);
296 
297  if (restype == UNKNOWNOID)
298  {
299  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
300  restype, TEXTOID, -1,
303  -1);
304  }
305  }
306 }
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:157
unsigned int Oid
Definition: postgres_ext.h:31
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:736
@ COERCION_IMPLICIT
Definition: primnodes.h:714

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

Referenced by transformReturningList(), transformReturnStmt(), and transformSelectStmt().

◆ transformAssignedExpr()

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

Definition at line 452 of file parse_target.c.

459 {
460  Relation rd = pstate->p_target_relation;
461  Oid type_id; /* type of value provided */
462  Oid attrtype; /* type of target column */
463  int32 attrtypmod;
464  Oid attrcollation; /* collation of target column */
465  ParseExprKind sv_expr_kind;
466 
467  /*
468  * Save and restore identity of expression type we're parsing. We must
469  * set p_expr_kind here because we can parse subscripts without going
470  * through transformExpr().
471  */
472  Assert(exprKind != EXPR_KIND_NONE);
473  sv_expr_kind = pstate->p_expr_kind;
474  pstate->p_expr_kind = exprKind;
475 
476  Assert(rd != NULL);
477  if (attrno <= 0)
478  ereport(ERROR,
479  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
480  errmsg("cannot assign to system column \"%s\"",
481  colname),
482  parser_errposition(pstate, location)));
483  attrtype = attnumTypeId(rd, attrno);
484  attrtypmod = TupleDescAttr(rd->rd_att, attrno - 1)->atttypmod;
485  attrcollation = TupleDescAttr(rd->rd_att, attrno - 1)->attcollation;
486 
487  /*
488  * If the expression is a DEFAULT placeholder, insert the attribute's
489  * type/typmod/collation into it so that exprType etc will report the
490  * right things. (We expect that the eventually substituted default
491  * expression will in fact have this type and typmod. The collation
492  * likely doesn't matter, but let's set it correctly anyway.) Also,
493  * reject trying to update a subfield or array element with DEFAULT, since
494  * there can't be any default for portions of a column.
495  */
496  if (expr && IsA(expr, SetToDefault))
497  {
498  SetToDefault *def = (SetToDefault *) expr;
499 
500  def->typeId = attrtype;
501  def->typeMod = attrtypmod;
502  def->collation = attrcollation;
503  if (indirection)
504  {
505  if (IsA(linitial(indirection), A_Indices))
506  ereport(ERROR,
507  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
508  errmsg("cannot set an array element to DEFAULT"),
509  parser_errposition(pstate, location)));
510  else
511  ereport(ERROR,
512  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
513  errmsg("cannot set a subfield to DEFAULT"),
514  parser_errposition(pstate, location)));
515  }
516  }
517 
518  /* Now we can use exprType() safely. */
519  type_id = exprType((Node *) expr);
520 
521  /*
522  * If there is indirection on the target column, prepare an array or
523  * subfield assignment expression. This will generate a new column value
524  * that the source value has been inserted into, which can then be placed
525  * in the new tuple constructed by INSERT or UPDATE.
526  */
527  if (indirection)
528  {
529  Node *colVar;
530 
531  if (pstate->p_is_insert)
532  {
533  /*
534  * The command is INSERT INTO table (col.something) ... so there
535  * is not really a source value to work with. Insert a NULL
536  * constant as the source value.
537  */
538  colVar = (Node *) makeNullConst(attrtype, attrtypmod,
539  attrcollation);
540  }
541  else
542  {
543  /*
544  * Build a Var for the column to be updated.
545  */
546  Var *var;
547 
548  var = makeVar(pstate->p_target_nsitem->p_rtindex, attrno,
549  attrtype, attrtypmod, attrcollation, 0);
550  var->location = location;
551 
552  colVar = (Node *) var;
553  }
554 
555  expr = (Expr *)
557  colVar,
558  colname,
559  false,
560  attrtype,
561  attrtypmod,
562  attrcollation,
563  indirection,
564  list_head(indirection),
565  (Node *) expr,
567  location);
568  }
569  else
570  {
571  /*
572  * For normal non-qualified target column, do type checking and
573  * coercion.
574  */
575  Node *orig_expr = (Node *) expr;
576 
577  expr = (Expr *)
578  coerce_to_target_type(pstate,
579  orig_expr, type_id,
580  attrtype, attrtypmod,
583  -1);
584  if (expr == NULL)
585  ereport(ERROR,
586  (errcode(ERRCODE_DATATYPE_MISMATCH),
587  errmsg("column \"%s\" is of type %s"
588  " but expression is of type %s",
589  colname,
590  format_type_be(attrtype),
591  format_type_be(type_id)),
592  errhint("You will need to rewrite or cast the expression."),
593  parser_errposition(pstate, exprLocation(orig_expr))));
594  }
595 
596  pstate->p_expr_kind = sv_expr_kind;
597 
598  return expr;
599 }
signed int int32
Definition: c.h:494
int errhint(const char *fmt,...)
Definition: elog.c:1317
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:339
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1386
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:78
ParseExprKind
Definition: parse_node.h:39
@ EXPR_KIND_NONE
Definition: parse_node.h:40
Oid attnumTypeId(Relation rd, int attid)
Node * transformAssignmentIndirection(ParseState *pstate, Node *basenode, const char *targetName, bool targetIsSubscripting, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, List *indirection, ListCell *indirection_cell, Node *rhs, CoercionContext ccontext, int location)
Definition: parse_target.c:683
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
@ COERCION_ASSIGNMENT
Definition: primnodes.h:715
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:208
ParseExprKind p_expr_kind
Definition: parse_node.h:211
bool p_is_insert
Definition: parse_node.h:209

References Assert, attnumTypeId(), COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_NONE, exprLocation(), exprType(), format_type_be(), IsA, linitial, list_head(), Var::location, makeNullConst(), makeVar(), ParseState::p_expr_kind, ParseState::p_is_insert, ParseNamespaceItem::p_rtindex, ParseState::p_target_nsitem, ParseState::p_target_relation, parser_errposition(), RelationData::rd_att, transformAssignmentIndirection(), TupleDescAttr, and SetToDefault::typeId.

Referenced by transformInsertRow(), and updateTargetListEntry().

◆ transformAssignmentIndirection()

Node* transformAssignmentIndirection ( ParseState pstate,
Node basenode,
const char *  targetName,
bool  targetIsSubscripting,
Oid  targetTypeId,
int32  targetTypMod,
Oid  targetCollation,
List indirection,
ListCell indirection_cell,
Node rhs,
CoercionContext  ccontext,
int  location 
)

Definition at line 683 of file parse_target.c.

695 {
696  Node *result;
697  List *subscripts = NIL;
698  ListCell *i;
699 
700  if (indirection_cell && !basenode)
701  {
702  /*
703  * Set up a substitution. We abuse CaseTestExpr for this. It's safe
704  * to do so because the only nodes that will be above the CaseTestExpr
705  * in the finished expression will be FieldStore and SubscriptingRef
706  * nodes. (There could be other stuff in the tree, but it will be
707  * within other child fields of those node types.)
708  */
710 
711  ctest->typeId = targetTypeId;
712  ctest->typeMod = targetTypMod;
713  ctest->collation = targetCollation;
714  basenode = (Node *) ctest;
715  }
716 
717  /*
718  * We have to split any field-selection operations apart from
719  * subscripting. Adjacent A_Indices nodes have to be treated as a single
720  * multidimensional subscript operation.
721  */
722  for_each_cell(i, indirection, indirection_cell)
723  {
724  Node *n = lfirst(i);
725 
726  if (IsA(n, A_Indices))
727  subscripts = lappend(subscripts, n);
728  else if (IsA(n, A_Star))
729  {
730  ereport(ERROR,
731  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
732  errmsg("row expansion via \"*\" is not supported here"),
733  parser_errposition(pstate, location)));
734  }
735  else
736  {
737  FieldStore *fstore;
738  Oid baseTypeId;
739  int32 baseTypeMod;
740  Oid typrelid;
742  Oid fieldTypeId;
743  int32 fieldTypMod;
744  Oid fieldCollation;
745 
746  Assert(IsA(n, String));
747 
748  /* process subscripts before this field selection */
749  if (subscripts)
750  {
751  /* recurse, and then return because we're done */
752  return transformAssignmentSubscripts(pstate,
753  basenode,
754  targetName,
755  targetTypeId,
756  targetTypMod,
757  targetCollation,
758  subscripts,
759  indirection,
760  i,
761  rhs,
762  ccontext,
763  location);
764  }
765 
766  /* No subscripts, so can process field selection here */
767 
768  /*
769  * Look up the composite type, accounting for possibility that
770  * what we are given is a domain over composite.
771  */
772  baseTypeMod = targetTypMod;
773  baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
774 
775  typrelid = typeidTypeRelid(baseTypeId);
776  if (!typrelid)
777  ereport(ERROR,
778  (errcode(ERRCODE_DATATYPE_MISMATCH),
779  errmsg("cannot assign to field \"%s\" of column \"%s\" because its type %s is not a composite type",
780  strVal(n), targetName,
781  format_type_be(targetTypeId)),
782  parser_errposition(pstate, location)));
783 
784  attnum = get_attnum(typrelid, strVal(n));
785  if (attnum == InvalidAttrNumber)
786  ereport(ERROR,
787  (errcode(ERRCODE_UNDEFINED_COLUMN),
788  errmsg("cannot assign to field \"%s\" of column \"%s\" because there is no such column in data type %s",
789  strVal(n), targetName,
790  format_type_be(targetTypeId)),
791  parser_errposition(pstate, location)));
792  if (attnum < 0)
793  ereport(ERROR,
794  (errcode(ERRCODE_UNDEFINED_COLUMN),
795  errmsg("cannot assign to system column \"%s\"",
796  strVal(n)),
797  parser_errposition(pstate, location)));
798 
799  get_atttypetypmodcoll(typrelid, attnum,
800  &fieldTypeId, &fieldTypMod, &fieldCollation);
801 
802  /* recurse to create appropriate RHS for field assign */
803  rhs = transformAssignmentIndirection(pstate,
804  NULL,
805  strVal(n),
806  false,
807  fieldTypeId,
808  fieldTypMod,
809  fieldCollation,
810  indirection,
811  lnext(indirection, i),
812  rhs,
813  ccontext,
814  location);
815 
816  /* and build a FieldStore node */
817  fstore = makeNode(FieldStore);
818  fstore->arg = (Expr *) basenode;
819  fstore->newvals = list_make1(rhs);
820  fstore->fieldnums = list_make1_int(attnum);
821  fstore->resulttype = baseTypeId;
822 
823  /*
824  * If target is a domain, apply constraints. Notice that this
825  * isn't totally right: the expression tree we build would check
826  * the domain's constraints on a composite value with only this
827  * one field populated or updated, possibly leading to an unwanted
828  * failure. The rewriter will merge together any subfield
829  * assignments to the same table column, resulting in the domain's
830  * constraints being checked only once after we've assigned to all
831  * the fields that the INSERT or UPDATE means to.
832  */
833  if (baseTypeId != targetTypeId)
834  return coerce_to_domain((Node *) fstore,
835  baseTypeId, baseTypeMod,
836  targetTypeId,
839  location,
840  false);
841 
842  return (Node *) fstore;
843  }
844  }
845 
846  /* process trailing subscripts, if any */
847  if (subscripts)
848  {
849  /* recurse, and then return because we're done */
850  return transformAssignmentSubscripts(pstate,
851  basenode,
852  targetName,
853  targetTypeId,
854  targetTypMod,
855  targetCollation,
856  subscripts,
857  indirection,
858  NULL,
859  rhs,
860  ccontext,
861  location);
862  }
863 
864  /* base case: just coerce RHS to match target type ID */
865 
866  result = coerce_to_target_type(pstate,
867  rhs, exprType(rhs),
868  targetTypeId, targetTypMod,
869  ccontext,
871  -1);
872  if (result == NULL)
873  {
874  if (targetIsSubscripting)
875  ereport(ERROR,
876  (errcode(ERRCODE_DATATYPE_MISMATCH),
877  errmsg("subscripted assignment to \"%s\" requires type %s"
878  " but expression is of type %s",
879  targetName,
880  format_type_be(targetTypeId),
881  format_type_be(exprType(rhs))),
882  errhint("You will need to rewrite or cast the expression."),
883  parser_errposition(pstate, location)));
884  else
885  ereport(ERROR,
886  (errcode(ERRCODE_DATATYPE_MISMATCH),
887  errmsg("subfield \"%s\" is of type %s"
888  " but expression is of type %s",
889  targetName,
890  format_type_be(targetTypeId),
891  format_type_be(exprType(rhs))),
892  errhint("You will need to rewrite or cast the expression."),
893  parser_errposition(pstate, location)));
894  }
895 
896  return result;
897 }
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:858
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2538
void get_atttypetypmodcoll(Oid relid, AttrNumber attnum, Oid *typid, int32 *typmod, Oid *collid)
Definition: lsyscache.c:943
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
Definition: parse_coerce.c:676
static Node * transformAssignmentSubscripts(ParseState *pstate, Node *basenode, const char *targetName, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, List *subscripts, List *indirection, ListCell *next_indirection, Node *rhs, CoercionContext ccontext, int location)
Definition: parse_target.c:903
Oid typeidTypeRelid(Oid type_id)
Definition: parse_type.c:668
#define list_make1(x1)
Definition: pg_list.h:212
#define for_each_cell(cell, lst, initcell)
Definition: pg_list.h:438
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define list_make1_int(x1)
Definition: pg_list.h:227
List * newvals
Definition: primnodes.h:1160
Expr * arg
Definition: primnodes.h:1159

References FieldStore::arg, Assert, attnum, COERCE_IMPLICIT_CAST, coerce_to_domain(), coerce_to_target_type(), COERCION_IMPLICIT, ereport, errcode(), errhint(), errmsg(), ERROR, exprType(), for_each_cell, format_type_be(), get_attnum(), get_atttypetypmodcoll(), getBaseTypeAndTypmod(), i, InvalidAttrNumber, IsA, lappend(), lfirst, list_make1, list_make1_int, lnext(), makeNode, FieldStore::newvals, NIL, parser_errposition(), strVal, transformAssignmentSubscripts(), CaseTestExpr::typeId, and typeidTypeRelid().

Referenced by transformAssignedExpr(), transformAssignmentSubscripts(), and transformPLAssignStmt().

◆ transformAssignmentSubscripts()

static Node * transformAssignmentSubscripts ( ParseState pstate,
Node basenode,
const char *  targetName,
Oid  targetTypeId,
int32  targetTypMod,
Oid  targetCollation,
List subscripts,
List indirection,
ListCell next_indirection,
Node rhs,
CoercionContext  ccontext,
int  location 
)
static

Definition at line 903 of file parse_target.c.

915 {
916  Node *result;
917  SubscriptingRef *sbsref;
918  Oid containerType;
919  int32 containerTypMod;
920  Oid typeNeeded;
921  int32 typmodNeeded;
922  Oid collationNeeded;
923 
924  Assert(subscripts != NIL);
925 
926  /* Identify the actual container type involved */
927  containerType = targetTypeId;
928  containerTypMod = targetTypMod;
929  transformContainerType(&containerType, &containerTypMod);
930 
931  /* Process subscripts and identify required type for RHS */
932  sbsref = transformContainerSubscripts(pstate,
933  basenode,
934  containerType,
935  containerTypMod,
936  subscripts,
937  true);
938 
939  typeNeeded = sbsref->refrestype;
940  typmodNeeded = sbsref->reftypmod;
941 
942  /*
943  * Container normally has same collation as its elements, but there's an
944  * exception: we might be subscripting a domain over a container type. In
945  * that case use collation of the base type. (This is shaky for arbitrary
946  * subscripting semantics, but it doesn't matter all that much since we
947  * only use this to label the collation of a possible CaseTestExpr.)
948  */
949  if (containerType == targetTypeId)
950  collationNeeded = targetCollation;
951  else
952  collationNeeded = get_typcollation(containerType);
953 
954  /* recurse to create appropriate RHS for container assign */
955  rhs = transformAssignmentIndirection(pstate,
956  NULL,
957  targetName,
958  true,
959  typeNeeded,
960  typmodNeeded,
961  collationNeeded,
962  indirection,
963  next_indirection,
964  rhs,
965  ccontext,
966  location);
967 
968  /*
969  * Insert the already-properly-coerced RHS into the SubscriptingRef. Then
970  * set refrestype and reftypmod back to the container type's values.
971  */
972  sbsref->refassgnexpr = (Expr *) rhs;
973  sbsref->refrestype = containerType;
974  sbsref->reftypmod = containerTypMod;
975 
976  result = (Node *) sbsref;
977 
978  /*
979  * If target was a domain over container, need to coerce up to the domain.
980  * As in transformAssignmentIndirection, this coercion is premature if the
981  * query assigns to multiple elements of the container; but we'll fix that
982  * during query rewrite.
983  */
984  if (containerType != targetTypeId)
985  {
986  Oid resulttype = exprType(result);
987 
988  result = coerce_to_target_type(pstate,
989  result, resulttype,
990  targetTypeId, targetTypMod,
991  ccontext,
993  -1);
994  /* can fail if we had int2vector/oidvector, but not for true domains */
995  if (result == NULL)
996  ereport(ERROR,
997  (errcode(ERRCODE_CANNOT_COERCE),
998  errmsg("cannot cast type %s to %s",
999  format_type_be(resulttype),
1000  format_type_be(targetTypeId)),
1001  parser_errposition(pstate, location)));
1002  }
1003 
1004  return result;
1005 }
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:3056
SubscriptingRef * transformContainerSubscripts(ParseState *pstate, Node *containerBase, Oid containerType, int32 containerTypMod, List *indirection, bool isAssignment)
Definition: parse_node.c:243
void transformContainerType(Oid *containerType, int32 *containerTypmod)
Definition: parse_node.c:189
Expr * refassgnexpr
Definition: primnodes.h:703

References Assert, COERCE_IMPLICIT_CAST, coerce_to_target_type(), ereport, errcode(), errmsg(), ERROR, exprType(), format_type_be(), get_typcollation(), NIL, parser_errposition(), SubscriptingRef::refassgnexpr, transformAssignmentIndirection(), transformContainerSubscripts(), and transformContainerType().

Referenced by transformAssignmentIndirection().

◆ transformExpressionList()

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

Definition at line 220 of file parse_target.c.

222 {
223  List *result = NIL;
224  ListCell *lc;
225 
226  foreach(lc, exprlist)
227  {
228  Node *e = (Node *) lfirst(lc);
229 
230  /*
231  * Check for "something.*". Depending on the complexity of the
232  * "something", the star could appear as the last field in ColumnRef,
233  * or as the last indirection item in A_Indirection.
234  */
235  if (IsA(e, ColumnRef))
236  {
237  ColumnRef *cref = (ColumnRef *) e;
238 
239  if (IsA(llast(cref->fields), A_Star))
240  {
241  /* It is something.*, expand into multiple items */
242  result = list_concat(result,
243  ExpandColumnRefStar(pstate, cref,
244  false));
245  continue;
246  }
247  }
248  else if (IsA(e, A_Indirection))
249  {
251 
252  if (IsA(llast(ind->indirection), A_Star))
253  {
254  /* It is something.*, expand into multiple items */
255  result = list_concat(result,
256  ExpandIndirectionStar(pstate, ind,
257  false, exprKind));
258  continue;
259  }
260  }
261 
262  /*
263  * Not "something.*", so transform as a single expression. If it's a
264  * SetToDefault node and we should allow that, pass it through
265  * unmodified. (transformExpr will throw the appropriate error if
266  * we're disallowing it.)
267  */
268  if (allowDefault && IsA(e, SetToDefault))
269  /* do nothing */ ;
270  else
271  e = transformExpr(pstate, e, exprKind);
272 
273  result = lappend(result, e);
274  }
275 
276  return result;
277 }
static List * ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref, bool make_target_entry)
static List * ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind, bool make_target_entry, ParseExprKind exprKind)
struct A_Star A_Star
e
Definition: preproc-init.c:82

References ExpandColumnRefStar(), ExpandIndirectionStar(), ColumnRef::fields, IsA, lappend(), lfirst, list_concat(), llast, NIL, and transformExpr().

Referenced by transformMergeStmt(), transformRowExpr(), and transformValuesClause().

◆ transformTargetEntry()

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

Definition at line 75 of file parse_target.c.

81 {
82  /* Transform the node if caller didn't do it already */
83  if (expr == NULL)
84  {
85  /*
86  * If it's a SetToDefault node and we should allow that, pass it
87  * through unmodified. (transformExpr will throw the appropriate
88  * error if we're disallowing it.)
89  */
90  if (exprKind == EXPR_KIND_UPDATE_SOURCE && IsA(node, SetToDefault))
91  expr = node;
92  else
93  expr = transformExpr(pstate, node, exprKind);
94  }
95 
96  if (colname == NULL && !resjunk)
97  {
98  /*
99  * Generate a suitable column name for a column without any explicit
100  * 'AS ColumnName' clause.
101  */
102  colname = FigureColname(node);
103  }
104 
105  return makeTargetEntry((Expr *) expr,
106  (AttrNumber) pstate->p_next_resno++,
107  colname,
108  resjunk);
109 }
@ EXPR_KIND_UPDATE_SOURCE
Definition: parse_node.h:56
char * FigureColname(Node *node)

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

Referenced by findTargetlistEntrySQL99(), and transformTargetList().

◆ transformTargetList()

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

Definition at line 121 of file parse_target.c.

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

References Assert, ExpandColumnRefStar(), ExpandIndirectionStar(), EXPR_KIND_UPDATE_SOURCE, ColumnRef::fields, if(), IsA, lappend(), lfirst, list_concat(), llast, NIL, ParseState::p_multiassign_exprs, res, and transformTargetEntry().

Referenced by transformPLAssignStmt(), transformReturningList(), transformSelectStmt(), and transformUpdateTargetList().

◆ updateTargetListEntry()

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

Definition at line 619 of file parse_target.c.

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

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

Referenced by transformUpdateTargetList().