Coverage Report

Created: 2026-03-31 14:24

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.44k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
45
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
215
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
2.00k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
876
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
41
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
11
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
250
    Counts() = default;
36
37
1.21k
    void merge(Counts* other) {
38
1.21k
        if (other != nullptr && !other->_nums.empty()) {
39
1.21k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
1.21k
        }
41
1.21k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
37
4
    void merge(Counts* other) {
38
4
        if (other != nullptr && !other->_nums.empty()) {
39
4
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
4
        }
41
4
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
37
16
    void merge(Counts* other) {
38
16
        if (other != nullptr && !other->_nums.empty()) {
39
16
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
16
        }
41
16
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
37
720
    void merge(Counts* other) {
38
720
        if (other != nullptr && !other->_nums.empty()) {
39
720
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
720
        }
41
720
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
383
    void merge(Counts* other) {
38
383
        if (other != nullptr && !other->_nums.empty()) {
39
383
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
383
        }
41
383
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
37
4
    void merge(Counts* other) {
38
4
        if (other != nullptr && !other->_nums.empty()) {
39
4
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
4
        }
41
4
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
37
4
    void merge(Counts* other) {
38
4
        if (other != nullptr && !other->_nums.empty()) {
39
4
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
4
        }
41
4
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
37
84
    void merge(Counts* other) {
38
84
        if (other != nullptr && !other->_nums.empty()) {
39
84
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
84
        }
41
84
    }
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.60k
    void serialize(BufferWritable& buf) {
56
1.60k
        if (!_nums.empty()) {
57
1.30k
            pdqsort(_nums.begin(), _nums.end());
58
1.30k
            size_t size = _nums.size();
59
1.30k
            buf.write_binary(size);
60
1.30k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
1.30k
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
302
            _convert_sorted_num_vec_to_nums();
64
302
            serialize(buf);
65
302
        }
66
1.60k
    }
_ZN5doris6CountsIaE9serializeERNS_14BufferWritableE
Line
Count
Source
55
4
    void serialize(BufferWritable& buf) {
56
4
        if (!_nums.empty()) {
57
4
            pdqsort(_nums.begin(), _nums.end());
58
4
            size_t size = _nums.size();
59
4
            buf.write_binary(size);
60
4
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
4
        } 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
4
    }
_ZN5doris6CountsIsE9serializeERNS_14BufferWritableE
Line
Count
Source
55
16
    void serialize(BufferWritable& buf) {
56
16
        if (!_nums.empty()) {
57
16
            pdqsort(_nums.begin(), _nums.end());
58
16
            size_t size = _nums.size();
59
16
            buf.write_binary(size);
60
16
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
16
        } 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
16
    }
_ZN5doris6CountsIiE9serializeERNS_14BufferWritableE
Line
Count
Source
55
993
    void serialize(BufferWritable& buf) {
56
993
        if (!_nums.empty()) {
57
837
            pdqsort(_nums.begin(), _nums.end());
58
837
            size_t size = _nums.size();
59
837
            buf.write_binary(size);
60
837
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
837
        } 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
993
    }
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
55
504
    void serialize(BufferWritable& buf) {
56
504
        if (!_nums.empty()) {
57
358
            pdqsort(_nums.begin(), _nums.end());
58
358
            size_t size = _nums.size();
59
358
            buf.write_binary(size);
60
358
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
358
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
146
            _convert_sorted_num_vec_to_nums();
64
146
            serialize(buf);
65
146
        }
66
504
    }
_ZN5doris6CountsInE9serializeERNS_14BufferWritableE
Line
Count
Source
55
4
    void serialize(BufferWritable& buf) {
56
4
        if (!_nums.empty()) {
57
4
            pdqsort(_nums.begin(), _nums.end());
58
4
            size_t size = _nums.size();
59
4
            buf.write_binary(size);
60
4
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
4
        } 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
4
    }
_ZN5doris6CountsIfE9serializeERNS_14BufferWritableE
Line
Count
Source
55
4
    void serialize(BufferWritable& buf) {
56
4
        if (!_nums.empty()) {
57
4
            pdqsort(_nums.begin(), _nums.end());
58
4
            size_t size = _nums.size();
59
4
            buf.write_binary(size);
60
4
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
4
        } 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
4
    }
_ZN5doris6CountsIdE9serializeERNS_14BufferWritableE
Line
Count
Source
55
84
    void serialize(BufferWritable& buf) {
56
84
        if (!_nums.empty()) {
57
84
            pdqsort(_nums.begin(), _nums.end());
58
84
            size_t size = _nums.size();
59
84
            buf.write_binary(size);
60
84
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
84
        } 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
84
    }
67
68
1.21k
    void unserialize(BufferReadable& buf) {
69
1.21k
        size_t size;
70
1.21k
        buf.read_binary(size);
71
1.21k
        _nums.resize(size);
72
1.21k
        auto buff = buf.read(sizeof(Ty) * size);
73
1.21k
        memcpy(_nums.data(), buff.data, buff.size);
74
1.21k
    }
_ZN5doris6CountsIaE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
4
    void unserialize(BufferReadable& buf) {
69
4
        size_t size;
70
4
        buf.read_binary(size);
71
4
        _nums.resize(size);
72
4
        auto buff = buf.read(sizeof(Ty) * size);
73
4
        memcpy(_nums.data(), buff.data, buff.size);
74
4
    }
_ZN5doris6CountsIsE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
16
    void unserialize(BufferReadable& buf) {
69
16
        size_t size;
70
16
        buf.read_binary(size);
71
16
        _nums.resize(size);
72
16
        auto buff = buf.read(sizeof(Ty) * size);
73
16
        memcpy(_nums.data(), buff.data, buff.size);
74
16
    }
_ZN5doris6CountsIiE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
720
    void unserialize(BufferReadable& buf) {
69
720
        size_t size;
70
720
        buf.read_binary(size);
71
720
        _nums.resize(size);
72
720
        auto buff = buf.read(sizeof(Ty) * size);
73
720
        memcpy(_nums.data(), buff.data, buff.size);
74
720
    }
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
383
    void unserialize(BufferReadable& buf) {
69
383
        size_t size;
70
383
        buf.read_binary(size);
71
383
        _nums.resize(size);
72
383
        auto buff = buf.read(sizeof(Ty) * size);
73
383
        memcpy(_nums.data(), buff.data, buff.size);
74
383
    }
_ZN5doris6CountsInE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
4
    void unserialize(BufferReadable& buf) {
69
4
        size_t size;
70
4
        buf.read_binary(size);
71
4
        _nums.resize(size);
72
4
        auto buff = buf.read(sizeof(Ty) * size);
73
4
        memcpy(_nums.data(), buff.data, buff.size);
74
4
    }
_ZN5doris6CountsIfE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
4
    void unserialize(BufferReadable& buf) {
69
4
        size_t size;
70
4
        buf.read_binary(size);
71
4
        _nums.resize(size);
72
4
        auto buff = buf.read(sizeof(Ty) * size);
73
4
        memcpy(_nums.data(), buff.data, buff.size);
74
4
    }
_ZN5doris6CountsIdE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
84
    void unserialize(BufferReadable& buf) {
69
84
        size_t size;
70
84
        buf.read_binary(size);
71
84
        _nums.resize(size);
72
84
        auto buff = buf.read(sizeof(Ty) * size);
73
84
        memcpy(_nums.data(), buff.data, buff.size);
74
84
    }
75
76
1.10k
    double terminate(double quantile) {
77
1.10k
        if (_sorted_nums_vec.size() <= 1) {
78
1.00k
            if (_sorted_nums_vec.size() == 1) {
79
349
                _nums = std::move(_sorted_nums_vec[0]);
80
349
            }
81
82
1.00k
            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.00k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
180
                pdqsort(_nums.begin(), _nums.end());
90
180
            }
91
92
1.00k
            if (quantile == 1 || _nums.size() == 1) {
93
473
                return _nums.back();
94
473
            }
95
96
529
            double u = (_nums.size() - 1) * quantile;
97
529
            auto index = static_cast<uint32_t>(u);
98
529
            return _nums[index] +
99
529
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
529
                                                       static_cast<double>(_nums[index]));
101
1.00k
        } else {
102
103
            DCHECK(_nums.empty());
103
103
            size_t rows = 0;
104
249
            for (const auto& i : _sorted_nums_vec) {
105
249
                rows += i.size();
106
249
            }
107
103
            const bool reverse = quantile > 0.5 && rows > 2;
108
103
            double u = (rows - 1) * quantile;
109
103
            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
103
            size_t target = reverse ? rows - index - 2 : index;
118
103
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
103
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
103
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
103
            return first_number +
126
103
                   (u - static_cast<double>(index)) *
127
103
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
103
        }
129
1.10k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
76
61
    double terminate(double quantile) {
77
61
        if (_sorted_nums_vec.size() <= 1) {
78
60
            if (_sorted_nums_vec.size() == 1) {
79
2
                _nums = std::move(_sorted_nums_vec[0]);
80
2
            }
81
82
60
            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
60
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
24
                pdqsort(_nums.begin(), _nums.end());
90
24
            }
91
92
60
            if (quantile == 1 || _nums.size() == 1) {
93
34
                return _nums.back();
94
34
            }
95
96
26
            double u = (_nums.size() - 1) * quantile;
97
26
            auto index = static_cast<uint32_t>(u);
98
26
            return _nums[index] +
99
26
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
26
                                                       static_cast<double>(_nums[index]));
101
60
        } else {
102
1
            DCHECK(_nums.empty());
103
1
            size_t rows = 0;
104
2
            for (const auto& i : _sorted_nums_vec) {
105
2
                rows += i.size();
106
2
            }
107
1
            const bool reverse = quantile > 0.5 && rows > 2;
108
1
            double u = (rows - 1) * quantile;
109
1
            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
1
            size_t target = reverse ? rows - index - 2 : index;
118
1
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
1
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
1
            return first_number +
126
1
                   (u - static_cast<double>(index)) *
127
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
1
        }
129
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
76
300
    double terminate(double quantile) {
77
300
        if (_sorted_nums_vec.size() <= 1) {
78
296
            if (_sorted_nums_vec.size() == 1) {
79
2
                _nums = std::move(_sorted_nums_vec[0]);
80
2
            }
81
82
296
            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
296
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
144
                pdqsort(_nums.begin(), _nums.end());
90
144
            }
91
92
296
            if (quantile == 1 || _nums.size() == 1) {
93
87
                return _nums.back();
94
87
            }
95
96
209
            double u = (_nums.size() - 1) * quantile;
97
209
            auto index = static_cast<uint32_t>(u);
98
209
            return _nums[index] +
99
209
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
209
                                                       static_cast<double>(_nums[index]));
101
296
        } else {
102
4
            DCHECK(_nums.empty());
103
4
            size_t rows = 0;
104
14
            for (const auto& i : _sorted_nums_vec) {
105
14
                rows += i.size();
106
14
            }
107
4
            const bool reverse = quantile > 0.5 && rows > 2;
108
4
            double u = (rows - 1) * quantile;
109
4
            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
4
            size_t target = reverse ? rows - index - 2 : index;
118
4
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
4
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
4
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
4
            return first_number +
126
4
                   (u - static_cast<double>(index)) *
127
4
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
4
        }
129
300
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
76
444
    double terminate(double quantile) {
77
444
        if (_sorted_nums_vec.size() <= 1) {
78
389
            if (_sorted_nums_vec.size() == 1) {
79
271
                _nums = std::move(_sorted_nums_vec[0]);
80
271
            }
81
82
389
            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
389
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
11
                pdqsort(_nums.begin(), _nums.end());
90
11
            }
91
92
389
            if (quantile == 1 || _nums.size() == 1) {
93
239
                return _nums.back();
94
239
            }
95
96
150
            double u = (_nums.size() - 1) * quantile;
97
150
            auto index = static_cast<uint32_t>(u);
98
150
            return _nums[index] +
99
150
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
150
                                                       static_cast<double>(_nums[index]));
101
389
        } else {
102
55
            DCHECK(_nums.empty());
103
55
            size_t rows = 0;
104
110
            for (const auto& i : _sorted_nums_vec) {
105
110
                rows += i.size();
106
110
            }
107
55
            const bool reverse = quantile > 0.5 && rows > 2;
108
55
            double u = (rows - 1) * quantile;
109
55
            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
55
            size_t target = reverse ? rows - index - 2 : index;
118
55
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
55
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
55
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
55
            return first_number +
126
55
                   (u - static_cast<double>(index)) *
127
55
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
55
        }
129
444
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
76
128
    double terminate(double quantile) {
77
128
        if (_sorted_nums_vec.size() <= 1) {
78
112
            if (_sorted_nums_vec.size() == 1) {
79
42
                _nums = std::move(_sorted_nums_vec[0]);
80
42
            }
81
82
112
            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
112
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
1
                pdqsort(_nums.begin(), _nums.end());
90
1
            }
91
92
112
            if (quantile == 1 || _nums.size() == 1) {
93
59
                return _nums.back();
94
59
            }
95
96
53
            double u = (_nums.size() - 1) * quantile;
97
53
            auto index = static_cast<uint32_t>(u);
98
53
            return _nums[index] +
99
53
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
53
                                                       static_cast<double>(_nums[index]));
101
112
        } else {
102
16
            DCHECK(_nums.empty());
103
16
            size_t rows = 0;
104
63
            for (const auto& i : _sorted_nums_vec) {
105
63
                rows += i.size();
106
63
            }
107
16
            const bool reverse = quantile > 0.5 && rows > 2;
108
16
            double u = (rows - 1) * quantile;
109
16
            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
16
            size_t target = reverse ? rows - index - 2 : index;
118
16
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
16
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
16
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
16
            return first_number +
126
16
                   (u - static_cast<double>(index)) *
127
16
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
16
        }
129
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
76
33
    double terminate(double quantile) {
77
33
        if (_sorted_nums_vec.size() <= 1) {
78
32
            if (_sorted_nums_vec.size() == 1) {
79
2
                _nums = std::move(_sorted_nums_vec[0]);
80
2
            }
81
82
32
            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
32
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
32
            if (quantile == 1 || _nums.size() == 1) {
93
24
                return _nums.back();
94
24
            }
95
96
8
            double u = (_nums.size() - 1) * quantile;
97
8
            auto index = static_cast<uint32_t>(u);
98
8
            return _nums[index] +
99
8
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
8
                                                       static_cast<double>(_nums[index]));
101
32
        } else {
102
1
            DCHECK(_nums.empty());
103
1
            size_t rows = 0;
104
2
            for (const auto& i : _sorted_nums_vec) {
105
2
                rows += i.size();
106
2
            }
107
1
            const bool reverse = quantile > 0.5 && rows > 2;
108
1
            double u = (rows - 1) * quantile;
109
1
            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
1
            size_t target = reverse ? rows - index - 2 : index;
118
1
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
1
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
1
            return first_number +
126
1
                   (u - static_cast<double>(index)) *
127
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
1
        }
129
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
76
3
    double terminate(double quantile) {
77
3
        if (_sorted_nums_vec.size() <= 1) {
78
2
            if (_sorted_nums_vec.size() == 1) {
79
2
                _nums = std::move(_sorted_nums_vec[0]);
80
2
            }
81
82
2
            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
2
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
2
            if (quantile == 1 || _nums.size() == 1) {
93
0
                return _nums.back();
94
0
            }
95
96
2
            double u = (_nums.size() - 1) * quantile;
97
2
            auto index = static_cast<uint32_t>(u);
98
2
            return _nums[index] +
99
2
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
2
                                                       static_cast<double>(_nums[index]));
101
2
        } else {
102
1
            DCHECK(_nums.empty());
103
1
            size_t rows = 0;
104
2
            for (const auto& i : _sorted_nums_vec) {
105
2
                rows += i.size();
106
2
            }
107
1
            const bool reverse = quantile > 0.5 && rows > 2;
108
1
            double u = (rows - 1) * quantile;
109
1
            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
1
            size_t target = reverse ? rows - index - 2 : index;
118
1
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
1
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
1
            return first_number +
126
1
                   (u - static_cast<double>(index)) *
127
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
1
        }
129
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
76
136
    double terminate(double quantile) {
77
136
        if (_sorted_nums_vec.size() <= 1) {
78
111
            if (_sorted_nums_vec.size() == 1) {
79
28
                _nums = std::move(_sorted_nums_vec[0]);
80
28
            }
81
82
111
            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
111
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
0
                pdqsort(_nums.begin(), _nums.end());
90
0
            }
91
92
111
            if (quantile == 1 || _nums.size() == 1) {
93
30
                return _nums.back();
94
30
            }
95
96
81
            double u = (_nums.size() - 1) * quantile;
97
81
            auto index = static_cast<uint32_t>(u);
98
81
            return _nums[index] +
99
81
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
81
                                                       static_cast<double>(_nums[index]));
101
111
        } else {
102
25
            DCHECK(_nums.empty());
103
25
            size_t rows = 0;
104
56
            for (const auto& i : _sorted_nums_vec) {
105
56
                rows += i.size();
106
56
            }
107
25
            const bool reverse = quantile > 0.5 && rows > 2;
108
25
            double u = (rows - 1) * quantile;
109
25
            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
25
            size_t target = reverse ? rows - index - 2 : index;
118
25
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
25
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
25
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
25
            return first_number +
126
25
                   (u - static_cast<double>(index)) *
127
25
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
25
        }
129
136
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
1.06k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIaE4NodessERKS2_
Line
Count
Source
137
2
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
137
68
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
483
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
448
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsInE4NodessERKS2_
Line
Count
Source
137
2
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIfE4NodessERKS2_
Line
Count
Source
137
2
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
137
58
        auto operator<=>(const Node& other) const { return value <=> other.value; }
138
    };
