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

121 lines
3.3 KiB
C++
Raw Normal View History

2014-05-21 13:27:40 +00:00
#pragma once
#include <DB/DataTypes/DataTypeAggregateFunction.h>
#include <DB/AggregateFunctions/IAggregateFunction.h>
2015-09-24 12:40:36 +00:00
#include <DB/Columns/ColumnAggregateFunction.h>
2014-05-21 13:27:40 +00:00
namespace DB
{
2017-03-09 00:56:38 +00:00
/** Not an aggregate function, but an adapter of aggregate functions,
* Aggregate functions with the `Merge` suffix accept `DataTypeAggregateFunction` as an argument
* (state of the aggregate function obtained earlier using the aggregate function with the `State` suffix)
* and combine them with aggregation.
2014-05-21 13:27:40 +00:00
*/
class AggregateFunctionMerge final : public IAggregateFunction
2014-05-21 13:27:40 +00:00
{
private:
AggregateFunctionPtr nested_func_owner;
IAggregateFunction * nested_func;
public:
AggregateFunctionMerge(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() + "Merge";
}
2015-11-11 02:04:23 +00:00
DataTypePtr getReturnType() const override
2014-05-21 13:27:40 +00:00
{
return nested_func->getReturnType();
}
2015-11-11 02:04:23 +00:00
void setArguments(const DataTypes & arguments) override
2014-05-21 13:27:40 +00:00
{
if (arguments.size() != 1)
throw Exception("Passed " + toString(arguments.size()) + " arguments to unary aggregate function " + this->getName(),
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
2014-06-03 20:29:04 +00:00
const DataTypeAggregateFunction * data_type = typeid_cast<const DataTypeAggregateFunction *>(&*arguments[0]);
2014-06-03 20:29:04 +00:00
2014-05-21 13:27:40 +00:00
if (!data_type || data_type->getFunctionName() != nested_func->getName())
throw Exception("Illegal type " + arguments[0]->getName() + " of argument for aggregate function " + getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
2014-06-06 17:46:03 +00:00
nested_func->setArguments(data_type->getArgumentsDataTypes());
2014-05-21 13:27:40 +00:00
}
2015-11-11 02:04:23 +00:00
void setParameters(const Array & params) override
2014-05-21 13:27:40 +00:00
{
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();
}
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override
2014-05-21 13:27:40 +00:00
{
nested_func->merge(place, static_cast<const ColumnAggregateFunction &>(*columns[0]).getData()[row_num], arena);
2014-05-21 13:27:40 +00:00
}
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
2014-05-21 13:27:40 +00:00
{
nested_func->merge(place, rhs, arena);
2014-05-21 13:27:40 +00:00
}
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-09-22 23:26:08 +00:00
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena * arena) const override
2014-05-21 13:27:40 +00:00
{
2016-09-22 23:26:08 +00:00
nested_func->deserialize(place, buf, arena);
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
{
nested_func->insertResultInto(place, to);
}
bool allocatesMemoryInArena() const override
{
return nested_func->allocatesMemoryInArena();
}
static void addFree(const IAggregateFunction * that, AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena)
{
static_cast<const AggregateFunctionMerge &>(*that).add(place, columns, row_num, arena);
}
IAggregateFunction::AddFunc getAddressOfAddFunction() const override final { return &addFree; }
2014-05-21 13:27:40 +00:00
};
}