org.apache.derby.impl.sql.compile
Class OrderByList

java.lang.Object
  extended by org.apache.derby.impl.sql.compile.QueryTreeNode
      extended by org.apache.derby.impl.sql.compile.QueryTreeNodeVector
          extended by org.apache.derby.impl.sql.compile.OrderedColumnList
              extended by org.apache.derby.impl.sql.compile.OrderByList
All Implemented Interfaces:
RequiredRowOrdering, Visitable

public class OrderByList
extends OrderedColumnList
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 Summary
private  boolean allAscending
           
private  boolean alwaysSort
           
private  ColumnOrdering[] columnOrdering
           
private  int estimatedRowSize
           
private  java.lang.Object[] resultRow
           
private  int resultSetNumber
           
private  ResultSetNode resultToSort
           
private  SortCostController scc
           
private  boolean sortNeeded
           
 
Fields inherited from class org.apache.derby.impl.sql.compile.QueryTreeNode
AUTOINCREMENT_CREATE_MODIFY, AUTOINCREMENT_INC_INDEX, AUTOINCREMENT_IS_AUTOINCREMENT_INDEX, AUTOINCREMENT_START_INDEX, isPrivilegeCollectionRequired
 
Fields inherited from interface org.apache.derby.iapi.sql.compile.RequiredRowOrdering
ELIMINATE_DUPS, NOTHING_REQUIRED, SORT_REQUIRED
 
Constructor Summary
OrderByList()
           
 
Method Summary
 void addOrderByColumn(OrderByColumn column)
          Add a column to the list
(package private)  boolean allAscending()
          Are all columns in the list ascending.
 void bindOrderByColumns(ResultSetNode target)
          Bind the update columns by their names to the target resultset of the cursor specification.
(package private)  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.
 void estimateCost(double estimatedInputRows, RowOrdering rowOrdering, CostEstimate resultCost)
          Estimate the cost of doing a sort for this row ordering, given the number of rows to be sorted.
 void generate(ActivationClassBuilder acb, MethodBuilder mb, ResultSetNode child)
          generate the sort result set operating over the source expression.
 OrderByColumn getOrderByColumn(int position)
          Get a column from the list
 int getResultSetNumber()
           
 boolean getSortNeeded()
          Get whether or not a sort is needed.
(package private)  boolean isInOrderPrefix(ResultColumnList sourceRCL)
          Is this order by list an in order prefix of the specified RCL.
 void pullUpOrderByColumns(ResultSetNode target)
          Pull up Order By columns by their names to the target resultset of the cursor specification.
(package private)  void remapColumnReferencesToExpressions()
          Remap all ColumnReferences in this tree to be clones of the underlying expression.
(package private)  void removeConstantColumns(PredicateList whereClause)
          Remove any constant columns from this order by list.
(package private)  void removeDupColumns()
          Remove any duplicate columns from this order by list.
(package private)  ResultColumnList reorderRCL(ResultColumnList resultColumns)
          Build a new RCL with the same RCs as the passed in RCL but in an order that matches the ordering columns.
(package private)  boolean requiresDescending(ColumnReference cRef, int numOptimizables)
          Determine whether or not this RequiredRowOrdering has a DESCENDING requirement for the column referenced by the received ColumnReference.
(package private)  void resetToSourceRCs()
          Order by columns now point to the PRN above the node of interest.
 void sortNeeded()
          Indicate that a sort is necessary to fulfill this required ordering.
 void sortNotNeeded()
          Indicate that a sort is *NOT* necessary to fulfill this required ordering.
 int sortRequired(RowOrdering rowOrdering, JBitSet tableMap, OptimizableList optimizableList)
          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.
 int sortRequired(RowOrdering rowOrdering, OptimizableList optimizableList)
          Tell whether sorting is required for this RequiredRowOrdering, given a RowOrdering.
 java.lang.String toString()
          Format this node as a string Each sub-class of QueryTreeNode should implement its own toString() method.
 
