Coverage Report

Created: 2026-09-17 12:27

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.72k
inline void check_quantile(double quantile) {
38
4.72k
    if (!std::isfinite(quantile) || 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
4.72k
}
44
45
template <typename Ty>
46
class Counts {
47
public:
48
1.61k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
48
86
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIsEC2Ev
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
48
795
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
48
484
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsInEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
48
245
    Counts() = default;
49
50
643
    void merge(Counts* other) {
51
643
        if (other != nullptr && !other->_nums.empty()) {
52
643
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
53
643
        }
54
643
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
50
20
    void merge(Counts* other) {
51
20
        if (other != nullptr && !other->_nums.empty()) {
52
20
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
53
20
        }
54
20
    }
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
228
    void merge(Counts* other) {
51
228
        if (other != nullptr && !other->_nums.empty()) {
52
228
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
53
228
        }
54
228
    }
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
50
56
    void merge(Counts* other) {
51
56
        if (other != nullptr && !other->_nums.empty()) {
52
56
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
53
56
        }
54
56
    }
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
619
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
64
32
    void increment(Ty key) { _nums.push_back(key); }
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
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
64
160
    void increment(Ty key) { _nums.push_back(key); }
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
1.01k
    void serialize(BufferWritable& buf) {
69
1.01k
        if (!_nums.empty()) {
70
756
            pdqsort(_nums.begin(), _nums.end());
71
756
            size_t size = _nums.size();
72
756
            buf.write_binary(size);
73
756
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
74
756
        } else {
75
            // convert _sorted_nums_vec to _nums and do seiralize again
76
261
            _convert_sorted_num_vec_to_nums();
77
261
            serialize(buf);
78
261
        }
79
1.01k
    }
_ZN5doris6CountsIaE9serializeERNS_14BufferWritableE
Line
Count
Source
68
36
    void serialize(BufferWritable& buf) {
69
36
        if (!_nums.empty()) {
70
34
            pdqsort(_nums.begin(), _nums.end());
71
34
            size_t size = _nums.size();
72
34
            buf.write_binary(size);
73
34
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
74
34
        } else {
75
            // convert _sorted_nums_vec to _nums and do seiralize again
76
2
            _convert_sorted_num_vec_to_nums();
77
2
            serialize(buf);
78
2
        }
79
36
    }
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
332
    void serialize(BufferWritable& buf) {
69
332
        if (!_nums.empty()) {
70
229
            pdqsort(_nums.begin(), _nums.end());
71
229
            size_t size = _nums.size();
72
229
            buf.write_binary(size);
73
229
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
74
229
        } else {
75
            // convert _sorted_nums_vec to _nums and do seiralize again
76
103
            _convert_sorted_num_vec_to_nums();
77
103
            serialize(buf);
78
103
        }
79
332
    }
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_14BufferWritableE
_ZN5doris6CountsIdE9serializeERNS_14BufferWritableE
Line
Count
Source
68
37
    void serialize(BufferWritable& buf) {
69
37
        if (!_nums.empty()) {
70
37
            pdqsort(_nums.begin(), _nums.end());
71
37
            size_t size = _nums.size();
72
37
            buf.write_binary(size);
73
37
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
74
37
        } else {
75
            // convert _sorted_nums_vec to _nums and do seiralize again
76
0
            _convert_sorted_num_vec_to_nums();
77
0
            serialize(buf);
78
0
        }
79
37
    }
80
81
652
    void unserialize(BufferReadable& buf) {
82
652
        size_t size;
83
652
        buf.read_binary(size);
84
652
        _nums.resize(size);
85
652
        auto buff = buf.read(sizeof(Ty) * size);
86
652
        memcpy(_nums.data(), buff.data, buff.size);
87
652
    }
_ZN5doris6CountsIaE11unserializeERNS_14BufferReadableE
Line
Count
Source
81
34
    void unserialize(BufferReadable& buf) {
82
34
        size_t size;
83
34
        buf.read_binary(size);
84
34
        _nums.resize(size);
85
34
        auto buff = buf.read(sizeof(Ty) * size);
86
34
        memcpy(_nums.data(), buff.data, buff.size);
87
34
    }
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
228
    void unserialize(BufferReadable& buf) {
82
228
        size_t size;
83
228
        buf.read_binary(size);
84
228
        _nums.resize(size);
85
228
        auto buff = buf.read(sizeof(Ty) * size);
86
228
        memcpy(_nums.data(), buff.data, buff.size);
87
228
    }
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_14BufferReadableE
_ZN5doris6CountsIdE11unserializeERNS_14BufferReadableE
Line
Count
Source
81
51
    void unserialize(BufferReadable& buf) {
82
51
        size_t size;
83
51
        buf.read_binary(size);
84
51
        _nums.resize(size);
85
51
        auto buff = buf.read(sizeof(Ty) * size);
86
51
        memcpy(_nums.data(), buff.data, buff.size);
87
51
    }
88
89
148
    double terminate(double quantile) {
90
148
        if (_sorted_nums_vec.size() <= 1) {
91
135
            if (_sorted_nums_vec.size() == 1) {
92
49
                _nums = std::move(_sorted_nums_vec[0]);
93
49
            }
94
95
135
            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
134
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
102
1
                pdqsort(_nums.begin(), _nums.end());
103
1
            }
104
105
134
            if (quantile == 1 || _nums.size() == 1) {
106
99
                return _nums.back();
107
99
            }
108
109
35
            double u = (_nums.size() - 1) * quantile;
110
35
            auto index = static_cast<uint32_t>(u);
111
35
            return _nums[index] +
112
35
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
113
35
                                                       static_cast<double>(_nums[index]));
114
134
        } else {
115
13
            DCHECK(_nums.empty());
116
13
            size_t rows = 0;
117
26
            for (const auto& i : _sorted_nums_vec) {
118
26
                rows += i.size();
119
26
            }
120
13
            const bool reverse = quantile > 0.5 && rows > 2;
121
13
            double u = (rows - 1) * quantile;
122
13
            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
13
            size_t target = reverse ? rows - index - 2 : index;
131
13
            if (quantile == 1) {
132
1
                target = 0;
133
1
            }
134
13
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
135
13
            if (quantile == 1) {
136
1
                return second_number;
137
1
            }
138
12
            return first_number +
139
12
                   (u - static_cast<double>(index)) *
140
12
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
141
13
        }
142
148
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
89
4
    double terminate(double quantile) {
90
4
        if (_sorted_nums_vec.size() <= 1) {
91
4
            if (_sorted_nums_vec.size() == 1) {
92
4
                _nums = std::move(_sorted_nums_vec[0]);
93
4
            }
94
95
4
            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
0
                return 0.0;
99
0
            }
100
101
4
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
102
0
                pdqsort(_nums.begin(), _nums.end());
103
0
            }
104
105
4
            if (quantile == 1 || _nums.size() == 1) {
106
4
                return _nums.back();
107
4
            }
108
109
0
            double u = (_nums.size() - 1) * quantile;
110
0
            auto index = static_cast<uint32_t>(u);
111
0
            return _nums[index] +
112
0
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
113
0
                                                       static_cast<double>(_nums[index]));
114
4
        } else {
115
0
            DCHECK(_nums.empty());
116
0
            size_t rows = 0;
117
0
            for (const auto& i : _sorted_nums_vec) {
118
0
                rows += i.size();
119
0
            }
120
0
            const bool reverse = quantile > 0.5 && rows > 2;
121
0
            double u = (rows - 1) * quantile;
122
0
            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
0
            size_t target = reverse ? rows - index - 2 : index;
131
0
            if (quantile == 1) {
132
0
                target = 0;
133
0
            }
134
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
135
0
            if (quantile == 1) {
136
0
                return second_number;
137
0
            }
138
0
            return first_number +
139
0
                   (u - static_cast<double>(index)) *
140
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
141
0
        }
