Coverage Report

Created: 2026-04-20 11:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/percentile_util.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 <cstdint>
25
#include <queue>
26
#include <string>
27
#include <vector>
28
29
#include "common/cast_set.h"
30
#include "common/exception.h"
31
#include "core/column/column_nullable.h"
32
#include "core/pod_array.h"
33
#include "core/string_buffer.hpp"
34
35
namespace doris {
36
37
2.00k
inline void check_quantile(double quantile) {
38
2.00k
    if (quantile < 0 || quantile > 1) {
39
17
        throw Exception(ErrorCode::INVALID_ARGUMENT,
40
17
                        "quantile in func percentile should in [0, 1], but real data is:" +
41
17
                                std::to_string(quantile));
42
17
    }
43
2.00k
}
44
45
template <typename Ty>
46
class Counts {
47
public:
48
13
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIaEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIsEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIiEC2Ev
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
48
13
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsInEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIdEC2Ev
49
50
4
    void merge(Counts* other) {
51
4
        if (other != nullptr && !other->_nums.empty()) {
52
4
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
53
4
        }
54
4
    }
Unexecuted instantiation: _ZN5doris6CountsIaE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIsE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIiE5mergeEPS1_
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
50
4
    void merge(Counts* other) {
51
4
        if (other != nullptr && !other->_nums.empty()) {
52
4
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
53
4
        }
54
4
    }
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIdE5mergeEPS1_
55
56
    void increment(Ty key, uint32_t i) {
57
        auto old_size = _nums.size();
58
        _nums.resize(_nums.size() + i);
59
        for (uint32_t j = 0; j < i; ++j) {
60
            _nums[old_size + j] = key;
61
        }
62
    }
63
64
5
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIaE9incrementEa
Unexecuted instantiation: _ZN5doris6CountsIsE9incrementEs
Unexecuted instantiation: _ZN5doris6CountsIiE9incrementEi
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
64
5
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsInE9incrementEn
Unexecuted instantiation: _ZN5doris6CountsIfE9incrementEf
Unexecuted instantiation: _ZN5doris6CountsIdE9incrementEd
65
66
0
    void increment_batch(const PaddedPODArray<Ty>& keys) { _nums.insert(keys.begin(), keys.end()); }
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIlE15increment_batchERKNS_8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
67
68
6
    void serialize(BufferWritable& buf) {
69
6
        if (!_nums.empty()) {
70
5
            pdqsort(_nums.begin(), _nums.end());
71
5
            size_t size = _nums.size();
72
5
            buf.write_binary(size);
73
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
74
5
        } else {
75
            // convert _sorted_nums_vec to _nums and do seiralize again
76
1
            _convert_sorted_num_vec_to_nums();
77
1
            serialize(buf);
78
1
        }
79
6
    }
Unexecuted instantiation: _ZN5doris6CountsIaE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIsE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIiE9serializeERNS_14BufferWritableE
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
68
6
    void serialize(BufferWritable& buf) {
69
6
        if (!_nums.empty()) {
70
5
            pdqsort(_nums.begin(), _nums.end());
71
5
            size_t size = _nums.size();
72
5
            buf.write_binary(size);
73
5
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
74
5
        } else {
75
            // convert _sorted_nums_vec to _nums and do seiralize again
76
1
            _convert_sorted_num_vec_to_nums();
77
1
            serialize(buf);
78
1
        }
79
6
    }
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIdE9serializeERNS_14BufferWritableE
80
81
5
    void unserialize(BufferReadable& buf) {
82
5
        size_t size;
83
5
        buf.read_binary(size);
84
5
        _nums.resize(size);
85
5
        auto buff = buf.read(sizeof(Ty) * size);
86
5
        memcpy(_nums.data(), buff.data, buff.size);
87
5
    }
Unexecuted instantiation: _ZN5doris6CountsIaE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIsE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIiE11unserializeERNS_14BufferReadableE
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
81
5
    void unserialize(BufferReadable& buf) {
82
5
        size_t size;
83
5
        buf.read_binary(size);
84
5
        _nums.resize(size);
85
5
        auto buff = buf.read(sizeof(Ty) * size);
86
5
        memcpy(_nums.data(), buff.data, buff.size);
87
5
    }
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIdE11unserializeERNS_14BufferReadableE
88
89
10
    double terminate(double quantile) {
90
10
        if (_sorted_nums_vec.size() <= 1) {
91
9
            if (_sorted_nums_vec.size() == 1) {
92
0
                _nums = std::move(_sorted_nums_vec[0]);
93
0
            }
94
95
9
            if (_nums.empty()) {
96
                // Although set null here, but the value is 0.0 and the call method just
97
                // get val in aggregate_function_percentile_approx.h
98
1
                return 0.0;
99
1
            }
100
101
8
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
102
1
                pdqsort(_nums.begin(), _nums.end());
103
1
            }
104
105
8
            if (quantile == 1 || _nums.size() == 1) {
106
4
                return _nums.back();
107
4
            }
108
109
4
            double u = (_nums.size() - 1) * quantile;
110
4
            auto index = static_cast<uint32_t>(u);
111
4
            return _nums[index] +
112
4
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
113
4
                                                       static_cast<double>(_nums[index]));
114
8
        } else {
115
1
            DCHECK(_nums.empty());
116
1
            size_t rows = 0;
117
2
            for (const auto& i : _sorted_nums_vec) {
118
2
                rows += i.size();
119
2
            }
120
1
            const bool reverse = quantile > 0.5 && rows > 2;
121
1
            double u = (rows - 1) * quantile;
122
1
            auto index = static_cast<uint32_t>(u);
123
            // if reverse, the step of target should start 0 like not reverse
124
            // so here rows need to minus index + 2
125
            // eg: rows = 10, index = 5
126
            // if not reverse, so the first number loc is 5, the second number loc is 6
127
            // if reverse, so the second number is 3, the first number is 4
128
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
129
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
130
1
            size_t target = reverse ? rows - index - 2 : index;
131
1
            if (quantile == 1) {
132
0
                target = 0;
133
0
            }
134
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
135
1
            if (quantile == 1) {
136
0
                return second_number;
137
0
            }
138
1
            return first_number +
139
1
                   (u - static_cast<double>(index)) *
140
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
141
1
        }
142
10
    }
