Coverage Report

Created: 2026-01-08 09:56

/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
class OldCounts {
34
public:
35
    OldCounts() = default;
36
37
0
    inline void merge(const OldCounts* other) {
38
0
        if (other == nullptr || other->_counts.empty()) {
39
0
            return;
40
0
        }
41
0
42
0
        for (auto& cell : other->_counts) {
43
0
            increment(cell.first, cell.second);
44
0
        }
45
0
    }
46
47
0
    void increment(int64_t key, uint32_t i) {
48
0
        auto item = _counts.find(key);
49
0
        if (item != _counts.end()) {
50
0
            item->second += i;
51
0
        } else {
52
0
            _counts.emplace(std::make_pair(key, i));
53
0
        }
54
0
    }
55
56
0
    uint32_t serialized_size() const {
57
0
        return sizeof(uint32_t) + sizeof(int64_t) * _counts.size() +
58
0
               sizeof(uint32_t) * _counts.size();
59
0
    }
60
61
0
    void serialize(uint8_t* writer) const {
62
0
        uint32_t size = _counts.size();
63
0
        memcpy(writer, &size, sizeof(uint32_t));
64
0
        writer += sizeof(uint32_t);
65
0
        for (auto& cell : _counts) {
66
0
            memcpy(writer, &cell.first, sizeof(int64_t));
67
0
            writer += sizeof(int64_t);
68
0
            memcpy(writer, &cell.second, sizeof(uint32_t));
69
0
            writer += sizeof(uint32_t);
70
0
        }
71
0
    }
72
73
0
    void unserialize(const uint8_t* type_reader) {
74
0
        uint32_t size;
75
0
        memcpy(&size, type_reader, sizeof(uint32_t));
76
0
        type_reader += sizeof(uint32_t);
77
0
        for (uint32_t i = 0; i < size; ++i) {
78
0
            int64_t key;
79
0
            uint32_t count;
80
0
            memcpy(&key, type_reader, sizeof(int64_t));
81
0
            type_reader += sizeof(int64_t);
82
0
            memcpy(&count, type_reader, sizeof(uint32_t));
83
0
            type_reader += sizeof(uint32_t);
84
0
            _counts.emplace(std::make_pair(key, count));
85
0
        }
86
0
    }
87
88
    double get_percentile(std::vector<std::pair<int64_t, uint32_t>>& counts,
89
0
                          double position) const {
90
0
        long lower = long(std::floor(position));
91
0
        long higher = long(std::ceil(position));
92
0
93
0
        auto iter = counts.begin();
94
0
        for (; iter != counts.end() && iter->second < lower + 1; ++iter)
95
0
            ;
96
0
97
0
        int64_t lower_key = iter->first;
98
0
        if (higher == lower) {
99
0
            return lower_key;
100
0
        }
101
0
102
0
        if (iter->second < higher + 1) {
103
0
            iter++;
104
0
        }
105
0
106
0
        int64_t higher_key = iter->first;
107
0
        if (lower_key == higher_key) {
108
0
            return lower_key;
109
0
        }
110
0
111
0
        return (higher - position) * lower_key + (position - lower) * higher_key;
112
0
    }
113
114
0
    double terminate(double quantile) const {
115
0
        if (_counts.empty()) {
116
0
            // Although set null here, but the value is 0.0 and the call method just
117
0
            // get val in aggregate_function_percentile_approx.h
118
0
            return 0.0;
119
0
        }
120
0
121
0
        std::vector<std::pair<int64_t, uint32_t>> elems(_counts.begin(), _counts.end());
122
0
        sort(elems.begin(), elems.end(),
123
0
             [](const std::pair<int64_t, uint32_t> l, const std::pair<int64_t, uint32_t> r) {
124
0
                 return l.first < r.first;
125
0
             });
126
0
127
0
        long total = 0;
128
0
        for (auto& cell : elems) {
129
0
            total += cell.second;
130
0
            cell.second = total;
131
0
        }
132
0
133
0
        long max_position = total - 1;
134
0
        double position = max_position * quantile;
135
0
        return get_percentile(elems, position);
136
0
    }
137
138
private:
139
    std::unordered_map<int64_t, uint32_t> _counts;
140
};
141
template <typename Ty>
142
class Counts {
143
public:
144
7.11k
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIhEC2Ev
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
144
68
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
144
450
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
144
2.65k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
144
3.39k
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
144
60
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
144
478
    Counts() = default;
145
146
2.48k
    void merge(Counts* other) {
147
2.48k
        if (other != nullptr && !other->_nums.empty()) {
148
2.48k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
149
2.48k
        }
150
2.48k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIaE5mergeEPS1_
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
146
6
    void merge(Counts* other) {
147
6
        if (other != nullptr && !other->_nums.empty()) {
148
6
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
149
6
        }
150
6
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
146
896
    void merge(Counts* other) {
147
896
        if (other != nullptr && !other->_nums.empty()) {
148
896
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
149
896
        }
150
896
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
146
1.42k
    void merge(Counts* other) {
147
1.42k
        if (other != nullptr && !other->_nums.empty()) {
148
1.42k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
149
1.42k
        }
150
1.42k
    }
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
146
160
    void merge(Counts* other) {
147
160
        if (other != nullptr && !other->_nums.empty()) {
148
160
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
149
160
        }
150
160
    }
151
152
    void increment(Ty key, uint32_t i) {
153
        auto old_size = _nums.size();
154
        _nums.resize(_nums.size() + i);
155
        for (uint32_t j = 0; j < i; ++j) {
156
            _nums[old_size + j] = key;
157
        }
158
    }
159
160
6.11k
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIhE9incrementEh
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
160
128
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
160
1.57k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
160
2.40k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
160
1.42k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
160
72
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIfE9incrementEf
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
160
500
    void increment(Ty key) { _nums.push_back(key); }
161
162
10
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
163
10
        _nums.insert(keys.begin(), keys.end());
164
10
    }
Unexecuted instantiation: _ZN5doris6CountsIhE15increment_batchERKNS_10vectorized8PODArrayIhLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_10vectorized8PODArrayIaLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
_ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Line
Count
Source
162
10
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
163
10
        _nums.insert(keys.begin(), keys.end());
164
10
    }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096E9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorELm16ELm15EEE
165
166
3.31k
    void serialize(vectorized::BufferWritable& buf) {
167
3.31k
        if (!_nums.empty()) {
168
2.70k
            pdqsort(_nums.begin(), _nums.end());
169
2.70k
            size_t size = _nums.size();
170
2.70k
            write_binary(size, buf);
171
2.70k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
172
2.70k
        } else {
173
            // convert _sorted_nums_vec to _nums and do seiralize again
174
604
            _convert_sorted_num_vec_to_nums();
175
604
            serialize(buf);
176
604
        }
177
3.31k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
166
6
    void serialize(vectorized::BufferWritable& buf) {
167
6
        if (!_nums.empty()) {
168
6
            pdqsort(_nums.begin(), _nums.end());
169
6
            size_t size = _nums.size();
170
6
            write_binary(size, buf);
171
6
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
172
6
        } else {
173
            // convert _sorted_nums_vec to _nums and do seiralize again
174
0
            _convert_sorted_num_vec_to_nums();
175
0
            serialize(buf);
176
0
        }
177
6
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
166
896
    void serialize(vectorized::BufferWritable& buf) {
167
896
        if (!_nums.empty()) {
168
896
            pdqsort(_nums.begin(), _nums.end());
169
896
            size_t size = _nums.size();
170
896
            write_binary(size, buf);
171
896
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
172
896
        } else {
173
            // convert _sorted_nums_vec to _nums and do seiralize again
174
0
            _convert_sorted_num_vec_to_nums();
175
0
            serialize(buf);
176
0
        }
177
896
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
166
2.25k
    void serialize(vectorized::BufferWritable& buf) {
167
2.25k
        if (!_nums.empty()) {
168
1.64k
            pdqsort(_nums.begin(), _nums.end());
169
1.64k
            size_t size = _nums.size();
170
1.64k
            write_binary(size, buf);
171
1.64k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
172
1.64k
        } else {
173
            // convert _sorted_nums_vec to _nums and do seiralize again
174
604
            _convert_sorted_num_vec_to_nums();
175
604
            serialize(buf);
176
604
        }
177
2.25k
    }
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
166
160
    void serialize(vectorized::BufferWritable& buf) {
167
160
        if (!_nums.empty()) {
168
160
            pdqsort(_nums.begin(), _nums.end());
169
160
            size_t size = _nums.size();
170
160
            write_binary(size, buf);
171
160
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
172
160
        } else {
173
            // convert _sorted_nums_vec to _nums and do seiralize again
174
0
            _convert_sorted_num_vec_to_nums();
175
0
            serialize(buf);
176
0
        }
177
160
    }
178
179
2.48k
    void unserialize(vectorized::BufferReadable& buf) {
180
2.48k
        size_t size;
181
2.48k
        read_binary(size, buf);
182
2.48k
        _nums.resize(size);
183
2.48k
        auto buff = buf.read(sizeof(Ty) * size);
184
2.48k
        memcpy(_nums.data(), buff.data, buff.size);
185
2.48k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
179
6
    void unserialize(vectorized::BufferReadable& buf) {
180
6
        size_t size;
181
6
        read_binary(size, buf);
182
6
        _nums.resize(size);
183
6
        auto buff = buf.read(sizeof(Ty) * size);
184
6
        memcpy(_nums.data(), buff.data, buff.size);
185
6
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
179
896
    void unserialize(vectorized::BufferReadable& buf) {
180
896
        size_t size;
181
896
        read_binary(size, buf);
182
896
        _nums.resize(size);
183
896
        auto buff = buf.read(sizeof(Ty) * size);
184
896
        memcpy(_nums.data(), buff.data, buff.size);
185
896
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
179
1.42k
    void unserialize(vectorized::BufferReadable& buf) {
180
1.42k
        size_t size;
181
1.42k
        read_binary(size, buf);
182
1.42k
        _nums.resize(size);
183
1.42k
        auto buff = buf.read(sizeof(Ty) * size);
184
1.42k
        memcpy(_nums.data(), buff.data, buff.size);
185
1.42k
    }
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
179
160
    void unserialize(vectorized::BufferReadable& buf) {
180
160
        size_t size;
181
160
        read_binary(size, buf);
182
160
        _nums.resize(size);
183
160
        auto buff = buf.read(sizeof(Ty) * size);
184
160
        memcpy(_nums.data(), buff.data, buff.size);
185
160
    }
186
187
2.20k
    double terminate(double quantile) {
188
2.20k
        if (_sorted_nums_vec.size() <= 1) {
189
1.93k
            if (_sorted_nums_vec.size() == 1) {
190
646
                _nums = std::move(_sorted_nums_vec[0]);
191
646
            }
192
193
1.93k
            if (_nums.empty()) {
194
                // Although set null here, but the value is 0.0 and the call method just
195
                // get val in aggregate_function_percentile_approx.h
196
0
                return 0.0;
197
0
            }
198
199
1.93k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
200
385
                pdqsort(_nums.begin(), _nums.end());
201
385
            }
202
203
1.93k
            if (quantile == 1 || _nums.size() == 1) {
204
954
                return _nums.back();
205
954
            }
206
207
980
            double u = (_nums.size() - 1) * quantile;
208
980
            auto index = static_cast<uint32_t>(u);
209
980
            return _nums[index] +
210
980
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
211
1.93k
        } else {
212
275
            DCHECK(_nums.empty());
213
275
            size_t rows = 0;
214
670
            for (const auto& i : _sorted_nums_vec) {
215
670
                rows += i.size();
216
670
            }
217
275
            const bool reverse = quantile > 0.5 && rows > 2;
218
275
            double u = (rows - 1) * quantile;
219
275
            auto index = static_cast<uint32_t>(u);
220
            // if reverse, the step of target should start 0 like not reverse
221
            // so here rows need to minus index + 2
222
            // eg: rows = 10, index = 5
223
            // if not reverse, so the first number loc is 5, the second number loc is 6
224
            // if reverse, so the second number is 3, the first number is 4
225
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
226
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
227
275
            size_t target = reverse ? rows - index - 2 : index;
228
275
            if (quantile == 1) {
229
0
                target = 0;
230
0
            }
231
275
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
232
275
            if (quantile == 1) {
233
0
                return second_number;
234
0
            }
235
275
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
236
275
        }
237
2.20k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9terminateEd
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
187
116
    double terminate(double quantile) {
188
116
        if (_sorted_nums_vec.size() <= 1) {
189
116
            if (_sorted_nums_vec.size() == 1) {
190
0
                _nums = std::move(_sorted_nums_vec[0]);
191
0
            }
192
193
116
            if (_nums.empty()) {
194
                // Although set null here, but the value is 0.0 and the call method just
195
                // get val in aggregate_function_percentile_approx.h
196
0
                return 0.0;
197
0
            }
198
199
116
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
200
48
                pdqsort(_nums.begin(), _nums.end());
201
48
            }
202
203
116
            if (quantile == 1 || _nums.size() == 1) {
204
68
                return _nums.back();
205
68
            }
206
207
48
            double u = (_nums.size() - 1) * quantile;
208
48
            auto index = static_cast<uint32_t>(u);
209
48
            return _nums[index] +
210
48
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
211
116
        } else {
212
0
            DCHECK(_nums.empty());
213
0
            size_t rows = 0;
214
0
            for (const auto& i : _sorted_nums_vec) {
215
0
                rows += i.size();
216
0
            }
217
0
            const bool reverse = quantile > 0.5 && rows > 2;
218
0
            double u = (rows - 1) * quantile;
219
0
            auto index = static_cast<uint32_t>(u);
220
            // if reverse, the step of target should start 0 like not reverse
221
            // so here rows need to minus index + 2
222
            // eg: rows = 10, index = 5
223
            // if not reverse, so the first number loc is 5, the second number loc is 6
224
            // if reverse, so the second number is 3, the first number is 4
225
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
226
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
227
0
            size_t target = reverse ? rows - index - 2 : index;
228
0
            if (quantile == 1) {
229
0
                target = 0;
230
0
            }
231
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
232
0
            if (quantile == 1) {
233
0
                return second_number;
234
0
            }
235
0
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
236
0
        }
237
116
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
187
594
    double terminate(double quantile) {
188
594
        if (_sorted_nums_vec.size() <= 1) {
189
594
            if (_sorted_nums_vec.size() == 1) {
190
6
                _nums = std::move(_sorted_nums_vec[0]);
191
6
            }
192
193
594
            if (_nums.empty()) {
194
                // Although set null here, but the value is 0.0 and the call method just
195
                // get val in aggregate_function_percentile_approx.h
196
0
                return 0.0;
197
0
            }
198
199
594
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
200
330
                pdqsort(_nums.begin(), _nums.end());
201
330
            }
202
203
594
            if (quantile == 1 || _nums.size() == 1) {
204
174
                return _nums.back();
205
174
            }
206
207
420
            double u = (_nums.size() - 1) * quantile;
208
420
            auto index = static_cast<uint32_t>(u);
209
420
            return _nums[index] +
210
420
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
211
594
        } else {
212
0
            DCHECK(_nums.empty());
213
0
            size_t rows = 0;
214
0
            for (const auto& i : _sorted_nums_vec) {
215
0
                rows += i.size();
216
0
            }
217
0
            const bool reverse = quantile > 0.5 && rows > 2;
218
0
            double u = (rows - 1) * quantile;
219
0
            auto index = static_cast<uint32_t>(u);
220
            // if reverse, the step of target should start 0 like not reverse
221
            // so here rows need to minus index + 2
222
            // eg: rows = 10, index = 5
223
            // if not reverse, so the first number loc is 5, the second number loc is 6
224
            // if reverse, so the second number is 3, the first number is 4
225
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
226
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
227
0
            size_t target = reverse ? rows - index - 2 : index;
228
0
            if (quantile == 1) {
229
0
                target = 0;
230
0
            }
231
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
232
0
            if (quantile == 1) {
233
0
                return second_number;
234
0
            }
235
0
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
236
0
        }
237
594
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
187
862
    double terminate(double quantile) {
188
862
        if (_sorted_nums_vec.size() <= 1) {
189
668
            if (_sorted_nums_vec.size() == 1) {
190
448
                _nums = std::move(_sorted_nums_vec[0]);
191
448
            }
192
193
668
            if (_nums.empty()) {
194
                // Although set null here, but the value is 0.0 and the call method just
195
                // get val in aggregate_function_percentile_approx.h
196
0
                return 0.0;
197
0
            }
198
199
668
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
200
6
                pdqsort(_nums.begin(), _nums.end());
201
6
            }
202
203
668
            if (quantile == 1 || _nums.size() == 1) {
204
478
                return _nums.back();
205
478
            }
206
207
190
            double u = (_nums.size() - 1) * quantile;
208
190
            auto index = static_cast<uint32_t>(u);
209
190
            return _nums[index] +
210
190
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
211
668
        } else {
212
194
            DCHECK(_nums.empty());
213
194
            size_t rows = 0;
214
448
            for (const auto& i : _sorted_nums_vec) {
215
448
                rows += i.size();
216
448
            }
217
194
            const bool reverse = quantile > 0.5 && rows > 2;
218
194
            double u = (rows - 1) * quantile;
219
194
            auto index = static_cast<uint32_t>(u);
220
            // if reverse, the step of target should start 0 like not reverse
221
            // so here rows need to minus index + 2
222
            // eg: rows = 10, index = 5
223
            // if not reverse, so the first number loc is 5, the second number loc is 6
224
            // if reverse, so the second number is 3, the first number is 4
225
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
226
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
227
194
            size_t target = reverse ? rows - index - 2 : index;
228
194
            if (quantile == 1) {
229
0
                target = 0;
230
0
            }
231
194
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
232
194
            if (quantile == 1) {
233
0
                return second_number;
234
0
            }
235
194
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
236
194
        }
237
862
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
187
311
    double terminate(double quantile) {
188
311
        if (_sorted_nums_vec.size() <= 1) {
189
266
            if (_sorted_nums_vec.size() == 1) {
190
128
                _nums = std::move(_sorted_nums_vec[0]);
191
128
            }
192
193
266
            if (_nums.empty()) {
194
                // Although set null here, but the value is 0.0 and the call method just
195
                // get val in aggregate_function_percentile_approx.h
196
0
                return 0.0;
197
0
            }
198
199
266
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
200
1
                pdqsort(_nums.begin(), _nums.end());
201
1
            }
202
203
266
            if (quantile == 1 || _nums.size() == 1) {
204
126
                return _nums.back();
205
126
            }
206
207
140
            double u = (_nums.size() - 1) * quantile;
208
140
            auto index = static_cast<uint32_t>(u);
209
140
            return _nums[index] +
210
140
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
211
266
        } else {
212
45
            DCHECK(_nums.empty());
213
45
            size_t rows = 0;
214
126
            for (const auto& i : _sorted_nums_vec) {
215
126
                rows += i.size();
216
126
            }
217
45
            const bool reverse = quantile > 0.5 && rows > 2;
218
45
            double u = (rows - 1) * quantile;
219
45
            auto index = static_cast<uint32_t>(u);
220
            // if reverse, the step of target should start 0 like not reverse
221
            // so here rows need to minus index + 2
222
            // eg: rows = 10, index = 5
223
            // if not reverse, so the first number loc is 5, the second number loc is 6
224
            // if reverse, so the second number is 3, the first number is 4
225
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
226
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
227
45
            size_t target = reverse ? rows - index - 2 : index;
228
45
            if (quantile == 1) {
229
0
                target = 0;
230
0
            }
231
45
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
232
45
            if (quantile == 1) {
233
0
                return second_number;
234
0
            }
235
45
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
236
45
        }
237
311
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
187
60
    double terminate(double quantile) {
188
60
        if (_sorted_nums_vec.size() <= 1) {
189
60
            if (_sorted_nums_vec.size() == 1) {
190
0
                _nums = std::move(_sorted_nums_vec[0]);
191
0
            }
192
193
60
            if (_nums.empty()) {
194
                // Although set null here, but the value is 0.0 and the call method just
195
                // get val in aggregate_function_percentile_approx.h
196
0
                return 0.0;
197
0
            }
198
199
60
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
200
0
                pdqsort(_nums.begin(), _nums.end());
201
0
            }
202
203
60
            if (quantile == 1 || _nums.size() == 1) {
204
48
                return _nums.back();
205
48
            }
206
207
12
            double u = (_nums.size() - 1) * quantile;
208
12
            auto index = static_cast<uint32_t>(u);
209
12
            return _nums[index] +
210
12
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
211
60
        } else {
212
0
            DCHECK(_nums.empty());
213
0
            size_t rows = 0;
214
0
            for (const auto& i : _sorted_nums_vec) {
215
0
                rows += i.size();
216
0
            }
217
0
            const bool reverse = quantile > 0.5 && rows > 2;
218
0
            double u = (rows - 1) * quantile;
219
0
            auto index = static_cast<uint32_t>(u);
220
            // if reverse, the step of target should start 0 like not reverse
221
            // so here rows need to minus index + 2
222
            // eg: rows = 10, index = 5
223
            // if not reverse, so the first number loc is 5, the second number loc is 6
224
            // if reverse, so the second number is 3, the first number is 4
225
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
226
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
227
0
            size_t target = reverse ? rows - index - 2 : index;
228
0
            if (quantile == 1) {
229
0
                target = 0;
230
0
            }
231
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
232
0
            if (quantile == 1) {
233
0
                return second_number;
234
0
            }
235
0
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
236
0
        }
237
60
    }
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
187
266
    double terminate(double quantile) {
188
266
        if (_sorted_nums_vec.size() <= 1) {
189
230
            if (_sorted_nums_vec.size() == 1) {
190
64
                _nums = std::move(_sorted_nums_vec[0]);
191
64
            }
192
193
230
            if (_nums.empty()) {
194
                // Although set null here, but the value is 0.0 and the call method just
195
                // get val in aggregate_function_percentile_approx.h
196
0
                return 0.0;
197
0
            }
198
199
230
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
200
0
                pdqsort(_nums.begin(), _nums.end());
201
0
            }
202
203
230
            if (quantile == 1 || _nums.size() == 1) {
204
60
                return _nums.back();
205
60
            }
206
207
170
            double u = (_nums.size() - 1) * quantile;
208
170
            auto index = static_cast<uint32_t>(u);
209
170
            return _nums[index] +
210
170
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
211
230
        } else {
212
36
            DCHECK(_nums.empty());
213
36
            size_t rows = 0;
214
96
            for (const auto& i : _sorted_nums_vec) {
215
96
                rows += i.size();
216
96
            }
217
36
            const bool reverse = quantile > 0.5 && rows > 2;
218
36
            double u = (rows - 1) * quantile;
219
36
            auto index = static_cast<uint32_t>(u);
220
            // if reverse, the step of target should start 0 like not reverse
221
            // so here rows need to minus index + 2
222
            // eg: rows = 10, index = 5
223
            // if not reverse, so the first number loc is 5, the second number loc is 6
224
            // if reverse, so the second number is 3, the first number is 4
225
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
226
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
227
36
            size_t target = reverse ? rows - index - 2 : index;
228
36
            if (quantile == 1) {
229
0
                target = 0;
230
0
            }
231
36
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
232
36
            if (quantile == 1) {
233
0
                return second_number;
234
0
            }
235
36
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
236
36
        }
237
266
    }
238
239
private:
240
    struct Node {
241
        Ty value;
242
        int array_index;
243
        int64_t element_index;
244
245
2.20k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIhE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIsE4NodessERKS2_
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
245
774
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
245
1.30k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
245
132
        auto operator<=>(const Node& other) const { return value <=> other.value; }
246
    };
247
248
604
    void _convert_sorted_num_vec_to_nums() {
249
604
        size_t rows = 0;
250
1.17k
        for (const auto& i : _sorted_nums_vec) {
251
1.17k
            rows += i.size();
252
1.17k
        }
253
604
        _nums.resize(rows);
254
604
        size_t count = 0;
255
256
604
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
257
1.77k
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
258
1.17k
            if (!_sorted_nums_vec[i].empty()) {
259
1.17k
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
260
1.17k
            }
261
1.17k
        }
262
263
1.87k
        while (!min_heap.empty()) {
264
1.26k
            Node node = min_heap.top();
265
1.26k
            min_heap.pop();
266
1.26k
            _nums[count++] = node.value;
267
1.26k
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
268
96
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
269
96
                min_heap.push(node);
270
96
            }
271
1.26k
        }
272
604
        _sorted_nums_vec.clear();
273
604
    }
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
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
248
604
    void _convert_sorted_num_vec_to_nums() {
249
604
        size_t rows = 0;
250
1.17k
        for (const auto& i : _sorted_nums_vec) {
251
1.17k
            rows += i.size();
252
1.17k
        }
253
604
        _nums.resize(rows);
254
604
        size_t count = 0;
255
256
604
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
257
1.77k
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
258
1.17k
            if (!_sorted_nums_vec[i].empty()) {
259
1.17k
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
260
1.17k
            }
261
1.17k
        }
262
263
1.87k
        while (!min_heap.empty()) {
264
1.26k
            Node node = min_heap.top();
265
1.26k
            min_heap.pop();
266
1.26k
            _nums[count++] = node.value;
267
1.26k
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
268
96
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
269
96
                min_heap.push(node);
270
96
            }
271
1.26k
        }
272
604
        _sorted_nums_vec.clear();
273
604
    }
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
274
275
275
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
276
275
        Ty first_number = 0, second_number = 0;
277
275
        size_t count = 0;
278
275
        if (reverse) {
279
60
            std::priority_queue<Node> max_heap;
280
226
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
281
166
                if (!_sorted_nums_vec[i].empty()) {
282
166
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
283
166
                                     _sorted_nums_vec[i].size() - 1);
284
166
                }
285
166
            }
286
287
248
            while (!max_heap.empty()) {
288
248
                Node node = max_heap.top();
289
248
                max_heap.pop();
290
248
                if (count == target) {
291
60
                    second_number = node.value;
292
188
                } else if (count == target + 1) {
293
60
                    first_number = node.value;
294
60
                    break;
295
60
                }
296
188
                ++count;
297
188
                if (--node.element_index >= 0) {
298
148
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
299
148
                    max_heap.push(node);
300
148
                }
301
188
            }
302
303
215
        } else {
304
215
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
305
719
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
306
504
                if (!_sorted_nums_vec[i].empty()) {
307
504
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
308
504
                }
309
504
            }
