Coverage Report

Created: 2025-04-30 17:40

/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
5.92k
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIhEC2Ev
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
68
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
474
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
2.63k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
2.52k
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
60
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
164
    Counts() = default;
37
38
2.14k
    void merge(Counts* other) {
39
2.14k
        if (other != nullptr && !other->_nums.empty()) {
40
2.14k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
2.14k
        }
42
2.14k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIaE5mergeEPS1_
_ZN5doris6CountsIsE5mergeEPS1_
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
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
1.00k
    void merge(Counts* other) {
39
1.00k
        if (other != nullptr && !other->_nums.empty()) {
40
1.00k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.00k
        }
42
1.00k
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
1.10k
    void merge(Counts* other) {
39
1.10k
        if (other != nullptr && !other->_nums.empty()) {
40
1.10k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.10k
        }
42
1.10k
    }
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
16
    void merge(Counts* other) {
39
16
        if (other != nullptr && !other->_nums.empty()) {
40
16
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
16
        }
42
16
    }
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
4.77k
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIhE9incrementEh
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
52
122
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
52
1.57k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
52
1.83k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
52
824
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
52
72
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIfE9incrementEf
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
52
344
    void increment(Ty key) { _nums.push_back(key); }
53
54
8
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
8
        _nums.insert(keys.begin(), keys.end());
56
8
    }
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
8
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
8
        _nums.insert(keys.begin(), keys.end());
56
8
    }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
