Coverage Report

Created: 2026-03-19 10: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.69k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
45
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
215
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
2.23k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
884
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
41
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
11
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
266
    Counts() = default;
36
37
1.34k
    void merge(Counts* other) {
38
1.34k
        if (other != nullptr && !other->_nums.empty()) {
39
1.34k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
1.34k
        }
41
1.34k
    }
_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
16
    void merge(Counts* other) {
38
16
        if (other != nullptr && !other->_nums.empty()) {
39
16
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
16
        }
41
16
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
37
836
    void merge(Counts* other) {
38
836
        if (other != nullptr && !other->_nums.empty()) {
39
836
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
836
        }
41
836
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
387
    void merge(Counts* other) {
38
387
        if (other != nullptr && !other->_nums.empty()) {
39
387
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
387
        }
41
387
    }
_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
90
    void merge(Counts* other) {
38
90
        if (other != nullptr && !other->_nums.empty()) {
39
90
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
90
        }
41
90
    }
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.73k
    void serialize(BufferWritable& buf) {
56
1.73k
        if (!_nums.empty()) {
57
1.43k
            pdqsort(_nums.begin(), _nums.end());
58
1.43k
            size_t size = _nums.size();
59
1.43k
            buf.write_binary(size);
60
1.43k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
1.43k
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
298
            _convert_sorted_num_vec_to_nums();
64
298
            serialize(buf);
65
298
        }
66
1.73k
    }
_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
16
    void serialize(BufferWritable& buf) {
56
16
        if (!_nums.empty()) {
57
16
            pdqsort(_nums.begin(), _nums.end());
58
16
            size_t size = _nums.size();
59
16
            buf.write_binary(size);
60
16
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
16
        } 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
16
    }
_ZN5doris6CountsIiE9serializeERNS_14BufferWritableE
Line
Count
Source
55
1.10k
    void serialize(BufferWritable& buf) {
56
1.10k
        if (!_nums.empty()) {
57
953
            pdqsort(_nums.begin(), _nums.end());
58
953
            size_t size = _nums.size();
59
953
            buf.write_binary(size);
60
953
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
953
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
156
            _convert_sorted_num_vec_to_nums();
64
156
            serialize(buf);
65
156
        }
66
1.10k
    }
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
55
504
    void serialize(BufferWritable& buf) {
56
504
        if (!_nums.empty()) {
57
362
            pdqsort(_nums.begin(), _nums.end());
58
362
            size_t size = _nums.size();
59
362
            buf.write_binary(size);
60
362
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
362
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
142
            _convert_sorted_num_vec_to_nums();
64
142
            serialize(buf);
65
142
        }
66
504
    }
_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
90
    void serialize(BufferWritable& buf) {
56
90
        if (!_nums.empty()) {
57
90
            pdqsort(_nums.begin(), _nums.end());
58
90
            size_t size = _nums.size();
59
90
            buf.write_binary(size);
60
90
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
90
        } 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
90
    }
67
68
1.34k
    void unserialize(BufferReadable& buf) {
69
1.34k
        size_t size;
70
1.34k
        buf.read_binary(size);
71
1.34k
        _nums.resize(size);
72
1.34k
        auto buff = buf.read(sizeof(Ty) * size);
73
1.34k
        memcpy(_nums.data(), buff.data, buff.size);
74
1.34k
    }
_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
16
    void unserialize(BufferReadable& buf) {
69
16
        size_t size;
70
16
        buf.read_binary(size);
71
16
        _nums.resize(size);
72
16
        auto buff = buf.read(sizeof(Ty) * size);
73
16
        memcpy(_nums.data(), buff.data, buff.size);
74
16
    }
_ZN5doris6CountsIiE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
836
    void unserialize(BufferReadable& buf) {
69
836
        size_t size;
70
836
        buf.read_binary(size);
71
836
        _nums.resize(size);
72
836
        auto buff = buf.read(sizeof(Ty) * size);
73
836
        memcpy(_nums.data(), buff.data, buff.size);
74
836
    }
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
387
    void unserialize(BufferReadable& buf) {
69
387
        size_t size;
70
387
        buf.read_binary(size);
71
387
        _nums.resize(size);
72
387
        auto buff = buf.read(sizeof(Ty) * size);
73
387
        memcpy(_nums.data(), buff.data, buff.size);
74
387
    }
_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
90
    void unserialize(BufferReadable& buf) {
69
90
        size_t size;
70
90
        buf.read_binary(size);
71
90
        _nums.resize(size);
72
90
        auto buff = buf.read(sizeof(Ty) * size);
73
90
        memcpy(_nums.data(), buff.data, buff.size);
74
90
    }
75
76
1.10k
    double terminate(double quantile) {
77
1.10k
        if (_sorted_nums_vec.size() <= 1) {
78
952
            if (_sorted_nums_vec.size() == 1) {
79
299
                _nums = std::move(_sorted_nums_vec[0]);
80
299
            }
81
82
952
            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
952
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
180
                pdqsort(_nums.begin(), _nums.end());
90
180
            }
91
92
952
            if (quantile == 1 || _nums.size() == 1) {
93
473
                return _nums.back();
94
473
            }
95
96
479
            double u = (_nums.size() - 1) * quantile;
97
479
            auto index = static_cast<uint32_t>(u);
98
479
            return _nums[index] +
99
479
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
479
                                                       static_cast<double>(_nums[index]));
101
952
        } else {
102
153
            DCHECK(_nums.empty());
103
153
            size_t rows = 0;
104
425
            for (const auto& i : _sorted_nums_vec) {
105
425
                rows += i.size();
106
425
            }
107
153
            const bool reverse = quantile > 0.5 && rows > 2;
108
153
            double u = (rows - 1) * quantile;
109
153
            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
153
            size_t target = reverse ? rows - index - 2 : index;
118
153
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
153
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
153
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
153
            return first_number +
126
153
                   (u - static_cast<double>(index)) *
127
153
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
153
        }
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
296
            if (_sorted_nums_vec.size() == 1) {
79
2
                _nums = std::move(_sorted_nums_vec[0]);
80
2
            }
81
82
296
            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
296
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
144
                pdqsort(_nums.begin(), _nums.end());
90
144
            }
