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 | 31 | : 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 | 7 | DataTypePtr get_return_type() const override { return std::make_shared<DataTypeInt64>(); } |
64 | | |
65 | 0 | bool is_trivial() const override { return true; } |
66 | | |
67 | 0 | void add(AggregateDataPtr __restrict place, const IColumn**, ssize_t, Arena&) const override { |
68 | 0 | ++data(place).count; |
69 | 0 | } |
70 | | |
71 | | void add_batch_single_place(size_t batch_size, AggregateDataPtr place, const IColumn**, |
72 | 6 | Arena&) const override { |
73 | 6 | data(place).count += batch_size; |
74 | 6 | } |
75 | | |
76 | 1 | void reset(AggregateDataPtr place) const override { |
77 | 1 | AggregateFunctionCount::data(place).count = 0; |
78 | 1 | } |
79 | | |
80 | | void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, |
81 | 2 | Arena&) const override { |
82 | 2 | data(place).count += data(rhs).count; |
83 | 2 | } |
84 | | |
85 | 0 | void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { |
86 | 0 | buf.write_var_uint(data(place).count); |
87 | 0 | } |
88 | | |
89 | | void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, |
90 | 0 | Arena&) const override { |
91 | 0 | buf.read_var_uint(data(place).count); |
92 | 0 | } |
93 | | |
94 | 6 | void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { |
95 | 6 | assert_cast<ColumnInt64&, TypeCheckOnRelease::DISABLE>(to).get_data().push_back( |
96 | 6 | data(place).count); |
97 | 6 | } |
98 | | |
99 | | void serialize_to_column(const std::vector<AggregateDataPtr>& places, size_t offset, |
100 | 3 | MutableColumnPtr& dst, const size_t num_rows) const override { |
101 | 3 | auto& col = assert_cast<ColumnFixedLengthObject&>(*dst); |
102 | 3 | DCHECK(col.item_size() == sizeof(Data)) |
103 | 0 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
104 | 3 | col.resize(num_rows); |
105 | 3 | auto* data = col.get_data().data(); |
106 | 6 | for (size_t i = 0; i != num_rows; ++i) { |
107 | 3 | *reinterpret_cast<Data*>(&data[sizeof(Data) * i]) = |
108 | 3 | *reinterpret_cast<Data*>(places[i] + offset); |
109 | 3 | } |
110 | 3 | } |
111 | | |
112 | | void streaming_agg_serialize_to_column(const IColumn** columns, MutableColumnPtr& dst, |
113 | 1 | const size_t num_rows, Arena&) const override { |
114 | 1 | auto& dst_col = assert_cast<ColumnFixedLengthObject&>(*dst); |
115 | 1 | DCHECK(dst_col.item_size() == sizeof(Data)) |
116 | 0 | << "size is not equal: " << dst_col.item_size() << " " << sizeof(Data); |
117 | 1 | dst_col.resize(num_rows); |
118 | 1 | auto* data = dst_col.get_data().data(); |
119 | 4 | for (size_t i = 0; i != num_rows; ++i) { |
120 | 3 | auto& state = *reinterpret_cast<Data*>(&data[sizeof(Data) * i]); |
121 | 3 | state.count = 1; |
122 | 3 | } |
123 | 1 | } |
124 | | |
125 | | void deserialize_and_merge_from_column_range(AggregateDataPtr __restrict place, |
126 | | const IColumn& column, size_t begin, size_t end, |
127 | 2 | Arena&) const override { |
128 | 2 | DCHECK(end <= column.size() && begin <= end) |
129 | 0 | << ", begin:" << begin << ", end:" << end << ", column.size():" << column.size(); |
130 | 2 | auto& col = assert_cast<const ColumnFixedLengthObject&>(column); |
131 | 2 | auto* data = reinterpret_cast<const Data*>(col.get_data().data()); |
132 | 6 | for (size_t i = begin; i <= end; ++i) { |
133 | 4 | doris::AggregateFunctionCount::data(place).count += data[i].count; |
134 | 4 | } |
135 | 2 | } |
136 | | |
137 | | void deserialize_and_merge_vec(const AggregateDataPtr* places, size_t offset, |
138 | | AggregateDataPtr rhs, const IColumn* column, Arena& arena, |
139 | 1 | const size_t num_rows) const override { |
140 | 1 | const auto& col = assert_cast<const ColumnFixedLengthObject&>(*column); |
141 | 1 | const auto* data = col.get_data().data(); |
142 | 1 | this->merge_vec(places, offset, AggregateDataPtr(data), arena, num_rows); |
143 | 1 | } |
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 | 1 | IColumn& to) const override { |
155 | 1 | auto& col = assert_cast<ColumnFixedLengthObject&>(to); |
156 | 1 | DCHECK(col.item_size() == sizeof(Data)) |
157 | 0 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
158 | 1 | size_t old_size = col.size(); |
159 | 1 | col.resize(old_size + 1); |
160 | 1 | (reinterpret_cast<Data*>(col.get_data().data()) + old_size)->count = |
161 | 1 | AggregateFunctionCount::data(place).count; |
162 | 1 | } |
163 | | |
164 | 18 | MutableColumnPtr create_serialize_column() const override { |
165 | 18 | return ColumnFixedLengthObject::create(sizeof(Data)); |
166 | 18 | } |
167 | | |
168 | 30 | DataTypePtr get_serialized_type() const override { |
169 | 30 | return std::make_shared<DataTypeFixedLengthObject>(); |
170 | 30 | } |
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 | 0 | UInt8* could_use_previous_result) const override { |
176 | 0 | frame_start = std::max<int64_t>(frame_start, partition_start); |
177 | 0 | frame_end = std::min<int64_t>(frame_end, partition_end); |
178 | 0 | if (frame_start >= frame_end) { |
179 | 0 | if (!*could_use_previous_result) { |
180 | 0 | *use_null_result = true; |
181 | 0 | } |
182 | 0 | } else { |
183 | 0 | AggregateFunctionCount::data(place).count += frame_end - frame_start; |
184 | 0 | *use_null_result = false; |
185 | 0 | *could_use_previous_result = true; |
186 | 0 | } |
187 | 0 | } |
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 | 3 | : IAggregateFunctionDataHelper(argument_types_) {} |
197 | | |
198 | 2 | String get_name() const override { return "count"; } |
199 | | |
200 | 16 | 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 | 0 | Arena&) const override { |
206 | 0 | data(place).count += |
207 | 0 | !assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]) |
208 | 0 | .is_null_at(row_num); |
209 | 0 | } |
210 | | |
211 | | void add_batch_single_place(size_t batch_size, AggregateDataPtr place, const IColumn** columns, |
212 | 12 | Arena&) const override { |
213 | 12 | const auto& nullable_column = |
214 | 12 | assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]); |
215 | 12 | const auto& null_map = nullable_column.get_null_map_data(); |
216 | 12 | DCHECK_LE(batch_size, null_map.size()); |
217 | 12 | if (!nullable_column.has_null(0, batch_size)) { |
218 | 6 | data(place).count += batch_size; |
219 | 6 | return; |
220 | 6 | } |
221 | 6 | data(place).count += |
222 | 6 | simd::count_zero_num(reinterpret_cast<const int8_t*>(null_map.data()), batch_size); |
223 | 6 | } |
224 | | |
225 | 2 | void reset(AggregateDataPtr place) const override { data(place).count = 0; } |
226 | | |
227 | | void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, |
228 | 4 | Arena&) const override { |
229 | 4 | data(place).count += data(rhs).count; |
230 | 4 | } |
231 | | |
232 | 0 | void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { |
233 | 0 | buf.write_var_uint(data(place).count); |
234 | 0 | } |
235 | | |
236 | | void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, |
237 | 0 | Arena&) const override { |
238 | 0 | buf.read_var_uint(data(place).count); |
239 | 0 | } |
240 | | |
241 | 13 | void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { |
242 | 13 | 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 | 12 | } else { |
249 | 12 | assert_cast<ColumnInt64&, TypeCheckOnRelease::DISABLE>(to).get_data().push_back( |
250 | 12 | data(place).count); |
251 | 12 | } |
252 | 13 | } |
253 | | |
254 | 13 | void check_result_column_type(const IColumn& to) const override { |
255 | 13 | 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 | IAggregateFunction::check_result_column_type(to); |
260 | 12 | } |
261 | | |
262 | | void serialize_to_column(const std::vector<AggregateDataPtr>& places, size_t offset, |
263 | 6 | MutableColumnPtr& dst, const size_t num_rows) const override { |
264 | 6 | auto& col = assert_cast<ColumnFixedLengthObject&>(*dst); |
265 | 6 | DCHECK(col.item_size() == sizeof(Data)) |
266 | 0 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
267 | 6 | col.resize(num_rows); |
268 | 6 | auto* data = col.get_data().data(); |
269 | 12 | for (size_t i = 0; i != num_rows; ++i) { |
270 | 6 | *reinterpret_cast<Data*>(&data[sizeof(Data) * i]) = |
271 | 6 | *reinterpret_cast<Data*>(places[i] + offset); |
272 | 6 | } |
273 | 6 | } |
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 | Arena&) const override { |
292 | 4 | DCHECK(end <= column.size() && begin <= end) |
293 | 0 | << ", begin:" << begin << ", end:" << end << ", column.size():" << column.size(); |
294 | 4 | auto& col = assert_cast<const ColumnFixedLengthObject&>(column); |
295 | 4 | auto* data = reinterpret_cast<const Data*>(col.get_data().data()); |
296 | 15 | for (size_t i = begin; i <= end; ++i) { |
297 | 11 | doris::AggregateFunctionCountNotNullUnary::data(place).count += data[i].count; |
298 | 11 | } |
299 | 4 | } |
300 | | |
301 | | void deserialize_and_merge_vec(const AggregateDataPtr* places, size_t offset, |
302 | | AggregateDataPtr rhs, const IColumn* column, Arena& arena, |
303 | 2 | const size_t num_rows) const override { |
304 | 2 | const auto& col = assert_cast<const ColumnFixedLengthObject&>(*column); |
305 | 2 | const auto* data = col.get_data().data(); |
306 | 2 | this->merge_vec(places, offset, AggregateDataPtr(data), arena, num_rows); |
307 | 2 | } |
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 | 2 | IColumn& to) const override { |
319 | 2 | auto& col = assert_cast<ColumnFixedLengthObject&>(to); |
320 | 2 | DCHECK(col.item_size() == sizeof(Data)) |
321 | 0 | << "size is not equal: " << col.item_size() << " " << sizeof(Data); |
322 | 2 | col.resize(1); |
323 | 2 | reinterpret_cast<Data*>(col.get_data().data())->count = |
324 | 2 | AggregateFunctionCountNotNullUnary::data(place).count; |
325 | 2 | } |
326 | | |
327 | 10 | MutableColumnPtr create_serialize_column() const override { |
328 | 10 | return ColumnFixedLengthObject::create(sizeof(Data)); |
329 | 10 | } |
330 | | |
331 | 0 | DataTypePtr get_serialized_type() const override { |
332 | 0 | return std::make_shared<DataTypeFixedLengthObject>(); |
333 | 0 | } |
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 | 0 | UInt8* could_use_previous_result) const override { |
339 | 0 | frame_start = std::max<int64_t>(frame_start, partition_start); |
340 | 0 | frame_end = std::min<int64_t>(frame_end, partition_end); |
341 | 0 | if (frame_start >= frame_end) { |
342 | 0 | if (!*could_use_previous_result) { |
343 | 0 | *use_null_result = true; |
344 | 0 | } |
345 | 0 | } else { |
346 | 0 | const auto& nullable_column = |
347 | 0 | assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]); |
348 | 0 | size_t count = 0; |
349 | 0 | if (nullable_column.has_null()) { |
350 | 0 | for (int64_t i = frame_start; i < frame_end; ++i) { |
351 | 0 | if (!nullable_column.is_null_at(i)) { |
352 | 0 | ++count; |
353 | 0 | } |
354 | 0 | } |
355 | 0 | } else { |
356 | 0 | count = frame_end - frame_start; |
357 | 0 | } |
358 | 0 | *use_null_result = false; |
359 | 0 | *could_use_previous_result = true; |
360 | 0 | AggregateFunctionCountNotNullUnary::data(place).count += count; |
361 | 0 | } |
362 | 0 | } |
363 | | }; |
364 | | |
365 | | } // namespace doris |