Coverage Report

Created: 2025-11-17 10:57

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.61k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
47
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
211
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
2.23k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
819
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
43
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
36
13
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
246
    Counts() = default;
37
38
1.28k
    void merge(Counts* other) {
39
1.28k
        if (other != nullptr && !other->_nums.empty()) {
40
1.28k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.28k
        }
42
1.28k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
38
5
    void merge(Counts* other) {
39
5
        if (other != nullptr && !other->_nums.empty()) {
40
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
5
        }
42
5
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
38
14
    void merge(Counts* other) {
39
14
        if (other != nullptr && !other->_nums.empty()) {
40
14
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
14
        }
42
14
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
837
    void merge(Counts* other) {
39
837
        if (other != nullptr && !other->_nums.empty()) {
40
837
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
837
        }
42
837
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
342
    void merge(Counts* other) {
39
342
        if (other != nullptr && !other->_nums.empty()) {
40
342
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
342
        }
42
342
    }
_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
79
    void merge(Counts* other) {
39
79
        if (other != nullptr && !other->_nums.empty()) {
40
79
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
79
        }
42
79
    }
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.69k
    void serialize(vectorized::BufferWritable& buf) {
59
1.69k
        if (!_nums.empty()) {
60
1.40k
            pdqsort(_nums.begin(), _nums.end());
61
1.40k
            size_t size = _nums.size();
62
1.40k
            buf.write_binary(size);
63
1.40k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.40k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
289
            _convert_sorted_num_vec_to_nums();
67
289
            serialize(buf);
68
289
        }
69
1.69k
    }
_ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
5
    void serialize(vectorized::BufferWritable& buf) {
59
5
        if (!_nums.empty()) {
60
5
            pdqsort(_nums.begin(), _nums.end());
61
5
            size_t size = _nums.size();
62
5
            buf.write_binary(size);
63
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
5
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
5
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
14
    void serialize(vectorized::BufferWritable& buf) {
59
14
        if (!_nums.empty()) {
60
14
            pdqsort(_nums.begin(), _nums.end());
61
14
            size_t size = _nums.size();
62
14
            buf.write_binary(size);
63
14
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
14
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
14
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
1.11k
    void serialize(vectorized::BufferWritable& buf) {
59
1.11k
        if (!_nums.empty()) {
60
954
            pdqsort(_nums.begin(), _nums.end());
61
954
            size_t size = _nums.size();
62
954
            buf.write_binary(size);
63
954
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
954
        } 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
1.11k
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
475
    void serialize(vectorized::BufferWritable& buf) {
59
475
        if (!_nums.empty()) {
60
342
            pdqsort(_nums.begin(), _nums.end());
61
342
            size_t size = _nums.size();
62
342
            buf.write_binary(size);
63
342
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
342
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
133
            _convert_sorted_num_vec_to_nums();
67
133
            serialize(buf);
68
133
        }
69
475
    }
_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
79
    void serialize(vectorized::BufferWritable& buf) {
59
79
        if (!_nums.empty()) {
60
79
            pdqsort(_nums.begin(), _nums.end());
61
79
            size_t size = _nums.size();
62
79
            buf.write_binary(size);
63
79
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
79
        } 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
79
    }
70
71
1.28k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.28k
        size_t size;
73
1.28k
        buf.read_binary(size);
74
1.28k
        _nums.resize(size);
75
1.28k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.28k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.28k
    }
_ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
5
    void unserialize(vectorized::BufferReadable& buf) {
72
5
        size_t size;
73
5
        buf.read_binary(size);
74
5
        _nums.resize(size);
75
5
        auto buff = buf.read(sizeof(Ty) * size);
76
5
        memcpy(_nums.data(), buff.data, buff.size);
77
5
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
14
    void unserialize(vectorized::BufferReadable& buf) {
72
14
        size_t size;
73
14
        buf.read_binary(size);
74
14
        _nums.resize(size);
75
14
        auto buff = buf.read(sizeof(Ty) * size);
76
14
        memcpy(_nums.data(), buff.data, buff.size);
77
14
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
837
    void unserialize(vectorized::BufferReadable& buf) {
72
837
        size_t size;
73
837
        buf.read_binary(size);
74
837
        _nums.resize(size);
75
837
        auto buff = buf.read(sizeof(Ty) * size);
76
837
        memcpy(_nums.data(), buff.data, buff.size);
77
837
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
342
    void unserialize(vectorized::BufferReadable& buf) {
72
342
        size_t size;
73
342
        buf.read_binary(size);
74
342
        _nums.resize(size);
75
342
        auto buff = buf.read(sizeof(Ty) * size);
76
342
        memcpy(_nums.data(), buff.data, buff.size);
77
342
    }
_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
79
    void unserialize(vectorized::BufferReadable& buf) {
72
79
        size_t size;
73
79
        buf.read_binary(size);
74
79
        _nums.resize(size);
75
79
        auto buff = buf.read(sizeof(Ty) * size);
76
79
        memcpy(_nums.data(), buff.data, buff.size);
77
79
    }
78
79
1.10k
    double terminate(double quantile) {
80
1.10k
        if (_sorted_nums_vec.size() <= 1) {
81
961
            if (_sorted_nums_vec.size() == 1) {
82
308
                _nums = std::move(_sorted_nums_vec[0]);
83
308
            }
84
85
961
            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
961
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
180
                pdqsort(_nums.begin(), _nums.end());
93
180
            }
94
95
961
            if (quantile == 1 || _nums.size() == 1) {
96
473
                return _nums.back();
97
473
            }
98
99
488
            double u = (_nums.size() - 1) * quantile;
100
488
            auto index = static_cast<uint32_t>(u);
101
488
            return _nums[index] +
102
488
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
488
                                                       static_cast<double>(_nums[index]));
104
961
        } else {
105
144
            DCHECK(_nums.empty());
106
144
            size_t rows = 0;
107
393
            for (const auto& i : _sorted_nums_vec) {
108
393
                rows += i.size();
109
393
            }
110
144
            const bool reverse = quantile > 0.5 && rows > 2;
111
144
            double u = (rows - 1) * quantile;
112
144
            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
144
            size_t target = reverse ? rows - index - 2 : index;
121
144
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
144
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
144
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
144
            return first_number +
129
144
                   (u - static_cast<double>(index)) *
130
144
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
144
        }
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
13
            for (const auto& i : _sorted_nums_vec) {
108
13
                rows += i.size();
109
13
            }
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
345
            if (_sorted_nums_vec.size() == 1) {
82
227
                _nums = std::move(_sorted_nums_vec[0]);
83
227
            }
84
85
345
            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
345
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
11
                pdqsort(_nums.begin(), _nums.end());
93
11
            }
94
95
345
            if (quantile == 1 || _nums.size() == 1) {
96
239
                return _nums.back();
97
239
            }
98
99
106
            double u = (_nums.size() - 1) * quantile;
100
106
            auto index = static_cast<uint32_t>(u);
101
106
            return _nums[index] +
102
106
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
106
                                                       static_cast<double>(_nums[index]));
104
345
        } else {
105
99
            DCHECK(_nums.empty());
106
99
            size_t rows = 0;
107
271
            for (const auto& i : _sorted_nums_vec) {
108
271
                rows += i.size();
109
271
            }
110
99
            const bool reverse = quantile > 0.5 && rows > 2;
111
99
            double u = (rows - 1) * quantile;
112
99
            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
99
            size_t target = reverse ? rows - index - 2 : index;
121
99
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
99
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
99
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
99
            return first_number +
129
99
                   (u - static_cast<double>(index)) *
130
99
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
99
        }
132
444
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
128
    double terminate(double quantile) {
80
128
        if (_sorted_nums_vec.size() <= 1) {
81
108
            if (_sorted_nums_vec.size() == 1) {
82
38
                _nums = std::move(_sorted_nums_vec[0]);
83
38
            }
84
85
108
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
108
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
108
            if (quantile == 1 || _nums.size() == 1) {
96
59
                return _nums.back();
97
59
            }
98
99
49
            double u = (_nums.size() - 1) * quantile;
100
49
            auto index = static_cast<uint32_t>(u);
101
49
            return _nums[index] +
102
49
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
49
                                                       static_cast<double>(_nums[index]));
104
108
        } else {
105
20
            DCHECK(_nums.empty());
106
20
            size_t rows = 0;
107
57
            for (const auto& i : _sorted_nums_vec) {
108
57
                rows += i.size();
109
57
            }
110
20
            const bool reverse = quantile > 0.5 && rows > 2;
111
20
            double u = (rows - 1) * quantile;
112
20
            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
20
            size_t target = reverse ? rows - index - 2 : index;
121
20
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
20
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
20
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
20
            return first_number +
129
20
                   (u - static_cast<double>(index)) *
130
20
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
20
        }
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
122
            if (_sorted_nums_vec.size() == 1) {
82
39
                _nums = std::move(_sorted_nums_vec[0]);
83
39
            }
84
85
122
            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
122
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
122
            if (quantile == 1 || _nums.size() == 1) {
96
30
                return _nums.back();
97
30
            }
98
99
92
            double u = (_nums.size() - 1) * quantile;
100
92
            auto index = static_cast<uint32_t>(u);
101
92
            return _nums[index] +
102
92
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
92
                                                       static_cast<double>(_nums[index]));
104
122
        } else {
105
14
            DCHECK(_nums.empty());
106
14
            size_t rows = 0;
107
40
            for (const auto& i : _sorted_nums_vec) {
108
40
                rows += i.size();
109
40
            }
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
136
    }
133
134
private:
135
    struct Node {
136
        Ty value;
137
        int array_index;
138
        int64_t element_index;
139
140
1.41k
        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
43
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
140
934
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
140
370
        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
57
        auto operator<=>(const Node& other) const { return value <=> other.value; }
141
    };
142
143
289
    void _convert_sorted_num_vec_to_nums() {
144
289
        size_t rows = 0;
145
586
        for (const auto& i : _sorted_nums_vec) {
146
586
            rows += i.size();
147
586
        }
148
289
        _nums.resize(rows);
149
289
        size_t count = 0;
150
151
289
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
874
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
586
            if (!_sorted_nums_vec[i].empty()) {
154
586
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
586
            }
156
585
        }
157
158
1.02k
        while (!min_heap.empty()) {
159
737
            Node node = min_heap.top();
160
737
            min_heap.pop();
161
737
            _nums[count++] = node.value;
162
737
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
151
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
151
                min_heap.push(node);
165
151
            }
166
737
        }
167
289
        _sorted_nums_vec.clear();
168
289
    }
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
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
338
        }
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
133
    void _convert_sorted_num_vec_to_nums() {
144
133
        size_t rows = 0;
145
247
        for (const auto& i : _sorted_nums_vec) {
146
247
            rows += i.size();
147
247
        }
148
133
        _nums.resize(rows);
149
133
        size_t count = 0;
150
151
133
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
380
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
247
            if (!_sorted_nums_vec[i].empty()) {
154
247
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
247
            }
156
247
        }
157
158
495
        while (!min_heap.empty()) {
159
362
            Node node = min_heap.top();
160
362
            min_heap.pop();
161
362
            _nums[count++] = node.value;
162
362
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
115
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
115
                min_heap.push(node);
165
115
            }
166
362
        }
167
133
        _sorted_nums_vec.clear();
168
133
    }
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
144
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
144
        Ty first_number = 0, second_number = 0;