310
311
593
            while (!min_heap.empty()) {
312
593
                Node node = min_heap.top();
313
593
                min_heap.pop();
314
593
                if (count == target) {
315
215
                    first_number = node.value;
316
378
                } else if (count == target + 1) {
317
215
                    second_number = node.value;
318
215
                    break;
319
215
                }
320
378
                ++count;
321
378
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
322
270
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
323
270
                    min_heap.push(node);
324
270
                }
325
378
            }
326
215
        }
327
328
275
        return {first_number, second_number};
329
275
    }
Unexecuted instantiation: _ZN5doris6CountsIhE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
275
194
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
276
194
        Ty first_number = 0, second_number = 0;
277
194
        size_t count = 0;
278
194
        if (reverse) {
279
20
            std::priority_queue<Node> max_heap;
280
68
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
281
48
                if (!_sorted_nums_vec[i].empty()) {
282
48
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
283
48
                                     _sorted_nums_vec[i].size() - 1);
284
48
                }
285
48
            }
286
287
78
            while (!max_heap.empty()) {
288
78
                Node node = max_heap.top();
289
78
                max_heap.pop();
290
78
                if (count == target) {
291
20
                    second_number = node.value;
292
58
                } else if (count == target + 1) {
293
20
                    first_number = node.value;
294
20
                    break;
295
20
                }
296
58
                ++count;
297
58
                if (--node.element_index >= 0) {
298
44
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
299
44
                    max_heap.push(node);
300
44
                }
301
58
            }
