Coverage Report

Created: 2026-03-26 10:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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 "core/pod_array.h"
27
#include "core/string_buffer.hpp"
28
#include "util/io_helper.h"
29
30
namespace doris {
31
32
template <typename Ty>
33
class Counts {
34
public:
35
3.65k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
47
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
205
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
2.23k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
841
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
43
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
13
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
270
    Counts() = default;
36
37
1.30k
    void merge(Counts* other) {
38
1.30k
        if (other != nullptr && !other->_nums.empty()) {
39
1.30k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
1.30k
        }
41
1.30k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
37
5
    void merge(Counts* other) {
38
5
        if (other != nullptr && !other->_nums.empty()) {
39
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
5
        }
41
5
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
37
11
    void merge(Counts* other) {
38
11
        if (other != nullptr && !other->_nums.empty()) {
39
11
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
11
        }
41
11
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
37
837
    void merge(Counts* other) {
38
837
        if (other != nullptr && !other->_nums.empty()) {
39
837
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
837
        }
41
837
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
353
    void merge(Counts* other) {
38
353
        if (other != nullptr && !other->_nums.empty()) {
39
353
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
353
        }
41
353
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
37
5
    void merge(Counts* other) {
38
5
        if (other != nullptr && !other->_nums.empty()) {
39
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
5
        }
41
5
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
37
5
    void merge(Counts* other) {
38
5
        if (other != nullptr && !other->_nums.empty()) {
39
5
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
5
        }
41
5
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
37
91
    void merge(Counts* other) {
38
91
        if (other != nullptr && !other->_nums.empty()) {
39
91
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
91
        }
41
91
    }
42
43
    void increment(Ty key, uint32_t i) {
44
        auto old_size = _nums.size();
45
        _nums.resize(_nums.size() + i);
46
        for (uint32_t j = 0; j < i; ++j) {
47
            _nums[old_size + j] = key;
48
        }
49
    }
50
51
2.93k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
51
73
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
51
552
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
51
1.59k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
51
401
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
51
45
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIfE9incrementEf
Line
Count
Source
51
9
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
51
259
    void increment(Ty key) { _nums.push_back(key); }
52
53
5
    void increment_batch(const PaddedPODArray<Ty>& keys) { _nums.insert(keys.begin(), keys.end()); }
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
_ZN5doris6CountsIlE15increment_batchERKNS_8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
53
5
    void increment_batch(const PaddedPODArray<Ty>& keys) { _nums.insert(keys.begin(), keys.end()); }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
54
55
1.71k
    void serialize(BufferWritable& buf) {
56
1.71k
        if (!_nums.empty()) {
57
1.42k
            pdqsort(_nums.begin(), _nums.end());
58
1.42k
            size_t size = _nums.size();
59
1.42k
            buf.write_binary(size);
60
1.42k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
1.42k
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
288
            _convert_sorted_num_vec_to_nums();
64
288
            serialize(buf);
65
288
        }
66
1.71k
    }
_ZN5doris6CountsIaE9serializeERNS_14BufferWritableE
Line
Count
Source
55
5
    void serialize(BufferWritable& buf) {
56
5
        if (!_nums.empty()) {
57
5
            pdqsort(_nums.begin(), _nums.end());
58
5
            size_t size = _nums.size();
59
5
            buf.write_binary(size);
60
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
5
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
5
    }
_ZN5doris6CountsIsE9serializeERNS_14BufferWritableE
Line
Count
Source
55
11
    void serialize(BufferWritable& buf) {
56
11
        if (!_nums.empty()) {
57
11
            pdqsort(_nums.begin(), _nums.end());
58
11
            size_t size = _nums.size();
59
11
            buf.write_binary(size);
60
11
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
11
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
11
    }
_ZN5doris6CountsIiE9serializeERNS_14BufferWritableE
Line
Count
Source
55
1.11k
    void serialize(BufferWritable& buf) {
56
1.11k
        if (!_nums.empty()) {
57
954
            pdqsort(_nums.begin(), _nums.end());
58
954
            size_t size = _nums.size();
59
954
            buf.write_binary(size);
60
954
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
954
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
156
            _convert_sorted_num_vec_to_nums();
64
156
            serialize(buf);
65
156
        }
66
1.11k
    }
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
55
485
    void serialize(BufferWritable& buf) {
56
485
        if (!_nums.empty()) {
57
353
            pdqsort(_nums.begin(), _nums.end());
58
353
            size_t size = _nums.size();
59
353
            buf.write_binary(size);
60
353
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
353
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
132
            _convert_sorted_num_vec_to_nums();
64
132
            serialize(buf);
65
132
        }
66
485
    }
_ZN5doris6CountsInE9serializeERNS_14BufferWritableE
Line
Count
Source
55
5
    void serialize(BufferWritable& buf) {
56
5
        if (!_nums.empty()) {
57
5
            pdqsort(_nums.begin(), _nums.end());
58
5
            size_t size = _nums.size();
59
5
            buf.write_binary(size);
60
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
5
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
5
    }
_ZN5doris6CountsIfE9serializeERNS_14BufferWritableE
Line
Count
Source
55
5
    void serialize(BufferWritable& buf) {
56
5
        if (!_nums.empty()) {
57
5
            pdqsort(_nums.begin(), _nums.end());
58
5
            size_t size = _nums.size();
59
5
            buf.write_binary(size);
60
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
5
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
5
    }
_ZN5doris6CountsIdE9serializeERNS_14BufferWritableE
Line
Count
Source
55
91
    void serialize(BufferWritable& buf) {
56
91
        if (!_nums.empty()) {
57
91
            pdqsort(_nums.begin(), _nums.end());
58
91
            size_t size = _nums.size();
59
91
            buf.write_binary(size);
60
91
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
91
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
0
            _convert_sorted_num_vec_to_nums();
64
0
            serialize(buf);
65
0
        }
66
91
    }
67
68
1.30k
    void unserialize(BufferReadable& buf) {
69
1.30k
        size_t size;
70
1.30k
        buf.read_binary(size);
71
1.30k
        _nums.resize(size);
72
1.30k
        auto buff = buf.read(sizeof(Ty) * size);
73
1.30k
        memcpy(_nums.data(), buff.data, buff.size);
74
1.30k
    }
_ZN5doris6CountsIaE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
5
    void unserialize(BufferReadable& buf) {
69
5
        size_t size;
70
5
        buf.read_binary(size);
71
5
        _nums.resize(size);
72
5
        auto buff = buf.read(sizeof(Ty) * size);
73
5
        memcpy(_nums.data(), buff.data, buff.size);
74
5
    }
_ZN5doris6CountsIsE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
11
    void unserialize(BufferReadable& buf) {
69
11
        size_t size;
70
11
        buf.read_binary(size);
71
11
        _nums.resize(size);
72
11
        auto buff = buf.read(sizeof(Ty) * size);
73
11
        memcpy(_nums.data(), buff.data, buff.size);
74
11
    }
_ZN5doris6CountsIiE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
837
    void unserialize(BufferReadable& buf) {
69
837
        size_t size;
70
837
        buf.read_binary(size);
71
837
        _nums.resize(size);
72
837
        auto buff = buf.read(sizeof(Ty) * size);
73
837
        memcpy(_nums.data(), buff.data, buff.size);
74
837
    }
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
353
    void unserialize(BufferReadable& buf) {
69
353
        size_t size;
70
353
        buf.read_binary(size);
71
353
        _nums.resize(size);
72
353
        auto buff = buf.read(sizeof(Ty) * size);
73
353
        memcpy(_nums.data(), buff.data, buff.size);
74
353
    }
_ZN5doris6CountsInE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
5
    void unserialize(BufferReadable& buf) {
69
5
        size_t size;
70
5
        buf.read_binary(size);
71
5
        _nums.resize(size);
72
5
        auto buff = buf.read(sizeof(Ty) * size);
73
5
        memcpy(_nums.data(), buff.data, buff.size);
74
5
    }
_ZN5doris6CountsIfE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
5
    void unserialize(BufferReadable& buf) {
69
5
        size_t size;
70
5
        buf.read_binary(size);
71
5
        _nums.resize(size);
72
5
        auto buff = buf.read(sizeof(Ty) * size);
73
5
        memcpy(_nums.data(), buff.data, buff.size);
74
5
    }
_ZN5doris6CountsIdE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
91
    void unserialize(BufferReadable& buf) {
69
91
        size_t size;
70
91
        buf.read_binary(size);
71
91
        _nums.resize(size);
72
91
        auto buff = buf.read(sizeof(Ty) * size);
73
91
        memcpy(_nums.data(), buff.data, buff.size);
74
91
    }
75
76
1.10k
    double terminate(double quantile) {
77
1.10k
        if (_sorted_nums_vec.size() <= 1) {
78
948
            if (_sorted_nums_vec.size() == 1) {
79
295
                _nums = std::move(_sorted_nums_vec[0]);
80
295
            }
81
82
948
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
948
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
180
                pdqsort(_nums.begin(), _nums.end());
90
180
            }
91
92
948
            if (quantile == 1 || _nums.size() == 1) {
93
473
                return _nums.back();
94
473
            }
95
96
475
            double u = (_nums.size() - 1) * quantile;
97
475
            auto index = static_cast<uint32_t>(u);
98
475
            return _nums[index] +
99
475
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
475
                                                       static_cast<double>(_nums[index]));
101
948
        } else {
102
157
            DCHECK(_nums.empty());
103
157
            size_t rows = 0;
104
426
            for (const auto& i : _sorted_nums_vec) {
105
426
                rows += i.size();
106
426
            }
107
157
            const bool reverse = quantile > 0.5 && rows > 2;
108
157
            double u = (rows - 1) * quantile;
109
157
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
157
            size_t target = reverse ? rows - index - 2 : index;
118
157
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
157
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
157
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
157
            return first_number +
126
157
                   (u - static_cast<double>(index)) *
127
157
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
157
        }
129
1.10k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
76
61
    double terminate(double quantile) {
77
61
        if (_sorted_nums_vec.size() <= 1) {
78
59
            if (_sorted_nums_vec.size() == 1) {
79
1
                _nums = std::move(_sorted_nums_vec[0]);
80
1
            }
81
82
59
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
59
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
24
                pdqsort(_nums.begin(), _nums.end());
90
24
            }
91
92
59
            if (quantile == 1 || _nums.size() == 1) {
93
34
                return _nums.back();
94
34
            }
95
96
25
            double u = (_nums.size() - 1) * quantile;
97
25
            auto index = static_cast<uint32_t>(u);
98
25
            return _nums[index] +
99
25
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
25
                                                       static_cast<double>(_nums[index]));
101
59
        } else {
102
2
            DCHECK(_nums.empty());
103
2
            size_t rows = 0;
104
4
            for (const auto& i : _sorted_nums_vec) {
105
4
                rows += i.size();
106
4
            }
107
2
            const bool reverse = quantile > 0.5 && rows > 2;
108
2
            double u = (rows - 1) * quantile;
109
2
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
2
            size_t target = reverse ? rows - index - 2 : index;
118
2
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
2
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
2
            return first_number +
126
2
                   (u - static_cast<double>(index)) *
127
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
2
        }
129
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
76
300
    double terminate(double quantile) {
77
300
        if (_sorted_nums_vec.size() <= 1) {
78
295
            if (_sorted_nums_vec.size() == 1) {
79
1
                _nums = std::move(_sorted_nums_vec[0]);
80
1
            }
81
82
295
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
295
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
144
                pdqsort(_nums.begin(), _nums.end());
90
144
            }
91
92
295
            if (quantile == 1 || _nums.size() == 1) {
93
87
                return _nums.back();
94
87
            }
95
96
208
            double u = (_nums.size() - 1) * quantile;
97
208
            auto index = static_cast<uint32_t>(u);
98
208
            return _nums[index] +
99
208
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
208
                                                       static_cast<double>(_nums[index]));
101
295
        } else {
102
5
            DCHECK(_nums.empty());
103
5
            size_t rows = 0;
104
10
            for (const auto& i : _sorted_nums_vec) {
105
10
                rows += i.size();
106
10
            }
107
5
            const bool reverse = quantile > 0.5 && rows > 2;
108
5
            double u = (rows - 1) * quantile;
109
5
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
5
            size_t target = reverse ? rows - index - 2 : index;
118
5
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
5
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
5
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
5
            return first_number +
126
5
                   (u - static_cast<double>(index)) *
127
5
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
5
        }
129
300
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
76
444
    double terminate(double quantile) {
77
444
        if (_sorted_nums_vec.size() <= 1) {
78
345
            if (_sorted_nums_vec.size() == 1) {
79
227
                _nums = std::move(_sorted_nums_vec[0]);
80
227
            }
81
82
345
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
345
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
11
                pdqsort(_nums.begin(), _nums.end());
90
11
            }
91
92
345
            if (quantile == 1 || _nums.size() == 1) {
93
239
                return _nums.back();
94
239
            }
95
96
106
            double u = (_nums.size() - 1) * quantile;
97
106
            auto index = static_cast<uint32_t>(u);
98
106
            return _nums[index] +
99
106
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
106
                                                       static_cast<double>(_nums[index]));
101
345
        } else {
102
99
            DCHECK(_nums.empty());
103
99
            size_t rows = 0;
104
271
            for (const auto& i : _sorted_nums_vec) {
105
271
                rows += i.size();
106
271
            }
107
99
            const bool reverse = quantile > 0.5 && rows > 2;
108
99
            double u = (rows - 1) * quantile;
109
99
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
99
            size_t target = reverse ? rows - index - 2 : index;
118
99
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
99
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
99
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
99
            return first_number +
126
99
                   (u - static_cast<double>(index)) *
127
99
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
99
        }
129
444
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
76
128
    double terminate(double quantile) {
77
128
        if (_sorted_nums_vec.size() <= 1) {
78
107
            if (_sorted_nums_vec.size() == 1) {
79
37
                _nums = std::move(_sorted_nums_vec[0]);
80
37
            }
81
82
107
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
107
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
1
                pdqsort(_nums.begin(), _nums.end());
90
1
            }
91
92
107
            if (quantile == 1 || _nums.size() == 1) {
93
59
                return _nums.back();
94
59
            }
95
96
48
            double u = (_nums.size() - 1) * quantile;
97
48
            auto index = static_cast<uint32_t>(u);
98
48
            return _nums[index] +
99
48
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
48
                                                       static_cast<double>(_nums[index]));
101
107
        } else {
102
21
            DCHECK(_nums.empty());
103
21
            size_t rows = 0;
104
69
            for (const auto& i : _sorted_nums_vec) {
105
69
                rows += i.size();
106
69
            }
107
21
            const bool reverse = quantile > 0.5 && rows > 2;
108
21
            double u = (rows - 1) * quantile;
109
21
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
21
            size_t target = reverse ? rows - index - 2 : index;
118
21
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
21
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
21
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
21
            return first_number +
126
21
                   (u - static_cast<double>(index)) *
127
21
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
21
        }
129
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
76
33
    double terminate(double quantile) {
77
33
        if (_sorted_nums_vec.size() <= 1) {
78
31
            if (_sorted_nums_vec.size() == 1) {
79
1
                _nums = std::move(_sorted_nums_vec[0]);
80
1
            }
81
82
31
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
31
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
31
            if (quantile == 1 || _nums.size() == 1) {
93
24
                return _nums.back();
94
24
            }
95
96
7
            double u = (_nums.size() - 1) * quantile;
97
7
            auto index = static_cast<uint32_t>(u);
98
7
            return _nums[index] +
99
7
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
7
                                                       static_cast<double>(_nums[index]));
101
31
        } else {
102
2
            DCHECK(_nums.empty());
103
2
            size_t rows = 0;
104
4
            for (const auto& i : _sorted_nums_vec) {
105
4
                rows += i.size();
106
4
            }
107
2
            const bool reverse = quantile > 0.5 && rows > 2;
108
2
            double u = (rows - 1) * quantile;
109
2
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
2
            size_t target = reverse ? rows - index - 2 : index;
118
2
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
2
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
2
            return first_number +
126
2
                   (u - static_cast<double>(index)) *
127
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
2
        }
129
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
76
3
    double terminate(double quantile) {
77
3
        if (_sorted_nums_vec.size() <= 1) {
78
1
            if (_sorted_nums_vec.size() == 1) {
79
1
                _nums = std::move(_sorted_nums_vec[0]);
80
1
            }
81
82
1
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
1
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
1
            if (quantile == 1 || _nums.size() == 1) {
93
0
                return _nums.back();
94
0
            }
95
96
1
            double u = (_nums.size() - 1) * quantile;
97
1
            auto index = static_cast<uint32_t>(u);
98
1
            return _nums[index] +
99
1
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
1
                                                       static_cast<double>(_nums[index]));
101
2
        } else {
102
2
            DCHECK(_nums.empty());
103
2
            size_t rows = 0;
104
4
            for (const auto& i : _sorted_nums_vec) {
105
4
                rows += i.size();
106
4
            }
107
2
            const bool reverse = quantile > 0.5 && rows > 2;
108
2
            double u = (rows - 1) * quantile;
109
2
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
2
            size_t target = reverse ? rows - index - 2 : index;
118
2
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
2
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
2
            return first_number +
126
2
                   (u - static_cast<double>(index)) *
127
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
2
        }
129
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
76
136
    double terminate(double quantile) {
77
136
        if (_sorted_nums_vec.size() <= 1) {
78
110
            if (_sorted_nums_vec.size() == 1) {
79
27
                _nums = std::move(_sorted_nums_vec[0]);
80
27
            }
81
82
110
            if (_nums.empty()) {
83
                // Although set null here, but the value is 0.0 and the call method just
84
                // get val in aggregate_function_percentile_approx.h
85
0
                return 0.0;
86
0
            }
87
88
110
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
110
            if (quantile == 1 || _nums.size() == 1) {
93
30
                return _nums.back();
94
30
            }
95
96
80
            double u = (_nums.size() - 1) * quantile;
97
80
            auto index = static_cast<uint32_t>(u);
98
80
            return _nums[index] +
99
80
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
80
                                                       static_cast<double>(_nums[index]));
101
110
        } else {
102
26
            DCHECK(_nums.empty());
103
26
            size_t rows = 0;
104
64
            for (const auto& i : _sorted_nums_vec) {
105
64
                rows += i.size();
106
64
            }
107
26
            const bool reverse = quantile > 0.5 && rows > 2;
108
26
            double u = (rows - 1) * quantile;
109
26
            auto index = static_cast<uint32_t>(u);
110
            // if reverse, the step of target should start 0 like not reverse
111
            // so here rows need to minus index + 2
112
            // eg: rows = 10, index = 5
113
            // if not reverse, so the first number loc is 5, the second number loc is 6
114
            // if reverse, so the second number is 3, the first number is 4
115
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
116
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
117
26
            size_t target = reverse ? rows - index - 2 : index;
118
26
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
26
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
26
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
26
            return first_number +
126
26
                   (u - static_cast<double>(index)) *
127
26
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
26
        }
129
136
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
1.48k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIaE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
137
23
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
942
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
440
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsInE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIfE4NodessERKS2_
Line
Count
Source
137
4
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
137
70
        auto operator<=>(const Node& other) const { return value <=> other.value; }
138
    };
139
140
288
    void _convert_sorted_num_vec_to_nums() {
141
288
        size_t rows = 0;
142
586
        for (const auto& i : _sorted_nums_vec) {
143
586
            rows += i.size();
144
586
        }
145
288
        _nums.resize(rows);
146
288
        size_t count = 0;
147
148
288
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
874
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
586
            if (!_sorted_nums_vec[i].empty()) {
151
586
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
586
            }
153
586
        }
154
155
1.02k
        while (!min_heap.empty()) {
156
737
            Node node = min_heap.top();
157
737
            min_heap.pop();
158
737
            _nums[count++] = node.value;
159
737
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
151
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
151
                min_heap.push(node);
162
151
            }
163
737
        }
164
288
        _sorted_nums_vec.clear();
165
288
    }
Unexecuted instantiation: _ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv
_ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
156
    void _convert_sorted_num_vec_to_nums() {
141
156
        size_t rows = 0;
142
339
        for (const auto& i : _sorted_nums_vec) {
143
339
            rows += i.size();
144
339
        }
145
156
        _nums.resize(rows);
146
156
        size_t count = 0;
147
148
156
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
495
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
339
            if (!_sorted_nums_vec[i].empty()) {
151
339
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
339
            }
153
339
        }
154
155
531
        while (!min_heap.empty()) {
156
375
            Node node = min_heap.top();
157
375
            min_heap.pop();
158
375
            _nums[count++] = node.value;
159
375
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
36
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
36
                min_heap.push(node);
162
36
            }
163
375
        }
164
156
        _sorted_nums_vec.clear();
165
156
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
132
    void _convert_sorted_num_vec_to_nums() {
141
132
        size_t rows = 0;
142
247
        for (const auto& i : _sorted_nums_vec) {
143
247
            rows += i.size();
144
247
        }
145
132
        _nums.resize(rows);
146
132
        size_t count = 0;
147
148
132
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
379
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
247
            if (!_sorted_nums_vec[i].empty()) {
151
247
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
247
            }
153
247
        }
154
155
494
        while (!min_heap.empty()) {
156
362
            Node node = min_heap.top();
157
362
            min_heap.pop();
158
362
            _nums[count++] = node.value;
159
362
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
115
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
115
                min_heap.push(node);
162
115
            }
163
362
        }
164
132
        _sorted_nums_vec.clear();
165
132
    }
Unexecuted instantiation: _ZN5doris6CountsInE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIfE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIdE31_convert_sorted_num_vec_to_numsEv
166
167
157
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
157
        Ty first_number = 0, second_number = 0;
169
157
        size_t count = 0;
170
157
        if (reverse) {
171
34
            std::priority_queue<Node> max_heap;
172
137
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
103
                if (!_sorted_nums_vec[i].empty()) {
174
103
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
103
                                     _sorted_nums_vec[i].size() - 1);
176
103
                }
177
103
            }
178
179
125
            while (!max_heap.empty()) {
180
125
                Node node = max_heap.top();
181
125
                max_heap.pop();
182
125
                if (count == target) {
183
34
                    second_number = node.value;
184
91
                } else if (count == target + 1) {
185
34
                    first_number = node.value;
186
34
                    break;
187
34
                }
188
91
                ++count;
189
91
                if (--node.element_index >= 0) {
190
73
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
73
                    max_heap.push(node);
192
73
                }
193
91
            }
194
195
123
        } else {
196
123
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
446
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
323
                if (!_sorted_nums_vec[i].empty()) {
199
323
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
323
                }
201
323
            }
