KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > plan > condition > AllTablesJoinRelation


1  package com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition;
2
3 import java.util.*;
4 import com.daffodilwoods.daffodildb.server.serversystem.*;
5 import com.daffodilwoods.daffodildb.server.sql99.common.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*;
8 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
9 import com.daffodilwoods.database.resource.*;
10 import com.daffodilwoods.database.utility.*;
11 import java.util.HashMap JavaDoc;
12
13 /**
14  * This class represents all the conditions of inner join present in select query.
15  * It is required in creation of plan for execution of select query.
16  * <p>Title: </p>
17  * <p>Description: </p>
18  * <p>Copyright: Copyright (c) 2003</p>
19  * <p>Company: </p>
20  * @author unascribed
21  * @version 1.0
22  */

23
24 public class AllTablesJoinRelation implements _AllTablesJoinRelation {
25   /**
26    * It represents join conditions of inner join presents in selectquery.
27    */

28   public _JoinRelation[] twoTableJoinRelations;
29
30    public AllTablesJoinRelation() {
31    }
32    public AllTablesJoinRelation(_JoinRelation[] joinRelation) {
33       this.twoTableJoinRelations = joinRelation;
34    }
35    /**
36     * This method is required to get all Join relation presents in query.
37     * @return
38     * @throws DException
39     */

40    public _JoinRelation[] getRelations() throws DException {
41       return twoTableJoinRelations;
42    }
43    /**
44     * This method is required to merge two list of join condition with AND logical
45     * operator.if join condition belong to same table then we merge join condition
46     * with AND logical operator and resultant merged condition is added to resultant
47     * list of join condition other wise join condition is added as such to resultant
48     * list.
49     * @param relations
50     * @throws DException
51     */

52    public void andRelation(_JoinRelation[] relations) throws DException {
53       /*dst*/
54       ArrayList aList = new ArrayList(5);
55       int length1 = relations.length;
56       int length2 = twoTableJoinRelations.length;
57       for (int i = 0; i < length1; i++) {
58          boolean found = false;
59          TableDetails[] tables1 = GeneralPurposeStaticClass.getAllTableDetails(relations[i].getCondition());
60          booleanvalueexpression bveCondition1 = relations[i].getCondition();
61          for (int j = 0; j < length2; j++) {
62            TableDetails[] tables2 = GeneralPurposeStaticClass.getAllTableDetails(twoTableJoinRelations[j].getCondition());
63             try {
64                if (compareTables(tables1, tables2)) {
65                   twoTableJoinRelations[j].setCondition(BVEPlanMerger.addAndConditions(bveCondition1, twoTableJoinRelations[j].getCondition()));
66                   found = true;
67                   break;
68                }
69             } catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
70                throw ex;
71             }
72          }
73          if (!found) {
74             aList.add(relations[i]);
75          }
76       }
77       if (aList.size() != 0) {
78          _JoinRelation[] jr = new _JoinRelation[twoTableJoinRelations.length + aList.size()];
79          System.arraycopy(twoTableJoinRelations, 0, jr, 0, twoTableJoinRelations.length);
80          System.arraycopy( (_JoinRelation[]) aList.toArray(new _JoinRelation[0]), 0, jr, twoTableJoinRelations.length, aList.size());
81          this.twoTableJoinRelations = jr;
82       }
83       /*dend*/
84    }
85
86    public String JavaDoc toString() {
87       int length = twoTableJoinRelations.length;
88       /*Done by vibha acc to findbug*/
89       String JavaDoc strBuffer="";
90       strBuffer = "[" + twoTableJoinRelations[0].toString() + "]";
91       for (int i = 1; i < length; i++) {
92          strBuffer += "[" + twoTableJoinRelations[i].toString() + "]";
93       }
94       return strBuffer;
95    }
96    /**
97     * This method is used to merged passed joinrelation with given joinrelation
98     * joinrelation are merged on basis of their tabledetails.when table details
99     * of two join relation are equals they are merged wirh Or logical operator.
100     * and merged codition will be set as twotablejoinrelation and this will return
101     * true, otherwise it will return false shows thatcondition set as remaining
102     * condition.
103     * @param relations
104     * @return
105     * @throws DException
106     */

