Class GroupedAggregateResultSet
- java.lang.Object
-
- org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl
-
- org.apache.derby.impl.sql.execute.NoPutResultSetImpl
-
- org.apache.derby.impl.sql.execute.GenericAggregateResultSet
-
- 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.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl
BasicNoPutResultSetImpl.FieldComparator
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<java.util.List<java.util.Set<DataValueDescriptor>>>distinctValuesprivate java.util.List<ExecRow>finishedResultsprivate longgenericSortIdbooleanhasDistinctAggregatebooleanisInSortedOrderprivate intmaxRowSizeprivate intnumDistinctAggsprivate ColumnOrdering[]orderprivate ExecIndexRow[]resultRowsprivate booleanresultsCompleteprivate booleanrollupintrowsInputintrowsReturnedprivate ScanControllerscanControllerjava.util.PropertiessortPropertiesprivate ExecIndexRowsortResultRowprivate ExecIndexRowsourceExecIndexRowprivate TransactionControllertcprivate booleanusingAggregateObserver-
Fields inherited from class org.apache.derby.impl.sql.execute.GenericAggregateResultSet
aggInfoList, aggregates, originalSource, 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.execute.NoPutResultSet
ABSOLUTE, FIRST, LAST, NEXT, PREVIOUS, RELATIVE, TEMPORARY_RESULT_SET_NUMBER
-
Fields inherited from interface org.apache.derby.iapi.sql.ResultSet
CURRENT_RESULTSET_ONLY, ENTIRE_RESULTSET_TREE, ISAFTERLAST, ISBEFOREFIRST, ISFIRST, ISLAST
-
-
Constructor Summary
Constructors Constructor Description GroupedAggregateResultSet(NoPutResultSet s, boolean isInSortedOrder, int aggregateItem, int orderingItem, Activation a, int ra, int maxRowSize, int resultSetNumber, double optimizerEstimatedRowCount, double optimizerEstimatedCost, boolean isRollup)Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()If the result set has been opened, close the open scan.voidcloseSource()Close the source of whatever we have been scanning.private ExecRowfinalizeResults()ExecRowgetCurrentRow()This result set has its row from the last fetch done.ExecRowgetNextRowCore()Return the next row.private ExecIndexRowgetNextRowFromRS()Get the next output row for processingprivate ExecIndexRowgetRowFromResultSet()Get a row from the input result set.private ExecIndexRowgetRowFromSorter()Get a row from the sorter.RowLocationgetRowLocation()This result set has its row location from the last fetch done.longgetTimeSpent(int type)Return the total amount of time spent in this ResultSetprivate voidinitializeDistinctMaps(int r, boolean allocate)private voidinitializeVectorAggregation(ExecRow row)Run the aggregator initialization method for each aggregator in the row.private ScanControllerloadSorter()Load up the sorter.private ExecRowmakeCurrent(java.lang.Object row)private voidmergeVectorAggregates(ExecRow newRow, ExecRow currRow, int level)Run the aggregator merge method for each aggregator in the row.private intnumGCols()Return the number of grouping columns.voidopenCore()Open the scan.private intsameGroupingValues(ExecRow currRow, ExecRow newRow)Return whether or not the new row has the same values for the grouping columns as the current row.private voidsetRollupColumnsToNull(ExecRow row, int resultNum)-
Methods inherited from class org.apache.derby.impl.sql.execute.GenericAggregateResultSet
finish, finishAggregation, getRowTemplate, getSortAggregators, toXML
-
Methods inherited from class org.apache.derby.impl.sql.execute.NoPutResultSetImpl
clearCurrentRow, clearOrderableCache, closeRowSource, getCursorName, getNextRowFromRowSource, getResultDescription, getValidColumns, isForUpdate, markRowAsDeleted, needsRowLocation, needsRowLocationForDeferredCheckConstraints, needsToClone, offendingRowLocation, positionScanAtRowLocation, printQualifiers, resultSetNumber, rowLocation, setCurrentRow, setHasDeferrableChecks, setNeedsRowLocation, setTargetResultSet, skipRow, skipScan, unpackHashValue, updateRow
-
Methods inherited from class org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl
addWarning, attachStatementContext, checkCancellationFlag, checkRowPosition, childrenToXML, 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, isXplainOnlyMode, markAsTopResultSet, modifiedRowCount, open, recordConstructorTime, reopenCore, requiresRelocking, returnsRows, setAfterLastRow, setBeforeFirstRow, setCompactRow, setCompatRow, toXML
-
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
addWarning, 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, toXML
-
-
-
-
Field Detail
-
rowsInput
public int rowsInput
-
rowsReturned
public int rowsReturned
-
order
private ColumnOrdering[] order
-
hasDistinctAggregate
public boolean hasDistinctAggregate
-
isInSortedOrder
public boolean isInSortedOrder
-
numDistinctAggs
private int numDistinctAggs
-
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<ExecRow> finishedResults
-
resultRows
private ExecIndexRow[] resultRows
-
distinctValues
private java.util.List<java.util.List<java.util.Set<DataValueDescriptor>>> 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, int ra, int maxRowSize, int resultSetNumber, double optimizerEstimatedRowCount, double optimizerEstimatedCost, boolean isRollup) throws StandardException
Constructor- Parameters:
s- input result setisInSortedOrder- true if the source results are in sorted orderaggregateItem- 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 routinea- activationra- saved object that builds an empty output rowmaxRowSize- approx row size, passed to sorterresultSetNumber- The resultSetNumber for this result set- Throws:
StandardException- Thrown on error
-
-
Method Detail
-
openCore
public void openCore() throws StandardExceptionOpen the scan. Load the sorter and prepare to get rows from it.- Specified by:
openCorein interfaceNoPutResultSet- 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.
-
numGCols
private int numGCols()
Return the number of grouping columns. Since some additional sort columns may have been included in the sort for DISTINCT aggregates, this function is used to ignore those columns when computing the grouped results.
-
getNextRowCore
public ExecRow getNextRowCore() throws StandardException
Return the next row.- Specified by:
getNextRowCorein interfaceNoPutResultSet- Specified by:
getNextRowCorein classBasicNoPutResultSetImpl- 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 StandardExceptionIf the result set has been opened, close the open scan.- Specified by:
closein interfaceResultSet- Overrides:
closein classNoPutResultSetImpl- Throws:
StandardException- thrown on error
-
getTimeSpent
public long getTimeSpent(int type)
Return the total amount of time spent in this ResultSet- Specified by:
getTimeSpentin interfaceResultSet- 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:
getRowLocationin interfaceCursorResultSet- 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:
getCurrentRowin interfaceCursorResultSet- 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 StandardExceptionClose 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 exceptionStandardException
-
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 mergecurrRow- the row to merge into- Throws:
standard- Derby exceptionStandardException
-
initializeDistinctMaps
private void initializeDistinctMaps(int r, boolean allocate) throws StandardException- Throws:
StandardException
-
-