139
140
302
    void _convert_sorted_num_vec_to_nums() {
141
302
        size_t rows = 0;
142
617
        for (const auto& i : _sorted_nums_vec) {
143
617
            rows += i.size();
144
617
        }
145
302
        _nums.resize(rows);
146
302
        size_t count = 0;
147
148
302
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
919
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
617
            if (!_sorted_nums_vec[i].empty()) {
151
617
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
617
            }
153
617
        }
154
155
1.03k
        while (!min_heap.empty()) {
156
729
            Node node = min_heap.top();
157
729
            min_heap.pop();
158
729
            _nums[count++] = node.value;
159
729
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
112
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
112
                min_heap.push(node);
162
112
            }
163
729
        }
164
302
        _sorted_nums_vec.clear();
165
302
    }
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
146
    void _convert_sorted_num_vec_to_nums() {
141
146
        size_t rows = 0;
142
278
        for (const auto& i : _sorted_nums_vec) {
143
278
            rows += i.size();
144
278
        }
145
146
        _nums.resize(rows);
146
146
        size_t count = 0;
147
148
146
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
424
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
278
            if (!_sorted_nums_vec[i].empty()) {
151
278
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
278
            }
153
278
        }
154
155
500
        while (!min_heap.empty()) {
156
354
            Node node = min_heap.top();
157
354
            min_heap.pop();
158
354
            _nums[count++] = node.value;
159
354
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
76
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
76
                min_heap.push(node);
162
76
            }
