Coverage Report

Created: 2026-01-14 11:17

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.44k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
47
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
217
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
1.99k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
843
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
43
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
36
13
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
280
    Counts() = default;
37
38
1.20k
    void merge(Counts* other) {
39
1.20k
        if (other != nullptr && !other->_nums.empty()) {
40
1.20k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.20k
        }
42
1.20k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
38
5
    void merge(Counts* other) {
39
5
        if (other != nullptr && !other->_nums.empty()) {
40
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
5
        }
42
5
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
38
17
    void merge(Counts* other) {
39
17
        if (other != nullptr && !other->_nums.empty()) {
40
17
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
17
        }
42
17
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
718
    void merge(Counts* other) {
39
718
        if (other != nullptr && !other->_nums.empty()) {
40
718
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
718
        }
42
718
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
354
    void merge(Counts* other) {
39
354
        if (other != nullptr && !other->_nums.empty()) {
40
354
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
354
        }
42
354
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
38
5
    void merge(Counts* other) {
39
5
        if (other != nullptr && !other->_nums.empty()) {
40
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
5
        }
42
5
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
38
5
    void merge(Counts* other) {
39
5
        if (other != nullptr && !other->_nums.empty()) {
40
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
5
        }
42
5
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
97
    void merge(Counts* other) {
39
97
        if (other != nullptr && !other->_nums.empty()) {
40
97
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
97
        }
42
97
    }
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.61k
    void serialize(vectorized::BufferWritable& buf) {
59
1.61k
        if (!_nums.empty()) {
60
1.31k
            pdqsort(_nums.begin(), _nums.end());
61
1.31k
            size_t size = _nums.size();
62
1.31k
            buf.write_binary(size);
63
1.31k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.31k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
295
            _convert_sorted_num_vec_to_nums();
67
295
            serialize(buf);
68
295
        }
69
1.61k
    }
_ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
5
    void serialize(vectorized::BufferWritable& buf) {
59
5
        if (!_nums.empty()) {
60
5
            pdqsort(_nums.begin(), _nums.end());
61
5
            size_t size = _nums.size();
62
5
            buf.write_binary(size);
63
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
5
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
5
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
17
    void serialize(vectorized::BufferWritable& buf) {
59
17
        if (!_nums.empty()) {
60
17
            pdqsort(_nums.begin(), _nums.end());
61
17
            size_t size = _nums.size();
62
17
            buf.write_binary(size);
63
17
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
17
        } 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
17
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
991
    void serialize(vectorized::BufferWritable& buf) {
59
991
        if (!_nums.empty()) {
60
835
            pdqsort(_nums.begin(), _nums.end());
61
835
            size_t size = _nums.size();
62
835
            buf.write_binary(size);
63
835
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
835
        } 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
991
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
493
    void serialize(vectorized::BufferWritable& buf) {
59
493
        if (!_nums.empty()) {
60
354
            pdqsort(_nums.begin(), _nums.end());
61
354
            size_t size = _nums.size();
62
354
            buf.write_binary(size);
63
354
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
354
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
139
            _convert_sorted_num_vec_to_nums();
67
139
            serialize(buf);
68
139
        }
69
493
    }
_ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
5
    void serialize(vectorized::BufferWritable& buf) {
59
5
        if (!_nums.empty()) {
60
5
            pdqsort(_nums.begin(), _nums.end());
61
5
            size_t size = _nums.size();
62
5
            buf.write_binary(size);
63
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
5
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
5
    }
_ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
5
    void serialize(vectorized::BufferWritable& buf) {
59
5
        if (!_nums.empty()) {
60
5
            pdqsort(_nums.begin(), _nums.end());
61
5
            size_t size = _nums.size();
62
5
            buf.write_binary(size);
63
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
5
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
5
    }
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
97
    void serialize(vectorized::BufferWritable& buf) {
59
97
        if (!_nums.empty()) {
60
97
            pdqsort(_nums.begin(), _nums.end());
61
97
            size_t size = _nums.size();
62
97
            buf.write_binary(size);
63
97
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
97
        } 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
97
    }
70
71
1.20k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.20k
        size_t size;
73
1.20k
        buf.read_binary(size);
74
1.20k
        _nums.resize(size);
75
1.20k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.20k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.20k
    }
_ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
5
    void unserialize(vectorized::BufferReadable& buf) {
72
5
        size_t size;
73
5
        buf.read_binary(size);
74
5
        _nums.resize(size);
75
5
        auto buff = buf.read(sizeof(Ty) * size);
76
5
        memcpy(_nums.data(), buff.data, buff.size);
77
5
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
17
    void unserialize(vectorized::BufferReadable& buf) {
72
17
        size_t size;
73
17
        buf.read_binary(size);
74
17
        _nums.resize(size);
75
17
        auto buff = buf.read(sizeof(Ty) * size);
76
17
        memcpy(_nums.data(), buff.data, buff.size);
77
17
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
718
    void unserialize(vectorized::BufferReadable& buf) {
72
718
        size_t size;
73
718
        buf.read_binary(size);
74
718
        _nums.resize(size);
75
718
        auto buff = buf.read(sizeof(Ty) * size);
76
718
        memcpy(_nums.data(), buff.data, buff.size);
77
718
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
354
    void unserialize(vectorized::BufferReadable& buf) {
72
354
        size_t size;
73
354
        buf.read_binary(size);
74
354
        _nums.resize(size);
75
354
        auto buff = buf.read(sizeof(Ty) * size);
76
354
        memcpy(_nums.data(), buff.data, buff.size);
77
354
    }
_ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
5
    void unserialize(vectorized::BufferReadable& buf) {
72
5
        size_t size;
73
5
        buf.read_binary(size);
74
5
        _nums.resize(size);
75
5
        auto buff = buf.read(sizeof(Ty) * size);
76
5
        memcpy(_nums.data(), buff.data, buff.size);
77
5
    }
_ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
5
    void unserialize(vectorized::BufferReadable& buf) {
72
5
        size_t size;
73
5
        buf.read_binary(size);
74
5
        _nums.resize(size);
75
5
        auto buff = buf.read(sizeof(Ty) * size);
76
5
        memcpy(_nums.data(), buff.data, buff.size);
77
5
    }
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
97
    void unserialize(vectorized::BufferReadable& buf) {
72
97
        size_t size;
73
97
        buf.read_binary(size);
74
97
        _nums.resize(size);
75
97
        auto buff = buf.read(sizeof(Ty) * size);
76
97
        memcpy(_nums.data(), buff.data, buff.size);
77
97
    }
78
79
1.10k
    double terminate(double quantile) {
80
1.10k
        if (_sorted_nums_vec.size() <= 1) {
81
1.03k
            if (_sorted_nums_vec.size() == 1) {
82
381
                _nums = std::move(_sorted_nums_vec[0]);
83
381
            }
84
85
1.03k
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
1.03k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
180
                pdqsort(_nums.begin(), _nums.end());
93
180
            }
94
95
1.03k
            if (quantile == 1 || _nums.size() == 1) {
96
473
                return _nums.back();
97
473
            }
98
99
561
            double u = (_nums.size() - 1) * quantile;
100
561
            auto index = static_cast<uint32_t>(u);
101
561
            return _nums[index] +
102
561
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
561
                                                       static_cast<double>(_nums[index]));
104
1.03k
        } else {
105
71
            DCHECK(_nums.empty());
106
71
            size_t rows = 0;
107
224
            for (const auto& i : _sorted_nums_vec) {
108
224
                rows += i.size();
109
224
            }
110
71
            const bool reverse = quantile > 0.5 && rows > 2;
111
71
            double u = (rows - 1) * quantile;
112
71
            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
71
            size_t target = reverse ? rows - index - 2 : index;
121
71
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
71
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
71
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
71
            return first_number +
129
71
                   (u - static_cast<double>(index)) *
130
71
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
71
        }
132
1.10k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
79
61
    double terminate(double quantile) {
80
61
        if (_sorted_nums_vec.size() <= 1) {
81
59
            if (_sorted_nums_vec.size() == 1) {
82
1
                _nums = std::move(_sorted_nums_vec[0]);
83
1
            }
84
85
59
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
59
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
24
                pdqsort(_nums.begin(), _nums.end());
93
24
            }
94
95
59
            if (quantile == 1 || _nums.size() == 1) {
96
34
                return _nums.back();
97
34
            }
98
99
25
            double u = (_nums.size() - 1) * quantile;
100
25
            auto index = static_cast<uint32_t>(u);
101
25
            return _nums[index] +
102
25
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
25
                                                       static_cast<double>(_nums[index]));
104
59
        } else {
105
2
            DCHECK(_nums.empty());
106
2
            size_t rows = 0;
107
4
            for (const auto& i : _sorted_nums_vec) {
108
4
                rows += i.size();
109
4
            }
110
2
            const bool reverse = quantile > 0.5 && rows > 2;
111
2
            double u = (rows - 1) * quantile;
112
2
            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
2
            size_t target = reverse ? rows - index - 2 : index;
121
2
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
2
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
2
            return first_number +
129
2
                   (u - static_cast<double>(index)) *
130
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
2
        }
132
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
79
300
    double terminate(double quantile) {
80
300
        if (_sorted_nums_vec.size() <= 1) {
81
295
            if (_sorted_nums_vec.size() == 1) {
82
1
                _nums = std::move(_sorted_nums_vec[0]);
83
1
            }
84
85
295
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
295
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
144
                pdqsort(_nums.begin(), _nums.end());
93
144
            }
94
95
295
            if (quantile == 1 || _nums.size() == 1) {
96
87
                return _nums.back();
97
87
            }
98
99
208
            double u = (_nums.size() - 1) * quantile;
100
208
            auto index = static_cast<uint32_t>(u);
101
208
            return _nums[index] +
102
208
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
208
                                                       static_cast<double>(_nums[index]));
104
295
        } else {
105
5
            DCHECK(_nums.empty());
106
5
            size_t rows = 0;
107
16
            for (const auto& i : _sorted_nums_vec) {
108
16
                rows += i.size();
109
16
            }
110
5
            const bool reverse = quantile > 0.5 && rows > 2;
111
5
            double u = (rows - 1) * quantile;
112
5
            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
5
            size_t target = reverse ? rows - index - 2 : index;
121
5
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
5
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
5
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
5
            return first_number +
129
5
                   (u - static_cast<double>(index)) *
130
5
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
5
        }
132
300
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
444
    double terminate(double quantile) {
80
444
        if (_sorted_nums_vec.size() <= 1) {
81
425
            if (_sorted_nums_vec.size() == 1) {
82
307
                _nums = std::move(_sorted_nums_vec[0]);
83
307
            }
84
85
425
            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
425
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
11
                pdqsort(_nums.begin(), _nums.end());
93
11
            }
94
95
425
            if (quantile == 1 || _nums.size() == 1) {
96
239
                return _nums.back();
97
239
            }
98
99
186
            double u = (_nums.size() - 1) * quantile;
100
186
            auto index = static_cast<uint32_t>(u);
101
186
            return _nums[index] +
102
186
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
186
                                                       static_cast<double>(_nums[index]));
104
425
        } else {
105
19
            DCHECK(_nums.empty());
106
19
            size_t rows = 0;
107
72
            for (const auto& i : _sorted_nums_vec) {
108
72
                rows += i.size();
109
72
            }
110
19
            const bool reverse = quantile > 0.5 && rows > 2;
111
19
            double u = (rows - 1) * quantile;
112
19
            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
19
            size_t target = reverse ? rows - index - 2 : index;
121
19
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
19
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
19
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
19
            return first_number +
129
19
                   (u - static_cast<double>(index)) *
130
19
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
19
        }
132
444
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
128
    double terminate(double quantile) {
80
128
        if (_sorted_nums_vec.size() <= 1) {
81
113
            if (_sorted_nums_vec.size() == 1) {
82
43
                _nums = std::move(_sorted_nums_vec[0]);
83
43
            }
84
85
113
            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
113
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
113
            if (quantile == 1 || _nums.size() == 1) {
96
59
                return _nums.back();
97
59
            }
98
99
54
            double u = (_nums.size() - 1) * quantile;
100
54
            auto index = static_cast<uint32_t>(u);
101
54
            return _nums[index] +
102
54
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
54
                                                       static_cast<double>(_nums[index]));
104
113
        } else {
105
15
            DCHECK(_nums.empty());
106
15
            size_t rows = 0;
107
54
            for (const auto& i : _sorted_nums_vec) {
108
54
                rows += i.size();
109
54
            }
110
15
            const bool reverse = quantile > 0.5 && rows > 2;
111
15
            double u = (rows - 1) * quantile;
112
15
            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
15
            size_t target = reverse ? rows - index - 2 : index;
121
15
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
15
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
15
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
15
            return first_number +
129
15
                   (u - static_cast<double>(index)) *
130
15
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
15
        }
132
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
79
33
    double terminate(double quantile) {
80
33
        if (_sorted_nums_vec.size() <= 1) {
81
31
            if (_sorted_nums_vec.size() == 1) {
82
1
                _nums = std::move(_sorted_nums_vec[0]);
83
1
            }
84
85
31
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
31
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
31
            if (quantile == 1 || _nums.size() == 1) {
96
24
                return _nums.back();
97
24
            }
98
99
7
            double u = (_nums.size() - 1) * quantile;
100
7
            auto index = static_cast<uint32_t>(u);
101
7
            return _nums[index] +
102
7
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
7
                                                       static_cast<double>(_nums[index]));
104
31
        } else {
105
2
            DCHECK(_nums.empty());
106
2
            size_t rows = 0;
107
4
            for (const auto& i : _sorted_nums_vec) {
108
4
                rows += i.size();
109
4
            }
110
2
            const bool reverse = quantile > 0.5 && rows > 2;
111
2
            double u = (rows - 1) * quantile;
112
2
            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
2
            size_t target = reverse ? rows - index - 2 : index;
121
2
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
2
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
2
            return first_number +
129
2
                   (u - static_cast<double>(index)) *
130
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
2
        }
132
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
79
3
    double terminate(double quantile) {
80
3
        if (_sorted_nums_vec.size() <= 1) {
81
1
            if (_sorted_nums_vec.size() == 1) {
82
1
                _nums = std::move(_sorted_nums_vec[0]);
83
1
            }
84
85
1
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
1
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
1
            if (quantile == 1 || _nums.size() == 1) {
96
0
                return _nums.back();
97
0
            }
98
99
1
            double u = (_nums.size() - 1) * quantile;
100
1
            auto index = static_cast<uint32_t>(u);
101
1
            return _nums[index] +
102
1
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
1
                                                       static_cast<double>(_nums[index]));
104
2
        } else {
105
2
            DCHECK(_nums.empty());
106
2
            size_t rows = 0;
107
4
            for (const auto& i : _sorted_nums_vec) {
108
4
                rows += i.size();
109
4
            }
110
2
            const bool reverse = quantile > 0.5 && rows > 2;
111
2
            double u = (rows - 1) * quantile;
112
2
            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
2
            size_t target = reverse ? rows - index - 2 : index;
121
2
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
2
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
2
            return first_number +
129
2
                   (u - static_cast<double>(index)) *
130
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
2
        }
132
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
79
136
    double terminate(double quantile) {
80
136
        if (_sorted_nums_vec.size() <= 1) {
81
110
            if (_sorted_nums_vec.size() == 1) {
82
27
                _nums = std::move(_sorted_nums_vec[0]);
83
27
            }
84
85
110
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
110
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
110
            if (quantile == 1 || _nums.size() == 1) {
96
30
                return _nums.back();
97
30
            }
98
99
80
            double u = (_nums.size() - 1) * quantile;
100
80
            auto index = static_cast<uint32_t>(u);
101
80
            return _nums[index] +
102
80
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
80
                                                       static_cast<double>(_nums[index]));
104
110
        } else {
105
26
            DCHECK(_nums.empty());
106
26
            size_t rows = 0;
107
70
            for (const auto& i : _sorted_nums_vec) {
108
70
                rows += i.size();
109
70
            }
110
26
            const bool reverse = quantile > 0.5 && rows > 2;
111
26
            double u = (rows - 1) * quantile;
112
26
            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
26
            size_t target = reverse ? rows - index - 2 : index;
121
26
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
26
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
26
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
26
            return first_number +
129
26
                   (u - static_cast<double>(index)) *
130
26
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
26
        }
132
136
    }
133
134
private:
135
    struct Node {
136
        Ty value;
137
        int array_index;
138
        int64_t element_index;
139
140
1.16k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIaE4NodessERKS2_
Line
Count
Source
140
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
140
71
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
140
580
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
140
425
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsInE4NodessERKS2_
Line
Count
Source
140
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIfE4NodessERKS2_
Line
Count
Source
140
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
140
76
        auto operator<=>(const Node& other) const { return value <=> other.value; }
141
    };
142
143
295
    void _convert_sorted_num_vec_to_nums() {
144
295
        size_t rows = 0;
145
596
        for (const auto& i : _sorted_nums_vec) {
146
596
            rows += i.size();
147
596
        }
148
295
        _nums.resize(rows);
149
295
        size_t count = 0;
150
151
295
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
891
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
596
            if (!_sorted_nums_vec[i].empty()) {
154
596
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
596
            }
156
596
        }
157
158
1.03k
        while (!min_heap.empty()) {
159
743
            Node node = min_heap.top();
160
743
            min_heap.pop();
161
743
            _nums[count++] = node.value;
162
743
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
147
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
147
                min_heap.push(node);
165
147
            }
166
743
        }
167
295
        _sorted_nums_vec.clear();
168
295
    }
Unexecuted instantiation: _ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv
_ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
143
156
    void _convert_sorted_num_vec_to_nums() {
144
156
        size_t rows = 0;
145
339
        for (const auto& i : _sorted_nums_vec) {
146
339
            rows += i.size();
147
339
        }
148
156
        _nums.resize(rows);
149
156
        size_t count = 0;
150
151
156
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
495
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
339
            if (!_sorted_nums_vec[i].empty()) {
154
339
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
339
            }
156
339
        }
157
158
531
        while (!min_heap.empty()) {
159
375
            Node node = min_heap.top();
160
375
            min_heap.pop();
161
375
            _nums[count++] = node.value;
162
375
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
36
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
36
                min_heap.push(node);
165
36
            }
166
375
        }
167
156
        _sorted_nums_vec.clear();
168
156
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
143
139
    void _convert_sorted_num_vec_to_nums() {
144
139
        size_t rows = 0;
145
257
        for (const auto& i : _sorted_nums_vec) {
146
257
            rows += i.size();
147
257
        }
148
139
        _nums.resize(rows);
149
139
        size_t count = 0;
150
151
139
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
396
        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
507
        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
139
        _sorted_nums_vec.clear();
168
139
    }
Unexecuted instantiation: _ZN5doris6CountsInE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIfE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIdE31_convert_sorted_num_vec_to_numsEv
169
170
71
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
71
        Ty first_number = 0, second_number = 0;
172
71
        size_t count = 0;
173
71
        if (reverse) {
174
24
            std::priority_queue<Node> max_heap;
175
105
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
81
                if (!_sorted_nums_vec[i].empty()) {
177
81
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
81
                                     _sorted_nums_vec[i].size() - 1);
179
81
                }
180
81
            }
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
24
                    second_number = node.value;
187
78
                } else if (count == target + 1) {
188
24
                    first_number = node.value;
189
24
                    break;
190
24
                }
191
78
                ++count;
192
78
                if (--node.element_index >= 0) {
193
65
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
65
                    max_heap.push(node);
195
65
                }
196
78
            }
197
198
47
        } else {
199
47
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
190
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
143
                if (!_sorted_nums_vec[i].empty()) {
202
143
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
143
                }
204
143
            }
205
206
156
            while (!min_heap.empty()) {
207
156
                Node node = min_heap.top();
208
156
                min_heap.pop();
209
156
                if (count == target) {
210
47
                    first_number = node.value;
211
109
                } else if (count == target + 1) {
212
47
                    second_number = node.value;
213
47
                    break;
214
47
                }
215
109
                ++count;
216
109
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
72
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
72
                    min_heap.push(node);
219
72
                }
220
109
            }
221
47
        }
222
223
71
        return {first_number, second_number};
224
71
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
2
        Ty first_number = 0, second_number = 0;
172
2
        size_t count = 0;
173
2
        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
1
        } else {
199
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
2
                if (!_sorted_nums_vec[i].empty()) {
202
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
2
                }
204
2
            }
