1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.table; 2 3 import com.daffodilwoods.daffodildb.client.*; 4 import com.daffodilwoods.daffodildb.server.serversystem.*; 5 import com.daffodilwoods.daffodildb.server.sql99.common.*; 6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 7 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.order.*; 10 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 11 import com.daffodilwoods.database.resource.*; 12 import com.daffodilwoods.database.sqlinitiator.*; 13 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference; 14 import com.daffodilwoods.database.utility.P; 15 16 31 32 public class TableExpressionPlan extends PlanExceptionAbstract { 33 34 37 38 private _TablePlan finalTablePlan; 39 40 44 45 private _Order order; 46 47 50 51 private _BVEPlan bveResultantPlan; 52 53 54 private _Reference[] whereClauseUnderlyingReferences; 55 56 private _Reference[] havingClauseUnderlyingReferences; 57 58 68 69 public TableExpressionPlan(_TablePlan finalTablePlan0, _BVEPlan bveResultantPlan0, _OrderPlan orderPlan0,_Reference[] whereClauseUnderlyingReferences0,_Reference[] havingClauseUnderlyingReferences0) throws DException { 70 bveResultantPlan = bveResultantPlan0; 71 finalTablePlan = finalTablePlan0; 72 if (orderPlan0 != null) { 73 order = orderPlan0.getJoinLevelOrder(); 74 } 75 whereClauseUnderlyingReferences=whereClauseUnderlyingReferences0; 76 havingClauseUnderlyingReferences=havingClauseUnderlyingReferences0; 77 } 78 79 86 87 95 96 public double getCost(_ServerSession session) throws DException { 97 return addCostForJoinOrder(addCostForRemainingCondition(finalTablePlan.getCost(session), session), session); 98 } 99 100 111 112 public double getCost(_ServerSession session, booleanvalueexpression condition, long estimatedRows) throws DException { 113 return addCostForJoinOrder(addCostForRemainingCondition(finalTablePlan.getCost(session, condition, estimatedRows), estimatedRows), session); 114 } 115 116 122 123 public long getRowCount(_ServerSession serverSession) throws DException { 124 return finalTablePlan.getRowCount(serverSession); 125 } 126 127 136 137 public _Iterator execute(_ServerSession session) throws DException { 138 _Iterator iterator = finalTablePlan.execute(session); 139 return makeIteratorForOrderAndRemainingCondition(session, iterator); 140 } 141 142 143 152 153 public _Iterator execute(_ServerSession session, booleanvalueexpression condition) throws DException { 154 _Iterator iterator = finalTablePlan.execute(session, condition); 155 return makeIteratorForOrderAndRemainingCondition(session, iterator); 156 } 157 158 public String getVerifier() throws DException { 159 StringBuffer buffer = new StringBuffer (); 160 inc(); 161 buffer.append(tabW("[ TABLE_EXPRESSION_PLAN ]")); 162 buffer.append("\n" + finalTablePlan.getVerifier()); 163 inc(); 164 buffer.append(tab("[RemainingCondition =\"" + bveResultantPlan.getRemainingCondition() + "\"]")); 165 buffer.append(tab("[Order =\"" + order + "\"]")); 166 dec(); 167 buffer.append(tab("[/ TABLE_EXPRESSION_PLAN ]")); 168 dec(); 169 return buffer.toString(); 170 } 171 172 177 178 public _QueryPlan getQueryPlan() throws DException { 179 int length = 0; _QueryPlan[] cplans = new _QueryPlan[1]; 181 cplans[0] = finalTablePlan.getQueryPlan(); 182 String cond = bveResultantPlan.getRemainingCondition() == null ? null : "" + bveResultantPlan.getRemainingCondition(); 183 String cond1 = bveResultantPlan.getAggregateCondition() == null ? null : "" + bveResultantPlan.getAggregateCondition(); 184 String ord = order == null ? null : "" + order; 185 cond = cond == null ? cond1 : cond1 == null ? cond : cond + " <::> " + cond1; 186 return new QueryPlan("TableExpression " + this, cplans, cond, ord); 187 } 188 189 197 198 private _Iterator executeWithBVE(_ServerSession session, _Iterator iterator) throws DException { 199 200 if( bveResultantPlan.getRemainingCondition() != null ) 201 iterator = isRemainingConditionNotNull()==false? iterator : getNonIndexedFilteredIteratorWithSubQuery(iterator, session, bveResultantPlan.getRemainingCondition()); 202 if( whereClauseUnderlyingReferences != null ) 203 iterator.setSpecificUnderlyingReferences(whereClauseUnderlyingReferences); 204 205 if(bveResultantPlan.getRemainingCondition()!=null){ 206 _Reference[] ref = GeneralPurposeStaticClass.getUnderLyingReferencesOnly(GeneralPurposeStaticClass.getAllReferences(bveResultantPlan.getRemainingCondition().getReferences(finalTablePlan.getTableDetails())), finalTablePlan.getTableDetails()); 207 if(ref!=null) 208 iterator.setSpecificUnderlyingReferences(ref); 209 } 210 return iterator; 211 } 212 213 219 220 private boolean isRemainingConditionNotNull() throws DException { 221 return bveResultantPlan != null && bveResultantPlan.getRemainingCondition() != null; 222 } 223 224 232 233 private double addCostForRemainingCondition(double cost, _ServerSession serverSession) throws DException { 234 if (isRemainingConditionNotNull()) { 235 return addCostForRemainingCondition(cost, getRowCount(serverSession)); 236 } 237 return cost; 238 } 239 240 248 249 private double addCostForRemainingCondition(double cost, long rows) throws DException { 250 return isRemainingConditionNotNull() 251 ? bveResultantPlan.getRemainingCondition().getCost(rows, false) + cost 252 : cost; 253 } 254 255 262 263 private double addCostForJoinOrder(double cost, _ServerSession serverSession) throws DException { 264 return order == null ? cost 265 : cost + getCostForOrder(getRowCount(serverSession)); 266 } 267 268 278 279 private double getCostForOrder(long rows) throws DException { 280 return CostCalculator.getCostForTemporaryOrder(rows); 281 } 282 283 291 292 private _Iterator makeIteratorForOrder(_ServerSession session, _Iterator iterator) throws DException { 293 if (order != null) { 294 return GeneralPurposeStaticClass.getTemporaryIndexIterator(iterator, finalTablePlan, session, order); 295 } 296 return iterator; 297 } 298 299 307 308 private _Iterator makeIteratorForOrderAndRemainingCondition(_ServerSession session, _Iterator iterator) throws DException { 309 iterator = executeWithBVE(session, iterator); 310 iterator = makeIteratorForOrder(session, iterator); 311 return iterator; 312 } 313 314 319 320 public _TablePlan[] getChildTablePlans() throws DException { 321 childPlans = finalTablePlan.getChildTablePlans(); 322 initializeTableDetails(); 323 return new _TablePlan[] {this}; 324 } 325 326 335 336 public boolean setAggregateColumnsForIndexing(ColumnDetails[] columns, _ServerSession session) throws DException { 337 return ( (SingleTablePlan) finalTablePlan).setAggregateColumnsForIndexing(columns, session); 338 } 339 340 345 346 public void setForUpdate() { 347 ( (SingleTablePlan) finalTablePlan).setForUpdate(); 348 } 349 350 355 356 public int getType() throws DException { 357 throw new UnsupportedOperationException ("Method not supported"); 358 } 359 360 365 366 public _Iterator executeWithoutOrder(_ServerSession session) throws DException { 367 _Iterator iterator = finalTablePlan.executeWithoutOrder(session); 368 return makeIteratorForOrderAndRemainingCondition(session, iterator); 369 } 370 371 public _Iterator executeWithOutOrder(_ServerSession session, booleanvalueexpression condition) throws DException { 372 _Iterator iterator = finalTablePlan.executeWithOutOrder(session, condition); 373 return executeWithBVE(session, iterator); 374 } 375 376 381 382 public double getCostWithoutOrder(_ServerSession session, booleanvalueexpression condition, long estimatedRowCount) throws DException { 383 return addCostForRemainingCondition(finalTablePlan.getCostWithoutOrder(session, condition, estimatedRowCount), estimatedRowCount); 384 } 385 386 public double getCostWithoutOrder(_ServerSession session) throws DException { 387 return addCostForRemainingCondition(finalTablePlan.getCostWithoutOrder(session), session); 388 } 389 390 394 395 public _TablePlan joinMissingLink() throws DException { 396 return finalTablePlan.joinMissingLink(); 397 } 398 399 public _Order getOrder() throws DException { 400 _Order order1 = finalTablePlan.getOrder(); 401 if (order != null && order1 != null) { 402 throw new DException("DSE3529", new Object [] {order, order1}); 403 } 404 return order != null ? order : order1; 405 } 406 } 407 | Popular Tags |