PostgreSQL Source Code  git master
parse_target.c File Reference
#include "postgres.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_func.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 "utils/typcache.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 1002 of file parse_target.c.

1003 {
1004  *attrnos = NIL;
1005 
1006  if (cols == NIL)
1007  {
1008  /*
1009  * Generate default column list for INSERT.
1010  */
1011  int numcol = RelationGetNumberOfAttributes(pstate->p_target_relation);
1012 
1013  int i;
1014 
1015  for (i = 0; i < numcol; i++)
1016  {
1017  ResTarget *col;
1018  Form_pg_attribute attr;
1019 
1020  attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
1021 
1022  if (attr->attisdropped)
1023  continue;
1024 
1025  col = makeNode(ResTarget);
1026  col->name = pstrdup(NameStr(attr->attname));
1027  col->indirection = NIL;
1028  col->val = NULL;
1029  col->location = -1;
1030  cols = lappend(cols, col);
1031  *attrnos = lappend_int(*attrnos, i + 1);
1032  }
1033  }
1034  else
1035  {
1036  /*
1037  * Do initial validation of user-supplied INSERT column list.
1038  */
1039  Bitmapset *wholecols = NULL;
1040  Bitmapset *partialcols = NULL;
1041  ListCell *tl;
1042 
1043  foreach(tl, cols)
1044  {
1045  ResTarget *col = (ResTarget *) lfirst(tl);
1046  char *name = col->name;
1047  int attrno;
1048 
1049  /* Lookup column name, ereport on failure */
1050  attrno = attnameAttNum(pstate->p_target_relation, name, false);
1051  if (attrno == InvalidAttrNumber)
1052  ereport(ERROR,
1053  (errcode(ERRCODE_UNDEFINED_COLUMN),
1054  errmsg("column \"%s\" of relation \"%s\" does not exist",
1055  name,
1057  parser_errposition(pstate, col->location)));
1058 
1059  /*
1060  * Check for duplicates, but only of whole columns --- we allow
1061  * INSERT INTO foo (col.subcol1, col.subcol2)
1062  */
1063  if (col->indirection == NIL)
1064  {
1065  /* whole column; must not have any other assignment */
1066  if (bms_is_member(attrno, wholecols) ||
1067  bms_is_member(attrno, partialcols))
1068  ereport(ERROR,
1069  (errcode(ERRCODE_DUPLICATE_COLUMN),
1070  errmsg("column \"%s\" specified more than once",
1071  name),
1072  parser_errposition(pstate, col->location)));
1073  wholecols = bms_add_member(wholecols, attrno);
1074  }
1075  else
1076  {
1077  /* partial column; must not have any whole assignment */
1078  if (bms_is_member(attrno, wholecols))
1079  ereport(ERROR,
1080  (errcode(ERRCODE_DUPLICATE_COLUMN),
1081  errmsg("column \"%s\" specified more than once",
1082  name),
1083  parser_errposition(pstate, col->location)));
1084  partialcols = bms_add_member(partialcols, attrno);
1085  }
1086 
1087  *attrnos = lappend_int(*attrnos, attrno);
1088  }
1089  }
1090 
1091  return cols;
1092 }
#define InvalidAttrNumber
Definition: attnum.h:23
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:444
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:755
#define NameStr(name)
Definition: c.h:730
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
const char * name
Definition: encode.c:571
int i
Definition: isn.c:73
List * lappend(List *list, void *datum)
Definition: list.c:338
List * lappend_int(List *list, int datum)
Definition: list.c:356
char * pstrdup(const char *in)
Definition: mcxt.c:1624
#define makeNode(_type_)
Definition: nodes.h:176
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
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:509
#define RelationGetRelationName(relation)
Definition: rel.h:537
Relation p_target_relation
Definition: parse_node.h:206
TupleDesc rd_att
Definition: rel.h:111
int location
Definition: parsenodes.h:518
Node * val
Definition: parsenodes.h:517
List * indirection
Definition: parsenodes.h:516
char * name
Definition: parsenodes.h:515
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

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

1281 {
1282  List *target = NIL;
1283  bool found_table = false;
1284  ListCell *l;
1285 
1286  foreach(l, pstate->p_namespace)
1287  {
1288  ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(l);
1289 
1290  /* Ignore table-only items */
1291  if (!nsitem->p_cols_visible)
1292  continue;
1293  /* Should not have any lateral-only items when parsing targetlist */
1294  Assert(!nsitem->p_lateral_only);
1295  /* Remember we found a p_cols_visible item */
1296  found_table = true;
1297 
1298  target = list_concat(target,
1299  expandNSItemAttrs(pstate,
1300  nsitem,
1301  0,
1302  true,
1303  location));
1304  }
1305 
1306  /*
1307  * Check for "SELECT *;". We do it this way, rather than checking for
1308  * target == NIL, because we want to allow SELECT * FROM a zero_column
1309  * table.
1310  */
1311  if (!found_table)
1312  ereport(ERROR,
1313  (errcode(ERRCODE_SYNTAX_ERROR),
1314  errmsg("SELECT * with no tables specified is not valid"),
1315  parser_errposition(pstate, location)));
1316 
1317  return target;
1318 }
Assert(fmt[strlen(fmt) - 1] !='\n')
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
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:200

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

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

1334 {
1335  Node *expr;
1336 
1337  /* Strip off the '*' to create a reference to the rowtype object */
1338  ind = copyObject(ind);
1339  ind->indirection = list_truncate(ind->indirection,
1340  list_length(ind->indirection) - 1);
1341 
1342  /* And transform that */
1343  expr = transformExpr(pstate, (Node *) ind, exprKind);
1344 
1345  /* Expand the rowtype expression into individual fields */
1346  return ExpandRowReference(pstate, expr, make_target_entry);
1347 }
List * list_truncate(List *list, int new_size)
Definition: list.c:630
#define copyObject(obj)
Definition: nodes.h:244
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:103

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

1506 {
1507  TupleDesc tupleDesc;
1508  int netlevelsup;
1509  RangeTblEntry *rte;
1511  Node *expr;
1512 
1513  /* Check my caller didn't mess up */
1514  Assert(IsA(var, Var));
1515  Assert(var->vartype == RECORDOID);
1516 
1517  /*
1518  * Note: it's tempting to use GetNSItemByRangeTablePosn here so that we
1519  * can use expandNSItemVars instead of expandRTE; but that does not work
1520  * for some of the recursion cases below, where we have consed up a
1521  * ParseState that lacks p_namespace data.
1522  */
1523  netlevelsup = var->varlevelsup + levelsup;
1524  rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
1525  attnum = var->varattno;
1526 
1527  if (attnum == InvalidAttrNumber)
1528  {
1529  /* Whole-row reference to an RTE, so expand the known fields */
1530  List *names,
1531  *vars;
1532  ListCell *lname,
1533  *lvar;
1534  int i;
1535 
1536  expandRTE(rte, var->varno, 0, var->location, false,
1537  &names, &vars);
1538 
1540  i = 1;
1541  forboth(lname, names, lvar, vars)
1542  {
1543  char *label = strVal(lfirst(lname));
1544  Node *varnode = (Node *) lfirst(lvar);
1545 
1546  TupleDescInitEntry(tupleDesc, i,
1547  label,
1548  exprType(varnode),
1549  exprTypmod(varnode),
1550  0);
1551  TupleDescInitEntryCollation(tupleDesc, i,
1552  exprCollation(varnode));
1553  i++;
1554  }
1555  Assert(lname == NULL && lvar == NULL); /* lists same length? */
1556 
1557  return tupleDesc;
1558  }
1559 
1560  expr = (Node *) var; /* default if we can't drill down */
1561 
1562  switch (rte->rtekind)
1563  {
1564  case RTE_RELATION:
1565  case RTE_VALUES:
1566  case RTE_NAMEDTUPLESTORE:
1567  case RTE_RESULT:
1568 
1569  /*
1570  * This case should not occur: a column of a table, values list,
1571  * or ENR shouldn't have type RECORD. Fall through and fail (most
1572  * likely) at the bottom.
1573  */
1574  break;
1575  case RTE_SUBQUERY:
1576  {
1577  /* Subselect-in-FROM: examine sub-select's output expr */
1579  attnum);
1580 
1581  if (ste == NULL || ste->resjunk)
1582  elog(ERROR, "subquery %s does not have attribute %d",
1583  rte->eref->aliasname, attnum);
1584  expr = (Node *) ste->expr;
1585  if (IsA(expr, Var))
1586  {
1587  /*
1588  * Recurse into the sub-select to see what its Var refers
1589  * to. We have to build an additional level of ParseState
1590  * to keep in step with varlevelsup in the subselect.
1591  */
1592  ParseState mypstate = {0};
1593 
1594  mypstate.parentParseState = pstate;
1595  mypstate.p_rtable = rte->subquery->rtable;
1596  /* don't bother filling the rest of the fake pstate */
1597 
1598  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1599  }
1600  /* else fall through to inspect the expression */
1601  }
1602  break;
1603  case RTE_JOIN:
1604  /* Join RTE --- recursively inspect the alias variable */
1605  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
1606  expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1607  Assert(expr != NULL);
1608  /* We intentionally don't strip implicit coercions here */
1609  if (IsA(expr, Var))
1610  return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
1611  /* else fall through to inspect the expression */
1612  break;
1613  case RTE_FUNCTION:
1614 
1615  /*
1616  * We couldn't get here unless a function is declared with one of
1617  * its result columns as RECORD, which is not allowed.
1618  */
1619  break;
1620  case RTE_TABLEFUNC:
1621 
1622  /*
1623  * Table function cannot have columns with RECORD type.
1624  */
1625  break;
1626  case RTE_CTE:
1627  /* CTE reference: examine subquery's output expr */
1628  if (!rte->self_reference)
1629  {
1630  CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
1631  TargetEntry *ste;
1632 
1634  if (ste == NULL || ste->resjunk)
1635  elog(ERROR, "CTE %s does not have attribute %d",
1636  rte->eref->aliasname, attnum);
1637  expr = (Node *) ste->expr;
1638  if (IsA(expr, Var))
1639  {
1640  /*
1641  * Recurse into the CTE to see what its Var refers to. We
1642  * have to build an additional level of ParseState to keep
1643  * in step with varlevelsup in the CTE; furthermore it
1644  * could be an outer CTE.
1645  */
1646  ParseState mypstate;
1647  Index levelsup;
1648 
1649  MemSet(&mypstate, 0, sizeof(mypstate));
1650  /* this loop must work, since GetCTEForRTE did */
1651  for (levelsup = 0;
1652  levelsup < rte->ctelevelsup + netlevelsup;
1653  levelsup++)
1654  pstate = pstate->parentParseState;
1655  mypstate.parentParseState = pstate;
1656  mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
1657  /* don't bother filling the rest of the fake pstate */
1658 
1659  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1660  }
1661  /* else fall through to inspect the expression */
1662  }
1663  break;
1664  }
1665 
1666  /*
1667  * We now have an expression we can't expand any more, so see if
1668  * get_expr_result_tupdesc() can do anything with it.
1669  */
1670  return get_expr_result_tupdesc(expr, false);
1671 }
int16 AttrNumber
Definition: attnum.h:21
unsigned int Index
Definition: c.h:598
#define MemSet(start, val, len)
Definition: c.h:1004
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:543
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:278
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:780
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
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:1659
@ RTE_JOIN
Definition: parsenodes.h:1016
@ RTE_CTE
Definition: parsenodes.h:1020
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1021
@ RTE_VALUES
Definition: parsenodes.h:1019
@ RTE_SUBQUERY
Definition: parsenodes.h:1015
@ RTE_RESULT
Definition: parsenodes.h:1022
@ RTE_FUNCTION
Definition: parsenodes.h:1017
@ RTE_TABLEFUNC
Definition: parsenodes.h:1018
@ RTE_RELATION
Definition: parsenodes.h:1014
int16 attnum
Definition: pg_attribute.h:74
static char * label
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:467
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
char * aliasname
Definition: primnodes.h:42
ParseState * parentParseState
Definition: parse_node.h:191
List * p_rtable
Definition: parse_node.h:193
List * rtable
Definition: parsenodes.h:175
List * targetList
Definition: parsenodes.h:189
bool self_reference
Definition: parsenodes.h:1166
Index ctelevelsup
Definition: parsenodes.h:1165
Query * subquery
Definition: parsenodes.h:1081
Alias * eref
Definition: parsenodes.h:1200
List * joinaliasvars
Definition: parsenodes.h:1129
RTEKind rtekind
Definition: parsenodes.h:1033
Expr * expr
Definition: primnodes.h:1816
Definition: primnodes.h:226
AttrNumber varattno
Definition: primnodes.h:238
int varno
Definition: primnodes.h:233
Index varlevelsup
Definition: primnodes.h:258
int location
Definition: primnodes.h:271
Definition: regcomp.c:282
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:767
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583