163
354
        }
164
146
        _sorted_nums_vec.clear();
165
146
    }
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
103
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
103
        Ty first_number = 0, second_number = 0;
169
103
        size_t count = 0;
170
103
        if (reverse) {
171
23
            std::priority_queue<Node> max_heap;
172
103
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
80
                if (!_sorted_nums_vec[i].empty()) {
174
80
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
80
                                     _sorted_nums_vec[i].size() - 1);
176
80
                }
177
80
            }
178
179
102
            while (!max_heap.empty()) {
180
102
                Node node = max_heap.top();
181
102
                max_heap.pop();
182
102
                if (count == target) {
183
23
                    second_number = node.value;
184
79
                } else if (count == target + 1) {
185
23
                    first_number = node.value;
186
23
                    break;
187
23
                }
188
79
                ++count;
189
79
                if (--node.element_index >= 0) {
190
58
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
58
                    max_heap.push(node);
192
58
                }
193
79
            }
194
195
80
        } else {
196
80
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
249
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
169
                if (!_sorted_nums_vec[i].empty()) {
199
169
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
169
                }
201
169
            }
202
203
258
            while (!min_heap.empty()) {
204
258
                Node node = min_heap.top();
205
258
                min_heap.pop();
206
258
                if (count == target) {
207
80
                    first_number = node.value;
208
178
                } else if (count == target + 1) {
209
80
                    second_number = node.value;
210
80
                    break;
211
80
                }
212
178
                ++count;
213
178
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
154
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
154
                    min_heap.push(node);
216
154
                }
