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 1018 of file parse_target.c.

1019 {
1020  *attrnos = NIL;
1021 
1022  if (cols == NIL)
1023  {
1024  /*
1025  * Generate default column list for INSERT.
1026  */
1027  int numcol = RelationGetNumberOfAttributes(pstate->p_target_relation);
1028 
1029  int i;
1030 
1031  for (i = 0; i < numcol; i++)
1032  {
1033  ResTarget *col;
1034  Form_pg_attribute attr;
1035 
1036  attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
1037 
1038  if (attr->attisdropped)
1039  continue;
1040 
1041  col = makeNode(ResTarget);
1042  col->name = pstrdup(NameStr(attr->attname));
1043  col->indirection = NIL;
1044  col->val = NULL;
1045  col->location = -1;
1046  cols = lappend(cols, col);
1047  *attrnos = lappend_int(*attrnos, i + 1);
1048  }
1049  }
1050  else
1051  {
1052  /*
1053  * Do initial validation of user-supplied INSERT column list.
1054  */
1055  Bitmapset *wholecols = NULL;
1056  Bitmapset *partialcols = NULL;
1057  ListCell *tl;
1058 
1059  foreach(tl, cols)
1060  {
1061  ResTarget *col = (ResTarget *) lfirst(tl);
1062  char *name = col->name;
1063  int attrno;
1064 
1065  /* Lookup column name, ereport on failure */
1066  attrno = attnameAttNum(pstate->p_target_relation, name, false);
1067  if (attrno == InvalidAttrNumber)
1068  ereport(ERROR,
1069  (errcode(ERRCODE_UNDEFINED_COLUMN),
1070  errmsg("column \"%s\" of relation \"%s\" does not exist",
1071  name,
1073  parser_errposition(pstate, col->location)));
1074 
1075  /*
1076  * Check for duplicates, but only of whole columns --- we allow
1077  * INSERT INTO foo (col.subcol1, col.subcol2)
1078  */
1079  if (col->indirection == NIL)
1080  {
1081  /* whole column; must not have any other assignment */
1082  if (bms_is_member(attrno, wholecols) ||
1083  bms_is_member(attrno, partialcols))
1084  ereport(ERROR,
1085  (errcode(ERRCODE_DUPLICATE_COLUMN),
1086  errmsg("column \"%s\" specified more than once",
1087  name),
1088  parser_errposition(pstate, col->location)));
1089  wholecols = bms_add_member(wholecols, attrno);
1090  }
1091  else
1092  {
1093  /* partial column; must not have any whole assignment */
1094  if (bms_is_member(attrno, wholecols))
1095  ereport(ERROR,
1096  (errcode(ERRCODE_DUPLICATE_COLUMN),
1097  errmsg("column \"%s\" specified more than once",
1098  name),
1099  parser_errposition(pstate, col->location)));
1100  partialcols = bms_add_member(partialcols, attrno);
1101  }
1102 
1103  *attrnos = lappend_int(*attrnos, attrno);
1104  }
1105  }
1106 
1107  return cols;
1108 }
#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:749
int errcode(int sqlerrcode)
Definition: elog.c:853
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:1696
#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:209
TupleDesc rd_att
Definition: rel.h:112
Node * val
Definition: parsenodes.h:521
ParseLoc location
Definition: parsenodes.h:522
List * indirection
Definition: parsenodes.h:520
char * name
Definition: parsenodes.h:519
#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 1296 of file parse_target.c.

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

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 1123 of file parse_target.c.

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

1350 {
1351  Node *expr;
1352 
1353  /* Strip off the '*' to create a reference to the rowtype object */
1354  ind = copyObject(ind);
1355  ind->indirection = list_truncate(ind->indirection,
1356  list_length(ind->indirection) - 1);
1357 
1358  /* And transform that */
1359  expr = transformExpr(pstate, (Node *) ind, exprKind);
1360 
1361  /* Expand the rowtype expression into individual fields */
1362  return ExpandRowReference(pstate, expr, make_target_entry);
1363 }
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:120

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 1522 of file parse_target.c.

1523 {
1524  TupleDesc tupleDesc;
1525  int netlevelsup;
1526  RangeTblEntry *rte;
1528  Node *expr;
1529 
1530  /* Check my caller didn't mess up */
1531  Assert(IsA(var, Var));
1532  Assert(var->vartype == RECORDOID);
1533 
1534  /*
1535  * Note: it's tempting to use GetNSItemByRangeTablePosn here so that we
1536  * can use expandNSItemVars instead of expandRTE; but that does not work
1537  * for some of the recursion cases below, where we have consed up a
1538  * ParseState that lacks p_namespace data.
1539  */
1540  netlevelsup = var->varlevelsup + levelsup;
1541  rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
1542  attnum = var->varattno;
1543 
1544  if (attnum == InvalidAttrNumber)
1545  {
1546  /* Whole-row reference to an RTE, so expand the known fields */
1547  List *names,
1548  *vars;
1549  ListCell *lname,
1550  *lvar;
1551  int i;
1552 
1553  expandRTE(rte, var->varno, 0, var->location, false,
1554  &names, &vars);
1555 
1557  i = 1;
1558  forboth(lname, names, lvar, vars)
1559  {
1560  char *label = strVal(lfirst(lname));
1561  Node *varnode = (Node *) lfirst(lvar);
1562 
1563  TupleDescInitEntry(tupleDesc, i,
1564  label,
1565  exprType(varnode),
1566  exprTypmod(varnode),
1567  0);
1568  TupleDescInitEntryCollation(tupleDesc, i,
1569  exprCollation(varnode));
1570  i++;
1571  }
1572  Assert(lname == NULL && lvar == NULL); /* lists same length? */
1573 
1574  return tupleDesc;
1575  }
1576 
1577  expr = (Node *) var; /* default if we can't drill down */
1578 
1579  switch (rte->rtekind)
1580  {
1581  case RTE_RELATION:
1582  case RTE_VALUES:
1583  case RTE_NAMEDTUPLESTORE:
1584  case RTE_RESULT:
1585 
1586  /*
1587  * This case should not occur: a column of a table, values list,
1588  * or ENR shouldn't have type RECORD. Fall through and fail (most
1589  * likely) at the bottom.
1590  */
1591  break;
1592  case RTE_SUBQUERY:
1593  {
1594  /* Subselect-in-FROM: examine sub-select's output expr */
1596  attnum);
1597 
1598  if (ste == NULL || ste->resjunk)
1599  elog(ERROR, "subquery %s does not have attribute %d",
1600  rte->eref->aliasname, attnum);
1601  expr = (Node *) ste->expr;
1602  if (IsA(expr, Var))
1603  {
1604  /*
1605  * Recurse into the sub-select to see what its Var refers
1606  * to. We have to build an additional level of ParseState
1607  * to keep in step with varlevelsup in the subselect;
1608  * furthermore, the subquery RTE might be from an outer
1609  * query level, in which case the ParseState for the
1610  * subselect must have that outer level as parent.
1611  */
1612  ParseState mypstate = {0};
1613  Index levelsup;
1614 
1615  /* this loop must work, since GetRTEByRangeTablePosn did */
1616  for (levelsup = 0; levelsup < netlevelsup; levelsup++)
1617  pstate = pstate->parentParseState;
1618  mypstate.parentParseState = pstate;
1619  mypstate.p_rtable = rte->subquery->rtable;
1620  /* don't bother filling the rest of the fake pstate */
1621 
1622  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1623  }
1624  /* else fall through to inspect the expression */
1625  }
1626  break;
1627  case RTE_JOIN:
1628  /* Join RTE --- recursively inspect the alias variable */
1629  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
1630  expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1631  Assert(expr != NULL);
1632  /* We intentionally don't strip implicit coercions here */
1633  if (IsA(expr, Var))
1634  return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
1635  /* else fall through to inspect the expression */
1636  break;
1637  case RTE_FUNCTION:
1638 
1639  /*
1640  * We couldn't get here unless a function is declared with one of
1641  * its result columns as RECORD, which is not allowed.
1642  */
1643  break;
1644  case RTE_TABLEFUNC:
1645 
1646  /*
1647  * Table function cannot have columns with RECORD type.
1648  */
1649  break;
1650  case RTE_CTE:
1651  /* CTE reference: examine subquery's output expr */
1652  if (!rte->self_reference)
1653  {
1654  CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
1655  TargetEntry *ste;
1656 
1658  if (ste == NULL || ste->resjunk)
1659  elog(ERROR, "CTE %s does not have attribute %d",
1660  rte->eref->aliasname, attnum);
1661  expr = (Node *) ste->expr;
1662  if (IsA(expr, Var))
1663  {
1664  /*
1665  * Recurse into the CTE to see what its Var refers to. We
1666  * have to build an additional level of ParseState to keep
1667  * in step with varlevelsup in the CTE; furthermore it
1668  * could be an outer CTE (compare SUBQUERY case above).
1669  */
1670  ParseState mypstate = {0};
1671  Index levelsup;
1672 
1673  /* this loop must work, since GetCTEForRTE did */
1674  for (levelsup = 0;
1675  levelsup < rte->ctelevelsup + netlevelsup;
1676  levelsup++)
1677  pstate = pstate->parentParseState;
1678  mypstate.parentParseState = pstate;
1679  mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
1680  /* don't bother filling the rest of the fake pstate */
1681 
1682  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1683  }
1684  /* else fall through to inspect the expression */
1685  }
1686  break;
1687  case RTE_GROUP:
1688 
1689  /*
1690  * We couldn't get here: the RTE_GROUP RTE has not been added.
1691  */
1692  break;
1693  }
1694 
1695  /*
1696  * We now have an expression we can't expand any more, so see if
1697  * get_expr_result_tupdesc() can do anything with it.
1698  */
1699  return get_expr_result_tupdesc(expr, false);
1700 }
int16 AttrNumber
Definition: attnum.h:21
unsigned int Index
Definition: c.h:617
#define elog(elevel,...)
Definition: elog.h:225
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:1704
@ RTE_JOIN
Definition: parsenodes.h:1019
@ RTE_CTE
Definition: parsenodes.h:1023
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1024
@ RTE_VALUES
Definition: parsenodes.h:1022
@ RTE_SUBQUERY
Definition: parsenodes.h:1018
@ RTE_RESULT
Definition: parsenodes.h:1025
@ RTE_FUNCTION
Definition: parsenodes.h:1020
@ RTE_TABLEFUNC
Definition: parsenodes.h:1021
@ RTE_GROUP
Definition: parsenodes.h:1028
@ RTE_RELATION
Definition: parsenodes.h:1017
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:194
List * p_rtable
Definition: parse_node.h:196
List * rtable
Definition: parsenodes.h:170
List * targetList
Definition: parsenodes.h:193
Index ctelevelsup
Definition: parsenodes.h:1198
Query * subquery
Definition: parsenodes.h:1104
RTEKind rtekind
Definition: parsenodes.h:1047
Expr * expr
Definition: primnodes.h:2186
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_GROUP, 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 1426 of file parse_target.c.

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

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 1375 of file parse_target.c.

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

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 1713 of file parse_target.c.

