Coverage Report

Created: 2026-01-23 02:13

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.22k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
49
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
201
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
1.89k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
819
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
45
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
15
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
198
    Counts() = default;
36
37
1.09k
    void merge(Counts* other) {
38
1.09k
        if (other != nullptr && !other->_nums.empty()) {
39
1.09k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
1.09k
        }
41
1.09k
    }
_ZN5doris6CountsIaE5mergeEPS1_
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
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
37
9
    void merge(Counts* other) {
38
9
        if (other != nullptr && !other->_nums.empty()) {
39
9
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
9
        }
41
9
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
37
668
    void merge(Counts* other) {
38
668
        if (other != nullptr && !other->_nums.empty()) {
39
668
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
668
        }
41
668
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
342
    void merge(Counts* other) {
38
342
        if (other != nullptr && !other->_nums.empty()) {
39
342
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
342
        }
41
342
    }
_ZN5doris6CountsInE5mergeEPS1_
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
    }
_ZN5doris6CountsIfE5mergeEPS1_
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
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
37
56
    void merge(Counts* other) {
38
56
        if (other != nullptr && !other->_nums.empty()) {
39
56
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
56
        }
41
56
    }
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_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
_ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Line
Count
Source
53
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
54
5
        _nums.insert(keys.begin(), keys.end());
55
5
    }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
56
57
1.49k
    void serialize(vectorized::BufferWritable& buf) {
58
1.49k
        if (!_nums.empty()) {
59
1.21k
            pdqsort(_nums.begin(), _nums.end());
60
1.21k
            size_t size = _nums.size();
61
1.21k
            buf.write_binary(size);
62
1.21k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
1.21k
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
287
            _convert_sorted_num_vec_to_nums();
66
287
            serialize(buf);
67
287
        }
68
1.49k
    }
_ZN5doris6CountsIaE9serializeERNS_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
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
9
    void serialize(vectorized::BufferWritable& buf) {
58
9
        if (!_nums.empty()) {
59
9
            pdqsort(_nums.begin(), _nums.end());
60
9
            size_t size = _nums.size();
61
9
            buf.write_binary(size);
62
9
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
9
        } 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
9
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
941
    void serialize(vectorized::BufferWritable& buf) {
58
941
        if (!_nums.empty()) {
59
785
            pdqsort(_nums.begin(), _nums.end());
60
785
            size_t size = _nums.size();
61
785
            buf.write_binary(size);
62
785
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
785
        } 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
941
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
473
    void serialize(vectorized::BufferWritable& buf) {
58
473
        if (!_nums.empty()) {
59
342
            pdqsort(_nums.begin(), _nums.end());
60
342
            size_t size = _nums.size();
61
342
            buf.write_binary(size);
62
342
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
342
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
131
            _convert_sorted_num_vec_to_nums();
66
131
            serialize(buf);
67
131
        }
68
473
    }
_ZN5doris6CountsInE9serializeERNS_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
    }
_ZN5doris6CountsIfE9serializeERNS_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
    }
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
56
    void serialize(vectorized::BufferWritable& buf) {
58
56
        if (!_nums.empty()) {
59
56
            pdqsort(_nums.begin(), _nums.end());
60
56
            size_t size = _nums.size();
61
56
            buf.write_binary(size);
62
56
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
56
        } 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
56
    }
69
70
1.09k
    void unserialize(vectorized::BufferReadable& buf) {
71
1.09k
        size_t size;
72
1.09k
        buf.read_binary(size);
73
1.09k
        _nums.resize(size);
74
1.09k
        auto buff = buf.read(sizeof(Ty) * size);
75
1.09k
        memcpy(_nums.data(), buff.data, buff.size);
76
1.09k
    }