142
4
    }
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
30
            if (_sorted_nums_vec.size() == 1) {
92
21
                _nums = std::move(_sorted_nums_vec[0]);
93
21
            }
94
95
30
            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
29
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
102
1
                pdqsort(_nums.begin(), _nums.end());
103
1
            }
104
105
29
            if (quantile == 1 || _nums.size() == 1) {
106
4
                return _nums.back();
107
4
            }
108
109
25
            double u = (_nums.size() - 1) * quantile;
110
25
            auto index = static_cast<uint32_t>(u);
111
25
            return _nums[index] +
112
25
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
113
25
                                                       static_cast<double>(_nums[index]));
114
29
        } else {
115
4
            DCHECK(_nums.empty());
116
4
            size_t rows = 0;
117
8
            for (const auto& i : _sorted_nums_vec) {
118
8
                rows += i.size();
119
8
            }
120
4
            const bool reverse = quantile > 0.5 && rows > 2;
121
4
            double u = (rows - 1) * quantile;
122
4
            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
4
            size_t target = reverse ? rows - index - 2 : index;
131
4
            if (quantile == 1) {
132
0
                target = 0;
133
0
            }
134
4
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
135
4
            if (quantile == 1) {
136
0
                return second_number;
137
0
            }
138
4
            return first_number +
139
4
                   (u - static_cast<double>(index)) *
140
4
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
141
4
        }
