Coverage Report

Created: 2026-01-08 14:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/counts.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <pdqsort.h>
21
22
#include <algorithm>
23
#include <cmath>
24
#include <queue>
25
26
#include "udf/udf.h"
27
#include "vec/common/pod_array.h"
28
#include "vec/common/string_buffer.hpp"
29
#include "vec/io/io_helper.h"
30
31
namespace doris {
32
33
template <typename Ty>
34
class Counts {
35
public:
36
3.53k
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
45
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
215
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
2.23k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
726
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
41
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
36
11
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
264
    Counts() = default;
37
38
1.24k
    void merge(Counts* other) {
39
1.24k
        if (other != nullptr && !other->_nums.empty()) {
40
1.24k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.24k
        }
42
1.24k
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
38
4
    void merge(Counts* other) {
39
4
        if (other != nullptr && !other->_nums.empty()) {
40
4
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
4
        }
42
4
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
38
16
    void merge(Counts* other) {
39
16
        if (other != nullptr && !other->_nums.empty()) {
40
16
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
16
        }
42
16
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
836
    void merge(Counts* other) {
39
836
        if (other != nullptr && !other->_nums.empty()) {
40
836
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
836
        }
42
836
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
288
    void merge(Counts* other) {
39
288
        if (other != nullptr && !other->_nums.empty()) {
40
288
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
288
        }
42
288
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
38
4
    void merge(Counts* other) {
39
4
        if (other != nullptr && !other->_nums.empty()) {
40
4
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
4
        }
42
4
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
38
4
    void merge(Counts* other) {
39
4
        if (other != nullptr && !other->_nums.empty()) {
40
4
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
4
        }
42
4
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
90
    void merge(Counts* other) {
39
90
        if (other != nullptr && !other->_nums.empty()) {
40
90
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
90
        }
42
90
    }
43
44
    void increment(Ty key, uint32_t i) {
45
        auto old_size = _nums.size();
46
        _nums.resize(_nums.size() + i);
47
        for (uint32_t j = 0; j < i; ++j) {
48
            _nums[old_size + j] = key;
49
        }
50
    }
51
52
2.93k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
52
73
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
52
552
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
52
1.59k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
52
401
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
52
45
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIfE9incrementEf
Line
Count
Source
52
9
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
52
259
    void increment(Ty key) { _nums.push_back(key); }
53
54
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
5
        _nums.insert(keys.begin(), keys.end());
56
5
    }
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_10vectorized8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
_ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Line
Count
Source
54
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
5
        _nums.insert(keys.begin(), keys.end());
56
5
    }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
57
58
1.62k
    void serialize(vectorized::BufferWritable& buf) {
59
1.62k
        if (!_nums.empty()) {
60
1.37k
            pdqsort(_nums.begin(), _nums.end());
61
1.37k
            size_t size = _nums.size();
62
1.37k
            buf.write_binary(size);
63
1.37k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.37k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
251
            _convert_sorted_num_vec_to_nums();
67
251
            serialize(buf);
68
251
        }
69
1.62k
    }
_ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
4
    void serialize(vectorized::BufferWritable& buf) {
59
4
        if (!_nums.empty()) {
60
4
            pdqsort(_nums.begin(), _nums.end());
61
4
            size_t size = _nums.size();
62
4
            buf.write_binary(size);
63
4
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
4
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
4
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
16
    void serialize(vectorized::BufferWritable& buf) {
59
16
        if (!_nums.empty()) {
60
16
            pdqsort(_nums.begin(), _nums.end());
61
16
            size_t size = _nums.size();
62
16
            buf.write_binary(size);
63
16
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
16
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
16
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
1.10k
    void serialize(vectorized::BufferWritable& buf) {
59
1.10k
        if (!_nums.empty()) {
60
953
            pdqsort(_nums.begin(), _nums.end());
61
953
            size_t size = _nums.size();
62
953
            buf.write_binary(size);
63
953
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
953
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
156
            _convert_sorted_num_vec_to_nums();
67
156
            serialize(buf);
68
156
        }
69
1.10k
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
398
    void serialize(vectorized::BufferWritable& buf) {
59
398
        if (!_nums.empty()) {
60
303
            pdqsort(_nums.begin(), _nums.end());
61
303
            size_t size = _nums.size();
62
303
            buf.write_binary(size);
63
303
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
303
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
95
            _convert_sorted_num_vec_to_nums();
67
95
            serialize(buf);
68
95
        }
69
398
    }
_ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
4
    void serialize(vectorized::BufferWritable& buf) {
59
4
        if (!_nums.empty()) {
60
4
            pdqsort(_nums.begin(), _nums.end());
61
4
            size_t size = _nums.size();
62
4
            buf.write_binary(size);
63
4
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
4
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
4
    }
_ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
4
    void serialize(vectorized::BufferWritable& buf) {
59
4
        if (!_nums.empty()) {
60
4
            pdqsort(_nums.begin(), _nums.end());
61
4
            size_t size = _nums.size();
62
4
            buf.write_binary(size);
63
4
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
4
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
4
    }
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
90
    void serialize(vectorized::BufferWritable& buf) {
59
90
        if (!_nums.empty()) {
60
90
            pdqsort(_nums.begin(), _nums.end());
61
90
            size_t size = _nums.size();
62
90
            buf.write_binary(size);
63
90
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
90
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
90
    }
70
71
1.24k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.24k
        size_t size;
73
1.24k
        buf.read_binary(size);
74
1.24k
        _nums.resize(size);
75
1.24k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.24k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.24k
    }
_ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
4
    void unserialize(vectorized::BufferReadable& buf) {
72
4
        size_t size;
73
4
        buf.read_binary(size);
74
4
        _nums.resize(size);
75
4
        auto buff = buf.read(sizeof(Ty) * size);
76
4
        memcpy(_nums.data(), buff.data, buff.size);
77
4
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
16
    void unserialize(vectorized::BufferReadable& buf) {
72
16
        size_t size;
73
16
        buf.read_binary(size);
74
16
        _nums.resize(size);
75
16
        auto buff = buf.read(sizeof(Ty) * size);
76
16
        memcpy(_nums.data(), buff.data, buff.size);
77
16
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
836
    void unserialize(vectorized::BufferReadable& buf) {
72
836
        size_t size;
73
836
        buf.read_binary(size);
74
836
        _nums.resize(size);
75
836
        auto buff = buf.read(sizeof(Ty) * size);
76
836
        memcpy(_nums.data(), buff.data, buff.size);
77
836
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
288
    void unserialize(vectorized::BufferReadable& buf) {
72
288
        size_t size;
73
288
        buf.read_binary(size);
74
288
        _nums.resize(size);
75
288
        auto buff = buf.read(sizeof(Ty) * size);
76
288
        memcpy(_nums.data(), buff.data, buff.size);
77
288
    }
_ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
4
    void unserialize(vectorized::BufferReadable& buf) {
72
4
        size_t size;
73
4
        buf.read_binary(size);
74
4
        _nums.resize(size);
75
4
        auto buff = buf.read(sizeof(Ty) * size);
76
4
        memcpy(_nums.data(), buff.data, buff.size);
77
4
    }
_ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
4
    void unserialize(vectorized::BufferReadable& buf) {
72
4
        size_t size;
73
4
        buf.read_binary(size);
74
4
        _nums.resize(size);
75
4
        auto buff = buf.read(sizeof(Ty) * size);
76
4
        memcpy(_nums.data(), buff.data, buff.size);
77
4
    }
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
90
    void unserialize(vectorized::BufferReadable& buf) {
72
90
        size_t size;
73
90
        buf.read_binary(size);
74
90
        _nums.resize(size);
75
90
        auto buff = buf.read(sizeof(Ty) * size);
76
90
        memcpy(_nums.data(), buff.data, buff.size);
77
90
    }
78
79
1.10k
    double terminate(double quantile) {
80
1.10k
        if (_sorted_nums_vec.size() <= 1) {
81
952
            if (_sorted_nums_vec.size() == 1) {
82
299
                _nums = std::move(_sorted_nums_vec[0]);
83
299
            }
84
85
952
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
952
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
180
                pdqsort(_nums.begin(), _nums.end());
93
180
            }
94
95
952
            if (quantile == 1 || _nums.size() == 1) {
96
473
                return _nums.back();
97
473
            }
98
99
479
            double u = (_nums.size() - 1) * quantile;
100
479
            auto index = static_cast<uint32_t>(u);
101
479
            return _nums[index] +
102
479
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
479
                                                       static_cast<double>(_nums[index]));
104
952
        } else {
105
153
            DCHECK(_nums.empty());
106
153
            size_t rows = 0;
107
401
            for (const auto& i : _sorted_nums_vec) {
108
401
                rows += i.size();
109
401
            }
110
153
            const bool reverse = quantile > 0.5 && rows > 2;
111
153
            double u = (rows - 1) * quantile;
112
153
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
153
            size_t target = reverse ? rows - index - 2 : index;
121
153
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
153
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
153
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
153
            return first_number +
129
153
                   (u - static_cast<double>(index)) *
130
153
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
153
        }
132
1.10k
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
79
61
    double terminate(double quantile) {
80
61
        if (_sorted_nums_vec.size() <= 1) {
81
60
            if (_sorted_nums_vec.size() == 1) {
82
2
                _nums = std::move(_sorted_nums_vec[0]);
83
2
            }
84
85
60
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
60
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
24
                pdqsort(_nums.begin(), _nums.end());
93
24
            }
94
95
60
            if (quantile == 1 || _nums.size() == 1) {
96
34
                return _nums.back();
97
34
            }
98
99
26
            double u = (_nums.size() - 1) * quantile;
100
26
            auto index = static_cast<uint32_t>(u);
101
26
            return _nums[index] +
102
26
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
26
                                                       static_cast<double>(_nums[index]));
104
60
        } else {
105
1
            DCHECK(_nums.empty());
106
1
            size_t rows = 0;
107
2
            for (const auto& i : _sorted_nums_vec) {
108
2
                rows += i.size();
109
2
            }
110
1
            const bool reverse = quantile > 0.5 && rows > 2;
111
1
            double u = (rows - 1) * quantile;
112
1
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
1
            size_t target = reverse ? rows - index - 2 : index;
121
1
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
1
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
1
            return first_number +
129
1
                   (u - static_cast<double>(index)) *
130
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
1
        }
132
61
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
79
300
    double terminate(double quantile) {
80
300
        if (_sorted_nums_vec.size() <= 1) {
81
296
            if (_sorted_nums_vec.size() == 1) {
82
2
                _nums = std::move(_sorted_nums_vec[0]);
83
2
            }
84
85
296
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
296
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
144
                pdqsort(_nums.begin(), _nums.end());
93
144
            }
94
95
296
            if (quantile == 1 || _nums.size() == 1) {
96
87
                return _nums.back();
97
87
            }
98
99
209
            double u = (_nums.size() - 1) * quantile;
100
209
            auto index = static_cast<uint32_t>(u);
101
209
            return _nums[index] +
102
209
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
209
                                                       static_cast<double>(_nums[index]));
104
296
        } else {
105
4
            DCHECK(_nums.empty());
106
4
            size_t rows = 0;
107
14
            for (const auto& i : _sorted_nums_vec) {
108
14
                rows += i.size();
109
14
            }
110
4
            const bool reverse = quantile > 0.5 && rows > 2;
111
4
            double u = (rows - 1) * quantile;
112
4
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
4
            size_t target = reverse ? rows - index - 2 : index;
121
4
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
4
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
4
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
4
            return first_number +
129
4
                   (u - static_cast<double>(index)) *
130
4
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
4
        }
132
300
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
444
    double terminate(double quantile) {
80
444
        if (_sorted_nums_vec.size() <= 1) {
81
346
            if (_sorted_nums_vec.size() == 1) {
82
228
                _nums = std::move(_sorted_nums_vec[0]);
83
228
            }
84
85
346
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
346
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
11
                pdqsort(_nums.begin(), _nums.end());
93
11
            }
94
95
346
            if (quantile == 1 || _nums.size() == 1) {
96
239
                return _nums.back();
97
239
            }
98
99
107
            double u = (_nums.size() - 1) * quantile;
100
107
            auto index = static_cast<uint32_t>(u);
101
107
            return _nums[index] +
102
107
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
107
                                                       static_cast<double>(_nums[index]));
104
346
        } else {
105
98
            DCHECK(_nums.empty());
106
98
            size_t rows = 0;
107
269
            for (const auto& i : _sorted_nums_vec) {
108
269
                rows += i.size();
109
269
            }
110
98
            const bool reverse = quantile > 0.5 && rows > 2;
111
98
            double u = (rows - 1) * quantile;
112
98
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
98
            size_t target = reverse ? rows - index - 2 : index;
121
98
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
98
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
98
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
98
            return first_number +
129
98
                   (u - static_cast<double>(index)) *
130
98
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
98
        }
132
444
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
128
    double terminate(double quantile) {
80
128
        if (_sorted_nums_vec.size() <= 1) {
81
105
            if (_sorted_nums_vec.size() == 1) {
82
35
                _nums = std::move(_sorted_nums_vec[0]);
83
35
            }
84
85
105
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
105
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
105
            if (quantile == 1 || _nums.size() == 1) {
96
59
                return _nums.back();
97
59
            }
98
99
46
            double u = (_nums.size() - 1) * quantile;
100
46
            auto index = static_cast<uint32_t>(u);
101
46
            return _nums[index] +
102
46
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
46
                                                       static_cast<double>(_nums[index]));
104
105
        } else {
105
23
            DCHECK(_nums.empty());
106
23
            size_t rows = 0;
107
50
            for (const auto& i : _sorted_nums_vec) {
108
50
                rows += i.size();
109
50
            }
110
23
            const bool reverse = quantile > 0.5 && rows > 2;
111
23
            double u = (rows - 1) * quantile;
112
23
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
23
            size_t target = reverse ? rows - index - 2 : index;
121
23
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
23
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
23
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
23
            return first_number +
129
23
                   (u - static_cast<double>(index)) *
130
23
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
23
        }
132
128
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
79
33
    double terminate(double quantile) {
80
33
        if (_sorted_nums_vec.size() <= 1) {
81
32
            if (_sorted_nums_vec.size() == 1) {
82
2
                _nums = std::move(_sorted_nums_vec[0]);
83
2
            }
84
85
32
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
32
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
32
            if (quantile == 1 || _nums.size() == 1) {
96
24
                return _nums.back();
97
24
            }
98
99
8
            double u = (_nums.size() - 1) * quantile;
100
8
            auto index = static_cast<uint32_t>(u);
101
8
            return _nums[index] +
102
8
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
8
                                                       static_cast<double>(_nums[index]));
104
32
        } else {
105
1
            DCHECK(_nums.empty());
106
1
            size_t rows = 0;
107
2
            for (const auto& i : _sorted_nums_vec) {
108
2
                rows += i.size();
109
2
            }
110
1
            const bool reverse = quantile > 0.5 && rows > 2;
111
1
            double u = (rows - 1) * quantile;
112
1
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
1
            size_t target = reverse ? rows - index - 2 : index;
121
1
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
1
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
1
            return first_number +
129
1
                   (u - static_cast<double>(index)) *
130
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
1
        }
132
33
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
79
3
    double terminate(double quantile) {
80
3
        if (_sorted_nums_vec.size() <= 1) {
81
2
            if (_sorted_nums_vec.size() == 1) {
82
2
                _nums = std::move(_sorted_nums_vec[0]);
83
2
            }
84
85
2
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
2
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
2
            if (quantile == 1 || _nums.size() == 1) {
96
0
                return _nums.back();
97
0
            }
98
99
2
            double u = (_nums.size() - 1) * quantile;
100
2
            auto index = static_cast<uint32_t>(u);
101
2
            return _nums[index] +
102
2
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
2
                                                       static_cast<double>(_nums[index]));
104
2
        } else {
105
1
            DCHECK(_nums.empty());
106
1
            size_t rows = 0;
107
2
            for (const auto& i : _sorted_nums_vec) {
108
2
                rows += i.size();
109
2
            }
110
1
            const bool reverse = quantile > 0.5 && rows > 2;
111
1
            double u = (rows - 1) * quantile;
112
1
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
1
            size_t target = reverse ? rows - index - 2 : index;
121
1
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
1
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
1
            return first_number +
129
1
                   (u - static_cast<double>(index)) *
130
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
1
        }
132
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
79
136
    double terminate(double quantile) {
80
136
        if (_sorted_nums_vec.size() <= 1) {
81
111
            if (_sorted_nums_vec.size() == 1) {
82
28
                _nums = std::move(_sorted_nums_vec[0]);
83
28
            }
84
85
111
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
111
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
111
            if (quantile == 1 || _nums.size() == 1) {
96
30
                return _nums.back();
97
30
            }
98
99
81
            double u = (_nums.size() - 1) * quantile;
100
81
            auto index = static_cast<uint32_t>(u);
101
81
            return _nums[index] +
102
81
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
103
81
                                                       static_cast<double>(_nums[index]));
104
111
        } else {
105
25
            DCHECK(_nums.empty());
106
25
            size_t rows = 0;
107
62
            for (const auto& i : _sorted_nums_vec) {
108
62
                rows += i.size();
109
62
            }
110
25
            const bool reverse = quantile > 0.5 && rows > 2;
111
25
            double u = (rows - 1) * quantile;
112
25
            auto index = static_cast<uint32_t>(u);
113
            // if reverse, the step of target should start 0 like not reverse
114
            // so here rows need to minus index + 2
115
            // eg: rows = 10, index = 5
116
            // if not reverse, so the first number loc is 5, the second number loc is 6
117
            // if reverse, so the second number is 3, the first number is 4
118
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
119
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
120
25
            size_t target = reverse ? rows - index - 2 : index;
121
25
            if (quantile == 1) {
122
0
                target = 0;
123
0
            }
124
25
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
125
25
            if (quantile == 1) {
126
0
                return second_number;
127
0
            }
128
25
            return first_number +
129
25
                   (u - static_cast<double>(index)) *
130
25
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
131
25
        }
132
136
    }
133
134
private:
135
    struct Node {
136
        Ty value;
137
        int array_index;
138
        int64_t element_index;
139
140
1.40k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIaE4NodessERKS2_
Line
Count
Source
140
2
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
140
71
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
140
932
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
140
320
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsInE4NodessERKS2_
Line
Count
Source
140
2
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIfE4NodessERKS2_
Line
Count
Source
140
2
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
140
72
        auto operator<=>(const Node& other) const { return value <=> other.value; }
141
    };
142
143
251
    void _convert_sorted_num_vec_to_nums() {
144
251
        size_t rows = 0;
145
542
        for (const auto& i : _sorted_nums_vec) {
146
542
            rows += i.size();
147
542
        }
148
251
        _nums.resize(rows);
149
251
        size_t count = 0;
150
151
251
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
793
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
542
            if (!_sorted_nums_vec[i].empty()) {
154
542
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
542
            }
156
542
        }
157
158
925
        while (!min_heap.empty()) {
159
674
            Node node = min_heap.top();
160
674
            min_heap.pop();
161
674
            _nums[count++] = node.value;
162
674
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
132
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
132
                min_heap.push(node);
165
132
            }
166
674
        }
167
251
        _sorted_nums_vec.clear();
168
251
    }
Unexecuted instantiation: _ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv
_ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
143
156
    void _convert_sorted_num_vec_to_nums() {
144
156
        size_t rows = 0;
145
339
        for (const auto& i : _sorted_nums_vec) {
146
339
            rows += i.size();
147
339
        }
148
156
        _nums.resize(rows);
149
156
        size_t count = 0;
150
151
156
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
495
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
339
            if (!_sorted_nums_vec[i].empty()) {
154
339
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
339
            }
156
339
        }
157
158
531
        while (!min_heap.empty()) {
159
375
            Node node = min_heap.top();
160
375
            min_heap.pop();
161
375
            _nums[count++] = node.value;
162
375
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
36
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
36
                min_heap.push(node);
165
36
            }
166
375
        }
167
156
        _sorted_nums_vec.clear();
168
156
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
143
95
    void _convert_sorted_num_vec_to_nums() {
144
95
        size_t rows = 0;
145
203
        for (const auto& i : _sorted_nums_vec) {
146
203
            rows += i.size();
147
203
        }
148
95
        _nums.resize(rows);
149
95
        size_t count = 0;
150
151
95
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
152
298
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
153
203
            if (!_sorted_nums_vec[i].empty()) {
154
203
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
155
203
            }
156
203
        }
157
158
394
        while (!min_heap.empty()) {
159
299
            Node node = min_heap.top();
160
299
            min_heap.pop();
161
299
            _nums[count++] = node.value;
162
299
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
163
96
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
164
96
                min_heap.push(node);
165
96
            }
166
299
        }
167
95
        _sorted_nums_vec.clear();
168
95
    }
Unexecuted instantiation: _ZN5doris6CountsInE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIfE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIdE31_convert_sorted_num_vec_to_numsEv
169
170
153
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
153
        Ty first_number = 0, second_number = 0;
172
153
        size_t count = 0;
173
153
        if (reverse) {
174
28
            std::priority_queue<Node> max_heap;
175
98
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
70
                if (!_sorted_nums_vec[i].empty()) {
177
70
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
70
                                     _sorted_nums_vec[i].size() - 1);
179
70
                }
180
70
            }
181
182
113
            while (!max_heap.empty()) {
183
113
                Node node = max_heap.top();
184
113
                max_heap.pop();
185
113
                if (count == target) {
186
28
                    second_number = node.value;
187
85
                } else if (count == target + 1) {
188
28
                    first_number = node.value;
189
28
                    break;
190
28
                }
191
85
                ++count;
192
85
                if (--node.element_index >= 0) {
193
71
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
71
                    max_heap.push(node);
195
71
                }
196
85
            }
197
198
125
        } else {
199
125
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
456
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
331
                if (!_sorted_nums_vec[i].empty()) {
202
331
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
331
                }
204
331
            }
205
206
352
            while (!min_heap.empty()) {
207
352
                Node node = min_heap.top();
208
352
                min_heap.pop();
209
352
                if (count == target) {
210
125
                    first_number = node.value;
211
227
                } else if (count == target + 1) {
212
125
                    second_number = node.value;
213
125
                    break;
214
125
                }
215
227
                ++count;
216
227
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
140
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
140
                    min_heap.push(node);
219
140
                }
220
227
            }
221
125
        }
222
223
153
        return {first_number, second_number};
224
153
    }
_ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
1
        Ty first_number = 0, second_number = 0;
172
1
        size_t count = 0;
173
1
        if (reverse) {
174
0
            std::priority_queue<Node> max_heap;
175
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
0
                if (!_sorted_nums_vec[i].empty()) {
177
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
0
                                     _sorted_nums_vec[i].size() - 1);
179
0
                }
180
0
            }
181
182
0
            while (!max_heap.empty()) {
183
0
                Node node = max_heap.top();
184
0
                max_heap.pop();
185
0
                if (count == target) {
186
0
                    second_number = node.value;
187
0
                } else if (count == target + 1) {
188
0
                    first_number = node.value;
189
0
                    break;
190
0
                }
191
0
                ++count;
192
0
                if (--node.element_index >= 0) {
193
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
0
                    max_heap.push(node);
195
0
                }
196
0
            }
197
198
1
        } else {
199
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
2
                if (!_sorted_nums_vec[i].empty()) {
202
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
2
                }
204
2
            }