_ZN5doris6CountsIaE11unserializeERNS_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
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
9
    void unserialize(vectorized::BufferReadable& buf) {
71
9
        size_t size;
72
9
        buf.read_binary(size);
73
9
        _nums.resize(size);
74
9
        auto buff = buf.read(sizeof(Ty) * size);
75
9
        memcpy(_nums.data(), buff.data, buff.size);
76
9
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
668
    void unserialize(vectorized::BufferReadable& buf) {
71
668
        size_t size;
72
668
        buf.read_binary(size);
73
668
        _nums.resize(size);
74
668
        auto buff = buf.read(sizeof(Ty) * size);
75
668
        memcpy(_nums.data(), buff.data, buff.size);
76
668
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
342
    void unserialize(vectorized::BufferReadable& buf) {
71
342
        size_t size;
72
342
        buf.read_binary(size);
73
342
        _nums.resize(size);
74
342
        auto buff = buf.read(sizeof(Ty) * size);
75
342
        memcpy(_nums.data(), buff.data, buff.size);
76
342
    }
_ZN5doris6CountsInE11unserializeERNS_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
    }
_ZN5doris6CountsIfE11unserializeERNS_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
    }
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
56
    void unserialize(vectorized::BufferReadable& buf) {
71
56
        size_t size;
72
56
        buf.read_binary(size);
73
56
        _nums.resize(size);
74
56
        auto buff = buf.read(sizeof(Ty) * size);
75
56
        memcpy(_nums.data(), buff.data, buff.size);
76
56
    }
77
78
1.10k
    double terminate(double quantile) {
79
1.10k
        if (_sorted_nums_vec.size() <= 1) {
80
1.07k
            if (_sorted_nums_vec.size() == 1) {
81
417
                _nums = std::move(_sorted_nums_vec[0]);
82
417
            }
83
84
1.07k
            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.07k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
180
                pdqsort(_nums.begin(), _nums.end());
92
180
            }
93
94
1.07k
            if (quantile == 1 || _nums.size() == 1) {
95
473
                return _nums.back();
96
473
            }
97
98
597
            double u = (_nums.size() - 1) * quantile;
99
597
            auto index = static_cast<uint32_t>(u);
100
597
            return _nums[index] +
101
597
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
597
                                                       static_cast<double>(_nums[index]));
103
1.07k
        } else {
104
35
            DCHECK(_nums.empty());
105
35
            size_t rows = 0;
106
90
            for (const auto& i : _sorted_nums_vec) {
107
90
                rows += i.size();
108
90
            }
109
35
            const bool reverse = quantile > 0.5 && rows > 2;
110
35
            double u = (rows - 1) * quantile;
111
35
            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
35
            size_t target = reverse ? rows - index - 2 : index;
120
35
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
35
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
35
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
35
            return first_number +
128
35
                   (u - static_cast<double>(index)) *
129
35
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
35
        }
131
1.10k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
78
61
    double terminate(double quantile) {
79
61
        if (_sorted_nums_vec.size() <= 1) {
80
58
            if (_sorted_nums_vec.size() == 1) {
81
0
                _nums = std::move(_sorted_nums_vec[0]);
82
0
            }
83
84
58
            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
58
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
24
                pdqsort(_nums.begin(), _nums.end());
92
24
            }
93
94
58
            if (quantile == 1 || _nums.size() == 1) {
95
34
                return _nums.back();
96
34
            }
97
98
24
            double u = (_nums.size() - 1) * quantile;
99
24
            auto index = static_cast<uint32_t>(u);
100
24
            return _nums[index] +
101
24
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
24
                                                       static_cast<double>(_nums[index]));
103
58
        } else {
104
3
            DCHECK(_nums.empty());
105
3
            size_t rows = 0;
106
6
            for (const auto& i : _sorted_nums_vec) {
107
6
                rows += i.size();
108
6
            }
109
3
            const bool reverse = quantile > 0.5 && rows > 2;
110
3
            double u = (rows - 1) * quantile;
111
3
            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
3
            size_t target = reverse ? rows - index - 2 : index;
120
3
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
3
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
3
            return first_number +
128
3
                   (u - static_cast<double>(index)) *
129
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
3
        }
