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:460
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:753
#define NameStr(name)
Definition: c.h:735
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
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:1644
#define makeNode(_type_)
Definition: nodes.h:176
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:510
#define RelationGetRelationName(relation)
Definition: rel.h:538
Relation p_target_relation
Definition: parse_node.h:206
TupleDesc rd_att
Definition: rel.h:112
int location
Definition: parsenodes.h:517
Node * val
Definition: parsenodes.h:516
List * indirection
Definition: parsenodes.h:515
char * name
Definition: parsenodes.h:514
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
const char * name

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

Referenced by transformInsertStmt(), and transformMergeStmt().

◆ ExpandAllTables()

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

Definition at line 1280 of file parse_target.c.

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

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

Referenced by ExpandColumnRefStar().

◆ ExpandColumnRefStar()

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

Definition at line 1107 of file parse_target.c.

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

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

Referenced by transformExpressionList(), and transformTargetList().

◆ ExpandIndirectionStar()

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

Definition at line 1332 of file parse_target.c.

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

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

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

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

Referenced by ExpandRowReference(), and ParseComplexProjection().

◆ ExpandRowReference()

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

Definition at line 1410 of file parse_target.c.

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

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

Referenced by ExpandColumnRefStar(), and ExpandIndirectionStar().

◆ ExpandSingleTable()

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

Definition at line 1359 of file parse_target.c.

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

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

Referenced by ExpandColumnRefStar(), and ExpandRowReference().

◆ FigureColname()

char* FigureColname ( Node node)

Definition at line 1691 of file parse_target.c.

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

