Coverage Report

Created: 2026-10-15 08:29

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
4.42k
inline void check_quantile(double quantile) {
38
4.42k
    if (!std::isfinite(quantile) || quantile < 0 || quantile > 1) {
39
26
        throw Exception(ErrorCode::INVALID_ARGUMENT,
40
26
                        "quantile in func percentile should in [0, 1], but real data is:" +
41
26
                                std::to_string(quantile));
42
26
    }
43
4.42k
}
44
45
template <typename Ty>
46
class Counts {
47
public:
48
957
    Counts() = default;
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
48
86
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIsEC2Ev
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
48
384
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
48
242
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsInEC2Ev
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
48
245
    Counts() = default;
49
50
    // Moves all samples of `other` into this state. `other` may itself be a merged state whose
51
    // samples live in `_sorted_nums_vec`, and either side may still hold unsorted raw samples.
52
385
    void merge(Counts* other) {
53
385
        _move_nums_to_sorted_vec();
54
385
        other->_move_nums_to_sorted_vec();
55
391
        for (auto& nums : other->_sorted_nums_vec) {
56
391
            _sorted_nums_vec.emplace_back(std::move(nums));
57
391
        }
58
385
        other->_sorted_nums_vec.clear();
59
385
    }
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
52
20
    void merge(Counts* other) {
53
20
        _move_nums_to_sorted_vec();
54
20
        other->_move_nums_to_sorted_vec();
55
20
        for (auto& nums : other->_sorted_nums_vec) {
56
20
            _sorted_nums_vec.emplace_back(std::move(nums));
57
20
        }
58
20
        other->_sorted_nums_vec.clear();
59
20
    }
Unexecuted instantiation: _ZN5doris6CountsIsE5mergeEPS1_
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
52
173
    void merge(Counts* other) {
53
173
        _move_nums_to_sorted_vec();
54
173
        other->_move_nums_to_sorted_vec();
55
180
        for (auto& nums : other->_sorted_nums_vec) {
56
180
            _sorted_nums_vec.emplace_back(std::move(nums));
57
180
        }
58
173
        other->_sorted_nums_vec.clear();
59
173
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
52
136
    void merge(Counts* other) {
53
136
        _move_nums_to_sorted_vec();
54
136
        other->_move_nums_to_sorted_vec();
55
136
        for (auto& nums : other->_sorted_nums_vec) {
56
135
            _sorted_nums_vec.emplace_back(std::move(nums));
57
135
        }
58
136
        other->_sorted_nums_vec.clear();
59
136
    }
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
52
56
    void merge(Counts* other) {
53
56
        _move_nums_to_sorted_vec();
54
56
        other->_move_nums_to_sorted_vec();
55
56
        for (auto& nums : other->_sorted_nums_vec) {
56
56
            _sorted_nums_vec.emplace_back(std::move(nums));
57
56
        }
58
56
        other->_sorted_nums_vec.clear();
59
56
    }
60
61
    void increment(Ty key, uint32_t i) {
62
        auto old_size = _nums.size();
63
        _nums.resize(_nums.size() + i);
64
        for (uint32_t j = 0; j < i; ++j) {
65
            _nums[old_size + j] = key;
66
        }
67
    }
68
69
355
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
69
32
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIsE9incrementEs
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
69
119
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
69
44
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsInE9incrementEn
Unexecuted instantiation: _ZN5doris6CountsIfE9incrementEf
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
69
160
    void increment(Ty key) { _nums.push_back(key); }
70
71
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
72
73
349
    void serialize(BufferWritable& buf) {
74
349
        if (_sorted_nums_vec.empty()) {
75
214
            pdqsort(_nums.begin(), _nums.end());
76
214
        } else {
77
            // merge all sorted runs (including the raw samples) into `_nums`
78
135
            _move_nums_to_sorted_vec();
79
135
            _convert_sorted_num_vec_to_nums();
80
135
        }
81
349
        size_t size = _nums.size();
82
349
        buf.write_binary(size);
83
349
        buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
84
349
    }
_ZN5doris6CountsIaE9serializeERNS_14BufferWritableE
Line
Count
Source
73
34
    void serialize(BufferWritable& buf) {
74
34
        if (_sorted_nums_vec.empty()) {
75
32
            pdqsort(_nums.begin(), _nums.end());
76
32
        } else {
77
            // merge all sorted runs (including the raw samples) into `_nums`
78
2
            _move_nums_to_sorted_vec();
79
2
            _convert_sorted_num_vec_to_nums();
80
2
        }
81
34
        size_t size = _nums.size();
82
34
        buf.write_binary(size);
83
34
        buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
84
34
    }
Unexecuted instantiation: _ZN5doris6CountsIsE9serializeERNS_14BufferWritableE
_ZN5doris6CountsIiE9serializeERNS_14BufferWritableE
Line
Count
Source
73
205
    void serialize(BufferWritable& buf) {
74
205
        if (_sorted_nums_vec.empty()) {
75
119
            pdqsort(_nums.begin(), _nums.end());
76
119
        } else {
77
            // merge all sorted runs (including the raw samples) into `_nums`
78
86
            _move_nums_to_sorted_vec();
79
86
            _convert_sorted_num_vec_to_nums();
80
86
        }
81
205
        size_t size = _nums.size();
82
205
        buf.write_binary(size);
83
205
        buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
84
205
    }
_ZN5doris6CountsIlE9serializeERNS_14BufferWritableE
Line
Count
Source
73
73
    void serialize(BufferWritable& buf) {
74
73
        if (_sorted_nums_vec.empty()) {
75
26
            pdqsort(_nums.begin(), _nums.end());
76
47
        } else {
77
            // merge all sorted runs (including the raw samples) into `_nums`
78
47
            _move_nums_to_sorted_vec();
79
47
            _convert_sorted_num_vec_to_nums();
80
47
        }
81
73
        size_t size = _nums.size();
82
73
        buf.write_binary(size);
83
73
        buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
84
73
    }
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_14BufferWritableE
_ZN5doris6CountsIdE9serializeERNS_14BufferWritableE
Line
Count
Source
73
37
    void serialize(BufferWritable& buf) {
74
37
        if (_sorted_nums_vec.empty()) {
75
37
            pdqsort(_nums.begin(), _nums.end());
76
37
        } else {
77
            // merge all sorted runs (including the raw samples) into `_nums`
78
0
            _move_nums_to_sorted_vec();
79
0
            _convert_sorted_num_vec_to_nums();
80
0
        }
81
37
        size_t size = _nums.size();
82
37
        buf.write_binary(size);
83
37
        buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
84
37
    }
85
86
378
    void unserialize(BufferReadable& buf) {
87
378
        size_t size;
88
378
        buf.read_binary(size);
89
378
        _nums.resize(size);
90
378
        auto buff = buf.read(sizeof(Ty) * size);
91
378
        memcpy(_nums.data(), buff.data, buff.size);
92
378
    }
_ZN5doris6CountsIaE11unserializeERNS_14BufferReadableE
Line
Count
Source
86
34
    void unserialize(BufferReadable& buf) {
87
34
        size_t size;
88
34
        buf.read_binary(size);
89
34
        _nums.resize(size);
90
34
        auto buff = buf.read(sizeof(Ty) * size);
91
34
        memcpy(_nums.data(), buff.data, buff.size);
92
34
    }
Unexecuted instantiation: _ZN5doris6CountsIsE11unserializeERNS_14BufferReadableE
_ZN5doris6CountsIiE11unserializeERNS_14BufferReadableE
Line
Count
Source
86
166
    void unserialize(BufferReadable& buf) {
87
166
        size_t size;
88
166
        buf.read_binary(size);
89
166
        _nums.resize(size);
90
166
        auto buff = buf.read(sizeof(Ty) * size);
91
166
        memcpy(_nums.data(), buff.data, buff.size);
92
166
    }
_ZN5doris6CountsIlE11unserializeERNS_14BufferReadableE
Line
Count
Source
86
127
    void unserialize(BufferReadable& buf) {
87
127
        size_t size;
88
127
        buf.read_binary(size);
89
127
        _nums.resize(size);
90
127
        auto buff = buf.read(sizeof(Ty) * size);
91
127
        memcpy(_nums.data(), buff.data, buff.size);
92
127
    }
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_14BufferReadableE
_ZN5doris6CountsIdE11unserializeERNS_14BufferReadableE
Line
Count
Source
86
51
    void unserialize(BufferReadable& buf) {
87
51
        size_t size;
88
51
        buf.read_binary(size);
89
51
        _nums.resize(size);
90
51
        auto buff = buf.read(sizeof(Ty) * size);
91
51
        memcpy(_nums.data(), buff.data, buff.size);
92
51
    }
93
94
165
    double terminate(double quantile) {
95
165
        if (!_sorted_nums_vec.empty()) {
96
76
            _move_nums_to_sorted_vec();
97
76
        }
98
165
        if (_sorted_nums_vec.size() <= 1) {
99
139
            if (_sorted_nums_vec.size() == 1) {
100
50
                _nums = std::move(_sorted_nums_vec[0]);
101
50
                _sorted_nums_vec.clear();
102
50
            }
103
104
139
            if (_nums.empty()) {
105
                // Although set null here, but the value is 0.0 and the call method just
106
                // get val in aggregate_function_percentile_approx.h
107
1
                return 0.0;
108
1
            }
109
110
138
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
111
1
                pdqsort(_nums.begin(), _nums.end());
112
1
            }
113
114
138
            if (quantile == 1 || _nums.size() == 1) {
115
98
                return _nums.back();
116
98
            }
117
118
40
            double u = (_nums.size() - 1) * quantile;
119
40
            auto index = static_cast<uint32_t>(u);
120
40
            return _nums[index] +
121
40
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
122
40
                                                       static_cast<double>(_nums[index]));
123
138
        } else {
124
26
            DCHECK(_nums.empty());
125
26
            size_t rows = 0;
126
56
            for (const auto& i : _sorted_nums_vec) {
127
56
                rows += i.size();
128
56
            }
129
26
            const bool reverse = quantile > 0.5 && rows > 2;
130
26
            double u = (rows - 1) * quantile;
131
26
            auto index = static_cast<uint32_t>(u);
132
            // if reverse, the step of target should start 0 like not reverse
133
            // so here rows need to minus index + 2
134
            // eg: rows = 10, index = 5
135
            // if not reverse, so the first number loc is 5, the second number loc is 6
136
            // if reverse, so the second number is 3, the first number is 4
137
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
138
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
139
26
            size_t target = reverse ? rows - index - 2 : index;
140
26
            if (quantile == 1) {
141
2
                target = 0;
142
2
            }
143
26
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
144
26
            if (quantile == 1) {
145
2
                return second_number;
146
2
            }
147
24
            return first_number +
148
24
                   (u - static_cast<double>(index)) *
149
24
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
150
26
        }
151
165
    }
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
94
4
    double terminate(double quantile) {
95
4
        if (!_sorted_nums_vec.empty()) {
96
4
            _move_nums_to_sorted_vec();
97
4
        }
98
4
        if (_sorted_nums_vec.size() <= 1) {
99
4
            if (_sorted_nums_vec.size() == 1) {
100
4
                _nums = std::move(_sorted_nums_vec[0]);
101
4
                _sorted_nums_vec.clear();
102
4
            }
103
104
4
            if (_nums.empty()) {
105
                // Although set null here, but the value is 0.0 and the call method just
106
                // get val in aggregate_function_percentile_approx.h
107
0
                return 0.0;
108
0
            }
109
110
4
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
111
0
                pdqsort(_nums.begin(), _nums.end());
112
0
            }
113
114
4
            if (quantile == 1 || _nums.size() == 1) {
115
4
                return _nums.back();
116
4
            }
117
118
0
            double u = (_nums.size() - 1) * quantile;
119
0
            auto index = static_cast<uint32_t>(u);
120
0
            return _nums[index] +
121
0
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
122
0
                                                       static_cast<double>(_nums[index]));