131
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
78
300
    double terminate(double quantile) {
79
300
        if (_sorted_nums_vec.size() <= 1) {
80
297
            if (_sorted_nums_vec.size() == 1) {
81
3
                _nums = std::move(_sorted_nums_vec[0]);
82
3
            }
83
84
297
            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
297
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
144
                pdqsort(_nums.begin(), _nums.end());
92
144
            }
93
94
297
            if (quantile == 1 || _nums.size() == 1) {
95
87
                return _nums.back();
96
87
            }
97
98
210
            double u = (_nums.size() - 1) * quantile;
99
210
            auto index = static_cast<uint32_t>(u);
100
210
            return _nums[index] +
101
210
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
210
                                                       static_cast<double>(_nums[index]));
103
297
        } else {
104
3
            DCHECK(_nums.empty());
105
3
            size_t rows = 0;
106
6
            for (const auto& i : _sorted_nums_vec) {
107
6
                rows += i.size();
108
6
            }
109
3
            const bool reverse = quantile > 0.5 && rows > 2;
110
3
            double u = (rows - 1) * quantile;
111
3
            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
3
            size_t target = reverse ? rows - index - 2 : index;
120
3
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
3
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
3
            return first_number +
128
3
                   (u - static_cast<double>(index)) *
129
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
3
        }
131
300
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
78
444
    double terminate(double quantile) {
79
444
        if (_sorted_nums_vec.size() <= 1) {
80
441
            if (_sorted_nums_vec.size() == 1) {
81
323
                _nums = std::move(_sorted_nums_vec[0]);
82
323
            }
83
84
441
            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
441
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
11
                pdqsort(_nums.begin(), _nums.end());
92
11
            }
93
94
441
            if (quantile == 1 || _nums.size() == 1) {
95
239
                return _nums.back();
96
239
            }
97
98
202
            double u = (_nums.size() - 1) * quantile;
99
202
            auto index = static_cast<uint32_t>(u);
100
202
            return _nums[index] +
101
202
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
202
                                                       static_cast<double>(_nums[index]));
103
441
        } else {
104
3
            DCHECK(_nums.empty());
105
3
            size_t rows = 0;
106
6
            for (const auto& i : _sorted_nums_vec) {
107
6
                rows += i.size();
108
6
            }
109
3
            const bool reverse = quantile > 0.5 && rows > 2;
110
3
            double u = (rows - 1) * quantile;
111
3
            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
3
            size_t target = reverse ? rows - index - 2 : index;
120
3
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
3
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
3
            return first_number +
128
3
                   (u - static_cast<double>(index)) *
129
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
3
        }
131
444
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
78
128
    double terminate(double quantile) {
79
128
        if (_sorted_nums_vec.size() <= 1) {
80
111
            if (_sorted_nums_vec.size() == 1) {
81
41
                _nums = std::move(_sorted_nums_vec[0]);
82
41
            }
83
84
111
            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
111
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
1
                pdqsort(_nums.begin(), _nums.end());
92
1
            }
93
94
111
            if (quantile == 1 || _nums.size() == 1) {
95
59
                return _nums.back();
96
59
            }
97
98
52
            double u = (_nums.size() - 1) * quantile;
99
52
            auto index = static_cast<uint32_t>(u);
100
52
            return _nums[index] +
101
52
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
52
                                                       static_cast<double>(_nums[index]));
