Coverage Report

Created: 2025-10-16 13:21

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.41k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
49
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
219
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
2.00k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
825
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
45
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
36
15
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
258
    Counts() = default;
37
38
1.18k
    void merge(Counts* other) {
39
1.18k
        if (other != nullptr && !other->_nums.empty()) {
40
1.18k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.18k
        }
42
1.18k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
38
6
    void merge(Counts* other) {
39
6
        if (other != nullptr && !other->_nums.empty()) {
40
6
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
6
        }
42
6
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
38
18
    void merge(Counts* other) {
39
18
        if (other != nullptr && !other->_nums.empty()) {
40
18
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
18
        }
42
18
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
722
    void merge(Counts* other) {
39
722
        if (other != nullptr && !other->_nums.empty()) {
40
722
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
722
        }
42
722
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
345
    void merge(Counts* other) {
39
345
        if (other != nullptr && !other->_nums.empty()) {
40
345
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
345
        }
42
345
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
38
6
    void merge(Counts* other) {
39
6
        if (other != nullptr && !other->_nums.empty()) {
40
6
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
6
        }
42
6
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
38
6
    void merge(Counts* other) {
39
6
        if (other != nullptr && !other->_nums.empty()) {
40
6
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
6
        }
42
6
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
86
    void merge(Counts* other) {
39
86
        if (other != nullptr && !other->_nums.empty()) {
40
86
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
86
        }
42
86
    }
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.60k
    void serialize(vectorized::BufferWritable& buf) {
59
1.60k
        if (!_nums.empty()) {
60
1.30k
            pdqsort(_nums.begin(), _nums.end());
61
1.30k
            size_t size = _nums.size();
62
1.30k
            buf.write_binary(size);
63
1.30k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.30k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
299
            _convert_sorted_num_vec_to_nums();
67
299
            serialize(buf);
68
299
        }
69
1.60k
    }
_ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
6
    void serialize(vectorized::BufferWritable& buf) {
59
6
        if (!_nums.empty()) {
60
6
            pdqsort(_nums.begin(), _nums.end());
61
6
            size_t size = _nums.size();
62
6
            buf.write_binary(size);
63
6
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
6
        } 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
6
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
18
    void serialize(vectorized::BufferWritable& buf) {
59
18
        if (!_nums.empty()) {
60
18
            pdqsort(_nums.begin(), _nums.end());
61
18
            size_t size = _nums.size();
62
18
            buf.write_binary(size);
63
18
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
18
        } 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
18
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
995
    void serialize(vectorized::BufferWritable& buf) {
59
995
        if (!_nums.empty()) {
60
839
            pdqsort(_nums.begin(), _nums.end());
61
839
            size_t size = _nums.size();
62
839
            buf.write_binary(size);
63
839
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
839
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
156
            _convert_sorted_num_vec_to_nums();
67
156
            serialize(buf);
68
156
        }
69
995
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
488
    void serialize(vectorized::BufferWritable& buf) {
59
488
        if (!_nums.empty()) {
60
345
            pdqsort(_nums.begin(), _nums.end());
61
345
            size_t size = _nums.size();
62
345
            buf.write_binary(size);
63
345
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
345
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
143
            _convert_sorted_num_vec_to_nums();
67
143
            serialize(buf);
68
143
        }
69
488
    }
_ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
6
    void serialize(vectorized::BufferWritable& buf) {
59
6
        if (!_nums.empty()) {
60
6
            pdqsort(_nums.begin(), _nums.end());
61
6
            size_t size = _nums.size();
62
6
            buf.write_binary(size);
63
6
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
6
        } 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
6
    }
_ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
6
    void serialize(vectorized::BufferWritable& buf) {
59
6
        if (!_nums.empty()) {
60
6
            pdqsort(_nums.begin(), _nums.end());
61
6
            size_t size = _nums.size();
62
6
            buf.write_binary(size);
63
6
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
6
        } 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
6
    }
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
86
    void serialize(vectorized::BufferWritable& buf) {
59
86
        if (!_nums.empty()) {
60
86
            pdqsort(_nums.begin(), _nums.end());
61
86
            size_t size = _nums.size();
62
86
            buf.write_binary(size);
63
86
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
86
        } 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
86
    }
70
71
1.18k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.18k
        size_t size;
73
1.18k
        buf.read_binary(size);
74
1.18k
        _nums.resize(size);
75
1.18k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.18k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.18k
    }
_ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
6
    void unserialize(vectorized::BufferReadable& buf) {
72
6
        size_t size;
73
6
        buf.read_binary(size);
74
6
        _nums.resize(size);
75
6
        auto buff = buf.read(sizeof(Ty) * size);
76
6
        memcpy(_nums.data(), buff.data, buff.size);
77
6
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
18
    void unserialize(vectorized::BufferReadable& buf) {
72
18
        size_t size;
73
18
        buf.read_binary(size);
74
18
        _nums.resize(size);
75
18
        auto buff = buf.read(sizeof(Ty) * size);
76
18
        memcpy(_nums.data(), buff.data, buff.size);
77
18
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
722
    void unserialize(vectorized::BufferReadable& buf) {
72
722
        size_t size;
73
722
        buf.read_binary(size);
74
722
        _nums.resize(size);
75
722
        auto buff = buf.read(sizeof(Ty) * size);
76
722
        memcpy(_nums.data(), buff.data, buff.size);
77
722
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
345
    void unserialize(vectorized::BufferReadable& buf) {
72
345
        size_t size;
73
345
        buf.read_binary(size);
74
345
        _nums.resize(size);
75
345
        auto buff = buf.read(sizeof(Ty) * size);
76
345
        memcpy(_nums.data(), buff.data, buff.size);
77
345
    }
_ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
6
    void unserialize(vectorized::BufferReadable& buf) {
72
6
        size_t size;
73
6
        buf.read_binary(size);
74
6
        _nums.resize(size);
75
6
        auto buff = buf.read(sizeof(Ty) * size);
76
6
        memcpy(_nums.data(), buff.data, buff.size);
77
6
    }
_ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
6
    void unserialize(vectorized::BufferReadable& buf) {
72
6
        size_t size;
73
6
        buf.read_binary(size);
74
6
        _nums.resize(size);
75
6
        auto buff = buf.read(sizeof(Ty) * size);
76
6
        memcpy(_nums.data(), buff.data, buff.size);
77
6
    }
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
86
    void unserialize(vectorized::BufferReadable& buf) {
72
86
        size_t size;
73
86
        buf.read_binary(size);
74
86
        _nums.resize(size);
75
86
        auto buff = buf.read(sizeof(Ty) * size);
76
86
        memcpy(_nums.data(), buff.data, buff.size);
77
86
    }
78
79
1.10k
    double terminate(double quantile) {
80
1.10k
        if (_sorted_nums_vec.size() <= 1) {
81
997
            if (_sorted_nums_vec.size() == 1) {
82
345
                _nums = std::move(_sorted_nums_vec[0]);
83
345
            }
84
85
997
            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
997
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
180
                pdqsort(_nums.begin(), _nums.end());
93
180
            }
94
95
997
            if (quantile == 1 || _nums.size() == 1) {
96
473
                return _nums.back();
97
473
            }
98
99
524
            double u = (_nums.size() - 1) * quantile;
100
524
            auto index = static_cast<uint32_t>(u);
101
524
            return _nums[index] +
102
524
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
524
                                                       static_cast<double>(_nums[index]));
104
997
        } else {
105
107
            DCHECK(_nums.empty());
106
107
            size_t rows = 0;
107
248
            for (const auto& i : _sorted_nums_vec) {
108
248
                rows += i.size();
109
248
            }
110
107
            const bool reverse = quantile > 0.5 && rows > 2;
111
107
            double u = (rows - 1) * quantile;
112
107
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
107
            size_t target = reverse ? rows - index - 2 : index;
121
107
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
107
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
107
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
107
            return first_number +
129
107
                   (u - static_cast<double>(index)) *
130
107
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
107
        }
132
1.10k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
79
61
    double terminate(double quantile) {
80
61
        if (_sorted_nums_vec.size() <= 1) {
81
58
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
58
            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
58
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
24
                pdqsort(_nums.begin(), _nums.end());
93
24
            }
94
95
58
            if (quantile == 1 || _nums.size() == 1) {
96
34
                return _nums.back();
97
34
            }
98
99
24
            double u = (_nums.size() - 1) * quantile;
100
24
            auto index = static_cast<uint32_t>(u);
101
24
            return _nums[index] +
102
24
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
24
                                                       static_cast<double>(_nums[index]));
104
58
        } else {
105
3
            DCHECK(_nums.empty());
106
3
            size_t rows = 0;
107
6
            for (const auto& i : _sorted_nums_vec) {
108
6
                rows += i.size();
109
6
            }
110
3
            const bool reverse = quantile > 0.5 && rows > 2;
111
3
            double u = (rows - 1) * quantile;
112
3
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
3
            size_t target = reverse ? rows - index - 2 : index;
121
3
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
3
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
3
            return first_number +
129
3
                   (u - static_cast<double>(index)) *
130
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
3
        }
132
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
79
299
    double terminate(double quantile) {
80
299
        if (_sorted_nums_vec.size() <= 1) {
81
293
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
293
            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
293
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
144
                pdqsort(_nums.begin(), _nums.end());
93
144
            }
94
95
294
            if (quantile == 1 || _nums.size() == 1) {
96
87
                return _nums.back();
97
87
            }
98
99
206
            double u = (_nums.size() - 1) * quantile;
100
206
            auto index = static_cast<uint32_t>(u);
101
206
            return _nums[index] +
102
206
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
206
                                                       static_cast<double>(_nums[index]));
104
293
        } else {
105
6
            DCHECK(_nums.empty());
106
6
            size_t rows = 0;
107
18
            for (const auto& i : _sorted_nums_vec) {
108
18
                rows += i.size();
109
18
            }
110
6
            const bool reverse = quantile > 0.5 && rows > 2;
111
6
            double u = (rows - 1) * quantile;
112
6
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
6
            size_t target = reverse ? rows - index - 2 : index;
121
6
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
6
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
6
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
6
            return first_number +
129
6
                   (u - static_cast<double>(index)) *
130
6
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
6
        }
132
299
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
444
    double terminate(double quantile) {
80
444
        if (_sorted_nums_vec.size() <= 1) {
81
387
            if (_sorted_nums_vec.size() == 1) {
82
269
                _nums = std::move(_sorted_nums_vec[0]);
83
269
            }
84
85
387
            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
387
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
11
                pdqsort(_nums.begin(), _nums.end());
93
11
            }
94
95
387
            if (quantile == 1 || _nums.size() == 1) {
96
239
                return _nums.back();
97
239
            }
98
99
148
            double u = (_nums.size() - 1) * quantile;
100
148
            auto index = static_cast<uint32_t>(u);
101
148
            return _nums[index] +
102
148
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
148
                                                       static_cast<double>(_nums[index]));
104
387
        } else {
105
57
            DCHECK(_nums.empty());
106
57
            size_t rows = 0;
107
114
            for (const auto& i : _sorted_nums_vec) {
108
114
                rows += i.size();
109
114
            }
110
57
            const bool reverse = quantile > 0.5 && rows > 2;
111
57
            double u = (rows - 1) * quantile;
112
57
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
57
            size_t target = reverse ? rows - index - 2 : index;
121
57
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
57
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
57
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
57
            return first_number +
129
57
                   (u - static_cast<double>(index)) *
130
57
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
57
        }
132
444
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
128
    double terminate(double quantile) {
80
128
        if (_sorted_nums_vec.size() <= 1) {
81
114
            if (_sorted_nums_vec.size() == 1) {
82
44
                _nums = std::move(_sorted_nums_vec[0]);
83
44
            }
84
85
114
            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
114
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
114
            if (quantile == 1 || _nums.size() == 1) {
96
59
                return _nums.back();
97
59
            }
98
99
55
            double u = (_nums.size() - 1) * quantile;
100
55
            auto index = static_cast<uint32_t>(u);
101
55
            return _nums[index] +
102
55
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
55
                                                       static_cast<double>(_nums[index]));
104
114
        } else {
105
14
            DCHECK(_nums.empty());
106
14
            size_t rows = 0;
107
44
            for (const auto& i : _sorted_nums_vec) {
108
44
                rows += i.size();
109
44
            }
110
14
            const bool reverse = quantile > 0.5 && rows > 2;
111
14
            double u = (rows - 1) * quantile;
112
14
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
14
            size_t target = reverse ? rows - index - 2 : index;
121
14
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
14
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
14
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
14
            return first_number +
129
14
                   (u - static_cast<double>(index)) *
130
14
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
14
        }
132
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
79
33
    double terminate(double quantile) {
80
33
        if (_sorted_nums_vec.size() <= 1) {
81
30
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
30
            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
30
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
30
            if (quantile == 1 || _nums.size() == 1) {
96
24
                return _nums.back();
97
24
            }
98
99
6
            double u = (_nums.size() - 1) * quantile;
100
6
            auto index = static_cast<uint32_t>(u);
101
6
            return _nums[index] +
102
6
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
6
                                                       static_cast<double>(_nums[index]));
104
30
        } else {
105
3
            DCHECK(_nums.empty());
106
3
            size_t rows = 0;
107
6
            for (const auto& i : _sorted_nums_vec) {
108
6
                rows += i.size();
109
6
            }
110
3
            const bool reverse = quantile > 0.5 && rows > 2;
111
3
            double u = (rows - 1) * quantile;
112
3
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
3
            size_t target = reverse ? rows - index - 2 : index;
121
3
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
3
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
3
            return first_number +
129
3
                   (u - static_cast<double>(index)) *
130
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
3
        }
132
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
79
3
    double terminate(double quantile) {
80
3
        if (_sorted_nums_vec.size() <= 1) {
81
0
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
0
            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
0
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
0
            if (quantile == 1 || _nums.size() == 1) {
96
0
                return _nums.back();
97
0
            }
98
99
0
            double u = (_nums.size() - 1) * quantile;
100
0
            auto index = static_cast<uint32_t>(u);
101
0
            return _nums[index] +
102
0
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
0
                                                       static_cast<double>(_nums[index]));
104
3
        } else {
105
3
            DCHECK(_nums.empty());
106
3
            size_t rows = 0;
107
6
            for (const auto& i : _sorted_nums_vec) {
108
6
                rows += i.size();
109
6
            }
110
3
            const bool reverse = quantile > 0.5 && rows > 2;
111
3
            double u = (rows - 1) * quantile;
112
3
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
3
            size_t target = reverse ? rows - index - 2 : index;
121
3
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
3
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
3
            return first_number +
129
3
                   (u - static_cast<double>(index)) *
130
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
3
        }
132
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
79
136
    double terminate(double quantile) {
80
136
        if (_sorted_nums_vec.size() <= 1) {
81
115
            if (_sorted_nums_vec.size() == 1) {
82
32
                _nums = std::move(_sorted_nums_vec[0]);
83
32
            }
84
85
115
            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
115
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
115
            if (quantile == 1 || _nums.size() == 1) {
96
30
                return _nums.back();
97
30
            }
98
99
85
            double u = (_nums.size() - 1) * quantile;
100
85
            auto index = static_cast<uint32_t>(u);
101
85
            return _nums[index] +
102
85
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
85
                                                       static_cast<double>(_nums[index]));
104
115
        } else {
105
21
            DCHECK(_nums.empty());
106
21
            size_t rows = 0;
107
54
            for (const auto& i : _sorted_nums_vec) {
108
54
                rows += i.size();
109
54
            }
110
21
            const bool reverse = quantile > 0.5 && rows > 2;
111
21
            double u = (rows - 1) * quantile;
112
21
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
21
            size_t target = reverse ? rows - index - 2 : index;
121
21
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
21
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
21
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
21
            return first_number +
129
21
                   (u - static_cast<double>(index)) *
130
21
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
21
        }
132
136
    }
133
134
private:
135
    struct Node {
136
        Ty value;
137
        int array_index;
138
        int64_t element_index;
139
140
991
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIaE4NodessERKS2_
Line
Count
Source
140
6
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
140
72
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
140
487
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
140
351
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsInE4NodessERKS2_
Line
Count
Source
140
6
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIfE4NodessERKS2_
Line
Count
Source
140
6
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
140
63
        auto operator<=>(const Node& other) const { return value <=> other.value; }
141
    };
142
143
298
    void _convert_sorted_num_vec_to_nums() {
144
298
        size_t rows = 0;
145
595
        for (const auto& i : _sorted_nums_vec) {
146
595
            rows += i.size();
147
595
        }
148
298
        _nums.resize(rows);
149
298
        size_t count = 0;
150
151
298
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
894
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
596
            if (!_sorted_nums_vec[i].empty()) {
154
596
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
596
            }
156
596
        }
157
158
1.04k
        while (!min_heap.empty()) {
159
743
            Node node = min_heap.top();
160
743
            min_heap.pop();
161
743
            _nums[count++] = node.value;
162
743
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
147
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
147
                min_heap.push(node);
165
147
            }
166
743
        }
167
298
        _sorted_nums_vec.clear();
168
298
    }
Unexecuted instantiation: _ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv
_ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
143
155
    void _convert_sorted_num_vec_to_nums() {
144
155
        size_t rows = 0;
145
338
        for (const auto& i : _sorted_nums_vec) {
146
338
            rows += i.size();
147
338
        }
148
155
        _nums.resize(rows);
149
155
        size_t count = 0;
150
151
155
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
494
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
339
            if (!_sorted_nums_vec[i].empty()) {
154
339
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
339
            }
156
339
        }
157
158
530
        while (!min_heap.empty()) {
159
375
            Node node = min_heap.top();
160
375
            min_heap.pop();
161
375
            _nums[count++] = node.value;
162
375
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
36
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
36
                min_heap.push(node);
165
36
            }
166
375
        }
167
155
        _sorted_nums_vec.clear();
168
155
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
143
143
    void _convert_sorted_num_vec_to_nums() {
144
143
        size_t rows = 0;
145
257
        for (const auto& i : _sorted_nums_vec) {
146
257
            rows += i.size();
147
257
        }
148
143
        _nums.resize(rows);
149
143
        size_t count = 0;
150
151
143
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
400
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
257
            if (!_sorted_nums_vec[i].empty()) {
154
257
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
257
            }
156
257
        }
157
158
511
        while (!min_heap.empty()) {
159
368
            Node node = min_heap.top();
160
368
            min_heap.pop();
161
368
            _nums[count++] = node.value;
162
368
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
111
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
111
                min_heap.push(node);
165
111
            }
166
368
        }
167
143
        _sorted_nums_vec.clear();
168
143
    }
Unexecuted instantiation: _ZN5doris6CountsInE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIfE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIdE31_convert_sorted_num_vec_to_numsEv
169
170
107
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
107
        Ty first_number = 0, second_number = 0;
172
107
        size_t count = 0;
173
107
        if (reverse) {
174
26
            std::priority_queue<Node> max_heap;
175
98
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
72
                if (!_sorted_nums_vec[i].empty()) {
177
72
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
72
                                     _sorted_nums_vec[i].size() - 1);
179
72
                }
180
72
            }
181
182
102
            while (!max_heap.empty()) {
183
102
                Node node = max_heap.top();
184
102
                max_heap.pop();
185
102
                if (count == target) {
186
26
                    second_number = node.value;
187
76
                } else if (count == target + 1) {
188
26
                    first_number = node.value;
189
26
                    break;
190
26
                }
191
76
                ++count;
192
76
                if (--node.element_index >= 0) {
193
62
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
62
                    max_heap.push(node);
195
62
                }
196
76
            }
197
198
81
        } else {
199
81
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
257
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
176
                if (!_sorted_nums_vec[i].empty()) {
202
176
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
176
                }
204
176
            }
