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 org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl
BasicNoPutResultSetImpl.FieldComparator
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate List
<List<Set<DataValueDescriptor>>> private long
boolean
boolean
private int
private int
private ColumnOrdering[]
private ExecIndexRow[]
private boolean
private boolean
int
int
private ScanController
private ExecIndexRow
private ExecIndexRow
private TransactionController
private boolean
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
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 TypeMethodDescriptionvoid
close()
If the result set has been opened, close the open scan.void
Close the source of whatever we have been scanning.private ExecRow
This result set has its row from the last fetch done.Return the next row.private ExecIndexRow
Get the next output row for processingprivate ExecIndexRow
Get a row from the input result set.private ExecIndexRow
Get a row from the sorter.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 ResultSetprivate void
initializeDistinctMaps
(int r, boolean allocate) private void
Run the aggregator initialization method for each aggregator in the row.private ScanController
Load up the sorter.private ExecRow
makeCurrent
(Object row) private void
mergeVectorAggregates
(ExecRow newRow, ExecRow currRow, int level) Run the aggregator merge method for each aggregator in the row.private int
numGCols()
Return the number of grouping columns.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, 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 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:
openCore
in 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:
getNextRowCore
in interfaceNoPutResultSet
- Specified by:
getNextRowCore
in 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:
close
in interfaceResultSet
- Overrides:
close
in classNoPutResultSetImpl
- 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 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:
getRowLocation
in 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:
getCurrentRow
in 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
-