Coverage Report

Created: 2025-03-12 11:55

/root/doris/be/src/util/counts.h
Line
Count
Source (jump to first uncovered line)
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
5
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
5
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIhEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIaEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIsEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIiEC2Ev
Unexecuted instantiation: _ZN5doris6CountsInEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIdEC2Ev
37
38
2
    void merge(Counts* other) {
39
2
        if (other != nullptr && !other->_nums.empty()) {
40
2
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
2
        }
42
2
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
2
    void merge(Counts* other) {
39
2
        if (other != nullptr && !other->_nums.empty()) {
40
2
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
2
        }
42
2
    }
Unexecuted instantiation: _ZN5doris6CountsIhE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIaE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIsE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIiE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIdE5mergeEPS1_
43
44
12
    void increment(Ty key, uint32_t i) {
45
12
        auto old_size = _nums.size();
46
12
        _nums.resize(_nums.size() + i);
47
32
        for (uint32_t j = 0; j < i; ++j) {
48
20
            _nums[old_size + j] = key;
49
20
        }
50
12
    }
51
52
0
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIhE9incrementEh
Unexecuted instantiation: _ZN5doris6CountsIaE9incrementEa
Unexecuted instantiation: _ZN5doris6CountsIsE9incrementEs
Unexecuted instantiation: _ZN5doris6CountsIiE9incrementEi
Unexecuted instantiation: _ZN5doris6CountsIlE9incrementEl
Unexecuted instantiation: _ZN5doris6CountsInE9incrementEn
Unexecuted instantiation: _ZN5doris6CountsIfE9incrementEf
Unexecuted instantiation: _ZN5doris6CountsIdE9incrementEd
53
54
0
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
0
        _nums.insert(keys.begin(), keys.end());
56
0
    }
Unexecuted instantiation: _ZN5doris6CountsIhE15increment_batchERKNS_10vectorized8PODArrayIhLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_10vectorized8PODArrayIaLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
57
58
2
    void serialize(vectorized::BufferWritable& buf) {
59
2
        if (!_nums.empty()) {
60
2
            pdqsort(_nums.begin(), _nums.end());
61
2
            size_t size = _nums.size();
62
2
            write_binary(size, buf);
63
2
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
2
        } 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
2
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
2
    void serialize(vectorized::BufferWritable& buf) {
59
2
        if (!_nums.empty()) {
60
2
            pdqsort(_nums.begin(), _nums.end());
61
2
            size_t size = _nums.size();
62
2
            write_binary(size, buf);
63
2
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
2
        } 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
2
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
70
71
2
    void unserialize(vectorized::BufferReadable& buf) {
72
2
        size_t size;
73
2
        read_binary(size, buf);
74
2
        _nums.resize(size);
75
2
        auto buff = buf.read(sizeof(Ty) * size);
76
2
        memcpy(_nums.data(), buff.data, buff.size);
77
2
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
2
    void unserialize(vectorized::BufferReadable& buf) {
72
2
        size_t size;
73
2
        read_binary(size, buf);
74
2
        _nums.resize(size);
75
2
        auto buff = buf.read(sizeof(Ty) * size);
76
2
        memcpy(_nums.data(), buff.data, buff.size);
77
2
    }
Unexecuted instantiation: _ZN5doris6CountsIhE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
78
79
3
    double terminate(double quantile) {
80
3
        if (_sorted_nums_vec.size() <= 1) {
81
2
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
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
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
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)) * (_nums[index + 1] - _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 + (u - static_cast<double>(index)) * (second_number - first_number);
128
1
        }
129
3
    }
_ZN5doris6CountsIlE9terminateEd
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
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
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
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
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)) * (_nums[index + 1] - _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 + (u - static_cast<double>(index)) * (second_number - first_number);
128
1
        }
129
3
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIaE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIsE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIiE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsInE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIdE9terminateEd
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
7
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
7
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIhE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIsE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIiE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_
138
    };
139
140
0
    void _convert_sorted_num_vec_to_nums() {
141
0
        size_t rows = 0;
142
0
        for (const auto& i : _sorted_nums_vec) {
143
0
            rows += i.size();
144
0
        }
145
0
        _nums.resize(rows);
146
0
        size_t count = 0;
147
148
0
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
0
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
0
            if (!_sorted_nums_vec[i].empty()) {
151
0
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
0
            }
153
0
        }
154
155
0
        while (!min_heap.empty()) {
156
0
            Node node = min_heap.top();
157
0
            min_heap.pop();
158
0
            _nums[count++] = node.value;
159
0
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
0
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
0
                min_heap.push(node);
162
0
            }
163
0
        }
164
0
        _sorted_nums_vec.clear();
165
0
    }
Unexecuted instantiation: _ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIhE31_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
166
167
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
1
        Ty first_number = 0, second_number = 0;
169
1
        size_t count = 0;
170
1
        if (reverse) {
171
0
            std::priority_queue<Node> max_heap;
172
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
0
                if (!_sorted_nums_vec[i].empty()) {
174
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
0
                                     _sorted_nums_vec[i].size() - 1);
176
0
                }
177
0
            }
178
179
0
            while (!max_heap.empty()) {
180
0
                Node node = max_heap.top();
181
0
                max_heap.pop();
182
0
                if (count == target) {
183
0
                    second_number = node.value;
184
0
                } else if (count == target + 1) {
185
0
                    first_number = node.value;
186
0
                    break;
187
0
                }
188
0
                ++count;
189
0
                if (--node.element_index >= 0) {
190
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
0
                    max_heap.push(node);
192
0
                }
193
0
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
7
            while (!min_heap.empty()) {
204
7
                Node node = min_heap.top();
205
7
                min_heap.pop();
206
7
                if (count == target) {
207
1
                    first_number = node.value;
208
6
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
6
                ++count;
213
6
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
6
                    min_heap.push(node);
216
6
                }
217
6
            }
218
1
        }
219
220
1
        return {first_number, second_number};
221
1
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
1
        Ty first_number = 0, second_number = 0;
169
1
        size_t count = 0;
170
1
        if (reverse) {
171
0
            std::priority_queue<Node> max_heap;
172
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
0
                if (!_sorted_nums_vec[i].empty()) {
174
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
0
                                     _sorted_nums_vec[i].size() - 1);
176
0
                }
177
0
            }
178
179
0
            while (!max_heap.empty()) {
180
0
                Node node = max_heap.top();
181
0
                max_heap.pop();
182
0
                if (count == target) {
183
0
                    second_number = node.value;
184
0
                } else if (count == target + 1) {
185
0
                    first_number = node.value;
186
0
                    break;
187
0
                }
188
0
                ++count;
189
0
                if (--node.element_index >= 0) {
190
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
0
                    max_heap.push(node);
192
0
                }
193
0
            }
194
195
1
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
7
            while (!min_heap.empty()) {
204
7
                Node node = min_heap.top();
205
7
                min_heap.pop();
206
7
                if (count == target) {
207
1
                    first_number = node.value;
208
6
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
6
                ++count;
213
6
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
6
                    min_heap.push(node);
216
6
                }
217
6
            }
218
1
        }
219
220
1
        return {first_number, second_number};
221
1
    }
Unexecuted instantiation: _ZN5doris6CountsIhE27_merge_sort_and_get_numbersElb
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
222
223
    vectorized::PODArray<Ty> _nums;
224
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
225
};
226
227
} // namespace doris