205
206
3
            while (!min_heap.empty()) {
207
3
                Node node = min_heap.top();
208
3
                min_heap.pop();
209
3
                if (count == target) {
210
1
                    first_number = node.value;
211
2
                } else if (count == target + 1) {
212
1
                    second_number = node.value;
213
1
                    break;
214
1
                }
215
2
                ++count;
216
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
1
                    min_heap.push(node);
219
1
                }
220
2
            }
221
1
        }
222
223
2
        return {first_number, second_number};
224
2
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
5
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
5
        Ty first_number = 0, second_number = 0;
172
5
        size_t count = 0;
173
5
        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
3
        } else {
199
3
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
13
            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
16
            while (!min_heap.empty()) {
207
16
                Node node = min_heap.top();
208
16
                min_heap.pop();
209
16
                if (count == target) {
210
3
                    first_number = node.value;
211
13
                } else if (count == target + 1) {
212
3
                    second_number = node.value;
213
3
                    break;
214
3
                }
215
13
                ++count;
216
13
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
12
                    min_heap.push(node);
219
12
                }
220
13
            }
221
3
        }
222
223
5
        return {first_number, second_number};
224
5
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
19
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
19
        Ty first_number = 0, second_number = 0;
172
19
        size_t count = 0;
173
19
        if (reverse) {
174
4
            std::priority_queue<Node> max_heap;
175
18
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
14
                if (!_sorted_nums_vec[i].empty()) {
177
14
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
14
                                     _sorted_nums_vec[i].size() - 1);
179
14
                }
