Coverage Report

Created: 2026-06-09 15:07

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 "vec/common/pod_array.h"
27
#include "vec/common/string_buffer.hpp"
28
#include "vec/io/io_helper.h"
29
30
namespace doris {
31
32
template <typename Ty>
33
class Counts {
34
public:
35
438
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
35
9
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
35
134
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
35
26
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
204
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
35
9
    Counts() = default;
_ZN5doris6CountsIfEC2Ev
Line
Count
Source
35
9
    Counts() = default;
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
35
47
    Counts() = default;
36
37
129
    void merge(Counts* other) {
38
129
        if (other != nullptr && !other->_nums.empty()) {
39
129
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
129
        }
41
129
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
37
3
    void merge(Counts* other) {
38
3
        if (other != nullptr && !other->_nums.empty()) {
39
3
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
3
        }
41
3
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
37
15
    void merge(Counts* other) {
38
15
        if (other != nullptr && !other->_nums.empty()) {
39
15
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
15
        }
41
15
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
37
3
    void merge(Counts* other) {
38
3
        if (other != nullptr && !other->_nums.empty()) {
39
3
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
3
        }
41
3
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
94
    void merge(Counts* other) {
38
94
        if (other != nullptr && !other->_nums.empty()) {
39
94
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
94
        }
41
94
    }
_ZN5doris6CountsInE5mergeEPS1_
Line
Count
Source
37
3
    void merge(Counts* other) {
38
3
        if (other != nullptr && !other->_nums.empty()) {
39
3
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
3
        }
41
3
    }
_ZN5doris6CountsIfE5mergeEPS1_
Line
Count
Source
37
3
    void merge(Counts* other) {
38
3
        if (other != nullptr && !other->_nums.empty()) {
39
3
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
3
        }
41
3
    }
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
37
8
    void merge(Counts* other) {
38
8
        if (other != nullptr && !other->_nums.empty()) {
39
8
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
8
        }
41
8
    }
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
569
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
51
9
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
51
362
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
51
27
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
51
76
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
51
9
    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
77
    void increment(Ty key) { _nums.push_back(key); }
52
53
0
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
54
0
        _nums.insert(keys.begin(), keys.end());
55
0
    }
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_10vectorized8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
56
57
145
    void serialize(vectorized::BufferWritable& buf) {
58
145
        if (!_nums.empty()) {
59
105
            pdqsort(_nums.begin(), _nums.end());
60
105
            size_t size = _nums.size();
61
105
            buf.write_binary(size);
62
105
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
105
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
40
            _convert_sorted_num_vec_to_nums();
66
40
            serialize(buf);
67
40
        }
68
145
    }
_ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
3
    void serialize(vectorized::BufferWritable& buf) {
58
3
        if (!_nums.empty()) {
59
3
            pdqsort(_nums.begin(), _nums.end());
60
3
            size_t size = _nums.size();
61
3
            buf.write_binary(size);
62
3
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
3
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
0
            _convert_sorted_num_vec_to_nums();
66
0
            serialize(buf);
67
0
        }
68
3
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
15
    void serialize(vectorized::BufferWritable& buf) {
58
15
        if (!_nums.empty()) {
59
15
            pdqsort(_nums.begin(), _nums.end());
60
15
            size_t size = _nums.size();
61
15
            buf.write_binary(size);
62
15
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
15
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
0
            _convert_sorted_num_vec_to_nums();
66
0
            serialize(buf);
67
0
        }
68
15
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
3
    void serialize(vectorized::BufferWritable& buf) {
58
3
        if (!_nums.empty()) {
59
3
            pdqsort(_nums.begin(), _nums.end());
60
3
            size_t size = _nums.size();
61
3
            buf.write_binary(size);
62
3
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
3
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
0
            _convert_sorted_num_vec_to_nums();
66
0
            serialize(buf);
67
0
        }
68
3
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
110
    void serialize(vectorized::BufferWritable& buf) {
58
110
        if (!_nums.empty()) {
59
70
            pdqsort(_nums.begin(), _nums.end());
60
70
            size_t size = _nums.size();
61
70
            buf.write_binary(size);
62
70
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
70
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
40
            _convert_sorted_num_vec_to_nums();
66
40
            serialize(buf);
67
40
        }
68
110
    }
_ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
3
    void serialize(vectorized::BufferWritable& buf) {
58
3
        if (!_nums.empty()) {
59
3
            pdqsort(_nums.begin(), _nums.end());
60
3
            size_t size = _nums.size();
61
3
            buf.write_binary(size);
62
3
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
3
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
0
            _convert_sorted_num_vec_to_nums();
66
0
            serialize(buf);
67
0
        }
68
3
    }
_ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
3
    void serialize(vectorized::BufferWritable& buf) {
58
3
        if (!_nums.empty()) {
59
3
            pdqsort(_nums.begin(), _nums.end());
60
3
            size_t size = _nums.size();
61
3
            buf.write_binary(size);
62
3
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
3
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
0
            _convert_sorted_num_vec_to_nums();
66
0
            serialize(buf);
67
0
        }
68
3
    }
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
8
    void serialize(vectorized::BufferWritable& buf) {
58
8
        if (!_nums.empty()) {
59
8
            pdqsort(_nums.begin(), _nums.end());
60
8
            size_t size = _nums.size();
61
8
            buf.write_binary(size);
62
8
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
8
        } else {
64
            // convert _sorted_nums_vec to _nums and do seiralize again
65
0
            _convert_sorted_num_vec_to_nums();
66
0
            serialize(buf);
67
0
        }
68
8
    }
69
70
129
    void unserialize(vectorized::BufferReadable& buf) {
71
129
        size_t size;
72
129
        buf.read_binary(size);
73
129
        _nums.resize(size);
74
129
        auto buff = buf.read(sizeof(Ty) * size);
75
129
        memcpy(_nums.data(), buff.data, buff.size);
76
129
    }
_ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
3
    void unserialize(vectorized::BufferReadable& buf) {
71
3
        size_t size;
72
3
        buf.read_binary(size);
73
3
        _nums.resize(size);
74
3
        auto buff = buf.read(sizeof(Ty) * size);
75
3
        memcpy(_nums.data(), buff.data, buff.size);
76
3
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
15
    void unserialize(vectorized::BufferReadable& buf) {
71
15
        size_t size;
72
15
        buf.read_binary(size);
73
15
        _nums.resize(size);
74
15
        auto buff = buf.read(sizeof(Ty) * size);
75
15
        memcpy(_nums.data(), buff.data, buff.size);
76
15
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
3
    void unserialize(vectorized::BufferReadable& buf) {
71
3
        size_t size;
72
3
        buf.read_binary(size);
73
3
        _nums.resize(size);
74
3
        auto buff = buf.read(sizeof(Ty) * size);
75
3
        memcpy(_nums.data(), buff.data, buff.size);
76
3
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
94
    void unserialize(vectorized::BufferReadable& buf) {
71
94
        size_t size;
72
94
        buf.read_binary(size);
73
94
        _nums.resize(size);
74
94
        auto buff = buf.read(sizeof(Ty) * size);
75
94
        memcpy(_nums.data(), buff.data, buff.size);
76
94
    }
_ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
3
    void unserialize(vectorized::BufferReadable& buf) {
71
3
        size_t size;
72
3
        buf.read_binary(size);
73
3
        _nums.resize(size);
74
3
        auto buff = buf.read(sizeof(Ty) * size);
75
3
        memcpy(_nums.data(), buff.data, buff.size);
76
3
    }
_ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
3
    void unserialize(vectorized::BufferReadable& buf) {
71
3
        size_t size;
72
3
        buf.read_binary(size);
73
3
        _nums.resize(size);
74
3
        auto buff = buf.read(sizeof(Ty) * size);
75
3
        memcpy(_nums.data(), buff.data, buff.size);
76
3
    }
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
8
    void unserialize(vectorized::BufferReadable& buf) {
71
8
        size_t size;
72
8
        buf.read_binary(size);
73
8
        _nums.resize(size);
74
8
        auto buff = buf.read(sizeof(Ty) * size);
75
8
        memcpy(_nums.data(), buff.data, buff.size);
76
8
    }
77
78
265
    double terminate(double quantile) {
79
265
        if (_sorted_nums_vec.size() <= 1) {
80
258
            if (_sorted_nums_vec.size() == 1) {
81
50
                _nums = std::move(_sorted_nums_vec[0]);
82
50
            }
83
84
258
            if (_nums.empty()) {
85
                // Although set null here, but the value is 0.0 and the call method just
86
                // get val in aggregate_function_percentile_approx.h
87
0
                return 0.0;
88
0
            }
89
90
258
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
97
                pdqsort(_nums.begin(), _nums.end());
92
97
            }
93
94
258
            if (quantile == 1 || _nums.size() == 1) {
95
66
                return _nums.back();
96
66
            }
97
98
192
            double u = (_nums.size() - 1) * quantile;
99
192
            auto index = static_cast<uint32_t>(u);
100
192
            return _nums[index] +
101
192
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
192
                                                       static_cast<double>(_nums[index]));
103
258
        } else {
104
7
            DCHECK(_nums.empty());
105
7
            size_t rows = 0;
106
20
            for (const auto& i : _sorted_nums_vec) {
107
20
                rows += i.size();
108
20
            }
109
7
            const bool reverse = quantile > 0.5 && rows > 2;
110
7
            double u = (rows - 1) * quantile;
111
7
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
7
            size_t target = reverse ? rows - index - 2 : index;
120
7
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
7
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
7
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
7
            return first_number +
128
7
                   (u - static_cast<double>(index)) *
129
7
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
7
        }
131
265
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
78
3
    double terminate(double quantile) {
79
3
        if (_sorted_nums_vec.size() <= 1) {
80
3
            if (_sorted_nums_vec.size() == 1) {
81
3
                _nums = std::move(_sorted_nums_vec[0]);
82
3
            }
83
84
3
            if (_nums.empty()) {
85
                // Although set null here, but the value is 0.0 and the call method just
86
                // get val in aggregate_function_percentile_approx.h
87
0
                return 0.0;
88
0
            }
89
90
3
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
3
            if (quantile == 1 || _nums.size() == 1) {
95
0
                return _nums.back();
96
0
            }
97
98
3
            double u = (_nums.size() - 1) * quantile;
99
3
            auto index = static_cast<uint32_t>(u);
100
3
            return _nums[index] +
101
3
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
3
                                                       static_cast<double>(_nums[index]));
103
3
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number +
128
0
                   (u - static_cast<double>(index)) *
129
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
0
        }
131
3
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
78
182
    double terminate(double quantile) {
79
182
        if (_sorted_nums_vec.size() <= 1) {
80
179
            if (_sorted_nums_vec.size() == 1) {
81
3
                _nums = std::move(_sorted_nums_vec[0]);
82
3
            }
83
84
179
            if (_nums.empty()) {
85
                // Although set null here, but the value is 0.0 and the call method just
86
                // get val in aggregate_function_percentile_approx.h
87
0
                return 0.0;
88
0
            }
89
90
179
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
96
                pdqsort(_nums.begin(), _nums.end());
92
96
            }
93
94
179
            if (quantile == 1 || _nums.size() == 1) {
95
42
                return _nums.back();
96
42
            }
97
98
137
            double u = (_nums.size() - 1) * quantile;
99
137
            auto index = static_cast<uint32_t>(u);
100
137
            return _nums[index] +
101
137
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
137
                                                       static_cast<double>(_nums[index]));
103
179
        } else {
104
3
            DCHECK(_nums.empty());
105
3
            size_t rows = 0;
106
12
            for (const auto& i : _sorted_nums_vec) {
107
12
                rows += i.size();
108
12
            }
109
3
            const bool reverse = quantile > 0.5 && rows > 2;
110
3
            double u = (rows - 1) * quantile;
111
3
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
3
            size_t target = reverse ? rows - index - 2 : index;
120
3
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
3
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
3
            return first_number +
128
3
                   (u - static_cast<double>(index)) *
129
3
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
3
        }
131
182
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
78
18
    double terminate(double quantile) {
79
18
        if (_sorted_nums_vec.size() <= 1) {
80
18
            if (_sorted_nums_vec.size() == 1) {
81
3
                _nums = std::move(_sorted_nums_vec[0]);
82
3
            }
83
84
18
            if (_nums.empty()) {
85
                // Although set null here, but the value is 0.0 and the call method just
86
                // get val in aggregate_function_percentile_approx.h
87
0
                return 0.0;
88
0
            }
89
90
18
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
18
            if (quantile == 1 || _nums.size() == 1) {
95
12
                return _nums.back();
96
12
            }
97
98
6
            double u = (_nums.size() - 1) * quantile;
99
6
            auto index = static_cast<uint32_t>(u);
100
6
            return _nums[index] +
101
6
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
6
                                                       static_cast<double>(_nums[index]));
103
18
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number +
128
0
                   (u - static_cast<double>(index)) *
129
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
0
        }
131
18
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
78
33
    double terminate(double quantile) {
79
33
        if (_sorted_nums_vec.size() <= 1) {
80
29
            if (_sorted_nums_vec.size() == 1) {
81
27
                _nums = std::move(_sorted_nums_vec[0]);
82
27
            }
83
84
29
            if (_nums.empty()) {
85
                // Although set null here, but the value is 0.0 and the call method just
86
                // get val in aggregate_function_percentile_approx.h
87
0
                return 0.0;
88
0
            }
89
90
29
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
1
                pdqsort(_nums.begin(), _nums.end());
92
1
            }
93
94
29
            if (quantile == 1 || _nums.size() == 1) {
95
0
                return _nums.back();
96
0
            }
97
98
29
            double u = (_nums.size() - 1) * quantile;
99
29
            auto index = static_cast<uint32_t>(u);
100
29
            return _nums[index] +
101
29
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
29
                                                       static_cast<double>(_nums[index]));
103
29
        } else {
104
4
            DCHECK(_nums.empty());
105
4
            size_t rows = 0;
106
8
            for (const auto& i : _sorted_nums_vec) {
107
8
                rows += i.size();
108
8
            }
109
4
            const bool reverse = quantile > 0.5 && rows > 2;
110
4
            double u = (rows - 1) * quantile;
111
4
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
4
            size_t target = reverse ? rows - index - 2 : index;
120
4
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
4
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
4
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
4
            return first_number +
128
4
                   (u - static_cast<double>(index)) *
129
4
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
4
        }
131
33
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
78
3
    double terminate(double quantile) {
79
3
        if (_sorted_nums_vec.size() <= 1) {
80
3
            if (_sorted_nums_vec.size() == 1) {
81
3
                _nums = std::move(_sorted_nums_vec[0]);
82
3
            }
83
84
3
            if (_nums.empty()) {
85
                // Although set null here, but the value is 0.0 and the call method just
86
                // get val in aggregate_function_percentile_approx.h
87
0
                return 0.0;
88
0
            }
89
90
3
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
3
            if (quantile == 1 || _nums.size() == 1) {
95
0
                return _nums.back();
96
0
            }
97
98
3
            double u = (_nums.size() - 1) * quantile;
99
3
            auto index = static_cast<uint32_t>(u);
100
3
            return _nums[index] +
101
3
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
3
                                                       static_cast<double>(_nums[index]));
103
3
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number +
128
0
                   (u - static_cast<double>(index)) *
129
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
0
        }
131
3
    }
_ZN5doris6CountsIfE9terminateEd
Line
Count
Source
78
3
    double terminate(double quantile) {
79
3
        if (_sorted_nums_vec.size() <= 1) {
80
3
            if (_sorted_nums_vec.size() == 1) {
81
3
                _nums = std::move(_sorted_nums_vec[0]);
82
3
            }
83
84
3
            if (_nums.empty()) {
85
                // Although set null here, but the value is 0.0 and the call method just
86
                // get val in aggregate_function_percentile_approx.h
87
0
                return 0.0;
88
0
            }
89
90
3
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
3
            if (quantile == 1 || _nums.size() == 1) {
95
0
                return _nums.back();
96
0
            }
97
98
3
            double u = (_nums.size() - 1) * quantile;
99
3
            auto index = static_cast<uint32_t>(u);
100
3
            return _nums[index] +
101
3
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
3
                                                       static_cast<double>(_nums[index]));
103
3
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number +
128
0
                   (u - static_cast<double>(index)) *
129
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
0
        }
131
3
    }
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
78
23
    double terminate(double quantile) {
79
23
        if (_sorted_nums_vec.size() <= 1) {
80
23
            if (_sorted_nums_vec.size() == 1) {
81
8
                _nums = std::move(_sorted_nums_vec[0]);
82
8
            }
83
84
23
            if (_nums.empty()) {
85
                // Although set null here, but the value is 0.0 and the call method just
86
                // get val in aggregate_function_percentile_approx.h
87
0
                return 0.0;
88
0
            }
89
90
23
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
0
                pdqsort(_nums.begin(), _nums.end());
92
0
            }
93
94
23
            if (quantile == 1 || _nums.size() == 1) {
95
12
                return _nums.back();
96
12
            }
97
98
11
            double u = (_nums.size() - 1) * quantile;
99
11
            auto index = static_cast<uint32_t>(u);
100
11
            return _nums[index] +
101
11
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
11
                                                       static_cast<double>(_nums[index]));
103
23
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number +
128
0
                   (u - static_cast<double>(index)) *
129
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
0
        }
131
23
    }
132
133
private:
134
    struct Node {
135
        Ty value;
136
        int array_index;
137
        int64_t element_index;
138
139
125
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
139
68
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIiE4NodessERKS2_
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
139
57
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_
140
    };
141
142
40
    void _convert_sorted_num_vec_to_nums() {
143
40
        size_t rows = 0;
144
59
        for (const auto& i : _sorted_nums_vec) {
145
59
            rows += i.size();
146
59
        }
147
40
        _nums.resize(rows);
148
40
        size_t count = 0;
149
150
40
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
151
99
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
152
59
            if (!_sorted_nums_vec[i].empty()) {
153
59
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
154
59
            }
155
59
        }
156
157
193
        while (!min_heap.empty()) {
158
153
            Node node = min_heap.top();
159
153
            min_heap.pop();
160
153
            _nums[count++] = node.value;
161
153
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
162
94
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
163
94
                min_heap.push(node);
164
94
            }
165
153
        }
166
40
        _sorted_nums_vec.clear();
167
40
    }
Unexecuted instantiation: _ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
142
40
    void _convert_sorted_num_vec_to_nums() {
143
40
        size_t rows = 0;
144
59
        for (const auto& i : _sorted_nums_vec) {
145
59
            rows += i.size();
146
59
        }
147
40
        _nums.resize(rows);
148
40
        size_t count = 0;
149
150
40
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
151
99
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
152
59
            if (!_sorted_nums_vec[i].empty()) {
153
59
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
154
59
            }
155
59
        }
156
157
193
        while (!min_heap.empty()) {
158
153
            Node node = min_heap.top();
159
153
            min_heap.pop();
160
153
            _nums[count++] = node.value;
161
153
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
162
94
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
163
94
                min_heap.push(node);
164
94
            }
165
153
        }
166
40
        _sorted_nums_vec.clear();
167
40
    }
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
168
169
7
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
7
        Ty first_number = 0, second_number = 0;
