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.services.io.Formatable; 32 33 import org.apache.derby.iapi.sql.execute.CursorResultSet; 34 import org.apache.derby.iapi.sql.ResultSet; 35 import org.apache.derby.iapi.sql.execute.ExecRow; 36 import org.apache.derby.iapi.sql.execute.ExecIndexRow; 37 import org.apache.derby.iapi.sql.execute.NoPutResultSet; 38 39 import org.apache.derby.iapi.sql.Activation; 40 41 import org.apache.derby.iapi.store.access.ColumnOrdering; 42 import org.apache.derby.iapi.store.access.TransactionController; 43 44 import org.apache.derby.iapi.services.loader.GeneratedMethod; 45 46 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 47 48 import org.apache.derby.iapi.error.StandardException; 49 50 import org.apache.derby.iapi.types.RowLocation; 51 52 import org.apache.derby.iapi.services.io.FormatableArrayHolder; 53 54 import java.util.Properties ; 55 import java.util.Vector ; 56 import java.util.Enumeration ; 57 58 66 class ScalarAggregateResultSet extends GenericAggregateResultSet 67 implements CursorResultSet 68 { 69 70 71 public int rowsInput; 72 73 public boolean singleInputRow; 76 protected ExecIndexRow sortTemplateRow; 77 protected boolean isInSortedOrder; 79 protected ExecIndexRow sourceExecIndexRow; 81 82 private boolean nextSatisfied; 84 85 100 ScalarAggregateResultSet(NoPutResultSet s, 101 boolean isInSortedOrder, 102 int aggregateItem, 103 Activation a, 104 GeneratedMethod ra, 105 int resultSetNumber, 106 boolean singleInputRow, 107 double optimizerEstimatedRowCount, 108 double optimizerEstimatedCost) throws StandardException 109 { 110 super(s, aggregateItem, a, ra, resultSetNumber, optimizerEstimatedRowCount, optimizerEstimatedCost); 111 this.isInSortedOrder = isInSortedOrder; 112 if (SanityManager.DEBUG) 115 { 116 SanityManager.ASSERT(source != null, 117 "SARS(), source expected to be non-null"); 118 } 119 sortTemplateRow = getExecutionFactory().getIndexableRow((ExecRow) rowAllocator.invoke(activation)); 120 this.singleInputRow = singleInputRow; 121 122 if (SanityManager.DEBUG) 123 { 124 SanityManager.DEBUG("AggregateTrace","execution time: "+ 125 a.getPreparedStatement().getSavedObject(aggregateItem)); 126 } 127 constructorTime += getElapsedMillis(beginTime); 128 } 129 130 131 137 143 public void openCore() throws StandardException 144 { 145 beginTime = getCurrentTimeMillis(); 146 147 if (SanityManager.DEBUG) 150 { 151 SanityManager.ASSERT(source != null, 152 "SARS.openCore(), source expected to be non-null"); 153 SanityManager.ASSERT( ! isOpen, "ScalarAggregateResultSet already open"); 154 } 155 156 sourceExecIndexRow = getExecutionFactory().getIndexableRow(sortTemplateRow); 157 158 source.openCore(); 159 160 isOpen = true; 161 numOpens++; 162 163 openTime += getElapsedMillis(beginTime); 164 } 165 166 167 protected int countOfRows; 168 169 174 182 public ExecRow getNextRowCore() throws StandardException 183 { 184 if (nextSatisfied) 185 { 186 clearCurrentRow(); 187 return null; 188 } 189 190 ExecIndexRow sortResult = null; 191 ExecRow result = null; 192 ExecIndexRow execIndexRow = null; 193 ExecIndexRow aggResult = null; 194 boolean minAgg = (singleInputRow && aggregates[0].aggInfo.aggregateName.equals("MIN")); 197 beginTime = getCurrentTimeMillis(); 198 if (isOpen) 199 { 200 206 while ((execIndexRow = getRowFromResultSet(false)) != null) 207 { 208 215 if (aggResult == null) 216 { 217 221 aggResult = (singleInputRow && minAgg) ? 222 execIndexRow : 223 (ExecIndexRow) execIndexRow.getClone(); 224 225 initializeScalarAggregation(aggResult); 226 } 227 else 228 { 229 accumulateScalarAggregation(execIndexRow, aggResult, false); 230 } 231 232 239 if (singleInputRow && 240 (minAgg || 241 !aggResult.getColumn(aggregates[0].aggregatorColumnId).isNull())) 242 { 243 break; 244 } 245 } 246 247 254 if (countOfRows == 0) 255 { 256 aggResult = finishAggregation(aggResult); 257 currentRow = aggResult; 258 setCurrentRow(aggResult); 259 countOfRows++; 260 } 261 } 262 263 nextSatisfied = true; 264 nextTime += getElapsedMillis(beginTime); 265 return aggResult; 266 } 267 268 274 public void close() throws StandardException 275 { 276 beginTime = getCurrentTimeMillis(); 277 if ( isOpen ) 278 { 279 clearCurrentRow(); 284 285 countOfRows = 0; 286 sourceExecIndexRow = null; 287 source.close(); 288 289 super.close(); 290 } 291 else 292 if (SanityManager.DEBUG) 293 SanityManager.DEBUG("CloseRepeatInfo","Close of SortResultSet repeated"); 294 295 closeTime += getElapsedMillis(beginTime); 296 297 nextSatisfied = false; 298 isOpen = false; 299 } 300 301 309 public long getTimeSpent(int type) 310 { 311 long totTime = constructorTime + openTime + nextTime + 312 closeTime; 313 314 if (type == NoPutResultSet.CURRENT_RESULTSET_ONLY) 315 { 316 return totTime - originalSource.getTimeSpent(ENTIRE_RESULTSET_TREE); 317 } 318 else 319 { 320 return totTime; 321 } 322 } 323 324 330 339 public RowLocation getRowLocation() throws StandardException 340 { 341 return null; 342 } 343 344 353 356 public ExecRow getCurrentRow() throws StandardException 357 { 358 if (SanityManager.DEBUG) 359 SanityManager.ASSERT(isOpen, "SortResultSet expected to be open"); 360 361 return currentRow; 362 } 363 364 370 377 public ExecIndexRow getRowFromResultSet(boolean doClone) 378 throws StandardException 379 { 380 ExecRow sourceRow; 381 ExecIndexRow inputRow = null; 382 383 if ((sourceRow = source.getNextRowCore()) != null) 384 { 385 rowsInput++; 386 sourceExecIndexRow.execRowToExecIndexRow( 387 doClone ? sourceRow.getClone() : sourceRow); 388 inputRow = sourceExecIndexRow; 389 } 390 391 return inputRow; 392 } 393 394 401 public void reopenCore() throws StandardException 402 { 403 beginTime = getCurrentTimeMillis(); 404 if (SanityManager.DEBUG) 405 SanityManager.ASSERT(isOpen, "NormalizeResultSet already open"); 406 407 source.reopenCore(); 408 numOpens++; 409 countOfRows = 0; 410 nextSatisfied = false; 411 412 openTime += getElapsedMillis(beginTime); 413 } 414 415 421 439 protected void accumulateScalarAggregation 440 ( 441 ExecRow inputRow, 442 ExecRow accumulateRow, 443 boolean hasDistinctAggregates 444 ) 445 throws StandardException 446 { 447 int size = aggregates.length; 448 449 if (SanityManager.DEBUG) 450 { 451 SanityManager.ASSERT((inputRow != null) && (accumulateRow != null), 452 "Null row passed to accumulateScalarAggregation"); 453 } 454 for (int i = 0; i < size; i++) 455 { 456 GenericAggregator currAggregate = aggregates[i]; 457 if (hasDistinctAggregates && 458 !currAggregate.getAggregatorInfo().isDistinct()) 459 { 460 currAggregate.merge(inputRow, accumulateRow); 461 } 462 else 463 { 464 currAggregate.accumulate(inputRow, accumulateRow); 465 } 466 } 467 } 468 469 475 485 private void initializeScalarAggregation(ExecRow row) 486 throws StandardException 487 { 488 int size = aggregates.length; 489 490 if (SanityManager.DEBUG) 491 { 492 SanityManager.ASSERT(row != null, 493 "Null row passed to initializeScalarAggregation"); 494 } 495 496 for (int i = 0; i < size; i++) 497 { 498 GenericAggregator currAggregate = aggregates[i]; 499 currAggregate.initialize(row); 500 currAggregate.accumulate(row, row); 501 } 502 } 503 } 504 | Popular Tags |