Class OrderByColumn
- java.lang.Object
-
- org.apache.derby.impl.sql.compile.QueryTreeNode
-
- org.apache.derby.impl.sql.compile.OrderedColumn
-
- org.apache.derby.impl.sql.compile.OrderByColumn
-
- All Implemented Interfaces:
Visitable
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
Fields Modifier and Type Field Description 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_CYCLE, AUTOINCREMENT_INC_INDEX, AUTOINCREMENT_IS_AUTOINCREMENT_INDEX, AUTOINCREMENT_START_INDEX
-
-
Constructor Summary
Constructors Constructor Description OrderByColumn(ValueNode expression, ContextManager cm)
Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) void
acceptChildren(Visitor v)
Accept the visitor for all visitable children of this node.(package private) 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.private boolean
columnMatchFound(ResultSetNode target, ColumnReference crOfExpression)
This method checks a ColumnReference of this OrderByColumn against the ColumnReferences of the select clause of the query.(package private) boolean
constantColumn(PredicateList whereClause)
Is this OrderByColumn constant, according to the given predicate list?private boolean
expressionMatch(ResultSetNode target)
Checks whether the whole expression (OrderByColumn) itself found in the select clause.(package private) ValueNode
getExpression()
(package private) ValueNode
getNonRedundantExpression()
Get the underlying expression, skipping over ResultColumns that are marked redundant.(package private) ResultColumn
getResultColumn()
Get the underlying ResultColumn.(package private) boolean
isAscending()
Get the column order.(package private) boolean
isNullsOrderedLow()
Get the column NULL ordering.private static boolean
isReferedColByNum(ValueNode expression)
(package private) void
printSubNodes(int depth)
Prints the sub-nodes of this object.(package private) 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)
(package private) void
setDescending()
Mark the column as descending order(package private) 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, 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
-
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
OrderByColumn(ValueNode expression, ContextManager cm)
Constructor.- Parameters:
expression
- Expression of this columncm
- The context manager
-
-
Method Detail
-
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 classOrderedColumn
- Returns:
- This object as a String
-
printSubNodes
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 classQueryTreeNode
- Parameters:
depth
- The depth of this node in the tree
-
setDescending
void setDescending()
Mark the column as descending order
-
isAscending
boolean isAscending()
Get the column order. Overrides OrderedColumn.isAscending.- Overrides:
isAscending
in classOrderedColumn
- Returns:
- true if ascending, false if descending
-
setNullsOrderedLow
void setNullsOrderedLow()
Mark the column as ordered NULL values lower than non-NULL values.
-
isNullsOrderedLow
boolean isNullsOrderedLow()
Get the column NULL ordering. Overrides OrderedColumn.getIsNullsOrderedLow.- Overrides:
isNullsOrderedLow
in classOrderedColumn
- 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
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 fromoblist
- OrderByList which contains this column- Throws:
StandardException
- Thrown on errorStandardException
- Thrown when column not found
-
expressionMatch
private boolean expressionMatch(ResultSetNode target) throws StandardException
Checks whether the whole expression (OrderByColumn) itself found in the select clause.- Parameters:
target
- Result set- Returns:
- boolean: whether any expression match found
- Throws:
StandardException
-
columnMatchFound
private boolean columnMatchFound(ResultSetNode target, ColumnReference crOfExpression) throws StandardException
This method checks a ColumnReference of this OrderByColumn against the ColumnReferences of the select clause of the query.- Parameters:
target
- result setcrOfExpression
- the CR to be checked- Returns:
- whether a match found or not
- Throws:
StandardException
-
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
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 classQueryTreeNode
- Parameters:
v
- the visitor- Throws:
StandardException
- on error
-
getExpression
ValueNode getExpression()
-
-