172
144
        size_t count = 0;
173
144
        if (reverse) {
174
31
            std::priority_queue<Node> max_heap;
175
119
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
88
                if (!_sorted_nums_vec[i].empty()) {
177
88
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
88
                                     _sorted_nums_vec[i].size() - 1);
179
88
                }
180
88
            }
181
182
113
            while (!max_heap.empty()) {
183
113
                Node node = max_heap.top();
184
113
                max_heap.pop();
185
113
                if (count == target) {
186
31
                    second_number = node.value;
187
82
                } else if (count == target + 1) {
188
31
                    first_number = node.value;
189
31
                    break;
190
31
                }
191
82
                ++count;
192
82
                if (--node.element_index >= 0) {
193
66
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
66
                    max_heap.push(node);
195
66
                }
196
82
            }
197
198
113
        } else {
199
113
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
418
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
305
                if (!_sorted_nums_vec[i].empty()) {
202
305
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
305
                }
204
305
            }
205
206
328
            while (!min_heap.empty()) {
207
328
                Node node = min_heap.top();
208
328
                min_heap.pop();
209
328
                if (count == target) {
210
113
                    first_number = node.value;
211
215
                } else if (count == target + 1) {
212
113
                    second_number = node.value;
213
113
                    break;
214
113
                }
215
215
                ++count;
216
215
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
136
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
136
                    min_heap.push(node);
219
136
                }
