Class AvgAggregator
java.lang.Object
org.apache.derby.impl.sql.execute.SystemAggregator
org.apache.derby.impl.sql.execute.OrderableAggregator
org.apache.derby.impl.sql.execute.SumAggregator
org.apache.derby.impl.sql.execute.AvgAggregator
- All Implemented Interfaces:
Externalizable, Serializable, Formatable, TypedFormat, ExecAggregator
Aggregator for AVG(). Extends the SumAggregator and
implements a count. Result is then sum()/count().
To handle overflow we catch the exception for
value out of range, then we swap the holder for
the current sum to one that can handle a larger
range. Eventually a sum may end up in a SQLDecimal
which can handle an infinite range. Once this
type promotion has happened, it will not revert back
to the original type, even if the sum would fit in
a lesser type.
- See Also:
-
Field Summary
FieldsFields inherited from class OrderableAggregator
value -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaccumulate(DataValueDescriptor addend) AccumulateReturn the result of the aggregation.intGet the formatID which corresponds to this class.voidmerge(ExecAggregator addend) Merges one aggregator into a another aggregator.Return a new initialized copy of this aggregator, any state set by the setup() method of the original Aggregator must be copied into the new aggregator.voidvoidAlthough we are not expected to be persistent per se, we may be written out by the sorter temporarily.Methods inherited from class SumAggregator
toStringMethods inherited from class OrderableAggregator
setupMethods inherited from class SystemAggregator
accumulate, didEliminateNulls
-
Field Details
-
count
private long count -
scale
private int scale
-
-
Constructor Details
-
AvgAggregator
public AvgAggregator()
-
-
Method Details
-
accumulate
Description copied from class:SumAggregatorAccumulate- Overrides:
accumulatein classSumAggregator- Parameters:
addend- value to be added in- Throws:
StandardException- on error- See Also:
-
merge
Description copied from interface:ExecAggregatorMerges one aggregator into a another aggregator. Merges two partial aggregates results into a single result. Needed for:- parallel aggregation
- vector aggregation (GROUP BY)
- distinct aggregates (e.g. MAX(DISTINCT Col))
An example of a merge would be: given two COUNT() aggregators, C1 and C2, a merge of C1 into C2 would set C1.count += C2.count. So, given a CountAggregator with a getCount() method that returns its counts, its merge method might look like this:
public void merge(ExecAggregator inputAggregator) throws StandardException { count += ((CountAccgregator)inputAggregator).getCount(); }- Specified by:
mergein interfaceExecAggregator- Overrides:
mergein classOrderableAggregator- Parameters:
addend- the other Aggregator (input partial aggregate)- Throws:
StandardException- on error- See Also:
-
getResult
Return the result of the aggregation. If the count is zero, then we haven't averaged anything yet, so we return null. Otherwise, return the running average as a double.- Specified by:
getResultin interfaceExecAggregator- Overrides:
getResultin classOrderableAggregator- Returns:
- null or the average as Double
- Throws:
StandardException- on error
-
newAggregator
Description copied from interface:ExecAggregatorReturn a new initialized copy of this aggregator, any state set by the setup() method of the original Aggregator must be copied into the new aggregator.- Specified by:
newAggregatorin interfaceExecAggregator- Overrides:
newAggregatorin classSumAggregator- Returns:
- ExecAggregator the new aggregator
-
writeExternal
Description copied from class:OrderableAggregatorAlthough we are not expected to be persistent per se, we may be written out by the sorter temporarily. So we need to be able to write ourselves out and read ourselves back in. We rely on formatable to handle situations where value is null.Why would we be called to write ourselves out if we are null? For scalar aggregates, we don't bother setting up the aggregator since we only need a single row. So for a scalar aggregate that needs to go to disk, the aggregator might be null.
- Specified by:
writeExternalin interfaceExternalizable- Overrides:
writeExternalin classOrderableAggregator- Throws:
IOException- on error- See Also:
-
readExternal
- Specified by:
readExternalin interfaceExternalizable- Overrides:
readExternalin classOrderableAggregator- Throws:
IOException- on errorClassNotFoundException- on error- See Also:
-
getTypeFormatId
public int getTypeFormatId()Get the formatID which corresponds to this class.- Specified by:
getTypeFormatIdin interfaceTypedFormat- Overrides:
getTypeFormatIdin classSumAggregator- Returns:
- the formatID of this class
-