initsplan.c 23.9 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * initsplan.c
4
 *	  Target list, qualification, joininfo initialization routines
5
 *
6
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/initsplan.c,v 1.62 2001/05/20 20:28:19 tgl Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
15 16
#include "postgres.h"

Marc G. Fournier's avatar
Marc G. Fournier committed
17 18
#include <sys/types.h>

19
#include "catalog/pg_operator.h"
20
#include "catalog/pg_type.h"
Bruce Momjian's avatar
Bruce Momjian committed
21 22 23
#include "nodes/makefuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
24 25
#include "optimizer/joininfo.h"
#include "optimizer/pathnode.h"
26
#include "optimizer/paths.h"
Bruce Momjian's avatar
Bruce Momjian committed
27
#include "optimizer/planmain.h"
28 29
#include "optimizer/tlist.h"
#include "optimizer/var.h"
30
#include "parser/parsetree.h"
31 32 33
#include "parser/parse_expr.h"
#include "parser/parse_oper.h"
#include "parser/parse_type.h"
Bruce Momjian's avatar
Bruce Momjian committed
34
#include "utils/lsyscache.h"
35
#include "utils/syscache.h"
36 37


38
static void mark_baserels_for_outer_join(Query *root, Relids rels,
39
							 Relids outerrels);
40
static void distribute_qual_to_rels(Query *root, Node *clause,
41 42 43
						bool ispusheddown,
						bool isouterjoin,
						Relids qualscope);
44
static void add_join_info_to_rels(Query *root, RestrictInfo *restrictinfo,
45
					  Relids join_relids);
46
static void add_vars_to_targetlist(Query *root, List *vars);
47 48
static void check_mergejoinable(RestrictInfo *restrictinfo);
static void check_hashjoinable(RestrictInfo *restrictinfo);
49 50 51 52


/*****************************************************************************
 *
53
 *	 TARGET LISTS
54 55 56
 *
 *****************************************************************************/

57
/*
58
 * build_base_rel_tlists
59 60
 *	  Creates rel nodes for every relation mentioned in the target list
 *	  'tlist' (if a node hasn't already been created) and adds them to
61
 *	  root->base_rel_list.	Creates targetlist entries for each var seen
62
 *	  in 'tlist' and adds them to the tlist of the appropriate rel node.
63 64
 */
void
65
build_base_rel_tlists(Query *root, List *tlist)
66
{
67
	List	   *tlist_vars = pull_var_clause((Node *) tlist, false);
68

69 70 71
	add_vars_to_targetlist(root, tlist_vars);
	freeList(tlist_vars);
}
72

73 74 75
/*
 * add_vars_to_targetlist
 *	  For each variable appearing in the list, add it to the relation's
76 77
 *	  targetlist if not already present.  Corresponding base rel nodes
 *	  will be created if not already present.
78 79 80 81 82
 */
static void
add_vars_to_targetlist(Query *root, List *vars)
{
	List	   *temp;
83

84
	foreach(temp, vars)
85
	{
86
		Var		   *var = (Var *) lfirst(temp);
87
		RelOptInfo *rel = build_base_rel(root, var->varno);
88

89
		add_var_to_tlist(rel, var);
90
	}
91 92
}

93
/*----------
94 95
 * add_missing_rels_to_query
 *
96
 *	  If we have a relation listed in the join tree that does not appear
97
 *	  in the target list nor qualifications, we must add it to the base
98
 *	  relation list so that it can be processed.  For instance,
99 100
 *			select count(*) from foo;
 *	  would fail to scan foo if this routine were not called.  More subtly,
101 102 103 104
 *			select f.x from foo f, foo f2
 *	  is a join of f and f2.  Note that if we have
 *			select foo.x from foo f
 *	  this also gets turned into a join (between foo as foo and foo as f).
105
 *
106 107 108 109 110
 *	  Returns a list of all the base relations (RelOptInfo nodes) that appear
 *	  in the join tree.  This list can be used for cross-checking in the
 *	  reverse direction, ie, that we have a join tree entry for every
 *	  relation used in the query.
 *----------
111
 */
