Coverage Report

Created: 2026-04-10 12:12

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.24k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
47
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
156
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
1.98k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
756
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
43
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
13
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
246
    Counts() = default;
36
37
1.13k
    void merge(Counts* other) {
38
1.13k
        if (other != nullptr && !other->_nums.empty()) {
39
1.13k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
1.13k
        }
41
1.13k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
37
5
    void merge(Counts* other) {
38
5
        if (other != nullptr && !other->_nums.empty()) {
39
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
5
        }
41
5
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
37
11
    void merge(Counts* other) {
38
11
        if (other != nullptr && !other->_nums.empty()) {
39
11
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
11
        }
41
11
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
37
721
    void merge(Counts* other) {
38
721
        if (other != nullptr && !other->_nums.empty()) {
39
721
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
721
        }
41
721
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
306
    void merge(Counts* other) {
38
306
        if (other != nullptr && !other->_nums.empty()) {
39
306
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
306
        }
41
306
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
37
5
    void merge(Counts* other) {
38
5
        if (other != nullptr && !other->_nums.empty()) {
39
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
5
        }
41
5
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
37
5
    void merge(Counts* other) {
38
5
        if (other != nullptr && !other->_nums.empty()) {
39
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
5
        }
41
5
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
37
79
    void merge(Counts* other) {
38
79
        if (other != nullptr && !other->_nums.empty()) {
39
79
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
79
        }
41
79
    }
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.52k
    void serialize(BufferWritable& buf) {
56
1.52k
        if (!_nums.empty()) {
57
1.25k
            pdqsort(_nums.begin(), _nums.end());
58
1.25k
            size_t size = _nums.size();
59
1.25k
            buf.write_binary(size);
60
1.25k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
1.25k
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
270
            _convert_sorted_num_vec_to_nums();
64
270
            serialize(buf);
65
270
        }
66
1.52k
    }
_ZN5doris6CountsIaE9serializeERNS_14BufferWritableE
Line
Count
Source
55
5
    void serialize(BufferWritable& buf) {
56
5
        if (!_nums.empty()) {
57
5
            pdqsort(_nums.begin(), _nums.end());
58
5
            size_t size = _nums.size();
59
5
            buf.write_binary(size);
60
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
5
        } 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
5
    }
_ZN5doris6CountsIsE9serializeERNS_14BufferWritableE
Line
Count
Source
55
11
    void serialize(BufferWritable& buf) {
56
11
        if (!_nums.empty()) {
57
11
            pdqsort(_nums.begin(), _nums.end());
58
11
            size_t size = _nums.size();
59
11
            buf.write_binary(size);
60
11
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
11
        } 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
11
    }
_ZN5doris6CountsIiE9serializeERNS_14BufferWritableE
Line
Count
Source
55
994
    void serialize(BufferWritable& buf) {
56
994
        if (!_nums.empty()) {
57
838
            pdqsort(_nums.begin(), _nums.end());
58
838
            size_t size = _nums.size();
59
838
            buf.write_binary(size);
60
838
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
838
        } 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
994
    }
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
55
429
    void serialize(BufferWritable& buf) {
56
429
        if (!_nums.empty()) {
57
315
            pdqsort(_nums.begin(), _nums.end());
58
315
            size_t size = _nums.size();
59
315
            buf.write_binary(size);
60
315
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
315
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
114
            _convert_sorted_num_vec_to_nums();
64
114
            serialize(buf);
65
114
        }
66
429
    }
_ZN5doris6CountsInE9serializeERNS_14BufferWritableE
Line
Count
Source
55
5
    void serialize(BufferWritable& buf) {
56
5
        if (!_nums.empty()) {
57
5
            pdqsort(_nums.begin(), _nums.end());
58
5
            size_t size = _nums.size();
59
5
            buf.write_binary(size);
60
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
5
        } 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
5
    }
_ZN5doris6CountsIfE9serializeERNS_14BufferWritableE
Line
Count
Source
55
5
    void serialize(BufferWritable& buf) {
56
5
        if (!_nums.empty()) {
57
5
            pdqsort(_nums.begin(), _nums.end());
58
5
            size_t size = _nums.size();
59
5
            buf.write_binary(size);
60
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
5
        } 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
5
    }
_ZN5doris6CountsIdE9serializeERNS_14BufferWritableE
Line
Count
Source
55
79
    void serialize(BufferWritable& buf) {
56
79
        if (!_nums.empty()) {
57
79
            pdqsort(_nums.begin(), _nums.end());
58
79
            size_t size = _nums.size();
59
79
            buf.write_binary(size);
60
79
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
79
        } 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
79
    }
67
68
1.13k
    void unserialize(BufferReadable& buf) {
69
1.13k
        size_t size;
70
1.13k
        buf.read_binary(size);
71
1.13k
        _nums.resize(size);
72
1.13k
        auto buff = buf.read(sizeof(Ty) * size);
73
1.13k
        memcpy(_nums.data(), buff.data, buff.size);
74
1.13k
    }
_ZN5doris6CountsIaE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
5
    void unserialize(BufferReadable& buf) {
69
5
        size_t size;
70
5
        buf.read_binary(size);
71
5
        _nums.resize(size);
72
5
        auto buff = buf.read(sizeof(Ty) * size);
73
5
        memcpy(_nums.data(), buff.data, buff.size);
74
5
    }
_ZN5doris6CountsIsE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
11
    void unserialize(BufferReadable& buf) {
69
11
        size_t size;
70
11
        buf.read_binary(size);
71
11
        _nums.resize(size);
72
11
        auto buff = buf.read(sizeof(Ty) * size);
73
11
        memcpy(_nums.data(), buff.data, buff.size);
74
11
    }
_ZN5doris6CountsIiE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
721
    void unserialize(BufferReadable& buf) {
69
721
        size_t size;
70
721
        buf.read_binary(size);
71
721
        _nums.resize(size);
72
721
        auto buff = buf.read(sizeof(Ty) * size);
73
721
        memcpy(_nums.data(), buff.data, buff.size);
74
721
    }
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
306
    void unserialize(BufferReadable& buf) {
69
306
        size_t size;
70
306
        buf.read_binary(size);
71
306
        _nums.resize(size);
72
306
        auto buff = buf.read(sizeof(Ty) * size);
73
306
        memcpy(_nums.data(), buff.data, buff.size);
74
306
    }
_ZN5doris6CountsInE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
5
    void unserialize(BufferReadable& buf) {
69
5
        size_t size;
70
5
        buf.read_binary(size);
71
5
        _nums.resize(size);
72
5
        auto buff = buf.read(sizeof(Ty) * size);
73
5
        memcpy(_nums.data(), buff.data, buff.size);
74
5
    }
_ZN5doris6CountsIfE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
5
    void unserialize(BufferReadable& buf) {
69
5
        size_t size;
70
5
        buf.read_binary(size);
71
5
        _nums.resize(size);
72
5
        auto buff = buf.read(sizeof(Ty) * size);
73
5
        memcpy(_nums.data(), buff.data, buff.size);
74
5
    }
_ZN5doris6CountsIdE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
79
    void unserialize(BufferReadable& buf) {
69
79
        size_t size;
70
79
        buf.read_binary(size);
71
79
        _nums.resize(size);
72
79
        auto buff = buf.read(sizeof(Ty) * size);
73
79
        memcpy(_nums.data(), buff.data, buff.size);
74
79
    }
75
76
1.00k
    double terminate(double quantile) {
77
1.00k
        if (_sorted_nums_vec.size() <= 1) {
78
896
            if (_sorted_nums_vec.size() == 1) {
79
346
                _nums = std::move(_sorted_nums_vec[0]);
80
346
            }
81
82
896
            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
896
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
132
                pdqsort(_nums.begin(), _nums.end());
90
132
            }
91
92
896
            if (quantile == 1 || _nums.size() == 1) {
93
439
                return _nums.back();
94
439
            }
95
96
457
            double u = (_nums.size() - 1) * quantile;
97
457
            auto index = static_cast<uint32_t>(u);
98
457
            return _nums[index] +
99
457
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
457
                                                       static_cast<double>(_nums[index]));
101
896
        } else {
102
106
            DCHECK(_nums.empty());
103
106
            size_t rows = 0;
104
225
            for (const auto& i : _sorted_nums_vec) {
105
225
                rows += i.size();
106
225
            }
107
106
            const bool reverse = quantile > 0.5 && rows > 2;
108
106
            double u = (rows - 1) * quantile;
109
106
            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
106
            size_t target = reverse ? rows - index - 2 : index;
118
106
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
106
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
106
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
106
            return first_number +
126
106
                   (u - static_cast<double>(index)) *
127
106
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
106
        }
129
1.00k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
76
61
    double terminate(double quantile) {
77
61
        if (_sorted_nums_vec.size() <= 1) {
78
59
            if (_sorted_nums_vec.size() == 1) {
79
1
                _nums = std::move(_sorted_nums_vec[0]);
80
1
            }
81
82
59
            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
59
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
24
                pdqsort(_nums.begin(), _nums.end());
90
24
            }
91
92
59
            if (quantile == 1 || _nums.size() == 1) {
93
34
                return _nums.back();
94
34
            }
95
96
25
            double u = (_nums.size() - 1) * quantile;
97
25
            auto index = static_cast<uint32_t>(u);
98
25
            return _nums[index] +
99
25
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
25
                                                       static_cast<double>(_nums[index]));
101
59
        } else {
102
2
            DCHECK(_nums.empty());
103
2
            size_t rows = 0;
104
4
            for (const auto& i : _sorted_nums_vec) {
105
4
                rows += i.size();
106
4
            }
107
2
            const bool reverse = quantile > 0.5 && rows > 2;
108
2
            double u = (rows - 1) * quantile;
109
2
            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
2
            size_t target = reverse ? rows - index - 2 : index;
118
2
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
2
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
2
            return first_number +
126
2
                   (u - static_cast<double>(index)) *
127
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
2
        }
129
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
76
212
    double terminate(double quantile) {
77
212
        if (_sorted_nums_vec.size() <= 1) {
78
207
            if (_sorted_nums_vec.size() == 1) {
79
1
                _nums = std::move(_sorted_nums_vec[0]);
80
1
            }
81
82
207
            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
207
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
96
                pdqsort(_nums.begin(), _nums.end());
90
96
            }
91
92
207
            if (quantile == 1 || _nums.size() == 1) {
93
66
                return _nums.back();
94
66
            }
95
96
141
            double u = (_nums.size() - 1) * quantile;
97
141
            auto index = static_cast<uint32_t>(u);
98
141
            return _nums[index] +
99
141
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
141
                                                       static_cast<double>(_nums[index]));
101
207
        } else {
102
5
            DCHECK(_nums.empty());
103
5
            size_t rows = 0;
104
10
            for (const auto& i : _sorted_nums_vec) {
105
10
                rows += i.size();
106
10
            }
107
5
            const bool reverse = quantile > 0.5 && rows > 2;
108
5
            double u = (rows - 1) * quantile;
109
5
            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
5
            size_t target = reverse ? rows - index - 2 : index;
118
5
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
5
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
5
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
5
            return first_number +
126
5
                   (u - static_cast<double>(index)) *
127
5
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
5
        }
129
212
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
76
429
    double terminate(double quantile) {
77
429
        if (_sorted_nums_vec.size() <= 1) {
78
373
            if (_sorted_nums_vec.size() == 1) {
79
270
                _nums = std::move(_sorted_nums_vec[0]);
80
270
            }
81
82
373
            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
373
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
11
                pdqsort(_nums.begin(), _nums.end());
90
11
            }
91
92
373
            if (quantile == 1 || _nums.size() == 1) {
93
226
                return _nums.back();
94
226
            }
95
96
147
            double u = (_nums.size() - 1) * quantile;
97
147
            auto index = static_cast<uint32_t>(u);
98
147
            return _nums[index] +
99
147
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
147
                                                       static_cast<double>(_nums[index]));
101
373
        } else {
102
56
            DCHECK(_nums.empty());
103
56
            size_t rows = 0;
104
112
            for (const auto& i : _sorted_nums_vec) {
105
112
                rows += i.size();
106
112
            }
107
56
            const bool reverse = quantile > 0.5 && rows > 2;
108
56
            double u = (rows - 1) * quantile;
109
56
            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
56
            size_t target = reverse ? rows - index - 2 : index;
118
56
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
56
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
56
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
56
            return first_number +
126
56
                   (u - static_cast<double>(index)) *
127
56
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
56
        }
129
429
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
76
128
    double terminate(double quantile) {
77
128
        if (_sorted_nums_vec.size() <= 1) {
78
115
            if (_sorted_nums_vec.size() == 1) {
79
45
                _nums = std::move(_sorted_nums_vec[0]);
80
45
            }
81
82
115
            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
115
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
1
                pdqsort(_nums.begin(), _nums.end());
90
1
            }
91
92
115
            if (quantile == 1 || _nums.size() == 1) {
93
59
                return _nums.back();
94
59
            }
95
96
56
            double u = (_nums.size() - 1) * quantile;
97
56
            auto index = static_cast<uint32_t>(u);
98
56
            return _nums[index] +
99
56
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
56
                                                       static_cast<double>(_nums[index]));
101
115
        } else {
102
13
            DCHECK(_nums.empty());
103
13
            size_t rows = 0;
104
39
            for (const auto& i : _sorted_nums_vec) {
105
39
                rows += i.size();
106
39
            }
107
13
            const bool reverse = quantile > 0.5 && rows > 2;
108
13
            double u = (rows - 1) * quantile;
109
13
            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
13
            size_t target = reverse ? rows - index - 2 : index;
118
13
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
13
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
13
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
13
            return first_number +
126
13
                   (u - static_cast<double>(index)) *
127
13
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
13
        }
129
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
76
33
    double terminate(double quantile) {
77
33
        if (_sorted_nums_vec.size() <= 1) {
78
31
            if (_sorted_nums_vec.size() == 1) {
79
1
                _nums = std::move(_sorted_nums_vec[0]);
80
1
            }
81
82
31
            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
31
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
31
            if (quantile == 1 || _nums.size() == 1) {
93
24
                return _nums.back();
94
24
            }
95
96
7
            double u = (_nums.size() - 1) * quantile;
97
7
            auto index = static_cast<uint32_t>(u);
98
7
            return _nums[index] +
99
7
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
7
                                                       static_cast<double>(_nums[index]));
101
31
        } else {
102
2
            DCHECK(_nums.empty());
103
2
            size_t rows = 0;
104
4
            for (const auto& i : _sorted_nums_vec) {
105
4
                rows += i.size();
106
4
            }
107
2
            const bool reverse = quantile > 0.5 && rows > 2;
108
2
            double u = (rows - 1) * quantile;
109
2
            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
2
            size_t target = reverse ? rows - index - 2 : index;
118
2
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
2
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
2
            return first_number +
126
2
                   (u - static_cast<double>(index)) *
127
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
2
        }
129
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
76
3
    double terminate(double quantile) {
77
3
        if (_sorted_nums_vec.size() <= 1) {
78
1
            if (_sorted_nums_vec.size() == 1) {
79
1
                _nums = std::move(_sorted_nums_vec[0]);
80
1
            }
81
82
1
            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
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
1
            if (quantile == 1 || _nums.size() == 1) {
93
0
                return _nums.back();
94
0
            }
95
96
1
            double u = (_nums.size() - 1) * quantile;
97
1
            auto index = static_cast<uint32_t>(u);
98
1
            return _nums[index] +
99
1
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
1
                                                       static_cast<double>(_nums[index]));
101
2
        } else {
102
2
            DCHECK(_nums.empty());
103
2
            size_t rows = 0;
104
4
            for (const auto& i : _sorted_nums_vec) {
105
4
                rows += i.size();
106
4
            }
107
2
            const bool reverse = quantile > 0.5 && rows > 2;
108
2
            double u = (rows - 1) * quantile;
109
2
            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
2
            size_t target = reverse ? rows - index - 2 : index;
118
2
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
2
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
2
            return first_number +
126
2
                   (u - static_cast<double>(index)) *
127
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
2
        }
129
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
76
136
    double terminate(double quantile) {
77
136
        if (_sorted_nums_vec.size() <= 1) {
78
110
            if (_sorted_nums_vec.size() == 1) {
79
27
                _nums = std::move(_sorted_nums_vec[0]);
80
27
            }
81
82
110
            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
110
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
110
            if (quantile == 1 || _nums.size() == 1) {
93
30
                return _nums.back();
94
30
            }
95
96
80
            double u = (_nums.size() - 1) * quantile;
97
80
            auto index = static_cast<uint32_t>(u);
98
80
            return _nums[index] +
99
80
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
80
                                                       static_cast<double>(_nums[index]));
101
110
        } else {
102
26
            DCHECK(_nums.empty());
103
26
            size_t rows = 0;
104
52
            for (const auto& i : _sorted_nums_vec) {
105
52
                rows += i.size();
106
52
            }
107
26
            const bool reverse = quantile > 0.5 && rows > 2;
108
26
            double u = (rows - 1) * quantile;
109
26
            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
26
            size_t target = reverse ? rows - index - 2 : index;
118
26
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
26
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
26
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
26
            return first_number +
126
26
                   (u - static_cast<double>(index)) *
127
26
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
26
        }
129
136
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
898
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIaE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
137
23
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
485
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
331
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsInE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIfE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
137
47
        auto operator<=>(const Node& other) const { return value <=> other.value; }
138
    };
139
140
270
    void _convert_sorted_num_vec_to_nums() {
141
270
        size_t rows = 0;
142
561
        for (const auto& i : _sorted_nums_vec) {
143
561
            rows += i.size();
144
561
        }
145
270
        _nums.resize(rows);
146
270
        size_t count = 0;
147
148
270
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
831
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
561
            if (!_sorted_nums_vec[i].empty()) {
151
561
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
561
            }
153
561
        }
154
155
989
        while (!min_heap.empty()) {
156
719
            Node node = min_heap.top();
157
719
            min_heap.pop();
158
719
            _nums[count++] = node.value;
159
719
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
158
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
158
                min_heap.push(node);
162
158
            }
163
719
        }
