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 *-------------------------------------------------------------------------
17 * adjust_rows: tweak estimated row numbers according to the hint.
20 adjust_rows(double rows, RowsHint *hint)
22 double result = 0.0; /* keep compiler quiet */
24 if (hint->value_type == RVT_ABSOLUTE)
26 else if (hint->value_type == RVT_ADD)
27 result = rows + hint->rows;
28 else if (hint->value_type == RVT_SUB)
29 result = rows - hint->rows;
30 else if (hint->value_type == RVT_MULTI)
31 result = rows * hint->rows;
33 Assert(false); /* unrecognized rows value type */
35 hint->base.state = HINT_STATE_USED;
38 (errmsg("Force estimate to be at least one row, to avoid possible divide-by-zero when interpolating costs : %s",
39 hint->base.hint_str)));
40 result = clamp_row_est(result);
41 elog(DEBUG1, "adjusted rows %d to %d", (int) rows, (int) result);
48 * Find or create a join RelOptInfo that represents the join of
49 * the two given rels, and add to it path information for paths
50 * created with the two rels as outer and inner rel.
51 * (The join rel may already contain paths generated from other
52 * pairs of rels that add up to the same set of base rels.)
54 * NB: will return NULL if attempted join is not valid. This can happen
55 * when working with outer joins, or with IN or EXISTS clauses that have been
59 make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
62 SpecialJoinInfo *sjinfo;
64 SpecialJoinInfo sjinfo_data;
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 /* !!! START: HERE IS THE PART WHICH ADDED FOR PG_HINT_PLAN !!! */
121 RowsHint *rows_hint = NULL;
123 RowsHint *justforme = NULL;
124 RowsHint *domultiply = NULL;
126 /* Search for applicable rows hint for this join node */
127 for (i = 0; i < current_hint->num_hints[HINT_TYPE_ROWS]; i++)
129 rows_hint = current_hint->rows_hints[i];
132 * Skip this rows_hint if it is invalid from the first or it
133 * doesn't target any join rels.
135 if (!rows_hint->joinrelids ||
136 rows_hint->base.state == HINT_STATE_ERROR)
139 if (bms_equal(joinrelids, rows_hint->joinrelids))
142 * This joinrel is just the target of this rows_hint, so tweak
143 * rows estimation according to the hint.
145 justforme = rows_hint;
147 else if (!(bms_is_subset(rows_hint->joinrelids, rel1->relids) ||
148 bms_is_subset(rows_hint->joinrelids, rel2->relids)) &&
149 bms_is_subset(rows_hint->joinrelids, joinrelids) &&
150 rows_hint->value_type == RVT_MULTI)
153 * If the rows_hint's target relids is not a subset of both of
154 * component rels and is a subset of this joinrel, ths hint's
155 * targets spread over both component rels. This menas that
156 * this hint has been never applied so far and this joinrel is
157 * the first (and only) chance to fire in current join tree.
158 * Only the multiplication hint has the cumulative nature so we
159 * apply only RVT_MULTI in this way.
161 domultiply = rows_hint;
168 * If a hint just for me is found, no other adjust method is
169 * useles, but this cannot be more than twice becuase this joinrel
170 * is already adjusted by this hint.
172 if (justforme->base.state == HINT_STATE_NOTUSED)
173 joinrel->rows = adjust_rows(joinrel->rows, justforme);
180 * If we have multiple routes up to this joinrel which are not
181 * applicable this hint, this multiply hint will applied more
182 * than twice. But there's no means to know of that,
183 * re-estimate the row number of this joinrel always just
184 * before applying the hint. This is a bit different from
185 * normal planner behavior but it doesn't harm so much.
187 set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
190 joinrel->rows = adjust_rows(joinrel->rows, domultiply);
195 /* !!! END: HERE IS THE PART WHICH ADDED FOR PG_HINT_PLAN !!! */
198 * If we've already proven this join is empty, we needn't consider any
201 if (is_dummy_rel(joinrel))
203 bms_free(joinrelids);
208 * Consider paths using each rel as both outer and inner. Depending on
209 * the join type, a provably empty outer or inner rel might mean the join
210 * is provably empty too; in which case throw away any previously computed
211 * paths and mark the join as dummy. (We do it this way since it's
212 * conceivable that dummy-ness of a multi-element join might only be
213 * noticeable for certain construction paths.)
215 * Also, a provably constant-false join restriction typically means that
216 * we can skip evaluating one or both sides of the join. We do this by
217 * marking the appropriate rel as dummy. For outer joins, a
218 * constant-false restriction that is pushed down still means the whole
219 * join is dummy, while a non-pushed-down one means that no inner rows
220 * will join so we can treat the inner rel as dummy.
222 * We need only consider the jointypes that appear in join_info_list, plus
225 switch (sjinfo->jointype)
228 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
229 restriction_is_constant_false(restrictlist, false))
231 mark_dummy_rel(joinrel);
234 add_paths_to_joinrel(root, joinrel, rel1, rel2,
237 add_paths_to_joinrel(root, joinrel, rel2, rel1,
242 if (is_dummy_rel(rel1) ||
243 restriction_is_constant_false(restrictlist, true))
245 mark_dummy_rel(joinrel);
248 if (restriction_is_constant_false(restrictlist, false) &&
249 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
250 mark_dummy_rel(rel2);
251 add_paths_to_joinrel(root, joinrel, rel1, rel2,
254 add_paths_to_joinrel(root, joinrel, rel2, rel1,
259 if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
260 restriction_is_constant_false(restrictlist, true))
262 mark_dummy_rel(joinrel);
265 add_paths_to_joinrel(root, joinrel, rel1, rel2,
268 add_paths_to_joinrel(root, joinrel, rel2, rel1,
273 * If there are join quals that aren't mergeable or hashable, we
274 * may not be able to build any valid plan. Complain here so that
275 * we can give a somewhat-useful error message. (Since we have no
276 * flexibility of planning for a full join, there's no chance of
277 * succeeding later with another pair of input rels.)
279 if (joinrel->pathlist == NIL)
281 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
282 errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
287 * We might have a normal semijoin, or a case where we don't have
288 * enough rels to do the semijoin but can unique-ify the RHS and
289 * then do an innerjoin (see comments in join_is_legal). In the
290 * latter case we can't apply JOIN_SEMI joining.
292 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
293 bms_is_subset(sjinfo->min_righthand, rel2->relids))
295 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
296 restriction_is_constant_false(restrictlist, false))
298 mark_dummy_rel(joinrel);
301 add_paths_to_joinrel(root, joinrel, rel1, rel2,
307 * If we know how to unique-ify the RHS and one input rel is
308 * exactly the RHS (not a superset) we can consider unique-ifying
309 * it and then doing a regular join. (The create_unique_path
310 * check here is probably redundant with what join_is_legal did,
311 * but if so the check is cheap because it's cached. So test
312 * anyway to be sure.)
314 if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
315 create_unique_path(root, rel2, rel2->cheapest_total_path,
318 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
319 restriction_is_constant_false(restrictlist, false))
321 mark_dummy_rel(joinrel);
324 add_paths_to_joinrel(root, joinrel, rel1, rel2,
325 JOIN_UNIQUE_INNER, sjinfo,
327 add_paths_to_joinrel(root, joinrel, rel2, rel1,
328 JOIN_UNIQUE_OUTER, sjinfo,
333 if (is_dummy_rel(rel1) ||
334 restriction_is_constant_false(restrictlist, true))
336 mark_dummy_rel(joinrel);
339 if (restriction_is_constant_false(restrictlist, false) &&
340 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
341 mark_dummy_rel(rel2);
342 add_paths_to_joinrel(root, joinrel, rel1, rel2,
347 /* other values not expected here */
348 elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
352 bms_free(joinrelids);