OSDN Git Service

スキャン方式試験のコメントが一部不適切だったため修正した。
[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         result = clamp_row_est(result);
46         elog(DEBUG1, "adjusted rows %d to %d", (int) rows, (int) result);
47
48         return result;
49 }
50
51 RelOptInfo *
52 make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
53 {
54         Relids          joinrelids;
55         SpecialJoinInfo *sjinfo;
56         bool            reversed;
57         SpecialJoinInfo sjinfo_data;
58         RelOptInfo *joinrel;
59         List       *restrictlist;
60
61         RowsHint   *rows_hint = NULL;
62         int                     i;
63
64         /* We should never try to join two overlapping sets of rels. */
65         Assert(!bms_overlap(rel1->relids, rel2->relids));
66
67         /* Construct Relids set that identifies the joinrel. */
68         joinrelids = bms_union(rel1->relids, rel2->relids);
69
70         /* Check validity and determine join type. */
71         if (!join_is_legal(root, rel1, rel2, joinrelids,
72                                            &sjinfo, &reversed))
73         {
74                 /* invalid join path */
75                 bms_free(joinrelids);
76                 return NULL;
77         }
78
79         /* Swap rels if needed to match the join info. */
80         if (reversed)
81         {
82                 RelOptInfo *trel = rel1;
83
84                 rel1 = rel2;
85                 rel2 = trel;
86         }
87
88         /*
89          * If it's a plain inner join, then we won't have found anything in
90          * join_info_list.      Make up a SpecialJoinInfo so that selectivity
91          * estimation functions will know what's being joined.
92          */
93         if (sjinfo == NULL)
94         {
95                 sjinfo = &sjinfo_data;
96                 sjinfo->type = T_SpecialJoinInfo;
97                 sjinfo->min_lefthand = rel1->relids;
98                 sjinfo->min_righthand = rel2->relids;
99                 sjinfo->syn_lefthand = rel1->relids;
100                 sjinfo->syn_righthand = rel2->relids;
101                 sjinfo->jointype = JOIN_INNER;
102                 /* we don't bother trying to make the remaining fields valid */
103                 sjinfo->lhs_strict = false;
104                 sjinfo->delay_upper_joins = false;
105                 sjinfo->join_quals = NIL;
106         }
107
108         /*
109          * Find or build the join RelOptInfo, and compute the restrictlist that
110          * goes with this particular joining.
111          */
112         joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
113                                                          &restrictlist);
114
115         /* Apply appropriate Rows hint to the join node, if any. */
116         for (i = 0; i < current_hint->num_hints[HINT_TYPE_ROWS]; i++)
117         {
118                 rows_hint = current_hint->rows_hints[i];
119
120                 if (bms_equal(joinrelids, rows_hint->joinrelids))
121                 {
122                         /*
123                          * This join RelOptInfo is exactly a Rows hint specifies, so adjust
124                          * rows estimateion with the hint's content.  Here we never have
125                          * another hint which has same relation combination, so we can skip
126                          * rest of hints.
127                          */
128                         if (rows_hint->base.state == HINT_STATE_NOTUSED)
129                                 joinrel->rows = adjust_rows(joinrel->rows, rows_hint);
130                 }
131                 else if (bms_is_subset(rows_hint->joinrelids, rel1->relids) ||
132                                  bms_is_subset(rows_hint->joinrelids, rel2->relids))
133                 {
134                         /*
135                          * Otherwise if the relation combination specified in thee Rows
136                          * hint is subset of the set of join elements, re-estimate rows and
137                          * costs again to reflect the adjustment done in down.  This is
138                          * necessary for the first permutation of the combination the
139                          * relations, but it's difficult to determine that this is the
140                          * first, so do this everytime.
141                          */
142                         set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
143                                                                            restrictlist);
144                 }
145                 else if (bms_is_subset(rows_hint->joinrelids, joinrelids))
146                 {
147                         /*
148                          * If the combination specifed in the Rows hints is subset of the
149                          * join relation and spreads over both children, 
150                          *
151                          * We do adjust rows estimation only when the value type was
152                          * multiplication, because other value types are meanless.
153                          */
154                         if (rows_hint->value_type == RVT_MULTI)
155                         {
156                                 set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
157                                                                                    restrictlist);
158                                 joinrel->rows = adjust_rows(joinrel->rows, rows_hint);
159                         }
160                 }
161         }
162
163         /*
164          * If we've already proven this join is empty, we needn't consider any
165          * more paths for it.
166          */
167         if (is_dummy_rel(joinrel))
168         {
169                 bms_free(joinrelids);
170                 return joinrel;
171         }
172
173         /*
174          * Consider paths using each rel as both outer and inner.  Depending on
175          * the join type, a provably empty outer or inner rel might mean the join
176          * is provably empty too; in which case throw away any previously computed
177          * paths and mark the join as dummy.  (We do it this way since it's
178          * conceivable that dummy-ness of a multi-element join might only be
179          * noticeable for certain construction paths.)
180          *
181          * Also, a provably constant-false join restriction typically means that
182          * we can skip evaluating one or both sides of the join.  We do this by
183          * marking the appropriate rel as dummy.  For outer joins, a
184          * constant-false restriction that is pushed down still means the whole
185          * join is dummy, while a non-pushed-down one means that no inner rows
186          * will join so we can treat the inner rel as dummy.
187          *
188          * We need only consider the jointypes that appear in join_info_list, plus
189          * JOIN_INNER.
190          */
191         switch (sjinfo->jointype)
192         {
193                 case JOIN_INNER:
194                         if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
195                                 restriction_is_constant_false(restrictlist, false))
196                         {
197                                 mark_dummy_rel(joinrel);
198                                 break;
199                         }
200                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
201                                                                  JOIN_INNER, sjinfo,
202                                                                  restrictlist);
203                         add_paths_to_joinrel(root, joinrel, rel2, rel1,
204                                                                  JOIN_INNER, sjinfo,
205                                                                  restrictlist);
206                         break;
207                 case JOIN_LEFT:
208                         if (is_dummy_rel(rel1) ||
209                                 restriction_is_constant_false(restrictlist, true))
210                         {
211                                 mark_dummy_rel(joinrel);
212                                 break;
213                         }
214                         if (restriction_is_constant_false(restrictlist, false) &&
215                                 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
216                                 mark_dummy_rel(rel2);
217                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
218                                                                  JOIN_LEFT, sjinfo,
219                                                                  restrictlist);
220                         add_paths_to_joinrel(root, joinrel, rel2, rel1,
221                                                                  JOIN_RIGHT, sjinfo,
222                                                                  restrictlist);
223                         break;
224                 case JOIN_FULL:
225                         if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
226                                 restriction_is_constant_false(restrictlist, true))
227                         {
228                                 mark_dummy_rel(joinrel);
229                                 break;
230                         }
231                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
232                                                                  JOIN_FULL, sjinfo,
233                                                                  restrictlist);
234                         add_paths_to_joinrel(root, joinrel, rel2, rel1,
235                                                                  JOIN_FULL, sjinfo,
236                                                                  restrictlist);
237
238                         /*
239                          * If there are join quals that aren't mergeable or hashable, we
240                          * may not be able to build any valid plan.  Complain here so that
241                          * we can give a somewhat-useful error message.  (Since we have no
242                          * flexibility of planning for a full join, there's no chance of
243                          * succeeding later with another pair of input rels.)
244                          */
245                         if (joinrel->pathlist == NIL)
246                                 ereport(ERROR,
247                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
248                                                  errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
249                         break;
250                 case JOIN_SEMI:
251
252                         /*
253                          * We might have a normal semijoin, or a case where we don't have
254                          * enough rels to do the semijoin but can unique-ify the RHS and
255                          * then do an innerjoin (see comments in join_is_legal).  In the
256                          * latter case we can't apply JOIN_SEMI joining.
257                          */
258                         if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
259                                 bms_is_subset(sjinfo->min_righthand, rel2->relids))
260                         {
261                                 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
262                                         restriction_is_constant_false(restrictlist, false))
263                                 {
264                                         mark_dummy_rel(joinrel);
265                                         break;
266                                 }
267                                 add_paths_to_joinrel(root, joinrel, rel1, rel2,
268                                                                          JOIN_SEMI, sjinfo,
269                                                                          restrictlist);
270                         }
271
272                         /*
273                          * If we know how to unique-ify the RHS and one input rel is
274                          * exactly the RHS (not a superset) we can consider unique-ifying
275                          * it and then doing a regular join.  (The create_unique_path
276                          * check here is probably redundant with what join_is_legal did,
277                          * but if so the check is cheap because it's cached.  So test
278                          * anyway to be sure.)
279                          */
280                         if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
281                                 create_unique_path(root, rel2, rel2->cheapest_total_path,
282                                                                    sjinfo) != NULL)
283                         {
284                                 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
285                                         restriction_is_constant_false(restrictlist, false))
286                                 {
287                                         mark_dummy_rel(joinrel);
288                                         break;
289                                 }
290                                 add_paths_to_joinrel(root, joinrel, rel1, rel2,
291                                                                          JOIN_UNIQUE_INNER, sjinfo,
292                                                                          restrictlist);
293                                 add_paths_to_joinrel(root, joinrel, rel2, rel1,
294                                                                          JOIN_UNIQUE_OUTER, sjinfo,
295                                                                          restrictlist);
296                         }
297                         break;
298                 case JOIN_ANTI:
299                         if (is_dummy_rel(rel1) ||
300                                 restriction_is_constant_false(restrictlist, true))
301                         {
302                                 mark_dummy_rel(joinrel);
303                                 break;
304                         }
305                         if (restriction_is_constant_false(restrictlist, false) &&
306                                 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
307                                 mark_dummy_rel(rel2);
308                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
309                                                                  JOIN_ANTI, sjinfo,
310                                                                  restrictlist);
311                         break;
312                 default:
313                         /* other values not expected here */
314                         elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
315                         break;
316         }
317
318         bms_free(joinrelids);
319
320         return joinrel;
321 }