Coverage Report

Created: 2026-01-19 12:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/counts.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <pdqsort.h>
21
22
#include <algorithm>
23
#include <cmath>
24
#include <queue>
25
26
#include "udf/udf.h"
27
#include "vec/common/pod_array.h"
28
#include "vec/common/string_buffer.hpp"
29
#include "vec/io/io_helper.h"
30
31
namespace doris {
32
33
template <typename Ty>
34
class Counts {
35
public:
36
3.47k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
47
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
217
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
2.08k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
825
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
43
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
36
13
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
246
    Counts() = default;
37
38
1.21k
    void merge(Counts* other) {
39
1.21k
        if (other != nullptr && !other->_nums.empty()) {
40
1.21k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.21k
        }
42
1.21k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
38
5
    void merge(Counts* other) {
39
5
        if (other != nullptr && !other->_nums.empty()) {
40
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
5
        }
42
5
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
38
17
    void merge(Counts* other) {
39
17
        if (other != nullptr && !other->_nums.empty()) {
40
17
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
17
        }
42
17
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
760
    void merge(Counts* other) {
39
760
        if (other != nullptr && !other->_nums.empty()) {
40
760
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
760
        }
42
760
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
345
    void merge(Counts* other) {
39
345
        if (other != nullptr && !other->_nums.empty()) {
40
345
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
345
        }
42
345
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
38
5
    void merge(Counts* other) {
39
5
        if (other != nullptr && !other->_nums.empty()) {
40
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
5
        }
42
5
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
38
5
    void merge(Counts* other) {
39
5
        if (other != nullptr && !other->_nums.empty()) {
40
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
5
        }
42
5
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
79
    void merge(Counts* other) {
39
79
        if (other != nullptr && !other->_nums.empty()) {
40
79
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
79
        }
42
79
    }
43
44
    void increment(Ty key, uint32_t i) {
45
        auto old_size = _nums.size();
46
        _nums.resize(_nums.size() + i);
47
        for (uint32_t j = 0; j < i; ++j) {
48
            _nums[old_size + j] = key;
49
        }
50
    }
51
52
2.93k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
52
73
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
52
552
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
52
1.59k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
52
401
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
52
45
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIfE9incrementEf
Line
Count
Source
52
9
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
52
259
    void increment(Ty key) { _nums.push_back(key); }
53
54
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
5
        _nums.insert(keys.begin(), keys.end());
56
5
    }
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_10vectorized8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
_ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Line
Count
Source
54
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
5
        _nums.insert(keys.begin(), keys.end());
56
5
    }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
57
58
1.58k
    void serialize(vectorized::BufferWritable& buf) {
59
1.58k
        if (!_nums.empty()) {
60
1.29k
            pdqsort(_nums.begin(), _nums.end());
61
1.29k
            size_t size = _nums.size();
62
1.29k
            buf.write_binary(size);
63
1.29k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.29k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
288
            _convert_sorted_num_vec_to_nums();
67
288
            serialize(buf);
68
288
        }
69
1.58k
    }
_ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
5
    void serialize(vectorized::BufferWritable& buf) {
59
5
        if (!_nums.empty()) {
60
5
            pdqsort(_nums.begin(), _nums.end());
61
5
            size_t size = _nums.size();
62
5
            buf.write_binary(size);
63
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
5
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
5
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
17
    void serialize(vectorized::BufferWritable& buf) {
59
17
        if (!_nums.empty()) {
60
17
            pdqsort(_nums.begin(), _nums.end());
61
17
            size_t size = _nums.size();
62
17
            buf.write_binary(size);
63
17
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
17
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
17
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
994
    void serialize(vectorized::BufferWritable& buf) {
59
994
        if (!_nums.empty()) {
60
838
            pdqsort(_nums.begin(), _nums.end());
61
838
            size_t size = _nums.size();
62
838
            buf.write_binary(size);
63
838
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
838
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
156
            _convert_sorted_num_vec_to_nums();
67
156
            serialize(buf);
68
156
        }
69
994
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
477
    void serialize(vectorized::BufferWritable& buf) {
59
477
        if (!_nums.empty()) {
60
345
            pdqsort(_nums.begin(), _nums.end());
61
345
            size_t size = _nums.size();
62
345
            buf.write_binary(size);
63
345
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
345
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
132
            _convert_sorted_num_vec_to_nums();
67
132
            serialize(buf);
68
132
        }
69
477
    }
_ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
5
    void serialize(vectorized::BufferWritable& buf) {
59
5
        if (!_nums.empty()) {
60
5
            pdqsort(_nums.begin(), _nums.end());
61
5
            size_t size = _nums.size();
62
5
            buf.write_binary(size);
63
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
5
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
5
    }
_ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
5
    void serialize(vectorized::BufferWritable& buf) {
59
5
        if (!_nums.empty()) {
60
5
            pdqsort(_nums.begin(), _nums.end());
61
5
            size_t size = _nums.size();
62
5
            buf.write_binary(size);
63
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
5
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
5
    }
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
79
    void serialize(vectorized::BufferWritable& buf) {
59
79
        if (!_nums.empty()) {
60
79
            pdqsort(_nums.begin(), _nums.end());
61
79
            size_t size = _nums.size();
62
79
            buf.write_binary(size);
63
79
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
79
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
79
    }
70
71
1.21k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.21k
        size_t size;
73
1.21k
        buf.read_binary(size);
74
1.21k
        _nums.resize(size);
75
1.21k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.21k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.21k
    }
_ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
5
    void unserialize(vectorized::BufferReadable& buf) {
72
5
        size_t size;
73
5
        buf.read_binary(size);
74
5
        _nums.resize(size);
75
5
        auto buff = buf.read(sizeof(Ty) * size);
76
5
        memcpy(_nums.data(), buff.data, buff.size);
77
5
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
17
    void unserialize(vectorized::BufferReadable& buf) {
72
17
        size_t size;
73
17
        buf.read_binary(size);
74
17
        _nums.resize(size);
75
17
        auto buff = buf.read(sizeof(Ty) * size);
76
17
        memcpy(_nums.data(), buff.data, buff.size);
77
17
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
760
    void unserialize(vectorized::BufferReadable& buf) {
72
760
        size_t size;
73
760
        buf.read_binary(size);
74
760
        _nums.resize(size);
75
760
        auto buff = buf.read(sizeof(Ty) * size);
76
760
        memcpy(_nums.data(), buff.data, buff.size);
77
760
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
345
    void unserialize(vectorized::BufferReadable& buf) {
72
345
        size_t size;
73
345
        buf.read_binary(size);
74
345
        _nums.resize(size);
75
345
        auto buff = buf.read(sizeof(Ty) * size);
76
345
        memcpy(_nums.data(), buff.data, buff.size);
77
345
    }
_ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
5
    void unserialize(vectorized::BufferReadable& buf) {
72
5
        size_t size;
73
5
        buf.read_binary(size);
74
5
        _nums.resize(size);
75
5
        auto buff = buf.read(sizeof(Ty) * size);
76
5
        memcpy(_nums.data(), buff.data, buff.size);
77
5
    }
_ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
5
    void unserialize(vectorized::BufferReadable& buf) {
72
5
        size_t size;
73
5
        buf.read_binary(size);
74
5
        _nums.resize(size);
75
5
        auto buff = buf.read(sizeof(Ty) * size);
76
5
        memcpy(_nums.data(), buff.data, buff.size);
77
5
    }
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
79
    void unserialize(vectorized::BufferReadable& buf) {
72
79
        size_t size;
73
79
        buf.read_binary(size);
74
79
        _nums.resize(size);
75
79
        auto buff = buf.read(sizeof(Ty) * size);
76
79
        memcpy(_nums.data(), buff.data, buff.size);
77
79
    }
78
79
1.14k
    double terminate(double quantile) {
80
1.14k
        if (_sorted_nums_vec.size() <= 1) {
81
1.03k
            if (_sorted_nums_vec.size() == 1) {
82
378
                _nums = std::move(_sorted_nums_vec[0]);
83
378
            }
84
85
1.03k
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
1.03k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
180
                pdqsort(_nums.begin(), _nums.end());
93
180
            }
94
95
1.03k
            if (quantile == 1 || _nums.size() == 1) {
96
476
                return _nums.back();
97
476
            }
98
99
554
            double u = (_nums.size() - 1) * quantile;
100
554
            auto index = static_cast<uint32_t>(u);
101
554
            return _nums[index] +
102
554
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
554
                                                       static_cast<double>(_nums[index]));
104
1.03k
        } else {
105
113
            DCHECK(_nums.empty());
106
113
            size_t rows = 0;
107
252
            for (const auto& i : _sorted_nums_vec) {
108
252
                rows += i.size();
109
252
            }
110
113
            const bool reverse = quantile > 0.5 && rows > 2;
111
113
            double u = (rows - 1) * quantile;
112
113
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
113
            size_t target = reverse ? rows - index - 2 : index;
121
113
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
113
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
113
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
113
            return first_number +
129
113
                   (u - static_cast<double>(index)) *
130
113
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
113
        }
132
1.14k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
79
61
    double terminate(double quantile) {
80
61
        if (_sorted_nums_vec.size() <= 1) {
81
59
            if (_sorted_nums_vec.size() == 1) {
82
1
                _nums = std::move(_sorted_nums_vec[0]);
83
1
            }
84
85
59
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
59
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
24
                pdqsort(_nums.begin(), _nums.end());
93
24
            }
94
95
59
            if (quantile == 1 || _nums.size() == 1) {
96
34
                return _nums.back();
97
34
            }
98
99
25
            double u = (_nums.size() - 1) * quantile;
100
25
            auto index = static_cast<uint32_t>(u);
101
25
            return _nums[index] +
102
25
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
25
                                                       static_cast<double>(_nums[index]));
104
59
        } else {
105
2
            DCHECK(_nums.empty());
106
2
            size_t rows = 0;
107
4
            for (const auto& i : _sorted_nums_vec) {
108
4
                rows += i.size();
109
4
            }
110
2
            const bool reverse = quantile > 0.5 && rows > 2;
111
2
            double u = (rows - 1) * quantile;
112
2
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
2
            size_t target = reverse ? rows - index - 2 : index;
121
2
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
2
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
2
            return first_number +
129
2
                   (u - static_cast<double>(index)) *
130
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
2
        }
132
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
79
299
    double terminate(double quantile) {
80
299
        if (_sorted_nums_vec.size() <= 1) {
81
294
            if (_sorted_nums_vec.size() == 1) {
82
1
                _nums = std::move(_sorted_nums_vec[0]);
83
1
            }
84
85
294
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
294
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
144
                pdqsort(_nums.begin(), _nums.end());
93
144
            }
94
95
295
            if (quantile == 1 || _nums.size() == 1) {
96
87
                return _nums.back();
97
87
            }
98
99
207
            double u = (_nums.size() - 1) * quantile;
100
207
            auto index = static_cast<uint32_t>(u);
101
207
            return _nums[index] +
102
207
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
207
                                                       static_cast<double>(_nums[index]));
104
294
        } else {
105
5
            DCHECK(_nums.empty());
106
5
            size_t rows = 0;
107
16
            for (const auto& i : _sorted_nums_vec) {
108
16
                rows += i.size();
109
16
            }
110
5
            const bool reverse = quantile > 0.5 && rows > 2;
111
5
            double u = (rows - 1) * quantile;
112
5
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
5
            size_t target = reverse ? rows - index - 2 : index;
121
5
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
5
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
5
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
5
            return first_number +
129
5
                   (u - static_cast<double>(index)) *
130
5
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
5
        }
132
299
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
483
    double terminate(double quantile) {
80
483
        if (_sorted_nums_vec.size() <= 1) {
81
427
            if (_sorted_nums_vec.size() == 1) {
82
309
                _nums = std::move(_sorted_nums_vec[0]);
83
309
            }
84
85
427
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
427
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
11
                pdqsort(_nums.begin(), _nums.end());
93
11
            }
94
95
427
            if (quantile == 1 || _nums.size() == 1) {
96
242
                return _nums.back();
97
242
            }
98
99
185
            double u = (_nums.size() - 1) * quantile;
100
185
            auto index = static_cast<uint32_t>(u);
101
185
            return _nums[index] +
102
185
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
185
                                                       static_cast<double>(_nums[index]));
104
427
        } else {
105
56
            DCHECK(_nums.empty());
106
56
            size_t rows = 0;
107
112
            for (const auto& i : _sorted_nums_vec) {
108
112
                rows += i.size();
109
112
            }
110
56
            const bool reverse = quantile > 0.5 && rows > 2;
111
56
            double u = (rows - 1) * quantile;
112
56
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
56
            size_t target = reverse ? rows - index - 2 : index;
121
56
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
56
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
56
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
56
            return first_number +
129
56
                   (u - static_cast<double>(index)) *
130
56
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
56
        }
132
483
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
128
    double terminate(double quantile) {
80
128
        if (_sorted_nums_vec.size() <= 1) {
81
108
            if (_sorted_nums_vec.size() == 1) {
82
38
                _nums = std::move(_sorted_nums_vec[0]);
83
38
            }
84
85
108
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
108
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
108
            if (quantile == 1 || _nums.size() == 1) {
96
59
                return _nums.back();
97
59
            }
98
99
49
            double u = (_nums.size() - 1) * quantile;
100
49
            auto index = static_cast<uint32_t>(u);
101
49
            return _nums[index] +
102
49
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
49
                                                       static_cast<double>(_nums[index]));
104
108
        } else {
105
20
            DCHECK(_nums.empty());
106
20
            size_t rows = 0;
107
60
            for (const auto& i : _sorted_nums_vec) {
108
60
                rows += i.size();
109
60
            }
110
20
            const bool reverse = quantile > 0.5 && rows > 2;
111
20
            double u = (rows - 1) * quantile;
112
20
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
20
            size_t target = reverse ? rows - index - 2 : index;
121
20
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
20
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
20
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
20
            return first_number +
129
20
                   (u - static_cast<double>(index)) *
130
20
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
20
        }
132
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
79
33
    double terminate(double quantile) {
80
33
        if (_sorted_nums_vec.size() <= 1) {
81
31
            if (_sorted_nums_vec.size() == 1) {
82
1
                _nums = std::move(_sorted_nums_vec[0]);
83
1
            }
84
85
31
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
31
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
31
            if (quantile == 1 || _nums.size() == 1) {
96
24
                return _nums.back();
97
24
            }
98
99
7
            double u = (_nums.size() - 1) * quantile;
100
7
            auto index = static_cast<uint32_t>(u);
101
7
            return _nums[index] +
102
7
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
7
                                                       static_cast<double>(_nums[index]));
104
31
        } else {
105
2
            DCHECK(_nums.empty());
106
2
            size_t rows = 0;
107
4
            for (const auto& i : _sorted_nums_vec) {
108
4
                rows += i.size();
109
4
            }
110
2
            const bool reverse = quantile > 0.5 && rows > 2;
111
2
            double u = (rows - 1) * quantile;
112
2
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
2
            size_t target = reverse ? rows - index - 2 : index;
121
2
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
2
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
2
            return first_number +
129
2
                   (u - static_cast<double>(index)) *
130
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
2
        }
132
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
79
3
    double terminate(double quantile) {
80
3
        if (_sorted_nums_vec.size() <= 1) {
81
1
            if (_sorted_nums_vec.size() == 1) {
82
1
                _nums = std::move(_sorted_nums_vec[0]);
83
1
            }
84
85
1
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
1
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
1
            if (quantile == 1 || _nums.size() == 1) {
96
0
                return _nums.back();
97
0
            }
98
99
1
            double u = (_nums.size() - 1) * quantile;
100
1
            auto index = static_cast<uint32_t>(u);
101
1
            return _nums[index] +
102
1
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
1
                                                       static_cast<double>(_nums[index]));
104
2
        } else {
105
2
            DCHECK(_nums.empty());
106
2
            size_t rows = 0;
107
4
            for (const auto& i : _sorted_nums_vec) {
108
4
                rows += i.size();
109
4
            }
110
2
            const bool reverse = quantile > 0.5 && rows > 2;
111
2
            double u = (rows - 1) * quantile;
112
2
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
2
            size_t target = reverse ? rows - index - 2 : index;
121
2
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
2
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
2
            return first_number +
129
2
                   (u - static_cast<double>(index)) *
130
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
2
        }
132
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
79
136
    double terminate(double quantile) {
80
136
        if (_sorted_nums_vec.size() <= 1) {
81
110
            if (_sorted_nums_vec.size() == 1) {
82
27
                _nums = std::move(_sorted_nums_vec[0]);
83
27
            }
84
85
110
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
110
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
110
            if (quantile == 1 || _nums.size() == 1) {
96
30
                return _nums.back();
97
30
            }
98
99
80
            double u = (_nums.size() - 1) * quantile;
100
80
            auto index = static_cast<uint32_t>(u);
101
80
            return _nums[index] +
102
80
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
80
                                                       static_cast<double>(_nums[index]));
104
110
        } else {
105
26
            DCHECK(_nums.empty());
106
26
            size_t rows = 0;
107
52
            for (const auto& i : _sorted_nums_vec) {
108
52
                rows += i.size();
109
52
            }
110
26
            const bool reverse = quantile > 0.5 && rows > 2;
111
26
            double u = (rows - 1) * quantile;
112
26
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
26
            size_t target = reverse ? rows - index - 2 : index;
121
26
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
26
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
26
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
26
            return first_number +
129
26
                   (u - static_cast<double>(index)) *
130
26
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
26
        }
132
136
    }
133
134
private:
135
    struct Node {
136
        Ty value;
137
        int array_index;
138
        int64_t element_index;
139
140
1.00k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIaE4NodessERKS2_
Line
Count
Source
140
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
140
73
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
140
485
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
140
392
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsInE4NodessERKS2_
Line
Count
Source
140
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIfE4NodessERKS2_
Line
Count
Source
140
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
140
46
        auto operator<=>(const Node& other) const { return value <=> other.value; }
141
    };
142
143
288
    void _convert_sorted_num_vec_to_nums() {
144
288
        size_t rows = 0;
145
586
        for (const auto& i : _sorted_nums_vec) {
146
586
            rows += i.size();
147
586
        }
148
288
        _nums.resize(rows);
149
288
        size_t count = 0;
150
151
288
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
874
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
586
            if (!_sorted_nums_vec[i].empty()) {
154
586
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
586
            }
156
586
        }
157
158
1.02k
        while (!min_heap.empty()) {
159
737
            Node node = min_heap.top();
160
737
            min_heap.pop();
161
737
            _nums[count++] = node.value;
162
737
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
151
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
151
                min_heap.push(node);
165
151
            }
166
737
        }
167
288
        _sorted_nums_vec.clear();
168
288
    }
Unexecuted instantiation: _ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv
_ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
143
156
    void _convert_sorted_num_vec_to_nums() {
144
156
        size_t rows = 0;
145
339
        for (const auto& i : _sorted_nums_vec) {
146
339
            rows += i.size();
147
339
        }
148
156
        _nums.resize(rows);
149
156
        size_t count = 0;
150
151
156
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
495
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
339
            if (!_sorted_nums_vec[i].empty()) {
154
339
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
339
            }
156
339
        }
157
158
531
        while (!min_heap.empty()) {
159
375
            Node node = min_heap.top();
160
375
            min_heap.pop();
161
375
            _nums[count++] = node.value;
162
375
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
36
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
36
                min_heap.push(node);
165
36
            }
166
375
        }
167
156
        _sorted_nums_vec.clear();
168
156
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
143
132
    void _convert_sorted_num_vec_to_nums() {
144
132
        size_t rows = 0;
145
247
        for (const auto& i : _sorted_nums_vec) {
146
247
            rows += i.size();
147
247
        }
148
132
        _nums.resize(rows);
149
132
        size_t count = 0;
150
151
132
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
379
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
247
            if (!_sorted_nums_vec[i].empty()) {
154
247
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
247
            }
156
247
        }
157
158
494
        while (!min_heap.empty()) {
159
362
            Node node = min_heap.top();
160
362
            min_heap.pop();
161
362
            _nums[count++] = node.value;
162
362
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
115
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
115
                min_heap.push(node);
165
115
            }
166
362
        }
167
132
        _sorted_nums_vec.clear();
168
132
    }
Unexecuted instantiation: _ZN5doris6CountsInE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIfE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIdE31_convert_sorted_num_vec_to_numsEv
169
170
113
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
113
        Ty first_number = 0, second_number = 0;
172
113
        size_t count = 0;
173
113
        if (reverse) {
174
31
            std::priority_queue<Node> max_heap;
175
115
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
84
                if (!_sorted_nums_vec[i].empty()) {
177
84
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
84
                                     _sorted_nums_vec[i].size() - 1);
179
84
                }
180
84
            }
181
182
118
            while (!max_heap.empty()) {
183
118
                Node node = max_heap.top();
184
118
                max_heap.pop();
185
118
                if (count == target) {
186
31
                    second_number = node.value;
187
87
                } else if (count == target + 1) {
188
31
                    first_number = node.value;
189
31
                    break;
190
31
                }
191
87
                ++count;
192
87
                if (--node.element_index >= 0) {
193
76
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
76
                    max_heap.push(node);
195
76
                }
196
87
            }
197
198
82
        } else {
199
82
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
250
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
168
                if (!_sorted_nums_vec[i].empty()) {
202
168
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
168
                }
204
168
            }
205
206
264
            while (!min_heap.empty()) {
207
264
                Node node = min_heap.top();
208
264
                min_heap.pop();
209
264
                if (count == target) {
210
82
                    first_number = node.value;
211
182
                } else if (count == target + 1) {
212
82
                    second_number = node.value;
213
82
                    break;
214
82
                }
215
182
                ++count;
216
182
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
160
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
160
                    min_heap.push(node);
219
160
                }
220
182
            }
221
82
        }
222
223
113
        return {first_number, second_number};
224
113
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
2
        Ty first_number = 0, second_number = 0;
172
2
        size_t count = 0;
173
2
        if (reverse) {
174
1
            std::priority_queue<Node> max_heap;
175
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
2
                if (!_sorted_nums_vec[i].empty()) {
177
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
2
                                     _sorted_nums_vec[i].size() - 1);
179
2
                }
180
2
            }
181
182
2
            while (!max_heap.empty()) {
183
2
                Node node = max_heap.top();
184
2
                max_heap.pop();
185
2
                if (count == target) {
186
1
                    second_number = node.value;
187
1
                } else if (count == target + 1) {
188
1
                    first_number = node.value;
189
1
                    break;
190
1
                }
191
1
                ++count;
192
1
                if (--node.element_index >= 0) {
193
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
1
                    max_heap.push(node);
195
1
                }
196
1
            }
197
198
1
        } else {
199
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
2
                if (!_sorted_nums_vec[i].empty()) {
202
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
2
                }
204
2
            }
205
206
3
            while (!min_heap.empty()) {
207
3
                Node node = min_heap.top();
208
3
                min_heap.pop();
209
3
                if (count == target) {
210
1
                    first_number = node.value;
211
2
                } else if (count == target + 1) {
212
1
                    second_number = node.value;
213
1
                    break;
214
1
                }
215
2
                ++count;
216
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
1
                    min_heap.push(node);
219
1
                }
220
2
            }