217
178
            }
218
80
        }
219
220
103
        return {first_number, second_number};
221
103
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
1
        Ty first_number = 0, second_number = 0;
169
1
        size_t count = 0;
170
1
        if (reverse) {
171
0
            std::priority_queue<Node> max_heap;
172
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
0
                if (!_sorted_nums_vec[i].empty()) {
174
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
0
                                     _sorted_nums_vec[i].size() - 1);
176
0
                }
177
0
            }
178
179
0
            while (!max_heap.empty()) {
180
0
                Node node = max_heap.top();
181
0
                max_heap.pop();
182
0
                if (count == target) {
183
0
                    second_number = node.value;
184
0
                } else if (count == target + 1) {
185
0
                    first_number = node.value;
186
0
                    break;
187
0
                }
188
0
                ++count;
189
0
                if (--node.element_index >= 0) {
190
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
0
                    max_heap.push(node);
192
0
                }
193
0
            }
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
1
        return {first_number, second_number};
221
1
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
4
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
4
        Ty first_number = 0, second_number = 0;
169
4
        size_t count = 0;
170
4
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
5
            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
6
            while (!max_heap.empty()) {
180
6
                Node node = max_heap.top();
181
6
                max_heap.pop();
182
6
                if (count == target) {
183
1
                    second_number = node.value;
184
5
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
5
                ++count;
189
5
                if (--node.element_index >= 0) {
190
3
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
3
                    max_heap.push(node);
192
3
                }
193
5
            }
194
195
3
        } else {
196
3
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
13
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
10
                if (!_sorted_nums_vec[i].empty()) {
199
10
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
10
                }
201
10
            }
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
4
        return {first_number, second_number};