Unexecuted instantiation: _ZN5doris6CountsIaE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIsE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIiE9terminateEd
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
89
10
    double terminate(double quantile) {
90
10
        if (_sorted_nums_vec.size() <= 1) {
91
9
            if (_sorted_nums_vec.size() == 1) {
92
0
                _nums = std::move(_sorted_nums_vec[0]);
93
0
            }
94
95
9
            if (_nums.empty()) {
96
                // Although set null here, but the value is 0.0 and the call method just
97
                // get val in aggregate_function_percentile_approx.h
98
1
                return 0.0;
99
1
            }
100
101
8
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
102
1
                pdqsort(_nums.begin(), _nums.end());
103
1
            }
104
105
8
            if (quantile == 1 || _nums.size() == 1) {
106
4
                return _nums.back();
107
4
            }
108
109
4
            double u = (_nums.size() - 1) * quantile;
110
4
            auto index = static_cast<uint32_t>(u);
111
4
            return _nums[index] +
112
4
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
113
4
                                                       static_cast<double>(_nums[index]));
114
8
        } else {
115
1
            DCHECK(_nums.empty());
116
1
            size_t rows = 0;
117
2
            for (const auto& i : _sorted_nums_vec) {
118
2
                rows += i.size();
119
2
            }
120
1
            const bool reverse = quantile > 0.5 && rows > 2;
121
1
            double u = (rows - 1) * quantile;
122
1
            auto index = static_cast<uint32_t>(u);
123
            // if reverse, the step of target should start 0 like not reverse
124
            // so here rows need to minus index + 2
125
            // eg: rows = 10, index = 5
126
            // if not reverse, so the first number loc is 5, the second number loc is 6
127
            // if reverse, so the second number is 3, the first number is 4
128
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
129
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
130
1
            size_t target = reverse ? rows - index - 2 : index;
131
1
            if (quantile == 1) {
132
0
                target = 0;
133
0
            }
134
1
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
135
1
            if (quantile == 1) {
136
0
                return second_number;
137
0
            }
138
1
            return first_number +
139
1
                   (u - static_cast<double>(index)) *
140
1
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
141
1
        }