221
1
        }
222
223
2
        return {first_number, second_number};
224
2
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
5
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
5
        Ty first_number = 0, second_number = 0;
172
5
        size_t count = 0;
173
5
        if (reverse) {
174
2
            std::priority_queue<Node> max_heap;
175
8
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
6
                if (!_sorted_nums_vec[i].empty()) {
177
6
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
6
                                     _sorted_nums_vec[i].size() - 1);
179
6
                }
180
6
            }
181
182
8
            while (!max_heap.empty()) {
183
8
                Node node = max_heap.top();
184
8
                max_heap.pop();
185
8
                if (count == target) {
186
2
                    second_number = node.value;
187
6
                } else if (count == target + 1) {
188
2
                    first_number = node.value;
189
2
                    break;
190
2
                }
191
6
                ++count;
192
6
                if (--node.element_index >= 0) {
193
4
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
4
                    max_heap.push(node);
195
4
                }
196
6
            }
197
198
3
        } else {
199
3
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
13
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
10
                if (!_sorted_nums_vec[i].empty()) {
202
10
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
10
                }
204
10
            }
205
206
16
            while (!min_heap.empty()) {
207
16
                Node node = min_heap.top();
208
16
                min_heap.pop();
209
16
                if (count == target) {
210
3
                    first_number = node.value;
211
13
                } else if (count == target + 1) {
212
3
                    second_number = node.value;
213
3
                    break;
214
3
                }
215
13
                ++count;
216
13
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
12
                    min_heap.push(node);
219
12
                }
