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-2001, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.71 2001/03/22 03:59:37 momjian Exp $
14 *-------------------------------------------------------------------------
16 #include <sys/types.h>
20 #include "nodes/makefuncs.h"
21 #include "nodes/nodeFuncs.h"
22 #include "optimizer/clauses.h"
23 #include "optimizer/planmain.h"
24 #include "optimizer/tlist.h"
31 } join_references_context;
36 List *subplanTargetList;
37 } replace_vars_with_subplan_refs_context;
39 static void fix_expr_references(Plan *plan, Node *node);
40 static void set_join_references(Join *join);
41 static void set_uppernode_references(Plan *plan, Index subvarno);
42 static Node *join_references_mutator(Node *node,
43 join_references_context *context);
44 static Node *replace_vars_with_subplan_refs(Node *node,
46 List *subplanTargetList);
47 static Node *replace_vars_with_subplan_refs_mutator(Node *node,
48 replace_vars_with_subplan_refs_context *context);
49 static bool fix_opids_walker(Node *node, void *context);
51 /*****************************************************************************
55 *****************************************************************************/
59 * This is the final processing pass of the planner/optimizer. The plan
60 * tree is complete; we just have to adjust some representational details
61 * for the convenience of the executor. We update Vars in upper plan nodes
62 * to refer to the outputs of their subplans, and we compute regproc OIDs
63 * for operators (ie, we look up the function that implements each op).
64 * We must also build lists of all the subplan nodes present in each
65 * plan node's expression trees.
67 * set_plan_references recursively traverses the whole plan tree.
69 * Returns nothing of interest, but modifies internal fields of nodes.
72 set_plan_references(Plan *plan)
80 * We must rebuild the plan's list of subplan nodes, since we are
81 * copying/mutating its expression trees.
86 * Plan-type-specific fixes
88 switch (nodeTag(plan))
91 fix_expr_references(plan, (Node *) plan->targetlist);
92 fix_expr_references(plan, (Node *) plan->qual);
95 fix_expr_references(plan, (Node *) plan->targetlist);
96 fix_expr_references(plan, (Node *) plan->qual);
97 fix_expr_references(plan,
98 (Node *) ((IndexScan *) plan)->indxqual);
99 fix_expr_references(plan,
100 (Node *) ((IndexScan *) plan)->indxqualorig);
103 fix_expr_references(plan, (Node *) plan->targetlist);
104 fix_expr_references(plan, (Node *) plan->qual);
109 * We do not do set_uppernode_references() here, because a
110 * SubqueryScan will always have been created with correct
111 * references to its subplan's outputs to begin with.
113 fix_expr_references(plan, (Node *) plan->targetlist);
114 fix_expr_references(plan, (Node *) plan->qual);
115 /* Recurse into subplan too */
116 set_plan_references(((SubqueryScan *) plan)->subplan);
119 set_join_references((Join *) plan);
120 fix_expr_references(plan, (Node *) plan->targetlist);
121 fix_expr_references(plan, (Node *) plan->qual);
122 fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
125 set_join_references((Join *) plan);
126 fix_expr_references(plan, (Node *) plan->targetlist);
127 fix_expr_references(plan, (Node *) plan->qual);
128 fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
129 fix_expr_references(plan,
130 (Node *) ((MergeJoin *) plan)->mergeclauses);
133 set_join_references((Join *) plan);
134 fix_expr_references(plan, (Node *) plan->targetlist);
135 fix_expr_references(plan, (Node *) plan->qual);
136 fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
137 fix_expr_references(plan,
138 (Node *) ((HashJoin *) plan)->hashclauses);
148 * These plan types don't actually bother to evaluate their
149 * targetlists or quals (because they just return their
150 * unmodified input tuples). The optimizer is lazy about
151 * creating really valid targetlists for them. Best to just
152 * leave the targetlist alone. In particular, we do not want
153 * to pull a subplan list for them, since we will likely end
154 * up with duplicate list entries for subplans that also
155 * appear in lower levels of the plan tree!
160 set_uppernode_references(plan, (Index) 0);
161 fix_expr_references(plan, (Node *) plan->targetlist);
162 fix_expr_references(plan, (Node *) plan->qual);
167 * Result may or may not have a subplan; no need to fix up
168 * subplan references if it hasn't got one...
170 * XXX why does Result use a different subvarno from Agg/Group?
172 if (plan->lefttree != NULL)
173 set_uppernode_references(plan, (Index) OUTER);
174 fix_expr_references(plan, (Node *) plan->targetlist);
175 fix_expr_references(plan, (Node *) plan->qual);
176 fix_expr_references(plan, ((Result *) plan)->resconstantqual);
181 * Append, like Sort et al, doesn't actually evaluate its
182 * targetlist or quals, and we haven't bothered to give it its
183 * own tlist copy. So, don't fix targetlist/qual. But do
184 * recurse into subplans.
186 foreach(pl, ((Append *) plan)->appendplans)
187 set_plan_references((Plan *) lfirst(pl));
190 elog(ERROR, "set_plan_references: unknown plan type %d",
196 * Now recurse into subplans, if any
198 * NOTE: it is essential that we recurse into subplans AFTER we set
199 * subplan references in this plan's tlist and quals. If we did the
200 * reference-adjustments bottom-up, then we would fail to match this
201 * plan's var nodes against the already-modified nodes of the
204 set_plan_references(plan->lefttree);
205 set_plan_references(plan->righttree);
206 foreach(pl, plan->initPlan)
208 SubPlan *sp = (SubPlan *) lfirst(pl);
210 Assert(IsA(sp, SubPlan));
211 set_plan_references(sp->plan);
213 foreach(pl, plan->subPlan)
215 SubPlan *sp = (SubPlan *) lfirst(pl);
217 Assert(IsA(sp, SubPlan));
218 set_plan_references(sp->plan);
223 * fix_expr_references
224 * Do final cleanup on expressions (targetlists or quals).
226 * This consists of looking up operator opcode info for Oper nodes
227 * and adding subplans to the Plan node's list of contained subplans.
230 fix_expr_references(Plan *plan, Node *node)
233 plan->subPlan = nconc(plan->subPlan, pull_subplans(node));
237 * set_join_references
238 * Modifies the target list of a join node to reference its subplans,
239 * by setting the varnos to OUTER or INNER and setting attno values to the
240 * result domain number of either the corresponding outer or inner join
243 * Note: this same transformation has already been applied to the quals
244 * of the join by createplan.c. It's a little odd to do it here for the
245 * targetlist and there for the quals, but it's easier that way. (Look
246 * at switch_outer() and the handling of nestloop inner indexscans to
249 * Because the quals are reference-adjusted sooner, we cannot do equal()
250 * comparisons between qual and tlist var nodes during the time between
251 * creation of a plan node by createplan.c and its fixing by this module.
252 * Fortunately, there doesn't seem to be any need to do that.
254 * 'join' is a join plan node
257 set_join_references(Join *join)
259 Plan *outer = join->plan.lefttree;
260 Plan *inner = join->plan.righttree;
261 List *outer_tlist = ((outer == NULL) ? NIL : outer->targetlist);
262 List *inner_tlist = ((inner == NULL) ? NIL : inner->targetlist);
264 join->plan.targetlist = join_references(join->plan.targetlist,
271 * set_uppernode_references
272 * Update the targetlist and quals of an upper-level plan node
273 * to refer to the tuples returned by its lefttree subplan.
275 * This is used for single-input plan types like Agg, Group, Result.
277 * In most cases, we have to match up individual Vars in the tlist and
278 * qual expressions with elements of the subplan's tlist (which was
279 * generated by flatten_tlist() from these selfsame expressions, so it
280 * should have all the required variables). There is an important exception,
281 * however: a GROUP BY expression that is also an output expression will
282 * have been pushed into the subplan tlist unflattened. We want to detect
283 * this case and reference the subplan output directly. Therefore, check
284 * for equality of the whole tlist expression to any subplan element before
285 * we resort to picking the expression apart for individual Vars.
288 set_uppernode_references(Plan *plan, Index subvarno)
290 Plan *subplan = plan->lefttree;
291 List *subplanTargetList,
296 subplanTargetList = subplan->targetlist;
298 subplanTargetList = NIL;
300 outputTargetList = NIL;
301 foreach(l, plan->targetlist)
303 TargetEntry *tle = (TargetEntry *) lfirst(l);
304 TargetEntry *subplantle;
307 subplantle = tlistentry_member(tle->expr, subplanTargetList);
310 /* Found a matching subplan output expression */
311 Resdom *resdom = subplantle->resdom;
314 newvar = makeVar(subvarno,
319 /* If we're just copying a simple Var, copy up original info */
320 if (subplantle->expr && IsA(subplantle->expr, Var))
322 Var *subvar = (Var *) subplantle->expr;
324 newvar->varnoold = subvar->varnoold;
325 newvar->varoattno = subvar->varoattno;
329 newvar->varnoold = 0;
330 newvar->varoattno = 0;
332 newexpr = (Node *) newvar;
336 /* No matching expression, so replace individual Vars */
337 newexpr = replace_vars_with_subplan_refs(tle->expr,
341 outputTargetList = lappend(outputTargetList,
342 makeTargetEntry(tle->resdom, newexpr));
344 plan->targetlist = outputTargetList;
346 plan->qual = (List *)
347 replace_vars_with_subplan_refs((Node *) plan->qual,
354 * Creates a new set of targetlist entries or join qual clauses by
355 * changing the varno/varattno values of variables in the clauses
356 * to reference target list values from the outer and inner join
357 * relation target lists.
359 * This is used in two different scenarios: a normal join clause, where
360 * all the Vars in the clause *must* be replaced by OUTER or INNER references;
361 * and an indexscan being used on the inner side of a nestloop join.
362 * In the latter case we want to replace the outer-relation Vars by OUTER
363 * references, but not touch the Vars of the inner relation.
365 * For a normal join, acceptable_rel should be zero so that any failure to
366 * match a Var will be reported as an error. For the indexscan case,
367 * pass inner_tlist = NIL and acceptable_rel = the ID of the inner relation.
369 * 'clauses' is the targetlist or list of join clauses
370 * 'outer_tlist' is the target list of the outer join relation
371 * 'inner_tlist' is the target list of the inner join relation, or NIL
372 * 'acceptable_rel' is either zero or the rangetable index of a relation
373 * whose Vars may appear in the clause without provoking an error.
375 * Returns the new expression tree. The original clause structure is
379 join_references(List *clauses,
382 Index acceptable_rel)
384 join_references_context context;
386 context.outer_tlist = outer_tlist;
387 context.inner_tlist = inner_tlist;
388 context.acceptable_rel = acceptable_rel;
389 return (List *) join_references_mutator((Node *) clauses, &context);
393 join_references_mutator(Node *node,
394 join_references_context *context)
400 Var *var = (Var *) node;
401 Var *newvar = (Var *) copyObject(var);
404 resdom = tlist_member((Node *) var, context->outer_tlist);
407 newvar->varno = OUTER;
408 newvar->varattno = resdom->resno;
409 return (Node *) newvar;
411 resdom = tlist_member((Node *) var, context->inner_tlist);
414 newvar->varno = INNER;
415 newvar->varattno = resdom->resno;
416 return (Node *) newvar;
420 * Var not in either tlist --- either raise an error, or return
421 * the Var unmodified.
423 if (var->varno != context->acceptable_rel)
424 elog(ERROR, "join_references: variable not in subplan target lists");
425 return (Node *) newvar;
427 return expression_tree_mutator(node,
428 join_references_mutator,
433 * replace_vars_with_subplan_refs
434 * This routine modifies an expression tree so that all Var nodes
435 * reference target nodes of a subplan. It is used to fix up
436 * target and qual expressions of non-join upper-level plan nodes.
438 * An error is raised if no matching var can be found in the subplan tlist
439 * --- so this routine should only be applied to nodes whose subplans'
440 * targetlists were generated via flatten_tlist() or some such method.
442 * 'node': the tree to be fixed (a targetlist or qual list)
443 * 'subvarno': varno to be assigned to all Vars
444 * 'subplanTargetList': target list for subplan
446 * The resulting tree is a copy of the original in which all Var nodes have
447 * varno = subvarno, varattno = resno of corresponding subplan target.
448 * The original tree is not modified.
451 replace_vars_with_subplan_refs(Node *node,
453 List *subplanTargetList)
455 replace_vars_with_subplan_refs_context context;
457 context.subvarno = subvarno;
458 context.subplanTargetList = subplanTargetList;
459 return replace_vars_with_subplan_refs_mutator(node, &context);
463 replace_vars_with_subplan_refs_mutator(Node *node,
464 replace_vars_with_subplan_refs_context *context)
470 Var *var = (Var *) node;
471 Var *newvar = (Var *) copyObject(var);
474 resdom = tlist_member((Node *) var, context->subplanTargetList);
476 elog(ERROR, "replace_vars_with_subplan_refs: variable not in subplan target list");
478 newvar->varno = context->subvarno;
479 newvar->varattno = resdom->resno;
480 return (Node *) newvar;
482 return expression_tree_mutator(node,
483 replace_vars_with_subplan_refs_mutator,
487 /*****************************************************************************
488 * OPERATOR REGPROC LOOKUP
489 *****************************************************************************/
493 * Calculate opid field from opno for each Oper node in given tree.
494 * The given tree can be anything expression_tree_walker handles.
496 * The argument is modified in-place. (This is OK since we'd want the
497 * same change for any node, even if it gets visited more than once due to
501 fix_opids(Node *node)
503 /* This tree walk requires no special setup, so away we go... */
504 fix_opids_walker(node, NULL);
508 fix_opids_walker(Node *node, void *context)
512 if (is_opclause(node))
513 replace_opid((Oper *) ((Expr *) node)->oper);
514 return expression_tree_walker(node, fix_opids_walker, context);