205
206
267
            while (!min_heap.empty()) {
207
267
                Node node = min_heap.top();
208
267
                min_heap.pop();
209
267
                if (count == target) {
210
81
                    first_number = node.value;
211
186
                } else if (count == target + 1) {
212
81
                    second_number = node.value;
213
81
                    break;
214
81
                }
215
186
                ++count;
216
186
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
153
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
153
                    min_heap.push(node);
219
153
                }
220
186
            }
221
81
        }
222
223
107
        return {first_number, second_number};
224
107
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
3
        Ty first_number = 0, second_number = 0;
172
3
        size_t count = 0;
173
3
        if (reverse) {
174
1
            std::priority_queue<Node> max_heap;
175
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
2
                if (!_sorted_nums_vec[i].empty()) {
177
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
2
                                     _sorted_nums_vec[i].size() - 1);
179
2
                }
180
2
            }
181
182
2
            while (!max_heap.empty()) {
183
2
                Node node = max_heap.top();
184
2
                max_heap.pop();
185
2
                if (count == target) {
186
1
                    second_number = node.value;
187
1
                } else if (count == target + 1) {
188
1
                    first_number = node.value;
189
1
                    break;
190
1
                }
191
1
                ++count;
192
1
                if (--node.element_index >= 0) {
193
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
1
                    max_heap.push(node);
195
1
                }
196
1
            }