220
13
            }
221
3
        }
222
223
5
        return {first_number, second_number};
224
5
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
56
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
56
        Ty first_number = 0, second_number = 0;
172
56
        size_t count = 0;
173
56
        if (reverse) {
174
9
            std::priority_queue<Node> max_heap;
175
27
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
18
                if (!_sorted_nums_vec[i].empty()) {
177
18
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
18
                                     _sorted_nums_vec[i].size() - 1);
179
18
                }
180
18
            }
181
182
37
            while (!max_heap.empty()) {
183
37
                Node node = max_heap.top();
184
37
                max_heap.pop();
185
37
                if (count == target) {
186
9
                    second_number = node.value;
187
28
                } else if (count == target + 1) {
188
9
                    first_number = node.value;
189
9
                    break;
190
9
                }
191
28
                ++count;
192
28
                if (--node.element_index >= 0) {
193
24
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
24
                    max_heap.push(node);
195
24
                }
196
28
            }
197
198
47
        } else {
199
47
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
141
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
94
                if (!_sorted_nums_vec[i].empty()) {
202
94
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
94
                }
204
94
            }
205
206
163
            while (!min_heap.empty()) {
207
163
                Node node = min_heap.top();
208
163
                min_heap.pop();
209
163
                if (count == target) {
210
47
                    first_number = node.value;
211
116
                } else if (count == target + 1) {
212
47
                    second_number = node.value;
213
47
                    break;
214
47
                }
215
116
                ++count;
216
116
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
114
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
114
                    min_heap.push(node);
219
114
                }
