1 | #pragma once |
2 | |
3 | #include <Columns/ColumnVector.h> |
4 | #include <Common/assert_cast.h> |
5 | #include <AggregateFunctions/IAggregateFunction.h> |
6 | #include <DataTypes/DataTypesNumber.h> |
7 | #include <Columns/ColumnAggregateFunction.h> |
8 | |
9 | // TODO include this last because of a broken roaring header. See the comment |
10 | // inside. |
11 | #include <AggregateFunctions/AggregateFunctionGroupBitmapData.h> |
12 | |
13 | namespace DB |
14 | { |
15 | |
16 | /// Counts bitmap operation on numbers. |
17 | template <typename T, typename Data> |
18 | class AggregateFunctionBitmap final : public IAggregateFunctionDataHelper<Data, AggregateFunctionBitmap<T, Data>> |
19 | { |
20 | public: |
21 | AggregateFunctionBitmap(const DataTypePtr & type) |
22 | : IAggregateFunctionDataHelper<Data, AggregateFunctionBitmap<T, Data>>({type}, {}) {} |
23 | |
24 | String getName() const override { return Data::name(); } |
25 | |
26 | DataTypePtr getReturnType() const override |
27 | { |
28 | return std::make_shared<DataTypeNumber<T>>(); |
29 | } |
30 | |
31 | void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override |
32 | { |
33 | this->data(place).rbs.add(assert_cast<const ColumnVector<T> &>(*columns[0]).getData()[row_num]); |
34 | } |
35 | |
36 | void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override |
37 | { |
38 | this->data(place).rbs.merge(this->data(rhs).rbs); |
39 | } |
40 | |
41 | void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override |
42 | { |
43 | this->data(place).rbs.write(buf); |
44 | } |
45 | |
46 | void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override |
47 | { |
48 | this->data(place).rbs.read(buf); |
49 | } |
50 | |
51 | void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override |
52 | { |
53 | assert_cast<ColumnVector<T> &>(to).getData().push_back(this->data(place).rbs.size()); |
54 | } |
55 | }; |
56 | |
57 | |
58 | template <typename T, typename Data, typename Policy> |
59 | class AggregateFunctionBitmapL2 final : public IAggregateFunctionDataHelper<Data, AggregateFunctionBitmapL2<T, Data, Policy>> |
60 | { |
61 | public: |
62 | AggregateFunctionBitmapL2(const DataTypePtr & type) |
63 | : IAggregateFunctionDataHelper<Data, AggregateFunctionBitmapL2<T, Data, Policy>>({type}, {}){} |
64 | |
65 | String getName() const override { return Data::name(); } |
66 | |
67 | DataTypePtr getReturnType() const override |
68 | { |
69 | return std::make_shared<DataTypeNumber<T>>(); |
70 | } |
71 | |
72 | void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override |
73 | { |
74 | Data & data_lhs = this->data(place); |
75 | const Data & data_rhs = this->data(assert_cast<const ColumnAggregateFunction &>(*columns[0]).getData()[row_num]); |
76 | if (!data_lhs.doneFirst) |
77 | { |
78 | data_lhs.doneFirst = true; |
79 | data_lhs.rbs.rb_or(data_rhs.rbs); |
80 | } |
81 | else |
82 | { |
83 | Policy::apply(data_lhs, data_rhs); |
84 | } |
85 | } |
86 | |
87 | void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override |
88 | { |
89 | Data & data_lhs = this->data(place); |
90 | const Data & data_rhs = this->data(rhs); |
91 | |
92 | if (!data_rhs.doneFirst) |
93 | return; |
94 | |
95 | if (!data_lhs.doneFirst) |
96 | { |
97 | data_lhs.doneFirst = true; |
98 | data_lhs.rbs.rb_or(data_rhs.rbs); |
99 | } |
100 | else |
101 | { |
102 | Policy::apply(data_lhs, data_rhs); |
103 | } |
104 | } |
105 | |
106 | void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override |
107 | { |
108 | this->data(place).rbs.write(buf); |
109 | } |
110 | |
111 | void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override |
112 | { |
113 | this->data(place).rbs.read(buf); |
114 | } |
115 | |
116 | void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override |
117 | { |
118 | assert_cast<ColumnVector<T> &>(to).getData().push_back(this->data(place).rbs.size()); |
119 | } |
120 | }; |
121 | |
122 | template <typename Data> |
123 | class BitmapAndPolicy |
124 | { |
125 | public: |
126 | static void apply(Data& lhs, const Data& rhs) |
127 | { |
128 | lhs.rbs.rb_and(rhs.rbs); |
129 | } |
130 | }; |
131 | |
132 | template <typename Data> |
133 | class BitmapOrPolicy |
134 | { |
135 | public: |
136 | static void apply(Data& lhs, const Data& rhs) |
137 | { |
138 | lhs.rbs.rb_or(rhs.rbs); |
139 | } |
140 | }; |
141 | |
142 | template <typename Data> |
143 | class BitmapXorPolicy |
144 | { |
145 | public: |
146 | static void apply(Data& lhs, const Data& rhs) |
147 | { |
148 | lhs.rbs.rb_xor(rhs.rbs); |
149 | } |
150 | }; |
151 | |
152 | template <typename T, typename Data> |
153 | using AggregateFunctionBitmapL2And = AggregateFunctionBitmapL2<T, Data, BitmapAndPolicy<Data> >; |
154 | |
155 | template <typename T, typename Data> |
156 | using AggregateFunctionBitmapL2Or = AggregateFunctionBitmapL2<T, Data, BitmapOrPolicy<Data> >; |
157 | |
158 | template <typename T, typename Data> |
159 | using AggregateFunctionBitmapL2Xor = AggregateFunctionBitmapL2<T, Data, BitmapXorPolicy<Data> >; |
160 | |
161 | } |
162 | |