Coverage Report

Created: 2025-10-24 10:16

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 "udf/udf.h"
27
#include "vec/common/pod_array.h"
28
#include "vec/common/string_buffer.hpp"
29
#include "vec/io/io_helper.h"
30
31
namespace doris {
32
33
template <typename Ty>
34
class Counts {
35
public:
36
3.41k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
49
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
201
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
2.00k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
808
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
45
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
36
15
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
296
    Counts() = default;
37
38
1.19k
    void merge(Counts* other) {
39
1.19k
        if (other != nullptr && !other->_nums.empty()) {
40
1.19k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.19k
        }
42
1.19k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
38
6
    void merge(Counts* other) {
39
6
        if (other != nullptr && !other->_nums.empty()) {
40
6
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
6
        }
42
6
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
38
9
    void merge(Counts* other) {
39
9
        if (other != nullptr && !other->_nums.empty()) {
40
9
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
9
        }
42
9
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
719
    void merge(Counts* other) {
39
719
        if (other != nullptr && !other->_nums.empty()) {
40
719
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
719
        }
42
719
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
349
    void merge(Counts* other) {
39
349
        if (other != nullptr && !other->_nums.empty()) {
40
349
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
349
        }
42
349
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
38
6
    void merge(Counts* other) {
39
6
        if (other != nullptr && !other->_nums.empty()) {
40
6
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
6
        }
42
6
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
38
6
    void merge(Counts* other) {
39
6
        if (other != nullptr && !other->_nums.empty()) {
40
6
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
6
        }
42
6
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
104
    void merge(Counts* other) {
39
104
        if (other != nullptr && !other->_nums.empty()) {
40
104
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
104
        }
42
104
    }
43
44
    void increment(Ty key, uint32_t i) {
45
        auto old_size = _nums.size();
46
        _nums.resize(_nums.size() + i);
47
        for (uint32_t j = 0; j < i; ++j) {
48
            _nums[old_size + j] = key;
49
        }
50
    }
51
52
2.93k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
52
73
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
52
552
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
52
1.59k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
52
401
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
52
45
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIfE9incrementEf
Line
Count
Source
52
9
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
52
259
    void increment(Ty key) { _nums.push_back(key); }
53
54
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
5
        _nums.insert(keys.begin(), keys.end());
56
5
    }
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_10vectorized8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
_ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Line
Count
Source
54
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
5
        _nums.insert(keys.begin(), keys.end());
56
5
    }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
57
58
1.57k
    void serialize(vectorized::BufferWritable& buf) {
59
1.57k
        if (!_nums.empty()) {
60
1.29k
            pdqsort(_nums.begin(), _nums.end());
61
1.29k
            size_t size = _nums.size();
62
1.29k
            buf.write_binary(size);
63
1.29k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.29k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
282
            _convert_sorted_num_vec_to_nums();
67
282
            serialize(buf);
68
282
        }
69
1.57k
    }
_ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
6
    void serialize(vectorized::BufferWritable& buf) {
59
6
        if (!_nums.empty()) {
60
6
            pdqsort(_nums.begin(), _nums.end());
61
6
            size_t size = _nums.size();
62
6
            buf.write_binary(size);
63
6
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
6
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
6
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
9
    void serialize(vectorized::BufferWritable& buf) {
59
9
        if (!_nums.empty()) {
60
9
            pdqsort(_nums.begin(), _nums.end());
61
9
            size_t size = _nums.size();
62
9
            buf.write_binary(size);
63
9
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
9
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
9
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
992
    void serialize(vectorized::BufferWritable& buf) {
59
992
        if (!_nums.empty()) {
60
836
            pdqsort(_nums.begin(), _nums.end());
61
836
            size_t size = _nums.size();
62
836
            buf.write_binary(size);
63
836
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
836
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
156
            _convert_sorted_num_vec_to_nums();
67
156
            serialize(buf);
68
156
        }
69
992
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
450
    void serialize(vectorized::BufferWritable& buf) {
59
450
        if (!_nums.empty()) {
60
324
            pdqsort(_nums.begin(), _nums.end());
61
324
            size_t size = _nums.size();
62
324
            buf.write_binary(size);
63
324
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
324
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
126
            _convert_sorted_num_vec_to_nums();
67
126
            serialize(buf);
68
126
        }
69
450
    }
_ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
6
    void serialize(vectorized::BufferWritable& buf) {
59
6
        if (!_nums.empty()) {
60
6
            pdqsort(_nums.begin(), _nums.end());
61
6
            size_t size = _nums.size();
62
6
            buf.write_binary(size);
63
6
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
6
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
6
    }
_ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
6
    void serialize(vectorized::BufferWritable& buf) {
59
6
        if (!_nums.empty()) {
60
6
            pdqsort(_nums.begin(), _nums.end());
61
6
            size_t size = _nums.size();
62
6
            buf.write_binary(size);
63
6
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
6
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
6
    }
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
104
    void serialize(vectorized::BufferWritable& buf) {
59
104
        if (!_nums.empty()) {
60
104
            pdqsort(_nums.begin(), _nums.end());
61
104
            size_t size = _nums.size();
62
104
            buf.write_binary(size);
63
104
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
104
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
104
    }
70
71
1.19k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.19k
        size_t size;
73
1.19k
        buf.read_binary(size);
74
1.19k
        _nums.resize(size);
75
1.19k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.19k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.19k
    }
_ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
6
    void unserialize(vectorized::BufferReadable& buf) {
72
6
        size_t size;
73
6
        buf.read_binary(size);
74
6
        _nums.resize(size);
75
6
        auto buff = buf.read(sizeof(Ty) * size);
76
6
        memcpy(_nums.data(), buff.data, buff.size);
77
6
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
9
    void unserialize(vectorized::BufferReadable& buf) {
72
9
        size_t size;
73
9
        buf.read_binary(size);
74
9
        _nums.resize(size);
75
9
        auto buff = buf.read(sizeof(Ty) * size);
76
9
        memcpy(_nums.data(), buff.data, buff.size);
77
9
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
719
    void unserialize(vectorized::BufferReadable& buf) {
72
719
        size_t size;
73
719
        buf.read_binary(size);
74
719
        _nums.resize(size);
75
719
        auto buff = buf.read(sizeof(Ty) * size);
76
719
        memcpy(_nums.data(), buff.data, buff.size);
77
719
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
349
    void unserialize(vectorized::BufferReadable& buf) {
72
349
        size_t size;
73
349
        buf.read_binary(size);
74
349
        _nums.resize(size);
75
349
        auto buff = buf.read(sizeof(Ty) * size);
76
349
        memcpy(_nums.data(), buff.data, buff.size);
77
349
    }
_ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
6
    void unserialize(vectorized::BufferReadable& buf) {
72
6
        size_t size;
73
6
        buf.read_binary(size);
74
6
        _nums.resize(size);
75
6
        auto buff = buf.read(sizeof(Ty) * size);
76
6
        memcpy(_nums.data(), buff.data, buff.size);
77
6
    }
_ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
6
    void unserialize(vectorized::BufferReadable& buf) {
72
6
        size_t size;
73
6
        buf.read_binary(size);
74
6
        _nums.resize(size);
75
6
        auto buff = buf.read(sizeof(Ty) * size);
76
6
        memcpy(_nums.data(), buff.data, buff.size);
77
6
    }
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
104
    void unserialize(vectorized::BufferReadable& buf) {
72
104
        size_t size;
73
104
        buf.read_binary(size);
74
104
        _nums.resize(size);
75
104
        auto buff = buf.read(sizeof(Ty) * size);
76
104
        memcpy(_nums.data(), buff.data, buff.size);
77
104
    }
78
79
1.10k
    double terminate(double quantile) {
80
1.10k
        if (_sorted_nums_vec.size() <= 1) {
81
1.03k
            if (_sorted_nums_vec.size() == 1) {
82
384
                _nums = std::move(_sorted_nums_vec[0]);
83
384
            }
84
85
1.03k
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
1.03k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
180
                pdqsort(_nums.begin(), _nums.end());
93
180
            }
94
95
1.03k
            if (quantile == 1 || _nums.size() == 1) {
96
473
                return _nums.back();
97
473
            }
98
99
564
            double u = (_nums.size() - 1) * quantile;
100
564
            auto index = static_cast<uint32_t>(u);
101
564
            return _nums[index] +
102
564
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
564
                                                       static_cast<double>(_nums[index]));
104
1.03k
        } else {
105
68
            DCHECK(_nums.empty());
106
68
            size_t rows = 0;
107
198
            for (const auto& i : _sorted_nums_vec) {
108
198
                rows += i.size();
109
198
            }
110
68
            const bool reverse = quantile > 0.5 && rows > 2;
111
68
            double u = (rows - 1) * quantile;
112
68
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
68
            size_t target = reverse ? rows - index - 2 : index;
121
68
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
68
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
68
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
68
            return first_number +
129
68
                   (u - static_cast<double>(index)) *
130
68
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
68
        }
132
1.10k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
79
61
    double terminate(double quantile) {
80
61
        if (_sorted_nums_vec.size() <= 1) {
81
58
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
58
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
58
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
24
                pdqsort(_nums.begin(), _nums.end());
93
24
            }
94
95
58
            if (quantile == 1 || _nums.size() == 1) {
96
34
                return _nums.back();
97
34
            }
98
99
24
            double u = (_nums.size() - 1) * quantile;
100
24
            auto index = static_cast<uint32_t>(u);
101
24
            return _nums[index] +
102
24
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
24
                                                       static_cast<double>(_nums[index]));
104
58
        } else {
105
3
            DCHECK(_nums.empty());
106
3
            size_t rows = 0;
107
6
            for (const auto& i : _sorted_nums_vec) {
108
6
                rows += i.size();
109
6
            }
110
3
            const bool reverse = quantile > 0.5 && rows > 2;
111
3
            double u = (rows - 1) * quantile;
112
3
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
3
            size_t target = reverse ? rows - index - 2 : index;
121
3
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
3
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
3
            return first_number +
129
3
                   (u - static_cast<double>(index)) *
130
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
3
        }
132
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
79
300
    double terminate(double quantile) {
80
300
        if (_sorted_nums_vec.size() <= 1) {
81
297
            if (_sorted_nums_vec.size() == 1) {
82
3
                _nums = std::move(_sorted_nums_vec[0]);
83
3
            }
84
85
297
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
297
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
144
                pdqsort(_nums.begin(), _nums.end());
93
144
            }
94
95
297
            if (quantile == 1 || _nums.size() == 1) {
96
87
                return _nums.back();
97
87
            }
98
99
210
            double u = (_nums.size() - 1) * quantile;
100
210
            auto index = static_cast<uint32_t>(u);
101
210
            return _nums[index] +
102
210
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
210
                                                       static_cast<double>(_nums[index]));
104
297
        } else {
105
3
            DCHECK(_nums.empty());
106
3
            size_t rows = 0;
107
6
            for (const auto& i : _sorted_nums_vec) {
108
6
                rows += i.size();
109
6
            }
110
3
            const bool reverse = quantile > 0.5 && rows > 2;
111
3
            double u = (rows - 1) * quantile;
112
3
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
3
            size_t target = reverse ? rows - index - 2 : index;
121
3
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
3
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
3
            return first_number +
129
3
                   (u - static_cast<double>(index)) *
130
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
3
        }
132
300
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
444
    double terminate(double quantile) {
80
444
        if (_sorted_nums_vec.size() <= 1) {
81
424
            if (_sorted_nums_vec.size() == 1) {
82
306
                _nums = std::move(_sorted_nums_vec[0]);
83
306
            }
84
85
424
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
424
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
11
                pdqsort(_nums.begin(), _nums.end());
93
11
            }
94
95
424
            if (quantile == 1 || _nums.size() == 1) {
96
239
                return _nums.back();
97
239
            }
98
99
185
            double u = (_nums.size() - 1) * quantile;
100
185
            auto index = static_cast<uint32_t>(u);
101
185
            return _nums[index] +
102
185
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
185
                                                       static_cast<double>(_nums[index]));
104
424
        } else {
105
20
            DCHECK(_nums.empty());
106
20
            size_t rows = 0;
107
74
            for (const auto& i : _sorted_nums_vec) {
108
74
                rows += i.size();
109
74
            }
110
20
            const bool reverse = quantile > 0.5 && rows > 2;
111
20
            double u = (rows - 1) * quantile;
112
20
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
20
            size_t target = reverse ? rows - index - 2 : index;
121
20
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
20
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
20
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
20
            return first_number +
129
20
                   (u - static_cast<double>(index)) *
130
20
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
20
        }
132
444
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
128
    double terminate(double quantile) {
80
128
        if (_sorted_nums_vec.size() <= 1) {
81
119
            if (_sorted_nums_vec.size() == 1) {
82
49
                _nums = std::move(_sorted_nums_vec[0]);
83
49
            }
84
85
119
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
119
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
119
            if (quantile == 1 || _nums.size() == 1) {
96
59
                return _nums.back();
97
59
            }
98
99
60
            double u = (_nums.size() - 1) * quantile;
100
60
            auto index = static_cast<uint32_t>(u);
101
60
            return _nums[index] +
102
60
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
60
                                                       static_cast<double>(_nums[index]));
104
119
        } else {
105
9
            DCHECK(_nums.empty());
106
9
            size_t rows = 0;
107
22
            for (const auto& i : _sorted_nums_vec) {
108
22
                rows += i.size();
109
22
            }
110
9
            const bool reverse = quantile > 0.5 && rows > 2;
111
9
            double u = (rows - 1) * quantile;
112
9
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
9
            size_t target = reverse ? rows - index - 2 : index;
121
9
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
9
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
9
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
9
            return first_number +
129
9
                   (u - static_cast<double>(index)) *
130
9
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
9
        }
132
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
79
33
    double terminate(double quantile) {
80
33
        if (_sorted_nums_vec.size() <= 1) {
81
30
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
30
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
30
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
30
            if (quantile == 1 || _nums.size() == 1) {
96
24
                return _nums.back();
97
24
            }
98
99
6
            double u = (_nums.size() - 1) * quantile;
100
6
            auto index = static_cast<uint32_t>(u);
101
6
            return _nums[index] +
102
6
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
6
                                                       static_cast<double>(_nums[index]));
104
30
        } else {
105
3
            DCHECK(_nums.empty());
106
3
            size_t rows = 0;
107
6
            for (const auto& i : _sorted_nums_vec) {
108
6
                rows += i.size();
109
6
            }
110
3
            const bool reverse = quantile > 0.5 && rows > 2;
111
3
            double u = (rows - 1) * quantile;
112
3
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
3
            size_t target = reverse ? rows - index - 2 : index;
121
3
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
3
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
3
            return first_number +
129
3
                   (u - static_cast<double>(index)) *
130
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
3
        }
132
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
79
3
    double terminate(double quantile) {
80
3
        if (_sorted_nums_vec.size() <= 1) {
81
0
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
0
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
0
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
0
            if (quantile == 1 || _nums.size() == 1) {
96
0
                return _nums.back();
97
0
            }
98
99
0
            double u = (_nums.size() - 1) * quantile;
100
0
            auto index = static_cast<uint32_t>(u);
101
0
            return _nums[index] +
102
0
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
0
                                                       static_cast<double>(_nums[index]));
104
3
        } else {
105
3
            DCHECK(_nums.empty());
106
3
            size_t rows = 0;
107
6
            for (const auto& i : _sorted_nums_vec) {
108
6
                rows += i.size();
109
6
            }
110
3
            const bool reverse = quantile > 0.5 && rows > 2;
111
3
            double u = (rows - 1) * quantile;
112
3
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
3
            size_t target = reverse ? rows - index - 2 : index;
121
3
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
3
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
3
            return first_number +
129
3
                   (u - static_cast<double>(index)) *
130
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
3
        }
132
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
79
136
    double terminate(double quantile) {
80
136
        if (_sorted_nums_vec.size() <= 1) {
81
109
            if (_sorted_nums_vec.size() == 1) {
82
26
                _nums = std::move(_sorted_nums_vec[0]);
83
26
            }
84
85
109
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
109
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
109
            if (quantile == 1 || _nums.size() == 1) {
96
30
                return _nums.back();
97
30
            }
98
99
79
            double u = (_nums.size() - 1) * quantile;
100
79
            auto index = static_cast<uint32_t>(u);
101
79
            return _nums[index] +
102
79
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
79
                                                       static_cast<double>(_nums[index]));
104
109
        } else {
105
27
            DCHECK(_nums.empty());
106
27
            size_t rows = 0;
107
78
            for (const auto& i : _sorted_nums_vec) {
108
78
                rows += i.size();
109
78
            }
110
27
            const bool reverse = quantile > 0.5 && rows > 2;
111
27
            double u = (rows - 1) * quantile;
112
27
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
27
            size_t target = reverse ? rows - index - 2 : index;
121
27
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
27
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
27
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
27
            return first_number +
129
27
                   (u - static_cast<double>(index)) *
130
27
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
27
        }
132
136
    }
133
134
private:
135
    struct Node {
136
        Ty value;
137
        int array_index;
138
        int64_t element_index;
139
140
1.09k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIaE4NodessERKS2_
Line
Count
Source
140
6
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
140
6
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
140
587
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
140
391
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsInE4NodessERKS2_
Line
Count
Source
140
6
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIfE4NodessERKS2_
Line
Count
Source
140
6
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
140
93
        auto operator<=>(const Node& other) const { return value <=> other.value; }
141
    };
142
143
282
    void _convert_sorted_num_vec_to_nums() {
144
282
        size_t rows = 0;
145
617
        for (const auto& i : _sorted_nums_vec) {
146
617
            rows += i.size();
147
617
        }
148
282
        _nums.resize(rows);
149
282
        size_t count = 0;
150
151
282
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
899
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
617
            if (!_sorted_nums_vec[i].empty()) {
154
617
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
617
            }
156
617
        }
157
158
1.01k
        while (!min_heap.empty()) {
159
729
            Node node = min_heap.top();
160
729
            min_heap.pop();
161
729
            _nums[count++] = node.value;
162
729
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
112
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
112
                min_heap.push(node);
165
112
            }
166
729
        }
167
282
        _sorted_nums_vec.clear();
168
282
    }
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
143
156
    void _convert_sorted_num_vec_to_nums() {
144
156
        size_t rows = 0;
145
339
        for (const auto& i : _sorted_nums_vec) {
146
339
            rows += i.size();
147
339
        }
148
156
        _nums.resize(rows);
149
156
        size_t count = 0;
150
151
156
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
495
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
339
            if (!_sorted_nums_vec[i].empty()) {
154
339
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
339
            }
156
339
        }
157
158
531
        while (!min_heap.empty()) {
159
375
            Node node = min_heap.top();
160
375
            min_heap.pop();
161
375
            _nums[count++] = node.value;
162
375
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
36
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
36
                min_heap.push(node);
165
36
            }
166
375
        }
167
156
        _sorted_nums_vec.clear();
168
156
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
143
126
    void _convert_sorted_num_vec_to_nums() {
144
126
        size_t rows = 0;
145
278
        for (const auto& i : _sorted_nums_vec) {
146
278
            rows += i.size();
147
278
        }
148
126
        _nums.resize(rows);
149
126
        size_t count = 0;
150
151
126
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
404
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
278
            if (!_sorted_nums_vec[i].empty()) {
154
278
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
278
            }
156
278
        }
157
158
480
        while (!min_heap.empty()) {
159
354
            Node node = min_heap.top();
160
354
            min_heap.pop();
161
354
            _nums[count++] = node.value;
162
354
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
76
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
76
                min_heap.push(node);
165
76
            }
166
354
        }
167
126
        _sorted_nums_vec.clear();
168
126
    }
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
169
170
68
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
68
        Ty first_number = 0, second_number = 0;
172
68
        size_t count = 0;
173
68
        if (reverse) {
174
14
            std::priority_queue<Node> max_heap;
175
54
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
40
                if (!_sorted_nums_vec[i].empty()) {
177
40
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
40
                                     _sorted_nums_vec[i].size() - 1);
179
40
                }
180
40
            }
181
182
53
            while (!max_heap.empty()) {
183
53
                Node node = max_heap.top();
184
53
                max_heap.pop();
185
53
                if (count == target) {
186
14
                    second_number = node.value;
187
39
                } else if (count == target + 1) {
188
14
                    first_number = node.value;
189
14
                    break;
190
14
                }
191
39
                ++count;
192
39
                if (--node.element_index >= 0) {
193
31
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
31
                    max_heap.push(node);
195
31
                }
196
39
            }
197
198
54
        } else {
199
54
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
212
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
158
                if (!_sorted_nums_vec[i].empty()) {
202
158
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
158
                }
204
158
            }
205
206
170
            while (!min_heap.empty()) {
207
170
                Node node = min_heap.top();
208
170
                min_heap.pop();
209
170
                if (count == target) {
210
54
                    first_number = node.value;
211
116
                } else if (count == target + 1) {
212
54
                    second_number = node.value;
213
54
                    break;
214
54
                }
215
116
                ++count;
216
116
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
67
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
67
                    min_heap.push(node);
219
67
                }
220
116
            }
221
54
        }
222
223
68
        return {first_number, second_number};
224
68
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
3
        Ty first_number = 0, second_number = 0;
172
3
        size_t count = 0;
173
3
        if (reverse) {
174
1
            std::priority_queue<Node> max_heap;
175
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
2
                if (!_sorted_nums_vec[i].empty()) {
177
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
2
                                     _sorted_nums_vec[i].size() - 1);
179
2
                }
180
2
            }
181
182
2
            while (!max_heap.empty()) {
183
2
                Node node = max_heap.top();
184
2
                max_heap.pop();
185
2
                if (count == target) {
186
1
                    second_number = node.value;
187
1
                } else if (count == target + 1) {
188
1
                    first_number = node.value;
189
1
                    break;
190
1
                }
191
1
                ++count;
192
1
                if (--node.element_index >= 0) {
193
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
1
                    max_heap.push(node);
195
1
                }
196
1
            }
197
198
2
        } else {
199
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
4
                if (!_sorted_nums_vec[i].empty()) {
202
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
4
                }
204
4
            }
205
206
6
            while (!min_heap.empty()) {
207
6
                Node node = min_heap.top();
208
6
                min_heap.pop();
209
6
                if (count == target) {
210
2
                    first_number = node.value;
211
4
                } else if (count == target + 1) {
212
2
                    second_number = node.value;
213
2
                    break;
214
2
                }
215
4
                ++count;
216
4
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
2
                    min_heap.push(node);
219
2
                }
220
4
            }
