1 | #pragma once |
2 | |
3 | #include <DataTypes/DataTypeAggregateFunction.h> |
4 | #include <AggregateFunctions/IAggregateFunction.h> |
5 | #include <Columns/ColumnAggregateFunction.h> |
6 | #include <Common/typeid_cast.h> |
7 | #include <Common/assert_cast.h> |
8 | |
9 | |
10 | namespace DB |
11 | { |
12 | |
13 | |
14 | /** Not an aggregate function, but an adapter of aggregate functions, |
15 | * Aggregate functions with the `Merge` suffix accept `DataTypeAggregateFunction` as an argument |
16 | * (state of the aggregate function obtained earlier using the aggregate function with the `State` suffix) |
17 | * and combine them with aggregation. |
18 | */ |
19 | |
20 | class AggregateFunctionMerge final : public IAggregateFunctionHelper<AggregateFunctionMerge> |
21 | { |
22 | private: |
23 | AggregateFunctionPtr nested_func; |
24 | |
25 | public: |
26 | AggregateFunctionMerge(const AggregateFunctionPtr & nested_, const DataTypePtr & argument) |
27 | : IAggregateFunctionHelper<AggregateFunctionMerge>({argument}, nested_->getParameters()) |
28 | , nested_func(nested_) |
29 | { |
30 | const DataTypeAggregateFunction * data_type = typeid_cast<const DataTypeAggregateFunction *>(argument.get()); |
31 | |
32 | if (!data_type || data_type->getFunctionName() != nested_func->getName()) |
33 | throw Exception("Illegal type " + argument->getName() + " of argument for aggregate function " + getName(), |
34 | ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); |
35 | } |
36 | |
37 | String getName() const override |
38 | { |
39 | return nested_func->getName() + "Merge" ; |
40 | } |
41 | |
42 | DataTypePtr getReturnType() const override |
43 | { |
44 | return nested_func->getReturnType(); |
45 | } |
46 | |
47 | void create(AggregateDataPtr place) const override |
48 | { |
49 | nested_func->create(place); |
50 | } |
51 | |
52 | void destroy(AggregateDataPtr place) const noexcept override |
53 | { |
54 | nested_func->destroy(place); |
55 | } |
56 | |
57 | bool hasTrivialDestructor() const override |
58 | { |
59 | return nested_func->hasTrivialDestructor(); |
60 | } |
61 | |
62 | size_t sizeOfData() const override |
63 | { |
64 | return nested_func->sizeOfData(); |
65 | } |
66 | |
67 | size_t alignOfData() const override |
68 | { |
69 | return nested_func->alignOfData(); |
70 | } |
71 | |
72 | void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override |
73 | { |
74 | nested_func->merge(place, assert_cast<const ColumnAggregateFunction &>(*columns[0]).getData()[row_num], arena); |
75 | } |
76 | |
77 | void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override |
78 | { |
79 | nested_func->merge(place, rhs, arena); |
80 | } |
81 | |
82 | void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override |
83 | { |
84 | nested_func->serialize(place, buf); |
85 | } |
86 | |
87 | void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena * arena) const override |
88 | { |
89 | nested_func->deserialize(place, buf, arena); |
90 | } |
91 | |
92 | void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override |
93 | { |
94 | nested_func->insertResultInto(place, to); |
95 | } |
96 | |
97 | bool allocatesMemoryInArena() const override |
98 | { |
99 | return nested_func->allocatesMemoryInArena(); |
100 | } |
101 | }; |
102 | |
103 | } |
104 | |