1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.table; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.client.*; 6 import com.daffodilwoods.daffodildb.server.serversystem.*; 7 import com.daffodilwoods.daffodildb.server.sql99.common.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.common.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*; 12 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*; 13 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.*; 14 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 15 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.predicates.*; 16 import com.daffodilwoods.database.resource.*; 17 import com.daffodilwoods.database.sqlinitiator.*; 18 import com.daffodilwoods.database.utility.P; 19 20 37 38 public class ViewPlan extends PlanAbstract implements TableExpressionConstants, _SingleTablePlan, ExecutionPlanConstants { 39 40 43 44 private _TablePlan plan; 45 46 49 50 private TableDetails tableDetail; 51 52 56 57 private Object [][] virtualAndActualColumns; 59 63 64 private _Order singleTableOrder; 65 66 71 72 private HashMap wayOfExecution; 73 74 public ViewPlan(queryexpression queryExpression0, TableDetails tableDetailsPassed, Object [][] virtualAndActualColumns0, _TablePlan plan0) { 75 tableDetail = tableDetailsPassed; 76 tableDetail.setTableType(TypeConstants.VIEW); 77 virtualAndActualColumns = virtualAndActualColumns0; 78 plan = plan0; 79 childPlans = new _TablePlan[] {plan0}; 80 wayOfExecution = new HashMap(); 81 82 } 83 84 89 90 public int getType() throws DException { 91 return TableExpressionConstants.SINGLETABLEPLAN; 92 } 93 94 104 105 public _Iterator execute(_ServerSession serverSession) throws DException { 106 _Iterator iter = ( (_TablePlanAdditional) plan).executePlan(serverSession); 107 iter = new ViewIterator(iter, virtualAndActualColumns, tableDetail); 108 return checkForOrderAndCondition(iter, serverSession, remainingCondition); 109 } 110 111 129 130 public _Iterator execute(_ServerSession session, booleanvalueexpression condition) throws DException { 131 booleanvalueexpression b = (booleanvalueexpression) wayOfExecution.get(condition); 132 _Iterator iter = null; 133 if (b == null) { map(condition); 135 iter = ( (_TablePlanAdditional) plan).executePlan(session, condition); 136 } else { 137 iter = ( (_TablePlanAdditional) plan).executePlan(session); 138 } 139 iter = new ViewIterator(iter, virtualAndActualColumns, tableDetail); 140 return checkForOrderAndCondition(iter, session, b); 141 } 142 143 160 161 public double getCost(_ServerSession session, booleanvalueexpression condition, long estimatedRows) throws 162 DException { 163 Object [][] mapping = map(condition); 164 double cost = 0.0; 165 166 168 if (condition.canUseForSeek()== TypeConstants.BOTHSIDESEEKABLE) { 169 cost = plan.getCost(session, condition, estimatedRows); 170 cost = addCostForCondition(cost, session, remainingCondition, 1); 171 } else { 172 booleanvalueexpression newBve = BVEPlanMerger.addAndConditions(condition, remainingCondition); 173 wayOfExecution.put(condition, newBve); 174 cost = plan.getCost(session); 175 cost = addCostForCondition(cost, session, newBve, estimatedRows); 176 } 177 reMap(mapping); 178 return cost; 179 } 180 181 189 190 public double getCost(_ServerSession session) throws DException { 191 double cost = plan.getCost(session); 192 return addCostForCondition(cost, session, remainingCondition, 1); 193 } 194 195 200 201 public _SingleTablePlan[] getSingleTablePlans() throws DException { 202 return new _SingleTablePlan[] {this}; 203 } 204 205 213 214 public _TablePlan joinMissingLink() throws DException { 215 return this; 216 } 217 218 224 225 public _Order getOrder() throws DException { 226 return GeneralPurposeStaticClass.getJoinOrdered(singleTableOrder, plan.getOrder()); 227 } 228 229 238 239 protected double addCostForCondition(double cost, _ServerSession serverSession, booleanvalueexpression condition, long rows) throws DException { 240 return condition == null 241 ? cost 242 : cost + condition.getCost(plan.getRowCount(serverSession) * rows, false); 243 } 244 245 252 253 private Object [][] map(booleanvalueexpression condition) throws DException { 254 ColumnDetails[] cd = condition.getColumnDetails(); 255 ArrayList list = new ArrayList(5); 256 try { 257 for (int i = 0; i < cd.length; i++) { 258 for (int j = 0; j < virtualAndActualColumns.length; j++) { 259 ColumnDetails virtualColumn = (ColumnDetails) virtualAndActualColumns[j][0]; 260 if (cd[i].getTable() == virtualColumn.getTable() && cd[i].getColumn().equalsIgnoreCase(virtualColumn.getColumn())) { 261 list.add(new Object [] {cd[i], cd[i].clone()}); 262 GeneralPurposeStaticClass.updateColumnDetailsProperties(cd[i], (ColumnDetails) virtualAndActualColumns[j][1]); 263 break; 264 } 265 } 266 } 267 } catch (CloneNotSupportedException ex) { 268 throw new DException("DSE0", new Object [] {"Clone Not Supported"}); 269 } 270 return (Object [][]) list.toArray(new Object [0][]); 271 } 272 273 279 280 private void reMap(Object [][] mapping) throws DException { 281 for (int i = 0; i < mapping.length; i++) { 282 GeneralPurposeStaticClass.updateColumnDetailsProperties( (ColumnDetails) mapping[i][0], (ColumnDetails) mapping[i][1]); 283 } 284 } 285 286 public String toString() { 287 if (plan == null) { 288 return "VIEWPLAN[NULL]"; 289 } 290 return "VIEWPLAN[" + plan + "]"; 291 } 292 293 298 299 public void setCondition(booleanvalueexpression condition) throws DException { 300 remainingCondition = condition; 301 } 302 303 308 309 public TableDetails[] getTableDetails() throws DException { 310 return new TableDetails[] {tableDetail}; 311 } 312 313 319 320 public TableDetails[] getViewTableDetails() throws DException { 321 return new TableDetails[] {tableDetail}; 322 } 323 324 public String getVerifier() throws DException { 325 if (plan == null) { 326 return "VIEWPLAN[NULL]"; 327 } 328 StringBuffer buffer = new StringBuffer (); 329 inc(); 330 buffer.append(tabW("[ VIEWPLAN ]")); 331 buffer.append("\n" + plan.getVerifier()); 332 buffer.append(tab("[/ VIEWPLAN ]")); 333 inc(); 334 buffer.append(tab("[RemainingCondition =\"" + remainingCondition + "\"]")); 335 buffer.append(tab("[Order =\"" + singleTableOrder + "\"]")); 336 dec(); 337 return buffer.toString(); 338 } 339 340 346 347 public boolean ifExists(TableDetails tableDetails0) throws DException { 348 return tableDetails0 == tableDetail; 349 } 350 351 359 360 private _Iterator checkForOrderAndCondition(_Iterator iterator, _ServerSession serverSession, booleanvalueexpression condition) throws DException { 361 iterator = checkForCondition(iterator, serverSession, condition); 362 if (singleTableOrder != null) { 363 iterator = GeneralPurposeStaticClass.getTemporaryIndexIterator(iterator, this, serverSession, singleTableOrder); 364 } 365 return iterator; 366 } 367 368 375 private _Iterator checkForCondition(_Iterator iterator, _ServerSession session, booleanvalueexpression condition) throws DException { 376 if (condition != null) { 377 return getNonIndexedFilteredIteratorWithSubQuery(iterator, session, condition); 378 } 379 return iterator; 380 } 381 382 387 388 public _TablePlan[] getChildTablePlans() throws DException { 389 childPlans = plan.getChildTablePlans(); 390 initializeTableDetails(); 391 return new _TablePlan[] {this}; 392 } 393 394 399 400 public _QueryPlan getQueryPlan() throws DException { 401 _QueryPlan[] cplans = new _QueryPlan[]{plan.getQueryPlan()}; 402 String cond = null; 403 String ord = singleTableOrder == null ? null : ("" + singleTableOrder); 404 return new QueryPlan("ViewPlan " + tableDetail.getQualifiedTableName(), 405 cplans, cond, ord); 406 } 407 408 413 414 public void setOrder(_Order order) throws DException { 415 this.singleTableOrder = order; 416 } 417 418 423 424 public double getCostWithoutOrder(_ServerSession session) throws DException { 425 double cost = plan.getCostWithoutOrder(session); 426 return addCostForCondition(cost, session, remainingCondition, 1); 427 } 428 429 public double getCostWithoutOrder(_ServerSession session, booleanvalueexpression condition, long estimatedRowCount) throws DException { 430 Object [][] mapping = map(condition); 431 double cost = 0.0; 432 433 434 435 if (condition.canUseForSeek() == TypeConstants.BOTHSIDESEEKABLE) { 436 cost = plan.getCostWithoutOrder(session, condition, estimatedRowCount); 437 cost = addCostForCondition(cost, session, remainingCondition, 1); 438 } else { 439 booleanvalueexpression newBve = BVEPlanMerger.addAndConditions(condition, remainingCondition); 440 wayOfExecution.put(condition, newBve); 441 cost = plan.getCostWithoutOrder(session); 442 cost = addCostForCondition(cost, session, newBve, estimatedRowCount); 443 } 444 reMap(mapping); 445 return cost; 446 } 447 448 453 454 public _Iterator executeWithoutOrder(_ServerSession serverSession) throws DException { 455 _Iterator iter = ( (_TablePlanAdditional) plan).executeWithoutOrderPlan(serverSession); 456 iter = new ViewIterator(iter, virtualAndActualColumns, tableDetail); 457 if (remainingCondition == null) { 458 return iter; 459 } 460 return getNonIndexedFilteredIterator(iter, serverSession, remainingCondition); 461 } 462 463 public _Iterator executeWithOutOrder(_ServerSession session, booleanvalueexpression condition) throws DException { 464 booleanvalueexpression cond = (booleanvalueexpression) wayOfExecution.get(condition); 465 _Iterator iter = null; 466 if (cond == null) { 467 map(condition); 468 iter = ( (_TablePlanAdditional) plan).executeWithOutOrderPlan(session, condition); 469 } else { 470 iter = ( (_TablePlanAdditional) plan).executeWithoutOrderPlan(session); 471 } 472 iter = new ViewIterator(iter, virtualAndActualColumns, tableDetail); 473 if (cond == null) { 474 return iter; 475 } 476 return getNonIndexedFilteredIterator(iter, session, cond); 477 } 478 479 } 480 | Popular Tags |