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:
java.io.Externalizable
,java.io.Serializable
,Formatable
,TypedFormat
,ExecAggregator
public final class AvgAggregator extends SumAggregator
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:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private long
count
private int
scale
-
Fields inherited from class org.apache.derby.impl.sql.execute.OrderableAggregator
value
-
-
Constructor Summary
Constructors Constructor Description AvgAggregator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
accumulate(DataValueDescriptor addend)
AccumulateDataValueDescriptor
getResult()
Return the result of the aggregation.int
getTypeFormatId()
Get the formatID which corresponds to this class.void
merge(ExecAggregator addend)
Merges one aggregator into a another aggregator.ExecAggregator
newAggregator()
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.void
readExternal(java.io.ObjectInput in)
void
writeExternal(java.io.ObjectOutput out)
Although we are not expected to be persistent per se, we may be written out by the sorter temporarily.-
Methods inherited from class org.apache.derby.impl.sql.execute.SumAggregator
toString
-
Methods inherited from class org.apache.derby.impl.sql.execute.OrderableAggregator
setup
-
Methods inherited from class org.apache.derby.impl.sql.execute.SystemAggregator
accumulate, didEliminateNulls
-
-
-
-
Method Detail
-
accumulate
protected void accumulate(DataValueDescriptor addend) throws StandardException
Description copied from class:SumAggregator
Accumulate- Overrides:
accumulate
in classSumAggregator
- Parameters:
addend
- value to be added in- Throws:
StandardException
- on error- See Also:
ExecAggregator.accumulate(org.apache.derby.iapi.types.DataValueDescriptor, java.lang.Object)
-
merge
public void merge(ExecAggregator addend) throws StandardException
Description copied from interface:ExecAggregator
Merges 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:
merge
in interfaceExecAggregator
- Overrides:
merge
in classOrderableAggregator
- Parameters:
addend
- the other Aggregator (input partial aggregate)- Throws:
StandardException
- on error- See Also:
ExecAggregator.merge(org.apache.derby.iapi.sql.execute.ExecAggregator)
-
getResult
public DataValueDescriptor getResult() throws StandardException
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:
getResult
in interfaceExecAggregator
- Overrides:
getResult
in classOrderableAggregator
- Returns:
- null or the average as Double
- Throws:
StandardException
- on error
-
newAggregator
public ExecAggregator newAggregator()
Description copied from interface:ExecAggregator
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.- Specified by:
newAggregator
in interfaceExecAggregator
- Overrides:
newAggregator
in classSumAggregator
- Returns:
- ExecAggregator the new aggregator
-
writeExternal
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException
Description copied from class:OrderableAggregator
Although 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:
writeExternal
in interfacejava.io.Externalizable
- Overrides:
writeExternal
in classOrderableAggregator
- Throws:
java.io.IOException
- on error- See Also:
Externalizable.writeExternal(java.io.ObjectOutput)
-
readExternal
public void readExternal(java.io.ObjectInput in) throws java.io.IOException, java.lang.ClassNotFoundException
- Specified by:
readExternal
in interfacejava.io.Externalizable
- Overrides:
readExternal
in classOrderableAggregator
- Throws:
java.io.IOException
- on errorjava.lang.ClassNotFoundException
- on error- See Also:
Externalizable.readExternal(java.io.ObjectInput)
-
getTypeFormatId
public int getTypeFormatId()
Get the formatID which corresponds to this class.- Specified by:
getTypeFormatId
in interfaceTypedFormat
- Overrides:
getTypeFormatId
in classSumAggregator
- Returns:
- the formatID of this class
-
-