org.eigenbase.sql
Class SqlWindowOperator

java.lang.Object
  extended by org.eigenbase.sql.SqlOperator
      extended by org.eigenbase.sql.SqlWindowOperator

public class SqlWindowOperator
extends SqlOperator

An operator describing a window specification.

Operands are as follows:

All operands are optional.

Since:
Oct 19, 2004
Version:
$Id: //open/dev/farrago/src/org/eigenbase/sql/SqlWindowOperator.java#23 $
Author:
jhyde

Nested Class Summary
(package private) static class SqlWindowOperator.Bound
          An enumeration of types of bounds in a window: CURRENT ROW, UNBOUNDED PRECEDING, and UNBOUNDED FOLLOWING.
static class SqlWindowOperator.OffsetRange
           
private static class SqlWindowOperator.ValSign
           
 
Field Summary
(package private) static SqlPostfixOperator followingOperator
          The FOLLOWING operator used exclusively in a window specification.
(package private) static SqlPostfixOperator precedingOperator
          The PRECEDING operator used exclusively in a window specification.
 
Fields inherited from class org.eigenbase.sql.SqlOperator
MaxPrec, NL
 
Constructor Summary
SqlWindowOperator()
           
 
Method Summary
<R> void
acceptCall(SqlVisitor<R> visitor, SqlCall call, boolean onlyExpressions, SqlBasicVisitor.ArgHandler<R> argHandler)
          Accepts a SqlVisitor, directing an SqlBasicVisitor.ArgHandler to visit operand of a call.
private static void checkSpecialLiterals(SqlWindow window, SqlValidator validator)
           
static SqlNode createBound(SqlLiteral range)
           
 SqlWindow createCall(SqlIdentifier declName, SqlIdentifier refName, SqlNodeList partitionList, SqlNodeList orderList, SqlLiteral isRows, SqlNode lowerBound, SqlNode upperBound, SqlLiteral allowPartial, SqlParserPos pos)
           
 SqlCall createCall(SqlLiteral functionQualifier, SqlParserPos pos, SqlNode... operands)
          Creates a call to this operand with an array of operands.
static SqlNode createCurrentRow(SqlParserPos pos)
           
 SqlWindow createCurrentRowWindow(String columnName)
          Creates a window (RANGE columnName CURRENT ROW).
static SqlNode createFollowing(SqlLiteral literal, SqlParserPos pos)
           
static SqlNode createPreceding(SqlLiteral literal, SqlParserPos pos)
           
static SqlNode createUnboundedFollowing(SqlParserPos pos)
           
static SqlNode createUnboundedPreceding(SqlParserPos pos)
           
 SqlWindow createUnboundedPrecedingWindow(String columnName)
          Creates a window (RANGE columnName UNBOUNDED PRECEDING).
static SqlWindowOperator.OffsetRange getOffsetAndRange(SqlNode lowerBound, SqlNode upperBound, boolean physical)
          Converts a pair of bounds into a (range, offset) pair.
private static SqlWindowOperator.ValSign getRangeOffset(SqlNode node, SqlPostfixOperator op)
          Decodes a node, representing an upper or lower bound to a window, into a range offset.
 SqlSyntax getSyntax()
          Returns the syntactic type of this operator.
static boolean isCurrentRow(SqlNode node)
          Returns whether an expression represents the "CURRENT ROW" bound.
private static boolean isTableSorted(SqlValidatorScope scope)
          This method retrieves the list of columns for the current table then walks through the list looking for a column that is monotonic (sorted)
static boolean isUnboundedFollowing(SqlNode node)
          Returns whether an expression represents the "UNBOUNDED FOLLOWING" bound.
static boolean isUnboundedPreceding(SqlNode node)
          Returns whether an expression represents the "UNBOUNDED PRECEDING" bound.
 void unparse(SqlWriter writer, SqlNode[] operands, int leftPrec, int rightPrec)
          Writes a SQL representation of a call to this operator to a writer, including parentheses if the operators on either side are of greater precedence.
 void validateCall(SqlCall call, SqlValidator validator, SqlValidatorScope scope, SqlValidatorScope operandScope)
          Validates a call to this operator.
private  void validateFrameBoundary(SqlNode bound, boolean isRows, SqlTypeFamily orderTypeFam, SqlValidator validator, SqlValidatorScope scope)
           
 
Methods inherited from class org.eigenbase.sql.SqlOperator
acceptCall, adjustType, argumentMustBeScalar, checkOperandCount, checkOperandTypes, createCall, createCall, createCall, deriveType, equals, getAllowedSignatures, getAllowedSignatures, getKind, getLeftPrec, getMonotonicity, getName, getOperandCountRange, getOperandTypeChecker, getOperandTypeInference, getReturnTypeInference, getRightPrec, getSignatureTemplate, hashCode, inferReturnType, inferReturnType, isAggregator, isDeterministic, isDynamicFunction, isName, leftPrec, preValidateCall, requiresDecimalExpansion, rewriteCall, rightPrec, toString, unparseListClause, unparseListClause, validateOperands
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

followingOperator

static final SqlPostfixOperator followingOperator
The FOLLOWING operator used exclusively in a window specification.


precedingOperator

static final SqlPostfixOperator precedingOperator
The PRECEDING operator used exclusively in a window specification.

Constructor Detail

SqlWindowOperator

public SqlWindowOperator()
Method Detail

getSyntax

public SqlSyntax getSyntax()
Description copied from class: SqlOperator
Returns the syntactic type of this operator.

Specified by:
getSyntax in class SqlOperator

createCall

public SqlCall createCall(SqlLiteral functionQualifier,
                          SqlParserPos pos,
                          SqlNode... operands)
Description copied from class: SqlOperator
Creates a call to this operand with an array of operands.

The position of the resulting call is the union of the pos and the positions of all of the operands.

Overrides:
createCall in class SqlOperator
Parameters:
functionQualifier - function qualifier (e.g. "DISTINCT"), may be
pos - parser position of the identifier of the call
operands - array of operands

createCall

public SqlWindow createCall(SqlIdentifier declName,
                            SqlIdentifier refName,
                            SqlNodeList partitionList,
                            SqlNodeList orderList,
                            SqlLiteral isRows,
                            SqlNode lowerBound,
                            SqlNode upperBound,
                            SqlLiteral allowPartial,
                            SqlParserPos pos)

acceptCall

public <R> void acceptCall(SqlVisitor<R> visitor,
                           SqlCall call,
                           boolean onlyExpressions,
                           SqlBasicVisitor.ArgHandler<R> argHandler)
Description copied from class: SqlOperator
Accepts a SqlVisitor, directing an SqlBasicVisitor.ArgHandler to visit operand of a call. The argument handler allows fine control about how the operands are visited, and how the results are combined.

Overrides:
acceptCall in class SqlOperator
Parameters:
visitor - Visitor
call - Call to visit
onlyExpressions - If true, ignores operands which are not expressions. For example, in the call to the AS operator
argHandler - Called for each operand

unparse

public void unparse(SqlWriter writer,
                    SqlNode[] operands,
                    int leftPrec,
                    int rightPrec)
Description copied from class: SqlOperator
Writes a SQL representation of a call to this operator to a writer, including parentheses if the operators on either side are of greater precedence.

The default implementation of this method delegates to SqlSyntax.unparse(org.eigenbase.sql.SqlWriter, org.eigenbase.sql.SqlOperator, org.eigenbase.sql.SqlNode[], int, int).

Overrides:
unparse in class SqlOperator

validateCall

public void validateCall(SqlCall call,
                         SqlValidator validator,
                         SqlValidatorScope scope,
                         SqlValidatorScope operandScope)
Description copied from class: SqlOperator
Validates a call to this operator.