1731 {
1732  int strength = 0;
1733 
1734  if (node == NULL)
1735  return strength;
1736 
1737  switch (nodeTag(node))
1738  {
1739  case T_ColumnRef:
1740  {
1741  char *fname = NULL;
1742  ListCell *l;
1743 
1744  /* find last field name, if any, ignoring "*" */
1745  foreach(l, ((ColumnRef *) node)->fields)
1746  {
1747  Node *i = lfirst(l);
1748 
1749  if (IsA(i, String))
1750  fname = strVal(i);
1751  }
1752  if (fname)
1753  {
1754  *name = fname;
1755  return 2;
1756  }
1757  }
1758  break;
1759  case T_A_Indirection:
1760  {
1761  A_Indirection *ind = (A_Indirection *) node;
1762  char *fname = NULL;
1763  ListCell *l;
1764 
1765  /* find last field name, if any, ignoring "*" and subscripts */
1766  foreach(l, ind->indirection)
1767  {
1768  Node *i = lfirst(l);
1769 
1770  if (IsA(i, String))
1771  fname = strVal(i);
1772  }
1773  if (fname)
1774  {
1775  *name = fname;
1776  return 2;
1777  }
1778  return FigureColnameInternal(ind->arg, name);
1779  }
1780  break;
1781  case T_FuncCall:
1782  *name = strVal(llast(((FuncCall *) node)->funcname));
1783  return 2;
1784  case T_A_Expr:
1785  if (((A_Expr *) node)->kind == AEXPR_NULLIF)
1786  {
1787  /* make nullif() act like a regular function */
1788  *name = "nullif";
1789  return 2;
1790  }
1791  break;
1792  case T_TypeCast:
1793  strength = FigureColnameInternal(((TypeCast *) node)->arg,
1794  name);
1795  if (strength <= 1)
1796  {
1797  if (((TypeCast *) node)->typeName != NULL)
1798  {
1799  *name = strVal(llast(((TypeCast *) node)->typeName->names));
1800  return 1;
1801  }
1802  }
1803  break;
1804  case T_CollateClause:
1805  return FigureColnameInternal(((CollateClause *) node)->arg, name);
1806  case T_GroupingFunc:
1807  /* make GROUPING() act like a regular function */
1808  *name = "grouping";
1809  return 2;
1810  case T_SubLink:
1811  switch (((SubLink *) node)->subLinkType)
1812  {
1813  case EXISTS_SUBLINK:
1814  *name = "exists";
1815  return 2;
1816  case ARRAY_SUBLINK:
1817  *name = "array";
1818  return 2;
1819  case EXPR_SUBLINK:
1820  {
1821  /* Get column name of the subquery's single target */
1822  SubLink *sublink = (SubLink *) node;
1823  Query *query = (Query *) sublink->subselect;
1824 
1825  /*
1826  * The subquery has probably already been transformed,
1827  * but let's be careful and check that. (The reason
1828  * we can see a transformed subquery here is that
1829  * transformSubLink is lazy and modifies the SubLink
1830  * node in-place.)
1831  */
1832  if (IsA(query, Query))
1833  {
1834  TargetEntry *te = (TargetEntry *) linitial(query->targetList);
1835 
1836  if (te->resname)
1837  {
1838  *name = te->resname;
1839  return 2;
1840  }
1841  }
1842  }
1843  break;
1844  /* As with other operator-like nodes, these have no names */
1845  case MULTIEXPR_SUBLINK:
1846  case ALL_SUBLINK:
1847  case ANY_SUBLINK:
1848  case ROWCOMPARE_SUBLINK:
1849  case CTE_SUBLINK:
1850  break;
1851  }
1852  break;
1853  case T_CaseExpr:
1854  strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult,
1855  name);
1856  if (strength <= 1)
1857  {
1858  *name = "case";
1859  return 1;
1860  }
1861  break;
1862  case T_A_ArrayExpr:
1863  /* make ARRAY[] act like a function */
1864  *name = "array";
1865  return 2;
1866  case T_RowExpr:
1867  /* make ROW() act like a function */
1868  *name = "row";
1869  return 2;
1870  case T_CoalesceExpr:
1871  /* make coalesce() act like a regular function */
1872  *name = "coalesce";
1873  return 2;
1874  case T_MinMaxExpr:
1875  /* make greatest/least act like a regular function */
1876  switch (((MinMaxExpr *) node)->op)
1877  {
1878  case IS_GREATEST:
1879  *name = "greatest";
1880  return 2;
1881  case IS_LEAST:
1882  *name = "least";
1883  return 2;
1884  }
1885  break;
1886  case T_SQLValueFunction:
1887  /* make these act like a function or variable */
1888  switch (((SQLValueFunction *) node)->op)
1889  {
1890  case SVFOP_CURRENT_DATE:
1891  *name = "current_date";
1892  return 2;
1893  case SVFOP_CURRENT_TIME:
1894  case SVFOP_CURRENT_TIME_N:
1895  *name = "current_time";
1896  return 2;
1899  *name = "current_timestamp";
1900  return 2;
1901  case SVFOP_LOCALTIME:
1902  case SVFOP_LOCALTIME_N:
1903  *name = "localtime";
1904  return 2;
1905  case SVFOP_LOCALTIMESTAMP:
1907  *name = "localtimestamp";
1908  return 2;
1909  case SVFOP_CURRENT_ROLE:
1910  *name = "current_role";
1911  return 2;
1912  case SVFOP_CURRENT_USER:
1913  *name = "current_user";
1914  return 2;
1915  case SVFOP_USER:
1916  *name = "user";
1917  return 2;
1918  case SVFOP_SESSION_USER:
1919  *name = "session_user";
1920  return 2;
1921  case SVFOP_CURRENT_CATALOG:
1922  *name = "current_catalog";
1923  return 2;
1924  case SVFOP_CURRENT_SCHEMA:
1925  *name = "current_schema";
1926  return 2;
1927  }
1928  break;
1929  case T_XmlExpr:
1930  /* make SQL/XML functions act like a regular function */
1931  switch (((XmlExpr *) node)->op)
1932  {
1933  case IS_XMLCONCAT:
1934  *name = "xmlconcat";
1935  return 2;
1936  case IS_XMLELEMENT:
1937  *name = "xmlelement";
1938  return 2;
1939  case IS_XMLFOREST:
1940  *name = "xmlforest";
1941  return 2;
1942  case IS_XMLPARSE:
1943  *name = "xmlparse";
1944  return 2;
1945  case IS_XMLPI:
1946  *name = "xmlpi";
1947  return 2;
1948  case IS_XMLROOT:
1949  *name = "xmlroot";
1950  return 2;
1951  case IS_XMLSERIALIZE:
1952  *name = "xmlserialize";
1953  return 2;
1954  case IS_DOCUMENT:
1955  /* nothing */
1956  break;
1957  }
1958  break;
1959  case T_XmlSerialize:
1960  /* make XMLSERIALIZE act like a regular function */
1961  *name = "xmlserialize";
1962  return 2;
1963  case T_JsonParseExpr:
1964  /* make JSON act like a regular function */
1965  *name = "json";
1966  return 2;
1967  case T_JsonScalarExpr:
1968  /* make JSON_SCALAR act like a regular function */
1969  *name = "json_scalar";
1970  return 2;
1971  case T_JsonSerializeExpr:
1972  /* make JSON_SERIALIZE act like a regular function */
1973  *name = "json_serialize";
1974  return 2;
1975  case T_JsonObjectConstructor:
1976  /* make JSON_OBJECT act like a regular function */
1977  *name = "json_object";
1978  return 2;
1979  case T_JsonArrayConstructor:
1980  case T_JsonArrayQueryConstructor:
1981  /* make JSON_ARRAY act like a regular function */
1982  *name = "json_array";
1983  return 2;
1984  case T_JsonObjectAgg:
1985  /* make JSON_OBJECTAGG act like a regular function */
1986  *name = "json_objectagg";
1987  return 2;
1988  case T_JsonArrayAgg:
1989  /* make JSON_ARRAYAGG act like a regular function */
1990  *name = "json_arrayagg";
1991  return 2;
1992  default:
1993  break;
1994  }
1995 
1996  return strength;
1997 }
#define funcname
Definition: indent_codes.h:69
#define nodeTag(nodeptr)
Definition: nodes.h:133
@ AEXPR_NULLIF
Definition: parsenodes.h:315
void * arg
#define llast(l)
Definition: pg_list.h:198
@ ARRAY_SUBLINK
Definition: primnodes.h:930
@ ANY_SUBLINK
Definition: primnodes.h:926
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:929
@ CTE_SUBLINK
Definition: primnodes.h:931
@ EXPR_SUBLINK
Definition: primnodes.h:928
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:927
@ ALL_SUBLINK
Definition: primnodes.h:925
@ EXISTS_SUBLINK
Definition: primnodes.h:924
@ IS_LEAST
Definition: primnodes.h:1430
@ IS_GREATEST
Definition: primnodes.h:1429
@ SVFOP_CURRENT_CATALOG
Definition: primnodes.h:1476
@ SVFOP_LOCALTIME_N
Definition: primnodes.h:1469
@ SVFOP_CURRENT_TIMESTAMP
Definition: primnodes.h:1466
@ SVFOP_LOCALTIME
Definition: primnodes.h:1468
@ SVFOP_CURRENT_TIMESTAMP_N
Definition: primnodes.h:1467
@ SVFOP_CURRENT_ROLE
Definition: primnodes.h:1472
@ SVFOP_USER
Definition: primnodes.h:1474
@ SVFOP_CURRENT_SCHEMA
Definition: primnodes.h:1477
@ SVFOP_LOCALTIMESTAMP_N
Definition: primnodes.h:1471
@ SVFOP_CURRENT_DATE
Definition: primnodes.h:1463
@ SVFOP_CURRENT_TIME_N
Definition: primnodes.h:1465
@ SVFOP_CURRENT_TIME
Definition: primnodes.h:1464
@ SVFOP_LOCALTIMESTAMP
Definition: primnodes.h:1470
@ SVFOP_CURRENT_USER
Definition: primnodes.h:1473
@ SVFOP_SESSION_USER
Definition: primnodes.h:1475
@ IS_DOCUMENT
Definition: primnodes.h:1514
@ IS_XMLFOREST
Definition: primnodes.h:1509
@ IS_XMLCONCAT
Definition: primnodes.h:1507
@ IS_XMLPI
Definition: primnodes.h:1511
@ IS_XMLPARSE
Definition: primnodes.h:1510
@ IS_XMLSERIALIZE
Definition: primnodes.h:1513
@ IS_XMLROOT
Definition: primnodes.h:1512
@ IS_XMLELEMENT
Definition: primnodes.h:1508
Definition: value.h:64

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