202
203
346
            while (!min_heap.empty()) {
204
346
                Node node = min_heap.top();
205
346
                min_heap.pop();
206
346
                if (count == target) {
207
123
                    first_number = node.value;
208
223
                } else if (count == target + 1) {
209
123
                    second_number = node.value;
210
123
                    break;
211
123
                }
212
223
                ++count;
213
223
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
138
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
138
                    min_heap.push(node);
216
138
                }
217
223
            }
218
123
        }
219
220
157
        return {first_number, second_number};
221
157
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
5
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
5
        Ty first_number = 0, second_number = 0;
169
5
        size_t count = 0;
170
5
        if (reverse) {
171
2
            std::priority_queue<Node> max_heap;
172
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
4
                if (!_sorted_nums_vec[i].empty()) {
174
4
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
4
                                     _sorted_nums_vec[i].size() - 1);
176
4
                }
177
4
            }
178
179
8
            while (!max_heap.empty()) {
180
8
                Node node = max_heap.top();
181
8
                max_heap.pop();
182
8
                if (count == target) {
183
2
                    second_number = node.value;
184
6
                } else if (count == target + 1) {
185
2
                    first_number = node.value;
186
2
                    break;
187
2
                }
188
6
                ++count;
189
6
                if (--node.element_index >= 0) {
190
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
6
                    max_heap.push(node);
192
6
                }
193
6
            }
