LCOV - code coverage report
Current view: top level - src/backend/parser - parse_target.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 482 567 85.0 %
Date: 2020-05-25 04:06:28 Functions: 20 20 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * parse_target.c
       4             :  *    handle target lists
       5             :  *
       6             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/parser/parse_target.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include "catalog/pg_type.h"
      18             : #include "commands/dbcommands.h"
      19             : #include "funcapi.h"
      20             : #include "miscadmin.h"
      21             : #include "nodes/makefuncs.h"
      22             : #include "nodes/nodeFuncs.h"
      23             : #include "parser/parse_coerce.h"
      24             : #include "parser/parse_expr.h"
      25             : #include "parser/parse_func.h"
      26             : #include "parser/parse_relation.h"
      27             : #include "parser/parse_target.h"
      28             : #include "parser/parse_type.h"
      29             : #include "parser/parsetree.h"
      30             : #include "utils/builtins.h"
      31             : #include "utils/lsyscache.h"
      32             : #include "utils/rel.h"
      33             : #include "utils/typcache.h"
      34             : 
      35             : static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle,
      36             :                                  Var *var, int levelsup);
      37             : static Node *transformAssignmentIndirection(ParseState *pstate,
      38             :                                             Node *basenode,
      39             :                                             const char *targetName,
      40             :                                             bool targetIsSubscripting,
      41             :                                             Oid targetTypeId,
      42             :                                             int32 targetTypMod,
      43             :                                             Oid targetCollation,
      44             :                                             List *indirection,
      45             :                                             ListCell *indirection_cell,
      46             :                                             Node *rhs,
      47             :                                             int location);
      48             : static Node *transformAssignmentSubscripts(ParseState *pstate,
      49             :                                            Node *basenode,
      50             :                                            const char *targetName,
      51             :                                            Oid targetTypeId,
      52             :                                            int32 targetTypMod,
      53             :                                            Oid targetCollation,
      54             :                                            List *subscripts,
      55             :                                            bool isSlice,
      56             :                                            List *indirection,
      57             :                                            ListCell *next_indirection,
      58             :                                            Node *rhs,
      59             :                                            int location);
      60             : static List *ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref,
      61             :                                  bool make_target_entry);
      62             : static List *ExpandAllTables(ParseState *pstate, int location);
      63             : static List *ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind,
      64             :                                    bool make_target_entry, ParseExprKind exprKind);
      65             : static List *ExpandSingleTable(ParseState *pstate, ParseNamespaceItem *nsitem,
      66             :                                int sublevels_up, int location,
      67             :                                bool make_target_entry);
      68             : static List *ExpandRowReference(ParseState *pstate, Node *expr,
      69             :                                 bool make_target_entry);
      70             : static int  FigureColnameInternal(Node *node, char **name);
      71             : 
      72             : 
      73             : /*
      74             :  * transformTargetEntry()
      75             :  *  Transform any ordinary "expression-type" node into a targetlist entry.
      76             :  *  This is exported so that parse_clause.c can generate targetlist entries
      77             :  *  for ORDER/GROUP BY items that are not already in the targetlist.
      78             :  *
      79             :  * node     the (untransformed) parse tree for the value expression.
      80             :  * expr     the transformed expression, or NULL if caller didn't do it yet.
      81             :  * exprKind expression kind (EXPR_KIND_SELECT_TARGET, etc)
      82             :  * colname  the column name to be assigned, or NULL if none yet set.
      83             :  * resjunk  true if the target should be marked resjunk, ie, it is not
      84             :  *          wanted in the final projected tuple.
      85             :  */
      86             : TargetEntry *
      87     1278226 : transformTargetEntry(ParseState *pstate,
      88             :                      Node *node,
      89             :                      Node *expr,
      90             :                      ParseExprKind exprKind,
      91             :                      char *colname,
      92             :                      bool resjunk)
      93             : {
      94             :     /* Transform the node if caller didn't do it already */
      95     1278226 :     if (expr == NULL)
      96             :     {
      97             :         /*
      98             :          * If it's a SetToDefault node and we should allow that, pass it
      99             :          * through unmodified.  (transformExpr will throw the appropriate
     100             :          * error if we're disallowing it.)
     101             :          */
     102     1250870 :         if (exprKind == EXPR_KIND_UPDATE_SOURCE && IsA(node, SetToDefault))
     103         108 :             expr = node;
     104             :         else
     105     1250762 :             expr = transformExpr(pstate, node, exprKind);
     106             :     }
     107             : 
     108     1276406 :     if (colname == NULL && !resjunk)
     109             :     {
     110             :         /*
     111             :          * Generate a suitable column name for a column without any explicit
     112             :          * 'AS ColumnName' clause.
     113             :          */
     114      759420 :         colname = FigureColname(node);
     115             :     }
     116             : 
     117     3829218 :     return makeTargetEntry((Expr *) expr,
     118     1276406 :                            (AttrNumber) pstate->p_next_resno++,
     119             :                            colname,
     120             :                            resjunk);
     121             : }
     122             : 
     123             : 
     124             : /*
     125             :  * transformTargetList()
     126             :  * Turns a list of ResTarget's into a list of TargetEntry's.
     127             :  *
     128             :  * This code acts mostly the same for SELECT, UPDATE, or RETURNING lists;
     129             :  * the main thing is to transform the given expressions (the "val" fields).
     130             :  * The exprKind parameter distinguishes these cases when necessary.
     131             :  */
     132             : List *
     133      378184 : transformTargetList(ParseState *pstate, List *targetlist,
     134             :                     ParseExprKind exprKind)
     135             : {
     136      378184 :     List       *p_target = NIL;
     137             :     bool        expand_star;
     138             :     ListCell   *o_target;
     139             : 
     140             :     /* Shouldn't have any leftover multiassign items at start */
     141             :     Assert(pstate->p_multiassign_exprs == NIL);
     142             : 
     143             :     /* Expand "something.*" in SELECT and RETURNING, but not UPDATE */
     144      378184 :     expand_star = (exprKind != EXPR_KIND_UPDATE_SOURCE);
     145             : 
     146     1664394 :     foreach(o_target, targetlist)
     147             :     {
     148     1288034 :         ResTarget  *res = (ResTarget *) lfirst(o_target);
     149             : 
     150             :         /*
     151             :          * Check for "something.*".  Depending on the complexity of the
     152             :          * "something", the star could appear as the last field in ColumnRef,
     153             :          * or as the last indirection item in A_Indirection.
     154             :          */
     155     1288034 :         if (expand_star)
     156             :         {
     157     1274380 :             if (IsA(res->val, ColumnRef))
     158             :             {
     159      644978 :                 ColumnRef  *cref = (ColumnRef *) res->val;
     160             : 
     161      644978 :                 if (IsA(llast(cref->fields), A_Star))
     162             :                 {
     163             :                     /* It is something.*, expand into multiple items */
     164       33872 :                     p_target = list_concat(p_target,
     165       33876 :                                            ExpandColumnRefStar(pstate,
     166             :                                                                cref,
     167             :                                                                true));
     168       33872 :                     continue;
     169             :                 }
     170             :             }
     171      629402 :             else if (IsA(res->val, A_Indirection))
     172             :             {
     173        6084 :                 A_Indirection *ind = (A_Indirection *) res->val;
     174             : 
     175        6084 :                 if (IsA(llast(ind->indirection), A_Star))
     176             :                 {
     177             :                     /* It is something.*, expand into multiple items */
     178        3288 :                     p_target = list_concat(p_target,
     179        3288 :                                            ExpandIndirectionStar(pstate,
     180             :                                                                  ind,
     181             :                                                                  true,
     182             :                                                                  exprKind));
     183        3288 :                     continue;
     184             :                 }
     185             :             }
     186             :         }
     187             : 
     188             :         /*
     189             :          * Not "something.*", or we want to treat that as a plain whole-row
     190             :          * variable, so transform as a single expression
     191             :          */
     192     1249050 :         p_target = lappend(p_target,
     193     1250870 :                            transformTargetEntry(pstate,
     194             :                                                 res->val,
     195             :                                                 NULL,
     196             :                                                 exprKind,
     197             :                                                 res->name,
     198             :                                                 false));
     199             :     }
     200             : 
     201             :     /*
     202             :      * If any multiassign resjunk items were created, attach them to the end
     203             :      * of the targetlist.  This should only happen in an UPDATE tlist.  We
     204             :      * don't need to worry about numbering of these items; transformUpdateStmt
     205             :      * will set their resnos.
     206             :      */
     207      376360 :     if (pstate->p_multiassign_exprs)
     208             :     {
     209             :         Assert(exprKind == EXPR_KIND_UPDATE_SOURCE);
     210          62 :         p_target = list_concat(p_target, pstate->p_multiassign_exprs);
     211          62 :         pstate->p_multiassign_exprs = NIL;
     212             :     }
     213             : 
     214      376360 :     return p_target;
     215             : }
     216             : 
     217             : 
     218             : /*
     219             :  * transformExpressionList()
     220             :  *
     221             :  * This is the identical transformation to transformTargetList, except that
     222             :  * the input list elements are bare expressions without ResTarget decoration,
     223             :  * and the output elements are likewise just expressions without TargetEntry
     224             :  * decoration.  We use this for ROW() and VALUES() constructs.
     225             :  *
     226             :  * exprKind is not enough to tell us whether to allow SetToDefault, so
     227             :  * an additional flag is needed for that.
     228             :  */
     229             : List *
     230       67574 : transformExpressionList(ParseState *pstate, List *exprlist,
     231             :                         ParseExprKind exprKind, bool allowDefault)
     232             : {
     233       67574 :     List       *result = NIL;
     234             :     ListCell   *lc;
     235             : 
     236      240142 :     foreach(lc, exprlist)
     237             :     {
     238      172590 :         Node       *e = (Node *) lfirst(lc);
     239             : 
     240             :         /*
     241             :          * Check for "something.*".  Depending on the complexity of the
     242             :          * "something", the star could appear as the last field in ColumnRef,
     243             :          * or as the last indirection item in A_Indirection.
     244             :          */
     245      172590 :         if (IsA(e, ColumnRef))
     246             :         {
     247        8932 :             ColumnRef  *cref = (ColumnRef *) e;
     248             : 
     249        8932 :             if (IsA(llast(cref->fields), A_Star))
     250             :             {
     251             :                 /* It is something.*, expand into multiple items */
     252         152 :                 result = list_concat(result,
     253         152 :                                      ExpandColumnRefStar(pstate, cref,
     254             :                                                          false));
     255         152 :                 continue;
     256             :             }
     257             :         }
     258      163658 :         else if (IsA(e, A_Indirection))
     259             :         {
     260          16 :             A_Indirection *ind = (A_Indirection *) e;
     261             : 
     262          16 :             if (IsA(llast(ind->indirection), A_Star))
     263             :             {
     264             :                 /* It is something.*, expand into multiple items */
     265           0 :                 result = list_concat(result,
     266           0 :                                      ExpandIndirectionStar(pstate, ind,
     267             :                                                            false, exprKind));
     268           0 :                 continue;
     269             :             }
     270             :         }
     271             : 
     272             :         /*
     273             :          * Not "something.*", so transform as a single expression.  If it's a
     274             :          * SetToDefault node and we should allow that, pass it through
     275             :          * unmodified.  (transformExpr will throw the appropriate error if
     276             :          * we're disallowing it.)
     277             :          */
     278      172438 :         if (allowDefault && IsA(e, SetToDefault))
     279             :              /* do nothing */ ;
     280             :         else
     281      171832 :             e = transformExpr(pstate, e, exprKind);
     282             : 
     283      172416 :         result = lappend(result, e);
     284             :     }
     285             : 
     286             :     /* Shouldn't have any multiassign items here */
     287             :     Assert(pstate->p_multiassign_exprs == NIL);
     288             : 
     289       67552 :     return result;
     290             : }
     291             : 
     292             : 
     293             : /*
     294             :  * resolveTargetListUnknowns()
     295             :  *      Convert any unknown-type targetlist entries to type TEXT.
     296             :  *
     297             :  * We do this after we've exhausted all other ways of identifying the output
     298             :  * column types of a query.
     299             :  */
     300             : void
     301      323648 : resolveTargetListUnknowns(ParseState *pstate, List *targetlist)
     302             : {
     303             :     ListCell   *l;
     304             : 
     305     1542740 :     foreach(l, targetlist)
     306             :     {
     307     1219092 :         TargetEntry *tle = (TargetEntry *) lfirst(l);
     308     1219092 :         Oid         restype = exprType((Node *) tle->expr);
     309             : 
     310     1219092 :         if (restype == UNKNOWNOID)
     311             :         {
     312        8776 :             tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
     313             :                                              restype, TEXTOID, -1,
     314             :                                              COERCION_IMPLICIT,
     315             :                                              COERCE_IMPLICIT_CAST,
     316             :                                              -1);
     317             :         }
     318             :     }
     319      323648 : }
     320             : 
     321             : 
     322             : /*
     323             :  * markTargetListOrigins()
     324             :  *      Mark targetlist columns that are simple Vars with the source
     325             :  *      table's OID and column number.
     326             :  *
     327             :  * Currently, this is done only for SELECT targetlists and RETURNING lists,
     328             :  * since we only need the info if we are going to send it to the frontend.
     329             :  */
     330             : void
     331      365698 : markTargetListOrigins(ParseState *pstate, List *targetlist)
     332             : {
     333             :     ListCell   *l;
     334             : 
     335     1765702 :     foreach(l, targetlist)
     336             :     {
     337     1400004 :         TargetEntry *tle = (TargetEntry *) lfirst(l);
     338             : 
     339     1400004 :         markTargetListOrigin(pstate, tle, (Var *) tle->expr, 0);
     340             :     }
     341      365698 : }
     342             : 
     343             : /*
     344             :  * markTargetListOrigin()
     345             :  *      If 'var' is a Var of a plain relation, mark 'tle' with its origin
     346             :  *
     347             :  * levelsup is an extra offset to interpret the Var's varlevelsup correctly.
     348             :  *
     349             :  * Note that we do not drill down into views, but report the view as the
     350             :  * column owner.  There's also no need to drill down into joins: if we see
     351             :  * a join alias Var, it must be a merged JOIN USING column (or possibly a
     352             :  * whole-row Var); that is not a direct reference to any plain table column,
     353             :  * so we don't report it.
     354             :  */
     355             : static void
     356     1400004 : markTargetListOrigin(ParseState *pstate, TargetEntry *tle,
     357             :                      Var *var, int levelsup)
     358             : {
     359             :     int         netlevelsup;
     360             :     RangeTblEntry *rte;
     361             :     AttrNumber  attnum;
     362             : 
     363     1400004 :     if (var == NULL || !IsA(var, Var))
     364      643072 :         return;
     365      756932 :     netlevelsup = var->varlevelsup + levelsup;
     366      756932 :     rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
     367      756932 :     attnum = var->varattno;
     368             : 
     369      756932 :     switch (rte->rtekind)
     370             :     {
     371      625710 :         case RTE_RELATION:
     372             :             /* It's a table or view, report it */
     373      625710 :             tle->resorigtbl = rte->relid;
     374      625710 :             tle->resorigcol = attnum;
     375      625710 :             break;
     376       13536 :         case RTE_SUBQUERY:
     377             :             /* Subselect-in-FROM: copy up from the subselect */
     378       13536 :             if (attnum != InvalidAttrNumber)
     379             :             {
     380       13496 :                 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
     381             :                                                     attnum);
     382             : 
     383       13496 :                 if (ste == NULL || ste->resjunk)
     384           0 :                     elog(ERROR, "subquery %s does not have attribute %d",
     385             :                          rte->eref->aliasname, attnum);
     386       13496 :                 tle->resorigtbl = ste->resorigtbl;
     387       13496 :                 tle->resorigcol = ste->resorigcol;
     388             :             }
     389       13536 :             break;
     390      115346 :         case RTE_JOIN:
     391             :         case RTE_FUNCTION:
     392             :         case RTE_VALUES:
     393             :         case RTE_TABLEFUNC:
     394             :         case RTE_NAMEDTUPLESTORE:
     395             :         case RTE_RESULT:
     396             :             /* not a simple relation, leave it unmarked */
     397      115346 :             break;
     398        2340 :         case RTE_CTE:
     399             : 
     400             :             /*
     401             :              * CTE reference: copy up from the subquery, if possible. If the
     402             :              * RTE is a recursive self-reference then we can't do anything
     403             :              * because we haven't finished analyzing it yet. However, it's no
     404             :              * big loss because we must be down inside the recursive term of a
     405             :              * recursive CTE, and so any markings on the current targetlist
     406             :              * are not going to affect the results anyway.
     407             :              */
     408        2340 :             if (attnum != InvalidAttrNumber && !rte->self_reference)
     409             :             {
     410        2116 :                 CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
     411             :                 TargetEntry *ste;
     412             : 
     413        2116 :                 ste = get_tle_by_resno(GetCTETargetList(cte), attnum);
     414        2116 :                 if (ste == NULL || ste->resjunk)
     415           0 :                     elog(ERROR, "subquery %s does not have attribute %d",
     416             :                          rte->eref->aliasname, attnum);
     417        2116 :                 tle->resorigtbl = ste->resorigtbl;
     418        2116 :                 tle->resorigcol = ste->resorigcol;
     419             :             }
     420        2340 :             break;
     421             :     }
     422             : }
     423             : 
     424             : 
     425             : /*
     426             :  * transformAssignedExpr()
     427             :  *  This is used in INSERT and UPDATE statements only.  It prepares an
     428             :  *  expression for assignment to a column of the target table.
     429             :  *  This includes coercing the given value to the target column's type
     430             :  *  (if necessary), and dealing with any subfield names or subscripts
     431             :  *  attached to the target column itself.  The input expression has
     432             :  *  already been through transformExpr().
     433             :  *
     434             :  * pstate       parse state
     435             :  * expr         expression to be modified
     436             :  * exprKind     indicates which type of statement we're dealing with
     437             :  * colname      target column name (ie, name of attribute to be assigned to)
     438             :  * attrno       target attribute number
     439             :  * indirection  subscripts/field names for target column, if any
     440             :  * location     error cursor position for the target column, or -1
     441             :  *
     442             :  * Returns the modified expression.
     443             :  *
     444             :  * Note: location points at the target column name (SET target or INSERT
     445             :  * column name list entry), and must therefore be -1 in an INSERT that
     446             :  * omits the column name list.  So we should usually prefer to use
     447             :  * exprLocation(expr) for errors that can happen in a default INSERT.
     448             :  */
     449             : Expr *
     450      241494 : transformAssignedExpr(ParseState *pstate,
     451             :                       Expr *expr,
     452             :                       ParseExprKind exprKind,
     453             :                       const char *colname,
     454             :                       int attrno,
     455             :                       List *indirection,
     456             :                       int location)
     457             : {
     458      241494 :     Relation    rd = pstate->p_target_relation;
     459             :     Oid         type_id;        /* type of value provided */
     460             :     Oid         attrtype;       /* type of target column */
     461             :     int32       attrtypmod;
     462             :     Oid         attrcollation;  /* collation of target column */
     463             :     ParseExprKind sv_expr_kind;
     464             : 
     465             :     /*
     466             :      * Save and restore identity of expression type we're parsing.  We must
     467             :      * set p_expr_kind here because we can parse subscripts without going
     468             :      * through transformExpr().
     469             :      */
     470             :     Assert(exprKind != EXPR_KIND_NONE);
     471      241494 :     sv_expr_kind = pstate->p_expr_kind;
     472      241494 :     pstate->p_expr_kind = exprKind;
     473             : 
     474             :     Assert(rd != NULL);
     475      241494 :     if (attrno <= 0)
     476           0 :         ereport(ERROR,
     477             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     478             :                  errmsg("cannot assign to system column \"%s\"",
     479             :                         colname),
     480             :                  parser_errposition(pstate, location)));
     481      241494 :     attrtype = attnumTypeId(rd, attrno);
     482      241494 :     attrtypmod = TupleDescAttr(rd->rd_att, attrno - 1)->atttypmod;
     483      241494 :     attrcollation = TupleDescAttr(rd->rd_att, attrno - 1)->attcollation;
     484             : 
     485             :     /*
     486             :      * If the expression is a DEFAULT placeholder, insert the attribute's
     487             :      * type/typmod/collation into it so that exprType etc will report the
     488             :      * right things.  (We expect that the eventually substituted default
     489             :      * expression will in fact have this type and typmod.  The collation
     490             :      * likely doesn't matter, but let's set it correctly anyway.)  Also,
     491             :      * reject trying to update a subfield or array element with DEFAULT, since
     492             :      * there can't be any default for portions of a column.
     493             :      */
     494      241494 :     if (expr && IsA(expr, SetToDefault))
     495             :     {
     496         694 :         SetToDefault *def = (SetToDefault *) expr;
     497             : 
     498         694 :         def->typeId = attrtype;
     499         694 :         def->typeMod = attrtypmod;
     500         694 :         def->collation = attrcollation;
     501         694 :         if (indirection)
     502             :         {
     503           8 :             if (IsA(linitial(indirection), A_Indices))
     504           4 :                 ereport(ERROR,
     505             :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     506             :                          errmsg("cannot set an array element to DEFAULT"),
     507             :                          parser_errposition(pstate, location)));
     508             :             else
     509           4 :                 ereport(ERROR,
     510             :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     511             :                          errmsg("cannot set a subfield to DEFAULT"),
     512             :                          parser_errposition(pstate, location)));
     513             :         }
     514             :     }
     515             : 
     516             :     /* Now we can use exprType() safely. */
     517      241486 :     type_id = exprType((Node *) expr);
     518             : 
     519             :     /*
     520             :      * If there is indirection on the target column, prepare an array or
     521             :      * subfield assignment expression.  This will generate a new column value
     522             :      * that the source value has been inserted into, which can then be placed
     523             :      * in the new tuple constructed by INSERT or UPDATE.
     524             :      */
     525      241486 :     if (indirection)
     526             :     {
     527             :         Node       *colVar;
     528             : 
     529         714 :         if (pstate->p_is_insert)
     530             :         {
     531             :             /*
     532             :              * The command is INSERT INTO table (col.something) ... so there
     533             :              * is not really a source value to work with. Insert a NULL
     534             :              * constant as the source value.
     535             :              */
     536         368 :             colVar = (Node *) makeNullConst(attrtype, attrtypmod,
     537             :                                             attrcollation);
     538             :         }
     539             :         else
     540             :         {
     541             :             /*
     542             :              * Build a Var for the column to be updated.
     543             :              */
     544             :             Var        *var;
     545             : 
     546         346 :             var = makeVar(pstate->p_target_nsitem->p_rtindex, attrno,
     547             :                           attrtype, attrtypmod, attrcollation, 0);
     548         346 :             var->location = location;
     549             : 
     550         346 :             colVar = (Node *) var;
     551             :         }
     552             : 
     553             :         expr = (Expr *)
     554         714 :             transformAssignmentIndirection(pstate,
     555             :                                            colVar,
     556             :                                            colname,
     557             :                                            false,
     558             :                                            attrtype,
     559             :                                            attrtypmod,
     560             :                                            attrcollation,
     561             :                                            indirection,
     562             :                                            list_head(indirection),
     563             :                                            (Node *) expr,
     564             :                                            location);
     565             :     }
     566             :     else
     567             :     {
     568             :         /*
     569             :          * For normal non-qualified target column, do type checking and
     570             :          * coercion.
     571             :          */
     572      240772 :         Node       *orig_expr = (Node *) expr;
     573             : 
     574             :         expr = (Expr *)
     575      240772 :             coerce_to_target_type(pstate,
     576             :                                   orig_expr, type_id,
     577             :                                   attrtype, attrtypmod,
     578             :                                   COERCION_ASSIGNMENT,
     579             :                                   COERCE_IMPLICIT_CAST,
     580             :                                   -1);
     581      240170 :         if (expr == NULL)
     582         132 :             ereport(ERROR,
     583             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
     584             :                      errmsg("column \"%s\" is of type %s"
     585             :                             " but expression is of type %s",
     586             :                             colname,
     587             :                             format_type_be(attrtype),
     588             :                             format_type_be(type_id)),
     589             :                      errhint("You will need to rewrite or cast the expression."),
     590             :                      parser_errposition(pstate, exprLocation(orig_expr))));
     591             :     }
     592             : 
     593      240752 :     pstate->p_expr_kind = sv_expr_kind;
     594             : 
     595      240752 :     return expr;
     596             : }
     597             : 
     598             : 
     599             : /*
     600             :  * updateTargetListEntry()
     601             :  *  This is used in UPDATE statements (and ON CONFLICT DO UPDATE)
     602             :  *  only.  It prepares an UPDATE TargetEntry for assignment to a
     603             :  *  column of the target table.  This includes coercing the given
     604             :  *  value to the target column's type (if necessary), and dealing with
     605             :  *  any subfield names or subscripts attached to the target column
     606             :  *  itself.
     607             :  *
     608             :  * pstate       parse state
     609             :  * tle          target list entry to be modified
     610             :  * colname      target column name (ie, name of attribute to be assigned to)
     611             :  * attrno       target attribute number
     612             :  * indirection  subscripts/field names for target column, if any
     613             :  * location     error cursor position (should point at column name), or -1
     614             :  */
     615             : void
     616       13618 : updateTargetListEntry(ParseState *pstate,
     617             :                       TargetEntry *tle,
     618             :                       char *colname,
     619             :                       int attrno,
     620             :                       List *indirection,
     621             :                       int location)
     622             : {
     623             :     /* Fix up expression as needed */
     624       13618 :     tle->expr = transformAssignedExpr(pstate,
     625             :                                       tle->expr,
     626             :                                       EXPR_KIND_UPDATE_TARGET,
     627             :                                       colname,
     628             :                                       attrno,
     629             :                                       indirection,
     630             :                                       location);
     631             : 
     632             :     /*
     633             :      * Set the resno to identify the target column --- the rewriter and
     634             :      * planner depend on this.  We also set the resname to identify the target
     635             :      * column, but this is only for debugging purposes; it should not be
     636             :      * relied on.  (In particular, it might be out of date in a stored rule.)
     637             :      */
     638       13614 :     tle->resno = (AttrNumber) attrno;
     639       13614 :     tle->resname = colname;
     640       13614 : }
     641             : 
     642             : 
     643             : /*
     644             :  * Process indirection (field selection or subscripting) of the target
     645             :  * column in INSERT/UPDATE.  This routine recurses for multiple levels
     646             :  * of indirection --- but note that several adjacent A_Indices nodes in
     647             :  * the indirection list are treated as a single multidimensional subscript
     648             :  * operation.
     649             :  *
     650             :  * In the initial call, basenode is a Var for the target column in UPDATE,
     651             :  * or a null Const of the target's type in INSERT.  In recursive calls,
     652             :  * basenode is NULL, indicating that a substitute node should be consed up if
     653             :  * needed.
     654             :  *
     655             :  * targetName is the name of the field or subfield we're assigning to, and
     656             :  * targetIsSubscripting is true if we're subscripting it.  These are just for
     657             :  * error reporting.
     658             :  *
     659             :  * targetTypeId, targetTypMod, targetCollation indicate the datatype and
     660             :  * collation of the object to be assigned to (initially the target column,
     661             :  * later some subobject).
     662             :  *
     663             :  * indirection is the list of indirection nodes, and indirection_cell is the
     664             :  * start of the sublist remaining to process.  When it's NULL, we're done
     665             :  * recursing and can just coerce and return the RHS.
     666             :  *
     667             :  * rhs is the already-transformed value to be assigned; note it has not been
     668             :  * coerced to any particular type.
     669             :  *
     670             :  * location is the cursor error position for any errors.  (Note: this points
     671             :  * to the head of the target clause, eg "foo" in "foo.bar[baz]".  Later we
     672             :  * might want to decorate indirection cells with their own location info,
     673             :  * in which case the location argument could probably be dropped.)
     674             :  */
     675             : static Node *
     676        1676 : transformAssignmentIndirection(ParseState *pstate,
     677             :                                Node *basenode,
     678             :                                const char *targetName,
     679             :                                bool targetIsSubscripting,
     680             :                                Oid targetTypeId,
     681             :                                int32 targetTypMod,
     682             :                                Oid targetCollation,
     683             :                                List *indirection,
     684             :                                ListCell *indirection_cell,
     685             :                                Node *rhs,
     686             :                                int location)
     687             : {
     688             :     Node       *result;
     689        1676 :     List       *subscripts = NIL;
     690        1676 :     bool        isSlice = false;
     691             :     ListCell   *i;
     692             : 
     693        1676 :     if (indirection_cell && !basenode)
     694             :     {
     695             :         /*
     696             :          * Set up a substitution.  We abuse CaseTestExpr for this.  It's safe
     697             :          * to do so because the only nodes that will be above the CaseTestExpr
     698             :          * in the finished expression will be FieldStore and SubscriptingRef
     699             :          * nodes. (There could be other stuff in the tree, but it will be
     700             :          * within other child fields of those node types.)
     701             :          */
     702         248 :         CaseTestExpr *ctest = makeNode(CaseTestExpr);
     703             : 
     704         248 :         ctest->typeId = targetTypeId;
     705         248 :         ctest->typeMod = targetTypMod;
     706         248 :         ctest->collation = targetCollation;
     707         248 :         basenode = (Node *) ctest;
     708             :     }
     709             : 
     710             :     /*
     711             :      * We have to split any field-selection operations apart from
     712             :      * subscripting.  Adjacent A_Indices nodes have to be treated as a single
     713             :      * multidimensional subscript operation.
     714             :      */
     715        2374 :     for_each_cell(i, indirection, indirection_cell)
     716             :     {
     717        1138 :         Node       *n = lfirst(i);
     718             : 
     719        1138 :         if (IsA(n, A_Indices))
     720             :         {
     721         698 :             subscripts = lappend(subscripts, n);
     722         698 :             if (((A_Indices *) n)->is_slice)
     723         176 :                 isSlice = true;
     724             :         }
     725         440 :         else if (IsA(n, A_Star))
     726             :         {
     727           0 :             ereport(ERROR,
     728             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     729             :                      errmsg("row expansion via \"*\" is not supported here"),
     730             :                      parser_errposition(pstate, location)));
     731             :         }
     732             :         else
     733             :         {
     734             :             FieldStore *fstore;
     735             :             Oid         baseTypeId;
     736             :             int32       baseTypeMod;
     737             :             Oid         typrelid;
     738             :             AttrNumber  attnum;
     739             :             Oid         fieldTypeId;
     740             :             int32       fieldTypMod;
     741             :             Oid         fieldCollation;
     742             : 
     743             :             Assert(IsA(n, String));
     744             : 
     745             :             /* process subscripts before this field selection */
     746         440 :             if (subscripts)
     747             :             {
     748             :                 /* recurse, and then return because we're done */
     749         136 :                 return transformAssignmentSubscripts(pstate,
     750             :                                                      basenode,
     751             :                                                      targetName,
     752             :                                                      targetTypeId,
     753             :                                                      targetTypMod,
     754             :                                                      targetCollation,
     755             :                                                      subscripts,
     756             :                                                      isSlice,
     757             :                                                      indirection,
     758             :                                                      i,
     759             :                                                      rhs,
     760             :                                                      location);
     761             :             }
     762             : 
     763             :             /* No subscripts, so can process field selection here */
     764             : 
     765             :             /*
     766             :              * Look up the composite type, accounting for possibility that
     767             :              * what we are given is a domain over composite.
     768             :              */
     769         304 :             baseTypeMod = targetTypMod;
     770         304 :             baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
     771             : 
     772         304 :             typrelid = typeidTypeRelid(baseTypeId);
     773         304 :             if (!typrelid)
     774           0 :                 ereport(ERROR,
     775             :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
     776             :                          errmsg("cannot assign to field \"%s\" of column \"%s\" because its type %s is not a composite type",
     777             :                                 strVal(n), targetName,
     778             :                                 format_type_be(targetTypeId)),
     779             :                          parser_errposition(pstate, location)));
     780             : 
     781         304 :             attnum = get_attnum(typrelid, strVal(n));
     782         304 :             if (attnum == InvalidAttrNumber)
     783           0 :                 ereport(ERROR,
     784             :                         (errcode(ERRCODE_UNDEFINED_COLUMN),
     785             :                          errmsg("cannot assign to field \"%s\" of column \"%s\" because there is no such column in data type %s",
     786             :                                 strVal(n), targetName,
     787             :                                 format_type_be(targetTypeId)),
     788             :                          parser_errposition(pstate, location)));
     789         304 :             if (attnum < 0)
     790           0 :                 ereport(ERROR,
     791             :                         (errcode(ERRCODE_UNDEFINED_COLUMN),
     792             :                          errmsg("cannot assign to system column \"%s\"",
     793             :                                 strVal(n)),
     794             :                          parser_errposition(pstate, location)));
     795             : 
     796         304 :             get_atttypetypmodcoll(typrelid, attnum,
     797             :                                   &fieldTypeId, &fieldTypMod, &fieldCollation);
     798             : 
     799             :             /* recurse to create appropriate RHS for field assign */
     800         304 :             rhs = transformAssignmentIndirection(pstate,
     801             :                                                  NULL,
     802         304 :                                                  strVal(n),
     803             :                                                  false,
     804             :                                                  fieldTypeId,
     805             :                                                  fieldTypMod,
     806             :                                                  fieldCollation,
     807             :                                                  indirection,
     808             :                                                  lnext(indirection, i),
     809             :                                                  rhs,
     810             :                                                  location);
     811             : 
     812             :             /* and build a FieldStore node */
     813         304 :             fstore = makeNode(FieldStore);
     814         304 :             fstore->arg = (Expr *) basenode;
     815         304 :             fstore->newvals = list_make1(rhs);
     816         304 :             fstore->fieldnums = list_make1_int(attnum);
     817         304 :             fstore->resulttype = baseTypeId;
     818             : 
     819             :             /* If target is a domain, apply constraints */
     820         304 :             if (baseTypeId != targetTypeId)
     821          56 :                 return coerce_to_domain((Node *) fstore,
     822             :                                         baseTypeId, baseTypeMod,
     823             :                                         targetTypeId,
     824             :                                         COERCION_IMPLICIT,
     825             :                                         COERCE_IMPLICIT_CAST,
     826             :                                         location,
     827             :                                         false);
     828             : 
     829         248 :             return (Node *) fstore;
     830             :         }
     831             :     }
     832             : 
     833             :     /* process trailing subscripts, if any */
     834        1236 :     if (subscripts)
     835             :     {
     836             :         /* recurse, and then return because we're done */
     837         522 :         return transformAssignmentSubscripts(pstate,
     838             :                                              basenode,
     839             :                                              targetName,
     840             :                                              targetTypeId,
     841             :                                              targetTypMod,
     842             :                                              targetCollation,
     843             :                                              subscripts,
     844             :                                              isSlice,
     845             :                                              indirection,
     846             :                                              NULL,
     847             :                                              rhs,
     848             :                                              location);
     849             :     }
     850             : 
     851             :     /* base case: just coerce RHS to match target type ID */
     852             : 
     853         714 :     result = coerce_to_target_type(pstate,
     854             :                                    rhs, exprType(rhs),
     855             :                                    targetTypeId, targetTypMod,
     856             :                                    COERCION_ASSIGNMENT,
     857             :                                    COERCE_IMPLICIT_CAST,
     858             :                                    -1);
     859         714 :     if (result == NULL)
     860             :     {
     861           0 :         if (targetIsSubscripting)
     862           0 :             ereport(ERROR,
     863             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
     864             :                      errmsg("array assignment to \"%s\" requires type %s"
     865             :                             " but expression is of type %s",
     866             :                             targetName,
     867             :                             format_type_be(targetTypeId),
     868             :                             format_type_be(exprType(rhs))),
     869             :                      errhint("You will need to rewrite or cast the expression."),
     870             :                      parser_errposition(pstate, location)));
     871             :         else
     872           0 :             ereport(ERROR,
     873             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
     874             :                      errmsg("subfield \"%s\" is of type %s"
     875             :                             " but expression is of type %s",
     876             :                             targetName,
     877             :                             format_type_be(targetTypeId),
     878             :                             format_type_be(exprType(rhs))),
     879             :                      errhint("You will need to rewrite or cast the expression."),
     880             :                      parser_errposition(pstate, location)));
     881             :     }
     882             : 
     883         714 :     return result;
     884             : }
     885             : 
     886             : /*
     887             :  * helper for transformAssignmentIndirection: process container assignment
     888             :  */
     889             : static Node *
     890         658 : transformAssignmentSubscripts(ParseState *pstate,
     891             :                               Node *basenode,
     892             :                               const char *targetName,
     893             :                               Oid targetTypeId,
     894             :                               int32 targetTypMod,
     895             :                               Oid targetCollation,
     896             :                               List *subscripts,
     897             :                               bool isSlice,
     898             :                               List *indirection,
     899             :                               ListCell *next_indirection,
     900             :                               Node *rhs,
     901             :                               int location)
     902             : {
     903             :     Node       *result;
     904             :     Oid         containerType;
     905             :     int32       containerTypMod;
     906             :     Oid         elementTypeId;
     907             :     Oid         typeNeeded;
     908             :     Oid         collationNeeded;
     909             : 
     910             :     Assert(subscripts != NIL);
     911             : 
     912             :     /* Identify the actual array type and element type involved */
     913         658 :     containerType = targetTypeId;
     914         658 :     containerTypMod = targetTypMod;
     915         658 :     elementTypeId = transformContainerType(&containerType, &containerTypMod);
     916             : 
     917             :     /* Identify type that RHS must provide */
     918         658 :     typeNeeded = isSlice ? containerType : elementTypeId;
     919             : 
     920             :     /*
     921             :      * container normally has same collation as elements, but there's an
     922             :      * exception: we might be subscripting a domain over a container type. In
     923             :      * that case use collation of the base type.
     924             :      */
     925         658 :     if (containerType == targetTypeId)
     926         570 :         collationNeeded = targetCollation;
     927             :     else
     928          88 :         collationNeeded = get_typcollation(containerType);
     929             : 
     930             :     /* recurse to create appropriate RHS for container assign */
     931         658 :     rhs = transformAssignmentIndirection(pstate,
     932             :                                          NULL,
     933             :                                          targetName,
     934             :                                          true,
     935             :                                          typeNeeded,
     936             :                                          containerTypMod,
     937             :                                          collationNeeded,
     938             :                                          indirection,
     939             :                                          next_indirection,
     940             :                                          rhs,
     941             :                                          location);
     942             : 
     943             :     /* process subscripts */
     944         658 :     result = (Node *) transformContainerSubscripts(pstate,
     945             :                                                    basenode,
     946             :                                                    containerType,
     947             :                                                    elementTypeId,
     948             :                                                    containerTypMod,
     949             :                                                    subscripts,
     950             :                                                    rhs);
     951             : 
     952             :     /* If target was a domain over container, need to coerce up to the domain */
     953         658 :     if (containerType != targetTypeId)
     954             :     {
     955          88 :         Oid         resulttype = exprType(result);
     956             : 
     957          88 :         result = coerce_to_target_type(pstate,
     958             :                                        result, resulttype,
     959             :                                        targetTypeId, targetTypMod,
     960             :                                        COERCION_ASSIGNMENT,
     961             :                                        COERCE_IMPLICIT_CAST,
     962             :                                        -1);
     963             :         /* can fail if we had int2vector/oidvector, but not for true domains */
     964          88 :         if (result == NULL)
     965           0 :             ereport(ERROR,
     966             :                     (errcode(ERRCODE_CANNOT_COERCE),
     967             :                      errmsg("cannot cast type %s to %s",
     968             :                             format_type_be(resulttype),
     969             :                             format_type_be(targetTypeId)),
     970             :                      parser_errposition(pstate, location)));
     971             :     }
     972             : 
     973         658 :     return result;
     974             : }
     975             : 
     976             : 
     977             : /*
     978             :  * checkInsertTargets -
     979             :  *    generate a list of INSERT column targets if not supplied, or
     980             :  *    test supplied column names to make sure they are in target table.
     981             :  *    Also return an integer list of the columns' attribute numbers.
     982             :  */
     983             : List *
     984       60890 : checkInsertTargets(ParseState *pstate, List *cols, List **attrnos)
     985             : {
     986       60890 :     *attrnos = NIL;
     987             : 
     988       60890 :     if (cols == NIL)
     989             :     {
     990             :         /*
     991             :          * Generate default column list for INSERT.
     992             :          */
     993       49310 :         int         numcol = RelationGetNumberOfAttributes(pstate->p_target_relation);
     994             : 
     995             :         int         i;
     996             : 
     997      230596 :         for (i = 0; i < numcol; i++)
     998             :         {
     999             :             ResTarget  *col;
    1000             :             Form_pg_attribute attr;
    1001             : 
    1002      181286 :             attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
    1003             : 
    1004      181286 :             if (attr->attisdropped)
    1005         178 :                 continue;
    1006             : 
    1007      181108 :             col = makeNode(ResTarget);
    1008      181108 :             col->name = pstrdup(NameStr(attr->attname));
    1009      181108 :             col->indirection = NIL;
    1010      181108 :             col->val = NULL;
    1011      181108 :             col->location = -1;
    1012      181108 :             cols = lappend(cols, col);
    1013      181108 :             *attrnos = lappend_int(*attrnos, i + 1);
    1014             :         }
    1015             :     }
    1016             :     else
    1017             :     {
    1018             :         /*
    1019             :          * Do initial validation of user-supplied INSERT column list.
    1020             :          */
    1021       11580 :         Bitmapset  *wholecols = NULL;
    1022       11580 :         Bitmapset  *partialcols = NULL;
    1023             :         ListCell   *tl;
    1024             : 
    1025       46432 :         foreach(tl, cols)
    1026             :         {
    1027       34884 :             ResTarget  *col = (ResTarget *) lfirst(tl);
    1028       34884 :             char       *name = col->name;
    1029             :             int         attrno;
    1030             : 
    1031             :             /* Lookup column name, ereport on failure */
    1032       34884 :             attrno = attnameAttNum(pstate->p_target_relation, name, false);
    1033       34884 :             if (attrno == InvalidAttrNumber)
    1034          32 :                 ereport(ERROR,
    1035             :                         (errcode(ERRCODE_UNDEFINED_COLUMN),
    1036             :                          errmsg("column \"%s\" of relation \"%s\" does not exist",
    1037             :                                 name,
    1038             :                                 RelationGetRelationName(pstate->p_target_relation)),
    1039             :                          parser_errposition(pstate, col->location)));
    1040             : 
    1041             :             /*
    1042             :              * Check for duplicates, but only of whole columns --- we allow
    1043             :              * INSERT INTO foo (col.subcol1, col.subcol2)
    1044             :              */
    1045       34852 :             if (col->indirection == NIL)
    1046             :             {
    1047             :                 /* whole column; must not have any other assignment */
    1048       69192 :                 if (bms_is_member(attrno, wholecols) ||
    1049       34596 :                     bms_is_member(attrno, partialcols))
    1050           0 :                     ereport(ERROR,
    1051             :                             (errcode(ERRCODE_DUPLICATE_COLUMN),
    1052             :                              errmsg("column \"%s\" specified more than once",
    1053             :                                     name),
    1054             :                              parser_errposition(pstate, col->location)));
    1055       34596 :                 wholecols = bms_add_member(wholecols, attrno);
    1056             :             }
    1057             :             else
    1058             :             {
    1059             :                 /* partial column; must not have any whole assignment */
    1060         256 :                 if (bms_is_member(attrno, wholecols))
    1061           0 :                     ereport(ERROR,
    1062             :                             (errcode(ERRCODE_DUPLICATE_COLUMN),
    1063             :                              errmsg("column \"%s\" specified more than once",
    1064             :                                     name),
    1065             :                              parser_errposition(pstate, col->location)));
    1066         256 :                 partialcols = bms_add_member(partialcols, attrno);
    1067             :             }
    1068             : 
    1069       34852 :             *attrnos = lappend_int(*attrnos, attrno);
    1070             :         }
    1071             :     }
    1072             : 
    1073       60858 :     return cols;
    1074             : }
    1075             : 
    1076             : /*
    1077             :  * ExpandColumnRefStar()
    1078             :  *      Transforms foo.* into a list of expressions or targetlist entries.
    1079             :  *
    1080             :  * This handles the case where '*' appears as the last or only item in a
    1081             :  * ColumnRef.  The code is shared between the case of foo.* at the top level
    1082             :  * in a SELECT target list (where we want TargetEntry nodes in the result)
    1083             :  * and foo.* in a ROW() or VALUES() construct (where we want just bare
    1084             :  * expressions).
    1085             :  *
    1086             :  * The referenced columns are marked as requiring SELECT access.
    1087             :  */
    1088             : static List *
    1089       34028 : ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref,
    1090             :                     bool make_target_entry)
    1091             : {
    1092       34028 :     List       *fields = cref->fields;
    1093       34028 :     int         numnames = list_length(fields);
    1094             : 
    1095       34028 :     if (numnames == 1)
    1096             :     {
    1097             :         /*
    1098             :          * Target item is a bare '*', expand all tables
    1099             :          *
    1100             :          * (e.g., SELECT * FROM emp, dept)
    1101             :          *
    1102             :          * Since the grammar only accepts bare '*' at top level of SELECT, we
    1103             :          * need not handle the make_target_entry==false case here.
    1104             :          */
    1105             :         Assert(make_target_entry);
    1106       32460 :         return ExpandAllTables(pstate, cref->location);
    1107             :     }
    1108             :     else
    1109             :     {
    1110             :         /*
    1111             :          * Target item is relation.*, expand that table
    1112             :          *
    1113             :          * (e.g., SELECT emp.*, dname FROM emp, dept)
    1114             :          *
    1115             :          * Note: this code is a lot like transformColumnRef; it's tempting to
    1116             :          * call that instead and then replace the resulting whole-row Var with
    1117             :          * a list of Vars.  However, that would leave us with the RTE's
    1118             :          * selectedCols bitmap showing the whole row as needing select
    1119             :          * permission, as well as the individual columns.  That would be
    1120             :          * incorrect (since columns added later shouldn't need select
    1121             :          * permissions).  We could try to remove the whole-row permission bit
    1122             :          * after the fact, but duplicating code is less messy.
    1123             :          */
    1124        1568 :         char       *nspname = NULL;
    1125        1568 :         char       *relname = NULL;
    1126        1568 :         ParseNamespaceItem *nsitem = NULL;
    1127             :         int         levels_up;
    1128             :         enum
    1129             :         {
    1130             :             CRSERR_NO_RTE,
    1131             :             CRSERR_WRONG_DB,
    1132             :             CRSERR_TOO_MANY
    1133        1568 :         }           crserr = CRSERR_NO_RTE;
    1134             : 
    1135             :         /*
    1136             :          * Give the PreParseColumnRefHook, if any, first shot.  If it returns
    1137             :          * non-null then we should use that expression.
    1138             :          */
    1139        1568 :         if (pstate->p_pre_columnref_hook != NULL)
    1140             :         {
    1141             :             Node       *node;
    1142             : 
    1143          26 :             node = pstate->p_pre_columnref_hook(pstate, cref);
    1144          26 :             if (node != NULL)
    1145           0 :                 return ExpandRowReference(pstate, node, make_target_entry);
    1146             :         }
    1147             : 
    1148        1568 :         switch (numnames)
    1149             :         {
    1150        1568 :             case 2:
    1151        1568 :                 relname = strVal(linitial(fields));
    1152        1568 :                 nsitem = refnameNamespaceItem(pstate, nspname, relname,
    1153             :                                               cref->location,
    1154             :                                               &levels_up);
    1155        1568 :                 break;
    1156           0 :             case 3:
    1157           0 :                 nspname = strVal(linitial(fields));
    1158           0 :                 relname = strVal(lsecond(fields));
    1159           0 :                 nsitem = refnameNamespaceItem(pstate, nspname, relname,
    1160             :                                               cref->location,
    1161             :                                               &levels_up);
    1162           0 :                 break;
    1163           0 :             case 4:
    1164             :                 {
    1165           0 :                     char       *catname = strVal(linitial(fields));
    1166             : 
    1167             :                     /*
    1168             :                      * We check the catalog name and then ignore it.
    1169             :                      */
    1170           0 :                     if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
    1171             :                     {
    1172           0 :                         crserr = CRSERR_WRONG_DB;
    1173           0 :                         break;
    1174             :                     }
    1175           0 :                     nspname = strVal(lsecond(fields));
    1176           0 :                     relname = strVal(lthird(fields));
    1177           0 :                     nsitem = refnameNamespaceItem(pstate, nspname, relname,
    1178             :                                                   cref->location,
    1179             :                                                   &levels_up);
    1180           0 :                     break;
    1181             :                 }
    1182           0 :             default:
    1183           0 :                 crserr = CRSERR_TOO_MANY;
    1184           0 :                 break;
    1185             :         }
    1186             : 
    1187             :         /*
    1188             :          * Now give the PostParseColumnRefHook, if any, a chance. We cheat a
    1189             :          * bit by passing the RangeTblEntry, not a Var, as the planned
    1190             :          * translation.  (A single Var wouldn't be strictly correct anyway.
    1191             :          * This convention allows hooks that really care to know what is
    1192             :          * happening.  It might be better to pass the nsitem, but we'd have to
    1193             :          * promote that struct to a full-fledged Node type so that callees
    1194             :          * could identify its type.)
    1195             :          */
    1196        1568 :         if (pstate->p_post_columnref_hook != NULL)
    1197             :         {
    1198             :             Node       *node;
    1199             : 
    1200          54 :             node = pstate->p_post_columnref_hook(pstate, cref,
    1201             :                                                  (Node *) (nsitem ? nsitem->p_rte : NULL));
    1202          54 :             if (node != NULL)
    1203             :             {
    1204          54 :                 if (nsitem != NULL)
    1205           0 :                     ereport(ERROR,
    1206             :                             (errcode(ERRCODE_AMBIGUOUS_COLUMN),
    1207             :                              errmsg("column reference \"%s\" is ambiguous",
    1208             :                                     NameListToString(cref->fields)),
    1209             :                              parser_errposition(pstate, cref->location)));
    1210          54 :                 return ExpandRowReference(pstate, node, make_target_entry);
    1211             :             }
    1212             :         }
    1213             : 
    1214             :         /*
    1215             :          * Throw error if no translation found.
    1216             :          */
    1217        1514 :         if (nsitem == NULL)
    1218             :         {
    1219           4 :             switch (crserr)
    1220             :             {
    1221           4 :                 case CRSERR_NO_RTE:
    1222           4 :                     errorMissingRTE(pstate, makeRangeVar(nspname, relname,
    1223             :                                                          cref->location));
    1224             :                     break;
    1225           0 :                 case CRSERR_WRONG_DB:
    1226           0 :                     ereport(ERROR,
    1227             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1228             :                              errmsg("cross-database references are not implemented: %s",
    1229             :                                     NameListToString(cref->fields)),
    1230             :                              parser_errposition(pstate, cref->location)));
    1231             :                     break;
    1232           0 :                 case CRSERR_TOO_MANY:
    1233           0 :                     ereport(ERROR,
    1234             :                             (errcode(ERRCODE_SYNTAX_ERROR),
    1235             :                              errmsg("improper qualified name (too many dotted names): %s",
    1236             :                                     NameListToString(cref->fields)),
    1237             :                              parser_errposition(pstate, cref->location)));
    1238             :                     break;
    1239             :             }
    1240        1510 :         }
    1241             : 
    1242             :         /*
    1243             :          * OK, expand the nsitem into fields.
    1244             :          */
    1245        1510 :         return ExpandSingleTable(pstate, nsitem, levels_up, cref->location,
    1246             :                                  make_target_entry);
    1247             :     }
    1248             : }
    1249             : 
    1250             : /*
    1251             :  * ExpandAllTables()
    1252             :  *      Transforms '*' (in the target list) into a list of targetlist entries.
    1253             :  *
    1254             :  * tlist entries are generated for each relation visible for unqualified
    1255             :  * column name access.  We do not consider qualified-name-only entries because
    1256             :  * that would include input tables of aliasless JOINs, NEW/OLD pseudo-entries,
    1257             :  * etc.
    1258             :  *
    1259             :  * The referenced relations/columns are marked as requiring SELECT access.
    1260             :  */
    1261             : static List *
    1262       32460 : ExpandAllTables(ParseState *pstate, int location)
    1263             : {
    1264       32460 :     List       *target = NIL;
    1265       32460 :     bool        found_table = false;
    1266             :     ListCell   *l;
    1267             : 
    1268       67906 :     foreach(l, pstate->p_namespace)
    1269             :     {
    1270       35446 :         ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(l);
    1271             : 
    1272             :         /* Ignore table-only items */
    1273       35446 :         if (!nsitem->p_cols_visible)
    1274        2308 :             continue;
    1275             :         /* Should not have any lateral-only items when parsing targetlist */
    1276             :         Assert(!nsitem->p_lateral_only);
    1277             :         /* Remember we found a p_cols_visible item */
    1278       33138 :         found_table = true;
    1279             : 
    1280       33138 :         target = list_concat(target,
    1281       33138 :                              expandNSItemAttrs(pstate,
    1282             :                                                nsitem,
    1283             :                                                0,
    1284             :                                                location));
    1285             :     }
    1286             : 
    1287             :     /*
    1288             :      * Check for "SELECT *;".  We do it this way, rather than checking for
    1289             :      * target == NIL, because we want to allow SELECT * FROM a zero_column
    1290             :      * table.
    1291             :      */
    1292       32460 :     if (!found_table)
    1293           0 :         ereport(ERROR,
    1294             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1295             :                  errmsg("SELECT * with no tables specified is not valid"),
    1296             :                  parser_errposition(pstate, location)));
    1297             : 
    1298       32460 :     return target;
    1299             : }
    1300             : 
    1301             : /*
    1302             :  * ExpandIndirectionStar()
    1303             :  *      Transforms foo.* into a list of expressions or targetlist entries.
    1304             :  *
    1305             :  * This handles the case where '*' appears as the last item in A_Indirection.
    1306             :  * The code is shared between the case of foo.* at the top level in a SELECT
    1307             :  * target list (where we want TargetEntry nodes in the result) and foo.* in
    1308             :  * a ROW() or VALUES() construct (where we want just bare expressions).
    1309             :  * For robustness, we use a separate "make_target_entry" flag to control
    1310             :  * this rather than relying on exprKind.
    1311             :  */
    1312             : static List *
    1313        3288 : ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind,
    1314             :                       bool make_target_entry, ParseExprKind exprKind)
    1315             : {
    1316             :     Node       *expr;
    1317             : 
    1318             :     /* Strip off the '*' to create a reference to the rowtype object */
    1319        3288 :     ind = copyObject(ind);
    1320        3288 :     ind->indirection = list_truncate(ind->indirection,
    1321        3288 :                                      list_length(ind->indirection) - 1);
    1322             : 
    1323             :     /* And transform that */
    1324        3288 :     expr = transformExpr(pstate, (Node *) ind, exprKind);
    1325             : 
    1326             :     /* Expand the rowtype expression into individual fields */
    1327        3288 :     return ExpandRowReference(pstate, expr, make_target_entry);
    1328             : }
    1329             : 
    1330             : /*
    1331             :  * ExpandSingleTable()
    1332             :  *      Transforms foo.* into a list of expressions or targetlist entries.
    1333             :  *
    1334             :  * This handles the case where foo has been determined to be a simple
    1335             :  * reference to an RTE, so we can just generate Vars for the expressions.
    1336             :  *
    1337             :  * The referenced columns are marked as requiring SELECT access.
    1338             :  */
    1339             : static List *
    1340        1510 : ExpandSingleTable(ParseState *pstate, ParseNamespaceItem *nsitem,
    1341             :                   int sublevels_up, int location, bool make_target_entry)
    1342             : {
    1343        1510 :     if (make_target_entry)
    1344             :     {
    1345             :         /* expandNSItemAttrs handles permissions marking */
    1346        1410 :         return expandNSItemAttrs(pstate, nsitem, sublevels_up, location);
    1347             :     }
    1348             :     else
    1349             :     {
    1350         100 :         RangeTblEntry *rte = nsitem->p_rte;
    1351             :         List       *vars;
    1352             :         ListCell   *l;
    1353             : 
    1354         100 :         vars = expandNSItemVars(nsitem, sublevels_up, location, NULL);
    1355             : 
    1356             :         /*
    1357             :          * Require read access to the table.  This is normally redundant with
    1358             :          * the markVarForSelectPriv calls below, but not if the table has zero
    1359             :          * columns.
    1360             :          */
    1361         100 :         rte->requiredPerms |= ACL_SELECT;
    1362             : 
    1363             :         /* Require read access to each column */
    1364         268 :         foreach(l, vars)
    1365             :         {
    1366         168 :             Var        *var = (Var *) lfirst(l);
    1367             : 
    1368         168 :             markVarForSelectPriv(pstate, var, rte);
    1369             :         }
    1370             : 
    1371         100 :         return vars;
    1372             :     }
    1373             : }
    1374             : 
    1375             : /*
    1376             :  * ExpandRowReference()
    1377             :  *      Transforms foo.* into a list of expressions or targetlist entries.
    1378             :  *
    1379             :  * This handles the case where foo is an arbitrary expression of composite
    1380             :  * type.
    1381             :  */
    1382             : static List *
    1383        3342 : ExpandRowReference(ParseState *pstate, Node *expr,
    1384             :                    bool make_target_entry)
    1385             : {
    1386        3342 :     List       *result = NIL;
    1387             :     TupleDesc   tupleDesc;
    1388             :     int         numAttrs;
    1389             :     int         i;
    1390             : 
    1391             :     /*
    1392             :      * If the rowtype expression is a whole-row Var, we can expand the fields
    1393             :      * as simple Vars.  Note: if the RTE is a relation, this case leaves us
    1394             :      * with the RTE's selectedCols bitmap showing the whole row as needing
    1395             :      * select permission, as well as the individual columns.  However, we can
    1396             :      * only get here for weird notations like (table.*).*, so it's not worth
    1397             :      * trying to clean up --- arguably, the permissions marking is correct
    1398             :      * anyway for such cases.
    1399             :      */
    1400        3342 :     if (IsA(expr, Var) &&
    1401          34 :         ((Var *) expr)->varattno == InvalidAttrNumber)
    1402             :     {
    1403           0 :         Var        *var = (Var *) expr;
    1404             :         ParseNamespaceItem *nsitem;
    1405             : 
    1406           0 :         nsitem = GetNSItemByRangeTablePosn(pstate, var->varno, var->varlevelsup);
    1407           0 :         return ExpandSingleTable(pstate, nsitem, var->varlevelsup, var->location, make_target_entry);
    1408             :     }
    1409             : 
    1410             :     /*
    1411             :      * Otherwise we have to do it the hard way.  Our current implementation is
    1412             :      * to generate multiple copies of the expression and do FieldSelects.
    1413             :      * (This can be pretty inefficient if the expression involves nontrivial
    1414             :      * computation :-(.)
    1415             :      *
    1416             :      * Verify it's a composite type, and get the tupdesc.
    1417             :      * get_expr_result_tupdesc() handles this conveniently.
    1418             :      *
    1419             :      * If it's a Var of type RECORD, we have to work even harder: we have to
    1420             :      * find what the Var refers to, and pass that to get_expr_result_tupdesc.
    1421             :      * That task is handled by expandRecordVariable().
    1422             :      */
    1423        3342 :     if (IsA(expr, Var) &&
    1424          34 :         ((Var *) expr)->vartype == RECORDOID)
    1425           4 :         tupleDesc = expandRecordVariable(pstate, (Var *) expr, 0);
    1426             :     else
    1427        3338 :         tupleDesc = get_expr_result_tupdesc(expr, false);
    1428             :     Assert(tupleDesc);
    1429             : 
    1430             :     /* Generate a list of references to the individual fields */
    1431        3342 :     numAttrs = tupleDesc->natts;
    1432       16642 :     for (i = 0; i < numAttrs; i++)
    1433             :     {
    1434       13300 :         Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
    1435             :         FieldSelect *fselect;
    1436             : 
    1437       13300 :         if (att->attisdropped)
    1438           8 :             continue;
    1439             : 
    1440       13292 :         fselect = makeNode(FieldSelect);
    1441       13292 :         fselect->arg = (Expr *) copyObject(expr);
    1442       13292 :         fselect->fieldnum = i + 1;
    1443       13292 :         fselect->resulttype = att->atttypid;
    1444       13292 :         fselect->resulttypmod = att->atttypmod;
    1445             :         /* save attribute's collation for parse_collate.c */
    1446       13292 :         fselect->resultcollid = att->attcollation;
    1447             : 
    1448       13292 :         if (make_target_entry)
    1449             :         {
    1450             :             /* add TargetEntry decoration */
    1451             :             TargetEntry *te;
    1452             : 
    1453       26352 :             te = makeTargetEntry((Expr *) fselect,
    1454       13176 :                                  (AttrNumber) pstate->p_next_resno++,
    1455       13176 :                                  pstrdup(NameStr(att->attname)),
    1456             :                                  false);
    1457       13176 :             result = lappend(result, te);
    1458             :         }
    1459             :         else
    1460         116 :             result = lappend(result, fselect);
    1461             :     }
    1462             : 
    1463        3342 :     return result;
    1464             : }
    1465             : 
    1466             : /*
    1467             :  * expandRecordVariable
    1468             :  *      Get the tuple descriptor for a Var of type RECORD, if possible.
    1469             :  *
    1470             :  * Since no actual table or view column is allowed to have type RECORD, such
    1471             :  * a Var must refer to a JOIN or FUNCTION RTE or to a subquery output.  We
    1472             :  * drill down to find the ultimate defining expression and attempt to infer
    1473             :  * the tupdesc from it.  We ereport if we can't determine the tupdesc.
    1474             :  *
    1475             :  * levelsup is an extra offset to interpret the Var's varlevelsup correctly.
    1476             :  */
    1477             : TupleDesc
    1478        6866 : expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
    1479             : {
    1480             :     TupleDesc   tupleDesc;
    1481             :     int         netlevelsup;
    1482             :     RangeTblEntry *rte;
    1483             :     AttrNumber  attnum;
    1484             :     Node       *expr;
    1485             : 
    1486             :     /* Check my caller didn't mess up */
    1487             :     Assert(IsA(var, Var));
    1488             :     Assert(var->vartype == RECORDOID);
    1489             : 
    1490             :     /*
    1491             :      * Note: it's tempting to use GetNSItemByRangeTablePosn here so that we
    1492             :      * can use expandNSItemVars instead of expandRTE; but that does not work
    1493             :      * for some of the recursion cases below, where we have consed up a
    1494             :      * ParseState that lacks p_namespace data.
    1495             :      */
    1496        6866 :     netlevelsup = var->varlevelsup + levelsup;
    1497        6866 :     rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
    1498        6866 :     attnum = var->varattno;
    1499             : 
    1500        6866 :     if (attnum == InvalidAttrNumber)
    1501             :     {
    1502             :         /* Whole-row reference to an RTE, so expand the known fields */
    1503             :         List       *names,
    1504             :                    *vars;
    1505             :         ListCell   *lname,
    1506             :                    *lvar;
    1507             :         int         i;
    1508             : 
    1509          20 :         expandRTE(rte, var->varno, 0, var->location, false,
    1510             :                   &names, &vars);
    1511             : 
    1512          20 :         tupleDesc = CreateTemplateTupleDesc(list_length(vars));
    1513          20 :         i = 1;
    1514          60 :         forboth(lname, names, lvar, vars)
    1515             :         {
    1516          40 :             char       *label = strVal(lfirst(lname));
    1517          40 :             Node       *varnode = (Node *) lfirst(lvar);
    1518             : 
    1519          40 :             TupleDescInitEntry(tupleDesc, i,
    1520             :                                label,
    1521             :                                exprType(varnode),
    1522             :                                exprTypmod(varnode),
    1523             :                                0);
    1524          40 :             TupleDescInitEntryCollation(tupleDesc, i,
    1525             :                                         exprCollation(varnode));
    1526          40 :             i++;
    1527             :         }
    1528             :         Assert(lname == NULL && lvar == NULL);  /* lists same length? */
    1529             : 
    1530          20 :         return tupleDesc;
    1531             :     }
    1532             : 
    1533        6846 :     expr = (Node *) var;        /* default if we can't drill down */
    1534             : 
    1535        6846 :     switch (rte->rtekind)
    1536             :     {
    1537           0 :         case RTE_RELATION:
    1538             :         case RTE_VALUES:
    1539             :         case RTE_NAMEDTUPLESTORE:
    1540             :         case RTE_RESULT:
    1541             : 
    1542             :             /*
    1543             :              * This case should not occur: a column of a table, values list,
    1544             :              * or ENR shouldn't have type RECORD.  Fall through and fail (most
    1545             :              * likely) at the bottom.
    1546             :              */
    1547           0 :             break;
    1548        6834 :         case RTE_SUBQUERY:
    1549        6822 :             {
    1550             :                 /* Subselect-in-FROM: examine sub-select's output expr */
    1551        6834 :                 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
    1552             :                                                     attnum);
    1553             : 
    1554        6834 :                 if (ste == NULL || ste->resjunk)
    1555           0 :                     elog(ERROR, "subquery %s does not have attribute %d",
    1556             :                          rte->eref->aliasname, attnum);
    1557        6834 :                 expr = (Node *) ste->expr;
    1558        6834 :                 if (IsA(expr, Var))
    1559             :                 {
    1560             :                     /*
    1561             :                      * Recurse into the sub-select to see what its Var refers
    1562             :                      * to.  We have to build an additional level of ParseState
    1563             :                      * to keep in step with varlevelsup in the subselect.
    1564             :                      */
    1565             :                     ParseState  mypstate;
    1566             : 
    1567         324 :                     MemSet(&mypstate, 0, sizeof(mypstate));
    1568          12 :                     mypstate.parentParseState = pstate;
    1569          12 :                     mypstate.p_rtable = rte->subquery->rtable;
    1570             :                     /* don't bother filling the rest of the fake pstate */
    1571             : 
    1572          12 :                     return expandRecordVariable(&mypstate, (Var *) expr, 0);
    1573             :                 }
    1574             :                 /* else fall through to inspect the expression */
    1575             :             }
    1576        6822 :             break;
    1577           0 :         case RTE_JOIN:
    1578             :             /* Join RTE --- recursively inspect the alias variable */
    1579             :             Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
    1580           0 :             expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
    1581             :             Assert(expr != NULL);
    1582             :             /* We intentionally don't strip implicit coercions here */
    1583           0 :             if (IsA(expr, Var))
    1584           0 :                 return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
    1585             :             /* else fall through to inspect the expression */
    1586           0 :             break;
    1587           0 :         case RTE_FUNCTION:
    1588             : 
    1589             :             /*
    1590             :              * We couldn't get here unless a function is declared with one of
    1591             :              * its result columns as RECORD, which is not allowed.
    1592             :              */
    1593           0 :             break;
    1594           0 :         case RTE_TABLEFUNC:
    1595             : 
    1596             :             /*
    1597             :              * Table function cannot have columns with RECORD type.
    1598             :              */
    1599           0 :             break;
    1600          12 :         case RTE_CTE:
    1601             :             /* CTE reference: examine subquery's output expr */
    1602          12 :             if (!rte->self_reference)
    1603             :             {
    1604          12 :                 CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
    1605             :                 TargetEntry *ste;
    1606             : 
    1607          12 :                 ste = get_tle_by_resno(GetCTETargetList(cte), attnum);
    1608          12 :                 if (ste == NULL || ste->resjunk)
    1609           0 :                     elog(ERROR, "subquery %s does not have attribute %d",
    1610             :                          rte->eref->aliasname, attnum);
    1611          12 :                 expr = (Node *) ste->expr;
    1612          12 :                 if (IsA(expr, Var))
    1613             :                 {
    1614             :                     /*
    1615             :                      * Recurse into the CTE to see what its Var refers to. We
    1616             :                      * have to build an additional level of ParseState to keep
    1617             :                      * in step with varlevelsup in the CTE; furthermore it
    1618             :                      * could be an outer CTE.
    1619             :                      */
    1620             :                     ParseState  mypstate;
    1621             :                     Index       levelsup;
    1622             : 
    1623         216 :                     MemSet(&mypstate, 0, sizeof(mypstate));
    1624             :                     /* this loop must work, since GetCTEForRTE did */
    1625           8 :                     for (levelsup = 0;
    1626           8 :                          levelsup < rte->ctelevelsup + netlevelsup;
    1627           0 :                          levelsup++)
    1628           0 :                         pstate = pstate->parentParseState;
    1629           8 :                     mypstate.parentParseState = pstate;
    1630           8 :                     mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
    1631             :                     /* don't bother filling the rest of the fake pstate */
    1632             : 
    1633           8 :                     return expandRecordVariable(&mypstate, (Var *) expr, 0);
    1634             :                 }
    1635             :                 /* else fall through to inspect the expression */
    1636             :             }
    1637           4 :             break;
    1638             :     }
    1639             : 
    1640             :     /*
    1641             :      * We now have an expression we can't expand any more, so see if
    1642             :      * get_expr_result_tupdesc() can do anything with it.
    1643             :      */
    1644        6826 :     return get_expr_result_tupdesc(expr, false);
    1645             : }
    1646             : 
    1647             : 
    1648             : /*
    1649             :  * FigureColname -
    1650             :  *    if the name of the resulting column is not specified in the target
    1651             :  *    list, we have to guess a suitable name.  The SQL spec provides some
    1652             :  *    guidance, but not much...
    1653             :  *
    1654             :  * Note that the argument is the *untransformed* parse tree for the target
    1655             :  * item.  This is a shade easier to work with than the transformed tree.
    1656             :  */
    1657             : char *
    1658      802438 : FigureColname(Node *node)
    1659             : {
    1660      802438 :     char       *name = NULL;
    1661             : 
    1662      802438 :     (void) FigureColnameInternal(node, &name);
    1663      802438 :     if (name != NULL)
    1664      707498 :         return name;
    1665             :     /* default result if we can't guess anything */
    1666       94940 :     return "?column?";
    1667             : }
    1668             : 
    1669             : /*
    1670             :  * FigureIndexColname -
    1671             :  *    choose the name for an expression column in an index
    1672             :  *
    1673             :  * This is actually just like FigureColname, except we return NULL if
    1674             :  * we can't pick a good name.
    1675             :  */
    1676             : char *
    1677         330 : FigureIndexColname(Node *node)
    1678             : {
    1679         330 :     char       *name = NULL;
    1680             : 
    1681         330 :     (void) FigureColnameInternal(node, &name);
    1682         330 :     return name;
    1683             : }
    1684             : 
    1685             : /*
    1686             :  * FigureColnameInternal -
    1687             :  *    internal workhorse for FigureColname
    1688             :  *
    1689             :  * Return value indicates strength of confidence in result:
    1690             :  *      0 - no information
    1691             :  *      1 - second-best name choice
    1692             :  *      2 - good name choice
    1693             :  * The return value is actually only used internally.
    1694             :  * If the result isn't zero, *name is set to the chosen name.
    1695             :  */
    1696             : static int
    1697      841894 : FigureColnameInternal(Node *node, char **name)
    1698             : {
    1699      841894 :     int         strength = 0;
    1700             : 
    1701      841894 :     if (node == NULL)
    1702         324 :         return strength;
    1703             : 
    1704      841570 :     switch (nodeTag(node))
    1705             :     {
    1706      540576 :         case T_ColumnRef:
    1707           0 :             {
    1708      540576 :                 char       *fname = NULL;
    1709             :                 ListCell   *l;
    1710             : 
    1711             :                 /* find last field name, if any, ignoring "*" */
    1712     1366842 :                 foreach(l, ((ColumnRef *) node)->fields)
    1713             :                 {
    1714      826266 :                     Node       *i = lfirst(l);
    1715             : 
    1716      826266 :                     if (IsA(i, String))
    1717      826234 :                         fname = strVal(i);
    1718             :                 }
    1719      540576 :                 if (fname)
    1720             :                 {
    1721      540576 :                     *name = fname;
    1722      540576 :                     return 2;
    1723             :                 }
    1724             :             }
    1725           0 :             break;
    1726        2822 :         case T_A_Indirection:
    1727             :             {
    1728        2822 :                 A_Indirection *ind = (A_Indirection *) node;
    1729        2822 :                 char       *fname = NULL;
    1730             :                 ListCell   *l;
    1731             : 
    1732             :                 /* find last field name, if any, ignoring "*" and subscripts */
    1733        5796 :                 foreach(l, ind->indirection)
    1734             :                 {
    1735        2974 :                     Node       *i = lfirst(l);
    1736             : 
    1737        2974 :                     if (IsA(i, String))
    1738        1938 :                         fname = strVal(i);
    1739             :                 }
    1740        2822 :                 if (fname)
    1741             :                 {
    1742        1926 :                     *name = fname;
    1743        1926 :                     return 2;
    1744             :                 }
    1745         896 :                 return FigureColnameInternal(ind->arg, name);
    1746             :             }
    1747             :             break;
    1748      141402 :         case T_FuncCall:
    1749      141402 :             *name = strVal(llast(((FuncCall *) node)->funcname));
    1750      141402 :             return 2;
    1751       21276 :         case T_A_Expr:
    1752       21276 :             if (((A_Expr *) node)->kind == AEXPR_NULLIF)
    1753             :             {
    1754             :                 /* make nullif() act like a regular function */
    1755         124 :                 *name = "nullif";
    1756         124 :                 return 2;
    1757             :             }
    1758       21152 :             if (((A_Expr *) node)->kind == AEXPR_PAREN)
    1759             :             {
    1760             :                 /* look through dummy parenthesis node */
    1761           0 :                 return FigureColnameInternal(((A_Expr *) node)->lexpr, name);
    1762             :             }
    1763       21152 :             break;
    1764       30610 :         case T_TypeCast:
    1765       30610 :             strength = FigureColnameInternal(((TypeCast *) node)->arg,
    1766             :                                              name);
    1767       30610 :             if (strength <= 1)
    1768             :             {
    1769       13884 :                 if (((TypeCast *) node)->typeName != NULL)
    1770             :                 {
    1771       13884 :                     *name = strVal(llast(((TypeCast *) node)->typeName->names));
    1772       13884 :                     return 1;
    1773             :                 }
    1774             :             }
    1775       16726 :             break;
    1776          42 :         case T_CollateClause:
    1777          42 :             return FigureColnameInternal(((CollateClause *) node)->arg, name);
    1778         148 :         case T_GroupingFunc:
    1779             :             /* make GROUPING() act like a regular function */
    1780         148 :             *name = "grouping";
    1781         148 :             return 2;
    1782        5180 :         case T_SubLink:
    1783        5180 :             switch (((SubLink *) node)->subLinkType)
    1784             :             {
    1785          30 :                 case EXISTS_SUBLINK:
    1786          30 :                     *name = "exists";
    1787          30 :                     return 2;
    1788          40 :                 case ARRAY_SUBLINK:
    1789          40 :                     *name = "array";
    1790          40 :                     return 2;
    1791        5090 :                 case EXPR_SUBLINK:
    1792             :                     {
    1793             :                         /* Get column name of the subquery's single target */
    1794        5090 :                         SubLink    *sublink = (SubLink *) node;
    1795        5090 :                         Query      *query = (Query *) sublink->subselect;
    1796             : 
    1797             :                         /*
    1798             :                          * The subquery has probably already been transformed,
    1799             :                          * but let's be careful and check that.  (The reason
    1800             :                          * we can see a transformed subquery here is that
    1801             :                          * transformSubLink is lazy and modifies the SubLink
    1802             :                          * node in-place.)
    1803             :                          */
    1804        5090 :                         if (IsA(query, Query))
    1805             :                         {
    1806        5090 :                             TargetEntry *te = (TargetEntry *) linitial(query->targetList);
    1807             : 
    1808        5090 :                             if (te->resname)
    1809             :                             {
    1810        5090 :                                 *name = te->resname;
    1811        5090 :                                 return 2;
    1812             :                             }
    1813             :                         }
    1814             :                     }
    1815           0 :                     break;
    1816             :                     /* As with other operator-like nodes, these have no names */
    1817          20 :                 case MULTIEXPR_SUBLINK:
    1818             :                 case ALL_SUBLINK:
    1819             :                 case ANY_SUBLINK:
    1820             :                 case ROWCOMPARE_SUBLINK:
    1821             :                 case CTE_SUBLINK:
    1822          20 :                     break;
    1823             :             }
    1824          20 :             break;
    1825        7578 :         case T_CaseExpr:
    1826        7578 :             strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult,
    1827             :                                              name);
    1828        7578 :             if (strength <= 1)
    1829             :             {
    1830        3764 :                 *name = "case";
    1831        3764 :                 return 1;
    1832             :             }
    1833        3814 :             break;
    1834         480 :         case T_A_ArrayExpr:
    1835             :             /* make ARRAY[] act like a function */
    1836         480 :             *name = "array";
    1837         480 :             return 2;
    1838         296 :         case T_RowExpr:
    1839             :             /* make ROW() act like a function */
    1840         296 :             *name = "row";
    1841         296 :             return 2;
    1842         148 :         case T_CoalesceExpr:
    1843             :             /* make coalesce() act like a regular function */
    1844         148 :             *name = "coalesce";
    1845         148 :             return 2;
    1846          98 :         case T_MinMaxExpr:
    1847             :             /* make greatest/least act like a regular function */
    1848          98 :             switch (((MinMaxExpr *) node)->op)
    1849             :             {
    1850          28 :                 case IS_GREATEST:
    1851          28 :                     *name = "greatest";
    1852          28 :                     return 2;
    1853          70 :                 case IS_LEAST:
    1854          70 :                     *name = "least";
    1855          70 :                     return 2;
    1856             :             }
    1857           0 :             break;
    1858          48 :         case T_SQLValueFunction:
    1859             :             /* make these act like a function or variable */
    1860          48 :             switch (((SQLValueFunction *) node)->op)
    1861             :             {
    1862           8 :                 case SVFOP_CURRENT_DATE:
    1863           8 :                     *name = "current_date";
    1864           8 :                     return 2;
    1865           0 :                 case SVFOP_CURRENT_TIME:
    1866             :                 case SVFOP_CURRENT_TIME_N:
    1867           0 :                     *name = "current_time";
    1868           0 :                     return 2;
    1869           0 :                 case SVFOP_CURRENT_TIMESTAMP:
    1870             :                 case SVFOP_CURRENT_TIMESTAMP_N:
    1871           0 :                     *name = "current_timestamp";
    1872           0 :                     return 2;
    1873           0 :                 case SVFOP_LOCALTIME:
    1874             :                 case SVFOP_LOCALTIME_N:
    1875           0 :                     *name = "localtime";
    1876           0 :                     return 2;
    1877           4 :                 case SVFOP_LOCALTIMESTAMP:
    1878             :                 case SVFOP_LOCALTIMESTAMP_N:
    1879           4 :                     *name = "localtimestamp";
    1880           4 :                     return 2;
    1881           0 :                 case SVFOP_CURRENT_ROLE:
    1882           0 :                     *name = "current_role";
    1883           0 :                     return 2;
    1884          12 :                 case SVFOP_CURRENT_USER:
    1885          12 :                     *name = "current_user";
    1886          12 :                     return 2;
    1887           0 :                 case SVFOP_USER:
    1888           0 :                     *name = "user";
    1889           0 :                     return 2;
    1890          12 :                 case SVFOP_SESSION_USER:
    1891          12 :                     *name = "session_user";
    1892          12 :                     return 2;
    1893           0 :                 case SVFOP_CURRENT_CATALOG:
    1894           0 :                     *name = "current_catalog";
    1895           0 :                     return 2;
    1896          12 :                 case SVFOP_CURRENT_SCHEMA:
    1897          12 :                     *name = "current_schema";
    1898          12 :                     return 2;
    1899             :             }
    1900           0 :             break;
    1901         300 :         case T_XmlExpr:
    1902             :             /* make SQL/XML functions act like a regular function */
    1903         300 :             switch (((XmlExpr *) node)->op)
    1904             :             {
    1905          32 :                 case IS_XMLCONCAT:
    1906          32 :                     *name = "xmlconcat";
    1907          32 :                     return 2;
    1908          72 :                 case IS_XMLELEMENT:
    1909          72 :                     *name = "xmlelement";
    1910          72 :                     return 2;
    1911           4 :                 case IS_XMLFOREST:
    1912           4 :                     *name = "xmlforest";
    1913           4 :                     return 2;
    1914          92 :                 case IS_XMLPARSE:
    1915          92 :                     *name = "xmlparse";
    1916          92 :                     return 2;
    1917          52 :                 case IS_XMLPI:
    1918          52 :                     *name = "xmlpi";
    1919          52 :                     return 2;
    1920          40 :                 case IS_XMLROOT:
    1921          40 :                     *name = "xmlroot";
    1922          40 :                     return 2;
    1923           0 :                 case IS_XMLSERIALIZE:
    1924           0 :                     *name = "xmlserialize";
    1925           0 :                     return 2;
    1926           8 :                 case IS_DOCUMENT:
    1927             :                     /* nothing */
    1928           8 :                     break;
    1929             :             }
    1930           8 :             break;
    1931          20 :         case T_XmlSerialize:
    1932          20 :             *name = "xmlserialize";
    1933          20 :             return 2;
    1934       90546 :         default:
    1935       90546 :             break;
    1936             :     }
    1937             : 
    1938      132266 :     return strength;
    1939             : }

Generated by: LCOV version 1.13