302
303
174
        } else {
304
174
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
305
574
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
306
400
                if (!_sorted_nums_vec[i].empty()) {
307
400
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
308
400
                }
309
400
            }
310
311
488
            while (!min_heap.empty()) {
312
488
                Node node = min_heap.top();
313
488
                min_heap.pop();
314
488
                if (count == target) {
315
174
                    first_number = node.value;
316
314
                } else if (count == target + 1) {
317
174
                    second_number = node.value;
318
174
                    break;
319
174
                }
320
314
                ++count;
321
314
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
322
232
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
323
232
                    min_heap.push(node);
324
232
                }
325
314
            }
326
174
        }
327
328
194
        return {first_number, second_number};
329
194
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
275
45
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
276
45
        Ty first_number = 0, second_number = 0;
277
45
        size_t count = 0;
278
45
        if (reverse) {
279
36
            std::priority_queue<Node> max_heap;
280
142
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
281
106
                if (!_sorted_nums_vec[i].empty()) {
282
106
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
283
106
                                     _sorted_nums_vec[i].size() - 1);
284
106
                }
285
106
            }
286
287
160
            while (!max_heap.empty()) {
288
160
                Node node = max_heap.top();
289
160
                max_heap.pop();
290
160
                if (count == target) {
291
36
                    second_number = node.value;
292
124
                } else if (count == target + 1) {
293
36
                    first_number = node.value;
294
36
                    break;
295
36
                }
296
124
                ++count;
297
124
                if (--node.element_index >= 0) {
298
100
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
299
100
                    max_heap.push(node);
300
100
                }
301
124
            }
