1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.table; 2 3 import com.daffodilwoods.daffodildb.client.*; 4 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*; 5 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 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.execution.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table.*; 12 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*; 13 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*; 14 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 15 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 16 import com.daffodilwoods.database.general.*; 17 import com.daffodilwoods.database.resource.*; 18 import com.daffodilwoods.database.sqlinitiator.*; 19 import com.daffodilwoods.database.utility.*; 20 import com.daffodilwoods.daffodildb.server.sql99.fulltext.expression.containsclause; 21 22 34 35 public class SingleTablePlan extends PlanAbstract implements TableExpressionConstants, _SingleTablePlan { 36 37 40 private booleanvalueexpression singleTableCondition; 41 42 45 46 private _Order order; 47 50 private TableDetails tableDetail; 51 52 55 56 private String [] queryColumns; 57 71 72 private Object conditionMappingArray[][]; 73 74 78 79 private Object conditionMappingArrayWithoutOrder[][]; 80 81 84 85 private _TableAlias[] tablelAliasArray; 87 90 91 private boolean isForeignKeyIterator; 93 96 97 private boolean forUpdate; 98 99 104 private ColumnDetails[] aggregateColumnsForIndexing; 105 106 110 111 boolean checkUserRight; 112 boolean isInternalIteratorRequired; 113 private _Reference[] underLyingRefofCondition; 114 115 public SingleTablePlan(String tableName, 116 booleanvalueexpression singleTableCondition0, 117 TableDetails tableDetail0, _Order order0, 118 boolean checkUserRight0, ColumnDetails[] columns) throws DException { 119 order = order0; 120 singleTableCondition = singleTableCondition0; 121 tableDetail = tableDetail0; 122 checkUserRight = checkUserRight0; 123 conditionMappingArray = new Object [1][2]; 124 conditionMappingArrayWithoutOrder = new Object [1][2]; 125 childPlans = new _TablePlan[] {this}; 126 initializeTableDetails(); 127 initializeQueryColumns(columns); 128 129 130 131 if(singleTableCondition!=null){ 132 underLyingRefofCondition = 133 GeneralPurposeStaticClass.getUnderLyingReferencesOnly( 134 GeneralPurposeStaticClass.getAllReferences( 135 singleTableCondition.getReferences(new TableDetails[] {tableDetail})), 136 new TableDetails[] {tableDetail}); 137 138 } 139 } 140 141 147 148 private void initializeQueryColumns(ColumnDetails[] columns) throws DException { 149 if (columns == null) { 150 return; 151 } 152 int length = columns.length; 153 queryColumns = new String [length]; 154 for (int i = 0; i < length; i++) { 155 queryColumns[i] = columns[i].getColumn(); 156 } 157 } 158 159 164 165 public void setCondition(booleanvalueexpression singleTableCondition0) throws 166 DException { 167 singleTableCondition = singleTableCondition0; 168 } 169 170 175 public int getType() throws DException { 176 return SINGLETABLEPLAN; 177 } 178 179 188 189 public double getCost(_ServerSession session) throws DException { 190 if (singleTableCondition != null) { 191 double cost = new ConditionSingleTableExecuter(order, tableDetail, 192 session, singleTableCondition, 193 queryColumns, aggregateColumnsForIndexing).getCost(session. 194 getIndexTable(tableDetail.getQualifiedIdentifier())); 195 return cost; 196 } 197 double cost = getCostWhenConditionNull(session); 198 return cost; 199 } 200 201 213 214 public double getCostWithoutOrder(_ServerSession session) throws DException { 215 if (singleTableCondition != null) { 216 double cost = new ConditionSingleTableExecuter(null, tableDetail, session, 217 singleTableCondition, 218 queryColumns, aggregateColumnsForIndexing).getCost(session.getIndexTable(tableDetail. 219 getQualifiedIdentifier())); 220 return cost; 221 } 222 double cost = getCostWithoutOrderWhenConditionNull(session); 223 return cost; 224 } 225 226 235 236 private double getCostWithoutOrderWhenConditionNull(_ServerSession session) throws 237 DException { 238 239 return new SingleTableExecuter(null, tableDetail, session, queryColumns, aggregateColumnsForIndexing). 240 getCost(session.getIndexTable(tableDetail.getQualifiedIdentifier())); 241 } 242 243 252 private double getCostWhenConditionNull(_ServerSession session) throws 253 DException { 254 255 return new SingleTableExecuter(order, tableDetail, session, queryColumns, aggregateColumnsForIndexing). 256 getCost(session.getIndexTable(tableDetail.getQualifiedIdentifier())); 257 } 258 259 273 274 public double getCost(_ServerSession session, 275 booleanvalueexpression conditionArg, long estimatedRows) throws 276 DException { 277 booleanvalueexpression bve = changeConditionMappingArray(conditionArg, true); 278 long myRows = tableDetail.getTableType() == TypeConstants.VIEW ? 1000 : 279 ( (_ServerSession) session).getEstimatedRowCount(tableDetail. 280 getQualifiedIdentifier()); 281 tableDetail.setRowCount(estimatedRows * (myRows == 0 ? 1 : myRows)); 282 double cost = getCostWithBve(session, bve); 283 tableDetail.setRowCount( -1); 284 return cost; 285 } 286 287 300 301 private booleanvalueexpression changeConditionMappingArray( 302 booleanvalueexpression conditionArg, boolean order) throws DException { 303 booleanvalueexpression bve = searchCostCaluledFrom(conditionArg, order); 304 if (bve != null) { 305 return bve; 306 } 307 308 bve = singleTableCondition == null ? 309 BVEPlanMerger.getBooleanFactor(new parenthesizedbooleanvalueexpression( 310 conditionArg)) 311 : BVEPlanMerger.addAndConditions(conditionArg, singleTableCondition); 312 313 if (order) { 314 conditionMappingArray = incrementAndAddInMapping(conditionMappingArray, 315 conditionArg, bve); 316 } else { 317 conditionMappingArrayWithoutOrder = incrementAndAddInMapping( 318 conditionMappingArrayWithoutOrder, conditionArg, bve); 319 } 320 return bve; 321 } 322 323 332 333 private Object [][] incrementAndAddInMapping(Object [][] mapping, 334 booleanvalueexpression 335 conditionArg, 336 booleanvalueexpression bve) throws 337 DException { 338 mapping[mapping.length - 1][0] = conditionArg; 339 mapping[mapping.length - 1][1] = bve; 340 Object [][] newMapping = new Object [mapping.length + 1][2]; 341 System.arraycopy(mapping, 0, newMapping, 0, mapping.length); 342 return newMapping; 343 } 344 345 350 351 public _TablePlan joinMissingLink() { 352 return this; 353 } 354 355 363 364 public long getRowCount(_ServerSession serverSession) throws DException { 365 long rowCount = tableDetail.getTableType() == 366 TypeConstants.VIEW ? 1000 : 367 ( (_ServerSession) serverSession). 368 getEstimatedRowCount(tableDetail.getQualifiedIdentifier()); 369 if (singleTableCondition == null) { 370 return rowCount; 371 } 372 return singleTableCondition.getEstimatedRows(rowCount); 373 } 374 375 385 386 public _Iterator execute(_ServerSession session) throws DException { 387 _SingleTableExecuter singleTableExecuter = getSingleTableExecuter(session, 388 singleTableCondition, order); 389 return isForeignKeyIterator ? 390 wrapSingleTableIteratorAndSolveOrder(session,session.getForeignKeyIterator(tableDetail.getQualifiedIdentifier(), singleTableExecuter, tablelAliasArray)) 391 : wrapSingleTableIteratorAndSolveOrder(session,session.getIterator(tableDetail.getQualifiedIdentifier(),singleTableExecuter)); 392 } 393 394 403 private _SingleTableExecuter getSingleTableExecuter(_ServerSession session, 404 booleanvalueexpression bve, _Order order) throws DException { 405 406 ConditionSingleTableExecuter ste = new ConditionSingleTableExecuter(order, tableDetail, session, bve, queryColumns, aggregateColumnsForIndexing); 407 if(bve instanceof containsclause) 408 ste.setContainsClauseFlag(true); 409 ste.setUserRight(checkUserRight); 410 if (forUpdate) { 411 ste.setForUpdate(); 412 } 413 ste.isInterIteratorRequired(getInternalIteratorRequired()); 414 return ste; 415 } 416 417 425 426 private _Iterator wrapSingleTableIteratorAndSolveOrder(_ServerSession session, 427 _Iterator iterator) throws DException { 428 iterator = wrapSingleTableIterator(iterator); 429 if (order != null) { 430 iterator = order.isSolvableByIndex() ? order.isSameOrReverse() ? iterator : new ReverseIterator(iterator) 431 : GeneralPurposeStaticClass.getTemporaryIndexIterator(iterator, this, session, order, getDefaultOrder()); } 433 iterator.setSpecificUnderlyingReferences(underLyingRefofCondition); 434 return iterator; 435 } 436 437 443 444 private _Iterator wrapSingleTableIterator(_Iterator iterator) throws 445 DException { 446 iterator = new SingleTableIterator(iterator, tableDetail, 447 tableDetail.getUnderLyingFKeyTables(), 448 order); 449 return iterator; 450 } 451 452 460 461 private _Order getDefaultOrder() throws DException { 462 ColumnDetails rowIdCd = new ColumnDetails(); 463 String catalogName = tableDetail.getCatalogName(); 464 String schemaName = tableDetail.getSchemaName(); 465 rowIdCd.setColumnName(new String [] {catalogName, schemaName, 466 SystemFields.systemFields[SystemFields.rowId]}); 467 rowIdCd.setTableDetails(tableDetail); 468 rowIdCd.setType(TypeConstants.REFERENCE); 469 rowIdCd.setDatatype(Datatype.LONG); 470 rowIdCd.setSize(Datatype.LONGSIZE); 471 return new SelectOrder(new ColumnDetails[] {rowIdCd}); 472 } 473 474 479 480 public _SingleTablePlan[] getSingleTablePlans() throws DException { 481 return new SingleTablePlan[] { 482 this}; 483 } 484 485 493 494 private _Iterator executeWithOutOrderWithBve(_ServerSession session, 495 booleanvalueexpression bve) throws 496 DException { 497 _SingleTableExecuter ste = getSingleTableExecuter(session, bve, null); 498 if (isForeignKeyIterator) { 499 _Iterator iterator=new SingleTableIterator(session.getForeignKeyIterator(tableDetail. 500 getQualifiedIdentifier(), ste, tablelAliasArray), tableDetail, 501 tableDetail.getUnderLyingFKeyTables(), 502 order); 503 iterator.setSpecificUnderlyingReferences(underLyingRefofCondition); 504 return iterator; 505 } 506 _Iterator iterator= new SingleTableIterator(session.getIterator(tableDetail. 507 getQualifiedIdentifier(), ste), tableDetail, 508 tableDetail.getUnderLyingFKeyTables(), order); 509 iterator.setSpecificUnderlyingReferences(underLyingRefofCondition); 510 return iterator; 511 } 512 513 525 526 527 528 public _Iterator executeWithOutOrder(_ServerSession session, 529 booleanvalueexpression condition) throws 530 DException { 531 booleanvalueexpression bve = ( (booleanvalueexpression) 532 searchCostCaluledFrom(condition, false)); 533 try { 534 return executeWithOutOrderWithBve(session, bve); 535 } catch (UnsupportedOperationException ex) { 536 throw ex; 537 } 538 } 539 540 550 551 public _Iterator executeWithoutOrder(_ServerSession session) throws 552 DException { 553 _SingleTableExecuter singleTableExecuter = getSingleTableExecuter(session, 554 singleTableCondition, null); 555 return isForeignKeyIterator ? 556 wrapSingleTableIterator(session.getForeignKeyIterator(tableDetail. 557 getQualifiedIdentifier(), singleTableExecuter, tablelAliasArray)) 558 : wrapSingleTableIterator(session.getIterator(tableDetail.getQualifiedIdentifier(), 559 singleTableExecuter)); 560 } 561 562 567 568 public _Order getOrder() throws DException { 569 return order; 570 } 571 572 580 581 private _Iterator executeWithBve(_ServerSession session, 582 booleanvalueexpression bve) throws 583 DException { 584 _SingleTableExecuter singleTableExecuter = getSingleTableExecuter(session, 585 bve, null); 586 if (isForeignKeyIterator) { 587 return wrapSingleTableIteratorAndSolveOrder(session, 588 session.getForeignKeyIterator(tableDetail.getQualifiedIdentifier(), singleTableExecuter, tablelAliasArray)); 589 } 590 return wrapSingleTableIteratorAndSolveOrder(session, session.getIterator(tableDetail.getQualifiedIdentifier(), singleTableExecuter)); 591 } 592 593 603 604 public _Iterator execute(_ServerSession session, 605 booleanvalueexpression condition) throws DException { 606 booleanvalueexpression bve = searchCostCaluledFrom(condition, true); 607 return executeWithBve(session, bve); 608 } 609 610 private booleanvalueexpression searchCostCaluledFrom(booleanvalueexpression 611 conditionArg, boolean order) throws DException { 612 Object [][] actualArray = order ? conditionMappingArray :conditionMappingArrayWithoutOrder; 613 for (int i = 0; i < actualArray.length; i++) { 614 if (actualArray[i][0] == conditionArg) { 615 booleanvalueexpression bve = (booleanvalueexpression) actualArray[i][1]; 616 return bve; 617 } 618 } 619 return null; 620 } 621 622 private double getCostWithoutOrderWithBve(_ServerSession session, 623 booleanvalueexpression bve, 624 long estimatedRows) throws 625 DException { 626 return bve.getCost(null, queryColumns, 627 session.getIndexTable(tableDetail.getQualifiedIdentifier()), 628 tableDetail, aggregateColumnsForIndexing).getCost(); 629 } 630 631 646 647 public double getCostWithoutOrder(_ServerSession session, 648 booleanvalueexpression conditionArg, 649 long estimatedRowCount) throws DException { 650 booleanvalueexpression bve = changeConditionMappingArray(conditionArg, false); 651 long myRows = tableDetail.getTableType() == TypeConstants.VIEW ? 1000 : 652 ( (_ServerSession) session).getEstimatedRowCount(tableDetail. 653 getQualifiedIdentifier()); 654 tableDetail.setRowCount(estimatedRowCount * myRows); 655 double cost = getCostWithoutOrderWithBve(session, bve, estimatedRowCount); 656 tableDetail.setRowCount( -1); 657 return cost; 658 } 659 660 669 public long getEstimatedRows(booleanvalueexpression conditionArg, 670 long estimatedRowCount, 671 _ServerSession serverSession) throws DException { 672 booleanvalueexpression condition1 = BVEPlanMerger.addAndConditions( 673 singleTableCondition, conditionArg); 674 return condition1.getEstimatedRows(estimatedRowCount); 675 } 676 677 685 686 private double getCostWithBve(_ServerSession session, 687 booleanvalueexpression bve) throws DException { 688 return bve.getCost(order, queryColumns, 689 session.getIndexTable(tableDetail.getQualifiedIdentifier()), 690 tableDetail, aggregateColumnsForIndexing).getCost(); 691 } 692 693 public String toString() { 694 try { 695 StringBuffer buffer = new StringBuffer (); 696 buffer.append("SINGLETABLEPLAN[" + tableDetail.getQualifiedTableName() + 697 "]"); 698 if (singleTableCondition != null) { 699 buffer.append("[" + singleTableCondition + "][" + 700 singleTableCondition.hashCode() + "]"); 701 } 702 if (order != null) { 703 buffer.append("[" + order + "]"); 704 } 705 if (order == null) { 706 buffer.append("[" + null +"]"); 707 } 708 return buffer.toString(); 709 } catch (DException d) { 710 return null; 711 } 712 713 } 714 715 720 721 public TableDetails[] getTableDetails() throws DException { 722 723 return new TableDetails[] { 724 tableDetail}; 725 } 726 727 733 734 public TableDetails[] getViewTableDetails() throws DException { 735 return getTableDetails(); 736 } 737 738 public String getVerifier() { 739 try { 740 StringBuffer buffer = new StringBuffer (); 741 inc(); 742 buffer.append(tabW("[ SINGLE_TABLE_PLAN ]")); 743 inc(); 744 buffer.append(tab("[Table = " + tableDetail.getQualifiedTableName() + "]")); 745 if (singleTableCondition != null) { 746 buffer.append("[Condition = " + singleTableCondition + "]"); 747 } 748 if (order != null) { 749 buffer.append("[Order = " + order + "]"); 750 } 751 if (order == null) { 752 buffer.append("[Order = " + null +"]"); 753 } 754 dec(); 755 buffer.append(tab("[/ SINGLE_TABLE_PLAN ]")); 756 dec(); 757 return buffer.toString(); 758 } catch (DException ex) { 759 return null; 760 } 761 } 762 763 769 770 public boolean ifExists(TableDetails tableDetails0) throws DException { 771 return tableDetails0 == tableDetail; 772 } 773 774 779 780 public _QueryPlan getQueryPlan() throws DException { 781 _QueryPlan[] cplans = null; 782 String cond = singleTableCondition == null ? null : 783 ("" + singleTableCondition); 784 String ord = order == null ? null : ("" + order); 785 return new QueryPlan("SingleTablePlan " + tableDetail.getQualifiedTableName(), 786 cplans, cond, ord); 787 } 788 789 795 796 public _TablePlan[] getChildTablePlans() throws DException { 797 return childPlans; 798 } 799 800 805 806 public void setOrder(_Order order) throws DException { 807 this.order = order; 808 } 809 810 818 819 public boolean setAggregateColumnsForIndexing(ColumnDetails[] columns, _ServerSession session) throws 820 DException { 821 _IndexTable indexTable = session.getIndexTable(tableDetail.getQualifiedIdentifier()); 822 _IndexInformation[] indexInformations = indexTable.getIndexInformations(); 823 for (int i = 0; i < indexInformations.length; i++) { 824 if (TableReferenceMerger.checkForAggregateColumn(columns[0].getExistingColumnDetails()[0], indexInformations[i])) { 825 aggregateColumnsForIndexing = columns; 826 return true; 827 } 828 } 829 return false; 830 } 831 832 838 839 public boolean containsOrderSequencePlan() throws com.daffodilwoods.database. 840 resource.DException { 841 return false; 842 } 843 844 847 public void setForUpdate() { 848 forUpdate = true; 849 } 850 public void setInternalIteratorRequired( boolean isInternal){ 851 isInternalIteratorRequired = isInternal; 852 } 853 public boolean getInternalIteratorRequired(){ 854 return isInternalIteratorRequired; 855 } 856 } 857 | Popular Tags |