1 /*-------------------------------------------------------------------------
4 * Post-processing of a completed plan tree: fix references to subplan
5 * vars, and compute regproc values for operators
7 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.68 2000/10/26 21:36:09 tgl Exp $
14 *-------------------------------------------------------------------------
16 #include <sys/types.h>
20 #include "nodes/nodeFuncs.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/planmain.h"
23 #include "optimizer/tlist.h"
30 } join_references_context;
35 List *subplanTargetList;
36 } replace_vars_with_subplan_refs_context;
38 static void fix_expr_references(Plan *plan, Node *node);
39 static void set_join_references(Join *join);
40 static void set_uppernode_references(Plan *plan, Index subvarno);
41 static Node *join_references_mutator(Node *node,
42 join_references_context *context);
43 static Node *replace_vars_with_subplan_refs(Node *node,
45 List *subplanTargetList);
46 static Node *replace_vars_with_subplan_refs_mutator(Node *node,
47 replace_vars_with_subplan_refs_context *context);
48 static bool fix_opids_walker(Node *node, void *context);
50 /*****************************************************************************
54 *****************************************************************************/
58 * This is the final processing pass of the planner/optimizer. The plan
59 * tree is complete; we just have to adjust some representational details
60 * for the convenience of the executor. We update Vars in upper plan nodes
61 * to refer to the outputs of their subplans, and we compute regproc OIDs
62 * for operators (ie, we look up the function that implements each op).
63 * We must also build lists of all the subplan nodes present in each
64 * plan node's expression trees.
66 * set_plan_references recursively traverses the whole plan tree.
68 * Returns nothing of interest, but modifies internal fields of nodes.
71 set_plan_references(Plan *plan)
79 * We must rebuild the plan's list of subplan nodes, since we are
80 * copying/mutating its expression trees.
85 * Plan-type-specific fixes
87 switch (nodeTag(plan))
90 fix_expr_references(plan, (Node *) plan->targetlist);
91 fix_expr_references(plan, (Node *) plan->qual);
94 fix_expr_references(plan, (Node *) plan->targetlist);
95 fix_expr_references(plan, (Node *) plan->qual);
96 fix_expr_references(plan,
97 (Node *) ((IndexScan *) plan)->indxqual);
98 fix_expr_references(plan,
99 (Node *) ((IndexScan *) plan)->indxqualorig);
102 fix_expr_references(plan, (Node *) plan->targetlist);
103 fix_expr_references(plan, (Node *) plan->qual);
107 * We do not do set_uppernode_references() here, because
108 * a SubqueryScan will always have been created with correct
109 * references to its subplan's outputs to begin with.
111 fix_expr_references(plan, (Node *) plan->targetlist);
112 fix_expr_references(plan, (Node *) plan->qual);
113 /* Recurse into subplan too */
114 set_plan_references(((SubqueryScan *) plan)->subplan);
117 set_join_references((Join *) plan);
118 fix_expr_references(plan, (Node *) plan->targetlist);
119 fix_expr_references(plan, (Node *) plan->qual);
120 fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
123 set_join_references((Join *) plan);
124 fix_expr_references(plan, (Node *) plan->targetlist);
125 fix_expr_references(plan, (Node *) plan->qual);
126 fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
127 fix_expr_references(plan,
128 (Node *) ((MergeJoin *) plan)->mergeclauses);
131 set_join_references((Join *) plan);
132 fix_expr_references(plan, (Node *) plan->targetlist);
133 fix_expr_references(plan, (Node *) plan->qual);
134 fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
135 fix_expr_references(plan,
136 (Node *) ((HashJoin *) plan)->hashclauses);
146 * These plan types don't actually bother to evaluate their
147 * targetlists or quals (because they just return their
148 * unmodified input tuples). The optimizer is lazy about
149 * creating really valid targetlists for them. Best to just
150 * leave the targetlist alone. In particular, we do not want
151 * to pull a subplan list for them, since we will likely end
152 * up with duplicate list entries for subplans that also appear
153 * in lower levels of the plan tree!
158 set_uppernode_references(plan, (Index) 0);
159 fix_expr_references(plan, (Node *) plan->targetlist);
160 fix_expr_references(plan, (Node *) plan->qual);
165 * Result may or may not have a subplan; no need to fix up
166 * subplan references if it hasn't got one...
168 * XXX why does Result use a different subvarno from Agg/Group?
170 if (plan->lefttree != NULL)
171 set_uppernode_references(plan, (Index) OUTER);
172 fix_expr_references(plan, (Node *) plan->targetlist);
173 fix_expr_references(plan, (Node *) plan->qual);
174 fix_expr_references(plan, ((Result *) plan)->resconstantqual);
178 * Append, like Sort et al, doesn't actually evaluate its
179 * targetlist or quals, and we haven't bothered to give it
180 * its own tlist copy. So, don't fix targetlist/qual.
181 * But do recurse into subplans.
183 foreach(pl, ((Append *) plan)->appendplans)
184 set_plan_references((Plan *) lfirst(pl));
187 elog(ERROR, "set_plan_references: unknown plan type %d",
193 * Now recurse into subplans, if any
195 * NOTE: it is essential that we recurse into subplans AFTER we set
196 * subplan references in this plan's tlist and quals. If we did the
197 * reference-adjustments bottom-up, then we would fail to match this
198 * plan's var nodes against the already-modified nodes of the
201 set_plan_references(plan->lefttree);
202 set_plan_references(plan->righttree);
203 foreach(pl, plan->initPlan)
205 SubPlan *sp = (SubPlan *) lfirst(pl);
207 Assert(IsA(sp, SubPlan));
208 set_plan_references(sp->plan);
210 foreach(pl, plan->subPlan)
212 SubPlan *sp = (SubPlan *) lfirst(pl);
214 Assert(IsA(sp, SubPlan));
215 set_plan_references(sp->plan);
220 * fix_expr_references
221 * Do final cleanup on expressions (targetlists or quals).
223 * This consists of looking up operator opcode info for Oper nodes
224 * and adding subplans to the Plan node's list of contained subplans.
227 fix_expr_references(Plan *plan, Node *node)
230 plan->subPlan = nconc(plan->subPlan, pull_subplans(node));
234 * set_join_references
235 * Modifies the target list of a join node to reference its subplans,
236 * by setting the varnos to OUTER or INNER and setting attno values to the
237 * result domain number of either the corresponding outer or inner join
240 * Note: this same transformation has already been applied to the quals
241 * of the join by createplan.c. It's a little odd to do it here for the
242 * targetlist and there for the quals, but it's easier that way. (Look
243 * at switch_outer() and the handling of nestloop inner indexscans to
246 * Because the quals are reference-adjusted sooner, we cannot do equal()
247 * comparisons between qual and tlist var nodes during the time between
248 * creation of a plan node by createplan.c and its fixing by this module.
249 * Fortunately, there doesn't seem to be any need to do that.
251 * 'join' is a join plan node
254 set_join_references(Join *join)
256 Plan *outer = join->plan.lefttree;
257 Plan *inner = join->plan.righttree;
258 List *outer_tlist = ((outer == NULL) ? NIL : outer->targetlist);
259 List *inner_tlist = ((inner == NULL) ? NIL : inner->targetlist);
261 join->plan.targetlist = join_references(join->plan.targetlist,
268 * set_uppernode_references
269 * Update the targetlist and quals of an upper-level plan node
270 * to refer to the tuples returned by its lefttree subplan.
272 * This is used for single-input plan types like Agg, Group, Result.
275 set_uppernode_references(Plan *plan, Index subvarno)
277 Plan *subplan = plan->lefttree;
278 List *subplanTargetList;
281 subplanTargetList = subplan->targetlist;
283 subplanTargetList = NIL;
285 plan->targetlist = (List *)
286 replace_vars_with_subplan_refs((Node *) plan->targetlist,
290 plan->qual = (List *)
291 replace_vars_with_subplan_refs((Node *) plan->qual,
298 * Creates a new set of targetlist entries or join qual clauses by
299 * changing the varno/varattno values of variables in the clauses
300 * to reference target list values from the outer and inner join
301 * relation target lists.
303 * This is used in two different scenarios: a normal join clause, where
304 * all the Vars in the clause *must* be replaced by OUTER or INNER references;
305 * and an indexscan being used on the inner side of a nestloop join.
306 * In the latter case we want to replace the outer-relation Vars by OUTER
307 * references, but not touch the Vars of the inner relation.
309 * For a normal join, acceptable_rel should be zero so that any failure to
310 * match a Var will be reported as an error. For the indexscan case,
311 * pass inner_tlist = NIL and acceptable_rel = the ID of the inner relation.
313 * 'clauses' is the targetlist or list of join clauses
314 * 'outer_tlist' is the target list of the outer join relation
315 * 'inner_tlist' is the target list of the inner join relation, or NIL
316 * 'acceptable_rel' is either zero or the rangetable index of a relation
317 * whose Vars may appear in the clause without provoking an error.
319 * Returns the new expression tree. The original clause structure is
323 join_references(List *clauses,
326 Index acceptable_rel)
328 join_references_context context;
330 context.outer_tlist = outer_tlist;
331 context.inner_tlist = inner_tlist;
332 context.acceptable_rel = acceptable_rel;
333 return (List *) join_references_mutator((Node *) clauses, &context);
337 join_references_mutator(Node *node,
338 join_references_context *context)
344 Var *var = (Var *) node;
345 Var *newvar = (Var *) copyObject(var);
348 resdom = tlist_member((Node *) var, context->outer_tlist);
351 newvar->varno = OUTER;
352 newvar->varattno = resdom->resno;
353 return (Node *) newvar;
355 resdom = tlist_member((Node *) var, context->inner_tlist);
358 newvar->varno = INNER;
359 newvar->varattno = resdom->resno;
360 return (Node *) newvar;
364 * Var not in either tlist --- either raise an error, or return
365 * the Var unmodified.
367 if (var->varno != context->acceptable_rel)
368 elog(ERROR, "join_references: variable not in subplan target lists");
369 return (Node *) newvar;
371 return expression_tree_mutator(node,
372 join_references_mutator,
377 * replace_vars_with_subplan_refs
378 * This routine modifies an expression tree so that all Var nodes
379 * reference target nodes of a subplan. It is used to fix up
380 * target and qual expressions of non-join upper-level plan nodes.
382 * An error is raised if no matching var can be found in the subplan tlist
383 * --- so this routine should only be applied to nodes whose subplans'
384 * targetlists were generated via flatten_tlist() or some such method.
386 * 'node': the tree to be fixed (a targetlist or qual list)
387 * 'subvarno': varno to be assigned to all Vars
388 * 'subplanTargetList': target list for subplan
390 * The resulting tree is a copy of the original in which all Var nodes have
391 * varno = subvarno, varattno = resno of corresponding subplan target.
392 * The original tree is not modified.
395 replace_vars_with_subplan_refs(Node *node,
397 List *subplanTargetList)
399 replace_vars_with_subplan_refs_context context;
401 context.subvarno = subvarno;
402 context.subplanTargetList = subplanTargetList;
403 return replace_vars_with_subplan_refs_mutator(node, &context);
407 replace_vars_with_subplan_refs_mutator(Node *node,
408 replace_vars_with_subplan_refs_context *context)
414 Var *var = (Var *) node;
415 Var *newvar = (Var *) copyObject(var);
418 resdom = tlist_member((Node *) var, context->subplanTargetList);
420 elog(ERROR, "replace_vars_with_subplan_refs: variable not in subplan target list");
422 newvar->varno = context->subvarno;
423 newvar->varattno = resdom->resno;
424 return (Node *) newvar;
426 return expression_tree_mutator(node,
427 replace_vars_with_subplan_refs_mutator,
431 /*****************************************************************************
432 * OPERATOR REGPROC LOOKUP
433 *****************************************************************************/
437 * Calculate opid field from opno for each Oper node in given tree.
438 * The given tree can be anything expression_tree_walker handles.
440 * The argument is modified in-place. (This is OK since we'd want the
441 * same change for any node, even if it gets visited more than once due to
445 fix_opids(Node *node)
447 /* This tree walk requires no special setup, so away we go... */
448 fix_opids_walker(node, NULL);
452 fix_opids_walker(Node *node, void *context)
456 if (is_opclause(node))
457 replace_opid((Oper *) ((Expr *) node)->oper);
458 return expression_tree_walker(node, fix_opids_walker, context);