Class OrderByList
- java.lang.Object
-
- org.apache.derby.impl.sql.compile.QueryTreeNode
-
- org.apache.derby.impl.sql.compile.QueryTreeNodeVector<E>
-
- org.apache.derby.impl.sql.compile.OrderedColumnList<OrderByColumn>
-
- org.apache.derby.impl.sql.compile.OrderByList
-
- All Implemented Interfaces:
java.lang.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 Summary
Fields Modifier and Type Field Description private booleanallAscendingprivate booleanalwaysSortprivate ColumnOrdering[]columnOrderingprivate intestimatedRowSizeprivate booleanisTableValueCtorOrderingtrueif this instance orders a <table value constructor>.private java.lang.Object[]resultRowprivate intresultSetNumberprivate ResultSetNoderesultToSortprivate SortCostControllersccprivate booleansortNeeded-
Fields inherited from class org.apache.derby.impl.sql.compile.QueryTreeNodeVector
eltClass
-
Fields inherited from class org.apache.derby.impl.sql.compile.QueryTreeNode
AUTOINCREMENT_CREATE_MODIFY, AUTOINCREMENT_CYCLE, AUTOINCREMENT_INC_INDEX, AUTOINCREMENT_IS_AUTOINCREMENT_INDEX, AUTOINCREMENT_START_INDEX
-
Fields inherited from interface org.apache.derby.iapi.sql.compile.RequiredRowOrdering
ELIMINATE_DUPS, NOTHING_REQUIRED, SORT_REQUIRED
-
-
Constructor Summary
Constructors Constructor Description OrderByList(ResultSetNode rs, ContextManager cm)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidaddOrderByColumn(OrderByColumn column)Add a column to the list(package private) booleanallAscending()Are all columns in the list ascending.(package private) voidbindOrderByColumns(ResultSetNode target)Bind the update columns by their names to the target resultset of the cursor specification.(package private) voidcloseGap(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.voidestimateCost(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.(package private) voidgenerate(ActivationClassBuilder acb, MethodBuilder mb, ResultSetNode child)generate the sort result set operating over the source expression.(package private) OrderByColumngetOrderByColumn(int position)Get a column from the listintgetResultSetNumber()booleangetSortNeeded()Get whether or not a sort is needed.(package private) booleanisInOrderPrefix(ResultColumnList sourceRCL)Is this order by list an in order prefix of the specified RCL.booleanisTableValueCtorOrdering()(package private) voidpullUpOrderByColumns(ResultSetNode target)Pull up Order By columns by their names to the target resultset of the cursor specification.(package private) voidremapColumnReferencesToExpressions()Remap all ColumnReferences in this tree to be clones of the underlying expression.(package private) voidremoveConstantColumns(PredicateList whereClause)Remove any constant columns from this order by list.(package private) voidremoveDupColumns()Remove any duplicate columns from this order by list.(package private) ResultColumnListreorderRCL(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) booleanrequiresDescending(ColumnReference cRef, int numOptimizables)Determine whether or not this RequiredRowOrdering has a DESCENDING requirement for the column referenced by the received ColumnReference.(package private) voidresetToSourceRCs()Order by columns now point to the PRN above the node of interest.voidsortNeeded()Indicate that a sort is necessary to fulfill this required ordering.voidsortNotNeeded()Indicate that a sort is *NOT* necessary to fulfill this required ordering.intsortRequired(RowOrdering rowOrdering, OptimizableList optimizableList, int[] proposedJoinOrder)Tell whether sorting is required for this RequiredRowOrdering, given a RowOrdering.intsortRequired(RowOrdering rowOrdering, JBitSet tableMap, OptimizableList optimizableList, int[] proposedJoinOrder)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.java.lang.StringtoString()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, iterator, nondestructiveAppend, printSubNodes, removeAllElements, removeElement, removeElementAt, setElementAt, size
-
Methods inherited from class org.apache.derby.impl.sql.compile.QueryTreeNode
accept, addTag, addUDTUsagePriv, addUDTUsagePriv, bindOffsetFetch, bindRowMultiSet, bindUserCatalogType, bindUserType, checkReliability, checkReliability, convertDefaultNode, copyTagsFrom, createTypeDependency, debugFlush, debugPrint, disablePrivilegeCollection, formatNodeString, generate, generateAuthorizeCheck, getBeginOffset, getClassFactory, getCompilerContext, getContext, getContextManager, getDataDictionary, getDependencyManager, getEndOffset, getExecutionFactory, getGenericConstantActionFactory, getIntProperty, getLanguageConnectionContext, getLongProperty, getNullNode, getOffsetOrderedNodes, getOptimizerFactory, getOptimizerTracer, getParameterTypes, getSchemaDescriptor, getSchemaDescriptor, getStatementType, getTableDescriptor, getTypeCompiler, getUDTDesc, isAtomic, isPrivilegeCollectionRequired, isSessionSchema, isSessionSchema, makeConstantAction, makeTableName, makeTableName, nodeHeader, optimizerTracingIsOn, orReliability, parseSearchCondition, parseStatement, printLabel, referencesSessionSchema, resolveTableToSynonym, setBeginOffset, setEndOffset, setRefActionInfo, stackPrint, taggedWith, treePrint, treePrint, verifyClassExist
-
-
-
-
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
-
isTableValueCtorOrdering
private boolean isTableValueCtorOrdering
trueif this instance orders a <table value constructor>. SeeisTableValueCtorOrdering.
-
-
Constructor Detail
-
OrderByList
OrderByList(ResultSetNode rs, ContextManager cm)
Constructor. Initialize with the type of the result set thisOrderByListis attached to, e.g.SELECT,VALUESor a set operation.- Parameters:
rs- The result set thisOrderByListis ordering. May be nullcm- The context manager
-
-
Method Detail
-
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 classmb- 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:RequiredRowOrderingTell whether sorting is required for this RequiredRowOrdering, given a RowOrdering.- Specified by:
sortRequiredin interfaceRequiredRowOrdering- Parameters:
rowOrdering- The order of rows in questionoptimizableList- The list of join participantsproposedJoinOrder- 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.proposedJoinOrderis a map ontooptimizableList: 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:
RequiredRowOrdering.sortRequired(RowOrdering, OptimizableList, int[])
-
sortRequired
public int sortRequired(RowOrdering rowOrdering, JBitSet tableMap, OptimizableList optimizableList, int[] proposedJoinOrder) throws StandardException
Description copied from interface:RequiredRowOrderingTell 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:
sortRequiredin interfaceRequiredRowOrdering- Parameters:
rowOrdering- The order of rows in the partial join ordertableMap- A bit map of the tables in the partial join orderoptimizableList- The list of join participantsproposedJoinOrder- 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.proposedJoinOrderis a map ontooptimizableList: 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:
RequiredRowOrdering.sortRequired(RowOrdering, JBitSet, OptimizableList, int[])
-
estimateCost
public void estimateCost(double estimatedInputRows, RowOrdering rowOrdering, CostEstimate resultCost) throws StandardExceptionDescription copied from interface:RequiredRowOrderingEstimate 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:
estimateCostin interfaceRequiredRowOrdering- Parameters:
estimatedInputRows- The estimated number of rows to sortrowOrdering- The ordering of the input rowsresultCost- 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:RequiredRowOrderingIndicate that a sort is necessary to fulfill this required ordering. This method may be called many times during a single optimization.- Specified by:
sortNeededin interfaceRequiredRowOrdering- See Also:
RequiredRowOrdering.sortNeeded()
-
sortNotNeeded
public void sortNotNeeded()
Description copied from interface:RequiredRowOrderingIndicate that a sort is *NOT* necessary to fulfill this required ordering. This method may be called many times during a single optimization.- Specified by:
sortNotNeededin interfaceRequiredRowOrdering- See Also:
RequiredRowOrdering.sortNotNeeded()
-
remapColumnReferencesToExpressions
void remapColumnReferencesToExpressions() throws StandardExceptionRemap 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:
getSortNeededin interfaceRequiredRowOrdering- 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:QueryTreeNodeFormat 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:
toStringin classQueryTreeNode- Returns:
- This node formatted as a String
-
getResultSetNumber
public int getResultSetNumber()
-
isTableValueCtorOrdering
public boolean isTableValueCtorOrdering()
- Returns:
trueif theORDER BYis attached to a <table value constructor>, i.e. aVALUESclause.
-
-