Coverage Report

Created: 2025-04-30 07:18

/root/doris/be/src/util/counts.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <pdqsort.h>
21
22
#include <algorithm>
23
#include <cmath>
24
#include <queue>
25
26
#include "udf/udf.h"
27
#include "vec/common/pod_array.h"
28
#include "vec/common/string_buffer.hpp"
29
#include "vec/io/io_helper.h"
30
31
namespace doris {
32
33
template <typename Ty>
34
class Counts {
35
public:
36
2.50k
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIhEC2Ev
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
70
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
231
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
1.27k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
825
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
30
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
76
    Counts() = default;
37
38
847
    void merge(Counts* other) {
39
847
        if (other != nullptr && !other->_nums.empty()) {
40
847
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
847
        }
42
847
    }
Unexecuted instantiation: _ZN5doris6CountsIhE5mergeEPS1_
_ZN5doris6CountsIaE5mergeEPS1_
Line
Count
Source
38
18
    void merge(Counts* other) {
39
18
        if (other != nullptr && !other->_nums.empty()) {
40
18
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
18
        }
42
18
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
38
6
    void merge(Counts* other) {
39
6
        if (other != nullptr && !other->_nums.empty()) {
40
6
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
6
        }
42
6
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
481
    void merge(Counts* other) {
39
481
        if (other != nullptr && !other->_nums.empty()) {
40
481
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
481
        }
42
481
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
334
    void merge(Counts* other) {
39
334
        if (other != nullptr && !other->_nums.empty()) {
40
334
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
334
        }
42
334
    }
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
8
    void merge(Counts* other) {
39
8
        if (other != nullptr && !other->_nums.empty()) {
40
8
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
8
        }
42
8
    }
43
44
    void increment(Ty key, uint32_t i) {
45
        auto old_size = _nums.size();
46
        _nums.resize(_nums.size() + i);
47
        for (uint32_t j = 0; j < i; ++j) {
48
            _nums[old_size + j] = key;
49
        }
50
    }
51
52
2.39k
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIhE9incrementEh
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
52
64
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
52
789
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
52
919
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
52
412
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
52
36
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIfE9incrementEf
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
52
172
    void increment(Ty key) { _nums.push_back(key); }
53
54
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
5
        _nums.insert(keys.begin(), keys.end());
56
5
    }
Unexecuted instantiation: _ZN5doris6CountsIhE15increment_batchERKNS_10vectorized8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_10vectorized8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
_ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Line
Count
Source
54
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
5
        _nums.insert(keys.begin(), keys.end());
56
5
    }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
57
58
1.25k
    void serialize(vectorized::BufferWritable& buf) {
59
1.25k
        if (!_nums.empty()) {
60
957
            pdqsort(_nums.begin(), _nums.end());
61
957
            size_t size = _nums.size();
62
957
            write_binary(size, buf);
63
957
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
957
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
302
            _convert_sorted_num_vec_to_nums();
67
302
            serialize(buf);
68
302
        }
69
1.25k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
18
    void serialize(vectorized::BufferWritable& buf) {
59
18
        if (!_nums.empty()) {
60
18
            pdqsort(_nums.begin(), _nums.end());
61
18
            size_t size = _nums.size();
62
18
            write_binary(size, buf);
63
18
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
18
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
18
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
6
    void serialize(vectorized::BufferWritable& buf) {
59
6
        if (!_nums.empty()) {
60
6
            pdqsort(_nums.begin(), _nums.end());
61
6
            size_t size = _nums.size();
62
6
            write_binary(size, buf);
63
6
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
6
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
6
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
715
    void serialize(vectorized::BufferWritable& buf) {
59
715
        if (!_nums.empty()) {
60
559
            pdqsort(_nums.begin(), _nums.end());
61
559
            size_t size = _nums.size();
62
559
            write_binary(size, buf);
63
559
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
559
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
156
            _convert_sorted_num_vec_to_nums();
67
156
            serialize(buf);
68
156
        }
69
715
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
512
    void serialize(vectorized::BufferWritable& buf) {
59
512
        if (!_nums.empty()) {
60
366
            pdqsort(_nums.begin(), _nums.end());
61
366
            size_t size = _nums.size();
62
366
            write_binary(size, buf);
63
366
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
366
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
146
            _convert_sorted_num_vec_to_nums();
67
146
            serialize(buf);
68
146
        }
69
512
    }
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
8
    void serialize(vectorized::BufferWritable& buf) {
59
8
        if (!_nums.empty()) {
60
8
            pdqsort(_nums.begin(), _nums.end());
61
8
            size_t size = _nums.size();
62
8
            write_binary(size, buf);
63
8
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
8
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
8
    }
70
71
847
    void unserialize(vectorized::BufferReadable& buf) {
72
847
        size_t size;
73
847
        read_binary(size, buf);
74
847
        _nums.resize(size);
75
847
        auto buff = buf.read(sizeof(Ty) * size);
76
847
        memcpy(_nums.data(), buff.data, buff.size);
77
847
    }
Unexecuted instantiation: _ZN5doris6CountsIhE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
18
    void unserialize(vectorized::BufferReadable& buf) {
72
18
        size_t size;
73
18
        read_binary(size, buf);
74
18
        _nums.resize(size);
75
18
        auto buff = buf.read(sizeof(Ty) * size);
76
18
        memcpy(_nums.data(), buff.data, buff.size);
77
18
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
6
    void unserialize(vectorized::BufferReadable& buf) {
72
6
        size_t size;
73
6
        read_binary(size, buf);
74
6
        _nums.resize(size);
75
6
        auto buff = buf.read(sizeof(Ty) * size);
76
6
        memcpy(_nums.data(), buff.data, buff.size);
77
6
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
481
    void unserialize(vectorized::BufferReadable& buf) {
72
481
        size_t size;
73
481
        read_binary(size, buf);
74
481
        _nums.resize(size);
75
481
        auto buff = buf.read(sizeof(Ty) * size);
76
481
        memcpy(_nums.data(), buff.data, buff.size);
77
481
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
334
    void unserialize(vectorized::BufferReadable& buf) {
72
334
        size_t size;
73
334
        read_binary(size, buf);
74
334
        _nums.resize(size);
75
334
        auto buff = buf.read(sizeof(Ty) * size);
76
334
        memcpy(_nums.data(), buff.data, buff.size);
77
334
    }
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
8
    void unserialize(vectorized::BufferReadable& buf) {
72
8
        size_t size;
73
8
        read_binary(size, buf);
74
8
        _nums.resize(size);
75
8
        auto buff = buf.read(sizeof(Ty) * size);
76
8
        memcpy(_nums.data(), buff.data, buff.size);
77
8
    }
78
79
840
    double terminate(double quantile) {
80
840
        if (_sorted_nums_vec.size() <= 1) {
81
789
            if (_sorted_nums_vec.size() == 1) {
82
124
                _nums = std::move(_sorted_nums_vec[0]);
83
124
            }
84
85
789
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
789
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
201
                pdqsort(_nums.begin(), _nums.end());
93
201
            }
94
95
789
            if (quantile == 1 || _nums.size() == 1) {
96
325
                return _nums.back();
97
325
            }
98
99
464
            double u = (_nums.size() - 1) * quantile;
100
464
            auto index = static_cast<uint32_t>(u);
101
464
            return _nums[index] +
102
464
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
789
        } else {
104
51
            DCHECK(_nums.empty());
105
51
            size_t rows = 0;
106
137
            for (const auto& i : _sorted_nums_vec) {
107
137
                rows += i.size();
108
137
            }
109
51
            const bool reverse = quantile > 0.5 && rows > 2;
110
51
            double u = (rows - 1) * quantile;
111
51
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
51
            size_t target = reverse ? rows - index - 2 : index;
120
51
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
51
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
51
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
51
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
51
        }
129
840
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9terminateEd
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
79
58
    double terminate(double quantile) {
80
58
        if (_sorted_nums_vec.size() <= 1) {
81
58
            if (_sorted_nums_vec.size() == 1) {
82
18
                _nums = std::move(_sorted_nums_vec[0]);
83
18
            }
84
85
58
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
58
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
24
                pdqsort(_nums.begin(), _nums.end());
93
24
            }
94
95
58
            if (quantile == 1 || _nums.size() == 1) {
96
34
                return _nums.back();
97
34
            }
98
99
24
            double u = (_nums.size() - 1) * quantile;
100
24
            auto index = static_cast<uint32_t>(u);
101
24
            return _nums[index] +
102
24
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
58
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
0
        }
129
58
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
79
297
    double terminate(double quantile) {
80
297
        if (_sorted_nums_vec.size() <= 1) {
81
294
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
294
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
294
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
165
                pdqsort(_nums.begin(), _nums.end());
93
165
            }
94
95
294
            if (quantile == 1 || _nums.size() == 1) {
96
87
                return _nums.back();
97
87
            }
98
99
207
            double u = (_nums.size() - 1) * quantile;
100
207
            auto index = static_cast<uint32_t>(u);
101
207
            return _nums[index] +
102
207
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
294
        } else {
104
3
            DCHECK(_nums.empty());
105
3
            size_t rows = 0;
106
6
            for (const auto& i : _sorted_nums_vec) {
107
6
                rows += i.size();
108
6
            }
109
3
            const bool reverse = quantile > 0.5 && rows > 2;
110
3
            double u = (rows - 1) * quantile;
111
3
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
3
            size_t target = reverse ? rows - index - 2 : index;
120
3
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
3
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
3
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
3
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
3
        }
129
297
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
231
    double terminate(double quantile) {
80
231
        if (_sorted_nums_vec.size() <= 1) {
81
204
            if (_sorted_nums_vec.size() == 1) {
82
71
                _nums = std::move(_sorted_nums_vec[0]);
83
71
            }
84
85
204
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
204
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
11
                pdqsort(_nums.begin(), _nums.end());
93
11
            }
94
95
204
            if (quantile == 1 || _nums.size() == 1) {
96
96
                return _nums.back();
97
96
            }
98
99
108
            double u = (_nums.size() - 1) * quantile;
100
108
            auto index = static_cast<uint32_t>(u);
101
108
            return _nums[index] +
102
108
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
204
        } else {
104
27
            DCHECK(_nums.empty());
105
27
            size_t rows = 0;
106
71
            for (const auto& i : _sorted_nums_vec) {
107
71
                rows += i.size();
108
71
            }
109
27
            const bool reverse = quantile > 0.5 && rows > 2;
110
27
            double u = (rows - 1) * quantile;
111
27
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
27
            size_t target = reverse ? rows - index - 2 : index;
120
27
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
27
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
27
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
27
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
27
        }
129
231
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
118
    double terminate(double quantile) {
80
118
        if (_sorted_nums_vec.size() <= 1) {
81
97
            if (_sorted_nums_vec.size() == 1) {
82
27
                _nums = std::move(_sorted_nums_vec[0]);
83
27
            }
84
85
97
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
97
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
97
            if (quantile == 1 || _nums.size() == 1) {
96
60
                return _nums.back();
97
60
            }
98
99
37
            double u = (_nums.size() - 1) * quantile;
100
37
            auto index = static_cast<uint32_t>(u);
101
37
            return _nums[index] +
102
37
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
97
        } else {
104
21
            DCHECK(_nums.empty());
105
21
            size_t rows = 0;
106
60
            for (const auto& i : _sorted_nums_vec) {
107
60
                rows += i.size();
108
60
            }
109
21
            const bool reverse = quantile > 0.5 && rows > 2;
110
21
            double u = (rows - 1) * quantile;
111
21
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
21
            size_t target = reverse ? rows - index - 2 : index;
120
21
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
21
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
21
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
21
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
21
        }
129
118
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
79
30
    double terminate(double quantile) {
80
30
        if (_sorted_nums_vec.size() <= 1) {
81
30
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
30
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
30
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
30
            if (quantile == 1 || _nums.size() == 1) {
96
24
                return _nums.back();
97
24
            }
98
99
6
            double u = (_nums.size() - 1) * quantile;
100
6
            auto index = static_cast<uint32_t>(u);
101
6
            return _nums[index] +
102
6
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
30
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
0
        }
129
30
    }
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
79
106
    double terminate(double quantile) {
80
106
        if (_sorted_nums_vec.size() <= 1) {
81
106
            if (_sorted_nums_vec.size() == 1) {
82
8
                _nums = std::move(_sorted_nums_vec[0]);
83
8
            }
84
85
106
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
106
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
106
            if (quantile == 1 || _nums.size() == 1) {
96
24
                return _nums.back();
97
24
            }
98
99
82
            double u = (_nums.size() - 1) * quantile;
100
82
            auto index = static_cast<uint32_t>(u);
101
82
            return _nums[index] +
102
82
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
106
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
0
        }
129
106
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
851
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIhE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
137
19
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
467
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
365
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_
138
    };
139
140
302
    void _convert_sorted_num_vec_to_nums() {
141
302
        size_t rows = 0;
142
586
        for (const auto& i : _sorted_nums_vec) {
143
586
            rows += i.size();
144
586
        }
145
302
        _nums.resize(rows);
146
302
        size_t count = 0;
147
148
302
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
888
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
586
            if (!_sorted_nums_vec[i].empty()) {
151
586
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
586
            }
153
586
        }
154
155
936
        while (!min_heap.empty()) {
156
634
            Node node = min_heap.top();
157
634
            min_heap.pop();
158
634
            _nums[count++] = node.value;
159
634
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
48
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
48
                min_heap.push(node);
162
48
            }
163
634
        }
164
302
        _sorted_nums_vec.clear();
165
302
    }
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
_ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
156
    void _convert_sorted_num_vec_to_nums() {
141
156
        size_t rows = 0;
142
339
        for (const auto& i : _sorted_nums_vec) {
143
339
            rows += i.size();
144
339
        }
145
156
        _nums.resize(rows);
146
156
        size_t count = 0;
147
148
156
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
495
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
339
            if (!_sorted_nums_vec[i].empty()) {
151
339
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
339
            }
153
339
        }
154
155
531
        while (!min_heap.empty()) {
156
375
            Node node = min_heap.top();
157
375
            min_heap.pop();
158
375
            _nums[count++] = node.value;
159
375
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
36
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
36
                min_heap.push(node);
162
36
            }
163
375
        }
164
156
        _sorted_nums_vec.clear();
165
156
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
146
    void _convert_sorted_num_vec_to_nums() {
141
146
        size_t rows = 0;
142
247
        for (const auto& i : _sorted_nums_vec) {
143
247
            rows += i.size();
144
247
        }
145
146
        _nums.resize(rows);
146
146
        size_t count = 0;
147
148
146
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
393
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
247
            if (!_sorted_nums_vec[i].empty()) {
151
247
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
247
            }
153
247
        }
154
155
405
        while (!min_heap.empty()) {
156
259
            Node node = min_heap.top();
157
259
            min_heap.pop();
158
259
            _nums[count++] = node.value;
159
259
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
12
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
12
                min_heap.push(node);
162
12
            }
163
259
        }
164
146
        _sorted_nums_vec.clear();
165
146
    }
Unexecuted instantiation: _ZN5doris6CountsInE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIfE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIdE31_convert_sorted_num_vec_to_numsEv
166
167
51
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
51
        Ty first_number = 0, second_number = 0;
169
51
        size_t count = 0;
170
51
        if (reverse) {
171
21
            std::priority_queue<Node> max_heap;
172
83
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
62
                if (!_sorted_nums_vec[i].empty()) {
174
62
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
62
                                     _sorted_nums_vec[i].size() - 1);
176
62
                }
177
62
            }
178
179
107
            while (!max_heap.empty()) {
180
107
                Node node = max_heap.top();
181
107
                max_heap.pop();
182
107
                if (count == target) {
183
21
                    second_number = node.value;
184
86
                } else if (count == target + 1) {
185
21
                    first_number = node.value;
186
21
                    break;
187
21
                }
188
86
                ++count;
189
86
                if (--node.element_index >= 0) {
190
75
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
75
                    max_heap.push(node);
192
75
                }
193
86
            }
194
195
30
        } else {
196
30
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
105
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
75
                if (!_sorted_nums_vec[i].empty()) {
199
75
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
75
                }
201
75
            }
202
203
108
            while (!min_heap.empty()) {
204
108
                Node node = min_heap.top();
205
108
                min_heap.pop();
206
108
                if (count == target) {
207
30
                    first_number = node.value;
208
78
                } else if (count == target + 1) {
209
30
                    second_number = node.value;
210
30
                    break;
211
30
                }
212
78
                ++count;
213
78
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
63
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
63
                    min_heap.push(node);
216
63
                }
217
78
            }
218
30
        }
219
220
51
        return {first_number, second_number};
221
51
    }
Unexecuted instantiation: _ZN5doris6CountsIhE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
_ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
3
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
3
        Ty first_number = 0, second_number = 0;
169
3
        size_t count = 0;
170
3
        if (reverse) {
171
1
            std::priority_queue<Node> max_heap;
172
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
2
                if (!_sorted_nums_vec[i].empty()) {
174
2
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
2
                                     _sorted_nums_vec[i].size() - 1);
176
2
                }
177
2
            }
178
179
6
            while (!max_heap.empty()) {
180
6
                Node node = max_heap.top();
181
6
                max_heap.pop();
182
6
                if (count == target) {
183
1
                    second_number = node.value;
184
5
                } else if (count == target + 1) {
185
1
                    first_number = node.value;
186
1
                    break;
187
1
                }
188
5
                ++count;
189
5
                if (--node.element_index >= 0) {
190
5
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
5
                    max_heap.push(node);
192
5
                }
193
5
            }
194
195
2
        } else {
196
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
6
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
4
                if (!_sorted_nums_vec[i].empty()) {
199
4
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
4
                }
201
4
            }
202
203
13
            while (!min_heap.empty()) {
204
13
                Node node = min_heap.top();
205
13
                min_heap.pop();
206
13
                if (count == target) {
207
2
                    first_number = node.value;
208
11
                } else if (count == target + 1) {
209
2
                    second_number = node.value;
210
2
                    break;
211
2
                }
212
11
                ++count;
213
11
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
11
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
11
                    min_heap.push(node);
216
11
                }
217
11
            }
218
2
        }
