Coverage Report

Created: 2026-05-14 04:42

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
564
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
15
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
134
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
32
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
304
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
15
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
15
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
49
    Counts() = default;
36
37
214
    void merge(Counts* other) {
38
214
        if (other != nullptr && !other->_nums.empty()) {
39
214
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
214
        }
41
214
    }
_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
15
    void merge(Counts* other) {
38
15
        if (other != nullptr && !other->_nums.empty()) {
39
15
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
15
        }
41
15
    }
_ZN5doris6CountsIiE5mergeEPS1_
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
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
164
    void merge(Counts* other) {
38
164
        if (other != nullptr && !other->_nums.empty()) {
39
164
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
164
        }
41
164
    }
_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
11
    void merge(Counts* other) {
38
11
        if (other != nullptr && !other->_nums.empty()) {
39
11
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
11
        }
41
11
    }
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
569
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
51
9
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
51
362
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
51
27
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
51
76
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
51
9
    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
77
    void increment(Ty key) { _nums.push_back(key); }
52
53
0
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
54
0
        _nums.insert(keys.begin(), keys.end());
55
0
    }
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_10vectorized8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
56
57
217
    void serialize(vectorized::BufferWritable& buf) {
58
217
        if (!_nums.empty()) {
59
150
            pdqsort(_nums.begin(), _nums.end());
60
150
            size_t size = _nums.size();
61
150
            buf.write_binary(size);
62
150
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
150
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
67
            _convert_sorted_num_vec_to_nums();
66
67
            serialize(buf);
67
67
        }
68
217
    }
_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
15
    void serialize(vectorized::BufferWritable& buf) {
58
15
        if (!_nums.empty()) {
59
15
            pdqsort(_nums.begin(), _nums.end());
60
15
            size_t size = _nums.size();
61
15
            buf.write_binary(size);
62
15
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
15
        } 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
15
    }
_ZN5doris6CountsIiE9serializeERNS_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
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
167
    void serialize(vectorized::BufferWritable& buf) {
58
167
        if (!_nums.empty()) {
59
100
            pdqsort(_nums.begin(), _nums.end());
60
100
            size_t size = _nums.size();
61
100
            buf.write_binary(size);
62
100
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
100
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
67
            _convert_sorted_num_vec_to_nums();
66
67
            serialize(buf);
67
67
        }
68
167
    }
_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
11
    void serialize(vectorized::BufferWritable& buf) {
58
11
        if (!_nums.empty()) {
59
11
            pdqsort(_nums.begin(), _nums.end());
60
11
            size_t size = _nums.size();
61
11
            buf.write_binary(size);
62
11
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
11
        } 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
11
    }
69
70
214
    void unserialize(vectorized::BufferReadable& buf) {
71
214
        size_t size;
72
214
        buf.read_binary(size);
73
214
        _nums.resize(size);
74
214
        auto buff = buf.read(sizeof(Ty) * size);
75
214
        memcpy(_nums.data(), buff.data, buff.size);
76
214
    }
_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
15
    void unserialize(vectorized::BufferReadable& buf) {
71
15
        size_t size;
72
15
        buf.read_binary(size);
73
15
        _nums.resize(size);
74
15
        auto buff = buf.read(sizeof(Ty) * size);
75
15
        memcpy(_nums.data(), buff.data, buff.size);
76
15
    }
_ZN5doris6CountsIiE11unserializeERNS_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
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
164
    void unserialize(vectorized::BufferReadable& buf) {
71
164
        size_t size;
72
164
        buf.read_binary(size);
73
164
        _nums.resize(size);
74
164
        auto buff = buf.read(sizeof(Ty) * size);
75
164
        memcpy(_nums.data(), buff.data, buff.size);
76
164
    }
_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
11
    void unserialize(vectorized::BufferReadable& buf) {
71
11
        size_t size;
72
11
        buf.read_binary(size);
73
11
        _nums.resize(size);
74
11
        auto buff = buf.read(sizeof(Ty) * size);
75
11
        memcpy(_nums.data(), buff.data, buff.size);
76
11
    }
77
78
265
    double terminate(double quantile) {
79
265
        if (_sorted_nums_vec.size() <= 1) {
80
240
            if (_sorted_nums_vec.size() == 1) {
81
32
                _nums = std::move(_sorted_nums_vec[0]);
82
32
            }
83
84
240
            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
240
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
97
                pdqsort(_nums.begin(), _nums.end());
92
97
            }
93
94
240
            if (quantile == 1 || _nums.size() == 1) {
95
66
                return _nums.back();
96
66
            }
97
98
174
            double u = (_nums.size() - 1) * quantile;
99
174
            auto index = static_cast<uint32_t>(u);
100
174
            return _nums[index] +
101
174
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
174
                                                       static_cast<double>(_nums[index]));
