Coverage Report

Created: 2026-04-01 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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 "core/pod_array.h"
27
#include "core/string_buffer.hpp"
28
#include "util/io_helper.h"
29
30
namespace doris {
31
32
template <typename Ty>
33
class Counts {
34
public:
35
3.43k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
45
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
154
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
2.11k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
827
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
41
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
11
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
244
    Counts() = default;
36
37
1.22k
    void merge(Counts* other) {
38
1.22k
        if (other != nullptr && !other->_nums.empty()) {
39
1.22k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
1.22k
        }
41
1.22k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
37
4
    void merge(Counts* other) {
38
4
        if (other != nullptr && !other->_nums.empty()) {
39
4
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
4
        }
41
4
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
37
10
    void merge(Counts* other) {
38
10
        if (other != nullptr && !other->_nums.empty()) {
39
10
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
10
        }
41
10
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
37
782
    void merge(Counts* other) {
38
782
        if (other != nullptr && !other->_nums.empty()) {
39
782
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
782
        }
41
782
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
346
    void merge(Counts* other) {
38
346
        if (other != nullptr && !other->_nums.empty()) {
39
346
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
346
        }
41
346
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
37
4
    void merge(Counts* other) {
38
4
        if (other != nullptr && !other->_nums.empty()) {
39
4
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
4
        }
41
4
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
37
4
    void merge(Counts* other) {
38
4
        if (other != nullptr && !other->_nums.empty()) {
39
4
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
4
        }
41
4
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
37
78
    void merge(Counts* other) {
38
78
        if (other != nullptr && !other->_nums.empty()) {
39
78
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
78
        }
41
78
    }
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.75k
    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
398
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
51
1.57k
    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 PaddedPODArray<Ty>& keys) { _nums.insert(keys.begin(), keys.end()); }
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
_ZN5doris6CountsIlE15increment_batchERKNS_8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
53
5
    void increment_batch(const PaddedPODArray<Ty>& keys) { _nums.insert(keys.begin(), keys.end()); }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
54
55
1.63k
    void serialize(BufferWritable& buf) {
56
1.63k
        if (!_nums.empty()) {
57
1.34k
            pdqsort(_nums.begin(), _nums.end());
58
1.34k
            size_t size = _nums.size();
59
1.34k
            buf.write_binary(size);
60
1.34k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
1.34k
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
286
            _convert_sorted_num_vec_to_nums();
64
286
            serialize(buf);
65
286
        }
66
1.63k
    }
_ZN5doris6CountsIaE9serializeERNS_14BufferWritableE
Line
Count
Source
55
4
    void serialize(BufferWritable& buf) {
56
4
        if (!_nums.empty()) {
57
4
            pdqsort(_nums.begin(), _nums.end());
58
4
            size_t size = _nums.size();
59
4
            buf.write_binary(size);
60
4
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
4
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
4
    }
_ZN5doris6CountsIsE9serializeERNS_14BufferWritableE
Line
Count
Source
55
10
    void serialize(BufferWritable& buf) {
56
10
        if (!_nums.empty()) {
57
10
            pdqsort(_nums.begin(), _nums.end());
58
10
            size_t size = _nums.size();
59
10
            buf.write_binary(size);
60
10
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
10
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
10
    }
_ZN5doris6CountsIiE9serializeERNS_14BufferWritableE
Line
Count
Source
55
1.05k
    void serialize(BufferWritable& buf) {
56
1.05k
        if (!_nums.empty()) {
57
899
            pdqsort(_nums.begin(), _nums.end());
58
899
            size_t size = _nums.size();
59
899
            buf.write_binary(size);
60
899
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
899
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
156
            _convert_sorted_num_vec_to_nums();
64
156
            serialize(buf);
65
156
        }
66
1.05k
    }
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
55
476
    void serialize(BufferWritable& buf) {
56
476
        if (!_nums.empty()) {
57
346
            pdqsort(_nums.begin(), _nums.end());
58
346
            size_t size = _nums.size();
59
346
            buf.write_binary(size);
60
346
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
346
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
130
            _convert_sorted_num_vec_to_nums();
64
130
            serialize(buf);
65
130
        }
66
476
    }
_ZN5doris6CountsInE9serializeERNS_14BufferWritableE
Line
Count
Source
55
4
    void serialize(BufferWritable& buf) {
56
4
        if (!_nums.empty()) {
57
4
            pdqsort(_nums.begin(), _nums.end());
58
4
            size_t size = _nums.size();
59
4
            buf.write_binary(size);
60
4
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
4
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
4
    }
_ZN5doris6CountsIfE9serializeERNS_14BufferWritableE
Line
Count
Source
55
4
    void serialize(BufferWritable& buf) {
56
4
        if (!_nums.empty()) {
57
4
            pdqsort(_nums.begin(), _nums.end());
58
4
            size_t size = _nums.size();
59
4
            buf.write_binary(size);
60
4
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
4
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
4
    }
_ZN5doris6CountsIdE9serializeERNS_14BufferWritableE
Line
Count
Source
55
78
    void serialize(BufferWritable& buf) {
56
78
        if (!_nums.empty()) {
57
78
            pdqsort(_nums.begin(), _nums.end());
58
78
            size_t size = _nums.size();
59
78
            buf.write_binary(size);
60
78
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
78
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
78
    }
67
68
1.22k
    void unserialize(BufferReadable& buf) {
69
1.22k
        size_t size;
70
1.22k
        buf.read_binary(size);
71
1.22k
        _nums.resize(size);
72
1.22k
        auto buff = buf.read(sizeof(Ty) * size);
73
1.22k
        memcpy(_nums.data(), buff.data, buff.size);
74
1.22k
    }
_ZN5doris6CountsIaE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
4
    void unserialize(BufferReadable& buf) {
69
4
        size_t size;
70
4
        buf.read_binary(size);
71
4
        _nums.resize(size);
72
4
        auto buff = buf.read(sizeof(Ty) * size);
73
4
        memcpy(_nums.data(), buff.data, buff.size);
74
4
    }
_ZN5doris6CountsIsE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
10
    void unserialize(BufferReadable& buf) {
69
10
        size_t size;
70
10
        buf.read_binary(size);
71
10
        _nums.resize(size);
72
10
        auto buff = buf.read(sizeof(Ty) * size);
73
10
        memcpy(_nums.data(), buff.data, buff.size);
74
10
    }
_ZN5doris6CountsIiE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
782
    void unserialize(BufferReadable& buf) {
69
782
        size_t size;
70
782
        buf.read_binary(size);
71
782
        _nums.resize(size);
72
782
        auto buff = buf.read(sizeof(Ty) * size);
73
782
        memcpy(_nums.data(), buff.data, buff.size);
74
782
    }
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
346
    void unserialize(BufferReadable& buf) {
69
346
        size_t size;
70
346
        buf.read_binary(size);
71
346
        _nums.resize(size);
72
346
        auto buff = buf.read(sizeof(Ty) * size);
73
346
        memcpy(_nums.data(), buff.data, buff.size);
74
346
    }
_ZN5doris6CountsInE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
4
    void unserialize(BufferReadable& buf) {
69
4
        size_t size;
70
4
        buf.read_binary(size);
71
4
        _nums.resize(size);
72
4
        auto buff = buf.read(sizeof(Ty) * size);
73
4
        memcpy(_nums.data(), buff.data, buff.size);
74
4
    }
_ZN5doris6CountsIfE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
4
    void unserialize(BufferReadable& buf) {
69
4
        size_t size;
70
4
        buf.read_binary(size);
71
4
        _nums.resize(size);
72
4
        auto buff = buf.read(sizeof(Ty) * size);
73
4
        memcpy(_nums.data(), buff.data, buff.size);
74
4
    }
_ZN5doris6CountsIdE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
78
    void unserialize(BufferReadable& buf) {
69
78
        size_t size;
70
78
        buf.read_binary(size);
71
78
        _nums.resize(size);
72
78
        auto buff = buf.read(sizeof(Ty) * size);
73
78
        memcpy(_nums.data(), buff.data, buff.size);
74
78
    }
75
76
1.00k
    double terminate(double quantile) {
77
1.00k
        if (_sorted_nums_vec.size() <= 1) {
78
862
            if (_sorted_nums_vec.size() == 1) {
79
312
                _nums = std::move(_sorted_nums_vec[0]);
80
312
            }
81
82
862
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
862
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
132
                pdqsort(_nums.begin(), _nums.end());
90
132
            }
91
92
862
            if (quantile == 1 || _nums.size() == 1) {
93
440
                return _nums.back();
94
440
            }
95
96
422
            double u = (_nums.size() - 1) * quantile;
97
422
            auto index = static_cast<uint32_t>(u);
98
422
            return _nums[index] +
99
422
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
422
                                                       static_cast<double>(_nums[index]));
101
862
        } else {
102
140
            DCHECK(_nums.empty());
103
140
            size_t rows = 0;
104
332
            for (const auto& i : _sorted_nums_vec) {
105
332
                rows += i.size();
106
332
            }
107
140
            const bool reverse = quantile > 0.5 && rows > 2;
108
140
            double u = (rows - 1) * quantile;
109
140
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
140
            size_t target = reverse ? rows - index - 2 : index;
118
140
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
140
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
140
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
140
            return first_number +
126
140
                   (u - static_cast<double>(index)) *
127
140
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
140
        }
129
1.00k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
76
61
    double terminate(double quantile) {
77
61
        if (_sorted_nums_vec.size() <= 1) {
78
60
            if (_sorted_nums_vec.size() == 1) {
79
2
                _nums = std::move(_sorted_nums_vec[0]);
80
2
            }
81
82
60
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
60
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
24
                pdqsort(_nums.begin(), _nums.end());
90
24
            }
91
92
60
            if (quantile == 1 || _nums.size() == 1) {
93
34
                return _nums.back();
94
34
            }
95
96
26
            double u = (_nums.size() - 1) * quantile;
97
26
            auto index = static_cast<uint32_t>(u);
98
26
            return _nums[index] +
99
26
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
26
                                                       static_cast<double>(_nums[index]));
101
60
        } else {
102
1
            DCHECK(_nums.empty());
103
1
            size_t rows = 0;
104
2
            for (const auto& i : _sorted_nums_vec) {
105
2
                rows += i.size();
106
2
            }
107
1
            const bool reverse = quantile > 0.5 && rows > 2;
108
1
            double u = (rows - 1) * quantile;
109
1
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
1
            size_t target = reverse ? rows - index - 2 : index;
118
1
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
1
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
1
            return first_number +
126
1
                   (u - static_cast<double>(index)) *
127
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
1
        }
129
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
76
212
    double terminate(double quantile) {
77
212
        if (_sorted_nums_vec.size() <= 1) {
78
208
            if (_sorted_nums_vec.size() == 1) {
79
2
                _nums = std::move(_sorted_nums_vec[0]);
80
2
            }
81
82
208
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
208
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
96
                pdqsort(_nums.begin(), _nums.end());
90
96
            }
91
92
208
            if (quantile == 1 || _nums.size() == 1) {
93
66
                return _nums.back();
94
66
            }
95
96
142
            double u = (_nums.size() - 1) * quantile;
97
142
            auto index = static_cast<uint32_t>(u);
98
142
            return _nums[index] +
99
142
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
142
                                                       static_cast<double>(_nums[index]));
101
208
        } else {
102
4
            DCHECK(_nums.empty());
103
4
            size_t rows = 0;
104
8
            for (const auto& i : _sorted_nums_vec) {
105
8
                rows += i.size();
106
8
            }
107
4
            const bool reverse = quantile > 0.5 && rows > 2;
108
4
            double u = (rows - 1) * quantile;
109
4
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
4
            size_t target = reverse ? rows - index - 2 : index;
118
4
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
4
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
4
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
4
            return first_number +
126
4
                   (u - static_cast<double>(index)) *
127
4
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
4
        }
129
212
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
76
429
    double terminate(double quantile) {
77
429
        if (_sorted_nums_vec.size() <= 1) {
78
342
            if (_sorted_nums_vec.size() == 1) {
79
239
                _nums = std::move(_sorted_nums_vec[0]);
80
239
            }
81
82
342
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
342
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
11
                pdqsort(_nums.begin(), _nums.end());
90
11
            }
91
92
342
            if (quantile == 1 || _nums.size() == 1) {
93
227
                return _nums.back();
94
227
            }
95
96
115
            double u = (_nums.size() - 1) * quantile;
97
115
            auto index = static_cast<uint32_t>(u);
98
115
            return _nums[index] +
99
115
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
115
                                                       static_cast<double>(_nums[index]));
101
342
        } else {
102
87
            DCHECK(_nums.empty());
103
87
            size_t rows = 0;
104
204
            for (const auto& i : _sorted_nums_vec) {
105
204
                rows += i.size();
106
204
            }
107
87
            const bool reverse = quantile > 0.5 && rows > 2;
108
87
            double u = (rows - 1) * quantile;
109
87
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
87
            size_t target = reverse ? rows - index - 2 : index;
118
87
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
87
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
87
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
87
            return first_number +
126
87
                   (u - static_cast<double>(index)) *
127
87
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
87
        }
129
429
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
76
128
    double terminate(double quantile) {
77
128
        if (_sorted_nums_vec.size() <= 1) {
78
107
            if (_sorted_nums_vec.size() == 1) {
79
37
                _nums = std::move(_sorted_nums_vec[0]);
80
37
            }
81
82
107
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
107
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
1
                pdqsort(_nums.begin(), _nums.end());
90
1
            }
91
92
107
            if (quantile == 1 || _nums.size() == 1) {
93
59
                return _nums.back();
94
59
            }
95
96
48
            double u = (_nums.size() - 1) * quantile;
97
48
            auto index = static_cast<uint32_t>(u);
98
48
            return _nums[index] +
99
48
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
48
                                                       static_cast<double>(_nums[index]));
101
107
        } else {
102
21
            DCHECK(_nums.empty());
103
21
            size_t rows = 0;
104
64
            for (const auto& i : _sorted_nums_vec) {
105
64
                rows += i.size();
106
64
            }
107
21
            const bool reverse = quantile > 0.5 && rows > 2;
108
21
            double u = (rows - 1) * quantile;
109
21
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
21
            size_t target = reverse ? rows - index - 2 : index;
118
21
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
21
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
21
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
21
            return first_number +
126
21
                   (u - static_cast<double>(index)) *
127
21
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
21
        }
129
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
76
33
    double terminate(double quantile) {
77
33
        if (_sorted_nums_vec.size() <= 1) {
78
32
            if (_sorted_nums_vec.size() == 1) {
79
2
                _nums = std::move(_sorted_nums_vec[0]);
80
2
            }
81
82
32
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
32
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
32
            if (quantile == 1 || _nums.size() == 1) {
93
24
                return _nums.back();
94
24
            }
95
96
8
            double u = (_nums.size() - 1) * quantile;
97
8
            auto index = static_cast<uint32_t>(u);
98
8
            return _nums[index] +
99
8
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
8
                                                       static_cast<double>(_nums[index]));
101
32
        } else {
102
1
            DCHECK(_nums.empty());
103
1
            size_t rows = 0;
104
2
            for (const auto& i : _sorted_nums_vec) {
105
2
                rows += i.size();
106
2
            }
107
1
            const bool reverse = quantile > 0.5 && rows > 2;
108
1
            double u = (rows - 1) * quantile;
109
1
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
1
            size_t target = reverse ? rows - index - 2 : index;
118
1
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
1
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
1
            return first_number +
126
1
                   (u - static_cast<double>(index)) *
127
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
1
        }
129
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
76
3
    double terminate(double quantile) {
77
3
        if (_sorted_nums_vec.size() <= 1) {
78
2
            if (_sorted_nums_vec.size() == 1) {
79
2
                _nums = std::move(_sorted_nums_vec[0]);
80
2
            }
81
82
2
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
2
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
2
            if (quantile == 1 || _nums.size() == 1) {
93
0
                return _nums.back();
94
0
            }
95
96
2
            double u = (_nums.size() - 1) * quantile;
97
2
            auto index = static_cast<uint32_t>(u);
98
2
            return _nums[index] +
99
2
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
2
                                                       static_cast<double>(_nums[index]));
101
2
        } else {
102
1
            DCHECK(_nums.empty());
103
1
            size_t rows = 0;
104
2
            for (const auto& i : _sorted_nums_vec) {
105
2
                rows += i.size();
106
2
            }
107
1
            const bool reverse = quantile > 0.5 && rows > 2;
108
1
            double u = (rows - 1) * quantile;
109
1
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
1
            size_t target = reverse ? rows - index - 2 : index;
118
1
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
1
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
1
            return first_number +
126
1
                   (u - static_cast<double>(index)) *
127
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
1
        }
129
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
76
136
    double terminate(double quantile) {
77
136
        if (_sorted_nums_vec.size() <= 1) {
78
111
            if (_sorted_nums_vec.size() == 1) {
79
28
                _nums = std::move(_sorted_nums_vec[0]);
80
28
            }
81
82
111
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
111
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
111
            if (quantile == 1 || _nums.size() == 1) {
93
30
                return _nums.back();
94
30
            }
95
96
81
            double u = (_nums.size() - 1) * quantile;
97
81
            auto index = static_cast<uint32_t>(u);
98
81
            return _nums[index] +
99
81
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
81
                                                       static_cast<double>(_nums[index]));
101
111
        } else {
102
25
            DCHECK(_nums.empty());
103
25
            size_t rows = 0;
104
50
            for (const auto& i : _sorted_nums_vec) {
105
50
                rows += i.size();
106
50
            }
107
25
            const bool reverse = quantile > 0.5 && rows > 2;
108
25
            double u = (rows - 1) * quantile;
109
25
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
25
            size_t target = reverse ? rows - index - 2 : index;
118
25
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
25
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
25
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
25
            return first_number +
126
25
                   (u - static_cast<double>(index)) *
127
25
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
25
        }
129
136
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
1.12k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIaE4NodessERKS2_
Line
Count
Source
137
2
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
137
21
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
648
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
408
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsInE4NodessERKS2_
Line
Count
Source
137
2
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIfE4NodessERKS2_
Line
Count
Source
137
2
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
137
38
        auto operator<=>(const Node& other) const { return value <=> other.value; }
138
    };
139
140
286
    void _convert_sorted_num_vec_to_nums() {
141
286
        size_t rows = 0;
142
584
        for (const auto& i : _sorted_nums_vec) {
143
584
            rows += i.size();
144
584
        }
145
286
        _nums.resize(rows);
146
286
        size_t count = 0;
147
148
286
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
870
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
584
            if (!_sorted_nums_vec[i].empty()) {
151
584
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
584
            }
153
584
        }
154
155
1.01k
        while (!min_heap.empty()) {
156
731
            Node node = min_heap.top();
157
731
            min_heap.pop();
158
731
            _nums[count++] = node.value;
159
731
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
147
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
147
                min_heap.push(node);
162
147
            }
163
731
        }
164
286
        _sorted_nums_vec.clear();
165
286
    }
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
531
        while (!min_heap.empty()) {
156
375
            Node node = min_heap.top();
157
375
            min_heap.pop();
158
375
            _nums[count++] = node.value;
159
375
            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
375
        }
164
156
        _sorted_nums_vec.clear();
165
156
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
130
    void _convert_sorted_num_vec_to_nums() {
141
130
        size_t rows = 0;
142
245
        for (const auto& i : _sorted_nums_vec) {
143
245
            rows += i.size();
144
245
        }
145
130
        _nums.resize(rows);
146
130
        size_t count = 0;
147
148
130
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
375
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
245
            if (!_sorted_nums_vec[i].empty()) {
151
245
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
245
            }
153
245
        }
154
155
486
        while (!min_heap.empty()) {
156
356
            Node node = min_heap.top();
157
356
            min_heap.pop();
158
356
            _nums[count++] = node.value;
159
356
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
111
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
111
                min_heap.push(node);
162
111
            }
163
356
        }
164
130
        _sorted_nums_vec.clear();
165
130
    }
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
140
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
140
        Ty first_number = 0, second_number = 0;
169
140
        size_t count = 0;
170
140
        if (reverse) {
171
23
            std::priority_queue<Node> max_heap;
172
94
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
71
                if (!_sorted_nums_vec[i].empty()) {
174
71
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
71
                                     _sorted_nums_vec[i].size() - 1);
176
71
                }
177
71
            }
178
179
102
            while (!max_heap.empty()) {
180
102
                Node node = max_heap.top();
181
102
                max_heap.pop();
182
102
                if (count == target) {
183
23
                    second_number = node.value;
184
79
                } else if (count == target + 1) {
185
23
                    first_number = node.value;
186
23
                    break;
187
23
                }
188
79
                ++count;
189
79
                if (--node.element_index >= 0) {
190
74
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
74
                    max_heap.push(node);
192
74
                }
193
79
            }
194
195
117
        } else {
196
117
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
378
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
261
                if (!_sorted_nums_vec[i].empty()) {
199
261
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
261
                }
201
261
            }
202
203
334
            while (!min_heap.empty()) {
204
334
                Node node = min_heap.top();
205
334
                min_heap.pop();
206
334
                if (count == target) {
207
117
                    first_number = node.value;
208
217
                } else if (count == target + 1) {
209
117
                    second_number = node.value;
210
117
                    break;
211
117
                }
212
217
                ++count;
213
217
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
136
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
136
                    min_heap.push(node);
216
136
                }
217
217
            }