57
58
3.28k
    void serialize(vectorized::BufferWritable& buf) {
59
3.28k
        if (!_nums.empty()) {
60
2.36k
            pdqsort(_nums.begin(), _nums.end());
61
2.36k
            size_t size = _nums.size();
62
2.36k
            write_binary(size, buf);
63
2.36k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
2.36k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
920
            _convert_sorted_num_vec_to_nums();
67
920
            serialize(buf);
68
920
        }
69
3.28k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIsE9serializeERNS_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
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
1.47k
    void serialize(vectorized::BufferWritable& buf) {
59
1.47k
        if (!_nums.empty()) {
60
1.16k
            pdqsort(_nums.begin(), _nums.end());
61
1.16k
            size_t size = _nums.size();
62
1.16k
            write_binary(size, buf);
63
1.16k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.16k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
312
            _convert_sorted_num_vec_to_nums();
67
312
            serialize(buf);
68
312
        }
69
1.47k
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
1.77k
    void serialize(vectorized::BufferWritable& buf) {
59
1.77k
        if (!_nums.empty()) {
60
1.17k
            pdqsort(_nums.begin(), _nums.end());
61
1.17k
            size_t size = _nums.size();
62
1.17k
            write_binary(size, buf);
63
1.17k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.17k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
608
            _convert_sorted_num_vec_to_nums();
67
608
            serialize(buf);
68
608
        }
69
1.77k
    }
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
16
    void serialize(vectorized::BufferWritable& buf) {
59
16
        if (!_nums.empty()) {
60
16
            pdqsort(_nums.begin(), _nums.end());
61
16
            size_t size = _nums.size();
62
16
            write_binary(size, buf);
63
16
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
16
        } 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
16
    }
70
71
2.14k
    void unserialize(vectorized::BufferReadable& buf) {
72
2.14k
        size_t size;
73
2.14k
        read_binary(size, buf);
74
2.14k
        _nums.resize(size);
75
2.14k
        auto buff = buf.read(sizeof(Ty) * size);
76
2.14k
        memcpy(_nums.data(), buff.data, buff.size);
77
2.14k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIsE11unserializeERNS_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
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
1.00k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.00k
        size_t size;
73
1.00k
        read_binary(size, buf);
74
1.00k
        _nums.resize(size);
75
1.00k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.00k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.00k
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
1.10k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.10k
        size_t size;
73
1.10k
        read_binary(size, buf);
74
1.10k
        _nums.resize(size);
75
1.10k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.10k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.10k
    }
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
16
    void unserialize(vectorized::BufferReadable& buf) {
72
16
        size_t size;
73
16
        read_binary(size, buf);
74
16
        _nums.resize(size);
75
16
        auto buff = buf.read(sizeof(Ty) * size);
76
16
        memcpy(_nums.data(), buff.data, buff.size);
77
16
    }
78
79
1.67k
    double terminate(double quantile) {
80
1.67k
        if (_sorted_nums_vec.size() <= 1) {
81
1.57k
            if (_sorted_nums_vec.size() == 1) {
82
320
                _nums = std::move(_sorted_nums_vec[0]);
83
320
            }
84
85
1.57k
            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
1.57k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
401
                pdqsort(_nums.begin(), _nums.end());
93
401
            }
94
95
1.57k
            if (quantile == 1 || _nums.size() == 1) {
96
650
                return _nums.back();
97
650
            }
98
99
926
            double u = (_nums.size() - 1) * quantile;
100
926
            auto index = static_cast<uint32_t>(u);
101
926
            return _nums[index] +
102
926
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
1.57k
        } else {
104
101
            DCHECK(_nums.empty());
105
101
            size_t rows = 0;
106
340
            for (const auto& i : _sorted_nums_vec) {
107
340
                rows += i.size();
108
340
            }
109
101
            const bool reverse = quantile > 0.5 && rows > 2;
110
101
            double u = (rows - 1) * quantile;
111
101
            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
101
            size_t target = reverse ? rows - index - 2 : index;
120
101
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
101
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
101
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
101
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
101
        }
129
1.67k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9terminateEd
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
79
116
    double terminate(double quantile) {
80
116
        if (_sorted_nums_vec.size() <= 1) {
81
116
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
116
            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
116
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
48
                pdqsort(_nums.begin(), _nums.end());
93
48
            }
94
95
116
            if (quantile == 1 || _nums.size() == 1) {
96
68
                return _nums.back();
97
68
            }
98
99
48
            double u = (_nums.size() - 1) * quantile;
100
48
            auto index = static_cast<uint32_t>(u);
101
48
            return _nums[index] +
102
48
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
116
        } 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
116
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
79
594
    double terminate(double quantile) {
80
594
        if (_sorted_nums_vec.size() <= 1) {
81
588
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
588
            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
588
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
330
                pdqsort(_nums.begin(), _nums.end());
93
330
            }
94
95
588
            if (quantile == 1 || _nums.size() == 1) {
96
174
                return _nums.back();
97
174
            }
98
99
414
            double u = (_nums.size() - 1) * quantile;
100
414
            auto index = static_cast<uint32_t>(u);
101
414
            return _nums[index] +
102
414
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
588
        } else {
104
6
            DCHECK(_nums.empty());
105
6
            size_t rows = 0;
106
18
            for (const auto& i : _sorted_nums_vec) {
107
18
                rows += i.size();
108
18
            }
109
6
            const bool reverse = quantile > 0.5 && rows > 2;
110
6
            double u = (rows - 1) * quantile;
111
6
            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
6
            size_t target = reverse ? rows - index - 2 : index;
120
6
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
6
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
6
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
6
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
6
        }
129
594
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
462
    double terminate(double quantile) {
80
462
        if (_sorted_nums_vec.size() <= 1) {
81
404
            if (_sorted_nums_vec.size() == 1) {
82
138
                _nums = std::move(_sorted_nums_vec[0]);
83
138
            }
84
85
404
            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
404
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
22
                pdqsort(_nums.begin(), _nums.end());
93
22
            }
94
95
404
            if (quantile == 1 || _nums.size() == 1) {
96
192
                return _nums.back();
97
192
            }
98
99
212
            double u = (_nums.size() - 1) * quantile;
100
212
            auto index = static_cast<uint32_t>(u);
101
212
            return _nums[index] +
102
212
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
404
        } else {
104
58
            DCHECK(_nums.empty());
105
58
            size_t rows = 0;
106
188
            for (const auto& i : _sorted_nums_vec) {
107
188
                rows += i.size();
108
188
            }
109
58
            const bool reverse = quantile > 0.5 && rows > 2;
110
58
            double u = (rows - 1) * quantile;
111
58
            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
58
            size_t target = reverse ? rows - index - 2 : index;
120
58
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
58
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
58
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
58
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
58
        }
129
462
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
233
    double terminate(double quantile) {
80
233
        if (_sorted_nums_vec.size() <= 1) {
81
196
            if (_sorted_nums_vec.size() == 1) {
82
166
                _nums = std::move(_sorted_nums_vec[0]);
83
166
            }
84
85
196
            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
196
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
196
            if (quantile == 1 || _nums.size() == 1) {
96
120
                return _nums.back();
97
120
            }
98
99
76
            double u = (_nums.size() - 1) * quantile;
100
76
            auto index = static_cast<uint32_t>(u);
101
76
            return _nums[index] +
102
76
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
196
        } else {
104
37
            DCHECK(_nums.empty());
105
37
            size_t rows = 0;
106
134
            for (const auto& i : _sorted_nums_vec) {
107
134
                rows += i.size();
108
134
            }
109
37
            const bool reverse = quantile > 0.5 && rows > 2;
110
37
            double u = (rows - 1) * quantile;
111
37
            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
37
            size_t target = reverse ? rows - index - 2 : index;
120
37
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
37
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
37
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
37
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
37
        }
129
233
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
79
60
    double terminate(double quantile) {
80
60
        if (_sorted_nums_vec.size() <= 1) {
81
60
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
60
            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
60
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
60
            if (quantile == 1 || _nums.size() == 1) {
96
48
                return _nums.back();
97
48
            }
98
99
12
            double u = (_nums.size() - 1) * quantile;
100
12
            auto index = static_cast<uint32_t>(u);
101
12
            return _nums[index] +
102
12
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
60
        } 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
60
    }
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
79
212
    double terminate(double quantile) {
80
212
        if (_sorted_nums_vec.size() <= 1) {
81
212
            if (_sorted_nums_vec.size() == 1) {
82
16
                _nums = std::move(_sorted_nums_vec[0]);
83
16
            }
84
85
212
            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
212
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
212
            if (quantile == 1 || _nums.size() == 1) {
96
48
                return _nums.back();
97
48
            }
98
99
164
            double u = (_nums.size() - 1) * quantile;
100
164
            auto index = static_cast<uint32_t>(u);
101
164
            return _nums[index] +
102
164
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
212
        } 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
212
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
2.04k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIhE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
137
76
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
1.19k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
775
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_
138
    };
139
140
920
    void _convert_sorted_num_vec_to_nums() {
141
920
        size_t rows = 0;
142
1.48k
        for (const auto& i : _sorted_nums_vec) {
143
1.48k
            rows += i.size();
144
1.48k
        }
145
920
        _nums.resize(rows);
146
920
        size_t count = 0;
147
148
920
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
2.40k
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
1.48k
            if (!_sorted_nums_vec[i].empty()) {
151
1.48k
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
1.48k
            }
153
1.48k
        }
154
155
2.57k
        while (!min_heap.empty()) {
156
1.65k
            Node node = min_heap.top();
157
1.65k
            min_heap.pop();
158
1.65k
            _nums[count++] = node.value;
159
1.65k
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
168
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
168
                min_heap.push(node);
162
168
            }
163
1.65k
        }
164
920
        _sorted_nums_vec.clear();
165
920
    }
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
312
    void _convert_sorted_num_vec_to_nums() {
141
312
        size_t rows = 0;
142
678
        for (const auto& i : _sorted_nums_vec) {
143
678
            rows += i.size();
144
678
        }
145
312
        _nums.resize(rows);
146
312
        size_t count = 0;
147
148
312
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
990
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
678
            if (!_sorted_nums_vec[i].empty()) {
151
678
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
678
            }
153
678
        }
154
155
1.06k
        while (!min_heap.empty()) {
156
750
            Node node = min_heap.top();
157
750
            min_heap.pop();
158
750
            _nums[count++] = node.value;
159
750
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
72
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
72
                min_heap.push(node);
162
72
            }
163
750
        }
