Coverage Report

Created: 2026-09-28 17:01

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
4.42k
inline void check_quantile(double quantile) {
38
4.42k
    if (!std::isfinite(quantile) || quantile < 0 || quantile > 1) {
39
10
        throw Exception(ErrorCode::INVALID_ARGUMENT,
40
10
                        "quantile in func percentile should in [0, 1], but real data is:" +
41
10
                                std::to_string(quantile));
42
10
    }
43
4.42k
}
44
45
template <typename Ty>
46
class Counts {
47
public:
48
550
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
48
60
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIaEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIsEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIiEC2Ev
Unexecuted instantiation: _ZN5doris6CountsInEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
48
490
    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
142
    void merge(Counts* other) {
53
142
        _move_nums_to_sorted_vec();
54
142
        other->_move_nums_to_sorted_vec();
55
142
        for (auto& nums : other->_sorted_nums_vec) {
56
140
            _sorted_nums_vec.emplace_back(std::move(nums));
57
140
        }
58
142
        other->_sorted_nums_vec.clear();
59
142
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
52
30
    void merge(Counts* other) {
53
30
        _move_nums_to_sorted_vec();
54
30
        other->_move_nums_to_sorted_vec();
55
30
        for (auto& nums : other->_sorted_nums_vec) {
56
28
            _sorted_nums_vec.emplace_back(std::move(nums));
57
28
        }
58
30
        other->_sorted_nums_vec.clear();
59
30
    }
Unexecuted instantiation: _ZN5doris6CountsIaE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIsE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIiE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
52
112
    void merge(Counts* other) {
53
112
        _move_nums_to_sorted_vec();
54
112
        other->_move_nums_to_sorted_vec();
55
112
        for (auto& nums : other->_sorted_nums_vec) {
56
112
            _sorted_nums_vec.emplace_back(std::move(nums));
57
112
        }
58
112
        other->_sorted_nums_vec.clear();
59
112
    }
60
61
24
    void increment(Ty key, uint32_t i) {
62
24
        auto old_size = _nums.size();
63
24
        _nums.resize(_nums.size() + i);
64
64
        for (uint32_t j = 0; j < i; ++j) {
65
40
            _nums[old_size + j] = key;
66
40
        }
67
24
    }
68
69
364
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
69
44
    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
320
    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
86
    void serialize(BufferWritable& buf) {
74
86
        if (_sorted_nums_vec.empty()) {
75
82
            pdqsort(_nums.begin(), _nums.end());
76
82
        } else {
77
            // merge all sorted runs (including the raw samples) into `_nums`
78
4
            _move_nums_to_sorted_vec();
79
4
            _convert_sorted_num_vec_to_nums();
80
4
        }
81
86
        size_t size = _nums.size();
82
86
        buf.write_binary(size);
83
86
        buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
84
86
    }
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
73
12
    void serialize(BufferWritable& buf) {
74
12
        if (_sorted_nums_vec.empty()) {
75
8
            pdqsort(_nums.begin(), _nums.end());
76
8
        } else {
77
            // merge all sorted runs (including the raw samples) into `_nums`
78
4
            _move_nums_to_sorted_vec();
79
4
            _convert_sorted_num_vec_to_nums();
80
4
        }
81
12
        size_t size = _nums.size();
82
12
        buf.write_binary(size);
83
12
        buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
84
12
    }
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
74
    void serialize(BufferWritable& buf) {
74
74
        if (_sorted_nums_vec.empty()) {
75
74
            pdqsort(_nums.begin(), _nums.end());
76
74
        } 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
74
        size_t size = _nums.size();
82
74
        buf.write_binary(size);
83
74
        buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
84
74
    }
85
86
114
    void unserialize(BufferReadable& buf) {
87
114
        size_t size;
88
114
        buf.read_binary(size);
89
114
        _nums.resize(size);
90
114
        auto buff = buf.read(sizeof(Ty) * size);
91
114
        memcpy(_nums.data(), buff.data, buff.size);
92
114
    }
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
86
12
    void unserialize(BufferReadable& buf) {
87
12
        size_t size;
88
12
        buf.read_binary(size);
89
12
        _nums.resize(size);
90
12
        auto buff = buf.read(sizeof(Ty) * size);
91
12
        memcpy(_nums.data(), buff.data, buff.size);
92
12
    }
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
102
    void unserialize(BufferReadable& buf) {
87
102
        size_t size;
88
102
        buf.read_binary(size);
89
102
        _nums.resize(size);
90
102
        auto buff = buf.read(sizeof(Ty) * size);
91
102
        memcpy(_nums.data(), buff.data, buff.size);
92
102
    }
93
94
262
    double terminate(double quantile) {
95
262
        if (!_sorted_nums_vec.empty()) {
96
84
            _move_nums_to_sorted_vec();
97
84
        }
98
262
        if (_sorted_nums_vec.size() <= 1) {
99
224
            if (_sorted_nums_vec.size() == 1) {
100
46
                _nums = std::move(_sorted_nums_vec[0]);
101
46
                _sorted_nums_vec.clear();
102
46
            }
103
104
224
            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
2
                return 0.0;
108
2
            }
109
110
222
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
111
2
                pdqsort(_nums.begin(), _nums.end());
112
2
            }
113
114
222
            if (quantile == 1 || _nums.size() == 1) {
115
188
                return _nums.back();
116
188
            }
117
118
34
            double u = (_nums.size() - 1) * quantile;
119
34
            auto index = static_cast<uint32_t>(u);
120
34
            return _nums[index] +
121
34
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
122
34
                                                       static_cast<double>(_nums[index]));
123
222
        } else {
124
38
            DCHECK(_nums.empty());
125
38
            size_t rows = 0;
126
76
            for (const auto& i : _sorted_nums_vec) {
127
76
                rows += i.size();
128
76
            }
129
38
            const bool reverse = quantile > 0.5 && rows > 2;
130
38
            double u = (rows - 1) * quantile;
131
38
            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
38
            size_t target = reverse ? rows - index - 2 : index;
140
38
            if (quantile == 1) {
141
4
                target = 0;
142
4
            }
143
38
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
144
38
            if (quantile == 1) {
145
4
                return second_number;
146
4
            }
147
34
            return first_number +
148
34
                   (u - static_cast<double>(index)) *
149
34
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
150
38
        }
