ClickHouse/dbms/include/DB/AggregateFunctions/IUnaryAggregateFunction.h
2015-12-23 10:00:33 +03:00

49 lines
1.6 KiB
C++

#pragma once
#include <DB/IO/WriteHelpers.h>
#include <DB/AggregateFunctions/IAggregateFunction.h>
namespace DB
{
/** Интерфейс для агрегатных функций, принимающих одно значение. Это почти все агрегатные функции.
*/
template <typename T, typename Derived>
class IUnaryAggregateFunction : public IAggregateFunctionHelper<T>
{
private:
Derived & getDerived() { return static_cast<Derived &>(*this); }
const Derived & getDerived() const { return static_cast<const Derived &>(*this); }
public:
void setArguments(const DataTypes & arguments) override final
{
if (arguments.size() != 1)
throw Exception("Passed " + toString(arguments.size()) + " arguments to unary aggregate function " + this->getName(),
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
getDerived().setArgument(arguments[0]);
}
/// Добавить значение.
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num) const override final
{
getDerived().addImpl(place, *columns[0], row_num);
}
static void addFree(const IAggregateFunction * that, AggregateDataPtr place, const IColumn ** columns, size_t row_num)
{
return static_cast<const Derived &>(*that).addImpl(place, *columns[0], row_num);
}
IAggregateFunction::AddFunc getAddressOfAddFunction() const override final { return &addFree; }
/** Реализуйте это в классе-наследнике:
* void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num) const;
* void setArgument(const DataTypePtr & argument);
*/
};
}