197
198
2
        } else {
199
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
4
                if (!_sorted_nums_vec[i].empty()) {
202
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
4
                }
204
4
            }
205
206
6
            while (!min_heap.empty()) {
207
6
                Node node = min_heap.top();
208
6
                min_heap.pop();
209
6
                if (count == target) {
210
2
                    first_number = node.value;
211
4
                } else if (count == target + 1) {
212
2
                    second_number = node.value;
213
2
                    break;
214
2
                }
215
4
                ++count;
216
4
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
2
                    min_heap.push(node);
219
2
                }
220
4
            }
221
2
        }
222
223
3
        return {first_number, second_number};
224
3
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
6
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
6
        Ty first_number = 0, second_number = 0;
172
6
        size_t count = 0;
173
6
        if (reverse) {
174
2
            std::priority_queue<Node> max_heap;
175
8
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
6
                if (!_sorted_nums_vec[i].empty()) {
177
6
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
6
                                     _sorted_nums_vec[i].size() - 1);
179
6
                }
180
6
            }
181
182
8
            while (!max_heap.empty()) {
183
8
                Node node = max_heap.top();
184
8
                max_heap.pop();
185
8
                if (count == target) {
186
2
                    second_number = node.value;
187
6
                } else if (count == target + 1) {
188
2
                    first_number = node.value;
189
2
                    break;
190
2
                }
191
6
                ++count;
192
6
                if (--node.element_index >= 0) {
193
4
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
4
                    max_heap.push(node);
195
4
                }
196
6
            }