151
262
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
94
42
    double terminate(double quantile) {
95
42
        if (!_sorted_nums_vec.empty()) {
96
18
            _move_nums_to_sorted_vec();
97
18
        }
98
42
        if (_sorted_nums_vec.size() <= 1) {
99
26
            if (_sorted_nums_vec.size() == 1) {
100
2
                _nums = std::move(_sorted_nums_vec[0]);
101
2
                _sorted_nums_vec.clear();
102
2
            }
103
104
26
            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
2
                return 0.0;
108
2
            }
109
110
24
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
111
2
                pdqsort(_nums.begin(), _nums.end());
112
2
            }
113
114
24
            if (quantile == 1 || _nums.size() == 1) {
115
10
                return _nums.back();
116
10
            }
117
118
14
            double u = (_nums.size() - 1) * quantile;
119
14
            auto index = static_cast<uint32_t>(u);
120
14
            return _nums[index] +
121
14
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
122
14
                                                       static_cast<double>(_nums[index]));
123
24
        } else {
124
16
            DCHECK(_nums.empty());
125
16
            size_t rows = 0;
126
32
            for (const auto& i : _sorted_nums_vec) {
127
32
                rows += i.size();
128
32
            }
129
16
            const bool reverse = quantile > 0.5 && rows > 2;
130
16
            double u = (rows - 1) * quantile;
131
16
            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
16
            size_t target = reverse ? rows - index - 2 : index;
140
16
            if (quantile == 1) {
141
2
                target = 0;
142
2
            }
143
16
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
144
16
            if (quantile == 1) {
145
2
                return second_number;
146
2
            }
147
14
            return first_number +
148
14
                   (u - static_cast<double>(index)) *
149
14
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
150
16
        }