221
2
        }
222
223
3
        return {first_number, second_number};
224
3
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
3
        Ty first_number = 0, second_number = 0;
172
3
        size_t count = 0;
173
3
        if (reverse) {
174
1
            std::priority_queue<Node> max_heap;
175
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
2
                if (!_sorted_nums_vec[i].empty()) {
177
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
2
                                     _sorted_nums_vec[i].size() - 1);
179
2
                }
180
2
            }
181
182
2
            while (!max_heap.empty()) {
183
2
                Node node = max_heap.top();
184
2
                max_heap.pop();
185
2
                if (count == target) {
186
1
                    second_number = node.value;
187
1
                } else if (count == target + 1) {
188
1
                    first_number = node.value;
189
1
                    break;
190
1
                }
191
1
                ++count;
192
1
                if (--node.element_index >= 0) {
193
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
1
                    max_heap.push(node);
195
1
                }
196
1
            }
197
198
2
        } else {
199
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
4
                if (!_sorted_nums_vec[i].empty()) {
202
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
4
                }
204
4
            }
205
206
6
            while (!min_heap.empty()) {
207
6
                Node node = min_heap.top();
208
6
                min_heap.pop();
209
6
                if (count == target) {
210
2
                    first_number = node.value;
211
4
                } else if (count == target + 1) {
212
2
                    second_number = node.value;
213
2
                    break;
214
2
                }
215
4
                ++count;
216
4
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
2
                    min_heap.push(node);
219
2
                }