205
206
3
            while (!min_heap.empty()) {
207
3
                Node node = min_heap.top();
208
3
                min_heap.pop();
209
3
                if (count == target) {
210
1
                    first_number = node.value;
211
2
                } else if (count == target + 1) {
212
1
                    second_number = node.value;
213
1
                    break;
214
1
                }
215
2
                ++count;
216
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
1
                    min_heap.push(node);
219
1
                }
220
2
            }
221
1
        }
222
223
1
        return {first_number, second_number};
224
1
    }
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
4
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
4
        Ty first_number = 0, second_number = 0;
172
4
        size_t count = 0;
173
4
        if (reverse) {
174
1
            std::priority_queue<Node> max_heap;
175
5
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
4
                if (!_sorted_nums_vec[i].empty()) {
177
4
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
4
                                     _sorted_nums_vec[i].size() - 1);
179
4
                }
180
4
            }
181
182
6
            while (!max_heap.empty()) {
183
6
                Node node = max_heap.top();
184
6
                max_heap.pop();
185
6
                if (count == target) {
186
1
                    second_number = node.value;
187
5
                } else if (count == target + 1) {
188
1
                    first_number = node.value;
189
1
                    break;
190
1
                }
191
5
                ++count;
192
5
                if (--node.element_index >= 0) {
193
3
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
3
                    max_heap.push(node);
195
3
                }
196
5
            }