123
4
        } else {
124
0
            DCHECK(_nums.empty());
125
0
            size_t rows = 0;
126
0
            for (const auto& i : _sorted_nums_vec) {
127
0
                rows += i.size();
128
0
            }
129
0
            const bool reverse = quantile > 0.5 && rows > 2;
130
0
            double u = (rows - 1) * quantile;
131
0
            auto index = static_cast<uint32_t>(u);
132
            // if reverse, the step of target should start 0 like not reverse
133
            // so here rows need to minus index + 2
134
            // eg: rows = 10, index = 5
135
            // if not reverse, so the first number loc is 5, the second number loc is 6
136
            // if reverse, so the second number is 3, the first number is 4
137
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
138
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
139
0
            size_t target = reverse ? rows - index - 2 : index;
140
0
            if (quantile == 1) {
141
0
                target = 0;
142
0
            }
143
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
144
0
            if (quantile == 1) {
145
0
                return second_number;
146
0
            }
147
0
            return first_number +
148
0
                   (u - static_cast<double>(index)) *
149
0
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
150
0
        }
151
4
    }
Unexecuted instantiation: _ZN5doris6CountsIsE9terminateEd
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
94
6
    double terminate(double quantile) {
95
6
        if (!_sorted_nums_vec.empty()) {
96
6
            _move_nums_to_sorted_vec();
97
6
        }
98
6
        if (_sorted_nums_vec.size() <= 1) {
99
4
            if (_sorted_nums_vec.size() == 1) {
100
4
                _nums = std::move(_sorted_nums_vec[0]);
101
4
                _sorted_nums_vec.clear();
102
4
            }
103
104
4
            if (_nums.empty()) {
105
                // Although set null here, but the value is 0.0 and the call method just
106
                // get val in aggregate_function_percentile_approx.h
107
0
                return 0.0;
108
0
            }
109
110
4
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
111
0
                pdqsort(_nums.begin(), _nums.end());
112
0
            }
113
114
4
            if (quantile == 1 || _nums.size() == 1) {
115
0
                return _nums.back();
116
0
            }
117
118
4
            double u = (_nums.size() - 1) * quantile;
119
4
            auto index = static_cast<uint32_t>(u);
120
4
            return _nums[index] +
121
4
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
122
4
                                                       static_cast<double>(_nums[index]));
