Class 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 Detail

      • ascending

        private boolean ascending
      • nullsOrderedLow

        private boolean nullsOrderedLow
      • 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 column
        cm - 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 class OrderedColumn
        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 class QueryTreeNode
        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 class OrderedColumn
        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 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

        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
      • 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 set
        crOfExpression - 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
      • 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