1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.services.context.ContextManager; 25 26 import org.apache.derby.iapi.sql.compile.CompilerContext; 27 import org.apache.derby.iapi.sql.compile.CostEstimate; 28 import org.apache.derby.iapi.sql.compile.Optimizer; 29 import org.apache.derby.iapi.sql.compile.OptimizableList; 30 import org.apache.derby.iapi.sql.compile.Optimizable; 31 import org.apache.derby.iapi.sql.compile.OptimizablePredicate; 32 import org.apache.derby.iapi.sql.compile.OptimizablePredicateList; 33 import org.apache.derby.iapi.sql.compile.RequiredRowOrdering; 34 import org.apache.derby.iapi.sql.compile.RowOrdering; 35 import org.apache.derby.iapi.sql.compile.C_NodeTypes; 36 37 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 38 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 39 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 40 41 import org.apache.derby.iapi.sql.Activation; 42 import org.apache.derby.iapi.sql.ResultSet; 43 import org.apache.derby.iapi.sql.Row; 44 import org.apache.derby.iapi.error.StandardException; 45 46 import org.apache.derby.impl.sql.compile.ActivationClassBuilder; 47 import org.apache.derby.iapi.services.compiler.MethodBuilder; 48 49 import org.apache.derby.iapi.store.access.Qualifier; 50 51 import org.apache.derby.iapi.services.sanity.SanityManager; 52 53 import org.apache.derby.iapi.util.JBitSet; 54 import org.apache.derby.iapi.reference.SQLState; 55 import org.apache.derby.iapi.reference.ClassName; 56 import org.apache.derby.iapi.services.classfile.VMOpcode; 57 58 import java.util.Enumeration ; 59 import java.util.Properties ; 60 import java.util.Vector ; 61 62 67 68 public class RowResultSetNode extends FromTable 69 { 70 SubqueryList subquerys; 71 Vector aggregateVector; 72 OrderByList orderByList; 73 74 80 public void init(Object valuesClause, Object tableProperties) 81 { 82 super.init(null, tableProperties); 83 resultColumns = (ResultColumnList) valuesClause; 84 if (resultColumns != null) 85 resultColumns.markInitialSize(); 86 } 87 88 94 95 public String toString() 96 { 97 if (SanityManager.DEBUG) 98 { 99 return "orderByList: " + 100 (orderByList != null ? orderByList.toString() : "null") + "\n" + 101 super.toString(); 102 } 103 else 104 { 105 return ""; 106 } 107 } 108 109 public String statementToString() 110 { 111 return "VALUES"; 112 } 113 114 120 121 public void printSubNodes(int depth) 122 { 123 if (SanityManager.DEBUG) 124 { 125 super.printSubNodes(depth); 126 127 if (subquerys != null) 128 { 129 printLabel(depth, "subquerys: "); 130 subquerys.treePrint(depth + 1); 131 } 132 } 133 } 134 135 136 139 140 145 public CostEstimate estimateCost(OptimizablePredicateList predList, 146 ConglomerateDescriptor cd, 147 CostEstimate outerCost, 148 Optimizer optimizer, 149 RowOrdering rowOrdering) 150 throws StandardException 151 { 152 157 if (costEstimate == null) 158 { 159 costEstimate = optimizer.newCostEstimate(); 160 } 161 162 costEstimate.setCost(0.0d, 1.0d, 1.0d); 163 164 165 rowOrdering.optimizableAlwaysOrdered(this); 166 167 return costEstimate; 168 } 169 170 181 182 public ResultSetNode bindNonVTITables(DataDictionary dataDictionary, 183 FromList fromListParam) 184 throws StandardException 185 { 186 187 if (tableNumber == -1) tableNumber = getCompilerContext().getNextTableNumber(); 189 190 191 return this; 192 } 193 194 201 202 public void bindExpressions(FromList fromListParam) 203 throws StandardException 204 { 205 int nestingLevel; 206 207 subquerys = (SubqueryList) getNodeFactory().getNode( 208 C_NodeTypes.SUBQUERY_LIST, 209 getContextManager()); 210 211 aggregateVector = new Vector (); 212 213 220 resultColumns.checkForInvalidDefaults(); 221 222 227 228 229 if (fromListParam.size() == 0) 230 { 231 nestingLevel = 0; 232 } 233 else 234 { 235 nestingLevel = ((FromTable) fromListParam.elementAt(0)).getLevel() + 1; 236 } 237 setLevel(nestingLevel); 238 fromListParam.insertElementAt(this, 0); 239 resultColumns.bindExpressions(fromListParam, subquerys, 240 aggregateVector); 241 fromListParam.removeElementAt(0); 243 244 if (aggregateVector.size() > 0) 245 { 246 throw StandardException.newException(SQLState.LANG_NO_AGGREGATES_IN_WHERE_CLAUSE); 247 } 248 } 249 250 259 public void bindExpressionsWithTables(FromList fromListParam) 260 throws StandardException 261 { 262 263 return; 264 } 265 266 275 276 public void bindTargetExpressions(FromList fromListParam) 277 throws StandardException 278 { 279 bindExpressions(fromListParam); 280 } 281 282 289 public void bindUntypedNullsToResultColumns(ResultColumnList bindingRCL) 290 throws StandardException 291 { 292 301 if (bindingRCL == null) 302 bindingRCL = resultColumns; 303 304 resultColumns.bindUntypedNullsToResultColumns(bindingRCL); 305 } 306 307 320 321 public ResultColumn getMatchingColumn( 322 ColumnReference columnReference) 323 throws StandardException 324 { 325 return null; 326 } 327 328 336 public String getExposedName() throws StandardException 337 { 338 return null; 339 } 340 341 349 public void verifySelectStarSubquery(FromList outerFromList, int subqueryType) 350 throws StandardException 351 { 352 return; 353 } 354 355 363 void pushOrderByList(OrderByList orderByList) 364 { 365 this.orderByList = orderByList; 366 } 367 368 392 393 public ResultSetNode preprocess(int numTables, 394 GroupByList gbl, 395 FromList fromList) 396 throws StandardException 397 { 398 399 if (subquerys.size() > 0) 400 { 401 subquerys.preprocess( 402 numTables, 403 (FromList) getNodeFactory().getNode( 404 C_NodeTypes.FROM_LIST, 405 getNodeFactory().doJoinOrderOptimization(), 406 getContextManager()), 407 (SubqueryList) getNodeFactory().getNode( 408 C_NodeTypes.SUBQUERY_LIST, 409 getContextManager()), 410 (PredicateList) getNodeFactory().getNode( 411 C_NodeTypes.PREDICATE_LIST, 412 getContextManager())); 413 } 414 415 416 referencedTableMap = new JBitSet(numTables); 417 referencedTableMap.set(tableNumber); 418 return this; 419 } 420 421 429 public ResultSetNode ensurePredicateList(int numTables) 430 throws StandardException 431 { 432 return genProjectRestrict(numTables); 433 } 434 435 446 public ResultSetNode addNewPredicate(Predicate predicate) 447 throws StandardException 448 { 449 PredicateList predList; 450 ResultColumnList prRCList; 451 ResultSetNode newPRN; 452 453 457 458 461 prRCList = resultColumns; 462 resultColumns = resultColumns.copyListAndObjects(); 463 464 468 prRCList.genVirtualColumnNodes(this, resultColumns); 469 470 471 predList = (PredicateList) getNodeFactory().getNode( 472 C_NodeTypes.PREDICATE_LIST, 473 getContextManager()); 474 predList.addPredicate(predicate); 475 476 477 return (ResultSetNode) getNodeFactory().getNode( 478 C_NodeTypes.PROJECT_RESTRICT_NODE, 479 this, 480 prRCList, 481 null, 482 predList, 483 null, 484 null, 485 tableProperties, 486 getContextManager() ); 487 } 488 489 504 public boolean flattenableInFromSubquery(FromList fromList) 505 { 506 if ((subquerys != null) && 507 (subquerys.size() > 0)) 508 { 509 return false; 510 } 511 512 if ((aggregateVector != null) && 513 (aggregateVector.size() > 0)) 514 { 515 return false; 516 } 517 518 522 if ( ! resultColumns.isCloneable()) 523 { 524 return false; 525 } 526 527 boolean nonRowResultSetFound = false; 528 int flSize = fromList.size(); 529 for (int index = 0; index < flSize; index++) 530 { 531 FromTable ft = (FromTable) fromList.elementAt(index); 532 533 if (ft instanceof FromSubquery) 534 { 535 ResultSetNode subq = ((FromSubquery) ft).getSubquery(); 536 if ( ! (subq instanceof RowResultSetNode)) 537 { 538 nonRowResultSetFound = true; 539 break; 540 } 541 } 542 else 543 { 544 nonRowResultSetFound = true; 545 break; 546 } 547 } 548 549 return nonRowResultSetFound; 550 } 551 552 564 public ResultSetNode optimize(DataDictionary dataDictionary, 565 PredicateList predicateList, 566 double outerRows) 567 throws StandardException 568 { 569 574 Optimizer optimizer = 575 getOptimizer( 576 (FromList) getNodeFactory().getNode( 577 C_NodeTypes.FROM_LIST, 578 getNodeFactory().doJoinOrderOptimization(), 579 getContextManager()), 580 predicateList, 581 dataDictionary, 582 (RequiredRowOrdering) null); 583 costEstimate = optimizer.newCostEstimate(); 584 585 costEstimate.setCost(0.0d, outerRows, outerRows); 587 588 subquerys.optimize(dataDictionary, outerRows); 589 return this; 590 } 591 592 597 public Optimizable modifyAccessPath(JBitSet outerTables) throws StandardException 598 { 599 600 return (Optimizable) modifyAccessPaths(); 601 } 602 603 608 public ResultSetNode modifyAccessPaths() throws StandardException 609 { 610 ResultSetNode treeTop = this; 611 612 subquerys.modifyAccessPaths(); 613 614 617 if (orderByList != null) 618 { 619 treeTop = (ResultSetNode) getNodeFactory().getNode( 620 C_NodeTypes.ORDER_BY_NODE, 621 treeTop, 622 orderByList, 623 tableProperties, 624 getContextManager()); 625 } 626 return treeTop; 627 } 628 629 637 boolean returnsAtMostOneRow() 638 { 639 return true; 640 } 641 642 650 public void generate(ActivationClassBuilder acb, 651 MethodBuilder mb) 652 throws StandardException 653 { 654 if (SanityManager.DEBUG) 655 SanityManager.ASSERT(resultColumns != null, "Tree structure bad"); 656 657 costEstimate = getFinalCostEstimate(); 659 660 665 boolean canCache = canWeCacheResults(); 666 667 670 assignResultSetNumber(); 671 672 676 679 688 acb.pushGetResultSetFactoryExpression(mb); 689 690 acb.pushThisAsActivation(mb); 691 resultColumns.generate(acb, mb); 692 mb.push(canCache); 693 mb.push(resultSetNumber); 694 mb.push(costEstimate.rowCount()); 695 mb.push(costEstimate.getEstimatedCost()); 696 mb.callMethod(VMOpcode.INVOKEINTERFACE, (String ) null, "getRowResultSet", 697 ClassName.NoPutResultSet, 6); 698 } 699 700 708 void replaceDefaults(TableDescriptor ttd, ResultColumnList tcl) 709 throws StandardException 710 { 711 resultColumns.replaceDefaults(ttd, tcl); 712 } 713 714 721 void optimizeSubqueries(DataDictionary dd, double rowCount) 722 throws StandardException 723 { 724 subquerys.optimize(dd, rowCount); 725 } 726 727 732 void markOrderingDependent() 733 { 734 } 735 736 752 private boolean canWeCacheResults() throws StandardException 753 { 754 755 758 HasVariantValueNodeVisitor visitor = 759 new HasVariantValueNodeVisitor(Qualifier.QUERY_INVARIANT, true); 760 761 super.accept(visitor); 762 boolean canCache = !visitor.hasVariant(); 763 764 return canCache; 765 } 766 } 767 | Popular Tags |