123
4
        } else {
124
2
            DCHECK(_nums.empty());
125
2
            size_t rows = 0;
126
4
            for (const auto& i : _sorted_nums_vec) {
127
4
                rows += i.size();
128
4
            }
129
2
            const bool reverse = quantile > 0.5 && rows > 2;
130
2
            double u = (rows - 1) * quantile;
131
2
            auto index = static_cast<uint32_t>(u);
132
            // if reverse, the step of target should start 0 like not reverse
133
            // so here rows need to minus index + 2
134
            // eg: rows = 10, index = 5
135
            // if not reverse, so the first number loc is 5, the second number loc is 6
136
            // if reverse, so the second number is 3, the first number is 4
137
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
138
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
139
2
            size_t target = reverse ? rows - index - 2 : index;
140
2
            if (quantile == 1) {
141
0
                target = 0;
142
0
            }
143
2
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
144
2
            if (quantile == 1) {
145
0
                return second_number;
146
0
            }
147
2
            return first_number +
148
2
                   (u - static_cast<double>(index)) *
149
2
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
150
2
        }
151
6
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
94
45
    double terminate(double quantile) {
95
45
        if (!_sorted_nums_vec.empty()) {
96
33
            _move_nums_to_sorted_vec();
97
33
        }
98
45
        if (_sorted_nums_vec.size() <= 1) {
99
32
            if (_sorted_nums_vec.size() == 1) {
100
20
                _nums = std::move(_sorted_nums_vec[0]);
101
20
                _sorted_nums_vec.clear();
102
20
            }
103
104
32
            if (_nums.empty()) {
105
                // Although set null here, but the value is 0.0 and the call method just
106
                // get val in aggregate_function_percentile_approx.h
107
1
                return 0.0;
108
1
            }
109
110
31
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
111
1
                pdqsort(_nums.begin(), _nums.end());
112
1
            }
113
114
31
            if (quantile == 1 || _nums.size() == 1) {
115
5
                return _nums.back();
116
5
            }
117
118
26
            double u = (_nums.size() - 1) * quantile;
119
26
            auto index = static_cast<uint32_t>(u);
120
26
            return _nums[index] +
121
26
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
122
26
                                                       static_cast<double>(_nums[index]));