220
4
            }
221
2
        }
222
223
3
        return {first_number, second_number};
224
3
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
20
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
20
        Ty first_number = 0, second_number = 0;
172
20
        size_t count = 0;
173
20
        if (reverse) {
174
4
            std::priority_queue<Node> max_heap;
175
18
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
14
                if (!_sorted_nums_vec[i].empty()) {
177
14
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
14
                                     _sorted_nums_vec[i].size() - 1);
179
14
                }
180
14
            }
181
182
26
            while (!max_heap.empty()) {
183
26
                Node node = max_heap.top();
184
26
                max_heap.pop();
185
26
                if (count == target) {
186
4
                    second_number = node.value;
187
22
                } else if (count == target + 1) {
188
4
                    first_number = node.value;
189
4
                    break;
190
4
                }
191
22
                ++count;
192
22
                if (--node.element_index >= 0) {
193
19
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
19
                    max_heap.push(node);
195
19
                }
196
22
            }
197
198
16
        } else {
199
16
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
76
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
60
                if (!_sorted_nums_vec[i].empty()) {
202
60
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
60
                }
204
60
            }
205
206
66
            while (!min_heap.empty()) {
207
66
                Node node = min_heap.top();
208
66
                min_heap.pop();
209
66
                if (count == target) {
210
16
                    first_number = node.value;
211
50
                } else if (count == target + 1) {
212
16
                    second_number = node.value;
213
16
                    break;
214
16
                }
215
50
                ++count;
216
50
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
43
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
43
                    min_heap.push(node);
219
43
                }