91
92
296
            if (quantile == 1 || _nums.size() == 1) {
93
87
                return _nums.back();
94
87
            }
95
96
209
            double u = (_nums.size() - 1) * quantile;
97
209
            auto index = static_cast<uint32_t>(u);
98
209
            return _nums[index] +
99
209
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
209
                                                       static_cast<double>(_nums[index]));
101
296
        } else {
102
4
            DCHECK(_nums.empty());
103
4
            size_t rows = 0;
104
14
            for (const auto& i : _sorted_nums_vec) {
105
14
                rows += i.size();
106
14
            }
107
4
            const bool reverse = quantile > 0.5 && rows > 2;
108
4
            double u = (rows - 1) * quantile;
109
4
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
4
            size_t target = reverse ? rows - index - 2 : index;
118
4
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
4
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
4
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
4
            return first_number +
126
4
                   (u - static_cast<double>(index)) *
127
4
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
4
        }
129
300
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
76
444
    double terminate(double quantile) {
77
444
        if (_sorted_nums_vec.size() <= 1) {
78
346
            if (_sorted_nums_vec.size() == 1) {
79
228
                _nums = std::move(_sorted_nums_vec[0]);
80
228
            }
81
82
346
            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
346
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
11
                pdqsort(_nums.begin(), _nums.end());
90
11
            }
91
92
346
            if (quantile == 1 || _nums.size() == 1) {
93
239
                return _nums.back();
94
239
            }
95
96
107
            double u = (_nums.size() - 1) * quantile;
97
107
            auto index = static_cast<uint32_t>(u);
98
107
            return _nums[index] +
99
107
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
107
                                                       static_cast<double>(_nums[index]));
101
346
        } else {
102
98
            DCHECK(_nums.empty());
103
98
            size_t rows = 0;
104
269
            for (const auto& i : _sorted_nums_vec) {
105
269
                rows += i.size();
106
269
            }
107
98
            const bool reverse = quantile > 0.5 && rows > 2;
108
98
            double u = (rows - 1) * quantile;
109
98
            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
98
            size_t target = reverse ? rows - index - 2 : index;
118
98
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
98
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
98
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
98
            return first_number +
126
98
                   (u - static_cast<double>(index)) *
127
98
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
98
        }
129
444
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
76
128
    double terminate(double quantile) {
77
128
        if (_sorted_nums_vec.size() <= 1) {
78
105
            if (_sorted_nums_vec.size() == 1) {
79
35
                _nums = std::move(_sorted_nums_vec[0]);
80
35
            }
81
82
105
            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
105
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
1
                pdqsort(_nums.begin(), _nums.end());
90
1
            }
91
92
105
            if (quantile == 1 || _nums.size() == 1) {
93
59
                return _nums.back();
94
59
            }
95
96
46
            double u = (_nums.size() - 1) * quantile;
97
46
            auto index = static_cast<uint32_t>(u);
98
46
            return _nums[index] +
99
46
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
46
                                                       static_cast<double>(_nums[index]));
