/root/doris/be/src/util/counts.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
18 | | #pragma once |
19 | | |
20 | | #include <pdqsort.h> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <cmath> |
24 | | #include <queue> |
25 | | |
26 | | #include "udf/udf.h" |
27 | | #include "vec/common/pod_array.h" |
28 | | #include "vec/common/string_buffer.hpp" |
29 | | #include "vec/io/io_helper.h" |
30 | | |
31 | | namespace doris { |
32 | | |
33 | | class OldCounts { |
34 | | public: |
35 | | OldCounts() = default; |
36 | | |
37 | 0 | inline void merge(const OldCounts* other) { |
38 | 0 | if (other == nullptr || other->_counts.empty()) { |
39 | 0 | return; |
40 | 0 | } |
41 | 0 |
|
42 | 0 | for (auto& cell : other->_counts) { |
43 | 0 | increment(cell.first, cell.second); |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | 0 | void increment(int64_t key, uint32_t i) { |
48 | 0 | auto item = _counts.find(key); |
49 | 0 | if (item != _counts.end()) { |
50 | 0 | item->second += i; |
51 | 0 | } else { |
52 | 0 | _counts.emplace(std::make_pair(key, i)); |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | 0 | uint32_t serialized_size() const { |
57 | 0 | return sizeof(uint32_t) + sizeof(int64_t) * _counts.size() + |
58 | 0 | sizeof(uint32_t) * _counts.size(); |
59 | 0 | } |
60 | | |
61 | 0 | void serialize(uint8_t* writer) const { |
62 | 0 | uint32_t size = _counts.size(); |
63 | 0 | memcpy(writer, &size, sizeof(uint32_t)); |
64 | 0 | writer += sizeof(uint32_t); |
65 | 0 | for (auto& cell : _counts) { |
66 | 0 | memcpy(writer, &cell.first, sizeof(int64_t)); |
67 | 0 | writer += sizeof(int64_t); |
68 | 0 | memcpy(writer, &cell.second, sizeof(uint32_t)); |
69 | 0 | writer += sizeof(uint32_t); |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | 0 | void unserialize(const uint8_t* type_reader) { |
74 | 0 | uint32_t size; |
75 | 0 | memcpy(&size, type_reader, sizeof(uint32_t)); |
76 | 0 | type_reader += sizeof(uint32_t); |
77 | 0 | for (uint32_t i = 0; i < size; ++i) { |
78 | 0 | int64_t key; |
79 | 0 | uint32_t count; |
80 | 0 | memcpy(&key, type_reader, sizeof(int64_t)); |
81 | 0 | type_reader += sizeof(int64_t); |
82 | 0 | memcpy(&count, type_reader, sizeof(uint32_t)); |
83 | 0 | type_reader += sizeof(uint32_t); |
84 | 0 | _counts.emplace(std::make_pair(key, count)); |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | | double get_percentile(std::vector<std::pair<int64_t, uint32_t>>& counts, |
89 | 0 | double position) const { |
90 | 0 | long lower = long(std::floor(position)); |
91 | 0 | long higher = long(std::ceil(position)); |
92 | 0 |
|
93 | 0 | auto iter = counts.begin(); |
94 | 0 | for (; iter != counts.end() && iter->second < lower + 1; ++iter) |
95 | 0 | ; |
96 | 0 |
|
97 | 0 | int64_t lower_key = iter->first; |
98 | 0 | if (higher == lower) { |
99 | 0 | return lower_key; |
100 | 0 | } |
101 | 0 |
|
102 | 0 | if (iter->second < higher + 1) { |
103 | 0 | iter++; |
104 | 0 | } |
105 | 0 |
|
106 | 0 | int64_t higher_key = iter->first; |
107 | 0 | if (lower_key == higher_key) { |
108 | 0 | return lower_key; |
109 | 0 | } |
110 | 0 |
|
111 | 0 | return (higher - position) * lower_key + (position - lower) * higher_key; |
112 | 0 | } |
113 | | |
114 | 0 | double terminate(double quantile) const { |
115 | 0 | if (_counts.empty()) { |
116 | 0 | // Although set null here, but the value is 0.0 and the call method just |
117 | 0 | // get val in aggregate_function_percentile_approx.h |
118 | 0 | return 0.0; |
119 | 0 | } |
120 | 0 |
|
121 | 0 | std::vector<std::pair<int64_t, uint32_t>> elems(_counts.begin(), _counts.end()); |
122 | 0 | sort(elems.begin(), elems.end(), |
123 | 0 | [](const std::pair<int64_t, uint32_t> l, const std::pair<int64_t, uint32_t> r) { |
124 | 0 | return l.first < r.first; |
125 | 0 | }); |
126 | 0 |
|
127 | 0 | long total = 0; |
128 | 0 | for (auto& cell : elems) { |
129 | 0 | total += cell.second; |
130 | 0 | cell.second = total; |
131 | 0 | } |
132 | 0 |
|
133 | 0 | long max_position = total - 1; |
134 | 0 | double position = max_position * quantile; |
135 | 0 | return get_percentile(elems, position); |
136 | 0 | } |
137 | | |
138 | | private: |
139 | | std::unordered_map<int64_t, uint32_t> _counts; |
140 | | }; |
141 | | template <typename Ty> |
142 | | class Counts { |
143 | | public: |
144 | 5 | Counts() = default; Line | Count | Source | 144 | 5 | Counts() = default; |
Unexecuted instantiation: _ZN5doris6CountsIhEC2Ev Unexecuted instantiation: _ZN5doris6CountsIaEC2Ev Unexecuted instantiation: _ZN5doris6CountsIsEC2Ev Unexecuted instantiation: _ZN5doris6CountsIiEC2Ev Unexecuted instantiation: _ZN5doris6CountsInEC2Ev Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev Unexecuted instantiation: _ZN5doris6CountsIdEC2Ev |
145 | | |
146 | 2 | void merge(Counts* other) { |
147 | 2 | if (other != nullptr && !other->_nums.empty()) { |
148 | 2 | _sorted_nums_vec.emplace_back(std::move(other->_nums)); |
149 | 2 | } |
150 | 2 | } _ZN5doris6CountsIlE5mergeEPS1_ Line | Count | Source | 146 | 2 | void merge(Counts* other) { | 147 | 2 | if (other != nullptr && !other->_nums.empty()) { | 148 | 2 | _sorted_nums_vec.emplace_back(std::move(other->_nums)); | 149 | 2 | } | 150 | 2 | } |
Unexecuted instantiation: _ZN5doris6CountsIhE5mergeEPS1_ Unexecuted instantiation: _ZN5doris6CountsIaE5mergeEPS1_ Unexecuted instantiation: _ZN5doris6CountsIsE5mergeEPS1_ Unexecuted instantiation: _ZN5doris6CountsIiE5mergeEPS1_ Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_ Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_ Unexecuted instantiation: _ZN5doris6CountsIdE5mergeEPS1_ |
151 | | |
152 | 12 | void increment(Ty key, uint32_t i) { |
153 | 12 | auto old_size = _nums.size(); |
154 | 12 | _nums.resize(_nums.size() + i); |
155 | 32 | for (uint32_t j = 0; j < i; ++j) { |
156 | 20 | _nums[old_size + j] = key; |
157 | 20 | } |
158 | 12 | } |
159 | | |
160 | 0 | void increment(Ty key) { _nums.push_back(key); } Unexecuted instantiation: _ZN5doris6CountsIhE9incrementEh Unexecuted instantiation: _ZN5doris6CountsIaE9incrementEa Unexecuted instantiation: _ZN5doris6CountsIsE9incrementEs Unexecuted instantiation: _ZN5doris6CountsIiE9incrementEi Unexecuted instantiation: _ZN5doris6CountsIlE9incrementEl Unexecuted instantiation: _ZN5doris6CountsInE9incrementEn Unexecuted instantiation: _ZN5doris6CountsIfE9incrementEf Unexecuted instantiation: _ZN5doris6CountsIdE9incrementEd |
161 | | |
162 | 0 | void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) { |
163 | 0 | _nums.insert(keys.begin(), keys.end()); |
164 | 0 | } Unexecuted instantiation: _ZN5doris6CountsIhE15increment_batchERKNS_10vectorized8PODArrayIhLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_10vectorized8PODArrayIaLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE Unexecuted instantiation: _ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE |
165 | | |
166 | 2 | void serialize(vectorized::BufferWritable& buf) { |
167 | 2 | if (!_nums.empty()) { |
168 | 2 | pdqsort(_nums.begin(), _nums.end()); |
169 | 2 | size_t size = _nums.size(); |
170 | 2 | write_binary(size, buf); |
171 | 2 | buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size); |
172 | 2 | } else { |
173 | | // convert _sorted_nums_vec to _nums and do seiralize again |
174 | 0 | _convert_sorted_num_vec_to_nums(); |
175 | 0 | serialize(buf); |
176 | 0 | } |
177 | 2 | } _ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE Line | Count | Source | 166 | 2 | void serialize(vectorized::BufferWritable& buf) { | 167 | 2 | if (!_nums.empty()) { | 168 | 2 | pdqsort(_nums.begin(), _nums.end()); | 169 | 2 | size_t size = _nums.size(); | 170 | 2 | write_binary(size, buf); | 171 | 2 | buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size); | 172 | 2 | } else { | 173 | | // convert _sorted_nums_vec to _nums and do seiralize again | 174 | 0 | _convert_sorted_num_vec_to_nums(); | 175 | 0 | serialize(buf); | 176 | 0 | } | 177 | 2 | } |
Unexecuted instantiation: _ZN5doris6CountsIhE9serializeERNS_10vectorized14BufferWritableE Unexecuted instantiation: _ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE Unexecuted instantiation: _ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE Unexecuted instantiation: _ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE Unexecuted instantiation: _ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE |
178 | | |
179 | 2 | void unserialize(vectorized::BufferReadable& buf) { |
180 | 2 | size_t size; |
181 | 2 | read_binary(size, buf); |
182 | 2 | _nums.resize(size); |
183 | 2 | auto buff = buf.read(sizeof(Ty) * size); |
184 | 2 | memcpy(_nums.data(), buff.data, buff.size); |
185 | 2 | } _ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE Line | Count | Source | 179 | 2 | void unserialize(vectorized::BufferReadable& buf) { | 180 | 2 | size_t size; | 181 | 2 | read_binary(size, buf); | 182 | 2 | _nums.resize(size); | 183 | 2 | auto buff = buf.read(sizeof(Ty) * size); | 184 | 2 | memcpy(_nums.data(), buff.data, buff.size); | 185 | 2 | } |
Unexecuted instantiation: _ZN5doris6CountsIhE11unserializeERNS_10vectorized14BufferReadableE Unexecuted instantiation: _ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE Unexecuted instantiation: _ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE Unexecuted instantiation: _ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE Unexecuted instantiation: _ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE |
186 | | |
187 | 3 | double terminate(double quantile) { |
188 | 3 | if (_sorted_nums_vec.size() <= 1) { |
189 | 2 | if (_sorted_nums_vec.size() == 1) { |
190 | 0 | _nums = std::move(_sorted_nums_vec[0]); |
191 | 0 | } |
192 | | |
193 | 2 | if (_nums.empty()) { |
194 | | // Although set null here, but the value is 0.0 and the call method just |
195 | | // get val in aggregate_function_percentile_approx.h |
196 | 0 | return 0.0; |
197 | 0 | } |
198 | 2 | if (quantile == 1 || _nums.size() == 1) { |
199 | 0 | return _nums.back(); |
200 | 0 | } |
201 | 2 | if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) { |
202 | 1 | pdqsort(_nums.begin(), _nums.end()); |
203 | 1 | } |
204 | | |
205 | 2 | double u = (_nums.size() - 1) * quantile; |
206 | 2 | auto index = static_cast<uint32_t>(u); |
207 | 2 | return _nums[index] + |
208 | 2 | (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]); |
209 | 2 | } else { |
210 | 1 | DCHECK(_nums.empty()); |
211 | 1 | size_t rows = 0; |
212 | 2 | for (const auto& i : _sorted_nums_vec) { |
213 | 2 | rows += i.size(); |
214 | 2 | } |
215 | 1 | const bool reverse = quantile > 0.5 && rows > 2; |
216 | 1 | double u = (rows - 1) * quantile; |
217 | 1 | auto index = static_cast<uint32_t>(u); |
218 | | // if reverse, the step of target should start 0 like not reverse |
219 | | // so here rows need to minus index + 2 |
220 | | // eg: rows = 10, index = 5 |
221 | | // if not reverse, so the first number loc is 5, the second number loc is 6 |
222 | | // if reverse, so the second number is 3, the first number is 4 |
223 | | // 5 + 4 = 3 + 6 = 9 = rows - 1. |
224 | | // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2 |
225 | 1 | size_t target = reverse ? rows - index - 2 : index; |
226 | 1 | if (quantile == 1) { |
227 | 0 | target = 0; |
228 | 0 | } |
229 | 1 | auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse); |
230 | 1 | if (quantile == 1) { |
231 | 0 | return second_number; |
232 | 0 | } |
233 | 1 | return first_number + (u - static_cast<double>(index)) * (second_number - first_number); |
234 | 1 | } |
235 | 3 | } _ZN5doris6CountsIlE9terminateEd Line | Count | Source | 187 | 3 | double terminate(double quantile) { | 188 | 3 | if (_sorted_nums_vec.size() <= 1) { | 189 | 2 | if (_sorted_nums_vec.size() == 1) { | 190 | 0 | _nums = std::move(_sorted_nums_vec[0]); | 191 | 0 | } | 192 | | | 193 | 2 | if (_nums.empty()) { | 194 | | // Although set null here, but the value is 0.0 and the call method just | 195 | | // get val in aggregate_function_percentile_approx.h | 196 | 0 | return 0.0; | 197 | 0 | } | 198 | 2 | if (quantile == 1 || _nums.size() == 1) { | 199 | 0 | return _nums.back(); | 200 | 0 | } | 201 | 2 | if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) { | 202 | 1 | pdqsort(_nums.begin(), _nums.end()); | 203 | 1 | } | 204 | | | 205 | 2 | double u = (_nums.size() - 1) * quantile; | 206 | 2 | auto index = static_cast<uint32_t>(u); | 207 | 2 | return _nums[index] + | 208 | 2 | (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]); | 209 | 2 | } else { | 210 | 1 | DCHECK(_nums.empty()); | 211 | 1 | size_t rows = 0; | 212 | 2 | for (const auto& i : _sorted_nums_vec) { | 213 | 2 | rows += i.size(); | 214 | 2 | } | 215 | 1 | const bool reverse = quantile > 0.5 && rows > 2; | 216 | 1 | double u = (rows - 1) * quantile; | 217 | 1 | auto index = static_cast<uint32_t>(u); | 218 | | // if reverse, the step of target should start 0 like not reverse | 219 | | // so here rows need to minus index + 2 | 220 | | // eg: rows = 10, index = 5 | 221 | | // if not reverse, so the first number loc is 5, the second number loc is 6 | 222 | | // if reverse, so the second number is 3, the first number is 4 | 223 | | // 5 + 4 = 3 + 6 = 9 = rows - 1. | 224 | | // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2 | 225 | 1 | size_t target = reverse ? rows - index - 2 : index; | 226 | 1 | if (quantile == 1) { | 227 | 0 | target = 0; | 228 | 0 | } | 229 | 1 | auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse); | 230 | 1 | if (quantile == 1) { | 231 | 0 | return second_number; | 232 | 0 | } | 233 | 1 | return first_number + (u - static_cast<double>(index)) * (second_number - first_number); | 234 | 1 | } | 235 | 3 | } |
Unexecuted instantiation: _ZN5doris6CountsIhE9terminateEd Unexecuted instantiation: _ZN5doris6CountsIaE9terminateEd Unexecuted instantiation: _ZN5doris6CountsIsE9terminateEd Unexecuted instantiation: _ZN5doris6CountsIiE9terminateEd Unexecuted instantiation: _ZN5doris6CountsInE9terminateEd Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd Unexecuted instantiation: _ZN5doris6CountsIdE9terminateEd |
236 | | |
237 | | private: |
238 | | struct Node { |
239 | | Ty value; |
240 | | int array_index; |
241 | | int64_t element_index; |
242 | | |
243 | 7 | auto operator<=>(const Node& other) const { return value <=> other.value; } _ZNK5doris6CountsIlE4NodessERKS2_ Line | Count | Source | 243 | 7 | auto operator<=>(const Node& other) const { return value <=> other.value; } |
Unexecuted instantiation: _ZNK5doris6CountsIhE4NodessERKS2_ Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_ Unexecuted instantiation: _ZNK5doris6CountsIsE4NodessERKS2_ Unexecuted instantiation: _ZNK5doris6CountsIiE4NodessERKS2_ Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_ Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_ Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_ |
244 | | }; |
245 | | |
246 | 0 | void _convert_sorted_num_vec_to_nums() { |
247 | 0 | size_t rows = 0; |
248 | 0 | for (const auto& i : _sorted_nums_vec) { |
249 | 0 | rows += i.size(); |
250 | 0 | } |
251 | 0 | _nums.resize(rows); |
252 | 0 | size_t count = 0; |
253 | |
|
254 | 0 | std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap; |
255 | 0 | for (int i = 0; i < _sorted_nums_vec.size(); ++i) { |
256 | 0 | if (!_sorted_nums_vec[i].empty()) { |
257 | 0 | min_heap.emplace(_sorted_nums_vec[i][0], i, 0); |
258 | 0 | } |
259 | 0 | } |
260 | |
|
261 | 0 | while (!min_heap.empty()) { |
262 | 0 | Node node = min_heap.top(); |
263 | 0 | min_heap.pop(); |
264 | 0 | _nums[count++] = node.value; |
265 | 0 | if (++node.element_index < _sorted_nums_vec[node.array_index].size()) { |
266 | 0 | node.value = _sorted_nums_vec[node.array_index][node.element_index]; |
267 | 0 | min_heap.push(node); |
268 | 0 | } |
269 | 0 | } |
270 | 0 | _sorted_nums_vec.clear(); |
271 | 0 | } Unexecuted instantiation: _ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv Unexecuted instantiation: _ZN5doris6CountsIhE31_convert_sorted_num_vec_to_numsEv Unexecuted instantiation: _ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv Unexecuted instantiation: _ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv Unexecuted instantiation: _ZN5doris6CountsInE31_convert_sorted_num_vec_to_numsEv Unexecuted instantiation: _ZN5doris6CountsIfE31_convert_sorted_num_vec_to_numsEv Unexecuted instantiation: _ZN5doris6CountsIdE31_convert_sorted_num_vec_to_numsEv |
272 | | |
273 | 1 | std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) { |
274 | 1 | Ty first_number = 0, second_number = 0; |
275 | 1 | size_t count = 0; |
276 | 1 | if (reverse) { |
277 | 0 | std::priority_queue<Node> max_heap; |
278 | 0 | for (int i = 0; i < _sorted_nums_vec.size(); ++i) { |
279 | 0 | if (!_sorted_nums_vec[i].empty()) { |
280 | 0 | max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i, |
281 | 0 | _sorted_nums_vec[i].size() - 1); |
282 | 0 | } |
283 | 0 | } |
284 | |
|
285 | 0 | while (!max_heap.empty()) { |
286 | 0 | Node node = max_heap.top(); |
287 | 0 | max_heap.pop(); |
288 | 0 | if (count == target) { |
289 | 0 | second_number = node.value; |
290 | 0 | } else if (count == target + 1) { |
291 | 0 | first_number = node.value; |
292 | 0 | break; |
293 | 0 | } |
294 | 0 | ++count; |
295 | 0 | if (--node.element_index >= 0) { |
296 | 0 | node.value = _sorted_nums_vec[node.array_index][node.element_index]; |
297 | 0 | max_heap.push(node); |
298 | 0 | } |
299 | 0 | } |
300 | |
|
301 | 1 | } else { |
302 | 1 | std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap; |
303 | 3 | for (int i = 0; i < _sorted_nums_vec.size(); ++i) { |
304 | 2 | if (!_sorted_nums_vec[i].empty()) { |
305 | 2 | min_heap.emplace(_sorted_nums_vec[i][0], i, 0); |
306 | 2 | } |
307 | 2 | } |
308 | | |
309 | 7 | while (!min_heap.empty()) { |
310 | 7 | Node node = min_heap.top(); |
311 | 7 | min_heap.pop(); |
312 | 7 | if (count == target) { |
313 | 1 | first_number = node.value; |
314 | 6 | } else if (count == target + 1) { |
315 | 1 | second_number = node.value; |
316 | 1 | break; |
317 | 1 | } |
318 | 6 | ++count; |
319 | 6 | if (++node.element_index < _sorted_nums_vec[node.array_index].size()) { |
320 | 6 | node.value = _sorted_nums_vec[node.array_index][node.element_index]; |
321 | 6 | min_heap.push(node); |
322 | 6 | } |
323 | 6 | } |
324 | 1 | } |
325 | | |
326 | 1 | return {first_number, second_number}; |
327 | 1 | } _ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb Line | Count | Source | 273 | 1 | std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) { | 274 | 1 | Ty first_number = 0, second_number = 0; | 275 | 1 | size_t count = 0; | 276 | 1 | if (reverse) { | 277 | 0 | std::priority_queue<Node> max_heap; | 278 | 0 | for (int i = 0; i < _sorted_nums_vec.size(); ++i) { | 279 | 0 | if (!_sorted_nums_vec[i].empty()) { | 280 | 0 | max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i, | 281 | 0 | _sorted_nums_vec[i].size() - 1); | 282 | 0 | } | 283 | 0 | } | 284 | |
| 285 | 0 | while (!max_heap.empty()) { | 286 | 0 | Node node = max_heap.top(); | 287 | 0 | max_heap.pop(); | 288 | 0 | if (count == target) { | 289 | 0 | second_number = node.value; | 290 | 0 | } else if (count == target + 1) { | 291 | 0 | first_number = node.value; | 292 | 0 | break; | 293 | 0 | } | 294 | 0 | ++count; | 295 | 0 | if (--node.element_index >= 0) { | 296 | 0 | node.value = _sorted_nums_vec[node.array_index][node.element_index]; | 297 | 0 | max_heap.push(node); | 298 | 0 | } | 299 | 0 | } | 300 | |
| 301 | 1 | } else { | 302 | 1 | std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap; | 303 | 3 | for (int i = 0; i < _sorted_nums_vec.size(); ++i) { | 304 | 2 | if (!_sorted_nums_vec[i].empty()) { | 305 | 2 | min_heap.emplace(_sorted_nums_vec[i][0], i, 0); | 306 | 2 | } | 307 | 2 | } | 308 | | | 309 | 7 | while (!min_heap.empty()) { | 310 | 7 | Node node = min_heap.top(); | 311 | 7 | min_heap.pop(); | 312 | 7 | if (count == target) { | 313 | 1 | first_number = node.value; | 314 | 6 | } else if (count == target + 1) { | 315 | 1 | second_number = node.value; | 316 | 1 | break; | 317 | 1 | } | 318 | 6 | ++count; | 319 | 6 | if (++node.element_index < _sorted_nums_vec[node.array_index].size()) { | 320 | 6 | node.value = _sorted_nums_vec[node.array_index][node.element_index]; | 321 | 6 | min_heap.push(node); | 322 | 6 | } | 323 | 6 | } | 324 | 1 | } | 325 | | | 326 | 1 | return {first_number, second_number}; | 327 | 1 | } |
Unexecuted instantiation: _ZN5doris6CountsIhE27_merge_sort_and_get_numbersElb Unexecuted instantiation: _ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb Unexecuted instantiation: _ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb Unexecuted instantiation: _ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb Unexecuted instantiation: _ZN5doris6CountsInE27_merge_sort_and_get_numbersElb Unexecuted instantiation: _ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb Unexecuted instantiation: _ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb |
328 | | |
329 | | vectorized::PODArray<Ty> _nums; |
330 | | std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec; |
331 | | }; |
332 | | |
333 | | } // namespace doris |