Coverage Report

Created: 2026-04-04 07:17

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.62k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
45
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
148
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
2.22k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
898
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
41
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
11
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
266
    Counts() = default;
36
37
1.33k
    void merge(Counts* other) {
38
1.33k
        if (other != nullptr && !other->_nums.empty()) {
39
1.33k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
1.33k
        }
41
1.33k
    }
_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
7
    void merge(Counts* other) {
38
7
        if (other != nullptr && !other->_nums.empty()) {
39
7
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
7
        }
41
7
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
37
836
    void merge(Counts* other) {
38
836
        if (other != nullptr && !other->_nums.empty()) {
39
836
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
836
        }
41
836
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
394
    void merge(Counts* other) {
38
394
        if (other != nullptr && !other->_nums.empty()) {
39
394
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
394
        }
41
394
    }
_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
90
    void merge(Counts* other) {
38
90
        if (other != nullptr && !other->_nums.empty()) {
39
90
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
90
        }
41
90
    }
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.75k
    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
398
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
51
1.57k
    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.73k
    void serialize(BufferWritable& buf) {
56
1.73k
        if (!_nums.empty()) {
57
1.43k
            pdqsort(_nums.begin(), _nums.end());
58
1.43k
            size_t size = _nums.size();
59
1.43k
            buf.write_binary(size);
60
1.43k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
1.43k
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
305
            _convert_sorted_num_vec_to_nums();
64
305
            serialize(buf);
65
305
        }
66
1.73k
    }
_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
7
    void serialize(BufferWritable& buf) {
56
7
        if (!_nums.empty()) {
57
7
            pdqsort(_nums.begin(), _nums.end());
58
7
            size_t size = _nums.size();
59
7
            buf.write_binary(size);
60
7
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
7
        } 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
7
    }
_ZN5doris6CountsIiE9serializeERNS_14BufferWritableE
Line
Count
Source
55
1.10k
    void serialize(BufferWritable& buf) {
56
1.10k
        if (!_nums.empty()) {
57
953
            pdqsort(_nums.begin(), _nums.end());
58
953
            size_t size = _nums.size();
59
953
            buf.write_binary(size);
60
953
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
953
        } 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.10k
    }
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
55
518
    void serialize(BufferWritable& buf) {
56
518
        if (!_nums.empty()) {
57
369
            pdqsort(_nums.begin(), _nums.end());
58
369
            size_t size = _nums.size();
59
369
            buf.write_binary(size);
60
369
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
369
        } else {
62
            // convert _sorted_nums_vec to _nums and do seiralize again
63
149
            _convert_sorted_num_vec_to_nums();
64
149
            serialize(buf);
65
149
        }
66
518
    }
_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
90
    void serialize(BufferWritable& buf) {
56
90
        if (!_nums.empty()) {
57
90
            pdqsort(_nums.begin(), _nums.end());
58
90
            size_t size = _nums.size();
59
90
            buf.write_binary(size);
60
90
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
61
90
        } 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
90
    }
67
68
1.33k
    void unserialize(BufferReadable& buf) {
69
1.33k
        size_t size;
70
1.33k
        buf.read_binary(size);
71
1.33k
        _nums.resize(size);
72
1.33k
        auto buff = buf.read(sizeof(Ty) * size);
73
1.33k
        memcpy(_nums.data(), buff.data, buff.size);
74
1.33k
    }
_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
7
    void unserialize(BufferReadable& buf) {
69
7
        size_t size;
70
7
        buf.read_binary(size);
71
7
        _nums.resize(size);
72
7
        auto buff = buf.read(sizeof(Ty) * size);
73
7
        memcpy(_nums.data(), buff.data, buff.size);
74
7
    }
_ZN5doris6CountsIiE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
836
    void unserialize(BufferReadable& buf) {
69
836
        size_t size;
70
836
        buf.read_binary(size);
71
836
        _nums.resize(size);
72
836
        auto buff = buf.read(sizeof(Ty) * size);
73
836
        memcpy(_nums.data(), buff.data, buff.size);
74
836
    }
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
68
394
    void unserialize(BufferReadable& buf) {
69
394
        size_t size;
70
394
        buf.read_binary(size);
71
394
        _nums.resize(size);
72
394
        auto buff = buf.read(sizeof(Ty) * size);
73
394
        memcpy(_nums.data(), buff.data, buff.size);
74
394
    }
_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
90
    void unserialize(BufferReadable& buf) {
69
90
        size_t size;
70
90
        buf.read_binary(size);
71
90
        _nums.resize(size);
72
90
        auto buff = buf.read(sizeof(Ty) * size);
73
90
        memcpy(_nums.data(), buff.data, buff.size);
74
90
    }
75
76
1.00k
    double terminate(double quantile) {
77
1.00k
        if (_sorted_nums_vec.size() <= 1) {
78
852
            if (_sorted_nums_vec.size() == 1) {
79
302
                _nums = std::move(_sorted_nums_vec[0]);
80
302
            }
81
82
852
            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
852
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
132
                pdqsort(_nums.begin(), _nums.end());
90
132
            }
91
92
852
            if (quantile == 1 || _nums.size() == 1) {
93
440
                return _nums.back();
94
440
            }
95
96
412
            double u = (_nums.size() - 1) * quantile;
97
412
            auto index = static_cast<uint32_t>(u);
98
412
            return _nums[index] +
99
412
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
412
                                                       static_cast<double>(_nums[index]));
101
852
        } else {
102
150
            DCHECK(_nums.empty());
103
150
            size_t rows = 0;
104
420
            for (const auto& i : _sorted_nums_vec) {
105
420
                rows += i.size();
106
420
            }
107
150
            const bool reverse = quantile > 0.5 && rows > 2;
108
150
            double u = (rows - 1) * quantile;
109
150
            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
150
            size_t target = reverse ? rows - index - 2 : index;
118
150
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
150
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
150
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
150
            return first_number +
126
150
                   (u - static_cast<double>(index)) *
127
150
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
150
        }
129
1.00k
    }
