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

java.lang.Object
  extended by org.apache.derby.impl.sql.compile.QueryTreeNode
      extended by org.apache.derby.impl.sql.compile.OrderedColumn
          extended by org.apache.derby.impl.sql.compile.OrderByColumn
All Implemented Interfaces:
Visitable

public class OrderByColumn
extends OrderedColumn

An OrderByColumn is a column in the ORDER BY clause. An OrderByColumn can be ordered ascending or descending. We need to make sure that the named columns are columns in that query, and that positions are within range.


Field Summary
private  int addedColumnOffset
          If this sort key is added to the result column list then it is at result column position 1 + resultColumnList.size() - resultColumnList.getOrderBySelect() + addedColumnOffset If the sort key is already in the result column list then addedColumnOffset < 0.
private  boolean ascending
           
private  ValueNode expression
           
private  OrderByList list
           
private  boolean nullsOrderedLow
           
private  ResultColumn resultCol
           
 
Fields inherited from class org.apache.derby.impl.sql.compile.OrderedColumn
columnPosition, UNMATCHEDPOSITION
 
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
 
Constructor Summary
OrderByColumn()
           
 
Method Summary
(package private)  void acceptChildren(Visitor v)
          Accept the visitor for all visitable children of this node.
 void bindOrderByColumn(ResultSetNode target, OrderByList oblist)
          Bind this column.
(package private)  void clearAddedColumnOffset()
          Reset addedColumnOffset to indicate that column is no longer added An added column is one which was artificially added to the result column list due to its presence in the ORDER BY clause, as opposed to having been explicitly selected by the user.
(package private)  void collapseAddedColumnGap(int gap)
          Adjust addedColumnOffset to reflect that a column has been removed This routine is called when a previously-added result column has been removed due to being detected as a duplicate.
(package private)  boolean constantColumn(PredicateList whereClause)
          Is this OrderByColumn constant, according to the given predicate list?
(package private)  ValueNode getNonRedundantExpression()
          Get the underlying expression, skipping over ResultColumns that are marked redundant.
(package private)  ResultColumn getResultColumn()
          Get the underlying ResultColumn.
 void init(java.lang.Object expression)
          Initializer.
 boolean isAscending()
          Get the column order.
 boolean isNullsOrderedLow()
          Get the column NULL ordering.
private static boolean isReferedColByNum(ValueNode expression)
           
 void printSubNodes(int depth)
          Prints the sub-nodes of this object.
 void pullUpOrderByColumn(ResultSetNode target)
          Pull up this orderby column if it doesn't appear in the resultset
(package private)  void remapColumnReferencesToExpressions()
          Remap all the column references under this OrderByColumn to their expressions.
(package private)  void resetToSourceRC()
          Order by columns now point to the PRN above the node of interest.
private  void resolveAddedColumn(ResultSetNode target)
          Assuming this OrderByColumn was "pulled" into the received target's ResultColumnList (because it wasn't there to begin with), use this.addedColumnOffset to figure out which of the target's result columns is the one corresponding to "this".
private  ResultColumn resolveColumnReference(ResultSetNode target, ColumnReference cr)
           
 void setDescending()
          Mark the column as descending order
 void setNullsOrderedLow()
          Mark the column as ordered NULL values lower than non-NULL values.
 java.lang.String toString()
          Convert this object to a String.
 
Methods inherited from class org.apache.derby.impl.sql.compile.OrderedColumn
getColumnPosition, setColumnPosition
 
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, 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

resultCol

private ResultColumn resultCol

ascending

private boolean ascending

nullsOrderedLow

private boolean nullsOrderedLow

expression

private ValueNode expression

list

private OrderByList list

addedColumnOffset

private int addedColumnOffset
If this sort key is added to the result column list then it is at result column position 1 + resultColumnList.size() - resultColumnList.getOrderBySelect() + addedColumnOffset If the sort key is already in the result column list then addedColumnOffset < 0.

Constructor Detail

OrderByColumn

public OrderByColumn()
Method Detail

init

public void init(java.lang.Object expression)
Initializer.

Overrides:
init in class QueryTreeNode
Parameters:
expression - Expression of this column

toString

public java.lang.String toString()
Convert this object to a String. See comments in QueryTreeNode.java for how this should be done for tree printing.

Overrides:
toString in class OrderedColumn
Returns:
This object as a String

printSubNodes

public void printSubNodes(int depth)
Prints the sub-nodes of this object. See QueryTreeNode.java for how tree printing is supposed to work.

Overrides:
printSubNodes in class QueryTreeNode
Parameters:
depth - The depth of this node in the tree

setDescending

public void setDescending()
Mark the column as descending order


isAscending

public boolean isAscending()
Get the column order. Overrides OrderedColumn.isAscending.

Overrides:
isAscending in class OrderedColumn
Returns:
true if ascending, false if descending

setNullsOrderedLow

public void setNullsOrderedLow()
Mark the column as ordered NULL values lower than non-NULL values.


isNullsOrderedLow