112 113
List *
add_missing_rels_to_query(Query *root, Node *jtnode)
114
{
115
	List	   *result = NIL;
116

117 118
	if (jtnode == NULL)
		return NIL;
119
	if (IsA(jtnode, RangeTblRef))
120
	{
121
		int			varno = ((RangeTblRef *) jtnode)->rtindex;
122

123 124
		/* This call to build_base_rel does the primary work... */
		RelOptInfo *rel = build_base_rel(root, varno);
125

126
		result = makeList1(rel);
127
	}
128
	else if (IsA(jtnode, FromExpr))
129
	{
130 131
		FromExpr   *f = (FromExpr *) jtnode;
		List	   *l;
132

133
		foreach(l, f->fromlist)
134
		{
135 136
			result = nconc(result,
						   add_missing_rels_to_query(root, lfirst(l)));
137
		}
138
	}
139 140 141 142 143 144 145 146 147 148 149 150
	else if (IsA(jtnode, JoinExpr))
	{
		JoinExpr   *j = (JoinExpr *) jtnode;

		result = add_missing_rels_to_query(root, j->larg);
		result = nconc(result,
					   add_missing_rels_to_query(root, j->rarg));
	}
	else
		elog(ERROR, "add_missing_rels_to_query: unexpected node type %d",
			 nodeTag(jtnode));
	return result;
151 152
}

153

154 155
/*****************************************************************************
 *
156
 *	  QUALIFICATIONS
157 158 159 160
 *
 *****************************************************************************/


161
/*
162 163 164
 * distribute_quals_to_rels
 *	  Recursively scan the query's join tree for WHERE and JOIN/ON qual
 *	  clauses, and add these to the appropriate RestrictInfo and JoinInfo
165
 *	  lists belonging to base RelOptInfos.	New base rel entries are created
166 167 168
 *	  as needed.  Also, base RelOptInfos are marked with outerjoinset
 *	  information, to aid in proper positioning of qual clauses that appear
 *	  above outer joins.
169 170 171
 *
 * NOTE: when dealing with inner joins, it is appropriate to let a qual clause
 * be evaluated at the lowest level where all the variables it mentions are
172 173
 * available.  However, we cannot push a qual down into the nullable side(s)
 * of an outer join since the qual might eliminate matching rows and cause a
174
 * NULL row to be incorrectly emitted by the join.	Therefore, rels appearing
175 176 177 178
 * within the nullable side(s) of an outer join are marked with
 * outerjoinset = list of Relids used at the outer join node.
 * This list will be added to the list of rels referenced by quals using such
 * a rel, thereby forcing them up the join tree to the right level.
179
 *
180
 * To ease the calculation of these values, distribute_quals_to_rels() returns
181 182 183 184
 * the list of Relids involved in its own level of join.  This is just an
 * internal convenience; no outside callers pay attention to the result.
 */