103
240
        } else {
104
25
            DCHECK(_nums.empty());
105
25
            size_t rows = 0;
106
53
            for (const auto& i : _sorted_nums_vec) {
107
53
                rows += i.size();
108
53
            }
109
25
            const bool reverse = quantile > 0.5 && rows > 2;
110
25
            double u = (rows - 1) * quantile;
111
25
            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
25
            size_t target = reverse ? rows - index - 2 : index;
120
25
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
25
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
25
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
25
            return first_number +
128
25
                   (u - static_cast<double>(index)) *
129
25
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
25
        }
131
265
    }
_ZN5doris6CountsIaE9terminateEd
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
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
78
182
    double terminate(double quantile) {
79
182
        if (_sorted_nums_vec.size() <= 1) {
80
176
            if (_sorted_nums_vec.size() == 1) {
81
0
                _nums = std::move(_sorted_nums_vec[0]);
82
0
            }
83
84
176
            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
176
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
96
                pdqsort(_nums.begin(), _nums.end());
92
96
            }
93
94
176
            if (quantile == 1 || _nums.size() == 1) {
95
42
                return _nums.back();
96
42
            }
97
98
134
            double u = (_nums.size() - 1) * quantile;
99
134
            auto index = static_cast<uint32_t>(u);
100
134
            return _nums[index] +
101
134
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
134
                                                       static_cast<double>(_nums[index]));
103
176
        } else {
104
6
            DCHECK(_nums.empty());
105
6
            size_t rows = 0;
106
15
            for (const auto& i : _sorted_nums_vec) {
107
15
                rows += i.size();
108
15
            }
109
6
            const bool reverse = quantile > 0.5 && rows > 2;
110
6
            double u = (rows - 1) * quantile;
111
6
            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
6
            size_t target = reverse ? rows - index - 2 : index;
120
6
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
6
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
6
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
6
            return first_number +
128
6
                   (u - static_cast<double>(index)) *
129
6
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
6
        }
131
182
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
78
18
    double terminate(double quantile) {
79
18
        if (_sorted_nums_vec.size() <= 1) {
80
15
            if (_sorted_nums_vec.size() == 1) {
81
0
                _nums = std::move(_sorted_nums_vec[0]);
82
0
            }
83
84
15
            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
15
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
15
            if (quantile == 1 || _nums.size() == 1) {
95
12
                return _nums.back();
96
12
            }
97
98
3
            double u = (_nums.size() - 1) * quantile;
99
3
            auto index = static_cast<uint32_t>(u);
100
3
            return _nums[index] +
101
3
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
3
                                                       static_cast<double>(_nums[index]));
103
15
        } 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
18
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
78
33
    double terminate(double quantile) {
79
33
        if (_sorted_nums_vec.size() <= 1) {
80
29
            if (_sorted_nums_vec.size() == 1) {
81
27
                _nums = std::move(_sorted_nums_vec[0]);
82
27
            }
83
84
29
            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
29
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
1
                pdqsort(_nums.begin(), _nums.end());
92
1
            }
93
94
29
            if (quantile == 1 || _nums.size() == 1) {
95
0
                return _nums.back();
96
0
            }
97
98
29
            double u = (_nums.size() - 1) * quantile;
99
29
            auto index = static_cast<uint32_t>(u);
100
29
            return _nums[index] +
101
29
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
29
                                                       static_cast<double>(_nums[index]));
103
29
        } else {
104
4
            DCHECK(_nums.empty());
105
4
            size_t rows = 0;
106
8
            for (const auto& i : _sorted_nums_vec) {
107
8
                rows += i.size();
108
8
            }
109
4
            const bool reverse = quantile > 0.5 && rows > 2;
110
4
            double u = (rows - 1) * quantile;
111
4
            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
4
            size_t target = reverse ? rows - index - 2 : index;
120
4
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
4
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
4
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
4
            return first_number +
128
4
                   (u - static_cast<double>(index)) *
129
4
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
4
        }
131
33
    }
_ZN5doris6CountsInE9terminateEd
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
    }
_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
23
    double terminate(double quantile) {
79
23
        if (_sorted_nums_vec.size() <= 1) {
80
20
            if (_sorted_nums_vec.size() == 1) {
81
5
                _nums = std::move(_sorted_nums_vec[0]);
82
5
            }
83
84
20
            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
20
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
20
            if (quantile == 1 || _nums.size() == 1) {
95
12
                return _nums.back();
96
12
            }
97
98
8
            double u = (_nums.size() - 1) * quantile;
99
8
            auto index = static_cast<uint32_t>(u);
100
8
            return _nums[index] +
101
8
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
8
                                                       static_cast<double>(_nums[index]));
103
20
        } 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
23
    }