197
198
3
        } else {
199
3
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
13
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
10
                if (!_sorted_nums_vec[i].empty()) {
202
10
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
10
                }
204
10
            }
205
206
16
            while (!min_heap.empty()) {
207
16
                Node node = min_heap.top();
208
16
                min_heap.pop();
209
16
                if (count == target) {
210
3
                    first_number = node.value;
211
13
                } else if (count == target + 1) {
212
3
                    second_number = node.value;
213
3
                    break;
214
3
                }
215
13
                ++count;
216
13
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
12
                    min_heap.push(node);
219
12
                }
220
13
            }
221
3
        }
222
223
4
        return {first_number, second_number};
224
4
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
98
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
98
        Ty first_number = 0, second_number = 0;
172
98
        size_t count = 0;
173
98
        if (reverse) {
174
10
            std::priority_queue<Node> max_heap;
175
38
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
28
                if (!_sorted_nums_vec[i].empty()) {
177
28
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
28
                                     _sorted_nums_vec[i].size() - 1);
179
28
                }
180
28
            }
181
182
39
            while (!max_heap.empty()) {
183
39
                Node node = max_heap.top();
184
39
                max_heap.pop();
185
39
                if (count == target) {
186
10
                    second_number = node.value;
187
29
                } else if (count == target + 1) {
188
10
                    first_number = node.value;
189
10
                    break;
190
10
                }
191
29
                ++count;
192
29
                if (--node.element_index >= 0) {
193
22
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
22
                    max_heap.push(node);
195
22
                }
196
29
            }
