Coverage Report

Created: 2025-07-30 15:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/counts.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <pdqsort.h>
21
22
#include <algorithm>
23
#include <cmath>
24
#include <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
6.88k
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIhEC2Ev
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
92
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
408
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
4.22k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
1.72k
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
60
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
374
    Counts() = default;
37
38
2.40k
    void merge(Counts* other) {
39
2.40k
        if (other != nullptr && !other->_nums.empty()) {
40
2.40k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
2.40k
        }
42
2.40k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE5mergeEPS1_
_ZN5doris6CountsIaE5mergeEPS1_
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
    }
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
38
24
    void merge(Counts* other) {
39
24
        if (other != nullptr && !other->_nums.empty()) {
40
24
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
24
        }
42
24
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
1.55k
    void merge(Counts* other) {
39
1.55k
        if (other != nullptr && !other->_nums.empty()) {
40
1.55k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.55k
        }
42
1.55k
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
706
    void merge(Counts* other) {
39
706
        if (other != nullptr && !other->_nums.empty()) {
40
706
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
706
        }
42
706
    }
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
100
    void merge(Counts* other) {
39
100
        if (other != nullptr && !other->_nums.empty()) {
40
100
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
100
        }
42
100
    }
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
5.77k
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIhE9incrementEh
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
52
128
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
52
1.08k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
52
3.16k
    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
500
    void increment(Ty key) { _nums.push_back(key); }
53
54
10
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
10
        _nums.insert(keys.begin(), keys.end());
56
10
    }
Unexecuted instantiation: _ZN5doris6CountsIhE15increment_batchERKNS_10vectorized8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_10vectorized8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
_ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Line
Count
Source
54
10
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
10
        _nums.insert(keys.begin(), keys.end());
56
10
    }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb0EEELm16ELm15EEE
