Coverage Report

Created: 2026-01-30 12:38

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