123
31
        } else {
124
13
            DCHECK(_nums.empty());
125
13
            size_t rows = 0;
126
30
            for (const auto& i : _sorted_nums_vec) {
127
30
                rows += i.size();
128
30
            }
129
13
            const bool reverse = quantile > 0.5 && rows > 2;
130
13
            double u = (rows - 1) * quantile;
131
13
            auto index = static_cast<uint32_t>(u);
132
            // if reverse, the step of target should start 0 like not reverse
133
            // so here rows need to minus index + 2
134
            // eg: rows = 10, index = 5
135
            // if not reverse, so the first number loc is 5, the second number loc is 6
136
            // if reverse, so the second number is 3, the first number is 4
137
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
138
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
139
13
            size_t target = reverse ? rows - index - 2 : index;
140
13
            if (quantile == 1) {
141
1
                target = 0;
142
1
            }
143
13
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
144
13
            if (quantile == 1) {
145
1
                return second_number;
146
1
            }
147
12
            return first_number +
148
12
                   (u - static_cast<double>(index)) *
149
12
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
150
13
        }
151
45
    }
Unexecuted instantiation: _ZN5doris6CountsInE9terminateEd
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
94
110
    double terminate(double quantile) {
95
110
        if (!_sorted_nums_vec.empty()) {
96
33
            _move_nums_to_sorted_vec();
97
33
        }
98
110
        if (_sorted_nums_vec.size() <= 1) {
99
99
            if (_sorted_nums_vec.size() == 1) {
100
22
                _nums = std::move(_sorted_nums_vec[0]);
101
22
                _sorted_nums_vec.clear();
102
22
            }
103
104
99
            if (_nums.empty()) {
105
                // Although set null here, but the value is 0.0 and the call method just
106
                // get val in aggregate_function_percentile_approx.h
107
0
                return 0.0;
108
0
            }
109
110
99
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
111
0
                pdqsort(_nums.begin(), _nums.end());
112
0
            }
113
114
99
            if (quantile == 1 || _nums.size() == 1) {
115
89
                return _nums.back();
116
89
            }
117
118
10
            double u = (_nums.size() - 1) * quantile;
119
10
            auto index = static_cast<uint32_t>(u);
120
10
            return _nums[index] +
121
10
                   (u - static_cast<double>(index)) * (static_cast<double>(_nums[index + 1]) -
122
10
                                                       static_cast<double>(_nums[index]));
123
99
        } else {
124
11
            DCHECK(_nums.empty());
125
11
            size_t rows = 0;
126
22
            for (const auto& i : _sorted_nums_vec) {
127
22
                rows += i.size();
128
22
            }
129
11
            const bool reverse = quantile > 0.5 && rows > 2;
130
11
            double u = (rows - 1) * quantile;
131
11
            auto index = static_cast<uint32_t>(u);
132
            // if reverse, the step of target should start 0 like not reverse
133
            // so here rows need to minus index + 2
134
            // eg: rows = 10, index = 5
135
            // if not reverse, so the first number loc is 5, the second number loc is 6
136
            // if reverse, so the second number is 3, the first number is 4
137
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
138
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
139
11
            size_t target = reverse ? rows - index - 2 : index;
140
11
            if (quantile == 1) {
141
1
                target = 0;
142
1
            }
143
11
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
144
11
            if (quantile == 1) {
145
1
                return second_number;
146
1
            }
147
10
            return first_number +
148
10
                   (u - static_cast<double>(index)) *
149
10
                           (static_cast<double>(second_number) - static_cast<double>(first_number));
150
11
        }