Relids
185
distribute_quals_to_rels(Query *root, Node *jtnode)
186 187 188 189 190
{
	Relids		result = NIL;

	if (jtnode == NULL)
		return result;
191
	if (IsA(jtnode, RangeTblRef))
192
	{
193 194 195 196 197 198 199 200
		int			varno = ((RangeTblRef *) jtnode)->rtindex;

		/* No quals to deal with, just return correct result */
		result = makeListi1(varno);
	}
	else if (IsA(jtnode, FromExpr))
	{
		FromExpr   *f = (FromExpr *) jtnode;
201
		List	   *l;
202
		List	   *qual;
203 204

		/*
205 206
		 * First, recurse to handle child joins.
		 *
207
		 * Note: we assume it's impossible to see same RT index from more
208
		 * than one subtree, so nconc() is OK rather than set_unioni().
209
		 */
210 211
		foreach(l, f->fromlist)
		{
212
			result = nconc(result,
213 214
						   distribute_quals_to_rels(root, lfirst(l)));
		}
215

216 217 218 219 220 221 222
		/*
		 * Now process the top-level quals.  These are always marked as
		 * "pushed down", since they clearly didn't come from a JOIN expr.
		 */
		foreach(qual, (List *) f->quals)
			distribute_qual_to_rels(root, (Node *) lfirst(qual),
									true, false, result);
223 224 225 226 227
	}
	else if (IsA(jtnode, JoinExpr))
	{
		JoinExpr   *j = (JoinExpr *) jtnode;
		Relids		leftids,
228 229
					rightids;
		bool		isouterjoin;
230 231 232
		List	   *qual;

		/*
233 234 235 236 237 238 239 240
		 * Order of operations here is subtle and critical.  First we
		 * recurse to handle sub-JOINs.  Their join quals will be placed
		 * without regard for whether this level is an outer join, which
		 * is correct. Then, if we are an outer join, we mark baserels
		 * contained within the nullable side(s) with our own rel list;
		 * this will restrict placement of subsequent quals using those
		 * rels, including our own quals and quals above us in the join
		 * tree. Finally we place our own join quals.
241
		 */
242 243
		leftids = distribute_quals_to_rels(root, j->larg);
		rightids = distribute_quals_to_rels(root, j->rarg);
244 245 246

		result = nconc(listCopy(leftids), rightids);

247
		isouterjoin = false;
248 249 250 251 252 253 254
		switch (j->jointype)
		{
			case JOIN_INNER:
				/* Inner join adds no restrictions for quals */
				break;
			case JOIN_LEFT:
				mark_baserels_for_outer_join(root, rightids, result);
255
				isouterjoin = true;
256 257 258
				break;
			case JOIN_FULL:
				mark_baserels_for_outer_join(root, result, result);
259
				isouterjoin = true;
260 261 262
				break;
			case JOIN_RIGHT:
				mark_baserels_for_outer_join(root, leftids, result);
263
				isouterjoin = true;
264 265
				break;
			case JOIN_UNION:
266

267
				/*
268 269
				 * This is where we fail if upper levels of planner
				 * haven't rewritten UNION JOIN as an Append ...
270 271 272 273
				 */
				elog(ERROR, "UNION JOIN is not implemented yet");
				break;
			default:
274 275
				elog(ERROR,
					 "distribute_quals_to_rels: unsupported join type %d",
276 277 278 279 280
					 (int) j->jointype);
				break;
		}

		foreach(qual, (List *) j->quals)
281 282
			distribute_qual_to_rels(root, (Node *) lfirst(qual),
									false, isouterjoin, result);
283 284
	}
	else
285
		elog(ERROR, "distribute_quals_to_rels: unexpected node type %d",
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
			 nodeTag(jtnode));
	return result;
}

/*
 * mark_baserels_for_outer_join
 *	  Mark all base rels listed in 'rels' as having the given outerjoinset.
 */
static void
mark_baserels_for_outer_join(Query *root, Relids rels, Relids outerrels)
{
	List	   *relid;

	foreach(relid, rels)
	{
301
		int			relno = lfirsti(relid);
302
		RelOptInfo *rel = build_base_rel(root, relno);
303 304 305 306 307 308 309

		/*
		 * Since we do this bottom-up, any outer-rels previously marked
		 * should be within the new outer join set.
		 */
		Assert(is_subseti(rel->outerjoinset, outerrels));

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
		/*
		 * Presently the executor cannot support FOR UPDATE marking of
		 * rels appearing on the nullable side of an outer join.
		 * (It's somewhat unclear what that would mean, anyway: what should
		 * we mark when a result row is generated from no element of the
		 * nullable relation?)  So, complain if target rel is FOR UPDATE.
		 * It's sufficient to make this check once per rel, so do it only
		 * if rel wasn't already known nullable.
		 */
		if (rel->outerjoinset == NIL)
		{
			if (intMember(relno, root->rowMarks))
				elog(ERROR, "SELECT FOR UPDATE cannot be applied to the nullable side of an OUTER JOIN");
		}

325 326 327 328
		rel->outerjoinset = outerrels;
	}
}

329
/*
330
 * distribute_qual_to_rels
331
 *	  Add clause information to either the 'RestrictInfo' or 'JoinInfo' field
332
 *	  (depending on whether the clause is a join) of each base relation
333
 *	  mentioned in the clause.	A RestrictInfo node is created and added to
334
 *	  the appropriate list for each rel.  Also, if the clause uses a
335 336 337
 *	  mergejoinable operator and is not an outer-join qual, enter the left-
 *	  and right-side expressions into the query's lists of equijoined vars.
 *
338 339 340 341 342 343 344 345 346
 * 'clause': the qual clause to be distributed
 * 'ispusheddown': if TRUE, force the clause to be marked 'ispusheddown'
 *		(this indicates the clause came from a FromExpr, not a JoinExpr)
 * 'isouterjoin': TRUE if the qual came from an OUTER JOIN's ON-clause
 * 'qualscope': list of baserels the qual's syntactic scope covers
 *
 * 'qualscope' identifies what level of JOIN the qual came from.  For a top
 * level qual (WHERE qual), qualscope lists all baserel ids and in addition
 * 'ispusheddown' will be TRUE.
347 348
 */