_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
212
    double terminate(double quantile) {
77
212
        if (_sorted_nums_vec.size() <= 1) {
78
211
            if (_sorted_nums_vec.size() == 1) {
79
5
                _nums = std::move(_sorted_nums_vec[0]);
80
5
            }
81
82
211
            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
211
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
96
                pdqsort(_nums.begin(), _nums.end());
90
96
            }
91
92
211
            if (quantile == 1 || _nums.size() == 1) {
93
66
                return _nums.back();
94
66
            }
95
96
145
            double u = (_nums.size() - 1) * quantile;
97
145
            auto index = static_cast<uint32_t>(u);
98
145
            return _nums[index] +
99
145
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
145
                                                       static_cast<double>(_nums[index]));
101
211
        } 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
212
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
76
429
    double terminate(double quantile) {
77
429
        if (_sorted_nums_vec.size() <= 1) {
78
331
            if (_sorted_nums_vec.size() == 1) {
79
228
                _nums = std::move(_sorted_nums_vec[0]);
80
228
            }
81
82
331
            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
331
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
11
                pdqsort(_nums.begin(), _nums.end());
90
11
            }
91
92
331
            if (quantile == 1 || _nums.size() == 1) {
93
227
                return _nums.back();
94
227
            }
95
96
104
            double u = (_nums.size() - 1) * quantile;
97
104
            auto index = static_cast<uint32_t>(u);
98
104
            return _nums[index] +
99
104
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
104
                                                       static_cast<double>(_nums[index]));
101
331
        } else {
102
98
            DCHECK(_nums.empty());
103
98
            size_t rows = 0;
104
269
            for (const auto& i : _sorted_nums_vec) {
105
269
                rows += i.size();
106
269
            }
107
98
            const bool reverse = quantile > 0.5 && rows > 2;
108
98
            double u = (rows - 1) * quantile;
109
98
            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
98
            size_t target = reverse ? rows - index - 2 : index;
118
98
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
98
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
98
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
98
            return first_number +
126
98
                   (u - static_cast<double>(index)) *
127
98
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
98
        }
129
429
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
76
128
    double terminate(double quantile) {
77
128
        if (_sorted_nums_vec.size() <= 1) {
78
105
            if (_sorted_nums_vec.size() == 1) {
79
35
                _nums = std::move(_sorted_nums_vec[0]);
80
35
            }
81
82
105
            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
105
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
89
1
                pdqsort(_nums.begin(), _nums.end());
90
1
            }
91
92
105
            if (quantile == 1 || _nums.size() == 1) {
93
59
                return _nums.back();
94
59
            }
95
96
46
            double u = (_nums.size() - 1) * quantile;
97
46
            auto index = static_cast<uint32_t>(u);
98
46
            return _nums[index] +
99
46
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
100
46
                                                       static_cast<double>(_nums[index]));
101
105
        } else {
102
23
            DCHECK(_nums.empty());
103
23
            size_t rows = 0;
104
81
            for (const auto& i : _sorted_nums_vec) {
105
81
                rows += i.size();
106
81
            }
107
23
            const bool reverse = quantile > 0.5 && rows > 2;
108
23
            double u = (rows - 1) * quantile;
109
23
            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
23
            size_t target = reverse ? rows - index - 2 : index;
118
23
            if (quantile == 1) {
119
0
                target = 0;
120
0
            }
121
23
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
122
23
            if (quantile == 1) {
123
0
                return second_number;
124
0
            }
125
23
            return first_number +
126
23
                   (u - static_cast<double>(index)) *
127
23
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
128
23
        }
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
62
            for (const auto& i : _sorted_nums_vec) {
105
62
                rows += i.size();
106
62
            }
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.46k
        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
