Coverage Report

Created: 2026-09-25 06:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/percentile_util.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
18
#pragma once
19
20
#include <pdqsort.h>
21
22
#include <algorithm>
23
#include <cmath>
24
#include <cstdint>
25
#include <queue>
26
#include <string>
27
#include <vector>
28
29
#include "common/cast_set.h"
30
#include "common/exception.h"
31
#include "core/column/column_nullable.h"
32
#include "core/pod_array.h"
33
#include "core/string_buffer.hpp"
34
35
namespace doris {
36
37
5
inline void check_quantile(double quantile) {
38
5
    if (quantile < 0 || quantile > 1) {
39
2
        throw Exception(ErrorCode::INVALID_ARGUMENT,
40
2
                        "quantile in func percentile should in [0, 1], but real data is:" +
41
2
                                std::to_string(quantile));
42
2
    }
43
5
}
44
45
template <typename Ty>
46
class Counts {
47
public:
48
30
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
48
30
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIaEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIsEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIiEC2Ev
Unexecuted instantiation: _ZN5doris6CountsInEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIdEC2Ev
49
50
    // Moves all samples of `other` into this state. `other` may itself be a merged state whose
51
    // samples live in `_sorted_nums_vec`, and either side may still hold unsorted raw samples.
52
15
    void merge(Counts* other) {
53
15
        _move_nums_to_sorted_vec();
54
15
        other->_move_nums_to_sorted_vec();
55
15
        for (auto& nums : other->_sorted_nums_vec) {
56
14
            _sorted_nums_vec.emplace_back(std::move(nums));
57
14
        }
58
15
        other->_sorted_nums_vec.clear();
59
15
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
52
15
    void merge(Counts* other) {
53
15
        _move_nums_to_sorted_vec();
54
15
        other->_move_nums_to_sorted_vec();
55
15
        for (auto& nums : other->_sorted_nums_vec) {
56
14
            _sorted_nums_vec.emplace_back(std::move(nums));
57
14
        }
58
15
        other->_sorted_nums_vec.clear();
59
15
    }
Unexecuted instantiation: _ZN5doris6CountsIaE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIsE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIiE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIdE5mergeEPS1_
60
61
12
    void increment(Ty key, uint32_t i) {
62
12
        auto old_size = _nums.size();
63
12
        _nums.resize(_nums.size() + i);
64
32
        for (uint32_t j = 0; j < i; ++j) {
65
20
            _nums[old_size + j] = key;
66
20
        }
67
12
    }
68
69
22
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
69
22
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIaE9incrementEa
Unexecuted instantiation: _ZN5doris6CountsIsE9incrementEs
Unexecuted instantiation: _ZN5doris6CountsIiE9incrementEi
Unexecuted instantiation: _ZN5doris6CountsInE9incrementEn
Unexecuted instantiation: _ZN5doris6CountsIfE9incrementEf
Unexecuted instantiation: _ZN5doris6CountsIdE9incrementEd
70
71
0
    void increment_batch(const PaddedPODArray<Ty>& keys) { _nums.insert(keys.begin(), keys.end()); }
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIlE15increment_batchERKNS_8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
72
73
6
    void serialize(BufferWritable& buf) {
74
6
        if (_sorted_nums_vec.empty()) {
75
4
            pdqsort(_nums.begin(), _nums.end());
76
4
        } else {
77
            // merge all sorted runs (including the raw samples) into `_nums`
78
2
            _move_nums_to_sorted_vec();
79
2
            _convert_sorted_num_vec_to_nums();
80
2
        }
81
6
        size_t size = _nums.size();
82
6
        buf.write_binary(size);
83
6
        buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
84
6
    }
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
73
6
    void serialize(BufferWritable& buf) {
74
6
        if (_sorted_nums_vec.empty()) {
75
4
            pdqsort(_nums.begin(), _nums.end());
76
4
        } else {
77
            // merge all sorted runs (including the raw samples) into `_nums`
78
2
            _move_nums_to_sorted_vec();
79
2
            _convert_sorted_num_vec_to_nums();
80
2
        }
81
6
        size_t size = _nums.size();
82
6
        buf.write_binary(size);
83
6
        buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
84
6
    }
Unexecuted instantiation: _ZN5doris6CountsIaE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIsE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIiE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIdE9serializeERNS_14BufferWritableE
85
86
6
    void unserialize(BufferReadable& buf) {
87
6
        size_t size;
88
6
        buf.read_binary(size);
89
6
        _nums.resize(size);
90
6
        auto buff = buf.read(sizeof(Ty) * size);
91
6
        memcpy(_nums.data(), buff.data, buff.size);
92
6
    }
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
86
6
    void unserialize(BufferReadable& buf) {
87
6
        size_t size;
88
6
        buf.read_binary(size);
89
6
        _nums.resize(size);
90
6
        auto buff = buf.read(sizeof(Ty) * size);
91
6
        memcpy(_nums.data(), buff.data, buff.size);
92
6
    }
Unexecuted instantiation: _ZN5doris6CountsIaE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIsE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIiE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIdE11unserializeERNS_14BufferReadableE
93
94
21
    double terminate(double quantile) {
95
21
        if (!_sorted_nums_vec.empty()) {
96
9
            _move_nums_to_sorted_vec();
97
9
        }
98
21
        if (_sorted_nums_vec.size() <= 1) {
99
13
            if (_sorted_nums_vec.size() == 1) {
100
1
                _nums = std::move(_sorted_nums_vec[0]);
101
1
                _sorted_nums_vec.clear();
102
1
            }
103
104
13
            if (_nums.empty()) {
105
                // Although set null here, but the value is 0.0 and the call method just
106
                // get val in aggregate_function_percentile_approx.h
107
1
                return 0.0;
108
1
            }
109
110
12
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
111
1
                pdqsort(_nums.begin(), _nums.end());
112
1
            }
113
114
12
            if (quantile == 1 || _nums.size() == 1) {
115
5
                return _nums.back();
116
5
            }
117
118
7
            double u = (_nums.size() - 1) * quantile;
119
7
            auto index = static_cast<uint32_t>(u);
120
7
            return _nums[index] +
121
7
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
122
7
                                                       static_cast<double>(_nums[index]));
123
12
        } else {
124
8
            DCHECK(_nums.empty());
125
8
            size_t rows = 0;
126
16
            for (const auto& i : _sorted_nums_vec) {
127
16
                rows += i.size();
128
16
            }
129
8
            const bool reverse = quantile > 0.5 && rows > 2;
130
8
            double u = (rows - 1) * quantile;
131
8
            auto index = static_cast<uint32_t>(u);
132
            // if reverse, the step of target should start 0 like not reverse
133
            // so here rows need to minus index + 2
134
            // eg: rows = 10, index = 5
135
            // if not reverse, so the first number loc is 5, the second number loc is 6
136
            // if reverse, so the second number is 3, the first number is 4
137
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
138
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
139
8
            size_t target = reverse ? rows - index - 2 : index;
140
8
            if (quantile == 1) {
141
1
                target = 0;
142
1
            }
143
8
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
144
8
            if (quantile == 1) {
145
1
                return second_number;
146
1
            }
147
7
            return first_number +
148
7
                   (u - static_cast<double>(index)) *
149
7
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
150
8
        }
151
21
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
94
21
    double terminate(double quantile) {
95
21
        if (!_sorted_nums_vec.empty()) {
96
9
            _move_nums_to_sorted_vec();
97
9
        }
98
21
        if (_sorted_nums_vec.size() <= 1) {
99
13
            if (_sorted_nums_vec.size() == 1) {
100
1
                _nums = std::move(_sorted_nums_vec[0]);
101
1
                _sorted_nums_vec.clear();
102
1
            }
103
104
13
            if (_nums.empty()) {
105
                // Although set null here, but the value is 0.0 and the call method just
106
                // get val in aggregate_function_percentile_approx.h
107
1
                return 0.0;
108
1
            }
109
110
12
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
111
1
                pdqsort(_nums.begin(), _nums.end());
112
1
            }
113
114
12
            if (quantile == 1 || _nums.size() == 1) {
115
5
                return _nums.back();
116
5
            }
117
118
7
            double u = (_nums.size() - 1) * quantile;
119
7
            auto index = static_cast<uint32_t>(u);
120
7
            return _nums[index] +
121
7
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
122
7
                                                       static_cast<double>(_nums[index]));
123
12
        } else {
124
8
            DCHECK(_nums.empty());
125
8
            size_t rows = 0;
126
16
            for (const auto& i : _sorted_nums_vec) {
127
16
                rows += i.size();
128
16
            }
129
8
            const bool reverse = quantile > 0.5 && rows > 2;
130
8
            double u = (rows - 1) * quantile;
131
8
            auto index = static_cast<uint32_t>(u);
132
            // if reverse, the step of target should start 0 like not reverse
133
            // so here rows need to minus index + 2
134
            // eg: rows = 10, index = 5
135
            // if not reverse, so the first number loc is 5, the second number loc is 6
136
            // if reverse, so the second number is 3, the first number is 4
137
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
138
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
139
8
            size_t target = reverse ? rows - index - 2 : index;
140
8
            if (quantile == 1) {
141
1
                target = 0;
142
1
            }
143
8
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
144
8
            if (quantile == 1) {
145
1
                return second_number;
146
1
            }
147
7
            return first_number +
148
7
                   (u - static_cast<double>(index)) *
149
7
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
150
8
        }
151
21
    }