219
220
3
        return {first_number, second_number};
221
3
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
27
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
27
        Ty first_number = 0, second_number = 0;
169
27
        size_t count = 0;
170
27
        if (reverse) {
171
3
            std::priority_queue<Node> max_heap;
172
12
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
9
                if (!_sorted_nums_vec[i].empty()) {
174
9
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
9
                                     _sorted_nums_vec[i].size() - 1);
176
9
                }
177
9
            }
178
179
24
            while (!max_heap.empty()) {
180
24
                Node node = max_heap.top();
181
24
                max_heap.pop();
182
24
                if (count == target) {
183
3
                    second_number = node.value;
184
21
                } else if (count == target + 1) {
185
3
                    first_number = node.value;
186
3
                    break;
187
3
                }
188
21
                ++count;
189
21
                if (--node.element_index >= 0) {
190
21
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
21
                    max_heap.push(node);
192
21
                }
193
21
            }
194
195
24
        } else {
196
24
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
86
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
62
                if (!_sorted_nums_vec[i].empty()) {
199
62
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
62
                }
201
62
            }
202
203
81
            while (!min_heap.empty()) {
204
81
                Node node = min_heap.top();
205
81
                min_heap.pop();
206
81
                if (count == target) {
207
24
                    first_number = node.value;
208
57
                } else if (count == target + 1) {
209
24
                    second_number = node.value;
210
24
                    break;
211
24
                }
212
57
                ++count;
213
57
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
43
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
43
                    min_heap.push(node);
216
43
                }