This method should not perform type-derivation or perform validation related related to types. That is done later, by SqlOperator.deriveType(SqlValidator, SqlValidatorScope, SqlCall). This method should focus on structural validation.

A typical implementation of this method first validates the operands, then performs some operator-specific logic. The default implementation just validates the operands.

This method is the default implementation of SqlCall.validate(org.eigenbase.sql.validate.SqlValidator, org.eigenbase.sql.validate.SqlValidatorScope); but note that some sub-classes of SqlCall never call this method.

Overrides:
validateCall in class SqlOperator
Parameters:
call - the call to this operator
validator - the active validator
scope - validator scope
operandScope - validator scope in which to validate operands to this call; usually equal to scope, but not always because some operators introduce new scopes
See Also:
SqlNode.validateExpr(SqlValidator, SqlValidatorScope), SqlOperator.deriveType(SqlValidator, SqlValidatorScope, SqlCall)

validateFrameBoundary

private void validateFrameBoundary(SqlNode bound,
                                   boolean isRows,
                                   SqlTypeFamily orderTypeFam,
                                   SqlValidator validator,
                                   SqlValidatorScope scope)

checkSpecialLiterals

private static void checkSpecialLiterals(SqlWindow window,
                                         SqlValidator validator)

isTableSorted

private static boolean isTableSorted(SqlValidatorScope scope)
This method retrieves the list of columns for the current table then walks through the list looking for a column that is monotonic (sorted)


createCurrentRowWindow

public SqlWindow createCurrentRowWindow(String columnName)
Creates a window (RANGE columnName CURRENT ROW).

Parameters:
columnName - Order column

createUnboundedPrecedingWindow

public SqlWindow createUnboundedPrecedingWindow(String columnName)
Creates a window (RANGE columnName UNBOUNDED PRECEDING).

Parameters:
columnName - Order column

createCurrentRow

public static SqlNode createCurrentRow(SqlParserPos pos)

createUnboundedFollowing

public static SqlNode createUnboundedFollowing(SqlParserPos pos)

createUnboundedPreceding

public static SqlNode createUnboundedPreceding(SqlParserPos pos)

createFollowing

public static SqlNode createFollowing(SqlLiteral literal,
                                      SqlParserPos pos)

createPreceding

public static SqlNode createPreceding(SqlLiteral literal,
                                      SqlParserPos pos)

createBound

public static SqlNode createBound(SqlLiteral range)

isCurrentRow

public static boolean isCurrentRow(SqlNode node)
Returns whether an expression represents the "CURRENT ROW" bound.


isUnboundedPreceding

public static boolean isUnboundedPreceding(SqlNode node)
Returns whether an expression represents the "UNBOUNDED PRECEDING" bound.


isUnboundedFollowing

public static boolean isUnboundedFollowing(SqlNode node)
Returns whether an expression represents the "UNBOUNDED FOLLOWING" bound.


getOffsetAndRange

public static SqlWindowOperator.OffsetRange getOffsetAndRange(SqlNode lowerBound,
                                                              SqlNode upperBound,
                                                              boolean physical)
Converts a pair of bounds into a (range, offset) pair.

If the upper bound is unbounded, returns null, since that cannot be represented as a (range, offset) pair. (The offset would be +infinity, but what would the range be?)

Parameters:
lowerBound - Lower bound
upperBound - Upper bound
physical - Whether interval is physical (rows), as opposed to logical (values)
Returns:
range-offset pair, or null

getRangeOffset

private static SqlWindowOperator.ValSign getRangeOffset(SqlNode node,
                                                        SqlPostfixOperator op)
Decodes a node, representing an upper or lower bound to a window, into a range offset. For example, '3 FOLLOWING' is 3, '3 PRECEDING' is -3, and 'UNBOUNDED PRECEDING' or 'UNBOUNDED FOLLOWING' is null.

Parameters:
node - Node representing window bound
op - Either precedingOperator or followingOperator
Returns:
range