Referenced by FigureColname(), and FigureIndexColname().

◆ FigureIndexColname()

char* FigureIndexColname ( Node node)

Definition at line 1710 of file parse_target.c.

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

References FigureColnameInternal(), and name.

Referenced by transformIndexStmt().

◆ markTargetListOrigin()

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

Definition at line 344 of file parse_target.c.

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

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

Referenced by markTargetListOrigins().

◆ markTargetListOrigins()

void markTargetListOrigins ( ParseState pstate,
List targetlist 
)

Definition at line 319 of file parse_target.c.

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

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

Referenced by transformReturningList(), and transformSelectStmt().

◆ resolveTargetListUnknowns()

void resolveTargetListUnknowns ( ParseState pstate,
List targetlist 
)

Definition at line 289 of file parse_target.c.

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

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

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

◆ transformAssignedExpr()

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

Definition at line 453 of file parse_target.c.

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

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

Referenced by transformInsertRow(), and updateTargetListEntry().

◆ transformAssignmentIndirection()

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

Definition at line 684 of file parse_target.c.

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

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

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

◆ transformAssignmentSubscripts()

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

Definition at line 895 of file parse_target.c.

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

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

Referenced by transformAssignmentIndirection().

◆ transformExpressionList()

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

Definition at line 221 of file parse_target.c.

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

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

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

◆ transformTargetEntry()

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

Definition at line 76 of file parse_target.c.

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

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

Referenced by findTargetlistEntrySQL99(), and transformTargetList().

◆ transformTargetList()

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

Definition at line 122 of file parse_target.c.

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

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

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

◆ updateTargetListEntry()

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

Definition at line 620 of file parse_target.c.

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

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

Referenced by transformUpdateTargetList().