static void
349 350 351 352
distribute_qual_to_rels(Query *root, Node *clause,
						bool ispusheddown,
						bool isouterjoin,
						Relids qualscope)
353
{
354
	RestrictInfo *restrictinfo = makeNode(RestrictInfo);
Bruce Momjian's avatar
Bruce Momjian committed
355
	Relids		relids;
356
	List	   *vars;
357
	bool		can_be_equijoin;
358

359
	restrictinfo->clause = (Expr *) clause;
360
	restrictinfo->eval_cost = -1;		/* not computed until needed */
361 362 363 364
	restrictinfo->subclauseindices = NIL;
	restrictinfo->mergejoinoperator = InvalidOid;
	restrictinfo->left_sortop = InvalidOid;
	restrictinfo->right_sortop = InvalidOid;
365
	restrictinfo->left_pathkey = NIL;	/* not computable yet */
366
	restrictinfo->right_pathkey = NIL;
367
	restrictinfo->hashjoinoperator = InvalidOid;
368 369
	restrictinfo->left_bucketsize = -1; /* not computed until needed */
	restrictinfo->right_bucketsize = -1;
370

371 372 373 374 375
	/*
	 * Retrieve all relids and vars contained within the clause.
	 */
	clause_get_relids_vars(clause, &relids, &vars);

376
	/*
377 378
	 * Cross-check: clause should contain no relids not within its scope.
	 * Otherwise the parser messed up.
379
	 */
380
	if (!is_subseti(relids, qualscope))
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
		elog(ERROR, "JOIN qualification may not refer to other relations");

	/*
	 * If the clause is variable-free, we force it to be evaluated at its
	 * original syntactic level.  Note that this should not happen for
	 * top-level clauses, because query_planner() special-cases them.  But
	 * it will happen for variable-free JOIN/ON clauses.  We don't have to
	 * be real smart about such a case, we just have to be correct.
	 */
	if (relids == NIL)
		relids = qualscope;

	/*
	 * For an outer-join qual, pretend that the clause references all rels
	 * appearing within its syntactic scope, even if it really doesn't.
	 * This ensures that the clause will be evaluated exactly at the level
	 * of joining corresponding to the outer join.
	 *
399 400 401 402 403 404 405 406
	 * For a non-outer-join qual, we can evaluate the qual as soon as (1) we
	 * have all the rels it mentions, and (2) we are at or above any outer
	 * joins that can null any of these rels and are below the syntactic
	 * location of the given qual.	To enforce the latter, scan the base
	 * rels listed in relids, and merge their outer-join lists into the
	 * clause's own reference list.  At the time we are called, the
	 * outerjoinset list of each baserel will show exactly those outer
	 * joins that are below the qual in the join tree.
407 408
	 */
	if (isouterjoin)
409
	{
410
		relids = qualscope;
411 412 413 414 415 416 417
		can_be_equijoin = false;
	}
	else
	{
		Relids		newrelids = relids;
		List	   *relid;

418 419 420 421
		/*
		 * We rely on set_unioni to be nondestructive of its input
		 * lists...
		 */
422 423 424
		can_be_equijoin = true;
		foreach(relid, relids)
		{
425
			RelOptInfo *rel = build_base_rel(root, lfirsti(relid));
426

427
			if (rel->outerjoinset &&
428
				!is_subseti(rel->outerjoinset, relids))
429
			{
430
				newrelids = set_unioni(newrelids, rel->outerjoinset);
431

432
				/*
433 434 435
				 * Because application of the qual will be delayed by
				 * outer join, we mustn't assume its vars are equal
				 * everywhere.
436 437 438 439 440
				 */
				can_be_equijoin = false;
			}
		}
		relids = newrelids;
441 442
		/* Should still be a subset of current scope ... */
		Assert(is_subseti(relids, qualscope));
443 444
	}

445
	/*
446 447 448 449 450
	 * Mark the qual as "pushed down" if it can be applied at a level
	 * below its original syntactic level.	This allows us to distinguish
	 * original JOIN/ON quals from higher-level quals pushed down to the
	 * same joinrel. A qual originating from WHERE is always considered
	 * "pushed down".
451 452 453 454
	 */
	restrictinfo->ispusheddown = ispusheddown || !sameseti(relids,
														   qualscope);

455 456
	if (length(relids) == 1)
	{
457
		/*
458
		 * There is only one relation participating in 'clause', so
459
		 * 'clause' is a restriction clause for that relation.
460
		 */
461
		RelOptInfo *rel = build_base_rel(root, lfirsti(relids));
462

463 464
		rel->baserestrictinfo = lappend(rel->baserestrictinfo,
										restrictinfo);
465

466 467
		/*
		 * Check for a "mergejoinable" clause even though it's not a join
468 469 470 471 472
		 * clause.	This is so that we can recognize that "a.x = a.y"
		 * makes x and y eligible to be considered equal, even when they
		 * belong to the same rel.	Without this, we would not recognize
		 * that "a.x = a.y AND a.x = b.z AND a.y = c.q" allows us to
		 * consider z and q equal after their rels are joined.
473
		 */
474 475
		if (can_be_equijoin)
			check_mergejoinable(restrictinfo);
476
	}
477
	else if (relids != NIL)
478
	{
479

480
		/*
481
		 * 'clause' is a join clause, since there is more than one rel in
482 483
		 * the relid list.	Set additional RestrictInfo fields for
		 * joining.
484
		 *
485 486 487 488
		 * We don't bother setting the merge/hashjoin info if we're not going
		 * to need it.	We do want to know about mergejoinable ops in any
		 * potential equijoin clause (see later in this routine), and we
		 * ignore enable_mergejoin if isouterjoin is true, because
489 490
		 * mergejoin is the only implementation we have for full and right
		 * outer joins.
491
		 */
492
		if (enable_mergejoin || isouterjoin || can_be_equijoin)
493
			check_mergejoinable(restrictinfo);
494 495
		if (enable_hashjoin)
			check_hashjoinable(restrictinfo);
496

497
		/*
498
		 * Add clause to the join lists of all the relevant relations.
499
		 */
500
		add_join_info_to_rels(root, restrictinfo, relids);
501

502
		/*
503
		 * Add vars used in the join clause to targetlists of their
504 505 506
		 * relations, so that they will be emitted by the plan nodes that
		 * scan those relations (else they won't be available at the join
		 * node!).
507
		 */
508
		add_vars_to_targetlist(root, vars);
509
	}
510 511
	else
	{
512

513 514
		/*
		 * 'clause' references no rels, and therefore we have no place to
515
		 * attach it.  Shouldn't get here if callers are working properly.
516
		 */
517
		elog(ERROR, "distribute_qual_to_rels: can't cope with variable-free clause");
518
	}
519 520

	/*
521 522 523 524 525 526
	 * If the clause has a mergejoinable operator, and is not an
	 * outer-join qualification nor bubbled up due to an outer join, then
	 * the two sides represent equivalent PathKeyItems for path keys: any
	 * path that is sorted by one side will also be sorted by the other
	 * (as soon as the two rels are joined, that is).  Record the key
	 * equivalence for future use.
527
	 */
528
	if (can_be_equijoin && restrictinfo->mergejoinoperator != InvalidOid)
529
		add_equijoined_keys(root, restrictinfo);
530 531
}

