setrefs.c 13.6 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * setrefs.c
4 5
 *	  Post-processing of a completed plan tree: fix references to subplan
 *	  vars, and compute regproc values for operators
6
 *
B
Add:  
Bruce Momjian 已提交
7 8
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
9 10 11
 *
 *
 * IDENTIFICATION
B
Add:  
Bruce Momjian 已提交
12
 *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.60 2000/01/26 05:56:38 momjian Exp $
13 14 15
 *
 *-------------------------------------------------------------------------
 */
M
Marc G. Fournier 已提交
16 17
#include <sys/types.h>

18 19
#include "postgres.h"

B
Bruce Momjian 已提交
20 21
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
22 23 24 25 26
#include "optimizer/clauses.h"
#include "optimizer/planmain.h"
#include "optimizer/tlist.h"
#include "optimizer/var.h"

27 28 29
typedef struct {
	List	   *outer_tlist;
	List	   *inner_tlist;
30 31
	Index		acceptable_rel;
} join_references_context;
32 33 34 35 36 37

typedef struct {
	Index		subvarno;
	List	   *subplanTargetList;
} replace_vars_with_subplan_refs_context;

38 39 40 41 42 43 44 45
static void set_join_references(Join *join);
static void set_uppernode_references(Plan *plan, Index subvarno);
static Node *join_references_mutator(Node *node,
									 join_references_context *context);
static Node *replace_vars_with_subplan_refs(Node *node,
											Index subvarno,
											List *subplanTargetList);
static Node *replace_vars_with_subplan_refs_mutator(Node *node,
46
					replace_vars_with_subplan_refs_context *context);
47
static bool fix_opids_walker(Node *node, void *context);
48 49

/*****************************************************************************
50 51 52
 *
 *		SUBPLAN REFERENCES
 *
53 54
 *****************************************************************************/

55
/*
56 57 58 59 60 61
 * set_plan_references
 *	  This is the final processing pass of the planner/optimizer.  The plan
 *	  tree is complete; we just have to adjust some representational details
 *	  for the convenience of the executor.  We update Vars in upper plan nodes
 *	  to refer to the outputs of their subplans, and we compute regproc OIDs
 *	  for operators (ie, we look up the function that implements each op).
62
 *
63
 *	  set_plan_references recursively traverses the whole plan tree.
64
 *
65 66 67
 * Returns nothing of interest, but modifies internal fields of nodes.
 */
