2011-09-25 05:07:47 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
2011-09-25 05:07:47 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
2011-09-25 05:07:47 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/IUnaryAggregateFunction.h>
|
2011-09-25 05:07:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename T>
|
2013-02-08 19:34:44 +00:00
|
|
|
struct AggregateFunctionSumData
|
2011-09-25 05:07:47 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
T sum;
|
2013-02-08 19:34:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionSumData() : sum(0) {}
|
2013-02-08 19:34:44 +00:00
|
|
|
};
|
|
|
|
|
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:
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override { return "sum"; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
|
|
|
return std::make_shared<DataTypeNumber<typename NearestFieldType<T>::Type>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setArgument(const DataTypePtr & argument)
|
|
|
|
{
|
|
|
|
if (!argument->behavesAsNumber())
|
|
|
|
throw Exception("Illegal type " + argument->getName() + " of argument for aggregate function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num, Arena *) const
|
|
|
|
{
|
|
|
|
this->data(place).sum += static_cast<const ColumnVector<T> &>(column).getData()[row_num];
|
|
|
|
}
|
|
|
|
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
|
|
|
{
|
|
|
|
this->data(place).sum += this->data(rhs).sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
|
|
|
{
|
|
|
|
writeBinary(this->data(place).sum, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override
|
|
|
|
{
|
|
|
|
readBinary(this->data(place).sum, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|