220
116
            }
221
47
        }
222
223
56
        return {first_number, second_number};
224
56
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
20
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
20
        Ty first_number = 0, second_number = 0;
172
20
        size_t count = 0;
173
20
        if (reverse) {
174
14
            std::priority_queue<Node> max_heap;
175
62
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
48
                if (!_sorted_nums_vec[i].empty()) {
177
48
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
48
                                     _sorted_nums_vec[i].size() - 1);
179
48
                }
180
48
            }
181
182
60
            while (!max_heap.empty()) {
183
60
                Node node = max_heap.top();
184
60
                max_heap.pop();
185
60
                if (count == target) {
186
14
                    second_number = node.value;
187
46
                } else if (count == target + 1) {
188
14
                    first_number = node.value;
189
14
                    break;
190
14
                }
191
46
                ++count;
192
46
                if (--node.element_index >= 0) {
193
42
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
42
                    max_heap.push(node);
195
42
                }
196
46
            }
197
198
14
        } else {
199
6
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
18
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
12
                if (!_sorted_nums_vec[i].empty()) {
202
12
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
12
                }
204
12
            }
205
206
21
            while (!min_heap.empty()) {
207
21
                Node node = min_heap.top();
208
21
                min_heap.pop();
209
21
                if (count == target) {
210
6
                    first_number = node.value;
211
15
                } else if (count == target + 1) {
212
6
                    second_number = node.value;
213
6
                    break;
214
6
                }
215
15
                ++count;
216
15
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
14
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
14
                    min_heap.push(node);
219
14
                }