References Alias::aliasname, Assert(), attnum, CreateTemplateTupleDesc(), RangeTblEntry::ctelevelsup, CommonTableExpr::ctequery, elog(), RangeTblEntry::eref, ERROR, expandRTE(), TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), forboth, get_expr_result_tupdesc(), get_tle_by_resno(), GetCTEForRTE(), GetCTETargetList, GetRTEByRangeTablePosn(), i, if(), InvalidAttrNumber, IsA, RangeTblEntry::joinaliasvars, label, lfirst, list_length(), list_nth(), Var::location, MemSet, 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, RangeTblEntry::self_reference, 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 1410 of file parse_target.c.

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

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

1361 {
1362  if (make_target_entry)
1363  {
1364  /* expandNSItemAttrs handles permissions marking */
1365  return expandNSItemAttrs(pstate, nsitem, sublevels_up, true, location);
1366  }
1367  else
1368  {
1369  RangeTblEntry *rte = nsitem->p_rte;
1370  RTEPermissionInfo *perminfo = nsitem->p_perminfo;
1371  List *vars;
1372  ListCell *l;
1373 
1374  vars = expandNSItemVars(pstate, nsitem, sublevels_up, location, NULL);
1375 
1376  /*
1377  * Require read access to the table. This is normally redundant with
1378  * the markVarForSelectPriv calls below, but not if the table has zero
1379  * columns. We need not do anything if the nsitem is for a join: its
1380  * component tables will have been marked ACL_SELECT when they were
1381  * added to the rangetable. (This step changes things only for the
1382  * target relation of UPDATE/DELETE, which cannot be under a join.)
1383  */
1384  if (rte->rtekind == RTE_RELATION)
1385  {
1386  Assert(perminfo != NULL);
1387  perminfo->requiredPerms |= ACL_SELECT;
1388  }
1389 
1390  /* Require read access to each column */
1391  foreach(l, vars)
1392  {
1393  Var *var = (Var *) lfirst(l);
1394 
1395  markVarForSelectPriv(pstate, var);
1396  }
1397 
1398  return vars;
1399  }
1400 }
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:84
RTEPermissionInfo * p_perminfo
Definition: parse_node.h:288
AclMode requiredPerms
Definition: parsenodes.h:1246

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

