1 package com.daffodilwoods.daffodildb.server.sql99.expression. 2 booleanvalueexpression.predicates; 3 4 import java.util.*; 5 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.listenerevents.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*; 12 import com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.fromclause.*; 13 import com.daffodilwoods.daffodildb.server.sql99.expression. 14 booleanvalueexpression.*; 15 import com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary.*; 16 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*; 17 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 18 import com.daffodilwoods.database.resource.*; 19 import com.daffodilwoods.database.utility.*; 20 import com.daffodilwoods.daffodildb.utils.field.FieldBase; 21 import com.daffodilwoods.daffodildb.utils.comparator.SuperComparator; 22 import com.daffodilwoods.daffodildb.utils.comparator. 23 ComparisionPredicateJoinComparator; 24 import com.daffodilwoods.daffodildb.utils.comparator.CJoDpnqbsbups; 25 import com.daffodilwoods.daffodildb.utils.comparator.CPckfduDpnqbsbups; 26 27 36 37 public class quantifiedcomparisonpredicate 38 extends PredicateAbstract 39 implements predicate, OperatorConstants, IntegerPool, 40 ExecutionPlanConstants, SimpleConstants { 41 public tablesubquery _tablesubquery0; 42 public quantifier _quantifier1; 43 public compop _compop2; 44 public rowvalueexpressionwithoutboolean _rowvalueexpressionwithoutboolean3; 45 46 49 private int Operator = -1; 50 51 54 private int quantifier = -1; 55 56 59 private Boolean subQueryFlag = Boolean.FALSE; 60 61 69 public double getCost(long rowCount, boolean index) throws DException { 70 switch (Operator) { 71 case EQUALTO: 72 rowCount *= CostFactorConstants.EQUALTO / 100; 73 case GREATERTHAN: 74 case LESSTHAN: 75 rowCount *= CostFactorConstants.GREATERTHAN / 100; 76 case GREATERTHANEQUALTO: 77 case LESSTHANEQUALTO: 78 rowCount *= CostFactorConstants.GREATERTHANEQUALTO / 100; 79 } 80 if (index) { 81 rowCount *= CostFactorConstants.INDEXPREDICATE / 100; 82 } 83 return rowCount; 84 } 85 86 106 public _QualifiedBVE getQualifiedBVE(TableDetails[] tableDetails0) throws 107 DException { 108 ArrayList uniqueTableList = getUniqueTableDetails(new ArrayList(), 109 columnDetails); 110 QualifiedBVE qualifiedBVE = getQualifiedBVEifBelongsToSingleTablePlan( 111 tableDetails0, uniqueTableList); 112 if (qualifiedBVE == null) { 113 TableDetails[] tableDetails = (TableDetails[]) uniqueTableList.toArray(new 114 TableDetails[uniqueTableList.size()]); 115 return new QualifiedBVE(makeBVESingleTablePlanArray(tableDetails, 116 BVEPlanMerger.getBooleanFactor(this)), null); 117 } 118 return qualifiedBVE; 119 } 120 121 128 private QualifiedBVE getQualifiedBVEifBelongsToSingleTablePlan(TableDetails[] 129 planTableDetails, ArrayList uniqueTableList) throws DException { 130 TableDetails[] conditionTableDetails = (TableDetails[]) uniqueTableList. 131 toArray(new TableDetails[uniqueTableList.size()]); 132 for (int i = 0, length2 = conditionTableDetails.length; i < length2; i++) { 133 for (int j = 0, length = planTableDetails.length; j < length; j++) { 134 if (! (planTableDetails[j] == conditionTableDetails[i])) { 135 return new QualifiedBVE(null, BVEPlanMerger.getBooleanFactor(this)); 136 } 137 } 138 } 139 return null; 140 } 141 142 165 166 173 private BVESingleTablePlan[] makeBVESingleTablePlanArray(TableDetails[] 174 tableDetails, booleanvalueexpression bve) throws DException { 175 BVESingleTablePlan[] bveSingleTablePlans = new BVESingleTablePlan[ 176 tableDetails.length]; 177 for (int i = 0; i < tableDetails.length; i++) { 178 bveSingleTablePlans[i] = new BVESingleTablePlan(bve, tableDetails[i]); 179 } 180 return bveSingleTablePlans; 181 } 182 183 188 public int getPredicateType() throws DException { 189 return OperatorConstants.QUANTIFIEDCOMPARISON; 190 } 191 192 197 public void setColumnPredicates(_AllColumnPredicates allColumnPredicates) throws 198 DException { 199 allColumnPredicates.addToNonIndex(new booleanfactor(new booleantest(this))); 200 } 201 202 219 private int getBVEPlanType(ArrayList tableList) throws DException { 220 for (int i = 0; i < columnDetails.length; i++) { 221 int type = columnDetails[i].getType(); 222 if (type == GROUPING) { 223 return BVEConstants.BVEAGGREGATEPLAN; 224 } 225 if (type == SCALARFUNCTION || type == CASEEXPRESSION || 226 type == FUNCTIONAL || type == USERFUNCTION) { 227 return BVEConstants.BVEALLTABLEPLAN; 228 } 229 if (type != CONSTANT) { 230 TableDetails tableDetail = columnDetails[i].getTableDetails(); 231 if (tableDetail != null && !tableList.contains(tableDetail)) { 232 tableList.add(tableDetail); 233 } 234 } 235 } 236 if (checkForSubQuery() && checkForComplexInnerQuery()) { 237 return BVEConstants.BVEALLTABLEPLAN; 238 } 239 int size = tableList.size(); 240 if (size == 1) { 241 return BVEConstants.BVESINGLETABLEPLAN; 242 } 243 else if (size == 2) { 244 return BVEConstants.BVEJOINRELATION; 245 } 246 else { 247 return BVEConstants.BVEALLTABLEPLAN; 248 } 249 } 250 251 268 public _BVEPlan getExecutionPlan() throws DException { 269 ArrayList tableList = new ArrayList(); 270 int type = getBVEPlanType(tableList); 271 switch (type) { 272 case BVEConstants.BVEAGGREGATEPLAN: 273 return new BVEAggregatePlan(BVEPlanMerger.getBooleanFactor(this)); 274 case BVEConstants.BVESINGLETABLEPLAN: 275 return new BVESingleTablePlan(BVEPlanMerger.getBooleanFactor(this), 276 (TableDetails) tableList.get(0)); 277 case BVEConstants.BVEJOINRELATION: 278 279 _JoinRelation sr = new SimpleRelation( (TableDetails[]) tableList. 280 toArray(new TableDetails[0]), 281 BVEPlanMerger.getBooleanFactor(this)); 282 AllTablesJoinRelation atjr = new AllTablesJoinRelation(new 283 _JoinRelation[] {sr}); 284 return new BVEAllTablePlan(null, atjr, null); 285 case BVEConstants.BVEALLTABLEPLAN: 286 return new BVEAllTablePlan(null, null, BVEPlanMerger.getBooleanFactor(this)); 287 } 288 throw new DException("DSE3547", new Object [] {this.toString()}); 289 } 290 291 297 298 private boolean checkForComplexInnerQuery() throws DException { 299 _Reference[] references = getReferences(null); 300 int length = references.length; 301 for (int i = 0; i < length; i++) { 302 if (references[i].getReferenceType() == SUBQUERY) { 303 if ( ( (subquery) references[i]).getUnderlyingReferences() != null) { 304 return true; 305 } 306 } 307 } 308 return false; 309 } 310 311 321 public Object run(Object object) throws com.daffodilwoods.database.resource. 322 DException { 323 Object fb1 = _rowvalueexpressionwithoutboolean3.run(object); 324 return handlingOfSubQuery(fb1, (_Iterator) _tablesubquery0.run(object), null, 325 object); 326 } 327 328 334 335 private int getQuantifier(String quantifierString) throws DException { 336 if (quantifierString.equalsIgnoreCase("ALL")) { 337 return ALL; 338 } 339 if (quantifierString.equalsIgnoreCase("SOME")) { 340 return SOME; 341 } 342 return ANY; 343 } 344 345 355 private Object handlingOfSubQuery(Object result1, _Iterator iterator, 356 _ServerSession serverSession, Object object) throws 357 DException { 358 if (!iterator.first()) { 359 return IntegerPool.Integer1; 360 } 361 if (quantifier == ALL) { 362 return handlingOfAll(result1, iterator, serverSession, object); 363 } 364 return handlingOfSomeAndAny(result1, iterator, serverSession, object); 365 } 366 367 377 private Object handlingOfAll(Object result1, _Iterator iterator, 378 _ServerSession serverSession, Object object) throws 379 DException { 380 do { 381 int cmp = 0; 382 Object iteratorValue = null; 383 try { 384 iteratorValue = ( (_SelectIterator) iterator).getSelectColumnValues(); 385 cmp = comparator.compare(result1, iteratorValue); 386 } 387 catch (NullPointerException ex) { 388 if (!_rowvalueexpressionwithoutboolean3.getByteComparison(object). 389 canUseByteComparison() || 390 !_tablesubquery0.getByteComparison(object).canUseByteComparison()) { 391 comparator = getAppropriateObjectComparatorInCaseofSubQuery(result1, 392 iteratorValue, serverSession); 393 } 394 else { 395 comparator = getAppropriateSuperComparatorInCaseofSubQuery(result1, 396 iteratorValue, serverSession); 397 } 398 cmp = comparator.compare(result1, iteratorValue); 399 } 400 Object result = booleanResult[Operator - 1][cmp + 2]; 401 if (result.hashCode() != 0) { return result; 403 } 404 } 405 while (iterator.next()); 406 return IntegerPool.Integer0; } 408 409 419 private Object handlingOfSomeAndAny(Object result1, _Iterator iterator, 420 _ServerSession serverSession, 421 Object object) throws DException { 422 423 if (result1 instanceof Object []) { 424 Object [] fbs = (Object []) result1; 425 for (int i = 0; i < fbs.length; i++) { 426 if ( ( (FieldBase) fbs[i]).isNull()) 427 return IntegerPool.Integer2; 428 } 429 } 430 else 431 if ( ( (FieldBase) result1).isNull()) 432 return IntegerPool.Integer2; 433 434 435 do { 436 int cmp = 0; 437 Object iteratorValue = null; 438 try { 439 iteratorValue = ( (_SelectIterator) iterator).getSelectColumnValues(); 440 cmp = comparator.compare(result1, iteratorValue); 441 } 442 catch (NullPointerException ex) { 443 if (!_rowvalueexpressionwithoutboolean3.getByteComparison(object). 444 canUseByteComparison() || 445 !_tablesubquery0.getByteComparison(object).canUseByteComparison()) { 446 comparator = getAppropriateObjectComparatorInCaseofSubQuery(result1, 447 iteratorValue, serverSession); 448 } 449 else { 450 comparator = getAppropriateSuperComparatorInCaseofSubQuery(result1, 451 iteratorValue, serverSession); 452 } 453 cmp = comparator.compare(result1, iteratorValue); 454 } 455 Object result = booleanResult[Operator - 1][cmp + 2]; 456 if (result.hashCode() == 0) { 457 return IntegerPool.Integer0; 458 } 459 } 460 while (iterator.next()); 461 462 463 return IntegerPool.Integer1; 464 465 } 466 467 473 private int getRespectiveOperator(String oper) throws DException { 474 if (oper.equals(EQUALTOSTRING)) { 475 return EQUALTO; 476 } 477 else if (oper.equals(GREATERTHANSTRING)) { 478 return GREATERTHAN; 479 } 480 else if (oper.equals(GREATERTHANEQUALTOSTRING)) { 481 return GREATERTHANEQUALTO; 482 } 483 else if (oper.equals(LESSTHANSTRING)) { 484 return LESSTHAN; 485 } 486 else if (oper.equals(LESSTHANEQUALTOSTRING)) { 487 return LESSTHANEQUALTO; 488 } 489 else if (oper.equals(NOTEQUALTOSTRING) || oper.equals(NOTEQUALTOSTRING1)) { 490 return NOTEQUALTO; 491 } 492 throw new DException("DSE528", new Object [] {oper}); 493 } 494 495 503 504 public ParameterInfo[] getParameterInfo() throws DException { 505 ParameterInfo[] p1 = _rowvalueexpressionwithoutboolean3.getParameterInfo(); 506 ParameterInfo[] p2 = _tablesubquery0.getParameterInfo()[0]. 507 getParameterInfoArray(); 508 if (p1 == null && p2 == null) { 509 return null; 510 } 511 if (p1 == null) { 512 return p2; 513 } 514 if (p2 == null) { 515 return p1; 516 } 517 ParameterInfo[] result = new ParameterInfo[p1.length + p2.length]; 518 System.arraycopy(p1, 0, result, 0, p1.length); 519 System.arraycopy(p2, 0, result, p1.length, p2.length); 520 return result; 521 } 522 523 public AbstractRowValueExpression[] getChilds() { 524 AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] { 525 (AbstractRowValueExpression) (_rowvalueexpressionwithoutboolean3), 526 (AbstractRowValueExpression) (_tablesubquery0)}; 527 return childs; 528 } 529 530 public void setDefaultValues(_VariableValueOperations variableValueOperation) throws 531 DException { 532 } 533 534 540 public com.daffodilwoods.daffodildb.server.sql99.utils._Reference[] 541 checkSemantic(com.daffodilwoods.daffodildb.server.serversystem. 542 _ServerSession parent) throws DException { 543 getColumnDetails(); 544 _Reference[] references = GeneralPurposeStaticClass.getJointReferences( 545 _rowvalueexpressionwithoutboolean3.checkSemantic(parent), 546 _tablesubquery0.checkSemantic(parent)); 547 performingCardinalityChecking(); 548 checkDataTypes(parent); 549 return references; 550 } 551 552 private void checkDataTypes(_ServerSession session) throws DException { 553 int[] dt1 = _rowvalueexpressionwithoutboolean3.getByteComparison(session). 554 getDataTypes(); 555 int[] dt2 = _tablesubquery0.getByteComparison(session).getDataTypes(); 556 Check.checkForDataTypes(dt1, dt2); 557 } 558 559 563 private void performingCardinalityChecking() throws DException { 564 int cardinality1 = _rowvalueexpressionwithoutboolean3.getCardinality(); 565 int card2 = _tablesubquery0.getCardinality(); 566 if (cardinality1 != -1 && card2 != -1 && cardinality1 != card2) { 567 throw new DException("DSE214", null); 568 } 569 if (Operator != -1) { 570 Operator = getRespectiveOperator( (String ) _compop2.run(null)); 571 } 572 } 573 574 public String toString() { 575 StringBuffer sb = new StringBuffer (); 576 sb.append(" "); 577 sb.append(_rowvalueexpressionwithoutboolean3); 578 sb.append(" "); 579 sb.append(_compop2); 580 sb.append(" "); 581 sb.append(_quantifier1); 582 sb.append(" "); 583 sb.append(_tablesubquery0); 584 return sb.toString(); 585 } 586 587 593 public long getEstimatedRows(long noOfRows) throws DException { 594 if (Operator == -1) { 595 Operator = getRespectiveOperator( (String ) _compop2.run(null)); 596 } 597 switch (Operator) { 598 case EQUALTO: 599 return noOfRows * CostFactorConstants.EQUALTO / 100; 600 case GREATERTHAN: 601 case LESSTHAN: 602 return noOfRows * CostFactorConstants.GREATERTHAN / 100; 603 case GREATERTHANEQUALTO: 604 case LESSTHANEQUALTO: 605 return noOfRows * CostFactorConstants.GREATERTHANEQUALTO / 100; 606 } 607 return noOfRows; 608 } 609 610 public Object clone() throws CloneNotSupportedException { 611 quantifiedcomparisonpredicate tempClass = new quantifiedcomparisonpredicate(); 612 tempClass._tablesubquery0 = (tablesubquery) _tablesubquery0.clone(); 613 tempClass._quantifier1 = (quantifier) _quantifier1.clone(); 614 tempClass._compop2 = (compop) _compop2.clone(); 615 tempClass._rowvalueexpressionwithoutboolean3 = ( 616 rowvalueexpressionwithoutboolean) _rowvalueexpressionwithoutboolean3. 617 clone(); 618 try { 619 tempClass.getColumnDetails(); 620 } 621 catch (DException ex) { 622 throw new RuntimeException (ex.getMessage()); 623 } 624 return tempClass; 625 } 626 627 public ColumnDetails[] getColumnDetails() throws DException { 628 if (columnDetails == null) { 629 ColumnDetails[] columnDetails1 = _rowvalueexpressionwithoutboolean3. 630 getColumnDetails(); 631 ColumnDetails[] columnDetails2 = _tablesubquery0.getColumnDetails(); 632 columnDetails = new ColumnDetails[columnDetails1.length + 633 columnDetails2.length]; 634 int i = 0; 635 for (; i < columnDetails1.length; ++i) { 636 columnDetails[i] = columnDetails1[i]; 637 } 638 for (int j = 0; j < columnDetails2.length; ++i, ++j) { 639 columnDetails[i] = columnDetails2[j]; 640 } 641 } 642 643 String qua = (String ) _quantifier1.run(null); 644 quantifier = getQuantifier(qua); 645 Operator = getRespectiveOperator( (String ) _compop2.run(null)); 646 return columnDetails; 647 } 648 649 650 protected SuperComparator getAppropriateSuperComparatorInCaseofSubQuery( 651 Object result1, Object iteratorValue, _ServerSession serverSession) throws 652 DException { 653 if (result1 instanceof Object []) { 654 Object [] rr = (Object []) result1; 655 Object [] values = (Object []) iteratorValue; 656 SuperComparator[] comparators = new SuperComparator[rr.length]; 657 for (int i = 0; i < rr.length; i++) { 658 comparators[i] = getComparator( (FieldBase) rr[i], (FieldBase) values[i], 659 serverSession); 660 } 661 return new ComparisionPredicateJoinComparator(comparators, Operator); 662 } 663 return new CJoDpnqbsbups(new SuperComparator[] {getComparator( (FieldBase) 664 result1, (FieldBase) ( (Object []) iteratorValue)[0], serverSession)}); 665 } 666 667 668 protected SuperComparator getAppropriateObjectComparatorInCaseofSubQuery( 669 Object result1, Object iteratorValue, _ServerSession serverSession) throws 670 DException { 671 if (result1 instanceof Object []) { 672 Object [] rr = (Object []) result1; 673 Object [] values = (Object []) iteratorValue; 674 SuperComparator[] comparators = new SuperComparator[rr.length]; 675 for (int i = 0; i < rr.length; i++) { 676 comparators[i] = new CPckfduDpnqbsbups(); 677 } 678 return new ComparisionPredicateJoinComparator(comparators, Operator); 679 } 680 return new CJoDpnqbsbups(new SuperComparator[] {new CPckfduDpnqbsbups()}); 681 } 682 683 } 684 | Popular Tags |