Class OrderByList

All Implemented Interfaces:
Iterable<OrderByColumn>, RequiredRowOrdering, Visitable

class OrderByList extends OrderedColumnList<OrderByColumn> implements RequiredRowOrdering
An OrderByList is an ordered list of columns in the ORDER BY clause. That is, the order of columns in this list is significant - the first column in the list is the most significant in the ordering, and the last column in the list is the least significant.
  • Field Details

    • allAscending

      private boolean allAscending
    • alwaysSort

      private boolean alwaysSort
    • resultToSort

      private ResultSetNode resultToSort
    • scc

      private SortCostController scc
    • resultRow

      private Object[] resultRow
    • columnOrdering

      private ColumnOrdering[] columnOrdering
    • estimatedRowSize

      private int estimatedRowSize
    • sortNeeded

      private boolean sortNeeded
    • resultSetNumber

      private int resultSetNumber
    • isTableValueCtorOrdering

      private boolean isTableValueCtorOrdering
      true if this instance orders a <table value constructor>. See isTableValueCtorOrdering.
  • Constructor Details

    • OrderByList

      OrderByList(ResultSetNode rs, ContextManager cm)
      Constructor. Initialize with the type of the result set this OrderByList is attached to, e.g. SELECT, VALUES or a set operation.
      Parameters:
      rs - The result set this OrderByList is ordering. May be null
      cm - The context manager
  • Method Details

    • addOrderByColumn

      void addOrderByColumn(OrderByColumn column)
      Add a column to the list
      Parameters:
      column - The column to add to the list
    • allAscending

      boolean allAscending()
      Are all columns in the list ascending.
      Returns:
      Whether or not all columns in the list ascending.
    • getOrderByColumn

      OrderByColumn getOrderByColumn(int position)
      Get a column from the list
      Parameters:
      position - The column to get from the list
    • bindOrderByColumns

      void bindOrderByColumns(ResultSetNode target) throws StandardException
      Bind the update columns by their names to the target resultset of the cursor specification.
      Parameters:
      target - The underlying result set
      Throws:
      StandardException - Thrown on error
    • closeGap

      void closeGap(int gap)
      Adjust addedColumnOffset values due to removal of a duplicate column This routine is called by bind processing when it identifies and removes a column from the result column list which was pulled up due to its presence in the ORDER BY clause, but which was later found to be a duplicate. The OrderByColumn instance for the removed column has been adjusted to point to the true column in the result column list and its addedColumnOffset has been reset to -1. This routine finds any other OrderByColumn instances which had an offset greater than that of the column that has been deleted, and decrements their addedColumOffset to account for the deleted column's removal.
      Parameters:
      gap - column which has been removed from the result column list
    • pullUpOrderByColumns

      void pullUpOrderByColumns(ResultSetNode target) throws StandardException
      Pull up Order By columns by their names to the target resultset of the cursor specification.
      Parameters:
      target - The underlying result set
      Throws:
      StandardException
    • isInOrderPrefix

      boolean isInOrderPrefix(ResultColumnList sourceRCL)
      Is this order by list an in order prefix of the specified RCL. This is useful when deciding if an order by list can be eliminated due to a sort from an underlying distinct or union.
      Parameters:
      sourceRCL - The source RCL.
      Returns:
      Whether or not this order by list an in order prefix of the specified RCL.
    • resetToSourceRCs

      void resetToSourceRCs()
      Order by columns now point to the PRN above the node of interest. We need them to point to the RCL under that one. This is useful when combining sorts where we need to reorder the sorting columns.
    • reorderRCL

      ResultColumnList reorderRCL(ResultColumnList resultColumns) throws StandardException
      Build a new RCL with the same RCs as the passed in RCL but in an order that matches the ordering columns.
      Parameters:
      resultColumns - The RCL to reorder.
      Throws:
      StandardException - Thrown on error
    • removeConstantColumns

      void removeConstantColumns(PredicateList whereClause)
      Remove any constant columns from this order by list. Constant columns are ones where all of the column references are equal to constant expressions according to the given predicate list.
    • removeDupColumns

      void removeDupColumns()
      Remove any duplicate columns from this order by list. For example, one may "ORDER BY 1, 1, 2" can be reduced to "ORDER BY 1, 2". Beetle 5401.
    • generate

      void generate(ActivationClassBuilder acb, MethodBuilder mb, ResultSetNode child) throws StandardException
      generate the sort result set operating over the source expression.
      Parameters:
      acb - the tool for building the class
      mb - the method the generated code is to go into
      Throws:
      StandardException - thrown on failure
    • sortRequired

      public int sortRequired(RowOrdering rowOrdering, OptimizableList optimizableList, int[] proposedJoinOrder) throws StandardException
      Description copied from interface: RequiredRowOrdering
      Tell whether sorting is required for this RequiredRowOrdering, given a RowOrdering.
      Specified by:
      sortRequired in interface RequiredRowOrdering
      Parameters:
      rowOrdering - The order of rows in question
      optimizableList - The list of join participants
      proposedJoinOrder - The current join order being considered by the optimizer. We need to look into this to determine if the outer optimizables are single row result set if the order by column is on an inner optimizable and that inner optimizable is not a one. proposedJoinOrder is a map onto optimizableList: it contains indexes of optimizables in that list in the join order proposed, cf. OptimizerImpl#proposedJoinOrder.
      Returns:
      SORT_REQUIRED if sorting is required, ELIMINATE_DUPS if no sorting is required but duplicates must be eliminated (i.e. the rows are in the right order but there may be duplicates), NOTHING_REQUIRED is no operation is required
      Throws:
      StandardException - Thrown on error
      See Also:
    • sortRequired

      public int sortRequired(RowOrdering rowOrdering, JBitSet tableMap, OptimizableList optimizableList, int[] proposedJoinOrder) throws StandardException
      Description copied from interface: RequiredRowOrdering
      Tell whether sorting is required for this RequiredRowOrdering, given a RowOrdering representing a partial join order, and a bit map telling what tables are represented in the join order. This is useful for reducing the number of cases the optimizer has to consider.
      Specified by:
      sortRequired in interface RequiredRowOrdering
      Parameters:
      rowOrdering - The order of rows in the partial join order
      tableMap - A bit map of the tables in the partial join order
      optimizableList - The list of join participants
      proposedJoinOrder - The current join order being considered by the optimizer. We need to look into this to determine if the outer optimizables are single row result set if the order by column is on an inner optimizable and that inner optimizable is not a one. proposedJoinOrder is a map onto optimizableList: it contains indexes of optimizables in that list in the join order proposed, cf. OptimizerImpl#proposedJoinOrder. DERBY-3926 and DERBY-6148
      Returns:
      SORT_REQUIRED if sorting is required, ELIMINATE_DUPS if no sorting is required by duplicates must be eliminated (i.e. the rows are in the right order but there may be duplicates), NOTHING_REQUIRED is no operation is required
      Throws:
      StandardException - Thrown on error
      See Also:
    • estimateCost

      public void estimateCost(double estimatedInputRows, RowOrdering rowOrdering, CostEstimate resultCost) throws StandardException
      Description copied from interface: RequiredRowOrdering
      Estimate the cost of doing a sort for this row ordering, given the number of rows to be sorted. This does not take into account whether the sort is really needed. It also estimates the number of result rows.
      Specified by:
      estimateCost in interface RequiredRowOrdering
      Parameters:
      estimatedInputRows - The estimated number of rows to sort
      rowOrdering - The ordering of the input rows
      resultCost - A place to store the resulting cost
      Throws:
      StandardException - Thrown on error
      See Also:
    • sortNeeded

      public void sortNeeded()
      Description copied from interface: RequiredRowOrdering
      Indicate that a sort is necessary to fulfill this required ordering. This method may be called many times during a single optimization.
      Specified by:
      sortNeeded in interface RequiredRowOrdering
      See Also:
    • sortNotNeeded

      public void sortNotNeeded()
      Description copied from interface: RequiredRowOrdering
      Indicate that a sort is *NOT* necessary to fulfill this required ordering. This method may be called many times during a single optimization.
      Specified by:
      sortNotNeeded in interface RequiredRowOrdering
      See Also:
    • remapColumnReferencesToExpressions

      void remapColumnReferencesToExpressions() throws StandardException
      Remap all ColumnReferences in this tree to be clones of the underlying expression.
      Throws:
      StandardException - Thrown on error
    • getSortNeeded

      public boolean getSortNeeded()
      Get whether or not a sort is needed.
      Specified by:
      getSortNeeded in interface RequiredRowOrdering
      Returns:
      Whether or not a sort is needed.
    • requiresDescending

      boolean requiresDescending(ColumnReference cRef, int numOptimizables) throws StandardException
      Determine whether or not this RequiredRowOrdering has a DESCENDING requirement for the column referenced by the received ColumnReference.
      Throws:
      StandardException
    • toString

      public String toString()
      Description copied from class: QueryTreeNode
      Format this node as a string Each sub-class of QueryTreeNode should implement its own toString() method. In each case, toString() should format the class members that are not sub-types of QueryTreeNode (printSubNodes() takes care of following the references to sub-nodes, and toString() takes care of all members that are not sub-nodes). Newlines should be used liberally - one good way to do this is to have a newline at the end of each formatted member. It's also a good idea to put the name of each member in front of the formatted value. For example, the code might look like: "memberName: " + memberName + "\n" + ... Vector members containing subclasses of QueryTreeNode should subclass QueryTreeNodeVector. Such subclasses form a special case: These classes should not implement printSubNodes, since there is generic handling in QueryTreeNodeVector. They should only implement toString if they contain additional members.
      Overrides:
      toString in class QueryTreeNode
      Returns:
      This node formatted as a String
    • getResultSetNumber

      public int getResultSetNumber()
    • isTableValueCtorOrdering

      public boolean isTableValueCtorOrdering()
      Returns:
      true if the ORDER BY is attached to a <table value constructor>, i.e. a VALUES clause.