Unexecuted instantiation: _ZN5doris6CountsIaE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIsE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIiE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsInE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIdE9terminateEd
152
153
private:
154
    struct Node {
155
        Ty value;
156
        int array_index;
157
        int64_t element_index;
158
159
29
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
159
29
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIsE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIiE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_
160
    };
161
162
41
    void _move_nums_to_sorted_vec() {
163
41
        if (_nums.empty()) {
164
28
            return;
165
28
        }
166
13
        if (!std::is_sorted(_nums.begin(), _nums.end())) {
167
6
            pdqsort(_nums.begin(), _nums.end());
168
6
        }
169
13
        _sorted_nums_vec.emplace_back(std::move(_nums));
170
13
        DCHECK(_nums.empty());
171
13
    }
_ZN5doris6CountsIlE24_move_nums_to_sorted_vecEv
Line
Count
Source
162
41
    void _move_nums_to_sorted_vec() {
163
41
        if (_nums.empty()) {
164
28
            return;
165
28
        }
166
13
        if (!std::is_sorted(_nums.begin(), _nums.end())) {
167
6
            pdqsort(_nums.begin(), _nums.end());
168
6
        }
169
13
        _sorted_nums_vec.emplace_back(std::move(_nums));
170
        DCHECK(_nums.empty());
171
13
    }
