summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2009-07-23 17:42:06 +0000
committerTom Lane2009-07-23 17:42:06 +0000
commit1f63bf9c741decd21c80471f3c158f6361517655 (patch)
tree0a4ad8701a0eeb187407a8522387050ccae057bb
parentb94f1fa6a08e00881766856975edb8c15f1405f9 (diff)
Fix another thinko in join_is_legal's handling of semijoins: we have to test
for the case that the semijoin was implemented within either input by unique-ifying its RHS before we test to see if it appears to match the current join situation. The previous coding would select semijoin logic in situations where we'd already unique-ified the RHS and joined it to some unrelated relation(s), and then came to join it to the semijoin's LHS. That still gave the right answer as far as the semijoin itself was concerned, but would lead to incorrectly examining only an arbitrary one of the matchable rows from the unrelated relation(s). The cause of this thinko was incorrect unification of the pre-8.4 logic for IN joins and OUTER joins --- the comparable case for outer joins can be handled after making the match test, but that's because there is nothing like the unique-ification escape hatch for outer joins. Per bug #4934 from Benjamin Reed.
-rw-r--r--src/backend/optimizer/path/joinrels.c25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/backend/optimizer/path/joinrels.c b/src/backend/optimizer/path/joinrels.c
index 0b1b4849f4..6531c258b1 100644
--- a/src/backend/optimizer/path/joinrels.c
+++ b/src/backend/optimizer/path/joinrels.c
@@ -400,6 +400,22 @@ join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
continue;
/*
+ * If it's a semijoin and we already joined the RHS to any other
+ * rels within either input, then we must have unique-ified the RHS
+ * at that point (see below). Therefore the semijoin is no longer
+ * relevant in this join path.
+ */
+ if (sjinfo->jointype == JOIN_SEMI)
+ {
+ if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
+ !bms_equal(sjinfo->syn_righthand, rel1->relids))
+ continue;
+ if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
+ !bms_equal(sjinfo->syn_righthand, rel2->relids))
+ continue;
+ }
+
+ /*
* If one input contains min_lefthand and the other contains
* min_righthand, then we can perform the SJ at this join.
*
@@ -491,9 +507,6 @@ join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
* We assume that make_outerjoininfo() set things up correctly
* so that we'll only match to some SJ if the join is valid.
* Set flag here to check at bottom of loop.
- *
- * For a semijoin, assume it's okay if either side fully contains
- * the RHS (per the unique-ification case above).
*----------
*/
if (sjinfo->jointype != JOIN_SEMI &&
@@ -503,12 +516,6 @@ join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
/* seems OK */
Assert(!bms_overlap(joinrelids, sjinfo->min_lefthand));
}
- else if (sjinfo->jointype == JOIN_SEMI &&
- (bms_is_subset(sjinfo->syn_righthand, rel1->relids) ||
- bms_is_subset(sjinfo->syn_righthand, rel2->relids)))
- {
- /* seems OK */
- }
else
is_valid_inner = false;
}