197
198
88
        } else {
199
88
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
329
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
241
                if (!_sorted_nums_vec[i].empty()) {
202
241
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
241
                }
204
241
            }
205
206
247
            while (!min_heap.empty()) {
207
247
                Node node = min_heap.top();
208
247
                min_heap.pop();
209
247
                if (count == target) {
210
88
                    first_number = node.value;
211
159
                } else if (count == target + 1) {
212
88
                    second_number = node.value;
213
88
                    break;
214
88
                }
215
159
                ++count;
216
159
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
98
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
98
                    min_heap.push(node);
219
98
                }
220
159
            }
221
88
        }
222
223
98
        return {first_number, second_number};
224
98
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
23
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
23
        Ty first_number = 0, second_number = 0;
172
23
        size_t count = 0;
173
23
        if (reverse) {
174
15
            std::priority_queue<Node> max_heap;
175
47
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
32
                if (!_sorted_nums_vec[i].empty()) {
177
32
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
32
                                     _sorted_nums_vec[i].size() - 1);
179
32
                }
180
32
            }
181
182
63
            while (!max_heap.empty()) {
183
63
                Node node = max_heap.top();
184
63
                max_heap.pop();
185
63
                if (count == target) {
186
15
                    second_number = node.value;
187
48
                } else if (count == target + 1) {
188
15
                    first_number = node.value;
189
15
                    break;
190
15
                }
191
48
                ++count;
192
48
                if (--node.element_index >= 0) {
193
45
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
45
                    max_heap.push(node);
195
45
                }
196
48
            }