1685 {
1686  char *name = NULL;
1687 
1688  (void) FigureColnameInternal(node, &name);
1689  if (name != NULL)
1690  return name;
1691  /* default result if we can't guess anything */
1692  return "?column?";
1693 }
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 1723 of file parse_target.c.

1724 {
1725  int strength = 0;
1726 
1727  if (node == NULL)
1728  return strength;
1729 
1730  switch (nodeTag(node))
1731  {
1732  case T_ColumnRef:
1733  {
1734  char *fname = NULL;
1735  ListCell *l;
1736 
1737  /* find last field name, if any, ignoring "*" */
1738  foreach(l, ((ColumnRef *) node)->fields)
1739  {
1740  Node *i = lfirst(l);
1741 
1742  if (IsA(i, String))
1743  fname = strVal(i);
1744  }
1745  if (fname)
1746  {
1747  *name = fname;
1748  return 2;
1749  }
1750  }
1751  break;
1752  case T_A_Indirection:
1753  {
1754  A_Indirection *ind = (A_Indirection *) node;
1755  char *fname = NULL;
1756  ListCell *l;
1757 
1758  /* find last field name, if any, ignoring "*" and subscripts */
1759  foreach(l, ind->indirection)
1760  {
1761  Node *i = lfirst(l);
1762 
1763  if (IsA(i, String))
1764  fname = strVal(i);
1765  }
1766  if (fname)
1767  {
1768  *name = fname;
1769  return 2;
1770  }
1771  return FigureColnameInternal(ind->arg, name);
1772  }
1773  break;
1774  case T_FuncCall:
1775  *name = strVal(llast(((FuncCall *) node)->funcname));
1776  return 2;
1777  case T_A_Expr:
1778  if (((A_Expr *) node)->kind == AEXPR_NULLIF)
1779  {
1780  /* make nullif() act like a regular function */
1781  *name = "nullif";
1782  return 2;
1783  }
1784  break;
1785  case T_TypeCast:
1786  strength = FigureColnameInternal(((TypeCast *) node)->arg,
1787  name);
1788  if (strength <= 1)
1789  {
1790  if (((TypeCast *) node)->typeName != NULL)
1791  {
1792  *name = strVal(llast(((TypeCast *) node)->typeName->names));
1793  return 1;
1794  }
1795  }
1796  break;
1797  case T_CollateClause:
1798  return FigureColnameInternal(((CollateClause *) node)->arg, name);
1799  case T_GroupingFunc:
1800  /* make GROUPING() act like a regular function */
1801  *name = "grouping";
1802  return 2;
1803  case T_SubLink:
1804  switch (((SubLink *) node)->subLinkType)
1805  {
1806  case EXISTS_SUBLINK:
1807  *name = "exists";
1808  return 2;
1809  case ARRAY_SUBLINK:
1810  *name = "array";
1811  return 2;
1812  case EXPR_SUBLINK:
1813  {
1814  /* Get column name of the subquery's single target */
1815  SubLink *sublink = (SubLink *) node;
1816  Query *query = (Query *) sublink->subselect;
1817 
1818  /*
1819  * The subquery has probably already been transformed,
1820  * but let's be careful and check that. (The reason
1821  * we can see a transformed subquery here is that
1822  * transformSubLink is lazy and modifies the SubLink
1823  * node in-place.)
1824  */
1825  if (IsA(query, Query))
1826  {
1827  TargetEntry *te = (TargetEntry *) linitial(query->targetList);
1828 
1829  if (te->resname)
1830  {
1831  *name = te->resname;
1832  return 2;
1833  }
1834  }
1835  }
1836  break;
1837  /* As with other operator-like nodes, these have no names */
1838  case MULTIEXPR_SUBLINK:
1839  case ALL_SUBLINK:
1840  case ANY_SUBLINK:
1841  case ROWCOMPARE_SUBLINK:
1842  case CTE_SUBLINK:
1843  break;
1844  }
1845  break;
1846  case T_CaseExpr:
1847  strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult,
1848  name);
1849  if (strength <= 1)
1850  {
1851  *name = "case";
1852  return 1;
1853  }
1854  break;
1855  case T_A_ArrayExpr:
1856  /* make ARRAY[] act like a function */
1857  *name = "array";
1858  return 2;
1859  case T_RowExpr:
1860  /* make ROW() act like a function */
1861  *name = "row";
1862  return 2;
1863  case T_CoalesceExpr:
1864  /* make coalesce() act like a regular function */
1865  *name = "coalesce";
1866  return 2;
1867  case T_MinMaxExpr:
1868  /* make greatest/least act like a regular function */
1869  switch (((MinMaxExpr *) node)->op)
1870  {
1871  case IS_GREATEST:
1872  *name = "greatest";
1873  return 2;
1874  case IS_LEAST:
1875  *name = "least";
1876  return 2;
1877  }
1878  break;
1879  case T_XmlExpr:
1880  /* make SQL/XML functions act like a regular function */
1881  switch (((XmlExpr *) node)->op)
1882  {
1883  case IS_XMLCONCAT:
1884  *name = "xmlconcat";
1885  return 2;
1886  case IS_XMLELEMENT:
1887  *name = "xmlelement";
1888  return 2;
1889  case IS_XMLFOREST:
1890  *name = "xmlforest";
1891  return 2;
1892  case IS_XMLPARSE:
1893  *name = "xmlparse";
1894  return 2;
1895  case IS_XMLPI:
1896  *name = "xmlpi";
1897  return 2;
1898  case IS_XMLROOT:
1899  *name = "xmlroot";
1900  return 2;
1901  case IS_XMLSERIALIZE:
1902  *name = "xmlserialize";
1903  return 2;
1904  case IS_DOCUMENT:
1905  /* nothing */
1906  break;
1907  }
1908  break;
1909  case T_XmlSerialize:
1910  /* make XMLSERIALIZE act like a regular function */
1911  *name = "xmlserialize";
1912  return 2;
1913  case T_JsonObjectConstructor:
1914  /* make JSON_OBJECT act like a regular function */
1915  *name = "json_object";
1916  return 2;
1917  case T_JsonArrayConstructor:
1918  case T_JsonArrayQueryConstructor:
1919  /* make JSON_ARRAY act like a regular function */
1920  *name = "json_array";
1921  return 2;
1922  case T_JsonObjectAgg:
1923  /* make JSON_OBJECTAGG act like a regular function */
1924  *name = "json_objectagg";
1925  return 2;
1926  case T_JsonArrayAgg:
1927  /* make JSON_ARRAYAGG act like a regular function */
1928  *name = "json_arrayagg";
1929  return 2;
1930  default:
1931  break;
1932  }
1933 
1934  return strength;
1935 }
#define funcname
Definition: indent_codes.h:69
#define nodeTag(nodeptr)
Definition: nodes.h:133
@ AEXPR_NULLIF
Definition: parsenodes.h:316
void * arg
#define llast(l)
Definition: pg_list.h:198
@ ARRAY_SUBLINK
Definition: primnodes.h:930
@ ANY_SUBLINK
Definition: primnodes.h:926
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:929
@ CTE_SUBLINK
Definition: primnodes.h:931
@ EXPR_SUBLINK
Definition: primnodes.h:928
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:927
@ ALL_SUBLINK
Definition: primnodes.h:925
@ EXISTS_SUBLINK
Definition: primnodes.h:924
@ IS_LEAST
Definition: primnodes.h:1428
@ IS_GREATEST
Definition: primnodes.h:1427
@ IS_DOCUMENT
Definition: primnodes.h:1468
@ IS_XMLFOREST
Definition: primnodes.h:1463
@ IS_XMLCONCAT
Definition: primnodes.h:1461
@ IS_XMLPI
Definition: primnodes.h:1465
@ IS_XMLPARSE
Definition: primnodes.h:1464
@ IS_XMLSERIALIZE
Definition: primnodes.h:1467
@ IS_XMLROOT
Definition: primnodes.h:1466
@ IS_XMLELEMENT
Definition: primnodes.h:1462
Definition: value.h:64