164
270
        _sorted_nums_vec.clear();
165
270
    }
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
114
    void _convert_sorted_num_vec_to_nums() {
141
114
        size_t rows = 0;
142
222
        for (const auto& i : _sorted_nums_vec) {
143
222
            rows += i.size();
144
222
        }
145
114
        _nums.resize(rows);
146
114
        size_t count = 0;
147
148
114
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
336
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
222
            if (!_sorted_nums_vec[i].empty()) {
151
222
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
222
            }
153
222
        }
154
155
458
        while (!min_heap.empty()) {
156
344
            Node node = min_heap.top();
157
344
            min_heap.pop();
158
344
            _nums[count++] = node.value;
159
344
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
122
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
122
                min_heap.push(node);
162
122
            }
163
344
        }
164
114
        _sorted_nums_vec.clear();
165
114
    }
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
106
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
106
        Ty first_number = 0, second_number = 0;
169
106
        size_t count = 0;
170
106
        if (reverse) {
171
26
            std::priority_queue<Node> max_heap;
172
91
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
65
                if (!_sorted_nums_vec[i].empty()) {
174
65
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
65
                                     _sorted_nums_vec[i].size() - 1);
176
65
                }
177
65
            }
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
26
                    second_number = node.value;
184
76
                } else if (count == target + 1) {
185
26
                    first_number = node.value;
186
26
                    break;
187
26
                }
