OSDN Git Service

Support PG12.
[pghintplan/pg_hint_plan.git] / make_join_rel.c
1 /*-------------------------------------------------------------------------
2  *
3  * make_join_rel.c
4  *        Routines copied from PostgreSQL core distribution with some
5  *        modifications.
6  *
7  * src/backend/optimizer/path/joinrels.c
8  *     make_join_rel()
9  *
10  * Portions Copyright (c) 2013-2020, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
11  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  *-------------------------------------------------------------------------
15  */
16
17 /*
18  * adjust_rows: tweak estimated row numbers according to the hint.
19  */
20 static double
21 adjust_rows(double rows, RowsHint *hint)
22 {
23         double          result = 0.0;   /* keep compiler quiet */
24
25         if (hint->value_type == RVT_ABSOLUTE)
26                 result = hint->rows;
27         else if (hint->value_type == RVT_ADD)
28                 result = rows + hint->rows;
29         else if (hint->value_type == RVT_SUB)
30                 result =  rows - hint->rows;
31         else if (hint->value_type == RVT_MULTI)
32                 result = rows * hint->rows;
33         else
34                 Assert(false);  /* unrecognized rows value type */
35
36         hint->base.state = HINT_STATE_USED;
37         if (result < 1.0)
38                 ereport(WARNING,
39                                 (errmsg("Force estimate to be at least one row, to avoid possible divide-by-zero when interpolating costs : %s",
40                                         hint->base.hint_str)));
41         result = clamp_row_est(result);
42         elog(DEBUG1, "adjusted rows %d to %d", (int) rows, (int) result);
43
44         return result;
45 }
46
47 /*
48  * make_join_rel
49  *         Find or create a join RelOptInfo that represents the join of
50  *         the two given rels, and add to it path information for paths
51  *         created with the two rels as outer and inner rel.
52  *         (The join rel may already contain paths generated from other
53  *         pairs of rels that add up to the same set of base rels.)
54  *
55  * NB: will return NULL if attempted join is not valid.  This can happen
56  * when working with outer joins, or with IN or EXISTS clauses that have been
57  * turned into joins.
58  */
59 RelOptInfo *
60 make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
61 {
62         Relids          joinrelids;
63         SpecialJoinInfo *sjinfo;
64         bool            reversed;
65         SpecialJoinInfo sjinfo_data;
66         RelOptInfo *joinrel;
67         List       *restrictlist;
68
69         /* We should never try to join two overlapping sets of rels. */
70         Assert(!bms_overlap(rel1->relids, rel2->relids));
71
72         /* Construct Relids set that identifies the joinrel. */
73         joinrelids = bms_union(rel1->relids, rel2->relids);
74
75         /* Check validity and determine join type. */
76         if (!join_is_legal(root, rel1, rel2, joinrelids,
77                                            &sjinfo, &reversed))
78         {
79                 /* invalid join path */
80                 bms_free(joinrelids);
81                 return NULL;
82         }
83
84         /* Swap rels if needed to match the join info. */
85         if (reversed)
86         {
87                 RelOptInfo *trel = rel1;
88
89                 rel1 = rel2;
90                 rel2 = trel;
91         }
92
93         /*
94          * If it's a plain inner join, then we won't have found anything in
95          * join_info_list.  Make up a SpecialJoinInfo so that selectivity
96          * estimation functions will know what's being joined.
97          */
98         if (sjinfo == NULL)
99         {
100                 sjinfo = &sjinfo_data;
101                 sjinfo->type = T_SpecialJoinInfo;
102                 sjinfo->min_lefthand = rel1->relids;
103                 sjinfo->min_righthand = rel2->relids;
104                 sjinfo->syn_lefthand = rel1->relids;
105                 sjinfo->syn_righthand = rel2->relids;
106                 sjinfo->jointype = JOIN_INNER;
107                 /* we don't bother trying to make the remaining fields valid */
108                 sjinfo->lhs_strict = false;
109                 sjinfo->delay_upper_joins = false;
110                 sjinfo->semi_can_btree = false;
111                 sjinfo->semi_can_hash = false;
112                 sjinfo->semi_operators = NIL;
113                 sjinfo->semi_rhs_exprs = NIL;
114         }
115
116         /*
117          * Find or build the join RelOptInfo, and compute the restrictlist that
118          * goes with this particular joining.
119          */
120         joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
121                                                          &restrictlist);
122
123         /* !!! START: HERE IS THE PART WHICH ADDED FOR PG_HINT_PLAN !!! */
124         {
125                 RowsHint   *rows_hint = NULL;
126                 int                     i;
127                 RowsHint   *justforme = NULL;
128                 RowsHint   *domultiply = NULL;
129
130                 /* Search for applicable rows hint for this join node */
131                 for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_ROWS]; i++)
132                 {
133                         rows_hint = current_hint_state->rows_hints[i];
134
135                         /*
136                          * Skip this rows_hint if it is invalid from the first or it
137                          * doesn't target any join rels.
138                          */
139                         if (!rows_hint->joinrelids ||
140                                 rows_hint->base.state == HINT_STATE_ERROR)
141                                 continue;
142
143                         if (bms_equal(joinrelids, rows_hint->joinrelids))
144                         {
145                                 /*
146                                  * This joinrel is just the target of this rows_hint, so tweak
147                                  * rows estimation according to the hint.
148                                  */
149                                 justforme = rows_hint;
150                         }
151                         else if (!(bms_is_subset(rows_hint->joinrelids, rel1->relids) ||
152                                            bms_is_subset(rows_hint->joinrelids, rel2->relids)) &&
153                                          bms_is_subset(rows_hint->joinrelids, joinrelids) &&
154                                          rows_hint->value_type == RVT_MULTI)
155                         {
156                                 /*
157                                  * If the rows_hint's target relids is not a subset of both of
158                                  * component rels and is a subset of this joinrel, ths hint's
159                                  * targets spread over both component rels. This menas that
160                                  * this hint has been never applied so far and this joinrel is
161                                  * the first (and only) chance to fire in current join tree.
162                                  * Only the multiplication hint has the cumulative nature so we
163                                  * apply only RVT_MULTI in this way.
164                                  */
165                                 domultiply = rows_hint;
166                         }
167                 }
168
169                 if (justforme)
170                 {
171                         /*
172                          * If a hint just for me is found, no other adjust method is
173                          * useles, but this cannot be more than twice becuase this joinrel
174                          * is already adjusted by this hint.
175                          */
176                         if (justforme->base.state == HINT_STATE_NOTUSED)
177                                 joinrel->rows = adjust_rows(joinrel->rows, justforme);
178                 }
179                 else
180                 {
181                         if (domultiply)
182                         {
183                                 /*
184                                  * If we have multiple routes up to this joinrel which are not
185                                  * applicable this hint, this multiply hint will applied more
186                                  * than twice. But there's no means to know of that,
187                                  * re-estimate the row number of this joinrel always just
188                                  * before applying the hint. This is a bit different from
189                                  * normal planner behavior but it doesn't harm so much.
190                                  */
191                                 set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
192                                                                                    restrictlist);
193                                 
194                                 joinrel->rows = adjust_rows(joinrel->rows, domultiply);
195                         }
196                         
197                 }
198         }
199         /* !!! END: HERE IS THE PART WHICH ADDED FOR PG_HINT_PLAN !!! */
200
201         /*
202          * If we've already proven this join is empty, we needn't consider any
203          * more paths for it.
204          */
205         if (is_dummy_rel(joinrel))
206         {
207                 bms_free(joinrelids);
208                 return joinrel;
209         }
210
211         /* Add paths to the join relation. */
212         populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
213                                                                 restrictlist);
214
215         bms_free(joinrelids);
216
217         return joinrel;
218 }
219
220 /*
221  * populate_joinrel_with_paths
222  *        Add paths to the given joinrel for given pair of joining relations. The
223  *        SpecialJoinInfo provides details about the join and the restrictlist
224  *        contains the join clauses and the other clauses applicable for given pair
225  *        of the joining relations.
226  */
227 static void
228 populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
229                                                         RelOptInfo *rel2, RelOptInfo *joinrel,
230                                                         SpecialJoinInfo *sjinfo, List *restrictlist)
231 {
232         /*
233          * Consider paths using each rel as both outer and inner.  Depending on
234          * the join type, a provably empty outer or inner rel might mean the join
235          * is provably empty too; in which case throw away any previously computed
236          * paths and mark the join as dummy.  (We do it this way since it's
237          * conceivable that dummy-ness of a multi-element join might only be
238          * noticeable for certain construction paths.)
239          *
240          * Also, a provably constant-false join restriction typically means that
241          * we can skip evaluating one or both sides of the join.  We do this by
242          * marking the appropriate rel as dummy.  For outer joins, a
243          * constant-false restriction that is pushed down still means the whole
244          * join is dummy, while a non-pushed-down one means that no inner rows
245          * will join so we can treat the inner rel as dummy.
246          *
247          * We need only consider the jointypes that appear in join_info_list, plus
248          * JOIN_INNER.
249          */
250         switch (sjinfo->jointype)
251         {
252                 case JOIN_INNER:
253                         if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
254                                 restriction_is_constant_false(restrictlist, joinrel, false))
255                         {
256                                 mark_dummy_rel(joinrel);
257                                 break;
258                         }
259                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
260                                                                  JOIN_INNER, sjinfo,
261                                                                  restrictlist);
262                         add_paths_to_joinrel(root, joinrel, rel2, rel1,
263                                                                  JOIN_INNER, sjinfo,
264                                                                  restrictlist);
265                         break;
266                 case JOIN_LEFT:
267                         if (is_dummy_rel(rel1) ||
268                                 restriction_is_constant_false(restrictlist, joinrel, true))
269                         {
270                                 mark_dummy_rel(joinrel);
271                                 break;
272                         }
273                         if (restriction_is_constant_false(restrictlist, joinrel, false) &&
274                                 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
275                                 mark_dummy_rel(rel2);
276                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
277                                                                  JOIN_LEFT, sjinfo,
278                                                                  restrictlist);
279                         add_paths_to_joinrel(root, joinrel, rel2, rel1,
280                                                                  JOIN_RIGHT, sjinfo,
281                                                                  restrictlist);
282                         break;
283                 case JOIN_FULL:
284                         if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
285                                 restriction_is_constant_false(restrictlist, joinrel, true))
286                         {
287                                 mark_dummy_rel(joinrel);
288                                 break;
289                         }
290                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
291                                                                  JOIN_FULL, sjinfo,
292                                                                  restrictlist);
293                         add_paths_to_joinrel(root, joinrel, rel2, rel1,
294                                                                  JOIN_FULL, sjinfo,
295                                                                  restrictlist);
296
297                         /*
298                          * If there are join quals that aren't mergeable or hashable, we
299                          * may not be able to build any valid plan.  Complain here so that
300                          * we can give a somewhat-useful error message.  (Since we have no
301                          * flexibility of planning for a full join, there's no chance of
302                          * succeeding later with another pair of input rels.)
303                          */
304                         if (joinrel->pathlist == NIL)
305                                 ereport(ERROR,
306                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
307                                                  errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
308                         break;
309                 case JOIN_SEMI:
310
311                         /*
312                          * We might have a normal semijoin, or a case where we don't have
313                          * enough rels to do the semijoin but can unique-ify the RHS and
314                          * then do an innerjoin (see comments in join_is_legal).  In the
315                          * latter case we can't apply JOIN_SEMI joining.
316                          */
317                         if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
318                                 bms_is_subset(sjinfo->min_righthand, rel2->relids))
319                         {
320                                 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
321                                         restriction_is_constant_false(restrictlist, joinrel, false))
322                                 {
323                                         mark_dummy_rel(joinrel);
324                                         break;
325                                 }
326                                 add_paths_to_joinrel(root, joinrel, rel1, rel2,
327                                                                          JOIN_SEMI, sjinfo,
328                                                                          restrictlist);
329                         }
330
331                         /*
332                          * If we know how to unique-ify the RHS and one input rel is
333                          * exactly the RHS (not a superset) we can consider unique-ifying
334                          * it and then doing a regular join.  (The create_unique_path
335                          * check here is probably redundant with what join_is_legal did,
336                          * but if so the check is cheap because it's cached.  So test
337                          * anyway to be sure.)
338                          */
339                         if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
340                                 create_unique_path(root, rel2, rel2->cheapest_total_path,
341                                                                    sjinfo) != NULL)
342                         {
343                                 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
344                                         restriction_is_constant_false(restrictlist, joinrel, false))
345                                 {
346                                         mark_dummy_rel(joinrel);
347                                         break;
348                                 }
349                                 add_paths_to_joinrel(root, joinrel, rel1, rel2,
350                                                                          JOIN_UNIQUE_INNER, sjinfo,
351                                                                          restrictlist);
352                                 add_paths_to_joinrel(root, joinrel, rel2, rel1,
353                                                                          JOIN_UNIQUE_OUTER, sjinfo,
354                                                                          restrictlist);
355                         }
356                         break;
357                 case JOIN_ANTI:
358                         if (is_dummy_rel(rel1) ||
359                                 restriction_is_constant_false(restrictlist, joinrel, true))
360                         {
361                                 mark_dummy_rel(joinrel);
362                                 break;
363                         }
364                         if (restriction_is_constant_false(restrictlist, joinrel, false) &&
365                                 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
366                                 mark_dummy_rel(rel2);
367                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
368                                                                  JOIN_ANTI, sjinfo,
369                                                                  restrictlist);
370                         break;
371                 default:
372                         /* other values not expected here */
373                         elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
374                         break;
375         }
376
377         /* Apply partitionwise join technique, if possible. */
378         try_partitionwise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);
379 }