Methods inherited from class org.apache.derby.impl.sql.compile.OrderedColumnList
getColumnOrdering
 
Methods inherited from class org.apache.derby.impl.sql.compile.QueryTreeNodeVector
acceptChildren, addElement, destructiveAppend, elementAt, indexOf, insertElementAt, nondestructiveAppend, printSubNodes, remove, removeAllElements, removeElement, removeElementAt, setElementAt, size
 
Methods inherited from class org.apache.derby.impl.sql.compile.QueryTreeNode
accept, bindOffsetFetch, bindRowMultiSet, bindUserType, checkReliability, checkReliability, convertDefaultNode, createTypeDependency, debugFlush, debugPrint, disablePrivilegeCollection, formatNodeString, foundString, generate, generateAuthorizeCheck, getBeginOffset, getClassFactory, getCompilerContext, getContextManager, getCursorInfo, getDataDictionary, getDependencyManager, getEndOffset, getExecutionFactory, getGenericConstantActionFactory, getIntProperty, getLanguageConnectionContext, getNodeFactory, getNodeType, getNullNode, getParameterTypes, getRowEstimate, getSchemaDescriptor, getSchemaDescriptor, getStatementType, getTableDescriptor, getTypeCompiler, init, init, init, init, init, init, init, init, init, init, init, init, init, init, isAtomic, isInstanceOf, isPrivilegeCollectionRequired, isSessionSchema, isSessionSchema, makeConstantAction, makeTableName, makeTableName, nodeHeader, orReliability, parseStatement, printLabel, referencesSessionSchema, resolveTableToSynonym, setBeginOffset, setContextManager, setEndOffset, setNodeType, setRefActionInfo, stackPrint, treePrint, treePrint, verifyClassExist
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

allAscending

private boolean allAscending

alwaysSort

private boolean alwaysSort

resultToSort

private ResultSetNode resultToSort

scc

private SortCostController scc

resultRow

private java.lang.Object[] resultRow

columnOrdering

private ColumnOrdering[] columnOrdering

estimatedRowSize

private int estimatedRowSize

sortNeeded

private boolean sortNeeded

resultSetNumber

private int resultSetNumber
Constructor Detail

OrderByList

public OrderByList()
Method Detail

addOrderByColumn

public 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

public OrderByColumn getOrderByColumn(int position)
Get a column from the list

Parameters:
position - The column to get from the list

bindOrderByColumns

public void bindOrderByColumns(ResultSetNode target)
                        throws StandardException
Bind the update columns by their names to the target resultset of the cursor specification. This variant is used by InsertNode.

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

public 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

public 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)
                 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 current join order being considered by the optimizer. We need to look into this to determine if the outer optimizables are single row resultset if the order by column is on an inner optimizable and that inner optimizable is not a one row resultset. DERBY-3926
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:
RequiredRowOrdering.sortRequired(org.apache.derby.iapi.sql.compile.RowOrdering, org.apache.derby.iapi.sql.compile.OptimizableList)

sortRequired

public int sortRequired(RowOrdering rowOrdering,
                        JBitSet tableMap,
                        OptimizableList optimizableList)
                 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 current join order being considered by the optimizer. We need to look into this to determine if the outer optimizables are single row resultset if the order by column is on an inner optimizable and that inner optimizable is not a one row resultset. DERBY-3926
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:
RequiredRowOrdering.sortRequired(org.apache.derby.iapi.sql.compile.RowOrdering, org.apache.derby.iapi.sql.compile.OptimizableList)

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:
RequiredRowOrdering.estimateCost(double, org.apache.derby.iapi.sql.compile.RowOrdering, org.apache.derby.iapi.sql.compile.CostEstimate)

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:
RequiredRowOrdering.sortNeeded()

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:
RequiredRowOrdering.sortNotNeeded()

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 java.lang.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()

Built on Thu 2011-03-10 11:54:14+0000, from revision ???

Apache Derby V10.6 Internals - Copyright © 2004,2007 The Apache Software Foundation. All Rights Reserved.