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

pack-priv Class SortResultSet

extends NoPutResultSetImpl
implements CursorResultSet
Class Inheritance
All Implemented Interfaces
org.apache.derby.iapi.sql.execute.CursorResultSet, org.apache.derby.iapi.sql.ResultSet
Imports
org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.iapi.sql.execute.CursorResultSet, .ExecPreparedStatement, .ExecRow, .NoPutResultSet, .ExecRowBuilder, org.apache.derby.iapi.sql.Activation, org.apache.derby.iapi.store.access.ColumnOrdering, .SortObserver, .TransactionController, .SortController, .ScanController, org.apache.derby.iapi.types.DataValueDescriptor, .RowLocation, org.apache.derby.shared.common.error.StandardException, org.apache.derby.iapi.services.io.FormatableArrayHolder, java.util.Properties

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:

Field Summary

Modifier and TypeField and Description
private ExecRow
public boolean
private boolean
private long
public boolean
private int
private boolean
private int
private SortObserver
private ColumnOrdering[]
private NoPutResultSet
public int
public int
private ColumnOrdering[]
private ScanController
private boolean
public Properties
private ExecRow
private ExecRow
public NoPutResultSet
Inherited from org.apache.derby.impl.sql.execute.NoPutResultSetImpl:
checkNullColsclonedExecRowcncLenresultSetNumbertargetResultSet

Constructor Summary

AccessConstructor and Description
public
SortResultSet(NoPutResultSet
input result set
s
,
boolean
if this is a DISTINCT select list. Also set to true for a GROUP BY w/o aggretates
distinct
,
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 ColumOrdering array used by this routine
orderingItem
,
Activation
activation
a
,
int
saved object that generates an empty row
ra
,
int
approx row size, passed to sorter
maxRowSize
,
int
The resultSetNumber for this result set
resultSetNumber
,
double optimizerEstimatedRowCount, double optimizerEstimatedCost)

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.

private void
closeSource()

Close the source of whatever we have been scanning.

private boolean

Returns:

Whether or not to filter out the new row.
filterRow
(ExecRow
The current row.
currRow
,
ExecRow
The new row.
newRow
)

Filter out the new row if it has the same contents as the current row.

public void
finish()

Overrides org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl.finish.

Implements org.apache.derby.iapi.sql.ResultSet.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().

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 ExecRow
getNextRowFromRS()

Get the next output row for processing

private ExecRow
getRowFromResultSet()

Get a row from the input result set.

private ExecRow
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 ScanController

Returns:

the sort controller
loadSorter
()

Load up the sorter.

public void
Inherited from org.apache.derby.impl.sql.execute.NoPutResultSetImpl:
clearCurrentRowclearOrderableCachecloseRowSourcegetCursorNamegetNextRowFromRowSourcegetResultDescriptiongetValidColumnsisForUpdatemarkRowAsDeletedneedsRowLocationneedsRowLocationForDeferredCheckConstraintsneedsToCloneoffendingRowLocationpositionScanAtRowLocationprintQualifiersresultSetNumberrowLocationsetCurrentRowsetHasDeferrableCheckssetNeedsRowLocationsetTargetResultSetskipRowskipScanunpackHashValueupdateRow

Field Detail

currSortedRowback to summary
private ExecRow currSortedRow
distinctback to summary
public boolean distinct
dropGenericSortback to summary
private boolean dropGenericSort
genericSortIdback to summary
private long genericSortId
isInSortedOrderback to summary
public boolean isInSortedOrder
maxRowSizeback to summary
private int maxRowSize
nextCalledback to summary
private boolean nextCalled
numColumnsback to summary
private int numColumns
observerback to summary
private SortObserver observer
orderback to summary
private ColumnOrdering[] order
originalSourceback to summary
private NoPutResultSet originalSource
rowsInputback to summary
public int rowsInput
rowsReturnedback to summary
public int rowsReturned
savedOrderback to summary
private ColumnOrdering[] savedOrder
scanControllerback to summary
private ScanController scanController
sortedback to summary
private boolean sorted
sortPropertiesback to summary
public Properties sortProperties
sortResultRowback to summary
private ExecRow sortResultRow
sortTemplateRowback to summary
private ExecRow sortTemplateRow
sourceback to summary
public NoPutResultSet source

Constructor Detail

SortResultSetback to summary
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:NoPutResultSet

input result set

distinct:boolean

if this is a DISTINCT select list. Also set to true for a GROUP BY w/o aggretates

isInSortedOrder:boolean

true if the source results are in sorted order

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 generates an empty 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.

Exceptions
StandardException:
thrown on error
closeSourceback to summary
private void closeSource() throws StandardException

Close the source of whatever we have been scanning.

filterRowback to summary
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:ExecRow

The current row.

newRow:ExecRow

The new row.

Returns:boolean

Whether or not to filter out the new row.

Exceptions
StandardException:
thrown on failure to get row location
finishback to summary
public void finish() throws StandardException

Overrides org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl.finish.

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

Doc from org.apache.derby.iapi.sql.ResultSet.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(). Will close the result set if it is not already closed.

Exceptions
StandardException:
on error
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 ExecRow getNextRowFromRS() throws StandardException

Get the next output row for processing

getRowFromResultSetback to summary
private ExecRow getRowFromResultSet() throws StandardException

Get a row from the input result set.

getRowFromSorterback to summary
private ExecRow 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).

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