101
105
        } else {
102
23
            DCHECK(_nums.empty());
103
23
            size_t rows = 0;
104
74
            for (const auto& i : _sorted_nums_vec) {
105
74
                rows += i.size();
106
74
            }
107
23
            const bool reverse = quantile > 0.5 && rows > 2;
108
23
            double u = (rows - 1) * quantile;
109
23
            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
23
            size_t target = reverse ? rows - index - 2 : index;
118
23
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
23
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
23
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
23
            return first_number +
126
23
                   (u - static_cast<double>(index)) *
127
23
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
23
        }
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
62
            for (const auto& i : _sorted_nums_vec) {
105
62
                rows += i.size();
106
62
            }
107
25
            const bool reverse = quantile > 0.5 && rows > 2;
108
25
            double u = (rows - 1) * quantile;
109
25
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
25
            size_t target = reverse ? rows - index - 2 : index;
118
25
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
25
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
25
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
25
            return first_number +
126
25
                   (u - static_cast<double>(index)) *
127
25
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
25
        }
129
136
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
1.54k
        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
71
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
935
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
466
        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
70
        auto operator<=>(const Node& other) const { return value <=> other.value; }
138
    };
139
140
298
    void _convert_sorted_num_vec_to_nums() {
141
298
        size_t rows = 0;
142
617
        for (const auto& i : _sorted_nums_vec) {
143
617
            rows += i.size();
144
617
        }
145
298
        _nums.resize(rows);
146
298
        size_t count = 0;
147
148
298
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
915
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
617
            if (!_sorted_nums_vec[i].empty()) {
151
617
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
617
            }
153
617
        }
154
155
1.02k
        while (!min_heap.empty()) {
156
729
            Node node = min_heap.top();
157
729
            min_heap.pop();
158
729
            _nums[count++] = node.value;
159
729
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
112
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
112
                min_heap.push(node);
162
112
            }
163
729
        }
164
298
        _sorted_nums_vec.clear();
165
298
    }
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
142
    void _convert_sorted_num_vec_to_nums() {
141
142
        size_t rows = 0;
142
278
        for (const auto& i : _sorted_nums_vec) {
143
278
            rows += i.size();
144
278
        }
145
142
        _nums.resize(rows);
146
142
        size_t count = 0;
147
148
142
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
420
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
278
            if (!_sorted_nums_vec[i].empty()) {
151
278
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
278
            }
153
278
        }
154
155
496
        while (!min_heap.empty()) {
156
354
            Node node = min_heap.top();
157
354
            min_heap.pop();
158
354
            _nums[count++] = node.value;
159
354
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
76
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
76
                min_heap.push(node);
162
76
            }
163
354
        }
164
142
        _sorted_nums_vec.clear();
165
142
    }
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
153
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
153
        Ty first_number = 0, second_number = 0;
169
153
        size_t count = 0;
170
153
        if (reverse) {
171
28
            std::priority_queue<Node> max_heap;
172
122
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
94
                if (!_sorted_nums_vec[i].empty()) {
174
94
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
94
                                     _sorted_nums_vec[i].size() - 1);
176
94
                }
177
94
            }
178
179
113
            while (!max_heap.empty()) {
180
113
                Node node = max_heap.top();
181
113
                max_heap.pop();
182
113
                if (count == target) {
183
28
                    second_number = node.value;
184
85
                } else if (count == target + 1) {
185
28
                    first_number = node.value;
186
28
                    break;
187
28
                }
188
85
                ++count;
189
85
                if (--node.element_index >= 0) {
190
64
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
64
                    max_heap.push(node);
192
64
                }
193
85
            }
194
195
125
        } else {
196
125
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
456
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
331
                if (!_sorted_nums_vec[i].empty()) {
199
331
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
331
                }
201
331
            }
202
203
352
            while (!min_heap.empty()) {
204
352
                Node node = min_heap.top();
205
352
                min_heap.pop();
206
352
                if (count == target) {
207
125
                    first_number = node.value;
208
227
                } else if (count == target + 1) {
209
125
                    second_number = node.value;
210
125
                    break;
211
125
                }
212
227
                ++count;
213
227
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
141
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
141
                    min_heap.push(node);
216
141
                }
217
227
            }
218
125
        }
219
220
153
        return {first_number, second_number};
221
153
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
1
        Ty first_number = 0, second_number = 0;
169
1
        size_t count = 0;
170
1
        if (reverse) {
171
0
            std::priority_queue<Node> max_heap;
172
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
0
                if (!_sorted_nums_vec[i].empty()) {
174
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
0
                                     _sorted_nums_vec[i].size() - 1);
176
0
                }
177
0
            }