164
312
        _sorted_nums_vec.clear();
165
312
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
608
    void _convert_sorted_num_vec_to_nums() {
141
608
        size_t rows = 0;
142
806
        for (const auto& i : _sorted_nums_vec) {
143
806
            rows += i.size();
144
806
        }
145
608
        _nums.resize(rows);
146
608
        size_t count = 0;
147
148
608
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
1.41k
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
806
            if (!_sorted_nums_vec[i].empty()) {
151
806
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
806
            }
153
806
        }
154
155
1.51k
        while (!min_heap.empty()) {
156
902
            Node node = min_heap.top();
157
902
            min_heap.pop();
158
902
            _nums[count++] = node.value;
159
902
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
96
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
96
                min_heap.push(node);
162
96
            }
163
902
        }
164
608
        _sorted_nums_vec.clear();
165
608
    }
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
101
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
101
        Ty first_number = 0, second_number = 0;
169
101
        size_t count = 0;
170
101
        if (reverse) {
171
36
            std::priority_queue<Node> max_heap;
172
178
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
142
                if (!_sorted_nums_vec[i].empty()) {
174
142
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
142
                                     _sorted_nums_vec[i].size() - 1);
176
142
                }
177
142
            }
178
179
192
            while (!max_heap.empty()) {
180
192
                Node node = max_heap.top();
181
192
                max_heap.pop();
182
192
                if (count == target) {
183
36
                    second_number = node.value;
184
156
                } else if (count == target + 1) {
185
36
                    first_number = node.value;
186
36
                    break;
187
36
                }
188
156
                ++count;
189
156
                if (--node.element_index >= 0) {
190
128
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
128
                    max_heap.push(node);
192
128
                }
193
156
            }
194
195
65
        } else {
196
65
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
263
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
198
                if (!_sorted_nums_vec[i].empty()) {
199
198
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
198
                }
201
198
            }
202
203
223
            while (!min_heap.empty()) {
204
223
                Node node = min_heap.top();
205
223
                min_heap.pop();
206
223
                if (count == target) {
207
65
                    first_number = node.value;
208
158
                } else if (count == target + 1) {
209
65
                    second_number = node.value;
210
65
                    break;
211
65
                }
212
158
                ++count;
213
158
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
128
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
128
                    min_heap.push(node);
216
128
                }