194
195
3
        } else {
196
3
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
9
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
6
                if (!_sorted_nums_vec[i].empty()) {
199
6
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
6
                }
201
6
            }
202
203
16
            while (!min_heap.empty()) {
204
16
                Node node = min_heap.top();
205
16
                min_heap.pop();
206
16
                if (count == target) {
207
3
                    first_number = node.value;
208
13
                } else if (count == target + 1) {
209
3
                    second_number = node.value;
210
3
                    break;
211
3
                }
212
13
                ++count;
213
13
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
12
                    min_heap.push(node);
216
12
                }
217
13
            }
218
3
        }
219
220
5
        return {first_number, second_number};
221
5
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
99
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
99
        Ty first_number = 0, second_number = 0;
169
99
        size_t count = 0;
170
99
        if (reverse) {
171
11
            std::priority_queue<Node> max_heap;
172
41
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
30
                if (!_sorted_nums_vec[i].empty()) {
174
30
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
30
                                     _sorted_nums_vec[i].size() - 1);
176
30
                }
177
30
            }
178
179
41
            while (!max_heap.empty()) {
180
41
                Node node = max_heap.top();
181
41
                max_heap.pop();
182
41
                if (count == target) {
183
11
                    second_number = node.value;
184
30
                } else if (count == target + 1) {
185
11
                    first_number = node.value;
186
11
                    break;
187
11
                }
188
30
                ++count;
189
30
                if (--node.element_index >= 0) {
190
23
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
23
                    max_heap.push(node);
192
23
                }
193
30
            }