197
198
4
        } else {
199
4
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
16
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
12
                if (!_sorted_nums_vec[i].empty()) {
202
12
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
12
                }
204
12
            }
205
206
19
            while (!min_heap.empty()) {
207
19
                Node node = min_heap.top();
208
19
                min_heap.pop();
209
19
                if (count == target) {
210
4
                    first_number = node.value;
211
15
                } else if (count == target + 1) {
212
4
                    second_number = node.value;
213
4
                    break;
214
4
                }
215
15
                ++count;
216
15
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
13
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
13
                    min_heap.push(node);
219
13
                }
220
15
            }
221
4
        }
222
223
6
        return {first_number, second_number};
224
6
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
57
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
57
        Ty first_number = 0, second_number = 0;
172
57
        size_t count = 0;
173
57
        if (reverse) {
174
9
            std::priority_queue<Node> max_heap;
175
27
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
18
                if (!_sorted_nums_vec[i].empty()) {
177
18
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
18
                                     _sorted_nums_vec[i].size() - 1);
179
18
                }
180
18
            }
181
182
37
            while (!max_heap.empty()) {
183
37
                Node node = max_heap.top();
184
37
                max_heap.pop();
185
37
                if (count == target) {
186
9
                    second_number = node.value;
187
28
                } else if (count == target + 1) {
188
9
                    first_number = node.value;
189
9
                    break;
190
9
                }
191
28
                ++count;
192
28
                if (--node.element_index >= 0) {
193
24
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
24
                    max_heap.push(node);
195
24
                }
196
28
            }
