1 /*-------------------------------------------------------------------------
4 * Routines copied from PostgreSQL core distribution.
6 * src/backend/optimizer/path/joinrels.c
9 * Portions Copyright (c) 2013-2014, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
10 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
18 * Find or create a join RelOptInfo that represents the join of
19 * the two given rels, and add to it path information for paths
20 * created with the two rels as outer and inner rel.
21 * (The join rel may already contain paths generated from other
22 * pairs of rels that add up to the same set of base rels.)
24 * NB: will return NULL if attempted join is not valid. This can happen
25 * when working with outer joins, or with IN or EXISTS clauses that have been
29 adjust_rows(double rows, RowsHint *hint)
31 double result = 0.0; /* keep compiler quiet */
33 if (hint->value_type == RVT_ABSOLUTE)
35 else if (hint->value_type == RVT_ADD)
36 result = rows + hint->rows;
37 else if (hint->value_type == RVT_SUB)
38 result = rows - hint->rows;
39 else if (hint->value_type == RVT_MULTI)
40 result = rows * hint->rows;
42 Assert(false); /* unrecognized rows value type */
44 hint->base.state = HINT_STATE_USED;
47 (errmsg("Force estimate to be at least one row, to avoid possible divide-by-zero when interpolating costs : %s",
48 hint->base.hint_str)));
49 result = clamp_row_est(result);
50 elog(DEBUG1, "adjusted rows %d to %d", (int) rows, (int) result);
56 make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
59 SpecialJoinInfo *sjinfo;
61 SpecialJoinInfo sjinfo_data;
65 RowsHint *rows_hint = NULL;
68 /* We should never try to join two overlapping sets of rels. */
69 Assert(!bms_overlap(rel1->relids, rel2->relids));
71 /* Construct Relids set that identifies the joinrel. */
72 joinrelids = bms_union(rel1->relids, rel2->relids);
74 /* Check validity and determine join type. */
75 if (!join_is_legal(root, rel1, rel2, joinrelids,
78 /* invalid join path */
83 /* Swap rels if needed to match the join info. */
86 RelOptInfo *trel = rel1;
93 * If it's a plain inner join, then we won't have found anything in
94 * join_info_list. Make up a SpecialJoinInfo so that selectivity
95 * estimation functions will know what's being joined.
99 sjinfo = &sjinfo_data;
100 sjinfo->type = T_SpecialJoinInfo;
101 sjinfo->min_lefthand = rel1->relids;
102 sjinfo->min_righthand = rel2->relids;
103 sjinfo->syn_lefthand = rel1->relids;
104 sjinfo->syn_righthand = rel2->relids;
105 sjinfo->jointype = JOIN_INNER;
106 /* we don't bother trying to make the remaining fields valid */
107 sjinfo->lhs_strict = false;
108 sjinfo->delay_upper_joins = false;
109 sjinfo->join_quals = NIL;
113 * Find or build the join RelOptInfo, and compute the restrictlist that
114 * goes with this particular joining.
116 joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
119 /* Apply appropriate Rows hint to the join node, if any. */
120 for (i = 0; i < current_hint->num_hints[HINT_TYPE_ROWS]; i++)
122 rows_hint = current_hint->rows_hints[i];
125 * This Rows hint is invalid for some reason, or it contains no
126 * aliasname which exists in the query.
128 if (!rows_hint->joinrelids ||
129 rows_hint->base.state == HINT_STATE_ERROR)
132 if (bms_equal(joinrelids, rows_hint->joinrelids))
135 * This join RelOptInfo is exactly a Rows hint specifies, so adjust
136 * rows estimateion with the hint's content. Here we never have
137 * another hint which has same relation combination, so we can skip
140 if (rows_hint->base.state == HINT_STATE_NOTUSED)
141 joinrel->rows = adjust_rows(joinrel->rows, rows_hint);
143 else if (bms_is_subset(rows_hint->joinrelids, rel1->relids) ||
144 bms_is_subset(rows_hint->joinrelids, rel2->relids))
147 * Otherwise if the relation combination specified in thee Rows
148 * hint is subset of the set of join elements, re-estimate rows and
149 * costs again to reflect the adjustment done in down. This is
150 * necessary for the first permutation of the combination the
151 * relations, but it's difficult to determine that this is the
152 * first, so do this everytime.
154 set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
157 else if (bms_is_subset(rows_hint->joinrelids, joinrelids))
160 * If the combination specifed in the Rows hints is subset of the
161 * join relation and spreads over both children,
163 * We do adjust rows estimation only when the value type was
164 * multiplication, because other value types are meanless.
166 if (rows_hint->value_type == RVT_MULTI)
168 set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
170 joinrel->rows = adjust_rows(joinrel->rows, rows_hint);
176 * If we've already proven this join is empty, we needn't consider any
179 if (is_dummy_rel(joinrel))
181 bms_free(joinrelids);
186 * Consider paths using each rel as both outer and inner. Depending on
187 * the join type, a provably empty outer or inner rel might mean the join
188 * is provably empty too; in which case throw away any previously computed
189 * paths and mark the join as dummy. (We do it this way since it's
190 * conceivable that dummy-ness of a multi-element join might only be
191 * noticeable for certain construction paths.)
193 * Also, a provably constant-false join restriction typically means that
194 * we can skip evaluating one or both sides of the join. We do this by
195 * marking the appropriate rel as dummy. For outer joins, a
196 * constant-false restriction that is pushed down still means the whole
197 * join is dummy, while a non-pushed-down one means that no inner rows
198 * will join so we can treat the inner rel as dummy.
200 * We need only consider the jointypes that appear in join_info_list, plus
203 switch (sjinfo->jointype)
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,
215 add_paths_to_joinrel(root, joinrel, rel2, rel1,
220 if (is_dummy_rel(rel1) ||
221 restriction_is_constant_false(restrictlist, true))
223 mark_dummy_rel(joinrel);
226 if (restriction_is_constant_false(restrictlist, false) &&
227 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
228 mark_dummy_rel(rel2);
229 add_paths_to_joinrel(root, joinrel, rel1, rel2,
232 add_paths_to_joinrel(root, joinrel, rel2, rel1,
237 if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
238 restriction_is_constant_false(restrictlist, true))
240 mark_dummy_rel(joinrel);
243 add_paths_to_joinrel(root, joinrel, rel1, rel2,
246 add_paths_to_joinrel(root, joinrel, rel2, rel1,
251 * If there are join quals that aren't mergeable or hashable, we
252 * may not be able to build any valid plan. Complain here so that
253 * we can give a somewhat-useful error message. (Since we have no
254 * flexibility of planning for a full join, there's no chance of
255 * succeeding later with another pair of input rels.)
257 if (joinrel->pathlist == NIL)
259 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
260 errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
265 * We might have a normal semijoin, or a case where we don't have
266 * enough rels to do the semijoin but can unique-ify the RHS and
267 * then do an innerjoin (see comments in join_is_legal). In the
268 * latter case we can't apply JOIN_SEMI joining.
270 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
271 bms_is_subset(sjinfo->min_righthand, rel2->relids))
273 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
274 restriction_is_constant_false(restrictlist, false))
276 mark_dummy_rel(joinrel);
279 add_paths_to_joinrel(root, joinrel, rel1, rel2,
285 * If we know how to unique-ify the RHS and one input rel is
286 * exactly the RHS (not a superset) we can consider unique-ifying
287 * it and then doing a regular join. (The create_unique_path
288 * check here is probably redundant with what join_is_legal did,
289 * but if so the check is cheap because it's cached. So test
290 * anyway to be sure.)
292 if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
293 create_unique_path(root, rel2, rel2->cheapest_total_path,
296 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
297 restriction_is_constant_false(restrictlist, false))
299 mark_dummy_rel(joinrel);
302 add_paths_to_joinrel(root, joinrel, rel1, rel2,
303 JOIN_UNIQUE_INNER, sjinfo,
305 add_paths_to_joinrel(root, joinrel, rel2, rel1,
306 JOIN_UNIQUE_OUTER, sjinfo,
311 if (is_dummy_rel(rel1) ||
312 restriction_is_constant_false(restrictlist, true))
314 mark_dummy_rel(joinrel);
317 if (restriction_is_constant_false(restrictlist, false) &&
318 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
319 mark_dummy_rel(rel2);
320 add_paths_to_joinrel(root, joinrel, rel1, rel2,
325 /* other values not expected here */
326 elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
330 bms_free(joinrelids);