220
15
            }
221
6
        }
222
223
20
        return {first_number, second_number};
224
20
    }
_ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
2
        Ty first_number = 0, second_number = 0;
172
2
        size_t count = 0;
173
2
        if (reverse) {
174
1
            std::priority_queue<Node> max_heap;
175
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
2
                if (!_sorted_nums_vec[i].empty()) {
177
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
2
                                     _sorted_nums_vec[i].size() - 1);
179
2
                }
180
2
            }
181
182
2
            while (!max_heap.empty()) {
183
2
                Node node = max_heap.top();
184
2
                max_heap.pop();
185
2
                if (count == target) {
186
1
                    second_number = node.value;
187
1
                } else if (count == target + 1) {
188
1
                    first_number = node.value;
189
1
                    break;
190
1
                }
191
1
                ++count;
192
1
                if (--node.element_index >= 0) {
193
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
1
                    max_heap.push(node);
195
1
                }
196
1
            }
197
198
1
        } else {
199
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
2
                if (!_sorted_nums_vec[i].empty()) {
202
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
2
                }
204
2
            }
205
206
3
            while (!min_heap.empty()) {
207
3
                Node node = min_heap.top();
208
3
                min_heap.pop();
209
3
                if (count == target) {
210
1
                    first_number = node.value;
211
2
                } else if (count == target + 1) {
212
1
                    second_number = node.value;
213
1
                    break;
214
1
                }
215
2
                ++count;
216
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
1
                    min_heap.push(node);
219
1
                }