178
179
0
            while (!max_heap.empty()) {
180
0
                Node node = max_heap.top();
181
0
                max_heap.pop();
182
0
                if (count == target) {
183
0
                    second_number = node.value;
184
0
                } else if (count == target + 1) {
185
0
                    first_number = node.value;
186
0
                    break;
187
0
                }
188
0
                ++count;
189
0
                if (--node.element_index >= 0) {
190
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
0
                    max_heap.push(node);
192
0
                }
193
0
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
1
        return {first_number, second_number};
221
1
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
4
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
4
        Ty first_number = 0, second_number = 0;
169
4
        size_t count = 0;
170
4
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
5
            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
6
            while (!max_heap.empty()) {
180
6
                Node node = max_heap.top();
181
6
                max_heap.pop();
182
6
                if (count == target) {
183
1
                    second_number = node.value;
184
5
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
5
                ++count;
189
5
                if (--node.element_index >= 0) {
190
3
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
3
                    max_heap.push(node);
192
3
                }
193
5
            }
194
195
3
        } else {
196
3
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
13
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
10
                if (!_sorted_nums_vec[i].empty()) {
199
10
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
10
                }
201
10
            }
202
203
16
            while (!min_heap.empty()) {
204
16
                Node node = min_heap.top();
205
16
                min_heap.pop();
206
16
                if (count == target) {
207
3
                    first_number = node.value;
208
13
                } else if (count == target + 1) {
209
3
                    second_number = node.value;
210
3
                    break;
211
3
                }
212
13
                ++count;
213
13
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
12
                    min_heap.push(node);
216
12
                }
217
13
            }
218
3
        }
219
220
4
        return {first_number, second_number};
221
4
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
98
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
98
        Ty first_number = 0, second_number = 0;
169
98
        size_t count = 0;
170
98
        if (reverse) {
171
10
            std::priority_queue<Node> max_heap;
172
38
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
28
                if (!_sorted_nums_vec[i].empty()) {
174
28
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
28
                                     _sorted_nums_vec[i].size() - 1);
176
28
                }
177
28
            }
178
179
39
            while (!max_heap.empty()) {
180
39
                Node node = max_heap.top();
181
39
                max_heap.pop();
182
39
                if (count == target) {
183
10
                    second_number = node.value;
184
29
                } else if (count == target + 1) {
185
10
                    first_number = node.value;
186
10
                    break;
187
10
                }
188
29
                ++count;
189
29
                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
29
            }
194
195
88
        } else {
196
88
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
329
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
241
                if (!_sorted_nums_vec[i].empty()) {
199
241
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
241
                }
201
241
            }
202
203
247
            while (!min_heap.empty()) {
204
247
                Node node = min_heap.top();
205
247
                min_heap.pop();
206
247
                if (count == target) {
207
88
                    first_number = node.value;
208
159
                } else if (count == target + 1) {
209
88
                    second_number = node.value;
210
88
                    break;
211
88
                }
212
159
                ++count;
213
159
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
99
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
99
                    min_heap.push(node);
216
99
                }
217
159
            }
218
88
        }
219
220
98
        return {first_number, second_number};
221
98
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
23
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
23
        Ty first_number = 0, second_number = 0;
169
23
        size_t count = 0;
170
23
        if (reverse) {
171
15
            std::priority_queue<Node> max_heap;
172
71
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
56
                if (!_sorted_nums_vec[i].empty()) {
174
56
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
56
                                     _sorted_nums_vec[i].size() - 1);
176
56
                }
177
56
            }
178
179
63
            while (!max_heap.empty()) {
180
63
                Node node = max_heap.top();
181
63
                max_heap.pop();
182
63
                if (count == target) {
183
15
                    second_number = node.value;
184
48
                } else if (count == target + 1) {
185
15
                    first_number = node.value;
186
15
                    break;
187
15
                }
188
48
                ++count;
189
48
                if (--node.element_index >= 0) {
190
38
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
38
                    max_heap.push(node);
192
38
                }
193
48
            }
194
195
15
        } else {
196
8
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
26
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
18
                if (!_sorted_nums_vec[i].empty()) {
199
18
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
18
                }
201
18
            }
202
203
25
            while (!min_heap.empty()) {
204
25
                Node node = min_heap.top();
205
25
                min_heap.pop();
206
25
                if (count == target) {
207
8
                    first_number = node.value;
208
17
                } else if (count == target + 1) {
209
8
                    second_number = node.value;
210
8
                    break;
211
8
                }
212
17
                ++count;
213
17
                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
17
            }
218
8
        }
219
220
23
        return {first_number, second_number};
221
23
    }
_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
8
            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
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
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
3
            }
194
195
23
        } else {
196
23
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
79
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
56
                if (!_sorted_nums_vec[i].empty()) {
199
56
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
56
                }
201
56
            }
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
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
12
                    min_heap.push(node);
216
12
                }
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