532
/*
533
 * add_join_info_to_rels
534
 *	  For every relation participating in a join clause, add 'restrictinfo' to
535
 *	  the appropriate joininfo list (creating a new list and adding it to the
536 537
 *	  appropriate rel node if necessary).
 *
538
 * 'restrictinfo' describes the join clause
539
 * 'join_relids' is the list of relations participating in the join clause
540 541
 */
static void
542
add_join_info_to_rels(Query *root, RestrictInfo *restrictinfo,
Bruce Momjian's avatar
Bruce Momjian committed
543
					  Relids join_relids)
544
{
545
	List	   *join_relid;
546

547
	/* For every relid, find the joininfo, and add the proper join entries */
548 549
	foreach(join_relid, join_relids)
	{
550
		int			cur_relid = lfirsti(join_relid);
Bruce Momjian's avatar
Bruce Momjian committed
551
		Relids		unjoined_relids = NIL;
552
		JoinInfo   *joininfo;
553
		List	   *otherrel;
554

Bruce Momjian's avatar
Bruce Momjian committed
555
		/* Get the relids not equal to the current relid */
556
		foreach(otherrel, join_relids)
557
		{
558 559
			if (lfirsti(otherrel) != cur_relid)
				unjoined_relids = lappendi(unjoined_relids, lfirsti(otherrel));
560 561
		}

562
		/*
563 564
		 * Find or make the joininfo node for this combination of rels,
		 * and add the restrictinfo node to it.
565
		 */
566
		joininfo = find_joininfo_node(build_base_rel(root, cur_relid),
Bruce Momjian's avatar
Bruce Momjian committed
567
									  unjoined_relids);
568 569
		joininfo->jinfo_restrictinfo = lappend(joininfo->jinfo_restrictinfo,
											   restrictinfo);
570
	}
571 572
}

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
/*
 * process_implied_equality
 *	  Check to see whether we already have a restrictinfo item that says
 *	  item1 = item2, and create one if not.  This is a consequence of
 *	  transitivity of mergejoin equality: if we have mergejoinable
 *	  clauses A = B and B = C, we can deduce A = C (where = is an
 *	  appropriate mergejoinable operator).
 */