197
198
15
        } else {
199
8
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
26
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
18
                if (!_sorted_nums_vec[i].empty()) {
202
18
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
18
                }
204
18
            }
205
206
25
            while (!min_heap.empty()) {
207
25
                Node node = min_heap.top();
208
25
                min_heap.pop();
209
25
                if (count == target) {
210
8
                    first_number = node.value;
211
17
                } else if (count == target + 1) {
212
8
                    second_number = node.value;
213
8
                    break;
214
8
                }
215
17
                ++count;
216
17
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
15
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
15
                    min_heap.push(node);
219
15
                }
220
17
            }
221
8
        }
222
223
23
        return {first_number, second_number};
224
23
    }
_ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
1
        Ty first_number = 0, second_number = 0;
172
1
        size_t count = 0;
173
1
        if (reverse) {
174
0
            std::priority_queue<Node> max_heap;
175
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
0
                if (!_sorted_nums_vec[i].empty()) {
177
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
0
                                     _sorted_nums_vec[i].size() - 1);
179
0
                }
180
0
            }
181
182
0
            while (!max_heap.empty()) {
183
0
                Node node = max_heap.top();
184
0
                max_heap.pop();
185
0
                if (count == target) {
186
0
                    second_number = node.value;
187
0
                } else if (count == target + 1) {
188
0
                    first_number = node.value;
189
0
                    break;
190
0
                }
191
0
                ++count;
192
0
                if (--node.element_index >= 0) {
193
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
0
                    max_heap.push(node);
195
0
                }
196
0
            }