197
198
48
        } else {
199
48
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
144
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
96
                if (!_sorted_nums_vec[i].empty()) {
202
96
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
96
                }
204
96
            }
205
206
166
            while (!min_heap.empty()) {
207
166
                Node node = min_heap.top();
208
166
                min_heap.pop();
209
166
                if (count == target) {
210
48
                    first_number = node.value;
211
118
                } else if (count == target + 1) {
212
48
                    second_number = node.value;
213
48
                    break;
214
48
                }
215
118
                ++count;
216
118
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
115
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
115
                    min_heap.push(node);
219
115
                }
220
118
            }
221
48
        }
222
223
57
        return {first_number, second_number};
224
57
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
14
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
14
        Ty first_number = 0, second_number = 0;
172
14
        size_t count = 0;
173
14
        if (reverse) {
174
9
            std::priority_queue<Node> max_heap;
175
43
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
34
                if (!_sorted_nums_vec[i].empty()) {
177
34
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
34
                                     _sorted_nums_vec[i].size() - 1);
179
34
                }
180
34
            }
181
182
44
            while (!max_heap.empty()) {
183
44
                Node node = max_heap.top();
184
44
                max_heap.pop();
185
44
                if (count == target) {
186
9
                    second_number = node.value;
187
35
                } else if (count == target + 1) {
188
9
                    first_number = node.value;
189
9
                    break;
190
9
                }
191
35
                ++count;
192
35
                if (--node.element_index >= 0) {
193
30
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
30
                    max_heap.push(node);
195
30
                }
196
35
            }
