com.mckoi.database
Class AbstractAggregateFunction

java.lang.Object
  extended by com.mckoi.database.AbstractFunction
      extended by com.mckoi.database.AbstractAggregateFunction
All Implemented Interfaces:
Function

public abstract class AbstractAggregateFunction
extends AbstractFunction

Provides convenience methods for handling aggregate functions (functions that are evaluated over a grouping set). Note that this class handles the most common form of aggregate functions. These are aggregates with no more or no less than one parameter, and that return NULL if the group set has a length of 0. If an aggregate function doesn't fit this design, then the developer must roll their own AbstractFunction to handle it.

This object handles full expressions being passed as parameters to the aggregate function. The expression is evaluated for each set in the group. Therefore the aggregate function, avg(length(description)) will find the average length of the description column. sum(price * quantity) will find the sum of the price * quantity of each set in the group.

Author:
Tobias Downer

Constructor Summary
AbstractAggregateFunction(java.lang.String name, Expression[] params)
          Constructs an aggregate function.
 
Method Summary
abstract  TObject evalAggregate(GroupResolver group, QueryContext context, TObject val1, TObject val2)
          Evaluates the aggregate function for the given values and returns the result.
 TObject evaluate(GroupResolver group, VariableResolver resolver, QueryContext context)
          Evaluates the function and returns a TObject that represents the result of the function.
 TObject postEvalAggregate(GroupResolver group, QueryContext context, TObject result)
          Called just before the value is returned to the parent.
 
Methods inherited from class com.mckoi.database.AbstractFunction
allElements, allVariables, getName, getParameter, init, isAggregate, isGlob, parameterCount, prepareParameters, returnTType, returnTType, setAggregate, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractAggregateFunction

public AbstractAggregateFunction(java.lang.String name,
                                 Expression[] params)
Constructs an aggregate function.

Method Detail

evalAggregate

public abstract TObject evalAggregate(GroupResolver group,
                                      QueryContext context,
                                      TObject val1,
                                      TObject val2)
Evaluates the aggregate function for the given values and returns the result. If this aggregate was 'sum' then this method would sum the two values. If this aggregate was 'avg' then this method would also sum the two values and the 'postEvalAggregate' would divide by the number processed.

NOTE: This first time this method is called on a set, 'val1' is 'null' and 'val2' contains the first value in the set.


postEvalAggregate

public TObject postEvalAggregate(GroupResolver group,
                                 QueryContext context,
                                 TObject result)
Called just before the value is returned to the parent. This does any final processing on the result before it is returned. If this aggregate was 'avg' then we'd divide by the size of the group.


evaluate

public final TObject evaluate(GroupResolver group,
                              VariableResolver resolver,
                              QueryContext context)
Description copied from interface: Function
Evaluates the function and returns a TObject that represents the result of the function. The VariableResolver object should be used to look up variables in the parameter of the function. The 'FunctionTable' object should only be used when the function is a grouping function. For example, 'avg(value_of)'.