Coverage Report

Created: 2026-03-16 18:36

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.47k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
43
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
195
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
2.07k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
842
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
39
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
9
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
276
    Counts() = default;
36
37
1.23k
    void merge(Counts* other) {
38
1.23k
        if (other != nullptr && !other->_nums.empty()) {
39
1.23k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
1.23k
        }
41
1.23k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
37
3
    void merge(Counts* other) {
38
3
        if (other != nullptr && !other->_nums.empty()) {
39
3
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
3
        }
41
3
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
37
6
    void merge(Counts* other) {
38
6
        if (other != nullptr && !other->_nums.empty()) {
39
6
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
6
        }
41
6
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
37
755
    void merge(Counts* other) {
38
755
        if (other != nullptr && !other->_nums.empty()) {
39
755
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
755
        }
41
755
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
366
    void merge(Counts* other) {
38
366
        if (other != nullptr && !other->_nums.empty()) {
39
366
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
366
        }
41
366
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
37
3
    void merge(Counts* other) {
38
3
        if (other != nullptr && !other->_nums.empty()) {
39
3
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
3
        }
41
3
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
37
3
    void merge(Counts* other) {
38
3
        if (other != nullptr && !other->_nums.empty()) {
39
3
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
3
        }
41
3
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
37
95
    void merge(Counts* other) {
38
95
        if (other != nullptr && !other->_nums.empty()) {
39
95
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
95
        }
41
95
    }
42
43
    void increment(Ty key, uint32_t i) {
44
        auto old_size = _nums.size();
45
        _nums.resize(_nums.size() + i);
46
        for (uint32_t j = 0; j < i; ++j) {
47
            _nums[old_size + j] = key;
48
        }
49
    }
50
51
2.93k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
51
73
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
51
552
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
51
1.59k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
51
401
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
51
45
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIfE9incrementEf
Line
Count
Source
51
9
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
51
259
    void increment(Ty key) { _nums.push_back(key); }
52
53
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
54
5
        _nums.insert(keys.begin(), keys.end());
55
5
    }
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_10vectorized8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
_ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
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_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
56
57
1.57k
    void serialize(vectorized::BufferWritable& buf) {
58
1.57k
        if (!_nums.empty()) {
59
1.28k
            pdqsort(_nums.begin(), _nums.end());
60
1.28k
            size_t size = _nums.size();
61
1.28k
            buf.write_binary(size);
62
1.28k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
1.28k
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
290
            _convert_sorted_num_vec_to_nums();
66
290
            serialize(buf);
67
290
        }
68
1.57k
    }
_ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
3
    void serialize(vectorized::BufferWritable& buf) {
58
3
        if (!_nums.empty()) {
59
3
            pdqsort(_nums.begin(), _nums.end());
60
3
            size_t size = _nums.size();
61
3
            buf.write_binary(size);
62
3
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
3
        } 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
3
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
6
    void serialize(vectorized::BufferWritable& buf) {
58
6
        if (!_nums.empty()) {
59
6
            pdqsort(_nums.begin(), _nums.end());
60
6
            size_t size = _nums.size();
61
6
            buf.write_binary(size);
62
6
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
6
        } 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
6
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
989
    void serialize(vectorized::BufferWritable& buf) {
58
989
        if (!_nums.empty()) {
59
833
            pdqsort(_nums.begin(), _nums.end());
60
833
            size_t size = _nums.size();
61
833
            buf.write_binary(size);
62
833
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
833
        } 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
989
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
475
    void serialize(vectorized::BufferWritable& buf) {
58
475
        if (!_nums.empty()) {
59
341
            pdqsort(_nums.begin(), _nums.end());
60
341
            size_t size = _nums.size();
61
341
            buf.write_binary(size);
62
341
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
341
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
134
            _convert_sorted_num_vec_to_nums();
66
134
            serialize(buf);
67
134
        }
68
475
    }
_ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
3
    void serialize(vectorized::BufferWritable& buf) {
58
3
        if (!_nums.empty()) {
59
3
            pdqsort(_nums.begin(), _nums.end());
60
3
            size_t size = _nums.size();
61
3
            buf.write_binary(size);
62
3
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
3
        } 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
3
    }
_ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
3
    void serialize(vectorized::BufferWritable& buf) {
58
3
        if (!_nums.empty()) {
59
3
            pdqsort(_nums.begin(), _nums.end());
60
3
            size_t size = _nums.size();
61
3
            buf.write_binary(size);
62
3
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
3
        } 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
3
    }
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
95
    void serialize(vectorized::BufferWritable& buf) {
58
95
        if (!_nums.empty()) {
59
95
            pdqsort(_nums.begin(), _nums.end());
60
95
            size_t size = _nums.size();
61
95
            buf.write_binary(size);
62
95
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
95
        } 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
95
    }
69
70
1.23k
    void unserialize(vectorized::BufferReadable& buf) {
71
1.23k
        size_t size;
72
1.23k
        buf.read_binary(size);
73
1.23k
        _nums.resize(size);
74
1.23k
        auto buff = buf.read(sizeof(Ty) * size);
75
1.23k
        memcpy(_nums.data(), buff.data, buff.size);
76
1.23k
    }
_ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
3
    void unserialize(vectorized::BufferReadable& buf) {
71
3
        size_t size;
72
3
        buf.read_binary(size);
73
3
        _nums.resize(size);
74
3
        auto buff = buf.read(sizeof(Ty) * size);
75
3
        memcpy(_nums.data(), buff.data, buff.size);
76
3
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
6
    void unserialize(vectorized::BufferReadable& buf) {
71
6
        size_t size;
72
6
        buf.read_binary(size);
73
6
        _nums.resize(size);
74
6
        auto buff = buf.read(sizeof(Ty) * size);
75
6
        memcpy(_nums.data(), buff.data, buff.size);
76
6
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
755
    void unserialize(vectorized::BufferReadable& buf) {
71
755
        size_t size;
72
755
        buf.read_binary(size);
73
755
        _nums.resize(size);
74
755
        auto buff = buf.read(sizeof(Ty) * size);
75
755
        memcpy(_nums.data(), buff.data, buff.size);
76
755
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
366
    void unserialize(vectorized::BufferReadable& buf) {
71
366
        size_t size;
72
366
        buf.read_binary(size);
73
366
        _nums.resize(size);
74
366
        auto buff = buf.read(sizeof(Ty) * size);
75
366
        memcpy(_nums.data(), buff.data, buff.size);
76
366
    }
_ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
3
    void unserialize(vectorized::BufferReadable& buf) {
71
3
        size_t size;
72
3
        buf.read_binary(size);
73
3
        _nums.resize(size);
74
3
        auto buff = buf.read(sizeof(Ty) * size);
75
3
        memcpy(_nums.data(), buff.data, buff.size);
76
3
    }
_ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
3
    void unserialize(vectorized::BufferReadable& buf) {
71
3
        size_t size;
72
3
        buf.read_binary(size);
73
3
        _nums.resize(size);
74
3
        auto buff = buf.read(sizeof(Ty) * size);
75
3
        memcpy(_nums.data(), buff.data, buff.size);
76
3
    }
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
95
    void unserialize(vectorized::BufferReadable& buf) {
71
95
        size_t size;
72
95
        buf.read_binary(size);
73
95
        _nums.resize(size);
74
95
        auto buff = buf.read(sizeof(Ty) * size);
75
95
        memcpy(_nums.data(), buff.data, buff.size);
76
95
    }
77
78
1.14k
    double terminate(double quantile) {
79
1.14k
        if (_sorted_nums_vec.size() <= 1) {
80
1.08k
            if (_sorted_nums_vec.size() == 1) {
81
434
                _nums = std::move(_sorted_nums_vec[0]);
82
434
            }
83
84
1.08k
            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.08k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
180
                pdqsort(_nums.begin(), _nums.end());
92
180
            }
93
94
1.08k
            if (quantile == 1 || _nums.size() == 1) {
95
476
                return _nums.back();
96
476
            }
97
98
611
            double u = (_nums.size() - 1) * quantile;
99
611
            auto index = static_cast<uint32_t>(u);
100
611
            return _nums[index] +
101
611
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
611
                                                       static_cast<double>(_nums[index]));
103
1.08k
        } else {
104
57
            DCHECK(_nums.empty());
105
57
            size_t rows = 0;
106
180
            for (const auto& i : _sorted_nums_vec) {
107
180
                rows += i.size();
108
180
            }
109
57
            const bool reverse = quantile > 0.5 && rows > 2;
110
57
            double u = (rows - 1) * quantile;
111
57
            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
57
            size_t target = reverse ? rows - index - 2 : index;
120
57
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
57
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
57
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
57
            return first_number +
128
57
                   (u - static_cast<double>(index)) *
129
57
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
57
        }
131
1.14k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
78
61
    double terminate(double quantile) {
79
61
        if (_sorted_nums_vec.size() <= 1) {
80
61
            if (_sorted_nums_vec.size() == 1) {
81
3
                _nums = std::move(_sorted_nums_vec[0]);
82
3
            }
83
84
61
            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
61
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
24
                pdqsort(_nums.begin(), _nums.end());
92
24
            }
93
94
61
            if (quantile == 1 || _nums.size() == 1) {
95
34
                return _nums.back();
96
34
            }
97
98
27
            double u = (_nums.size() - 1) * quantile;
99
27
            auto index = static_cast<uint32_t>(u);
100
27
            return _nums[index] +
101
27
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
27
                                                       static_cast<double>(_nums[index]));
103
61
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            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
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number +
128
0
                   (u - static_cast<double>(index)) *
129
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
0
        }
131
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
78
300
    double terminate(double quantile) {
79
300
        if (_sorted_nums_vec.size() <= 1) {
80
300
            if (_sorted_nums_vec.size() == 1) {
81
6
                _nums = std::move(_sorted_nums_vec[0]);
82
6
            }
83
84
300
            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
300
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
144
                pdqsort(_nums.begin(), _nums.end());
92
144
            }
93
94
300
            if (quantile == 1 || _nums.size() == 1) {
95
87
                return _nums.back();
96
87
            }
97
98
213
            double u = (_nums.size() - 1) * quantile;
99
213
            auto index = static_cast<uint32_t>(u);
100
213
            return _nums[index] +
101
213
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
213
                                                       static_cast<double>(_nums[index]));
103
300
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            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
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number +
128
0
                   (u - static_cast<double>(index)) *
129
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
0
        }
131
300
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
78
483
    double terminate(double quantile) {
79
483
        if (_sorted_nums_vec.size() <= 1) {
80
466
            if (_sorted_nums_vec.size() == 1) {
81
348
                _nums = std::move(_sorted_nums_vec[0]);
82
348
            }
83
84
466
            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
466
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
11
                pdqsort(_nums.begin(), _nums.end());
92
11
            }
93
94
466
            if (quantile == 1 || _nums.size() == 1) {
95
242
                return _nums.back();
96
242
            }
97
98
224
            double u = (_nums.size() - 1) * quantile;
99
224
            auto index = static_cast<uint32_t>(u);
100
224
            return _nums[index] +
101
224
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
224
                                                       static_cast<double>(_nums[index]));
103
466
        } else {
104
17
            DCHECK(_nums.empty());
105
17
            size_t rows = 0;
106
68
            for (const auto& i : _sorted_nums_vec) {
107
68
                rows += i.size();
108
68
            }
109
17
            const bool reverse = quantile > 0.5 && rows > 2;
110
17
            double u = (rows - 1) * quantile;
111
17
            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
17
            size_t target = reverse ? rows - index - 2 : index;
120
17
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
17
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
17
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
17
            return first_number +
128
17
                   (u - static_cast<double>(index)) *
129
17
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
17
        }
131
483
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
78
128
    double terminate(double quantile) {
79
128
        if (_sorted_nums_vec.size() <= 1) {
80
112
            if (_sorted_nums_vec.size() == 1) {
81
42
                _nums = std::move(_sorted_nums_vec[0]);
82
42
            }
83
84
112
            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
112
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
1
                pdqsort(_nums.begin(), _nums.end());
92
1
            }
93
94
112
            if (quantile == 1 || _nums.size() == 1) {
95
59
                return _nums.back();
96
59
            }
97
98
53
            double u = (_nums.size() - 1) * quantile;
99
53
            auto index = static_cast<uint32_t>(u);
100
53
            return _nums[index] +
101
53
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
53
                                                       static_cast<double>(_nums[index]));
103
112
        } else {
104
16
            DCHECK(_nums.empty());
105
16
            size_t rows = 0;
106
46
            for (const auto& i : _sorted_nums_vec) {
107
46
                rows += i.size();
108
46
            }
109
16
            const bool reverse = quantile > 0.5 && rows > 2;
110
16
            double u = (rows - 1) * quantile;
111
16
            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
16
            size_t target = reverse ? rows - index - 2 : index;
120
16
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
16
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
16
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
16
            return first_number +
128
16
                   (u - static_cast<double>(index)) *
129
16
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
16
        }
131
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
78
33
    double terminate(double quantile) {
79
33
        if (_sorted_nums_vec.size() <= 1) {
80
33
            if (_sorted_nums_vec.size() == 1) {
81
3
                _nums = std::move(_sorted_nums_vec[0]);
82
3
            }
83
84
33
            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
33
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
33
            if (quantile == 1 || _nums.size() == 1) {
95
24
                return _nums.back();
96
24
            }
97
98
9
            double u = (_nums.size() - 1) * quantile;
99
9
            auto index = static_cast<uint32_t>(u);
100
9
            return _nums[index] +
101
9
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
9
                                                       static_cast<double>(_nums[index]));
103
33
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            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
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number +
128
0
                   (u - static_cast<double>(index)) *
129
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
0
        }
131
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
78
3
    double terminate(double quantile) {
79
3
        if (_sorted_nums_vec.size() <= 1) {
80
3
            if (_sorted_nums_vec.size() == 1) {
81
3
                _nums = std::move(_sorted_nums_vec[0]);
82
3
            }
83
84
3
            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
3
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
3
            if (quantile == 1 || _nums.size() == 1) {
95
0
                return _nums.back();
96
0
            }
97
98
3
            double u = (_nums.size() - 1) * quantile;
99
3
            auto index = static_cast<uint32_t>(u);
100
3
            return _nums[index] +
101
3
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
3
                                                       static_cast<double>(_nums[index]));
103
3
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            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
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number +
128
0
                   (u - static_cast<double>(index)) *
129
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
0
        }
131
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
78
136
    double terminate(double quantile) {
79
136
        if (_sorted_nums_vec.size() <= 1) {
80
112
            if (_sorted_nums_vec.size() == 1) {
81
29
                _nums = std::move(_sorted_nums_vec[0]);
82
29
            }
83
84
112
            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
112
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
112
            if (quantile == 1 || _nums.size() == 1) {
95
30
                return _nums.back();
96
30
            }
97
98
82
            double u = (_nums.size() - 1) * quantile;
99
82
            auto index = static_cast<uint32_t>(u);
100
82
            return _nums[index] +
101
82
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
82
                                                       static_cast<double>(_nums[index]));
103
112
        } else {
104
24
            DCHECK(_nums.empty());
105
24
            size_t rows = 0;
106
66
            for (const auto& i : _sorted_nums_vec) {
107
66
                rows += i.size();
108
66
            }
109
24
            const bool reverse = quantile > 0.5 && rows > 2;
110
24
            double u = (rows - 1) * quantile;
111
24
            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
24
            size_t target = reverse ? rows - index - 2 : index;
120
24
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
24
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
24
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
24
            return first_number +
128
24
                   (u - static_cast<double>(index)) *
129
24
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
24
        }
131
136
    }
132
133
private:
134
    struct Node {
135
        Ty value;
136
        int array_index;
137
        int64_t element_index;
138
139
1.06k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIsE4NodessERKS2_
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
139
577
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
139
415
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
139
68
        auto operator<=>(const Node& other) const { return value <=> other.value; }
140
    };
141
142
290
    void _convert_sorted_num_vec_to_nums() {
143
290
        size_t rows = 0;
144
617
        for (const auto& i : _sorted_nums_vec) {
145
617
            rows += i.size();
146
617
        }
147
290
        _nums.resize(rows);
148
290
        size_t count = 0;
149
150
290
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
151
907
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
152
617
            if (!_sorted_nums_vec[i].empty()) {
153
617
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
154
617
            }
155
617
        }
156
157
1.01k
        while (!min_heap.empty()) {
158
729
            Node node = min_heap.top();
159
729
            min_heap.pop();
160
729
            _nums[count++] = node.value;
161
729
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
162
112
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
163
112
                min_heap.push(node);
164
112
            }
165
729
        }
166
290
        _sorted_nums_vec.clear();
167
290
    }
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
134
    void _convert_sorted_num_vec_to_nums() {
143
134
        size_t rows = 0;
144
278
        for (const auto& i : _sorted_nums_vec) {
145
278
            rows += i.size();
146
278
        }
147
134
        _nums.resize(rows);
148
134
        size_t count = 0;
149
150
134
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
151
412
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
152
278
            if (!_sorted_nums_vec[i].empty()) {
153
278
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
154
278
            }
155
278
        }
156
157
488
        while (!min_heap.empty()) {
158
354
            Node node = min_heap.top();
159
354
            min_heap.pop();
160
354
            _nums[count++] = node.value;
161
354
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
162
76
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
163
76
                min_heap.push(node);
164
76
            }
165
354
        }
166
134
        _sorted_nums_vec.clear();
167
134
    }
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
57
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
57
        Ty first_number = 0, second_number = 0;
171
57
        size_t count = 0;
172
57
        if (reverse) {
173
17
            std::priority_queue<Node> max_heap;
174
72
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
55
                if (!_sorted_nums_vec[i].empty()) {
176
55
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
55
                                     _sorted_nums_vec[i].size() - 1);
178
55
                }
179
55
            }
180
181
84
            while (!max_heap.empty()) {
182
84
                Node node = max_heap.top();
183
84
                max_heap.pop();
184
84
                if (count == target) {
185
17
                    second_number = node.value;
186
67
                } else if (count == target + 1) {
187
17
                    first_number = node.value;
188
17
                    break;
189
17
                }
190
67
                ++count;
191
67
                if (--node.element_index >= 0) {
192
60
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
60
                    max_heap.push(node);
194
60
                }
195
67
            }
196
197
40
        } else {
198
40
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
165
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
125
                if (!_sorted_nums_vec[i].empty()) {
201
125
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
125
                }
203
125
            }
