LCOV - code coverage report
Current view: top level - src/backend/optimizer/util - var.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 240 263 91.3 %
Date: 2019-09-19 16:06:56 Functions: 18 18 100.0 %
Legend: Lines: hit not hit

          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-2019, 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/prep.h"
      27             : #include "parser/parsetree.h"
      28             : #include "rewrite/rewriteManip.h"
      29             : 
      30             : 
      31             : typedef struct
      32             : {
      33             :     Relids      varnos;
      34             :     int         sublevels_up;
      35             : } pull_varnos_context;
      36             : 
      37             : typedef struct
      38             : {
      39             :     Bitmapset  *varattnos;
      40             :     Index       varno;
      41             : } pull_varattnos_context;
      42             : 
      43             : typedef struct
      44             : {
      45             :     List       *vars;
      46             :     int         sublevels_up;
      47             : } pull_vars_context;
      48             : 
      49             : typedef struct
      50             : {
      51             :     int         var_location;
      52             :     int         sublevels_up;
      53             : } locate_var_of_level_context;
      54             : 
      55             : typedef struct
      56             : {
      57             :     List       *varlist;
      58             :     int         flags;
      59             : } pull_var_clause_context;
      60             : 
      61             : typedef struct
      62             : {
      63             :     Query      *query;          /* outer Query */
      64             :     int         sublevels_up;
      65             :     bool        possible_sublink;   /* could aliases include a SubLink? */
      66             :     bool        inserted_sublink;   /* have we inserted a SubLink? */
      67             : } flatten_join_alias_vars_context;
      68             : 
      69             : static bool pull_varnos_walker(Node *node,
      70             :                                pull_varnos_context *context);
      71             : static bool pull_varattnos_walker(Node *node, pull_varattnos_context *context);
      72             : static bool pull_vars_walker(Node *node, pull_vars_context *context);
      73             : static bool contain_var_clause_walker(Node *node, void *context);
      74             : static bool contain_vars_of_level_walker(Node *node, int *sublevels_up);
      75             : static bool locate_var_of_level_walker(Node *node,
      76             :                                        locate_var_of_level_context *context);
      77             : static bool pull_var_clause_walker(Node *node,
      78             :                                    pull_var_clause_context *context);
      79             : static Node *flatten_join_alias_vars_mutator(Node *node,
      80             :                                              flatten_join_alias_vars_context *context);
      81             : static Relids alias_relid_set(Query *query, Relids relids);
      82             : 
      83             : 
      84             : /*
      85             :  * pull_varnos
      86             :  *      Create a set of all the distinct varnos present in a parsetree.
      87             :  *      Only varnos that reference level-zero rtable entries are considered.
      88             :  *
      89             :  * NOTE: this is used on not-yet-planned expressions.  It may therefore find
      90             :  * bare SubLinks, and if so it needs to recurse into them to look for uplevel
      91             :  * references to the desired rtable level!  But when we find a completed
      92             :  * SubPlan, we only need to look at the parameters passed to the subplan.
      93             :  */
      94             : Relids
      95     2307284 : pull_varnos(Node *node)
      96             : {
      97             :     pull_varnos_context context;
      98             : 
      99     2307284 :     context.varnos = NULL;
     100     2307284 :     context.sublevels_up = 0;
     101             : 
     102             :     /*
     103             :      * Must be prepared to start with a Query or a bare expression tree; if
     104             :      * it's a Query, we don't want to increment sublevels_up.
     105             :      */
     106     2307284 :     query_or_expression_tree_walker(node,
     107             :                                     pull_varnos_walker,
     108             :                                     (void *) &context,
     109             :                                     0);
     110             : 
     111     2307284 :     return context.varnos;
     112             : }
     113             : 
     114             : /*
     115             :  * pull_varnos_of_level
     116             :  *      Create a set of all the distinct varnos present in a parsetree.
     117             :  *      Only Vars of the specified level are considered.
     118             :  */
     119             : Relids
     120         588 : pull_varnos_of_level(Node *node, int levelsup)
     121             : {
     122             :     pull_varnos_context context;
     123             : 
     124         588 :     context.varnos = NULL;
     125         588 :     context.sublevels_up = levelsup;
     126             : 
     127             :     /*
     128             :      * Must be prepared to start with a Query or a bare expression tree; if
     129             :      * it's a Query, we don't want to increment sublevels_up.
     130             :      */
     131         588 :     query_or_expression_tree_walker(node,
     132             :                                     pull_varnos_walker,
     133             :                                     (void *) &context,
     134             :                                     0);
     135             : 
     136         588 :     return context.varnos;
     137             : }
     138             : 
     139             : static bool
     140     3351012 : pull_varnos_walker(Node *node, pull_varnos_context *context)
     141             : {
     142     3351012 :     if (node == NULL)
     143       18208 :         return false;
     144     3332804 :     if (IsA(node, Var))
     145             :     {
     146     2018866 :         Var        *var = (Var *) node;
     147             : 
     148     2018866 :         if (var->varlevelsup == context->sublevels_up)
     149     2017950 :             context->varnos = bms_add_member(context->varnos, var->varno);
     150     2018866 :         return false;
     151             :     }
     152     1313938 :     if (IsA(node, CurrentOfExpr))
     153             :     {
     154         760 :         CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
     155             : 
     156         760 :         if (context->sublevels_up == 0)
     157         760 :             context->varnos = bms_add_member(context->varnos, cexpr->cvarno);
     158         760 :         return false;
     159             :     }
     160     1313178 :     if (IsA(node, PlaceHolderVar))
     161             :     {
     162             :         /*
     163             :          * A PlaceHolderVar acts as a variable of its syntactic scope, or
     164             :          * lower than that if it references only a subset of the rels in its
     165             :          * syntactic scope.  It might also contain lateral references, but we
     166             :          * should ignore such references when computing the set of varnos in
     167             :          * an expression tree.  Also, if the PHV contains no variables within
     168             :          * its syntactic scope, it will be forced to be evaluated exactly at
     169             :          * the syntactic scope, so take that as the relid set.
     170             :          */
     171        1752 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
     172             :         pull_varnos_context subcontext;
     173             : 
     174        1752 :         subcontext.varnos = NULL;
     175        1752 :         subcontext.sublevels_up = context->sublevels_up;
     176        1752 :         (void) pull_varnos_walker((Node *) phv->phexpr, &subcontext);
     177        1752 :         if (phv->phlevelsup == context->sublevels_up)
     178             :         {
     179        1752 :             subcontext.varnos = bms_int_members(subcontext.varnos,
     180        1752 :                                                 phv->phrels);
     181        1752 :             if (bms_is_empty(subcontext.varnos))
     182        1268 :                 context->varnos = bms_add_members(context->varnos,
     183        1268 :                                                   phv->phrels);
     184             :         }
     185        1752 :         context->varnos = bms_join(context->varnos, subcontext.varnos);
     186        1752 :         return false;
     187             :     }
     188     1311426 :     if (IsA(node, Query))
     189             :     {
     190             :         /* Recurse into RTE subquery or not-yet-planned sublink subquery */
     191             :         bool        result;
     192             : 
     193          24 :         context->sublevels_up++;
     194          24 :         result = query_tree_walker((Query *) node, pull_varnos_walker,
     195             :                                    (void *) context, 0);
     196          24 :         context->sublevels_up--;
     197          24 :         return result;
     198             :     }
     199     1311402 :     return expression_tree_walker(node, pull_varnos_walker,
     200             :                                   (void *) context);
     201             : }
     202             : 
     203             : 
     204             : /*
     205             :  * pull_varattnos
     206             :  *      Find all the distinct attribute numbers present in an expression tree,
     207             :  *      and add them to the initial contents of *varattnos.
     208             :  *      Only Vars of the given varno and rtable level zero are considered.
     209             :  *
     210             :  * Attribute numbers are offset by FirstLowInvalidHeapAttributeNumber so that
     211             :  * we can include system attributes (e.g., OID) in the bitmap representation.
     212             :  *
     213             :  * Currently, this does not support unplanned subqueries; that is not needed
     214             :  * for current uses.  It will handle already-planned SubPlan nodes, though,
     215             :  * looking into only the "testexpr" and the "args" list.  (The subplan cannot
     216             :  * contain any other references to Vars of the current level.)
     217             :  */
     218             : void
     219      885420 : pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
     220             : {
     221             :     pull_varattnos_context context;
     222             : 
     223      885420 :     context.varattnos = *varattnos;
     224      885420 :     context.varno = varno;
     225             : 
     226      885420 :     (void) pull_varattnos_walker(node, &context);
     227             : 
     228      885420 :     *varattnos = context.varattnos;
     229      885420 : }
     230             : 
     231             : static bool
     232     3409186 : pull_varattnos_walker(Node *node, pull_varattnos_context *context)
     233             : {
     234     3409186 :     if (node == NULL)
     235       40368 :         return false;
     236     3368818 :     if (IsA(node, Var))
     237             :     {
     238     2135942 :         Var        *var = (Var *) node;
     239             : 
     240     2135942 :         if (var->varno == context->varno && var->varlevelsup == 0)
     241     2135900 :             context->varattnos =
     242     2135900 :                 bms_add_member(context->varattnos,
     243     2135900 :                                var->varattno - FirstLowInvalidHeapAttributeNumber);
     244     2135942 :         return false;
     245             :     }
     246             : 
     247             :     /* Should not find an unplanned subquery */
     248             :     Assert(!IsA(node, Query));
     249             : 
     250     1232876 :     return expression_tree_walker(node, pull_varattnos_walker,
     251             :                                   (void *) context);
     252             : }
     253             : 
     254             : 
     255             : /*
     256             :  * pull_vars_of_level
     257             :  *      Create a list of all Vars (and PlaceHolderVars) referencing the
     258             :  *      specified query level in the given parsetree.
     259             :  *
     260             :  * Caution: the Vars are not copied, only linked into the list.
     261             :  */
     262             : List *
     263         696 : pull_vars_of_level(Node *node, int levelsup)
     264             : {
     265             :     pull_vars_context context;
     266             : 
     267         696 :     context.vars = NIL;
     268         696 :     context.sublevels_up = levelsup;
     269             : 
     270             :     /*
     271             :      * Must be prepared to start with a Query or a bare expression tree; if
     272             :      * it's a Query, we don't want to increment sublevels_up.
     273             :      */
     274         696 :     query_or_expression_tree_walker(node,
     275             :                                     pull_vars_walker,
     276             :                                     (void *) &context,
     277             :                                     0);
     278             : 
     279         696 :     return context.vars;
     280             : }
     281             : 
     282             : static bool
     283       10952 : pull_vars_walker(Node *node, pull_vars_context *context)
     284             : {
     285       10952 :     if (node == NULL)
     286        4032 :         return false;
     287        6920 :     if (IsA(node, Var))
     288             :     {
     289        1488 :         Var        *var = (Var *) node;
     290             : 
     291        1488 :         if (var->varlevelsup == context->sublevels_up)
     292         984 :             context->vars = lappend(context->vars, var);
     293        1488 :         return false;
     294             :     }
     295        5432 :     if (IsA(node, PlaceHolderVar))
     296             :     {
     297          24 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
     298             : 
     299          24 :         if (phv->phlevelsup == context->sublevels_up)
     300          24 :             context->vars = lappend(context->vars, phv);
     301             :         /* we don't want to look into the contained expression */
     302          24 :         return false;
     303             :     }
     304        5408 :     if (IsA(node, Query))
     305             :     {
     306             :         /* Recurse into RTE subquery or not-yet-planned sublink subquery */
     307             :         bool        result;
     308             : 
     309         100 :         context->sublevels_up++;
     310         100 :         result = query_tree_walker((Query *) node, pull_vars_walker,
     311             :                                    (void *) context, 0);
     312         100 :         context->sublevels_up--;
     313         100 :         return result;
     314             :     }
     315        5308 :     return expression_tree_walker(node, pull_vars_walker,
     316             :                                   (void *) context);
     317             : }
     318             : 
     319             : 
     320             : /*
     321             :  * contain_var_clause
     322             :  *    Recursively scan a clause to discover whether it contains any Var nodes
     323             :  *    (of the current query level).
     324             :  *
     325             :  *    Returns true if any varnode found.
     326             :  *
     327             :  * Does not examine subqueries, therefore must only be used after reduction
     328             :  * of sublinks to subplans!
     329             :  */
     330             : bool
     331       15964 : contain_var_clause(Node *node)
     332             : {
     333       15964 :     return contain_var_clause_walker(node, NULL);
     334             : }
     335             : 
     336             : static bool
     337       22626 : contain_var_clause_walker(Node *node, void *context)
     338             : {
     339       22626 :     if (node == NULL)
     340          60 :         return false;
     341       22566 :     if (IsA(node, Var))
     342             :     {
     343        1644 :         if (((Var *) node)->varlevelsup == 0)
     344        1644 :             return true;        /* abort the tree traversal and return true */
     345           0 :         return false;
     346             :     }
     347       20922 :     if (IsA(node, CurrentOfExpr))
     348           0 :         return true;
     349       20922 :     if (IsA(node, PlaceHolderVar))
     350             :     {
     351           0 :         if (((PlaceHolderVar *) node)->phlevelsup == 0)
     352           0 :             return true;        /* abort the tree traversal and return true */
     353             :         /* else fall through to check the contained expr */
     354             :     }
     355       20922 :     return expression_tree_walker(node, contain_var_clause_walker, context);
     356             : }
     357             : 
     358             : 
     359             : /*
     360             :  * contain_vars_of_level
     361             :  *    Recursively scan a clause to discover whether it contains any Var nodes
     362             :  *    of the specified query level.
     363             :  *
     364             :  *    Returns true if any such Var found.
     365             :  *
     366             :  * Will recurse into sublinks.  Also, may be invoked directly on a Query.
     367             :  */
     368             : bool
     369      159606 : contain_vars_of_level(Node *node, int levelsup)
     370             : {
     371      159606 :     int         sublevels_up = levelsup;
     372             : 
     373      159606 :     return query_or_expression_tree_walker(node,
     374             :                                            contain_vars_of_level_walker,
     375             :                                            (void *) &sublevels_up,
     376             :                                            0);
     377             : }
     378             : 
     379             : static bool
     380      584666 : contain_vars_of_level_walker(Node *node, int *sublevels_up)
     381             : {
     382      584666 :     if (node == NULL)
     383      135796 :         return false;
     384      448870 :     if (IsA(node, Var))
     385             :     {
     386       86696 :         if (((Var *) node)->varlevelsup == *sublevels_up)
     387       26796 :             return true;        /* abort tree traversal and return true */
     388       59900 :         return false;
     389             :     }
     390      362174 :     if (IsA(node, CurrentOfExpr))
     391             :     {
     392         124 :         if (*sublevels_up == 0)
     393         124 :             return true;
     394           0 :         return false;
     395             :     }
     396      362050 :     if (IsA(node, PlaceHolderVar))
     397             :     {
     398          16 :         if (((PlaceHolderVar *) node)->phlevelsup == *sublevels_up)
     399          16 :             return true;        /* abort the tree traversal and return true */
     400             :         /* else fall through to check the contained expr */
     401             :     }
     402      362034 :     if (IsA(node, Query))
     403             :     {
     404             :         /* Recurse into subselects */
     405             :         bool        result;
     406             : 
     407        2316 :         (*sublevels_up)++;
     408        2316 :         result = query_tree_walker((Query *) node,
     409             :                                    contain_vars_of_level_walker,
     410             :                                    (void *) sublevels_up,
     411             :                                    0);
     412        2316 :         (*sublevels_up)--;
     413        2316 :         return result;
     414             :     }
     415      359718 :     return expression_tree_walker(node,
     416             :                                   contain_vars_of_level_walker,
     417             :                                   (void *) sublevels_up);
     418             : }
     419             : 
     420             : 
     421             : /*
     422             :  * locate_var_of_level
     423             :  *    Find the parse location of any Var of the specified query level.
     424             :  *
     425             :  * Returns -1 if no such Var is in the querytree, or if they all have
     426             :  * unknown parse location.  (The former case is probably caller error,
     427             :  * but we don't bother to distinguish it from the latter case.)
     428             :  *
     429             :  * Will recurse into sublinks.  Also, may be invoked directly on a Query.
     430             :  *
     431             :  * Note: it might seem appropriate to merge this functionality into
     432             :  * contain_vars_of_level, but that would complicate that function's API.
     433             :  * Currently, the only uses of this function are for error reporting,
     434             :  * and so shaving cycles probably isn't very important.
     435             :  */
     436             : int
     437           4 : locate_var_of_level(Node *node, int levelsup)
     438             : {
     439             :     locate_var_of_level_context context;
     440             : 
     441           4 :     context.var_location = -1;  /* in case we find nothing */
     442           4 :     context.sublevels_up = levelsup;
     443             : 
     444           4 :     (void) query_or_expression_tree_walker(node,
     445             :                                            locate_var_of_level_walker,
     446             :                                            (void *) &context,
     447             :                                            0);
     448             : 
     449           4 :     return context.var_location;
     450             : }
     451             : 
     452             : static bool
     453          12 : locate_var_of_level_walker(Node *node,
     454             :                            locate_var_of_level_context *context)
     455             : {
     456          12 :     if (node == NULL)
     457           0 :         return false;
     458          12 :     if (IsA(node, Var))
     459             :     {
     460           4 :         Var        *var = (Var *) node;
     461             : 
     462           8 :         if (var->varlevelsup == context->sublevels_up &&
     463           4 :             var->location >= 0)
     464             :         {
     465           4 :             context->var_location = var->location;
     466           4 :             return true;        /* abort tree traversal and return true */
     467             :         }
     468           0 :         return false;
     469             :     }
     470           8 :     if (IsA(node, CurrentOfExpr))
     471             :     {
     472             :         /* since CurrentOfExpr doesn't carry location, nothing we can do */
     473           0 :         return false;
     474             :     }
     475             :     /* No extra code needed for PlaceHolderVar; just look in contained expr */
     476           8 :     if (IsA(node, Query))
     477             :     {
     478             :         /* Recurse into subselects */
     479             :         bool        result;
     480             : 
     481           0 :         context->sublevels_up++;
     482           0 :         result = query_tree_walker((Query *) node,
     483             :                                    locate_var_of_level_walker,
     484             :                                    (void *) context,
     485             :                                    0);
     486           0 :         context->sublevels_up--;
     487           0 :         return result;
     488             :     }
     489           8 :     return expression_tree_walker(node,
     490             :                                   locate_var_of_level_walker,
     491             :                                   (void *) context);
     492             : }
     493             : 
     494             : 
     495             : /*
     496             :  * pull_var_clause
     497             :  *    Recursively pulls all Var nodes from an expression clause.
     498             :  *
     499             :  *    Aggrefs are handled according to these bits in 'flags':
     500             :  *      PVC_INCLUDE_AGGREGATES      include Aggrefs in output list
     501             :  *      PVC_RECURSE_AGGREGATES      recurse into Aggref arguments
     502             :  *      neither flag                throw error if Aggref found
     503             :  *    Vars within an Aggref's expression are included in the result only
     504             :  *    when PVC_RECURSE_AGGREGATES is specified.
     505             :  *
     506             :  *    WindowFuncs are handled according to these bits in 'flags':
     507             :  *      PVC_INCLUDE_WINDOWFUNCS     include WindowFuncs in output list
     508             :  *      PVC_RECURSE_WINDOWFUNCS     recurse into WindowFunc arguments
     509             :  *      neither flag                throw error if WindowFunc found
     510             :  *    Vars within a WindowFunc's expression are included in the result only
     511             :  *    when PVC_RECURSE_WINDOWFUNCS is specified.
     512             :  *
     513             :  *    PlaceHolderVars are handled according to these bits in 'flags':
     514             :  *      PVC_INCLUDE_PLACEHOLDERS    include PlaceHolderVars in output list
     515             :  *      PVC_RECURSE_PLACEHOLDERS    recurse into PlaceHolderVar arguments
     516             :  *      neither flag                throw error if PlaceHolderVar found
     517             :  *    Vars within a PHV's expression are included in the result only
     518             :  *    when PVC_RECURSE_PLACEHOLDERS is specified.
     519             :  *
     520             :  *    GroupingFuncs are treated mostly like Aggrefs, and so do not need
     521             :  *    their own flag bits.
     522             :  *
     523             :  *    CurrentOfExpr nodes are ignored in all cases.
     524             :  *
     525             :  *    Upper-level vars (with varlevelsup > 0) should not be seen here,
     526             :  *    likewise for upper-level Aggrefs and PlaceHolderVars.
     527             :  *
     528             :  *    Returns list of nodes found.  Note the nodes themselves are not
     529             :  *    copied, only referenced.
     530             :  *
     531             :  * Does not examine subqueries, therefore must only be used after reduction
     532             :  * of sublinks to subplans!
     533             :  */
     534             : List *
     535      348258 : pull_var_clause(Node *node, int flags)
     536             : {
     537             :     pull_var_clause_context context;
     538             : 
     539             :     /* Assert that caller has not specified inconsistent flags */
     540             :     Assert((flags & (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES))
     541             :            != (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES));
     542             :     Assert((flags & (PVC_INCLUDE_WINDOWFUNCS | PVC_RECURSE_WINDOWFUNCS))
     543             :            != (PVC_INCLUDE_WINDOWFUNCS | PVC_RECURSE_WINDOWFUNCS));
     544             :     Assert((flags & (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS))
     545             :            != (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS));
     546             : 
     547      348258 :     context.varlist = NIL;
     548      348258 :     context.flags = flags;
     549             : 
     550      348258 :     pull_var_clause_walker(node, &context);
     551      348258 :     return context.varlist;
     552             : }
     553             : 
     554             : static bool
     555     2866764 : pull_var_clause_walker(Node *node, pull_var_clause_context *context)
     556             : {
     557     2866764 :     if (node == NULL)
     558      113944 :         return false;
     559     2752820 :     if (IsA(node, Var))
     560             :     {
     561     1047640 :         if (((Var *) node)->varlevelsup != 0)
     562           0 :             elog(ERROR, "Upper-level Var found where not expected");
     563     1047640 :         context->varlist = lappend(context->varlist, node);
     564     1047640 :         return false;
     565             :     }
     566     1705180 :     else if (IsA(node, Aggref))
     567             :     {
     568       53876 :         if (((Aggref *) node)->agglevelsup != 0)
     569           0 :             elog(ERROR, "Upper-level Aggref found where not expected");
     570       53876 :         if (context->flags & PVC_INCLUDE_AGGREGATES)
     571             :         {
     572        2950 :             context->varlist = lappend(context->varlist, node);
     573             :             /* we do NOT descend into the contained expression */
     574        2950 :             return false;
     575             :         }
     576       50926 :         else if (context->flags & PVC_RECURSE_AGGREGATES)
     577             :         {
     578             :             /* fall through to recurse into the aggregate's arguments */
     579             :         }
     580             :         else
     581           0 :             elog(ERROR, "Aggref found where not expected");
     582             :     }
     583     1651304 :     else if (IsA(node, GroupingFunc))
     584             :     {
     585         320 :         if (((GroupingFunc *) node)->agglevelsup != 0)
     586           0 :             elog(ERROR, "Upper-level GROUPING found where not expected");
     587         320 :         if (context->flags & PVC_INCLUDE_AGGREGATES)
     588             :         {
     589           4 :             context->varlist = lappend(context->varlist, node);
     590             :             /* we do NOT descend into the contained expression */
     591           4 :             return false;
     592             :         }
     593         316 :         else if (context->flags & PVC_RECURSE_AGGREGATES)
     594             :         {
     595             :             /*
     596             :              * We do NOT descend into the contained expression, even if the
     597             :              * caller asked for it, because we never actually evaluate it -
     598             :              * the result is driven entirely off the associated GROUP BY
     599             :              * clause, so we never need to extract the actual Vars here.
     600             :              */
     601         316 :             return false;
     602             :         }
     603             :         else
     604           0 :             elog(ERROR, "GROUPING found where not expected");
     605             :     }
     606     1650984 :     else if (IsA(node, WindowFunc))
     607             :     {
     608             :         /* WindowFuncs have no levelsup field to check ... */
     609        2888 :         if (context->flags & PVC_INCLUDE_WINDOWFUNCS)
     610             :         {
     611           8 :             context->varlist = lappend(context->varlist, node);
     612             :             /* we do NOT descend into the contained expressions */
     613           8 :             return false;
     614             :         }
     615        2880 :         else if (context->flags & PVC_RECURSE_WINDOWFUNCS)
     616             :         {
     617             :             /* fall through to recurse into the windowfunc's arguments */
     618             :         }
     619             :         else
     620           0 :             elog(ERROR, "WindowFunc found where not expected");
     621             :     }
     622     1648096 :     else if (IsA(node, PlaceHolderVar))
     623             :     {
     624         984 :         if (((PlaceHolderVar *) node)->phlevelsup != 0)
     625           0 :             elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
     626         984 :         if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
     627             :         {
     628         888 :             context->varlist = lappend(context->varlist, node);
     629             :             /* we do NOT descend into the contained expression */
     630         888 :             return false;
     631             :         }
     632          96 :         else if (context->flags & PVC_RECURSE_PLACEHOLDERS)
     633             :         {
     634             :             /* fall through to recurse into the placeholder's expression */
     635             :         }
     636             :         else
     637           0 :             elog(ERROR, "PlaceHolderVar found where not expected");
     638             :     }
     639     1701014 :     return expression_tree_walker(node, pull_var_clause_walker,
     640             :                                   (void *) context);
     641             : }
     642             : 
     643             : 
     644             : /*
     645             :  * flatten_join_alias_vars
     646             :  *    Replace Vars that reference JOIN outputs with references to the original
     647             :  *    relation variables instead.  This allows quals involving such vars to be
     648             :  *    pushed down.  Whole-row Vars that reference JOIN relations are expanded
     649             :  *    into RowExpr constructs that name the individual output Vars.  This
     650             :  *    is necessary since we will not scan the JOIN as a base relation, which
     651             :  *    is the only way that the executor can directly handle whole-row Vars.
     652             :  *
     653             :  * This also adjusts relid sets found in some expression node types to
     654             :  * substitute the contained base rels for any join relid.
     655             :  *
     656             :  * If a JOIN contains sub-selects that have been flattened, its join alias
     657             :  * entries might now be arbitrary expressions, not just Vars.  This affects
     658             :  * this function in one important way: we might find ourselves inserting
     659             :  * SubLink expressions into subqueries, and we must make sure that their
     660             :  * Query.hasSubLinks fields get set to true if so.  If there are any
     661             :  * SubLinks in the join alias lists, the outer Query should already have
     662             :  * hasSubLinks = true, so this is only relevant to un-flattened subqueries.
     663             :  *
     664             :  * NOTE: this is used on not-yet-planned expressions.  We do not expect it
     665             :  * to be applied directly to the whole Query, so if we see a Query to start
     666             :  * with, we do want to increment sublevels_up (this occurs for LATERAL
     667             :  * subqueries).
     668             :  */
     669             : Node *
     670      162624 : flatten_join_alias_vars(Query *query, Node *node)
     671             : {
     672             :     flatten_join_alias_vars_context context;
     673             : 
     674      162624 :     context.query = query;
     675      162624 :     context.sublevels_up = 0;
     676             :     /* flag whether join aliases could possibly contain SubLinks */
     677      162624 :     context.possible_sublink = query->hasSubLinks;
     678             :     /* if hasSubLinks is already true, no need to work hard */
     679      162624 :     context.inserted_sublink = query->hasSubLinks;
     680             : 
     681      162624 :     return flatten_join_alias_vars_mutator(node, &context);
     682             : }
     683             : 
     684             : static Node *
     685     3468168 : flatten_join_alias_vars_mutator(Node *node,
     686             :                                 flatten_join_alias_vars_context *context)
     687             : {
     688     3468168 :     if (node == NULL)
     689      483900 :         return NULL;
     690     2984268 :     if (IsA(node, Var))
     691             :     {
     692      938894 :         Var        *var = (Var *) node;
     693             :         RangeTblEntry *rte;
     694             :         Node       *newvar;
     695             : 
     696             :         /* No change unless Var belongs to a JOIN of the target level */
     697      938894 :         if (var->varlevelsup != context->sublevels_up)
     698      119410 :             return node;        /* no need to copy, really */
     699      819484 :         rte = rt_fetch(var->varno, context->query->rtable);
     700      819484 :         if (rte->rtekind != RTE_JOIN)
     701      672872 :             return node;
     702      146612 :         if (var->varattno == InvalidAttrNumber)
     703             :         {
     704             :             /* Must expand whole-row reference */
     705             :             RowExpr    *rowexpr;
     706           4 :             List       *fields = NIL;
     707           4 :             List       *colnames = NIL;
     708             :             AttrNumber  attnum;
     709             :             ListCell   *lv;
     710             :             ListCell   *ln;
     711             : 
     712           4 :             attnum = 0;
     713             :             Assert(list_length(rte->joinaliasvars) == list_length(rte->eref->colnames));
     714          32 :             forboth(lv, rte->joinaliasvars, ln, rte->eref->colnames)
     715             :             {
     716          28 :                 newvar = (Node *) lfirst(lv);
     717          28 :                 attnum++;
     718             :                 /* Ignore dropped columns */
     719          28 :                 if (newvar == NULL)
     720           0 :                     continue;
     721          28 :                 newvar = copyObject(newvar);
     722             : 
     723             :                 /*
     724             :                  * If we are expanding an alias carried down from an upper
     725             :                  * query, must adjust its varlevelsup fields.
     726             :                  */
     727          28 :                 if (context->sublevels_up != 0)
     728           0 :                     IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
     729             :                 /* Preserve original Var's location, if possible */
     730          28 :                 if (IsA(newvar, Var))
     731          28 :                     ((Var *) newvar)->location = var->location;
     732             :                 /* Recurse in case join input is itself a join */
     733             :                 /* (also takes care of setting inserted_sublink if needed) */
     734          28 :                 newvar = flatten_join_alias_vars_mutator(newvar, context);
     735          28 :                 fields = lappend(fields, newvar);
     736             :                 /* We need the names of non-dropped columns, too */
     737          28 :                 colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
     738             :             }
     739           4 :             rowexpr = makeNode(RowExpr);
     740           4 :             rowexpr->args = fields;
     741           4 :             rowexpr->row_typeid = var->vartype;
     742           4 :             rowexpr->row_format = COERCE_IMPLICIT_CAST;
     743           4 :             rowexpr->colnames = colnames;
     744           4 :             rowexpr->location = var->location;
     745             : 
     746           4 :             return (Node *) rowexpr;
     747             :         }
     748             : 
     749             :         /* Expand join alias reference */
     750             :         Assert(var->varattno > 0);
     751      146608 :         newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
     752             :         Assert(newvar != NULL);
     753      146608 :         newvar = copyObject(newvar);
     754             : 
     755             :         /*
     756             :          * If we are expanding an alias carried down from an upper query, must
     757             :          * adjust its varlevelsup fields.
     758             :          */
     759      146608 :         if (context->sublevels_up != 0)
     760        7848 :             IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
     761             : 
     762             :         /* Preserve original Var's location, if possible */
     763      146608 :         if (IsA(newvar, Var))
     764      146172 :             ((Var *) newvar)->location = var->location;
     765             : 
     766             :         /* Recurse in case join input is itself a join */
     767      146608 :         newvar = flatten_join_alias_vars_mutator(newvar, context);
     768             : 
     769             :         /* Detect if we are adding a sublink to query */
     770      146608 :         if (context->possible_sublink && !context->inserted_sublink)
     771        6696 :             context->inserted_sublink = checkExprHasSubLink(newvar);
     772             : 
     773      146608 :         return newvar;
     774             :     }
     775     2045374 :     if (IsA(node, PlaceHolderVar))
     776             :     {
     777             :         /* Copy the PlaceHolderVar node with correct mutation of subnodes */
     778             :         PlaceHolderVar *phv;
     779             : 
     780         740 :         phv = (PlaceHolderVar *) expression_tree_mutator(node,
     781             :                                                          flatten_join_alias_vars_mutator,
     782             :                                                          (void *) context);
     783             :         /* now fix PlaceHolderVar's relid sets */
     784         740 :         if (phv->phlevelsup == context->sublevels_up)
     785             :         {
     786         708 :             phv->phrels = alias_relid_set(context->query,
     787             :                                           phv->phrels);
     788             :         }
     789         740 :         return (Node *) phv;
     790             :     }
     791             : 
     792     2044634 :     if (IsA(node, Query))
     793             :     {
     794             :         /* Recurse into RTE subquery or not-yet-planned sublink subquery */
     795             :         Query      *newnode;
     796             :         bool        save_inserted_sublink;
     797             : 
     798       37810 :         context->sublevels_up++;
     799       37810 :         save_inserted_sublink = context->inserted_sublink;
     800       37810 :         context->inserted_sublink = ((Query *) node)->hasSubLinks;
     801       37810 :         newnode = query_tree_mutator((Query *) node,
     802             :                                      flatten_join_alias_vars_mutator,
     803             :                                      (void *) context,
     804             :                                      QTW_IGNORE_JOINALIASES);
     805       37810 :         newnode->hasSubLinks |= context->inserted_sublink;
     806       37810 :         context->inserted_sublink = save_inserted_sublink;
     807       37810 :         context->sublevels_up--;
     808       37810 :         return (Node *) newnode;
     809             :     }
     810             :     /* Already-planned tree not supported */
     811             :     Assert(!IsA(node, SubPlan));
     812             :     /* Shouldn't need to handle these planner auxiliary nodes here */
     813             :     Assert(!IsA(node, SpecialJoinInfo));
     814             :     Assert(!IsA(node, PlaceHolderInfo));
     815             :     Assert(!IsA(node, MinMaxAggInfo));
     816             : 
     817     2006824 :     return expression_tree_mutator(node, flatten_join_alias_vars_mutator,
     818             :                                    (void *) context);
     819             : }
     820             : 
     821             : /*
     822             :  * alias_relid_set: in a set of RT indexes, replace joins by their
     823             :  * underlying base relids
     824             :  */
     825             : static Relids
     826         708 : alias_relid_set(Query *query, Relids relids)
     827             : {
     828         708 :     Relids      result = NULL;
     829             :     int         rtindex;
     830             : 
     831         708 :     rtindex = -1;
     832        2352 :     while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
     833             :     {
     834         936 :         RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
     835             : 
     836         936 :         if (rte->rtekind == RTE_JOIN)
     837           0 :             result = bms_join(result, get_relids_for_join(query, rtindex));
     838             :         else
     839         936 :             result = bms_add_member(result, rtindex);
     840             :     }
     841         708 :     return result;
     842             : }

Generated by: LCOV version 1.13