Handle PlaceHolderVar case in replace_varno_walker
authorAlexander Korotkov <akorotkov@postgresql.org>
Sun, 24 Dec 2023 23:16:09 +0000 (01:16 +0200)
committerAlexander Korotkov <akorotkov@postgresql.org>
Sun, 24 Dec 2023 23:33:08 +0000 (01:33 +0200)
This commit also retires sje_walker.  This increases the generalty of replacing
varno in the parse tree and simplifies the code.

Discussion: https://postgr.es/m/18187-831da249cbd2ff8e%40postgresql.org
Author: Richard Guo
Reviewed-by: Andrei Lepikhov
src/backend/optimizer/plan/analyzejoins.c

index b9be19a687f5013511d9d273be83f151f519b627..00404a8beb0c57a9f5c22e691a4f19bd833b0fcc 100644 (file)
@@ -1456,7 +1456,16 @@ replace_varno_walker(Node *node, ReplaceVarnoContext *ctx)
                }
                return false;
        }
-       if (IsA(node, RestrictInfo))
+       else if (IsA(node, PlaceHolderVar))
+       {
+               PlaceHolderVar *phv = (PlaceHolderVar *) node;
+
+               phv->phrels = replace_relid(phv->phrels, ctx->from, ctx->to);
+               phv->phnullingrels = replace_relid(phv->phnullingrels, ctx->from, ctx->to);
+
+               /* fall through to recurse into the placeholder's expression */
+       }
+       else if (IsA(node, RestrictInfo))
        {
                RestrictInfo *rinfo = (RestrictInfo *) node;
                int                     relid = -1;
@@ -1641,26 +1650,6 @@ update_eclasses(EquivalenceClass *ec, int from, int to)
        ec->ec_relids = replace_relid(ec->ec_relids, from, to);
 }
 
-static bool
-sje_walker(Node *node, ReplaceVarnoContext *ctx)
-{
-       if (node == NULL)
-               return false;
-
-       if (IsA(node, Var))
-       {
-               Var                *var = (Var *) node;
-
-               if (var->varno == ctx->from)
-               {
-                       var->varno = ctx->to;
-                       var->varnosyn = ctx->to;
-               }
-               return false;
-       }
-       return expression_tree_walker(node, sje_walker, (void *) ctx);
-}
-
 /*
  * Remove a relation after we have proven that it participates only in an
  * unneeded unique self join.
@@ -1868,7 +1857,8 @@ remove_self_join_rel(PlannerInfo *root, PlanRowMark *kmark, PlanRowMark *rmark,
        }
 
        /* Replace varno in all the query structures */
-       query_tree_walker(root->parse, sje_walker, &ctx, QTW_EXAMINE_SORTGROUP);
+       query_tree_walker(root->parse, replace_varno_walker, &ctx,
+                                         QTW_EXAMINE_SORTGROUP);
 
        /* Replace links in the planner info */
        remove_rel_from_query(root, toRemove, toKeep->relid, NULL, NULL);