Coverage Report

Created: 2026-01-05 13:55

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 "udf/udf.h"
27
#include "vec/common/pod_array.h"
28
#include "vec/common/string_buffer.hpp"
29
#include "vec/io/io_helper.h"
30
31
namespace doris {
32
33
template <typename Ty>
34
class Counts {
35
public:
36
3.49k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
43
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
213
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
2.12k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
828
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
39
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
36
9
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
240
    Counts() = default;
37
38
1.22k
    void merge(Counts* other) {
39
1.22k
        if (other != nullptr && !other->_nums.empty()) {
40
1.22k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.22k
        }
42
1.22k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
38
3
    void merge(Counts* other) {
39
3
        if (other != nullptr && !other->_nums.empty()) {
40
3
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
3
        }
42
3
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
38
15
    void merge(Counts* other) {
39
15
        if (other != nullptr && !other->_nums.empty()) {
40
15
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
15
        }
42
15
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
781
    void merge(Counts* other) {
39
781
        if (other != nullptr && !other->_nums.empty()) {
40
781
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
781
        }
42
781
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
347
    void merge(Counts* other) {
39
347
        if (other != nullptr && !other->_nums.empty()) {
40
347
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
347
        }
42
347
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
38
3
    void merge(Counts* other) {
39
3
        if (other != nullptr && !other->_nums.empty()) {
40
3
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
3
        }
42
3
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
38
3
    void merge(Counts* other) {
39
3
        if (other != nullptr && !other->_nums.empty()) {
40
3
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
3
        }
42
3
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
77
    void merge(Counts* other) {
39
77
        if (other != nullptr && !other->_nums.empty()) {
40
77
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
77
        }
42
77
    }
43
44
    void increment(Ty key, uint32_t i) {
45
        auto old_size = _nums.size();
46
        _nums.resize(_nums.size() + i);
47
        for (uint32_t j = 0; j < i; ++j) {
48
            _nums[old_size + j] = key;
49
        }
50
    }
51
52
2.92k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
52
73
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
52
552
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
52
1.59k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
52
401
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
52
45
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIfE9incrementEf
Line
Count
Source
52
9
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
52
259
    void increment(Ty key) { _nums.push_back(key); }
53
54
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
5
        _nums.insert(keys.begin(), keys.end());
56
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
54
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
5
        _nums.insert(keys.begin(), keys.end());
56
5
    }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
57
58
1.64k
    void serialize(vectorized::BufferWritable& buf) {
59
1.64k
        if (!_nums.empty()) {
60
1.34k
            pdqsort(_nums.begin(), _nums.end());
61
1.34k
            size_t size = _nums.size();
62
1.34k
            buf.write_binary(size);
63
1.34k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.34k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
294
            _convert_sorted_num_vec_to_nums();
67
294
            serialize(buf);
68
294
        }
69
1.64k
    }
_ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
3
    void serialize(vectorized::BufferWritable& buf) {
59
3
        if (!_nums.empty()) {
60
3
            pdqsort(_nums.begin(), _nums.end());
61
3
            size_t size = _nums.size();
62
3
            buf.write_binary(size);
63
3
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
3
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
3
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
15
    void serialize(vectorized::BufferWritable& buf) {
59
15
        if (!_nums.empty()) {
60
15
            pdqsort(_nums.begin(), _nums.end());
61
15
            size_t size = _nums.size();
62
15
            buf.write_binary(size);
63
15
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
15
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
15
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
1.05k
    void serialize(vectorized::BufferWritable& buf) {
59
1.05k
        if (!_nums.empty()) {
60
898
            pdqsort(_nums.begin(), _nums.end());
61
898
            size_t size = _nums.size();
62
898
            buf.write_binary(size);
63
898
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
898
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
156
            _convert_sorted_num_vec_to_nums();
67
156
            serialize(buf);
68
156
        }
69
1.05k
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
485
    void serialize(vectorized::BufferWritable& buf) {
59
485
        if (!_nums.empty()) {
60
347
            pdqsort(_nums.begin(), _nums.end());
61
347
            size_t size = _nums.size();
62
347
            buf.write_binary(size);
63
347
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
347
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
138
            _convert_sorted_num_vec_to_nums();
67
138
            serialize(buf);
68
138
        }
69
485
    }
_ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
3
    void serialize(vectorized::BufferWritable& buf) {
59
3
        if (!_nums.empty()) {
60
3
            pdqsort(_nums.begin(), _nums.end());
61
3
            size_t size = _nums.size();
62
3
            buf.write_binary(size);
63
3
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
3
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
3
    }
_ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
3
    void serialize(vectorized::BufferWritable& buf) {
59
3
        if (!_nums.empty()) {
60
3
            pdqsort(_nums.begin(), _nums.end());
61
3
            size_t size = _nums.size();
62
3
            buf.write_binary(size);
63
3
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
3
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
3
    }
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
77
    void serialize(vectorized::BufferWritable& buf) {
59
77
        if (!_nums.empty()) {
60
77
            pdqsort(_nums.begin(), _nums.end());
61
77
            size_t size = _nums.size();
62
77
            buf.write_binary(size);
63
77
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
77
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
77
    }
70
71
1.22k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.22k
        size_t size;
73
1.22k
        buf.read_binary(size);
74
1.22k
        _nums.resize(size);
75
1.22k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.22k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.22k
    }
_ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
3
    void unserialize(vectorized::BufferReadable& buf) {
72
3
        size_t size;
73
3
        buf.read_binary(size);
74
3
        _nums.resize(size);
75
3
        auto buff = buf.read(sizeof(Ty) * size);
76
3
        memcpy(_nums.data(), buff.data, buff.size);
77
3
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
15
    void unserialize(vectorized::BufferReadable& buf) {
72
15
        size_t size;
73
15
        buf.read_binary(size);
74
15
        _nums.resize(size);
75
15
        auto buff = buf.read(sizeof(Ty) * size);
76
15
        memcpy(_nums.data(), buff.data, buff.size);
77
15
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
781
    void unserialize(vectorized::BufferReadable& buf) {
72
781
        size_t size;
73
781
        buf.read_binary(size);
74
781
        _nums.resize(size);
75
781
        auto buff = buf.read(sizeof(Ty) * size);
76
781
        memcpy(_nums.data(), buff.data, buff.size);
77
781
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
347
    void unserialize(vectorized::BufferReadable& buf) {
72
347
        size_t size;
73
347
        buf.read_binary(size);
74
347
        _nums.resize(size);
75
347
        auto buff = buf.read(sizeof(Ty) * size);
76
347
        memcpy(_nums.data(), buff.data, buff.size);
77
347
    }
_ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
3
    void unserialize(vectorized::BufferReadable& buf) {
72
3
        size_t size;
73
3
        buf.read_binary(size);
74
3
        _nums.resize(size);
75
3
        auto buff = buf.read(sizeof(Ty) * size);
76
3
        memcpy(_nums.data(), buff.data, buff.size);
77
3
    }
_ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
3
    void unserialize(vectorized::BufferReadable& buf) {
72
3
        size_t size;
73
3
        buf.read_binary(size);
74
3
        _nums.resize(size);
75
3
        auto buff = buf.read(sizeof(Ty) * size);
76
3
        memcpy(_nums.data(), buff.data, buff.size);
77
3
    }
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
77
    void unserialize(vectorized::BufferReadable& buf) {
72
77
        size_t size;
73
77
        buf.read_binary(size);
74
77
        _nums.resize(size);
75
77
        auto buff = buf.read(sizeof(Ty) * size);
76
77
        memcpy(_nums.data(), buff.data, buff.size);
77
77
    }
78
79
1.10k
    double terminate(double quantile) {
80
1.10k
        if (_sorted_nums_vec.size() <= 1) {
81
971
            if (_sorted_nums_vec.size() == 1) {
82
319
                _nums = std::move(_sorted_nums_vec[0]);
83
319
            }
84
85
971
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
971
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
180
                pdqsort(_nums.begin(), _nums.end());
93
180
            }
94
95
971
            if (quantile == 1 || _nums.size() == 1) {
96
473
                return _nums.back();
97
473
            }
98
99
498
            double u = (_nums.size() - 1) * quantile;
100
498
            auto index = static_cast<uint32_t>(u);
101
498
            return _nums[index] +
102
498
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
498
                                                       static_cast<double>(_nums[index]));
104
971
        } else {
105
133
            DCHECK(_nums.empty());
106
133
            size_t rows = 0;
107
314
            for (const auto& i : _sorted_nums_vec) {
108
314
                rows += i.size();
109
314
            }
110
133
            const bool reverse = quantile > 0.5 && rows > 2;
111
133
            double u = (rows - 1) * quantile;
112
133
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
133
            size_t target = reverse ? rows - index - 2 : index;
121
133
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
133
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
133
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
133
            return first_number +
129
133
                   (u - static_cast<double>(index)) *
130
133
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
133
        }
132
1.10k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
79
61
    double terminate(double quantile) {
80
61
        if (_sorted_nums_vec.size() <= 1) {
81
61
            if (_sorted_nums_vec.size() == 1) {
82
3
                _nums = std::move(_sorted_nums_vec[0]);
83
3
            }
84
85
61
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
61
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
24
                pdqsort(_nums.begin(), _nums.end());
93
24
            }
94
95
61
            if (quantile == 1 || _nums.size() == 1) {
96
34
                return _nums.back();
97
34
            }
98
99
27
            double u = (_nums.size() - 1) * quantile;
100
27
            auto index = static_cast<uint32_t>(u);
101
27
            return _nums[index] +
102
27
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
27
                                                       static_cast<double>(_nums[index]));
104
61
        } else {
105
0
            DCHECK(_nums.empty());
106
0
            size_t rows = 0;
107
0
            for (const auto& i : _sorted_nums_vec) {
108
0
                rows += i.size();
109
0
            }
110
0
            const bool reverse = quantile > 0.5 && rows > 2;
111
0
            double u = (rows - 1) * quantile;
112
0
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
0
            size_t target = reverse ? rows - index - 2 : index;
121
0
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
0
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
0
            return first_number +
129
0
                   (u - static_cast<double>(index)) *
130
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
0
        }
132
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
79
299
    double terminate(double quantile) {
80
299
        if (_sorted_nums_vec.size() <= 1) {
81
296
            if (_sorted_nums_vec.size() == 1) {
82
3
                _nums = std::move(_sorted_nums_vec[0]);
83
3
            }
84
85
296
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
296
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
144
                pdqsort(_nums.begin(), _nums.end());
93
144
            }
94
95
297
            if (quantile == 1 || _nums.size() == 1) {
96
87
                return _nums.back();
97
87
            }
98
99
209
            double u = (_nums.size() - 1) * quantile;
100
209
            auto index = static_cast<uint32_t>(u);
101
209
            return _nums[index] +
102
209
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
209
                                                       static_cast<double>(_nums[index]));
104
296
        } else {
105
3
            DCHECK(_nums.empty());
106
3
            size_t rows = 0;
107
12
            for (const auto& i : _sorted_nums_vec) {
108
12
                rows += i.size();
109
12
            }
110
3
            const bool reverse = quantile > 0.5 && rows > 2;
111
3
            double u = (rows - 1) * quantile;
112
3
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
3
            size_t target = reverse ? rows - index - 2 : index;
121
3
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
3
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
3
            return first_number +
129
3
                   (u - static_cast<double>(index)) *
130
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
3
        }
132
299
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
444
    double terminate(double quantile) {
80
444
        if (_sorted_nums_vec.size() <= 1) {
81
358
            if (_sorted_nums_vec.size() == 1) {
82
240
                _nums = std::move(_sorted_nums_vec[0]);
83
240
            }
84
85
358
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
358
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
11
                pdqsort(_nums.begin(), _nums.end());
93
11
            }
94
95
358
            if (quantile == 1 || _nums.size() == 1) {
96
239
                return _nums.back();
97
239
            }
98
99
119
            double u = (_nums.size() - 1) * quantile;
100
119
            auto index = static_cast<uint32_t>(u);
101
119
            return _nums[index] +
102
119
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
119
                                                       static_cast<double>(_nums[index]));
104
358
        } else {
105
86
            DCHECK(_nums.empty());
106
86
            size_t rows = 0;
107
202
            for (const auto& i : _sorted_nums_vec) {
108
202
                rows += i.size();
109
202
            }
110
86
            const bool reverse = quantile > 0.5 && rows > 2;
111
86
            double u = (rows - 1) * quantile;
112
86
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
86
            size_t target = reverse ? rows - index - 2 : index;
121
86
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
86
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
86
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
86
            return first_number +
129
86
                   (u - static_cast<double>(index)) *
130
86
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
86
        }
132
444
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
128
    double terminate(double quantile) {
80
128
        if (_sorted_nums_vec.size() <= 1) {
81
108
            if (_sorted_nums_vec.size() == 1) {
82
38
                _nums = std::move(_sorted_nums_vec[0]);
83
38
            }
84
85
108
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
108
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
108
            if (quantile == 1 || _nums.size() == 1) {
96
59
                return _nums.back();
97
59
            }
98
99
49
            double u = (_nums.size() - 1) * quantile;
100
49
            auto index = static_cast<uint32_t>(u);
101
49
            return _nums[index] +
102
49
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
49
                                                       static_cast<double>(_nums[index]));
104
108
        } else {
105
20
            DCHECK(_nums.empty());
106
20
            size_t rows = 0;
107
52
            for (const auto& i : _sorted_nums_vec) {
108
52
                rows += i.size();
109
52
            }
110
20
            const bool reverse = quantile > 0.5 && rows > 2;
111
20
            double u = (rows - 1) * quantile;
112
20
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
20
            size_t target = reverse ? rows - index - 2 : index;
121
20
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
20
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
20
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
20
            return first_number +
129
20
                   (u - static_cast<double>(index)) *
130
20
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
20
        }
132
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
79
33
    double terminate(double quantile) {
80
33
        if (_sorted_nums_vec.size() <= 1) {
81
33
            if (_sorted_nums_vec.size() == 1) {
82
3
                _nums = std::move(_sorted_nums_vec[0]);
83
3
            }
84
85
33
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
33
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
33
            if (quantile == 1 || _nums.size() == 1) {
96
24
                return _nums.back();
97
24
            }
98
99
9
            double u = (_nums.size() - 1) * quantile;
100
9
            auto index = static_cast<uint32_t>(u);
101
9
            return _nums[index] +
102
9
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
9
                                                       static_cast<double>(_nums[index]));
104
33
        } else {
105
0
            DCHECK(_nums.empty());
106
0
            size_t rows = 0;
107
0
            for (const auto& i : _sorted_nums_vec) {
108
0
                rows += i.size();
109
0
            }
110
0
            const bool reverse = quantile > 0.5 && rows > 2;
111
0
            double u = (rows - 1) * quantile;
112
0
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
0
            size_t target = reverse ? rows - index - 2 : index;
121
0
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
0
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
0
            return first_number +
129
0
                   (u - static_cast<double>(index)) *
130
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
0
        }
132
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
79
3
    double terminate(double quantile) {
80
3
        if (_sorted_nums_vec.size() <= 1) {
81
3
            if (_sorted_nums_vec.size() == 1) {
82
3
                _nums = std::move(_sorted_nums_vec[0]);
83
3
            }
84
85
3
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
3
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
3
            if (quantile == 1 || _nums.size() == 1) {
96
0
                return _nums.back();
97
0
            }
98
99
3
            double u = (_nums.size() - 1) * quantile;
100
3
            auto index = static_cast<uint32_t>(u);
101
3
            return _nums[index] +
102
3
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
3
                                                       static_cast<double>(_nums[index]));
104
3
        } else {
105
0
            DCHECK(_nums.empty());
106
0
            size_t rows = 0;
107
0
            for (const auto& i : _sorted_nums_vec) {
108
0
                rows += i.size();
109
0
            }
110
0
            const bool reverse = quantile > 0.5 && rows > 2;
111
0
            double u = (rows - 1) * quantile;
112
0
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
0
            size_t target = reverse ? rows - index - 2 : index;
121
0
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
0
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
0
            return first_number +
129
0
                   (u - static_cast<double>(index)) *
130
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
0
        }
132
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
79
136
    double terminate(double quantile) {
80
136
        if (_sorted_nums_vec.size() <= 1) {
81
112
            if (_sorted_nums_vec.size() == 1) {
82
29
                _nums = std::move(_sorted_nums_vec[0]);
83
29
            }
84
85
112
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
112
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
112
            if (quantile == 1 || _nums.size() == 1) {
96
30
                return _nums.back();
97
30
            }
98
99
82
            double u = (_nums.size() - 1) * quantile;
100
82
            auto index = static_cast<uint32_t>(u);
101
82
            return _nums[index] +
102
82
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
82
                                                       static_cast<double>(_nums[index]));
104
112
        } else {
105
24
            DCHECK(_nums.empty());
106
24
            size_t rows = 0;
107
48
            for (const auto& i : _sorted_nums_vec) {
108
48
                rows += i.size();
109
48
            }
110
24
            const bool reverse = quantile > 0.5 && rows > 2;
111
24
            double u = (rows - 1) * quantile;
112
24
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
24
            size_t target = reverse ? rows - index - 2 : index;
121
24
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
24
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
24
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
24
            return first_number +
129
24
                   (u - static_cast<double>(index)) *
130
24
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
24
        }
132
136
    }
133
134
private:
135
    struct Node {
136
        Ty value;
137
        int array_index;
138
        int64_t element_index;
139
140
1.12k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
140
68
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
140
648
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
140
367
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
140
37
        auto operator<=>(const Node& other) const { return value <=> other.value; }
141
    };
142
143
294
    void _convert_sorted_num_vec_to_nums() {
144
294
        size_t rows = 0;
145
596
        for (const auto& i : _sorted_nums_vec) {
146
596
            rows += i.size();
147
596
        }
148
294
        _nums.resize(rows);
149
294
        size_t count = 0;
150
151
294
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
890
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
596
            if (!_sorted_nums_vec[i].empty()) {
154
596
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
596
            }
156
596
        }
157
158
1.03k
        while (!min_heap.empty()) {
159
743
            Node node = min_heap.top();
160
743
            min_heap.pop();
161
743
            _nums[count++] = node.value;
162
743
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
147
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
147
                min_heap.push(node);
165
147
            }
166
743
        }
167
294
        _sorted_nums_vec.clear();
168
294
    }
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
143
156
    void _convert_sorted_num_vec_to_nums() {
144
156
        size_t rows = 0;
145
339
        for (const auto& i : _sorted_nums_vec) {
146
339
            rows += i.size();
147
339
        }
148
156
        _nums.resize(rows);
149
156
        size_t count = 0;
150
151
156
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
495
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
339
            if (!_sorted_nums_vec[i].empty()) {
154
339
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
339
            }
156
339
        }
157
158
531
        while (!min_heap.empty()) {
159
375
            Node node = min_heap.top();
160
375
            min_heap.pop();
161
375
            _nums[count++] = node.value;
162
375
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
36
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
36
                min_heap.push(node);
165
36
            }
166
375
        }
167
156
        _sorted_nums_vec.clear();
168
156
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
143
138
    void _convert_sorted_num_vec_to_nums() {
144
138
        size_t rows = 0;
145
257
        for (const auto& i : _sorted_nums_vec) {
146
257
            rows += i.size();
147
257
        }
148
138
        _nums.resize(rows);
149
138
        size_t count = 0;
150
151
138
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
395
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
257
            if (!_sorted_nums_vec[i].empty()) {
154
257
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
257
            }
156
257
        }
157
158
506
        while (!min_heap.empty()) {
159
368
            Node node = min_heap.top();
160
368
            min_heap.pop();
161
368
            _nums[count++] = node.value;
162
368
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
111
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
111
                min_heap.push(node);
165
111
            }
166
368
        }
167
138
        _sorted_nums_vec.clear();
168
138
    }
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
169
170
133
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
133
        Ty first_number = 0, second_number = 0;
172
133
        size_t count = 0;
173
133
        if (reverse) {
174
23
            std::priority_queue<Node> max_heap;
175
86
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
63
                if (!_sorted_nums_vec[i].empty()) {
177
63
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
63
                                     _sorted_nums_vec[i].size() - 1);
179
63
                }
180
63
            }
181
182
102
            while (!max_heap.empty()) {
183
102
                Node node = max_heap.top();
184
102
                max_heap.pop();
185
102
                if (count == target) {
186
23
                    second_number = node.value;
187
79
                } else if (count == target + 1) {
188
23
                    first_number = node.value;
189
23
                    break;
190
23
                }
191
79
                ++count;
192
79
                if (--node.element_index >= 0) {
193
77
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
77
                    max_heap.push(node);
195
77
                }
196
79
            }
197
198
110
        } else {
199
110
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
361
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
251
                if (!_sorted_nums_vec[i].empty()) {
202
251
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
251
                }
204
251
            }
205
206
313
            while (!min_heap.empty()) {
207
313
                Node node = min_heap.top();
208
313
                min_heap.pop();
209
313
                if (count == target) {
210
110
                    first_number = node.value;
211
203
                } else if (count == target + 1) {
212
110
                    second_number = node.value;
213
110
                    break;
214
110
                }
215
203
                ++count;
216
203
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
131
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
131
                    min_heap.push(node);
219
131
                }
220
203
            }
221
110
        }
222
223
133
        return {first_number, second_number};
224
133
    }
Unexecuted instantiation: _ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
3
        Ty first_number = 0, second_number = 0;
172
3
        size_t count = 0;
173
3
        if (reverse) {
174
1
            std::priority_queue<Node> max_heap;
175
5
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
4
                if (!_sorted_nums_vec[i].empty()) {
177
4
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
4
                                     _sorted_nums_vec[i].size() - 1);
179
4
                }
180
4
            }
181
182
6
            while (!max_heap.empty()) {
183
6
                Node node = max_heap.top();
184
6
                max_heap.pop();
185
6
                if (count == target) {
186
1
                    second_number = node.value;
187
5
                } else if (count == target + 1) {
188
1
                    first_number = node.value;
189
1
                    break;
190
1
                }
191
5
                ++count;
192
5
                if (--node.element_index >= 0) {
193
3
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
3
                    max_heap.push(node);
195
3
                }
196
5
            }
197
198
2
        } else {
199
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
10
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
8
                if (!_sorted_nums_vec[i].empty()) {
202
8
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
8
                }
204
8
            }
205
206
13
            while (!min_heap.empty()) {
207
13
                Node node = min_heap.top();
208
13
                min_heap.pop();
209
13
                if (count == target) {
210
2
                    first_number = node.value;
211
11
                } else if (count == target + 1) {
212
2
                    second_number = node.value;
213
2
                    break;
214
2
                }
215
11
                ++count;
216
11
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
11
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
11
                    min_heap.push(node);
219
11
                }
220
11
            }
