org.apache.derby.impl.sql.execute
Class GroupedAggregateResultSet

java.lang.Object
  extended by org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl
      extended by org.apache.derby.impl.sql.execute.NoPutResultSetImpl
          extended by org.apache.derby.impl.sql.execute.GenericAggregateResultSet
              extended by org.apache.derby.impl.sql.execute.GroupedAggregateResultSet
All Implemented Interfaces:
CursorResultSet, NoPutResultSet, ResultSet, RowLocationRetRowSource, RowSource
Direct Known Subclasses:
DistinctGroupedAggregateResultSet

 class GroupedAggregateResultSet
extends GenericAggregateResultSet
implements CursorResultSet

This ResultSet evaluates grouped, non distinct aggregates. It will scan the entire source result set and calculate the grouped aggregates when scanning the source during the first call to next(). The implementation is capable of computing multiple levels of grouping in a single result set (this is requested using GROUP BY ROLLUP). This implementation has 3 variations, which it chooses according to the following rules: - If the data are guaranteed to arrive already in sorted order, we make a single pass over the data, computing the aggregates in-line as the data are read. - If the statement requests either multiple ROLLUP levels, or a DISTINCT grouping, then the data are first sorted, then we make a single pass over the data as above. - Otherwise, the data are sorted, and a SortObserver is used to compute the aggregations inside the sort, and the results are read back directly from the sorter. Note that, as of the introduction of the ROLLUP support, we no longer ALWAYS compute the aggregates using a SortObserver, which is an arrangement by which the sorter calls back into the aggregates during the sort process each time it consolidates two rows with the same sort key. Using aggregate sort observers is an efficient technique, but it was complex to extend it to the ROLLUP case, so to simplify the code we just have one path for both already-sorted and un-sorted data sources in the ROLLUP case.


Field Summary
private  java.util.HashSet[][] distinctValues
           
private  java.util.List finishedResults
           
private  long genericSortId
           
 boolean hasDistinctAggregate
           
 boolean isInSortedOrder
           
private  int maxRowSize
           
private  ColumnOrdering[] order
           
private  ExecIndexRow[] resultRows
           
private  boolean resultsComplete
           
private  boolean rollup
           
 int rowsInput
           
 int rowsReturned
           
private  ScanController scanController
           
 java.util.Properties sortProperties
           
private  ExecIndexRow sortResultRow
           
private  ExecIndexRow sortTemplateRow
           
private  ExecIndexRow sourceExecIndexRow
           
private  TransactionController tc
           
private  boolean usingAggregateObserver
           
 
Fields inherited from class org.apache.derby.impl.sql.execute.GenericAggregateResultSet
aggInfoList, aggregates, originalSource, rowAllocator, source
 
Fields inherited from class org.apache.derby.impl.sql.execute.NoPutResultSetImpl
checkNullCols, clonedExecRow, cncLen, resultSetNumber, targetResultSet
 
Fields inherited from class org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl
activation, beginTime, closeTime, compactRow, constructorTime, currentRow, endExecutionTime, finished, isOpen, isTopResultSet, nextTime, numOpens, openTime, optimizerEstimatedCost, optimizerEstimatedRowCount, resultDescription, rowsFiltered, rowsSeen, startExecutionTime, subqueryTrackingArray
 
Fields inherited from interface org.apache.derby.iapi.sql.ResultSet
CURRENT_RESULTSET_ONLY, ENTIRE_RESULTSET_TREE, ISAFTERLAST, ISBEFOREFIRST, ISFIRST, ISLAST
 
Fields inherited from interface org.apache.derby.iapi.sql.execute.NoPutResultSet
ABSOLUTE, FIRST, LAST, NEXT, PREVIOUS, RELATIVE
 
Constructor Summary
GroupedAggregateResultSet(NoPutResultSet s, boolean isInSortedOrder, int aggregateItem, int orderingItem, Activation a, GeneratedMethod ra, int maxRowSize, int resultSetNumber, double optimizerEstimatedRowCount, double optimizerEstimatedCost, boolean isRollup)
          Constructor
 
Method Summary
 void close()
          If the result set has been opened, close the open scan.
 void closeSource()
          Close the source of whatever we have been scanning.
private  void dumpAllRows(int cR)
           
private  java.lang.String dumpRow(ExecRow r)
           
private  ExecRow finalizeResults()
           
 ExecRow getCurrentRow()
          This result set has its row from the last fetch done.
 ExecRow getNextRowCore()
          Return the next row.
private  ExecIndexRow getNextRowFromRS()
          Get the next output row for processing
private  ExecIndexRow getRowFromResultSet()
          Get a row from the input result set.
private  ExecIndexRow getRowFromSorter()
          Get a row from the sorter.
 RowLocation getRowLocation()
          This result set has its row location from the last fetch done.
 long getTimeSpent(int type)
          Return the total amount of time spent in this ResultSet