151
110
    }
152
153
private:
154
    struct Node {
155
        Ty value;
156
        int array_index;
157
        int64_t element_index;
158
159
296
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIsE4NodessERKS2_
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
159
146
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
159
139
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
159
11
        auto operator<=>(const Node& other) const { return value <=> other.value; }
160
    };
161
162
981
    void _move_nums_to_sorted_vec() {
163
981
        if (_nums.empty()) {
164
603
            return;
165
603
        }
166
378
        if (!std::is_sorted(_nums.begin(), _nums.end())) {
167
6
            pdqsort(_nums.begin(), _nums.end());
168
6
        }
169
378
        _sorted_nums_vec.emplace_back(std::move(_nums));
170
378
        DCHECK(_nums.empty());
171
378
    }
_ZN5doris6CountsIaE24_move_nums_to_sorted_vecEv
Line
Count
Source
162
46
    void _move_nums_to_sorted_vec() {
163
46
        if (_nums.empty()) {
164
26
            return;
165
26
        }
166
20
        if (!std::is_sorted(_nums.begin(), _nums.end())) {
167
0
            pdqsort(_nums.begin(), _nums.end());
168
0
        }
169
20
        _sorted_nums_vec.emplace_back(std::move(_nums));
170
        DCHECK(_nums.empty());
171
20
    }
Unexecuted instantiation: _ZN5doris6CountsIsE24_move_nums_to_sorted_vecEv
_ZN5doris6CountsIiE24_move_nums_to_sorted_vecEv
Line
Count
Source
162
438
    void _move_nums_to_sorted_vec() {
163
438
        if (_nums.empty()) {
164
272
            return;
165
272
        }
166
166
        if (!std::is_sorted(_nums.begin(), _nums.end())) {
167
0
            pdqsort(_nums.begin(), _nums.end());
168
0
        }
169
166
        _sorted_nums_vec.emplace_back(std::move(_nums));
170
        DCHECK(_nums.empty());
171
166
    }
