Coverage Report

Created: 2026-01-23 11:23

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