180
14
            }
181
182
26
            while (!max_heap.empty()) {
183
26
                Node node = max_heap.top();
184
26
                max_heap.pop();
185
26
                if (count == target) {
186
4
                    second_number = node.value;
187
22
                } else if (count == target + 1) {
188
4
                    first_number = node.value;
189
4
                    break;
190
4
                }
191
22
                ++count;
192
22
                if (--node.element_index >= 0) {
193
19
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
19
                    max_heap.push(node);
195
19
                }
196
22
            }
197
198
15
        } else {
199
15
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
73
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
58
                if (!_sorted_nums_vec[i].empty()) {
202
58
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
58
                }
204
58
            }
205
206
63
            while (!min_heap.empty()) {
207
63
                Node node = min_heap.top();
208
63
                min_heap.pop();
209
63
                if (count == target) {
210
15
                    first_number = node.value;
211
48
                } else if (count == target + 1) {
212
15
                    second_number = node.value;
213
15
                    break;
214
15
                }
215
48
                ++count;
216
48
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
42
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
42
                    min_heap.push(node);
219
42
                }
220
48
            }
221
15
        }
222
223
19
        return {first_number, second_number};
224
19
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
15
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
15
        Ty first_number = 0, second_number = 0;
172
15
        size_t count = 0;
173
15
        if (reverse) {
174
12
            std::priority_queue<Node> max_heap;
175
58
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
46
                if (!_sorted_nums_vec[i].empty()) {
177
46
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
46
                                     _sorted_nums_vec[i].size() - 1);
179
46
                }