void
68
set_plan_references(Plan *plan)
69
{
70 71
	List	   *pl;

72 73 74
	if (plan == NULL)
		return;

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	/*
	 * Plan-type-specific fixes
	 */
	switch (nodeTag(plan))
	{
		case T_SeqScan:
			/* nothing special */
			break;
		case T_IndexScan:
			fix_opids((Node *) ((IndexScan *) plan)->indxqual);
			fix_opids((Node *) ((IndexScan *) plan)->indxqualorig);
			break;
		case T_NestLoop:
			set_join_references((Join *) plan);
			break;
		case T_MergeJoin:
			set_join_references((Join *) plan);
			fix_opids((Node *) ((MergeJoin *) plan)->mergeclauses);
			break;
		case T_HashJoin:
			set_join_references((Join *) plan);
			fix_opids((Node *) ((HashJoin *) plan)->hashclauses);
			break;
		case T_Material:
		case T_Sort:
		case T_Unique:
		case T_Hash:
			/* These plan types don't actually bother to evaluate their
			 * targetlists or quals (because they just return their
			 * unmodified input tuples).  The optimizer is lazy about
			 * creating really valid targetlists for them.  Best to
			 * just leave the targetlist alone.
			 */
			break;
		case T_Agg:
		case T_Group:
			set_uppernode_references(plan, (Index) 0);
			break;
		case T_Result:
114 115 116 117 118 119 120
			/* Result may or may not have a subplan; no need to fix up
			 * subplan references if it hasn't got one...
			 *
			 * XXX why does Result use a different subvarno from Agg/Group?
			 */
			if (plan->lefttree != NULL)
				set_uppernode_references(plan, (Index) OUTER);
121 122 123 124 125 126 127 128
			fix_opids(((Result *) plan)->resconstantqual);
			break;
		case T_Append:
			foreach(pl, ((Append *) plan)->appendplans)
			{
				set_plan_references((Plan *) lfirst(pl));
			}
			break;
129 130 131
		case T_TidScan:
			/* nothing special */
			break;
132 133 134 135 136
		default:
			elog(ERROR, "set_plan_references: unknown plan type %d",
				 nodeTag(plan));
			break;
	}
137

138 139 140 141 142
	/*
	 * For all plan types, fix operators in targetlist and qual expressions
	 */
	fix_opids((Node *) plan->targetlist);
	fix_opids((Node *) plan->qual);
143

144 145 146 147 148 149 150 151 152 153 154
	/*
	 * Now recurse into subplans, if any
	 *
	 * NOTE: it is essential that we recurse into subplans AFTER we set
	 * subplan references in this plan's tlist and quals.  If we did the
	 * reference-adjustments bottom-up, then we would fail to match this
	 * plan's var nodes against the already-modified nodes of the subplans.
	 */
	set_plan_references(plan->lefttree);
	set_plan_references(plan->righttree);
	foreach(pl, plan->initPlan)
155
	{
156
		SubPlan	   *sp = (SubPlan *) lfirst(pl);
B
Bruce Momjian 已提交
157

158 159
		Assert(IsA(sp, SubPlan));
		set_plan_references(sp->plan);
160
	}
161 162 163
	foreach(pl, plan->subPlan)
	{
		SubPlan	   *sp = (SubPlan *) lfirst(pl);
164

165 166 167
		Assert(IsA(sp, SubPlan));
		set_plan_references(sp->plan);
	}
168 169
}

170
/*
171 172 173 174 175
 * set_join_references
 *	  Modifies the target list of a join node to reference its subplans,
 *	  by setting the varnos to OUTER or INNER and setting attno values to the
 *	  result domain number of either the corresponding outer or inner join
 *	  tuple item.
176
 *
177 178 179 180 181
 * Note: this same transformation has already been applied to the quals
 * of the join by createplan.c.  It's a little odd to do it here for the
 * targetlist and there for the quals, but it's easier that way.  (Look
 * at switch_outer() and the handling of nestloop inner indexscans to
 * see why.)
182
 *
183 184 185 186
 * Because the quals are reference-adjusted sooner, we cannot do equal()
 * comparisons between qual and tlist var nodes during the time between
 * creation of a plan node by createplan.c and its fixing by this module.
 * Fortunately, there doesn't seem to be any need to do that.
187
 *
188
 * 'join' is a join plan node
189 190
 */
static void
191
set_join_references(Join *join)
192
{
193 194 195 196
	Plan	   *outer = join->lefttree;
	Plan	   *inner = join->righttree;
	List	   *outer_tlist = ((outer == NULL) ? NIL : outer->targetlist);
	List	   *inner_tlist = ((inner == NULL) ? NIL : inner->targetlist);
197

198 199 200 201
	join->targetlist = join_references(join->targetlist,
									   outer_tlist,
									   inner_tlist,
									   (Index) 0);
202 203
}

204
/*
205 206 207
 * set_uppernode_references
 *	  Update the targetlist and quals of an upper-level plan node
 *	  to refer to the tuples returned by its lefttree subplan.
208
 *
209
 * This is used for single-input plan types like Agg, Group, Result.
210 211
 */
static void
212
set_uppernode_references(Plan *plan, Index subvarno)
213
{
214 215
	Plan	   *subplan = plan->lefttree;
	List	   *subplanTargetList;
216

217 218
	if (subplan != NULL)
		subplanTargetList = subplan->targetlist;
219
	else
220 221 222 223 224 225 226 227 228 229 230
		subplanTargetList = NIL;

	plan->targetlist = (List *)
		replace_vars_with_subplan_refs((Node *) plan->targetlist,
									   subvarno,
									   subplanTargetList);

	plan->qual = (List *)
		replace_vars_with_subplan_refs((Node *) plan->qual,
									   subvarno,
									   subplanTargetList);
231 232
}