220
215
            }
221
113
        }
222
223
144
        return {first_number, second_number};
224
144
    }
_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
7
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
5
                if (!_sorted_nums_vec[i].empty()) {
177
5
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
5
                                     _sorted_nums_vec[i].size() - 1);
179
5
                }
180
5
            }
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
5
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
5
                    max_heap.push(node);
195
5
                }
196
6
            }
197
198
3
        } 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
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
99
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
99
        Ty first_number = 0, second_number = 0;
172
99
        size_t count = 0;
173
99
        if (reverse) {
174
11
            std::priority_queue<Node> max_heap;
175
41
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
30
                if (!_sorted_nums_vec[i].empty()) {
177
30
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
30
                                     _sorted_nums_vec[i].size() - 1);
179
30
                }
180
30
            }
181
182
41
            while (!max_heap.empty()) {
183
41
                Node node = max_heap.top();
184
41
                max_heap.pop();
185
41
                if (count == target) {
186
11
                    second_number = node.value;
187
30
                } else if (count == target + 1) {
188
11
                    first_number = node.value;
189
11
                    break;
190
11
                }
191
30
                ++count;
192
30
                if (--node.element_index >= 0) {
193
23
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
23
                    max_heap.push(node);
195
23
                }
196
30
            }
197
198
88
        } else {
199
88
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
329
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
241
                if (!_sorted_nums_vec[i].empty()) {
202
241
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
241
                }
204
241
            }
