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:428
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:739
#define NameStr(name)
Definition: c.h:682
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
const char * name
Definition: encode.c:561
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:1483
#define makeNode(_type_)
Definition: nodes.h:159
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
#define lfirst(lc)
Definition: pg_list.h:170
#define NIL
Definition: pg_list.h:66
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:507
#define RelationGetRelationName(relation)
Definition: rel.h:535
Relation p_target_relation
Definition: parse_node.h:193
TupleDesc rd_att
Definition: rel.h:111
int location
Definition: parsenodes.h:483
Node * val
Definition: parsenodes.h:482
List * indirection
Definition: parsenodes.h:481
char * name
Definition: parsenodes.h:480
#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:52
List * p_namespace
Definition: parse_node.h:187

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 RTE'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:2981
Oid MyDatabaseId
Definition: globals.c:89
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:422
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:150
#define lthird(l)
Definition: pg_list.h:186
#define linitial(l)
Definition: pg_list.h:176
#define lsecond(l)
Definition: pg_list.h:181
int location
Definition: parsenodes.h:258
List * fields
Definition: parsenodes.h:257
Definition: nodes.h:112
RangeTblEntry * p_rte
Definition: parse_node.h:272
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:221
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:222
#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:227
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:92

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

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

Referenced by ExpandRowReference(), and ParseComplexProjection().

◆ ExpandRowReference()

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

Definition at line 1406 of file parse_target.c.

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

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(), FieldSelect::resultcollid, FieldSelect::resulttype, FieldSelect::resulttypmod, 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  List *vars;
1371  ListCell *l;
1372 
1373  vars = expandNSItemVars(nsitem, sublevels_up, location, NULL);
1374 
1375  /*
1376  * Require read access to the table. This is normally redundant with
1377  * the markVarForSelectPriv calls below, but not if the table has zero
1378  * columns. We need not do anything if the nsitem is for a join: its
1379  * component tables will have been marked ACL_SELECT when they were
1380  * added to the rangetable. (This step changes things only for the
1381  * target relation of UPDATE/DELETE, which cannot be under a join.)
1382  */
1383  if (rte->rtekind == RTE_RELATION)
1384  rte->requiredPerms |= ACL_SELECT;
1385 
1386  /* Require read access to each column */
1387  foreach(l, vars)
1388  {
1389  Var *var = (Var *) lfirst(l);
1390 
1391  markVarForSelectPriv(pstate, var);
1392  }
1393 
1394  return vars;
1395  }
1396 }
void markVarForSelectPriv(ParseState *pstate, Var *var)
List * expandNSItemVars(ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
#define ACL_SELECT
Definition: parsenodes.h:84
AclMode requiredPerms
Definition: parsenodes.h:1178

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

Referenced by ExpandColumnRefStar(), and ExpandRowReference().

◆ FigureColname()

char* FigureColname ( Node node)

Definition at line 1680 of file parse_target.c.

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

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

References AEXPR_NULLIF, ALL_SUBLINK, ANY_SUBLINK, arg, ARRAY_SUBLINK, CTE_SUBLINK, EXISTS_SUBLINK, EXPR_SUBLINK, 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, TargetEntry::resname, ROWCOMPARE_SUBLINK, strVal, SubLink::subselect, and Query::targetList.

Referenced by FigureColname(), and FigureIndexColname().

◆ FigureIndexColname()

char* FigureIndexColname ( Node node)

Definition at line 1699 of file parse_target.c.

1700 {
1701  char *name = NULL;
1702 
1703  (void) FigureColnameInternal(node, &name);
1704  return name;
1705 }

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 }
CTECycleClause * cycle_clause
Definition: parsenodes.h:1542
CTESearchClause * search_clause
Definition: parsenodes.h:1541
Oid resorigtbl
Definition: primnodes.h:1560
AttrNumber resorigcol
Definition: primnodes.h:1561

References Alias::aliasname, attnum, CommonTableExpr::cycle_clause, elog(), RangeTblEntry::eref, ERROR, get_tle_by_resno(), GetCTEForRTE(), GetCTETargetList, GetRTEByRangeTablePosn(), InvalidAttrNumber, IsA, list_length(), RangeTblEntry::relid, TargetEntry::resjunk, TargetEntry::resorigcol, TargetEntry::resorigtbl, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, CommonTableExpr::search_clause, 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:588
@ COERCION_IMPLICIT
Definition: primnodes.h:566

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:430
int errhint(const char *fmt,...)
Definition: elog.c:1153
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:337
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:1243
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:126
@ COERCION_ASSIGNMENT
Definition: primnodes.h:567
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:194
ParseExprKind p_expr_kind
Definition: parse_node.h:197
bool p_is_insert
Definition: parse_node.h:195
int32 typeMod
Definition: primnodes.h:1442

References Assert(), attnumTypeId(), COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, SetToDefault::collation, ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_NONE, exprLocation(), exprType(), format_type_be(), IsA, linitial, list_head(), 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, SetToDefault::typeId, and SetToDefault::typeMod.

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:210
#define for_each_cell(cell, lst, initcell)
Definition: pg_list.h:436
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:341
#define list_make1_int(x1)
Definition: pg_list.h:225
int32 typeMod
Definition: primnodes.h:1160
List * newvals
Definition: primnodes.h:979
Expr * arg
Definition: primnodes.h:978
Oid resulttype
Definition: primnodes.h:981
List * fieldnums
Definition: primnodes.h:980

References FieldStore::arg, Assert(), attnum, COERCE_IMPLICIT_CAST, coerce_to_domain(), coerce_to_target_type(), COERCION_IMPLICIT, CaseTestExpr::collation, ereport, errcode(), errhint(), errmsg(), ERROR, exprType(), FieldStore::fieldnums, 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(), FieldStore::resulttype, strVal, transformAssignmentSubscripts(), CaseTestExpr::typeId, typeidTypeRelid(), and CaseTestExpr::typeMod.

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:247
void transformContainerType(Oid *containerType, int32 *containerTypmod)
Definition: parse_node.c:193
Expr * refassgnexpr
Definition: primnodes.h:554

References Assert(), COERCE_IMPLICIT_CAST, coerce_to_target_type(), ereport, errcode(), errmsg(), ERROR, exprType(), format_type_be(), get_typcollation(), NIL, parser_errposition(), SubscriptingRef::refassgnexpr, SubscriptingRef::refrestype, SubscriptingRef::reftypmod, 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:199

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:1556

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

Referenced by transformUpdateTargetList().