Unexecuted instantiation: _ZN5doris6CountsIaE24_move_nums_to_sorted_vecEv
Unexecuted instantiation: _ZN5doris6CountsIsE24_move_nums_to_sorted_vecEv
Unexecuted instantiation: _ZN5doris6CountsIiE24_move_nums_to_sorted_vecEv
Unexecuted instantiation: _ZN5doris6CountsInE24_move_nums_to_sorted_vecEv
Unexecuted instantiation: _ZN5doris6CountsIfE24_move_nums_to_sorted_vecEv
Unexecuted instantiation: _ZN5doris6CountsIdE24_move_nums_to_sorted_vecEv
172
173
2
    void _convert_sorted_num_vec_to_nums() {
174
2
        size_t rows = 0;
175
4
        for (const auto& i : _sorted_nums_vec) {
176
4
            rows += i.size();
177
4
        }
178
2
        _nums.resize(rows);
179
2
        size_t count = 0;
180
181
2
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
182
6
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
183
4
            if (!_sorted_nums_vec[i].empty()) {
184
4
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
185
4
            }
186
4
        }
187
188
10
        while (!min_heap.empty()) {
189
8
            Node node = min_heap.top();
190
8
            min_heap.pop();
191
8
            _nums[count++] = node.value;
192
8
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
193
4
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
4
                min_heap.push(node);
195
4
            }
196
8
        }
197
2
        _sorted_nums_vec.clear();
198
2
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
173
2
    void _convert_sorted_num_vec_to_nums() {
174
2
        size_t rows = 0;
175
4
        for (const auto& i : _sorted_nums_vec) {
176
4
            rows += i.size();
177
4
        }
178
2
        _nums.resize(rows);
179
2
        size_t count = 0;
180
181
2
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
182
6
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
183
4
            if (!_sorted_nums_vec[i].empty()) {
184
4
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
185
4
            }
186
4
        }
187
188
10
        while (!min_heap.empty()) {
189
8
            Node node = min_heap.top();
190
8
            min_heap.pop();
191
8
            _nums[count++] = node.value;
192
8
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
193
4
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
4
                min_heap.push(node);
195
4
            }
196
8
        }
197
2
        _sorted_nums_vec.clear();
198
2
    }
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
199
200
8
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
201
8
        Ty first_number = 0, second_number = 0;
202
8
        size_t count = 0;
203
8
        if (reverse) {
204
4
            std::priority_queue<Node> max_heap;
205
12
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
206
8
                if (!_sorted_nums_vec[i].empty()) {
207
8
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
208
8
                                     _sorted_nums_vec[i].size() - 1);
209
8
                }
210
8
            }
211
212
8
            while (!max_heap.empty()) {
213
8
                Node node = max_heap.top();
214
8
                max_heap.pop();
215
8
                if (count == target) {
216
4
                    second_number = node.value;
217
4
                } else if (count == target + 1) {
218
4
                    first_number = node.value;
219
4
                    break;
220
4
                }
221
4
                ++count;
222
4
                if (--node.element_index >= 0) {
223
4
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
224
4
                    max_heap.push(node);
225
4
                }
226
4
            }
