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 ; 12 13 23 24 public class AllTablesJoinRelation implements _AllTablesJoinRelation { 25 28 public _JoinRelation[] twoTableJoinRelations; 29 30 public AllTablesJoinRelation() { 31 } 32 public AllTablesJoinRelation(_JoinRelation[] joinRelation) { 33 this.twoTableJoinRelations = joinRelation; 34 } 35 40 public _JoinRelation[] getRelations() throws DException { 41 return twoTableJoinRelations; 42 } 43 52 public void andRelation(_JoinRelation[] relations) throws DException { 53 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 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 84 } 85 86 public String toString() { 87 int length = twoTableJoinRelations.length; 88 89 String 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 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 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 ex) { 143 throw ex; 144 } 145 } 146 156 public _JoinRelation[] sort(Object [][] 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 [] childObj = new Object [2]; 164 Object [] resultObj = new Object [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 E) { 194 java.math.BigDecimal b = new java.math.BigDecimal (tablePlan12.getRowCount(session) * tablePlan22.getRowCount(session)); 195 return twoTableJoinRelations; 196 } 197 } 198 199 207 private int getTablePlanPosition(TableDetails tableDetail, Object [][] 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 [] {tableDetail.getNameOfTable()}); } 218 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 246 class CostAndJoinRelation implements Comparable { 247 250 double cost; 251 254 _JoinRelation joinRelation; 255 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 272 public int compareTo(Object 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 toString() { 282 return "[Relation = " + joinRelation + " indexPossible = " + indexPossible + " Cost = " + cost + "]"; 283 } 284 } 285 } 286 | Popular Tags |