Coverage Report

Created: 2026-05-17 11:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
1.93k
inline void check_quantile(double quantile) {
38
1.93k
    if (quantile < 0 || quantile > 1) {
39
23
        throw Exception(ErrorCode::INVALID_ARGUMENT,
40
23
                        "quantile in func percentile should in [0, 1], but real data is:" +
41
23
                                std::to_string(quantile));
42
23
    }
43
1.93k
}
44
45
template <typename Ty>
46
class Counts {
47
public:
48
1.30k
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIaEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIsEC2Ev
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
48
795
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
48
510
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsInEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIdEC2Ev
49
50
575
    void merge(Counts* other) {
51
575
        if (other != nullptr && !other->_nums.empty()) {
52
575
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
53
575
        }
54
575
    }
Unexecuted instantiation: _ZN5doris6CountsIaE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIsE5mergeEPS1_
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
50
339
    void merge(Counts* other) {
51
339
        if (other != nullptr && !other->_nums.empty()) {
52
339
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
53
339
        }
54
339
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
50
236
    void merge(Counts* other) {
51
236
        if (other != nullptr && !other->_nums.empty()) {
52
236
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
53
236
        }
54
236
    }
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIdE5mergeEPS1_
55
56
    void increment(Ty key, uint32_t i) {
57
        auto old_size = _nums.size();
58
        _nums.resize(_nums.size() + i);
59
        for (uint32_t j = 0; j < i; ++j) {
60
            _nums[old_size + j] = key;
61
        }
62
    }
63
64
427
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIaE9incrementEa
Unexecuted instantiation: _ZN5doris6CountsIsE9incrementEs
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
64
300
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
64
127
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsInE9incrementEn
Unexecuted instantiation: _ZN5doris6CountsIfE9incrementEf
Unexecuted instantiation: _ZN5doris6CountsIdE9incrementEd
65
66
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
67
68
978
    void serialize(BufferWritable& buf) {
69
978
        if (!_nums.empty()) {
70
702
            pdqsort(_nums.begin(), _nums.end());
71
702
            size_t size = _nums.size();
72
702
            buf.write_binary(size);
73
702
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
74
702
        } else {
75
            // convert _sorted_nums_vec to _nums and do seiralize again
76
276
            _convert_sorted_num_vec_to_nums();
77
276
            serialize(buf);
78
276
        }
79
978
    }
Unexecuted instantiation: _ZN5doris6CountsIaE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIsE9serializeERNS_14BufferWritableE
_ZN5doris6CountsIiE9serializeERNS_14BufferWritableE
Line
Count
Source
68
612
    void serialize(BufferWritable& buf) {
69
612
        if (!_nums.empty()) {
70
456
            pdqsort(_nums.begin(), _nums.end());
71
456
            size_t size = _nums.size();
72
456
            buf.write_binary(size);
73
456
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
74
456
        } else {
75
            // convert _sorted_nums_vec to _nums and do seiralize again
76
156
            _convert_sorted_num_vec_to_nums();
77
156
            serialize(buf);
78
156
        }
79
612
    }
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
68
366
    void serialize(BufferWritable& buf) {
69
366
        if (!_nums.empty()) {
70
246
            pdqsort(_nums.begin(), _nums.end());
71
246
            size_t size = _nums.size();
72
246
            buf.write_binary(size);
73
246
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
74
246
        } else {
75
            // convert _sorted_nums_vec to _nums and do seiralize again
76
120
            _convert_sorted_num_vec_to_nums();
77
120
            serialize(buf);
78
120
        }
79
366
    }
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIdE9serializeERNS_14BufferWritableE
80
81
576
    void unserialize(BufferReadable& buf) {
82
576
        size_t size;
83
576
        buf.read_binary(size);
84
576
        _nums.resize(size);
85
576
        auto buff = buf.read(sizeof(Ty) * size);
86
576
        memcpy(_nums.data(), buff.data, buff.size);
87
576
    }
Unexecuted instantiation: _ZN5doris6CountsIaE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIsE11unserializeERNS_14BufferReadableE
_ZN5doris6CountsIiE11unserializeERNS_14BufferReadableE
Line
Count
Source
81
339
    void unserialize(BufferReadable& buf) {
82
339
        size_t size;
83
339
        buf.read_binary(size);
84
339
        _nums.resize(size);
85
339
        auto buff = buf.read(sizeof(Ty) * size);
86
339
        memcpy(_nums.data(), buff.data, buff.size);
87
339
    }
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
81
237
    void unserialize(BufferReadable& buf) {
82
237
        size_t size;
83
237
        buf.read_binary(size);
84
237
        _nums.resize(size);
85
237
        auto buff = buf.read(sizeof(Ty) * size);
86
237
        memcpy(_nums.data(), buff.data, buff.size);
87
237
    }
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIdE11unserializeERNS_14BufferReadableE
88
89
34
    double terminate(double quantile) {
90
34
        if (_sorted_nums_vec.size() <= 1) {
91
33
            if (_sorted_nums_vec.size() == 1) {
92
24
                _nums = std::move(_sorted_nums_vec[0]);
93
24
            }
94
95
33
            if (_nums.empty()) {
96
                // Although set null here, but the value is 0.0 and the call method just
97
                // get val in aggregate_function_percentile_approx.h
98
1
                return 0.0;
99
1
            }
100
101
32
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
102
1
                pdqsort(_nums.begin(), _nums.end());
103
1
            }
104
105
32
            if (quantile == 1 || _nums.size() == 1) {
106
4
                return _nums.back();
107
4
            }
108
109
28
            double u = (_nums.size() - 1) * quantile;
110
28
            auto index = static_cast<uint32_t>(u);
111
28
            return _nums[index] +
112
28
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
113
28
                                                       static_cast<double>(_nums[index]));
114
32
        } else {
115
1
            DCHECK(_nums.empty());
116
1
            size_t rows = 0;
117
2
            for (const auto& i : _sorted_nums_vec) {
118
2
                rows += i.size();
119
2
            }
120
1
            const bool reverse = quantile > 0.5 && rows > 2;
121
1
            double u = (rows - 1) * quantile;
122
1
            auto index = static_cast<uint32_t>(u);
123
            // if reverse, the step of target should start 0 like not reverse
124
            // so here rows need to minus index + 2
125
            // eg: rows = 10, index = 5
126
            // if not reverse, so the first number loc is 5, the second number loc is 6
127
            // if reverse, so the second number is 3, the first number is 4
128
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
129
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
130
1
            size_t target = reverse ? rows - index - 2 : index;
131
1
            if (quantile == 1) {
132
0
                target = 0;
133
0
            }
134
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
135
1
            if (quantile == 1) {
136
0
                return second_number;
137
0
            }
138
1
            return first_number +
139
1
                   (u - static_cast<double>(index)) *
140
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
141
1
        }
142
34
    }