218
117
        }
219
220
140
        return {first_number, second_number};
221
140
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
1
        Ty first_number = 0, second_number = 0;
169
1
        size_t count = 0;
170
1
        if (reverse) {
171
0
            std::priority_queue<Node> max_heap;
172
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
0
                if (!_sorted_nums_vec[i].empty()) {
174
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
0
                                     _sorted_nums_vec[i].size() - 1);
176
0
                }
177
0
            }
178
179
0
            while (!max_heap.empty()) {
180
0
                Node node = max_heap.top();
181
0
                max_heap.pop();
182
0
                if (count == target) {
183
0
                    second_number = node.value;
184
0
                } else if (count == target + 1) {
185
0
                    first_number = node.value;
186
0
                    break;
187
0
                }
188
0
                ++count;
189
0
                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
0
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
1
        return {first_number, second_number};
221
1
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
4
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
4
        Ty first_number = 0, second_number = 0;
169
4
        size_t count = 0;
170
4
        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
6
            while (!max_heap.empty()) {
180
6
                Node node = max_heap.top();
181
6
                max_heap.pop();
182
6
                if (count == target) {
183
1
                    second_number = node.value;
184
5
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
5
                ++count;
189
5
                if (--node.element_index >= 0) {
190
5
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
5
                    max_heap.push(node);
192
5
                }
193
5
            }
194
195
3
        } 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
16
            while (!min_heap.empty()) {
204
16
                Node node = min_heap.top();
205
16
                min_heap.pop();
206
16
                if (count == target) {
207
3
                    first_number = node.value;
208
13
                } else if (count == target + 1) {
209
3
                    second_number = node.value;
210
3
                    break;
211
3
                }
212
13
                ++count;
213
13
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
12
                    min_heap.push(node);
216
12
                }
217
13
            }
