setrefs.c 14.2 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
12
 *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.67 2000/10/05 19:11:29 tgl Exp $
13 14 15
 *
 *-------------------------------------------------------------------------
 */
M
Marc G. Fournier 已提交
16 17
#include <sys/types.h>

18 19
#include "postgres.h"

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

25 26
typedef struct
{
27 28
	List	   *outer_tlist;
	List	   *inner_tlist;
29 30
	Index		acceptable_rel;
} join_references_context;
31

32 33
typedef struct
{
34 35 36 37
	Index		subvarno;
	List	   *subplanTargetList;
} replace_vars_with_subplan_refs_context;

38
static void fix_expr_references(Plan *plan, Node *node);
39 40 41
static void set_join_references(Join *join);
static void set_uppernode_references(Plan *plan, Index subvarno);
static Node *join_references_mutator(Node *node,
42
						join_references_context *context);
43
static Node *replace_vars_with_subplan_refs(Node *node,
44 45
							   Index subvarno,
							   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);
49 50

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

56
/*
57 58 59
 * 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
60
 *	  for the convenience of the executor.	We update Vars in upper plan nodes
61 62
 *	  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).
63 64
 *	  We must also build lists of all the subplan nodes present in each
 *	  plan node's expression trees.
65
 *
66
 *	  set_plan_references recursively traverses the whole plan tree.
67
 *
68 69 70
 * Returns nothing of interest, but modifies internal fields of nodes.
 */
void
71
set_plan_references(Plan *plan)
72
{
73 74
	List	   *pl;

75 76 77
	if (plan == NULL)
		return;

78 79
	/*
	 * We must rebuild the plan's list of subplan nodes, since we are
80 81 82 83
	 * copying/mutating its expression trees.
	 */
	plan->subPlan = NIL;

84 85 86 87 88 89
	/*
	 * Plan-type-specific fixes
	 */
	switch (nodeTag(plan))
	{
		case T_SeqScan:
90 91
			fix_expr_references(plan, (Node *) plan->targetlist);
			fix_expr_references(plan, (Node *) plan->qual);
92 93
			break;
		case T_IndexScan:
94 95 96 97 98 99 100 101 102 103
			fix_expr_references(plan, (Node *) plan->targetlist);
			fix_expr_references(plan, (Node *) plan->qual);
			fix_expr_references(plan,
								(Node *) ((IndexScan *) plan)->indxqual);
			fix_expr_references(plan,
								(Node *) ((IndexScan *) plan)->indxqualorig);
			break;
		case T_TidScan:
			fix_expr_references(plan, (Node *) plan->targetlist);
			fix_expr_references(plan, (Node *) plan->qual);
104
			break;
105
		case T_SubqueryScan:
106 107 108 109 110
			/*
			 * We do not do set_uppernode_references() here, because
			 * a SubqueryScan will always have been created with correct
			 * references to its subplan's outputs to begin with.
			 */
111 112
			fix_expr_references(plan, (Node *) plan->targetlist);
			fix_expr_references(plan, (Node *) plan->qual);
113 114
			/* Recurse into subplan too */
			set_plan_references(((SubqueryScan *) plan)->subplan);
115
			break;
116 117
		case T_NestLoop:
			set_join_references((Join *) plan);
118 119
			fix_expr_references(plan, (Node *) plan->targetlist);
			fix_expr_references(plan, (Node *) plan->qual);
120
			fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
121 122 123
			break;
		case T_MergeJoin:
			set_join_references((Join *) plan);
124 125
			fix_expr_references(plan, (Node *) plan->targetlist);
			fix_expr_references(plan, (Node *) plan->qual);
126
			fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
127 128
			fix_expr_references(plan,
								(Node *) ((MergeJoin *) plan)->mergeclauses);
129 130 131
			break;
		case T_HashJoin:
			set_join_references((Join *) plan);
132 133
			fix_expr_references(plan, (Node *) plan->targetlist);
			fix_expr_references(plan, (Node *) plan->qual);
134
			fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
135 136
			fix_expr_references(plan,
								(Node *) ((HashJoin *) plan)->hashclauses);
137 138 139 140
			break;
		case T_Material:
		case T_Sort:
		case T_Unique:
141
		case T_SetOp:
142
		case T_Hash:
143 144 145

			/*
			 * These plan types don't actually bother to evaluate their
146 147
			 * targetlists or quals (because they just return their
			 * unmodified input tuples).  The optimizer is lazy about
148
			 * creating really valid targetlists for them.	Best to just
149 150 151 152
			 * leave the targetlist alone.  In particular, we do not want
			 * to pull a subplan list for them, since we will likely end
			 * up with duplicate list entries for subplans that also appear
			 * in lower levels of the plan tree!
153 154 155 156 157
			 */
			break;
		case T_Agg:
		case T_Group:
			set_uppernode_references(plan, (Index) 0);
158 159
			fix_expr_references(plan, (Node *) plan->targetlist);
			fix_expr_references(plan, (Node *) plan->qual);
160 161
			break;
		case T_Result:
162 163 164

			/*
			 * Result may or may not have a subplan; no need to fix up
165 166 167 168 169 170
			 * 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);
171 172 173
			fix_expr_references(plan, (Node *) plan->targetlist);
			fix_expr_references(plan, (Node *) plan->qual);
			fix_expr_references(plan, ((Result *) plan)->resconstantqual);
174 175
			break;
		case T_Append:
176 177 178 179
			/*
			 * Append, like Sort et al, doesn't actually evaluate its
			 * targetlist or quals, and we haven't bothered to give it
			 * its own tlist copy.  So, don't fix targetlist/qual.
180
			 * But do recurse into subplans.
181
			 */
182 183 184 185 186 187 188 189
			foreach(pl, ((Append *) plan)->appendplans)
				set_plan_references((Plan *) lfirst(pl));
			break;
		default:
			elog(ERROR, "set_plan_references: unknown plan type %d",
				 nodeTag(plan));
			break;
	}
190

191 192 193 194 195 196
	/*
	 * 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
197 198
	 * plan's var nodes against the already-modified nodes of the
	 * subplans.
199 200 201 202
	 */
	set_plan_references(plan->lefttree);
	set_plan_references(plan->righttree);
	foreach(pl, plan->initPlan)
203
	{
204
		SubPlan    *sp = (SubPlan *) lfirst(pl);
B
Bruce Momjian 已提交
205

206 207
		Assert(IsA(sp, SubPlan));
		set_plan_references(sp->plan);
208
	}
209 210
	foreach(pl, plan->subPlan)
	{
211
		SubPlan    *sp = (SubPlan *) lfirst(pl);
212

213 214 215
		Assert(IsA(sp, SubPlan));
		set_plan_references(sp->plan);
	}
216 217
}

