1 /*-------------------------------------------------------------------------
4 * Routines copied from PostgreSQL core distribution with some
7 * src/backend/optimizer/path/joinrels.c
9 * This file contains the following functions from corresponding files.
13 * populate_joinrel_with_paths()
15 * Portions Copyright (c) 2013-2021, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
16 * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
17 * Portions Copyright (c) 1994, Regents of the University of California
19 *-------------------------------------------------------------------------
23 * adjust_rows: tweak estimated row numbers according to the hint.
26 adjust_rows(double rows, RowsHint *hint)
28 double result = 0.0; /* keep compiler quiet */
30 if (hint->value_type == RVT_ABSOLUTE)
32 else if (hint->value_type == RVT_ADD)
33 result = rows + hint->rows;
34 else if (hint->value_type == RVT_SUB)
35 result = rows - hint->rows;
36 else if (hint->value_type == RVT_MULTI)
37 result = rows * hint->rows;
39 Assert(false); /* unrecognized rows value type */
41 hint->base.state = HINT_STATE_USED;
44 (errmsg("Force estimate to be at least one row, to avoid possible divide-by-zero when interpolating costs : %s",
45 hint->base.hint_str)));
46 result = clamp_row_est(result);
47 elog(DEBUG1, "adjusted rows %d to %d", (int) rows, (int) result);
55 * Find or create a join RelOptInfo that represents the join of
56 * the two given rels, and add to it path information for paths
57 * created with the two rels as outer and inner rel.
58 * (The join rel may already contain paths generated from other
59 * pairs of rels that add up to the same set of base rels.)
61 * NB: will return NULL if attempted join is not valid. This can happen
62 * when working with outer joins, or with IN or EXISTS clauses that have been
66 make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
69 SpecialJoinInfo *sjinfo;
71 SpecialJoinInfo sjinfo_data;
75 /* We should never try to join two overlapping sets of rels. */
76 Assert(!bms_overlap(rel1->relids, rel2->relids));
78 /* Construct Relids set that identifies the joinrel. */
79 joinrelids = bms_union(rel1->relids, rel2->relids);
81 /* Check validity and determine join type. */
82 if (!join_is_legal(root, rel1, rel2, joinrelids,
85 /* invalid join path */
90 /* Swap rels if needed to match the join info. */
93 RelOptInfo *trel = rel1;
100 * If it's a plain inner join, then we won't have found anything in
101 * join_info_list. Make up a SpecialJoinInfo so that selectivity
102 * estimation functions will know what's being joined.
106 sjinfo = &sjinfo_data;
107 sjinfo->type = T_SpecialJoinInfo;
108 sjinfo->min_lefthand = rel1->relids;
109 sjinfo->min_righthand = rel2->relids;
110 sjinfo->syn_lefthand = rel1->relids;
111 sjinfo->syn_righthand = rel2->relids;
112 sjinfo->jointype = JOIN_INNER;
113 /* we don't bother trying to make the remaining fields valid */
114 sjinfo->lhs_strict = false;
115 sjinfo->delay_upper_joins = false;
116 sjinfo->semi_can_btree = false;
117 sjinfo->semi_can_hash = false;
118 sjinfo->semi_operators = NIL;
119 sjinfo->semi_rhs_exprs = NIL;
123 * Find or build the join RelOptInfo, and compute the restrictlist that
124 * goes with this particular joining.
126 joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
129 /* !!! START: HERE IS THE PART WHICH IS ADDED FOR PG_HINT_PLAN !!! */
131 RowsHint *rows_hint = NULL;
133 RowsHint *justforme = NULL;
134 RowsHint *domultiply = NULL;
136 /* Search for applicable rows hint for this join node */
137 for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_ROWS]; i++)
139 rows_hint = current_hint_state->rows_hints[i];
142 * Skip this rows_hint if it is invalid from the first or it
143 * doesn't target any join rels.
145 if (!rows_hint->joinrelids ||
146 rows_hint->base.state == HINT_STATE_ERROR)
149 if (bms_equal(joinrelids, rows_hint->joinrelids))
152 * This joinrel is just the target of this rows_hint, so tweak
153 * rows estimation according to the hint.
155 justforme = rows_hint;
157 else if (!(bms_is_subset(rows_hint->joinrelids, rel1->relids) ||
158 bms_is_subset(rows_hint->joinrelids, rel2->relids)) &&
159 bms_is_subset(rows_hint->joinrelids, joinrelids) &&
160 rows_hint->value_type == RVT_MULTI)
163 * If the rows_hint's target relids is not a subset of both of
164 * component rels and is a subset of this joinrel, ths hint's
165 * targets spread over both component rels. This menas that
166 * this hint has been never applied so far and this joinrel is
167 * the first (and only) chance to fire in current join tree.
168 * Only the multiplication hint has the cumulative nature so we
169 * apply only RVT_MULTI in this way.
171 domultiply = rows_hint;
178 * If a hint just for me is found, no other adjust method is
179 * useles, but this cannot be more than twice becuase this joinrel
180 * is already adjusted by this hint.
182 if (justforme->base.state == HINT_STATE_NOTUSED)
183 joinrel->rows = adjust_rows(joinrel->rows, justforme);
190 * If we have multiple routes up to this joinrel which are not
191 * applicable this hint, this multiply hint will applied more
192 * than twice. But there's no means to know of that,
193 * re-estimate the row number of this joinrel always just
194 * before applying the hint. This is a bit different from
195 * normal planner behavior but it doesn't harm so much.
197 set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
200 joinrel->rows = adjust_rows(joinrel->rows, domultiply);
205 /* !!! END: HERE IS THE PART WHICH IS ADDED FOR PG_HINT_PLAN !!! */
208 * If we've already proven this join is empty, we needn't consider any
211 if (is_dummy_rel(joinrel))
213 bms_free(joinrelids);
217 /* Add paths to the join relation. */
218 populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
221 bms_free(joinrelids);
228 * populate_joinrel_with_paths
229 * Add paths to the given joinrel for given pair of joining relations. The
230 * SpecialJoinInfo provides details about the join and the restrictlist
231 * contains the join clauses and the other clauses applicable for given pair
232 * of the joining relations.
235 populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
236 RelOptInfo *rel2, RelOptInfo *joinrel,
237 SpecialJoinInfo *sjinfo, List *restrictlist)
240 * Consider paths using each rel as both outer and inner. Depending on
241 * the join type, a provably empty outer or inner rel might mean the join
242 * is provably empty too; in which case throw away any previously computed
243 * paths and mark the join as dummy. (We do it this way since it's
244 * conceivable that dummy-ness of a multi-element join might only be
245 * noticeable for certain construction paths.)
247 * Also, a provably constant-false join restriction typically means that
248 * we can skip evaluating one or both sides of the join. We do this by
249 * marking the appropriate rel as dummy. For outer joins, a
250 * constant-false restriction that is pushed down still means the whole
251 * join is dummy, while a non-pushed-down one means that no inner rows
252 * will join so we can treat the inner rel as dummy.
254 * We need only consider the jointypes that appear in join_info_list, plus
257 switch (sjinfo->jointype)
260 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
261 restriction_is_constant_false(restrictlist, joinrel, false))
263 mark_dummy_rel(joinrel);
266 add_paths_to_joinrel(root, joinrel, rel1, rel2,
269 add_paths_to_joinrel(root, joinrel, rel2, rel1,
274 if (is_dummy_rel(rel1) ||
275 restriction_is_constant_false(restrictlist, joinrel, true))
277 mark_dummy_rel(joinrel);
280 if (restriction_is_constant_false(restrictlist, joinrel, false) &&
281 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
282 mark_dummy_rel(rel2);
283 add_paths_to_joinrel(root, joinrel, rel1, rel2,
286 add_paths_to_joinrel(root, joinrel, rel2, rel1,
291 if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
292 restriction_is_constant_false(restrictlist, joinrel, true))
294 mark_dummy_rel(joinrel);
297 add_paths_to_joinrel(root, joinrel, rel1, rel2,
300 add_paths_to_joinrel(root, joinrel, rel2, rel1,
305 * If there are join quals that aren't mergeable or hashable, we
306 * may not be able to build any valid plan. Complain here so that
307 * we can give a somewhat-useful error message. (Since we have no
308 * flexibility of planning for a full join, there's no chance of
309 * succeeding later with another pair of input rels.)
311 if (joinrel->pathlist == NIL)
313 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
314 errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
319 * We might have a normal semijoin, or a case where we don't have
320 * enough rels to do the semijoin but can unique-ify the RHS and
321 * then do an innerjoin (see comments in join_is_legal). In the
322 * latter case we can't apply JOIN_SEMI joining.
324 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
325 bms_is_subset(sjinfo->min_righthand, rel2->relids))
327 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
328 restriction_is_constant_false(restrictlist, joinrel, false))
330 mark_dummy_rel(joinrel);
333 add_paths_to_joinrel(root, joinrel, rel1, rel2,
339 * If we know how to unique-ify the RHS and one input rel is
340 * exactly the RHS (not a superset) we can consider unique-ifying
341 * it and then doing a regular join. (The create_unique_path
342 * check here is probably redundant with what join_is_legal did,
343 * but if so the check is cheap because it's cached. So test
344 * anyway to be sure.)
346 if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
347 create_unique_path(root, rel2, rel2->cheapest_total_path,
350 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
351 restriction_is_constant_false(restrictlist, joinrel, false))
353 mark_dummy_rel(joinrel);
356 add_paths_to_joinrel(root, joinrel, rel1, rel2,
357 JOIN_UNIQUE_INNER, sjinfo,
359 add_paths_to_joinrel(root, joinrel, rel2, rel1,
360 JOIN_UNIQUE_OUTER, sjinfo,
365 if (is_dummy_rel(rel1) ||
366 restriction_is_constant_false(restrictlist, joinrel, true))
368 mark_dummy_rel(joinrel);
371 if (restriction_is_constant_false(restrictlist, joinrel, false) &&
372 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
373 mark_dummy_rel(rel2);
374 add_paths_to_joinrel(root, joinrel, rel1, rel2,
379 /* other values not expected here */
380 elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
384 /* Apply partitionwise join technique, if possible. */
385 try_partitionwise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);