151
42
    }
Unexecuted instantiation: _ZN5doris6CountsIaE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIsE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIiE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsInE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
94
220
    double terminate(double quantile) {
95
220
        if (!_sorted_nums_vec.empty()) {
96
66
            _move_nums_to_sorted_vec();
97
66
        }
98
220
        if (_sorted_nums_vec.size() <= 1) {
99
198
            if (_sorted_nums_vec.size() == 1) {
100
44
                _nums = std::move(_sorted_nums_vec[0]);
101
44
                _sorted_nums_vec.clear();
102
44
            }
103
104
198
            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
198
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
111
0
                pdqsort(_nums.begin(), _nums.end());
112
0
            }
113
114
198
            if (quantile == 1 || _nums.size() == 1) {
115
178
                return _nums.back();
116
178
            }
117
118
20
            double u = (_nums.size() - 1) * quantile;
119
20
            auto index = static_cast<uint32_t>(u);
120
20
            return _nums[index] +
121
20
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
122
20
                                                       static_cast<double>(_nums[index]));
123
198
        } else {
124
22
            DCHECK(_nums.empty());
125
22
            size_t rows = 0;
126
44
            for (const auto& i : _sorted_nums_vec) {
127
44
                rows += i.size();
128
44
            }
129
22
            const bool reverse = quantile > 0.5 && rows > 2;
130
22
            double u = (rows - 1) * quantile;
131
22
            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
22
            size_t target = reverse ? rows - index - 2 : index;
140
22
            if (quantile == 1) {
141
2
                target = 0;
142
2
            }
143
22
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
144
22
            if (quantile == 1) {
145
2
                return second_number;
146
2
            }
147
20
            return first_number +
148
20
                   (u - static_cast<double>(index)) *
149
20
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
150
22
        }
151
220
    }
152
153
private:
154
    struct Node {
155
        Ty value;
156
        int array_index;
157
        int64_t element_index;
158
159
80
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
159
58
        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
22
        auto operator<=>(const Node& other) const { return value <=> other.value; }
160
    };
161
162
372
    void _move_nums_to_sorted_vec() {
163
372
        if (_nums.empty()) {
164
230
            return;
165
230
        }
166
142
        if (!std::is_sorted(_nums.begin(), _nums.end())) {
167
12
            pdqsort(_nums.begin(), _nums.end());
168
12
        }
169
142
        _sorted_nums_vec.emplace_back(std::move(_nums));
170
142
        DCHECK(_nums.empty());
171
142
    }
_ZN5doris6CountsIlE24_move_nums_to_sorted_vecEv
Line
Count
Source
162
82
    void _move_nums_to_sorted_vec() {
163
82
        if (_nums.empty()) {
164
56
            return;
165
56
        }
166
26
        if (!std::is_sorted(_nums.begin(), _nums.end())) {
167
12
            pdqsort(_nums.begin(), _nums.end());
168
12
        }
169
26
        _sorted_nums_vec.emplace_back(std::move(_nums));
170
        DCHECK(_nums.empty());
171
26
    }
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
290
    void _move_nums_to_sorted_vec() {
163
290
        if (_nums.empty()) {
164
174
            return;
165
174
        }
166
116
        if (!std::is_sorted(_nums.begin(), _nums.end())) {
167
0
            pdqsort(_nums.begin(), _nums.end());
168
0
        }
169
116
        _sorted_nums_vec.emplace_back(std::move(_nums));
170
        DCHECK(_nums.empty());
171
116
    }
