1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import org.apache.derby.iapi.sql.execute.ExecRow; 27 import org.apache.derby.iapi.sql.execute.ExecIndexRow; 28 import org.apache.derby.iapi.sql.execute.NoPutResultSet; 29 30 import org.apache.derby.iapi.sql.Activation; 31 32 import org.apache.derby.iapi.store.access.ColumnOrdering; 33 import org.apache.derby.iapi.store.access.SortObserver; 34 import org.apache.derby.iapi.store.access.TransactionController; 35 import org.apache.derby.iapi.store.access.SortController; 36 import org.apache.derby.iapi.store.access.ScanController; 37 38 import org.apache.derby.iapi.services.loader.GeneratedMethod; 39 40 41 import org.apache.derby.iapi.error.StandardException; 42 43 import org.apache.derby.iapi.services.io.FormatableArrayHolder; 44 45 import java.util.Properties ; 46 47 48 57 class DistinctScalarAggregateResultSet extends ScalarAggregateResultSet 58 { 59 private ColumnOrdering[] order; 60 private int maxRowSize; 61 private boolean dropDistinctAggSort; 62 private long sortId; 63 64 private ScanController scanController; 66 67 private ExecIndexRow sortResultRow; 68 69 private boolean sorted; 71 72 87 DistinctScalarAggregateResultSet(NoPutResultSet s, 88 boolean isInSortedOrder, 89 int aggregateItem, 90 int orderingItem, 91 Activation a, 92 GeneratedMethod ra, 93 int maxRowSize, 94 int resultSetNumber, 95 boolean singleInputRow, 96 double optimizerEstimatedRowCount, 97 double optimizerEstimatedCost) throws StandardException 98 { 99 super(s, isInSortedOrder, aggregateItem, a, ra, 100 resultSetNumber, 101 singleInputRow, 102 optimizerEstimatedRowCount, 103 optimizerEstimatedCost); 104 105 order = (ColumnOrdering[]) 106 ((FormatableArrayHolder) 107 (a.getPreparedStatement().getSavedObject(orderingItem))) 108 .getArray(ColumnOrdering.class); 109 110 this.maxRowSize = maxRowSize; 111 112 constructorTime += getElapsedMillis(beginTime); 113 } 114 115 116 122 128 public void openCore() throws StandardException 129 { 130 beginTime = getCurrentTimeMillis(); 131 if (SanityManager.DEBUG) 135 SanityManager.ASSERT( ! isOpen, "DistinctScalarResultSet already open"); 136 137 sortResultRow = getExecutionFactory().getIndexableRow(sortTemplateRow.getClone()); 138 sourceExecIndexRow = getExecutionFactory().getIndexableRow(sortTemplateRow.getClone()); 139 140 source.openCore(); 141 142 145 scanController = loadSorter(); 146 sorted = true; 147 148 isOpen = true; 149 numOpens++; 150 151 openTime += getElapsedMillis(beginTime); 152 } 153 154 158 166 public ExecRow getNextRowCore() throws StandardException 167 { 168 ExecIndexRow sortResult = null; 169 ExecRow result = null; 170 ExecIndexRow execIndexRow = null; 171 ExecIndexRow aggResult = null; 172 boolean cloneArg = true; 173 174 beginTime = getCurrentTimeMillis(); 175 if (isOpen) 176 { 177 183 while ((execIndexRow = getRowFromResultSet(cloneArg)) != null) 184 { 185 192 if (aggResult == null) 193 { 194 cloneArg = false; 195 aggResult = (ExecIndexRow) execIndexRow.getClone(); 196 } 197 else 198 { 199 204 accumulateScalarAggregation(execIndexRow, aggResult, true); 205 } 206 } 207 208 215 if (countOfRows == 0) 216 { 217 aggResult = finishAggregation(aggResult); 218 currentRow = aggResult; 219 setCurrentRow(aggResult); 220 countOfRows++; 221 } 222 } 223 224 nextTime += getElapsedMillis(beginTime); 225 return aggResult; 226 } 227 228 235 public void reopenCore() throws StandardException 236 { 237 beginTime = getCurrentTimeMillis(); 238 if (SanityManager.DEBUG) 239 SanityManager.ASSERT(isOpen, "NormalizeResultSet already open"); 240 241 if (scanController != null) 242 { 243 scanController.close(); 244 scanController = null; 245 } 246 247 source.reopenCore(); 248 249 252 scanController = loadSorter(); 253 sorted = true; 254 numOpens++; 255 countOfRows = 0; 256 257 openTime += getElapsedMillis(beginTime); 258 } 259 260 264 public void close() throws StandardException 265 { 266 super.close(); 267 closeSource(); 268 } 269 275 281 public ExecIndexRow getRowFromResultSet(boolean doClone) 282 throws StandardException 283 { 284 ExecIndexRow inputRow = null; 285 286 if (scanController.next()) 287 { 288 currentRow = doClone ? 292 sortResultRow.getClone() : sortResultRow; 293 294 inputRow = getExecutionFactory().getIndexableRow(currentRow); 295 296 scanController.fetch(inputRow.getRowArray()); 297 } 298 return inputRow; 299 } 300 301 306 protected void closeSource() throws StandardException 307 { 308 if (scanController != null) 309 { 310 if (dropDistinctAggSort) 311 { 312 try 313 { 314 getTransactionController().dropSort(sortId); 315 } 316 catch (StandardException se) 317 { 318 } 320 dropDistinctAggSort = false; 321 } 322 scanController.close(); 323 scanController = null; 324 } 325 source.close(); 326 } 327 328 334 345 private ScanController loadSorter() 346 throws StandardException 347 { 348 SortController sorter; 349 ExecRow sourceRow; 350 int inputRowCountEstimate = (int) optimizerEstimatedRowCount; 351 352 TransactionController tc = getTransactionController(); 353 354 361 GenericAggregator[] aggsNoDistinct = getSortAggregators(aggInfoList, true, 362 activation.getLanguageConnectionContext(), source); 363 SortObserver sortObserver = new AggregateSortObserver(true, aggsNoDistinct, aggregates, 364 sortTemplateRow); 365 366 sortId = tc.createSort((Properties )null, 367 sortTemplateRow.getRowArray(), 368 order, 369 sortObserver, 370 false, inputRowCountEstimate, maxRowSize ); 374 sorter = tc.openSort(sortId); 375 dropDistinctAggSort = true; 376 377 while ((sourceRow = source.getNextRowCore())!=null) 378 { 379 sorter.insert(sourceRow.getRowArray()); 380 rowsInput++; 381 } 382 383 386 sorter.close(); 387 388 scanController = 389 tc.openSortScan(sortId, activation.getResultSetHoldability()); 390 391 395 inputRowCountEstimate = rowsInput; 396 397 return scanController; 398 } 399 400 } 401 | Popular Tags |