103
111
        } else {
104
17
            DCHECK(_nums.empty());
105
17
            size_t rows = 0;
106
54
            for (const auto& i : _sorted_nums_vec) {
107
54
                rows += i.size();
108
54
            }
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
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
78
33
    double terminate(double quantile) {
79
33
        if (_sorted_nums_vec.size() <= 1) {
80
30
            if (_sorted_nums_vec.size() == 1) {
81
0
                _nums = std::move(_sorted_nums_vec[0]);
82
0
            }
83
84
30
            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
30
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
30
            if (quantile == 1 || _nums.size() == 1) {
95
24
                return _nums.back();
96
24
            }
97
98
6
            double u = (_nums.size() - 1) * quantile;
99
6
            auto index = static_cast<uint32_t>(u);
100
6
            return _nums[index] +
101
6
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
6
                                                       static_cast<double>(_nums[index]));
103
30
        } else {
104
3
            DCHECK(_nums.empty());
105
3
            size_t rows = 0;
106
6
            for (const auto& i : _sorted_nums_vec) {
107
6
                rows += i.size();
108
6
            }
109
3
            const bool reverse = quantile > 0.5 && rows > 2;
110
3
            double u = (rows - 1) * quantile;
111
3
            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
3
            size_t target = reverse ? rows - index - 2 : index;
120
3
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
3
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
3
            return first_number +
128
3
                   (u - static_cast<double>(index)) *
129
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
3
        }
131
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
78
3
    double terminate(double quantile) {
79
3
        if (_sorted_nums_vec.size() <= 1) {
80
0
            if (_sorted_nums_vec.size() == 1) {
81
0
                _nums = std::move(_sorted_nums_vec[0]);
82
0
            }
83
84
0
            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
0
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
0
            if (quantile == 1 || _nums.size() == 1) {
95
0
                return _nums.back();
96
0
            }
97
98
0
            double u = (_nums.size() - 1) * quantile;
99
0
            auto index = static_cast<uint32_t>(u);
100
0
            return _nums[index] +
101
0
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
0
                                                       static_cast<double>(_nums[index]));
103
3
        } else {
104
3
            DCHECK(_nums.empty());
105
3
            size_t rows = 0;
106
6
            for (const auto& i : _sorted_nums_vec) {
107
6
                rows += i.size();
108
6
            }
109
3
            const bool reverse = quantile > 0.5 && rows > 2;
110
3
            double u = (rows - 1) * quantile;
111
3
            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
3
            size_t target = reverse ? rows - index - 2 : index;
120
3
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
3
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
3
            return first_number +
128
3
                   (u - static_cast<double>(index)) *
129
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
3
        }
131
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
78
136
    double terminate(double quantile) {
79
136
        if (_sorted_nums_vec.size() <= 1) {
80
133
            if (_sorted_nums_vec.size() == 1) {
81
50
                _nums = std::move(_sorted_nums_vec[0]);
82
50
            }
83
84
133
            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
133
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
133
            if (quantile == 1 || _nums.size() == 1) {
95
30
                return _nums.back();
96
30
            }
97
98
103
            double u = (_nums.size() - 1) * quantile;
99
103
            auto index = static_cast<uint32_t>(u);
100
103
            return _nums[index] +
101
103
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
103
                                                       static_cast<double>(_nums[index]));
103
133
        } else {
104
3
            DCHECK(_nums.empty());
105
3
            size_t rows = 0;
106
6
            for (const auto& i : _sorted_nums_vec) {
107
6
                rows += i.size();
108
6
            }
109
3
            const bool reverse = quantile > 0.5 && rows > 2;
110
3
            double u = (rows - 1) * quantile;
111
3
            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
3
            size_t target = reverse ? rows - index - 2 : index;
120
3
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
3
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
3
            return first_number +
128
3
                   (u - static_cast<double>(index)) *
129
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
3
        }
131
136
    }
132
133
private:
134
    struct Node {
135
        Ty value;
136
        int array_index;
137
        int64_t element_index;
138
139
706
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIaE4NodessERKS2_
Line
Count
Source
139
6
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
139
6
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
139
297
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
139
380
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsInE4NodessERKS2_
Line
Count
Source
139
6
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIfE4NodessERKS2_
Line
Count
Source
139
6
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
139
5
        auto operator<=>(const Node& other) const { return value <=> other.value; }
140
    };
141
142
288
    void _convert_sorted_num_vec_to_nums() {
143
288
        size_t rows = 0;
144
586
        for (const auto& i : _sorted_nums_vec) {
145
586
            rows += i.size();
146
586
        }
147
288
        _nums.resize(rows);
148
288
        size_t count = 0;
149
150
288
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
151
874
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
152
586
            if (!_sorted_nums_vec[i].empty()) {
153
586
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
154
586
            }
155
586
        }
156
157
1.02k
        while (!min_heap.empty()) {
158
737
            Node node = min_heap.top();
159
737
            min_heap.pop();
160
737
            _nums[count++] = node.value;
161
737
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
162
151
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
163
151
                min_heap.push(node);
164
151
            }
165
737
        }
166
288
        _sorted_nums_vec.clear();