172
173
4
    void _convert_sorted_num_vec_to_nums() {
174
4
        size_t rows = 0;
175
8
        for (const auto& i : _sorted_nums_vec) {
176
8
            rows += i.size();
177
8
        }
178
4
        _nums.resize(rows);
179
4
        size_t count = 0;
180
181
4
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
182
12
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
183
8
            if (!_sorted_nums_vec[i].empty()) {
184
8
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
185
8
            }
186
8
        }
187
188
20
        while (!min_heap.empty()) {
189
16
            Node node = min_heap.top();
190
16
            min_heap.pop();
191
16
            _nums[count++] = node.value;
192
16
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
193
8
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
8
                min_heap.push(node);
195
8
            }
196
16
        }
197
4
        _sorted_nums_vec.clear();
198
4
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
173
4
    void _convert_sorted_num_vec_to_nums() {
174
4
        size_t rows = 0;
175
8
        for (const auto& i : _sorted_nums_vec) {
176
8
            rows += i.size();
177
8
        }
178
4
        _nums.resize(rows);
179
4
        size_t count = 0;
180
181
4
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
182
12
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
183
8
            if (!_sorted_nums_vec[i].empty()) {
184
8
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
185
8
            }
186
8
        }
187
188
20
        while (!min_heap.empty()) {
189
16
            Node node = min_heap.top();
190
16
            min_heap.pop();
191
16
            _nums[count++] = node.value;
192
16
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
193
8
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
8
                min_heap.push(node);
195
8
            }
196
16
        }
197
4
        _sorted_nums_vec.clear();
198
4
    }
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
38
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
201
38
        Ty first_number = 0, second_number = 0;
202
38
        size_t count = 0;
203
38
        if (reverse) {
204
8
            std::priority_queue<Node> max_heap;
205
24
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
206
16
                if (!_sorted_nums_vec[i].empty()) {
207
16
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
208
16
                                     _sorted_nums_vec[i].size() - 1);
209
16
                }
210
16
            }
211
212
16
            while (!max_heap.empty()) {
213
16
                Node node = max_heap.top();
214
16
                max_heap.pop();
215
16
                if (count == target) {
216
8
                    second_number = node.value;
217
8
                } else if (count == target + 1) {
218
8
                    first_number = node.value;
219
8
                    break;
220
8
                }
221
8
                ++count;
222
8
                if (--node.element_index >= 0) {
223
8
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
224
8
                    max_heap.push(node);
225
8
                }
226
8
            }
227
228
30
        } else {
229
30
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
230
90
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
231
60
                if (!_sorted_nums_vec[i].empty()) {
232
60
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
233
60
                }
234
60
            }
235
236
76
            while (!min_heap.empty()) {
237
76
                Node node = min_heap.top();
238
76
                min_heap.pop();
239
76
                if (count == target) {
240
30
                    first_number = node.value;
241
46
                } else if (count == target + 1) {
242
30
                    second_number = node.value;
243
30
                    break;
244
30
                }
245
46
                ++count;
246
46
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
247
24
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
248
24
                    min_heap.push(node);
249
24
                }
250
46
            }
251
30
        }
252
253
38
        return {first_number, second_number};
254
38
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
200
16
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
201
16
        Ty first_number = 0, second_number = 0;
202
16
        size_t count = 0;
203
16
        if (reverse) {
204
8
            std::priority_queue<Node> max_heap;
205
24
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
206
16
                if (!_sorted_nums_vec[i].empty()) {
207
16
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
208
16
                                     _sorted_nums_vec[i].size() - 1);
209
16
                }
210
16
            }
211
212
16
            while (!max_heap.empty()) {
213
16
                Node node = max_heap.top();
214
16
                max_heap.pop();
215
16
                if (count == target) {
216
8
                    second_number = node.value;
217
8
                } else if (count == target + 1) {
218
8
                    first_number = node.value;
219
8
                    break;
220
8
                }
221
8
                ++count;
222
8
                if (--node.element_index >= 0) {
223
8
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
224
8
                    max_heap.push(node);
225
8
                }
226
8
            }
