Coverage Report

Created: 2026-03-25 17:26

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.35k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
45
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
197
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
2.00k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
801
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
41
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
11
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
254
    Counts() = default;
36
37
1.15k
    void merge(Counts* other) {
38
1.15k
        if (other != nullptr && !other->_nums.empty()) {
39
1.15k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
1.15k
        }
41
1.15k
    }
_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
7
    void merge(Counts* other) {
38
7
        if (other != nullptr && !other->_nums.empty()) {
39
7
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
7
        }
41
7
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
37
720
    void merge(Counts* other) {
38
720
        if (other != nullptr && !other->_nums.empty()) {
39
720
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
720
        }
41
720
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
333
    void merge(Counts* other) {
38
333
        if (other != nullptr && !other->_nums.empty()) {
39
333
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
333
        }
41
333
    }
_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
84
    void merge(Counts* other) {
38
84
        if (other != nullptr && !other->_nums.empty()) {
39
84
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
84
        }
41
84
    }
42
43
    void increment(Ty key, uint32_t i) {
44
        auto old_size = _nums.size();
45
        _nums.resize(_nums.size() + i);
46
        for (uint32_t j = 0; j < i; ++j) {
47
            _nums[old_size + j] = key;
48
        }
49
    }
50
51
2.93k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
51
73
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
51
552
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
51
1.59k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
51
401
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
51
45
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIfE9incrementEf
Line
Count
Source
51
9
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
51
259
    void increment(Ty key) { _nums.push_back(key); }
52
53
5
    void increment_batch(const 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.56k
    void serialize(BufferWritable& buf) {
56
1.56k
        if (!_nums.empty()) {
57
1.27k
            pdqsort(_nums.begin(), _nums.end());
58
1.27k
            size_t size = _nums.size();
59
1.27k
            buf.write_binary(size);
60
1.27k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
1.27k
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
295
            _convert_sorted_num_vec_to_nums();
64
295
            serialize(buf);
65
295
        }
66
1.56k
    }
_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
7
    void serialize(BufferWritable& buf) {
56
7
        if (!_nums.empty()) {
57
7
            pdqsort(_nums.begin(), _nums.end());
58
7
            size_t size = _nums.size();
59
7
            buf.write_binary(size);
60
7
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
7
        } 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
7
    }
_ZN5doris6CountsIiE9serializeERNS_14BufferWritableE
Line
Count
Source
55
993
    void serialize(BufferWritable& buf) {
56
993
        if (!_nums.empty()) {
57
837
            pdqsort(_nums.begin(), _nums.end());
58
837
            size_t size = _nums.size();
59
837
            buf.write_binary(size);
60
837
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
837
        } 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
993
    }
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
55
472
    void serialize(BufferWritable& buf) {
56
472
        if (!_nums.empty()) {
57
333
            pdqsort(_nums.begin(), _nums.end());
58
333
            size_t size = _nums.size();
59
333
            buf.write_binary(size);
60
333
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
333
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
139
            _convert_sorted_num_vec_to_nums();
64
139
            serialize(buf);
65
139
        }
66
472
    }
_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
84
    void serialize(BufferWritable& buf) {
56
84
        if (!_nums.empty()) {
57
84
            pdqsort(_nums.begin(), _nums.end());
58
84
            size_t size = _nums.size();
59
84
            buf.write_binary(size);
60
84
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
84
        } 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
84
    }
67
68
1.15k
    void unserialize(BufferReadable& buf) {
69
1.15k
        size_t size;
70
1.15k
        buf.read_binary(size);
71
1.15k
        _nums.resize(size);
72
1.15k
        auto buff = buf.read(sizeof(Ty) * size);
73
1.15k
        memcpy(_nums.data(), buff.data, buff.size);
74
1.15k
    }
_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
7
    void unserialize(BufferReadable& buf) {
69
7
        size_t size;
70
7
        buf.read_binary(size);
71
7
        _nums.resize(size);
72
7
        auto buff = buf.read(sizeof(Ty) * size);
73
7
        memcpy(_nums.data(), buff.data, buff.size);
74
7
    }
_ZN5doris6CountsIiE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
720
    void unserialize(BufferReadable& buf) {
69
720
        size_t size;
70
720
        buf.read_binary(size);
71
720
        _nums.resize(size);
72
720
        auto buff = buf.read(sizeof(Ty) * size);
73
720
        memcpy(_nums.data(), buff.data, buff.size);
74
720
    }
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
333
    void unserialize(BufferReadable& buf) {
69
333
        size_t size;
70
333
        buf.read_binary(size);
71
333
        _nums.resize(size);
72
333
        auto buff = buf.read(sizeof(Ty) * size);
73
333
        memcpy(_nums.data(), buff.data, buff.size);
74
333
    }
_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
84
    void unserialize(BufferReadable& buf) {
69
84
        size_t size;
70
84
        buf.read_binary(size);
71
84
        _nums.resize(size);
72
84
        auto buff = buf.read(sizeof(Ty) * size);
73
84
        memcpy(_nums.data(), buff.data, buff.size);
74
84
    }
75
76
1.10k
    double terminate(double quantile) {
77
1.10k
        if (_sorted_nums_vec.size() <= 1) {
78
1.01k
            if (_sorted_nums_vec.size() == 1) {
79
358
                _nums = std::move(_sorted_nums_vec[0]);
80
358
            }
81
82
1.01k
            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
1.01k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
180
                pdqsort(_nums.begin(), _nums.end());
90
180
            }
91
92
1.01k
            if (quantile == 1 || _nums.size() == 1) {
93
473
                return _nums.back();
94
473
            }
95
96
538
            double u = (_nums.size() - 1) * quantile;
97
538
            auto index = static_cast<uint32_t>(u);
98
538
            return _nums[index] +
99
538
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
538
                                                       static_cast<double>(_nums[index]));
101
1.01k
        } else {
102
94
            DCHECK(_nums.empty());
103
94
            size_t rows = 0;
104
202
            for (const auto& i : _sorted_nums_vec) {
105
202
                rows += i.size();
106
202
            }
107
94
            const bool reverse = quantile > 0.5 && rows > 2;
108
94
            double u = (rows - 1) * quantile;
109
94
            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
94
            size_t target = reverse ? rows - index - 2 : index;
118
94
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
94
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
94
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
94
            return first_number +
126
94
                   (u - static_cast<double>(index)) *
127
94
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
94
        }
129
1.10k
    }
_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
300
    double terminate(double quantile) {
77
300
        if (_sorted_nums_vec.size() <= 1) {
78
299
            if (_sorted_nums_vec.size() == 1) {
79
5
                _nums = std::move(_sorted_nums_vec[0]);
80
5
            }
81
82
299
            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
299
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
144
                pdqsort(_nums.begin(), _nums.end());
90
144
            }
91
92
299
            if (quantile == 1 || _nums.size() == 1) {
93
87
                return _nums.back();
94
87
            }
95
96
212
            double u = (_nums.size() - 1) * quantile;
97
212
            auto index = static_cast<uint32_t>(u);
98
212
            return _nums[index] +
99
212
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
212
                                                       static_cast<double>(_nums[index]));
101
299
        } 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
