PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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/parsetree.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 "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 NodetransformAssignmentIndirection (ParseState *pstate, Node *basenode, const char *targetName, bool targetIsArray, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, ListCell *indirection, Node *rhs, int location)
 
static NodetransformAssignmentSubscripts (ParseState *pstate, Node *basenode, const char *targetName, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, List *subscripts, bool isSlice, ListCell *next_indirection, Node *rhs, 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, RangeTblEntry *rte, 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, char *colname, int attrno, List *indirection, int location)
 
void updateTargetListEntry (ParseState *pstate, TargetEntry *tle, char *colname, int attrno, List *indirection, 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

List* checkInsertTargets ( ParseState pstate,
List cols,
List **  attrnos 
)

Definition at line 953 of file parse_target.c.

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

Referenced by transformInsertStmt().

954 {
955  *attrnos = NIL;
956 
957  if (cols == NIL)
958  {
959  /*
960  * Generate default column list for INSERT.
961  */
962  int numcol = pstate->p_target_relation->rd_rel->relnatts;
963  int i;
964 
965  for (i = 0; i < numcol; i++)
966  {
967  ResTarget *col;
968  Form_pg_attribute attr;
969 
970  attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
971 
972  if (attr->attisdropped)
973  continue;
974 
975  col = makeNode(ResTarget);
976  col->name = pstrdup(NameStr(attr->attname));
977  col->indirection = NIL;
978  col->val = NULL;
979  col->location = -1;
980  cols = lappend(cols, col);
981  *attrnos = lappend_int(*attrnos, i + 1);
982  }
983  }
984  else
985  {
986  /*
987  * Do initial validation of user-supplied INSERT column list.
988  */
989  Bitmapset *wholecols = NULL;
990  Bitmapset *partialcols = NULL;
991  ListCell *tl;
992 
993  foreach(tl, cols)
994  {
995  ResTarget *col = (ResTarget *) lfirst(tl);
996  char *name = col->name;
997  int attrno;
998 
999  /* Lookup column name, ereport on failure */
1000  attrno = attnameAttNum(pstate->p_target_relation, name, false);
1001  if (attrno == InvalidAttrNumber)
1002  ereport(ERROR,
1003  (errcode(ERRCODE_UNDEFINED_COLUMN),
1004  errmsg("column \"%s\" of relation \"%s\" does not exist",
1005  name,
1007  parser_errposition(pstate, col->location)));
1008 
1009  /*
1010  * Check for duplicates, but only of whole columns --- we allow
1011  * INSERT INTO foo (col.subcol1, col.subcol2)
1012  */
1013  if (col->indirection == NIL)
1014  {
1015  /* whole column; must not have any other assignment */
1016  if (bms_is_member(attrno, wholecols) ||
1017  bms_is_member(attrno, partialcols))
1018  ereport(ERROR,
1019  (errcode(ERRCODE_DUPLICATE_COLUMN),
1020  errmsg("column \"%s\" specified more than once",
1021  name),
1022  parser_errposition(pstate, col->location)));
1023  wholecols = bms_add_member(wholecols, attrno);
1024  }
1025  else
1026  {
1027  /* partial column; must not have any whole assignment */
1028  if (bms_is_member(attrno, wholecols))
1029  ereport(ERROR,
1030  (errcode(ERRCODE_DUPLICATE_COLUMN),
1031  errmsg("column \"%s\" specified more than once",
1032  name),
1033  parser_errposition(pstate, col->location)));
1034  partialcols = bms_add_member(partialcols, attrno);
1035  }
1036 
1037  *attrnos = lappend_int(*attrnos, attrno);
1038  }
1039  }
1040 
1041  return cols;
1042 }
List * indirection
Definition: parsenodes.h:440
#define NIL
Definition: pg_list.h:69
Node * val
Definition: parsenodes.h:441
char * name
Definition: parsenodes.h:439
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
char * pstrdup(const char *in)
Definition: mcxt.c:1077
int errcode(int sqlerrcode)
Definition: elog.c:575
Form_pg_class rd_rel
Definition: rel.h:114
int location
Definition: parsenodes.h:442
#define ERROR
Definition: elog.h:43
#define RelationGetRelationName(relation)
Definition: rel.h:436
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
TupleDesc rd_att
Definition: rel.h:115
#define makeNode(_type_)
Definition: nodes.h:557
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
const char * name
Definition: encode.c:521
#define InvalidAttrNumber
Definition: attnum.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation p_target_relation
Definition: parse_node.h:182
int i
#define NameStr(name)
Definition: c.h:499
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
static List * ExpandAllTables ( ParseState pstate,
int  location 
)
static

Definition at line 1227 of file parse_target.c.

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

Referenced by ExpandColumnRefStar().

1228 {
1229  List *target = NIL;
1230  bool found_table = false;
1231  ListCell *l;
1232 
1233  foreach(l, pstate->p_namespace)
1234  {
1235  ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(l);
1236  RangeTblEntry *rte = nsitem->p_rte;
1237 
1238  /* Ignore table-only items */
1239  if (!nsitem->p_cols_visible)
1240  continue;
1241  /* Should not have any lateral-only items when parsing targetlist */
1242  Assert(!nsitem->p_lateral_only);
1243  /* Remember we found a p_cols_visible item */
1244  found_table = true;
1245 
1246  target = list_concat(target,
1247  expandRelAttrs(pstate,
1248  rte,
1249  RTERangeTablePosn(pstate, rte,
1250  NULL),
1251  0,
1252  location));
1253  }
1254 
1255  /*
1256  * Check for "SELECT *;". We do it this way, rather than checking for
1257  * target == NIL, because we want to allow SELECT * FROM a zero_column
1258  * table.
1259  */
1260  if (!found_table)
1261  ereport(ERROR,
1262  (errcode(ERRCODE_SYNTAX_ERROR),
1263  errmsg("SELECT * with no tables specified is not valid"),
1264  parser_errposition(pstate, location)));
1265 
1266  return target;
1267 }
#define NIL
Definition: pg_list.h:69
int errcode(int sqlerrcode)
Definition: elog.c:575
List * list_concat(List *list1, List *list2)
Definition: list.c:321
RangeTblEntry * p_rte
Definition: parse_node.h:246
#define ERROR
Definition: elog.h:43
List * p_namespace
Definition: parse_node.h:176
List * expandRelAttrs(ParseState *pstate, RangeTblEntry *rte, int rtindex, int sublevels_up, int location)
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: pg_list.h:45
static List * ExpandColumnRefStar ( ParseState pstate,
ColumnRef cref,
bool  make_target_entry 
)
static

Definition at line 1057 of file parse_target.c.

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(), NULL, ParseState::p_post_columnref_hook, ParseState::p_pre_columnref_hook, parser_errposition(), refnameRangeTblEntry(), and strVal.

Referenced by transformExpressionList(), and transformTargetList().

1059 {
1060  List *fields = cref->fields;
1061  int numnames = list_length(fields);
1062 
1063  if (numnames == 1)
1064  {
1065  /*
1066  * Target item is a bare '*', expand all tables
1067  *
1068  * (e.g., SELECT * FROM emp, dept)
1069  *
1070  * Since the grammar only accepts bare '*' at top level of SELECT, we
1071  * need not handle the make_target_entry==false case here.
1072  */
1073  Assert(make_target_entry);
1074  return ExpandAllTables(pstate, cref->location);
1075  }
1076  else
1077  {
1078  /*
1079  * Target item is relation.*, expand that table
1080  *
1081  * (e.g., SELECT emp.*, dname FROM emp, dept)
1082  *
1083  * Note: this code is a lot like transformColumnRef; it's tempting to
1084  * call that instead and then replace the resulting whole-row Var with
1085  * a list of Vars. However, that would leave us with the RTE's
1086  * selectedCols bitmap showing the whole row as needing select
1087  * permission, as well as the individual columns. That would be
1088  * incorrect (since columns added later shouldn't need select
1089  * permissions). We could try to remove the whole-row permission bit
1090  * after the fact, but duplicating code is less messy.
1091  */
1092  char *nspname = NULL;
1093  char *relname = NULL;
1094  RangeTblEntry *rte = NULL;
1095  int levels_up;
1096  enum
1097  {
1098  CRSERR_NO_RTE,
1099  CRSERR_WRONG_DB,
1100  CRSERR_TOO_MANY
1101  } crserr = CRSERR_NO_RTE;
1102 
1103  /*
1104  * Give the PreParseColumnRefHook, if any, first shot. If it returns
1105  * non-null then we should use that expression.
1106  */
1107  if (pstate->p_pre_columnref_hook != NULL)
1108  {
1109  Node *node;
1110 
1111  node = (*pstate->p_pre_columnref_hook) (pstate, cref);
1112  if (node != NULL)
1113  return ExpandRowReference(pstate, node, make_target_entry);
1114  }
1115 
1116  switch (numnames)
1117  {
1118  case 2:
1119  relname = strVal(linitial(fields));
1120  rte = refnameRangeTblEntry(pstate, nspname, relname,
1121  cref->location,
1122  &levels_up);
1123  break;
1124  case 3:
1125  nspname = strVal(linitial(fields));
1126  relname = strVal(lsecond(fields));
1127  rte = refnameRangeTblEntry(pstate, nspname, relname,
1128  cref->location,
1129  &levels_up);
1130  break;
1131  case 4:
1132  {
1133  char *catname = strVal(linitial(fields));
1134 
1135  /*
1136  * We check the catalog name and then ignore it.
1137  */
1138  if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
1139  {
1140  crserr = CRSERR_WRONG_DB;
1141  break;
1142  }
1143  nspname = strVal(lsecond(fields));
1144  relname = strVal(lthird(fields));
1145  rte = refnameRangeTblEntry(pstate, nspname, relname,
1146  cref->location,
1147  &levels_up);
1148  break;
1149  }
1150  default:
1151  crserr = CRSERR_TOO_MANY;
1152  break;
1153  }
1154 
1155  /*
1156  * Now give the PostParseColumnRefHook, if any, a chance. We cheat a
1157  * bit by passing the RangeTblEntry, not a Var, as the planned
1158  * translation. (A single Var wouldn't be strictly correct anyway.
1159  * This convention allows hooks that really care to know what is
1160  * happening.)
1161  */
1162  if (pstate->p_post_columnref_hook != NULL)
1163  {
1164  Node *node;
1165 
1166  node = (*pstate->p_post_columnref_hook) (pstate, cref,
1167  (Node *) rte);
1168  if (node != NULL)
1169  {
1170  if (rte != NULL)
1171  ereport(ERROR,
1172  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1173  errmsg("column reference \"%s\" is ambiguous",
1174  NameListToString(cref->fields)),
1175  parser_errposition(pstate, cref->location)));
1176  return ExpandRowReference(pstate, node, make_target_entry);
1177  }
1178  }
1179 
1180  /*
1181  * Throw error if no translation found.
1182  */
1183  if (rte == NULL)
1184  {
1185  switch (crserr)
1186  {
1187  case CRSERR_NO_RTE:
1188  errorMissingRTE(pstate, makeRangeVar(nspname, relname,
1189  cref->location));
1190  break;
1191  case CRSERR_WRONG_DB:
1192  ereport(ERROR,
1193  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1194  errmsg("cross-database references are not implemented: %s",
1195  NameListToString(cref->fields)),
1196  parser_errposition(pstate, cref->location)));
1197  break;
1198  case CRSERR_TOO_MANY:
1199  ereport(ERROR,
1200  (errcode(ERRCODE_SYNTAX_ERROR),
1201  errmsg("improper qualified name (too many dotted names): %s",
1202  NameListToString(cref->fields)),
1203  parser_errposition(pstate, cref->location)));
1204  break;
1205  }
1206  }
1207 
1208  /*
1209  * OK, expand the RTE into fields.
1210  */
1211  return ExpandSingleTable(pstate, rte, cref->location, make_target_entry);
1212  }
1213 }
Definition: nodes.h:509
#define strVal(v)
Definition: value.h:54
static List * ExpandSingleTable(ParseState *pstate, RangeTblEntry *rte, int location, bool make_target_entry)
int errcode(int sqlerrcode)
Definition: elog.c:575
RangeTblEntry * refnameRangeTblEntry(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
#define lsecond(l)
Definition: pg_list.h:116
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:211
int location
Definition: parsenodes.h:235
static List * ExpandRowReference(ParseState *pstate, Node *expr, bool make_target_entry)
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2056
void errorMissingRTE(ParseState *pstate, RangeVar *relation)
#define ereport(elevel, rest)
Definition: elog.h:122
char * NameListToString(List *names)
Definition: namespace.c:3063
Oid MyDatabaseId
Definition: globals.c:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
static List * ExpandAllTables(ParseState *pstate, int location)
#define lthird(l)
Definition: pg_list.h:121
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:210
Definition: pg_list.h:45
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:419
List * fields
Definition: parsenodes.h:234
static List * ExpandIndirectionStar ( ParseState pstate,
A_Indirection ind,
bool  make_target_entry,
ParseExprKind  exprKind 
)
static

Definition at line 1281 of file parse_target.c.

References copyObject, ExpandRowReference(), A_Indirection::indirection, list_length(), list_truncate(), and transformExpr().

Referenced by transformExpressionList(), and transformTargetList().

1283 {
1284  Node *expr;
1285 
1286  /* Strip off the '*' to create a reference to the rowtype object */
1287  ind = copyObject(ind);
1289  list_length(ind->indirection) - 1);
1290 
1291  /* And transform that */
1292  expr = transformExpr(pstate, (Node *) ind, exprKind);
1293 
1294  /* Expand the rowtype expression into individual fields */
1295  return ExpandRowReference(pstate, expr, make_target_entry);
1296 }
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
List * list_truncate(List *list, int new_size)
Definition: list.c:350
Definition: nodes.h:509
static List * ExpandRowReference(ParseState *pstate, Node *expr, bool make_target_entry)
List * indirection
Definition: parsenodes.h:405
static int list_length(const List *l)
Definition: pg_list.h:89
#define copyObject(obj)
Definition: nodes.h:622
TupleDesc expandRecordVariable ( ParseState pstate,
Var var,
int  levelsup 
)

