OSDN Git Service

Rows関連試験の予測結果を、全試験を流した場合のものに変更した。
[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, 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("make rows estimation 1 since below 1 : %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         RowsHint   *rows_hint = NULL;
66         int                     i;
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->join_quals = NIL;
110         }
111
112         /*
113          * Find or build the join RelOptInfo, and compute the restrictlist that
114          * goes with this particular joining.
115          */
116         joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
117                                                          &restrictlist);
118
119         /* Apply appropriate Rows hint to the join node, if any. */
120         for (i = 0; i < current_hint->num_hints[HINT_TYPE_ROWS]; i++)
121         {
122                 rows_hint = current_hint->rows_hints[i];
123
124                 /*
125                  * This Rows hint specifies aliasname is error, or does not exist in
126                  * query.
127                  */
128                 if (!rows_hint->joinrelids ||
129                         rows_hint->base.state == HINT_STATE_ERROR)
130                         continue;
131                 if (bms_equal(joinrelids, rows_hint->joinrelids))
132                 {
133                         /*
134                          * This join RelOptInfo is exactly a Rows hint specifies, so adjust
135                          * rows estimateion with the hint's content.  Here we never have
136                          * another hint which has same relation combination, so we can skip
137                          * rest of hints.
138                          */
139                         if (rows_hint->base.state == HINT_STATE_NOTUSED)
140                                 joinrel->rows = adjust_rows(joinrel->rows, rows_hint);
141                 }
142                 else if (bms_is_subset(rows_hint->joinrelids, rel1->relids) ||
143                                  bms_is_subset(rows_hint->joinrelids, rel2->relids))
144                 {
145                         /*
146                          * Otherwise if the relation combination specified in thee Rows
147                          * hint is subset of the set of join elements, re-estimate rows and
148                          * costs again to reflect the adjustment done in down.  This is
149                          * necessary for the first permutation of the combination the
150                          * relations, but it's difficult to determine that this is the
151                          * first, so do this everytime.
152                          */
153                         set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
154                                                                            restrictlist);
155                 }
156                 else if (bms_is_subset(rows_hint->joinrelids, joinrelids))
157                 {
158                         /*
159                          * If the combination specifed in the Rows hints is subset of the
160                          * join relation and spreads over both children, 
161                          *
162                          * We do adjust rows estimation only when the value type was
163                          * multiplication, because other value types are meanless.
164                          */
165                         if (rows_hint->value_type == RVT_MULTI)
166                         {
167                                 set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
168                                                                                    restrictlist);
169                                 joinrel->rows = adjust_rows(joinrel->rows, rows_hint);
170                         }
171                 }
172         }
173
174         /*
175          * If we've already proven this join is empty, we needn't consider any
176          * more paths for it.
177          */
178         if (is_dummy_rel(joinrel))
179         {
180                 bms_free(joinrelids);
181                 return joinrel;
182         }
183
184         /*
185          * Consider paths using each rel as both outer and inner.  Depending on
186          * the join type, a provably empty outer or inner rel might mean the join
187          * is provably empty too; in which case throw away any previously computed
188          * paths and mark the join as dummy.  (We do it this way since it's
189          * conceivable that dummy-ness of a multi-element join might only be
190          * noticeable for certain construction paths.)
191          *
192          * Also, a provably constant-false join restriction typically means that
193          * we can skip evaluating one or both sides of the join.  We do this by
194          * marking the appropriate rel as dummy.  For outer joins, a
195          * constant-false restriction that is pushed down still means the whole
196          * join is dummy, while a non-pushed-down one means that no inner rows
197          * will join so we can treat the inner rel as dummy.
198          *
199          * We need only consider the jointypes that appear in join_info_list, plus
200          * JOIN_INNER.
201          */
202         switch (sjinfo->jointype)
203         {
204                 case JOIN_INNER:
205                         if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
206                                 restriction_is_constant_false(restrictlist, false))
207                         {
208                                 mark_dummy_rel(joinrel);
209                                 break;
210                         }
211                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
212                                                                  JOIN_INNER, sjinfo,
213                                                                  restrictlist);
214                         add_paths_to_joinrel(root, joinrel, rel2, rel1,
215                                                                  JOIN_INNER, sjinfo,
216                                                                  restrictlist);
217                         break;
218                 case JOIN_LEFT:
219                         if (is_dummy_rel(rel1) ||
220                                 restriction_is_constant_false(restrictlist, true))
221                         {
222                                 mark_dummy_rel(joinrel);
223                                 break;
224                         }
225                         if (restriction_is_constant_false(restrictlist, false) &&
226                                 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
227                                 mark_dummy_rel(rel2);
228                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
229                                                                  JOIN_LEFT, sjinfo,
230                                                                  restrictlist);
231                         add_paths_to_joinrel(root, joinrel, rel2, rel1,
232                                                                  JOIN_RIGHT, sjinfo,
233                                                                  restrictlist);
234                         break;
235                 case JOIN_FULL:
236                         if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
237                                 restriction_is_constant_false(restrictlist, true))
238                         {
239                                 mark_dummy_rel(joinrel);
240                                 break;
241                         }
242                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
243                                                                  JOIN_FULL, sjinfo,
244                                                                  restrictlist);
245                         add_paths_to_joinrel(root, joinrel, rel2, rel1,
246                                                                  JOIN_FULL, sjinfo,
247                                                                  restrictlist);
248
249                         /*
250                          * If there are join quals that aren't mergeable or hashable, we
251                          * may not be able to build any valid plan.  Complain here so that
252                          * we can give a somewhat-useful error message.  (Since we have no
253                          * flexibility of planning for a full join, there's no chance of
254                          * succeeding later with another pair of input rels.)
255                          */
256                         if (joinrel->pathlist == NIL)
257                                 ereport(ERROR,
258                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
259                                                  errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
260                         break;
261                 case JOIN_SEMI:
262
263                         /*
264                          * We might have a normal semijoin, or a case where we don't have
265                          * enough rels to do the semijoin but can unique-ify the RHS and
266                          * then do an innerjoin (see comments in join_is_legal).  In the
267                          * latter case we can't apply JOIN_SEMI joining.
268                          */
269                         if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
270                                 bms_is_subset(sjinfo->min_righthand, rel2->relids))
271                         {
272                                 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
273                                         restriction_is_constant_false(restrictlist, false))
274                                 {
275                                         mark_dummy_rel(joinrel);
276                                         break;
277                                 }
278                                 add_paths_to_joinrel(root, joinrel, rel1, rel2,
279                                                                          JOIN_SEMI, sjinfo,
280                                                                          restrictlist);
281                         }
282
283                         /*
284                          * If we know how to unique-ify the RHS and one input rel is
285                          * exactly the RHS (not a superset) we can consider unique-ifying
286                          * it and then doing a regular join.  (The create_unique_path
287                          * check here is probably redundant with what join_is_legal did,
288                          * but if so the check is cheap because it's cached.  So test
289                          * anyway to be sure.)
290                          */
291                         if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
292                                 create_unique_path(root, rel2, rel2->cheapest_total_path,
293                                                                    sjinfo) != NULL)
294                         {
295                                 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
296                                         restriction_is_constant_false(restrictlist, false))
297                                 {
298                                         mark_dummy_rel(joinrel);
299                                         break;
300                                 }
301                                 add_paths_to_joinrel(root, joinrel, rel1, rel2,
302                                                                          JOIN_UNIQUE_INNER, sjinfo,
303                                                                          restrictlist);
304                                 add_paths_to_joinrel(root, joinrel, rel2, rel1,
305                                                                          JOIN_UNIQUE_OUTER, sjinfo,
306                                                                          restrictlist);
307                         }
308                         break;
309                 case JOIN_ANTI:
310                         if (is_dummy_rel(rel1) ||
311                                 restriction_is_constant_false(restrictlist, true))
312                         {
313                                 mark_dummy_rel(joinrel);
314                                 break;
315                         }
316                         if (restriction_is_constant_false(restrictlist, false) &&
317                                 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
318                                 mark_dummy_rel(rel2);
319                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
320                                                                  JOIN_ANTI, sjinfo,
321                                                                  restrictlist);
322                         break;
323                 default:
324                         /* other values not expected here */
325                         elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
326                         break;
327         }
328
329         bms_free(joinrelids);
330
331         return joinrel;
332 }