Top Description Fields Constructors Methods
org.apache.derby.impl.sql.execute

pack-priv Class GroupedAggregateResultSet

extends GenericAggregateResultSet
implements CursorResultSet
Class Inheritance
All Implemented Interfaces
org.apache.derby.iapi.sql.execute.CursorResultSet, org.apache.derby.iapi.sql.ResultSet
Known Direct Subclasses
org.apache.derby.impl.sql.execute.DistinctGroupedAggregateResultSet
Imports
java.util.ArrayList, .List, .Properties, .HashSet, .Set, org.apache.derby.shared.common.error.StandardException, org.apache.derby.iapi.services.io.FormatableArrayHolder, org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.iapi.sql.Activation, org.apache.derby.iapi.sql.execute.CursorResultSet, .ExecAggregator, .ExecIndexRow, .ExecRow, .NoPutResultSet, org.apache.derby.iapi.store.access.ColumnOrdering, .ScanController, .SortController, .SortObserver, .TransactionController, org.apache.derby.iapi.types.DataValueDescriptor, .RowLocation

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

Modifier and TypeField and Description
private List<List<Set<DataValueDescriptor>>>
private List<ExecRow>
private long
public boolean
public boolean
private int
private int
private ColumnOrdering[]
private ExecIndexRow[]
private boolean
private boolean
public int
public int
private ScanController
public Properties
private ExecIndexRow
private ExecIndexRow
private TransactionController
private boolean
Inherited from org.apache.derby.impl.sql.execute.GenericAggregateResultSet:
aggInfoListaggregatesoriginalSourcesource

Constructor Summary

AccessConstructor and Description
pack-priv
GroupedAggregateResultSet(NoPutResultSet
input result set
s
,
boolean
true if the source results are in sorted order
isInSortedOrder
,
int
indicates the number of the SavedObject off of the PreparedStatement that holds the AggregatorInfoList used by this routine.
aggregateItem
,
int
indicates the number of the SavedObject off of the PreparedStatement that holds the ColumOrdering array used by this routine
orderingItem
,
Activation
activation
a
,
int
saved object that builds an empty output row
ra
,
int
approx row size, passed to sorter
maxRowSize
,
int
The resultSetNumber for this result set
resultSetNumber
,
double optimizerEstimatedRowCount, double optimizerEstimatedCost, boolean isRollup)

Constructor

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides org.apache.derby.impl.sql.execute.NoPutResultSetImpl.close.

Implements org.apache.derby.iapi.sql.ResultSet.close.

If the result set has been opened, close the open scan.

public void
closeSource()

Close the source of whatever we have been scanning.

private ExecRow
public ExecRow

Returns:

the last row returned;
getCurrentRow
()

Implements org.apache.derby.iapi.sql.execute.CursorResultSet.getCurrentRow.

This result set has its row from the last fetch done.

public ExecRow
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.

public RowLocation

Returns:

the row location of the current cursor row.
getRowLocation
()

Implements org.apache.derby.iapi.sql.execute.CursorResultSet.getRowLocation.

This result set has its row location from the last fetch done.

public long

Returns:

long The total amount of time spent (in milliseconds).
getTimeSpent
(int
CURRENT_RESULTSET_ONLY - time spent only in this ResultSet ENTIRE_RESULTSET_TREE - time spent in this ResultSet and below.
type
)

Implements org.apache.derby.iapi.sql.ResultSet.getTimeSpent.

Return the total amount of time spent in this ResultSet

private void
initializeDistinctMaps(int r, boolean allocate)

private void
initializeVectorAggregation(ExecRow
the row to initialize
row
)

Run the aggregator initialization method for each aggregator in the row.

private ScanController

Returns:

the sort controller
loadSorter
()

Load up the sorter.

private ExecRow
private void
mergeVectorAggregates(ExecRow
the row to merge
newRow
,
ExecRow
the row to merge into
currRow
,
int level)

Run the aggregator merge method for each aggregator in the row.

private int
numGCols()

Return the number of grouping columns.

public void
private int

Returns:

The order index number which first distinguished these rows, or order.length if the rows match.
sameGroupingValues
(ExecRow
The current row.
currRow
,
ExecRow
The new row.
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)

Inherited from org.apache.derby.impl.sql.execute.GenericAggregateResultSet:
finishfinishAggregationgetRowTemplategetSortAggregatorstoXML

Field Detail

distinctValuesback to summary
private List<List<Set<DataValueDescriptor>>> distinctValues
finishedResultsback to summary
private List<ExecRow> finishedResults
genericSortIdback to summary
private long genericSortId
hasDistinctAggregateback to summary
public boolean hasDistinctAggregate
isInSortedOrderback to summary
public boolean isInSortedOrder
maxRowSizeback to summary
private int maxRowSize
numDistinctAggsback to summary
private int numDistinctAggs
orderback to summary
private ColumnOrdering[] order
resultRowsback to summary
private ExecIndexRow[] resultRows
resultsCompleteback to summary
private boolean resultsComplete
rollupback to summary
private boolean rollup
rowsInputback to summary
public int rowsInput
rowsReturnedback to summary
public int rowsReturned
scanControllerback to summary
private ScanController scanController
sortPropertiesback to summary
public Properties sortProperties
sortResultRowback to summary
private ExecIndexRow sortResultRow
sourceExecIndexRowback to summary
private ExecIndexRow sourceExecIndexRow
tcback to summary
private TransactionController tc