1714 {
1715  char *name = NULL;
1716 
1717  (void) FigureColnameInternal(node, &name);
1718  if (name != NULL)
1719  return name;
1720  /* default result if we can't guess anything */
1721  return "?column?";
1722 }
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 1752 of file parse_target.c.

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

1733 {
1734  char *name = NULL;
1735 
1736  (void) FigureColnameInternal(node, &name);
1737  return name;
1738 }

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  case RTE_GROUP:
424  /* We couldn't get here: the RTE_GROUP RTE has not been added */
425  break;
426  }
427 }

References attnum, elog, ERROR, get_tle_by_resno(), GetCTEForRTE(), GetCTETargetList, GetRTEByRangeTablePosn(), InvalidAttrNumber, IsA, list_length(), RangeTblEntry::relid, RTE_CTE, RTE_FUNCTION, RTE_GROUP, 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 455 of file parse_target.c.

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

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 686 of file parse_target.c.

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

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

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 622 of file parse_target.c.

628 {
629  /* Fix up expression as needed */
630  tle->expr = transformAssignedExpr(pstate,
631  tle->expr,
633  colname,
634  attrno,
635  indirection,
636  location);
637 
638  /*
639  * Set the resno to identify the target column --- the rewriter and
640  * planner depend on this. We also set the resname to identify the target
641  * column, but this is only for debugging purposes; it should not be
642  * relied on. (In particular, it might be out of date in a stored rule.)
643  */
644  tle->resno = (AttrNumber) attrno;
645  tle->resname = colname;
646 }
@ 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:455
AttrNumber resno
Definition: primnodes.h:2188

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

Referenced by transformUpdateTargetList().