Unexecuted instantiation: _ZN5doris6CountsIaE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIsE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIiE9terminateEd
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
89
34
    double terminate(double quantile) {
90
34
        if (_sorted_nums_vec.size() <= 1) {
91
33
            if (_sorted_nums_vec.size() == 1) {
92
24
                _nums = std::move(_sorted_nums_vec[0]);
93
24
            }
94
95
33
            if (_nums.empty()) {
96
                // Although set null here, but the value is 0.0 and the call method just
97
                // get val in aggregate_function_percentile_approx.h
98
1
                return 0.0;
99
1
            }
100
101
32
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
102
1
                pdqsort(_nums.begin(), _nums.end());
103
1
            }
104
105
32
            if (quantile == 1 || _nums.size() == 1) {
106
4
                return _nums.back();
107
4
            }
108
109
28
            double u = (_nums.size() - 1) * quantile;
110
28
            auto index = static_cast<uint32_t>(u);
111
28
            return _nums[index] +
112
28
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
113
28
                                                       static_cast<double>(_nums[index]));
114
32
        } else {
115
1
            DCHECK(_nums.empty());
116
1
            size_t rows = 0;
117
2
            for (const auto& i : _sorted_nums_vec) {
118
2
                rows += i.size();
119
2
            }
120
1
            const bool reverse = quantile > 0.5 && rows > 2;
121
1
            double u = (rows - 1) * quantile;
122
1
            auto index = static_cast<uint32_t>(u);
123
            // if reverse, the step of target should start 0 like not reverse
124
            // so here rows need to minus index + 2
125
            // eg: rows = 10, index = 5
126
            // if not reverse, so the first number loc is 5, the second number loc is 6
127
            // if reverse, so the second number is 3, the first number is 4
128
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
129
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
130
1
            size_t target = reverse ? rows - index - 2 : index;
131
1
            if (quantile == 1) {
132
0
                target = 0;
133
0
            }
134
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
135
1
            if (quantile == 1) {
136
0
                return second_number;
137
0
            }
138
1
            return first_number +
139
1
                   (u - static_cast<double>(index)) *
140
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
141
1
        }