217
57
            }
218
24
        }
219
220
27
        return {first_number, second_number};
221
27
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
21
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
21
        Ty first_number = 0, second_number = 0;
169
21
        size_t count = 0;
170
21
        if (reverse) {
171
17
            std::priority_queue<Node> max_heap;
172
68
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
51
                if (!_sorted_nums_vec[i].empty()) {
174
51
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
51
                                     _sorted_nums_vec[i].size() - 1);
176
51
                }
177
51
            }
178
179
77
            while (!max_heap.empty()) {
180
77
                Node node = max_heap.top();
181
77
                max_heap.pop();
182
77
                if (count == target) {
183
17
                    second_number = node.value;
184
60
                } else if (count == target + 1) {
185
17
                    first_number = node.value;
186
17
                    break;
187
17
                }
188
60
                ++count;
189
60
                if (--node.element_index >= 0) {
190
49
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
49
                    max_heap.push(node);
192
49
                }
193
60
            }
194
195
17
        } else {
196
4
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
13
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
9
                if (!_sorted_nums_vec[i].empty()) {
199
9
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
9
                }
201
9
            }
202
203
14
            while (!min_heap.empty()) {
204
14
                Node node = min_heap.top();
205
14
                min_heap.pop();
206
14
                if (count == target) {
207
4
                    first_number = node.value;
208
10
                } else if (count == target + 1) {
209
4
                    second_number = node.value;
210
4
                    break;
211
4
                }
212
10
                ++count;
213
10
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
9
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
9
                    min_heap.push(node);
216
9
                }
217
10
            }
218
4
        }
219
220
21
        return {first_number, second_number};
221
21
    }
Unexecuted instantiation: _ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
222
223
    vectorized::PODArray<Ty> _nums;
224
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
225
};
226
227
} // namespace doris