Class Aggregate

All Implemented Interfaces:
ExpressionWithFlags, HasSQL, Typed

public class Aggregate extends AbstractAggregate implements ExpressionWithFlags
Implements the integrated aggregate functions, such as COUNT, MAX, SUM.
  • Field Details

    • ADDITIONAL_SUM_PRECISION

      private static final int ADDITIONAL_SUM_PRECISION
      The additional result precision in decimal digits for a SUM aggregate function.
      See Also:
    • ADDITIONAL_AVG_SCALE

      private static final int ADDITIONAL_AVG_SCALE
      The additional precision and scale in decimal digits for an AVG aggregate function.
      See Also:
    • AGGREGATES

      private static final HashMap<String,AggregateType> AGGREGATES
    • aggregateType

      private final AggregateType aggregateType
    • orderByList

      private ArrayList<QueryOrderBy> orderByList
    • orderBySort

      private SortOrder orderBySort
    • extraArguments

      private Object extraArguments
    • flags

      private int flags
  • Constructor Details

    • Aggregate

      public Aggregate(AggregateType aggregateType, Expression[] args, Select select, boolean distinct)
      Create a new aggregate object.
      Parameters:
      aggregateType - the aggregate type
      args - the aggregated expressions
      select - the select statement
      distinct - if distinct is used
  • Method Details

    • addAggregate

      private static void addAggregate(String name, AggregateType type)
    • getAggregateType

      public static AggregateType getAggregateType(String name)
      Get the aggregate type for this name, or -1 if no aggregate has been found.
      Parameters:
      name - the aggregate function name
      Returns:
      null if no aggregate function has been found, or the aggregate type
    • setOrderByList

      public void setOrderByList(ArrayList<QueryOrderBy> orderByList)
      Set the order for ARRAY_AGG() or GROUP_CONCAT() aggregate.
      Parameters:
      orderByList - the order by list
    • getAggregateType

      public AggregateType getAggregateType()
      Returns the type of this aggregate.
      Returns:
      the type of this aggregate
    • setExtraArguments

      public void setExtraArguments(Object extraArguments)
      Sets the additional arguments.
      Parameters:
      extraArguments - the additional arguments
    • getExtraArguments

      public Object getExtraArguments()
      Returns the additional arguments.
      Returns:
      the additional arguments
    • setFlags

      public void setFlags(int flags)
      Description copied from interface: ExpressionWithFlags
      Set the flags for this expression.
      Specified by:
      setFlags in interface ExpressionWithFlags
      Parameters:
      flags - the flags to set
    • getFlags

      public int getFlags()
      Description copied from interface: ExpressionWithFlags
      Returns the flags.
      Specified by:
      getFlags in interface ExpressionWithFlags
      Returns:
      the flags
    • sortWithOrderBy

      private void sortWithOrderBy(Value[] array)
    • updateAggregate

      protected void updateAggregate(SessionLocal session, Object aggregateData)
      Description copied from class: AbstractAggregate
      Updates an aggregate value.
      Specified by:
      updateAggregate in class AbstractAggregate
      Parameters:
      session - the session
      aggregateData - aggregate data
    • updateData

      private void updateData(SessionLocal session, AggregateData data, Value v, Value[] remembered)
    • getSecondValue

      private Value getSecondValue(SessionLocal session, Value[] remembered)
    • updateGroupAggregates

      protected void updateGroupAggregates(SessionLocal session, int stage)
      Description copied from class: DataAnalysisOperation
      Invoked when processing group stage of grouped window queries to update arguments of this aggregate.
      Overrides:
      updateGroupAggregates in class AbstractAggregate
      Parameters:
      session - the session
      stage - select stage
    • updateCollecting

      private Value updateCollecting(SessionLocal session, Value v, Value[] remembered)
    • getNumExpressions

      protected int getNumExpressions()
      Description copied from class: DataAnalysisOperation
      Returns the number of expressions, excluding OVER clause.
      Specified by:
      getNumExpressions in class DataAnalysisOperation
      Returns:
      the number of expressions
    • rememberExpressions

      protected void rememberExpressions(SessionLocal session, Value[] array)
      Description copied from class: DataAnalysisOperation
      Stores current values of expressions into the specified array.
      Specified by:
      rememberExpressions in class DataAnalysisOperation
      Parameters:
      session - the session
      array - array to store values of expressions
    • updateFromExpressions

      protected void updateFromExpressions(SessionLocal session, Object aggregateData, Value[] array)
      Description copied from class: AbstractAggregate
      Updates the provided aggregate data from the remembered expressions.
      Specified by:
      updateFromExpressions in class AbstractAggregate
      Parameters:
      session - the session
      aggregateData - aggregate data
      array - values of expressions
    • createAggregateData

      protected Object createAggregateData()
      Description copied from class: DataAnalysisOperation
      Create aggregate data object specific to the subclass.
      Specified by:
      createAggregateData in class DataAnalysisOperation
      Returns:
      aggregate-specific data object.
    • getValue

      public Value getValue(SessionLocal session)
      Description copied from class: Expression
      Return the resulting value for the current row.
      Overrides:
      getValue in class DataAnalysisOperation
      Parameters:
      session - the session
      Returns:
      the result
    • getValueQuick

      private Value getValueQuick(SessionLocal session)
    • getAggregatedValue

      public Value getAggregatedValue(SessionLocal session, Object aggregateData)
      Description copied from class: DataAnalysisOperation
      Returns aggregated value.
      Specified by:
      getAggregatedValue in class DataAnalysisOperation
      Parameters:
      session - the session
      aggregateData - the aggregate data
      Returns:
      aggregated value.
    • collect

      private static Value collect(SessionLocal session, AggregateDataCollecting c, AggregateData d)
    • getHypotheticalSet

      private Value getHypotheticalSet(SessionLocal session, AggregateData data)
    • getRank

      private Value getRank(Value[] ordered, Value arg, Comparator<Value> sort)
    • getCumeDist

      private static Value getCumeDist(Value[] ordered, Value arg, Comparator<Value> sort)
    • getListagg

      private Value getListagg(SessionLocal session, AggregateData data)
    • getListaggError

      private StringBuilder getListaggError(Value[] array, String separator)
    • getListaggTruncate

      private StringBuilder getListaggTruncate(Value[] array, String separator, String filter, boolean withoutCount)
    • getListaggItem

      private String getListaggItem(Value v)
    • getHistogram

      private Value getHistogram(SessionLocal session, AggregateData data)
    • getMode

      private Value getMode(SessionLocal session, AggregateData data)
    • mapColumnsAnalysis

      public void mapColumnsAnalysis(ColumnResolver resolver, int level, int innerState)
      Description copied from class: DataAnalysisOperation
      Map the columns of the resolver to expression columns.
      Overrides:
      mapColumnsAnalysis in class AbstractAggregate
      Parameters:
      resolver - the column resolver
      level - the subquery nesting level
      innerState - one of the Expression MAP_IN_* values
    • optimize

      public Expression optimize(SessionLocal session)
      Description copied from class: Expression
      Try to optimize the expression.
      Overrides:
      optimize in class AbstractAggregate
      Parameters:
      session - the session
      Returns:
      the optimized expression
    • getSumType

      private static TypeInfo getSumType(TypeInfo type)
    • getAvgType

      private static TypeInfo getAvgType(TypeInfo type)
    • setEvaluatable

      public void setEvaluatable(TableFilter tableFilter, boolean b)
      Description copied from class: Expression
      Tell the expression columns whether the table filter can return values now. This is used when optimizing the query.
      Overrides:
      setEvaluatable in class AbstractAggregate
      Parameters:
      tableFilter - the table filter
      b - true if the table filter can return value
    • getUnenclosedSQL

      public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags)
      Description copied from class: Expression
      Get the SQL statement of this expression. This may not always be the original SQL statement, especially after optimization. Enclosing '(' and ')' are never appended.
      Specified by:
      getUnenclosedSQL in class Expression
      Parameters:
      builder - string builder
      sqlFlags - formatting flags
      Returns:
      the specified string builder
    • getSQLArrayAggregate

      private StringBuilder getSQLArrayAggregate(StringBuilder builder, int sqlFlags)
    • getSQLListagg

      private StringBuilder getSQLListagg(StringBuilder builder, int sqlFlags)
    • getSQLJsonObjectAggregate

      private StringBuilder getSQLJsonObjectAggregate(StringBuilder builder, int sqlFlags)
    • getSQLJsonArrayAggregate

      private StringBuilder getSQLJsonArrayAggregate(StringBuilder builder, int sqlFlags)
    • getMinMaxColumnIndex

      private Index getMinMaxColumnIndex()
    • isEverything

      public boolean isEverything(ExpressionVisitor visitor)
      Description copied from class: Expression
      Check if this expression and all sub-expressions can fulfill a criteria. If any part returns false, the result is false.
      Overrides:
      isEverything in class DataAnalysisOperation
      Parameters:
      visitor - the visitor
      Returns:
      if the criteria can be fulfilled
    • getCost

      public int getCost()
      Description copied from class: Expression
      Estimate the cost to process the expression. Used when optimizing the query, to calculate the query plan with the lowest estimated cost.
      Specified by:
      getCost in class Expression
      Returns:
      the estimated cost
    • getSelect

      public Select getSelect()
      Returns the select statement.
      Returns:
      the select statement
    • isDistinct

      public boolean isDistinct()
      Returns if distinct is used.
      Returns:
      if distinct is used