private  void initializeDistinctMaps(int r, boolean allocate)
           
private  void initializeVectorAggregation(ExecRow row)
          Run the aggregator initialization method for each aggregator in the row.
private  ScanController loadSorter()
          Load up the sorter.
private  ExecRow makeCurrent(java.lang.Object row)
           
private  void mergeVectorAggregates(ExecRow newRow, ExecRow currRow, int level)
          Run the aggregator merge method for each aggregator in the row.
 void openCore()
          Open the scan.
private  int sameGroupingValues(ExecRow currRow, ExecRow newRow)
          Return whether or not the new row has the same values for the grouping columns as the current row.
private  void setRollupColumnsToNull(ExecRow row, int resultNum)
           
 
Methods inherited from class org.apache.derby.impl.sql.execute.GenericAggregateResultSet
finish, finishAggregation, getSortAggregators
 
Methods inherited from class org.apache.derby.impl.sql.execute.NoPutResultSetImpl
clearCurrentRow, clearOrderableCache, closeRowSource, getCursorName, getNextRowFromRowSource, getResultDescription, getValidColumns, isForUpdate, markRowAsDeleted, needsRowLocation, needsToClone, positionScanAtRowLocation, printQualifiers, resultSetNumber, rowLocation, setCurrentRow, setNeedsRowLocation, setTargetResultSet, skipRow, skipScan, updateRow
 
Methods inherited from class org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl
addWarning, attachStatementContext, checkCancellationFlag, checkRowPosition, cleanUp, dumpTimeStats, finishAndRTS, getAbsoluteRow, getActivation, getAutoGeneratedKeysResultset, getBeginExecutionTimestamp, getCompactRow, getCurrentTimeMillis, getElapsedMillis, getEndExecutionTimestamp, getEstimatedRowCount, getExecuteTime, getExecutionFactory, getFirstRow, getLanguageConnectionContext, getLastRow, getNextRow, getPointOfAttachment, getPreviousRow, getRelativeRow, getRowNumber, getScanIsolationLevel, getSubqueryTrackingArray, getTransactionController, getWarnings, isClosed, markAsTopResultSet, modifiedRowCount, open, recordConstructorTime, reopenCore, requiresRelocking, returnsRows, setAfterLastRow, setBeforeFirstRow, setCompactRow, setCompatRow
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.derby.iapi.sql.ResultSet
checkRowPosition, cleanUp, clearCurrentRow, finish, getAbsoluteRow, getActivation, getAutoGeneratedKeysResultset, getBeginExecutionTimestamp, getCursorName, getEndExecutionTimestamp, getExecuteTime, getFirstRow, getLastRow, getNextRow, getPreviousRow, getRelativeRow, getResultDescription, getRowNumber, getSubqueryTrackingArray, getWarnings, isClosed, modifiedRowCount, open, returnsRows, setAfterLastRow, setBeforeFirstRow
 

Field Detail

rowsInput

public int rowsInput

rowsReturned

public int rowsReturned

order

private ColumnOrdering[] order

sortTemplateRow

private ExecIndexRow sortTemplateRow

hasDistinctAggregate

public boolean hasDistinctAggregate

isInSortedOrder

public boolean isInSortedOrder

maxRowSize

private int maxRowSize

scanController

private ScanController scanController

sourceExecIndexRow

private ExecIndexRow sourceExecIndexRow

sortResultRow

private ExecIndexRow sortResultRow

resultsComplete

private boolean resultsComplete

finishedResults

private java.util.List finishedResults

resultRows

private ExecIndexRow[] resultRows

distinctValues

private java.util.HashSet[][] distinctValues

rollup

private boolean rollup

usingAggregateObserver

private boolean usingAggregateObserver

genericSortId

private long genericSortId

tc

private TransactionController tc

sortProperties

public java.util.Properties sortProperties
Constructor Detail

GroupedAggregateResultSet

GroupedAggregateResultSet(NoPutResultSet s,
                          boolean isInSortedOrder,
                          int aggregateItem,
                          int orderingItem,
                          Activation a,
                          GeneratedMethod ra,
                          int maxRowSize,
                          int resultSetNumber,
                          double optimizerEstimatedRowCount,
                          double optimizerEstimatedCost,
                          boolean isRollup)
                    throws StandardException
Constructor

Parameters:
s - input result set
isInSortedOrder - true if the source results are in sorted order
aggregateItem - indicates the number of the SavedObject off of the PreparedStatement that holds the AggregatorInfoList used by this routine.
orderingItem - indicates the number of the SavedObject off of the PreparedStatement that holds the ColumOrdering array used by this routine
a - activation
ra - generated method to build an empty output row
maxRowSize - approx row size, passed to sorter
resultSetNumber - The resultSetNumber for this result set
Throws:
StandardException - Thrown on error
Method Detail

