Handle PlaceHolderVar case in replace_varno_walker

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
This commit is contained in:
Alexander Korotkov 2023-12-25 01:16:09 +02:00
parent 12915a58ee
commit 8a8ed916f7

View file

@ -1456,7 +1456,16 @@ replace_varno_walker(Node *node, ReplaceVarnoContext *ctx)
} }
return false; 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; RestrictInfo *rinfo = (RestrictInfo *) node;
int relid = -1; 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); 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 * Remove a relation after we have proven that it participates only in an
* unneeded unique self join. * 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 */ /* 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 */ /* Replace links in the planner info */
remove_rel_from_query(root, toRemove, toKeep->relid, NULL, NULL); remove_rel_from_query(root, toRemove, toKeep->relid, NULL, NULL);