Coverage Report

Created: 2026-02-14 10:15

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.58k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
47
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
205
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
2.15k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
835
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
43
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
13
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
294
    Counts() = default;
36
37
1.27k
    void merge(Counts* other) {
38
1.27k
        if (other != nullptr && !other->_nums.empty()) {
39
1.27k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
1.27k
        }
41
1.27k
    }
_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
794
    void merge(Counts* other) {
38
794
        if (other != nullptr && !other->_nums.empty()) {
39
794
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
794
        }
41
794
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
350
    void merge(Counts* other) {
38
350
        if (other != nullptr && !other->_nums.empty()) {
39
350
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
350
        }
41
350
    }
_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
103
    void merge(Counts* other) {
38
103
        if (other != nullptr && !other->_nums.empty()) {
39
103
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
103
        }
41
103
    }
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.68k
    void serialize(vectorized::BufferWritable& buf) {
58
1.68k
        if (!_nums.empty()) {
59
1.39k
            pdqsort(_nums.begin(), _nums.end());
60
1.39k
            size_t size = _nums.size();
61
1.39k
            buf.write_binary(size);
62
1.39k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
1.39k
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
299
            _convert_sorted_num_vec_to_nums();
66
299
            serialize(buf);
67
299
        }
68
1.68k
    }
_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
1.06k
    void serialize(vectorized::BufferWritable& buf) {
58
1.06k
        if (!_nums.empty()) {
59
911
            pdqsort(_nums.begin(), _nums.end());
60
911
            size_t size = _nums.size();
61
911
            buf.write_binary(size);
62
911
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
911
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
156
            _convert_sorted_num_vec_to_nums();
66
156
            serialize(buf);
67
156
        }
68
1.06k
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
493
    void serialize(vectorized::BufferWritable& buf) {
58
493
        if (!_nums.empty()) {
59
350
            pdqsort(_nums.begin(), _nums.end());
60
350
            size_t size = _nums.size();
61
350
            buf.write_binary(size);
62
350
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
350
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
143
            _convert_sorted_num_vec_to_nums();
66
143
            serialize(buf);
67
143
        }
68
493
    }
_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
103
    void serialize(vectorized::BufferWritable& buf) {
58
103
        if (!_nums.empty()) {
59
103
            pdqsort(_nums.begin(), _nums.end());
60
103
            size_t size = _nums.size();
61
103
            buf.write_binary(size);
62
103
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
103
        } 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
103
    }
69
70
1.27k
    void unserialize(vectorized::BufferReadable& buf) {
71
1.27k
        size_t size;
72
1.27k
        buf.read_binary(size);
73
1.27k
        _nums.resize(size);
74
1.27k
        auto buff = buf.read(sizeof(Ty) * size);
75
1.27k
        memcpy(_nums.data(), buff.data, buff.size);
76
1.27k
    }
_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
794
    void unserialize(vectorized::BufferReadable& buf) {
71
794
        size_t size;
72
794
        buf.read_binary(size);
73
794
        _nums.resize(size);
74
794
        auto buff = buf.read(sizeof(Ty) * size);
75
794
        memcpy(_nums.data(), buff.data, buff.size);
76
794
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
350
    void unserialize(vectorized::BufferReadable& buf) {
71
350
        size_t size;
72
350
        buf.read_binary(size);
73
350
        _nums.resize(size);
74
350
        auto buff = buf.read(sizeof(Ty) * size);
75
350
        memcpy(_nums.data(), buff.data, buff.size);
76
350
    }
_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
103
    void unserialize(vectorized::BufferReadable& buf) {
71
103
        size_t size;
72
103
        buf.read_binary(size);
73
103
        _nums.resize(size);
74
103
        auto buff = buf.read(sizeof(Ty) * size);
75
103
        memcpy(_nums.data(), buff.data, buff.size);
76
103
    }
77
78
1.10k
    double terminate(double quantile) {
79
1.10k
        if (_sorted_nums_vec.size() <= 1) {
80
949
            if (_sorted_nums_vec.size() == 1) {
81
296
                _nums = std::move(_sorted_nums_vec[0]);
82
296
            }
83
84
949
            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
949
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
180
                pdqsort(_nums.begin(), _nums.end());
92
180
            }
93
94
949
            if (quantile == 1 || _nums.size() == 1) {
95
473
                return _nums.back();
96
473
            }
97
98
476
            double u = (_nums.size() - 1) * quantile;
99
476
            auto index = static_cast<uint32_t>(u);
100
476
            return _nums[index] +
101
476
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
476
                                                       static_cast<double>(_nums[index]));
103
949
        } else {
104
156
            DCHECK(_nums.empty());
105
156
            size_t rows = 0;
106
391
            for (const auto& i : _sorted_nums_vec) {
107
391
                rows += i.size();
108
391
            }
109
156
            const bool reverse = quantile > 0.5 && rows > 2;
110
156
            double u = (rows - 1) * quantile;
111
156
            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
156
            size_t target = reverse ? rows - index - 2 : index;
120
156
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
156
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
156
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
156
            return first_number +
128
156
                   (u - static_cast<double>(index)) *
129
156
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
156
        }
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
300
    double terminate(double quantile) {
79
300
        if (_sorted_nums_vec.size() <= 1) {
80
295
            if (_sorted_nums_vec.size() == 1) {
81
1
                _nums = std::move(_sorted_nums_vec[0]);
82
1
            }
83
84
295
            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
295
            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
208
            double u = (_nums.size() - 1) * quantile;
99
208
            auto index = static_cast<uint32_t>(u);
100
208
            return _nums[index] +
101
208
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
208
                                                       static_cast<double>(_nums[index]));
103
295
        } else {
104
5
            DCHECK(_nums.empty());
105
5
            size_t rows = 0;
106
10
            for (const auto& i : _sorted_nums_vec) {
107
10
                rows += i.size();
108
10
            }
109
5
            const bool reverse = quantile > 0.5 && rows > 2;
110
5
            double u = (rows - 1) * quantile;
111
5
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
5
            size_t target = reverse ? rows - index - 2 : index;
120
5
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
5
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
5
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
5
            return first_number +
128
5
                   (u - static_cast<double>(index)) *
129
5
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
5
        }
131
300
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
78
444
    double terminate(double quantile) {
79
444
        if (_sorted_nums_vec.size() <= 1) {
80
345
            if (_sorted_nums_vec.size() == 1) {
81
227
                _nums = std::move(_sorted_nums_vec[0]);
82
227
            }
83
84
345
            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
345
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
11
                pdqsort(_nums.begin(), _nums.end());
92
11
            }
93
94
345
            if (quantile == 1 || _nums.size() == 1) {
95
239
                return _nums.back();
96
239
            }
97
98
106
            double u = (_nums.size() - 1) * quantile;
99
106
            auto index = static_cast<uint32_t>(u);
100
106
            return _nums[index] +
101
106
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
106
                                                       static_cast<double>(_nums[index]));
103
345
        } else {
104
99
            DCHECK(_nums.empty());
105
99
            size_t rows = 0;
106
228
            for (const auto& i : _sorted_nums_vec) {
107
228
                rows += i.size();
108
228
            }
109
99
            const bool reverse = quantile > 0.5 && rows > 2;
110
99
            double u = (rows - 1) * quantile;
111
99
            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
99
            size_t target = reverse ? rows - index - 2 : index;
120
99
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
99
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
99
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
99
            return first_number +
128
99
                   (u - static_cast<double>(index)) *
129
99
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
99
        }
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
65
            for (const auto& i : _sorted_nums_vec) {
107
65
                rows += i.size();
108
65
            }
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
76
            for (const auto& i : _sorted_nums_vec) {
107
76
                rows += i.size();
108
76
            }
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
1.16k
        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
682
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
139
353
        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
94
        auto operator<=>(const Node& other) const { return value <=> other.value; }
140
    };