302
303
36
        } else {
304
9
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
305
29
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
306
20
                if (!_sorted_nums_vec[i].empty()) {
307
20
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
308
20
                }
309
20
            }
310
311
25
            while (!min_heap.empty()) {
312
25
                Node node = min_heap.top();
313
25
                min_heap.pop();
314
25
                if (count == target) {
315
9
                    first_number = node.value;
316
16
                } else if (count == target + 1) {
317
9
                    second_number = node.value;
318
9
                    break;
319
9
                }
320
16
                ++count;
321
16
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
322
14
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
323
14
                    min_heap.push(node);
324
14
                }
325
16
            }
326
9
        }
327
328
45
        return {first_number, second_number};
329
45
    }
Unexecuted instantiation: _ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
275
36
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
276
36
        Ty first_number = 0, second_number = 0;
277
36
        size_t count = 0;
278
36
        if (reverse) {
279
4
            std::priority_queue<Node> max_heap;
280
16
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
281
12
                if (!_sorted_nums_vec[i].empty()) {
282
12
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
283
12
                                     _sorted_nums_vec[i].size() - 1);
284
12
                }
285
12
            }
286
287
10
            while (!max_heap.empty()) {
288
10
                Node node = max_heap.top();
289
10
                max_heap.pop();
290
10
                if (count == target) {
291
4
                    second_number = node.value;
292
6
                } else if (count == target + 1) {
293
4
                    first_number = node.value;
294
4
                    break;
295
4
                }
296
6
                ++count;
297
6
                if (--node.element_index >= 0) {
298
4
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
299
4
                    max_heap.push(node);
300
4
                }
301
6
            }
302
303
32
        } else {
304
32
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
305
116
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
306
84
                if (!_sorted_nums_vec[i].empty()) {
307
84
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
308
84
                }
309
84
            }
310
311
80
            while (!min_heap.empty()) {
312
80
                Node node = min_heap.top();
313
80
                min_heap.pop();
314
80
                if (count == target) {
315
32
                    first_number = node.value;
316
48
                } else if (count == target + 1) {
317
32
                    second_number = node.value;
318
32
                    break;
319
32
                }
320
48
                ++count;
321
48
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
322
24
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
323
24
                    min_heap.push(node);
324
24
                }
325
48
            }
326
32
        }
327
328
36
        return {first_number, second_number};
329
36
    }
330
331
    vectorized::PODArray<Ty> _nums;
332
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
333
};
334
335
} // namespace doris