Class SqlWindow

All Implemented Interfaces:
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.

  • Field Details

    • FOLLOWING_OPERATOR

      public static final SqlPostfixOperator FOLLOWING_OPERATOR
      The FOLLOWING operator used exclusively in a window specification.
    • PRECEDING_OPERATOR

      public static final SqlPostfixOperator PRECEDING_OPERATOR
      The PRECEDING operator used exclusively in a window specification.
  • Constructor Details

  • Method Details

    • create

      public static SqlWindow create(@Nullable SqlIdentifier declName, @Nullable SqlIdentifier refName, SqlNodeList partitionList, SqlNodeList orderList, SqlLiteral isRows, @Nullable SqlNode lowerBound, @Nullable SqlNode upperBound, @Nullable SqlLiteral allowPartial, SqlParserPos pos)
    • getOperator

      public SqlOperator getOperator()
      Specified by:
      getOperator in class SqlCall
    • getKind

      public SqlKind getKind()
      Description copied from class: SqlNode
      Returns the type of node this is, or SqlKind.OTHER if it's nothing special.
      Overrides:
      getKind in class SqlCall
      Returns:
      a SqlKind value, never null
      See Also:
    • getOperandList

      public List<SqlNode> getOperandList()
      Description copied from class: SqlCall
      Returns the list of operands. The set and order of operands is call-specific.

      Note: the proper type would be List<@Nullable SqlNode>, however, it would trigger too many changes to the current codebase.

      Specified by:
      getOperandList in class SqlCall
      Returns:
      the list of call operands, never null, the operands can be null
    • setOperand

      public void setOperand(int i, @Nullable SqlNode operand)
      Description copied from class: SqlCall
      Changes the value of an operand. Allows some rewrite by SqlValidator; use sparingly.
      Overrides:
      setOperand in class SqlCall
      Parameters:
      i - Operand index
      operand - 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 and rightPrec 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).

      Overrides:
      unparse in class SqlCall
      Parameters:
      writer - Target writer
      leftPrec - The precedence of the SqlNode immediately preceding this node in a depth-first scan of the parse tree
      rightPrec - The precedence of the SqlNode immediately
    • getDeclName

      public @Nullable SqlIdentifier getDeclName()
    • setDeclName

      public void setDeclName(SqlIdentifier declName)
    • getLowerBound

      public @Nullable SqlNode getLowerBound()
    • setLowerBound

      public void setLowerBound(@Nullable SqlNode lowerBound)
    • getUpperBound

      public @Nullable SqlNode getUpperBound()
    • setUpperBound

      public void setUpperBound(@Nullable 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:
    • isAlwaysNonEmpty

      public static boolean isAlwaysNonEmpty(RexWindowBound lower, RexWindowBound upper)
    • setRows

      public void setRows(SqlLiteral isRows)
    • isRows

      @Pure 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 @Nullable SqlIdentifier getRefName()
    • setWindowCall

      public void setWindowCall(@Nullable SqlCall windowCall)
    • getWindowCall

      public @Nullable SqlCall getWindowCall()
    • 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
    • equalsDeep

      public boolean equalsDeep(@Nullable SqlNode node, Litmus litmus)
      Overridden method to specifically check only the right subtree of a window definition.
      Overrides:
      equalsDeep in class SqlCall
      Parameters:
      node - The SqlWindow to compare to "this" window
      litmus - What to do if an error is detected (nodes are not equal)
      Returns:
      boolean true if all nodes in the subtree are equal
    • isAllowPartial

      @EnsuresNonNullIf(expression="allowPartial", result=false) 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 example SqlSelect and SqlUpdate).

      Overrides:
      validate in class SqlCall
      Parameters:
      scope - Validator
    • 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
    • populateBounds

      @Deprecated public void populateBounds()
      Deprecated.