142
10
    }
Unexecuted instantiation: _ZN5doris6CountsInE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIdE9terminateEd
143
144
private:
145
    struct Node {
146
        Ty value;
147
        int array_index;
148
        int64_t element_index;
149
150
9
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIsE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIiE4NodessERKS2_
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
150
9
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_
151
    };
152
153
1
    void _convert_sorted_num_vec_to_nums() {
154
1
        size_t rows = 0;
155
2
        for (const auto& i : _sorted_nums_vec) {
156
2
            rows += i.size();
157
2
        }
158
1
        _nums.resize(rows);
159
1
        size_t count = 0;
160
161
1
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
162
3
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
163
2
            if (!_sorted_nums_vec[i].empty()) {
164
2
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
165
2
            }
166
2
        }
167
168
5
        while (!min_heap.empty()) {
169
4
            Node node = min_heap.top();
170
4
            min_heap.pop();
171
4
            _nums[count++] = node.value;
172
4
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
173
2
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
174
2
                min_heap.push(node);
175
2
            }
176
4
        }
177
1
        _sorted_nums_vec.clear();
178
1
    }
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
153
1
    void _convert_sorted_num_vec_to_nums() {
154
1
        size_t rows = 0;
155
2
        for (const auto& i : _sorted_nums_vec) {
156
2
            rows += i.size();
157
2
        }
158
1
        _nums.resize(rows);
159
1
        size_t count = 0;
160
161
1
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
162
3
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
163
2
            if (!_sorted_nums_vec[i].empty()) {
164
2
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
165
2
            }
166
2
        }
167
168
5
        while (!min_heap.empty()) {
169
4
            Node node = min_heap.top();
170
4
            min_heap.pop();
171
4
            _nums[count++] = node.value;
172
4
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
173
2
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
174
2
                min_heap.push(node);
175
2
            }
176
4
        }
177
1
        _sorted_nums_vec.clear();
178
1
    }
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
179
180
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
181
1
        Ty first_number = 0, second_number = 0;
182
1
        size_t count = 0;
183
1
        if (reverse) {
184
0
            std::priority_queue<Node> max_heap;
185
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
186
0
                if (!_sorted_nums_vec[i].empty()) {
187
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
188
0
                                     _sorted_nums_vec[i].size() - 1);
189
0
                }
190
0
            }
191
192
0
            while (!max_heap.empty()) {
193
0
                Node node = max_heap.top();
194
0
                max_heap.pop();
195
0
                if (count == target) {
196
0
                    second_number = node.value;
197
0
                } else if (count == target + 1) {
198
0
                    first_number = node.value;
199
0
                    break;
200
0
                }
201
0
                ++count;
202
0
                if (--node.element_index >= 0) {
203
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
204
0
                    max_heap.push(node);
205
0
                }
206
0
            }
207
208
1
        } else {
209
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
210
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
211
2
                if (!_sorted_nums_vec[i].empty()) {
212
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
213
2
                }
214
2
            }
215
216
7
            while (!min_heap.empty()) {
217
7
                Node node = min_heap.top();
218
7
                min_heap.pop();
219
7
                if (count == target) {
220
1
                    first_number = node.value;
221
6
                } else if (count == target + 1) {
222
1
                    second_number = node.value;
223
1
                    break;
224
1
                }
225
6
                ++count;
226
6
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
227
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
228
6
                    min_heap.push(node);
229
6
                }
230
6
            }
231
1
        }
