Top Description Fields Constructors Methods
org.apache.derby.impl.sql.execute

public final Class AvgAggregator

extends SumAggregator
Class Inheritance
Imports
org.apache.derby.iapi.types.NumberDataValue, .DataValueDescriptor, .SQLDecimal, .TypeId, org.apache.derby.shared.common.error.StandardException, org.apache.derby.iapi.sql.execute.ExecAggregator, org.apache.derby.iapi.services.io.StoredFormatIds, org.apache.derby.shared.common.reference.SQLState, java.io.ObjectOutput, .ObjectInput, .IOException

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.

Field Summary

Modifier and TypeField and Description
private long
private int

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
public DataValueDescriptor

Returns:

null or the average as Double
getResult
()

Overrides org.apache.derby.impl.sql.execute.OrderableAggregator.getResult.

Implements org.apache.derby.iapi.sql.execute.ExecAggregator.getResult.

Return the result of the aggregation.

public int

Returns:

the formatID of this class
getTypeFormatId
()

Overrides org.apache.derby.impl.sql.execute.SumAggregator.getTypeFormatId.

Implements org.apache.derby.iapi.services.io.TypedFormat.getTypeFormatId.

Get the formatID which corresponds to this class.

public void
merge(ExecAggregator
the other Aggregator (input partial aggregate)
addend
)

Overrides org.apache.derby.impl.sql.execute.OrderableAggregator.merge.

Implements org.apache.derby.iapi.sql.execute.ExecAggregator.merge.

Merges one aggregator into a another aggregator.

public ExecAggregator
newAggregator()

Overrides org.apache.derby.impl.sql.execute.SumAggregator.newAggregator.

Implements org.apache.derby.iapi.sql.execute.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.

public void
readExternal(ObjectInput
the stream to read data from in order to restore the object
in
)

Overrides org.apache.derby.impl.sql.execute.OrderableAggregator.readExternal.

Implements java.io.Externalizable.readExternal.

The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays.

public void
writeExternal(ObjectOutput
the stream to write the object to
out
)

Overrides org.apache.derby.impl.sql.execute.OrderableAggregator.writeExternal.

Implements java.io.Externalizable.writeExternal.

Although we are not expected to be persistent per se, we may be written out by the sorter temporarily.

Inherited from org.apache.derby.impl.sql.execute.SumAggregator:
toString

Field Detail

countback to summary
private long count
scaleback to summary
private int scale

Constructor Detail

AvgAggregatorback to summary
public AvgAggregator()

Method Detail

accumulateback to summary
protected void accumulate(DataValueDescriptor addend) throws StandardException

Overrides org.apache.derby.impl.sql.execute.SumAggregator.accumulate.

Doc from org.apache.derby.impl.sql.execute.SumAggregator.accumulate.

Accumulate

Parameters
addend:DataValueDescriptor

value to be added in

Exceptions
StandardException:
on error
getResultback to summary
public DataValueDescriptor getResult() throws StandardException

Overrides org.apache.derby.impl.sql.execute.OrderableAggregator.getResult.

Implements org.apache.derby.iapi.sql.execute.ExecAggregator.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.

Returns:DataValueDescriptor

null or the average as Double

Exceptions
StandardException:

Doc from org.apache.derby.iapi.sql.execute.ExecAggregator.getResult.

on error

getTypeFormatIdback to summary
public int getTypeFormatId()

Overrides org.apache.derby.impl.sql.execute.SumAggregator.getTypeFormatId.

Implements org.apache.derby.iapi.services.io.TypedFormat.getTypeFormatId.

Get the formatID which corresponds to this class.

Returns:int

the formatID of this class

mergeback to summary
public void merge(ExecAggregator addend) throws StandardException

Overrides org.apache.derby.impl.sql.execute.OrderableAggregator.merge.

Implements org.apache.derby.iapi.sql.execute.ExecAggregator.merge.

Doc from org.apache.derby.iapi.sql.execute.ExecAggregator.merge.

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();
		} 
Parameters
addend:ExecAggregator

the other Aggregator (input partial aggregate)

Exceptions
StandardException:

Doc from org.apache.derby.impl.sql.execute.OrderableAggregator.merge.

on error

newAggregatorback to summary
public ExecAggregator newAggregator()

Overrides org.apache.derby.impl.sql.execute.SumAggregator.newAggregator.

Implements org.apache.derby.iapi.sql.execute.ExecAggregator.newAggregator.

Doc from org.apache.derby.iapi.sql.execute.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.

Returns:ExecAggregator

Doc from org.apache.derby.impl.sql.execute.SumAggregator.newAggregator.

ExecAggregator the new aggregator

readExternalback to summary
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException

Overrides org.apache.derby.impl.sql.execute.OrderableAggregator.readExternal.

Implements java.io.Externalizable.readExternal.

Doc from java.io.Externalizable.readExternal.

The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays. The readExternal method must read the values in the same sequence and with the same types as were written by writeExternal.

Parameters
in:ObjectInput

the stream to read data from in order to restore the object

Exceptions
IOException:
on error
ClassNotFoundException:

Doc from org.apache.derby.impl.sql.execute.OrderableAggregator.readExternal.

on error

See Also
java.io.Externalizable#readExternal
writeExternalback to summary
public void writeExternal(ObjectOutput out) throws IOException

Overrides org.apache.derby.impl.sql.execute.OrderableAggregator.writeExternal.

Implements java.io.Externalizable.writeExternal.

Doc from org.apache.derby.impl.sql.execute.OrderableAggregator.writeExternal.

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.

Parameters
out:ObjectOutput

Doc from java.io.Externalizable.writeExternal.

the stream to write the object to

Exceptions
IOException:
on error