OSDN Git Service

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