221
4
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
55
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
55
        Ty first_number = 0, second_number = 0;
169
55
        size_t count = 0;
170
55
        if (reverse) {
171
8
            std::priority_queue<Node> max_heap;
172
24
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
16
                if (!_sorted_nums_vec[i].empty()) {
174
16
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
16
                                     _sorted_nums_vec[i].size() - 1);
176
16
                }
177
16
            }
178
179
35
            while (!max_heap.empty()) {
180
35
                Node node = max_heap.top();
181
35
                max_heap.pop();
182
35
                if (count == target) {
183
8
                    second_number = node.value;
184
27
                } else if (count == target + 1) {
185
8
                    first_number = node.value;
186
8
                    break;
187
8
                }
188
27
                ++count;
189
27
                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
27
            }
194
195
47
        } else {
196
47
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
141
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
94
                if (!_sorted_nums_vec[i].empty()) {
199
94
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
94
                }
201
94
            }
202
203
163
            while (!min_heap.empty()) {
204
163
                Node node = min_heap.top();
205
163
                min_heap.pop();
206
163
                if (count == target) {
207
47
                    first_number = node.value;
208
116
                } else if (count == target + 1) {
209
47
                    second_number = node.value;
210
47
                    break;
211
47
                }
212
116
                ++count;
213
116
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
114
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
114
                    min_heap.push(node);
216
114
                }