171
7
        size_t count = 0;
172
7
        if (reverse) {
173
2
            std::priority_queue<Node> max_heap;
174
8
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
6
                if (!_sorted_nums_vec[i].empty()) {
176
6
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
6
                                     _sorted_nums_vec[i].size() - 1);
178
6
                }
179
6
            }
180
181
8
            while (!max_heap.empty()) {
182
8
                Node node = max_heap.top();
183
8
                max_heap.pop();
184
8
                if (count == target) {
185
2
                    second_number = node.value;
186
6
                } else if (count == target + 1) {
187
2
                    first_number = node.value;
188
2
                    break;
189
2
                }
190
6
                ++count;
191
6
                if (--node.element_index >= 0) {
192
4
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
4
                    max_heap.push(node);
194
4
                }
195
6
            }
196
197
5
        } else {
198
5
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
19
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
14
                if (!_sorted_nums_vec[i].empty()) {
201
14
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
14
                }
203
14
            }
204
205
26
            while (!min_heap.empty()) {
206
26
                Node node = min_heap.top();
207
26
                min_heap.pop();
208
26
                if (count == target) {
209
5
                    first_number = node.value;
210
21
                } else if (count == target + 1) {
211
5
                    second_number = node.value;
212
5
                    break;
213
5
                }
214
21
                ++count;
215
21
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
21
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
21
                    min_heap.push(node);
218
21
                }
