1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.services.monitor.Monitor; 25 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 28 import org.apache.derby.iapi.services.stream.HeaderPrintWriter; 29 import org.apache.derby.iapi.services.stream.InfoStreams; 30 31 import org.apache.derby.iapi.error.StandardException; 32 import org.apache.derby.iapi.sql.Activation; 33 import org.apache.derby.iapi.sql.ResultSet; 34 import org.apache.derby.iapi.types.DataValueDescriptor; 35 import org.apache.derby.iapi.reference.SQLState; 36 37 import org.apache.derby.iapi.sql.execute.ExecRow; 38 import org.apache.derby.iapi.sql.execute.ExecutionContext; 39 import org.apache.derby.iapi.sql.execute.NoPutResultSet; 40 41 import org.apache.derby.iapi.services.loader.GeneratedMethod; 42 43 44 48 class NestedLoopJoinResultSet extends JoinResultSet 49 { 50 private boolean returnedRowMatchingRightSide = false; 51 private ExecRow rightTemplate; 52 53 57 68 void clearScanState() 69 { 70 returnedRowMatchingRightSide = false; 71 super.clearScanState(); 72 } 73 74 86 public ExecRow getNextRowCore() throws StandardException 87 { 88 ExecRow result = null; 89 boolean haveRow = false; 90 boolean restrict = false; 91 int colInCtr; 92 int colOutCtr; 93 DataValueDescriptor restrictBoolean; 94 95 beginTime = getCurrentTimeMillis(); 96 if (! isOpen) 97 throw StandardException.newException(SQLState.LANG_RESULT_SET_NOT_OPEN, "next"); 98 99 104 if (! isRightOpen && leftRow != null) 105 { 106 leftRow = leftResultSet.getNextRowCore(); 107 if (leftRow == null) 108 { 109 closeRight(); 110 } 111 else 112 { 113 rowsSeenLeft++; 114 openRight(); 115 } 116 } 117 118 while (leftRow != null && !haveRow) 119 { 120 if (oneRowRightSide && returnedRowMatchingRightSide) 121 { 122 rightRow = null; 123 returnedRowMatchingRightSide = false; 124 } 125 else 126 { 127 rightRow = rightResultSet.getNextRowCore(); 128 129 133 if (notExistsRightSide) 134 { 135 if (rightRow == null) rightRow = rightTemplate; else 138 rightRow = null; 139 } 140 141 returnedRowMatchingRightSide = (rightRow != null); 142 } 143 144 if (rightRow == null) 145 { 146 150 leftRow = leftResultSet.getNextRowCore(); 151 if (leftRow == null) 152 { 153 closeRight(); 154 } 155 else 156 { 157 rowsSeenLeft++; 158 openRight(); 159 } 160 } 161 else 162 { 163 rowsSeenRight++; 164 165 if (restriction != null) 166 { 167 restrictBoolean = 168 (DataValueDescriptor) restriction.invoke(activation); 169 170 restrict = (! restrictBoolean.isNull()) && 173 restrictBoolean.getBoolean(); 174 175 if (! restrict) 176 { 177 178 rowsFiltered++; 179 continue; 180 } 181 } 182 183 186 if (mergedRow == null) 187 { 188 mergedRow = getExecutionFactory().getValueRow(leftNumCols + rightNumCols); 189 } 190 191 for (colInCtr = 1, colOutCtr = 1; colInCtr <= leftNumCols; 192 colInCtr++, colOutCtr++) 193 { 194 mergedRow.setColumn(colOutCtr, 195 leftRow.getColumn(colInCtr)); 196 } 197 if (! notExistsRightSide) 198 { 199 for (colInCtr = 1; colInCtr <= rightNumCols; 200 colInCtr++, colOutCtr++) 201 { 202 mergedRow.setColumn(colOutCtr, 203 rightRow.getColumn(colInCtr)); 204 } 205 } 206 207 setCurrentRow(mergedRow); 208 haveRow = true; 209 } 210 } 211 212 213 if (haveRow) 214 { 215 result = mergedRow; 216 rowsReturned++; 217 } 218 else 219 { 220 clearCurrentRow(); 221 } 222 223 nextTime += getElapsedMillis(beginTime); 224 return result; 225 } 226 227 233 public void close() throws StandardException 234 { 235 if ( isOpen ) 236 { 237 beginTime = getCurrentTimeMillis(); 238 239 clearCurrentRow(); 244 245 super.close(); 246 returnedRowMatchingRightSide = false; 247 closeTime += getElapsedMillis(beginTime); 248 } 249 250 } 251 252 253 261 public long getTimeSpent(int type) 262 { 263 long totTime = constructorTime + openTime + nextTime + closeTime; 264 265 if (type == NoPutResultSet.CURRENT_RESULTSET_ONLY) 266 { 267 return totTime - leftResultSet.getTimeSpent(ENTIRE_RESULTSET_TREE) - 268 rightResultSet.getTimeSpent(ENTIRE_RESULTSET_TREE); 269 } 270 else 271 { 272 return totTime; 273 } 274 } 275 276 280 NestedLoopJoinResultSet(NoPutResultSet leftResultSet, 281 int leftNumCols, 282 NoPutResultSet rightResultSet, 283 int rightNumCols, 284 Activation activation, 285 GeneratedMethod restriction, 286 int resultSetNumber, 287 boolean oneRowRightSide, 288 boolean notExistsRightSide, 289 double optimizerEstimatedRowCount, 290 double optimizerEstimatedCost, 291 String userSuppliedOptimizerOverrides) 292 { 293 super(leftResultSet, leftNumCols, rightResultSet, rightNumCols, 294 activation, restriction, resultSetNumber, 295 oneRowRightSide, notExistsRightSide, optimizerEstimatedRowCount, 296 optimizerEstimatedCost, userSuppliedOptimizerOverrides); 297 if (notExistsRightSide) 298 rightTemplate = getExecutionFactory().getValueRow(rightNumCols); 299 } 300 } 301 | Popular Tags |