LCOV - differential code coverage report
Current view: top level - src/backend/parser - parse_target.c (source / functions) Coverage Total Hit LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 89.2 % 567 506 3 48 10 12 331 17 146 35 321 4 36
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 20 20 20 20
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 [..60] days: 100.0 % 12 12 12
Legend: Lines: hit not hit (60,120] days: 100.0 % 1 1 1
(120,180] days: 100.0 % 3 3 3
(240..) days: 88.9 % 551 490 3 48 10 12 331 1 146 35 321
Function coverage date bins:
(240..) days: 50.0 % 40 20 20 20

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

Generated by: LCOV version v1.16-55-g56c0a2a