300
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
76
444
    double terminate(double quantile) {
77
444
        if (_sorted_nums_vec.size() <= 1) {
78
389
            if (_sorted_nums_vec.size() == 1) {
79
271
                _nums = std::move(_sorted_nums_vec[0]);
80
271
            }
81
82
389
            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
389
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
11
                pdqsort(_nums.begin(), _nums.end());
90
11
            }
91
92
389
            if (quantile == 1 || _nums.size() == 1) {
93
239
                return _nums.back();
94
239
            }
95
96
150
            double u = (_nums.size() - 1) * quantile;
97
150
            auto index = static_cast<uint32_t>(u);
98
150
            return _nums[index] +
99
150
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
150
                                                       static_cast<double>(_nums[index]));
101
389
        } else {
102
55
            DCHECK(_nums.empty());
103
55
            size_t rows = 0;
104
110
            for (const auto& i : _sorted_nums_vec) {
105
110
                rows += i.size();
106
110
            }
107
55
            const bool reverse = quantile > 0.5 && rows > 2;
108
55
            double u = (rows - 1) * quantile;
109
55
            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
55
            size_t target = reverse ? rows - index - 2 : index;
118
55
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
55
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
55
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
55
            return first_number +
126
55
                   (u - static_cast<double>(index)) *
127
55
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
55
        }