180
46
            }
181
182
55
            while (!max_heap.empty()) {
183
55
                Node node = max_heap.top();
184
55
                max_heap.pop();
185
55
                if (count == target) {
186
12
                    second_number = node.value;
187
43
                } else if (count == target + 1) {
188
12
                    first_number = node.value;
189
12
                    break;
190
12
                }
191
43
                ++count;
192
43
                if (--node.element_index >= 0) {
193
39
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
39
                    max_heap.push(node);
195
39
                }
196
43
            }
197
198
12
        } else {
199
3
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
11
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
8
                if (!_sorted_nums_vec[i].empty()) {
202
8
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
8
                }
204
8
            }
205
206
13
            while (!min_heap.empty()) {
207
13
                Node node = min_heap.top();
208
13
                min_heap.pop();
209
13
                if (count == target) {
210
3
                    first_number = node.value;
211
10
                } else if (count == target + 1) {
212
3
                    second_number = node.value;
213
3
                    break;
214
3
                }
215
10
                ++count;
216
10
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
9
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
9
                    min_heap.push(node);
219
9
                }
220
10
            }
221
3
        }
222
223
15
        return {first_number, second_number};
224
15
    }
_ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
2
        Ty first_number = 0, second_number = 0;
172
2
        size_t count = 0;
173
2
        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