227
228
4
        } else {
229
4
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
230
12
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
231
8
                if (!_sorted_nums_vec[i].empty()) {
232
8
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
233
8
                }
234
8
            }
235
236
16
            while (!min_heap.empty()) {
237
16
                Node node = min_heap.top();
238
16
                min_heap.pop();
239
16
                if (count == target) {
240
4
                    first_number = node.value;
241
12
                } else if (count == target + 1) {
242
4
                    second_number = node.value;
243
4
                    break;
244
4
                }
245
12
                ++count;
246
12
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
247
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
248
12
                    min_heap.push(node);
249
12
                }
250
12
            }
251
4
        }
252
253
8
        return {first_number, second_number};
254
8
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
200
8
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
201
8
        Ty first_number = 0, second_number = 0;
202
8
        size_t count = 0;
203
8
        if (reverse) {
204
4
            std::priority_queue<Node> max_heap;
205
12
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
206
8
                if (!_sorted_nums_vec[i].empty()) {
207
8
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
208
8
                                     _sorted_nums_vec[i].size() - 1);
209
8
                }
210
8
            }
211
212
8
            while (!max_heap.empty()) {
213
8
                Node node = max_heap.top();
214
8
                max_heap.pop();
215
8
                if (count == target) {
216
4
                    second_number = node.value;
217
4
                } else if (count == target + 1) {
218
4
                    first_number = node.value;
219
4
                    break;
220
4
                }
221
4
                ++count;
222
4
                if (--node.element_index >= 0) {
223
4
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
224
4
                    max_heap.push(node);
225
4
                }
226
4
            }
227
228
4
        } else {
229
4
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
230
12
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
231
8
                if (!_sorted_nums_vec[i].empty()) {
232
8
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
233
8
                }
234
8
            }
235
236
16
            while (!min_heap.empty()) {
237
16
                Node node = min_heap.top();
238
16
                min_heap.pop();
239
16
                if (count == target) {
240
4
                    first_number = node.value;
241
12
                } else if (count == target + 1) {
242
4
                    second_number = node.value;
243
4
                    break;
244
4
                }
245
12
                ++count;
246
12
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
247
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
248
12
                    min_heap.push(node);
249
12
                }
250
12
            }
251
4
        }
252
253
8
        return {first_number, second_number};
254
8
    }
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
255
256
    PODArray<Ty> _nums;
257
    std::vector<PODArray<Ty>> _sorted_nums_vec;
258
};
259
260
class PercentileLevels {
261
public:
262
3
    void merge(const PercentileLevels& rhs) {
263
3
        if (rhs.empty()) {
264
1
            return;
265
1
        }
266
267
2
        if (empty()) {
268
1
            quantiles = rhs.quantiles;
269
1
            permutation = rhs.permutation;
270
1
            return;
271
1
        }
272
273
2
        DCHECK_EQ(quantiles.size(), rhs.quantiles.size());
274
5
        for (size_t i = 0; i < quantiles.size(); ++i) {
275
4
            DCHECK_EQ(quantiles[i], rhs.quantiles[i]);
276
4
        }
277
1
    }
278
279
2
    void write(BufferWritable& buf) const {
280
2
        int size_num = cast_set<int>(quantiles.size());
281
2
        buf.write_binary(size_num);
282
4
        for (const auto& quantile : quantiles) {
283
4
            buf.write_binary(quantile);
284
4
        }
285
2
    }
286
287
3
    void read(BufferReadable& buf) {
288
3
        int size_num = 0;
289
3
        buf.read_binary(size_num);
290
291
3
        quantiles.resize(size_num);
292
3
        permutation.resize(size_num);
293
11
        for (int i = 0; i < size_num; ++i) {
294
8
            buf.read_binary(quantiles[i]);
295
8
            permutation[i] = cast_set<size_t>(i);
296
8
        }
297
3
    }
298
299
1
    void clear() {
300
1
        quantiles.clear();
301
1
        permutation.clear();
302
1
    }
303
304
8
    bool empty() const { return quantiles.empty(); }
305
306
6
    const std::vector<size_t>& get_permutation() const {
307
6
        sort_permutation();
308
6
        return permutation;
309
6
    }
310
311
6
    void sort_permutation() const {
312
6
        pdqsort(permutation.begin(), permutation.end(),
313
36
                [this](size_t lhs, size_t rhs) { return quantiles[lhs] < quantiles[rhs]; });
314
6
    }
315
316
    std::vector<double> quantiles;
317
    mutable std::vector<size_t> permutation;
318
};
319
320
} // namespace doris