2011-09-25 05:07:47 +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
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename T> struct AggregateFunctionSumTraits;
|
|
|
|
|
|
|
|
template <> struct AggregateFunctionSumTraits<UInt64>
|
|
|
|
{
|
2012-12-14 20:24:26 +00:00
|
|
|
static DataTypePtr getReturnType() { return new DataTypeUInt64; }
|
2011-09-25 05:07:47 +00:00
|
|
|
static void write(UInt64 x, WriteBuffer & buf) { writeVarUInt(x, buf); }
|
|
|
|
static void read(UInt64 & x, ReadBuffer & buf) { readVarUInt(x, buf); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct AggregateFunctionSumTraits<Int64>
|
|
|
|
{
|
2012-12-14 20:24:26 +00:00
|
|
|
static DataTypePtr getReturnType() { return new DataTypeInt64; }
|
2011-09-25 05:07:47 +00:00
|
|
|
static void write(Int64 x, WriteBuffer & buf) { writeVarInt(x, buf); }
|
|
|
|
static void read(Int64 & x, ReadBuffer & buf) { readVarInt(x, buf); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct AggregateFunctionSumTraits<Float64>
|
|
|
|
{
|
|
|
|
static DataTypePtr getReturnType() { return new DataTypeFloat64; }
|
|
|
|
static void write(Float64 x, WriteBuffer & buf) { writeFloatBinary(x, buf); }
|
|
|
|
static void read(Float64 & x, ReadBuffer & buf) { readFloatBinary(x, buf); }
|
|
|
|
};
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
2011-09-25 05:07:47 +00:00
|
|
|
template <typename T>
|
2013-02-08 19:34:44 +00:00
|
|
|
struct AggregateFunctionSumData
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
|
|
|
T sum;
|
2013-02-08 19:34:44 +00:00
|
|
|
|
|
|
|
AggregateFunctionSumData() : sum(0) {}
|
|
|
|
};
|
|
|
|
|
2011-09-25 05:07:47 +00:00
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
/// Считает сумму чисел. Параметром шаблона может быть UInt64, Int64 или Float64.
|
|
|
|
template <typename T>
|
|
|
|
class AggregateFunctionSum : public IUnaryAggregateFunction<AggregateFunctionSumData<T> >
|
|
|
|
{
|
2011-09-25 05:07:47 +00:00
|
|
|
public:
|
|
|
|
String getName() const { return "sum"; }
|
|
|
|
String getTypeID() const { return "sum_" + TypeName<T>::get(); }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const
|
|
|
|
{
|
|
|
|
return AggregateFunctionSumTraits<T>::getReturnType();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setArgument(const DataTypePtr & argument)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
void addOne(AggregateDataPtr place, const Field & value) const
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
2013-02-08 23:41:05 +00:00
|
|
|
this->data(place).sum += get<const T &>(value);
|
2011-09-25 05:07:47 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
2013-02-08 23:41:05 +00:00
|
|
|
this->data(place).sum += this->data(rhs).sum;
|
2011-09-25 05:07:47 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
2013-02-08 23:41:05 +00:00
|
|
|
AggregateFunctionSumTraits<T>::write(this->data(place).sum, buf);
|
2011-09-25 05:07:47 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
|
|
|
T tmp;
|
|
|
|
AggregateFunctionSumTraits<T>::read(tmp, buf);
|
2013-02-08 23:41:05 +00:00
|
|
|
this->data(place).sum += tmp;
|
2011-09-25 05:07:47 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
Field getResult(ConstAggregateDataPtr place) const
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
2013-02-08 23:41:05 +00:00
|
|
|
return this->data(place).sum;
|
2011-09-25 05:07:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|