Coverage Report

Created: 2026-04-23 11:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/counts.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 <queue>
25
26
#include "core/pod_array.h"
27
#include "core/string_buffer.hpp"
28
#include "util/io_helper.h"
29
30
namespace doris {
31
32
template <typename Ty>
33
class Counts {
34
public:
35
3.60k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
47
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
199
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
2.23k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
794
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
43
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
13
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
268
    Counts() = default;
36
37
1.27k
    void merge(Counts* other) {
38
1.27k
        if (other != nullptr && !other->_nums.empty()) {
39
1.27k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
1.27k
        }
41
1.27k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
37
5
    void merge(Counts* other) {
38
5
        if (other != nullptr && !other->_nums.empty()) {
39
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
5
        }
41
5
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
37
8
    void merge(Counts* other) {
38
8
        if (other != nullptr && !other->_nums.empty()) {
39
8
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
8
        }
41
8
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
37
837
    void merge(Counts* other) {
38
837
        if (other != nullptr && !other->_nums.empty()) {
39
837
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
837
        }
41
837
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
322
    void merge(Counts* other) {
38
322
        if (other != nullptr && !other->_nums.empty()) {
39
322
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
322
        }
41
322
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
37
5
    void merge(Counts* other) {
38
5
        if (other != nullptr && !other->_nums.empty()) {
39
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
5
        }
41
5
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
37
5
    void merge(Counts* other) {
38
5
        if (other != nullptr && !other->_nums.empty()) {
39
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
5
        }
41
5
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
37
91
    void merge(Counts* other) {
38
91
        if (other != nullptr && !other->_nums.empty()) {
39
91
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
91
        }
41
91
    }
42
43
    void increment(Ty key, uint32_t i) {
44
        auto old_size = _nums.size();
45
        _nums.resize(_nums.size() + i);
46
        for (uint32_t j = 0; j < i; ++j) {
47
            _nums[old_size + j] = key;
48
        }
49
    }
50
51
2.93k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
51
73
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
51
552
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
51
1.59k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
51
401
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
51
45
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIfE9incrementEf
Line
Count
Source
51
9
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
51
259
    void increment(Ty key) { _nums.push_back(key); }
52
53
5
    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
_ZN5doris6CountsIlE15increment_batchERKNS_8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
53
5
    void increment_batch(const PaddedPODArray<Ty>& keys) { _nums.insert(keys.begin(), keys.end()); }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
54
55
1.67k
    void serialize(BufferWritable& buf) {
56
1.67k
        if (!_nums.empty()) {
57
1.40k
            pdqsort(_nums.begin(), _nums.end());
58
1.40k
            size_t size = _nums.size();
59
1.40k
            buf.write_binary(size);
60
1.40k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
1.40k
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
272
            _convert_sorted_num_vec_to_nums();
64
272
            serialize(buf);
65
272
        }
66
1.67k
    }
_ZN5doris6CountsIaE9serializeERNS_14BufferWritableE
Line
Count
Source
55
5
    void serialize(BufferWritable& buf) {
56
5
        if (!_nums.empty()) {
57
5
            pdqsort(_nums.begin(), _nums.end());
58
5
            size_t size = _nums.size();
59
5
            buf.write_binary(size);
60
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
5
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
5
    }
_ZN5doris6CountsIsE9serializeERNS_14BufferWritableE
Line
Count
Source
55
8
    void serialize(BufferWritable& buf) {
56
8
        if (!_nums.empty()) {
57
8
            pdqsort(_nums.begin(), _nums.end());
58
8
            size_t size = _nums.size();
59
8
            buf.write_binary(size);
60
8
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
8
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
8
    }
_ZN5doris6CountsIiE9serializeERNS_14BufferWritableE
Line
Count
Source
55
1.11k
    void serialize(BufferWritable& buf) {
56
1.11k
        if (!_nums.empty()) {
57
954
            pdqsort(_nums.begin(), _nums.end());
58
954
            size_t size = _nums.size();
59
954
            buf.write_binary(size);
60
954
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
954
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
156
            _convert_sorted_num_vec_to_nums();
64
156
            serialize(buf);
65
156
        }
66
1.11k
    }
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
55
453
    void serialize(BufferWritable& buf) {
56
453
        if (!_nums.empty()) {
57
337
            pdqsort(_nums.begin(), _nums.end());
58
337
            size_t size = _nums.size();
59
337
            buf.write_binary(size);
60
337
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
337
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
116
            _convert_sorted_num_vec_to_nums();
64
116
            serialize(buf);
65
116
        }
66
453
    }
_ZN5doris6CountsInE9serializeERNS_14BufferWritableE
Line
Count
Source
55
5
    void serialize(BufferWritable& buf) {
56
5
        if (!_nums.empty()) {
57
5
            pdqsort(_nums.begin(), _nums.end());
58
5
            size_t size = _nums.size();
59
5
            buf.write_binary(size);
60
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
5
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
5
    }
_ZN5doris6CountsIfE9serializeERNS_14BufferWritableE
Line
Count
Source
55
5
    void serialize(BufferWritable& buf) {
56
5
        if (!_nums.empty()) {
57
5
            pdqsort(_nums.begin(), _nums.end());
58
5
            size_t size = _nums.size();
59
5
            buf.write_binary(size);
60
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
5
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
5
    }
_ZN5doris6CountsIdE9serializeERNS_14BufferWritableE
Line
Count
Source
55
91
    void serialize(BufferWritable& buf) {
56
91
        if (!_nums.empty()) {
57
91
            pdqsort(_nums.begin(), _nums.end());
58
91
            size_t size = _nums.size();
59
91
            buf.write_binary(size);
60
91
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
91
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
91
    }
67
68
1.27k
    void unserialize(BufferReadable& buf) {
69
1.27k
        size_t size;
70
1.27k
        buf.read_binary(size);
71
1.27k
        _nums.resize(size);
72
1.27k
        auto buff = buf.read(sizeof(Ty) * size);
73
1.27k
        memcpy(_nums.data(), buff.data, buff.size);
74
1.27k
    }
_ZN5doris6CountsIaE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
5
    void unserialize(BufferReadable& buf) {
69
5
        size_t size;
70
5
        buf.read_binary(size);
71
5
        _nums.resize(size);
72
5
        auto buff = buf.read(sizeof(Ty) * size);
73
5
        memcpy(_nums.data(), buff.data, buff.size);
74
5
    }
_ZN5doris6CountsIsE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
8
    void unserialize(BufferReadable& buf) {
69
8
        size_t size;
70
8
        buf.read_binary(size);
71
8
        _nums.resize(size);
72
8
        auto buff = buf.read(sizeof(Ty) * size);
73
8
        memcpy(_nums.data(), buff.data, buff.size);
74
8
    }
_ZN5doris6CountsIiE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
837
    void unserialize(BufferReadable& buf) {
69
837
        size_t size;
70
837
        buf.read_binary(size);
71
837
        _nums.resize(size);
72
837
        auto buff = buf.read(sizeof(Ty) * size);
73
837
        memcpy(_nums.data(), buff.data, buff.size);
74
837
    }
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
322
    void unserialize(BufferReadable& buf) {
69
322
        size_t size;
70
322
        buf.read_binary(size);
71
322
        _nums.resize(size);
72
322
        auto buff = buf.read(sizeof(Ty) * size);
73
322
        memcpy(_nums.data(), buff.data, buff.size);
74
322
    }
_ZN5doris6CountsInE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
5
    void unserialize(BufferReadable& buf) {
69
5
        size_t size;
70
5
        buf.read_binary(size);
71
5
        _nums.resize(size);
72
5
        auto buff = buf.read(sizeof(Ty) * size);
73
5
        memcpy(_nums.data(), buff.data, buff.size);
74
5
    }
_ZN5doris6CountsIfE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
5
    void unserialize(BufferReadable& buf) {
69
5
        size_t size;
70
5
        buf.read_binary(size);
71
5
        _nums.resize(size);
72
5
        auto buff = buf.read(sizeof(Ty) * size);
73
5
        memcpy(_nums.data(), buff.data, buff.size);
74
5
    }
_ZN5doris6CountsIdE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
91
    void unserialize(BufferReadable& buf) {
69
91
        size_t size;
70
91
        buf.read_binary(size);
71
91
        _nums.resize(size);
72
91
        auto buff = buf.read(sizeof(Ty) * size);
73
91
        memcpy(_nums.data(), buff.data, buff.size);
74
91
    }
75
76
1.10k
    double terminate(double quantile) {
77
1.10k
        if (_sorted_nums_vec.size() <= 1) {
78
948
            if (_sorted_nums_vec.size() == 1) {
79
295
                _nums = std::move(_sorted_nums_vec[0]);
80
295
            }
81
82
948
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
948
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
180
                pdqsort(_nums.begin(), _nums.end());
90
180
            }
91
92
948
            if (quantile == 1 || _nums.size() == 1) {
93
473
                return _nums.back();
94
473
            }
95
96
475
            double u = (_nums.size() - 1) * quantile;
97
475
            auto index = static_cast<uint32_t>(u);
98
475
            return _nums[index] +
99
475
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
475
                                                       static_cast<double>(_nums[index]));
101
948
        } else {
102
157
            DCHECK(_nums.empty());
103
157
            size_t rows = 0;
104
423
            for (const auto& i : _sorted_nums_vec) {
105
423
                rows += i.size();
106
423
            }
107
157
            const bool reverse = quantile > 0.5 && rows > 2;
108
157
            double u = (rows - 1) * quantile;
109
157
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
157
            size_t target = reverse ? rows - index - 2 : index;
118
157
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
157
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
157
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
157
            return first_number +
126
157
                   (u - static_cast<double>(index)) *
127
157
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
157
        }
129
1.10k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
76
61
    double terminate(double quantile) {
77
61
        if (_sorted_nums_vec.size() <= 1) {
78
59
            if (_sorted_nums_vec.size() == 1) {
79
1
                _nums = std::move(_sorted_nums_vec[0]);
80
1
            }
81
82
59
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
59
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
24
                pdqsort(_nums.begin(), _nums.end());
90
24
            }
91
92
59
            if (quantile == 1 || _nums.size() == 1) {
93
34
                return _nums.back();
94
34
            }
95
96
25
            double u = (_nums.size() - 1) * quantile;
97
25
            auto index = static_cast<uint32_t>(u);
98
25
            return _nums[index] +
99
25
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
25
                                                       static_cast<double>(_nums[index]));
101
59
        } else {
102
2
            DCHECK(_nums.empty());
103
2
            size_t rows = 0;
104
4
            for (const auto& i : _sorted_nums_vec) {
105
4
                rows += i.size();
106
4
            }
107
2
            const bool reverse = quantile > 0.5 && rows > 2;
108
2
            double u = (rows - 1) * quantile;
109
2
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
2
            size_t target = reverse ? rows - index - 2 : index;
118
2
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
2
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
2
            return first_number +
126
2
                   (u - static_cast<double>(index)) *
127
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
2
        }
129
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
76
300
    double terminate(double quantile) {
77
300
        if (_sorted_nums_vec.size() <= 1) {
78
298
            if (_sorted_nums_vec.size() == 1) {
79
4
                _nums = std::move(_sorted_nums_vec[0]);
80
4
            }
81
82
298
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
298
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
144
                pdqsort(_nums.begin(), _nums.end());
90
144
            }
91
92
298
            if (quantile == 1 || _nums.size() == 1) {
93
87
                return _nums.back();
94
87
            }
95
96
211
            double u = (_nums.size() - 1) * quantile;
97
211
            auto index = static_cast<uint32_t>(u);
98
211
            return _nums[index] +
99
211
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
211
                                                       static_cast<double>(_nums[index]));
101
298
        } else {
102
2
            DCHECK(_nums.empty());
103
2
            size_t rows = 0;
104
4
            for (const auto& i : _sorted_nums_vec) {
105
4
                rows += i.size();
106
4
            }
107
2
            const bool reverse = quantile > 0.5 && rows > 2;
108
2
            double u = (rows - 1) * quantile;
109
2
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
2
            size_t target = reverse ? rows - index - 2 : index;
118
2
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
2
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
2
            return first_number +
126
2
                   (u - static_cast<double>(index)) *
127
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
2
        }
129
300
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
76
444
    double terminate(double quantile) {
77
444
        if (_sorted_nums_vec.size() <= 1) {
78
345
            if (_sorted_nums_vec.size() == 1) {
79
227
                _nums = std::move(_sorted_nums_vec[0]);
80
227
            }
81
82
345
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
345
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
11
                pdqsort(_nums.begin(), _nums.end());
90
11
            }
91
92
345
            if (quantile == 1 || _nums.size() == 1) {
93
239
                return _nums.back();
94
239
            }
95
96
106
            double u = (_nums.size() - 1) * quantile;
97
106
            auto index = static_cast<uint32_t>(u);
98
106
            return _nums[index] +
99
106
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
106
                                                       static_cast<double>(_nums[index]));
101
345
        } else {
102
99
            DCHECK(_nums.empty());
103
99
            size_t rows = 0;
104
271
            for (const auto& i : _sorted_nums_vec) {
105
271
                rows += i.size();
106
271
            }
107
99
            const bool reverse = quantile > 0.5 && rows > 2;
108
99
            double u = (rows - 1) * quantile;
109
99
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
99
            size_t target = reverse ? rows - index - 2 : index;
118
99
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
99
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
99
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
99
            return first_number +
126
99
                   (u - static_cast<double>(index)) *
127
99
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
99
        }
129
444
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
76
128
    double terminate(double quantile) {
77
128
        if (_sorted_nums_vec.size() <= 1) {
78
104
            if (_sorted_nums_vec.size() == 1) {
79
34
                _nums = std::move(_sorted_nums_vec[0]);
80
34
            }
81
82
104
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
104
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
1
                pdqsort(_nums.begin(), _nums.end());
90
1
            }
91
92
104
            if (quantile == 1 || _nums.size() == 1) {
93
59
                return _nums.back();
94
59
            }
95
96
45
            double u = (_nums.size() - 1) * quantile;
97
45
            auto index = static_cast<uint32_t>(u);
98
45
            return _nums[index] +
99
45
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
45
                                                       static_cast<double>(_nums[index]));
101
104
        } else {
102
24
            DCHECK(_nums.empty());
103
24
            size_t rows = 0;
104
72
            for (const auto& i : _sorted_nums_vec) {
105
72
                rows += i.size();
106
72
            }
107
24
            const bool reverse = quantile > 0.5 && rows > 2;
108
24
            double u = (rows - 1) * quantile;
109
24
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
24
            size_t target = reverse ? rows - index - 2 : index;
118
24
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
24
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
24
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
24
            return first_number +
126
24
                   (u - static_cast<double>(index)) *
127
24
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
24
        }
129
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
76
33
    double terminate(double quantile) {
77
33
        if (_sorted_nums_vec.size() <= 1) {
78
31
            if (_sorted_nums_vec.size() == 1) {
79
1
                _nums = std::move(_sorted_nums_vec[0]);
80
1
            }
81
82
31
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
31
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
31
            if (quantile == 1 || _nums.size() == 1) {
93
24
                return _nums.back();
94
24
            }
95
96
7
            double u = (_nums.size() - 1) * quantile;
97
7
            auto index = static_cast<uint32_t>(u);
98
7
            return _nums[index] +
99
7
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
7
                                                       static_cast<double>(_nums[index]));
101
31
        } else {
102
2
            DCHECK(_nums.empty());
103
2
            size_t rows = 0;
104
4
            for (const auto& i : _sorted_nums_vec) {
105
4
                rows += i.size();
106
4
            }
107
2
            const bool reverse = quantile > 0.5 && rows > 2;
108
2
            double u = (rows - 1) * quantile;
109
2
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
2
            size_t target = reverse ? rows - index - 2 : index;
118
2
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
2
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
2
            return first_number +
126
2
                   (u - static_cast<double>(index)) *
127
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
2
        }
129
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
76
3
    double terminate(double quantile) {
77
3
        if (_sorted_nums_vec.size() <= 1) {
78
1
            if (_sorted_nums_vec.size() == 1) {
79
1
                _nums = std::move(_sorted_nums_vec[0]);
80
1
            }
81
82
1
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
1
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
1
            if (quantile == 1 || _nums.size() == 1) {
93
0
                return _nums.back();
94
0
            }
95
96
1
            double u = (_nums.size() - 1) * quantile;
97
1
            auto index = static_cast<uint32_t>(u);
98
1
            return _nums[index] +
99
1
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
1
                                                       static_cast<double>(_nums[index]));
101
2
        } else {
102
2
            DCHECK(_nums.empty());
103
2
            size_t rows = 0;
104
4
            for (const auto& i : _sorted_nums_vec) {
105
4
                rows += i.size();
106
4
            }
107
2
            const bool reverse = quantile > 0.5 && rows > 2;
108
2
            double u = (rows - 1) * quantile;
109
2
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
2
            size_t target = reverse ? rows - index - 2 : index;
118
2
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
2
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
2
            return first_number +
126
2
                   (u - static_cast<double>(index)) *
127
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
2
        }
129
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
76
136
    double terminate(double quantile) {
77
136
        if (_sorted_nums_vec.size() <= 1) {
78
110
            if (_sorted_nums_vec.size() == 1) {
79
27
                _nums = std::move(_sorted_nums_vec[0]);
80
27
            }
81
82
110
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
110
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
110
            if (quantile == 1 || _nums.size() == 1) {
93
30
                return _nums.back();
94
30
            }
95
96
80
            double u = (_nums.size() - 1) * quantile;
97
80
            auto index = static_cast<uint32_t>(u);
98
80
            return _nums[index] +
99
80
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
80
                                                       static_cast<double>(_nums[index]));
101
110
        } else {
102
26
            DCHECK(_nums.empty());
103
26
            size_t rows = 0;
104
64
            for (const auto& i : _sorted_nums_vec) {
105
64
                rows += i.size();
106
64
            }
107
26
            const bool reverse = quantile > 0.5 && rows > 2;
108
26
            double u = (rows - 1) * quantile;
109
26
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
26
            size_t target = reverse ? rows - index - 2 : index;
118
26
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
26
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
26
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
26
            return first_number +
126
26
                   (u - static_cast<double>(index)) *
127
26
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
26
        }
129
136
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
1.41k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIaE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
932
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
392
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsInE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIfE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
137
78
        auto operator<=>(const Node& other) const { return value <=> other.value; }
138
    };
139
140
272
    void _convert_sorted_num_vec_to_nums() {
141
272
        size_t rows = 0;
142
555
        for (const auto& i : _sorted_nums_vec) {
143
555
            rows += i.size();
144
555
        }
145
272
        _nums.resize(rows);
146
272
        size_t count = 0;
147
148
272
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
827
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
555
            if (!_sorted_nums_vec[i].empty()) {
151
555
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
555
            }
153
555
        }
154
155
995
        while (!min_heap.empty()) {
156
723
            Node node = min_heap.top();
157
723
            min_heap.pop();
158
723
            _nums[count++] = node.value;
159
723
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
168
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
168
                min_heap.push(node);
162
168
            }
163
723
        }
164
272
        _sorted_nums_vec.clear();
165
272
    }
Unexecuted instantiation: _ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv
_ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
156
    void _convert_sorted_num_vec_to_nums() {
141
156
        size_t rows = 0;
142
339
        for (const auto& i : _sorted_nums_vec) {
143
339
            rows += i.size();
144
339
        }
145
156
        _nums.resize(rows);
146
156
        size_t count = 0;
147
148
156
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
495
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
339
            if (!_sorted_nums_vec[i].empty()) {
151
339
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
339
            }
153
339
        }
154
155
531
        while (!min_heap.empty()) {
156
375
            Node node = min_heap.top();
157
375
            min_heap.pop();
158
375
            _nums[count++] = node.value;
159
375
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
36
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
36
                min_heap.push(node);
162
36
            }
163
375
        }
164
156
        _sorted_nums_vec.clear();
165
156
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
116
    void _convert_sorted_num_vec_to_nums() {
141
116
        size_t rows = 0;
142
216
        for (const auto& i : _sorted_nums_vec) {
143
216
            rows += i.size();
144
216
        }
145
116
        _nums.resize(rows);
146
116
        size_t count = 0;
147
148
116
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
332
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
216
            if (!_sorted_nums_vec[i].empty()) {
151
216
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
216
            }
153
216
        }
154
155
464
        while (!min_heap.empty()) {
156
348
            Node node = min_heap.top();
157
348
            min_heap.pop();
158
348
            _nums[count++] = node.value;
159
348
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
132
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
132
                min_heap.push(node);
162
132
            }
163
348
        }
164
116
        _sorted_nums_vec.clear();
165
116
    }
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
166
167
157
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
157
        Ty first_number = 0, second_number = 0;
169
157
        size_t count = 0;
170
157
        if (reverse) {
171
34
            std::priority_queue<Node> max_heap;
172
134
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
100
                if (!_sorted_nums_vec[i].empty()) {
174
100
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
100
                                     _sorted_nums_vec[i].size() - 1);
176
100
                }
177
100
            }
178
179
121
            while (!max_heap.empty()) {
180
121
                Node node = max_heap.top();
181
121
                max_heap.pop();
182
121
                if (count == target) {
183
34
                    second_number = node.value;
184
87
                } else if (count == target + 1) {
185
34
                    first_number = node.value;
186
34
                    break;
187
34
                }
188
87
                ++count;
189
87
                if (--node.element_index >= 0) {
190
73
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
73
                    max_heap.push(node);
192
73
                }
193
87
            }
194
195
123
        } else {
196
123
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
446
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
323
                if (!_sorted_nums_vec[i].empty()) {
199
323
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
323
                }
201
323
            }
202
203
339
            while (!min_heap.empty()) {
204
339
                Node node = min_heap.top();
205
339
                min_heap.pop();
206
339
                if (count == target) {
207
123
                    first_number = node.value;
208
216
                } else if (count == target + 1) {
209
123
                    second_number = node.value;
210
123
                    break;
211
123
                }
212
216
                ++count;
213
216
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
131
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
131
                    min_heap.push(node);
216
131
                }
217
216
            }