public boolean isNullsOrderedLow()
Get the column NULL ordering. Overrides OrderedColumn.getIsNullsOrderedLow.

Overrides:
isNullsOrderedLow in class OrderedColumn
Returns:
true if NULLs ordered low, false if NULLs ordered high

getResultColumn

ResultColumn getResultColumn()
Get the underlying ResultColumn.

Returns:
The underlying ResultColumn.

getNonRedundantExpression

ValueNode getNonRedundantExpression()
Get the underlying expression, skipping over ResultColumns that are marked redundant.


bindOrderByColumn

public void bindOrderByColumn(ResultSetNode target,
                              OrderByList oblist)
                       throws StandardException
Bind this column. During binding, we may discover that this order by column was pulled up into the result column list, but is now a duplicate, because the actual result column was expanded into the result column list when "*" expressions were replaced with the list of the table's columns. In such a situation, we will end up calling back to the OrderByList to adjust the addedColumnOffset values of the columns; the "oblist" parameter exists to allow that callback to be performed.

Parameters:
target - The result set being selected from
oblist - OrderByList which contains this column
Throws:
StandardException - Thrown on error
StandardException - Thrown when column not found

resolveAddedColumn

private void resolveAddedColumn(ResultSetNode target)
Assuming this OrderByColumn was "pulled" into the received target's ResultColumnList (because it wasn't there to begin with), use this.addedColumnOffset to figure out which of the target's result columns is the one corresponding to "this". The desired position is w.r.t. the original, user-specified result column list--which is what "visibleSize()" gives us. I.e. To get this OrderByColumn's position in target's RCL, first subtract out all columns which were "pulled" into the RCL for GROUP BY or ORDER BY, then add "this.addedColumnOffset". As an example, if the query was: select sum(j) as s from t1 group by i, k order by k, sum(k) then we will internally add columns "K" and "SUM(K)" to the RCL for ORDER BY, *AND* we will add a generated column "I" to the RCL for GROUP BY. Thus we end up with four result columns: (1) (2) (3) (4) select sum(j) as s, K, SUM(K), I from t1 ... So when we get here and we want to find out which column "this" corresponds to, we begin by taking the total number of VISIBLE columns, which is 1 (i.e. 4 total columns minus 1 GROUP BY column minus 2 ORDER BY columns). Then we add this.addedColumnOffset in order to find the target column position. Since addedColumnOffset is 0-based, an addedColumnOffset value of "0" means we want the the first ORDER BY column added to target's RCL, "1" means we want the second ORDER BY column added, etc. So if we assume that this.addedColumnOffset is "1" in this example then we add that to the RCL's "visible size". And finally, we add 1 more to account for fact that addedColumnOffset is 0-based while column positions are 1-based. This gives: position = 1 + 1 + 1 = 3 which points to SUM(K) in the RCL. Thus an addedColumnOffset value of "1" resolves to column SUM(K) in target's RCL; similarly, an addedColumnOffset value of "0" resolves to "K". DERBY-3303.


pullUpOrderByColumn

public void pullUpOrderByColumn(ResultSetNode target)
                         throws StandardException
Pull up this orderby column if it doesn't appear in the resultset

Parameters:
target - The result set being selected from
Throws:
StandardException

resetToSourceRC

void resetToSourceRC()
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.


constantColumn

boolean constantColumn(PredicateList whereClause)
Is this OrderByColumn constant, according to the given predicate list? A constant column is one where all the column references it uses are compared equal to constants.


remapColumnReferencesToExpressions

void remapColumnReferencesToExpressions()
                                  throws StandardException
Remap all the column references under this OrderByColumn to their expressions.

Throws:
StandardException - Thrown on error

isReferedColByNum

private static boolean isReferedColByNum(ValueNode expression)
                                  throws StandardException
Throws:
StandardException

resolveColumnReference

private ResultColumn resolveColumnReference(ResultSetNode target,
                                            ColumnReference cr)
                                     throws StandardException
Throws:
StandardException

clearAddedColumnOffset

void clearAddedColumnOffset()
Reset addedColumnOffset to indicate that column is no longer added An added column is one which was artificially added to the result column list due to its presence in the ORDER BY clause, as opposed to having been explicitly selected by the user. Since * is not expanded until after the ORDER BY columns have been pulled up, we may add a column, then later decide it is a duplicate of an explicitly selected column. In that case, this method is called, and it does the following: - resets addedColumnOffset to -1 to indicate this is not an added col - calls back to the OrderByList to adjust any other added cols


collapseAddedColumnGap

void collapseAddedColumnGap(int gap)
Adjust addedColumnOffset to reflect that a column has been removed This routine is called when a previously-added result column has been removed due to being detected as a duplicate. If that added column had a lower offset than our column, we decrement our offset to reflect that we have just been moved down one slot in the result column list.

Parameters:
gap - offset of the column which has just been removed from list

acceptChildren

void acceptChildren(Visitor v)
              throws StandardException
Accept the visitor for all visitable children of this node.

Overrides:
acceptChildren in class QueryTreeNode
Parameters:
v - the visitor
Throws:
StandardException - on error

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.