Class SortResultSet
- All Implemented Interfaces:
CursorResultSet
,NoPutResultSet
,ResultSet
,RowLocationRetRowSource
,RowSource
If aggregate is true, then it feeds any number of aggregates to the sorter. Each aggregate is an instance of GenericAggregator which knows which Aggregator to call to perform the aggregation.
Brief background on the sorter and aggregates: the sorter has some rudimentary knowledge about aggregates. If it is passed aggregates, it will eliminate duplicates on the ordering columns. In the process it will call the aggregator on each row that is discarded.
Note that a DISTINCT on the SELECT list and an aggregate cannot be processed by the same SortResultSet(), if there are both aggregates (distinct or otherwise) and a DISTINCT on the select list, then 2 separate SortResultSets are required (the DISTINCT is a sort on the output of the sort with the aggregation).
Currently, all rows are fed through the sorter. This is true even if there is no sorting needed. In this case we feed every row in and just pull every row out (this is an obvious area for a performance improvement). We'll need to know if the rows are sorted before we can make any optimizations in this area.
CLONING: Cloning and sorts are an important topic. Currently we do a lot of cloning. We clone the following:
- every row that is inserted into the sorter. We need to clone the rows because the source result set might be reusing rows, and we need to be able to accumulate the entire result set in the sorter.
There are two cloning APIs: cloning by the sorter on rows that are not discarded as duplicates or cloning in the SortResultSet prior to inserting into the sorter. If we have any aggregates at all we always clone prior to inserting into the sorter. We need to do this because we have to set up the aggregators before passing them into the sorter. When we don't have aggregates we let the sorter to the cloning to avoid unnecessary clones on duplicate rows that are going to be discarded anyway.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl
BasicNoPutResultSetImpl.FieldComparator
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ExecRow
boolean
private boolean
private long
boolean
private int
private boolean
private int
private SortObserver
private ColumnOrdering[]
private NoPutResultSet
int
int
private ColumnOrdering[]
private ScanController
private boolean
private ExecRow
private ExecRow
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
ConstructorsConstructorDescriptionSortResultSet
(NoPutResultSet s, boolean distinct, boolean isInSortedOrder, int orderingItem, Activation a, int ra, int maxRowSize, int resultSetNumber, double optimizerEstimatedRowCount, double optimizerEstimatedCost) Constructor -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
If the result set has been opened, close the open scan.private void
Close the source of whatever we have been scanning.private boolean
Filter out the new row if it has the same contents as the current row.void
finish()
Tells the system that there will be no more access to any database information via this result set; in particular, no more calls to open().This result set has its row from the last fetch done.Return the next row.private ExecRow
Get the next output row for processingprivate ExecRow
Get a row from the input result set.private ExecRow
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 ScanController
Load up the sorter.void
openCore()
Open the scan.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, 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, 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 -
distinct
public boolean distinct -
source
-
order
-
savedOrder
-
observer
-
sortTemplateRow
-
isInSortedOrder
public boolean isInSortedOrder -
originalSource
-
maxRowSize
private int maxRowSize -
scanController
-
sortResultRow
-
currSortedRow
-
nextCalled
private boolean nextCalled -
numColumns
private int numColumns -
genericSortId
private long genericSortId -
dropGenericSort
private boolean dropGenericSort -
sorted
private boolean sorted -
sortProperties
-
-
Constructor Details
-
SortResultSet
public SortResultSet(NoPutResultSet s, boolean distinct, boolean isInSortedOrder, int orderingItem, Activation a, int ra, int maxRowSize, int resultSetNumber, double optimizerEstimatedRowCount, double optimizerEstimatedCost) throws StandardException Constructor- Parameters:
s
- input result setdistinct
- if this is a DISTINCT select list. Also set to true for a GROUP BY w/o aggretatesisInSortedOrder
- true if the source results are in sorted orderorderingItem
- indicates the number of the SavedObject off of the PreparedStatement that holds the ColumOrdering array used by this routinea
- activationra
- saved object that generates an empty 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.
-
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:
-
filterRow
Filter out the new row if it has the same contents as the current row. (This allows us to process in-order distincts without a sorter.)- Parameters:
currRow
- The current row.newRow
- The new row.- Returns:
- Whether or not to filter out the new row.
- 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
-
finish
Description copied from interface:ResultSet
Tells the system that there will be no more access to any database information via this result set; in particular, no more calls to open(). Will close the result set if it is not already closed.- Specified by:
finish
in interfaceResultSet
- Overrides:
finish
in classBasicNoPutResultSetImpl
- Throws:
StandardException
- 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
-
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
-