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.Optimizable; 27 import org.apache.derby.iapi.sql.compile.OptimizablePredicate; 28 import org.apache.derby.iapi.sql.compile.OptimizablePredicateList; 29 import org.apache.derby.iapi.sql.compile.Optimizer; 30 import org.apache.derby.iapi.sql.compile.CostEstimate; 31 import org.apache.derby.iapi.sql.compile.OptimizableList; 32 import org.apache.derby.iapi.sql.compile.Visitable; 33 import org.apache.derby.iapi.sql.compile.Visitor; 34 import org.apache.derby.iapi.sql.compile.RequiredRowOrdering; 35 import org.apache.derby.iapi.sql.compile.RowOrdering; 36 import org.apache.derby.iapi.sql.compile.AccessPath; 37 import org.apache.derby.iapi.sql.compile.C_NodeTypes; 38 39 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 40 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 41 42 import org.apache.derby.iapi.types.DataValueDescriptor; 43 44 import org.apache.derby.iapi.sql.execute.NoPutResultSet; 45 46 import org.apache.derby.iapi.sql.Activation; 47 import org.apache.derby.iapi.sql.ResultSet; 48 49 import org.apache.derby.iapi.error.StandardException; 50 import org.apache.derby.iapi.reference.ClassName; 51 52 import org.apache.derby.iapi.store.access.TransactionController; 53 54 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder; 55 import org.apache.derby.impl.sql.compile.ActivationClassBuilder; 56 57 import org.apache.derby.iapi.services.compiler.MethodBuilder; 58 59 import org.apache.derby.iapi.services.loader.GeneratedMethod; 60 61 import org.apache.derby.iapi.services.sanity.SanityManager; 62 63 import org.apache.derby.catalog.types.ReferencedColumnsDescriptorImpl; 64 import org.apache.derby.iapi.util.JBitSet; 65 import org.apache.derby.iapi.services.classfile.VMOpcode; 66 67 import java.util.Properties ; 68 import java.util.HashSet ; 69 import java.util.Set ; 70 71 82 83 public class ProjectRestrictNode extends SingleChildResultSetNode 84 { 85 88 public ValueNode restriction; 89 90 93 ValueNode constantRestriction = null; 94 95 98 public PredicateList restrictionList; 99 100 103 SubqueryList projectSubquerys; 104 105 108 SubqueryList restrictSubquerys; 109 110 private boolean accessPathModified; 111 112 private boolean accessPathConsidered; 113 114 private boolean childResultOptimized; 115 116 private boolean materialize; 117 118 121 private boolean getTableNumberHere; 122 123 135 136 public void init( 137 Object childResult, 138 Object projection, 139 Object restriction, 140 Object restrictionList, 141 Object projectSubquerys, 142 Object restrictSubquerys, 143 Object tableProperties) 144 { 145 super.init(childResult, tableProperties); 146 resultColumns = (ResultColumnList) projection; 147 this.restriction = (ValueNode) restriction; 148 this.restrictionList = (PredicateList) restrictionList; 149 this.projectSubquerys = (SubqueryList) projectSubquerys; 150 this.restrictSubquerys = (SubqueryList) restrictSubquerys; 151 152 157 if (tableProperties != null && 158 (childResult instanceof Optimizable)) 159 { 160 ((Optimizable) childResult).setProperties(getProperties()); 161 setProperties((Properties ) null); 162 } 163 } 164 165 168 169 173 public boolean nextAccessPath(Optimizer optimizer, 174 OptimizablePredicateList predList, 175 RowOrdering rowOrdering) 176 throws StandardException 177 { 178 187 if (childResult instanceof Optimizable) 188 { 189 return ((Optimizable) childResult).nextAccessPath(optimizer, 190 restrictionList, 191 rowOrdering); 192 } 193 else 194 { 195 return super.nextAccessPath(optimizer, predList, rowOrdering); 196 } 197 } 198 199 202 public void rememberAsBest(int planType, Optimizer optimizer) 203 throws StandardException 204 { 205 super.rememberAsBest(planType, optimizer); 206 if (childResult instanceof Optimizable) 207 ((Optimizable) childResult).rememberAsBest(planType, optimizer); 208 } 209 210 213 void printRememberingBestAccessPath(int planType, AccessPath bestPath) 214 { 215 } 216 217 218 public void startOptimizing(Optimizer optimizer, RowOrdering rowOrdering) 219 { 220 if (childResult instanceof Optimizable) 221 { 222 ((Optimizable) childResult).startOptimizing(optimizer, rowOrdering); 223 } 224 else 225 { 226 accessPathConsidered = false; 227 228 super.startOptimizing(optimizer, rowOrdering); 229 } 230 } 231 232 233 public int getTableNumber() 234 { 235 243 if (getTableNumberHere) 244 { 245 return super.getTableNumber(); 246 } 247 248 if (childResult instanceof Optimizable) 249 return ((Optimizable) childResult).getTableNumber(); 250 251 return super.getTableNumber(); 252 } 253 254 259 public CostEstimate optimizeIt( 260 Optimizer optimizer, 261 OptimizablePredicateList predList, 262 CostEstimate outerCost, 263 RowOrdering rowOrdering) 264 throws StandardException 265 { 266 280 281 CostEstimate childCost; 282 283 costEstimate = getCostEstimate(optimizer); 284 285 291 297 updateBestPlanMap(ADD_PLAN, this); 308 309 315 if (childResult instanceof Optimizable) 316 { 317 childCost = ((Optimizable) childResult).optimizeIt( 318 optimizer, 319 restrictionList, 320 outerCost, 321 rowOrdering); 322 323 costEstimate.setCost( 324 childCost.getEstimatedCost(), 325 childCost.rowCount(), 326 childCost.singleScanRowCount()); 327 328 329 } 339 else if ( ! accessPathModified) 340 { 341 if (SanityManager.DEBUG) 342 { 343 if (! ((childResult instanceof SelectNode) || 344 (childResult instanceof RowResultSetNode))) 345 { 346 SanityManager.THROWASSERT( 347 "childResult is expected to be instanceof " + 348 "SelectNode or RowResultSetNode - it is a " + 349 childResult.getClass().getName()); 350 } 351 } 352 childResult = childResult.optimize(optimizer.getDataDictionary(), 353 restrictionList, 354 outerCost.rowCount()); 355 356 357 childCost = childResult.costEstimate; 358 359 costEstimate.setCost( 360 childCost.getEstimatedCost(), 361 childCost.rowCount(), 362 childCost.singleScanRowCount()); 363 364 395 396 398 404 optimizer.considerCost(this, restrictionList, getCostEstimate(), outerCost); 405 } 406 407 return costEstimate; 408 } 409 410 415 public boolean feasibleJoinStrategy(OptimizablePredicateList predList, 416 Optimizer optimizer) 417 throws StandardException 418 { 419 AccessPath ap; 420 421 425 if (childResult instanceof Optimizable) 426 { 427 if (childResult instanceof UnionNode) 442 ((UnionNode)childResult).pullOptPredicates(restrictionList); 443 444 return ((Optimizable) childResult). 445 feasibleJoinStrategy(restrictionList, optimizer); 446 } 447 else 448 { 449 return super.feasibleJoinStrategy(restrictionList, optimizer); 450 } 451 } 452 453 454 public AccessPath getCurrentAccessPath() 455 { 456 if (childResult instanceof Optimizable) 457 return ((Optimizable) childResult).getCurrentAccessPath(); 458 459 return super.getCurrentAccessPath(); 460 } 461 462 463 public AccessPath getBestAccessPath() 464 { 465 if (childResult instanceof Optimizable) 466 return ((Optimizable) childResult).getBestAccessPath(); 467 468 return super.getBestAccessPath(); 469 } 470 471 472 public AccessPath getBestSortAvoidancePath() 473 { 474 if (childResult instanceof Optimizable) 475 return ((Optimizable) childResult).getBestSortAvoidancePath(); 476 477 return super.getBestSortAvoidancePath(); 478 } 479 480 481 public AccessPath getTrulyTheBestAccessPath() 482 { 483 491 if (hasTrulyTheBestAccessPath) 492 { 493 return super.getTrulyTheBestAccessPath(); 494 } 495 496 if (childResult instanceof Optimizable) 497 return ((Optimizable) childResult).getTrulyTheBestAccessPath(); 498 499 return super.getTrulyTheBestAccessPath(); 500 } 501 502 503 public void rememberSortAvoidancePath() 504 { 505 if (childResult instanceof Optimizable) 506 ((Optimizable) childResult).rememberSortAvoidancePath(); 507 else 508 super.rememberSortAvoidancePath(); 509 } 510 511 512 public boolean considerSortAvoidancePath() 513 { 514 if (childResult instanceof Optimizable) 515 return ((Optimizable) childResult).considerSortAvoidancePath(); 516 517 return super.considerSortAvoidancePath(); 518 } 519 520 525 526 public boolean pushOptPredicate(OptimizablePredicate optimizablePredicate) 527 throws StandardException 528 { 529 if (SanityManager.DEBUG) 530 { 531 SanityManager.ASSERT(optimizablePredicate instanceof Predicate, 532 "optimizablePredicate expected to be instanceof Predicate"); 533 SanityManager.ASSERT(! optimizablePredicate.hasSubquery() && 534 ! optimizablePredicate.hasMethodCall(), 535 "optimizablePredicate either has a subquery or a method call"); 536 } 537 538 539 if (restrictionList == null) 540 { 541 restrictionList = (PredicateList) getNodeFactory().getNode( 542 C_NodeTypes.PREDICATE_LIST, 543 getContextManager()); 544 } 545 restrictionList.addPredicate((Predicate) optimizablePredicate); 546 547 550 Predicate pred = (Predicate)optimizablePredicate; 551 552 556 if (!pred.remapScopedPred()) 557 { 558 RemapCRsVisitor rcrv = new RemapCRsVisitor(true); 559 pred.getAndNode().accept(rcrv); 560 } 561 562 return true; 563 } 564 565 570 public void pullOptPredicates( 571 OptimizablePredicateList optimizablePredicates) 572 throws StandardException 573 { 574 if (restrictionList != null) 575 { 576 if (childResult instanceof UnionNode) 579 ((UnionNode)childResult).pullOptPredicates(restrictionList); 580 581 RemapCRsVisitor rcrv = new RemapCRsVisitor(false); 582 for (int i = restrictionList.size() - 1; i >= 0; i--) 583 { 584 OptimizablePredicate optPred = 585 restrictionList.getOptPredicate(i); 586 ((Predicate) optPred).getAndNode().accept(rcrv); 587 optimizablePredicates.addOptPredicate(optPred); 588 restrictionList.removeOptPredicate(i); 589 } 590 } 591 } 592 593 598 public Optimizable modifyAccessPath(JBitSet outerTables) 599 throws StandardException 600 { 601 boolean origChildOptimizable = true; 602 603 610 if (accessPathModified) 611 { 612 return this; 613 } 614 615 619 boolean alreadyPushed = false; 620 if ( ! (childResult instanceof Optimizable)) 621 { 622 origChildOptimizable = false; 624 625 631 childResult = childResult.modifyAccessPaths(restrictionList); 632 633 636 hasTrulyTheBestAccessPath = true; 637 638 639 if (trulyTheBestAccessPath.getJoinStrategy().isHashJoin()) 640 { 641 if (SanityManager.DEBUG) 642 { 643 SanityManager.ASSERT(restrictionList != null, 644 "restrictionList expected to be non-null"); 645 SanityManager.ASSERT(restrictionList.size() != 0, 646 "restrictionList.size() expected to be non-zero"); 647 } 648 653 getTableNumberHere = true; 654 } 655 else 656 { 657 662 return (Optimizable) considerMaterialization(outerTables); 663 } 664 } 665 666 669 else if (!(childResult instanceof FromBaseTable)) 670 { 671 672 if (trulyTheBestAccessPath.getJoinStrategy() == null) 673 { 674 trulyTheBestAccessPath = (AccessPathImpl) ((Optimizable) childResult).getTrulyTheBestAccessPath(); 675 } 676 677 if (childResult instanceof SetOperatorNode) { 687 childResult = (ResultSetNode) 688 ((SetOperatorNode) childResult).modifyAccessPath( 689 outerTables, restrictionList); 690 691 alreadyPushed = true; 701 } 702 else { 703 childResult = 704 (ResultSetNode) ((FromTable) childResult). 705 modifyAccessPath(outerTables); 706 } 707 } 708 709 boolean hashJoinWithThisPRN = hasTrulyTheBestAccessPath && 719 (trulyTheBestAccessPath.getJoinStrategy() != null) && 720 trulyTheBestAccessPath.getJoinStrategy().isHashJoin(); 721 722 if ((restrictionList != null) && !alreadyPushed && !hashJoinWithThisPRN) 723 { 724 restrictionList.pushUsefulPredicates((Optimizable) childResult); 725 } 726 727 734 if (origChildOptimizable) 735 { 736 childResult = childResult.changeAccessPath(); 737 } 738 accessPathModified = true; 739 740 745 if (trulyTheBestAccessPath.getJoinStrategy() != null && 746 trulyTheBestAccessPath.getJoinStrategy().isHashJoin()) 747 { 748 return replaceWithHashTableNode(); 749 } 750 751 756 return (Optimizable) considerMaterialization(outerTables); 757 } 758 759 769 private Optimizable replaceWithHashTableNode() 770 throws StandardException 771 { 772 if (hasTrulyTheBestAccessPath) 779 { 780 ((FromTable)childResult).trulyTheBestAccessPath = 781 (AccessPathImpl)getTrulyTheBestAccessPath(); 782 783 if (childResult instanceof SingleChildResultSetNode) 789 { 790 ((SingleChildResultSetNode)childResult) 791 .hasTrulyTheBestAccessPath = hasTrulyTheBestAccessPath; 792 793 childResult.getReferencedTableMap().set(tableNumber); 808 } 809 } 810 811 824 PredicateList searchRestrictionList = 825 (PredicateList) getNodeFactory().getNode( 826 C_NodeTypes.PREDICATE_LIST, 827 getContextManager()); 828 PredicateList joinQualifierList = 829 (PredicateList) getNodeFactory().getNode( 830 C_NodeTypes.PREDICATE_LIST, 831 getContextManager()); 832 PredicateList requalificationRestrictionList = 833 (PredicateList) getNodeFactory().getNode( 834 C_NodeTypes.PREDICATE_LIST, 835 getContextManager()); 836 trulyTheBestAccessPath.getJoinStrategy().divideUpPredicateLists( 837 this, 838 restrictionList, 839 searchRestrictionList, 840 joinQualifierList, 841 requalificationRestrictionList, 842 getDataDictionary()); 843 844 847 restrictionList = (PredicateList) getNodeFactory().getNode( 848 C_NodeTypes.PREDICATE_LIST, 849 getContextManager()); 850 852 for (int i = 0; i < searchRestrictionList.size(); i++) 853 requalificationRestrictionList.removeOptPredicate((Predicate) searchRestrictionList.elementAt(i)); 854 for (int i = 0; i < joinQualifierList.size(); i++) 855 requalificationRestrictionList.removeOptPredicate((Predicate) joinQualifierList.elementAt(i)); 856 857 joinQualifierList.transferNonQualifiers(this, restrictionList); requalificationRestrictionList.copyPredicatesToOtherList(restrictionList); 860 ResultColumnList htRCList; 861 862 865 htRCList = childResult.getResultColumns(); 866 childResult.setResultColumns(htRCList.copyListAndObjects()); 867 868 874 htRCList.genVirtualColumnNodes(childResult, childResult.getResultColumns(), false); 875 876 882 RemapCRsVisitor rcrv = new RemapCRsVisitor(true); 883 searchRestrictionList.accept(rcrv); 884 885 886 childResult = (ResultSetNode) getNodeFactory().getNode( 887 C_NodeTypes.HASH_TABLE_NODE, 888 childResult, 889 tableProperties, 890 htRCList, 891 searchRestrictionList, 892 joinQualifierList, 893 trulyTheBestAccessPath, 894 getCostEstimate(), 895 projectSubquerys, 896 restrictSubquerys, 897 hashKeyColumns(), 898 getContextManager()); 899 return this; 900 } 901 902 905 public void verifyProperties(DataDictionary dDictionary) 906 throws StandardException 907 { 908 912 913 if (childResult instanceof Optimizable) 914 { 915 ((Optimizable) childResult).verifyProperties(dDictionary); 916 } 917 else 918 { 919 super.verifyProperties(dDictionary); 920 } 921 } 922 923 926 public boolean legalJoinOrder(JBitSet assignedTableMap) 927 { 928 if (childResult instanceof Optimizable) 929 { 930 return ((Optimizable) childResult).legalJoinOrder(assignedTableMap); 931 } 932 else 933 { 934 return true; 935 } 936 } 937 938 943 public double uniqueJoin(OptimizablePredicateList predList) 944 throws StandardException 945 { 946 if (childResult instanceof Optimizable) 947 { 948 return ((Optimizable) childResult).uniqueJoin(predList); 949 } 950 else 951 { 952 return super.uniqueJoin(predList); 953 } 954 } 955 956 961 PredicateList getRestrictionList() 962 { 963 return restrictionList; 964 } 965 966 971 String getUserSpecifiedJoinStrategy() 972 { 973 if (childResult instanceof FromTable) 974 { 975 return ((FromTable) childResult).getUserSpecifiedJoinStrategy(); 976 } 977 else 978 { 979 return userSpecifiedJoinStrategy; 980 } 981 } 982 983 989 990 public void printSubNodes(int depth) 991 { 992 if (SanityManager.DEBUG) 993 { 994 super.printSubNodes(depth); 995 996 if (restriction != null) 997 { 998 printLabel(depth, "restriction: "); 999 restriction.treePrint(depth + 1); 1000 } 1001 1002 if (restrictionList != null) 1003 { 1004 printLabel(depth, "restrictionList: "); 1005 restrictionList.treePrint(depth + 1); 1006 } 1007 1008 if (projectSubquerys != null) 1009 { 1010 printLabel(depth, "projectSubquerys: "); 1011 projectSubquerys.treePrint(depth + 1); 1012 } 1013 1014 if (restrictSubquerys != null) 1015 { 1016 printLabel(depth, "restrictSubquerys: "); 1017 restrictSubquerys.treePrint(depth + 1); 1018 } 1019 } 1020 } 1021 1022 1046 1047 public ResultSetNode preprocess(int numTables, 1048 GroupByList gbl, 1049 FromList fromList) 1050 throws StandardException 1051 { 1052 childResult = childResult.preprocess(numTables, gbl, fromList); 1053 1054 1055 referencedTableMap = (JBitSet) childResult.getReferencedTableMap().clone(); 1056 1057 return this; 1058 } 1059 1060 1071 public void pushExpressions(PredicateList predicateList) 1072 throws StandardException 1073 { 1074 PredicateList pushPList = null; 1075 1076 if (SanityManager.DEBUG) 1077 SanityManager.ASSERT(predicateList != null, 1078 "predicateList is expected to be non-null"); 1079 1080 1084 if (childResult instanceof JoinNode) 1085 { 1086 ((FromTable) childResult).pushExpressions(predicateList); 1087 1088 } 1089 1090 1091 pushPList = predicateList.getPushablePredicates(referencedTableMap); 1092 1093 1098 if (pushPList != null && (childResult instanceof SelectNode)) 1099 { 1100 pushPList.pushExpressionsIntoSelect((SelectNode) childResult, false); 1101 } 1102 1103 1111 if (pushPList != null && (childResult instanceof UnionNode)) 1112 ((UnionNode)childResult).pushExpressions(pushPList); 1113 1114 if (restrictionList == null) 1115 { 1116 restrictionList = pushPList; 1117 } 1118 else if (pushPList != null && pushPList.size() != 0) 1119 { 1120 1121 restrictionList.destructiveAppend(pushPList); 1122 } 1123 1124 1130 } 1131 1132 1143 public ResultSetNode addNewPredicate(Predicate predicate) 1144 throws StandardException 1145 { 1146 if (restrictionList == null) 1147 { 1148 restrictionList = (PredicateList) getNodeFactory().getNode( 1149 C_NodeTypes.PREDICATE_LIST, 1150 getContextManager()); 1151 } 1152 restrictionList.addPredicate(predicate); 1153 return this; 1154 } 1155 1156 1168 public boolean flattenableInFromSubquery(FromList fromList) 1169 { 1170 1173 1174 return false; 1175 } 1176 1177 1185 public ResultSetNode ensurePredicateList(int numTables) 1186 throws StandardException 1187 { 1188 return this; 1189 } 1190 1191 1203 1204 public ResultSetNode optimize(DataDictionary dataDictionary, 1205 PredicateList predicates, 1206 double outerRows) 1207 throws StandardException 1208 { 1209 1212 childResult = childResult.optimize(dataDictionary, 1213 restrictionList, 1214 outerRows); 1215 1216 Optimizer optimizer = getOptimizer( 1217 (FromList) getNodeFactory().getNode( 1218 C_NodeTypes.FROM_LIST, 1219 getNodeFactory().doJoinOrderOptimization(), 1220 this, 1221 getContextManager()), 1222 predicates, 1223 dataDictionary, 1224 (RequiredRowOrdering) null); 1225 1226 costEstimate = optimizer.newCostEstimate(); 1229 1230 costEstimate.setCost(childResult.getCostEstimate().getEstimatedCost(), 1231 childResult.getCostEstimate().rowCount(), 1232 childResult.getCostEstimate().singleScanRowCount()); 1233 1234 return this; 1235 } 1236 1237 1243 public CostEstimate getCostEstimate() 1244 { 1245 1251 if (costEstimate == null) 1252 return childResult.getCostEstimate(); 1253 else 1254 { 1255 return costEstimate; 1256 } 1257 } 1258 1259 1265 public CostEstimate getFinalCostEstimate() 1266 throws StandardException 1267 { 1268 if (finalCostEstimate != null) 1269 return finalCostEstimate; 1271 1272 if (childResult instanceof Optimizable) 1277 finalCostEstimate = childResult.getFinalCostEstimate(); 1278 else 1279 finalCostEstimate = getTrulyTheBestAccessPath().getCostEstimate(); 1280 1281 return finalCostEstimate; 1282 } 1283 1284 1296 public void generate(ActivationClassBuilder acb, 1297 MethodBuilder mb) 1298 throws StandardException 1299 { 1300 if (SanityManager.DEBUG) 1301 SanityManager.ASSERT(resultColumns != null, "Tree structure bad"); 1302 1303 generateMinion( acb, mb, false); 1304 } 1305 1306 1315 1316 public void generateResultSet(ExpressionClassBuilder acb, 1317 MethodBuilder mb) 1318 throws StandardException 1319 { 1320 generateMinion( acb, mb, true); 1321 } 1322 1323 1331 1332 private void generateMinion(ExpressionClassBuilder acb, 1333 MethodBuilder mb, boolean genChildResultSet) 1334 throws StandardException 1335 { 1336 1337 1341 if (restrictionList != null && restrictionList.size() > 0) 1342 { 1343 restrictionList.eliminateBooleanTrueAndBooleanTrue(); 1344 } 1345 1346 if (nopProjectRestrict()) 1347 { 1348 generateNOPProjectRestrict(); 1349 if (genChildResultSet) 1350 childResult.generateResultSet(acb, mb); 1351 else 1352 childResult.generate((ActivationClassBuilder)acb, mb); 1353 costEstimate = childResult.getFinalCostEstimate(); 1354 return; 1355 } 1356 1357 1359 1360 if (restrictionList != null) 1361 { 1362 constantRestriction = restrictionList.restoreConstantPredicates(); 1363 restrictionList.removeRedundantPredicates(); 1365 restriction = restrictionList.restorePredicates(); 1366 1369 restrictionList = null; 1370 } 1371 1372 1378 1386 1387 1388 1389 int[] mapArray = resultColumns.mapSourceColumns(); 1391 int mapArrayItem = acb.addItem(new ReferencedColumnsDescriptorImpl(mapArray)); 1392 1393 1394 boolean doesProjection = true; 1395 1396 1399 if ( (! reflectionNeededForProjection()) && 1400 mapArray != null && 1401 mapArray.length == childResult.getResultColumns().size()) 1402 { 1403 1404 int index = 0; 1405 for ( ; index < mapArray.length; index++) 1406 { 1407 if (mapArray[index] != index + 1) 1408 { 1409 break; 1410 } 1411 } 1412 if (index == mapArray.length) 1413 { 1414 doesProjection = false; 1415 } 1416 } 1417 1418 1419 1420 1436 1437 acb.pushGetResultSetFactoryExpression(mb); 1438 if (genChildResultSet) 1439 childResult.generateResultSet(acb, mb); 1440 else 1441 childResult.generate((ActivationClassBuilder)acb, mb); 1442 1443 1446 assignResultSetNumber(); 1447 1448 1451 if (projectSubquerys != null && projectSubquerys.size() > 0) 1452 { 1453 projectSubquerys.setPointOfAttachment(resultSetNumber); 1454 } 1455 if (restrictSubquerys != null && restrictSubquerys.size() > 0) 1456 { 1457 restrictSubquerys.setPointOfAttachment(resultSetNumber); 1458 } 1459 1460 costEstimate = getFinalCostEstimate(); 1462 1463 if (restriction == null) 1465 { 1466 mb.pushNull(ClassName.GeneratedMethod); 1467 } 1468 else 1469 { 1470 MethodBuilder userExprFun = acb.newUserExprFun(); 1474 1475 1477 1485 restriction.generateExpression(acb, userExprFun); 1486 userExprFun.methodReturn(); 1487 1488 userExprFun.complete(); 1490 1491 acb.pushMethodReference(mb, userExprFun); 1498 } 1499 1500 1504 if (reflectionNeededForProjection()) 1505 { 1506 1515 1516 resultColumns.generateCore(acb, mb, false); 1517 } 1518 else 1519 { 1520 mb.pushNull(ClassName.GeneratedMethod); 1521 } 1522 1523 mb.push(resultSetNumber); 1524 1525 if (constantRestriction == null) 1527 { 1528 mb.pushNull(ClassName.GeneratedMethod); 1529 } 1530 else 1531 { 1532 MethodBuilder userExprFun = acb.newUserExprFun(); 1536 1537 1539 1547 constantRestriction.generateExpression(acb, userExprFun); 1548 1549 userExprFun.methodReturn(); 1550 1551 userExprFun.complete(); 1553 1554 acb.pushMethodReference(mb, userExprFun); 1561 } 1562 1563 mb.push(mapArrayItem); 1564 mb.push(resultColumns.reusableResult()); 1565 mb.push(doesProjection); 1566 mb.push(costEstimate.rowCount()); 1567 mb.push(costEstimate.getEstimatedCost()); 1568 1569 mb.callMethod(VMOpcode.INVOKEINTERFACE, (String ) null, "getProjectRestrictResultSet", 1570 ClassName.NoPutResultSet, 10); 1571 } 1572 1573 1579 boolean nopProjectRestrict() 1580 { 1581 1585 if ( (restriction != null) || 1586 (restrictionList != null && restrictionList.size() > 0) ) 1587 { 1588 return false; 1589 } 1590 1591 ResultColumnList childColumns = childResult.getResultColumns(); 1592 ResultColumnList PRNColumns = this.getResultColumns(); 1593 1594 1600 if (PRNColumns.nopProjection(childColumns)) 1601 return true; 1602 1603 return false; 1604 } 1605 1606 1612 public void generateNOPProjectRestrict() 1613 throws StandardException 1614 { 1615 this.getResultColumns().setRedundant(); 1616 } 1617 1618 1626 public ResultSetNode considerMaterialization(JBitSet outerTables) 1627 throws StandardException 1628 { 1629 childResult = childResult.considerMaterialization(outerTables); 1630 if (childResult.performMaterialization(outerTables)) 1631 { 1632 MaterializeResultSetNode mrsn; 1633 ResultColumnList prRCList; 1634 1635 1648 ReferencedTablesVisitor rtv = new ReferencedTablesVisitor( 1649 (JBitSet) childResult.getReferencedTableMap().clone()); 1650 boolean emptyRestrictionList = (restrictionList == null || restrictionList.size() == 0); 1651 if (! emptyRestrictionList) 1652 { 1653 restrictionList.accept(rtv); 1654 } 1655 if (emptyRestrictionList || 1656 childResult.getReferencedTableMap().contains(rtv.getTableMap())) 1657 { 1658 1661 prRCList = resultColumns; 1662 setResultColumns(resultColumns.copyListAndObjects()); 1663 1664 1668 prRCList.genVirtualColumnNodes(this, resultColumns); 1669 1670 1671 mrsn = (MaterializeResultSetNode) getNodeFactory().getNode( 1672 C_NodeTypes.MATERIALIZE_RESULT_SET_NODE, 1673 this, 1674 prRCList, 1675 tableProperties, 1676 getContextManager()); 1677 if (referencedTableMap != null) 1679 { 1680 mrsn.setReferencedTableMap((JBitSet) referencedTableMap.clone()); 1681 } 1682 return mrsn; 1683 } 1684 else 1685 { 1686 1689 prRCList = childResult.getResultColumns(); 1690 childResult.setResultColumns(prRCList.copyListAndObjects()); 1691 1692 1696 prRCList.genVirtualColumnNodes(childResult, childResult.getResultColumns()); 1697 1698 1701 1702 1703 mrsn = (MaterializeResultSetNode) getNodeFactory().getNode( 1704 C_NodeTypes.MATERIALIZE_RESULT_SET_NODE, 1705 childResult, 1706 prRCList, 1707 tableProperties, 1708 getContextManager()); 1709 if (childResult.getReferencedTableMap() != null) 1711 { 1712 mrsn.setReferencedTableMap((JBitSet) childResult.getReferencedTableMap().clone()); 1713 } 1714 childResult = mrsn; 1715 } 1716 } 1717 1718 return this; 1719 } 1720 1721 1734 protected FromTable getFromTableByName(String name, String schemaName, boolean exactMatch) 1735 throws StandardException 1736 { 1737 return childResult.getFromTableByName(name, schemaName, exactMatch); 1738 } 1739 1740 1747 public int updateTargetLockMode() 1748 { 1749 if (restriction != null || constantRestriction != null) 1750 { 1751 return TransactionController.MODE_RECORD; 1752 } 1753 else 1754 { 1755 return childResult.updateTargetLockMode(); 1756 } 1757 } 1758 1759 1766 boolean isPossibleDistinctScan(Set distinctColumns) 1767 { 1768 if (restriction != null || 1769 (restrictionList != null && restrictionList.size() != 0)) 1770 { 1771 return false; 1772 } 1773 1774 HashSet columns = new HashSet (); 1775 for (int i = 0; i < resultColumns.size(); i++) { 1776 ResultColumn rc = (ResultColumn) resultColumns.elementAt(i); 1777 BaseColumnNode bc = rc.getBaseColumnNode(); 1778 if (bc == null) return false; 1779 columns.add(bc); 1780 } 1781 1782 return columns.equals(distinctColumns) && childResult.isPossibleDistinctScan(distinctColumns); 1783 } 1784 1785 1788 void markForDistinctScan() 1789 { 1790 childResult.markForDistinctScan(); 1791 } 1792 1793 1794 1802 public Visitable accept(Visitor v) 1803 throws StandardException 1804 { 1805 if (v.skipChildren(this)) 1806 { 1807 return v.visit(this); 1808 } 1809 1810 Visitable returnNode = super.accept(v); 1811 1812 if (restriction != null && !v.stopTraversal()) 1813 { 1814 restriction = (ValueNode)restriction.accept(v); 1815 } 1816 1817 if (restrictionList != null && !v.stopTraversal()) 1818 { 1819 restrictionList = (PredicateList)restrictionList.accept(v); 1820 } 1821 1822 return returnNode; 1823 } 1824 1825 1826 1827 1831 public void setRefActionInfo(long fkIndexConglomId, 1832 int[]fkColArray, 1833 String parentResultSetId, 1834 boolean dependentScan) 1835 { 1836 childResult.setRefActionInfo(fkIndexConglomId, 1837 fkColArray, 1838 parentResultSetId, 1839 dependentScan); 1840 } 1841 1842} 1843 | Popular Tags |