KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > plan > table > JoinRelationAbstract


1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.table;
2
3 import com.daffodilwoods.daffodildb.server.sql99.common.*;
4 import com.daffodilwoods.daffodildb.server.sql99.dql.common.*;
5 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*;
6 import com.daffodilwoods.database.resource.*;
7 import com.daffodilwoods.database.utility.P;
8
9 /**
10  * This abstract class provides the functionality of merging of join relations
11  * among different plans.
12  * <p>Title: </p>
13  * <p>Description: </p>
14  * <p>Copyright: Copyright (c) 2003</p>
15  * <p>Company: </p>
16  * @author unascribed
17  * @version 1.0
18  */

19
20 public abstract class JoinRelationAbstract implements TableExpressionConstants {
21
22    /**
23     * This method is used to replace the execution plan of tables representing
24     * by mappingPositions with passed tablePlan. It is required when we apply
25     * join condition on two plans.
26     * @param mappingPositions
27     * @param tablePlan
28     * @throws DException
29     */

30
31    protected abstract void updateMapping(int[] mappingPositions, _TablePlan tablePlan) throws DException;
32
33    /**
34     * This method is used when join condition belong to non executable plans
35     * like OrderSequencePlan or TemporaryMerge.
36     * @param tablePlan1
37     * @param joinRelation
38     * @throws DException
39     */

40
41    abstract protected void mergeJoinRelationInOrderSequencePlan(_TablePlan tablePlan1, _JoinRelation joinRelation) throws DException;
42
43    /**
44     * This method is used in merging or Join Relation with Two Plans.
45     * The following cases arises while calling getTablePlan for each join
46     * relation.
47     * Case1. If both tablePlans are Different
48     * Case1.1 SingleTablePlan,SingleTablePlan
49     * Make TwoTableJoinPlan.
50     * Case1.2 SingleTablePlan,OrderSequencePlan
51     * Make TwoTableJoinPlan.First TablePlan will be
52     * OrderSequencePlan and other will be SingleTablePlan.
53     * Case1.3 SingleTablePlan,QualifiedPlan
54     * Add SingleTablePlan into QualifiedPlan.Also check
55     * whether qualified join can be discarded.See the sequence
56     * QualifiedPlan.merge(_TablePlan,Relation,tableName).
57     * e.g if join condition is on A and B , plans are A and B
58     * Loj C then resultant plan will be (A join B on joinRelation) Loj C.
59     * Case1.4 OrderSequencePlan,QualifiedPlan
60     * Make TwoTableJoinPlan.First TablePlan will be OrderSequencePlan
61     * and other will be QualifiedPlan.
62     * Case1.5 QualifiedPlan,QualifiedPlan
63     * Add QualifiedPlan into QualifiedPlan.See the sequence
64     * QualifiedPlan.merge(_TablePlan,Relation,tableName).
65     * Case2. if both tablePlans are Same.
66     * Case2.1 OrderSequencePlan,OrderSequencePlan
67     * The relation will be passed to OrderSequencePlan.It will
68     * check condition belongs to which TablePlans.If there is
69     * any gap between matched tablePlans then a TemporaryMerge
70     * will be create from first matched to last matched.See the
71     * sequence OrderSequencePlan.mergeTwoTables(Relation).
72     * Case2.2 QualifiedPlan,QualifiedPlan.
73     * If relation belong to leftPlan and rightPlan then add
74     * this relation to the relation of QualifiedPlan.And set
75     * the type = NONE(For discarding qualifiedJoin).Also check
76     * For RightPlan it can also be QualifiedPlan.So also check
77     * whether it can discard the qualified join.See the sequence
78     * QualifiedPlan.mergeTwoTables(Relation).
79     * @param tablePlan1
80     * @param tablePlan2
81     * @param joinRelation
82     * @param tableDetails
83     * @param mappingPositions
84     * @param tablePlans
85     * @return
86     * @throws DException
87     */

88
89    protected _TablePlan[] mergingOfJoinRelation(_TablePlan tablePlan1, _TablePlan tablePlan2, _JoinRelation joinRelation, TableDetails[] tableDetails, int[] mappingPositions, _TablePlan[] tablePlans) throws DException {
90       int type1 = tablePlan1.getType();
91       int type2 = tablePlan2.getType();
92       if (!tablePlan1.equals(tablePlan2)) {
93          switch (type1) {
94             case SINGLETABLEPLAN:
95                switch (type2) {
96                   case SINGLETABLEPLAN:
97                      TwoTableJoinPlan ttJp = new TwoTableJoinPlan(tablePlan1, tablePlan2, joinRelation.getCondition());
98                      updateMapping(mappingPositions, ttJp);
99                      return updateTablePlans(ttJp, tablePlan1, tablePlan2, tablePlans);
100                   case TWOTABLEJOINPLAN:
101                   case TableExpressionConstants.NESTEDLOOPJOINPLAN:
102                      ttJp = tablePlan2.containsOrderSequencePlan() ?
103                          new TwoTableJoinPlan(tablePlan2, tablePlan1, joinRelation.getCondition()) :
104                          new TwoTableJoinPlan(tablePlan1, tablePlan2, joinRelation.getCondition());
105                      updateMapping(mappingPositions, ttJp);
106                      return updateTablePlans(ttJp, tablePlan1, tablePlan2, tablePlans);
107
108                   case ORDERSEQUENCEPLAN:
109                      TEMPORARARYMERGE:
110                          ttJp = new TwoTableJoinPlan(tablePlan2, tablePlan1, joinRelation.getCondition());
111                      updateMapping(mappingPositions, ttJp);
112                      return updateTablePlans(ttJp, tablePlan1, tablePlan2, tablePlans);
113
114                   case QUALIFIEDLEFTPLAN:
115                   case QUALIFIEDRIGHTPLAN:
116                   case QUALIFIEDFULLPLAN:
117                   case QUALIFIEDCROSSPLAN:
118                      ( (_QualifiedPlan) tablePlan2).merge(tablePlan1, joinRelation, tableDetails[1]);
119                      updateMapping(mappingPositions, tablePlan2);
120                      return updateTablePlans(tablePlan2, tablePlan1, tablePlan2, tablePlans);
121
122                }
123             case TWOTABLEJOINPLAN:
124                switch (type2) {
125                   case SINGLETABLEPLAN:
126                      TwoTableJoinPlan ttJp = new TwoTableJoinPlan(tablePlan1, tablePlan2, joinRelation.getCondition());
127                      updateMapping(mappingPositions, ttJp);
128                      return updateTablePlans(ttJp, tablePlan1, tablePlan2, tablePlans);
129
130                   case ORDERSEQUENCEPLAN:
131                      TEMPORARARYMERGE:
132                          ttJp = new TwoTableJoinPlan(tablePlan2, tablePlan1, joinRelation.getCondition());
133                      updateMapping(mappingPositions, ttJp);
134                      return updateTablePlans(ttJp, tablePlan2, tablePlan1, tablePlans);
135
136                   case TWOTABLEJOINPLAN:
137                   case TableExpressionConstants.NESTEDLOOPJOINPLAN:
138                      ttJp = tablePlan2.containsOrderSequencePlan() ?
139                          new TwoTableJoinPlan(tablePlan2, tablePlan1, joinRelation.getCondition()) :
140                          new TwoTableJoinPlan(tablePlan1, tablePlan2, joinRelation.getCondition());
141                      updateMapping(mappingPositions, ttJp);
142                      return updateTablePlans(ttJp, tablePlan1, tablePlan2, tablePlans);
143
144                   case QUALIFIEDLEFTPLAN:
145                   case QUALIFIEDRIGHTPLAN:
146                   case QUALIFIEDFULLPLAN:
147                   case QUALIFIEDCROSSPLAN:
148                      ( (_QualifiedPlan) tablePlan2).merge(tablePlan1, joinRelation, tableDetails[1]);
149                      updateMapping(mappingPositions, tablePlan2);
150                      return updateTablePlans(tablePlan2, tablePlan2, tablePlan1, tablePlans);
151
152                }
153                break;
154
155             case QUALIFIEDLEFTPLAN:
156             case QUALIFIEDRIGHTPLAN:
157             case QUALIFIEDFULLPLAN:
158             case QUALIFIEDCROSSPLAN:
159                switch (type2) {
160                   case SINGLETABLEPLAN:
161                   case TWOTABLEJOINPLAN:
162                   case TableExpressionConstants.NESTEDLOOPJOINPLAN:
163                      ( (_QualifiedPlan) tablePlan1).merge(tablePlan2, joinRelation, tableDetails[0]);
164                      updateMapping(mappingPositions, tablePlan1);
165                      return updateTablePlans(tablePlan1, tablePlan1, tablePlan2, tablePlans);
166
167                   case ORDERSEQUENCEPLAN:
168                   case TEMPORARYMERGE:
169                      TwoTableJoinPlan ttJp = new TwoTableJoinPlan(tablePlan2, tablePlan1, joinRelation.getCondition());
170                      updateMapping(mappingPositions, ttJp);
171                      return updateTablePlans(ttJp, tablePlan1, tablePlan2, tablePlans);
172
173                   case QUALIFIEDLEFTPLAN:
174                   case QUALIFIEDRIGHTPLAN:
175                   case QUALIFIEDFULLPLAN:
176                   case QUALIFIEDCROSSPLAN:
177                      ( (_QualifiedPlan) tablePlan2).merge(tablePlan1, joinRelation, tableDetails[1]);
178                      updateMapping(mappingPositions, tablePlan2);
179                      return updateTablePlans(tablePlan2, tablePlan1, tablePlan2, tablePlans);
180
181                }
182                break;
183             case ORDERSEQUENCEPLAN:
184             case TEMPORARYMERGE:
185                switch (type2) {
186                   case QUALIFIEDLEFTPLAN:
187                   case QUALIFIEDRIGHTPLAN:
188                   case QUALIFIEDFULLPLAN:
189                   case QUALIFIEDCROSSPLAN:
190                      TwoTableJoinPlan ttJp = new TwoTableJoinPlan(tablePlan1, tablePlan2, joinRelation.getCondition());
191                      updateMapping(mappingPositions, ttJp);
192                      return updateTablePlans(ttJp, tablePlan1, tablePlan2, tablePlans);
193
194                   case SINGLETABLEPLAN:
195                   case TWOTABLEJOINPLAN:
196                   case TableExpressionConstants.NESTEDLOOPJOINPLAN:
197                      ttJp = new TwoTableJoinPlan(tablePlan1, tablePlan2, joinRelation.getCondition());
198                      updateMapping(mappingPositions, ttJp);
199                      return updateTablePlans(ttJp, tablePlan1, tablePlan2, tablePlans);
200
201                }
202          }
203       } else {
204          if (type1 == ORDERSEQUENCEPLAN || type1 == TEMPORARYMERGE) {
205             mergeJoinRelationInOrderSequencePlan(tablePlan1, joinRelation);
206             return tablePlans;
207          } else {
208             tablePlan1.merge(joinRelation);
209             return tablePlans;
210          }
211       }
212       throw new DException("DSE3537", new Object JavaDoc[] {joinRelation.toString()});
213    }
214
215    /**
216     * Replaces the tablePlan1 and tablePlan2 with the tablePlan
217     * The method comes into the picture when we merges two plans into a single Plan
218     * @param tablePlan tablePlan created from tablePlan1 and tablePlan2
219     * @param tablePlan1 tablePlan1 will be replaced with tablePlan
220     * @param tablePlan2 tablePlan2 will be replaced with tablePlan
221     * @throws DException
222     */

223    protected _TablePlan[] updateTablePlans(_TablePlan tablePlan, _TablePlan tablePlan1, _TablePlan tablePlan2, _TablePlan[] tablePlans) throws DException {
224       int index1 = searchInTablePlans(tablePlan1, tablePlans);
225       int index2 = searchInTablePlans(tablePlan2, tablePlans);
226       if (index1 > index2) {
227          int temp = index1;
228          index1 = index2;
229          index2 = temp;
230       }
231       _TablePlan newTablePlans[] = new _TablePlan[tablePlans.length - 1];
232       System.arraycopy(tablePlans, 0, newTablePlans, 0, index1);
233       newTablePlans[index1] = tablePlan;
234       System.arraycopy(tablePlans, index1 + 1, newTablePlans, index1 + 1, index2 - index1 - 1);
235       System.arraycopy(tablePlans, index2 + 1, newTablePlans, index2, tablePlans.length - index2 - 1);
236       return newTablePlans;
237    }
238
239    /**
240     * Searches the tablePlan from tablePlan List.
241     * @param tPlan tPlan to be searched
242     * @return
243     * @throws DException
244     */

245    private int searchInTablePlans(_TablePlan tPlan, _TablePlan[] tablePlans) throws DException {
246       for (int i = 0; i < tablePlans.length; i++) {
247          if (tablePlans[i] == tPlan) {
248             return i;
249          }
250       }
251       return -1;
252    }
253 }
254
Popular Tags