218
3
        }
219
220
4
        return {first_number, second_number};
221
4
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
87
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
87
        Ty first_number = 0, second_number = 0;
169
87
        size_t count = 0;
170
87
        if (reverse) {
171
6
            std::priority_queue<Node> max_heap;
172
22
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
16
                if (!_sorted_nums_vec[i].empty()) {
174
16
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
16
                                     _sorted_nums_vec[i].size() - 1);
176
16
                }
177
16
            }
178
179
31
            while (!max_heap.empty()) {
180
31
                Node node = max_heap.top();
181
31
                max_heap.pop();
182
31
                if (count == target) {
183
6
                    second_number = node.value;
184
25
                } else if (count == target + 1) {
185
6
                    first_number = node.value;
186
6
                    break;
187
6
                }
188
25
                ++count;
189
25
                if (--node.element_index >= 0) {
190
25
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
25
                    max_heap.push(node);
192
25
                }
193
25
            }
194
195
81
        } else {
196
81
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
269
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
188
                if (!_sorted_nums_vec[i].empty()) {
199
188
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
188
                }
201
188
            }
202
203
231
            while (!min_heap.empty()) {
204
231
                Node node = min_heap.top();
205
231
                min_heap.pop();
206
231
                if (count == target) {
207
81
                    first_number = node.value;
208
150
                } else if (count == target + 1) {
209
81
                    second_number = node.value;
210
81
                    break;
211
81
                }
212
150
                ++count;
213
150
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
94
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
94
                    min_heap.push(node);
216
94
                }