_ZN5doris6CountsIlE24_move_nums_to_sorted_vecEv
Line
Count
Source
162
352
    void _move_nums_to_sorted_vec() {
163
352
        if (_nums.empty()) {
164
218
            return;
165
218
        }
166
134
        if (!std::is_sorted(_nums.begin(), _nums.end())) {
167
6
            pdqsort(_nums.begin(), _nums.end());
168
6
        }
169
134
        _sorted_nums_vec.emplace_back(std::move(_nums));
170
        DCHECK(_nums.empty());
171
134
    }
Unexecuted instantiation: _ZN5doris6CountsInE24_move_nums_to_sorted_vecEv
Unexecuted instantiation: _ZN5doris6CountsIfE24_move_nums_to_sorted_vecEv
_ZN5doris6CountsIdE24_move_nums_to_sorted_vecEv
Line
Count
Source
162
145
    void _move_nums_to_sorted_vec() {
163
145
        if (_nums.empty()) {
164
87
            return;
165
87
        }
166
58
        if (!std::is_sorted(_nums.begin(), _nums.end())) {
167
0
            pdqsort(_nums.begin(), _nums.end());
168
0
        }
169
58
        _sorted_nums_vec.emplace_back(std::move(_nums));
170
        DCHECK(_nums.empty());
171
58
    }
172
173
135
    void _convert_sorted_num_vec_to_nums() {
174
135
        size_t rows = 0;
175
252
        for (const auto& i : _sorted_nums_vec) {
176
252
            rows += i.size();
177
252
        }
178
135
        _nums.resize(rows);
179
135
        size_t count = 0;
180
181
135
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
182
387
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
183
252
            if (!_sorted_nums_vec[i].empty()) {
184
252
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
185
252
            }
186
252
        }
187
188
483
        while (!min_heap.empty()) {
189
348
            Node node = min_heap.top();
190
348
            min_heap.pop();
191
348
            _nums[count++] = node.value;
192
348
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
193
96
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
96
                min_heap.push(node);
195
96
            }
196
348
        }
197
135
        _sorted_nums_vec.clear();
198
135
    }
_ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
173
2
    void _convert_sorted_num_vec_to_nums() {
174
2
        size_t rows = 0;
175
2
        for (const auto& i : _sorted_nums_vec) {
176
2
            rows += i.size();
177
2
        }
178
2
        _nums.resize(rows);
179
2
        size_t count = 0;
180
181
2
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
182
4
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
183
2
            if (!_sorted_nums_vec[i].empty()) {
184
2
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
185
2
            }
186
2
        }
187
188
4
        while (!min_heap.empty()) {
189
2
            Node node = min_heap.top();
190
2
            min_heap.pop();
191
2
            _nums[count++] = node.value;
192
2
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
193
0
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
0
                min_heap.push(node);
195
0
            }
196
2
        }
197
2
        _sorted_nums_vec.clear();
198
2
    }
Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv
_ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
173
86
    void _convert_sorted_num_vec_to_nums() {
174
86
        size_t rows = 0;
175
158
        for (const auto& i : _sorted_nums_vec) {
176
158
            rows += i.size();
177
158
        }
178
86
        _nums.resize(rows);
179
86
        size_t count = 0;
180
181
86
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
182
244
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
183
158
            if (!_sorted_nums_vec[i].empty()) {
184
158
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
185
158
            }
186
158
        }
187
188
280
        while (!min_heap.empty()) {
189
194
            Node node = min_heap.top();
190
194
            min_heap.pop();
191
194
            _nums[count++] = node.value;
192
194
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
193
36
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
36
                min_heap.push(node);
195
36
            }
196
194
        }
197
86
        _sorted_nums_vec.clear();
198
86
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
173
47
    void _convert_sorted_num_vec_to_nums() {
174
47
        size_t rows = 0;
175
92
        for (const auto& i : _sorted_nums_vec) {
176
92
            rows += i.size();
177
92
        }
178
47
        _nums.resize(rows);
179
47
        size_t count = 0;
180
181
47
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
182
139
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
183
92
            if (!_sorted_nums_vec[i].empty()) {
184
92
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
185
92
            }
186
92
        }
187
188
199
        while (!min_heap.empty()) {
189
152
            Node node = min_heap.top();
190
152
            min_heap.pop();
191
152
            _nums[count++] = node.value;
192
152
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
193
60
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
194
60
                min_heap.push(node);
195
60
            }