129
444
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
76
128
    double terminate(double quantile) {
77
128
        if (_sorted_nums_vec.size() <= 1) {
78
118
            if (_sorted_nums_vec.size() == 1) {
79
48
                _nums = std::move(_sorted_nums_vec[0]);
80
48
            }
81
82
118
            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
118
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
1
                pdqsort(_nums.begin(), _nums.end());
90
1
            }
91
92
118
            if (quantile == 1 || _nums.size() == 1) {
93
59
                return _nums.back();
94
59
            }
95
96
59
            double u = (_nums.size() - 1) * quantile;
97
59
            auto index = static_cast<uint32_t>(u);
98
59
            return _nums[index] +
99
59
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
59
                                                       static_cast<double>(_nums[index]));
101
118
        } else {
102
10
            DCHECK(_nums.empty());
103
10
            size_t rows = 0;
104
28
            for (const auto& i : _sorted_nums_vec) {
105
28
                rows += i.size();
106
28
            }
107
10
            const bool reverse = quantile > 0.5 && rows > 2;
108
10
            double u = (rows - 1) * quantile;
109
10
            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
10
            size_t target = reverse ? rows - index - 2 : index;
118
10
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
10
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
10
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
10
            return first_number +
126
10
                   (u - static_cast<double>(index)) *
127
10
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
10
        }
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
56
            for (const auto& i : _sorted_nums_vec) {
105
56
                rows += i.size();
106
56
            }
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
858
        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
2
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
483
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
309
        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
58
        auto operator<=>(const Node& other) const { return value <=> other.value; }
138
    };
139
140
295
    void _convert_sorted_num_vec_to_nums() {
141
295
        size_t rows = 0;
142
596
        for (const auto& i : _sorted_nums_vec) {
143
596
            rows += i.size();
144
596
        }
145
295
        _nums.resize(rows);
146
295
        size_t count = 0;
147
148
295
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
891
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
596
            if (!_sorted_nums_vec[i].empty()) {
151
596
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
596
            }
153
596
        }
154
155
1.03k
        while (!min_heap.empty()) {
156
743
            Node node = min_heap.top();
157
743
            min_heap.pop();
158
743
            _nums[count++] = node.value;
159
743
            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
743
        }
164
295
        _sorted_nums_vec.clear();
165
295
    }
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
139
    void _convert_sorted_num_vec_to_nums() {
141
139
        size_t rows = 0;
142
257
        for (const auto& i : _sorted_nums_vec) {
143
257
            rows += i.size();
144
257
        }
145
139
        _nums.resize(rows);
146
139
        size_t count = 0;
147
148
139
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
396
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
257
            if (!_sorted_nums_vec[i].empty()) {
151
257
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
257
            }
153
257
        }
154
155
507
        while (!min_heap.empty()) {
156
368
            Node node = min_heap.top();
157
368
            min_heap.pop();
158
368
            _nums[count++] = node.value;
159
368
            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
368
        }
164
139
        _sorted_nums_vec.clear();
165
139
    }
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
94
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
94
        Ty first_number = 0, second_number = 0;
169
94
        size_t count = 0;
170
94
        if (reverse) {
171
16
            std::priority_queue<Node> max_heap;
172
57
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
41
                if (!_sorted_nums_vec[i].empty()) {
174
41
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
41
                                     _sorted_nums_vec[i].size() - 1);
176
41
                }
177
41
            }
178
179
70
            while (!max_heap.empty()) {
180
70
                Node node = max_heap.top();
181
70
                max_heap.pop();
182
70
                if (count == target) {
183
16
                    second_number = node.value;
184
54
                } else if (count == target + 1) {
185
16
                    first_number = node.value;
186
16
                    break;
187
16
                }
188
54
                ++count;
189
54
                if (--node.element_index >= 0) {
190
47
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
47
                    max_heap.push(node);
192
47
                }
193
54
            }
