2014-05-21 13:27:40 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <DB/DataTypes/DataTypeAggregateFunction.h>
|
|
|
|
|
#include <DB/AggregateFunctions/IAggregateFunction.h>
|
2014-05-28 14:54:42 +00:00
|
|
|
|
#include <DB/Columns/ColumnAggregateFunction.h>
|
2014-05-21 13:27:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Не агрегатная функция, а адаптер агрегатных функций,
|
|
|
|
|
* Агрегатные функции с суффиксом State отличаются от соответствующих тем, что их состояния не финализируются.
|
2014-06-03 20:29:04 +00:00
|
|
|
|
* Возвращаемый тип - DataTypeAggregateFunction.
|
2014-05-21 13:27:40 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2014-06-04 01:00:09 +00:00
|
|
|
|
class AggregateFunctionState final : public IAggregateFunction
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
AggregateFunctionPtr nested_func_owner;
|
|
|
|
|
IAggregateFunction * nested_func;
|
|
|
|
|
DataTypes arguments;
|
|
|
|
|
Array params;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
AggregateFunctionState(AggregateFunctionPtr nested_) : nested_func_owner(nested_), nested_func(nested_func_owner.get()) {}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
String getName() const override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
return nested_func->getName() + "State";
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
DataTypePtr getReturnType() const override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
return new DataTypeAggregateFunction(nested_func_owner, arguments, params);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void setArguments(const DataTypes & arguments_) override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
arguments = arguments_;
|
|
|
|
|
nested_func->setArguments(arguments);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void setParameters(const Array & params_) override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
params = params_;
|
|
|
|
|
nested_func->setParameters(params);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void create(AggregateDataPtr place) const override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
nested_func->create(place);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void destroy(AggregateDataPtr place) const noexcept override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
nested_func->destroy(place);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
bool hasTrivialDestructor() const override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
return nested_func->hasTrivialDestructor();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
size_t sizeOfData() const override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
return nested_func->sizeOfData();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
size_t alignOfData() const override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
return nested_func->alignOfData();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num) const override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
nested_func->add(place, columns, row_num);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
nested_func->merge(place, rhs);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
|
|
|
|
nested_func->serialize(place, buf);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-12 04:01:03 +00:00
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf) const override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
2016-03-12 04:01:03 +00:00
|
|
|
|
nested_func->deserialize(place, buf);
|
2014-05-21 13:27:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
2014-05-21 13:27:40 +00:00
|
|
|
|
{
|
2014-05-30 16:21:30 +00:00
|
|
|
|
static_cast<ColumnAggregateFunction &>(to).getData().push_back(const_cast<AggregateDataPtr>(place));
|
2014-05-21 13:27:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-30 16:21:30 +00:00
|
|
|
|
/// Аггрегатная функция или состояние аггрегатной функции.
|
2015-11-11 02:04:23 +00:00
|
|
|
|
bool isState() const override { return true; }
|
2014-05-21 13:27:40 +00:00
|
|
|
|
|
2015-11-11 01:28:34 +00:00
|
|
|
|
AggregateFunctionPtr getNestedFunction() const { return nested_func_owner; }
|
2015-11-21 19:46:27 +00:00
|
|
|
|
|
|
|
|
|
static void addFree(const IAggregateFunction * that, AggregateDataPtr place, const IColumn ** columns, size_t row_num)
|
|
|
|
|
{
|
|
|
|
|
return static_cast<const AggregateFunctionState &>(*that).add(place, columns, row_num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IAggregateFunction::AddFunc getAddressOfAddFunction() const override final { return &addFree; }
|
2014-05-21 13:27:40 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|