142
34
    }
Unexecuted instantiation: _ZN5doris6CountsInE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
89
110
    double terminate(double quantile) {
90
110
        if (_sorted_nums_vec.size() <= 1) {
91
101
            if (_sorted_nums_vec.size() == 1) {
92
24
                _nums = std::move(_sorted_nums_vec[0]);
93
24
            }
94
95
101
            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
0
                return 0.0;
99
0
            }
100
101
101
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
102
0
                pdqsort(_nums.begin(), _nums.end());
103
0
            }
104
105
101
            if (quantile == 1 || _nums.size() == 1) {
106
91
                return _nums.back();
107
91
            }
108
109
10
            double u = (_nums.size() - 1) * quantile;
110
10
            auto index = static_cast<uint32_t>(u);
111
10
            return _nums[index] +
112
10
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
113
10
                                                       static_cast<double>(_nums[index]));
114
101
        } else {
115
9
            DCHECK(_nums.empty());
116
9
            size_t rows = 0;
117
18
            for (const auto& i : _sorted_nums_vec) {
118
18
                rows += i.size();
119
18
            }
120
9
            const bool reverse = quantile > 0.5 && rows > 2;
121
9
            double u = (rows - 1) * quantile;
122
9
            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
9
            size_t target = reverse ? rows - index - 2 : index;
131
9
            if (quantile == 1) {
132
1
                target = 0;
133
1
            }
134
9
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
135
9
            if (quantile == 1) {
136
1
                return second_number;
137
1
            }
138
8
            return first_number +
139
8
                   (u - static_cast<double>(index)) *
140
8
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
141
9
        }
142
110
    }
143
144
private:
145
    struct Node {
146
        Ty value;
147
        int array_index;
148
        int64_t element_index;
149
150
486
        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
186
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
150
9
        auto operator<=>(const Node& other) const { return value <=> other.value; }
151
    };
152
153
261
    void _convert_sorted_num_vec_to_nums() {
154
261
        size_t rows = 0;
155
540
        for (const auto& i : _sorted_nums_vec) {
156
540
            rows += i.size();
157
540
        }
158
261
        _nums.resize(rows);
159
261
        size_t count = 0;
160
161
261
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
162
801
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
163
540
            if (!_sorted_nums_vec[i].empty()) {
164
540
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
165
540
            }
166
540
        }
167
168
958
        while (!min_heap.empty()) {
169
697
            Node node = min_heap.top();
170
697
            min_heap.pop();
171
697
            _nums[count++] = node.value;
172
697
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
173
159
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
174
159
                min_heap.push(node);
175
159
            }
176
697
        }
177
261
        _sorted_nums_vec.clear();
