: Class Aggregate_Math

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## ModSQL
Class Aggregate_Math

    java.lang.Object
      |
      +--ModSQL.AbstractFunction
            |
            +--ModSQL.AbstractAggregate
                  |
                  +--ModSQL.Aggregate_Math

All Implemented Interfaces:
  Aggregate, Function

final class Aggregate_Math
extends AbstractAggregate

Implementation of SQL aggregate functions SUM(), AVG() and STDEV(). The return type of each function is function and parameter dependent. AVG() and STDEV() will return type DOUBLE and SUM() will return either type BIGINT or DOUBLE, depending on whether the parameter is an integer or a floating point number, respectively. If there is no parameter, the return type is NULL for all functions. If there are no non-null values to aggregate, SUM() will return 0, but AVG() and STDEV() will return NULL.

Author:
  chris.studholme@utoronto.ca

Field Summary
`static int` | `AVG` Function is AVG().
`private long` | `count` Current count of non-null values.
`private int` | `op` Which function are we? One of the constants above.
`private int` | `return_type` SQL return type for this function.
`static int` | `STDEV` Function is STDEV().
`static int` | `SUM` Function is SUM().
`private double` | `sum_double` Current sum as a DOUBLE.
`private long` | `sum_long` Current sum as a BIGINT.
`private double` | `sum_square` Current sum of squares.

Fields inherited from class ModSQL.AbstractAggregate
`distinct, distinct_set`

Fields inherited from class ModSQL.AbstractFunction
`all_constant, constant_non_null, evaluate_order, evaluate_order_array, out_of_order, parameter_constant, parameter_value, parameters, parameters_array`

Fields inherited from interface ModSQL.Function
`MATCH_BEGINS, MATCH_EQU, MATCH_GT, MATCH_GTE, MATCH_LT, MATCH_LTE, MATCH_NE`

Constructor Summary
`Aggregate_Math(int op)` Constructor.

Method Summary
`Object` | `evaluate(boolean aggregate)` Evaluate parameters and update count and sums.
`static Function` | `forName(String name)` Returns a new instance of this class if name is "sum", "avg", or "stdev".
`String` | `functionName()` Returns the name of this function for use by toString() method.
`int` | `getMaxResultSize()` Returns -1 as these functions never return String objects.
`int` | `getSQLType()` Return the SQL type of the value that this function expects to return.
`void` | `optimize()` Prepare the function for use.
`void` | `reset()` Resets the current count and sums to zero.

Methods inherited from class ModSQL.AbstractAggregate
`addDistinctValue, getDistinctSetSize, isAggregate, isConstant, prefixParameters, registerWith, setDistinct`

Methods inherited from class ModSQL.AbstractFunction
`addParameter, convertToSQLType, evaluate, evaluateConstantParameters, evaluateOrder, evaluateParameters, evaluateParameters, getCompatableType, getObjectSQLType, getParameter, getParameterCount, postfixParameters, toString`

Methods inherited from class java.lang.Object
`, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait`

Methods inherited from interface ModSQL.Function
`addParameter, evaluate, evaluateOrder, getParameter, getParameterCount`

Field Detail

### SUM

    public static final int SUM

  Function is SUM().

### AVG

    public static final int AVG

  Function is AVG().

### STDEV

    public static final int STDEV

  Function is STDEV().

### op

    private int op

  Which function are we? One of the constants above.

### return_type

    private int return_type

  SQL return type for this function.

### count

    private long count

  Current count of non-null values.

### sum_long

    private long sum_long

  Current sum as a BIGINT.

### sum_double

    private double sum_double

  Current sum as a DOUBLE.

### sum_square

    private double sum_square

  Current sum of squares.

Constructor Detail

### Aggregate_Math

    public Aggregate_Math(int op)

  Constructor.
  Parameters:
    `op` - function type

Method Detail

### forName

    public static Function forName(String name)

  Returns a new instance of this class if name is "sum", "avg", or "stdev". The instance is initialized as needed to represent the desired function.
  Parameters:
    `name` - name of function to find
  Returns:
    instance of this class or null if name does not match

### optimize

    public void optimize()
                  throws SQLException

  Prepare the function for use. All functions accept at most one parameter and require a parameter if DISTINCT is specified. This method also decides on an appropriate return type.

  Overrides:
    `optimize` in class `AbstractFunction`
  Throws:
    `SQLException` - if the parameters are invalid

### functionName

    public String functionName()

  Returns the name of this function for use by toString() method.
  Overrides:
    `functionName` in class `AbstractFunction`
  Returns:
    name of function

### getSQLType

    public int getSQLType()
                   throws SQLException

  Return the SQL type of the value that this function expects to return.
  Returns:
    SQL type of data to be returned

### getMaxResultSize

    public int getMaxResultSize()

  Returns -1 as these functions never return String objects.
  Returns:
    maximum size of String returned or -1 if unknown

### reset

    public void reset()
               throws SQLException

  Resets the current count and sums to zero.
  Overrides:
    `reset` in class `AbstractAggregate`
  Throws:
    `SQLException` - if a database error occurs

### evaluate

    public Object evaluate(boolean aggregate)
                    throws SQLException,
                           EndOfTable

  Evaluate parameters and update count and sums. If aggregate is false, the parameter is evaluated and the count and sums are updated, but this method always returns null. If aggregate is true, the parameter is not evaluated, and the current evaluation of the function is returned.

  Parameters:
    `aggregate` - true to return final aggregate value
  Returns:
    result object
  Throws:
    `SQLException` - if a database-access error occurs
    `EndOfTable` - if thrown by a parameter

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD