Coverage Report

Created: 2025-05-06 10:17

/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.56k
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIhEC2Ev
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
34
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
243
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
1.32k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
847
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
30
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
84
    Counts() = default;
37
38
871
    void merge(Counts* other) {
39
871
        if (other != nullptr && !other->_nums.empty()) {
40
871
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
871
        }
42
871
    }
Unexecuted instantiation: _ZN5doris6CountsIhE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIaE5mergeEPS1_
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
38
12
    void merge(Counts* other) {
39
12
        if (other != nullptr && !other->_nums.empty()) {
40
12
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
12
        }
42
12
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
506
    void merge(Counts* other) {
39
506
        if (other != nullptr && !other->_nums.empty()) {
40
506
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
506
        }
42
506
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
345
    void merge(Counts* other) {
39
345
        if (other != nullptr && !other->_nums.empty()) {
40
345
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
345
        }
42
345
    }
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.32k
    void serialize(vectorized::BufferWritable& buf) {
59
1.32k
        if (!_nums.empty()) {
60
981
            pdqsort(_nums.begin(), _nums.end());
61
981
            size_t size = _nums.size();
62
981
            write_binary(size, buf);
63
981
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
981
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
339
            _convert_sorted_num_vec_to_nums();
67
339
            serialize(buf);
68
339
        }
69
1.32k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
12
    void serialize(vectorized::BufferWritable& buf) {
59
12
        if (!_nums.empty()) {
60
12
            pdqsort(_nums.begin(), _nums.end());
61
12
            size_t size = _nums.size();
62
12
            write_binary(size, buf);
63
12
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
12
        } 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
12
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
767
    void serialize(vectorized::BufferWritable& buf) {
59
767
        if (!_nums.empty()) {
60
584
            pdqsort(_nums.begin(), _nums.end());
61
584
            size_t size = _nums.size();
62
584
            write_binary(size, buf);
63
584
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
584
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
183
            _convert_sorted_num_vec_to_nums();
67
183
            serialize(buf);
68
183
        }
69
767
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
533
    void serialize(vectorized::BufferWritable& buf) {
59
533
        if (!_nums.empty()) {
60
377
            pdqsort(_nums.begin(), _nums.end());
61
377
            size_t size = _nums.size();
62
377
            write_binary(size, buf);
63
377
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
377
        } 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
533
    }
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
871
    void unserialize(vectorized::BufferReadable& buf) {
72
871
        size_t size;
73
871
        read_binary(size, buf);
74
871
        _nums.resize(size);
75
871
        auto buff = buf.read(sizeof(Ty) * size);
76
871
        memcpy(_nums.data(), buff.data, buff.size);
77
871
    }
Unexecuted instantiation: _ZN5doris6CountsIhE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
12
    void unserialize(vectorized::BufferReadable& buf) {
72
12
        size_t size;
73
12
        read_binary(size, buf);
74
12
        _nums.resize(size);
75
12
        auto buff = buf.read(sizeof(Ty) * size);
76
12
        memcpy(_nums.data(), buff.data, buff.size);
77
12
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
506
    void unserialize(vectorized::BufferReadable& buf) {
72
506
        size_t size;
73
506
        read_binary(size, buf);
74
506
        _nums.resize(size);
75
506
        auto buff = buf.read(sizeof(Ty) * size);
76
506
        memcpy(_nums.data(), buff.data, buff.size);
77
506
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
345
    void unserialize(vectorized::BufferReadable& buf) {
72
345
        size_t size;
73
345
        read_binary(size, buf);
74
345
        _nums.resize(size);
75
345
        auto buff = buf.read(sizeof(Ty) * size);
76
345
        memcpy(_nums.data(), buff.data, buff.size);
77
345
    }
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
822
            if (_sorted_nums_vec.size() == 1) {
82
190
                _nums = std::move(_sorted_nums_vec[0]);
83
190
            }
84
85
822
            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
822
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
198
                pdqsort(_nums.begin(), _nums.end());
93
198
            }
94
95
822
            if (quantile == 1 || _nums.size() == 1) {
96
325
                return _nums.back();
97
325
            }
98
99
497
            double u = (_nums.size() - 1) * quantile;
100
497
            auto index = static_cast<uint32_t>(u);
101
497
            return _nums[index] +
102
497
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
822
        } else {
104
18
            DCHECK(_nums.empty());
105
18
            size_t rows = 0;
106
58
            for (const auto& i : _sorted_nums_vec) {
107
58
                rows += i.size();
108
58
            }
109
18
            const bool reverse = quantile > 0.5 && rows > 2;
110
18
            double u = (rows - 1) * quantile;
111
18
            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
18
            size_t target = reverse ? rows - index - 2 : index;
120
18
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
18
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
18
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
18
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
18
        }
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
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
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
12
            for (const auto& i : _sorted_nums_vec) {
107
12
                rows += i.size();
108
12
            }
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
231
            if (_sorted_nums_vec.size() == 1) {
82
140
                _nums = std::move(_sorted_nums_vec[0]);
83
140
            }
84
85
231
            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
231
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
8
                pdqsort(_nums.begin(), _nums.end());
93
8
            }
94
95
231
            if (quantile == 1 || _nums.size() == 1) {
96
96
                return _nums.back();
97
96
            }
98
99
135
            double u = (_nums.size() - 1) * quantile;
100
135
            auto index = static_cast<uint32_t>(u);
101
135
            return _nums[index] +
102
135
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
231
        } 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
231
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
118
    double terminate(double quantile) {
80
118
        if (_sorted_nums_vec.size() <= 1) {
81
103
            if (_sorted_nums_vec.size() == 1) {
82
42
                _nums = std::move(_sorted_nums_vec[0]);
83
42
            }
84
85
103
            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
103
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
103
            if (quantile == 1 || _nums.size() == 1) {
96
60
                return _nums.back();
97
60
            }
98
99
43
            double u = (_nums.size() - 1) * quantile;
100
43
            auto index = static_cast<uint32_t>(u);
101
43
            return _nums[index] +
102
43
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
103
        } else {
104
15
            DCHECK(_nums.empty());
105
15
            size_t rows = 0;
106
46
            for (const auto& i : _sorted_nums_vec) {
107
46
                rows += i.size();
108
46
            }
109
15
            const bool reverse = quantile > 0.5 && rows > 2;
110
15
            double u = (rows - 1) * quantile;
111
15
            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
15
            size_t target = reverse ? rows - index - 2 : index;
120
15
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
15
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
15
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
15
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
15
        }
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
691
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIhE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
137
68
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
291
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
332
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_
138
    };
139
140
339
    void _convert_sorted_num_vec_to_nums() {
141
339
        size_t rows = 0;
142
623
        for (const auto& i : _sorted_nums_vec) {
143
623
            rows += i.size();
144
623
        }
145
339
        _nums.resize(rows);
146
339
        size_t count = 0;
147
148
339
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
962
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
623
            if (!_sorted_nums_vec[i].empty()) {
151
623
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
623
            }
153
623
        }
154
155
1.04k
        while (!min_heap.empty()) {
156
704
            Node node = min_heap.top();
157
704
            min_heap.pop();
158
704
            _nums[count++] = node.value;
159
704
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
81
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
81
                min_heap.push(node);
162
81
            }
163
704
        }
164
339
        _sorted_nums_vec.clear();
165
339
    }
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
183
    void _convert_sorted_num_vec_to_nums() {
141
183
        size_t rows = 0;
142
366
        for (const auto& i : _sorted_nums_vec) {
143
366
            rows += i.size();
144
366
        }
145
183
        _nums.resize(rows);
146
183
        size_t count = 0;
147
148
183
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
549
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
366
            if (!_sorted_nums_vec[i].empty()) {
151
366
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
366
            }
153
366
        }
154
155
609
        while (!min_heap.empty()) {
156
426
            Node node = min_heap.top();
157
426
            min_heap.pop();
158
426
            _nums[count++] = node.value;
159
426
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
60
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
60
                min_heap.push(node);
162
60
            }
163
426
        }
164
183
        _sorted_nums_vec.clear();
165
183
    }
_ZN5doris6CountsIlE31_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
257
        for (const auto& i : _sorted_nums_vec) {
143
257
            rows += i.size();
144
257
        }
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
413
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
257
            if (!_sorted_nums_vec[i].empty()) {
151
257
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
257
            }
153
257
        }
154
155
434
        while (!min_heap.empty()) {
156
278
            Node node = min_heap.top();
157
278
            min_heap.pop();
158
278
            _nums[count++] = node.value;
159
278
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
21
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
21
                min_heap.push(node);
162
21
            }
163
278
        }
164
156
        _sorted_nums_vec.clear();
165
156
    }
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
18
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
18
        Ty first_number = 0, second_number = 0;
169
18
        size_t count = 0;
170
18
        if (reverse) {
171
15
            std::priority_queue<Node> max_heap;
172
63
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
48
                if (!_sorted_nums_vec[i].empty()) {
174
48
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
48
                                     _sorted_nums_vec[i].size() - 1);
176
48
                }
177
48
            }
178
179
70
            while (!max_heap.empty()) {
180
70
                Node node = max_heap.top();
181
70
                max_heap.pop();
182
70
                if (count == target) {
183
15
                    second_number = node.value;
184
55
                } else if (count == target + 1) {
185
15
                    first_number = node.value;
186
15
                    break;
187
15
                }
188
55
                ++count;
189
55
                if (--node.element_index >= 0) {
190
43
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
43
                    max_heap.push(node);
192
43
                }
193
55
            }