220
50
            }
221
16
        }
222
223
20
        return {first_number, second_number};
224
20
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
9
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
9
        Ty first_number = 0, second_number = 0;
172
9
        size_t count = 0;
173
9
        if (reverse) {
174
3
            std::priority_queue<Node> max_heap;
175
11
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
8
                if (!_sorted_nums_vec[i].empty()) {
177
8
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
8
                                     _sorted_nums_vec[i].size() - 1);
179
8
                }
180
8
            }
181
182
12
            while (!max_heap.empty()) {
183
12
                Node node = max_heap.top();
184
12
                max_heap.pop();
185
12
                if (count == target) {
186
3
                    second_number = node.value;
187
9
                } else if (count == target + 1) {
188
3
                    first_number = node.value;
189
3
                    break;
190
3
                }
191
9
                ++count;
192
9
                if (--node.element_index >= 0) {
193
8
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
8
                    max_heap.push(node);
195
8
                }
196
9
            }
197
198
6
        } else {
199
6
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
20
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
14
                if (!_sorted_nums_vec[i].empty()) {
202
14
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
14
                }
204
14
            }
205
206
22
            while (!min_heap.empty()) {
207
22
                Node node = min_heap.top();
208
22
                min_heap.pop();
209
22
                if (count == target) {
210
6
                    first_number = node.value;
211
16
                } else if (count == target + 1) {
212
6
                    second_number = node.value;
213
6
                    break;
214
6
                }
215
16
                ++count;
216
16
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
14
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
14
                    min_heap.push(node);
219
14
                }
