Coverage Report

Created: 2026-02-06 04:14

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