141
142
299
    void _convert_sorted_num_vec_to_nums() {
143
299
        size_t rows = 0;
144
586
        for (const auto& i : _sorted_nums_vec) {
145
586
            rows += i.size();
146
586
        }
147
299
        _nums.resize(rows);
148
299
        size_t count = 0;
149
150
299
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
151
885
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
152
586
            if (!_sorted_nums_vec[i].empty()) {
153
586
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
154
586
            }
155
586
        }
156
157
1.03k
        while (!min_heap.empty()) {
158
737
            Node node = min_heap.top();
159
737
            min_heap.pop();
160
737
            _nums[count++] = node.value;
161
737
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
162
151
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
163
151
                min_heap.push(node);
164
151
            }
165
737
        }
166
299
        _sorted_nums_vec.clear();
167
299
    }
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
143
    void _convert_sorted_num_vec_to_nums() {
143
143
        size_t rows = 0;
144
247
        for (const auto& i : _sorted_nums_vec) {
145
247
            rows += i.size();
146
247
        }
147
143
        _nums.resize(rows);
148
143
        size_t count = 0;
149
150
143
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
151
390
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
152
247
            if (!_sorted_nums_vec[i].empty()) {
153
247
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
154
247
            }
155
247
        }
156
157
505
        while (!min_heap.empty()) {
158
362
            Node node = min_heap.top();
159
362
            min_heap.pop();
160
362
            _nums[count++] = node.value;
161
362
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
162
115
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
163
115
                min_heap.push(node);
164
115
            }
165
362
        }
166
143
        _sorted_nums_vec.clear();
167
143
    }
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
156
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
156
        Ty first_number = 0, second_number = 0;
171
156
        size_t count = 0;
172
156
        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
