Coverage Report

Created: 2026-09-24 17:53

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
2.21k
inline void check_quantile(double quantile) {
38
2.21k
    if (!std::isfinite(quantile) || quantile < 0 || quantile > 1) {
39
5
        throw Exception(ErrorCode::INVALID_ARGUMENT,
40
5
                        "quantile in func percentile should in [0, 1], but real data is:" +
41
5
                                std::to_string(quantile));
42
5
    }
43
2.21k
}
44
45
template <typename Ty>
46
class Counts {
47
public:
48
275
    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
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
48
245
    Counts() = default;
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
71
    void merge(Counts* other) {
53
71
        _move_nums_to_sorted_vec();
54
71
        other->_move_nums_to_sorted_vec();
55
71
        for (auto& nums : other->_sorted_nums_vec) {
56
70
            _sorted_nums_vec.emplace_back(std::move(nums));
57
70
        }
58
71
        other->_sorted_nums_vec.clear();
59
71
    }
_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_
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
52
56
    void merge(Counts* other) {
53
56
        _move_nums_to_sorted_vec();
54
56
        other->_move_nums_to_sorted_vec();
55
56
        for (auto& nums : other->_sorted_nums_vec) {
56
56
            _sorted_nums_vec.emplace_back(std::move(nums));
57
56
        }
58
56
        other->_sorted_nums_vec.clear();
59
56
    }
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
182
    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
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
69
160
    void increment(Ty key) { _nums.push_back(key); }
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
43
    void serialize(BufferWritable& buf) {
74
43
        if (_sorted_nums_vec.empty()) {
75
41
            pdqsort(_nums.begin(), _nums.end());
76
41
        } 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
43
        size_t size = _nums.size();
82
43
        buf.write_binary(size);
83
43
        buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
84
43
    }
_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
_ZN5doris6CountsIdE9serializeERNS_14BufferWritableE
Line
Count
Source
73
37
    void serialize(BufferWritable& buf) {
74
37
        if (_sorted_nums_vec.empty()) {
75
37
            pdqsort(_nums.begin(), _nums.end());
76
37
        } else {
77
            // merge all sorted runs (including the raw samples) into `_nums`
78
0
            _move_nums_to_sorted_vec();
79
0
            _convert_sorted_num_vec_to_nums();
80
0
        }
81
37
        size_t size = _nums.size();
82
37
        buf.write_binary(size);
83
37
        buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
84
37
    }
85
86
57
    void unserialize(BufferReadable& buf) {
87
57
        size_t size;
88
57
        buf.read_binary(size);
89
57
        _nums.resize(size);
90
57
        auto buff = buf.read(sizeof(Ty) * size);
91
57
        memcpy(_nums.data(), buff.data, buff.size);
92
57
    }
_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
_ZN5doris6CountsIdE11unserializeERNS_14BufferReadableE
Line
Count
Source
86
51
    void unserialize(BufferReadable& buf) {
87
51
        size_t size;
88
51
        buf.read_binary(size);
89
51
        _nums.resize(size);
90
51
        auto buff = buf.read(sizeof(Ty) * size);
91
51
        memcpy(_nums.data(), buff.data, buff.size);
92
51
    }
93
94
131
    double terminate(double quantile) {
95
131
        if (!_sorted_nums_vec.empty()) {
96
42
            _move_nums_to_sorted_vec();
97
42
        }
98
131
        if (_sorted_nums_vec.size() <= 1) {
99
112
            if (_sorted_nums_vec.size() == 1) {
100
23
                _nums = std::move(_sorted_nums_vec[0]);
101
23
                _sorted_nums_vec.clear();
102
23
            }
103
104
112
            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
111
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
111
1
                pdqsort(_nums.begin(), _nums.end());
112
1
            }
113
114
111
            if (quantile == 1 || _nums.size() == 1) {
115
94
                return _nums.back();
116
94
            }
117
118
17
            double u = (_nums.size() - 1) * quantile;
119
17
            auto index = static_cast<uint32_t>(u);
120
17
            return _nums[index] +
121
17
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
122
17
                                                       static_cast<double>(_nums[index]));
123
111
        } else {
124
19
            DCHECK(_nums.empty());
125
19
            size_t rows = 0;
126
38
            for (const auto& i : _sorted_nums_vec) {
127
38
                rows += i.size();
128
38
            }
129
19
            const bool reverse = quantile > 0.5 && rows > 2;
130
19
            double u = (rows - 1) * quantile;
131
19
            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
19
            size_t target = reverse ? rows - index - 2 : index;
140
19
            if (quantile == 1) {
141
2
                target = 0;
142
2
            }
143
19
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
144
19
            if (quantile == 1) {
145
2
                return second_number;
146
2
            }
147
17
            return first_number +
148
17
                   (u - static_cast<double>(index)) *
149
17
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
150
19
        }