204
205
128
            while (!min_heap.empty()) {
206
128
                Node node = min_heap.top();
207
128
                min_heap.pop();
208
128
                if (count == target) {
209
40
                    first_number = node.value;
210
88
                } else if (count == target + 1) {
211
40
                    second_number = node.value;
212
40
                    break;
213
40
                }
214
88
                ++count;
215
88
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
55
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
55
                    min_heap.push(node);
218
55
                }
219
88
            }
220
40
        }
221
222
57
        return {first_number, second_number};
223
57
    }
Unexecuted instantiation: _ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
169
17
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
17
        Ty first_number = 0, second_number = 0;
171
17
        size_t count = 0;
172
17
        if (reverse) {
173
3
            std::priority_queue<Node> max_heap;
174
15
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
12
                if (!_sorted_nums_vec[i].empty()) {
176
12
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
12
                                     _sorted_nums_vec[i].size() - 1);
178
12
                }
179
12
            }
180
181
24
            while (!max_heap.empty()) {
182
24
                Node node = max_heap.top();
183
24
                max_heap.pop();
184
24
                if (count == target) {
185
3
                    second_number = node.value;
186
21
                } else if (count == target + 1) {
187
3
                    first_number = node.value;
188
3
                    break;
189
3
                }
190
21
                ++count;
191
21
                if (--node.element_index >= 0) {
192
18
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
18
                    max_heap.push(node);
194
18
                }
195
21
            }
