Class SortResultSet
- java.lang.Object
-
- org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl
-
- org.apache.derby.impl.sql.execute.NoPutResultSetImpl
-
- org.apache.derby.impl.sql.execute.SortResultSet
-
- All Implemented Interfaces:
CursorResultSet
,NoPutResultSet
,ResultSet
,RowLocationRetRowSource
,RowSource
class SortResultSet extends NoPutResultSetImpl implements CursorResultSet
Takes a source result set, sends it to the sorter, and returns the results. If distinct is true, removes all but one copy of duplicate rows using DistinctAggregator, which really doesn't aggregate anything at all -- the sorter assumes that the presence of an aggregator means that it should return a single row for each set with identical ordering columns.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
Fields Modifier and Type Field Description private ExecRow
currSortedRow
boolean
distinct
private boolean
dropGenericSort
private long
genericSortId
boolean
isInSortedOrder
private int
maxRowSize
private boolean
nextCalled
private int
numColumns
private SortObserver
observer
private ColumnOrdering[]
order
private NoPutResultSet
originalSource
int
rowsInput
int
rowsReturned
private ColumnOrdering[]
savedOrder
private ScanController
scanController
private boolean
sorted
java.util.Properties
sortProperties
private ExecRow
sortResultRow
private ExecRow
sortTemplateRow
NoPutResultSet
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 SortResultSet(NoPutResultSet s, boolean distinct, boolean isInSortedOrder, int orderingItem, Activation a, int ra, int maxRowSize, int resultSetNumber, double optimizerEstimatedRowCount, double optimizerEstimatedCost)
Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
If the result set has been opened, close the open scan.private void
closeSource()
Close the source of whatever we have been scanning.private boolean
filterRow(ExecRow currRow, ExecRow newRow)
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().ExecRow
getCurrentRow()
This result set has its row from the last fetch done.ExecRow
getNextRowCore()
Return the next row.private ExecRow
getNextRowFromRS()
Get the next output row for processingprivate ExecRow
getRowFromResultSet()
Get a row from the input result set.private ExecRow
getRowFromSorter()
Get a row from the sorter.RowLocation
getRowLocation()
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
loadSorter()
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 Detail
-
rowsInput
public int rowsInput
-
rowsReturned
public int rowsReturned
-
distinct
public boolean distinct
-
source
public NoPutResultSet source
-
order
private ColumnOrdering[] order
-
savedOrder
private ColumnOrdering[] savedOrder
-
observer
private SortObserver observer
-
sortTemplateRow
private ExecRow sortTemplateRow
-
isInSortedOrder
public boolean isInSortedOrder
-
originalSource
private NoPutResultSet originalSource
-
maxRowSize
private int maxRowSize
-
scanController
private ScanController scanController
-
sortResultRow
private ExecRow sortResultRow
-
currSortedRow
private ExecRow currSortedRow
-
nextCalled
private boolean nextCalled
-
numColumns
private int numColumns
-
genericSortId
private long genericSortId
-
dropGenericSort
private boolean dropGenericSort
-
sorted
private boolean sorted
-
sortProperties
public java.util.Properties sortProperties
-
-
Constructor Detail
-
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 Detail
-
openCore
public void openCore() throws StandardException
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
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.
-
getNextRowCore
public ExecRow getNextRowCore() throws StandardException
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:
NoPutResultSet.getNextRowCore()
-
filterRow
private boolean filterRow(ExecRow currRow, ExecRow newRow) throws StandardException
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
public void close() throws StandardException
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
public void finish() throws StandardException
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
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:
getRowLocation
in 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:
getCurrentRow
in interfaceCursorResultSet
- Returns:
- the last row returned;
- Throws:
StandardException
- thrown on failure.- See Also:
CursorResultSet
-
getNextRowFromRS
private ExecRow getNextRowFromRS() throws StandardException
Get the next output row for processing- Throws:
StandardException
-
getRowFromResultSet
private ExecRow getRowFromResultSet() throws StandardException
Get a row from the input result set.- Throws:
StandardException
-
getRowFromSorter
private ExecRow getRowFromSorter() throws StandardException
Get a row from the sorter. Side effects: sets currentRow.- Throws:
StandardException
-
closeSource
private void closeSource() throws StandardException
Close the source of whatever we have been scanning.- Throws:
StandardException
-
-