Coverage Report

Created: 2025-09-23 14:19

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.85k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
47
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
211
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
2.39k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
856
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
43
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
36
13
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
296
    Counts() = default;
37
38
1.42k
    void merge(Counts* other) {
39
1.42k
        if (other != nullptr && !other->_nums.empty()) {
40
1.42k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.42k
        }
42
1.42k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
38
5
    void merge(Counts* other) {
39
5
        if (other != nullptr && !other->_nums.empty()) {
40
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
5
        }
42
5
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
38
14
    void merge(Counts* other) {
39
14
        if (other != nullptr && !other->_nums.empty()) {
40
14
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
14
        }
42
14
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
915
    void merge(Counts* other) {
39
915
        if (other != nullptr && !other->_nums.empty()) {
40
915
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
915
        }
42
915
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
373
    void merge(Counts* other) {
39
373
        if (other != nullptr && !other->_nums.empty()) {
40
373
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
373
        }
42
373
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
38
5
    void merge(Counts* other) {
39
5
        if (other != nullptr && !other->_nums.empty()) {
40
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
5
        }
42
5
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
38
5
    void merge(Counts* other) {
39
5
        if (other != nullptr && !other->_nums.empty()) {
40
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
5
        }
42
5
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
103
    void merge(Counts* other) {
39
103
        if (other != nullptr && !other->_nums.empty()) {
40
103
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
103
        }
42
103
    }
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.93k
    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.80k
    void serialize(vectorized::BufferWritable& buf) {
59
1.80k
        if (!_nums.empty()) {
60
1.47k
            pdqsort(_nums.begin(), _nums.end());
61
1.47k
            size_t size = _nums.size();
62
1.47k
            buf.write_binary(size);
63
1.47k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.47k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
334
            _convert_sorted_num_vec_to_nums();
67
334
            serialize(buf);
68
334
        }
69
1.80k
    }
_ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
5
    void serialize(vectorized::BufferWritable& buf) {
59
5
        if (!_nums.empty()) {
60
5
            pdqsort(_nums.begin(), _nums.end());
61
5
            size_t size = _nums.size();
62
5
            buf.write_binary(size);
63
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
5
        } 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
5
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
14
    void serialize(vectorized::BufferWritable& buf) {
59
14
        if (!_nums.empty()) {
60
14
            pdqsort(_nums.begin(), _nums.end());
61
14
            size_t size = _nums.size();
62
14
            buf.write_binary(size);
63
14
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
14
        } 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
14
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
1.18k
    void serialize(vectorized::BufferWritable& buf) {
59
1.18k
        if (!_nums.empty()) {
60
993
            pdqsort(_nums.begin(), _nums.end());
61
993
            size_t size = _nums.size();
62
993
            buf.write_binary(size);
63
993
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
993
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
195
            _convert_sorted_num_vec_to_nums();
67
195
            serialize(buf);
68
195
        }
69
1.18k
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
487
    void serialize(vectorized::BufferWritable& buf) {
59
487
        if (!_nums.empty()) {
60
348
            pdqsort(_nums.begin(), _nums.end());
61
348
            size_t size = _nums.size();
62
348
            buf.write_binary(size);
63
348
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
348
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
139
            _convert_sorted_num_vec_to_nums();
67
139
            serialize(buf);
68
139
        }
69
487
    }
_ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
5
    void serialize(vectorized::BufferWritable& buf) {
59
5
        if (!_nums.empty()) {
60
5
            pdqsort(_nums.begin(), _nums.end());
61
5
            size_t size = _nums.size();
62
5
            buf.write_binary(size);
63
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
5
        } 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
5
    }
_ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
5
    void serialize(vectorized::BufferWritable& buf) {
59
5
        if (!_nums.empty()) {
60
5
            pdqsort(_nums.begin(), _nums.end());
61
5
            size_t size = _nums.size();
62
5
            buf.write_binary(size);
63
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
5
        } 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
5
    }
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
103
    void serialize(vectorized::BufferWritable& buf) {
59
103
        if (!_nums.empty()) {
60
103
            pdqsort(_nums.begin(), _nums.end());
61
103
            size_t size = _nums.size();
62
103
            buf.write_binary(size);
63
103
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
103
        } 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
103
    }
70
71
1.42k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.42k
        size_t size;
73
1.42k
        buf.read_binary(size);
74
1.42k
        _nums.resize(size);
75
1.42k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.42k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.42k
    }
_ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
5
    void unserialize(vectorized::BufferReadable& buf) {
72
5
        size_t size;
73
5
        buf.read_binary(size);
74
5
        _nums.resize(size);
75
5
        auto buff = buf.read(sizeof(Ty) * size);
76
5
        memcpy(_nums.data(), buff.data, buff.size);
77
5
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
14
    void unserialize(vectorized::BufferReadable& buf) {
72
14
        size_t size;
73
14
        buf.read_binary(size);
74
14
        _nums.resize(size);
75
14
        auto buff = buf.read(sizeof(Ty) * size);
76
14
        memcpy(_nums.data(), buff.data, buff.size);
77
14
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
915
    void unserialize(vectorized::BufferReadable& buf) {
72
915
        size_t size;
73
915
        buf.read_binary(size);
74
915
        _nums.resize(size);
75
915
        auto buff = buf.read(sizeof(Ty) * size);
76
915
        memcpy(_nums.data(), buff.data, buff.size);
77
915
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
373
    void unserialize(vectorized::BufferReadable& buf) {
72
373
        size_t size;
73
373
        buf.read_binary(size);
74
373
        _nums.resize(size);
75
373
        auto buff = buf.read(sizeof(Ty) * size);
76
373
        memcpy(_nums.data(), buff.data, buff.size);
77
373
    }
_ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
5
    void unserialize(vectorized::BufferReadable& buf) {
72
5
        size_t size;
73
5
        buf.read_binary(size);
74
5
        _nums.resize(size);
75
5
        auto buff = buf.read(sizeof(Ty) * size);
76
5
        memcpy(_nums.data(), buff.data, buff.size);
77
5
    }
_ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
5
    void unserialize(vectorized::BufferReadable& buf) {
72
5
        size_t size;
73
5
        buf.read_binary(size);
74
5
        _nums.resize(size);
75
5
        auto buff = buf.read(sizeof(Ty) * size);
76
5
        memcpy(_nums.data(), buff.data, buff.size);
77
5
    }
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
103
    void unserialize(vectorized::BufferReadable& buf) {
72
103
        size_t size;
73
103
        buf.read_binary(size);
74
103
        _nums.resize(size);
75
103
        auto buff = buf.read(sizeof(Ty) * size);
76
103
        memcpy(_nums.data(), buff.data, buff.size);
77
103
    }
78
79
1.14k
    double terminate(double quantile) {
80
1.14k
        if (_sorted_nums_vec.size() <= 1) {
81
988
            if (_sorted_nums_vec.size() == 1) {
82
335
                _nums = std::move(_sorted_nums_vec[0]);
83
335
            }
84
85
988
            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
988
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
180
                pdqsort(_nums.begin(), _nums.end());
93
180
            }
94
95
988
            if (quantile == 1 || _nums.size() == 1) {
96
476
                return _nums.back();
97
476
            }
98
99
512
            double u = (_nums.size() - 1) * quantile;
100
512
            auto index = static_cast<uint32_t>(u);
101
512
            return _nums[index] +
102
512
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
988
        } else {
104
156
            DCHECK(_nums.empty());
105
156
            size_t rows = 0;
106
429
            for (const auto& i : _sorted_nums_vec) {
107
429
                rows += i.size();
108
429
            }
109
156
            const bool reverse = quantile > 0.5 && rows > 2;
110
156
            double u = (rows - 1) * quantile;
111
156
            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
156
            size_t target = reverse ? rows - index - 2 : index;
120
156
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
156
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
156
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
156
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
156
        }
129
1.14k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
79
61
    double terminate(double quantile) {
80
61
        if (_sorted_nums_vec.size() <= 1) {
81
59
            if (_sorted_nums_vec.size() == 1) {
82
1
                _nums = std::move(_sorted_nums_vec[0]);
83
1
            }
84
85
59
            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
59
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
24
                pdqsort(_nums.begin(), _nums.end());
93
24
            }
94
95
59
            if (quantile == 1 || _nums.size() == 1) {
96
34
                return _nums.back();
97
34
            }
98
99
25
            double u = (_nums.size() - 1) * quantile;
100
25
            auto index = static_cast<uint32_t>(u);
101
25
            return _nums[index] +
102
25
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
59
        } else {
104
2
            DCHECK(_nums.empty());
105
2
            size_t rows = 0;
106
4
            for (const auto& i : _sorted_nums_vec) {
107
4
                rows += i.size();
108
4
            }
109
2
            const bool reverse = quantile > 0.5 && rows > 2;
110
2
            double u = (rows - 1) * quantile;
111
2
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
2
            size_t target = reverse ? rows - index - 2 : index;
120
2
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
2
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
2
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
2
        }
129
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
79
300
    double terminate(double quantile) {
80
300
        if (_sorted_nums_vec.size() <= 1) {
81
295
            if (_sorted_nums_vec.size() == 1) {
82
1
                _nums = std::move(_sorted_nums_vec[0]);
83
1
            }
84
85
295
            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
295
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
144
                pdqsort(_nums.begin(), _nums.end());
93
144
            }
94
95
295
            if (quantile == 1 || _nums.size() == 1) {
96
87
                return _nums.back();
97
87
            }
98
99
208
            double u = (_nums.size() - 1) * quantile;
100
208
            auto index = static_cast<uint32_t>(u);
101
208
            return _nums[index] +
102
208
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
295
        } else {
104
5
            DCHECK(_nums.empty());
105
5
            size_t rows = 0;
106
13
            for (const auto& i : _sorted_nums_vec) {
107
13
                rows += i.size();
108
13
            }
109
5
            const bool reverse = quantile > 0.5 && rows > 2;
110
5
            double u = (rows - 1) * quantile;
111
5
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
5
            size_t target = reverse ? rows - index - 2 : index;
120
5
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
5
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
5
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
5
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
5
        }
129
300
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
483
    double terminate(double quantile) {
80
483
        if (_sorted_nums_vec.size() <= 1) {
81
384
            if (_sorted_nums_vec.size() == 1) {
82
266
                _nums = std::move(_sorted_nums_vec[0]);
83
266
            }
84
85
384
            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
384
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
11
                pdqsort(_nums.begin(), _nums.end());
93
11
            }
94
95
384
            if (quantile == 1 || _nums.size() == 1) {
96
242
                return _nums.back();
97
242
            }
98
99
142
            double u = (_nums.size() - 1) * quantile;
100
142
            auto index = static_cast<uint32_t>(u);
101
142
            return _nums[index] +
102
142
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
384
        } else {
104
99
            DCHECK(_nums.empty());
105
99
            size_t rows = 0;
106
271
            for (const auto& i : _sorted_nums_vec) {
107
271
                rows += i.size();
108
271
            }
109
99
            const bool reverse = quantile > 0.5 && rows > 2;
110
99
            double u = (rows - 1) * quantile;
111
99
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
99
            size_t target = reverse ? rows - index - 2 : index;
120
99
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
99
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
99
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
99
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
99
        }
129
483
    }
_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)) * (_nums[index + 1] - _nums[index]);
103
108
        } else {
104
20
            DCHECK(_nums.empty());
105
20
            size_t rows = 0;
106
57
            for (const auto& i : _sorted_nums_vec) {
107
57
                rows += i.size();
108
57
            }
109
20
            const bool reverse = quantile > 0.5 && rows > 2;
110
20
            double u = (rows - 1) * quantile;
111
20
            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
20
            size_t target = reverse ? rows - index - 2 : index;
120
20
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
20
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
20
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
20
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
20
        }
129
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
79
33
    double terminate(double quantile) {
80
33
        if (_sorted_nums_vec.size() <= 1) {
81
31
            if (_sorted_nums_vec.size() == 1) {
82
1
                _nums = std::move(_sorted_nums_vec[0]);
83
1
            }
84
85
31
            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
31
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
31
            if (quantile == 1 || _nums.size() == 1) {
96
24
                return _nums.back();
97
24
            }
98
99
7
            double u = (_nums.size() - 1) * quantile;
100
7
            auto index = static_cast<uint32_t>(u);
101
7
            return _nums[index] +
102
7
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
31
        } else {
104
2
            DCHECK(_nums.empty());
105
2
            size_t rows = 0;
106
4
            for (const auto& i : _sorted_nums_vec) {
107
4
                rows += i.size();
108
4
            }
109
2
            const bool reverse = quantile > 0.5 && rows > 2;
110
2
            double u = (rows - 1) * quantile;
111
2
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
2
            size_t target = reverse ? rows - index - 2 : index;
120
2
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
2
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
2
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
2
        }
129
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
79
3
    double terminate(double quantile) {
80
3
        if (_sorted_nums_vec.size() <= 1) {
81
1
            if (_sorted_nums_vec.size() == 1) {
82
1
                _nums = std::move(_sorted_nums_vec[0]);
83
1
            }
84
85
1
            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
1
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
1
            if (quantile == 1 || _nums.size() == 1) {
96
0
                return _nums.back();
97
0
            }
98
99
1
            double u = (_nums.size() - 1) * quantile;
100
1
            auto index = static_cast<uint32_t>(u);
101
1
            return _nums[index] +
102
1
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
2
        } else {
104
2
            DCHECK(_nums.empty());
105
2
            size_t rows = 0;
106
4
            for (const auto& i : _sorted_nums_vec) {
107
4
                rows += i.size();
108
4
            }
109
2
            const bool reverse = quantile > 0.5 && rows > 2;
110
2
            double u = (rows - 1) * quantile;
111
2
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
2
            size_t target = reverse ? rows - index - 2 : index;
120
2
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
2
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
2
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
2
        }
129
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
79
136
    double terminate(double quantile) {
80
136
        if (_sorted_nums_vec.size() <= 1) {
81
110
            if (_sorted_nums_vec.size() == 1) {
82
27
                _nums = std::move(_sorted_nums_vec[0]);
83
27
            }
84
85
110
            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
110
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
110
            if (quantile == 1 || _nums.size() == 1) {
96
30
                return _nums.back();
97
30
            }
98
99
80
            double u = (_nums.size() - 1) * quantile;
100
80
            auto index = static_cast<uint32_t>(u);
101
80
            return _nums[index] +
102
80
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
110
        } else {
104
26
            DCHECK(_nums.empty());
105
26
            size_t rows = 0;
106
76
            for (const auto& i : _sorted_nums_vec) {
107
76
                rows += i.size();
108
76
            }
109
26
            const bool reverse = quantile > 0.5 && rows > 2;
110
26
            double u = (rows - 1) * quantile;
111
26
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
26
            size_t target = reverse ? rows - index - 2 : index;
120
26
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
26
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
26
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
26
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
26
        }
129
136
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
1.49k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIaE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
137
42
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
929
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
416
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsInE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIfE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
137
98
        auto operator<=>(const Node& other) const { return value <=> other.value; }
138
    };
139
140
334
    void _convert_sorted_num_vec_to_nums() {
141
334
        size_t rows = 0;
142
656
        for (const auto& i : _sorted_nums_vec) {
143
656
            rows += i.size();
144
656
        }
145
334
        _nums.resize(rows);
146
334
        size_t count = 0;
147
148
334
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
990
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
656
            if (!_sorted_nums_vec[i].empty()) {
151
656
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
656
            }
153
656
        }
154
155
1.13k
        while (!min_heap.empty()) {
156
804
            Node node = min_heap.top();
157
804
            min_heap.pop();
158
804
            _nums[count++] = node.value;
159
804
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
148
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
148
                min_heap.push(node);
162
148
            }
163
804
        }
164
334
        _sorted_nums_vec.clear();
165
334
    }
Unexecuted instantiation: _ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv
_ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
195
    void _convert_sorted_num_vec_to_nums() {
141
195
        size_t rows = 0;
142
378
        for (const auto& i : _sorted_nums_vec) {
143
378
            rows += i.size();
144
378
        }
145
195
        _nums.resize(rows);
146
195
        size_t count = 0;
147
148
195
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
573
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
378
            if (!_sorted_nums_vec[i].empty()) {
151
378
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
378
            }
153
378
        }
154
155
645
        while (!min_heap.empty()) {
156
450
            Node node = min_heap.top();
157
450
            min_heap.pop();
158
450
            _nums[count++] = node.value;
159
450
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
72
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
72
                min_heap.push(node);
162
72
            }
163
450
        }
164
195
        _sorted_nums_vec.clear();
165
195
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
139
    void _convert_sorted_num_vec_to_nums() {
141
139
        size_t rows = 0;
142
278
        for (const auto& i : _sorted_nums_vec) {
143
278
            rows += i.size();
144
278
        }
145
139
        _nums.resize(rows);
146
139
        size_t count = 0;
147
148
139
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
417
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
278
            if (!_sorted_nums_vec[i].empty()) {
151
278
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
278
            }
153
278
        }
154
155
493
        while (!min_heap.empty()) {
156
354
            Node node = min_heap.top();
157
354
            min_heap.pop();
158
354
            _nums[count++] = node.value;
159
354
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
76
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
76
                min_heap.push(node);
162
76
            }
163
354
        }
164
139
        _sorted_nums_vec.clear();
165
139
    }
Unexecuted instantiation: _ZN5doris6CountsInE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIfE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIdE31_convert_sorted_num_vec_to_numsEv
166
167
156
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
156
        Ty first_number = 0, second_number = 0;
169
156
        size_t count = 0;
170
156
        if (reverse) {
171
31
            std::priority_queue<Node> max_heap;
172
121
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
90
                if (!_sorted_nums_vec[i].empty()) {
174
90
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
90
                                     _sorted_nums_vec[i].size() - 1);
176
90
                }
177
90
            }
178
179
113
            while (!max_heap.empty()) {
180
113
                Node node = max_heap.top();
181
113
                max_heap.pop();
182
113
                if (count == target) {
183
31
                    second_number = node.value;
184
82
                } else if (count == target + 1) {
185
31
                    first_number = node.value;
186
31
                    break;
187
31
                }
188
82
                ++count;
189
82
                if (--node.element_index >= 0) {
190
65
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
65
                    max_heap.push(node);
192
65
                }
193
82
            }
194
195
125
        } else {
196
125
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
464
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
339
                if (!_sorted_nums_vec[i].empty()) {
199
339
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
339
                }
201
339
            }
202
203
352
            while (!min_heap.empty()) {
204
352
                Node node = min_heap.top();
205
352
                min_heap.pop();
206
352
                if (count == target) {
207
125
                    first_number = node.value;
208
227
                } else if (count == target + 1) {
209
125
                    second_number = node.value;
210
125
                    break;
211
125
                }
212
227
                ++count;
213
227
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
136
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
136
                    min_heap.push(node);
216
136
                }
217
227
            }
218
125
        }
219
220
156
        return {first_number, second_number};
221
156
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
5
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
5
        Ty first_number = 0, second_number = 0;
169
5
        size_t count = 0;
170
5
        if (reverse) {
171
2
            std::priority_queue<Node> max_heap;
172
7
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
5
                if (!_sorted_nums_vec[i].empty()) {
174
5
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
5
                                     _sorted_nums_vec[i].size() - 1);
176
5
                }
177
5
            }
