2011-09-26 04:00:46 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
|
|
|
#include <DB/IO/ReadHelpers.h>
|
|
|
|
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
|
|
|
|
|
|
|
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2013-06-25 14:16:16 +00:00
|
|
|
template <typename T>
|
2013-02-08 19:34:44 +00:00
|
|
|
struct AggregateFunctionAvgData
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
2013-06-25 14:16:16 +00:00
|
|
|
T sum;
|
2011-09-26 04:00:46 +00:00
|
|
|
UInt64 count;
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
AggregateFunctionAvgData() : sum(0), count(0) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-06-25 14:16:16 +00:00
|
|
|
/// Считает арифметическое среднее значение чисел.
|
2013-02-08 19:34:44 +00:00
|
|
|
template <typename T>
|
2014-06-04 01:00:09 +00:00
|
|
|
class AggregateFunctionAvg final : public IUnaryAggregateFunction<AggregateFunctionAvgData<typename NearestFieldType<T>::Type>, AggregateFunctionAvg<T> >
|
2013-02-08 19:34:44 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-11-11 02:04:23 +00:00
|
|
|
String getName() const override { return "avg"; }
|
2011-09-26 04:00:46 +00:00
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
DataTypePtr getReturnType() const override
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
|
|
|
return new DataTypeFloat64;
|
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void setArgument(const DataTypePtr & argument) override
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
|
|
|
if (!argument->isNumeric())
|
|
|
|
throw Exception("Illegal type " + argument->getName() + " of argument for aggregate function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
2013-06-25 08:06:40 +00:00
|
|
|
void addOne(AggregateDataPtr place, const IColumn & column, size_t row_num) const
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
2013-06-25 14:16:16 +00:00
|
|
|
this->data(place).sum += static_cast<const ColumnVector<T> &>(column).getData()[row_num];
|
|
|
|
++this->data(place).count;
|
2011-09-26 04:00:46 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
2013-06-25 14:16:16 +00:00
|
|
|
this->data(place).sum += this->data(rhs).sum;
|
|
|
|
this->data(place).count += this->data(rhs).count;
|
2011-09-26 04:00:46 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
2013-06-25 14:16:16 +00:00
|
|
|
writeBinary(this->data(place).sum, buf);
|
|
|
|
writeVarUInt(this->data(place).count, buf);
|
2011-09-26 04:00:46 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const override
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
2013-06-25 14:16:16 +00:00
|
|
|
typename NearestFieldType<T>::Type tmp_sum = 0;
|
2011-09-26 04:00:46 +00:00
|
|
|
UInt64 tmp_count = 0;
|
2013-06-25 14:16:16 +00:00
|
|
|
readBinary(tmp_sum, buf);
|
2013-02-08 17:42:07 +00:00
|
|
|
readVarUInt(tmp_count, buf);
|
2013-06-25 14:16:16 +00:00
|
|
|
this->data(place).sum += tmp_sum;
|
|
|
|
this->data(place).count += tmp_count;
|
2011-09-26 04:00:46 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
2013-06-30 11:38:46 +00:00
|
|
|
static_cast<ColumnFloat64 &>(to).getData().push_back(
|
|
|
|
static_cast<Float64>(this->data(place).sum) / this->data(place).count);
|
2011-09-26 04:00:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-13 20:30:47 +00:00
|
|
|
|
2011-09-26 04:00:46 +00:00
|
|
|
}
|