188
76
                ++count;
189
76
                if (--node.element_index >= 0) {
190
67
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
67
                    max_heap.push(node);
192
67
                }
193
76
            }
194
195
80
        } else {
196
80
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
240
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
160
                if (!_sorted_nums_vec[i].empty()) {
199
160
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
160
                }
201
160
            }
202
203
258
            while (!min_heap.empty()) {
204
258
                Node node = min_heap.top();
205
258
                min_heap.pop();
206
258
                if (count == target) {
207
80
                    first_number = node.value;
208
178
                } else if (count == target + 1) {
209
80
                    second_number = node.value;
210
80
                    break;
211
80
                }
212
178
                ++count;
213
178
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
157
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
157
                    min_heap.push(node);
216
157
                }
217
178
            }
218
80
        }
219
220
106
        return {first_number, second_number};
221
106
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
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
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
5
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
5
        Ty first_number = 0, second_number = 0;
169
5
        size_t count = 0;
170
5
        if (reverse) {
171
2
            std::priority_queue<Node> max_heap;
172
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
4
                if (!_sorted_nums_vec[i].empty()) {
174
4
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
4
                                     _sorted_nums_vec[i].size() - 1);
176
4
                }
177
4
            }
178
179
8
            while (!max_heap.empty()) {
180
8
                Node node = max_heap.top();
181
8
                max_heap.pop();
182
8
                if (count == target) {
183
2
                    second_number = node.value;
184
6
                } else if (count == target + 1) {
185
2
                    first_number = node.value;
186
2
                    break;
187
2
                }
188
6
                ++count;
189
6
                if (--node.element_index >= 0) {
190
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
6
                    max_heap.push(node);
192
6
                }
193
6
            }
