Class SqlWindow
- java.lang.Object
-
- org.apache.calcite.sql.SqlNode
-
- org.apache.calcite.sql.SqlCall
-
- org.apache.calcite.sql.SqlWindow
-
- All Implemented Interfaces:
java.lang.Cloneable
public class SqlWindow extends SqlCall
SQL window specification.For example, the query
SELECT sum(a) OVER (w ROWS 3 PRECEDING) FROM t WINDOW w AS (PARTITION BY x, y ORDER BY z), w1 AS (w ROWS 5 PRECEDING UNBOUNDED FOLLOWING)
declares windows w and w1, and uses a window in an OVER clause. It thus contains 3
SqlWindow
objects.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
SqlWindow.Bound
An enumeration of types of bounds in a window:CURRENT ROW
,UNBOUNDED PRECEDING
, andUNBOUNDED FOLLOWING
.private static class
SqlWindow.SqlWindowOperator
An operator describing a window specification.
-
Field Summary
Fields Modifier and Type Field Description (package private) SqlLiteral
allowPartial
Whether to allow partial results.(package private) SqlIdentifier
declName
The name of the window being declared.(package private) static SqlPostfixOperator
FOLLOWING_OPERATOR
The FOLLOWING operator used exclusively in a window specification.(package private) SqlLiteral
isRows
Whether it is a physical (rows) or logical (values) range.(package private) SqlNode
lowerBound
The lower bound of the window.(package private) SqlNodeList
orderList
The list of ordering columns.(package private) SqlNodeList
partitionList
The list of partitioning columns.(package private) static SqlPostfixOperator
PRECEDING_OPERATOR
The PRECEDING operator used exclusively in a window specification.(package private) SqlIdentifier
refName
The name of the window being referenced, or null.(package private) SqlNode
upperBound
The upper bound of the window.private SqlCall
windowCall
-
Fields inherited from class org.apache.calcite.sql.SqlNode
EMPTY_ARRAY, pos
-
-
Constructor Summary
Constructors Constructor Description SqlWindow(SqlParserPos pos, SqlIdentifier declName, SqlIdentifier refName, SqlNodeList partitionList, SqlNodeList orderList, SqlLiteral isRows, SqlNode lowerBound, SqlNode upperBound, SqlLiteral allowPartial)
Creates a window.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) static void
checkSpecialLiterals(SqlWindow window, SqlValidator validator)
static SqlWindow
create(SqlIdentifier declName, SqlIdentifier refName, SqlNodeList partitionList, SqlNodeList orderList, SqlLiteral isRows, SqlNode lowerBound, SqlNode upperBound, SqlLiteral allowPartial, SqlParserPos pos)
static SqlNode
createBound(SqlLiteral range)
static SqlNode
createCurrentRow(SqlParserPos pos)
SqlWindow
createCurrentRowWindow(java.lang.String columnName)
Creates a window(RANGE columnName CURRENT ROW)
.static SqlNode
createFollowing(SqlNode e, SqlParserPos pos)
static SqlNode
createPreceding(SqlNode e, SqlParserPos pos)
static SqlNode
createUnboundedFollowing(SqlParserPos pos)
static SqlNode
createUnboundedPreceding(SqlParserPos pos)
SqlWindow
createUnboundedPrecedingWindow(java.lang.String columnName)
Creates a window(RANGE columnName UNBOUNDED PRECEDING)
.boolean
equalsDeep(SqlNode node, Litmus litmus)
Overridden method to specifically check only the right subtree of a window definition.SqlIdentifier
getDeclName()
SqlKind
getKind()
Returns the type of node this is, orSqlKind.OTHER
if it's nothing special.SqlNode
getLowerBound()
java.util.List<SqlNode>
getOperandList()
SqlOperator
getOperator()
SqlNodeList
getOrderList()
SqlNodeList
getPartitionList()
SqlIdentifier
getRefName()
SqlNode
getUpperBound()
SqlCall
getWindowCall()
boolean
isAllowPartial()
Returns whether partial windows are allowed.boolean
isAlwaysNonEmpty()
Returns if the window is guaranteed to have rows.static boolean
isCurrentRow(SqlNode node)
Returns whether an expression represents the "CURRENT ROW" bound.boolean
isRows()
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.SqlWindow
overlay(SqlWindow that, SqlValidator validator)
Creates a new window by combining this one with another.void
populateBounds()
Fill in missing bounds.void
setDeclName(SqlIdentifier declName)
void
setLowerBound(SqlNode lowerBound)
void
setOperand(int i, SqlNode operand)
Changes the value of an operand.private static boolean
setOperand(SqlNode clonedOperand, SqlNode thatOperand, SqlValidator validator)
void
setOrderList(SqlNodeList orderList)
void
setPartitionList(SqlNodeList partitionList)
void
setRows(SqlLiteral isRows)
void
setUpperBound(SqlNode upperBound)
void
setWindowCall(SqlCall windowCall)
void
unparse(SqlWriter writer, int leftPrec, int rightPrec)
Writes a SQL representation of this node to a writer.void
validate(SqlValidator validator, SqlValidatorScope scope)
Validates this call.private void
validateFrameBoundary(SqlNode bound, boolean isRows, SqlTypeFamily orderTypeFam, SqlValidator validator, SqlValidatorScope scope)
-
Methods inherited from class org.apache.calcite.sql.SqlCall
accept, clone, findValidOptions, getCallSignature, getFunctionQuantifier, getMonotonicity, isCountStar, isExpanded, operand, operandCount
-
Methods inherited from class org.apache.calcite.sql.SqlNode
clone, clone, cloneArray, equalDeep, equalDeep, equalsDeep, getParserPosition, isA, toSqlString, toSqlString, toString, validateExpr
-
-
-
-
Field Detail
-
FOLLOWING_OPERATOR
static final SqlPostfixOperator FOLLOWING_OPERATOR
The FOLLOWING operator used exclusively in a window specification.
-
PRECEDING_OPERATOR
static final SqlPostfixOperator PRECEDING_OPERATOR
The PRECEDING operator used exclusively in a window specification.
-
declName
SqlIdentifier declName
The name of the window being declared.
-
refName
SqlIdentifier refName
The name of the window being referenced, or null.
-
partitionList
SqlNodeList partitionList
The list of partitioning columns.
-
orderList
SqlNodeList orderList
The list of ordering columns.
-
isRows
SqlLiteral isRows
Whether it is a physical (rows) or logical (values) range.
-
lowerBound
SqlNode lowerBound
The lower bound of the window.
-
upperBound
SqlNode upperBound
The upper bound of the window.
-
allowPartial
SqlLiteral allowPartial
Whether to allow partial results. It may be null.
-
windowCall
private SqlCall windowCall
-
-
Constructor Detail
-
SqlWindow
public SqlWindow(SqlParserPos pos, SqlIdentifier declName, SqlIdentifier refName, SqlNodeList partitionList, SqlNodeList orderList, SqlLiteral isRows, SqlNode lowerBound, SqlNode upperBound, SqlLiteral allowPartial)
Creates a window.
-
-
Method Detail
-
create
public static SqlWindow create(SqlIdentifier declName, SqlIdentifier refName, SqlNodeList partitionList, SqlNodeList orderList, SqlLiteral isRows, SqlNode lowerBound, SqlNode upperBound, SqlLiteral allowPartial, SqlParserPos pos)
-
getOperator
public SqlOperator getOperator()
- Specified by:
getOperator
in classSqlCall
-
getKind
public SqlKind getKind()
Description copied from class:SqlNode
Returns the type of node this is, orSqlKind.OTHER
if it's nothing special.- Overrides:
getKind
in classSqlCall
- Returns:
- a
SqlKind
value, never null - See Also:
SqlNode.isA(java.util.Set<org.apache.calcite.sql.SqlKind>)
-
getOperandList
public java.util.List<SqlNode> getOperandList()
- Specified by:
getOperandList
in classSqlCall
-
setOperand
public void setOperand(int i, SqlNode operand)
Description copied from class:SqlCall
Changes the value of an operand. Allows some rewrite bySqlValidator
; use sparingly.- Overrides:
setOperand
in classSqlCall
- Parameters:
i
- Operand indexoperand
- Operand value
-
unparse
public void unparse(SqlWriter writer, int leftPrec, int rightPrec)
Description copied from class:SqlNode
Writes a SQL representation of this node to a writer.The
leftPrec
andrightPrec
parameters give us enough context to decide whether we need to enclose the expression in parentheses. For example, we need parentheses around "2 + 3" if preceded by "5 *". This is because the precedence of the "*" operator is greater than the precedence of the "+" operator.The algorithm handles left- and right-associative operators by giving them slightly different left- and right-precedence.
If
SqlWriter.isAlwaysUseParentheses()
is true, we use parentheses even when they are not required by the precedence rules.For the details of this algorithm, see
SqlCall.unparse(org.apache.calcite.sql.SqlWriter, int, int)
.
-
getDeclName
public SqlIdentifier getDeclName()
-
setDeclName
public void setDeclName(SqlIdentifier declName)
-
getLowerBound
public SqlNode getLowerBound()
-
setLowerBound
public void setLowerBound(SqlNode lowerBound)
-
getUpperBound
public SqlNode getUpperBound()
-
setUpperBound
public void setUpperBound(SqlNode upperBound)
-
isAlwaysNonEmpty
public boolean isAlwaysNonEmpty()
Returns if the window is guaranteed to have rows. This is useful to refine data type of window aggregates. For instance sum(non-nullable) over (empty window) is NULL.- Returns:
- true when the window is non-empty
- See Also:
Window.Group.isAlwaysNonEmpty()
,SqlOperatorBinding.getGroupCount()
,SqlValidatorImpl.resolveWindow(SqlNode, org.apache.calcite.sql.validate.SqlValidatorScope, boolean)
-
setRows
public void setRows(SqlLiteral isRows)
-
isRows
public boolean isRows()
-
getOrderList
public SqlNodeList getOrderList()
-
setOrderList
public void setOrderList(SqlNodeList orderList)
-
getPartitionList
public SqlNodeList getPartitionList()
-
setPartitionList
public void setPartitionList(SqlNodeList partitionList)
-
getRefName
public SqlIdentifier getRefName()
-
setWindowCall
public void setWindowCall(SqlCall windowCall)
-
getWindowCall
public SqlCall getWindowCall()
-
checkSpecialLiterals
static void checkSpecialLiterals(SqlWindow window, SqlValidator validator)
- See Also:
Util.deprecated(Object, boolean)
-
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(SqlNode e, SqlParserPos pos)
-
createPreceding
public static SqlNode createPreceding(SqlNode e, 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.
-
overlay
public SqlWindow overlay(SqlWindow that, SqlValidator validator)
Creates a new window by combining this one with another.For example,
WINDOW (w PARTITION BY x ORDER BY y) overlay WINDOW w AS (PARTITION BY z)
yields
WINDOW (PARTITION BY z ORDER BY y)
Does not alter this or the other window.
- Returns:
- A new window
-
setOperand
private static boolean setOperand(SqlNode clonedOperand, SqlNode thatOperand, SqlValidator validator)
-
equalsDeep
public boolean equalsDeep(SqlNode node, Litmus litmus)
Overridden method to specifically check only the right subtree of a window definition.- Overrides:
equalsDeep
in classSqlCall
- Parameters:
node
- The SqlWindow to compare to "this" windowlitmus
- What to do if an error is detected (nodes are not equal)- Returns:
- boolean true if all nodes in the subtree are equal
-
isAllowPartial
public boolean isAllowPartial()
Returns whether partial windows are allowed. If false, a partial window (for example, a window of size 1 hour which has only 45 minutes of data in it) will appear to windowed aggregate functions to be empty.
-
validate
public void validate(SqlValidator validator, SqlValidatorScope scope)
Description copied from class:SqlCall
Validates this call.The default implementation delegates the validation to the operator's
SqlOperator.validateCall(org.apache.calcite.sql.SqlCall, org.apache.calcite.sql.validate.SqlValidator, org.apache.calcite.sql.validate.SqlValidatorScope, org.apache.calcite.sql.validate.SqlValidatorScope)
. Derived classes may override (as do, for exampleSqlSelect
andSqlUpdate
).
-
validateFrameBoundary
private void validateFrameBoundary(SqlNode bound, boolean isRows, SqlTypeFamily orderTypeFam, SqlValidator validator, SqlValidatorScope scope)
-
createCurrentRowWindow
public SqlWindow createCurrentRowWindow(java.lang.String columnName)
Creates a window(RANGE columnName CURRENT ROW)
.- Parameters:
columnName
- Order column
-
createUnboundedPrecedingWindow
public SqlWindow createUnboundedPrecedingWindow(java.lang.String columnName)
Creates a window(RANGE columnName UNBOUNDED PRECEDING)
.- Parameters:
columnName
- Order column
-
populateBounds
public void populateBounds()
Fill in missing bounds. Default bounds are "BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" when ORDER BY present and "BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" when no ORDER BY present.
-
-