107    public boolean orRelation(_JoinRelation[] relations) throws DException {
108       ArrayList aList = new ArrayList();
109       int length1 = relations.length;
110       int length2 = twoTableJoinRelations.length;
111       for (int i = 0; i < length1; i++) {
112          TableDetails[] tables1 = relations[i].getTableDetails();
113          for (int j = 0; j < length2; j++) {
114             TableDetails[] tables2 = twoTableJoinRelations[j].getTableDetails();
115             if (compareTables(tables1, tables2)) {
116                booleanvalueexpression bveCondition1 = relations[i].getCondition();
117                twoTableJoinRelations[j].setCondition(BVEPlanMerger.addOrConditions(twoTableJoinRelations[j].getCondition(), bveCondition1));
118                aList.add(twoTableJoinRelations[j]);
119             }
120          }
121       }
122       if (aList.size() != 0) {
123          this.twoTableJoinRelations = (_JoinRelation[]) aList.toArray(new _JoinRelation[0]);
124          return true;
125       }
126       return false;
127    }
128    /**
129     * This method is required to check whether two table details passed are equal
130     * or not.when table details are equal it return true else return false.
131     * @param tables1
132     * @param tables2
133     * @return
134     * @throws DException
135     */

136    private boolean compareTables(TableDetails[] tables1, TableDetails[] tables2) throws DException {
137       try {
138          return ( (tables1[0] == tables2[0]) && (tables1[1] == tables2[1]) ||
139                  (tables1[0] == tables2[1]) && (tables1[1] == tables2[0]))
140              ? true :
141              false;
142       } catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
143          throw ex;
144       }
145    }
146    /**
147     * This method is required to sort joinrelation.joinrealtion are sorted on basis
148     * of their cost of solving that joinrelation.join relation which are solved
149     * by index have less cost that those which are not solved by index.cost also
150     * depends on number of rows present in table.
151     * @param tableAndQualifiedPlan
152     * @param session
153     * @return
154     * @throws DException
155     */

156    public _JoinRelation[] sort(Object JavaDoc[][] tableAndQualifiedPlan, _ServerSession session) throws DException {
157       if (session == null) {
158          Thread.dumpStack();
159       }
160       _TablePlan tablePlan12 = null;
161       _TablePlan tablePlan22 = null;
162       try {
163          Object JavaDoc[] childObj = new Object JavaDoc[2];
164          Object JavaDoc[] resultObj = new Object JavaDoc[twoTableJoinRelations.length];
165          int length = twoTableJoinRelations.length;
166          CostAndJoinRelation cjRelation[] = new CostAndJoinRelation[twoTableJoinRelations.length];
167          for (int i = 0; i < length; i++) {
168             TableDetails[] tableNames = twoTableJoinRelations[i].getTableDetails();
169             if (tableNames.length != 2 || tableNames[0] == null || tableNames[1] == null) {
170                cjRelation[i] = new CostAndJoinRelation(twoTableJoinRelations[i], Double.MAX_VALUE, false);
171                continue;
172             }
173             try {
174                int position1 = getTablePlanPosition(tableNames[0], tableAndQualifiedPlan);
175                int position2 = getTablePlanPosition(tableNames[1], tableAndQualifiedPlan);
176                tablePlan12 = (_TablePlan) tableAndQualifiedPlan[position1][2];
177                tablePlan22 = (_TablePlan) tableAndQualifiedPlan[position2][2];
178                long rows1 = ( (_TablePlan) tableAndQualifiedPlan[position1][1]).getRowCount(session);
179                long rows2 = ( (_TablePlan) tableAndQualifiedPlan[position2][1]).getRowCount(session);
180                double maxRows = Math.max(rows1, rows2);
181                double minRows = Math.min(rows1, rows2);
182                boolean b = twoTableJoinRelations[i].isIndexPossible(session);
183                cjRelation[i] = new CostAndJoinRelation(twoTableJoinRelations[i], maxRows / minRows, b);
184             } catch (DException ex) {
185                throw ex;
186             }
187          }
188          Arrays.sort(cjRelation);
189          for (int i = 0; i < resultObj.length; i++) {
190             twoTableJoinRelations[i] = cjRelation[i].joinRelation;
191          }
192          return twoTableJoinRelations;
193       } catch (java.lang.ArithmeticException JavaDoc E) {
194          java.math.BigDecimal JavaDoc b = new java.math.BigDecimal JavaDoc(tablePlan12.getRowCount(session) * tablePlan22.getRowCount(session));
195          return twoTableJoinRelations;
196       }
197    }
198
199    /**
200     * This method is used to get position of passed tabledetails in passed
201     * tableandqualifiedplan array
202     * @param tableDetail
203     * @param tableAndQualifiedPlan
204     * @return
205     * @throws DException
206     */