219
21
            }
220
5
        }
221
222
7
        return {first_number, second_number};
223
7
    }
Unexecuted instantiation: _ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
169
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
3
        Ty first_number = 0, second_number = 0;
171
3
        size_t count = 0;
172
3
        if (reverse) {
173
1
            std::priority_queue<Node> max_heap;
174
5
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
4
                if (!_sorted_nums_vec[i].empty()) {
176
4
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
4
                                     _sorted_nums_vec[i].size() - 1);
178
4
                }
179
4
            }
180
181
6
            while (!max_heap.empty()) {
182
6
                Node node = max_heap.top();
183
6
                max_heap.pop();
184
6
                if (count == target) {
185
1
                    second_number = node.value;
186
5
                } else if (count == target + 1) {
187
1
                    first_number = node.value;
188
1
                    break;
189
1
                }
190
5
                ++count;
191
5
                if (--node.element_index >= 0) {
192
3
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
3
                    max_heap.push(node);
194
3
                }
195
5
            }
196
197
2
        } else {
198
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
10
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
8
                if (!_sorted_nums_vec[i].empty()) {
201
8
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
8
                }
203
8
            }
204
205
13
            while (!min_heap.empty()) {
206
13
                Node node = min_heap.top();
207
13
                min_heap.pop();
208
13
                if (count == target) {
209
2
                    first_number = node.value;
210
11
                } else if (count == target + 1) {
211
2
                    second_number = node.value;
212
2
                    break;
213
2
                }
214
11
                ++count;
215
11
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
11
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
11
                    min_heap.push(node);
218
11
                }