220
16
            }
221
6
        }
222
223
9
        return {first_number, second_number};
224
9
    }
_ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
3
        Ty first_number = 0, second_number = 0;
172
3
        size_t count = 0;
173
3
        if (reverse) {
174
1
            std::priority_queue<Node> max_heap;
175
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
2
                if (!_sorted_nums_vec[i].empty()) {
177
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
2
                                     _sorted_nums_vec[i].size() - 1);
179
2
                }
180
2
            }
181
182
2
            while (!max_heap.empty()) {
183
2
                Node node = max_heap.top();
184
2
                max_heap.pop();
185
2
                if (count == target) {
186
1
                    second_number = node.value;
187
1
                } else if (count == target + 1) {
188
1
                    first_number = node.value;
189
1
                    break;
190
1
                }
191
1
                ++count;
192
1
                if (--node.element_index >= 0) {
193
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
1
                    max_heap.push(node);
195
1
                }
196
1
            }
197
198
2
        } else {
199
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
4
                if (!_sorted_nums_vec[i].empty()) {
202
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
4
                }
204
4
            }
205
206
6
            while (!min_heap.empty()) {
207
6
                Node node = min_heap.top();
208
6
                min_heap.pop();
209
6
                if (count == target) {
210
2
                    first_number = node.value;
211
4
                } else if (count == target + 1) {
212
2
                    second_number = node.value;
213
2
                    break;
214
2
                }
215
4
                ++count;
216
4
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
2
                    min_heap.push(node);
219
2
                }