142
34
    }
Unexecuted instantiation: _ZN5doris6CountsInE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIdE9terminateEd
143
144
private:
145
    struct Node {
146
        Ty value;
147
        int array_index;
148
        int64_t element_index;
149
150
465
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIsE4NodessERKS2_
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
150
291
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
150
174
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_
151
    };
152
153
276
    void _convert_sorted_num_vec_to_nums() {
154
276
        size_t rows = 0;
155
549
        for (const auto& i : _sorted_nums_vec) {
156
549
            rows += i.size();
157
549
        }
158
276
        _nums.resize(rows);
159
276
        size_t count = 0;
160
161
276
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
162
825
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
163
549
            if (!_sorted_nums_vec[i].empty()) {
164
549
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
165
549
            }
166
549
        }
167
168
969
        while (!min_heap.empty()) {
169
693
            Node node = min_heap.top();
170
693
            min_heap.pop();
171
693
            _nums[count++] = node.value;
172
693
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
173
144
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
174
144
                min_heap.push(node);
175
144
            }
176
693
        }
177
276
        _sorted_nums_vec.clear();
178
276
    }
Unexecuted instantiation: _ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv
_ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
153
156
    void _convert_sorted_num_vec_to_nums() {
154
156
        size_t rows = 0;
155
339
        for (const auto& i : _sorted_nums_vec) {
156
339
            rows += i.size();
157
339
        }
158
156
        _nums.resize(rows);
159
156
        size_t count = 0;
160
161
156
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
162
495
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
163
339
            if (!_sorted_nums_vec[i].empty()) {
164
339
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
165
339
            }
166
339
        }
167
168
531
        while (!min_heap.empty()) {
169
375
            Node node = min_heap.top();
170
375
            min_heap.pop();
171
375
            _nums[count++] = node.value;
172
375
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
173
36
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
174
36
                min_heap.push(node);
175
36
            }
176
375
        }
177
156
        _sorted_nums_vec.clear();
178
156
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
153
120
    void _convert_sorted_num_vec_to_nums() {
154
120
        size_t rows = 0;
155
210
        for (const auto& i : _sorted_nums_vec) {
156
210
            rows += i.size();
157
210
        }
158
120
        _nums.resize(rows);
159
120
        size_t count = 0;
160
161
120
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
162
330
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
163
210
            if (!_sorted_nums_vec[i].empty()) {
164
210
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
165
210
            }
166
210
        }
167
168
438
        while (!min_heap.empty()) {
169
318
            Node node = min_heap.top();
170
318
            min_heap.pop();
171
318
            _nums[count++] = node.value;
172
318
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
173
108
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
174
108
                min_heap.push(node);
175
108
            }
176
318
        }
177
120
        _sorted_nums_vec.clear();
178
120
    }
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
179
180
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
181
1
        Ty first_number = 0, second_number = 0;
182
1
        size_t count = 0;
183
1
        if (reverse) {
184
0
            std::priority_queue<Node> max_heap;
185
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
186
0
                if (!_sorted_nums_vec[i].empty()) {
187
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
188
0
                                     _sorted_nums_vec[i].size() - 1);
189
0
                }
190
0
            }
191
192
0
            while (!max_heap.empty()) {
193
0
                Node node = max_heap.top();
194
0
                max_heap.pop();
195
0
                if (count == target) {
196
0
                    second_number = node.value;
197
0
                } else if (count == target + 1) {
198
0
                    first_number = node.value;
199
0
                    break;
200
0
                }
201
0
                ++count;
202
0
                if (--node.element_index >= 0) {
203
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
204
0
                    max_heap.push(node);
205
0
                }
206
0
            }
207
208
1
        } else {
209
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
210
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
211
2
                if (!_sorted_nums_vec[i].empty()) {
212
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
213
2
                }
214
2
            }