void
process_implied_equality(Query *root, Node *item1, Node *item2,
						 Oid sortop1, Oid sortop2)
{
	Index		irel1;
	Index		irel2;
	RelOptInfo *rel1;
	List	   *restrictlist;
	List	   *itm;
	Oid			ltype,
				rtype;
	Operator	eq_operator;
	Form_pg_operator pgopform;
	Expr	   *clause;

	/*
597 598 599 600
	 * Currently, since check_mergejoinable only accepts Var = Var
	 * clauses, we should only see Var nodes here.	Would have to work a
	 * little harder to locate the right rel(s) if more-general mergejoin
	 * clauses were accepted.
601 602 603 604 605
	 */
	Assert(IsA(item1, Var));
	irel1 = ((Var *) item1)->varno;
	Assert(IsA(item2, Var));
	irel2 = ((Var *) item2)->varno;
606

607 608 609 610
	/*
	 * If both vars belong to same rel, we need to look at that rel's
	 * baserestrictinfo list.  If different rels, each will have a
	 * joininfo node for the other, and we can scan either list.
611 612 613
	 *
	 * All baserel entries should already exist at this point, so use
	 * find_base_rel not build_base_rel.
614
	 */
615
	rel1 = find_base_rel(root, irel1);
616 617 618 619 620
	if (irel1 == irel2)
		restrictlist = rel1->baserestrictinfo;
	else
	{
		JoinInfo   *joininfo = find_joininfo_node(rel1,
621
												  makeListi1(irel2));
622 623 624

		restrictlist = joininfo->jinfo_restrictinfo;
	}
625

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
	/*
	 * Scan to see if equality is already known.
	 */
	foreach(itm, restrictlist)
	{
		RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(itm);
		Node	   *left,
				   *right;

		if (restrictinfo->mergejoinoperator == InvalidOid)
			continue;			/* ignore non-mergejoinable clauses */
		/* We now know the restrictinfo clause is a binary opclause */
		left = (Node *) get_leftop(restrictinfo->clause);
		right = (Node *) get_rightop(restrictinfo->clause);
		if ((equal(item1, left) && equal(item2, right)) ||
			(equal(item2, left) && equal(item1, right)))
			return;				/* found a matching clause */
	}
644

645 646 647 648 649 650
	/*
	 * This equality is new information, so construct a clause
	 * representing it to add to the query data structures.
	 */
	ltype = exprType(item1);
	rtype = exprType(item2);
651
	eq_operator = compatible_oper("=", ltype, rtype, true);
652 653
	if (!HeapTupleIsValid(eq_operator))
	{
654

655 656 657 658 659 660 661 662 663
		/*
		 * Would it be safe to just not add the equality to the query if
		 * we have no suitable equality operator for the combination of
		 * datatypes?  NO, because sortkey selection may screw up anyway.
		 */
		elog(ERROR, "Unable to identify an equality operator for types '%s' and '%s'",
			 typeidTypeName(ltype), typeidTypeName(rtype));
	}
	pgopform = (Form_pg_operator) GETSTRUCT(eq_operator);
664

665 666 667 668 669 670 671 672 673 674 675 676
	/*
	 * Let's just make sure this appears to be a compatible operator.
	 */
	if (pgopform->oprlsortop != sortop1 ||
		pgopform->oprrsortop != sortop2 ||
		pgopform->oprresult != BOOLOID)
		elog(ERROR, "Equality operator for types '%s' and '%s' should be mergejoinable, but isn't",
			 typeidTypeName(ltype), typeidTypeName(rtype));

	clause = makeNode(Expr);
	clause->typeOid = BOOLOID;
	clause->opType = OP_EXPR;
677 678 679
	clause->oper = (Node *) makeOper(oprid(eq_operator),		/* opno */
									 InvalidOid,		/* opid */
									 BOOLOID);	/* operator result type */
680
	clause->args = makeList2(item1, item2);
681

682 683
	ReleaseSysCache(eq_operator);

684 685
	/*
	 * Note: we mark the qual "pushed down" to ensure that it can never be
686 687 688 689 690 691
	 * taken for an original JOIN/ON clause.  We also claim it is an
	 * outer- join clause, which it isn't, but that keeps
	 * distribute_qual_to_rels from examining the outerjoinsets of the
	 * relevant rels (which are no longer of interest, but could keep the
	 * qual from being pushed down to where it should be).	It'll also
	 * save a useless call to add_equijoined keys...
692 693 694 695
	 */
	distribute_qual_to_rels(root, (Node *) clause,
							true, true,
							pull_varnos((Node *) clause));
696 697 698
}


