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, bool isSlice, 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 1011 of file parse_target.c.

1012 {
1013  *attrnos = NIL;
1014 
1015  if (cols == NIL)
1016  {
1017  /*
1018  * Generate default column list for INSERT.
1019  */
1020  int numcol = RelationGetNumberOfAttributes(pstate->p_target_relation);
1021 
1022  int i;
1023 
1024  for (i = 0; i < numcol; i++)
1025  {
1026  ResTarget *col;
1027  Form_pg_attribute attr;
1028 
1029  attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
1030 
1031  if (attr->attisdropped)
1032  continue;
1033 
1034  col = makeNode(ResTarget);
1035  col->name = pstrdup(NameStr(attr->attname));
1036  col->indirection = NIL;
1037  col->val = NULL;
1038  col->location = -1;
1039  cols = lappend(cols, col);
1040  *attrnos = lappend_int(*attrnos, i + 1);
1041  }
1042  }
1043  else
1044  {
1045  /*
1046  * Do initial validation of user-supplied INSERT column list.
1047  */
1048  Bitmapset *wholecols = NULL;
1049  Bitmapset *partialcols = NULL;
1050  ListCell *tl;
1051 
1052  foreach(tl, cols)
1053  {
1054  ResTarget *col = (ResTarget *) lfirst(tl);
1055  char *name = col->name;
1056  int attrno;
1057 
1058  /* Lookup column name, ereport on failure */
1059  attrno = attnameAttNum(pstate->p_target_relation, name, false);
1060  if (attrno == InvalidAttrNumber)
1061  ereport(ERROR,
1062  (errcode(ERRCODE_UNDEFINED_COLUMN),
1063  errmsg("column \"%s\" of relation \"%s\" does not exist",
1064  name,
1066  parser_errposition(pstate, col->location)));
1067 
1068  /*
1069  * Check for duplicates, but only of whole columns --- we allow
1070  * INSERT INTO foo (col.subcol1, col.subcol2)
1071  */
1072  if (col->indirection == NIL)
1073  {
1074  /* whole column; must not have any other assignment */
1075  if (bms_is_member(attrno, wholecols) ||
1076  bms_is_member(attrno, partialcols))
1077  ereport(ERROR,
1078  (errcode(ERRCODE_DUPLICATE_COLUMN),
1079  errmsg("column \"%s\" specified more than once",
1080  name),
1081  parser_errposition(pstate, col->location)));
1082  wholecols = bms_add_member(wholecols, attrno);
1083  }
1084  else
1085  {
1086  /* partial column; must not have any whole assignment */
1087  if (bms_is_member(attrno, wholecols))
1088  ereport(ERROR,
1089  (errcode(ERRCODE_DUPLICATE_COLUMN),
1090  errmsg("column \"%s\" specified more than once",
1091  name),
1092  parser_errposition(pstate, col->location)));
1093  partialcols = bms_add_member(partialcols, attrno);
1094  }
1095 
1096  *attrnos = lappend_int(*attrnos, attrno);
1097  }
1098  }
1099 
1100  return cols;
1101 }
#define InvalidAttrNumber
Definition: attnum.h:23
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:738
#define NameStr(name)
Definition: c.h:681
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
const char * name
Definition: encode.c:561
int i
Definition: isn.c:73
List * lappend(List *list, void *datum)
Definition: list.c:336
List * lappend_int(List *list, int datum)
Definition: list.c:354
char * pstrdup(const char *in)
Definition: mcxt.c:1305
#define makeNode(_type_)
Definition: nodes.h:621
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:169
#define NIL
Definition: pg_list.h:65
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:495
#define RelationGetRelationName(relation)
Definition: rel.h:523
Relation p_target_relation
Definition: parse_node.h:193
TupleDesc rd_att
Definition: rel.h:110
int location
Definition: parsenodes.h:474
Node * val
Definition: parsenodes.h:473
List * indirection
Definition: parsenodes.h:472
char * name
Definition: parsenodes.h:471
#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 1289 of file parse_target.c.

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

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

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

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

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

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

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

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

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

1691 {
1692  char *name = NULL;
1693 
1694  (void) FigureColnameInternal(node, &name);
1695  if (name != NULL)
1696  return name;
1697  /* default result if we can't guess anything */
1698  return "?column?";
1699 }
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 1729 of file parse_target.c.

1730 {
1731  int strength = 0;
1732 
1733  if (node == NULL)
1734  return strength;
1735 
1736  switch (nodeTag(node))
1737  {
1738  case T_ColumnRef:
1739  {
1740  char *fname = NULL;
1741  ListCell *l;
1742 
1743  /* find last field name, if any, ignoring "*" */
1744  foreach(l, ((ColumnRef *) node)->fields)
1745  {
1746  Node *i = lfirst(l);
1747 
1748  if (IsA(i, String))
1749  fname = strVal(i);
1750  }
1751  if (fname)
1752  {
1753  *name = fname;
1754  return 2;
1755  }
1756  }
1757  break;
1758  case T_A_Indirection:
1759  {
1760  A_Indirection *ind = (A_Indirection *) node;
1761  char *fname = NULL;
1762  ListCell *l;
1763 
1764  /* find last field name, if any, ignoring "*" and subscripts */
1765  foreach(l, ind->indirection)
1766  {
1767  Node *i = lfirst(l);
1768 
1769  if (IsA(i, String))
1770  fname = strVal(i);
1771  }
1772  if (fname)
1773  {
1774  *name = fname;
1775  return 2;
1776  }
1777  return FigureColnameInternal(ind->arg, name);
1778  }
1779  break;
1780  case T_FuncCall:
1781  *name = strVal(llast(((FuncCall *) node)->funcname));
1782  return 2;
1783  case T_A_Expr:
1784  if (((A_Expr *) node)->kind == AEXPR_NULLIF)
1785  {
1786  /* make nullif() act like a regular function */
1787  *name = "nullif";
1788  return 2;
1789  }
1790  break;
1791  case T_TypeCast:
1792  strength = FigureColnameInternal(((TypeCast *) node)->arg,
1793  name);
1794  if (strength <= 1)
1795  {
1796  if (((TypeCast *) node)->typeName != NULL)
1797  {
1798  *name = strVal(llast(((TypeCast *) node)->typeName->names));
1799  return 1;
1800  }
1801  }
1802  break;
1803  case T_CollateClause:
1804  return FigureColnameInternal(((CollateClause *) node)->arg, name);
1805  case T_GroupingFunc:
1806  /* make GROUPING() act like a regular function */
1807  *name = "grouping";
1808  return 2;
1809  case T_SubLink:
1810  switch (((SubLink *) node)->subLinkType)
1811  {
1812  case EXISTS_SUBLINK:
1813  *name = "exists";
1814  return 2;
1815  case ARRAY_SUBLINK:
1816  *name = "array";
1817  return 2;
1818  case EXPR_SUBLINK:
1819  {
1820  /* Get column name of the subquery's single target */
1821  SubLink *sublink = (SubLink *) node;
1822  Query *query = (Query *) sublink->subselect;
1823 
1824  /*
1825  * The subquery has probably already been transformed,
1826  * but let's be careful and check that. (The reason
1827  * we can see a transformed subquery here is that
1828  * transformSubLink is lazy and modifies the SubLink
1829  * node in-place.)
1830  */
1831  if (IsA(query, Query))
1832  {
1833  TargetEntry *te = (TargetEntry *) linitial(query->targetList);
1834 
1835  if (te->resname)
1836  {
1837  *name = te->resname;
1838  return 2;
1839  }
1840  }
1841  }
1842  break;
1843  /* As with other operator-like nodes, these have no names */
1844  case MULTIEXPR_SUBLINK:
1845  case ALL_SUBLINK:
1846  case ANY_SUBLINK:
1847  case ROWCOMPARE_SUBLINK:
1848  case CTE_SUBLINK:
1849  break;
1850  }
1851  break;
1852  case T_CaseExpr:
1853  strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult,
1854  name);
1855  if (strength <= 1)
1856  {
1857  *name = "case";
1858  return 1;
1859  }
1860  break;
1861  case T_A_ArrayExpr:
1862  /* make ARRAY[] act like a function */
1863  *name = "array";
1864  return 2;
1865  case T_RowExpr:
1866  /* make ROW() act like a function */
1867  *name = "row";
1868  return 2;
1869  case T_CoalesceExpr:
1870  /* make coalesce() act like a regular function */
1871  *name = "coalesce";
1872  return 2;
1873  case T_MinMaxExpr:
1874  /* make greatest/least act like a regular function */
1875  switch (((MinMaxExpr *) node)->op)
1876  {
1877  case IS_GREATEST:
1878  *name = "greatest";
1879  return 2;
1880  case IS_LEAST:
1881  *name = "least";
1882  return 2;
1883  }
1884  break;
1885  case T_SQLValueFunction:
1886  /* make these act like a function or variable */
1887  switch (((SQLValueFunction *) node)->op)
1888  {
1889  case SVFOP_CURRENT_DATE:
1890  *name = "current_date";
1891  return 2;
1892  case SVFOP_CURRENT_TIME:
1893  case SVFOP_CURRENT_TIME_N:
1894  *name = "current_time";
1895  return 2;
1898  *name = "current_timestamp";
1899  return 2;
1900  case SVFOP_LOCALTIME:
1901  case SVFOP_LOCALTIME_N:
1902  *name = "localtime";
1903  return 2;
1904  case SVFOP_LOCALTIMESTAMP:
1906  *name = "localtimestamp";
1907  return 2;
1908  case SVFOP_CURRENT_ROLE:
1909  *name = "current_role";
1910  return 2;
1911  case SVFOP_CURRENT_USER:
1912  *name = "current_user";
1913  return 2;
1914  case SVFOP_USER:
1915  *name = "user";
1916  return 2;
1917  case SVFOP_SESSION_USER:
1918  *name = "session_user";
1919  return 2;
1920  case SVFOP_CURRENT_CATALOG:
1921  *name = "current_catalog";
1922  return 2;
1923  case SVFOP_CURRENT_SCHEMA:
1924  *name = "current_schema";
1925  return 2;
1926  }
1927  break;
1928  case T_XmlExpr:
1929  /* make SQL/XML functions act like a regular function */
1930  switch (((XmlExpr *) node)->op)
1931  {
1932  case IS_XMLCONCAT:
1933  *name = "xmlconcat";
1934  return 2;
1935  case IS_XMLELEMENT:
1936  *name = "xmlelement";
1937  return 2;
1938  case IS_XMLFOREST:
1939  *name = "xmlforest";
1940  return 2;
1941  case IS_XMLPARSE:
1942  *name = "xmlparse";
1943  return 2;
1944  case IS_XMLPI:
1945  *name = "xmlpi";
1946  return 2;
1947  case IS_XMLROOT:
1948  *name = "xmlroot";
1949  return 2;
1950  case IS_XMLSERIALIZE:
1951  *name = "xmlserialize";
1952  return 2;
1953  case IS_DOCUMENT:
1954  /* nothing */
1955  break;
1956  }
1957  break;
1958  case T_XmlSerialize:
1959  *name = "xmlserialize";
1960  return 2;
1961  case T_JsonParseExpr:
1962  *name = "json";
1963  return 2;
1964  case T_JsonScalarExpr:
1965  *name = "json_scalar";
1966  return 2;
1967  case T_JsonSerializeExpr:
1968  *name = "json_serialize";
1969  return 2;
1971  *name = "json_object";
1972  return 2;
1975  *name = "json_array";
1976  return 2;
1977  case T_JsonObjectAgg:
1978  *name = "json_objectagg";
1979  return 2;
1980  case T_JsonArrayAgg:
1981  *name = "json_arrayagg";
1982  return 2;
1983  case T_JsonFuncExpr:
1984  /* make SQL/JSON functions act like a regular function */
1985  switch (((JsonFuncExpr *) node)->op)
1986  {
1987  case JSON_QUERY_OP:
1988  *name = "json_query";
1989  return 2;
1990  case JSON_VALUE_OP:
1991  *name = "json_value";
1992  return 2;
1993  case JSON_EXISTS_OP:
1994  *name = "json_exists";
1995  return 2;
1996  case JSON_TABLE_OP:
1997  *name = "json_table";
1998  return 2;
1999  }
2000  break;
2001  default:
2002  break;
2003  }
2004 
2005  return strength;
2006 }
#define nodeTag(nodeptr)
Definition: nodes.h:578
@ T_CoalesceExpr
Definition: nodes.h:187
@ T_JsonParseExpr
Definition: nodes.h:208
@ T_JsonSerializeExpr
Definition: nodes.h:210
@ T_XmlSerialize
Definition: nodes.h:493
@ T_SubLink
Definition: nodes.h:171
@ T_JsonArrayConstructor
Definition: nodes.h:512
@ T_CaseExpr
Definition: nodes.h:181
@ T_XmlExpr
Definition: nodes.h:190
@ T_JsonFuncExpr
Definition: nodes.h:517
@ T_JsonObjectConstructor
Definition: nodes.h:511
@ T_JsonScalarExpr
Definition: nodes.h:209
@ T_A_ArrayExpr
Definition: nodes.h:461
@ T_RowExpr
Definition: nodes.h:185
@ T_FuncCall
Definition: nodes.h:457
@ T_MinMaxExpr
Definition: nodes.h:188
@ T_A_Expr
Definition: nodes.h:453
@ T_A_Indirection
Definition: nodes.h:460
@ T_JsonArrayQueryConstructor
Definition: nodes.h:513
@ T_SQLValueFunction
Definition: nodes.h:189
@ T_JsonArrayAgg
Definition: nodes.h:516
@ T_JsonObjectAgg
Definition: nodes.h:515
@ T_GroupingFunc
Definition: nodes.h:161
@ T_CollateClause
Definition: nodes.h:465
@ T_ColumnRef
Definition: nodes.h:454
@ T_TypeCast
Definition: nodes.h:464
@ AEXPR_NULLIF
Definition: parsenodes.h:276
void * arg
#define llast(l)
Definition: pg_list.h:194
@ ARRAY_SUBLINK
Definition: primnodes.h:698
@ ANY_SUBLINK
Definition: primnodes.h:694
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:697
@ CTE_SUBLINK
Definition: primnodes.h:699
@ EXPR_SUBLINK
Definition: primnodes.h:696
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:695
@ ALL_SUBLINK
Definition: primnodes.h:693
@ EXISTS_SUBLINK
Definition: primnodes.h:692
@ IS_LEAST
Definition: primnodes.h:1149
@ IS_GREATEST
Definition: primnodes.h:1148
@ SVFOP_CURRENT_CATALOG
Definition: primnodes.h:1189
@ SVFOP_LOCALTIME_N
Definition: primnodes.h:1182
@ SVFOP_CURRENT_TIMESTAMP
Definition: primnodes.h:1179
@ SVFOP_LOCALTIME
Definition: primnodes.h:1181
@ SVFOP_CURRENT_TIMESTAMP_N
Definition: primnodes.h:1180
@ SVFOP_CURRENT_ROLE
Definition: primnodes.h:1185
@ SVFOP_USER
Definition: primnodes.h:1187
@ SVFOP_CURRENT_SCHEMA
Definition: primnodes.h:1190
@ SVFOP_LOCALTIMESTAMP_N
Definition: primnodes.h:1184
@ SVFOP_CURRENT_DATE
Definition: primnodes.h:1176
@ SVFOP_CURRENT_TIME_N
Definition: primnodes.h:1178
@ SVFOP_CURRENT_TIME
Definition: primnodes.h:1177
@ SVFOP_LOCALTIMESTAMP
Definition: primnodes.h:1183
@ SVFOP_CURRENT_USER
Definition: primnodes.h:1186
@ SVFOP_SESSION_USER
Definition: primnodes.h:1188
@ IS_DOCUMENT
Definition: primnodes.h:1222
@ IS_XMLFOREST
Definition: primnodes.h:1217
@ IS_XMLCONCAT
Definition: primnodes.h:1215
@ IS_XMLPI
Definition: primnodes.h:1219
@ IS_XMLPARSE
Definition: primnodes.h:1218
@ IS_XMLSERIALIZE
Definition: primnodes.h:1221
@ IS_XMLROOT
Definition: primnodes.h:1220
@ IS_XMLELEMENT
Definition: primnodes.h:1216
@ JSON_QUERY_OP
Definition: primnodes.h:1252
@ JSON_TABLE_OP
Definition: primnodes.h:1254
@ JSON_EXISTS_OP
Definition: primnodes.h:1253
@ JSON_VALUE_OP
Definition: primnodes.h:1251
Definition: value.h:58
char * resname
Definition: primnodes.h:1718

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, JSON_EXISTS_OP, JSON_QUERY_OP, JSON_TABLE_OP, JSON_VALUE_OP, lfirst, linitial, llast, MULTIEXPR_SUBLINK, name, nodeTag, TargetEntry::resname, ROWCOMPARE_SUBLINK, strVal, SubLink::subselect, SVFOP_CURRENT_CATALOG, SVFOP_CURRENT_DATE, SVFOP_CURRENT_ROLE, SVFOP_CURRENT_SCHEMA, SVFOP_CURRENT_TIME, SVFOP_CURRENT_TIME_N, SVFOP_CURRENT_TIMESTAMP, SVFOP_CURRENT_TIMESTAMP_N, SVFOP_CURRENT_USER, SVFOP_LOCALTIME, SVFOP_LOCALTIME_N, SVFOP_LOCALTIMESTAMP, SVFOP_LOCALTIMESTAMP_N, SVFOP_SESSION_USER, SVFOP_USER, T_A_ArrayExpr, T_A_Expr, T_A_Indirection, T_CaseExpr, T_CoalesceExpr, T_CollateClause, T_ColumnRef, T_FuncCall, T_GroupingFunc, T_JsonArrayAgg, T_JsonArrayConstructor, T_JsonArrayQueryConstructor, T_JsonFuncExpr, T_JsonObjectAgg, T_JsonObjectConstructor, T_JsonParseExpr, T_JsonScalarExpr, T_JsonSerializeExpr, T_MinMaxExpr, T_RowExpr, T_SQLValueFunction, T_SubLink, T_TypeCast, T_XmlExpr, T_XmlSerialize, and Query::targetList.

Referenced by FigureColname(), and FigureIndexColname().

◆ FigureIndexColname()

char* FigureIndexColname ( Node node)

Definition at line 1709 of file parse_target.c.

1710 {
1711  char *name = NULL;
1712 
1713  (void) FigureColnameInternal(node, &name);
1714  return name;
1715 }

References FigureColnameInternal(), and name.

Referenced by transformIndexStmt().

◆ markTargetListOrigin()

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

Definition at line 345 of file parse_target.c.

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

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

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

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

Referenced by transformReturningList(), and transformSelectStmt().

◆ resolveTargetListUnknowns()

void resolveTargetListUnknowns ( ParseState pstate,
List targetlist 
)

Definition at line 290 of file parse_target.c.

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

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

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

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

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

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,
bool  isSlice,
List indirection,
ListCell next_indirection,
Node rhs,
CoercionContext  ccontext,
int  location 
)
static

Definition at line 903 of file parse_target.c.

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

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

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

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

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

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

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

Referenced by transformUpdateTargetList().