217
116
            }
218
47
        }
219
220
55
        return {first_number, second_number};
221
55
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
16
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
16
        Ty first_number = 0, second_number = 0;
169
16
        size_t count = 0;
170
16
        if (reverse) {
171
12
            std::priority_queue<Node> max_heap;
172
67
            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
56
            while (!max_heap.empty()) {
180
56
                Node node = max_heap.top();
181
56
                max_heap.pop();
182
56
                if (count == target) {
183
12
                    second_number = node.value;
184
44
                } else if (count == target + 1) {
185
12
                    first_number = node.value;
186
12
                    break;
187
12
                }
188
44
                ++count;
189
44
                if (--node.element_index >= 0) {
190
30
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
30
                    max_heap.push(node);
192
30
                }
193
44
            }
194
195
12
        } else {
196
4
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
12
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
8
                if (!_sorted_nums_vec[i].empty()) {
199
8
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
8
                }
201
8
            }
202
203
15
            while (!min_heap.empty()) {
204
15
                Node node = min_heap.top();
205
15
                min_heap.pop();
206
15
                if (count == target) {
207
4
                    first_number = node.value;
208
11
                } else if (count == target + 1) {
209
4
                    second_number = node.value;
210
4
                    break;
211
4
                }
212
11
                ++count;
213
11
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
10
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
10
                    min_heap.push(node);
216
10
                }