132
133
private:
134
    struct Node {
135
        Ty value;
136
        int array_index;
137
        int64_t element_index;
138
139
224
        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
44
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
139
6
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
139
151
        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
5
        auto operator<=>(const Node& other) const { return value <=> other.value; }
140
    };
141
142
67
    void _convert_sorted_num_vec_to_nums() {
143
67
        size_t rows = 0;
144
129
        for (const auto& i : _sorted_nums_vec) {
145
129
            rows += i.size();
146
129
        }
147
67
        _nums.resize(rows);
148
67
        size_t count = 0;
149
150
67
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
151
196
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
152
129
            if (!_sorted_nums_vec[i].empty()) {
153
129
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
154
129
            }
155
129
        }
156
157
260
        while (!min_heap.empty()) {
158
193
            Node node = min_heap.top();
159
193
            min_heap.pop();
160
193
            _nums[count++] = node.value;
161
193
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
162
64
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
163
64
                min_heap.push(node);
164
64
            }
165
193
        }
166
67
        _sorted_nums_vec.clear();
167
67
    }
Unexecuted instantiation: _ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
142
67
    void _convert_sorted_num_vec_to_nums() {
143
67
        size_t rows = 0;
144
129
        for (const auto& i : _sorted_nums_vec) {
145
129
            rows += i.size();
146
129
        }
147
67
        _nums.resize(rows);
148
67
        size_t count = 0;
149
150
67
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
151
196
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
152
129
            if (!_sorted_nums_vec[i].empty()) {
153
129
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
154
129
            }
155
129
        }
156
157
260
        while (!min_heap.empty()) {
158
193
            Node node = min_heap.top();
159
193
            min_heap.pop();
160
193
            _nums[count++] = node.value;
161
193
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
162
64
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
163
64
                min_heap.push(node);
164
64
            }
165
193
        }
166
67
        _sorted_nums_vec.clear();
167
67
    }
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
25
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
25
        Ty first_number = 0, second_number = 0;
171
25
        size_t count = 0;
172
25
        if (reverse) {
173
8
            std::priority_queue<Node> max_heap;
174
25
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
17
                if (!_sorted_nums_vec[i].empty()) {
176
17
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
17
                                     _sorted_nums_vec[i].size() - 1);
178
17
                }
179
17
            }
180
181
20
            while (!max_heap.empty()) {
182
20
                Node node = max_heap.top();
183
20
                max_heap.pop();
184
20
                if (count == target) {
185
8
                    second_number = node.value;
186
12
                } else if (count == target + 1) {
187
8
                    first_number = node.value;
188
8
                    break;
189
8
                }
190
12
                ++count;
191
12
                if (--node.element_index >= 0) {
192
10
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
10
                    max_heap.push(node);
194
10
                }
195
12
            }
196
197
17
        } else {
198
17
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
53
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
36
                if (!_sorted_nums_vec[i].empty()) {
201
36
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
36
                }
203
36
            }
204
205
62
            while (!min_heap.empty()) {
206
62
                Node node = min_heap.top();
207
62
                min_heap.pop();
208
62
                if (count == target) {
209
17
                    first_number = node.value;
210
45
                } else if (count == target + 1) {
211
17
                    second_number = node.value;
212
17
                    break;
213
17
                }
214
45
                ++count;
215
45
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
31
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
31
                    min_heap.push(node);
218
31
                }
219
45
            }
220
17
        }
221
222
25
        return {first_number, second_number};
223
25
    }
_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
7
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
5
                if (!_sorted_nums_vec[i].empty()) {
176
5
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
5
                                     _sorted_nums_vec[i].size() - 1);
178
5
                }
179
5
            }
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
5
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
5
                    max_heap.push(node);
194
5
                }
195
6
            }
196
197
4
        } else {
198
4
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
14
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
10
                if (!_sorted_nums_vec[i].empty()) {
201
10
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
10
                }
203
10
            }
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
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
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
169
4
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
4
        Ty first_number = 0, second_number = 0;
171
4
        size_t count = 0;
172
4
        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
3
        } else {
198
3
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
9
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
6
                if (!_sorted_nums_vec[i].empty()) {
201
6
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
6
                }
203
6
            }
204
205
13
            while (!min_heap.empty()) {
206
13
                Node node = min_heap.top();
207
13
                min_heap.pop();
208
13
                if (count == target) {
209
3
                    first_number = node.value;
210
10
                } else if (count == target + 1) {
211
3
                    second_number = node.value;
212
3
                    break;
213
3
                }
214
10
                ++count;
215
10
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
8
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
8
                    min_heap.push(node);
218
8
                }
219
10
            }
220
3
        }
221
222
4
        return {first_number, second_number};
223
4
    }
_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
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
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
0
                    max_heap.push(node);
194
0
                }
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
    }
224
225
    vectorized::PODArray<Ty> _nums;
226
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
227
};
228
229
} // namespace doris