1 /*-------------------------------------------------------------------------
4 * Routines copied from PostgreSQL core distribution with some
7 * src/backend/optimizer/path/joinrels.c
10 * Portions Copyright (c) 2013-2017, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
11 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
14 *-------------------------------------------------------------------------
17 static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
18 RelOptInfo *rel2, RelOptInfo *joinrel,
19 SpecialJoinInfo *sjinfo,
22 * adjust_rows: tweak estimated row numbers according to the hint.
25 adjust_rows(double rows, RowsHint *hint)
27 double result = 0.0; /* keep compiler quiet */
29 if (hint->value_type == RVT_ABSOLUTE)
31 else if (hint->value_type == RVT_ADD)
32 result = rows + hint->rows;
33 else if (hint->value_type == RVT_SUB)
34 result = rows - hint->rows;
35 else if (hint->value_type == RVT_MULTI)
36 result = rows * hint->rows;
38 Assert(false); /* unrecognized rows value type */
40 hint->base.state = HINT_STATE_USED;
43 (errmsg("Force estimate to be at least one row, to avoid possible divide-by-zero when interpolating costs : %s",
44 hint->base.hint_str)));
45 result = clamp_row_est(result);
46 elog(DEBUG1, "adjusted rows %d to %d", (int) rows, (int) result);
53 * Find or create a join RelOptInfo that represents the join of
54 * the two given rels, and add to it path information for paths
55 * created with the two rels as outer and inner rel.
56 * (The join rel may already contain paths generated from other
57 * pairs of rels that add up to the same set of base rels.)
59 * NB: will return NULL if attempted join is not valid. This can happen
60 * when working with outer joins, or with IN or EXISTS clauses that have been
64 make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
67 SpecialJoinInfo *sjinfo;
69 SpecialJoinInfo sjinfo_data;
73 /* We should never try to join two overlapping sets of rels. */
74 Assert(!bms_overlap(rel1->relids, rel2->relids));
76 /* Construct Relids set that identifies the joinrel. */
77 joinrelids = bms_union(rel1->relids, rel2->relids);
79 /* Check validity and determine join type. */
80 if (!join_is_legal(root, rel1, rel2, joinrelids,
83 /* invalid join path */
88 /* Swap rels if needed to match the join info. */
91 RelOptInfo *trel = rel1;
98 * If it's a plain inner join, then we won't have found anything in
99 * join_info_list. Make up a SpecialJoinInfo so that selectivity
100 * estimation functions will know what's being joined.
104 sjinfo = &sjinfo_data;
105 sjinfo->type = T_SpecialJoinInfo;
106 sjinfo->min_lefthand = rel1->relids;
107 sjinfo->min_righthand = rel2->relids;
108 sjinfo->syn_lefthand = rel1->relids;
109 sjinfo->syn_righthand = rel2->relids;
110 sjinfo->jointype = JOIN_INNER;
111 /* we don't bother trying to make the remaining fields valid */
112 sjinfo->lhs_strict = false;
113 sjinfo->delay_upper_joins = false;
114 sjinfo->semi_can_btree = false;
115 sjinfo->semi_can_hash = false;
116 sjinfo->semi_operators = NIL;
117 sjinfo->semi_rhs_exprs = NIL;
121 * Find or build the join RelOptInfo, and compute the restrictlist that
122 * goes with this particular joining.
124 joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
127 /* !!! START: HERE IS THE PART WHICH ADDED FOR PG_HINT_PLAN !!! */
129 RowsHint *rows_hint = NULL;
131 RowsHint *justforme = NULL;
132 RowsHint *domultiply = NULL;
134 /* Search for applicable rows hint for this join node */
135 for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_ROWS]; i++)
137 rows_hint = current_hint_state->rows_hints[i];
140 * Skip this rows_hint if it is invalid from the first or it
141 * doesn't target any join rels.
143 if (!rows_hint->joinrelids ||
144 rows_hint->base.state == HINT_STATE_ERROR)
147 if (bms_equal(joinrelids, rows_hint->joinrelids))
150 * This joinrel is just the target of this rows_hint, so tweak
151 * rows estimation according to the hint.
153 justforme = rows_hint;
155 else if (!(bms_is_subset(rows_hint->joinrelids, rel1->relids) ||
156 bms_is_subset(rows_hint->joinrelids, rel2->relids)) &&
157 bms_is_subset(rows_hint->joinrelids, joinrelids) &&
158 rows_hint->value_type == RVT_MULTI)
161 * If the rows_hint's target relids is not a subset of both of
162 * component rels and is a subset of this joinrel, ths hint's
163 * targets spread over both component rels. This menas that
164 * this hint has been never applied so far and this joinrel is
165 * the first (and only) chance to fire in current join tree.
166 * Only the multiplication hint has the cumulative nature so we
167 * apply only RVT_MULTI in this way.
169 domultiply = rows_hint;
176 * If a hint just for me is found, no other adjust method is
177 * useles, but this cannot be more than twice becuase this joinrel
178 * is already adjusted by this hint.
180 if (justforme->base.state == HINT_STATE_NOTUSED)
181 joinrel->rows = adjust_rows(joinrel->rows, justforme);
188 * If we have multiple routes up to this joinrel which are not
189 * applicable this hint, this multiply hint will applied more
190 * than twice. But there's no means to know of that,
191 * re-estimate the row number of this joinrel always just
192 * before applying the hint. This is a bit different from
193 * normal planner behavior but it doesn't harm so much.
195 set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
198 joinrel->rows = adjust_rows(joinrel->rows, domultiply);
203 /* !!! END: HERE IS THE PART WHICH ADDED FOR PG_HINT_PLAN !!! */
206 * If we've already proven this join is empty, we needn't consider any
209 if (is_dummy_rel(joinrel))
211 bms_free(joinrelids);
215 /* Add paths to the join relation. */
216 populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
219 bms_free(joinrelids);
225 * populate_joinrel_with_paths
226 * Add paths to the given joinrel for given pair of joining relations. The
227 * SpecialJoinInfo provides details about the join and the restrictlist
228 * contains the join clauses and the other clauses applicable for given pair
229 * of the joining relations.
232 populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
233 RelOptInfo *rel2, RelOptInfo *joinrel,
234 SpecialJoinInfo *sjinfo, List *restrictlist)
237 * Consider paths using each rel as both outer and inner. Depending on
238 * the join type, a provably empty outer or inner rel might mean the join
239 * is provably empty too; in which case throw away any previously computed
240 * paths and mark the join as dummy. (We do it this way since it's
241 * conceivable that dummy-ness of a multi-element join might only be
242 * noticeable for certain construction paths.)
244 * Also, a provably constant-false join restriction typically means that
245 * we can skip evaluating one or both sides of the join. We do this by
246 * marking the appropriate rel as dummy. For outer joins, a
247 * constant-false restriction that is pushed down still means the whole
248 * join is dummy, while a non-pushed-down one means that no inner rows
249 * will join so we can treat the inner rel as dummy.
251 * We need only consider the jointypes that appear in join_info_list, plus
254 switch (sjinfo->jointype)
257 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
258 restriction_is_constant_false(restrictlist, false))
260 mark_dummy_rel(joinrel);
263 add_paths_to_joinrel(root, joinrel, rel1, rel2,
266 add_paths_to_joinrel(root, joinrel, rel2, rel1,
271 if (is_dummy_rel(rel1) ||
272 restriction_is_constant_false(restrictlist, true))
274 mark_dummy_rel(joinrel);
277 if (restriction_is_constant_false(restrictlist, false) &&
278 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
279 mark_dummy_rel(rel2);
280 add_paths_to_joinrel(root, joinrel, rel1, rel2,
283 add_paths_to_joinrel(root, joinrel, rel2, rel1,
288 if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
289 restriction_is_constant_false(restrictlist, true))
291 mark_dummy_rel(joinrel);
294 add_paths_to_joinrel(root, joinrel, rel1, rel2,
297 add_paths_to_joinrel(root, joinrel, rel2, rel1,
302 * If there are join quals that aren't mergeable or hashable, we
303 * may not be able to build any valid plan. Complain here so that
304 * we can give a somewhat-useful error message. (Since we have no
305 * flexibility of planning for a full join, there's no chance of
306 * succeeding later with another pair of input rels.)
308 if (joinrel->pathlist == NIL)
310 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
311 errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
316 * We might have a normal semijoin, or a case where we don't have
317 * enough rels to do the semijoin but can unique-ify the RHS and
318 * then do an innerjoin (see comments in join_is_legal). In the
319 * latter case we can't apply JOIN_SEMI joining.
321 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
322 bms_is_subset(sjinfo->min_righthand, rel2->relids))
324 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
325 restriction_is_constant_false(restrictlist, false))
327 mark_dummy_rel(joinrel);
330 add_paths_to_joinrel(root, joinrel, rel1, rel2,
336 * If we know how to unique-ify the RHS and one input rel is
337 * exactly the RHS (not a superset) we can consider unique-ifying
338 * it and then doing a regular join. (The create_unique_path
339 * check here is probably redundant with what join_is_legal did,
340 * but if so the check is cheap because it's cached. So test
341 * anyway to be sure.)
343 if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
344 create_unique_path(root, rel2, rel2->cheapest_total_path,
347 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
348 restriction_is_constant_false(restrictlist, false))
350 mark_dummy_rel(joinrel);
353 add_paths_to_joinrel(root, joinrel, rel1, rel2,
354 JOIN_UNIQUE_INNER, sjinfo,
356 add_paths_to_joinrel(root, joinrel, rel2, rel1,
357 JOIN_UNIQUE_OUTER, sjinfo,
362 if (is_dummy_rel(rel1) ||
363 restriction_is_constant_false(restrictlist, true))
365 mark_dummy_rel(joinrel);
368 if (restriction_is_constant_false(restrictlist, false) &&
369 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
370 mark_dummy_rel(rel2);
371 add_paths_to_joinrel(root, joinrel, rel1, rel2,
376 /* other values not expected here */
377 elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);