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

  • Field Details

    • 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 Properties 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 set
      distinct - if this is a DISTINCT select list. Also set to true for a GROUP BY w/o aggretates
      isInSortedOrder - true if the source results are in sorted order
      orderingItem - indicates the number of the SavedObject off of the PreparedStatement that holds the ColumOrdering array used by this routine
      a - activation
      ra - saved object that generates an empty row
      maxRowSize - approx row size, passed to sorter
      resultSetNumber - The resultSetNumber for this result set
      Throws:
      StandardException - Thrown on error
  • Method Details