194
195
88
        } else {
196
88
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
329
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
241
                if (!_sorted_nums_vec[i].empty()) {
199
241
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
241
                }
201
241
            }
202
203
247
            while (!min_heap.empty()) {
204
247
                Node node = min_heap.top();
205
247
                min_heap.pop();
206
247
                if (count == target) {
207
88
                    first_number = node.value;
208
159
                } else if (count == target + 1) {
209
88
                    second_number = node.value;
210
88
                    break;
211
88
                }
212
159
                ++count;
213
159
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
100
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
100
                    min_heap.push(node);
216
100
                }
217
159
            }
218
88
        }
219
220
99
        return {first_number, second_number};
221
99
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
21
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
21
        Ty first_number = 0, second_number = 0;
169
21
        size_t count = 0;
170
21
        if (reverse) {
171
15
            std::priority_queue<Node> max_heap;
172
70
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
55
                if (!_sorted_nums_vec[i].empty()) {
174
55
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
55
                                     _sorted_nums_vec[i].size() - 1);
176
55
                }
177
55
            }
178
179
63
            while (!max_heap.empty()) {
180
63
                Node node = max_heap.top();
181
63
                max_heap.pop();
182
63
                if (count == target) {
183
15
                    second_number = node.value;
184
48
                } else if (count == target + 1) {
185
15
                    first_number = node.value;
186
15
                    break;
187
15
                }
188
48
                ++count;
189
48
                if (--node.element_index >= 0) {
190
39
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
39
                    max_heap.push(node);
192
39
                }
193
48
            }
