Class OrderByList
java.lang.Object
org.apache.derby.impl.sql.compile.QueryTreeNode
org.apache.derby.impl.sql.compile.QueryTreeNodeVector<OrderByColumn>
org.apache.derby.impl.sql.compile.OrderedColumnList<OrderByColumn>
org.apache.derby.impl.sql.compile.OrderByList
- All Implemented Interfaces:
Iterable<OrderByColumn>, RequiredRowOrdering, Visitable
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
FieldsModifier and TypeFieldDescriptionprivate booleanprivate booleanprivate ColumnOrdering[]private intprivate booleantrueif this instance orders a <table value constructor>.private Object[]private intprivate ResultSetNodeprivate SortCostControllerprivate booleanFields inherited from class QueryTreeNodeVector
eltClassFields inherited from class QueryTreeNode
AUTOINCREMENT_CREATE_MODIFY, AUTOINCREMENT_CYCLE, AUTOINCREMENT_INC_INDEX, AUTOINCREMENT_IS_AUTOINCREMENT_INDEX, AUTOINCREMENT_START_INDEXFields inherited from interface RequiredRowOrdering
ELIMINATE_DUPS, NOTHING_REQUIRED, SORT_REQUIRED -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) voidaddOrderByColumn(OrderByColumn column) Add a column to the list(package private) booleanAre 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 listintbooleanGet 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.boolean(package private) voidpullUpOrderByColumns(ResultSetNode target) Pull up Order By columns by their names to the target resultset of the cursor specification.(package private) voidRemap 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) voidRemove 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) voidOrder by columns now point to the PRN above the node of interest.voidIndicate that a sort is necessary to fulfill this required ordering.voidIndicate 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.toString()Format this node as a string Each sub-class of QueryTreeNode should implement its own toString() method.Methods inherited from class OrderedColumnList
getColumnOrderingMethods inherited from class QueryTreeNodeVector
acceptChildren, addElement, destructiveAppend, elementAt, indexOf, insertElementAt, iterator, nondestructiveAppend, printSubNodes, removeAllElements, removeElement, removeElementAt, setElementAt, sizeMethods inherited from class 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, verifyClassExistMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
allAscending
private boolean allAscending -
alwaysSort
private boolean alwaysSort -
resultToSort
-
scc
-
resultRow
-
columnOrdering
-
estimatedRowSize
private int estimatedRowSize -
sortNeeded
private boolean sortNeeded -
resultSetNumber
private int resultSetNumber -
isTableValueCtorOrdering
private boolean isTableValueCtorOrderingtrueif this instance orders a <table value constructor>. SeeisTableValueCtorOrdering.
-
-
Constructor Details
-
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 Details
-
addOrderByColumn
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
Get a column from the list- Parameters:
position- The column to get from the list
-
bindOrderByColumns
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
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
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
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
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:
-
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:
-
estimateCost
public void estimateCost(double estimatedInputRows, RowOrdering rowOrdering, CostEstimate resultCost) throws StandardException Description 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:
-
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:
-
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:
-
remapColumnReferencesToExpressions
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:
getSortNeededin interfaceRequiredRowOrdering- Returns:
- Whether or not a sort is needed.
-
requiresDescending
Determine whether or not this RequiredRowOrdering has a DESCENDING requirement for the column referenced by the received ColumnReference.- Throws:
StandardException
-
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.
-