233
/*
234
 * join_references
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
 *	   Creates a new set of targetlist entries or join qual clauses by
 *	   changing the varno/varattno values of variables in the clauses
 *	   to reference target list values from the outer and inner join
 *	   relation target lists.
 *
 * This is used in two different scenarios: a normal join clause, where
 * all the Vars in the clause *must* be replaced by OUTER or INNER references;
 * and an indexscan being used on the inner side of a nestloop join.
 * In the latter case we want to replace the outer-relation Vars by OUTER
 * references, but not touch the Vars of the inner relation.
 *
 * For a normal join, acceptable_rel should be zero so that any failure to
 * match a Var will be reported as an error.  For the indexscan case,
 * pass inner_tlist = NIL and acceptable_rel = the ID of the inner relation.
 *
 * 'clauses' is the targetlist or list of join clauses
251
 * 'outer_tlist' is the target list of the outer join relation
252 253 254
 * 'inner_tlist' is the target list of the inner join relation, or NIL
 * 'acceptable_rel' is either zero or the rangetable index of a relation
 *		whose Vars may appear in the clause without provoking an error.
255
 *
256
 * Returns the new expression tree.  The original clause structure is
257
 * not modified.
258
 */
259
List *
260 261
join_references(List *clauses,
				List *outer_tlist,
262 263
				List *inner_tlist,
				Index acceptable_rel)
264
{
265
	join_references_context context;
B
Bruce Momjian 已提交
266

267 268
	context.outer_tlist = outer_tlist;
	context.inner_tlist = inner_tlist;
269 270
	context.acceptable_rel = acceptable_rel;
	return (List *) join_references_mutator((Node *) clauses, &context);
271 272
}

273
static Node *
274 275
join_references_mutator(Node *node,
						join_references_context *context)
276
{
277 278 279 280 281
	if (node == NULL)
		return NULL;
	if (IsA(node, Var))
	{
		Var		   *var = (Var *) node;
282 283
		Var		   *newvar = (Var *) copyObject(var);
		Resdom	   *resdom;
284

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
		resdom = tlist_member((Node *) var, context->outer_tlist);
		if (resdom)
		{
			newvar->varno = OUTER;
			newvar->varattno = resdom->resno;
			return (Node *) newvar;
		}
		resdom = tlist_member((Node *) var, context->inner_tlist);
		if (resdom)
		{
			newvar->varno = INNER;
			newvar->varattno = resdom->resno;
			return (Node *) newvar;
		}
		/*
		 * Var not in either tlist --- either raise an error,
		 * or return the Var unmodified.
		 */
		if (var->varno != context->acceptable_rel)
			elog(ERROR, "join_references: variable not in subplan target lists");
		return (Node *) newvar;	/* copy is probably not necessary here... */
306 307
	}
	/*
308 309 310 311 312 313
	 * expression_tree_mutator will copy SubPlan nodes if given a chance.
	 * We do not want to do that here, because subselect.c has already
	 * constructed the initPlan and subPlan lists of the current plan node
	 * and we mustn't leave those dangling (ie, pointing to different
	 * copies of the nodes than what's in the targetlist & quals...)
	 * Instead, alter the SubPlan in-place.  Grotty --- is there a better way?
314
	 */
315
	if (is_subplan(node))
316
	{
317 318 319 320 321 322 323 324 325 326 327 328 329
		Expr	   *expr = (Expr *) node;
		SubLink	   *sublink = ((SubPlan *) expr->oper)->sublink;

		/* transform args list (params to be passed to subplan) */
		expr->args = (List *)
			join_references_mutator((Node *) expr->args,
									context);
		/* transform sublink's oper list as well */
		sublink->oper = (List *)
			join_references_mutator((Node *) sublink->oper,
									context);

		return (Node *) expr;
330
	}
331 332 333
	return expression_tree_mutator(node,
								   join_references_mutator,
								   (void *) context);
334 335
}

