Coverage Report

Created: 2026-01-19 16:54

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
5
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
35
5
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIaEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIsEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIiEC2Ev
Unexecuted instantiation: _ZN5doris6CountsInEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIdEC2Ev
36
37
2
    void merge(Counts* other) {
38
2
        if (other != nullptr && !other->_nums.empty()) {
39
2
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
2
        }
41
2
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
37
2
    void merge(Counts* other) {
38
2
        if (other != nullptr && !other->_nums.empty()) {
39
2
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
40
2
        }
41
2
    }
Unexecuted instantiation: _ZN5doris6CountsIaE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIsE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIiE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIdE5mergeEPS1_
42
43
12
    void increment(Ty key, uint32_t i) {
44
12
        auto old_size = _nums.size();
45
12
        _nums.resize(_nums.size() + i);
46
32
        for (uint32_t j = 0; j < i; ++j) {
47
20
            _nums[old_size + j] = key;
48
20
        }
49
12
    }
50
51
0
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIaE9incrementEa
Unexecuted instantiation: _ZN5doris6CountsIsE9incrementEs
Unexecuted instantiation: _ZN5doris6CountsIiE9incrementEi
Unexecuted instantiation: _ZN5doris6CountsIlE9incrementEl
Unexecuted instantiation: _ZN5doris6CountsInE9incrementEn
Unexecuted instantiation: _ZN5doris6CountsIfE9incrementEf
Unexecuted instantiation: _ZN5doris6CountsIdE9incrementEd
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_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
56
57
2
    void serialize(vectorized::BufferWritable& buf) {
58
2
        if (!_nums.empty()) {
59
2
            pdqsort(_nums.begin(), _nums.end());
60
2
            size_t size = _nums.size();
61
2
            buf.write_binary(size);
62
2
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
2
        } 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
2
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
57
2
    void serialize(vectorized::BufferWritable& buf) {
58
2
        if (!_nums.empty()) {
59
2
            pdqsort(_nums.begin(), _nums.end());
60
2
            size_t size = _nums.size();
61
2
            buf.write_binary(size);
62
2
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
63
2
        } 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
2
    }
Unexecuted instantiation: _ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
69
70
2
    void unserialize(vectorized::BufferReadable& buf) {
71
2
        size_t size;
72
2
        buf.read_binary(size);
73
2
        _nums.resize(size);
74
2
        auto buff = buf.read(sizeof(Ty) * size);
75
2
        memcpy(_nums.data(), buff.data, buff.size);
76
2
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
70
2
    void unserialize(vectorized::BufferReadable& buf) {
71
2
        size_t size;
72
2
        buf.read_binary(size);
73
2
        _nums.resize(size);
74
2
        auto buff = buf.read(sizeof(Ty) * size);
75
2
        memcpy(_nums.data(), buff.data, buff.size);
76
2
    }
Unexecuted instantiation: _ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
77
78
3
    double terminate(double quantile) {
79
3
        if (_sorted_nums_vec.size() <= 1) {
80
2
            if (_sorted_nums_vec.size() == 1) {
81
0
                _nums = std::move(_sorted_nums_vec[0]);
82
0
            }
83
84
2
            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
2
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
1
                pdqsort(_nums.begin(), _nums.end());
92
1
            }
93
94
2
            if (quantile == 1 || _nums.size() == 1) {
95
0
                return _nums.back();
96
0
            }
97
98
2
            double u = (_nums.size() - 1) * quantile;
99
2
            auto index = static_cast<uint32_t>(u);
100
2
            return _nums[index] +
101
2
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
2
                                                       static_cast<double>(_nums[index]));
103
2
        } else {
104
1
            DCHECK(_nums.empty());
105
1
            size_t rows = 0;
106
2
            for (const auto& i : _sorted_nums_vec) {
107
2
                rows += i.size();
108
2
            }
109
1
            const bool reverse = quantile > 0.5 && rows > 2;
110
1
            double u = (rows - 1) * quantile;
111
1
            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
1
            size_t target = reverse ? rows - index - 2 : index;
120
1
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
1
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
1
            return first_number +
128
1
                   (u - static_cast<double>(index)) *
129
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
1
        }
131
3
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
78
3
    double terminate(double quantile) {
79
3
        if (_sorted_nums_vec.size() <= 1) {
80
2
            if (_sorted_nums_vec.size() == 1) {
81
0
                _nums = std::move(_sorted_nums_vec[0]);
82
0
            }
83
84
2
            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
2
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
91
1
                pdqsort(_nums.begin(), _nums.end());
92
1
            }
93
94
2
            if (quantile == 1 || _nums.size() == 1) {
95
0
                return _nums.back();
96
0
            }
97
98
2
            double u = (_nums.size() - 1) * quantile;
99
2
            auto index = static_cast<uint32_t>(u);
100
2
            return _nums[index] +
101
2
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
102
2
                                                       static_cast<double>(_nums[index]));
103
2
        } else {
104
1
            DCHECK(_nums.empty());
105
1
            size_t rows = 0;
106
2
            for (const auto& i : _sorted_nums_vec) {
107
2
                rows += i.size();
108
2
            }
109
1
            const bool reverse = quantile > 0.5 && rows > 2;
110
1
            double u = (rows - 1) * quantile;
111
1
            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
1
            size_t target = reverse ? rows - index - 2 : index;
120
1
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
1
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
1
            return first_number +
128
1
                   (u - static_cast<double>(index)) *
129
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
130
1
        }
131
3
    }