217
150
            }
218
81
        }
219
220
87
        return {first_number, second_number};
221
87
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
21
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
21
        Ty first_number = 0, second_number = 0;
169
21
        size_t count = 0;
170
21
        if (reverse) {
171
14
            std::priority_queue<Node> max_heap;
172
63
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
49
                if (!_sorted_nums_vec[i].empty()) {
174
49
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
49
                                     _sorted_nums_vec[i].size() - 1);
176
49
                }
177
49
            }
178
179
60
            while (!max_heap.empty()) {
180
60
                Node node = max_heap.top();
181
60
                max_heap.pop();
182
60
                if (count == target) {
183
14
                    second_number = node.value;
184
46
                } else if (count == target + 1) {
185
14
                    first_number = node.value;
186
14
                    break;
187
14
                }
188
46
                ++count;
189
46
                if (--node.element_index >= 0) {
190
42
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
42
                    max_heap.push(node);
192
42
                }
193
46
            }
194
195
14
        } else {
196
7
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
22
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
15
                if (!_sorted_nums_vec[i].empty()) {
199
15
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
15
                }
201
15
            }
202
203
23
            while (!min_heap.empty()) {
204
23
                Node node = min_heap.top();
205
23
                min_heap.pop();
206
23
                if (count == target) {
207
7
                    first_number = node.value;
208
16
                } else if (count == target + 1) {
209
7
                    second_number = node.value;
210
7
                    break;
211
7
                }
212
16
                ++count;
213
16
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
14
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
14
                    min_heap.push(node);
216
14
                }