220
4
            }
221
2
        }
222
223
3
        return {first_number, second_number};
224
3
    }
_ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
3
        Ty first_number = 0, second_number = 0;
172
3
        size_t count = 0;
173
3
        if (reverse) {
174
1
            std::priority_queue<Node> max_heap;
175
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
2
                if (!_sorted_nums_vec[i].empty()) {
177
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
2
                                     _sorted_nums_vec[i].size() - 1);
179
2
                }
180
2
            }
181
182
2
            while (!max_heap.empty()) {
183
2
                Node node = max_heap.top();
184
2
                max_heap.pop();
185
2
                if (count == target) {
186
1
                    second_number = node.value;
187
1
                } else if (count == target + 1) {
188
1
                    first_number = node.value;
189
1
                    break;
190
1
                }
191
1
                ++count;
192
1
                if (--node.element_index >= 0) {
193
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
1
                    max_heap.push(node);
195
1
                }
196
1
            }
197
198
2
        } else {
199
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
4
                if (!_sorted_nums_vec[i].empty()) {
202
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
4
                }
204
4
            }
205
206
6
            while (!min_heap.empty()) {
207
6
                Node node = min_heap.top();
208
6
                min_heap.pop();
209
6
                if (count == target) {
210
2
                    first_number = node.value;
211
4
                } else if (count == target + 1) {
212
2
                    second_number = node.value;
213
2
                    break;
214
2
                }
215
4
                ++count;
216
4
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
2
                    min_heap.push(node);
219
2
                }