Hides org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl.tc.

usingAggregateObserverback to summary
private boolean usingAggregateObserver

Constructor Detail

GroupedAggregateResultSetback to summary
pack-priv 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:NoPutResultSet

input result set

isInSortedOrder:boolean

true if the source results are in sorted order

aggregateItem:int

indicates the number of the SavedObject off of the PreparedStatement that holds the AggregatorInfoList used by this routine.

orderingItem:int

indicates the number of the SavedObject off of the PreparedStatement that holds the ColumOrdering array used by this routine

a:Activation

activation

ra:int

saved object that builds an empty output row

maxRowSize:int

approx row size, passed to sorter

resultSetNumber:int

The resultSetNumber for this result set

Exceptions
StandardException:
Thrown on error

Method Detail

closeback to summary
public void close() throws StandardException

Overrides org.apache.derby.impl.sql.execute.NoPutResultSetImpl.close.

Implements org.apache.derby.iapi.sql.ResultSet.close.

If the result set has been opened, close the open scan.

Annotations
@Override
Exceptions
StandardException:
thrown on error
closeSourceback to summary
public void closeSource() throws StandardException

Close the source of whatever we have been scanning.

Exceptions
StandardException:
thrown on error
finalizeResultsback to summary
private ExecRow finalizeResults() throws StandardException
getCurrentRowback to summary
public ExecRow getCurrentRow() throws StandardException

Implements org.apache.derby.iapi.sql.execute.CursorResultSet.getCurrentRow.

This result set has its row from the last fetch done. If the cursor is closed, a null is returned.

Returns:ExecRow

the last row returned;

Exceptions
StandardException:
thrown on failure.
See Also
CursorResultSet
getNextRowCoreback to summary
public ExecRow getNextRowCore() throws StandardException

Implements abstract org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl.getNextRowCore.

Implements org.apache.derby.iapi.sql.execute.NoPutResultSet.getNextRowCore.

Return the next row.

Returns:ExecRow

the next row in the result

Exceptions
StandardException:
  • thrown on failure.
  • ResultSetNotOpen thrown if not yet open.
getNextRowFromRSback to summary
private ExecIndexRow getNextRowFromRS() throws StandardException

Get the next output row for processing

getRowFromResultSetback to summary
private ExecIndexRow getRowFromResultSet() throws StandardException

Get a row from the input result set.

getRowFromSorterback to summary
private ExecIndexRow getRowFromSorter() throws StandardException

Get a row from the sorter. Side effects: sets currentRow.

getRowLocationback to summary
public RowLocation getRowLocation() throws StandardException

Implements org.apache.derby.iapi.sql.execute.CursorResultSet.getRowLocation.

This result set has its row location from the last fetch done. If the cursor is closed, a null is returned.

Returns:RowLocation

the row location of the current cursor row.

Exceptions
StandardException:
thrown on failure to get row location
See Also
CursorResultSet
getTimeSpentback to summary
public long getTimeSpent(int type)

Implements org.apache.derby.iapi.sql.ResultSet.getTimeSpent.

Return the total amount of time spent in this ResultSet

Parameters
type:int

CURRENT_RESULTSET_ONLY - time spent only in this ResultSet ENTIRE_RESULTSET_TREE - time spent in this ResultSet and below.

Returns:long

long The total amount of time spent (in milliseconds).

initializeDistinctMapsback to summary
private void initializeDistinctMaps(int r, boolean allocate) throws StandardException
initializeVectorAggregationback to summary
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:ExecRow

the row to initialize

Exceptions
StandardException:
standard Derby exception
loadSorterback to summary
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:ScanController

the sort controller

Exceptions
StandardException:
thrown on failure.
makeCurrentback to summary
private ExecRow makeCurrent(Object row) throws StandardException
mergeVectorAggregatesback to summary
private void mergeVectorAggregates(ExecRow newRow, ExecRow currRow, int level) throws StandardException

Run the aggregator merge method for each aggregator in the row.

Parameters
newRow:ExecRow

the row to merge

currRow:ExecRow

the row to merge into

Exceptions
StandardException:
standard Derby exception
numGColsback to summary
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.

openCoreback to summary
public void openCore() throws StandardException

Implements org.apache.derby.iapi.sql.execute.NoPutResultSet.openCore.

Open the scan. Load the sorter and prepare to get rows from it.

Exceptions
StandardException:
thrown if cursor finished.
sameGroupingValuesback to summary
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:ExecRow

The current row.

newRow:ExecRow

The new row.

Returns:int

The order index number which first distinguished these rows, or order.length if the rows match.

Exceptions
StandardException:
thrown on failure to get row location
setRollupColumnsToNullback to summary
private void setRollupColumnsToNull(ExecRow row, int resultNum) throws StandardException