227
228
8
        } else {
229
8
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
230
24
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
231
16
                if (!_sorted_nums_vec[i].empty()) {
232
16
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
233
16
                }
234
16
            }
235
236
32
            while (!min_heap.empty()) {
237
32
                Node node = min_heap.top();
238
32
                min_heap.pop();
239
32
                if (count == target) {
240
8
                    first_number = node.value;
241
24
                } else if (count == target + 1) {
242
8
                    second_number = node.value;
243
8
                    break;
244
8
                }
245
24
                ++count;
246
24
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
247
24
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
248
24
                    min_heap.push(node);
249
24
                }
250
24
            }
251
8
        }
252
253
16
        return {first_number, second_number};
254
16
    }
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
22
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
201
22
        Ty first_number = 0, second_number = 0;
202
22
        size_t count = 0;
203
22
        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
22
        } else {
229
22
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
230
66
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
231
44
                if (!_sorted_nums_vec[i].empty()) {
232
44
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
233
44
                }
234
44
            }
235
236
44
            while (!min_heap.empty()) {
237
44
                Node node = min_heap.top();
238
44
                min_heap.pop();
239
44
                if (count == target) {
240
22
                    first_number = node.value;
241
22
                } else if (count == target + 1) {
242
22
                    second_number = node.value;
243
22
                    break;
244
22
                }
245
22
                ++count;
246
22
                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
22
            }
251
22
        }
252
253
22
        return {first_number, second_number};
254
22
    }
255
256
    PODArray<Ty> _nums;
257
    std::vector<PODArray<Ty>> _sorted_nums_vec;
258
};
259
260
class PercentileLevels {
261
public:
262
6
    void merge(const PercentileLevels& rhs) {
263
6
        if (rhs.empty()) {
264
2
            return;
265
2
        }
266
267
4
        if (empty()) {
268
2
            quantiles = rhs.quantiles;
269
2
            permutation = rhs.permutation;
270
2
            return;
271
2
        }
272
273
4
        DCHECK_EQ(quantiles.size(), rhs.quantiles.size());
274
10
        for (size_t i = 0; i < quantiles.size(); ++i) {
275
8
            DCHECK_EQ(quantiles[i], rhs.quantiles[i]);
276
8
        }
277
2
    }
278
279
408
    void write(BufferWritable& buf) const {
280
408
        int size_num = cast_set<int>(quantiles.size());
281
408
        buf.write_binary(size_num);
282
724
        for (const auto& quantile : quantiles) {
283
724
            buf.write_binary(quantile);
284
724
        }
285
408
    }
286
287
458
    void read(BufferReadable& buf) {
288
458
        int size_num = 0;
289
458
        buf.read_binary(size_num);
290
291
458
        quantiles.resize(size_num);
292
458
        permutation.resize(size_num);
293
1.24k
        for (int i = 0; i < size_num; ++i) {
294
788
            buf.read_binary(quantiles[i]);
295
788
            permutation[i] = cast_set<size_t>(i);
296
788
        }
297
458
    }
298
299
842
    void clear() {
300
842
        quantiles.clear();
301
842
        permutation.clear();
302
842
    }
303
304
8.53k
    bool empty() const { return quantiles.empty(); }
305
306
798
    const std::vector<size_t>& get_permutation() const {
307
798
        sort_permutation();
308
798
        return permutation;
309
798
    }
310
311
798
    void sort_permutation() const {
312
798
        pdqsort(permutation.begin(), permutation.end(),
313
1.04k
                [this](size_t lhs, size_t rhs) { return quantiles[lhs] < quantiles[rhs]; });
314
798
    }
315
316
    std::vector<double> quantiles;
317
    mutable std::vector<size_t> permutation;
318
};
319
320
} // namespace doris