178
179
8
            while (!max_heap.empty()) {
180
8
                Node node = max_heap.top();
181
8
                max_heap.pop();
182
8
                if (count == target) {
183
2
                    second_number = node.value;
184
6
                } else if (count == target + 1) {
185
2
                    first_number = node.value;
186
2
                    break;
187
2
                }
188
6
                ++count;
189
6
                if (--node.element_index >= 0) {
190
5
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
5
                    max_heap.push(node);
192
5
                }
193
6
            }
194
195
3
        } else {
196
3
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
11
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
8
                if (!_sorted_nums_vec[i].empty()) {
199
8
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
8
                }
201
8
            }
202
203
16
            while (!min_heap.empty()) {
204
16
                Node node = min_heap.top();
205
16
                min_heap.pop();
206
16
                if (count == target) {
207
3
                    first_number = node.value;
208
13
                } else if (count == target + 1) {
209
3
                    second_number = node.value;
210
3
                    break;
211
3
                }
212
13
                ++count;
213
13
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
12
                    min_heap.push(node);
216
12
                }
217
13
            }
218
3
        }
219
220
5
        return {first_number, second_number};
221
5
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
99
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
99
        Ty first_number = 0, second_number = 0;
169
99
        size_t count = 0;
170
99
        if (reverse) {
171
11
            std::priority_queue<Node> max_heap;
172
41
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
30
                if (!_sorted_nums_vec[i].empty()) {
174
30
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
30
                                     _sorted_nums_vec[i].size() - 1);
176
30
                }
