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

Generated by: LCOV version 1.14