218
123
        }
219
220
157
        return {first_number, second_number};
221
157
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
99
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
99
        Ty first_number = 0, second_number = 0;
169
99
        size_t count = 0;
170
99
        if (reverse) {
171
11
            std::priority_queue<Node> max_heap;
172
41
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
30
                if (!_sorted_nums_vec[i].empty()) {
174
30
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
30
                                     _sorted_nums_vec[i].size() - 1);
176
30
                }
177
30
            }
178
179
41
            while (!max_heap.empty()) {
180
41
                Node node = max_heap.top();
181
41
                max_heap.pop();
182
41
                if (count == target) {
183
11
                    second_number = node.value;
184
30
                } else if (count == target + 1) {
185
11
                    first_number = node.value;
186
11
                    break;
187
11
                }
188
30
                ++count;
189
30
                if (--node.element_index >= 0) {
190
23
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
23
                    max_heap.push(node);
192
23
                }
193
30
            }
194
195
88
        } else {
196
88
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
329
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
241
                if (!_sorted_nums_vec[i].empty()) {
199
241
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
241
                }
201
241
            }
202
203
247
            while (!min_heap.empty()) {
204
247
                Node node = min_heap.top();
205
247
                min_heap.pop();
206
247
                if (count == target) {
207
88
                    first_number = node.value;
208
159
                } else if (count == target + 1) {
209
88
                    second_number = node.value;
210
88
                    break;
211
88
                }
212
159
                ++count;
213
159
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
99
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
99
                    min_heap.push(node);
216
99
                }