57
58
3.32k
    void serialize(vectorized::BufferWritable& buf) {
59
3.32k
        if (!_nums.empty()) {
60
2.61k
            pdqsort(_nums.begin(), _nums.end());
61
2.61k
            size_t size = _nums.size();
62
2.61k
            buf.write_binary(size);
63
2.61k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
2.61k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
710
            _convert_sorted_num_vec_to_nums();
67
710
            serialize(buf);
68
710
        }
69
3.32k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIaE9serializeERNS_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
            buf.write_binary(size);
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
    }
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
24
    void serialize(vectorized::BufferWritable& buf) {
59
24
        if (!_nums.empty()) {
60
24
            pdqsort(_nums.begin(), _nums.end());
61
24
            size_t size = _nums.size();
62
24
            buf.write_binary(size);
63
24
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
24
        } 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
24
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
2.10k
    void serialize(vectorized::BufferWritable& buf) {
59
2.10k
        if (!_nums.empty()) {
60
1.71k
            pdqsort(_nums.begin(), _nums.end());
61
1.71k
            size_t size = _nums.size();
62
1.71k
            buf.write_binary(size);
63
1.71k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.71k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
390
            _convert_sorted_num_vec_to_nums();
67
390
            serialize(buf);
68
390
        }
69
2.10k
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
1.08k
    void serialize(vectorized::BufferWritable& buf) {
59
1.08k
        if (!_nums.empty()) {
60
768
            pdqsort(_nums.begin(), _nums.end());
61
768
            size_t size = _nums.size();
62
768
            buf.write_binary(size);
63
768
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
768
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
320
            _convert_sorted_num_vec_to_nums();
67
320
            serialize(buf);
68
320
        }
69
1.08k
    }
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
100
    void serialize(vectorized::BufferWritable& buf) {
59
100
        if (!_nums.empty()) {
60
100
            pdqsort(_nums.begin(), _nums.end());
61
100
            size_t size = _nums.size();
62
100
            buf.write_binary(size);
63
100
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
100
        } 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
100
    }
70
71
2.39k
    void unserialize(vectorized::BufferReadable& buf) {
72
2.39k
        size_t size;
73
2.39k
        buf.read_binary(size);
74
2.39k
        _nums.resize(size);
75
2.39k
        auto buff = buf.read(sizeof(Ty) * size);
76
2.39k
        memcpy(_nums.data(), buff.data, buff.size);
77
2.39k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
12
    void unserialize(vectorized::BufferReadable& buf) {
72
12
        size_t size;
73
12
        buf.read_binary(size);
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
    }
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
24
    void unserialize(vectorized::BufferReadable& buf) {
72
24
        size_t size;
73
24
        buf.read_binary(size);
74
24
        _nums.resize(size);
75
24
        auto buff = buf.read(sizeof(Ty) * size);
76
24
        memcpy(_nums.data(), buff.data, buff.size);
77
24
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
1.55k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.55k
        size_t size;
73
1.55k
        buf.read_binary(size);
74
1.55k
        _nums.resize(size);
75
1.55k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.55k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.55k
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
704
    void unserialize(vectorized::BufferReadable& buf) {
72
704
        size_t size;
73
704
        buf.read_binary(size);
74
704
        _nums.resize(size);
75
704
        auto buff = buf.read(sizeof(Ty) * size);
76
704
        memcpy(_nums.data(), buff.data, buff.size);
77
704
    }
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
100
    void unserialize(vectorized::BufferReadable& buf) {
72
100
        size_t size;
73
100
        buf.read_binary(size);
74
100
        _nums.resize(size);
75
100
        auto buff = buf.read(sizeof(Ty) * size);
76
100
        memcpy(_nums.data(), buff.data, buff.size);
77
100
    }
78
79
2.22k
    double terminate(double quantile) {
80
2.22k
        if (_sorted_nums_vec.size() <= 1) {
81
2.19k
            if (_sorted_nums_vec.size() == 1) {
82
1.01k
                _nums = std::move(_sorted_nums_vec[0]);
83
1.01k
            }
84
85
2.19k
            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
2.19k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
357
                pdqsort(_nums.begin(), _nums.end());
93
357
            }
94
95
2.19k
            if (quantile == 1 || _nums.size() == 1) {
96
954
                return _nums.back();
97
954
            }
98
99
1.24k
            double u = (_nums.size() - 1) * quantile;
100
1.24k
            auto index = static_cast<uint32_t>(u);
101
1.24k
            return _nums[index] +
102
1.24k
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
2.19k
        } else {
104
27
            DCHECK(_nums.empty());
105
27
            size_t rows = 0;
106
122
            for (const auto& i : _sorted_nums_vec) {
107
122
                rows += i.size();
108
122
            }
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
2.22k
    }
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
12
                _nums = std::move(_sorted_nums_vec[0]);
83
12
            }
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
590
    double terminate(double quantile) {
80
590
        if (_sorted_nums_vec.size() <= 1) {
81
584
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
584
            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
584
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
286
                pdqsort(_nums.begin(), _nums.end());
93
286
            }
94
95
588
            if (quantile == 1 || _nums.size() == 1) {
96
174
                return _nums.back();
97
174
            }
98
99
410
            double u = (_nums.size() - 1) * quantile;
100
410
            auto index = static_cast<uint32_t>(u);
101
410
            return _nums[index] +
102
410
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
584
        } else {
104
6
            DCHECK(_nums.empty());
105
6
            size_t rows = 0;
106
24
            for (const auto& i : _sorted_nums_vec) {
107
24
                rows += i.size();
108
24
            }
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
590
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
960
    double terminate(double quantile) {
80
960
        if (_sorted_nums_vec.size() <= 1) {
81
960
            if (_sorted_nums_vec.size() == 1) {
82
802
                _nums = std::move(_sorted_nums_vec[0]);
83
802
            }
84
85
960
            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
960
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
22
                pdqsort(_nums.begin(), _nums.end());
93
22
            }
94
95
960
            if (quantile == 1 || _nums.size() == 1) {
96
484
                return _nums.back();
97
484
            }
98
99
476
            double u = (_nums.size() - 1) * quantile;
100
476
            auto index = static_cast<uint32_t>(u);
101
476
            return _nums[index] +
102
476
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
960
        } 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
960
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
233
    double terminate(double quantile) {
80
233
        if (_sorted_nums_vec.size() <= 1) {
81
212
            if (_sorted_nums_vec.size() == 1) {
82
100
                _nums = std::move(_sorted_nums_vec[0]);
83
100
            }
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
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
212
            if (quantile == 1 || _nums.size() == 1) {
96
120
                return _nums.back();
97
120
            }
98
99
92
            double u = (_nums.size() - 1) * quantile;
100
92
            auto index = static_cast<uint32_t>(u);
101
92
            return _nums[index] +
102
92
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
212
        } else {
104
21
            DCHECK(_nums.empty());
105
21
            size_t rows = 0;
106
98
            for (const auto& i : _sorted_nums_vec) {
107
98
                rows += i.size();
108
98
            }
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
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
266
    double terminate(double quantile) {
80
266
        if (_sorted_nums_vec.size() <= 1) {
81
266
            if (_sorted_nums_vec.size() == 1) {
82
100
                _nums = std::move(_sorted_nums_vec[0]);
83
100
            }
84
85
266
            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
266
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
266
            if (quantile == 1 || _nums.size() == 1) {
96
60
                return _nums.back();
97
60
            }
98
99
206
            double u = (_nums.size() - 1) * quantile;
100
206
            auto index = static_cast<uint32_t>(u);
101
206
            return _nums[index] +
102
206
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
266
        } 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
266
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
1.37k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIhE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
_ZNK5doris6CountsIsE4NodessERKS2_
Line
Count
Source
137
136
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
582
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
661
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_
138
    };
139
140
712
    void _convert_sorted_num_vec_to_nums() {
141
712
        size_t rows = 0;
142
1.26k
        for (const auto& i : _sorted_nums_vec) {
143
1.26k
            rows += i.size();
144
1.26k
        }
145
712
        _nums.resize(rows);
146
712
        size_t count = 0;
147
148
712
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
1.97k
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
1.26k
            if (!_sorted_nums_vec[i].empty()) {
151
1.26k
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
1.26k
            }
153
1.26k
        }
154
155
2.16k
        while (!min_heap.empty()) {
156
1.45k
            Node node = min_heap.top();
157
1.45k
            min_heap.pop();
158
1.45k
            _nums[count++] = node.value;
159
1.45k
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
188
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
188
                min_heap.push(node);
162
188
            }
163
1.45k
        }
164
712
        _sorted_nums_vec.clear();
165
712
    }
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
390
    void _convert_sorted_num_vec_to_nums() {
141
390
        size_t rows = 0;
142
756
        for (const auto& i : _sorted_nums_vec) {
143
756
            rows += i.size();
144
756
        }
145
390
        _nums.resize(rows);
146
390
        size_t count = 0;
147
148
390
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
1.14k
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
756
            if (!_sorted_nums_vec[i].empty()) {
151
756
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
756
            }
153
756
        }
154
155
1.29k
        while (!min_heap.empty()) {
156
900
            Node node = min_heap.top();
157
900
            min_heap.pop();
158
900
            _nums[count++] = node.value;
159
900
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
142
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
142
                min_heap.push(node);
162
142
            }
163
900
        }
164
390
        _sorted_nums_vec.clear();
165
390
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
322
    void _convert_sorted_num_vec_to_nums() {
141
322
        size_t rows = 0;
142
508
        for (const auto& i : _sorted_nums_vec) {
143
508
            rows += i.size();
144
508
        }
145
322
        _nums.resize(rows);
146
322
        size_t count = 0;
147
148
322
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
830
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
508
            if (!_sorted_nums_vec[i].empty()) {
151
508
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
508
            }
153
508
        }
154
155
872
        while (!min_heap.empty()) {
156
550
            Node node = min_heap.top();
157
550
            min_heap.pop();
158
550
            _nums[count++] = node.value;
159
550
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
46
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
46
                min_heap.push(node);
162
46
            }
163
550
        }
164
322
        _sorted_nums_vec.clear();
165
322
    }
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
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
22
            std::priority_queue<Node> max_heap;
172
126
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
104
                if (!_sorted_nums_vec[i].empty()) {
174
104
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
104
                                     _sorted_nums_vec[i].size() - 1);
176
104
                }