151
131
    }
_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
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
94
110
    double terminate(double quantile) {
95
110
        if (!_sorted_nums_vec.empty()) {
96
33
            _move_nums_to_sorted_vec();
97
33
        }
98
110
        if (_sorted_nums_vec.size() <= 1) {
99
99
            if (_sorted_nums_vec.size() == 1) {
100
22
                _nums = std::move(_sorted_nums_vec[0]);
101
22
                _sorted_nums_vec.clear();
102
22
            }
103
104
99
            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
0
                return 0.0;
108
0
            }
109
110
99
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
111
0
                pdqsort(_nums.begin(), _nums.end());
112
0
            }
113
114
99
            if (quantile == 1 || _nums.size() == 1) {
115
89
                return _nums.back();
116
89
            }
117
118
10
            double u = (_nums.size() - 1) * quantile;
119
10
            auto index = static_cast<uint32_t>(u);
120
10
            return _nums[index] +
121
10
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
122
10
                                                       static_cast<double>(_nums[index]));
123
99
        } else {
124
11
            DCHECK(_nums.empty());
125
11
            size_t rows = 0;
126
22
            for (const auto& i : _sorted_nums_vec) {
127
22
                rows += i.size();
128
22
            }
129
11
            const bool reverse = quantile > 0.5 && rows > 2;
130
11
            double u = (rows - 1) * quantile;
131
11
            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
11
            size_t target = reverse ? rows - index - 2 : index;
140
11
            if (quantile == 1) {
141
1
                target = 0;
142
1
            }
143
11
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
144
11
            if (quantile == 1) {
145
1
                return second_number;
146
1
            }
147
10
            return first_number +
148
10
                   (u - static_cast<double>(index)) *
149
10
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
150
11
        }
151
110
    }
152
153
private:
154
    struct Node {
155
        Ty value;
156
        int array_index;
157
        int64_t element_index;
158
159
40
        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_
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
159
11
        auto operator<=>(const Node& other) const { return value <=> other.value; }
160
    };
161
162
186
    void _move_nums_to_sorted_vec() {
163
186
        if (_nums.empty()) {
164
115
            return;
165
115
        }
166
71
        if (!std::is_sorted(_nums.begin(), _nums.end())) {
167
6
            pdqsort(_nums.begin(), _nums.end());
168
6
        }
169
71
        _sorted_nums_vec.emplace_back(std::move(_nums));
170
71
        DCHECK(_nums.empty());
171
71
    }
_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
_ZN5doris6CountsIdE24_move_nums_to_sorted_vecEv
Line
Count
Source
162
145
    void _move_nums_to_sorted_vec() {
163
145
        if (_nums.empty()) {
164
87
            return;
165
87
        }
166
58
        if (!std::is_sorted(_nums.begin(), _nums.end())) {
167
0
            pdqsort(_nums.begin(), _nums.end());
168
0
        }
169
58
        _sorted_nums_vec.emplace_back(std::move(_nums));
170
        DCHECK(_nums.empty());
171
58
    }
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
19
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
201
19
        Ty first_number = 0, second_number = 0;