699 700
/*****************************************************************************
 *
701
 *	 CHECKS FOR MERGEJOINABLE AND HASHJOINABLE CLAUSES
702 703 704
 *
 *****************************************************************************/

705
/*
706 707 708 709 710 711
 * check_mergejoinable
 *	  If the restrictinfo's clause is mergejoinable, set the mergejoin
 *	  info fields in the restrictinfo.
 *
 *	  Currently, we support mergejoin for binary opclauses where
 *	  both operands are simple Vars and the operator is a mergejoinable
712
 *	  operator.
713
 */
714 715
static void
check_mergejoinable(RestrictInfo *restrictinfo)
716
{
717
	Expr	   *clause = restrictinfo->clause;
718 719 720 721
	Var		   *left,
			   *right;
	Oid			opno,
				leftOp,
722
				rightOp;
723

724
	if (!is_opclause((Node *) clause))
725
		return;
726 727 728 729 730

	left = get_leftop(clause);
	right = get_rightop(clause);

	/* caution: is_opclause accepts more than I do, so check it */
731
	if (!right)
732
		return;					/* unary opclauses need not apply */
733
	if (!IsA(left, Var) ||!IsA(right, Var))
734
		return;
735 736 737

	opno = ((Oper *) clause->oper)->opno;

738 739 740 741 742
	if (op_mergejoinable(opno,
						 left->vartype,
						 right->vartype,
						 &leftOp,
						 &rightOp))
743
	{
744 745 746
		restrictinfo->mergejoinoperator = opno;
		restrictinfo->left_sortop = leftOp;
		restrictinfo->right_sortop = rightOp;
747
	}
748 749
}

750
/*
751 752 753 754 755 756
 * check_hashjoinable
 *	  If the restrictinfo's clause is hashjoinable, set the hashjoin
 *	  info fields in the restrictinfo.
 *
 *	  Currently, we support hashjoin for binary opclauses where
 *	  both operands are simple Vars and the operator is a hashjoinable
757
 *	  operator.
758
 */
759 760
static void
check_hashjoinable(RestrictInfo *restrictinfo)
761
{
762
	Expr	   *clause = restrictinfo->clause;
763 764
	Var		   *left,
			   *right;
765
	Oid			opno;
766

767
	if (!is_opclause((Node *) clause))
768
		return;
769 770 771 772 773

	left = get_leftop(clause);
	right = get_rightop(clause);

	/* caution: is_opclause accepts more than I do, so check it */
774
	if (!right)
775
		return;					/* unary opclauses need not apply */
776
	if (!IsA(left, Var) ||!IsA(right, Var))
777 778 779
		return;

	opno = ((Oper *) clause->oper)->opno;
780

781 782 783 784
	if (op_hashjoinable(opno,
						left->vartype,
						right->vartype))
		restrictinfo->hashjoinoperator = opno;
785
}