2014-05-21 13:27:40 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypeAggregateFunction.h>
|
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
|
|
|
#include <Columns/ColumnAggregateFunction.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2014-05-21 13:27:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-05-26 11:32:14 +00:00
|
|
|
struct Settings;
|
|
|
|
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
2014-05-21 13:27:40 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
class AggregateFunctionMerge final : public IAggregateFunctionHelper<AggregateFunctionMerge>
|
2014-05-21 13:27:40 +00:00
|
|
|
{
|
|
|
|
private:
|
2017-12-20 07:36:30 +00:00
|
|
|
AggregateFunctionPtr nested_func;
|
2014-05-21 13:27:40 +00:00
|
|
|
|
|
|
|
public:
|
2021-07-26 14:37:47 +00:00
|
|
|
AggregateFunctionMerge(const AggregateFunctionPtr & nested_, const DataTypePtr & argument, const Array & params_)
|
|
|
|
: IAggregateFunctionHelper<AggregateFunctionMerge>({argument}, params_)
|
2019-02-11 19:26:32 +00:00
|
|
|
, nested_func(nested_)
|
2017-12-20 07:36:30 +00:00
|
|
|
{
|
2019-02-11 19:26:32 +00:00
|
|
|
const DataTypeAggregateFunction * data_type = typeid_cast<const DataTypeAggregateFunction *>(argument.get());
|
2017-12-20 07:36:30 +00:00
|
|
|
|
2021-07-28 17:55:13 +00:00
|
|
|
if (!data_type || !nested_func->haveSameStateRepresentation(*data_type->getFunction()))
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument for aggregate function {}, "
|
|
|
|
"expected {} or equivalent type", argument->getName(), getName(), getStateType()->getName());
|
2017-12-20 07:36:30 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return nested_func->getName() + "Merge";
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
|
|
|
return nested_func->getReturnType();
|
|
|
|
}
|
|
|
|
|
2021-06-06 21:49:55 +00:00
|
|
|
DataTypePtr getStateType() const override
|
|
|
|
{
|
|
|
|
return nested_func->getStateType();
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void create(AggregateDataPtr __restrict place) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
nested_func->create(place);
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void destroy(AggregateDataPtr __restrict place) const noexcept override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
nested_func->destroy(place);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasTrivialDestructor() const override
|
|
|
|
{
|
|
|
|
return nested_func->hasTrivialDestructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t sizeOfData() const override
|
|
|
|
{
|
|
|
|
return nested_func->sizeOfData();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t alignOfData() const override
|
|
|
|
{
|
|
|
|
return nested_func->alignOfData();
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void add(AggregateDataPtr __restrict place, const IColumn ** columns, size_t row_num, Arena * arena) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
nested_func->merge(place, assert_cast<const ColumnAggregateFunction &>(*columns[0]).getData()[row_num], arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
nested_func->merge(place, rhs, arena);
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void serialize(ConstAggregateDataPtr __restrict place, WriteBuffer & buf) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
nested_func->serialize(place, buf);
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void deserialize(AggregateDataPtr __restrict place, ReadBuffer & buf, Arena * arena) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
nested_func->deserialize(place, buf, arena);
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void insertResultInto(AggregateDataPtr __restrict place, IColumn & to, Arena * arena) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-06-17 19:36:27 +00:00
|
|
|
nested_func->insertResultInto(place, to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool allocatesMemoryInArena() const override
|
|
|
|
{
|
|
|
|
return nested_func->allocatesMemoryInArena();
|
|
|
|
}
|
2020-11-15 10:05:52 +00:00
|
|
|
|
|
|
|
AggregateFunctionPtr getNestedFunction() const override { return nested_func; }
|
2014-05-21 13:27:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|