177
30
            }
178
179
41
            while (!max_heap.empty()) {
180
41
                Node node = max_heap.top();
181
41
                max_heap.pop();
182
41
                if (count == target) {
183
11
                    second_number = node.value;
184
30
                } else if (count == target + 1) {
185
11
                    first_number = node.value;
186
11
                    break;
187
11
                }
188
30
                ++count;
189
30
                if (--node.element_index >= 0) {
190
23
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
23
                    max_heap.push(node);
192
23
                }
193
30
            }
194
195
88
        } else {
196
88
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
329
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
241
                if (!_sorted_nums_vec[i].empty()) {
199
241
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
241
                }
201
241
            }
202
203
247
            while (!min_heap.empty()) {
204
247
                Node node = min_heap.top();
205
247
                min_heap.pop();
206
247
                if (count == target) {
207
88
                    first_number = node.value;
208
159
                } else if (count == target + 1) {
209
88
                    second_number = node.value;
210
88
                    break;
211
88
                }
212
159
                ++count;
213
159
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
100
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
100
                    min_heap.push(node);
216
100
                }
217
159
            }
218
88
        }
219
220
99
        return {first_number, second_number};
221
99
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
20
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
20
        Ty first_number = 0, second_number = 0;
169
20
        size_t count = 0;
170
20
        if (reverse) {
171
12
            std::priority_queue<Node> max_heap;
172
51
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
39
                if (!_sorted_nums_vec[i].empty()) {
174
39
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
39
                                     _sorted_nums_vec[i].size() - 1);
176
39
                }
