Coverage Report

Created: 2025-09-16 11:06

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