Definition at line 1456 of file parse_target.c.

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

Referenced by expandRecordVariable(), ExpandRowReference(), and ParseComplexProjection().

1457 {
1459  int netlevelsup;
1460  RangeTblEntry *rte;
1461  AttrNumber attnum;
1462  Node *expr;
1463 
1464  /* Check my caller didn't mess up */
1465  Assert(IsA(var, Var));
1466  Assert(var->vartype == RECORDOID);
1467 
1468  netlevelsup = var->varlevelsup + levelsup;
1469  rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
1470  attnum = var->varattno;
1471 
1472  if (attnum == InvalidAttrNumber)
1473  {
1474  /* Whole-row reference to an RTE, so expand the known fields */
1475  List *names,
1476  *vars;
1477  ListCell *lname,
1478  *lvar;
1479  int i;
1480 
1481  expandRTE(rte, var->varno, 0, var->location, false,
1482  &names, &vars);
1483 
1484  tupleDesc = CreateTemplateTupleDesc(list_length(vars), false);
1485  i = 1;
1486  forboth(lname, names, lvar, vars)
1487  {
1488  char *label = strVal(lfirst(lname));
1489  Node *varnode = (Node *) lfirst(lvar);
1490 
1491  TupleDescInitEntry(tupleDesc, i,
1492  label,
1493  exprType(varnode),
1494  exprTypmod(varnode),
1495  0);
1496  TupleDescInitEntryCollation(tupleDesc, i,
1497  exprCollation(varnode));
1498  i++;
1499  }
1500  Assert(lname == NULL && lvar == NULL); /* lists same length? */
1501 
1502  return tupleDesc;
1503  }
1504 
1505  expr = (Node *) var; /* default if we can't drill down */
1506 
1507  switch (rte->rtekind)
1508  {
1509  case RTE_RELATION:
1510  case RTE_VALUES:
1511  case RTE_NAMEDTUPLESTORE:
1512 
1513  /*
1514  * This case should not occur: a column of a table or values list
1515  * shouldn't have type RECORD. Fall through and fail (most
1516  * likely) at the bottom.
1517  */
1518  break;
1519  case RTE_SUBQUERY:
1520  {
1521  /* Subselect-in-FROM: examine sub-select's output expr */
1523  attnum);
1524 
1525  if (ste == NULL || ste->resjunk)
1526  elog(ERROR, "subquery %s does not have attribute %d",
1527  rte->eref->aliasname, attnum);
1528  expr = (Node *) ste->expr;
1529  if (IsA(expr, Var))
1530  {
1531  /*
1532  * Recurse into the sub-select to see what its Var refers
1533  * to. We have to build an additional level of ParseState
1534  * to keep in step with varlevelsup in the subselect.
1535  */
1536  ParseState mypstate;
1537 
1538  MemSet(&mypstate, 0, sizeof(mypstate));
1539  mypstate.parentParseState = pstate;
1540  mypstate.p_rtable = rte->subquery->rtable;
1541  /* don't bother filling the rest of the fake pstate */
1542 
1543  return expandRecordVariable(&mypstate, (Var *) expr, 0);
1544  }
1545  /* else fall through to inspect the expression */
1546  }
1547  break;
1548  case RTE_JOIN:
1549  /* Join RTE --- recursively inspect the alias variable */
1550  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
1551  expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1552  Assert(expr != NULL);
1553  /* We intentionally don't strip implicit coercions here */
1554  if (IsA(expr, Var))
1555  return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
1556  /* else fall through to inspect the expression */
1557  break;
1558  case RTE_FUNCTION:
1559 
1560  /*
1561  * We couldn't get here unless a function is declared with one of
1562  * its result columns as RECORD, which is not allowed.
1563  */
1564  break;
1565  case RTE_TABLEFUNC:
1566 
1567  /*
1568  * Table function cannot have columns with RECORD type.
1569  */
1570  break;
1571  case RTE_CTE:
1572  /* CTE reference: examine subquery's output expr */
1573  if (!rte->self_reference)
1574  {
1575  CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
1576  TargetEntry *ste;
1577 
1578  ste = get_tle_by_resno(GetCTETargetList(cte), attnum);
1579  if (ste == NULL || ste->resjunk)
1580  elog(ERROR, "subquery %s does not have attribute %d",
1581  rte->eref->aliasname, attnum);
1582  expr = (Node *) ste->expr;
1583  if (IsA(expr, Var))
1584  {
1585  /*
1586  * Recurse into the CTE to see what its Var refers to. We
1587  * have to build an additional level of ParseState to keep
1588  * in step with varlevelsup in the CTE; furthermore it
1589  * could be an outer CTE.
1590  */
1591  ParseState mypstate;
1592  Index levelsup;
1593 
1594  MemSet(&mypstate, 0, sizeof(mypstate));
1595  /* this loop must work, since GetCTEForRTE did */
1596  for (levelsup = 0;
1597  levelsup < rte->ctelevelsup + netlevelsup;
1598  levelsup++)
1599  pstate = pstate->parentParseState;
1600  mypstate.parentParseState = pstate;
1601  mypstate.p_rtable = ((Query *) cte->ctequery)->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  }
1610 
1611  /*
1612  * We now have an expression we can't expand any more, so see if
1613  * get_expr_result_type() can do anything with it. If not, pass to
1614  * lookup_rowtype_tupdesc() which will probably fail, but will give an
1615  * appropriate error message while failing.
1616  */
1617  if (get_expr_result_type(expr, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
1618  tupleDesc = lookup_rowtype_tupdesc_copy(exprType(expr),
1619  exprTypmod(expr));
1620 
1621  return tupleDesc;
1622 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
List * joinaliasvars
Definition: parsenodes.h:988
Index varlevelsup
Definition: primnodes.h:173
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
CommonTableExpr * GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
Definition: nodes.h:509
#define strVal(v)
Definition: value.h:54
#define MemSet(start, val, len)
Definition: c.h:858
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
List * targetList
Definition: parsenodes.h:138
bool resjunk
Definition: primnodes.h:1375
List * rtable
Definition: parsenodes.h:135
#define ERROR
Definition: elog.h:43
Oid vartype
Definition: primnodes.h:170
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:621
int location
Definition: primnodes.h:178
void * list_nth(const List *list, int n)
Definition: list.c:410
#define RECORDOID
Definition: pg_type.h:680
TupleDesc expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:462
Index varno
Definition: primnodes.h:166
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
struct ParseState * parentParseState
Definition: parse_node.h:170
bool self_reference
Definition: parsenodes.h:1016
static char * label
Definition: pg_basebackup.c:81
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
unsigned int Index
Definition: c.h:365
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
Expr * expr
Definition: primnodes.h:1368
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
Index ctelevelsup
Definition: parsenodes.h:1015
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:944
Query * subquery
Definition: parsenodes.h:967
int i
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1043
Definition: regcomp.c:224
Definition: pg_list.h:45
#define GetCTETargetList(cte)
Definition: parsenodes.h:1378
int16 AttrNumber
Definition: attnum.h:21
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1292
List * p_rtable
Definition: parse_node.h:172
static List * ExpandRowReference ( ParseState pstate,
Node expr,
bool  make_target_entry 
)
static

Definition at line 1357 of file parse_target.c.

References FieldSelect::arg, Assert, copyObject, expandRecordVariable(), ExpandSingleTable(), exprType(), exprTypmod(), FieldSelect::fieldnum, get_expr_result_type(), GetRTEByRangeTablePosn(), i, InvalidAttrNumber, IsA, lappend(), Var::location, lookup_rowtype_tupdesc_copy(), makeNode, makeTargetEntry(), NameStr, tupleDesc::natts, NIL, NULL, ParseState::p_next_resno, pstrdup(), RECORDOID, result, FieldSelect::resultcollid, FieldSelect::resulttype, FieldSelect::resulttypmod, TupleDescAttr, TYPEFUNC_COMPOSITE, Var::varlevelsup, and Var::varno.

Referenced by ExpandColumnRefStar(), and ExpandIndirectionStar().

1359 {
1360  List *result = NIL;
1362  int numAttrs;
1363  int i;
1364 
1365  /*
1366  * If the rowtype expression is a whole-row Var, we can expand the fields
1367  * as simple Vars. Note: if the RTE is a relation, this case leaves us
1368  * with the RTE's selectedCols bitmap showing the whole row as needing
1369  * select permission, as well as the individual columns. However, we can
1370  * only get here for weird notations like (table.*).*, so it's not worth
1371  * trying to clean up --- arguably, the permissions marking is correct
1372  * anyway for such cases.
1373  */
1374  if (IsA(expr, Var) &&
1375  ((Var *) expr)->varattno == InvalidAttrNumber)
1376  {
1377  Var *var = (Var *) expr;
1378  RangeTblEntry *rte;
1379 
1380  rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1381  return ExpandSingleTable(pstate, rte, var->location, make_target_entry);
1382  }
1383 
1384  /*
1385  * Otherwise we have to do it the hard way. Our current implementation is
1386  * to generate multiple copies of the expression and do FieldSelects.
1387  * (This can be pretty inefficient if the expression involves nontrivial
1388  * computation :-(.)
1389  *
1390  * Verify it's a composite type, and get the tupdesc. We use
1391  * get_expr_result_type() because that can handle references to functions
1392  * returning anonymous record types. If that fails, use
1393  * lookup_rowtype_tupdesc(), which will almost certainly fail as well, but
1394  * it will give an appropriate error message.
1395  *
1396  * If it's a Var of type RECORD, we have to work even harder: we have to
1397  * find what the Var refers to, and pass that to get_expr_result_type.
1398  * That task is handled by expandRecordVariable().
1399  */
1400  if (IsA(expr, Var) &&
1401  ((Var *) expr)->vartype == RECORDOID)
1402  tupleDesc = expandRecordVariable(pstate, (Var *) expr, 0);
1403  else if (get_expr_result_type(expr, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
1404  tupleDesc = lookup_rowtype_tupdesc_copy(exprType(expr),
1405  exprTypmod(expr));
1406  Assert(tupleDesc);
1407 
1408  /* Generate a list of references to the individual fields */
1409  numAttrs = tupleDesc->natts;
1410  for (i = 0; i < numAttrs; i++)
1411  {
1412  Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
1413  FieldSelect *fselect;
1414 
1415  if (att->attisdropped)
1416  continue;
1417 
1418  fselect = makeNode(FieldSelect);
1419  fselect->arg = (Expr *) copyObject(expr);
1420  fselect->fieldnum = i + 1;
1421  fselect->resulttype = att->atttypid;
1422  fselect->resulttypmod = att->atttypmod;
1423  /* save attribute's collation for parse_collate.c */
1424  fselect->resultcollid = att->attcollation;
1425 
1426  if (make_target_entry)
1427  {
1428  /* add TargetEntry decoration */
1429  TargetEntry *te;
1430 
1431  te = makeTargetEntry((Expr *) fselect,
1432  (AttrNumber) pstate->p_next_resno++,
1433  pstrdup(NameStr(att->attname)),
1434  false);
1435  result = lappend(result, te);
1436  }
1437  else
1438  result = lappend(result, fselect);
1439  }
1440 
1441  return result;
1442 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Index varlevelsup
Definition: primnodes.h:173
Oid resulttype
Definition: primnodes.h:744
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
char * pstrdup(const char *in)
Definition: mcxt.c:1077
static List * ExpandSingleTable(ParseState *pstate, RangeTblEntry *rte, int location, bool make_target_entry)
Expr * arg
Definition: primnodes.h:742
return result
Definition: formatting.c:1633
Definition: primnodes.h:163
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
int natts
Definition: tupdesc.h:73
Oid resultcollid
Definition: primnodes.h:747
int location
Definition: primnodes.h:178
int p_next_resno
Definition: parse_node.h:187
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define RECORDOID
Definition: pg_type.h:680
TupleDesc expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:235
List * lappend(List *list, void *datum)
Definition: list.c:128
Index varno
Definition: primnodes.h:166
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define InvalidAttrNumber
Definition: attnum.h:23
int i
#define NameStr(name)
Definition: c.h:499
#define copyObject(obj)
Definition: nodes.h:622
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
int32 resulttypmod
Definition: primnodes.h:746
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1292
AttrNumber fieldnum
Definition: primnodes.h:743
static List * ExpandSingleTable ( ParseState pstate,
RangeTblEntry rte,
int  location,
bool  make_target_entry 
)
static

Definition at line 1308 of file parse_target.c.

References ACL_SELECT, expandRelAttrs(), expandRTE(), lfirst, markVarForSelectPriv(), NULL, RangeTblEntry::requiredPerms, and RTERangeTablePosn().

Referenced by ExpandColumnRefStar(), and ExpandRowReference().

1310 {
1311  int sublevels_up;
1312  int rtindex;
1313 
1314  rtindex = RTERangeTablePosn(pstate, rte, &sublevels_up);
1315 
1316  if (make_target_entry)
1317  {
1318  /* expandRelAttrs handles permissions marking */
1319  return expandRelAttrs(pstate, rte, rtindex, sublevels_up,
1320  location);
1321  }
1322  else
1323  {
1324  List *vars;
1325  ListCell *l;
1326 
1327  expandRTE(rte, rtindex, sublevels_up, location, false,
1328  NULL, &vars);
1329 
1330  /*
1331  * Require read access to the table. This is normally redundant with
1332  * the markVarForSelectPriv calls below, but not if the table has zero
1333  * columns.
1334  */
1335  rte->requiredPerms |= ACL_SELECT;
1336 
1337  /* Require read access to each column */
1338  foreach(l, vars)
1339  {
1340  Var *var = (Var *) lfirst(l);
1341 
1342  markVarForSelectPriv(pstate, var, rte);
1343  }
1344 
1345  return vars;
1346  }
1347 }
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
AclMode requiredPerms
Definition: parsenodes.h:1047
Definition: primnodes.h:163
List * expandRelAttrs(ParseState *pstate, RangeTblEntry *rte, int rtindex, int sublevels_up, int location)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
#define ACL_SELECT
Definition: parsenodes.h:73
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
int RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
Definition: regcomp.c:224
Definition: pg_list.h:45
char* FigureColname ( Node node)

Definition at line 1635 of file parse_target.c.

References FigureColnameInternal(), name, and NULL.

Referenced by transformRangeFunction(), transformTargetEntry(), and transformXmlExpr().

1636 {
1637  char *name = NULL;
1638 
1639  (void) FigureColnameInternal(node, &name);
1640  if (name != NULL)
1641  return name;
1642  /* default result if we can't guess anything */
1643  return "?column?";
1644 }
#define NULL
Definition: c.h:229
static int FigureColnameInternal(Node *node, char **name)
const char * name
Definition: encode.c:521
static int FigureColnameInternal ( Node node,
char **  name 
)
static

Definition at line 1674 of file parse_target.c.

References AEXPR_NULLIF, AEXPR_PAREN, ALL_SUBLINK, ANY_SUBLINK, arg, A_Indirection::arg, ARRAY_SUBLINK, CTE_SUBLINK, EXISTS_SUBLINK, EXPR_SUBLINK, i, A_Indirection::indirection, 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, nodeTag, NULL, 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_MinMaxExpr, T_RowExpr, T_SQLValueFunction, T_SubLink, T_TypeCast, T_XmlExpr, T_XmlSerialize, and Query::targetList.

Referenced by FigureColname(), and FigureIndexColname().

1675 {
1676  int strength = 0;
1677 
1678  if (node == NULL)
1679  return strength;
1680 
1681  switch (nodeTag(node))
1682  {
1683  case T_ColumnRef:
1684  {
1685  char *fname = NULL;
1686  ListCell *l;
1687 
1688  /* find last field name, if any, ignoring "*" */
1689  foreach(l, ((ColumnRef *) node)->fields)
1690  {
1691  Node *i = lfirst(l);
1692 
1693  if (IsA(i, String))
1694  fname = strVal(i);
1695  }
1696  if (fname)
1697  {
1698  *name = fname;
1699  return 2;
1700  }
1701  }
1702  break;
1703  case T_A_Indirection:
1704  {
1705  A_Indirection *ind = (A_Indirection *) node;
1706  char *fname = NULL;
1707  ListCell *l;
1708 
1709  /* find last field name, if any, ignoring "*" and subscripts */
1710  foreach(l, ind->indirection)
1711  {
1712  Node *i = lfirst(l);
1713 
1714  if (IsA(i, String))
1715  fname = strVal(i);
1716  }
1717  if (fname)
1718  {
1719  *name = fname;
1720  return 2;
1721  }
1722  return FigureColnameInternal(ind->arg, name);
1723  }
1724  break;
1725  case T_FuncCall:
1726  *name = strVal(llast(((FuncCall *) node)->funcname));
1727  return 2;
1728  case T_A_Expr:
1729  if (((A_Expr *) node)->kind == AEXPR_NULLIF)
1730  {
1731  /* make nullif() act like a regular function */
1732  *name = "nullif";
1733  return 2;
1734  }
1735  if (((A_Expr *) node)->kind == AEXPR_PAREN)
1736  {
1737  /* look through dummy parenthesis node */
1738  return FigureColnameInternal(((A_Expr *) node)->lexpr, name);
1739  }
1740  break;
1741  case T_TypeCast:
1742  strength = FigureColnameInternal(((TypeCast *) node)->arg,
1743  name);
1744  if (strength <= 1)
1745  {
1746  if (((TypeCast *) node)->typeName != NULL)
1747  {
1748  *name = strVal(llast(((TypeCast *) node)->typeName->names));
1749  return 1;
1750  }
1751  }
1752  break;
1753  case T_CollateClause:
1754  return FigureColnameInternal(((CollateClause *) node)->arg, name);
1755  case T_GroupingFunc:
1756  /* make GROUPING() act like a regular function */
1757  *name = "grouping";
1758  return 2;
1759  case T_SubLink:
1760  switch (((SubLink *) node)->subLinkType)
1761  {
1762  case EXISTS_SUBLINK:
1763  *name = "exists";
1764  return 2;
1765  case ARRAY_SUBLINK:
1766  *name = "array";
1767  return 2;
1768  case EXPR_SUBLINK:
1769  {
1770  /* Get column name of the subquery's single target */
1771  SubLink *sublink = (SubLink *) node;
1772  Query *query = (Query *) sublink->subselect;
1773 
1774  /*
1775  * The subquery has probably already been transformed,
1776  * but let's be careful and check that. (The reason
1777  * we can see a transformed subquery here is that
1778  * transformSubLink is lazy and modifies the SubLink
1779  * node in-place.)
1780  */
1781  if (IsA(query, Query))
1782  {
1783  TargetEntry *te = (TargetEntry *) linitial(query->targetList);
1784 
1785  if (te->resname)
1786  {
1787  *name = te->resname;
1788  return 2;
1789  }
1790  }
1791  }
1792  break;
1793  /* As with other operator-like nodes, these have no names */
1794  case MULTIEXPR_SUBLINK:
1795  case ALL_SUBLINK:
1796  case ANY_SUBLINK:
1797  case ROWCOMPARE_SUBLINK:
1798  case CTE_SUBLINK:
1799  break;
1800  }
1801  break;
1802  case T_CaseExpr:
1803  strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult,
1804  name);
1805  if (strength <= 1)
1806  {
1807  *name = "case";
1808  return 1;
1809  }
1810  break;
1811  case T_A_ArrayExpr:
1812  /* make ARRAY[] act like a function */
1813  *name = "array";
1814  return 2;
1815  case T_RowExpr:
1816  /* make ROW() act like a function */
1817  *name = "row";
1818  return 2;
1819  case T_CoalesceExpr:
1820  /* make coalesce() act like a regular function */
1821  *name = "coalesce";
1822  return 2;
1823  case T_MinMaxExpr:
1824  /* make greatest/least act like a regular function */
1825  switch (((MinMaxExpr *) node)->op)
1826  {
1827  case IS_GREATEST:
1828  *name = "greatest";
1829  return 2;
1830  case IS_LEAST:
1831  *name = "least";
1832  return 2;
1833  }
1834  break;
1835  case T_SQLValueFunction:
1836  /* make these act like a function or variable */
1837  switch (((SQLValueFunction *) node)->op)
1838  {
1839  case SVFOP_CURRENT_DATE:
1840  *name = "current_date";
1841  return 2;
1842  case SVFOP_CURRENT_TIME:
1843  case SVFOP_CURRENT_TIME_N:
1844  *name = "current_time";
1845  return 2;
1848  *name = "current_timestamp";
1849  return 2;
1850  case SVFOP_LOCALTIME:
1851  case SVFOP_LOCALTIME_N:
1852  *name = "localtime";
1853  return 2;
1854  case SVFOP_LOCALTIMESTAMP:
1856  *name = "localtimestamp";
1857  return 2;
1858  case SVFOP_CURRENT_ROLE:
1859  *name = "current_role";
1860  return 2;
1861  case SVFOP_CURRENT_USER:
1862  *name = "current_user";
1863  return 2;
1864  case SVFOP_USER:
1865  *name = "user";
1866  return 2;
1867  case SVFOP_SESSION_USER:
1868  *name = "session_user";
1869  return 2;
1870  case SVFOP_CURRENT_CATALOG:
1871  *name = "current_catalog";
1872  return 2;
1873  case SVFOP_CURRENT_SCHEMA:
1874  *name = "current_schema";
1875  return 2;
1876  }
1877  break;
1878  case T_XmlExpr:
1879  /* make SQL/XML functions act like a regular function */
1880  switch (((XmlExpr *) node)->op)
1881  {
1882  case IS_XMLCONCAT:
1883  *name = "xmlconcat";
1884  return 2;
1885  case IS_XMLELEMENT:
1886  *name = "xmlelement";
1887  return 2;
1888  case IS_XMLFOREST:
1889  *name = "xmlforest";
1890  return 2;
1891  case IS_XMLPARSE:
1892  *name = "xmlparse";
1893  return 2;
1894  case IS_XMLPI:
1895  *name = "xmlpi";
1896  return 2;
1897  case IS_XMLROOT:
1898  *name = "xmlroot";
1899  return 2;
1900  case IS_XMLSERIALIZE:
1901  *name = "xmlserialize";
1902  return 2;
1903  case IS_DOCUMENT:
1904  /* nothing */
1905  break;
1906  }
1907  break;
1908  case T_XmlSerialize:
1909  *name = "xmlserialize";
1910  return 2;
1911  default:
1912  break;
1913  }
1914 
1915  return strength;
1916 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define llast(l)
Definition: pg_list.h:131
Definition: nodes.h:509
#define strVal(v)
Definition: value.h:54
char * resname
Definition: primnodes.h:1370
List * targetList
Definition: parsenodes.h:138
#define linitial(l)
Definition: pg_list.h:111
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
List * indirection
Definition: parsenodes.h:405
static int FigureColnameInternal(Node *node, char **name)
const char * name
Definition: encode.c:521
#define nodeTag(nodeptr)
Definition: nodes.h:514
int i
void * arg
char* FigureIndexColname ( Node node)

Definition at line 1654 of file parse_target.c.

References FigureColnameInternal(), name, and NULL.

Referenced by transformIndexStmt().

1655 {
1656  char *name = NULL;
1657 
1658  (void) FigureColnameInternal(node, &name);
1659  return name;
1660 }
#define NULL
Definition: c.h:229
static int FigureColnameInternal(Node *node, char **name)
const char * name
Definition: encode.c:521
static void markTargetListOrigin ( ParseState pstate,
TargetEntry tle,
Var var,
int  levelsup 
)
static

Definition at line 351 of file parse_target.c.

References Alias::aliasname, Assert, elog, RangeTblEntry::eref, ERROR, get_tle_by_resno(), GetCTEForRTE(), GetCTETargetList, GetRTEByRangeTablePosn(), InvalidAttrNumber, IsA, RangeTblEntry::joinaliasvars, list_length(), list_nth(), NULL, RangeTblEntry::relid, TargetEntry::resjunk, TargetEntry::resorigcol, TargetEntry::resorigtbl, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, 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().

353 {
354  int netlevelsup;
355  RangeTblEntry *rte;
356  AttrNumber attnum;
357 
358  if (var == NULL || !IsA(var, Var))
359  return;
360  netlevelsup = var->varlevelsup + levelsup;
361  rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
362  attnum = var->varattno;
363 
364  switch (rte->rtekind)
365  {
366  case RTE_RELATION:
367  /* It's a table or view, report it */
368  tle->resorigtbl = rte->relid;
369  tle->resorigcol = attnum;
370  break;
371  case RTE_SUBQUERY:
372  /* Subselect-in-FROM: copy up from the subselect */
373  if (attnum != InvalidAttrNumber)
374  {
376  attnum);
377 
378  if (ste == NULL || ste->resjunk)
379  elog(ERROR, "subquery %s does not have attribute %d",
380  rte->eref->aliasname, attnum);
381  tle->resorigtbl = ste->resorigtbl;
382  tle->resorigcol = ste->resorigcol;
383  }
384  break;
385  case RTE_JOIN:
386  /* Join RTE --- recursively inspect the alias variable */
387  if (attnum != InvalidAttrNumber)
388  {
389  Var *aliasvar;
390 
391  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
392  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
393  /* We intentionally don't strip implicit coercions here */
394  markTargetListOrigin(pstate, tle, aliasvar, netlevelsup);
395  }
396  break;
397  case RTE_FUNCTION:
398  case RTE_VALUES:
399  case RTE_TABLEFUNC:
400  case RTE_NAMEDTUPLESTORE:
401  /* not a simple relation, leave it unmarked */
402  break;
403  case RTE_CTE:
404 
405  /*
406  * CTE reference: copy up from the subquery, if possible. If the
407  * RTE is a recursive self-reference then we can't do anything
408  * because we haven't finished analyzing it yet. However, it's no
409  * big loss because we must be down inside the recursive term of a
410  * recursive CTE, and so any markings on the current targetlist
411  * are not going to affect the results anyway.
412  */
413  if (attnum != InvalidAttrNumber && !rte->self_reference)
414  {
415  CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
416  TargetEntry *ste;
417 
418  ste = get_tle_by_resno(GetCTETargetList(cte), attnum);
419  if (ste == NULL || ste->resjunk)
420  elog(ERROR, "subquery %s does not have attribute %d",
421  rte->eref->aliasname, attnum);
422  tle->resorigtbl = ste->resorigtbl;
423  tle->resorigcol = ste->resorigcol;
424  }
425  break;
426  }
427 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
List * joinaliasvars
Definition: parsenodes.h:988
Index varlevelsup
Definition: primnodes.h:173
CommonTableExpr * GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
Oid resorigtbl
Definition: primnodes.h:1373
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
List * targetList
Definition: parsenodes.h:138
bool resjunk
Definition: primnodes.h:1375
#define ERROR
Definition: elog.h:43
void * list_nth(const List *list, int n)
Definition: list.c:410
Index varno
Definition: primnodes.h:166
bool self_reference
Definition: parsenodes.h:1016
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:89
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:944
static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle, Var *var, int levelsup)
Definition: parse_target.c:351
AttrNumber resorigcol
Definition: primnodes.h:1374
Query * subquery
Definition: parsenodes.h:967
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1043
#define GetCTETargetList(cte)
Definition: parsenodes.h:1378
int16 AttrNumber
Definition: attnum.h:21
void markTargetListOrigins ( ParseState pstate,
List targetlist 
)

Definition at line 329 of file parse_target.c.

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

Referenced by transformReturningList(), and transformSelectStmt().

330 {
331  ListCell *l;
332 
333  foreach(l, targetlist)
334  {
335  TargetEntry *tle = (TargetEntry *) lfirst(l);
336 
337  markTargetListOrigin(pstate, tle, (Var *) tle->expr, 0);
338  }
339 }
Definition: primnodes.h:163
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1368
static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle, Var *var, int levelsup)
Definition: parse_target.c:351
void resolveTargetListUnknowns ( ParseState pstate,
List targetlist 
)

Definition at line 299 of file parse_target.c.

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

Referenced by transformReturningList(), and transformSelectStmt().

300 {
301  ListCell *l;
302 
303  foreach(l, targetlist)
304  {
305  TargetEntry *tle = (TargetEntry *) lfirst(l);
306  Oid restype = exprType((Node *) tle->expr);
307 
308  if (restype == UNKNOWNOID)
309  {
310  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
311  restype, TEXTOID, -1,
314  -1);
315  }
316  }
317 }
#define TEXTOID
Definition: pg_type.h:324
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:156
Definition: nodes.h:509
unsigned int Oid
Definition: postgres_ext.h:31
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1368
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define UNKNOWNOID
Definition: pg_type.h:431
Expr* transformAssignedExpr ( ParseState pstate,
Expr expr,
ParseExprKind  exprKind,
char *  colname,
int  attrno,
List indirection,
int  location 
)

Definition at line 455 of file parse_target.c.

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(), make_var(), makeNullConst(), NULL, ParseState::p_expr_kind, ParseState::p_is_insert, ParseState::p_target_rangetblentry, ParseState::p_target_relation, parser_errposition(), RelationData::rd_att, transformAssignmentIndirection(), TupleDescAttr, SetToDefault::typeId, and SetToDefault::typeMod.

Referenced by transformInsertRow(), and updateTargetListEntry().

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

Definition at line 676 of file parse_target.c.

References FieldStore::arg, Assert, COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, CaseTestExpr::collation, ereport, errcode(), errhint(), errmsg(), ERROR, exprType(), FieldStore::fieldnums, for_each_cell, format_type_be(), get_attnum(), get_atttypetypmodcoll(), i, InvalidAttrNumber, IsA, lappend(), lfirst, list_make1, list_make1_int, lnext, makeNode, FieldStore::newvals, NIL, NULL, parser_errposition(), result, FieldStore::resulttype, strVal, transformAssignmentSubscripts(), CaseTestExpr::typeId, typeidTypeRelid(), and CaseTestExpr::typeMod.

Referenced by transformAssignedExpr(), and transformAssignmentSubscripts().

686 {
687  Node *result;
688  List *subscripts = NIL;
689  bool isSlice = false;
690  ListCell *i;
691 
692  if (indirection && !basenode)
693  {
694  /* Set up a substitution. We reuse CaseTestExpr for this. */
696 
697  ctest->typeId = targetTypeId;
698  ctest->typeMod = targetTypMod;
699  ctest->collation = targetCollation;
700  basenode = (Node *) ctest;
701  }
702 
703  /*
704  * We have to split any field-selection operations apart from
705  * subscripting. Adjacent A_Indices nodes have to be treated as a single
706  * multidimensional subscript operation.
707  */
708  for_each_cell(i, indirection)
709  {
710  Node *n = lfirst(i);
711 
712  if (IsA(n, A_Indices))
713  {
714  subscripts = lappend(subscripts, n);
715  if (((A_Indices *) n)->is_slice)
716  isSlice = true;
717  }
718  else if (IsA(n, A_Star))
719  {
720  ereport(ERROR,
721  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
722  errmsg("row expansion via \"*\" is not supported here"),
723  parser_errposition(pstate, location)));
724  }
725  else
726  {
727  FieldStore *fstore;
728  Oid typrelid;
729  AttrNumber attnum;
730  Oid fieldTypeId;
731  int32 fieldTypMod;
732  Oid fieldCollation;
733 
734  Assert(IsA(n, String));
735 
736  /* process subscripts before this field selection */
737  if (subscripts)
738  {
739  /* recurse, and then return because we're done */
740  return transformAssignmentSubscripts(pstate,
741  basenode,
742  targetName,
743  targetTypeId,
744  targetTypMod,
745  targetCollation,
746  subscripts,
747  isSlice,
748  i,
749  rhs,
750  location);
751  }
752 
753  /* No subscripts, so can process field selection here */
754 
755  typrelid = typeidTypeRelid(targetTypeId);
756  if (!typrelid)
757  ereport(ERROR,
758  (errcode(ERRCODE_DATATYPE_MISMATCH),
759  errmsg("cannot assign to field \"%s\" of column \"%s\" because its type %s is not a composite type",
760  strVal(n), targetName,
761  format_type_be(targetTypeId)),
762  parser_errposition(pstate, location)));
763 
764  attnum = get_attnum(typrelid, strVal(n));
765  if (attnum == InvalidAttrNumber)
766  ereport(ERROR,
767  (errcode(ERRCODE_UNDEFINED_COLUMN),
768  errmsg("cannot assign to field \"%s\" of column \"%s\" because there is no such column in data type %s",
769  strVal(n), targetName,
770  format_type_be(targetTypeId)),
771  parser_errposition(pstate, location)));
772  if (attnum < 0)
773  ereport(ERROR,
774  (errcode(ERRCODE_UNDEFINED_COLUMN),
775  errmsg("cannot assign to system column \"%s\"",
776  strVal(n)),
777  parser_errposition(pstate, location)));
778 
779  get_atttypetypmodcoll(typrelid, attnum,
780  &fieldTypeId, &fieldTypMod, &fieldCollation);
781 
782  /* recurse to create appropriate RHS for field assign */
783  rhs = transformAssignmentIndirection(pstate,
784  NULL,
785  strVal(n),
786  false,
787  fieldTypeId,
788  fieldTypMod,
789  fieldCollation,
790  lnext(i),
791  rhs,
792  location);
793 
794  /* and build a FieldStore node */
795  fstore = makeNode(FieldStore);
796  fstore->arg = (Expr *) basenode;
797  fstore->newvals = list_make1(rhs);
798  fstore->fieldnums = list_make1_int(attnum);
799  fstore->resulttype = targetTypeId;
800 
801  return (Node *) fstore;
802  }
803  }
804 
805  /* process trailing subscripts, if any */
806  if (subscripts)
807  {
808  /* recurse, and then return because we're done */
809  return transformAssignmentSubscripts(pstate,
810  basenode,
811  targetName,
812  targetTypeId,
813  targetTypMod,
814  targetCollation,
815  subscripts,
816  isSlice,
817  NULL,
818  rhs,
819  location);
820  }
821 
822  /* base case: just coerce RHS to match target type ID */
823 
824  result = coerce_to_target_type(pstate,
825  rhs, exprType(rhs),
826  targetTypeId, targetTypMod,
829  -1);
830  if (result == NULL)
831  {
832  if (targetIsArray)
833  ereport(ERROR,
834  (errcode(ERRCODE_DATATYPE_MISMATCH),
835  errmsg("array assignment to \"%s\" requires type %s"
836  " but expression is of type %s",
837  targetName,
838  format_type_be(targetTypeId),
839  format_type_be(exprType(rhs))),
840  errhint("You will need to rewrite or cast the expression."),
841  parser_errposition(pstate, location)));
842  else
843  ereport(ERROR,
844  (errcode(ERRCODE_DATATYPE_MISMATCH),
845  errmsg("subfield \"%s\" is of type %s"
846  " but expression is of type %s",
847  targetName,
848  format_type_be(targetTypeId),
849  format_type_be(exprType(rhs))),
850  errhint("You will need to rewrite or cast the expression."),
851  parser_errposition(pstate, location)));
852  }
853 
854  return result;
855 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Expr * arg
Definition: primnodes.h:768
int errhint(const char *fmt,...)
Definition: elog.c:987
Definition: nodes.h:509
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
return result
Definition: formatting.c:1633
Oid typeidTypeRelid(Oid type_id)
Definition: parse_type.c:646
unsigned int Oid
Definition: postgres_ext.h:31
static Node * transformAssignmentSubscripts(ParseState *pstate, Node *basenode, const char *targetName, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, List *subscripts, bool isSlice, ListCell *next_indirection, Node *rhs, int location)
Definition: parse_target.c:861
int32 typeMod
Definition: primnodes.h:937
signed int int32
Definition: c.h:256
#define list_make1(x1)
Definition: pg_list.h:139
#define ERROR
Definition: elog.h:43
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:77
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:821
Oid resulttype
Definition: primnodes.h:771
#define list_make1_int(x1)
Definition: pg_list.h:145
#define lnext(lc)
Definition: pg_list.h:105
#define ereport(elevel, rest)
Definition: elog.h:122
List * newvals
Definition: primnodes.h:769
List * lappend(List *list, void *datum)
Definition: list.c:128
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
void get_atttypetypmodcoll(Oid relid, AttrNumber attnum, Oid *typid, int32 *typmod, Oid *collid)
Definition: lsyscache.c:935
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
#define for_each_cell(cell, initcell)
Definition: pg_list.h:169
#define InvalidAttrNumber
Definition: attnum.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:797
List * fieldnums
Definition: primnodes.h:770
int i
static Node * transformAssignmentIndirection(ParseState *pstate, Node *basenode, const char *targetName, bool targetIsArray, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, ListCell *indirection, Node *rhs, int location)
Definition: parse_target.c:676
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
static Node * transformAssignmentSubscripts ( ParseState pstate,
Node basenode,
const char *  targetName,
Oid  targetTypeId,
int32  targetTypMod,
Oid  targetCollation,
List subscripts,
bool  isSlice,
ListCell next_indirection,
Node rhs,
int  location 
)
static

Definition at line 861 of file parse_target.c.

References Assert, COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, ereport, errcode(), errmsg(), ERROR, exprType(), format_type_be(), get_typcollation(), NIL, NULL, parser_errposition(), result, transformArraySubscripts(), transformArrayType(), and transformAssignmentIndirection().

Referenced by transformAssignmentIndirection().

872 {
873  Node *result;
874  Oid arrayType;
875  int32 arrayTypMod;
876  Oid elementTypeId;
877  Oid typeNeeded;
878  Oid collationNeeded;
879 
880  Assert(subscripts != NIL);
881 
882  /* Identify the actual array type and element type involved */
883  arrayType = targetTypeId;
884  arrayTypMod = targetTypMod;
885  elementTypeId = transformArrayType(&arrayType, &arrayTypMod);
886 
887  /* Identify type that RHS must provide */
888  typeNeeded = isSlice ? arrayType : elementTypeId;
889 
890  /*
891  * Array normally has same collation as elements, but there's an
892  * exception: we might be subscripting a domain over an array type. In
893  * that case use collation of the base type.
894  */
895  if (arrayType == targetTypeId)
896  collationNeeded = targetCollation;
897  else
898  collationNeeded = get_typcollation(arrayType);
899 
900  /* recurse to create appropriate RHS for array assign */
901  rhs = transformAssignmentIndirection(pstate,
902  NULL,
903  targetName,
904  true,
905  typeNeeded,
906  arrayTypMod,
907  collationNeeded,
908  next_indirection,
909  rhs,
910  location);
911 
912  /* process subscripts */
913  result = (Node *) transformArraySubscripts(pstate,
914  basenode,
915  arrayType,
916  elementTypeId,
917  arrayTypMod,
918  subscripts,
919  rhs);
920 
921  /* If target was a domain over array, need to coerce up to the domain */
922  if (arrayType != targetTypeId)
923  {
924  Oid resulttype = exprType(result);
925 
926  result = coerce_to_target_type(pstate,
927  result, resulttype,
928  targetTypeId, targetTypMod,
931  -1);
932  /* can fail if we had int2vector/oidvector, but not for true domains */
933  if (result == NULL)
934  ereport(ERROR,
935  (errcode(ERRCODE_CANNOT_COERCE),
936  errmsg("cannot cast type %s to %s",
937  format_type_be(resulttype),
938  format_type_be(targetTypeId)),
939  parser_errposition(pstate, location)));
940  }
941 
942  return result;
943 }
#define NIL
Definition: pg_list.h:69
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
return result
Definition: formatting.c:1633
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
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:77
#define ereport(elevel, rest)
Definition: elog.h:122
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:2781
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Oid transformArrayType(Oid *arrayType, int32 *arrayTypmod)
Definition: parse_node.c:216
int errmsg(const char *fmt,...)
Definition: elog.c:797
static Node * transformAssignmentIndirection(ParseState *pstate, Node *basenode, const char *targetName, bool targetIsArray, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, ListCell *indirection, Node *rhs, int location)
Definition: parse_target.c:676
ArrayRef * transformArraySubscripts(ParseState *pstate, Node *arrayBase, Oid arrayType, Oid elementType, int32 arrayTypMod, List *indirection, Node *assignFrom)
Definition: parse_node.c:294
List* transformExpressionList ( ParseState pstate,
List exprlist,
ParseExprKind  exprKind,
bool  allowDefault 
)

Definition at line 228 of file parse_target.c.

References Assert, ExpandColumnRefStar(), ExpandIndirectionStar(), ColumnRef::fields, A_Indirection::indirection, IsA, lappend(), lfirst, list_concat(), llast, NIL, ParseState::p_multiassign_exprs, result, and transformExpr().

Referenced by transformRowExpr(), and transformValuesClause().

230 {
231  List *result = NIL;
232  ListCell *lc;
233 
234  foreach(lc, exprlist)
235  {
236  Node *e = (Node *) lfirst(lc);
237 
238  /*
239  * Check for "something.*". Depending on the complexity of the
240  * "something", the star could appear as the last field in ColumnRef,
241  * or as the last indirection item in A_Indirection.
242  */
243  if (IsA(e, ColumnRef))
244  {
245  ColumnRef *cref = (ColumnRef *) e;
246 
247  if (IsA(llast(cref->fields), A_Star))
248  {
249  /* It is something.*, expand into multiple items */
250  result = list_concat(result,
251  ExpandColumnRefStar(pstate, cref,
252  false));
253  continue;
254  }
255  }
256  else if (IsA(e, A_Indirection))
257  {
259 
260  if (IsA(llast(ind->indirection), A_Star))
261  {
262  /* It is something.*, expand into multiple items */
263  result = list_concat(result,
264  ExpandIndirectionStar(pstate, ind,
265  false, exprKind));
266  continue;
267  }
268  }
269 
270  /*
271  * Not "something.*", so transform as a single expression. If it's a
272  * SetToDefault node and we should allow that, pass it through
273  * unmodified. (transformExpr will throw the appropriate error if
274  * we're disallowing it.)
275  */
276  if (allowDefault && IsA(e, SetToDefault))
277  /* do nothing */ ;
278  else
279  e = transformExpr(pstate, e, exprKind);
280 
281  result = lappend(result, e);
282  }
283 
284  /* Shouldn't have any multiassign items here */
285  Assert(pstate->p_multiassign_exprs == NIL);
286 
287  return result;
288 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
#define llast(l)
Definition: pg_list.h:131
Definition: nodes.h:509
List * p_multiassign_exprs
Definition: parse_node.h:188
List * list_concat(List *list1, List *list2)
Definition: list.c:321
return result
Definition: formatting.c:1633
static List * ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref, bool make_target_entry)
struct A_Star A_Star
static List * ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind, bool make_target_entry, ParseExprKind exprKind)
List * lappend(List *list, void *datum)
Definition: list.c:128
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
List * indirection
Definition: parsenodes.h:405
e
Definition: preproc-init.c:82
Definition: pg_list.h:45
List * fields
Definition: parsenodes.h:234
TargetEntry* transformTargetEntry ( ParseState pstate,
Node node,
Node expr,
ParseExprKind  exprKind,
char *  colname,
bool  resjunk 
)

Definition at line 85 of file parse_target.c.

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

Referenced by findTargetlistEntrySQL99(), and transformTargetList().

91 {
92  /* Transform the node if caller didn't do it already */
93  if (expr == NULL)
94  {
95  /*
96  * If it's a SetToDefault node and we should allow that, pass it
97  * through unmodified. (transformExpr will throw the appropriate
98  * error if we're disallowing it.)
99  */
100  if (exprKind == EXPR_KIND_UPDATE_SOURCE && IsA(node, SetToDefault))
101  expr = node;
102  else
103  expr = transformExpr(pstate, node, exprKind);
104  }
105 
106  if (colname == NULL && !resjunk)
107  {
108  /*
109  * Generate a suitable column name for a column without any explicit
110  * 'AS ColumnName' clause.
111  */
112  colname = FigureColname(node);
113  }
114 
115  return makeTargetEntry((Expr *) expr,
116  (AttrNumber) pstate->p_next_resno++,
117  colname,
118  resjunk);
119 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
char * FigureColname(Node *node)
int p_next_resno
Definition: parse_node.h:187
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:235
#define NULL
Definition: c.h:229
int16 AttrNumber
Definition: attnum.h:21
List* transformTargetList ( ParseState pstate,
List targetlist,
ParseExprKind  exprKind 
)

Definition at line 131 of file parse_target.c.

References Assert, ExpandColumnRefStar(), ExpandIndirectionStar(), EXPR_KIND_UPDATE_SOURCE, ColumnRef::fields, A_Indirection::indirection, IsA, lappend(), lfirst, list_concat(), llast, ResTarget::name, NIL, NULL, ParseState::p_multiassign_exprs, transformTargetEntry(), and ResTarget::val.

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

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

Definition at line 617 of file parse_target.c.

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

Referenced by transformUpdateTargetList().

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