217
16
            }
218
7
        }
219
220
21
        return {first_number, second_number};
221
21
    }
_ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
1
        Ty first_number = 0, second_number = 0;
169
1
        size_t count = 0;
170
1
        if (reverse) {
171
0
            std::priority_queue<Node> max_heap;
172
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
0
                if (!_sorted_nums_vec[i].empty()) {
174
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
0
                                     _sorted_nums_vec[i].size() - 1);
176
0
                }
177
0
            }
178
179
0
            while (!max_heap.empty()) {
180
0
                Node node = max_heap.top();
181
0
                max_heap.pop();
182
0
                if (count == target) {
183
0
                    second_number = node.value;
184
0
                } else if (count == target + 1) {
185
0
                    first_number = node.value;
186
0
                    break;
187
0
                }
188
0
                ++count;
189
0
                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
0
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
1
        return {first_number, second_number};
221
1
    }
_ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
1
        Ty first_number = 0, second_number = 0;
169
1
        size_t count = 0;
170
1
        if (reverse) {
171
0
            std::priority_queue<Node> max_heap;
172
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
0
                if (!_sorted_nums_vec[i].empty()) {
174
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
0
                                     _sorted_nums_vec[i].size() - 1);
176
0
                }
177
0
            }
178
179
0
            while (!max_heap.empty()) {
180
0
                Node node = max_heap.top();
181
0
                max_heap.pop();
182
0
                if (count == target) {
183
0
                    second_number = node.value;
184
0
                } else if (count == target + 1) {
185
0
                    first_number = node.value;
186
0
                    break;
187
0
                }
188
0
                ++count;
189
0
                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
0
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
1
        return {first_number, second_number};
221
1
    }
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
25
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
25
        Ty first_number = 0, second_number = 0;