217
158
            }
218
65
        }
219
220
101
        return {first_number, second_number};
221
101
    }
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
6
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
6
        Ty first_number = 0, second_number = 0;
169
6
        size_t count = 0;
170
6
        if (reverse) {
171
2
            std::priority_queue<Node> max_heap;
172
8
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
6
                if (!_sorted_nums_vec[i].empty()) {
174
6
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
6
                                     _sorted_nums_vec[i].size() - 1);
176
6
                }
177
6
            }
178
179
12
            while (!max_heap.empty()) {
180
12
                Node node = max_heap.top();
181
12
                max_heap.pop();
182
12
                if (count == target) {
183
2
                    second_number = node.value;
184
10
                } else if (count == target + 1) {
185
2
                    first_number = node.value;
186
2
                    break;
187
2
                }
188
10
                ++count;
189
10
                if (--node.element_index >= 0) {
190
8
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
8
                    max_heap.push(node);
192
8
                }
193
10
            }
194
195
4
        } else {
196
4
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
16
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
12
                if (!_sorted_nums_vec[i].empty()) {
199
12
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
12
                }
201
12
            }
202
203
26
            while (!min_heap.empty()) {
204
26
                Node node = min_heap.top();
205
26
                min_heap.pop();
206
26
                if (count == target) {
207
4
                    first_number = node.value;
208
22
                } else if (count == target + 1) {
209
4
                    second_number = node.value;
210
4
                    break;
211
4
                }
212
22
                ++count;
213
22
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
22
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
22
                    min_heap.push(node);
216
22
                }
217
22
            }
218
4
        }
219
220
6
        return {first_number, second_number};
221
6
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
58
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
58
        Ty first_number = 0, second_number = 0;
169
58
        size_t count = 0;
170
58
        if (reverse) {
171
6
            std::priority_queue<Node> max_heap;
172
30
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
24
                if (!_sorted_nums_vec[i].empty()) {
174
24
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
24
                                     _sorted_nums_vec[i].size() - 1);
176
24
                }
177
24
            }
178
179
48
            while (!max_heap.empty()) {
180
48
                Node node = max_heap.top();
181
48
                max_heap.pop();
182
48
                if (count == target) {
183
6
                    second_number = node.value;
184
42
                } else if (count == target + 1) {
185
6
                    first_number = node.value;
186
6
                    break;
187
6
                }
188
42
                ++count;
189
42
                if (--node.element_index >= 0) {
190
36
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
36
                    max_heap.push(node);
192
36
                }
193
42
            }
194
195
52
        } else {
196
52
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
216
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
164
                if (!_sorted_nums_vec[i].empty()) {
199
164
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
164
                }
201
164
            }
202
203
172
            while (!min_heap.empty()) {
204
172
                Node node = min_heap.top();
205
172
                min_heap.pop();
206
172
                if (count == target) {
207
52
                    first_number = node.value;
208
120
                } else if (count == target + 1) {
209
52
                    second_number = node.value;
210
52
                    break;
211
52
                }
212
120
                ++count;
213
120
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
92
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
92
                    min_heap.push(node);
216
92
                }
217
120
            }
218
52
        }
219
220
58
        return {first_number, second_number};
221
58
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
37
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
37
        Ty first_number = 0, second_number = 0;
169
37
        size_t count = 0;
170
37
        if (reverse) {
171
28
            std::priority_queue<Node> max_heap;
172
140
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
112
                if (!_sorted_nums_vec[i].empty()) {
174
112
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
112
                                     _sorted_nums_vec[i].size() - 1);
176
112
                }
177
112
            }
178
179
132
            while (!max_heap.empty()) {
180
132
                Node node = max_heap.top();
181
132
                max_heap.pop();
182
132
                if (count == target) {
183
28
                    second_number = node.value;
184
104
                } else if (count == target + 1) {
185
28
                    first_number = node.value;
186
28
                    break;
187
28
                }
188
104
                ++count;
189
104
                if (--node.element_index >= 0) {
190
84
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
84
                    max_heap.push(node);
192
84
                }
193
104
            }
194
195
28
        } else {
196
9
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
31
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
22
                if (!_sorted_nums_vec[i].empty()) {
199
22
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
22
                }
201
22
            }
202
203
25
            while (!min_heap.empty()) {
204
25
                Node node = min_heap.top();
205
25
                min_heap.pop();
206
25
                if (count == target) {
207
9
                    first_number = node.value;
208
16
                } else if (count == target + 1) {
209
9
                    second_number = node.value;
210
9
                    break;
211
9
                }
212
16
                ++count;
213
16
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
14
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
14
                    min_heap.push(node);
216
14
                }
217
16
            }
218
9
        }
219
220
37
        return {first_number, second_number};
221
37
    }
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