197
198
1
        } else {
199
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
2
                if (!_sorted_nums_vec[i].empty()) {
202
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
2
                }
204
2
            }
205
206
3
            while (!min_heap.empty()) {
207
3
                Node node = min_heap.top();
208
3
                min_heap.pop();
209
3
                if (count == target) {
210
1
                    first_number = node.value;
211
2
                } else if (count == target + 1) {
212
1
                    second_number = node.value;
213
1
                    break;
214
1
                }
215
2
                ++count;
216
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
1
                    min_heap.push(node);
219
1
                }
220
2
            }
221
1
        }
222
223
1
        return {first_number, second_number};
224
1
    }
_ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
1
        Ty first_number = 0, second_number = 0;
172
1
        size_t count = 0;
173
1
        if (reverse) {
174
0
            std::priority_queue<Node> max_heap;
175
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
0
                if (!_sorted_nums_vec[i].empty()) {
177
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
0
                                     _sorted_nums_vec[i].size() - 1);
179
0
                }
180
0
            }
181
182
0
            while (!max_heap.empty()) {
183
0
                Node node = max_heap.top();
184
0
                max_heap.pop();
185
0
                if (count == target) {
186
0
                    second_number = node.value;
187
0
                } else if (count == target + 1) {
188
0
                    first_number = node.value;
189
0
                    break;
190
0
                }
191
0
                ++count;
192
0
                if (--node.element_index >= 0) {
193
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
0
                    max_heap.push(node);
195
0
                }
196
0
            }