207    private int getTablePlanPosition(TableDetails tableDetail, Object JavaDoc[][] tableAndQualifiedPlan) throws DException {
208       int length = tableAndQualifiedPlan.length;
209
210       for (int i = 0; i < length; i++) {
211          if (ifExists( ( (_TablePlan) tableAndQualifiedPlan[i][2]), tableDetail)) {
212             return i;
213          }
214       }
215       P.show2DArray(tableAndQualifiedPlan);
216       throw new DException("DSE3516", new Object JavaDoc[] {tableDetail.getNameOfTable()}); // InVaild Operation
217
}
218    /**
219     * This method is used to check whether passed tabledetails match with tabledetaiils
220     * of passed tableplan or not.
221     * @param tablePlan
222     * @param tableDetail
223     * @return
224     * @throws DException
225     */

226    private boolean ifExists(_TablePlan tablePlan, TableDetails tableDetail) throws DException {
227       TableDetails[] tds = tablePlan.getTableDetails();
228       for (int i = 0; i < tds.length; i++) {
229          if (tds[i] == tableDetail) {
230             return true;
231          }
232       }
233       return false;
234    }
235    /**
236     * This class provide functionality for comparing joinrelation.joinrelation are
237     * compared on basis of their cost and whether solved by index or not.
238     * utilization of this are in sorting of join relation.
239     * <p>Title: </p>
240     * <p>Description: </p>
241     * <p>Copyright: Copyright (c) 2004</p>
242     * <p>Company: </p>
243     * @author not attributable
244     * @version 1.0
245     */

246    class CostAndJoinRelation implements Comparable JavaDoc {
247      /**
248       * It represents cost
249       */

250      double cost;
251      /**
252       * It represnts join realtion
253       */

254      _JoinRelation joinRelation;
255      /**
256       * It represents whether join realtion are solved by index or not.
257       */

258      boolean indexPossible;
259       public CostAndJoinRelation(_JoinRelation joinRelation, double cost, boolean indexPossible) {
260          this.cost = cost;
261          this.joinRelation = joinRelation;
262          this.indexPossible = indexPossible;
263       }
264       /**
265        * This method is used to compare joinrelation ,those joinrealtion which
266        * are solved by index are smaller than those which are not solved by index.
267        * Another parameter for comparing joinrelation is cost,which depends on number
268        * of rows.
269        * @param o
270        * @return
271        */

272       public int compareTo(Object JavaDoc o) {
273          boolean indexPossibleInOtherCase = ( (CostAndJoinRelation) o).indexPossible;
274          if (! (indexPossible ^ indexPossibleInOtherCase)) {
275             double c = ( (CostAndJoinRelation) o).cost;
276             return cost - c == 0 ? 0 : cost - c > 0 ? -1 : 1;
277          }
278          return indexPossibleInOtherCase ? 1 : -1;
279       }
280
281       public String JavaDoc toString() {
282          return "[Relation = " + joinRelation + " indexPossible = " + indexPossible + " Cost = " + cost + "]";
283       }
284    }
285 }
286
Popular Tags