232
233
1
        return {first_number, second_number};
234
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
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
180
1
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
181
1
        Ty first_number = 0, second_number = 0;
182
1
        size_t count = 0;
183
1
        if (reverse) {
184
0
            std::priority_queue<Node> max_heap;
185
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
186
0
                if (!_sorted_nums_vec[i].empty()) {
187
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
188
0
                                     _sorted_nums_vec[i].size() - 1);
189
0
                }
190
0
            }
191
192
0
            while (!max_heap.empty()) {
193
0
                Node node = max_heap.top();
194
0
                max_heap.pop();
195
0
                if (count == target) {
196
0
                    second_number = node.value;
197
0
                } else if (count == target + 1) {
198
0
                    first_number = node.value;
199
0
                    break;
200
0
                }
201
0
                ++count;
202
0
                if (--node.element_index >= 0) {
203
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
204
0
                    max_heap.push(node);
205
0
                }
206
0
            }
207
208
1
        } else {
209
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
210
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
211
2
                if (!_sorted_nums_vec[i].empty()) {
212
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
213
2
                }
214
2
            }
215
216
7
            while (!min_heap.empty()) {
217
7
                Node node = min_heap.top();
218
7
                min_heap.pop();
219
7
                if (count == target) {
220
1
                    first_number = node.value;
221
6
                } else if (count == target + 1) {
222
1
                    second_number = node.value;
223
1
                    break;
224
1
                }
225
6
                ++count;
226
6
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
227
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
228
6
                    min_heap.push(node);
229
6
                }
230
6
            }
231
1
        }
232
233
1
        return {first_number, second_number};
234
1
    }
Unexecuted instantiation: _ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
235
236
    PODArray<Ty> _nums;
237
    std::vector<PODArray<Ty>> _sorted_nums_vec;
238
};
239
240
class PercentileLevels {
241
public:
242
344
    void merge(const PercentileLevels& rhs) {
243
344
        if (rhs.empty()) {
244
1
            return;
245
1
        }
246
247
343
        if (empty()) {
248
1
            quantiles = rhs.quantiles;
249
1
            permutation = rhs.permutation;
250
1
            return;
251
1
        }
252
253
343
        DCHECK_EQ(quantiles.size(), rhs.quantiles.size());
254
916
        for (size_t i = 0; i < quantiles.size(); ++i) {
255
574
            DCHECK_EQ(quantiles[i], rhs.quantiles[i]);
256
574
        }
257
342
    }
258
259
801
    void write(BufferWritable& buf) const {
260
801
        int size_num = cast_set<int>(quantiles.size());
261
801
        buf.write_binary(size_num);
262
1.41k
        for (const auto& quantile : quantiles) {
263
1.41k
            buf.write_binary(quantile);
264
1.41k
        }
265
801
    }
266
267
763
    void read(BufferReadable& buf) {
268
763
        int size_num = 0;
269
763
        buf.read_binary(size_num);
270
271
763
        quantiles.resize(size_num);
272
763
        permutation.resize(size_num);
273
2.06k
        for (int i = 0; i < size_num; ++i) {
274
1.29k
            buf.read_binary(quantiles[i]);
275
1.29k
            permutation[i] = cast_set<size_t>(i);
276
1.29k
        }
277
763
    }
278
279
980
    void clear() {
280
980
        quantiles.clear();
281
980
        permutation.clear();
282
980
    }
283
284
2.99k
    bool empty() const { return quantiles.empty(); }
285
286
129
    const std::vector<size_t>& get_permutation() const {
287
129
        sort_permutation();
288
129
        return permutation;
289
129
    }
290
291
129
    void sort_permutation() const {
292
129
        pdqsort(permutation.begin(), permutation.end(),
293
209
                [this](size_t lhs, size_t rhs) { return quantiles[lhs] < quantiles[rhs]; });
294
129
    }
295
296
    std::vector<double> quantiles;
297
    mutable std::vector<size_t> permutation;
298
};
299
300
} // namespace doris