be/src/exprs/aggregate/aggregate_function_count.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/AggregateFunctions/AggregateFunctionCount.h |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <stddef.h> |
24 | | |
25 | | #include <algorithm> |
26 | | #include <boost/iterator/iterator_facade.hpp> |
27 | | #include <memory> |
28 | | #include <vector> |
29 | | |
30 | | #include "core/assert_cast.h" |
31 | | #include "core/column/column.h" |
32 | | #include "core/column/column_fixed_length_object.h" |
33 | | #include "core/column/column_nullable.h" |
34 | | #include "core/column/column_vector.h" |
35 | | #include "core/data_type/data_type.h" |
36 | | #include "core/data_type/data_type_fixed_length_object.h" |
37 | | #include "core/data_type/data_type_number.h" |
38 | | #include "core/types.h" |
39 | | #include "exprs/aggregate/aggregate_function.h" |
40 | | |
41 | | namespace doris { |
42 | | #include "common/compile_check_begin.h" |
43 | | class Arena; |
44 | | class BufferReadable; |
45 | | class BufferWritable; |
46 | | |
47 | | struct AggregateFunctionCountData { |
48 | | UInt64 count = 0; |
49 | | }; |
50 | | |
51 | | /// Simply count number of calls. |
52 | | class AggregateFunctionCount final |
53 | | : public IAggregateFunctionDataHelper<AggregateFunctionCountData, AggregateFunctionCount>, |
54 | | VarargsExpression, |
55 | | NotNullableAggregateFunction { |
56 | | public: |
57 | | AggregateFunctionCount(const DataTypes& argument_types_) |
58 | 29 | : IAggregateFunctionDataHelper(argument_types_) {} |
59 | | |
60 | 0 | bool is_simple_count() const override { return true; } |
61 | 1 | String get_name() const override { return "count"; } |
62 | | |
63 | 1 | DataTypePtr get_return_type() const override { return std::make_shared<DataTypeInt64>(); } |
64 | | |
65 | 0 | bool is_trivial() const override { return true; } |
66 | | |
67 | 18 | void add(AggregateDataPtr __restrict place, const IColumn**, ssize_t, Arena&) const override { |
68 | 18 | ++data(place).count; |
69 | 18 | } |
70 | | |
71 | 1 | void reset(AggregateDataPtr place) const override { |
72 | 1 | AggregateFunctionCount::data(place).count = 0; |
73 | 1 | } |
74 | | |
75 | | void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, |
76 | 2 | Arena&) const override { |
77 | 2 | data(place).count += data(rhs).count; |
78 | 2 | } |
79 | | |
80 | 0 | void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { |
81 | 0 | buf.write_var_uint(data(place).count); |
82 | 0 | } |
83 | | |
84 | | void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, |
85 | 0 | Arena&) const override { |
86 | 0 | buf.read_var_uint(data(place).count); |
87 | 0 | } |
88 | | |
89 | 6 | void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { |
90 | 6 | assert_cast<ColumnInt64&>(to).get_data().push_back(data(place).count); |
91 | 6 | } |
92 | | |
93 | | void serialize_to_column(const std::vector<AggregateDataPtr>& places, size_t offset, |
94 | 3 | MutableColumnPtr& dst, const size_t num_rows) const override { |
95 | 3 | auto& col = assert_cast<ColumnFixedLengthObject&>(*dst); |
96 | 3 | DCHECK(col.item_size() == sizeof(Data)) |
97 | 0 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
98 | 3 | col.resize(num_rows); |
99 | 3 | auto* data = col.get_data().data(); |
100 | 6 | for (size_t i = 0; i != num_rows; ++i) { |
101 | 3 | *reinterpret_cast<Data*>(&data[sizeof(Data) * i]) = |
102 | 3 | *reinterpret_cast<Data*>(places[i] + offset); |
103 | 3 | } |
104 | 3 | } |
105 | | |
106 | | void streaming_agg_serialize_to_column(const IColumn** columns, MutableColumnPtr& dst, |
107 | 1 | const size_t num_rows, Arena&) const override { |
108 | 1 | auto& dst_col = assert_cast<ColumnFixedLengthObject&>(*dst); |
109 | 1 | DCHECK(dst_col.item_size() == sizeof(Data)) |
110 | 0 | << "size is not equal: " << dst_col.item_size() << " " << sizeof(Data); |
111 | 1 | dst_col.resize(num_rows); |
112 | 1 | auto* data = dst_col.get_data().data(); |
113 | 4 | for (size_t i = 0; i != num_rows; ++i) { |
114 | 3 | auto& state = *reinterpret_cast<Data*>(&data[sizeof(Data) * i]); |
115 | 3 | state.count = 1; |
116 | 3 | } |
117 | 1 | } |
118 | | |
119 | | void deserialize_and_merge_from_column_range(AggregateDataPtr __restrict place, |
120 | | const IColumn& column, size_t begin, size_t end, |
121 | 2 | Arena&) const override { |
122 | 2 | DCHECK(end <= column.size() && begin <= end) |
123 | 0 | << ", begin:" << begin << ", end:" << end << ", column.size():" << column.size(); |
124 | 2 | auto& col = assert_cast<const ColumnFixedLengthObject&>(column); |
125 | 2 | auto* data = reinterpret_cast<const Data*>(col.get_data().data()); |
126 | 6 | for (size_t i = begin; i <= end; ++i) { |
127 | 4 | doris::AggregateFunctionCount::data(place).count += data[i].count; |
128 | 4 | } |
129 | 2 | } |
130 | | |
131 | | void deserialize_and_merge_vec(const AggregateDataPtr* places, size_t offset, |
132 | | AggregateDataPtr rhs, const IColumn* column, Arena& arena, |
133 | 1 | const size_t num_rows) const override { |
134 | 1 | const auto& col = assert_cast<const ColumnFixedLengthObject&>(*column); |
135 | 1 | const auto* data = col.get_data().data(); |
136 | 1 | this->merge_vec(places, offset, AggregateDataPtr(data), arena, num_rows); |
137 | 1 | } |
138 | | |
139 | | void deserialize_and_merge_vec_selected(const AggregateDataPtr* places, size_t offset, |
140 | | AggregateDataPtr rhs, const IColumn* column, |
141 | 1 | Arena& arena, const size_t num_rows) const override { |
142 | 1 | const auto& col = assert_cast<const ColumnFixedLengthObject&>(*column); |
143 | 1 | const auto* data = col.get_data().data(); |
144 | 1 | this->merge_vec_selected(places, offset, AggregateDataPtr(data), arena, num_rows); |
145 | 1 | } |
146 | | |
147 | | void serialize_without_key_to_column(ConstAggregateDataPtr __restrict place, |
148 | 1 | IColumn& to) const override { |
149 | 1 | auto& col = assert_cast<ColumnFixedLengthObject&>(to); |
150 | 1 | DCHECK(col.item_size() == sizeof(Data)) |
151 | 0 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
152 | 1 | size_t old_size = col.size(); |
153 | 1 | col.resize(old_size + 1); |
154 | 1 | (reinterpret_cast<Data*>(col.get_data().data()) + old_size)->count = |
155 | 1 | AggregateFunctionCount::data(place).count; |
156 | 1 | } |
157 | | |
158 | 17 | MutableColumnPtr create_serialize_column() const override { |
159 | 17 | return ColumnFixedLengthObject::create(sizeof(Data)); |
160 | 17 | } |
161 | | |
162 | 28 | DataTypePtr get_serialized_type() const override { |
163 | 28 | return std::make_shared<DataTypeFixedLengthObject>(); |
164 | 28 | } |
165 | | |
166 | | void add_range_single_place(int64_t partition_start, int64_t partition_end, int64_t frame_start, |
167 | | int64_t frame_end, AggregateDataPtr place, const IColumn** columns, |
168 | | Arena& arena, UInt8* use_null_result, |
169 | 0 | UInt8* could_use_previous_result) const override { |
170 | 0 | frame_start = std::max<int64_t>(frame_start, partition_start); |
171 | 0 | frame_end = std::min<int64_t>(frame_end, partition_end); |
172 | 0 | if (frame_start >= frame_end) { |
173 | 0 | if (!*could_use_previous_result) { |
174 | 0 | *use_null_result = true; |
175 | 0 | } |
176 | 0 | } else { |
177 | 0 | AggregateFunctionCount::data(place).count += frame_end - frame_start; |
178 | 0 | *use_null_result = false; |
179 | 0 | *could_use_previous_result = true; |
180 | 0 | } |
181 | 0 | } |
182 | | }; |
183 | | |
184 | | // TODO: Maybe AggregateFunctionCountNotNullUnary should be a subclass of AggregateFunctionCount |
185 | | // Simply count number of not-NULL values. |
186 | | class AggregateFunctionCountNotNullUnary final |
187 | | : public IAggregateFunctionDataHelper<AggregateFunctionCountData, |
188 | | AggregateFunctionCountNotNullUnary> { |
189 | | public: |
190 | | AggregateFunctionCountNotNullUnary(const DataTypes& argument_types_) |
191 | 0 | : IAggregateFunctionDataHelper(argument_types_) {} |
192 | | |
193 | 0 | String get_name() const override { return "count"; } |
194 | | |
195 | 0 | DataTypePtr get_return_type() const override { return std::make_shared<DataTypeInt64>(); } |
196 | | |
197 | 0 | bool is_trivial() const override { return true; } |
198 | | |
199 | | void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num, |
200 | 0 | Arena&) const override { |
201 | 0 | data(place).count += |
202 | 0 | !assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]) |
203 | 0 | .is_null_at(row_num); |
204 | 0 | } |
205 | | |
206 | 0 | void reset(AggregateDataPtr place) const override { data(place).count = 0; } |
207 | | |
208 | | void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, |
209 | 0 | Arena&) const override { |
210 | 0 | data(place).count += data(rhs).count; |
211 | 0 | } |
212 | | |
213 | 0 | void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { |
214 | 0 | buf.write_var_uint(data(place).count); |
215 | 0 | } |
216 | | |
217 | | void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, |
218 | 0 | Arena&) const override { |
219 | 0 | buf.read_var_uint(data(place).count); |
220 | 0 | } |
221 | | |
222 | 0 | void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { |
223 | 0 | if (to.is_nullable()) { |
224 | 0 | auto& null_column = assert_cast<ColumnNullable&>(to); |
225 | 0 | null_column.get_null_map_data().push_back(0); |
226 | 0 | assert_cast<ColumnInt64&>(null_column.get_nested_column()) |
227 | 0 | .get_data() |
228 | 0 | .push_back(data(place).count); |
229 | 0 | } else { |
230 | 0 | assert_cast<ColumnInt64&>(to).get_data().push_back(data(place).count); |
231 | 0 | } |
232 | 0 | } |
233 | | |
234 | | void serialize_to_column(const std::vector<AggregateDataPtr>& places, size_t offset, |
235 | 0 | MutableColumnPtr& dst, const size_t num_rows) const override { |
236 | 0 | auto& col = assert_cast<ColumnFixedLengthObject&>(*dst); |
237 | 0 | DCHECK(col.item_size() == sizeof(Data)) |
238 | 0 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
239 | 0 | col.resize(num_rows); |
240 | 0 | auto* data = col.get_data().data(); |
241 | 0 | for (size_t i = 0; i != num_rows; ++i) { |
242 | 0 | *reinterpret_cast<Data*>(&data[sizeof(Data) * i]) = |
243 | 0 | *reinterpret_cast<Data*>(places[i] + offset); |
244 | 0 | } |
245 | 0 | } |
246 | | |
247 | | void streaming_agg_serialize_to_column(const IColumn** columns, MutableColumnPtr& dst, |
248 | 0 | const size_t num_rows, Arena&) const override { |
249 | 0 | auto& col = assert_cast<ColumnFixedLengthObject&>(*dst); |
250 | 0 | DCHECK(col.item_size() == sizeof(Data)) |
251 | 0 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
252 | 0 | col.resize(num_rows); |
253 | 0 | auto& data = col.get_data(); |
254 | 0 | const ColumnNullable& input_col = assert_cast<const ColumnNullable&>(*columns[0]); |
255 | 0 | for (size_t i = 0; i < num_rows; i++) { |
256 | 0 | auto& state = *reinterpret_cast<Data*>(&data[sizeof(Data) * i]); |
257 | 0 | state.count = !input_col.is_null_at(i); |
258 | 0 | } |
259 | 0 | } |
260 | | |
261 | | void deserialize_and_merge_from_column_range(AggregateDataPtr __restrict place, |
262 | | const IColumn& column, size_t begin, size_t end, |
263 | 0 | Arena&) const override { |
264 | 0 | DCHECK(end <= column.size() && begin <= end) |
265 | 0 | << ", begin:" << begin << ", end:" << end << ", column.size():" << column.size(); |
266 | 0 | auto& col = assert_cast<const ColumnFixedLengthObject&>(column); |
267 | 0 | auto* data = reinterpret_cast<const Data*>(col.get_data().data()); |
268 | 0 | for (size_t i = begin; i <= end; ++i) { |
269 | 0 | doris::AggregateFunctionCountNotNullUnary::data(place).count += data[i].count; |
270 | 0 | } |
271 | 0 | } |
272 | | |
273 | | void deserialize_and_merge_vec(const AggregateDataPtr* places, size_t offset, |
274 | | AggregateDataPtr rhs, const IColumn* column, Arena& arena, |
275 | 0 | const size_t num_rows) const override { |
276 | 0 | const auto& col = assert_cast<const ColumnFixedLengthObject&>(*column); |
277 | 0 | const auto* data = col.get_data().data(); |
278 | 0 | this->merge_vec(places, offset, AggregateDataPtr(data), arena, num_rows); |
279 | 0 | } |
280 | | |
281 | | void deserialize_and_merge_vec_selected(const AggregateDataPtr* places, size_t offset, |
282 | | AggregateDataPtr rhs, const IColumn* column, |
283 | 0 | Arena& arena, const size_t num_rows) const override { |
284 | 0 | const auto& col = assert_cast<const ColumnFixedLengthObject&>(*column); |
285 | 0 | const auto* data = col.get_data().data(); |
286 | 0 | this->merge_vec_selected(places, offset, AggregateDataPtr(data), arena, num_rows); |
287 | 0 | } |
288 | | |
289 | | void serialize_without_key_to_column(ConstAggregateDataPtr __restrict place, |
290 | 0 | IColumn& to) const override { |
291 | 0 | auto& col = assert_cast<ColumnFixedLengthObject&>(to); |
292 | 0 | DCHECK(col.item_size() == sizeof(Data)) |
293 | 0 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
294 | 0 | col.resize(1); |
295 | 0 | reinterpret_cast<Data*>(col.get_data().data())->count = |
296 | 0 | AggregateFunctionCountNotNullUnary::data(place).count; |
297 | 0 | } |
298 | | |
299 | 0 | MutableColumnPtr create_serialize_column() const override { |
300 | 0 | return ColumnFixedLengthObject::create(sizeof(Data)); |
301 | 0 | } |
302 | | |
303 | 0 | DataTypePtr get_serialized_type() const override { |
304 | 0 | return std::make_shared<DataTypeFixedLengthObject>(); |
305 | 0 | } |
306 | | |
307 | | void add_range_single_place(int64_t partition_start, int64_t partition_end, int64_t frame_start, |
308 | | int64_t frame_end, AggregateDataPtr place, const IColumn** columns, |
309 | | Arena& arena, UInt8* use_null_result, |
310 | 0 | UInt8* could_use_previous_result) const override { |
311 | 0 | frame_start = std::max<int64_t>(frame_start, partition_start); |
312 | 0 | frame_end = std::min<int64_t>(frame_end, partition_end); |
313 | 0 | if (frame_start >= frame_end) { |
314 | 0 | if (!*could_use_previous_result) { |
315 | 0 | *use_null_result = true; |
316 | 0 | } |
317 | 0 | } else { |
318 | 0 | const auto& nullable_column = |
319 | 0 | assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]); |
320 | 0 | size_t count = 0; |
321 | 0 | if (nullable_column.has_null()) { |
322 | 0 | for (int64_t i = frame_start; i < frame_end; ++i) { |
323 | 0 | if (!nullable_column.is_null_at(i)) { |
324 | 0 | ++count; |
325 | 0 | } |
326 | 0 | } |
327 | 0 | } else { |
328 | 0 | count = frame_end - frame_start; |
329 | 0 | } |
330 | 0 | *use_null_result = false; |
331 | 0 | *could_use_previous_result = true; |
332 | 0 | AggregateFunctionCountNotNullUnary::data(place).count += count; |
333 | 0 | } |
334 | 0 | } |
335 | | }; |
336 | | |
337 | | } // namespace doris |
338 | | |
339 | | #include "common/compile_check_end.h" |