194
195
15
        } else {
196
3
            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
10
                if (!_sorted_nums_vec[i].empty()) {
199
10
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
10
                }
201
10
            }
202
203
20
            while (!min_heap.empty()) {
204
20
                Node node = min_heap.top();
205
20
                min_heap.pop();
206
20
                if (count == target) {
207
3
                    first_number = node.value;
208
17
                } else if (count == target + 1) {
209
3
                    second_number = node.value;
210
3
                    break;
211
3
                }
212
17
                ++count;
213
17
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
17
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
17
                    min_heap.push(node);
216
17
                }
217
17
            }
218
3
        }
219
220
18
        return {first_number, second_number};
221
18
    }
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
5
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
4
                if (!_sorted_nums_vec[i].empty()) {
174
4
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
4
                                     _sorted_nums_vec[i].size() - 1);
176
4
                }
177
4
            }
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
3
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
3
                    max_heap.push(node);
192
3
                }
193
5
            }
194
195
2
        } else {
196
2
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
10
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
8
                if (!_sorted_nums_vec[i].empty()) {
199
8
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
8
                }
201
8
            }
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
    }
Unexecuted instantiation: _ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
15
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
15
        Ty first_number = 0, second_number = 0;
169
15
        size_t count = 0;
170
15
        if (reverse) {
171
14
            std::priority_queue<Node> max_heap;
172
58
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
44
                if (!_sorted_nums_vec[i].empty()) {
174
44
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
44
                                     _sorted_nums_vec[i].size() - 1);
176
44
                }
177
44
            }
178
179
64
            while (!max_heap.empty()) {
180
64
                Node node = max_heap.top();
181
64
                max_heap.pop();
182
64
                if (count == target) {
183
14
                    second_number = node.value;
184
50
                } else if (count == target + 1) {
185
14
                    first_number = node.value;
186
14
                    break;
187
14
                }
188
50
                ++count;
189
50
                if (--node.element_index >= 0) {
190
40
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
40
                    max_heap.push(node);
192
40
                }
193
50
            }
194
195
14
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
7
            while (!min_heap.empty()) {
204
7
                Node node = min_heap.top();
205
7
                min_heap.pop();
206
7
                if (count == target) {
207
1
                    first_number = node.value;
208
6
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
6
                ++count;
213
6
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
6
                    min_heap.push(node);
216
6
                }
217
6
            }
218
1
        }
219
220
15
        return {first_number, second_number};
221
15
    }
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