References AEXPR_NULLIF, ALL_SUBLINK, ANY_SUBLINK, arg, ARRAY_SUBLINK, CTE_SUBLINK, 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, lfirst, linitial, llast, MULTIEXPR_SUBLINK, name, nodeTag, ROWCOMPARE_SUBLINK, strVal, SubLink::subselect, and Query::targetList.

Referenced by FigureColname(), and FigureIndexColname().

◆ FigureIndexColname()

char* FigureIndexColname ( Node node)

Definition at line 1703 of file parse_target.c.

1704 {
1705  char *name = NULL;
1706 
1707  (void) FigureColnameInternal(node, &name);
1708  return name;
1709 }

References FigureColnameInternal(), and name.

Referenced by transformIndexStmt().

◆ markTargetListOrigin()

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

Definition at line 344 of file parse_target.c.

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

References Alias::aliasname, attnum, elog(), RangeTblEntry::eref, 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::self_reference, RangeTblEntry::subquery, Query::targetList, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by markTargetListOrigins().

◆ markTargetListOrigins()

void markTargetListOrigins ( ParseState pstate,
List targetlist 
)

Definition at line 319 of file parse_target.c.

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

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

Referenced by transformReturningList(), and transformSelectStmt().

◆ resolveTargetListUnknowns()

void resolveTargetListUnknowns ( ParseState pstate,
List targetlist 
)

Definition at line 289 of file parse_target.c.

290 {
291  ListCell *l;
292 
293  foreach(l, targetlist)
294  {
295  TargetEntry *tle = (TargetEntry *) lfirst(l);
296  Oid restype = exprType((Node *) tle->expr);
297 
298  if (restype == UNKNOWNOID)
299  {
300  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
301  restype, TEXTOID, -1,
304  -1);
305  }
306  }
307 }
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:663
@ COERCION_IMPLICIT
Definition: primnodes.h:641

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

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

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

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

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

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

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

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

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

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

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

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

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

Referenced by transformUpdateTargetList().