Top Description Fields Constructors Methods
org.apache.derby.impl.sql.compile

pack-priv Class OrderByList

extends OrderedColumnList<OrderByColumn>
implements RequiredRowOrdering
Class Inheritance
All Implemented Interfaces
org.apache.derby.iapi.sql.compile.RequiredRowOrdering
Imports
org.apache.derby.shared.common.error.StandardException, org.apache.derby.shared.common.reference.ClassName, .Limits, .SQLState, org.apache.derby.iapi.services.classfile.VMOpcode, org.apache.derby.iapi.services.compiler.MethodBuilder, org.apache.derby.iapi.services.context.ContextManager, org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.iapi.sql.compile.CompilerContext, .CostEstimate, .Optimizable, .OptimizableList, .RequiredRowOrdering, .RowOrdering, org.apache.derby.iapi.store.access.ColumnOrdering, .SortCostController, org.apache.derby.iapi.types.DataValueDescriptor, org.apache.derby.iapi.util.JBitSet

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

Modifier and TypeField and Description
private boolean
private boolean
private ColumnOrdering[]
private int
private boolean
isTableValueCtorOrdering

true if this instance orders a <table value constructor>.

private Object[]
private int
private ResultSetNode
private SortCostController
private boolean

Constructor Summary

AccessConstructor and Description
pack-priv
OrderByList(ResultSetNode
The result set this OrderByList is ordering. May be null
rs
,
ContextManager
The context manager
cm
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
addOrderByColumn(OrderByColumn
The column to add to the list
column
)

Add a column to the list

pack-priv boolean

Returns:

Whether or not all columns in the list ascending.
allAscending
()

Are all columns in the list ascending.

pack-priv void
bindOrderByColumns(ResultSetNode
The underlying result set
target
)

Bind the update columns by their names to the target resultset of the cursor specification.

pack-priv void
closeGap(int
column which has been removed from the result column list
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.

public void
estimateCost(double
The estimated number of rows to sort
estimatedInputRows
,
RowOrdering
The ordering of the input rows
rowOrdering
,
CostEstimate
A place to store the resulting cost
resultCost
)

Implements org.apache.derby.iapi.sql.compile.RequiredRowOrdering.estimateCost.

Estimate the cost of doing a sort for this row ordering, given the number of rows to be sorted.

pack-priv void
generate(ActivationClassBuilder
the tool for building the class
acb
,
MethodBuilder
the method the generated code is to go into
mb
,
ResultSetNode child)

generate the sort result set operating over the source expression.

pack-priv OrderByColumn
getOrderByColumn(int
The column to get from the list
position
)

Get a column from the list

public int
public boolean

Returns:

Whether or not a sort is needed.
getSortNeeded
()

Implements org.apache.derby.iapi.sql.compile.RequiredRowOrdering.getSortNeeded.

Get whether or not a sort is needed.

pack-priv boolean

Returns:

Whether or not this order by list an in order prefix of the specified RCL.
isInOrderPrefix
(ResultColumnList
The source RCL.
sourceRCL
)

Is this order by list an in order prefix of the specified RCL.

public boolean

Returns:

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

pack-priv void
pullUpOrderByColumns(ResultSetNode
The underlying result set
target
)

Pull up Order By columns by their names to the target resultset of the cursor specification.

pack-priv void
remapColumnReferencesToExpressions()

Remap all ColumnReferences in this tree to be clones of the underlying expression.

pack-priv void
removeConstantColumns(PredicateList whereClause)

Remove any constant columns from this order by list.

pack-priv void
removeDupColumns()

Remove any duplicate columns from this order by list.

pack-priv ResultColumnList
reorderRCL(ResultColumnList
The RCL to reorder.
resultColumns
)

Build a new RCL with the same RCs as the passed in RCL but in an order that matches the ordering columns.

pack-priv boolean
requiresDescending(ColumnReference cRef, int numOptimizables)

Determine whether or not this RequiredRowOrdering has a DESCENDING requirement for the column referenced by the received ColumnReference.

pack-priv void
resetToSourceRCs()

Order by columns now point to the PRN above the node of interest.

public void
sortNeeded()

Implements org.apache.derby.iapi.sql.compile.RequiredRowOrdering.sortNeeded.

Indicate that a sort is necessary to fulfill this required ordering.

public void
sortNotNeeded()

Implements org.apache.derby.iapi.sql.compile.RequiredRowOrdering.sortNotNeeded.

Indicate that a sort is *NOT* necessary to fulfill this required ordering.

public int
sortRequired(RowOrdering
The order of rows in question
rowOrdering
,
OptimizableList
The list of join participants
optimizableList
,
int[]
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.
proposedJoinOrder
)

Implements org.apache.derby.iapi.sql.compile.RequiredRowOrdering.sortRequired.

Tell whether sorting is required for this RequiredRowOrdering, given a RowOrdering.

public int
sortRequired(RowOrdering
The order of rows in the partial join order
rowOrdering
,
JBitSet
A bit map of the tables in the partial join order
tableMap
,
OptimizableList
The list of join participants
optimizableList
,
int[]
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
proposedJoinOrder
)

Implements org.apache.derby.iapi.sql.compile.RequiredRowOrdering.sortRequired.

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.

public String
toString()

Overrides org.apache.derby.impl.sql.compile.QueryTreeNode.toString.

Format this node as a string Each sub-class of QueryTreeNode should implement its own toString() method.

Inherited from org.apache.derby.impl.sql.compile.OrderedColumnList:
getColumnOrdering

Field Detail

allAscendingback to summary
private boolean allAscending
alwaysSortback to summary
private boolean alwaysSort
columnOrderingback to summary
private ColumnOrdering[] columnOrdering
estimatedRowSizeback to summary
private int estimatedRowSize
isTableValueCtorOrderingback to summary
private boolean isTableValueCtorOrdering

true if this instance orders a <table value constructor>. See isTableValueCtorOrdering.

resultRowback to summary
private Object[] resultRow
resultSetNumberback to summary
private int resultSetNumber
resultToSortback to summary
private ResultSetNode resultToSort
sccback to summary
private SortCostController scc
sortNeededback to summary
private boolean sortNeeded

Constructor Detail

OrderByListback to summary
pack-priv 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:ResultSetNode

The result set this OrderByList is ordering. May be null

cm:ContextManager

The context manager

Method Detail

addOrderByColumnback to summary
pack-priv void addOrderByColumn(OrderByColumn column)

Add a column to the list

Parameters
column:OrderByColumn

The column to add to the list

allAscendingback to summary
pack-priv boolean allAscending()

Are all columns in the list ascending.

Returns:boolean

Whether or not all columns in the list ascending.

bindOrderByColumnsback to summary
pack-priv void bindOrderByColumns(ResultSetNode target) throws StandardException

Bind the update columns by their names to the target resultset of the cursor specification.

Parameters
target:ResultSetNode

The underlying result set

Exceptions
StandardException:
Thrown on error
closeGapback to summary
pack-priv 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:int

column which has been removed from the result column list

estimateCostback to summary
public void estimateCost(double estimatedInputRows, RowOrdering rowOrdering, CostEstimate resultCost) throws StandardException

Implements org.apache.derby.iapi.sql.compile.RequiredRowOrdering.estimateCost.

Doc from org.apache.derby.iapi.sql.compile.RequiredRowOrdering.estimateCost.

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.

Parameters
estimatedInputRows:double

The estimated number of rows to sort

rowOrdering:RowOrdering

The ordering of the input rows

resultCost:CostEstimate

A place to store the resulting cost

Exceptions
StandardException:
Thrown on error
See Also
RequiredRowOrdering#estimateCost
generateback to summary
pack-priv void generate(ActivationClassBuilder acb, MethodBuilder mb, ResultSetNode child) throws StandardException

generate the sort result set operating over the source expression.

Parameters
acb:ActivationClassBuilder

the tool for building the class

mb:MethodBuilder

the method the generated code is to go into

Exceptions
StandardException:
thrown on failure
getOrderByColumnback to summary
pack-priv OrderByColumn getOrderByColumn(int position)

Get a column from the list

Parameters
position:int

The column to get from the list

getResultSetNumberback to summary
public int getResultSetNumber()
getSortNeededback to summary
public boolean getSortNeeded()

Implements org.apache.derby.iapi.sql.compile.RequiredRowOrdering.getSortNeeded.

Get whether or not a sort is needed.

Returns:boolean

Whether or not a sort is needed.

isInOrderPrefixback to summary
pack-priv 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:ResultColumnList

The source RCL.

Returns:boolean

Whether or not this order by list an in order prefix of the specified RCL.

isTableValueCtorOrderingback to summary
public boolean isTableValueCtorOrdering()
Returns:boolean

true if the ORDER BY is attached to a <table value constructor>, i.e. a VALUES clause.

pullUpOrderByColumnsback to summary
pack-priv void pullUpOrderByColumns(ResultSetNode target) throws StandardException

Pull up Order By columns by their names to the target resultset of the cursor specification.

Parameters
target:ResultSetNode

The underlying result set

remapColumnReferencesToExpressionsback to summary
pack-priv void remapColumnReferencesToExpressions() throws StandardException

Remap all ColumnReferences in this tree to be clones of the underlying expression.

Exceptions
StandardException:
Thrown on error
removeConstantColumnsback to summary
pack-priv 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.

removeDupColumnsback to summary
pack-priv 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.

reorderRCLback to summary
pack-priv 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:ResultColumnList

The RCL to reorder.

Exceptions
StandardException:
Thrown on error
requiresDescendingback to summary
pack-priv 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.

resetToSourceRCsback to summary
pack-priv 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.

sortNeededback to summary
public void sortNeeded()

Implements org.apache.derby.iapi.sql.compile.RequiredRowOrdering.sortNeeded.

Doc from org.apache.derby.iapi.sql.compile.RequiredRowOrdering.sortNeeded.

Indicate that a sort is necessary to fulfill this required ordering. This method may be called many times during a single optimization.

See Also
RequiredRowOrdering#sortNeeded
sortNotNeededback to summary
public void sortNotNeeded()

Implements org.apache.derby.iapi.sql.compile.RequiredRowOrdering.sortNotNeeded.

Doc from org.apache.derby.iapi.sql.compile.RequiredRowOrdering.sortNotNeeded.

Indicate that a sort is *NOT* necessary to fulfill this required ordering. This method may be called many times during a single optimization.

See Also
RequiredRowOrdering#sortNotNeeded
sortRequiredback to summary
public int sortRequired(RowOrdering rowOrdering, OptimizableList optimizableList, int[] proposedJoinOrder) throws StandardException

Implements org.apache.derby.iapi.sql.compile.RequiredRowOrdering.sortRequired.

Doc from org.apache.derby.iapi.sql.compile.RequiredRowOrdering.sortRequired.

Tell whether sorting is required for this RequiredRowOrdering, given a RowOrdering.

Parameters
rowOrdering:RowOrdering

The order of rows in question

optimizableList:OptimizableList

The list of join participants

proposedJoinOrder:int[]

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:int

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

Exceptions
StandardException:
Thrown on error
See Also
RequiredRowOrdering#sortRequired(RowOrdering, OptimizableList, int[])
sortRequiredback to summary
public int sortRequired(RowOrdering rowOrdering, JBitSet tableMap, OptimizableList optimizableList, int[] proposedJoinOrder) throws StandardException

Implements org.apache.derby.iapi.sql.compile.RequiredRowOrdering.sortRequired.

Doc from org.apache.derby.iapi.sql.compile.RequiredRowOrdering.sortRequired.

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.

Parameters
rowOrdering:RowOrdering

The order of rows in the partial join order

tableMap:JBitSet

A bit map of the tables in the partial join order

optimizableList:OptimizableList

The list of join participants

proposedJoinOrder:int[]

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:int

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

Exceptions
StandardException:
Thrown on error
See Also
RequiredRowOrdering#sortRequired(RowOrdering, JBitSet, OptimizableList, int[])
toStringback to summary
public String toString()

Overrides org.apache.derby.impl.sql.compile.QueryTreeNode.toString.

Doc from org.apache.derby.impl.sql.compile.QueryTreeNode.toString.

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.

Returns:String

This node formatted as a String

Annotations
@Override