221
2
        }
222
223
3
        return {first_number, second_number};
224
3
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
86
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
86
        Ty first_number = 0, second_number = 0;
172
86
        size_t count = 0;
173
86
        if (reverse) {
174
6
            std::priority_queue<Node> max_heap;
175
22
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
16
                if (!_sorted_nums_vec[i].empty()) {
177
16
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
16
                                     _sorted_nums_vec[i].size() - 1);
179
16
                }
180
16
            }
181
182
31
            while (!max_heap.empty()) {
183
31
                Node node = max_heap.top();
184
31
                max_heap.pop();
185
31
                if (count == target) {
186
6
                    second_number = node.value;
187
25
                } else if (count == target + 1) {
188
6
                    first_number = node.value;
189
6
                    break;
190
6
                }
191
25
                ++count;
192
25
                if (--node.element_index >= 0) {
193
25
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
25
                    max_heap.push(node);
195
25
                }
196
25
            }
197
198
80
        } else {
199
80
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
266
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
186
                if (!_sorted_nums_vec[i].empty()) {
202
186
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
186
                }
204
186
            }
205
206
228
            while (!min_heap.empty()) {
207
228
                Node node = min_heap.top();
208
228
                min_heap.pop();
209
228
                if (count == target) {
210
80
                    first_number = node.value;
211
148
                } else if (count == target + 1) {
212
80
                    second_number = node.value;
213
80
                    break;
214
80
                }
215
148
                ++count;
216
148
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
95
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
95
                    min_heap.push(node);
219
95
                }