1
        } else {
199
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
2
                if (!_sorted_nums_vec[i].empty()) {
202
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
2
                }
204
2
            }
205
206
3
            while (!min_heap.empty()) {
207
3
                Node node = min_heap.top();
208
3
                min_heap.pop();
209
3
                if (count == target) {
210
1
                    first_number = node.value;
211
2
                } else if (count == target + 1) {
212
1
                    second_number = node.value;
213
1
                    break;
214
1
                }
215
2
                ++count;
216
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
1
                    min_heap.push(node);
219
1
                }
220
2
            }
221
1
        }
222
223
2
        return {first_number, second_number};
224
2
    }
_ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
2
        Ty first_number = 0, second_number = 0;
172
2
        size_t count = 0;
173
2
        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
1
        } else {
199
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
2
                if (!_sorted_nums_vec[i].empty()) {
202
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
2
                }
204
2
            }
205
206
3
            while (!min_heap.empty()) {
207
3
                Node node = min_heap.top();
208
3
                min_heap.pop();
209
3
                if (count == target) {
210
1
                    first_number = node.value;
211
2
                } else if (count == target + 1) {
212
1
                    second_number = node.value;
213
1
                    break;
214
1
                }
215
2
                ++count;
216
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
1
                    min_heap.push(node);
219
1
                }
