2011-09-25 05:07:47 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
|
|
|
#include <DB/IO/ReadHelpers.h>
|
|
|
|
|
2017-03-12 10:13:45 +00:00
|
|
|
#include <DB/DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DB/Columns/ColumnVector.h>
|
2011-09-25 05:07:47 +00:00
|
|
|
|
|
|
|
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
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) {}
|
|
|
|
};
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/// Counts the sum of the numbers.
|
2013-02-08 19:34:44 +00:00
|
|
|
template <typename T>
|
2014-06-04 01:00:09 +00:00
|
|
|
class AggregateFunctionSum final : public IUnaryAggregateFunction<AggregateFunctionSumData<typename NearestFieldType<T>::Type>, AggregateFunctionSum<T> >
|
2013-02-08 19:34:44 +00:00
|
|
|
{
|
2011-09-25 05:07:47 +00:00
|
|
|
public:
|
2015-11-11 02:04:23 +00:00
|
|
|
String getName() const override { return "sum"; }
|
2011-09-25 05:07:47 +00:00
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
DataTypePtr getReturnType() const override
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
2017-03-12 10:13:45 +00:00
|
|
|
return std::make_shared<DataTypeNumber<typename NearestFieldType<T>::Type>>();
|
2011-09-25 05:07:47 +00:00
|
|
|
}
|
|
|
|
|
2015-11-15 08:31:08 +00:00
|
|
|
void setArgument(const DataTypePtr & argument)
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
2015-12-22 11:38:53 +00:00
|
|
|
if (!argument->behavesAsNumber())
|
2011-09-25 05:07:47 +00:00
|
|
|
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
|
|
|
|
2016-09-19 22:30:40 +00:00
|
|
|
void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num, Arena *) const
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
2013-06-25 14:16:16 +00:00
|
|
|
this->data(place).sum += static_cast<const ColumnVector<T> &>(column).getData()[row_num];
|
2011-09-25 05:07:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 23:33:17 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
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
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
2013-06-25 14:16:16 +00:00
|
|
|
writeBinary(this->data(place).sum, buf);
|
2011-09-25 05:07:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-22 23:26:08 +00:00
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
2016-03-12 04:01:03 +00:00
|
|
|
readBinary(this->data(place).sum, buf);
|
2011-09-25 05:07:47 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
2013-06-30 11:38:46 +00:00
|
|
|
static_cast<ColumnVector<typename NearestFieldType<T>::Type> &>(to).getData().push_back(this->data(place).sum);
|
2011-09-25 05:07:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-14 16:17:02 +00:00
|
|
|
|
2011-09-25 05:07:47 +00:00
|
|
|
}
|