1 /*-------------------------------------------------------------------------
4 * Routines copied from PostgreSQL core distribution.
6 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 *-------------------------------------------------------------------------
14 * Find or create a join RelOptInfo that represents the join of
15 * the two given rels, and add to it path information for paths
16 * created with the two rels as outer and inner rel.
17 * (The join rel may already contain paths generated from other
18 * pairs of rels that add up to the same set of base rels.)
20 * NB: will return NULL if attempted join is not valid. This can happen
21 * when working with outer joins, or with IN or EXISTS clauses that have been
25 make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
28 SpecialJoinInfo *sjinfo;
30 SpecialJoinInfo sjinfo_data;
34 /* We should never try to join two overlapping sets of rels. */
35 Assert(!bms_overlap(rel1->relids, rel2->relids));
37 /* Construct Relids set that identifies the joinrel. */
38 joinrelids = bms_union(rel1->relids, rel2->relids);
40 /* Check validity and determine join type. */
41 if (!join_is_legal(root, rel1, rel2, joinrelids,
44 /* invalid join path */
49 /* Swap rels if needed to match the join info. */
52 RelOptInfo *trel = rel1;
59 * If it's a plain inner join, then we won't have found anything in
60 * join_info_list. Make up a SpecialJoinInfo so that selectivity
61 * estimation functions will know what's being joined.
65 sjinfo = &sjinfo_data;
66 sjinfo->type = T_SpecialJoinInfo;
67 sjinfo->min_lefthand = rel1->relids;
68 sjinfo->min_righthand = rel2->relids;
69 sjinfo->syn_lefthand = rel1->relids;
70 sjinfo->syn_righthand = rel2->relids;
71 sjinfo->jointype = JOIN_INNER;
72 /* we don't bother trying to make the remaining fields valid */
73 sjinfo->lhs_strict = false;
74 sjinfo->delay_upper_joins = false;
75 sjinfo->join_quals = NIL;
79 * Find or build the join RelOptInfo, and compute the restrictlist that
80 * goes with this particular joining.
82 joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
86 * If we've already proven this join is empty, we needn't consider any
89 if (is_dummy_rel(joinrel))
96 * Consider paths using each rel as both outer and inner. Depending on
97 * the join type, a provably empty outer or inner rel might mean the join
98 * is provably empty too; in which case throw away any previously computed
99 * paths and mark the join as dummy. (We do it this way since it's
100 * conceivable that dummy-ness of a multi-element join might only be
101 * noticeable for certain construction paths.)
103 * Also, a provably constant-false join restriction typically means that
104 * we can skip evaluating one or both sides of the join. We do this by
105 * marking the appropriate rel as dummy. For outer joins, a
106 * constant-false restriction that is pushed down still means the whole
107 * join is dummy, while a non-pushed-down one means that no inner rows
108 * will join so we can treat the inner rel as dummy.
110 * We need only consider the jointypes that appear in join_info_list, plus
113 switch (sjinfo->jointype)
116 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
117 restriction_is_constant_false(restrictlist, false))
119 mark_dummy_rel(joinrel);
122 add_paths_to_joinrel(root, joinrel, rel1, rel2,
125 add_paths_to_joinrel(root, joinrel, rel2, rel1,
130 if (is_dummy_rel(rel1) ||
131 restriction_is_constant_false(restrictlist, true))
133 mark_dummy_rel(joinrel);
136 if (restriction_is_constant_false(restrictlist, false) &&
137 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
138 mark_dummy_rel(rel2);
139 add_paths_to_joinrel(root, joinrel, rel1, rel2,
142 add_paths_to_joinrel(root, joinrel, rel2, rel1,
147 if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
148 restriction_is_constant_false(restrictlist, true))
150 mark_dummy_rel(joinrel);
153 add_paths_to_joinrel(root, joinrel, rel1, rel2,
156 add_paths_to_joinrel(root, joinrel, rel2, rel1,
161 * If there are join quals that aren't mergeable or hashable, we
162 * may not be able to build any valid plan. Complain here so that
163 * we can give a somewhat-useful error message. (Since we have no
164 * flexibility of planning for a full join, there's no chance of
165 * succeeding later with another pair of input rels.)
167 if (joinrel->pathlist == NIL)
169 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
170 errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
175 * We might have a normal semijoin, or a case where we don't have
176 * enough rels to do the semijoin but can unique-ify the RHS and
177 * then do an innerjoin (see comments in join_is_legal). In the
178 * latter case we can't apply JOIN_SEMI joining.
180 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
181 bms_is_subset(sjinfo->min_righthand, rel2->relids))
183 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
184 restriction_is_constant_false(restrictlist, false))
186 mark_dummy_rel(joinrel);
189 add_paths_to_joinrel(root, joinrel, rel1, rel2,
195 * If we know how to unique-ify the RHS and one input rel is
196 * exactly the RHS (not a superset) we can consider unique-ifying
197 * it and then doing a regular join. (The create_unique_path
198 * check here is probably redundant with what join_is_legal did,
199 * but if so the check is cheap because it's cached. So test
200 * anyway to be sure.)
202 if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
203 create_unique_path(root, rel2, rel2->cheapest_total_path,
206 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
207 restriction_is_constant_false(restrictlist, false))
209 mark_dummy_rel(joinrel);
212 add_paths_to_joinrel(root, joinrel, rel1, rel2,
213 JOIN_UNIQUE_INNER, sjinfo,
215 add_paths_to_joinrel(root, joinrel, rel2, rel1,
216 JOIN_UNIQUE_OUTER, sjinfo,
221 if (is_dummy_rel(rel1) ||
222 restriction_is_constant_false(restrictlist, true))
224 mark_dummy_rel(joinrel);
227 if (restriction_is_constant_false(restrictlist, false) &&
228 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
229 mark_dummy_rel(rel2);
230 add_paths_to_joinrel(root, joinrel, rel1, rel2,
235 /* other values not expected here */
236 elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
240 bms_free(joinrelids);