196
197
14
        } else {
198
14
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
70
            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
60
            while (!min_heap.empty()) {
206
60
                Node node = min_heap.top();
207
60
                min_heap.pop();
208
60
                if (count == target) {
209
14
                    first_number = node.value;
210
46
                } else if (count == target + 1) {
211
14
                    second_number = node.value;
212
14
                    break;
213
14
                }
214
46
                ++count;
215
46
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
41
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
41
                    min_heap.push(node);
218
41
                }
219
46
            }
220
14
        }
221
222
17
        return {first_number, second_number};
223
17
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
169
16
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
16
        Ty first_number = 0, second_number = 0;
171
16
        size_t count = 0;
172
16
        if (reverse) {
173
12
            std::priority_queue<Node> max_heap;
174
48
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
36
                if (!_sorted_nums_vec[i].empty()) {
176
36
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
36
                                     _sorted_nums_vec[i].size() - 1);
178
36
                }
179
36
            }
180
181
55
            while (!max_heap.empty()) {
182
55
                Node node = max_heap.top();
183
55
                max_heap.pop();
184
55
                if (count == target) {
185
12
                    second_number = node.value;
186
43
                } else if (count == target + 1) {
187
12
                    first_number = node.value;
188
12
                    break;
189
12
                }
190
43
                ++count;
191
43
                if (--node.element_index >= 0) {
192
42
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
42
                    max_heap.push(node);
194
42
                }
195
43
            }
