LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/util - var.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 88.1 % 371 327 21 10 12 1 9 155 74 89 33 219 1 9
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 21 21 17 4 21
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*-------------------------------------------------------------------------
       2                 :  *
       3                 :  * var.c
       4                 :  *    Var node manipulation routines
       5                 :  *
       6                 :  * Note: for most purposes, PlaceHolderVar is considered a Var too,
       7                 :  * even if its contained expression is variable-free.  Also, CurrentOfExpr
       8                 :  * is treated as a Var for purposes of determining whether an expression
       9                 :  * contains variables.
      10                 :  *
      11                 :  *
      12                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
      13                 :  * Portions Copyright (c) 1994, Regents of the University of California
      14                 :  *
      15                 :  *
      16                 :  * IDENTIFICATION
      17                 :  *    src/backend/optimizer/util/var.c
      18                 :  *
      19                 :  *-------------------------------------------------------------------------
      20                 :  */
      21                 : #include "postgres.h"
      22                 : 
      23                 : #include "access/sysattr.h"
      24                 : #include "nodes/nodeFuncs.h"
      25                 : #include "optimizer/optimizer.h"
      26                 : #include "optimizer/placeholder.h"
      27                 : #include "optimizer/prep.h"
      28                 : #include "parser/parsetree.h"
      29                 : #include "rewrite/rewriteManip.h"
      30                 : 
      31                 : 
      32                 : typedef struct
      33                 : {
      34                 :     Relids      varnos;
      35                 :     PlannerInfo *root;
      36                 :     int         sublevels_up;
      37                 : } pull_varnos_context;
      38                 : 
      39                 : typedef struct
      40                 : {
      41                 :     Bitmapset  *varattnos;
      42                 :     Index       varno;
      43                 : } pull_varattnos_context;
      44                 : 
      45                 : typedef struct
      46                 : {
      47                 :     List       *vars;
      48                 :     int         sublevels_up;
      49                 : } pull_vars_context;
      50                 : 
      51                 : typedef struct
      52                 : {
      53                 :     int         var_location;
      54                 :     int         sublevels_up;
      55                 : } locate_var_of_level_context;
      56                 : 
      57                 : typedef struct
      58                 : {
      59                 :     List       *varlist;
      60                 :     int         flags;
      61                 : } pull_var_clause_context;
      62                 : 
      63                 : typedef struct
      64                 : {
      65                 :     PlannerInfo *root;          /* could be NULL! */
      66                 :     Query      *query;          /* outer Query */
      67                 :     int         sublevels_up;
      68                 :     bool        possible_sublink;   /* could aliases include a SubLink? */
      69                 :     bool        inserted_sublink;   /* have we inserted a SubLink? */
      70                 : } flatten_join_alias_vars_context;
      71                 : 
      72                 : static bool pull_varnos_walker(Node *node,
      73                 :                                pull_varnos_context *context);
      74                 : static bool pull_varattnos_walker(Node *node, pull_varattnos_context *context);
      75                 : static bool pull_vars_walker(Node *node, pull_vars_context *context);
      76                 : static bool contain_var_clause_walker(Node *node, void *context);
      77                 : static bool contain_vars_of_level_walker(Node *node, int *sublevels_up);
      78                 : static bool locate_var_of_level_walker(Node *node,
      79                 :                                        locate_var_of_level_context *context);
      80                 : static bool pull_var_clause_walker(Node *node,
      81                 :                                    pull_var_clause_context *context);
      82                 : static Node *flatten_join_alias_vars_mutator(Node *node,
      83                 :                                              flatten_join_alias_vars_context *context);
      84                 : static Node *add_nullingrels_if_needed(PlannerInfo *root, Node *newnode,
      85                 :                                        Var *oldvar);
      86                 : static bool is_standard_join_alias_expression(Node *newnode, Var *oldvar);
      87                 : static void adjust_standard_join_alias_expression(Node *newnode, Var *oldvar);
      88                 : static Relids alias_relid_set(Query *query, Relids relids);
      89                 : 
      90                 : 
      91                 : /*
      92                 :  * pull_varnos
      93                 :  *      Create a set of all the distinct varnos present in a parsetree.
      94                 :  *      Only varnos that reference level-zero rtable entries are considered.
      95                 :  *
      96                 :  * The result includes outer-join relids mentioned in Var.varnullingrels and
      97                 :  * PlaceHolderVar.phnullingrels fields in the parsetree.
      98                 :  *
      99                 :  * "root" can be passed as NULL if it is not necessary to process
     100                 :  * PlaceHolderVars.
     101                 :  *
     102                 :  * NOTE: this is used on not-yet-planned expressions.  It may therefore find
     103                 :  * bare SubLinks, and if so it needs to recurse into them to look for uplevel
     104                 :  * references to the desired rtable level!  But when we find a completed
     105                 :  * SubPlan, we only need to look at the parameters passed to the subplan.
     106                 :  */
     107                 : Relids
     108 GIC     1145028 : pull_varnos(PlannerInfo *root, Node *node)
     109                 : {
     110                 :     pull_varnos_context context;
     111                 : 
     112         1145028 :     context.varnos = NULL;
     113         1145028 :     context.root = root;
     114         1145028 :     context.sublevels_up = 0;
     115                 : 
     116 ECB             :     /*
     117                 :      * Must be prepared to start with a Query or a bare expression tree; if
     118                 :      * it's a Query, we don't want to increment sublevels_up.
     119                 :      */
     120 CBC     1145028 :     query_or_expression_tree_walker(node,
     121 ECB             :                                     pull_varnos_walker,
     122                 :                                     (void *) &context,
     123                 :                                     0);
     124                 : 
     125 GIC     1145028 :     return context.varnos;
     126                 : }
     127                 : 
     128 ECB             : /*
     129                 :  * pull_varnos_of_level
     130                 :  *      Create a set of all the distinct varnos present in a parsetree.
     131                 :  *      Only Vars of the specified level are considered.
     132                 :  */
     133                 : Relids
     134 GIC         517 : pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
     135                 : {
     136                 :     pull_varnos_context context;
     137                 : 
     138             517 :     context.varnos = NULL;
     139             517 :     context.root = root;
     140             517 :     context.sublevels_up = levelsup;
     141                 : 
     142 ECB             :     /*
     143                 :      * Must be prepared to start with a Query or a bare expression tree; if
     144                 :      * it's a Query, we don't want to increment sublevels_up.
     145                 :      */
     146 CBC         517 :     query_or_expression_tree_walker(node,
     147 ECB             :                                     pull_varnos_walker,
     148                 :                                     (void *) &context,
     149                 :                                     0);
     150                 : 
     151 GIC         517 :     return context.varnos;
     152                 : }
     153                 : 
     154 ECB             : static bool
     155 GIC     1924780 : pull_varnos_walker(Node *node, pull_varnos_context *context)
     156                 : {
     157         1924780 :     if (node == NULL)
     158           24511 :         return false;
     159 CBC     1900269 :     if (IsA(node, Var))
     160                 :     {
     161 GIC      932637 :         Var        *var = (Var *) node;
     162                 : 
     163 CBC      932637 :         if (var->varlevelsup == context->sublevels_up)
     164                 :         {
     165 GIC      931912 :             context->varnos = bms_add_member(context->varnos, var->varno);
     166 GNC      931912 :             context->varnos = bms_add_members(context->varnos,
     167          931912 :                                               var->varnullingrels);
     168                 :         }
     169 CBC      932637 :         return false;
     170 ECB             :     }
     171 CBC      967632 :     if (IsA(node, CurrentOfExpr))
     172                 :     {
     173             388 :         CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
     174                 : 
     175             388 :         if (context->sublevels_up == 0)
     176 GIC         388 :             context->varnos = bms_add_member(context->varnos, cexpr->cvarno);
     177 CBC         388 :         return false;
     178 ECB             :     }
     179 CBC      967244 :     if (IsA(node, PlaceHolderVar))
     180                 :     {
     181            1756 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
     182                 : 
     183 ECB             :         /*
     184                 :          * If a PlaceHolderVar is not of the target query level, ignore it,
     185                 :          * instead recursing into its expression to see if it contains any
     186                 :          * vars that are of the target level.  We'll also do that when the
     187                 :          * caller doesn't pass a "root" pointer.  (We probably shouldn't see
     188                 :          * PlaceHolderVars at all in such cases, but if we do, this is a
     189                 :          * reasonable behavior.)
     190                 :          */
     191 CBC        1756 :         if (phv->phlevelsup == context->sublevels_up &&
     192 GIC        1756 :             context->root != NULL)
     193 ECB             :         {
     194                 :             /*
     195                 :              * Ideally, the PHV's contribution to context->varnos is its
     196                 :              * ph_eval_at set.  However, this code can be invoked before
     197                 :              * that's been computed.  If we cannot find a PlaceHolderInfo,
     198                 :              * fall back to the conservative assumption that the PHV will be
     199                 :              * evaluated at its syntactic level (phv->phrels).
     200                 :              *
     201                 :              * Another problem is that a PlaceHolderVar can appear in quals or
     202                 :              * tlists that have been translated for use in a child appendrel.
     203                 :              * Typically such a PHV is a parameter expression sourced by some
     204                 :              * other relation, so that the translation from parent appendrel
     205                 :              * to child doesn't change its phrels, and we should still take
     206                 :              * ph_eval_at at face value.  But in corner cases, the PHV's
     207                 :              * original phrels can include the parent appendrel itself, in
     208                 :              * which case the translated PHV will have the child appendrel in
     209                 :              * phrels, and we must translate ph_eval_at to match.
     210                 :              */
     211 GIC        1756 :             PlaceHolderInfo *phinfo = NULL;
     212                 : 
     213 CBC        1756 :             if (phv->phlevelsup == 0)
     214                 :             {
     215 GNC        1744 :                 if (phv->phid < context->root->placeholder_array_size)
     216            1617 :                     phinfo = context->root->placeholder_array[phv->phid];
     217 ECB             :             }
     218 GIC        1756 :             if (phinfo == NULL)
     219 ECB             :             {
     220                 :                 /* No PlaceHolderInfo yet, use phrels */
     221 GIC         145 :                 context->varnos = bms_add_members(context->varnos,
     222 CBC         145 :                                                   phv->phrels);
     223 ECB             :             }
     224 GIC        1611 :             else if (bms_equal(phv->phrels, phinfo->ph_var->phrels))
     225                 :             {
     226                 :                 /* Normal case: use ph_eval_at */
     227            1389 :                 context->varnos = bms_add_members(context->varnos,
     228            1389 :                                                   phinfo->ph_eval_at);
     229                 :             }
     230                 :             else
     231                 :             {
     232 ECB             :                 /* Translated PlaceHolderVar: translate ph_eval_at to match */
     233                 :                 Relids      newevalat,
     234                 :                             delta;
     235                 : 
     236                 :                 /* remove what was removed from phv->phrels ... */
     237 GIC         222 :                 delta = bms_difference(phinfo->ph_var->phrels, phv->phrels);
     238 CBC         222 :                 newevalat = bms_difference(phinfo->ph_eval_at, delta);
     239 ECB             :                 /* ... then if that was in fact part of ph_eval_at ... */
     240 GIC         222 :                 if (!bms_equal(newevalat, phinfo->ph_eval_at))
     241 ECB             :                 {
     242                 :                     /* ... add what was added */
     243 GIC         156 :                     delta = bms_difference(phv->phrels, phinfo->ph_var->phrels);
     244             156 :                     newevalat = bms_join(newevalat, delta);
     245                 :                 }
     246             222 :                 context->varnos = bms_join(context->varnos,
     247                 :                                            newevalat);
     248                 :             }
     249                 : 
     250                 :             /*
     251                 :              * In all three cases, include phnullingrels in the result.  We
     252                 :              * don't worry about possibly needing to translate it, because
     253                 :              * appendrels only translate varnos of baserels, not outer joins.
     254                 :              */
     255 GNC        3512 :             context->varnos = bms_add_members(context->varnos,
     256            1756 :                                               phv->phnullingrels);
     257 GIC        1756 :             return false;       /* don't recurse into expression */
     258 ECB             :         }
     259                 :     }
     260 CBC      965488 :     else if (IsA(node, Query))
     261                 :     {
     262                 :         /* Recurse into RTE subquery or not-yet-planned sublink subquery */
     263 ECB             :         bool        result;
     264                 : 
     265 GIC          33 :         context->sublevels_up++;
     266              33 :         result = query_tree_walker((Query *) node, pull_varnos_walker,
     267                 :                                    (void *) context, 0);
     268 CBC          33 :         context->sublevels_up--;
     269              33 :         return result;
     270                 :     }
     271          965455 :     return expression_tree_walker(node, pull_varnos_walker,
     272 ECB             :                                   (void *) context);
     273                 : }
     274                 : 
     275                 : 
     276                 : /*
     277                 :  * pull_varattnos
     278                 :  *      Find all the distinct attribute numbers present in an expression tree,
     279                 :  *      and add them to the initial contents of *varattnos.
     280                 :  *      Only Vars of the given varno and rtable level zero are considered.
     281                 :  *
     282                 :  * Attribute numbers are offset by FirstLowInvalidHeapAttributeNumber so that
     283                 :  * we can include system attributes (e.g., OID) in the bitmap representation.
     284                 :  *
     285                 :  * Currently, this does not support unplanned subqueries; that is not needed
     286                 :  * for current uses.  It will handle already-planned SubPlan nodes, though,
     287                 :  * looking into only the "testexpr" and the "args" list.  (The subplan cannot
     288                 :  * contain any other references to Vars of the current level.)
     289                 :  */
     290                 : void
     291 GIC      668803 : pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
     292                 : {
     293                 :     pull_varattnos_context context;
     294 ECB             : 
     295 GIC      668803 :     context.varattnos = *varattnos;
     296          668803 :     context.varno = varno;
     297                 : 
     298 CBC      668803 :     (void) pull_varattnos_walker(node, &context);
     299 ECB             : 
     300 GIC      668803 :     *varattnos = context.varattnos;
     301 CBC      668803 : }
     302                 : 
     303 ECB             : static bool
     304 CBC     2130140 : pull_varattnos_walker(Node *node, pull_varattnos_context *context)
     305                 : {
     306 GIC     2130140 :     if (node == NULL)
     307 CBC       40868 :         return false;
     308 GIC     2089272 :     if (IsA(node, Var))
     309 ECB             :     {
     310 CBC     1189340 :         Var        *var = (Var *) node;
     311 ECB             : 
     312 GIC     1189340 :         if (var->varno == context->varno && var->varlevelsup == 0)
     313 CBC     1189304 :             context->varattnos =
     314 GIC     1189304 :                 bms_add_member(context->varattnos,
     315 CBC     1189304 :                                var->varattno - FirstLowInvalidHeapAttributeNumber);
     316         1189340 :         return false;
     317 ECB             :     }
     318                 : 
     319                 :     /* Should not find an unplanned subquery */
     320 GIC      899932 :     Assert(!IsA(node, Query));
     321                 : 
     322          899932 :     return expression_tree_walker(node, pull_varattnos_walker,
     323 ECB             :                                   (void *) context);
     324                 : }
     325                 : 
     326                 : 
     327                 : /*
     328                 :  * pull_vars_of_level
     329                 :  *      Create a list of all Vars (and PlaceHolderVars) referencing the
     330                 :  *      specified query level in the given parsetree.
     331                 :  *
     332                 :  * Caution: the Vars are not copied, only linked into the list.
     333                 :  */
     334                 : List *
     335 GIC        3444 : pull_vars_of_level(Node *node, int levelsup)
     336                 : {
     337                 :     pull_vars_context context;
     338 ECB             : 
     339 GIC        3444 :     context.vars = NIL;
     340            3444 :     context.sublevels_up = levelsup;
     341                 : 
     342 ECB             :     /*
     343                 :      * Must be prepared to start with a Query or a bare expression tree; if
     344                 :      * it's a Query, we don't want to increment sublevels_up.
     345                 :      */
     346 GIC        3444 :     query_or_expression_tree_walker(node,
     347                 :                                     pull_vars_walker,
     348                 :                                     (void *) &context,
     349 ECB             :                                     0);
     350                 : 
     351 GIC        3444 :     return context.vars;
     352                 : }
     353                 : 
     354 ECB             : static bool
     355 GIC       39042 : pull_vars_walker(Node *node, pull_vars_context *context)
     356                 : {
     357           39042 :     if (node == NULL)
     358 CBC        4669 :         return false;
     359 GIC       34373 :     if (IsA(node, Var))
     360 ECB             :     {
     361 CBC        4502 :         Var        *var = (Var *) node;
     362 ECB             : 
     363 GIC        4502 :         if (var->varlevelsup == context->sublevels_up)
     364 CBC        3645 :             context->vars = lappend(context->vars, var);
     365 GIC        4502 :         return false;
     366 ECB             :     }
     367 CBC       29871 :     if (IsA(node, PlaceHolderVar))
     368 ECB             :     {
     369 GIC          36 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
     370 ECB             : 
     371 GIC          36 :         if (phv->phlevelsup == context->sublevels_up)
     372 CBC          36 :             context->vars = lappend(context->vars, phv);
     373                 :         /* we don't want to look into the contained expression */
     374              36 :         return false;
     375 ECB             :     }
     376 GIC       29835 :     if (IsA(node, Query))
     377 ECB             :     {
     378                 :         /* Recurse into RTE subquery or not-yet-planned sublink subquery */
     379                 :         bool        result;
     380                 : 
     381 GIC         141 :         context->sublevels_up++;
     382             141 :         result = query_tree_walker((Query *) node, pull_vars_walker,
     383                 :                                    (void *) context, 0);
     384 CBC         141 :         context->sublevels_up--;
     385             141 :         return result;
     386                 :     }
     387           29694 :     return expression_tree_walker(node, pull_vars_walker,
     388 ECB             :                                   (void *) context);
     389                 : }
     390                 : 
     391                 : 
     392                 : /*
     393                 :  * contain_var_clause
     394                 :  *    Recursively scan a clause to discover whether it contains any Var nodes
     395                 :  *    (of the current query level).
     396                 :  *
     397                 :  *    Returns true if any varnode found.
     398                 :  *
     399                 :  * Does not examine subqueries, therefore must only be used after reduction
     400                 :  * of sublinks to subplans!
     401                 :  */
     402                 : bool
     403 GIC       32240 : contain_var_clause(Node *node)
     404                 : {
     405           32240 :     return contain_var_clause_walker(node, NULL);
     406 ECB             : }
     407                 : 
     408                 : static bool
     409 GIC       39753 : contain_var_clause_walker(Node *node, void *context)
     410                 : {
     411           39753 :     if (node == NULL)
     412 CBC          41 :         return false;
     413 GIC       39712 :     if (IsA(node, Var))
     414 ECB             :     {
     415 CBC        1084 :         if (((Var *) node)->varlevelsup == 0)
     416            1084 :             return true;        /* abort the tree traversal and return true */
     417 UIC           0 :         return false;
     418 ECB             :     }
     419 CBC       38628 :     if (IsA(node, CurrentOfExpr))
     420 UBC           0 :         return true;
     421 GIC       38628 :     if (IsA(node, PlaceHolderVar))
     422 ECB             :     {
     423 GBC           6 :         if (((PlaceHolderVar *) node)->phlevelsup == 0)
     424 CBC           6 :             return true;        /* abort the tree traversal and return true */
     425                 :         /* else fall through to check the contained expr */
     426 ECB             :     }
     427 CBC       38622 :     return expression_tree_walker(node, contain_var_clause_walker, context);
     428                 : }
     429                 : 
     430 ECB             : 
     431                 : /*
     432                 :  * contain_vars_of_level
     433                 :  *    Recursively scan a clause to discover whether it contains any Var nodes
     434                 :  *    of the specified query level.
     435                 :  *
     436                 :  *    Returns true if any such Var found.
     437                 :  *
     438                 :  * Will recurse into sublinks.  Also, may be invoked directly on a Query.
     439                 :  */
     440                 : bool
     441 GIC      130186 : contain_vars_of_level(Node *node, int levelsup)
     442                 : {
     443          130186 :     int         sublevels_up = levelsup;
     444 ECB             : 
     445 GIC      130186 :     return query_or_expression_tree_walker(node,
     446 ECB             :                                            contain_vars_of_level_walker,
     447                 :                                            (void *) &sublevels_up,
     448                 :                                            0);
     449                 : }
     450                 : 
     451                 : static bool
     452 GIC      330574 : contain_vars_of_level_walker(Node *node, int *sublevels_up)
     453                 : {
     454          330574 :     if (node == NULL)
     455 CBC       67335 :         return false;
     456 GIC      263239 :     if (IsA(node, Var))
     457 ECB             :     {
     458 CBC       33585 :         if (((Var *) node)->varlevelsup == *sublevels_up)
     459           18462 :             return true;        /* abort tree traversal and return true */
     460 GIC       15123 :         return false;
     461 ECB             :     }
     462 CBC      229654 :     if (IsA(node, CurrentOfExpr))
     463 ECB             :     {
     464 GIC          92 :         if (*sublevels_up == 0)
     465 CBC          92 :             return true;
     466 UIC           0 :         return false;
     467 ECB             :     }
     468 CBC      229562 :     if (IsA(node, PlaceHolderVar))
     469 EUB             :     {
     470 GIC           9 :         if (((PlaceHolderVar *) node)->phlevelsup == *sublevels_up)
     471 CBC           6 :             return true;        /* abort the tree traversal and return true */
     472                 :         /* else fall through to check the contained expr */
     473 ECB             :     }
     474 CBC      229556 :     if (IsA(node, Query))
     475                 :     {
     476                 :         /* Recurse into subselects */
     477 ECB             :         bool        result;
     478                 : 
     479 GIC        2196 :         (*sublevels_up)++;
     480            2196 :         result = query_tree_walker((Query *) node,
     481                 :                                    contain_vars_of_level_walker,
     482 ECB             :                                    (void *) sublevels_up,
     483                 :                                    0);
     484 GIC        2196 :         (*sublevels_up)--;
     485            2196 :         return result;
     486                 :     }
     487 CBC      227360 :     return expression_tree_walker(node,
     488 ECB             :                                   contain_vars_of_level_walker,
     489                 :                                   (void *) sublevels_up);
     490                 : }
     491                 : 
     492                 : 
     493                 : /*
     494                 :  * locate_var_of_level
     495                 :  *    Find the parse location of any Var of the specified query level.
     496                 :  *
     497                 :  * Returns -1 if no such Var is in the querytree, or if they all have
     498                 :  * unknown parse location.  (The former case is probably caller error,
     499                 :  * but we don't bother to distinguish it from the latter case.)
     500                 :  *
     501                 :  * Will recurse into sublinks.  Also, may be invoked directly on a Query.
     502                 :  *
     503                 :  * Note: it might seem appropriate to merge this functionality into
     504                 :  * contain_vars_of_level, but that would complicate that function's API.
     505                 :  * Currently, the only uses of this function are for error reporting,
     506                 :  * and so shaving cycles probably isn't very important.
     507                 :  */
     508                 : int
     509 GIC           6 : locate_var_of_level(Node *node, int levelsup)
     510                 : {
     511                 :     locate_var_of_level_context context;
     512 ECB             : 
     513 GIC           6 :     context.var_location = -1;  /* in case we find nothing */
     514               6 :     context.sublevels_up = levelsup;
     515                 : 
     516 CBC           6 :     (void) query_or_expression_tree_walker(node,
     517 ECB             :                                            locate_var_of_level_walker,
     518                 :                                            (void *) &context,
     519                 :                                            0);
     520                 : 
     521 GIC           6 :     return context.var_location;
     522                 : }
     523                 : 
     524 ECB             : static bool
     525 GIC          15 : locate_var_of_level_walker(Node *node,
     526                 :                            locate_var_of_level_context *context)
     527                 : {
     528 CBC          15 :     if (node == NULL)
     529 UIC           0 :         return false;
     530 GIC          15 :     if (IsA(node, Var))
     531 ECB             :     {
     532 GBC           6 :         Var        *var = (Var *) node;
     533 ECB             : 
     534 GIC           6 :         if (var->varlevelsup == context->sublevels_up &&
     535 CBC           6 :             var->location >= 0)
     536                 :         {
     537               6 :             context->var_location = var->location;
     538               6 :             return true;        /* abort tree traversal and return true */
     539                 :         }
     540 LBC           0 :         return false;
     541 ECB             :     }
     542 GIC           9 :     if (IsA(node, CurrentOfExpr))
     543 EUB             :     {
     544                 :         /* since CurrentOfExpr doesn't carry location, nothing we can do */
     545 LBC           0 :         return false;
     546                 :     }
     547                 :     /* No extra code needed for PlaceHolderVar; just look in contained expr */
     548 GBC           9 :     if (IsA(node, Query))
     549                 :     {
     550                 :         /* Recurse into subselects */
     551 ECB             :         bool        result;
     552                 : 
     553 UIC           0 :         context->sublevels_up++;
     554               0 :         result = query_tree_walker((Query *) node,
     555                 :                                    locate_var_of_level_walker,
     556 EUB             :                                    (void *) context,
     557                 :                                    0);
     558 UIC           0 :         context->sublevels_up--;
     559               0 :         return result;
     560                 :     }
     561 GBC           9 :     return expression_tree_walker(node,
     562 EUB             :                                   locate_var_of_level_walker,
     563                 :                                   (void *) context);
     564 ECB             : }
     565                 : 
     566                 : 
     567                 : /*
     568                 :  * pull_var_clause
     569                 :  *    Recursively pulls all Var nodes from an expression clause.
     570                 :  *
     571                 :  *    Aggrefs are handled according to these bits in 'flags':
     572                 :  *      PVC_INCLUDE_AGGREGATES      include Aggrefs in output list
     573                 :  *      PVC_RECURSE_AGGREGATES      recurse into Aggref arguments
     574                 :  *      neither flag                throw error if Aggref found
     575                 :  *    Vars within an Aggref's expression are included in the result only
     576                 :  *    when PVC_RECURSE_AGGREGATES is specified.
     577                 :  *
     578                 :  *    WindowFuncs are handled according to these bits in 'flags':
     579                 :  *      PVC_INCLUDE_WINDOWFUNCS     include WindowFuncs in output list
     580                 :  *      PVC_RECURSE_WINDOWFUNCS     recurse into WindowFunc arguments
     581                 :  *      neither flag                throw error if WindowFunc found
     582                 :  *    Vars within a WindowFunc's expression are included in the result only
     583                 :  *    when PVC_RECURSE_WINDOWFUNCS is specified.
     584                 :  *
     585                 :  *    PlaceHolderVars are handled according to these bits in 'flags':
     586                 :  *      PVC_INCLUDE_PLACEHOLDERS    include PlaceHolderVars in output list
     587                 :  *      PVC_RECURSE_PLACEHOLDERS    recurse into PlaceHolderVar arguments
     588                 :  *      neither flag                throw error if PlaceHolderVar found
     589                 :  *    Vars within a PHV's expression are included in the result only
     590                 :  *    when PVC_RECURSE_PLACEHOLDERS is specified.
     591                 :  *
     592                 :  *    GroupingFuncs are treated exactly like Aggrefs, and so do not need
     593                 :  *    their own flag bits.
     594                 :  *
     595                 :  *    CurrentOfExpr nodes are ignored in all cases.
     596                 :  *
     597                 :  *    Upper-level vars (with varlevelsup > 0) should not be seen here,
     598                 :  *    likewise for upper-level Aggrefs and PlaceHolderVars.
     599                 :  *
     600                 :  *    Returns list of nodes found.  Note the nodes themselves are not
     601                 :  *    copied, only referenced.
     602                 :  *
     603                 :  * Does not examine subqueries, therefore must only be used after reduction
     604                 :  * of sublinks to subplans!
     605                 :  */
     606                 : List *
     607 GIC      270950 : pull_var_clause(Node *node, int flags)
     608                 : {
     609                 :     pull_var_clause_context context;
     610 ECB             : 
     611                 :     /* Assert that caller has not specified inconsistent flags */
     612 GIC      270950 :     Assert((flags & (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES))
     613                 :            != (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES));
     614          270950 :     Assert((flags & (PVC_INCLUDE_WINDOWFUNCS | PVC_RECURSE_WINDOWFUNCS))
     615 ECB             :            != (PVC_INCLUDE_WINDOWFUNCS | PVC_RECURSE_WINDOWFUNCS));
     616 GIC      270950 :     Assert((flags & (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS))
     617 ECB             :            != (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS));
     618                 : 
     619 CBC      270950 :     context.varlist = NIL;
     620 GIC      270950 :     context.flags = flags;
     621                 : 
     622 CBC      270950 :     pull_var_clause_walker(node, &context);
     623          270950 :     return context.varlist;
     624                 : }
     625 ECB             : 
     626                 : static bool
     627 GIC     1636963 : pull_var_clause_walker(Node *node, pull_var_clause_context *context)
     628                 : {
     629         1636963 :     if (node == NULL)
     630 CBC       65135 :         return false;
     631 GIC     1571828 :     if (IsA(node, Var))
     632 ECB             :     {
     633 CBC      587095 :         if (((Var *) node)->varlevelsup != 0)
     634 LBC           0 :             elog(ERROR, "Upper-level Var found where not expected");
     635 GIC      587095 :         context->varlist = lappend(context->varlist, node);
     636 CBC      587095 :         return false;
     637 EUB             :     }
     638 CBC      984733 :     else if (IsA(node, Aggref))
     639 ECB             :     {
     640 GIC       39165 :         if (((Aggref *) node)->agglevelsup != 0)
     641 LBC           0 :             elog(ERROR, "Upper-level Aggref found where not expected");
     642 GIC       39165 :         if (context->flags & PVC_INCLUDE_AGGREGATES)
     643 ECB             :         {
     644 GBC        2307 :             context->varlist = lappend(context->varlist, node);
     645 ECB             :             /* we do NOT descend into the contained expression */
     646 GIC        2307 :             return false;
     647 ECB             :         }
     648 GIC       36858 :         else if (context->flags & PVC_RECURSE_AGGREGATES)
     649 ECB             :         {
     650                 :             /* fall through to recurse into the aggregate's arguments */
     651                 :         }
     652                 :         else
     653 UIC           0 :             elog(ERROR, "Aggref found where not expected");
     654                 :     }
     655 GIC      945568 :     else if (IsA(node, GroupingFunc))
     656 EUB             :     {
     657 GIC         275 :         if (((GroupingFunc *) node)->agglevelsup != 0)
     658 LBC           0 :             elog(ERROR, "Upper-level GROUPING found where not expected");
     659 GIC         275 :         if (context->flags & PVC_INCLUDE_AGGREGATES)
     660 ECB             :         {
     661 GBC           2 :             context->varlist = lappend(context->varlist, node);
     662 ECB             :             /* we do NOT descend into the contained expression */
     663 GIC           2 :             return false;
     664 ECB             :         }
     665 GIC         273 :         else if (context->flags & PVC_RECURSE_AGGREGATES)
     666 ECB             :         {
     667                 :             /* fall through to recurse into the GroupingFunc's arguments */
     668                 :         }
     669                 :         else
     670 UIC           0 :             elog(ERROR, "GROUPING found where not expected");
     671                 :     }
     672 GIC      945293 :     else if (IsA(node, WindowFunc))
     673 EUB             :     {
     674                 :         /* WindowFuncs have no levelsup field to check ... */
     675 CBC        2769 :         if (context->flags & PVC_INCLUDE_WINDOWFUNCS)
     676                 :         {
     677 GIC           6 :             context->varlist = lappend(context->varlist, node);
     678 ECB             :             /* we do NOT descend into the contained expressions */
     679 GIC           6 :             return false;
     680 ECB             :         }
     681 GIC        2763 :         else if (context->flags & PVC_RECURSE_WINDOWFUNCS)
     682 ECB             :         {
     683                 :             /* fall through to recurse into the windowfunc's arguments */
     684                 :         }
     685                 :         else
     686 UIC           0 :             elog(ERROR, "WindowFunc found where not expected");
     687                 :     }
     688 GIC      942524 :     else if (IsA(node, PlaceHolderVar))
     689 EUB             :     {
     690 GIC        1102 :         if (((PlaceHolderVar *) node)->phlevelsup != 0)
     691 LBC           0 :             elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
     692 GIC        1102 :         if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
     693 ECB             :         {
     694 GBC         912 :             context->varlist = lappend(context->varlist, node);
     695 ECB             :             /* we do NOT descend into the contained expression */
     696 GIC         912 :             return false;
     697 ECB             :         }
     698 GIC         190 :         else if (context->flags & PVC_RECURSE_PLACEHOLDERS)
     699 ECB             :         {
     700                 :             /* fall through to recurse into the placeholder's expression */
     701                 :         }
     702                 :         else
     703 UIC           0 :             elog(ERROR, "PlaceHolderVar found where not expected");
     704                 :     }
     705 GIC      981506 :     return expression_tree_walker(node, pull_var_clause_walker,
     706 EUB             :                                   (void *) context);
     707                 : }
     708 ECB             : 
     709                 : 
     710                 : /*
     711                 :  * flatten_join_alias_vars
     712                 :  *    Replace Vars that reference JOIN outputs with references to the original
     713                 :  *    relation variables instead.  This allows quals involving such vars to be
     714                 :  *    pushed down.  Whole-row Vars that reference JOIN relations are expanded
     715                 :  *    into RowExpr constructs that name the individual output Vars.  This
     716                 :  *    is necessary since we will not scan the JOIN as a base relation, which
     717                 :  *    is the only way that the executor can directly handle whole-row Vars.
     718                 :  *
     719                 :  * This also adjusts relid sets found in some expression node types to
     720                 :  * substitute the contained base+OJ rels for any join relid.
     721                 :  *
     722                 :  * If a JOIN contains sub-selects that have been flattened, its join alias
     723                 :  * entries might now be arbitrary expressions, not just Vars.  This affects
     724                 :  * this function in two important ways.  First, we might find ourselves
     725                 :  * inserting SubLink expressions into subqueries, and we must make sure that
     726                 :  * their Query.hasSubLinks fields get set to true if so.  If there are any
     727                 :  * SubLinks in the join alias lists, the outer Query should already have
     728                 :  * hasSubLinks = true, so this is only relevant to un-flattened subqueries.
     729                 :  * Second, we have to preserve any varnullingrels info attached to the
     730                 :  * alias Vars we're replacing.  If the replacement expression is a Var or
     731                 :  * PlaceHolderVar or constructed from those, we can just add the
     732                 :  * varnullingrels bits to the existing nullingrels field(s); otherwise
     733                 :  * we have to add a PlaceHolderVar wrapper.
     734                 :  *
     735                 :  * NOTE: this is also used by the parser, to expand join alias Vars before
     736                 :  * checking GROUP BY validity.  For that use-case, root will be NULL, which
     737                 :  * is why we have to pass the Query separately.  We need the root itself only
     738                 :  * for making PlaceHolderVars.  We can avoid making PlaceHolderVars in the
     739                 :  * parser's usage because it won't be dealing with arbitrary expressions:
     740                 :  * so long as adjust_standard_join_alias_expression can handle everything
     741                 :  * the parser would make as a join alias expression, we're OK.
     742                 :  */
     743                 : Node *
     744 GNC       90090 : flatten_join_alias_vars(PlannerInfo *root, Query *query, Node *node)
     745                 : {
     746                 :     flatten_join_alias_vars_context context;
     747                 : 
     748                 :     /*
     749                 :      * We do not expect this to be applied to the whole Query, only to
     750                 :      * expressions or LATERAL subqueries.  Hence, if the top node is a Query,
     751                 :      * it's okay to immediately increment sublevels_up.
     752                 :      */
     753           90090 :     Assert(node != (Node *) query);
     754                 : 
     755           90090 :     context.root = root;
     756 GIC       90090 :     context.query = query;
     757           90090 :     context.sublevels_up = 0;
     758                 :     /* flag whether join aliases could possibly contain SubLinks */
     759           90090 :     context.possible_sublink = query->hasSubLinks;
     760                 :     /* if hasSubLinks is already true, no need to work hard */
     761           90090 :     context.inserted_sublink = query->hasSubLinks;
     762                 : 
     763 CBC       90090 :     return flatten_join_alias_vars_mutator(node, &context);
     764                 : }
     765                 : 
     766                 : static Node *
     767 GIC     1237744 : flatten_join_alias_vars_mutator(Node *node,
     768                 :                                 flatten_join_alias_vars_context *context)
     769                 : {
     770         1237744 :     if (node == NULL)
     771          125601 :         return NULL;
     772 CBC     1112143 :     if (IsA(node, Var))
     773                 :     {
     774          324467 :         Var        *var = (Var *) node;
     775 ECB             :         RangeTblEntry *rte;
     776                 :         Node       *newvar;
     777                 : 
     778                 :         /* No change unless Var belongs to a JOIN of the target level */
     779 GIC      324467 :         if (var->varlevelsup != context->sublevels_up)
     780 CBC       22497 :             return node;        /* no need to copy, really */
     781 GIC      301970 :         rte = rt_fetch(var->varno, context->query->rtable);
     782 CBC      301970 :         if (rte->rtekind != RTE_JOIN)
     783 GIC      301703 :             return node;
     784             267 :         if (var->varattno == InvalidAttrNumber)
     785                 :         {
     786 ECB             :             /* Must expand whole-row reference */
     787                 :             RowExpr    *rowexpr;
     788 GIC           3 :             List       *fields = NIL;
     789 CBC           3 :             List       *colnames = NIL;
     790 ECB             :             ListCell   *lv;
     791                 :             ListCell   *ln;
     792                 : 
     793 CBC           3 :             Assert(list_length(rte->joinaliasvars) == list_length(rte->eref->colnames));
     794 GIC          24 :             forboth(lv, rte->joinaliasvars, ln, rte->eref->colnames)
     795                 :             {
     796              21 :                 newvar = (Node *) lfirst(lv);
     797                 :                 /* Ignore dropped columns */
     798 CBC          21 :                 if (newvar == NULL)
     799 LBC           0 :                     continue;
     800 CBC          21 :                 newvar = copyObject(newvar);
     801 ECB             : 
     802                 :                 /*
     803                 :                  * If we are expanding an alias carried down from an upper
     804                 :                  * query, must adjust its varlevelsup fields.
     805                 :                  */
     806 GIC          21 :                 if (context->sublevels_up != 0)
     807 LBC           0 :                     IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
     808 ECB             :                 /* Preserve original Var's location, if possible */
     809 GIC          21 :                 if (IsA(newvar, Var))
     810              21 :                     ((Var *) newvar)->location = var->location;
     811                 :                 /* Recurse in case join input is itself a join */
     812 ECB             :                 /* (also takes care of setting inserted_sublink if needed) */
     813 CBC          21 :                 newvar = flatten_join_alias_vars_mutator(newvar, context);
     814 GIC          21 :                 fields = lappend(fields, newvar);
     815 ECB             :                 /* We need the names of non-dropped columns, too */
     816 GIC          21 :                 colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
     817 ECB             :             }
     818 GBC           3 :             rowexpr = makeNode(RowExpr);
     819 CBC           3 :             rowexpr->args = fields;
     820 GIC           3 :             rowexpr->row_typeid = var->vartype;
     821               3 :             rowexpr->row_format = COERCE_IMPLICIT_CAST;
     822                 :             /* vartype will always be RECORDOID, so we always need colnames */
     823               3 :             rowexpr->colnames = colnames;
     824               3 :             rowexpr->location = var->location;
     825 ECB             : 
     826                 :             /* Lastly, add any varnullingrels to the replacement expression */
     827 GNC           3 :             return add_nullingrels_if_needed(context->root, (Node *) rowexpr,
     828                 :                                              var);
     829                 :         }
     830 ECB             : 
     831                 :         /* Expand join alias reference */
     832 GIC         264 :         Assert(var->varattno > 0);
     833             264 :         newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
     834 CBC         264 :         Assert(newvar != NULL);
     835             264 :         newvar = copyObject(newvar);
     836                 : 
     837 ECB             :         /*
     838                 :          * If we are expanding an alias carried down from an upper query, must
     839                 :          * adjust its varlevelsup fields.
     840                 :          */
     841 CBC         264 :         if (context->sublevels_up != 0)
     842 LBC           0 :             IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
     843                 : 
     844 ECB             :         /* Preserve original Var's location, if possible */
     845 CBC         264 :         if (IsA(newvar, Var))
     846 UIC           0 :             ((Var *) newvar)->location = var->location;
     847                 : 
     848 ECB             :         /* Recurse in case join input is itself a join */
     849 GIC         264 :         newvar = flatten_join_alias_vars_mutator(newvar, context);
     850                 : 
     851                 :         /* Detect if we are adding a sublink to query */
     852             264 :         if (context->possible_sublink && !context->inserted_sublink)
     853 LBC           0 :             context->inserted_sublink = checkExprHasSubLink(newvar);
     854 ECB             : 
     855                 :         /* Lastly, add any varnullingrels to the replacement expression */
     856 GNC         264 :         return add_nullingrels_if_needed(context->root, newvar, var);
     857 ECB             :     }
     858 GIC      787676 :     if (IsA(node, PlaceHolderVar))
     859                 :     {
     860                 :         /* Copy the PlaceHolderVar node with correct mutation of subnodes */
     861                 :         PlaceHolderVar *phv;
     862                 : 
     863 CBC         705 :         phv = (PlaceHolderVar *) expression_tree_mutator(node,
     864 EUB             :                                                          flatten_join_alias_vars_mutator,
     865                 :                                                          (void *) context);
     866                 :         /* now fix PlaceHolderVar's relid sets */
     867 CBC         705 :         if (phv->phlevelsup == context->sublevels_up)
     868 EUB             :         {
     869 GIC         675 :             phv->phrels = alias_relid_set(context->query,
     870                 :                                           phv->phrels);
     871                 :             /* we *don't* change phnullingrels */
     872 ECB             :         }
     873 GIC         705 :         return (Node *) phv;
     874                 :     }
     875 ECB             : 
     876 GBC      786971 :     if (IsA(node, Query))
     877                 :     {
     878                 :         /* Recurse into RTE subquery or not-yet-planned sublink subquery */
     879 ECB             :         Query      *newnode;
     880                 :         bool        save_inserted_sublink;
     881                 : 
     882 GIC        7692 :         context->sublevels_up++;
     883            7692 :         save_inserted_sublink = context->inserted_sublink;
     884            7692 :         context->inserted_sublink = ((Query *) node)->hasSubLinks;
     885            7692 :         newnode = query_tree_mutator((Query *) node,
     886 ECB             :                                      flatten_join_alias_vars_mutator,
     887                 :                                      (void *) context,
     888                 :                                      QTW_IGNORE_JOINALIASES);
     889 GIC        7692 :         newnode->hasSubLinks |= context->inserted_sublink;
     890 CBC        7692 :         context->inserted_sublink = save_inserted_sublink;
     891 GIC        7692 :         context->sublevels_up--;
     892 CBC        7692 :         return (Node *) newnode;
     893                 :     }
     894                 :     /* Already-planned tree not supported */
     895 GIC      779279 :     Assert(!IsA(node, SubPlan));
     896 ECB             :     /* Shouldn't need to handle these planner auxiliary nodes here */
     897 GIC      779279 :     Assert(!IsA(node, SpecialJoinInfo));
     898          779279 :     Assert(!IsA(node, PlaceHolderInfo));
     899 CBC      779279 :     Assert(!IsA(node, MinMaxAggInfo));
     900                 : 
     901 GIC      779279 :     return expression_tree_mutator(node, flatten_join_alias_vars_mutator,
     902                 :                                    (void *) context);
     903                 : }
     904                 : 
     905                 : /*
     906                 :  * Add oldvar's varnullingrels, if any, to a flattened join alias expression.
     907                 :  * The newnode has been copied, so we can modify it freely.
     908                 :  */
     909                 : static Node *
     910 GNC         267 : add_nullingrels_if_needed(PlannerInfo *root, Node *newnode, Var *oldvar)
     911                 : {
     912             267 :     if (oldvar->varnullingrels == NULL)
     913             189 :         return newnode;         /* nothing to do */
     914                 :     /* If possible, do it by adding to existing nullingrel fields */
     915              78 :     if (is_standard_join_alias_expression(newnode, oldvar))
     916              72 :         adjust_standard_join_alias_expression(newnode, oldvar);
     917               6 :     else if (root)
     918                 :     {
     919                 :         /*
     920                 :          * We can insert a PlaceHolderVar to carry the nullingrels.  However,
     921                 :          * deciding where to evaluate the PHV is slightly tricky.  We first
     922                 :          * try to evaluate it at the natural semantic level of the new
     923                 :          * expression; but if that expression is variable-free, fall back to
     924                 :          * evaluating it at the join that the oldvar is an alias Var for.
     925                 :          */
     926                 :         PlaceHolderVar *newphv;
     927               6 :         Index       levelsup = oldvar->varlevelsup;
     928               6 :         Relids      phrels = pull_varnos_of_level(root, newnode, levelsup);
     929                 : 
     930               6 :         if (bms_is_empty(phrels))   /* variable-free? */
     931                 :         {
     932               6 :             if (levelsup != 0)  /* this won't work otherwise */
     933 UNC           0 :                 elog(ERROR, "unsupported join alias expression");
     934 GNC           6 :             phrels = get_relids_for_join(root->parse, oldvar->varno);
     935                 :             /* If it's an outer join, eval below not above the join */
     936               6 :             phrels = bms_del_member(phrels, oldvar->varno);
     937               6 :             Assert(!bms_is_empty(phrels));
     938                 :         }
     939               6 :         newphv = make_placeholder_expr(root, (Expr *) newnode, phrels);
     940                 :         /* newphv has zero phlevelsup and NULL phnullingrels; fix it */
     941               6 :         newphv->phlevelsup = levelsup;
     942               6 :         newphv->phnullingrels = bms_copy(oldvar->varnullingrels);
     943               6 :         newnode = (Node *) newphv;
     944                 :     }
     945                 :     else
     946                 :     {
     947                 :         /* ooops, we're missing support for something the parser can make */
     948 UNC           0 :         elog(ERROR, "unsupported join alias expression");
     949                 :     }
     950 GNC          78 :     return newnode;
     951                 : }
     952                 : 
     953                 : /*
     954                 :  * Check to see if we can insert nullingrels into this join alias expression
     955                 :  * without use of a separate PlaceHolderVar.
     956                 :  *
     957                 :  * This will handle Vars, PlaceHolderVars, and implicit-coercion and COALESCE
     958                 :  * expressions built from those.  This coverage needs to handle anything
     959                 :  * that the parser would put into joinaliasvars.
     960                 :  */
     961                 : static bool
     962             234 : is_standard_join_alias_expression(Node *newnode, Var *oldvar)
     963                 : {
     964             234 :     if (newnode == NULL)
     965 UNC           0 :         return false;
     966 GNC         234 :     if (IsA(newnode, Var) &&
     967             138 :         ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
     968             138 :         return true;
     969              96 :     else if (IsA(newnode, PlaceHolderVar) &&
     970 UNC           0 :              ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
     971               0 :         return true;
     972 GNC          96 :     else if (IsA(newnode, FuncExpr))
     973                 :     {
     974              15 :         FuncExpr   *fexpr = (FuncExpr *) newnode;
     975                 : 
     976                 :         /*
     977                 :          * We need to assume that the function wouldn't produce non-NULL from
     978                 :          * NULL, which is reasonable for implicit coercions but otherwise not
     979                 :          * so much.  (Looking at its strictness is likely overkill, and anyway
     980                 :          * it would cause us to fail if someone forgot to mark an implicit
     981                 :          * coercion as strict.)
     982                 :          */
     983              15 :         if (fexpr->funcformat != COERCE_IMPLICIT_CAST ||
     984              15 :             fexpr->args == NIL)
     985 UNC           0 :             return false;
     986                 : 
     987                 :         /*
     988                 :          * Examine only the first argument --- coercions might have additional
     989                 :          * arguments that are constants.
     990                 :          */
     991 GNC          15 :         return is_standard_join_alias_expression(linitial(fexpr->args), oldvar);
     992                 :     }
     993              81 :     else if (IsA(newnode, RelabelType))
     994                 :     {
     995               9 :         RelabelType *relabel = (RelabelType *) newnode;
     996                 : 
     997                 :         /* This definitely won't produce non-NULL from NULL */
     998               9 :         return is_standard_join_alias_expression((Node *) relabel->arg, oldvar);
     999                 :     }
    1000              72 :     else if (IsA(newnode, CoerceViaIO))
    1001                 :     {
    1002 UNC           0 :         CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
    1003                 : 
    1004                 :         /* This definitely won't produce non-NULL from NULL */
    1005               0 :         return is_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
    1006                 :     }
    1007 GNC          72 :     else if (IsA(newnode, ArrayCoerceExpr))
    1008                 :     {
    1009 UNC           0 :         ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) newnode;
    1010                 : 
    1011                 :         /* This definitely won't produce non-NULL from NULL (at array level) */
    1012               0 :         return is_standard_join_alias_expression((Node *) acoerce->arg, oldvar);
    1013                 :     }
    1014 GNC          72 :     else if (IsA(newnode, CoalesceExpr))
    1015                 :     {
    1016              66 :         CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
    1017                 :         ListCell   *lc;
    1018                 : 
    1019              66 :         Assert(cexpr->args != NIL);
    1020             198 :         foreach(lc, cexpr->args)
    1021                 :         {
    1022             132 :             if (!is_standard_join_alias_expression(lfirst(lc), oldvar))
    1023 UNC           0 :                 return false;
    1024                 :         }
    1025 GNC          66 :         return true;
    1026                 :     }
    1027                 :     else
    1028               6 :         return false;
    1029                 : }
    1030                 : 
    1031                 : /*
    1032                 :  * Insert nullingrels into an expression accepted by
    1033                 :  * is_standard_join_alias_expression.
    1034                 :  */
    1035                 : static void
    1036             222 : adjust_standard_join_alias_expression(Node *newnode, Var *oldvar)
    1037                 : {
    1038             222 :     if (IsA(newnode, Var) &&
    1039             138 :         ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
    1040             138 :     {
    1041             138 :         Var        *newvar = (Var *) newnode;
    1042                 : 
    1043             138 :         newvar->varnullingrels = bms_add_members(newvar->varnullingrels,
    1044             138 :                                                  oldvar->varnullingrels);
    1045                 :     }
    1046              84 :     else if (IsA(newnode, PlaceHolderVar) &&
    1047 UNC           0 :              ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
    1048               0 :     {
    1049               0 :         PlaceHolderVar *newphv = (PlaceHolderVar *) newnode;
    1050                 : 
    1051               0 :         newphv->phnullingrels = bms_add_members(newphv->phnullingrels,
    1052               0 :                                                 oldvar->varnullingrels);
    1053                 :     }
    1054 GNC          84 :     else if (IsA(newnode, FuncExpr))
    1055                 :     {
    1056               9 :         FuncExpr   *fexpr = (FuncExpr *) newnode;
    1057                 : 
    1058               9 :         adjust_standard_join_alias_expression(linitial(fexpr->args), oldvar);
    1059                 :     }
    1060              75 :     else if (IsA(newnode, RelabelType))
    1061                 :     {
    1062               9 :         RelabelType *relabel = (RelabelType *) newnode;
    1063                 : 
    1064               9 :         adjust_standard_join_alias_expression((Node *) relabel->arg, oldvar);
    1065                 :     }
    1066              66 :     else if (IsA(newnode, CoerceViaIO))
    1067                 :     {
    1068 UNC           0 :         CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
    1069                 : 
    1070               0 :         adjust_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
    1071                 :     }
    1072 GNC          66 :     else if (IsA(newnode, ArrayCoerceExpr))
    1073                 :     {
    1074 UNC           0 :         ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) newnode;
    1075                 : 
    1076               0 :         adjust_standard_join_alias_expression((Node *) acoerce->arg, oldvar);
    1077                 :     }
    1078 GNC          66 :     else if (IsA(newnode, CoalesceExpr))
    1079                 :     {
    1080              66 :         CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
    1081                 :         ListCell   *lc;
    1082                 : 
    1083              66 :         Assert(cexpr->args != NIL);
    1084             198 :         foreach(lc, cexpr->args)
    1085                 :         {
    1086             132 :             adjust_standard_join_alias_expression(lfirst(lc), oldvar);
    1087                 :         }
    1088                 :     }
    1089                 :     else
    1090 UNC           0 :         Assert(false);
    1091 GNC         222 : }
    1092                 : 
    1093 ECB             : /*
    1094                 :  * alias_relid_set: in a set of RT indexes, replace joins by their
    1095                 :  * underlying base+OJ relids
    1096                 :  */
    1097                 : static Relids
    1098 GIC         675 : alias_relid_set(Query *query, Relids relids)
    1099                 : {
    1100 CBC         675 :     Relids      result = NULL;
    1101 ECB             :     int         rtindex;
    1102                 : 
    1103 CBC         675 :     rtindex = -1;
    1104 GIC        1739 :     while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
    1105                 :     {
    1106 CBC        1064 :         RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
    1107                 : 
    1108            1064 :         if (rte->rtekind == RTE_JOIN)
    1109             134 :             result = bms_join(result, get_relids_for_join(query, rtindex));
    1110 ECB             :         else
    1111 GIC         930 :             result = bms_add_member(result, rtindex);
    1112 ECB             :     }
    1113 GIC         675 :     return result;
    1114                 : }
        

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