218 219 220 221 222 223 224 225 226 227 228 229 230 231
/*
 * fix_expr_references
 *	  Do final cleanup on expressions (targetlists or quals).
 *
 * This consists of looking up operator opcode info for Oper nodes
 * and adding subplans to the Plan node's list of contained subplans.
 */
static void
fix_expr_references(Plan *plan, Node *node)
{
	fix_opids(node);
	plan->subPlan = nconc(plan->subPlan, pull_subplans(node));
}

232
/*
233 234 235 236 237
 * 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.
238
 *
239 240 241 242 243
 * 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.)
244
 *
245 246 247 248
 * 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.
249
 *
250
 * 'join' is a join plan node
251 252
 */
static void
253
set_join_references(Join *join)
254
{
255 256
	Plan	   *outer = join->plan.lefttree;
	Plan	   *inner = join->plan.righttree;
257 258
	List	   *outer_tlist = ((outer == NULL) ? NIL : outer->targetlist);
	List	   *inner_tlist = ((inner == NULL) ? NIL : inner->targetlist);
259

260 261 262 263
	join->plan.targetlist = join_references(join->plan.targetlist,
											outer_tlist,
											inner_tlist,
											(Index) 0);
264 265
}

266
/*
267 268 269
 * set_uppernode_references
 *	  Update the targetlist and quals of an upper-level plan node
 *	  to refer to the tuples returned by its lefttree subplan.
270
 *
271
 * This is used for single-input plan types like Agg, Group, Result.
272 273
 */
static void
274
set_uppernode_references(Plan *plan, Index subvarno)
275
{
276 277
	Plan	   *subplan = plan->lefttree;
	List	   *subplanTargetList;
278

279 280
	if (subplan != NULL)
		subplanTargetList = subplan->targetlist;
281
	else
282 283 284 285 286 287 288 289 290 291 292
		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);