220
4
            }
221
2
        }
222
223
3
        return {first_number, second_number};
224
3
    }
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
27
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
27
        Ty first_number = 0, second_number = 0;
172
27
        size_t count = 0;
173
27
        if (reverse) {
174
3
            std::priority_queue<Node> max_heap;
175
13
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
10
                if (!_sorted_nums_vec[i].empty()) {
177
10
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
10
                                     _sorted_nums_vec[i].size() - 1);
179
10
                }
180
10
            }
181
182
7
            while (!max_heap.empty()) {
183
7
                Node node = max_heap.top();
184
7
                max_heap.pop();
185
7
                if (count == target) {
186
3
                    second_number = node.value;
187
4
                } else if (count == target + 1) {
188
3
                    first_number = node.value;
189
3
                    break;
190
3
                }
191
4
                ++count;
192
4
                if (--node.element_index >= 0) {
193
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
0
                    max_heap.push(node);
195
0
                }
196
4
            }
197
198
24
        } else {
199
24
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
92
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
68
                if (!_sorted_nums_vec[i].empty()) {
202
68
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
68
                }
204
68
            }
205
206
58
            while (!min_heap.empty()) {
207
58
                Node node = min_heap.top();
208
58
                min_heap.pop();
209
58
                if (count == target) {
210
24
                    first_number = node.value;
211
34
                } else if (count == target + 1) {
212
24
                    second_number = node.value;
213
24
                    break;
214
24
                }
215
34
                ++count;
216
34
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
2
                    min_heap.push(node);
219
2
                }
220
34
            }
221
24
        }
222
223
27
        return {first_number, second_number};
224
27
    }
225
226
    vectorized::PODArray<Ty> _nums;
227
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
228
};
229
230
} // namespace doris