OSDN Git Service

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