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 | | #include "util/simd/bits.h" |
41 | | |
42 | | namespace doris { |
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 | 34.4k | : IAggregateFunctionDataHelper(argument_types_) {} |
59 | | |
60 | 8.32k | bool is_simple_count() const override { return true; } |
61 | 76 | String get_name() const override { return "count"; } |
62 | | |
63 | 122k | DataTypePtr get_return_type() const override { return std::make_shared<DataTypeInt64>(); } |
64 | | |
65 | 0 | bool is_trivial() const override { return true; } |
66 | | |
67 | 21.2M | void add(AggregateDataPtr __restrict place, const IColumn**, ssize_t, Arena&) const override { |
68 | 21.2M | ++data(place).count; |
69 | 21.2M | } |
70 | | |
71 | | void add_batch_single_place(size_t batch_size, AggregateDataPtr place, const IColumn**, |
72 | 69.6k | Arena&) const override { |
73 | 69.6k | data(place).count += batch_size; |
74 | 69.6k | } |
75 | | |
76 | 142 | void reset(AggregateDataPtr place) const override { |
77 | 142 | AggregateFunctionCount::data(place).count = 0; |
78 | 142 | } |
79 | | |
80 | | void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, |
81 | 6.18k | Arena&) const override { |
82 | 6.18k | data(place).count += data(rhs).count; |
83 | 6.18k | } |
84 | | |
85 | 73 | void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { |
86 | 73 | buf.write_var_uint(data(place).count); |
87 | 73 | } |
88 | | |
89 | | void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, |
90 | 73 | Arena&) const override { |
91 | 73 | buf.read_var_uint(data(place).count); |
92 | 73 | } |
93 | | |
94 | 230k | void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { |
95 | 230k | assert_cast<ColumnInt64&, TypeCheckOnRelease::DISABLE>(to).get_data().push_back( |
96 | 230k | data(place).count); |
97 | 230k | } |
98 | | |
99 | | void serialize_to_column(const std::vector<AggregateDataPtr>& places, size_t offset, |
100 | 4.21k | MutableColumnPtr& dst, const size_t num_rows) const override { |
101 | 4.21k | auto& col = assert_cast<ColumnFixedLengthObject&>(*dst); |
102 | 4.21k | DCHECK(col.item_size() == sizeof(Data)) |
103 | 1 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
104 | 4.21k | col.resize(num_rows); |
105 | 4.21k | auto* data = col.get_data().data(); |
106 | 10.0k | for (size_t i = 0; i != num_rows; ++i) { |
107 | 5.82k | *reinterpret_cast<Data*>(&data[sizeof(Data) * i]) = |
108 | 5.82k | *reinterpret_cast<Data*>(places[i] + offset); |
109 | 5.82k | } |
110 | 4.21k | } |
111 | | |
112 | | void streaming_agg_serialize_to_column(const IColumn** columns, MutableColumnPtr& dst, |
113 | 72 | const size_t num_rows, Arena&) const override { |
114 | 72 | auto& dst_col = assert_cast<ColumnFixedLengthObject&>(*dst); |
115 | 72 | DCHECK(dst_col.item_size() == sizeof(Data)) |
116 | 0 | << "size is not equal: " << dst_col.item_size() << " " << sizeof(Data); |
117 | 72 | dst_col.resize(num_rows); |
118 | 72 | auto* data = dst_col.get_data().data(); |
119 | 347 | for (size_t i = 0; i != num_rows; ++i) { |
120 | 275 | auto& state = *reinterpret_cast<Data*>(&data[sizeof(Data) * i]); |
121 | 275 | state.count = 1; |
122 | 275 | } |
123 | 72 | } |
124 | | |
125 | | void deserialize_and_merge_from_column_range(AggregateDataPtr __restrict place, |
126 | | const IColumn& column, size_t begin, size_t end, |
127 | 12.7k | Arena&) const override { |
128 | 12.7k | DCHECK(end <= column.size() && begin <= end) |
129 | 0 | << ", begin:" << begin << ", end:" << end << ", column.size():" << column.size(); |
130 | 12.7k | auto& col = assert_cast<const ColumnFixedLengthObject&>(column); |
131 | 12.7k | auto* data = reinterpret_cast<const Data*>(col.get_data().data()); |
132 | 25.6k | for (size_t i = begin; i <= end; ++i) { |
133 | 12.8k | doris::AggregateFunctionCount::data(place).count += data[i].count; |
134 | 12.8k | } |
135 | 12.7k | } |
136 | | |
137 | | void deserialize_and_merge_vec(const AggregateDataPtr* places, size_t offset, |
138 | | AggregateDataPtr rhs, const IColumn* column, Arena& arena, |
139 | 2.58k | const size_t num_rows) const override { |
140 | 2.58k | const auto& col = assert_cast<const ColumnFixedLengthObject&>(*column); |
141 | 2.58k | const auto* data = col.get_data().data(); |
142 | 2.58k | this->merge_vec(places, offset, AggregateDataPtr(data), arena, num_rows); |
143 | 2.58k | } |
144 | | |
145 | | void deserialize_and_merge_vec_selected(const AggregateDataPtr* places, size_t offset, |
146 | | AggregateDataPtr rhs, const IColumn* column, |
147 | 1 | Arena& arena, const size_t num_rows) const override { |
148 | 1 | const auto& col = assert_cast<const ColumnFixedLengthObject&>(*column); |
149 | 1 | const auto* data = col.get_data().data(); |
150 | 1 | this->merge_vec_selected(places, offset, AggregateDataPtr(data), arena, num_rows); |
151 | 1 | } |
152 | | |
153 | | void serialize_without_key_to_column(ConstAggregateDataPtr __restrict place, |
154 | 12.5k | IColumn& to) const override { |
155 | 12.5k | auto& col = assert_cast<ColumnFixedLengthObject&>(to); |
156 | 12.5k | DCHECK(col.item_size() == sizeof(Data)) |
157 | 8 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
158 | 12.5k | size_t old_size = col.size(); |
159 | 12.5k | col.resize(old_size + 1); |
160 | 12.5k | (reinterpret_cast<Data*>(col.get_data().data()) + old_size)->count = |
161 | 12.5k | AggregateFunctionCount::data(place).count; |
162 | 12.5k | } |
163 | | |
164 | 18.9k | MutableColumnPtr create_serialize_column() const override { |
165 | 18.9k | return ColumnFixedLengthObject::create(sizeof(Data)); |
166 | 18.9k | } |
167 | | |
168 | 18.9k | DataTypePtr get_serialized_type() const override { |
169 | 18.9k | return std::make_shared<DataTypeFixedLengthObject>(); |
170 | 18.9k | } |
171 | | |
172 | | void add_range_single_place(int64_t partition_start, int64_t partition_end, int64_t frame_start, |
173 | | int64_t frame_end, AggregateDataPtr place, const IColumn** columns, |
174 | | Arena& arena, UInt8* use_null_result, |
175 | 250 | UInt8* could_use_previous_result) const override { |
176 | 250 | frame_start = std::max<int64_t>(frame_start, partition_start); |
177 | 250 | frame_end = std::min<int64_t>(frame_end, partition_end); |
178 | 250 | if (frame_start >= frame_end) { |
179 | 12 | if (!*could_use_previous_result) { |
180 | 0 | *use_null_result = true; |
181 | 0 | } |
182 | 238 | } else { |
183 | 238 | AggregateFunctionCount::data(place).count += frame_end - frame_start; |
184 | 238 | *use_null_result = false; |
185 | 238 | *could_use_previous_result = true; |
186 | 238 | } |
187 | 250 | } |
188 | | }; |
189 | | |
190 | | // Used for unary count(nullable_expr). SQL count(expr) counts non-NULL values. |
191 | | class AggregateFunctionCountNotNullUnary final |
192 | | : public IAggregateFunctionDataHelper<AggregateFunctionCountData, |
193 | | AggregateFunctionCountNotNullUnary> { |
194 | | public: |
195 | | AggregateFunctionCountNotNullUnary(const DataTypes& argument_types_) |
196 | 11.3k | : IAggregateFunctionDataHelper(argument_types_) {} |
197 | | |
198 | 113 | String get_name() const override { return "count"; } |
199 | | |
200 | 40.8k | DataTypePtr get_return_type() const override { return std::make_shared<DataTypeInt64>(); } |
201 | | |
202 | 0 | bool is_trivial() const override { return true; } |
203 | | |
204 | | void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num, |
205 | 1.19M | Arena&) const override { |
206 | 1.19M | data(place).count += |
207 | 1.19M | !assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]) |
208 | 1.19M | .is_null_at(row_num); |
209 | 1.19M | } |
210 | | |
211 | | void add_batch_single_place(size_t batch_size, AggregateDataPtr place, const IColumn** columns, |
212 | 18.1k | Arena&) const override { |
213 | 18.1k | const auto& nullable_column = |
214 | 18.1k | assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]); |
215 | 18.1k | const auto& null_map = nullable_column.get_null_map_data(); |
216 | 18.1k | DCHECK_LE(batch_size, null_map.size()); |
217 | 18.1k | if (!nullable_column.has_null(0, batch_size)) { |
218 | 15.4k | data(place).count += batch_size; |
219 | 15.4k | return; |
220 | 15.4k | } |
221 | 2.70k | data(place).count += |
222 | 2.70k | simd::count_zero_num(reinterpret_cast<const int8_t*>(null_map.data()), batch_size); |
223 | 2.70k | } |
224 | | |
225 | 231 | void reset(AggregateDataPtr place) const override { data(place).count = 0; } |
226 | | |
227 | | void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, |
228 | 45.7k | Arena&) const override { |
229 | 45.7k | data(place).count += data(rhs).count; |
230 | 45.7k | } |
231 | | |
232 | 8 | void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { |
233 | 8 | buf.write_var_uint(data(place).count); |
234 | 8 | } |
235 | | |
236 | | void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, |
237 | 8 | Arena&) const override { |
238 | 8 | buf.read_var_uint(data(place).count); |
239 | 8 | } |
240 | | |
241 | 96.9k | void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { |
242 | 96.9k | if (is_column_nullable(to)) { |
243 | 1 | auto& null_column = assert_cast<ColumnNullable&, TypeCheckOnRelease::DISABLE>(to); |
244 | 1 | null_column.get_null_map_data().push_back(0); |
245 | 1 | assert_cast<ColumnInt64&, TypeCheckOnRelease::DISABLE>(null_column.get_nested_column()) |
246 | 1 | .get_data() |
247 | 1 | .push_back(data(place).count); |
248 | 96.9k | } else { |
249 | 96.9k | assert_cast<ColumnInt64&, TypeCheckOnRelease::DISABLE>(to).get_data().push_back( |
250 | 96.9k | data(place).count); |
251 | 96.9k | } |
252 | 96.9k | } |
253 | | |
254 | 12.0k | void check_result_column_type(const IColumn& to) const override { |
255 | 12.0k | if (const auto* null_column = check_and_get_column<ColumnNullable>(to)) { |
256 | 1 | IAggregateFunction::check_result_column_type(null_column->get_nested_column()); |
257 | 1 | return; |
258 | 1 | } |
259 | 12.0k | IAggregateFunction::check_result_column_type(to); |
260 | 12.0k | } |
261 | | |
262 | | void serialize_to_column(const std::vector<AggregateDataPtr>& places, size_t offset, |
263 | 1.94k | MutableColumnPtr& dst, const size_t num_rows) const override { |
264 | 1.94k | auto& col = assert_cast<ColumnFixedLengthObject&>(*dst); |
265 | 1.94k | DCHECK(col.item_size() == sizeof(Data)) |
266 | 7 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
267 | 1.94k | col.resize(num_rows); |
268 | 1.94k | auto* data = col.get_data().data(); |
269 | 57.9k | for (size_t i = 0; i != num_rows; ++i) { |
270 | 55.9k | *reinterpret_cast<Data*>(&data[sizeof(Data) * i]) = |
271 | 55.9k | *reinterpret_cast<Data*>(places[i] + offset); |
272 | 55.9k | } |
273 | 1.94k | } |
274 | | |
275 | | void streaming_agg_serialize_to_column(const IColumn** columns, MutableColumnPtr& dst, |
276 | 2 | const size_t num_rows, Arena&) const override { |
277 | 2 | auto& col = assert_cast<ColumnFixedLengthObject&>(*dst); |
278 | 2 | DCHECK(col.item_size() == sizeof(Data)) |
279 | 0 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
280 | 2 | col.resize(num_rows); |
281 | 2 | auto& data = col.get_data(); |
282 | 2 | const ColumnNullable& input_col = assert_cast<const ColumnNullable&>(*columns[0]); |
283 | 11 | for (size_t i = 0; i < num_rows; i++) { |
284 | 9 | auto& state = *reinterpret_cast<Data*>(&data[sizeof(Data) * i]); |
285 | 9 | state.count = !input_col.is_null_at(i); |
286 | 9 | } |
287 | 2 | } |
288 | | |
289 | | void deserialize_and_merge_from_column_range(AggregateDataPtr __restrict place, |
290 | | const IColumn& column, size_t begin, size_t end, |
291 | 4.51k | Arena&) const override { |
292 | 4.51k | DCHECK(end <= column.size() && begin <= end) |
293 | 0 | << ", begin:" << begin << ", end:" << end << ", column.size():" << column.size(); |
294 | 4.51k | auto& col = assert_cast<const ColumnFixedLengthObject&>(column); |
295 | 4.51k | auto* data = reinterpret_cast<const Data*>(col.get_data().data()); |
296 | 9.03k | for (size_t i = begin; i <= end; ++i) { |
297 | 4.52k | doris::AggregateFunctionCountNotNullUnary::data(place).count += data[i].count; |
298 | 4.52k | } |
299 | 4.51k | } |
300 | | |
301 | | void deserialize_and_merge_vec(const AggregateDataPtr* places, size_t offset, |
302 | | AggregateDataPtr rhs, const IColumn* column, Arena& arena, |
303 | 1.57k | const size_t num_rows) const override { |
304 | 1.57k | const auto& col = assert_cast<const ColumnFixedLengthObject&>(*column); |
305 | 1.57k | const auto* data = col.get_data().data(); |
306 | 1.57k | this->merge_vec(places, offset, AggregateDataPtr(data), arena, num_rows); |
307 | 1.57k | } |
308 | | |
309 | | void deserialize_and_merge_vec_selected(const AggregateDataPtr* places, size_t offset, |
310 | | AggregateDataPtr rhs, const IColumn* column, |
311 | 2 | Arena& arena, const size_t num_rows) const override { |
312 | 2 | const auto& col = assert_cast<const ColumnFixedLengthObject&>(*column); |
313 | 2 | const auto* data = col.get_data().data(); |
314 | 2 | this->merge_vec_selected(places, offset, AggregateDataPtr(data), arena, num_rows); |
315 | 2 | } |
316 | | |
317 | | void serialize_without_key_to_column(ConstAggregateDataPtr __restrict place, |
318 | 4.50k | IColumn& to) const override { |
319 | 4.50k | auto& col = assert_cast<ColumnFixedLengthObject&>(to); |
320 | 4.50k | DCHECK(col.item_size() == sizeof(Data)) |
321 | 4 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
322 | 4.50k | col.resize(1); |
323 | 4.50k | reinterpret_cast<Data*>(col.get_data().data())->count = |
324 | 4.50k | AggregateFunctionCountNotNullUnary::data(place).count; |
325 | 4.50k | } |
326 | | |
327 | 6.45k | MutableColumnPtr create_serialize_column() const override { |
328 | 6.45k | return ColumnFixedLengthObject::create(sizeof(Data)); |
329 | 6.45k | } |
330 | | |
331 | 6.44k | DataTypePtr get_serialized_type() const override { |
332 | 6.44k | return std::make_shared<DataTypeFixedLengthObject>(); |
333 | 6.44k | } |
334 | | |
335 | | void add_range_single_place(int64_t partition_start, int64_t partition_end, int64_t frame_start, |
336 | | int64_t frame_end, AggregateDataPtr place, const IColumn** columns, |
337 | | Arena& arena, UInt8* use_null_result, |
338 | 330 | UInt8* could_use_previous_result) const override { |
339 | 330 | frame_start = std::max<int64_t>(frame_start, partition_start); |
340 | 330 | frame_end = std::min<int64_t>(frame_end, partition_end); |
341 | 330 | if (frame_start >= frame_end) { |
342 | 14 | if (!*could_use_previous_result) { |
343 | 0 | *use_null_result = true; |
344 | 0 | } |
345 | 316 | } else { |
346 | 316 | const auto& nullable_column = |
347 | 316 | assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]); |
348 | 316 | size_t count = 0; |
349 | 316 | if (nullable_column.has_null()) { |
350 | 485 | for (int64_t i = frame_start; i < frame_end; ++i) { |
351 | 293 | if (!nullable_column.is_null_at(i)) { |
352 | 259 | ++count; |
353 | 259 | } |
354 | 293 | } |
355 | 192 | } else { |
356 | 124 | count = frame_end - frame_start; |
357 | 124 | } |
358 | 316 | *use_null_result = false; |
359 | 316 | *could_use_previous_result = true; |
360 | 316 | AggregateFunctionCountNotNullUnary::data(place).count += count; |
361 | 316 | } |
362 | 330 | } |
363 | | }; |
364 | | |
365 | | } // namespace doris |