196
152
        }
197
47
        _sorted_nums_vec.clear();
198
47
    }
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
199
200
26
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
201
26
        Ty first_number = 0, second_number = 0;
202
26
        size_t count = 0;
203
26
        if (reverse) {
204
8
            std::priority_queue<Node> max_heap;
205
26
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
206
18
                if (!_sorted_nums_vec[i].empty()) {
207
18
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
208
18
                                     _sorted_nums_vec[i].size() - 1);
209
18
                }
210
18
            }
211
212
22
            while (!max_heap.empty()) {
213
22
                Node node = max_heap.top();
214
22
                max_heap.pop();
215
22
                if (count == target) {
216
8
                    second_number = node.value;
217
14
                } else if (count == target + 1) {
218
8
                    first_number = node.value;
219
8
                    break;
220
8
                }
221
14
                ++count;
222
14
                if (--node.element_index >= 0) {
223
13
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
224
13
                    max_heap.push(node);
225
13
                }
226
14
            }
227
228
18
        } else {
229
18
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
230
56
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
231
38
                if (!_sorted_nums_vec[i].empty()) {
232
38
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
233
38
                }
234
38
            }
235
236
46
            while (!min_heap.empty()) {
237
46
                Node node = min_heap.top();
238
46
                min_heap.pop();
239
46
                if (count == target) {
240
18
                    first_number = node.value;
241
28
                } else if (count == target + 1) {
242
18
                    second_number = node.value;
243
18
                    break;
244
18
                }
245
28
                ++count;
246
28
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
247
16
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
248
16
                    min_heap.push(node);
249
16
                }
250
28
            }
251
18
        }
252
253
26
        return {first_number, second_number};
254
26
    }
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
200
2
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
201
2
        Ty first_number = 0, second_number = 0;
202
2
        size_t count = 0;
203
2
        if (reverse) {
204
2
            std::priority_queue<Node> max_heap;
205
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
206
4
                if (!_sorted_nums_vec[i].empty()) {
207
4
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
208
4
                                     _sorted_nums_vec[i].size() - 1);
209
4
                }
210
4
            }
211
212
10
            while (!max_heap.empty()) {
213
10
                Node node = max_heap.top();
214
10
                max_heap.pop();
215
10
                if (count == target) {
216
2
                    second_number = node.value;
217
8
                } else if (count == target + 1) {
218
2
                    first_number = node.value;
219
2
                    break;
220
2
                }
221
8
                ++count;
222
8
                if (--node.element_index >= 0) {
223
8
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
224
8
                    max_heap.push(node);
225
8
                }
226
8
            }
227
228
2
        } else {
229
0
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
230
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
231
0
                if (!_sorted_nums_vec[i].empty()) {
232
0
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
233
0
                }
234
0
            }
235
236
0
            while (!min_heap.empty()) {
237
0
                Node node = min_heap.top();
238
0
                min_heap.pop();
239
0
                if (count == target) {
240
0
                    first_number = node.value;
241
0
                } else if (count == target + 1) {
242
0
                    second_number = node.value;
243
0
                    break;
244
0
                }
245
0
                ++count;
246
0
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
247
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
248
0
                    min_heap.push(node);
249
0
                }
250
0
            }
251
0
        }
252
253
2
        return {first_number, second_number};
254
2
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
200
13
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
201
13
        Ty first_number = 0, second_number = 0;
202
13
        size_t count = 0;
203
13
        if (reverse) {
204
6
            std::priority_queue<Node> max_heap;
205
20
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
206
14
                if (!_sorted_nums_vec[i].empty()) {
207
14
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
208
14
                                     _sorted_nums_vec[i].size() - 1);
209
14
                }
210
14
            }
211
212
12
            while (!max_heap.empty()) {
213
12
                Node node = max_heap.top();
214
12
                max_heap.pop();
215
12
                if (count == target) {
216
6
                    second_number = node.value;
217
6
                } else if (count == target + 1) {
218
6
                    first_number = node.value;
219
6
                    break;
220
6
                }
221
6
                ++count;
222
6
                if (--node.element_index >= 0) {
223
5
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
224
5
                    max_heap.push(node);
225
5
                }
226
6
            }
227
228
7
        } else {
229
7
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
230
23
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
231
16
                if (!_sorted_nums_vec[i].empty()) {
232
16
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
233
16
                }
234
16
            }