220
148
            }
221
80
        }
222
223
86
        return {first_number, second_number};
224
86
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
20
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
20
        Ty first_number = 0, second_number = 0;
172
20
        size_t count = 0;
173
20
        if (reverse) {
174
14
            std::priority_queue<Node> max_heap;
175
53
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
39
                if (!_sorted_nums_vec[i].empty()) {
177
39
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
39
                                     _sorted_nums_vec[i].size() - 1);
179
39
                }
180
39
            }
181
182
60
            while (!max_heap.empty()) {
183
60
                Node node = max_heap.top();
184
60
                max_heap.pop();
185
60
                if (count == target) {
186
14
                    second_number = node.value;
187
46
                } else if (count == target + 1) {
188
14
                    first_number = node.value;
189
14
                    break;
190
14
                }
191
46
                ++count;
192
46
                if (--node.element_index >= 0) {
193
46
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
46
                    max_heap.push(node);
195
46
                }
196
46
            }
197
198
14
        } else {
199
6
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
19
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
13
                if (!_sorted_nums_vec[i].empty()) {
202
13
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
13
                }
204
13
            }
205
206
20
            while (!min_heap.empty()) {
207
20
                Node node = min_heap.top();
208
20
                min_heap.pop();
209
20
                if (count == target) {
210
6
                    first_number = node.value;
211
14
                } else if (count == target + 1) {
212
6
                    second_number = node.value;
213
6
                    break;
214
6
                }
215
14
                ++count;
216
14
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
13
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
13
                    min_heap.push(node);
219
13
                }