217
11
            }
218
4
        }
219
220
16
        return {first_number, second_number};
221
16
    }
_ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
1
        Ty first_number = 0, second_number = 0;
169
1
        size_t count = 0;
170
1
        if (reverse) {
171
0
            std::priority_queue<Node> max_heap;
172
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
0
                if (!_sorted_nums_vec[i].empty()) {
174
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
0
                                     _sorted_nums_vec[i].size() - 1);
176
0
                }
177
0
            }
178
179
0
            while (!max_heap.empty()) {
180
0
                Node node = max_heap.top();
181
0
                max_heap.pop();
182
0
                if (count == target) {
183
0
                    second_number = node.value;
184
0
                } else if (count == target + 1) {
185
0
                    first_number = node.value;
186
0
                    break;
187
0
                }
188
0
                ++count;
189
0
                if (--node.element_index >= 0) {
190
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
0
                    max_heap.push(node);
192
0
                }
193
0
            }
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
1
        return {first_number, second_number};
221
1
    }
_ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
1
        Ty first_number = 0, second_number = 0;
169
1
        size_t count = 0;
170
1
        if (reverse) {
171
0
            std::priority_queue<Node> max_heap;
172
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
0
                if (!_sorted_nums_vec[i].empty()) {
174
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
0
                                     _sorted_nums_vec[i].size() - 1);
176
0
                }
177
0
            }
178
179
0
            while (!max_heap.empty()) {
180
0
                Node node = max_heap.top();
181
0
                max_heap.pop();
182
0
                if (count == target) {
183
0
                    second_number = node.value;
184
0
                } else if (count == target + 1) {
185
0
                    first_number = node.value;
186
0
                    break;
187
0
                }
188
0
                ++count;
189
0
                if (--node.element_index >= 0) {
190
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
0
                    max_heap.push(node);
192
0
                }
193
0
            }
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
1
        return {first_number, second_number};
221
1
    }
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
25
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
25
        Ty first_number = 0, second_number = 0;
169
25
        size_t count = 0;
170
25
        if (reverse) {
171
2
            std::priority_queue<Node> max_heap;
172
7
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
5
                if (!_sorted_nums_vec[i].empty()) {
174
5
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
5
                                     _sorted_nums_vec[i].size() - 1);
176
5
                }
177
5
            }
178
179
5
            while (!max_heap.empty()) {
180
5
                Node node = max_heap.top();
181
5
                max_heap.pop();
182
5
                if (count == target) {
183
2
                    second_number = node.value;
184
3
                } else if (count == target + 1) {
185
2
                    first_number = node.value;
186
2
                    break;
187
2
                }
188
3
                ++count;
189
3
                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
3
            }
194
195
23
        } else {
196
23
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
74
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
51
                if (!_sorted_nums_vec[i].empty()) {
199
51
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
51
                }
201
51
            }
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
15
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
15
                    min_heap.push(node);
216
15
                }
217
32
            }
218
23
        }
219
220
25
        return {first_number, second_number};
221
25
    }
222
223
    PODArray<Ty> _nums;
224
    std::vector<PODArray<Ty>> _sorted_nums_vec;
225
};
226
227
} // namespace doris