197
198
9
        } else {
199
5
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
15
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
10
                if (!_sorted_nums_vec[i].empty()) {
202
10
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
10
                }
204
10
            }
205
206
18
            while (!min_heap.empty()) {
207
18
                Node node = min_heap.top();
208
18
                min_heap.pop();
209
18
                if (count == target) {
210
5
                    first_number = node.value;
211
13
                } else if (count == target + 1) {
212
5
                    second_number = node.value;
213
5
                    break;
214
5
                }
215
13
                ++count;
216
13
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
11
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
11
                    min_heap.push(node);
219
11
                }
220
13
            }
221
5
        }
222
223
14
        return {first_number, second_number};
224
14
    }
_ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
3
        Ty first_number = 0, second_number = 0;
172
3
        size_t count = 0;
173
3
        if (reverse) {
174
1
            std::priority_queue<Node> max_heap;
175
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
2
                if (!_sorted_nums_vec[i].empty()) {
177
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
2
                                     _sorted_nums_vec[i].size() - 1);
179
2
                }
180
2
            }
181
182
2
            while (!max_heap.empty()) {
183
2
                Node node = max_heap.top();
184
2
                max_heap.pop();
185
2
                if (count == target) {
186
1
                    second_number = node.value;
187
1
                } else if (count == target + 1) {
188
1
                    first_number = node.value;
189
1
                    break;
190
1
                }
191
1
                ++count;
192
1
                if (--node.element_index >= 0) {
193
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
1
                    max_heap.push(node);
195
1
                }
196
1
            }