113
            while (!max_heap.empty()) {
182
113
                Node node = max_heap.top();
183
113
                max_heap.pop();
184
113
                if (count == target) {
185
31
                    second_number = node.value;
186
82
                } else if (count == target + 1) {
187
31
                    first_number = node.value;
188
31
                    break;
189
31
                }
190
82
                ++count;
191
82
                if (--node.element_index >= 0) {
192
58
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
58
                    max_heap.push(node);
194
58
                }
195
82
            }
196
197
125
        } else {
198
125
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
422
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
297
                if (!_sorted_nums_vec[i].empty()) {
201
297
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
297
                }
203
297
            }
204
205
352
            while (!min_heap.empty()) {
206
352
                Node node = min_heap.top();
207
352
                min_heap.pop();
208
352
                if (count == target) {
209
125
                    first_number = node.value;
210
227
                } else if (count == target + 1) {
211
125
                    second_number = node.value;
212
125
                    break;
213
125
                }
214
227
                ++count;
215
227
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
152
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
152
                    min_heap.push(node);
218
152
                }
219
227
            }
220
125
        }
221
222
156
        return {first_number, second_number};
223
156
    }
_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
99
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
99
        Ty first_number = 0, second_number = 0;
171
99
        size_t count = 0;
172
99
        if (reverse) {
173
11
            std::priority_queue<Node> max_heap;
174
37
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
26
                if (!_sorted_nums_vec[i].empty()) {
176
26
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
26
                                     _sorted_nums_vec[i].size() - 1);
178
26
                }
179
26
            }
180
181
41
            while (!max_heap.empty()) {
182
41
                Node node = max_heap.top();
183
41
                max_heap.pop();
184
41
                if (count == target) {
185
11
                    second_number = node.value;
186
30
                } else if (count == target + 1) {
187
11
                    first_number = node.value;
188
11
                    break;
189
11
                }
190
30
                ++count;
191
30
                if (--node.element_index >= 0) {
192
23
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
23
                    max_heap.push(node);
194
23
                }
195
30
            }
196
197
88
        } else {
198
88
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
290
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
202
                if (!_sorted_nums_vec[i].empty()) {
201
202
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
202
                }
203
202
            }
204
205
247
            while (!min_heap.empty()) {
206
247
                Node node = min_heap.top();
207
247
                min_heap.pop();
208
247
                if (count == target) {
209
88
                    first_number = node.value;
210
159
                } else if (count == target + 1) {
211
88
                    second_number = node.value;
212
88
                    break;
213
88
                }
214
159
                ++count;
215
159
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
117
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
117
                    min_heap.push(node);
218
117
                }
219
159
            }
220
88
        }
221
222
99
        return {first_number, second_number};
223
99
    }
_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
12
            std::priority_queue<Node> max_heap;
174
60
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
48
                if (!_sorted_nums_vec[i].empty()) {
176
48
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
48
                                     _sorted_nums_vec[i].size() - 1);
178
48
                }
179
48
            }
180
181
51
            while (!max_heap.empty()) {
182
51
                Node node = max_heap.top();
183
51
                max_heap.pop();
184
51
                if (count == target) {
185
12
                    second_number = node.value;
186
39
                } else if (count == target + 1) {
187
12
                    first_number = node.value;
188
12
                    break;
189
12
                }
190
39
                ++count;
191
39
                if (--node.element_index >= 0) {
192
26
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
26
                    max_heap.push(node);
194
26
                }
195
39
            }
196
197
12
        } else {
198
8
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
25
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
17
                if (!_sorted_nums_vec[i].empty()) {
201
17
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
17
                }
203
17
            }
204
205
25
            while (!min_heap.empty()) {
206
25
                Node node = min_heap.top();
207
25
                min_heap.pop();
208
25
                if (count == target) {
209
8
                    first_number = node.value;
210
17
                } else if (count == target + 1) {
211
8
                    second_number = node.value;
212
8
                    break;
213
8
                }
214
17
                ++count;
215
17
                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
17
            }
220
8
        }
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
13
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
10
                if (!_sorted_nums_vec[i].empty()) {
176
10
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
10
                                     _sorted_nums_vec[i].size() - 1);
178
10
                }
179
10
            }
180
181
7
            while (!max_heap.empty()) {
182
7
                Node node = max_heap.top();
183
7
                max_heap.pop();
184
7
                if (count == target) {
185
3
                    second_number = node.value;
186
4
                } else if (count == target + 1) {
187
3
                    first_number = node.value;
188
3
                    break;
189
3
                }
190
4
                ++count;
191
4
                if (--node.element_index >= 0) {
192
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
0
                    max_heap.push(node);
194
0
                }
195
4
            }
196
197
23
        } else {
198
23
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
89
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
66
                if (!_sorted_nums_vec[i].empty()) {
201
66
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
66
                }
203
66
            }
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
5
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
5
                    min_heap.push(node);
218
5
                }
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