167
288
    }
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
132
    void _convert_sorted_num_vec_to_nums() {
143
132
        size_t rows = 0;
144
247
        for (const auto& i : _sorted_nums_vec) {
145
247
            rows += i.size();
146
247
        }
147
132
        _nums.resize(rows);
148
132
        size_t count = 0;
149
150
132
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
151
379
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
152
247
            if (!_sorted_nums_vec[i].empty()) {
153
247
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
154
247
            }
155
247
        }
156
157
494
        while (!min_heap.empty()) {
158
362
            Node node = min_heap.top();
159
362
            min_heap.pop();
160
362
            _nums[count++] = node.value;
161
362
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
162
115
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
163
115
                min_heap.push(node);
164
115
            }
165
362
        }
166
132
        _sorted_nums_vec.clear();
167
132
    }
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
35
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
35
        Ty first_number = 0, second_number = 0;
171
35
        size_t count = 0;
172
35
        if (reverse) {
173
18
            std::priority_queue<Node> max_heap;
174
74
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
56
                if (!_sorted_nums_vec[i].empty()) {
176
56
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
56
                                     _sorted_nums_vec[i].size() - 1);
178
56
                }
179
56
            }
180
181
61
            while (!max_heap.empty()) {
182
61
                Node node = max_heap.top();
183
61
                max_heap.pop();
184
61
                if (count == target) {
185
18
                    second_number = node.value;
186
43
                } else if (count == target + 1) {
187
18
                    first_number = node.value;
188
18
                    break;
189
18
                }
190
43
                ++count;
191
43
                if (--node.element_index >= 0) {
192
39
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
39
                    max_heap.push(node);
194
39
                }
195
43
            }
196
197
18
        } else {
198
17
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
51
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
34
                if (!_sorted_nums_vec[i].empty()) {
201
34
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
34
                }
203
34
            }
204
205
55
            while (!min_heap.empty()) {
206
55
                Node node = min_heap.top();
207
55
                min_heap.pop();
208
55
                if (count == target) {
209
17
                    first_number = node.value;
210
38
                } else if (count == target + 1) {
211
17
                    second_number = node.value;
212
17
                    break;
213
17
                }
214
38
                ++count;
215
38
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
24
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
24
                    min_heap.push(node);
218
24
                }
219
38
            }
220
17
        }
221
222
35
        return {first_number, second_number};
223
35
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
169
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
3
        Ty first_number = 0, second_number = 0;
171
3
        size_t count = 0;
172
3
        if (reverse) {
173
1
            std::priority_queue<Node> max_heap;
174
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
2
                if (!_sorted_nums_vec[i].empty()) {
176
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
2
                                     _sorted_nums_vec[i].size() - 1);
178
2
                }
179
2
            }
180
181
2
            while (!max_heap.empty()) {
182
2
                Node node = max_heap.top();
183
2
                max_heap.pop();
184
2
                if (count == target) {
185
1
                    second_number = node.value;
186
1
                } else if (count == target + 1) {
187
1
                    first_number = node.value;
188
1
                    break;
189
1
                }
190
1
                ++count;
191
1
                if (--node.element_index >= 0) {
192
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
1
                    max_heap.push(node);
194
1
                }
195
1
            }
196
197
2
        } else {
198
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
4
                if (!_sorted_nums_vec[i].empty()) {
201
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
4
                }
203
4
            }
204
205
6
            while (!min_heap.empty()) {
206
6
                Node node = min_heap.top();
207
6
                min_heap.pop();
208
6
                if (count == target) {
209
2
                    first_number = node.value;
210
4
                } else if (count == target + 1) {
211
2
                    second_number = node.value;
212
2
                    break;
213
2
                }
214
4
                ++count;
215
4
                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
4
            }
220
2
        }
221
222
3
        return {first_number, second_number};
223
3
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
169
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
3
        Ty first_number = 0, second_number = 0;
171
3
        size_t count = 0;
172
3
        if (reverse) {
173
1
            std::priority_queue<Node> max_heap;
174
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
2
                if (!_sorted_nums_vec[i].empty()) {
176
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
2
                                     _sorted_nums_vec[i].size() - 1);
178
2
                }
179
2
            }