197
198
2
        } else {
199
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
4
                if (!_sorted_nums_vec[i].empty()) {
202
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
4
                }
204
4
            }
205
206
6
            while (!min_heap.empty()) {
207
6
                Node node = min_heap.top();
208
6
                min_heap.pop();
209
6
                if (count == target) {
210
2
                    first_number = node.value;
211
4
                } else if (count == target + 1) {
212
2
                    second_number = node.value;
213
2
                    break;
214
2
                }
215
4
                ++count;
216
4
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
2
                    min_heap.push(node);
219
2
                }
220
4
            }
221
2
        }
222
223
3
        return {first_number, second_number};
224
3
    }
_ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
3
        Ty first_number = 0, second_number = 0;
172
3
        size_t count = 0;
173
3
        if (reverse) {
174
1
            std::priority_queue<Node> max_heap;
175
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
2
                if (!_sorted_nums_vec[i].empty()) {
177
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
2
                                     _sorted_nums_vec[i].size() - 1);
179
2
                }
180
2
            }
181
182
2
            while (!max_heap.empty()) {
183
2
                Node node = max_heap.top();
184
2
                max_heap.pop();
185
2
                if (count == target) {
186
1
                    second_number = node.value;
187
1
                } else if (count == target + 1) {
188
1
                    first_number = node.value;
189
1
                    break;
190
1
                }
191
1
                ++count;
192
1
                if (--node.element_index >= 0) {
193
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
1
                    max_heap.push(node);
195
1
                }
196
1
            }