194
195
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
5
        return {first_number, second_number};
221
5
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
56
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
56
        Ty first_number = 0, second_number = 0;
169
56
        size_t count = 0;
170
56
        if (reverse) {
171
9
            std::priority_queue<Node> max_heap;
172
27
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
18
                if (!_sorted_nums_vec[i].empty()) {
174
18
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
18
                                     _sorted_nums_vec[i].size() - 1);
176
18
                }
177
18
            }
178
179
37
            while (!max_heap.empty()) {
180
37
                Node node = max_heap.top();
181
37
                max_heap.pop();
182
37
                if (count == target) {
183
9
                    second_number = node.value;
184
28
                } else if (count == target + 1) {
185
9
                    first_number = node.value;
186
9
                    break;
187
9
                }
188
28
                ++count;
189
28
                if (--node.element_index >= 0) {
190
24
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
24
                    max_heap.push(node);
192
24
                }
193
28
            }
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
56
        return {first_number, second_number};
221
56
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
13
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
13
        Ty first_number = 0, second_number = 0;
169
13
        size_t count = 0;
170
13
        if (reverse) {
171
9
            std::priority_queue<Node> max_heap;
172
40
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
31
                if (!_sorted_nums_vec[i].empty()) {
174
31
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
31
                                     _sorted_nums_vec[i].size() - 1);
176
31
                }