2
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
929
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
464
        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
59
        auto operator<=>(const Node& other) const { return value <=> other.value; }
138
    };
139
140
305
    void _convert_sorted_num_vec_to_nums() {
141
305
        size_t rows = 0;
142
617
        for (const auto& i : _sorted_nums_vec) {
143
617
            rows += i.size();
144
617
        }
145
305
        _nums.resize(rows);
146
305
        size_t count = 0;
147
148
305
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
922
        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
305
        _sorted_nums_vec.clear();
165
305
    }
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
149
    void _convert_sorted_num_vec_to_nums() {
141
149
        size_t rows = 0;
142
278
        for (const auto& i : _sorted_nums_vec) {
143
278
            rows += i.size();
144
278
        }
145
149
        _nums.resize(rows);
146
149
        size_t count = 0;
147
148
149
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
427
        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
503
        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
149
        _sorted_nums_vec.clear();
165
149
    }
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
150
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
150
        Ty first_number = 0, second_number = 0;
169
150
        size_t count = 0;
170
150
        if (reverse) {
171
27
            std::priority_queue<Node> max_heap;
172
124
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
97
                if (!_sorted_nums_vec[i].empty()) {
174
97
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
97
                                     _sorted_nums_vec[i].size() - 1);
176
97
                }
177
97
            }
178
179
107
            while (!max_heap.empty()) {
180
107
                Node node = max_heap.top();
181
107
                max_heap.pop();
182
107
                if (count == target) {
183
27
                    second_number = node.value;
184
80
                } else if (count == target + 1) {
185
27
                    first_number = node.value;
186
27
                    break;
187
27
                }
188
80
                ++count;
189
80
                if (--node.element_index >= 0) {
190
56
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
56
                    max_heap.push(node);
192
56
                }
193
80
            }
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
339
            while (!min_heap.empty()) {
204
339
                Node node = min_heap.top();
205
339
                min_heap.pop();
206
339
                if (count == target) {
207
123
                    first_number = node.value;
208
216
                } else if (count == target + 1) {
209
123
                    second_number = node.value;
210
123
                    break;
211
123
                }
212
216
                ++count;
213
216
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
126
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
126
                    min_heap.push(node);
216
126
                }
217
216
            }
218
123
        }
219
220
150
        return {first_number, second_number};
221
150
    }
_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
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
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
98
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
98
        Ty first_number = 0, second_number = 0;
169
98
        size_t count = 0;
170
98
        if (reverse) {
171
10
            std::priority_queue<Node> max_heap;
172
38
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
28
                if (!_sorted_nums_vec[i].empty()) {
174
28
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
28
                                     _sorted_nums_vec[i].size() - 1);
176
28
                }
177
28
            }
178
179
39
            while (!max_heap.empty()) {
180
39
                Node node = max_heap.top();
181
39
                max_heap.pop();
182
39
                if (count == target) {
183
10
                    second_number = node.value;
184
29
                } else if (count == target + 1) {
185
10
                    first_number = node.value;
186
10
                    break;
187
10
                }
188
29
                ++count;
189
29
                if (--node.element_index >= 0) {
190
22
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
22
                    max_heap.push(node);
192
22
                }
193
29
            }
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
98
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
98
                    min_heap.push(node);
216
98
                }
217
159
            }
218
88
        }
219
220
98
        return {first_number, second_number};
221
98
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
23
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
23
        Ty first_number = 0, second_number = 0;
169
23
        size_t count = 0;
170
23
        if (reverse) {
171
15
            std::priority_queue<Node> max_heap;
172
78
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
63
                if (!_sorted_nums_vec[i].empty()) {
174
63
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
63
                                     _sorted_nums_vec[i].size() - 1);
176
63
                }
177
63
            }
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
33
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
33
                    max_heap.push(node);
192
33
                }
193
48
            }
194
195
15
        } else {
196
8
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
26
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
18
                if (!_sorted_nums_vec[i].empty()) {
199
18
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
18
                }
201
18
            }
202
203
25
            while (!min_heap.empty()) {
204
25
                Node node = min_heap.top();
205
25
                min_heap.pop();
206
25
                if (count == target) {
207
8
                    first_number = node.value;
208
17
                } else if (count == target + 1) {
209
8
                    second_number = node.value;
210
8
                    break;
211
8
                }
212
17
                ++count;
213
17
                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
17
            }
218
8
        }
219
220
23
        return {first_number, second_number};
221
23
    }
_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
8
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
6
                if (!_sorted_nums_vec[i].empty()) {
174
6
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
6
                                     _sorted_nums_vec[i].size() - 1);
176
6
                }
177
6
            }
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
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
1
                    max_heap.push(node);
192
1
                }
193
3
            }
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
9
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
9
                    min_heap.push(node);
216
9
                }
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