177
39
            }
178
179
51
            while (!max_heap.empty()) {
180
51
                Node node = max_heap.top();
181
51
                max_heap.pop();
182
51
                if (count == target) {
183
12
                    second_number = node.value;
184
39
                } else if (count == target + 1) {
185
12
                    first_number = node.value;
186
12
                    break;
187
12
                }
188
39
                ++count;
189
39
                if (--node.element_index >= 0) {
190
33
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
33
                    max_heap.push(node);
192
33
                }
193
39
            }
194
195
12
        } else {
196
8
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
26
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
18
                if (!_sorted_nums_vec[i].empty()) {
199
18
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
18
                }
201
18
            }
202
203
25
            while (!min_heap.empty()) {
204
25
                Node node = min_heap.top();
205
25
                min_heap.pop();
206
25
                if (count == target) {
207
8
                    first_number = node.value;
208
17
                } else if (count == target + 1) {
209
8
                    second_number = node.value;
210
8
                    break;
211
8
                }
212
17
                ++count;
213
17
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
15
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
15
                    min_heap.push(node);
216
15
                }
217
17
            }
218
8
        }
219
220
20
        return {first_number, second_number};
221
20
    }
_ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
26
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
26
        Ty first_number = 0, second_number = 0;
169
26
        size_t count = 0;