197
198
1
        } else {
199
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
2
                if (!_sorted_nums_vec[i].empty()) {
202
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
2
                }
204
2
            }
205
206
3
            while (!min_heap.empty()) {
207
3
                Node node = min_heap.top();
208
3
                min_heap.pop();
209
3
                if (count == target) {
210
1
                    first_number = node.value;
211
2
                } else if (count == target + 1) {
212
1
                    second_number = node.value;
213
1
                    break;
214
1
                }
215
2
                ++count;
216
2
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
1
                    min_heap.push(node);
219
1
                }
220
2
            }
221
1
        }
222
223
1
        return {first_number, second_number};
224
1
    }
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
170
25
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
171
25
        Ty first_number = 0, second_number = 0;
172
25
        size_t count = 0;
173
25
        if (reverse) {
174
2
            std::priority_queue<Node> max_heap;
175
8
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
176
6
                if (!_sorted_nums_vec[i].empty()) {
177
6
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
178
6
                                     _sorted_nums_vec[i].size() - 1);
179
6
                }
180
6
            }
181
182
5
            while (!max_heap.empty()) {
183
5
                Node node = max_heap.top();
184
5
                max_heap.pop();
185
5
                if (count == target) {
186
2
                    second_number = node.value;
187
3
                } else if (count == target + 1) {
188
2
                    first_number = node.value;
189
2
                    break;
190
2
                }
191
3
                ++count;
192
3
                if (--node.element_index >= 0) {
193
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
1
                    max_heap.push(node);
195
1
                }
196
3
            }
197
198
23
        } else {
199
23
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
200
79
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
201
56
                if (!_sorted_nums_vec[i].empty()) {
202
56
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
203
56
                }
204
56
            }
205
206
55
            while (!min_heap.empty()) {
207
55
                Node node = min_heap.top();
208
55
                min_heap.pop();
209
55
                if (count == target) {
210
23
                    first_number = node.value;
211
32
                } else if (count == target + 1) {
212
23
                    second_number = node.value;
213
23
                    break;
214
23
                }
215
32
                ++count;
216
32
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
217
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
218
12
                    min_heap.push(node);
219
12
                }
220
32
            }
221
23
        }
222
223
25
        return {first_number, second_number};
224
25
    }
225
226
    vectorized::PODArray<Ty> _nums;
227
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
228
};
229
230
} // namespace doris