178
261
    }
_ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
153
2
    void _convert_sorted_num_vec_to_nums() {
154
2
        size_t rows = 0;
155
2
        for (const auto& i : _sorted_nums_vec) {
156
2
            rows += i.size();
157
2
        }
158
2
        _nums.resize(rows);
159
2
        size_t count = 0;
160
161
2
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
162
4
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
163
2
            if (!_sorted_nums_vec[i].empty()) {
164
2
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
165
2
            }
166
2
        }
167
168
4
        while (!min_heap.empty()) {
169
2
            Node node = min_heap.top();
170
2
            min_heap.pop();
171
2
            _nums[count++] = node.value;
172
2
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
173
0
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
174
0
                min_heap.push(node);
175
0
            }
176
2
        }
177
2
        _sorted_nums_vec.clear();
178
2
    }
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
103
    void _convert_sorted_num_vec_to_nums() {
154
103
        size_t rows = 0;
155
199
        for (const auto& i : _sorted_nums_vec) {
156
199
            rows += i.size();
157
199
        }
158
103
        _nums.resize(rows);
159
103
        size_t count = 0;
160
161
103
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
162
302
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
163
199
            if (!_sorted_nums_vec[i].empty()) {
164
199
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
165
199
            }
166
199
        }
167
168
423
        while (!min_heap.empty()) {
169
320
            Node node = min_heap.top();
170
320
            min_heap.pop();
171
320
            _nums[count++] = node.value;
172
320
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
173
123
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
174
123
                min_heap.push(node);
175
123
            }
176
320
        }
177
103
        _sorted_nums_vec.clear();
178
103
    }
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
13
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
181
13
        Ty first_number = 0, second_number = 0;
182
13
        size_t count = 0;
183
13
        if (reverse) {
184
1
            std::priority_queue<Node> max_heap;
185
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
186
2
                if (!_sorted_nums_vec[i].empty()) {
187
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
188
2
                                     _sorted_nums_vec[i].size() - 1);
189
2
                }
190
2
            }
191
192
2
            while (!max_heap.empty()) {
193
2
                Node node = max_heap.top();
194
2
                max_heap.pop();
195
2
                if (count == target) {
196
1
                    second_number = node.value;
197
1
                } else if (count == target + 1) {
198
1
                    first_number = node.value;
199
1
                    break;
200
1
                }
201
1
                ++count;
202
1
                if (--node.element_index >= 0) {
203
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
204
1
                    max_heap.push(node);
205
1
                }
206
1
            }
207
208
12
        } else {
209
12
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
210
36
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
211
24
                if (!_sorted_nums_vec[i].empty()) {
212
24
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
213
24
                }
214
24
            }
215
216
31
            while (!min_heap.empty()) {
217
31
                Node node = min_heap.top();
218
31
                min_heap.pop();
219
31
                if (count == target) {
220
12
                    first_number = node.value;
221
19
                } else if (count == target + 1) {
222
12
                    second_number = node.value;
223
12
                    break;
224
12
                }
225
19
                ++count;
226
19
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
227
10
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
228
10
                    min_heap.push(node);
229
10
                }
230
19
            }
231
12
        }
232
233
13
        return {first_number, second_number};
234
13
    }
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
4
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
181
4
        Ty first_number = 0, second_number = 0;
182
4
        size_t count = 0;
183
4
        if (reverse) {
184
1
            std::priority_queue<Node> max_heap;
185
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
186
2
                if (!_sorted_nums_vec[i].empty()) {
187
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
188
2
                                     _sorted_nums_vec[i].size() - 1);
189
2
                }
190
2
            }
191
192
2
            while (!max_heap.empty()) {
193
2
                Node node = max_heap.top();
194
2
                max_heap.pop();
195
2
                if (count == target) {
196
1
                    second_number = node.value;
197
1
                } else if (count == target + 1) {
198
1
                    first_number = node.value;
199
1
                    break;
200
1
                }
201
1
                ++count;
202
1
                if (--node.element_index >= 0) {
203
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
204
1
                    max_heap.push(node);
205
1
                }
206
1
            }