220
2
            }
221
1
        }
222
223
2
        return {first_number, second_number};
224
2
    }
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
26
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
26
        Ty first_number = 0, second_number = 0;
172
26
        size_t count = 0;
173
26
        if (reverse) {
174
3
            std::priority_queue<Node> max_heap;
175
12
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
9
                if (!_sorted_nums_vec[i].empty()) {
177
9
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
9
                                     _sorted_nums_vec[i].size() - 1);
179
9
                }
180
9
            }
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
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
0
                    max_heap.push(node);
195
0
                }
196
4
            }
197
198
23
        } else {
199
23
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
84
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
61
                if (!_sorted_nums_vec[i].empty()) {
202
61
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
61
                }
204
61
            }
205
206
55
            while (!min_heap.empty()) {
207
55
                Node node = min_heap.top();
208
55
                min_heap.pop();
209
55
                if (count == target) {
210
23
                    first_number = node.value;
211
32
                } else if (count == target + 1) {
212
23
                    second_number = node.value;
213
23
                    break;
214
23
                }
215
32
                ++count;
216
32
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
6
                    min_heap.push(node);
219
6
                }
220
32
            }
221
23
        }
222
223
26
        return {first_number, second_number};
224
26
    }
225
226
    vectorized::PODArray<Ty> _nums;
227
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
228
};
229
230
} // namespace doris