180
181
2
            while (!max_heap.empty()) {
182
2
                Node node = max_heap.top();
183
2
                max_heap.pop();
184
2
                if (count == target) {
185
1
                    second_number = node.value;
186
1
                } else if (count == target + 1) {
187
1
                    first_number = node.value;
188
1
                    break;
189
1
                }
190
1
                ++count;
191
1
                if (--node.element_index >= 0) {
192
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
1
                    max_heap.push(node);
194
1
                }
195
1
            }
196
197
2
        } else {
198
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
4
                if (!_sorted_nums_vec[i].empty()) {
201
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
4
                }
203
4
            }
204
205
6
            while (!min_heap.empty()) {
206
6
                Node node = min_heap.top();
207
6
                min_heap.pop();
208
6
                if (count == target) {
209
2
                    first_number = node.value;
210
4
                } else if (count == target + 1) {
211
2
                    second_number = node.value;
212
2
                    break;
213
2
                }
214
4
                ++count;
215
4
                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
4
            }
220
2
        }
221
222
3
        return {first_number, second_number};
223
3
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
169
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
3
        Ty first_number = 0, second_number = 0;
171
3
        size_t count = 0;
172
3
        if (reverse) {
173
1
            std::priority_queue<Node> max_heap;
174
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
2
                if (!_sorted_nums_vec[i].empty()) {
176
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
2
                                     _sorted_nums_vec[i].size() - 1);
178
2
                }
179
2
            }
180
181
2
            while (!max_heap.empty()) {
182
2
                Node node = max_heap.top();
183
2
                max_heap.pop();
184
2
                if (count == target) {
185
1
                    second_number = node.value;
186
1
                } else if (count == target + 1) {
187
1
                    first_number = node.value;
188
1
                    break;
189
1
                }
190
1
                ++count;
191
1
                if (--node.element_index >= 0) {
192
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
1
                    max_heap.push(node);
194
1
                }
195
1
            }
196
197
2
        } else {
198
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
4
                if (!_sorted_nums_vec[i].empty()) {
201
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
4
                }
203
4
            }
204
205
6
            while (!min_heap.empty()) {
206
6
                Node node = min_heap.top();
207
6
                min_heap.pop();
208
6
                if (count == target) {
209
2
                    first_number = node.value;
210
4
                } else if (count == target + 1) {
211
2
                    second_number = node.value;
212
2
                    break;
213
2
                }
214
4
                ++count;
215
4
                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
4
            }
220
2
        }
221
222
3
        return {first_number, second_number};
223
3
    }
_ZN5doris6CountsIlE27_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
12
            std::priority_queue<Node> max_heap;
174
56
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
44
                if (!_sorted_nums_vec[i].empty()) {
176
44
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
44
                                     _sorted_nums_vec[i].size() - 1);
178
44
                }
179
44
            }
180
181
49
            while (!max_heap.empty()) {
182
49
                Node node = max_heap.top();
183
49
                max_heap.pop();
184
49
                if (count == target) {
185
12
                    second_number = node.value;
186
37
                } else if (count == target + 1) {
187
12
                    first_number = node.value;
188
12
                    break;
189
12
                }
190
37
                ++count;
191
37
                if (--node.element_index >= 0) {
192
34
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
34
                    max_heap.push(node);
194
34
                }
195
37
            }
196
197
12
        } else {
198
5
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
15
            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
19
            while (!min_heap.empty()) {
206
19
                Node node = min_heap.top();
207
19
                min_heap.pop();
208
19
                if (count == target) {
209
5
                    first_number = node.value;
210
14
                } else if (count == target + 1) {
211
5
                    second_number = node.value;
212
5
                    break;
213
5
                }
214
14
                ++count;
215
14
                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
14
            }
220
5
        }
221
222
17
        return {first_number, second_number};
223
17
    }
_ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Line
Count
Source
169
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
3
        Ty first_number = 0, second_number = 0;
171
3
        size_t count = 0;
172
3
        if (reverse) {
173
1
            std::priority_queue<Node> max_heap;
174
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
2
                if (!_sorted_nums_vec[i].empty()) {
176
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
2
                                     _sorted_nums_vec[i].size() - 1);
178
2
                }
179
2
            }
