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.NoPutResultSet; 39 40 import org.apache.derby.iapi.services.loader.GeneratedMethod; 41 42 47 52 class MergeJoinResultSet extends JoinResultSet 53 { 54 private static final int GREATER_THAN = 1; 55 private static final int EQUAL = 0; 56 private static final int LESS_THAN = -1; 57 58 private GeneratedMethod leftGreaterThanRight; 59 60 79 MergeJoinResultSet(NoPutResultSet leftResultSet, 80 int leftNumCols, 81 NoPutResultSet rightResultSet, 82 int rightNumCols, 83 Activation activation, 84 GeneratedMethod leftGreaterThanRight, 85 GeneratedMethod restriction, 86 int resultSetNumber, 87 boolean oneRowRightSide, 88 boolean notExistsRightSide, 89 double optimizerEstimatedRowCount, 90 double optimizerEstimatedCost) 91 { 92 super(leftResultSet, leftNumCols, rightResultSet, rightNumCols, 93 activation, restriction, resultSetNumber, 94 oneRowRightSide, notExistsRightSide, optimizerEstimatedRowCount, 95 optimizerEstimatedCost, null); 96 97 this.leftGreaterThanRight = leftGreaterThanRight; 98 } 99 100 117 public ExecRow getNextRowCore() throws StandardException 118 { 119 beginTime = getCurrentTimeMillis(); 120 if (! isOpen) 121 throw StandardException.newException(SQLState.LANG_RESULT_SET_NOT_OPEN, "next"); 122 123 if (!isRightOpen) 124 { 125 openRight(); 126 } 127 128 int compareResult; 129 130 133 while (leftRow != null) 134 { 135 139 while ((compareResult = ((Integer )leftGreaterThanRight.invoke(activation)).intValue()) 140 == GREATER_THAN) 141 { 142 rightRow = rightResultSet.getNextRowCore(); 143 rowsSeenRight++; 144 145 149 if (rightRow == null) 150 { 151 clearCurrentRow(); 152 return (ExecRow)null; 153 } 154 } 155 156 160 if ((compareResult == EQUAL) && restrictionIsTrue()) 161 { 162 ExecRow returnRow = getReturnRow(leftRow, rightRow); 163 164 168 leftRow = leftResultSet.getNextRowCore(); 169 170 return returnRow; 171 } 172 173 176 leftRow = leftResultSet.getNextRowCore(); 177 rowsSeenLeft++; 178 } 179 180 clearCurrentRow(); 181 return (ExecRow)null; 182 } 183 184 190 public void close() throws StandardException 191 { 192 beginTime = getCurrentTimeMillis(); 193 if (SanityManager.DEBUG) 194 SanityManager.ASSERT( isOpen, "MergeJoinResultSet not open"); 195 196 if ( isOpen ) 197 { 198 199 clearCurrentRow(); 204 205 super.close(); 206 } 207 208 closeTime += getElapsedMillis(beginTime); 209 } 210 211 219 public long getTimeSpent(int type) 220 { 221 long totTime = constructorTime + openTime + nextTime + closeTime; 222 223 if (type == NoPutResultSet.CURRENT_RESULTSET_ONLY) 224 { 225 return totTime - leftResultSet.getTimeSpent(ENTIRE_RESULTSET_TREE) - 226 rightResultSet.getTimeSpent(ENTIRE_RESULTSET_TREE); 227 } 228 else 229 { 230 return totTime; 231 } 232 } 233 234 235 private ExecRow getReturnRow(ExecRow leftRow, ExecRow rightRow) 241 throws StandardException 242 { 243 int colInCtr; 244 int colOutCtr; 245 246 249 if (mergedRow == null) 250 { 251 mergedRow = getExecutionFactory().getValueRow(leftNumCols + rightNumCols); 252 } 253 254 for (colInCtr = 1, colOutCtr = 1; colInCtr <= leftNumCols; 255 colInCtr++, colOutCtr++) 256 { 257 mergedRow.setColumn(colOutCtr, 258 leftRow.getColumn(colInCtr)); 259 } 260 261 for (colInCtr = 1; colInCtr <= rightNumCols; 262 colInCtr++, colOutCtr++) 263 { 264 mergedRow.setColumn(colOutCtr, 265 rightRow.getColumn(colInCtr)); 266 } 267 268 setCurrentRow(mergedRow); 269 rowsReturned++; 270 nextTime += getElapsedMillis(beginTime); 271 272 return mergedRow; 273 } 274 275 private boolean restrictionIsTrue() 276 throws StandardException 277 { 278 if (restriction != null) 279 { 280 DataValueDescriptor restrictBoolean = 281 (DataValueDescriptor) restriction.invoke(activation); 282 283 287 if (restrictBoolean.isNull() || 288 !restrictBoolean.getBoolean()) 289 { 290 291 rowsFiltered++; 292 return false; 293 } 294 } 295 return true; 296 } 297 298 } 299 | Popular Tags |