194
195
15
        } else {
196
6
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
20
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
14
                if (!_sorted_nums_vec[i].empty()) {
199
14
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
14
                }
201
14
            }
202
203
19
            while (!min_heap.empty()) {
204
19
                Node node = min_heap.top();
205
19
                min_heap.pop();
206
19
                if (count == target) {
207
6
                    first_number = node.value;
208
13
                } else if (count == target + 1) {
209
6
                    second_number = node.value;
210
6
                    break;
211
6
                }
212
13
                ++count;
213
13
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
11
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
11
                    min_heap.push(node);
216
11
                }
217
13
            }
218
6
        }
219
220
21
        return {first_number, second_number};
221
21
    }
_ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
2
        Ty first_number = 0, second_number = 0;
169
2
        size_t count = 0;
170
2
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
2
            while (!max_heap.empty()) {
180
2
                Node node = max_heap.top();
181
2
                max_heap.pop();
182
2
                if (count == target) {
183
1
                    second_number = node.value;
184
1
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
1
                ++count;
189
1
                if (--node.element_index >= 0) {
190
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
1
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
3
            while (!min_heap.empty()) {
204
3
                Node node = min_heap.top();
205
3
                min_heap.pop();
206
3
                if (count == target) {
207
1
                    first_number = node.value;
208
2
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
2
                ++count;
213
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
1
                    min_heap.push(node);
216
1
                }
217
2
            }
218
1
        }
219
220
2
        return {first_number, second_number};
221
2
    }
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
26
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
26
        Ty first_number = 0, second_number = 0;
169
26
        size_t count = 0;
170
26
        if (reverse) {
171
3
            std::priority_queue<Node> max_heap;
172
11
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
8
                if (!_sorted_nums_vec[i].empty()) {
174
8
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
8
                                     _sorted_nums_vec[i].size() - 1);
176
8
                }
177
8
            }
