1
2#pragma once
3
4#include <DataTypes/DataTypeAggregateFunction.h>
5#include <AggregateFunctions/IAggregateFunction.h>
6#include <Columns/ColumnAggregateFunction.h>
7#include <Common/assert_cast.h>
8
9
10namespace DB
11{
12
13
14/** Not an aggregate function, but an adapter of aggregate functions,
15 * Aggregate functions with the `State` suffix differ from the corresponding ones in that their states are not finalized.
16 * Return type - DataTypeAggregateFunction.
17 */
18
19class AggregateFunctionState final : public IAggregateFunctionHelper<AggregateFunctionState>
20{
21private:
22 AggregateFunctionPtr nested_func;
23 DataTypes arguments;
24 Array params;
25
26public:
27 AggregateFunctionState(AggregateFunctionPtr nested_, const DataTypes & arguments_, const Array & params_)
28 : IAggregateFunctionHelper<AggregateFunctionState>(arguments_, params_)
29 , nested_func(nested_), arguments(arguments_), params(params_) {}
30
31 String getName() const override
32 {
33 return nested_func->getName() + "State";
34 }
35
36 DataTypePtr getReturnType() const override;
37
38 void create(AggregateDataPtr place) const override
39 {
40 nested_func->create(place);
41 }
42
43 void destroy(AggregateDataPtr place) const noexcept override
44 {
45 nested_func->destroy(place);
46 }
47
48 bool hasTrivialDestructor() const override
49 {
50 return nested_func->hasTrivialDestructor();
51 }
52
53 size_t sizeOfData() const override
54 {
55 return nested_func->sizeOfData();
56 }
57
58 size_t alignOfData() const override
59 {
60 return nested_func->alignOfData();
61 }
62
63 void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override
64 {
65 nested_func->add(place, columns, row_num, arena);
66 }
67
68 void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
69 {
70 nested_func->merge(place, rhs, arena);
71 }
72
73 void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
74 {
75 nested_func->serialize(place, buf);
76 }
77
78 void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena * arena) const override
79 {
80 nested_func->deserialize(place, buf, arena);
81 }
82
83 void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
84 {
85 assert_cast<ColumnAggregateFunction &>(to).getData().push_back(const_cast<AggregateDataPtr>(place));
86 }
87
88 /// Aggregate function or aggregate function state.
89 bool isState() const override { return true; }
90
91 bool allocatesMemoryInArena() const override
92 {
93 return nested_func->allocatesMemoryInArena();
94 }
95
96 AggregateFunctionPtr getNestedFunction() const { return nested_func; }
97};
98
99}
100