215
216
7
            while (!min_heap.empty()) {
217
7
                Node node = min_heap.top();
218
7
                min_heap.pop();
219
7
                if (count == target) {
220
1
                    first_number = node.value;
221
6
                } else if (count == target + 1) {
222
1
                    second_number = node.value;
223
1
                    break;
224
1
                }
225
6
                ++count;
226
6
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
227
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
228
6
                    min_heap.push(node);
229
6
                }
230
6
            }
231
1
        }
232
233
1
        return {first_number, second_number};
234
1
    }
Unexecuted instantiation: _ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
180
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
181
1
        Ty first_number = 0, second_number = 0;
182
1
        size_t count = 0;
183
1
        if (reverse) {
184
0
            std::priority_queue<Node> max_heap;
185
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
186
0
                if (!_sorted_nums_vec[i].empty()) {
187
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
188
0
                                     _sorted_nums_vec[i].size() - 1);
189
0
                }
190
0
            }
191
192
0
            while (!max_heap.empty()) {
193
0
                Node node = max_heap.top();
194
0
                max_heap.pop();
195
0
                if (count == target) {
196
0
                    second_number = node.value;
197
0
                } else if (count == target + 1) {
198
0
                    first_number = node.value;
199
0
                    break;
200
0
                }
201
0
                ++count;
202
0
                if (--node.element_index >= 0) {
203
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
204
0
                    max_heap.push(node);
205
0
                }
206
0
            }
207
208
1
        } else {
209
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
210
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
211
2
                if (!_sorted_nums_vec[i].empty()) {
212
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
213
2
                }
214
2
            }
215
216
7
            while (!min_heap.empty()) {
217
7
                Node node = min_heap.top();
218
7
                min_heap.pop();
219
7
                if (count == target) {
220
1
                    first_number = node.value;
221
6
                } else if (count == target + 1) {
222
1
                    second_number = node.value;
223
1
                    break;
224
1
                }
225
6
                ++count;
226
6
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
227
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
228
6
                    min_heap.push(node);
229
6
                }
230
6
            }
231
1
        }
232
233
1
        return {first_number, second_number};
234
1
    }
Unexecuted instantiation: _ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
235
236
    PODArray<Ty> _nums;
237
    std::vector<PODArray<Ty>> _sorted_nums_vec;
238
};
239
240
class PercentileLevels {
241
public:
242
143
    void merge(const PercentileLevels& rhs) {
243
143
        if (rhs.empty()) {
244
1
            return;
245
1
        }
246
247
142
        if (empty()) {
248
1
            quantiles = rhs.quantiles;
249
1
            permutation = rhs.permutation;
250
1
            return;
251
1
        }
252
253
142
        DCHECK_EQ(quantiles.size(), rhs.quantiles.size());
254
350
        for (size_t i = 0; i < quantiles.size(); ++i) {
255
209
            DCHECK_EQ(quantiles[i], rhs.quantiles[i]);
256
209
        }
257
141
    }
258
259
388
    void write(BufferWritable& buf) const {
260
388
        int size_num = cast_set<int>(quantiles.size());
261
388
        buf.write_binary(size_num);
262
652
        for (const auto& quantile : quantiles) {
263
652
            buf.write_binary(quantile);
264
652
        }
265
388
    }
266
267
389
    void read(BufferReadable& buf) {
268
389
        int size_num = 0;
269
389
        buf.read_binary(size_num);
270
271
389
        quantiles.resize(size_num);
272
389
        permutation.resize(size_num);
273
1.04k
        for (int i = 0; i < size_num; ++i) {
274
656
            buf.read_binary(quantiles[i]);
275
656
            permutation[i] = cast_set<size_t>(i);
276
656
        }
277
389
    }
278
279
505
    void clear() {
280
505
        quantiles.clear();
281
505
        permutation.clear();
282
505
    }
283
284
2.20k
    bool empty() const { return quantiles.empty(); }
285
286
129
    const std::vector<size_t>& get_permutation() const {
287
129
        sort_permutation();
288
129
        return permutation;
289
129
    }
290
291
129
    void sort_permutation() const {
292
129
        pdqsort(permutation.begin(), permutation.end(),
293
209
                [this](size_t lhs, size_t rhs) { return quantiles[lhs] < quantiles[rhs]; });
294
129
    }
295
296
    std::vector<double> quantiles;
297
    mutable std::vector<size_t> permutation;
298
};
299
300
} // namespace doris