OSDN Git Service

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