170
26
        if (reverse) {
171
3
            std::priority_queue<Node> max_heap;
172
13
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
10
                if (!_sorted_nums_vec[i].empty()) {
174
10
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
10
                                     _sorted_nums_vec[i].size() - 1);
176
10
                }
177
10
            }
178
179
7
            while (!max_heap.empty()) {
180
7
                Node node = max_heap.top();
181
7
                max_heap.pop();
182
7
                if (count == target) {
183
3
                    second_number = node.value;
184
4
                } else if (count == target + 1) {
185
3
                    first_number = node.value;
186
3
                    break;
187
3
                }
188
4
                ++count;
189
4
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
4
            }
194
195
23
        } else {
196
23
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
89
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
66
                if (!_sorted_nums_vec[i].empty()) {
199
66
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
66
                }
201
66
            }
202
203
55
            while (!min_heap.empty()) {
204
55
                Node node = min_heap.top();
205
55
                min_heap.pop();
206
55
                if (count == target) {
207
23
                    first_number = node.value;
208
32
                } else if (count == target + 1) {
209
23
                    second_number = node.value;
210
23
                    break;
211
23
                }
212
32
                ++count;
213
32
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
6
                    min_heap.push(node);
216
6
                }
217
32
            }
218
23
        }
219
220
26
        return {first_number, second_number};
221
26
    }
222
223
    vectorized::PODArray<Ty> _nums;
224
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
225
};
226
227
} // namespace doris