219
11
            }
220
2
        }
221
222
3
        return {first_number, second_number};
223
3
    }
Unexecuted instantiation: _ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
169
4
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
4
        Ty first_number = 0, second_number = 0;
171
4
        size_t count = 0;
172
4
        if (reverse) {
173
1
            std::priority_queue<Node> max_heap;
174
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
2
                if (!_sorted_nums_vec[i].empty()) {
176
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
2
                                     _sorted_nums_vec[i].size() - 1);
178
2
                }
179
2
            }
180
181
2
            while (!max_heap.empty()) {
182
2
                Node node = max_heap.top();
183
2
                max_heap.pop();
184
2
                if (count == target) {
185
1
                    second_number = node.value;
186
1
                } else if (count == target + 1) {
187
1
                    first_number = node.value;
188
1
                    break;
189
1
                }
190
1
                ++count;
191
1
                if (--node.element_index >= 0) {
192
1
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
1
                    max_heap.push(node);
194
1
                }
195
1
            }
196
197
3
        } else {
198
3
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
9
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
6
                if (!_sorted_nums_vec[i].empty()) {
201
6
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
6
                }
203
6
            }
204
205
13
            while (!min_heap.empty()) {
206
13
                Node node = min_heap.top();
207
13
                min_heap.pop();
208
13
                if (count == target) {
209
3
                    first_number = node.value;
210
10
                } else if (count == target + 1) {
211
3
                    second_number = node.value;
212
3
                    break;
213
3
                }
214
10
                ++count;
215
10
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
10
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
10
                    min_heap.push(node);
218
10
                }
219
10
            }
220
3
        }
221
222
4
        return {first_number, second_number};
223
4
    }
Unexecuted instantiation: _ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
224
225
    vectorized::PODArray<Ty> _nums;
226
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
227
};
228
229
} // namespace doris