202
19
        size_t count = 0;
203
19
        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
15
        } else {
229
15
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
230
45
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
231
30
                if (!_sorted_nums_vec[i].empty()) {
232
30
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
233
30
                }
234
30
            }
235
236
38
            while (!min_heap.empty()) {
237
38
                Node node = min_heap.top();
238
38
                min_heap.pop();
239
38
                if (count == target) {
240
15
                    first_number = node.value;
241
23
                } else if (count == target + 1) {
242
15
                    second_number = node.value;
243
15
                    break;
244
15
                }
245
23
                ++count;
246
23
                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
23
            }
251
15
        }
252
253
19
        return {first_number, second_number};
254
19
    }
_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
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
200
11
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
201
11
        Ty first_number = 0, second_number = 0;
202
11
        size_t count = 0;
203
11
        if (reverse) {
204
0
            std::priority_queue<Node> max_heap;
205
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
206
0
                if (!_sorted_nums_vec[i].empty()) {
207
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
208
0
                                     _sorted_nums_vec[i].size() - 1);
209
0
                }
210
0
            }
211
212
0
            while (!max_heap.empty()) {
213
0
                Node node = max_heap.top();
214
0
                max_heap.pop();
215
0
                if (count == target) {
216
0
                    second_number = node.value;
217
0
                } else if (count == target + 1) {
218
0
                    first_number = node.value;
219
0
                    break;
220
0
                }
221
0
                ++count;
222
0
                if (--node.element_index >= 0) {
223
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
224
0
                    max_heap.push(node);
225
0
                }
226
0
            }
227
228
11
        } else {
229
11
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
230
33
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
231
22
                if (!_sorted_nums_vec[i].empty()) {
232
22
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
233
22
                }
234
22
            }
235
236
22
            while (!min_heap.empty()) {
237
22
                Node node = min_heap.top();
238
22
                min_heap.pop();
239
22
                if (count == target) {
240
11
                    first_number = node.value;
241
11
                } else if (count == target + 1) {
242
11
                    second_number = node.value;
243
11
                    break;
244
11
                }
245
11
                ++count;
246
11
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
247
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
248
0
                    min_heap.push(node);
249
0
                }
250
11
            }
251
11
        }
252
253
11
        return {first_number, second_number};
254
11
    }
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
204
    void write(BufferWritable& buf) const {
280
204
        int size_num = cast_set<int>(quantiles.size());
281
204
        buf.write_binary(size_num);
282
362
        for (const auto& quantile : quantiles) {
283
362
            buf.write_binary(quantile);
284
362
        }
285
204
    }
286
287
229
    void read(BufferReadable& buf) {
288
229
        int size_num = 0;
289
229
        buf.read_binary(size_num);
290
291
229
        quantiles.resize(size_num);
292
229
        permutation.resize(size_num);
293
623
        for (int i = 0; i < size_num; ++i) {
294
394
            buf.read_binary(quantiles[i]);
295
394
            permutation[i] = cast_set<size_t>(i);
296
394
        }
297
229
    }
298
299
421
    void clear() {
300
421
        quantiles.clear();
301
421
        permutation.clear();
302
421
    }
303
304
4.26k
    bool empty() const { return quantiles.empty(); }
305
306
399
    const std::vector<size_t>& get_permutation() const {
307
399
        sort_permutation();
308
399
        return permutation;
309
399
    }
310
311
399
    void sort_permutation() const {
312
399
        pdqsort(permutation.begin(), permutation.end(),
313
520
                [this](size_t lhs, size_t rhs) { return quantiles[lhs] < quantiles[rhs]; });
314
399
    }
315
316
    std::vector<double> quantiles;
317
    mutable std::vector<size_t> permutation;
318
};
319
320
} // namespace doris