196
197
12
        } else {
198
4
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
14
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
10
                if (!_sorted_nums_vec[i].empty()) {
201
10
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
10
                }
203
10
            }
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
4
                    first_number = node.value;
210
12
                } else if (count == target + 1) {
211
4
                    second_number = node.value;
212
4
                    break;
213
4
                }
214
12
                ++count;
215
12
                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
12
            }
220
4
        }
221
222
16
        return {first_number, second_number};
223
16
    }
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
169
24
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
24
        Ty first_number = 0, second_number = 0;
171
24
        size_t count = 0;
172
24
        if (reverse) {
173
2
            std::priority_queue<Node> max_heap;
174
9
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
7
                if (!_sorted_nums_vec[i].empty()) {
176
7
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
7
                                     _sorted_nums_vec[i].size() - 1);
178
7
                }
179
7
            }
180
181
5
            while (!max_heap.empty()) {
182
5
                Node node = max_heap.top();
183
5
                max_heap.pop();
184
5
                if (count == target) {
185
2
                    second_number = node.value;
186
3
                } else if (count == target + 1) {
187
2
                    first_number = node.value;
188
2
                    break;
189
2
                }
190
3
                ++count;
191
3
                if (--node.element_index >= 0) {
192
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
0
                    max_heap.push(node);
194
0
                }
195
3
            }
196
197
22
        } else {
198
22
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
81
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
59
                if (!_sorted_nums_vec[i].empty()) {
201
59
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
59
                }
203
59
            }
204
205
52
            while (!min_heap.empty()) {
206
52
                Node node = min_heap.top();
207
52
                min_heap.pop();
208
52
                if (count == target) {
209
22
                    first_number = node.value;
210
30
                } else if (count == target + 1) {
211
22
                    second_number = node.value;
212
22
                    break;
213
22
                }
214
30
                ++count;
215
30
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
2
                    min_heap.push(node);
218
2
                }
219
30
            }
220
22
        }
221
222
24
        return {first_number, second_number};
223
24
    }
224
225
    vectorized::PODArray<Ty> _nums;
226
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
227
};
228
229
} // namespace doris