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

Generated by: LCOV version 1.11