220
2
            }
221
1
        }
222
223
2
        return {first_number, second_number};
224
2
    }
_ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
2
        Ty first_number = 0, second_number = 0;
172
2
        size_t count = 0;
173
2
        if (reverse) {
174
1
            std::priority_queue<Node> max_heap;
175
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
2
                if (!_sorted_nums_vec[i].empty()) {
177
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
2
                                     _sorted_nums_vec[i].size() - 1);
179
2
                }
180
2
            }
181
182
2
            while (!max_heap.empty()) {
183
2
                Node node = max_heap.top();
184
2
                max_heap.pop();
185
2
                if (count == target) {
186
1
                    second_number = node.value;
187
1
                } else if (count == target + 1) {
188
1
                    first_number = node.value;
189
1
                    break;
190
1
                }
191
1
                ++count;
192
1
                if (--node.element_index >= 0) {
193
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
1
                    max_heap.push(node);
195
1
                }
196
1
            }
197
198
1
        } else {
199
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
2
                if (!_sorted_nums_vec[i].empty()) {
202
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
2
                }
204
2
            }
205
206
3
            while (!min_heap.empty()) {
207
3
                Node node = min_heap.top();
208
3
                min_heap.pop();
209
3
                if (count == target) {
210
1
                    first_number = node.value;
211
2
                } else if (count == target + 1) {
212
1
                    second_number = node.value;
213
1
                    break;
214
1
                }
215
2
                ++count;
216
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
1
                    min_heap.push(node);
219
1
                }