293 294
}

295
/*
296
 * join_references
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
 *	   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
313
 * 'outer_tlist' is the target list of the outer join relation
314 315 316
 * '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.
317
 *
318
 * Returns the new expression tree.  The original clause structure is
319
 * not modified.
320
 */
321
List *
322 323
join_references(List *clauses,
				List *outer_tlist,
324 325
				List *inner_tlist,
				Index acceptable_rel)
326
{
327
	join_references_context context;
B
Bruce Momjian 已提交
328

329 330
	context.outer_tlist = outer_tlist;
	context.inner_tlist = inner_tlist;
331 332
	context.acceptable_rel = acceptable_rel;
	return (List *) join_references_mutator((Node *) clauses, &context);
333 334
}

335
static Node *
336 337
join_references_mutator(Node *node,
						join_references_context *context)
338
{
339 340 341 342 343
	if (node == NULL)
		return NULL;
	if (IsA(node, Var))
	{
		Var		   *var = (Var *) node;
344 345
		Var		   *newvar = (Var *) copyObject(var);
		Resdom	   *resdom;
346

347 348 349 350 351 352 353 354 355 356 357 358 359 360
		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;
		}
361

362
		/*
363 364
		 * Var not in either tlist --- either raise an error, or return
		 * the Var unmodified.
365 366 367
		 */
		if (var->varno != context->acceptable_rel)
			elog(ERROR, "join_references: variable not in subplan target lists");
368
		return (Node *) newvar;
369
	}
370 371 372
	return expression_tree_mutator(node,
								   join_references_mutator,
								   (void *) context);
373 374
}

375
/*
376
 * replace_vars_with_subplan_refs
377 378 379
 *		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.
380
 *
381 382 383
 * 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.
384
 *
385
 * 'node': the tree to be fixed (a targetlist or qual list)
386 387
 * 'subvarno': varno to be assigned to all Vars
 * 'subplanTargetList': target list for subplan
388
 *
389 390 391
 * 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.
392
 */
393 394
static Node *
replace_vars_with_subplan_refs(Node *node,
395 396
							   Index subvarno,
							   List *subplanTargetList)
397
{
398
	replace_vars_with_subplan_refs_context context;
399

400 401
	context.subvarno = subvarno;
	context.subplanTargetList = subplanTargetList;
402
	return replace_vars_with_subplan_refs_mutator(node, &context);
403
}
B
Bruce Momjian 已提交
404

405 406
static Node *
replace_vars_with_subplan_refs_mutator(Node *node,
407
						 replace_vars_with_subplan_refs_context *context)
408 409
{
	if (node == NULL)
410
		return NULL;
411 412 413
	if (IsA(node, Var))
	{
		Var		   *var = (Var *) node;
414 415
		Var		   *newvar = (Var *) copyObject(var);
		Resdom	   *resdom;
416

417 418 419
		resdom = tlist_member((Node *) var, context->subplanTargetList);
		if (!resdom)
			elog(ERROR, "replace_vars_with_subplan_refs: variable not in subplan target list");
420

421 422 423 424 425 426 427
		newvar->varno = context->subvarno;
		newvar->varattno = resdom->resno;
		return (Node *) newvar;
	}
	return expression_tree_mutator(node,
								   replace_vars_with_subplan_refs_mutator,
								   (void *) context);
428 429 430
}

/*****************************************************************************
431
 *					OPERATOR REGPROC LOOKUP
432 433
 *****************************************************************************/

434
/*
435 436 437
 * fix_opids
 *	  Calculate opid field from opno for each Oper node in given tree.
 *	  The given tree can be anything expression_tree_walker handles.
438
 *
439 440 441
 * 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.)
442
 */
443
void
444
fix_opids(Node *node)
445
{
446 447
	/* This tree walk requires no special setup, so away we go... */
	fix_opids_walker(node, NULL);
448
}
B
Bruce Momjian 已提交
449

450
static bool
451
fix_opids_walker(Node *node, void *context)
452 453 454
{
	if (node == NULL)
		return false;
455 456 457
	if (is_opclause(node))
		replace_opid((Oper *) ((Expr *) node)->oper);
	return expression_tree_walker(node, fix_opids_walker, context);
458
}