197
198
2
        } else {
199
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
4
                if (!_sorted_nums_vec[i].empty()) {
202
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
4
                }
204
4
            }
205
206
6
            while (!min_heap.empty()) {
207
6
                Node node = min_heap.top();
208
6
                min_heap.pop();
209
6
                if (count == target) {
210
2
                    first_number = node.value;
211
4
                } else if (count == target + 1) {
212
2
                    second_number = node.value;
213
2
                    break;
214
2
                }
215
4
                ++count;
216
4
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
2
                    min_heap.push(node);
219
2
                }
220
4
            }
221
2
        }
222
223
3
        return {first_number, second_number};
224
3
    }
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
21
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
21
        Ty first_number = 0, second_number = 0;
172
21
        size_t count = 0;
173
21
        if (reverse) {
174
3
            std::priority_queue<Node> max_heap;
175
11
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
8
                if (!_sorted_nums_vec[i].empty()) {
177
8
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
8
                                     _sorted_nums_vec[i].size() - 1);
179
8
                }
180
8
            }
181
182
7
            while (!max_heap.empty()) {
183
7
                Node node = max_heap.top();
184
7
                max_heap.pop();
185
7
                if (count == target) {
186
3
                    second_number = node.value;
187
4
                } else if (count == target + 1) {
188
3
                    first_number = node.value;
189
3
                    break;
190
3
                }
191
4
                ++count;
192
4
                if (--node.element_index >= 0) {
193
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
1
                    max_heap.push(node);
195
1
                }
196
4
            }
197
198
18
        } else {
199
18
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
64
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
46
                if (!_sorted_nums_vec[i].empty()) {
202
46
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
46
                }
204
46
            }
205
206
46
            while (!min_heap.empty()) {
207
46
                Node node = min_heap.top();
208
46
                min_heap.pop();
209
46
                if (count == target) {
210
18
                    first_number = node.value;
211
28
                } else if (count == target + 1) {
212
18
                    second_number = node.value;
213
18
                    break;
214
18
                }
215
28
                ++count;
216
28
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
8
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
8
                    min_heap.push(node);
219
8
                }
220
28
            }
221
18
        }
222
223
21
        return {first_number, second_number};
224
21
    }
225
226
    vectorized::PODArray<Ty> _nums;
227
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
228
};
229
230
} // namespace doris