205
206
247
            while (!min_heap.empty()) {
207
247
                Node node = min_heap.top();
208
247
                min_heap.pop();
209
247
                if (count == target) {
210
88
                    first_number = node.value;
211
159
                } else if (count == target + 1) {
212
88
                    second_number = node.value;
213
88
                    break;
214
88
                }
215
159
                ++count;
216
159
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
98
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
98
                    min_heap.push(node);
219
98
                }
220
159
            }
221
88
        }
222
223
99
        return {first_number, second_number};
224
99
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
20
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
20
        Ty first_number = 0, second_number = 0;
172
20
        size_t count = 0;
173
20
        if (reverse) {
174
12
            std::priority_queue<Node> max_heap;
175
51
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
39
                if (!_sorted_nums_vec[i].empty()) {
177
39
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
39
                                     _sorted_nums_vec[i].size() - 1);
179
39
                }
180
39
            }
181
182
51
            while (!max_heap.empty()) {
183
51
                Node node = max_heap.top();
184
51
                max_heap.pop();
185
51
                if (count == target) {
186
12
                    second_number = node.value;
187
39
                } else if (count == target + 1) {
188
12
                    first_number = node.value;
189
12
                    break;
190
12
                }
191
39
                ++count;
192
39
                if (--node.element_index >= 0) {
193
33
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
33
                    max_heap.push(node);
195
33
                }
196
39
            }
197
198
12
        } else {
199
8
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
26
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
18
                if (!_sorted_nums_vec[i].empty()) {
202
18
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
18
                }
204
18
            }
205
206
25
            while (!min_heap.empty()) {
207
25
                Node node = min_heap.top();
208
25
                min_heap.pop();
209
25
                if (count == target) {
210
8
                    first_number = node.value;
211
17
                } else if (count == target + 1) {
212
8
                    second_number = node.value;
213
8
                    break;
214
8
                }
215
17
                ++count;
216
17
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
15
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
15
                    min_heap.push(node);
219
15
                }
220
17
            }
221
8
        }
222
223
20
        return {first_number, second_number};
224
20
    }
_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
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
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
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
2
                    max_heap.push(node);
195
2
                }
196
4
            }
197
198
11
        } else {
199
11
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
43
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
32
                if (!_sorted_nums_vec[i].empty()) {
202
32
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
32
                }
204
32
            }
205
206
31
            while (!min_heap.empty()) {
207
31
                Node node = min_heap.top();
208
31
                min_heap.pop();
209
31
                if (count == target) {
210
11
                    first_number = node.value;
211
20
                } else if (count == target + 1) {
212
11
                    second_number = node.value;
213
11
                    break;
214
11
                }
215
20
                ++count;
216
20
                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
20
            }
221
11
        }
222
223
14
        return {first_number, second_number};
224
14
    }
225
226
    vectorized::PODArray<Ty> _nums;
227
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
228
};
229
230
} // namespace doris