openCore

public void openCore()
              throws StandardException
Open the scan. Load the sorter and prepare to get rows from it.

Specified by:
openCore in interface NoPutResultSet
Throws:
StandardException - thrown if cursor finished.

loadSorter

private ScanController loadSorter()
                           throws StandardException
Load up the sorter. Feed it every row from the source scan. When done, close the source scan and open the sort. Return the sort scan controller.

Returns:
the sort controller
Throws:
StandardException - thrown on failure.

getNextRowCore

public ExecRow getNextRowCore()
                       throws StandardException
Return the next row.

Specified by:
getNextRowCore in interface NoPutResultSet
Specified by:
getNextRowCore in class BasicNoPutResultSetImpl
Returns:
the next row in the result
Throws:
StandardException - thrown on failure.
StandardException - ResultSetNotOpen thrown if not yet open.
See Also:
NoPutResultSet.getNextRowCore()

makeCurrent

private ExecRow makeCurrent(java.lang.Object row)
                     throws StandardException
Throws:
StandardException

finalizeResults

private ExecRow finalizeResults()
                         throws StandardException
Throws:
StandardException

sameGroupingValues

private int sameGroupingValues(ExecRow currRow,
                               ExecRow newRow)
                        throws StandardException
Return whether or not the new row has the same values for the grouping columns as the current row. (This allows us to process in-order group bys without a sorter.)

Parameters:
currRow - The current row.
newRow - The new row.
Returns:
The order index number which first distinguished these rows, or order.length if the rows match.
Throws:
StandardException - thrown on failure to get row location

close

public void close()
           throws StandardException
If the result set has been opened, close the open scan.

Specified by:
close in interface ResultSet
Overrides:
close in class NoPutResultSetImpl
Throws:
StandardException - thrown on error

getTimeSpent

public long getTimeSpent(int type)
Return the total amount of time spent in this ResultSet

Specified by:
getTimeSpent in interface ResultSet
Parameters:
type - CURRENT_RESULTSET_ONLY - time spent only in this ResultSet ENTIRE_RESULTSET_TREE - time spent in this ResultSet and below.
Returns:
long The total amount of time spent (in milliseconds).

getRowLocation

public RowLocation getRowLocation()
                           throws StandardException
This result set has its row location from the last fetch done. If the cursor is closed, a null is returned.

Specified by:
getRowLocation in interface CursorResultSet
Returns:
the row location of the current cursor row.
Throws:
StandardException - thrown on failure to get row location
See Also:
CursorResultSet

getCurrentRow

public ExecRow getCurrentRow()
                      throws StandardException
This result set has its row from the last fetch done. If the cursor is closed, a null is returned.

Specified by:
getCurrentRow in interface CursorResultSet
Returns:
the last row returned;
Throws:
StandardException - thrown on failure.
See Also:
CursorResultSet

getNextRowFromRS

private ExecIndexRow getNextRowFromRS()
                               throws StandardException
Get the next output row for processing

Throws:
StandardException

getRowFromResultSet

private ExecIndexRow getRowFromResultSet()
                                  throws StandardException
Get a row from the input result set.

Throws:
StandardException

setRollupColumnsToNull

private void setRollupColumnsToNull(ExecRow row,
                                    int resultNum)
                             throws StandardException
Throws:
StandardException

getRowFromSorter

private ExecIndexRow getRowFromSorter()
                               throws StandardException
Get a row from the sorter. Side effects: sets currentRow.

Throws:
StandardException

closeSource

public void closeSource()
                 throws StandardException
Close the source of whatever we have been scanning.

Throws:
StandardException - thrown on error

initializeVectorAggregation

private void initializeVectorAggregation(ExecRow row)
                                  throws StandardException
Run the aggregator initialization method for each aggregator in the row. Accumulate the input column. WARNING: initializiation performs accumulation -- no need to accumulate a row that has been passed to initialization.

Parameters:
row - the row to initialize
Throws:
standard - Derby exception
StandardException

mergeVectorAggregates

private void mergeVectorAggregates(ExecRow newRow,
                                   ExecRow currRow,
                                   int level)
                            throws StandardException
Run the aggregator merge method for each aggregator in the row.

Parameters:
newRow - the row to merge
currRow - the row to merge into
Throws:
standard - Derby exception
StandardException

initializeDistinctMaps

private void initializeDistinctMaps(int r,
                                    boolean allocate)
                             throws StandardException
Throws:
StandardException

dumpAllRows

private void dumpAllRows(int cR)
                  throws StandardException
Throws:
StandardException

dumpRow

private java.lang.String dumpRow(ExecRow r)
                          throws StandardException
Throws:
StandardException

Built on Thu 2012-03-29 21:53:33+0000, from revision ???

Apache Derby V10.6 Internals - Copyright © 2004,2007 The Apache Software Foundation. All Rights Reserved.