220
14
            }
221
6
        }
222
223
20
        return {first_number, second_number};
224
20
    }
Unexecuted instantiation: _ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
24
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
24
        Ty first_number = 0, second_number = 0;
172
24
        size_t count = 0;
173
24
        if (reverse) {
174
2
            std::priority_queue<Node> max_heap;
175
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
4
                if (!_sorted_nums_vec[i].empty()) {
177
4
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
4
                                     _sorted_nums_vec[i].size() - 1);
179
4
                }
180
4
            }
181
182
5
            while (!max_heap.empty()) {
183
5
                Node node = max_heap.top();
184
5
                max_heap.pop();
185
5
                if (count == target) {
186
2
                    second_number = node.value;
187
3
                } else if (count == target + 1) {
188
2
                    first_number = node.value;
189
2
                    break;
190
2
                }
191
3
                ++count;
192
3
                if (--node.element_index >= 0) {
193
3
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
3
                    max_heap.push(node);
195
3
                }
196
3
            }
197
198
22
        } else {
199
22
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
66
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
44
                if (!_sorted_nums_vec[i].empty()) {
202
44
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
44
                }
204
44
            }
205
206
52
            while (!min_heap.empty()) {
207
52
                Node node = min_heap.top();
208
52
                min_heap.pop();
209
52
                if (count == target) {
210
22
                    first_number = node.value;
211
30
                } else if (count == target + 1) {
212
22
                    second_number = node.value;
213
22
                    break;
214
22
                }
215
30
                ++count;
216
30
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
12
                    min_heap.push(node);
219
12
                }
220
30
            }
221
22
        }
222
223
24
        return {first_number, second_number};
224
24
    }
225
226
    vectorized::PODArray<Ty> _nums;
227
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
228
};
229
230
} // namespace doris