ClickHouse/dbms/include/DB/AggregateFunctions/AggregateFunctionSum.h

125 lines
3.5 KiB
C
Raw Normal View History

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 AggregateFunctionSumData
2011-09-25 05:07:47 +00:00
{
T sum;
AggregateFunctionSumData() : sum(0) {}
};
2011-09-25 05:07:47 +00:00
/// Считает сумму чисел.
template <typename T>
class AggregateFunctionSum : public IUnaryAggregateFunction<AggregateFunctionSumData<typename NearestFieldType<T>::Type> >
{
2011-09-25 05:07:47 +00:00
public:
String getName() const { return "sum"; }
DataTypePtr getReturnType() const
{
return new typename DataTypeFromFieldType<typename NearestFieldType<T>::Type>::Type;
2011-09-25 05:07:47 +00:00
}
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);
}
void addOne(AggregateDataPtr place, const IColumn & column, size_t row_num) const
2011-09-25 05:07:47 +00:00
{
this->data(place).sum += static_cast<const ColumnVector<T> &>(column).getData()[row_num];
2011-09-25 05:07:47 +00:00
}
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const
2011-09-25 05:07:47 +00:00
{
this->data(place).sum += this->data(rhs).sum;
2011-09-25 05:07:47 +00:00
}
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const
2011-09-25 05:07:47 +00:00
{
writeBinary(this->data(place).sum, buf);
2011-09-25 05:07:47 +00:00
}
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const
2011-09-25 05:07:47 +00:00
{
typename NearestFieldType<T>::Type tmp;
readBinary(tmp, buf);
this->data(place).sum += tmp;
2011-09-25 05:07:47 +00:00
}
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
2011-09-25 05:07:47 +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:27:15 +00:00
/// Считает сумму чисел при выполнении условия. sumIf(x, cond) эквивалентно sum(cond ? x : 0).
template <typename T>
class AggregateFunctionSumIf : public IAggregateFunctionHelper<AggregateFunctionSumData<typename NearestFieldType<T>::Type> >
{
public:
String getName() const { return "sumIf"; }
DataTypePtr getReturnType() const
{
return new typename DataTypeFromFieldType<typename NearestFieldType<T>::Type>::Type;
}
void setArguments(const DataTypes & arguments)
{
if (!arguments[0]->isNumeric())
throw Exception("Illegal type " + arguments[0]->getName() + " of first argument for aggregate function " + getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if (!dynamic_cast<const DataTypeUInt8 *>(&*arguments[1]))
throw Exception("Illegal type " + arguments[1]->getName() + " of second argument for aggregate function " + getName() + ". Must be UInt8.",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
}
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num) const
{
if (static_cast<const ColumnUInt8 &>(*columns[1]).getData()[row_num])
this->data(place).sum += static_cast<const ColumnVector<T> &>(*columns[0]).getData()[row_num];
}
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const
{
this->data(place).sum += this->data(rhs).sum;
}
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const
{
writeBinary(this->data(place).sum, buf);
}
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const
{
typename NearestFieldType<T>::Type tmp;
readBinary(tmp, buf);
this->data(place).sum += tmp;
}
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
{
static_cast<ColumnVector<typename NearestFieldType<T>::Type> &>(to).getData().push_back(this->data(place).sum);
}
};
2011-09-25 05:07:47 +00:00
}