220
2
            }
221
1
        }
222
223
2
        return {first_number, second_number};
224
2
    }
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
26
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
26
        Ty first_number = 0, second_number = 0;
172
26
        size_t count = 0;
173
26
        if (reverse) {
174
3
            std::priority_queue<Node> max_heap;
175
9
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
6
                if (!_sorted_nums_vec[i].empty()) {
177
6
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
6
                                     _sorted_nums_vec[i].size() - 1);
179
6
                }
180
6
            }
181
182
7
            while (!max_heap.empty()) {
183
7
                Node node = max_heap.top();
184
7
                max_heap.pop();
185
7
                if (count == target) {
186
3
                    second_number = node.value;
187
4
                } else if (count == target + 1) {
188
3
                    first_number = node.value;
189
3
                    break;
190
3
                }
191
4
                ++count;
192
4
                if (--node.element_index >= 0) {
193
3
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
3
                    max_heap.push(node);
195
3
                }
196
4
            }
197
198
23
        } else {
199
23
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
69
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
46
                if (!_sorted_nums_vec[i].empty()) {
202
46
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
46
                }
204
46
            }
205
206
55
            while (!min_heap.empty()) {
207
55
                Node node = min_heap.top();
208
55
                min_heap.pop();
209
55
                if (count == target) {
210
23
                    first_number = node.value;
211
32
                } else if (count == target + 1) {
212
23
                    second_number = node.value;
213
23
                    break;
214
23
                }
215
32
                ++count;
216
32
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
17
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
17
                    min_heap.push(node);
219
17
                }
220
32
            }
221
23
        }
222
223
26
        return {first_number, second_number};
224
26
    }
225
226
    vectorized::PODArray<Ty> _nums;
227
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
228
};
229
230
} // namespace doris