Skip to content

Commit 1bd2635

Browse files
committed
Marginal code cleanup in joinpath.c: factor out clause variable-membership
tests into a small common subroutine, and eliminate an unnecessary difference in the order in which conditions are tested. Per a comment from Robert Haas.
1 parent f92bbb8 commit 1bd2635

File tree

1 file changed

+44
-50
lines changed

1 file changed

+44
-50
lines changed

src/backend/optimizer/path/joinpath.c

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/path/joinpath.c,v 1.124 2009/09/17 20:49:28 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/path/joinpath.c,v 1.125 2009/09/18 17:24:51 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -102,7 +102,8 @@ add_paths_to_joinrel(PlannerInfo *root,
102102
*
103103
* Note: do this after join_is_removable(), because this sets the
104104
* outer_is_left flags in the mergejoin clauses, while join_is_removable
105-
* uses those flags for its own purposes.
105+
* uses those flags for its own purposes. Currently, they set the flags
106+
* the same way anyway, but let's avoid unnecessary entanglement.
106107
*/
107108
if (enable_mergejoin || jointype == JOIN_FULL)
108109
mergeclause_list = select_mergejoin_clauses(root,
@@ -153,6 +154,37 @@ add_paths_to_joinrel(PlannerInfo *root,
153154
restrictlist, jointype, sjinfo);
154155
}
155156

157+
/*
158+
* clause_matches_join
159+
* Determine whether a join clause is of the right form to use in this join.
160+
*
161+
* We already know that the clause is a binary opclause referencing only the
162+
* rels in the current join. The point here is to check whether it has the
163+
* form "outerrel_expr op innerrel_expr" or "innerrel_expr op outerrel_expr",
164+
* rather than mixing outer and inner vars on either side. If it is usable,
165+
* we set the transient flag outer_is_left to identify which side is which.
166+
*/
167+
static inline bool
168+
clause_matches_join(RestrictInfo *rinfo, RelOptInfo *outerrel,
169+
RelOptInfo *innerrel)
170+
{
171+
if (bms_is_subset(rinfo->left_relids, outerrel->relids) &&
172+
bms_is_subset(rinfo->right_relids, innerrel->relids))
173+
{
174+
/* lefthand side is outer */
175+
rinfo->outer_is_left = true;
176+
return true;
177+
}
178+
else if (bms_is_subset(rinfo->left_relids, innerrel->relids) &&
179+
bms_is_subset(rinfo->right_relids, outerrel->relids))
180+
{
181+
/* righthand side is outer */
182+
rinfo->outer_is_left = false;
183+
return true;
184+
}
185+
return false; /* no good for these input relations */
186+
}
187+
156188
/*
157189
* join_is_removable
158190
* Determine whether we need not perform the join at all, because
@@ -233,23 +265,9 @@ join_is_removable(PlannerInfo *root,
233265
continue; /* not mergejoinable */
234266

235267
/*
236-
* Check if clause is usable with these input rels. All the vars
237-
* needed on each side of the clause must be available from one or the
238-
* other of the input rels.
268+
* Check if clause has the form "outer op inner" or "inner op outer".
239269
*/
240-
if (bms_is_subset(restrictinfo->left_relids, outerrel->relids) &&
241-
bms_is_subset(restrictinfo->right_relids, innerrel->relids))
242-
{
243-
/* righthand side is inner */
244-
restrictinfo->outer_is_left = true;
245-
}
246-
else if (bms_is_subset(restrictinfo->left_relids, innerrel->relids) &&
247-
bms_is_subset(restrictinfo->right_relids, outerrel->relids))
248-
{
249-
/* lefthand side is inner */
250-
restrictinfo->outer_is_left = false;
251-
}
252-
else
270+
if (!clause_matches_join(restrictinfo, outerrel, innerrel))
253271
continue; /* no good for these input relations */
254272

255273
/* OK, add to list */
@@ -977,31 +995,21 @@ hash_inner_and_outer(PlannerInfo *root,
977995
{
978996
RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
979997

980-
if (!restrictinfo->can_join ||
981-
restrictinfo->hashjoinoperator == InvalidOid)
982-
continue; /* not hashjoinable */
983-
984998
/*
985999
* If processing an outer join, only use its own join clauses for
9861000
* hashing. For inner joins we need not be so picky.
9871001
*/
9881002
if (isouterjoin && restrictinfo->is_pushed_down)
9891003
continue;
9901004

1005+
if (!restrictinfo->can_join ||
1006+
restrictinfo->hashjoinoperator == InvalidOid)
1007+
continue; /* not hashjoinable */
1008+
9911009
/*
992-
* Check if clause is usable with these input rels.
1010+
* Check if clause has the form "outer op inner" or "inner op outer".
9931011
*/
994-
if (bms_is_subset(restrictinfo->left_relids, outerrel->relids) &&
995-
bms_is_subset(restrictinfo->right_relids, innerrel->relids))
996-
{
997-
/* righthand side is inner */
998-
}
999-
else if (bms_is_subset(restrictinfo->left_relids, innerrel->relids) &&
1000-
bms_is_subset(restrictinfo->right_relids, outerrel->relids))
1001-
{
1002-
/* lefthand side is inner */
1003-
}
1004-
else
1012+
if (!clause_matches_join(restrictinfo, outerrel, innerrel))
10051013
continue; /* no good for these input relations */
10061014

10071015
hashclauses = lappend(hashclauses, restrictinfo);
@@ -1176,23 +1184,9 @@ select_mergejoin_clauses(PlannerInfo *root,
11761184
}
11771185

11781186
/*
1179-
* Check if clause is usable with these input rels. All the vars
1180-
* needed on each side of the clause must be available from one or the
1181-
* other of the input rels.
1187+
* Check if clause has the form "outer op inner" or "inner op outer".
11821188
*/
1183-
if (bms_is_subset(restrictinfo->left_relids, outerrel->relids) &&
1184-
bms_is_subset(restrictinfo->right_relids, innerrel->relids))
1185-
{
1186-
/* righthand side is inner */
1187-
restrictinfo->outer_is_left = true;
1188-
}
1189-
else if (bms_is_subset(restrictinfo->left_relids, innerrel->relids) &&
1190-
bms_is_subset(restrictinfo->right_relids, outerrel->relids))
1191-
{
1192-
/* lefthand side is inner */
1193-
restrictinfo->outer_is_left = false;
1194-
}
1195-
else
1189+
if (!clause_matches_join(restrictinfo, outerrel, innerrel))
11961190
{
11971191
have_nonmergeable_joinclause = true;
11981192
continue; /* no good for these input relations */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy