2011-09-19 03:40:05 +00:00
|
|
|
#pragma once
|
|
|
|
|
2015-12-23 05:31:51 +00:00
|
|
|
#include <DB/IO/WriteHelpers.h>
|
2011-09-19 03:40:05 +00:00
|
|
|
#include <DB/AggregateFunctions/IAggregateFunction.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-02-10 05:03:42 +00:00
|
|
|
/** Interface for aggregate functions, that take single argument. These are almost all aggregate functions.
|
2011-09-19 03:40:05 +00:00
|
|
|
*/
|
2013-12-22 08:06:30 +00:00
|
|
|
template <typename T, typename Derived>
|
2013-02-08 19:34:44 +00:00
|
|
|
class IUnaryAggregateFunction : public IAggregateFunctionHelper<T>
|
2011-09-19 03:40:05 +00:00
|
|
|
{
|
2015-11-15 08:31:08 +00:00
|
|
|
private:
|
|
|
|
Derived & getDerived() { return static_cast<Derived &>(*this); }
|
|
|
|
const Derived & getDerived() const { return static_cast<const Derived &>(*this); }
|
|
|
|
|
2011-09-19 03:40:05 +00:00
|
|
|
public:
|
2015-11-15 08:31:08 +00:00
|
|
|
void setArguments(const DataTypes & arguments) override final
|
2011-09-19 03:40:05 +00:00
|
|
|
{
|
|
|
|
if (arguments.size() != 1)
|
2013-06-21 20:34:19 +00:00
|
|
|
throw Exception("Passed " + toString(arguments.size()) + " arguments to unary aggregate function " + this->getName(),
|
2011-09-19 03:40:05 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2015-11-15 08:31:08 +00:00
|
|
|
getDerived().setArgument(arguments[0]);
|
|
|
|
}
|
2011-09-25 05:07:47 +00:00
|
|
|
|
2017-02-10 05:03:42 +00:00
|
|
|
/// Accumulate a value.
|
2016-09-19 22:30:40 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override final
|
2011-09-19 03:40:05 +00:00
|
|
|
{
|
2016-09-19 22:30:40 +00:00
|
|
|
getDerived().addImpl(place, *columns[0], row_num, arena);
|
2011-09-19 03:40:05 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 22:30:40 +00:00
|
|
|
static void addFree(const IAggregateFunction * that, AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena)
|
2015-11-21 19:46:27 +00:00
|
|
|
{
|
2016-09-19 22:30:40 +00:00
|
|
|
static_cast<const Derived &>(*that).addImpl(place, *columns[0], row_num, arena);
|
2015-11-21 19:46:27 +00:00
|
|
|
}
|
|
|
|
|
2016-02-20 18:11:50 +00:00
|
|
|
IAggregateFunction::AddFunc getAddressOfAddFunction() const override { return &addFree; }
|
2015-11-21 19:46:27 +00:00
|
|
|
|
2017-02-10 05:03:42 +00:00
|
|
|
/** Implement the following in descendant class:
|
2017-02-10 09:02:10 +00:00
|
|
|
* void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num, Arena * arena) const;
|
2015-11-15 08:31:08 +00:00
|
|
|
* void setArgument(const DataTypePtr & argument);
|
2013-12-22 08:06:30 +00:00
|
|
|
*/
|
2011-09-19 03:40:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|