Unexecuted instantiation: _ZN5doris6CountsIaE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIsE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIiE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsInE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIdE9terminateEd
132
133
private:
134
    struct Node {
135
        Ty value;
136
        int array_index;
137
        int64_t element_index;
138
139
7
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
139
7
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIsE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIiE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_
140
    };
141
142
0
    void _convert_sorted_num_vec_to_nums() {
143
0
        size_t rows = 0;
144
0
        for (const auto& i : _sorted_nums_vec) {
145
0
            rows += i.size();
146
0
        }
147
0
        _nums.resize(rows);
148
0
        size_t count = 0;
149
150
0
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
151
0
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
152
0
            if (!_sorted_nums_vec[i].empty()) {
153
0
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
154
0
            }
155
0
        }
156
157
0
        while (!min_heap.empty()) {
158
0
            Node node = min_heap.top();
159
0
            min_heap.pop();
160
0
            _nums[count++] = node.value;
161
0
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
162
0
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
163
0
                min_heap.push(node);
164
0
            }
165
0
        }
166
0
        _sorted_nums_vec.clear();
167
0
    }
Unexecuted instantiation: _ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
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
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
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
1
        Ty first_number = 0, second_number = 0;
171
1
        size_t count = 0;
172
1
        if (reverse) {
173
0
            std::priority_queue<Node> max_heap;
174
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
0
                if (!_sorted_nums_vec[i].empty()) {
176
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
0
                                     _sorted_nums_vec[i].size() - 1);
178
0
                }
179
0
            }
180
181
0
            while (!max_heap.empty()) {
182
0
                Node node = max_heap.top();
183
0
                max_heap.pop();
184
0
                if (count == target) {
185
0
                    second_number = node.value;
186
0
                } else if (count == target + 1) {
187
0
                    first_number = node.value;
188
0
                    break;
189
0
                }
190
0
                ++count;
191
0
                if (--node.element_index >= 0) {
192
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
0
                    max_heap.push(node);
194
0
                }
195
0
            }
196
197
1
        } else {
198
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
2
                if (!_sorted_nums_vec[i].empty()) {
201
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
2
                }
203
2
            }
204
205
7
            while (!min_heap.empty()) {
206
7
                Node node = min_heap.top();
207
7
                min_heap.pop();
208
7
                if (count == target) {
209
1
                    first_number = node.value;
210
6
                } else if (count == target + 1) {
211
1
                    second_number = node.value;
212
1
                    break;
213
1
                }
214
6
                ++count;
215
6
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
6
                    min_heap.push(node);
218
6
                }
219
6
            }
220
1
        }
221
222
1
        return {first_number, second_number};
223
1
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
169
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
170
1
        Ty first_number = 0, second_number = 0;
171
1
        size_t count = 0;
172
1
        if (reverse) {
173
0
            std::priority_queue<Node> max_heap;
174
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
175
0
                if (!_sorted_nums_vec[i].empty()) {
176
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
177
0
                                     _sorted_nums_vec[i].size() - 1);
178
0
                }
179
0
            }
180
181
0
            while (!max_heap.empty()) {
182
0
                Node node = max_heap.top();
183
0
                max_heap.pop();
184
0
                if (count == target) {
185
0
                    second_number = node.value;
186
0
                } else if (count == target + 1) {
187
0
                    first_number = node.value;
188
0
                    break;
189
0
                }
190
0
                ++count;
191
0
                if (--node.element_index >= 0) {
192
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
193
0
                    max_heap.push(node);
194
0
                }
195
0
            }
196
197
1
        } else {
198
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
199
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
200
2
                if (!_sorted_nums_vec[i].empty()) {
201
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
202
2
                }
203
2
            }
204
205
7
            while (!min_heap.empty()) {
206
7
                Node node = min_heap.top();
207
7
                min_heap.pop();
208
7
                if (count == target) {
209
1
                    first_number = node.value;
210
6
                } else if (count == target + 1) {
211
1
                    second_number = node.value;
212
1
                    break;
213
1
                }
214
6
                ++count;
215
6
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
216
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
217
6
                    min_heap.push(node);
218
6
                }
219
6
            }
220
1
        }
221
222
1
        return {first_number, second_number};
223
1
    }
Unexecuted instantiation: _ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
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