217
159
            }
218
88
        }
219
220
99
        return {first_number, second_number};
221
99
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
24
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
24
        Ty first_number = 0, second_number = 0;
169
24
        size_t count = 0;
170
24
        if (reverse) {
171
16
            std::priority_queue<Node> max_heap;
172
70
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
54
                if (!_sorted_nums_vec[i].empty()) {
174
54
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
54
                                     _sorted_nums_vec[i].size() - 1);
176
54
                }
177
54
            }
178
179
65
            while (!max_heap.empty()) {
180
65
                Node node = max_heap.top();
181
65
                max_heap.pop();
182
65
                if (count == target) {
183
16
                    second_number = node.value;
184
49
                } else if (count == target + 1) {
185
16
                    first_number = node.value;
186
16
                    break;
187
16
                }
188
49
                ++count;
189
49
                if (--node.element_index >= 0) {
190
44
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
44
                    max_heap.push(node);
192
44
                }
193
49
            }
194
195
16
        } else {
196
8
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
26
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
18
                if (!_sorted_nums_vec[i].empty()) {
199
18
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
18
                }
201
18
            }
202
203
25
            while (!min_heap.empty()) {
204
25
                Node node = min_heap.top();
205
25
                min_heap.pop();
206
25
                if (count == target) {
207
8
                    first_number = node.value;
208
17
                } else if (count == target + 1) {
209
8
                    second_number = node.value;
210
8
                    break;
211
8
                }
212
17
                ++count;
213
17
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
15
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
15
                    min_heap.push(node);
216
15
                }
