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
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 BasicNoPutResultSetImpl
BasicNoPutResultSetImpl.FieldComparator -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate List<List<Set<DataValueDescriptor>>> private longbooleanbooleanprivate intprivate intprivate ColumnOrdering[]private ExecIndexRow[]private booleanprivate booleanintintprivate ScanControllerprivate ExecIndexRowprivate ExecIndexRowprivate TransactionControllerprivate booleanFields inherited from class GenericAggregateResultSet
aggInfoList, aggregates, originalSource, sourceFields inherited from class NoPutResultSetImpl
checkNullCols, clonedExecRow, cncLen, resultSetNumber, targetResultSetFields inherited from class BasicNoPutResultSetImpl
activation, beginTime, closeTime, compactRow, constructorTime, currentRow, endExecutionTime, finished, isOpen, isTopResultSet, nextTime, numOpens, openTime, optimizerEstimatedCost, optimizerEstimatedRowCount, resultDescription, rowsFiltered, rowsSeen, startExecutionTime, subqueryTrackingArrayFields inherited from interface NoPutResultSet
ABSOLUTE, FIRST, LAST, NEXT, PREVIOUS, RELATIVE, TEMPORARY_RESULT_SET_NUMBERFields inherited from interface ResultSet
CURRENT_RESULTSET_ONLY, ENTIRE_RESULTSET_TREE, ISAFTERLAST, ISBEFOREFIRST, ISFIRST, ISLAST -
Constructor Summary
ConstructorsConstructorDescriptionGroupedAggregateResultSet(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
Modifier and TypeMethodDescriptionvoidclose()If the result set has been opened, close the open scan.voidClose the source of whatever we have been scanning.private ExecRowThis result set has its row from the last fetch done.Return the next row.private ExecIndexRowGet the next output row for processingprivate ExecIndexRowGet a row from the input result set.private ExecIndexRowGet a row from the sorter.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 voidRun the aggregator initialization method for each aggregator in the row.private ScanControllerLoad up the sorter.private ExecRowmakeCurrent(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 GenericAggregateResultSet
finish, finishAggregation, getRowTemplate, getSortAggregators, toXMLMethods inherited from class 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, updateRowMethods inherited from class 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, toXMLMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface 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 Details
-
rowsInput
public int rowsInput -
rowsReturned
public int rowsReturned -
order
-
hasDistinctAggregate
public boolean hasDistinctAggregate -
isInSortedOrder
public boolean isInSortedOrder -
numDistinctAggs
private int numDistinctAggs -
maxRowSize
private int maxRowSize -
scanController
-
sourceExecIndexRow
-
sortResultRow
-
resultsComplete
private boolean resultsComplete -
finishedResults
-
resultRows
-
distinctValues
-
rollup
private boolean rollup -
usingAggregateObserver
private boolean usingAggregateObserver -
genericSortId
private long genericSortId -
tc
-
sortProperties
-
-
Constructor Details
-
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 Details
-
openCore
Open the scan. Load the sorter and prepare to get rows from it.- Specified by:
openCorein interfaceNoPutResultSet- Throws:
StandardException- thrown if cursor finished.
-
loadSorter
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
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:
-
makeCurrent
- Throws:
StandardException
-
finalizeResults
- Throws:
StandardException
-
sameGroupingValues
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
If 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
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:
-
getCurrentRow
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:
-
getNextRowFromRS
Get the next output row for processing- Throws:
StandardException
-
getRowFromResultSet
Get a row from the input result set.- Throws:
StandardException
-
setRollupColumnsToNull
- Throws:
StandardException
-
getRowFromSorter
Get a row from the sorter. Side effects: sets currentRow.- Throws:
StandardException
-
closeSource
Close the source of whatever we have been scanning.- Throws:
StandardException- thrown on error
-
initializeVectorAggregation
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
-
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
-
initializeDistinctMaps
- Throws:
StandardException
-