Class SqlAggFunction

All Implemented Interfaces:
Context, Wrapper
Direct Known Subclasses:
MockSqlOperatorTable.MyAggFunc, MockSqlOperatorTable.MyAvgAggFunction, SqlAbstractGroupFunction, SqlAnyValueAggFunction, SqlAvgAggFunction, SqlBasicAggFunction, SqlBitOpAggFunction, SqlCountAggFunction, SqlCovarAggFunction, SqlFirstLastValueAggFunction, SqlHistogramAggFunction, SqlJsonArrayAggAggFunction, SqlJsonObjectAggAggFunction, SqlLeadLagAggFunction, SqlMinMaxAggFunction, SqlNthValueAggFunction, SqlNtileAggFunction, SqlRankFunction, SqlSingleValueAggFunction, SqlSumAggFunction, SqlSumEmptyIsZeroAggFunction, SqlUserDefinedAggFunction

public abstract class SqlAggFunction extends SqlFunction implements Context
Abstract base class for the definition of an aggregate function: an operator which aggregates sets of values into a result.
See Also:
  • Constructor Details

  • Method Details

    • unwrap

      public <T> @Nullable T unwrap(Class<T> clazz)
      Description copied from interface: Wrapper
      Finds an instance of an interface implemented by this object, or returns null if this object does not support that interface.
      Specified by:
      unwrap in interface Wrapper
    • isAggregator

      public boolean isAggregator()
      Description copied from class: SqlOperator
      Returns whether this operator is an aggregate function. By default, subclass type is used (an instance of SqlAggFunction is assumed to be an aggregator; anything else is not).

      Per SQL:2011, there are aggregate functions and window functions. Every aggregate function (e.g. SUM) is also a window function. There are window functions that are not aggregate functions, e.g. RANK, NTILE, LEAD, FIRST_VALUE.

      Collectively, aggregate and window functions are called analytic functions. Despite its name, this method returns true for every analytic function.

      isAggregator in class SqlOperator
      whether this operator is an analytic function (aggregate function or window function)
      See Also:
    • isQuantifierAllowed

      public boolean isQuantifierAllowed()
      Description copied from class: SqlFunction
      Returns whether this function allows a DISTINCT or ALL quantifier. The default is false; some aggregate functions return true.
      isQuantifierAllowed in class SqlFunction
    • 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.apache.calcite.sql.validate.SqlValidator, org.apache.calcite.sql.validate.SqlValidatorScope); but note that some sub-classes of SqlCall never call this method.

      validateCall in class SqlFunction
      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:
    • requiresOrder

      public final boolean requiresOrder()
      Description copied from class: SqlOperator
      Returns whether this is a window function that requires ordering.

      Per SQL:2011, 2, 6.10: "If <ntile function>, <lead or lag function>, RANK or DENSE_RANK is specified, then the window ordering clause shall be present."

      requiresOrder in class SqlOperator
      See Also:
    • requiresGroupOrder

      public Optionality requiresGroupOrder()
      Returns whether this aggregate function must, may, or must not contain a WITHIN GROUP (ORDER ...) clause.


      • If Optionality.MANDATORY, then AGG(x) WITHIN GROUP (ORDER BY 1) is valid, and AGG(x) is invalid.
      • If Optionality.OPTIONAL, then AGG(x) WITHIN GROUP (ORDER BY 1) and AGG(x) are both valid.
      • If Optionality.IGNORED, then AGG(x) is valid, and AGG(x) WITHIN GROUP (ORDER BY 1) is valid but is treated the same as AGG(x).
      • If Optionality.FORBIDDEN, then AGG(x) WITHIN GROUP (ORDER BY 1) is invalid, and AGG(x) is valid.
    • requiresOver

      public final boolean requiresOver()
      Description copied from class: SqlOperator
      Returns whether this is a window function that requires an OVER clause.

      For example, returns true for RANK, DENSE_RANK and other ranking functions; returns false for SUM, COUNT, MIN, MAX, AVG (they can be used as non-window aggregate functions).

      If requiresOver returns true, then SqlOperator.isAggregator() must also return true.

      requiresOver in class SqlOperator
      See Also:
    • getDistinctOptionality

      public Optionality getDistinctOptionality()
      Returns whether this aggregate function allows the DISTINCT keyword.

      The default implementation returns Optionality.OPTIONAL, which is appropriate for most aggregate functions, including SUM and COUNT.

      Some aggregate functions, for example MIN, produce the same result with or without DISTINCT, and therefore return Optionality.IGNORED to indicate this. For such functions, Calcite will probably remove DISTINCT while optimizing the query.

    • getParameterTypes

      @Deprecated public List<RelDataType> getParameterTypes(RelDataTypeFactory typeFactory)
    • getReturnType

      @Deprecated public RelDataType getReturnType(RelDataTypeFactory typeFactory)
    • allowsFilter

      public boolean allowsFilter()
      Whether this aggregate function allows a FILTER (WHERE ...) clause.
    • allowsNullTreatment

      public boolean allowsNullTreatment()
      Returns whether this aggregate function allows specifying null treatment (RESPECT NULLS or IGNORE NULLS).
    • getRollup

      public @Nullable SqlAggFunction getRollup()
      Gets rollup aggregation function.
    • isPercentile

      public boolean isPercentile()
      Returns whether this aggregate function is a PERCENTILE function. Such functions require a WITHIN GROUP clause that has precisely one sort key.

      NOTE: This API is experimental and subject to change without notice.