217
17
            }
218
8
        }
219
220
24
        return {first_number, second_number};
221
24
    }
_ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
26
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
26
        Ty first_number = 0, second_number = 0;
169
26
        size_t count = 0;
170
26
        if (reverse) {
171
3
            std::priority_queue<Node> max_heap;
172
11
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
8
                if (!_sorted_nums_vec[i].empty()) {
174
8
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
8
                                     _sorted_nums_vec[i].size() - 1);
176
8
                }
177
8
            }
178
179
7
            while (!max_heap.empty()) {
180
7
                Node node = max_heap.top();
181
7
                max_heap.pop();
182
7
                if (count == target) {
183
3
                    second_number = node.value;
184
4
                } else if (count == target + 1) {
185
3
                    first_number = node.value;
186
3
                    break;
187
3
                }
188
4
                ++count;
189
4
                if (--node.element_index >= 0) {
190
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
2
                    max_heap.push(node);
192
2
                }
193
4
            }
194
195
23
        } else {
196
23
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
79
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
56
                if (!_sorted_nums_vec[i].empty()) {
199
56
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
56
                }
201
56
            }
202
203
55
            while (!min_heap.empty()) {
204
55
                Node node = min_heap.top();
205
55
                min_heap.pop();
206
55
                if (count == target) {
207
23
                    first_number = node.value;
208
32
                } else if (count == target + 1) {
209
23
                    second_number = node.value;
210
23
                    break;
211
23
                }
212
32
                ++count;
213
32
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
13
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
13
                    min_heap.push(node);
216
13
                }
217
32
            }
218
23
        }
219
220
26
        return {first_number, second_number};
221
26
    }
222
223
    PODArray<Ty> _nums;
224
    std::vector<PODArray<Ty>> _sorted_nums_vec;
225
};
226
227
} // namespace doris