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.iterator.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*; 12 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression. 13 queryspecification.*; 14 import com.daffodilwoods.daffodildb.server.sql99.dql.semanticchecker.*; 15 import com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.*; 16 import com.daffodilwoods.daffodildb.server.sql99.expression. 17 booleanvalueexpression.*; 18 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 19 import com.daffodilwoods.daffodildb.utils.field.*; 20 import com.daffodilwoods.database.resource.*; 21 import com.daffodilwoods.database.sqlinitiator.*; 22 import com.daffodilwoods.database.utility.P; 23 import com.daffodilwoods.daffodildb.server.sql99.expression. 24 booleanvalueexpression.predicates.inpredicate; 25 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition. 26 BVEPlanMerger; 27 import com.daffodilwoods.daffodildb.server.sql99.utils._VariableValues; 28 import com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary. 29 subquery; 30 import com.daffodilwoods.daffodildb.client.QueryPlan; 31 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._Iterator; 32 33 48 49 public class QuerySpecificationPlan 50 extends PlanExceptionAbstract 51 implements _TablePlanAdditional { 52 53 56 57 private _TablePlan tablePlan; 58 59 62 63 private ColumnDetails[] selectListColumnDetails; 64 65 68 69 private _Reference[] selectReferences; 70 71 74 75 private topfunction _Opttopfunction2; 76 77 80 81 private SemanticChecker semanticChecker; 82 83 87 88 private tableexpression tableExpression; 89 90 93 94 private _Order order; 96 100 101 private _Reference[] underlyingReferencesSolvableByCurrentIterator; 102 103 106 107 private queryspecification qs; 108 109 private _Reference[] subqueryReferences; 110 111 public QuerySpecificationPlan(_TablePlan tPlan, 112 ColumnDetails[] selectListCD, 113 114 SemanticChecker semanticChecker0, 115 _Reference[] selectReferences0, 116 topfunction _Opttopfunction20, 117 tableexpression tableExpression0, 118 _Reference[] 119 underlyingReferencesSolvableByCurrentIterator0, 120 queryspecification qs) throws DException { 121 this.qs = qs; 122 tablePlan = tPlan; selectListColumnDetails = selectListCD; 124 tableExpression = tableExpression0; 125 selectReferences = selectReferences0; _Opttopfunction2 = _Opttopfunction20; 127 semanticChecker = semanticChecker0; underlyingReferencesSolvableByCurrentIterator = 129 underlyingReferencesSolvableByCurrentIterator0; 130 getChildTablePlans(); 131 132 } 133 134 141 142 public double getCost(_ServerSession session) throws DException { 143 return addCostForOrder(tablePlan.getCost(session), session); 144 } 145 146 153 154 private double addCostForOrder(double cost, _ServerSession serverSession) throws 155 DException { 156 return order == null 157 ? cost 158 : cost + getCostForOrder(getRowCount(serverSession)); 159 } 160 161 168 169 private double getCostForOrder(double rows) throws DException { 170 return CostCalculator.getCostForTemporaryOrder(rows); 171 } 172 173 180 181 public long getRowCount(_ServerSession serverSession) throws DException { 182 return tablePlan.getRowCount(serverSession); 183 } 184 185 193 private _Iterator getOrderedIterator(_ServerSession session, 194 _Iterator iterator, 195 _Reference[] subQueryRef, 196 _Iterator[] iterator1) throws DException { 197 return order == null 198 ? iterator 199 : 200 GeneralPurposeStaticClass.getTemporaryIndexIteratorForSubQuery(iterator, 201 tablePlan, 202 session, order, subQueryRef, iterator1); 203 } 204 205 213 public _Iterator execute(_ServerSession session) throws DException { 214 semanticChecker.checkSelectListDistinctSemantic(); 215 _Iterator iter = tablePlan.execute(session); 216 _Iterator[] iter1 = getIteratorForSubQuery(iter, session); 217 iter = getOrderedIterator(session, iter, subqueryReferences, iter1); 218 return getAppropriateIterator(session, iter, false, iter1, 219 subqueryReferences); 220 221 } 222 223 232 233 public _Iterator executeWithOutOrder(_ServerSession session, 234 booleanvalueexpression condition) throws 235 DException { 236 semanticChecker.checkSelectListDistinctSemantic(); 237 _Iterator iter = tablePlan.executeWithOutOrder(session, condition); 238 _Iterator[] iter1 = getIteratorForSubQuery(iter, session); 239 return getAppropriateIterator(session, iter, false, iter1, 240 subqueryReferences); 241 } 242 243 251 252 public _Iterator executeWithoutOrder(_ServerSession session) throws 253 DException { 254 semanticChecker.checkSelectListDistinctSemantic(); 255 _Iterator iter = tablePlan.executeWithoutOrder(session); 256 _Iterator[] iter1 = getIteratorForSubQuery(iter, session); 257 return getAppropriateIterator(session, iter, false, iter1, 258 subqueryReferences); 259 } 260 261 270 271 public _Iterator execute(_ServerSession session, 272 booleanvalueexpression condition) throws DException { 273 semanticChecker.checkSelectListDistinctSemantic(); 274 _Iterator iter = tablePlan.execute(session, condition); 275 _Iterator[] iter1 = getIteratorForSubQuery(iter, session); 276 iter = getOrderedIterator(session, iter, subqueryReferences, iter1); 277 return getAppropriateIterator(session, iter, false, iter1, 278 subqueryReferences); 279 } 280 281 290 291 public double getCostWithoutOrder(_ServerSession session, 292 booleanvalueexpression condition, 293 long estimatedRowCount) throws DException { 294 return tablePlan.getCostWithoutOrder(session, condition, estimatedRowCount); 295 } 296 297 304 305 public double getCostWithoutOrder(_ServerSession session) throws DException { 306 return tablePlan.getCostWithoutOrder(session); 307 } 308 309 318 319 public double getCost(_ServerSession session, 320 booleanvalueexpression condition, long estimatedRows) throws 321 DException { 322 return addCostForOrder(tablePlan.getCost(session, condition, estimatedRows), 323 session); 324 } 325 326 public String getVerifier() throws DException { 327 StringBuffer buffer = new StringBuffer (); 328 inc(); 329 buffer.append(tabW("[ QUERY_SPECIFICATION_PLAN ]")); 330 buffer.append("\n" + tablePlan.getVerifier()).toString(); 331 buffer.append(tab("[/ QUERY_SPECIFICATION_PLAN ]")); 332 dec(); 333 return buffer.toString(); 334 } 335 336 341 342 public _QueryPlan getQueryPlan() throws DException { 343 _QueryPlan cplan = tablePlan.getQueryPlan(); 344 _QueryPlan[] cplans = cplan == null ? null : new _QueryPlan[] { 345 cplan}; 346 return new QueryPlan("QuerySpecification", cplans, "" + this, null); 347 } 348 349 354 355 public int[] getHasRecordIndexes() throws DException { 356 ArrayList list = new ArrayList(); 357 for (int i = 0, length = selectListColumnDetails.length; i < length; i++) { 358 if (selectListColumnDetails[i].getType() == ColumnDetails.HAS_RECORD) { 359 list.add(new Integer (i)); 360 } 361 } 362 return list.isEmpty() ? null : getIntFromList(list); 363 } 364 365 371 372 private int[] getIntFromList(ArrayList list) throws DException { 373 int size = list.size(); 374 int[] indexes = new int[size]; 375 for (int i = 0; i < size; i++) { 376 indexes[i] = list.get(i).hashCode(); 377 } 378 return indexes; 379 } 380 381 400 401 private _Iterator getAppropriateIterator(_ServerSession session, 402 _Iterator iterator, boolean isView, 403 _Iterator[] iter, _Reference[] ref) throws 404 DException { 405 406 int type = session.getType(); 407 iterator = getIterator(iterator); 408 TableDetails[] tableDetails = tableExpression.getTableDetails(); 409 switch (type) { 410 case IteratorConstants.NONSCROLLABLE: 411 return new SelectedColumnIterator(selectListColumnDetails, iterator, 412 selectReferences, tableDetails, 413 isView, 414 underlyingReferencesSolvableByCurrentIterator, 415 session, qs, ref, iter); 416 case IteratorConstants.SCROLLABLE: 417 return new SelectedColumnIterator(selectListColumnDetails, iterator, 418 selectReferences, tableDetails, 419 isView, type, 420 underlyingReferencesSolvableByCurrentIterator, 421 session, qs, ref, iter); 422 case IteratorConstants.UPDATABLE: 423 ArrayList list = new ArrayList(); 424 semanticChecker.getGroupByPrimaryColumns(list); 425 String [] groupByPrimaryColumns = list.isEmpty() ? null : 426 (String []) list.toArray(new String [list.size()]); 427 428 ColumnDetails[] changedCD = changeSelectedColumnForView( 429 selectListColumnDetails, session); 430 431 return new UpdatableSelectIterator(changedCD, iterator, 432 selectReferences, 433 tableExpression.getAllTableDetails(), 434 tableExpression.getViewTableDetails(), 435 groupByPrimaryColumns, isView, 436 tableExpression, session, 437 underlyingReferencesSolvableByCurrentIterator, 438 qs, ref, iter); 439 default: 440 throw new DException("DSE3522", new Object [] {new Integer (type)}); 441 } 442 } 443 444 454 455 private _Iterator getIterator(_Iterator iter) throws DException { 456 ArrayList list = new ArrayList(); 457 for (int i = 0; i < selectListColumnDetails.length; i++) { 458 if (selectListColumnDetails[i].getType() == ColumnDetails.CONSTANT && 459 selectListColumnDetails[i].getOriginalColumn().equalsIgnoreCase( 460 "rownum")) { 461 list.add(selectListColumnDetails[i]); 462 } 463 } 464 if (list.size() > 0) { 465 _Reference[] refs = (_Reference[]) list.toArray(new _Reference[list.size()]); 466 iter = new CounterIterator(iter, refs); 467 } 468 return _Opttopfunction2 != null ? 469 new TopIterator(iter, (FieldBase) _Opttopfunction2.run(null)) : iter; 470 } 471 472 477 478 public void setOrder(_Order order0) throws DException { 479 order = order0; 480 } 481 482 487 488 public _Order getOrder() throws DException { 489 return order; 490 } 491 492 497 498 public _TablePlan[] getChildTablePlans() throws DException { 499 childPlans = tablePlan.getChildTablePlans(); 500 initializeTableDetails(); 501 return new _TablePlan[] { 502 this}; 503 } 504 505 509 510 public void setForUpdate() { 511 ( (TableExpressionPlan) tablePlan).setForUpdate(); 512 } 513 514 519 520 public _Iterator executePlan(_ServerSession session) throws DException { 521 semanticChecker.checkSelectListDistinctSemantic(); 522 _Iterator iter = tablePlan.execute(session); 523 _Iterator[] iter1 = getIteratorForSubQuery(iter, session); 524 iter = getOrderedIterator(session, iter, subqueryReferences, iter1); 525 return getAppropriateIterator(session, iter, true, iter1, 526 subqueryReferences); 527 } 528 529 public _Iterator executeWithOutOrderPlan(_ServerSession session, 530 booleanvalueexpression condition) throws 531 DException { 532 semanticChecker.checkSelectListDistinctSemantic(); 533 _Iterator iter = tablePlan.executeWithOutOrder(session, condition); 534 _Iterator[] iter1 = getIteratorForSubQuery(iter, session); 535 return getAppropriateIterator(session, iter, true, iter1, 536 subqueryReferences); 537 } 538 539 public _Iterator executeWithoutOrderPlan(_ServerSession session) throws 540 DException { 541 semanticChecker.checkSelectListDistinctSemantic(); 542 _Iterator iter = tablePlan.executeWithoutOrder(session); 543 _Iterator[] iter1 = getIteratorForSubQuery(iter, session); 544 return getAppropriateIterator(session, iter, true, iter1, 545 subqueryReferences); 546 } 547 548 public _Iterator executePlan(_ServerSession session, 549 booleanvalueexpression condition) throws 550 DException { 551 552 semanticChecker.checkSelectListDistinctSemantic(); 553 _Iterator iter = tablePlan.execute(session, condition); 554 _Iterator[] iter1 = getIteratorForSubQuery(iter, session); 555 iter = getOrderedIterator(session, iter, subqueryReferences, iter1); 556 return getAppropriateIterator(session, iter, true, iter1, 557 subqueryReferences); 558 } 559 560 561 public ColumnDetails[] changeSelectedColumnForView(ColumnDetails[] 562 selectListColumnDetails, 563 _ServerSession session) throws DException { 564 ColumnDetails[] newCD = new ColumnDetails[selectListColumnDetails.length]; 565 for (int i = 0; i < selectListColumnDetails.length; i++) { 566 TableDetails td = selectListColumnDetails[i].getTableDetails(); 567 if (td == null || td.getTableType() != TableDetails.VIEW) 568 newCD[i] = selectListColumnDetails[i]; 569 else { 570 ViewObject viewObject = (ViewObject) session.getViewObject(td. 571 getQualifiedIdentifier(), false); 572 ColumnDetails[] cd = viewObject.getQueryExpression().getSelectedColumns(); 573 String columnName = selectListColumnDetails[i].getColumn(); 574 for (int j = 0; j < cd.length; j++) { 575 String viewColumnName = cd[j].getAppropriateColumn(); 576 if (columnName.equalsIgnoreCase(viewColumnName)) { 577 newCD[i] = cd[j]; 578 break; 579 } 580 } 581 if (newCD[i] == null) 582 ; } 584 } 585 return newCD; 586 } 587 588 public _Iterator[] getIteratorForSubQuery(_Iterator iterator, 589 _ServerSession serverSession) throws 590 DException { 591 ArrayList list = new ArrayList(); 592 ArrayList al = new ArrayList(); 593 TableDetails[] allTableDetails = tableExpression.getTableDetails(); 594 if (selectReferences != null) { 595 for (int i = 0; i < selectReferences.length; i++) { 596 if (selectReferences[i].getReferenceType() == SimpleConstants.SUBQUERY) { 597 list.add(selectReferences[i]); 598 al.add( ( (subquery) selectReferences[i]).getSelectIterator( 599 serverSession)); 600 } 601 } 602 if (list.size() > 0) { 603 subqueryReferences = (_Reference[]) list.toArray(new _Reference[list. 604 size()]); 605 } 606 } 607 608 609 if (subqueryReferences != null) { 610 for (int i = 0; i < subqueryReferences.length; i++) { 611 _Iterator subQueryIterator = (_Iterator)al.get(i); 612 _Reference[] ref = GeneralPurposeStaticClass.getAllReferences(new 613 _Reference[] {subqueryReferences[i]}); 614 ref = GeneralPurposeStaticClass.getUnderLyingReferencesOnly(ref, 615 allTableDetails); 616 if (ref != null) { 617 Object [] values = new Object [ref.length]; 618 Arrays.fill(values, iterator); 619 subQueryIterator.setConditionVariableValue(ref, values, 0); 620 } 621 } 622 } 623 return (_Iterator[]) al.toArray(new _Iterator[al.size()]); 624 } 625 } 626 627 628 629 630 | Popular Tags |