177
31
            }
178
179
44
            while (!max_heap.empty()) {
180
44
                Node node = max_heap.top();
181
44
                max_heap.pop();
182
44
                if (count == target) {
183
9
                    second_number = node.value;
184
35
                } else if (count == target + 1) {
185
9
                    first_number = node.value;
186
9
                    break;
187
9
                }
188
35
                ++count;
189
35
                if (--node.element_index >= 0) {
190
31
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
31
                    max_heap.push(node);
192
31
                }
193
35
            }
194
195
9
        } 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
13
        return {first_number, second_number};
221
13
    }
_ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
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
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
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
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
26
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
26
        Ty first_number = 0, second_number = 0;
169
26
        size_t count = 0;
170
26
        if (reverse) {
171
3
            std::priority_queue<Node> max_heap;
172
9
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
6
                if (!_sorted_nums_vec[i].empty()) {
174
6
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
6
                                     _sorted_nums_vec[i].size() - 1);
176
6
                }
177
6
            }
178
179
7
            while (!max_heap.empty()) {
180
7
                Node node = max_heap.top();
181
7
                max_heap.pop();
182
7
                if (count == target) {
183
3
                    second_number = node.value;
184
4
                } else if (count == target + 1) {
185
3
                    first_number = node.value;
186
3
                    break;
187
3
                }
188
4
                ++count;
189
4
                if (--node.element_index >= 0) {
190
3
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
3
                    max_heap.push(node);
192
3
                }
193
4
            }
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
18
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
18
                    min_heap.push(node);
216
18
                }
217
32
            }
218
23
        }
219
220
26
        return {first_number, second_number};
221
26
    }
222
223
    PODArray<Ty> _nums;
224
    std::vector<PODArray<Ty>> _sorted_nums_vec;
225
};
226
227
} // namespace doris