169
25
        size_t count = 0;
170
25
        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
5
            while (!max_heap.empty()) {
180
5
                Node node = max_heap.top();
181
5
                max_heap.pop();
182
5
                if (count == target) {
183
2
                    second_number = node.value;
184
3
                } else if (count == target + 1) {
185
2
                    first_number = node.value;
186
2
                    break;
187
2
                }
188
3
                ++count;
189
3
                if (--node.element_index >= 0) {
190
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
2
                    max_heap.push(node);
192
2
                }
193
3
            }
194
195
23
        } else {
196
23
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
69
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
46
                if (!_sorted_nums_vec[i].empty()) {
199
46
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
46
                }
201
46
            }
202
203
55
            while (!min_heap.empty()) {
204
55
                Node node = min_heap.top();
205
55
                min_heap.pop();
206
55
                if (count == target) {
207
23
                    first_number = node.value;
208
32
                } else if (count == target + 1) {
209
23
                    second_number = node.value;
210
23
                    break;
211
23
                }
212
32
                ++count;
213
32
                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
32
            }
218
23
        }
219
220
25
        return {first_number, second_number};
221
25
    }
222
223
    PODArray<Ty> _nums;
224
    std::vector<PODArray<Ty>> _sorted_nums_vec;
225
};
226
227
} // namespace doris