OSDN Git Service

1e5a015ac6550b91ea6f8bd8d86c22b4389eeeb4
[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  * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *-------------------------------------------------------------------------
10  */
11
12 /*
13  * make_join_rel
14  *         Find or create a join RelOptInfo that represents the join of
15  *         the two given rels, and add to it path information for paths
16  *         created with the two rels as outer and inner rel.
17  *         (The join rel may already contain paths generated from other
18  *         pairs of rels that add up to the same set of base rels.)
19  *
20  * NB: will return NULL if attempted join is not valid.  This can happen
21  * when working with outer joins, or with IN or EXISTS clauses that have been
22  * turned into joins.
23  */
24 RelOptInfo *
25 make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
26 {
27         Relids          joinrelids;
28         SpecialJoinInfo *sjinfo;
29         bool            reversed;
30         SpecialJoinInfo sjinfo_data;
31         RelOptInfo *joinrel;
32         List       *restrictlist;
33
34         /* We should never try to join two overlapping sets of rels. */
35         Assert(!bms_overlap(rel1->relids, rel2->relids));
36
37         /* Construct Relids set that identifies the joinrel. */
38         joinrelids = bms_union(rel1->relids, rel2->relids);
39
40         /* Check validity and determine join type. */
41         if (!join_is_legal(root, rel1, rel2, joinrelids,
42                                            &sjinfo, &reversed))
43         {
44                 /* invalid join path */
45                 bms_free(joinrelids);
46                 return NULL;
47         }
48
49         /* Swap rels if needed to match the join info. */
50         if (reversed)
51         {
52                 RelOptInfo *trel = rel1;
53
54                 rel1 = rel2;
55                 rel2 = trel;
56         }
57
58         /*
59          * If it's a plain inner join, then we won't have found anything in
60          * join_info_list.      Make up a SpecialJoinInfo so that selectivity
61          * estimation functions will know what's being joined.
62          */
63         if (sjinfo == NULL)
64         {
65                 sjinfo = &sjinfo_data;
66                 sjinfo->type = T_SpecialJoinInfo;
67                 sjinfo->min_lefthand = rel1->relids;
68                 sjinfo->min_righthand = rel2->relids;
69                 sjinfo->syn_lefthand = rel1->relids;
70                 sjinfo->syn_righthand = rel2->relids;
71                 sjinfo->jointype = JOIN_INNER;
72                 /* we don't bother trying to make the remaining fields valid */
73                 sjinfo->lhs_strict = false;
74                 sjinfo->delay_upper_joins = false;
75                 sjinfo->join_quals = NIL;
76         }
77
78         /*
79          * Find or build the join RelOptInfo, and compute the restrictlist that
80          * goes with this particular joining.
81          */
82         joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
83                                                          &restrictlist);
84
85         /*
86          * If we've already proven this join is empty, we needn't consider any
87          * more paths for it.
88          */
89         if (is_dummy_rel(joinrel))
90         {
91                 bms_free(joinrelids);
92                 return joinrel;
93         }
94
95         /*
96          * Consider paths using each rel as both outer and inner.  Depending on
97          * the join type, a provably empty outer or inner rel might mean the join
98          * is provably empty too; in which case throw away any previously computed
99          * paths and mark the join as dummy.  (We do it this way since it's
100          * conceivable that dummy-ness of a multi-element join might only be
101          * noticeable for certain construction paths.)
102          *
103          * Also, a provably constant-false join restriction typically means that
104          * we can skip evaluating one or both sides of the join.  We do this by
105          * marking the appropriate rel as dummy.  For outer joins, a
106          * constant-false restriction that is pushed down still means the whole
107          * join is dummy, while a non-pushed-down one means that no inner rows
108          * will join so we can treat the inner rel as dummy.
109          *
110          * We need only consider the jointypes that appear in join_info_list, plus
111          * JOIN_INNER.
112          */
113         switch (sjinfo->jointype)
114         {
115                 case JOIN_INNER:
116                         if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
117                                 restriction_is_constant_false(restrictlist, false))
118                         {
119                                 mark_dummy_rel(joinrel);
120                                 break;
121                         }
122                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
123                                                                  JOIN_INNER, sjinfo,
124                                                                  restrictlist);
125                         add_paths_to_joinrel(root, joinrel, rel2, rel1,
126                                                                  JOIN_INNER, sjinfo,
127                                                                  restrictlist);
128                         break;
129                 case JOIN_LEFT:
130                         if (is_dummy_rel(rel1) ||
131                                 restriction_is_constant_false(restrictlist, true))
132                         {
133                                 mark_dummy_rel(joinrel);
134                                 break;
135                         }
136                         if (restriction_is_constant_false(restrictlist, false) &&
137                                 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
138                                 mark_dummy_rel(rel2);
139                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
140                                                                  JOIN_LEFT, sjinfo,
141                                                                  restrictlist);
142                         add_paths_to_joinrel(root, joinrel, rel2, rel1,
143                                                                  JOIN_RIGHT, sjinfo,
144                                                                  restrictlist);
145                         break;
146                 case JOIN_FULL:
147                         if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
148                                 restriction_is_constant_false(restrictlist, true))
149                         {
150                                 mark_dummy_rel(joinrel);
151                                 break;
152                         }
153                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
154                                                                  JOIN_FULL, sjinfo,
155                                                                  restrictlist);
156                         add_paths_to_joinrel(root, joinrel, rel2, rel1,
157                                                                  JOIN_FULL, sjinfo,
158                                                                  restrictlist);
159
160                         /*
161                          * If there are join quals that aren't mergeable or hashable, we
162                          * may not be able to build any valid plan.  Complain here so that
163                          * we can give a somewhat-useful error message.  (Since we have no
164                          * flexibility of planning for a full join, there's no chance of
165                          * succeeding later with another pair of input rels.)
166                          */
167                         if (joinrel->pathlist == NIL)
168                                 ereport(ERROR,
169                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
170                                                  errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
171                         break;
172                 case JOIN_SEMI:
173
174                         /*
175                          * We might have a normal semijoin, or a case where we don't have
176                          * enough rels to do the semijoin but can unique-ify the RHS and
177                          * then do an innerjoin (see comments in join_is_legal).  In the
178                          * latter case we can't apply JOIN_SEMI joining.
179                          */
180                         if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
181                                 bms_is_subset(sjinfo->min_righthand, rel2->relids))
182                         {
183                                 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
184                                         restriction_is_constant_false(restrictlist, false))
185                                 {
186                                         mark_dummy_rel(joinrel);
187                                         break;
188                                 }
189                                 add_paths_to_joinrel(root, joinrel, rel1, rel2,
190                                                                          JOIN_SEMI, sjinfo,
191                                                                          restrictlist);
192                         }
193
194                         /*
195                          * If we know how to unique-ify the RHS and one input rel is
196                          * exactly the RHS (not a superset) we can consider unique-ifying
197                          * it and then doing a regular join.  (The create_unique_path
198                          * check here is probably redundant with what join_is_legal did,
199                          * but if so the check is cheap because it's cached.  So test
200                          * anyway to be sure.)
201                          */
202                         if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
203                                 create_unique_path(root, rel2, rel2->cheapest_total_path,
204                                                                    sjinfo) != NULL)
205                         {
206                                 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
207                                         restriction_is_constant_false(restrictlist, false))
208                                 {
209                                         mark_dummy_rel(joinrel);
210                                         break;
211                                 }
212                                 add_paths_to_joinrel(root, joinrel, rel1, rel2,
213                                                                          JOIN_UNIQUE_INNER, sjinfo,
214                                                                          restrictlist);
215                                 add_paths_to_joinrel(root, joinrel, rel2, rel1,
216                                                                          JOIN_UNIQUE_OUTER, sjinfo,
217                                                                          restrictlist);
218                         }
219                         break;
220                 case JOIN_ANTI:
221                         if (is_dummy_rel(rel1) ||
222                                 restriction_is_constant_false(restrictlist, true))
223                         {
224                                 mark_dummy_rel(joinrel);
225                                 break;
226                         }
227                         if (restriction_is_constant_false(restrictlist, false) &&
228                                 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
229                                 mark_dummy_rel(rel2);
230                         add_paths_to_joinrel(root, joinrel, rel1, rel2,
231                                                                  JOIN_ANTI, sjinfo,
232                                                                  restrictlist);
233                         break;
234                 default:
235                         /* other values not expected here */
236                         elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
237                         break;
238         }
239
240         bms_free(joinrelids);
241
242         return joinrel;
243 }