180
181
2
            while (!max_heap.empty()) {
182
2
                Node node = max_heap.top();
183
2
                max_heap.pop();
184
2
                if (count == target) {
185
1
                    second_number = node.value;
186
1
                } else if (count == target + 1) {
187
1
                    first_number = node.value;
188
1
                    break;
189
1
                }
190
1
                ++count;
191
1
                if (--node.element_index >= 0) {
192
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
1
                    max_heap.push(node);
194
1
                }
195
1
            }
196
197
2
        } else {
198
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
4
                if (!_sorted_nums_vec[i].empty()) {
201
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
4
                }
203
4
            }
204
205
6
            while (!min_heap.empty()) {
206
6
                Node node = min_heap.top();
207
6
                min_heap.pop();
208
6
                if (count == target) {
209
2
                    first_number = node.value;
210
4
                } else if (count == target + 1) {
211
2
                    second_number = node.value;
212
2
                    break;
213
2
                }
214
4
                ++count;
215
4
                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
4
            }
220
2
        }
221
222
3
        return {first_number, second_number};
223
3
    }
_ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Line
Count
Source
169
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
3
        Ty first_number = 0, second_number = 0;
171
3
        size_t count = 0;
172
3
        if (reverse) {
173
1
            std::priority_queue<Node> max_heap;
174
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
2
                if (!_sorted_nums_vec[i].empty()) {
176
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
2
                                     _sorted_nums_vec[i].size() - 1);
178
2
                }
179
2
            }
180
181
2
            while (!max_heap.empty()) {
182
2
                Node node = max_heap.top();
183
2
                max_heap.pop();
184
2
                if (count == target) {
185
1
                    second_number = node.value;
186
1
                } else if (count == target + 1) {
187
1
                    first_number = node.value;
188
1
                    break;
189
1
                }
190
1
                ++count;
191
1
                if (--node.element_index >= 0) {
192
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
1
                    max_heap.push(node);
194
1
                }
195
1
            }
196
197
2
        } else {
198
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
4
                if (!_sorted_nums_vec[i].empty()) {
201
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
4
                }
203
4
            }
204
205
6
            while (!min_heap.empty()) {
206
6
                Node node = min_heap.top();
207
6
                min_heap.pop();
208
6
                if (count == target) {
209
2
                    first_number = node.value;
210
4
                } else if (count == target + 1) {
211
2
                    second_number = node.value;
212
2
                    break;
213
2
                }
214
4
                ++count;
215
4
                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
4
            }
220
2
        }
221
222
3
        return {first_number, second_number};
223
3
    }
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
169
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
3
        Ty first_number = 0, second_number = 0;
171
3
        size_t count = 0;
172
3
        if (reverse) {
173
1
            std::priority_queue<Node> max_heap;
174
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
2
                if (!_sorted_nums_vec[i].empty()) {
176
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
2
                                     _sorted_nums_vec[i].size() - 1);
178
2
                }
179
2
            }
180
181
2
            while (!max_heap.empty()) {
182
2
                Node node = max_heap.top();
183
2
                max_heap.pop();
184
2
                if (count == target) {
185
1
                    second_number = node.value;
186
1
                } else if (count == target + 1) {
187
1
                    first_number = node.value;
188
1
                    break;
189
1
                }
190
1
                ++count;
191
1
                if (--node.element_index >= 0) {
192
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
0
                    max_heap.push(node);
194
0
                }
195
1
            }
196
197
2
        } else {
198
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
4
                if (!_sorted_nums_vec[i].empty()) {
201
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
4
                }
203
4
            }
204
205
6
            while (!min_heap.empty()) {
206
6
                Node node = min_heap.top();
207
6
                min_heap.pop();
208
6
                if (count == target) {
209
2
                    first_number = node.value;
210
4
                } else if (count == target + 1) {
211
2
                    second_number = node.value;
212
2
                    break;
213
2
                }
214
4
                ++count;
215
4
                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
4
            }
220
2
        }
221
222
3
        return {first_number, second_number};
223
3
    }
224
225
    vectorized::PODArray<Ty> _nums;
226
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
227
};
228
229
} // namespace doris