336
/*
337
 * replace_vars_with_subplan_refs
338 339 340
 *		This routine modifies an expression tree so that all Var nodes
 *		reference target nodes of a subplan.  It is used to fix up
 *		target and qual expressions of non-join upper-level plan nodes.
341
 *
342 343 344
 * An error is raised if no matching var can be found in the subplan tlist
 * --- so this routine should only be applied to nodes whose subplans'
 * targetlists were generated via flatten_tlist() or some such method.
345
 *
346
 * 'node': the tree to be fixed (a targetlist or qual list)
347 348
 * 'subvarno': varno to be assigned to all Vars
 * 'subplanTargetList': target list for subplan
349
 *
350 351 352
 * The resulting tree is a copy of the original in which all Var nodes have
 * varno = subvarno, varattno = resno of corresponding subplan target.
 * The original tree is not modified.
353
 */
354 355
static Node *
replace_vars_with_subplan_refs(Node *node,
356 357
							   Index subvarno,
							   List *subplanTargetList)
358
{
359
	replace_vars_with_subplan_refs_context context;
360

361 362
	context.subvarno = subvarno;
	context.subplanTargetList = subplanTargetList;
363
	return replace_vars_with_subplan_refs_mutator(node, &context);
364
}
B
Bruce Momjian 已提交
365

366 367
static Node *
replace_vars_with_subplan_refs_mutator(Node *node,
368 369 370
							 replace_vars_with_subplan_refs_context *context)
{
	if (node == NULL)
371
		return NULL;
372 373 374
	if (IsA(node, Var))
	{
		Var		   *var = (Var *) node;
375 376
		Var		   *newvar = (Var *) copyObject(var);
		Resdom	   *resdom;
377

378 379 380
		resdom = tlist_member((Node *) var, context->subplanTargetList);
		if (!resdom)
			elog(ERROR, "replace_vars_with_subplan_refs: variable not in subplan target list");
381

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
		newvar->varno = context->subvarno;
		newvar->varattno = resdom->resno;
		return (Node *) newvar;
	}
	/*
	 * expression_tree_mutator will copy SubPlan nodes if given a chance.
	 * We do not want to do that here, because subselect.c has already
	 * constructed the initPlan and subPlan lists of the current plan node
	 * and we mustn't leave those dangling (ie, pointing to different
	 * copies of the nodes than what's in the targetlist & quals...)
	 * Instead, alter the SubPlan in-place.  Grotty --- is there a better way?
	 */
	if (is_subplan(node))
	{
		Expr	   *expr = (Expr *) node;
		SubLink	   *sublink = ((SubPlan *) expr->oper)->sublink;

		/* transform args list (params to be passed to subplan) */
		expr->args = (List *)
			replace_vars_with_subplan_refs_mutator((Node *) expr->args,
												   context);
		/* transform sublink's oper list as well */
		sublink->oper = (List *)
			replace_vars_with_subplan_refs_mutator((Node *) sublink->oper,
												   context);

		return (Node *) expr;
409
	}
410 411 412
	return expression_tree_mutator(node,
								   replace_vars_with_subplan_refs_mutator,
								   (void *) context);
413 414 415
}

/*****************************************************************************
416
 *					OPERATOR REGPROC LOOKUP
417 418
 *****************************************************************************/

419
/*
420 421 422
 * fix_opids
 *	  Calculate opid field from opno for each Oper node in given tree.
 *	  The given tree can be anything expression_tree_walker handles.
423
 *
424 425 426
 * The argument is modified in-place.  (This is OK since we'd want the
 * same change for any node, even if it gets visited more than once due to
 * shared structure.)
427
 */
428
void
429
fix_opids(Node *node)
430
{
431 432
	/* This tree walk requires no special setup, so away we go... */
	fix_opids_walker(node, NULL);
433
}
B
Bruce Momjian 已提交
434

435
static bool
436
fix_opids_walker (Node *node, void *context)
437 438 439
{
	if (node == NULL)
		return false;
440 441 442
	if (is_opclause(node))
		replace_opid((Oper *) ((Expr *) node)->oper);
	return expression_tree_walker(node, fix_opids_walker, context);
443
}