235
236
24
            while (!min_heap.empty()) {
237
24
                Node node = min_heap.top();
238
24
                min_heap.pop();
239
24
                if (count == target) {
240
7
                    first_number = node.value;
241
17
                } else if (count == target + 1) {
242
7
                    second_number = node.value;
243
7
                    break;
244
7
                }
245
17
                ++count;
246
17
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
247
16
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
248
16
                    min_heap.push(node);
249
16
                }
250
17
            }
251
7
        }
252
253
13
        return {first_number, second_number};
254
13
    }
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
200
11
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
201
11
        Ty first_number = 0, second_number = 0;
202
11
        size_t count = 0;
203
11
        if (reverse) {
204
0
            std::priority_queue<Node> max_heap;
205
0
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
206
0
                if (!_sorted_nums_vec[i].empty()) {
207
0
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
208
0
                                     _sorted_nums_vec[i].size() - 1);
209
0
                }
210
0
            }
211
212
0
            while (!max_heap.empty()) {
213
0
                Node node = max_heap.top();
214
0
                max_heap.pop();
215
0
                if (count == target) {
216
0
                    second_number = node.value;
217
0
                } else if (count == target + 1) {
218
0
                    first_number = node.value;
219
0
                    break;
220
0
                }
221
0
                ++count;
222
0
                if (--node.element_index >= 0) {
223
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
224
0
                    max_heap.push(node);
225
0
                }
226
0
            }
227
228
11
        } else {
229
11
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
230
33
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
231
22
                if (!_sorted_nums_vec[i].empty()) {
232
22
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
233
22
                }
234
22
            }
235
236
22
            while (!min_heap.empty()) {
237
22
                Node node = min_heap.top();
238
22
                min_heap.pop();
239
22
                if (count == target) {
240
11
                    first_number = node.value;
241
11
                } else if (count == target + 1) {
242
11
                    second_number = node.value;
243
11
                    break;
244
11
                }
245
11
                ++count;
246
11
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
247
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
248
0
                    min_heap.push(node);
249
0
                }
250
11
            }
251
11
        }
252
253
11
        return {first_number, second_number};
254
11
    }
255
256
    PODArray<Ty> _nums;
257
    std::vector<PODArray<Ty>> _sorted_nums_vec;
258
};
259
260
class PercentileLevels {
261
public:
262
0
    void merge(const PercentileLevels& rhs) {
263
0
        if (rhs.empty()) {
264
0
            return;
265
0
        }
266
0
267
0
        if (empty()) {
268
0
            quantiles = rhs.quantiles;
269
0
            permutation = rhs.permutation;
270
0
            return;
271
0
        }
272
0
273
0
        DCHECK_EQ(quantiles.size(), rhs.quantiles.size());
274
0
        for (size_t i = 0; i < quantiles.size(); ++i) {
275
0
            DCHECK_EQ(quantiles[i], rhs.quantiles[i]);
276
0
        }
277
0
    }
278
279
799
    void write(BufferWritable& buf) const {
280
799
        int size_num = cast_set<int>(quantiles.size());
281
799
        buf.write_binary(size_num);
282
1.48k
        for (const auto& quantile : quantiles) {
283
1.48k
            buf.write_binary(quantile);
284
1.48k
        }
285
799
    }
286
287
824
    void read(BufferReadable& buf) {
288
824
        int size_num = 0;
289
824
        buf.read_binary(size_num);
290
291
824
        quantiles.resize(size_num);
292
824
        permutation.resize(size_num);
293
2.33k
        for (int i = 0; i < size_num; ++i) {
294
1.51k
            buf.read_binary(quantiles[i]);
295
1.51k
            permutation[i] = cast_set<size_t>(i);
296
1.51k
        }
297
824
    }
298
299
1.26k
    void clear() {
300
1.26k
        quantiles.clear();
301
1.26k
        permutation.clear();
302
1.26k
    }
303
304
7.77k
    bool empty() const { return quantiles.empty(); }
305
306
593
    const std::vector<size_t>& get_permutation() const {
307
593
        sort_permutation();
308
593
        return permutation;
309
593
    }
310
311
593
    void sort_permutation() const {
312
593
        pdqsort(permutation.begin(), permutation.end(),
313
818
                [this](size_t lhs, size_t rhs) { return quantiles[lhs] < quantiles[rhs]; });
314
593
    }
315
316
    std::vector<double> quantiles;
317
    mutable std::vector<size_t> permutation;
318
};
319
320
} // namespace doris