178
179
7
            while (!max_heap.empty()) {
180
7
                Node node = max_heap.top();
181
7
                max_heap.pop();
182
7
                if (count == target) {
183
3
                    second_number = node.value;
184
4
                } else if (count == target + 1) {
185
3
                    first_number = node.value;
186
3
                    break;
187
3
                }
188
4
                ++count;
189
4
                if (--node.element_index >= 0) {
190
2
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
2
                    max_heap.push(node);
192
2
                }
193
4
            }
194
195
23
        } else {
196
23
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
79
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
56
                if (!_sorted_nums_vec[i].empty()) {
199
56
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
56
                }
201
56
            }
202
203
55
            while (!min_heap.empty()) {
204
55
                Node node = min_heap.top();
205
55
                min_heap.pop();
206
55
                if (count == target) {
207
23
                    first_number = node.value;
208
32
                } else if (count == target + 1) {
209
23
                    second_number = node.value;
210
23
                    break;
211
23
                }
212
32
                ++count;
213
32
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
12
                    min_heap.push(node);
216
12
                }
217
32
            }
218
23
        }
219
220
26
        return {first_number, second_number};
221
26
    }
222
223
    PODArray<Ty> _nums;
224
    std::vector<PODArray<Ty>> _sorted_nums_vec;
225
};
226
227
} // namespace doris