194
195
78
        } else {
196
78
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
239
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
161
                if (!_sorted_nums_vec[i].empty()) {
199
161
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
161
                }
201
161
            }
202
203
245
            while (!min_heap.empty()) {
204
245
                Node node = min_heap.top();
205
245
                min_heap.pop();
206
245
                if (count == target) {
207
78
                    first_number = node.value;
208
167
                } else if (count == target + 1) {
209
78
                    second_number = node.value;
210
78
                    break;
211
78
                }
212
167
                ++count;
213
167
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
143
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
143
                    min_heap.push(node);
216
143
                }
217
167
            }
218
78
        }
219
220
94
        return {first_number, second_number};
221
94
    }
_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
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
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
55
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
55
        Ty first_number = 0, second_number = 0;
169
55
        size_t count = 0;
170
55
        if (reverse) {
171
8
            std::priority_queue<Node> max_heap;
172
24
            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
35
            while (!max_heap.empty()) {
180
35
                Node node = max_heap.top();
181
35
                max_heap.pop();
182
35
                if (count == target) {
183
8
                    second_number = node.value;
184
27
                } else if (count == target + 1) {
185
8
                    first_number = node.value;
186
8
                    break;
187
8
                }
188
27
                ++count;
189
27
                if (--node.element_index >= 0) {
190
23
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
23
                    max_heap.push(node);
192
23
                }
193
27
            }
194
195
47
        } else {
196
47
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
141
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
94
                if (!_sorted_nums_vec[i].empty()) {
199
94
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
94
                }
201
94
            }
202
203
163
            while (!min_heap.empty()) {
204
163
                Node node = min_heap.top();
205
163
                min_heap.pop();
206
163
                if (count == target) {
207
47
                    first_number = node.value;
208
116
                } else if (count == target + 1) {
209
47
                    second_number = node.value;
210
47
                    break;
211
47
                }
212
116
                ++count;
213
116
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
114
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
114
                    min_heap.push(node);
216
114
                }
217
116
            }
218
47
        }
219
220
55
        return {first_number, second_number};
221
55
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
10
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
10
        Ty first_number = 0, second_number = 0;
169
10
        size_t count = 0;
170
10
        if (reverse) {
171
6
            std::priority_queue<Node> max_heap;
172
26
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
20
                if (!_sorted_nums_vec[i].empty()) {
174
20
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
20
                                     _sorted_nums_vec[i].size() - 1);
176
20
                }
177
20
            }
178
179
30
            while (!max_heap.empty()) {
180
30
                Node node = max_heap.top();
181
30
                max_heap.pop();
182
30
                if (count == target) {
183
6
                    second_number = node.value;
184
24
                } else if (count == target + 1) {
185
6
                    first_number = node.value;
186
6
                    break;
187
6
                }
188
24
                ++count;
189
24
                if (--node.element_index >= 0) {
190
22
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
22
                    max_heap.push(node);
192
22
                }
193
24
            }
194
195
6
        } 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
15
            while (!min_heap.empty()) {
204
15
                Node node = min_heap.top();
205
15
                min_heap.pop();
206
15
                if (count == target) {
207
4
                    first_number = node.value;
208
11
                } else if (count == target + 1) {
209
4
                    second_number = node.value;
210
4
                    break;
211
4
                }
212
11
                ++count;
213
11
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
10
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
10
                    min_heap.push(node);
216
10
                }
217
11
            }
218
4
        }
219
220
10
        return {first_number, second_number};
221
10
    }
_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
7
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
5
                if (!_sorted_nums_vec[i].empty()) {
174
5
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
5
                                     _sorted_nums_vec[i].size() - 1);
176
5
                }
177
5
            }
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
74
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
51
                if (!_sorted_nums_vec[i].empty()) {
199
51
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
51
                }
201
51
            }
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
15
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
15
                    min_heap.push(node);
216
15
                }
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