177
104
            }
178
179
112
            while (!max_heap.empty()) {
180
112
                Node node = max_heap.top();
181
112
                max_heap.pop();
182
112
                if (count == target) {
183
22
                    second_number = node.value;
184
90
                } else if (count == target + 1) {
185
22
                    first_number = node.value;
186
22
                    break;
187
22
                }
188
90
                ++count;
189
90
                if (--node.element_index >= 0) {
190
66
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
66
                    max_heap.push(node);
192
66
                }
193
90
            }
194
195
22
        } else {
196
5
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
23
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
18
                if (!_sorted_nums_vec[i].empty()) {
199
18
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
18
                }
201
18
            }
202
203
33
            while (!min_heap.empty()) {
204
33
                Node node = min_heap.top();
205
33
                min_heap.pop();
206
33
                if (count == target) {
207
5
                    first_number = node.value;
208
28
                } else if (count == target + 1) {
209
5
                    second_number = node.value;
210
5
                    break;
211
5
                }
212
28
                ++count;
213
28
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
28
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
28
                    min_heap.push(node);
216
28
                }
217
28
            }
218
5
        }
219
220
27
        return {first_number, second_number};
221
27
    }
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
10
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
8
                if (!_sorted_nums_vec[i].empty()) {
174
8
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
8
                                     _sorted_nums_vec[i].size() - 1);
176
8
                }
177
8
            }
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
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
6
                    max_heap.push(node);
192
6
                }
193
10
            }
194
195
4
        } else {
196
4
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
20
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
16
                if (!_sorted_nums_vec[i].empty()) {
199
16
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
16
                }
201
16
            }
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
    }
Unexecuted instantiation: _ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
_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
20
            std::priority_queue<Node> max_heap;
172
116
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
96
                if (!_sorted_nums_vec[i].empty()) {
174
96
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
96
                                     _sorted_nums_vec[i].size() - 1);
176
96
                }
177
96
            }
178
179
100
            while (!max_heap.empty()) {
180
100
                Node node = max_heap.top();
181
100
                max_heap.pop();
182
100
                if (count == target) {
183
20
                    second_number = node.value;
184
80
                } else if (count == target + 1) {
185
20
                    first_number = node.value;
186
20
                    break;
187
20
                }
188
80
                ++count;
189
80
                if (--node.element_index >= 0) {
190
60
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
60
                    max_heap.push(node);
192
60
                }
193
80
            }
194
195
20
        } 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
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