207
208
3
        } else {
209
3
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
210
9
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
211
6
                if (!_sorted_nums_vec[i].empty()) {
212
6
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
213
6
                }
214
6
            }
215
216
13
            while (!min_heap.empty()) {
217
13
                Node node = min_heap.top();
218
13
                min_heap.pop();
219
13
                if (count == target) {
220
3
                    first_number = node.value;
221
10
                } else if (count == target + 1) {
222
3
                    second_number = node.value;
223
3
                    break;
224
3
                }
225
10
                ++count;
226
10
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
227
10
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
228
10
                    min_heap.push(node);
229
10
                }
230
10
            }
231
3
        }
232
233
4
        return {first_number, second_number};
234
4
    }
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
180
9
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
181
9
        Ty first_number = 0, second_number = 0;
182
9
        size_t count = 0;
183
9
        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
9
        } else {
209
9
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
210
27
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
211
18
                if (!_sorted_nums_vec[i].empty()) {
212
18
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
213
18
                }
214
18
            }
215
216
18
            while (!min_heap.empty()) {
217
18
                Node node = min_heap.top();
218
18
                min_heap.pop();
219
18
                if (count == target) {
220
9
                    first_number = node.value;
221
9
                } else if (count == target + 1) {
222
9
                    second_number = node.value;
223
9
                    break;
224
9
                }
225
9
                ++count;
226
9
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
227
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
228
0
                    min_heap.push(node);
229
0
                }
230
9
            }
231
9
        }
232
233
9
        return {first_number, second_number};
234
9
    }
235
236
    PODArray<Ty> _nums;
237
    std::vector<PODArray<Ty>> _sorted_nums_vec;
238
};
239
240
class PercentileLevels {
241
public:
242
0
    void merge(const PercentileLevels& rhs) {
243
0
        if (rhs.empty()) {
244
0
            return;
245
0
        }
246
0
247
0
        if (empty()) {
248
0
            quantiles = rhs.quantiles;
249
0
            permutation = rhs.permutation;
250
0
            return;
251
0
        }
252
0
253
0
        DCHECK_EQ(quantiles.size(), rhs.quantiles.size());
254
0
        for (size_t i = 0; i < quantiles.size(); ++i) {
255
0
            DCHECK_EQ(quantiles[i], rhs.quantiles[i]);
256
0
        }
257
0
    }
258
259
786
    void write(BufferWritable& buf) const {
260
786
        int size_num = cast_set<int>(quantiles.size());
261
786
        buf.write_binary(size_num);
262
1.50k
        for (const auto& quantile : quantiles) {
263
1.50k
            buf.write_binary(quantile);
264
1.50k
        }
265
786
    }
266
267
811
    void read(BufferReadable& buf) {
268
811
        int size_num = 0;
269
811
        buf.read_binary(size_num);
270
271
811
        quantiles.resize(size_num);
272
811
        permutation.resize(size_num);
273
2.34k
        for (int i = 0; i < size_num; ++i) {
274
1.53k
            buf.read_binary(quantiles[i]);
275
1.53k
            permutation[i] = cast_set<size_t>(i);
276
1.53k
        }
277
811
    }
278
279
1.29k
    void clear() {
280
1.29k
        quantiles.clear();
281
1.29k
        permutation.clear();
282
1.29k
    }
283
284
7.90k
    bool empty() const { return quantiles.empty(); }
285
286
589
    const std::vector<size_t>& get_permutation() const {
287
589
        sort_permutation();
288
589
        return permutation;
289
589
    }
290
291
589
    void sort_permutation() const {
292
589
        pdqsort(permutation.begin(), permutation.end(),
293
806
                [this](size_t lhs, size_t rhs) { return quantiles[lhs] < quantiles[rhs]; });
294
589
    }
295
296
    std::vector<double> quantiles;
297
    mutable std::vector<size_t> permutation;
298
};
299
300
} // namespace doris