Coverage Report

Created: 2025-07-28 08:25

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
7.66k
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIhEC2Ev
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
68
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
408
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
4.77k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
1.79k
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
60
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
558
    Counts() = default;
37
38
2.78k
    void merge(Counts* other) {
39
2.78k
        if (other != nullptr && !other->_nums.empty()) {
40
2.78k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
2.78k
        }
42
2.78k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIaE5mergeEPS1_
_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.82k
    void merge(Counts* other) {
39
1.82k
        if (other != nullptr && !other->_nums.empty()) {
40
1.82k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.82k
        }
42
1.82k
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
742
    void merge(Counts* other) {
39
742
        if (other != nullptr && !other->_nums.empty()) {
40
742
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
742
        }
42
742
    }
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
196
    void merge(Counts* other) {
39
196
        if (other != nullptr && !other->_nums.empty()) {
40
196
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
196
        }
42
196
    }
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
822
    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.73k
    void serialize(vectorized::BufferWritable& buf) {
59
3.73k
        if (!_nums.empty()) {
60
3.00k
            pdqsort(_nums.begin(), _nums.end());
61
3.00k
            size_t size = _nums.size();
62
3.00k
            buf.write_binary(size);
63
3.00k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
3.00k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
724
            _convert_sorted_num_vec_to_nums();
67
724
            serialize(buf);
68
724
        }
69
3.73k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
_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.37k
    void serialize(vectorized::BufferWritable& buf) {
59
2.37k
        if (!_nums.empty()) {
60
1.98k
            pdqsort(_nums.begin(), _nums.end());
61
1.98k
            size_t size = _nums.size();
62
1.98k
            buf.write_binary(size);
63
1.98k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.98k
        } 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.37k
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
1.14k
    void serialize(vectorized::BufferWritable& buf) {
59
1.14k
        if (!_nums.empty()) {
60
806
            pdqsort(_nums.begin(), _nums.end());
61
806
            size_t size = _nums.size();
62
806
            buf.write_binary(size);
63
806
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
806
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
334
            _convert_sorted_num_vec_to_nums();
67
334
            serialize(buf);
68
334
        }
69
1.14k
    }
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
196
    void serialize(vectorized::BufferWritable& buf) {
59
196
        if (!_nums.empty()) {
60
196
            pdqsort(_nums.begin(), _nums.end());
61
196
            size_t size = _nums.size();
62
196
            buf.write_binary(size);
63
196
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
196
        } 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
196
    }
70
71
2.78k
    void unserialize(vectorized::BufferReadable& buf) {
72
2.78k
        size_t size;
73
2.78k
        buf.read_binary(size);
74
2.78k
        _nums.resize(size);
75
2.78k
        auto buff = buf.read(sizeof(Ty) * size);
76
2.78k
        memcpy(_nums.data(), buff.data, buff.size);
77
2.78k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
_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.82k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.82k
        size_t size;
73
1.82k
        buf.read_binary(size);
74
1.82k
        _nums.resize(size);
75
1.82k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.82k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.82k
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
740
    void unserialize(vectorized::BufferReadable& buf) {
72
740
        size_t size;
73
740
        buf.read_binary(size);
74
740
        _nums.resize(size);
75
740
        auto buff = buf.read(sizeof(Ty) * size);
76
740
        memcpy(_nums.data(), buff.data, buff.size);
77
740
    }
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
196
    void unserialize(vectorized::BufferReadable& buf) {
72
196
        size_t size;
73
196
        buf.read_binary(size);
74
196
        _nums.resize(size);
75
196
        auto buff = buf.read(sizeof(Ty) * size);
76
196
        memcpy(_nums.data(), buff.data, buff.size);
77
196
    }
78
79
2.22k
    double terminate(double quantile) {
80
2.22k
        if (_sorted_nums_vec.size() <= 1) {
81
1.93k
            if (_sorted_nums_vec.size() == 1) {
82
640
                _nums = std::move(_sorted_nums_vec[0]);
83
640
            }
84
85
1.93k
            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.93k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
357
                pdqsort(_nums.begin(), _nums.end());
93
357
            }
94
95
1.93k
            if (quantile == 1 || _nums.size() == 1) {
96
954
                return _nums.back();
97
954
            }
98
99
980
            double u = (_nums.size() - 1) * quantile;
100
980
            auto index = static_cast<uint32_t>(u);
101
980
            return _nums[index] +
102
980
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
1.93k
        } else {
104
295
            DCHECK(_nums.empty());
105
295
            size_t rows = 0;
106
872
            for (const auto& i : _sorted_nums_vec) {
107
872
                rows += i.size();
108
872
            }
109
295
            const bool reverse = quantile > 0.5 && rows > 2;
110
295
            double u = (rows - 1) * quantile;
111
295
            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
295
            size_t target = reverse ? rows - index - 2 : index;
120
295
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
295
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
295
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
295
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
295
        }
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
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
586
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
586
            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
586
            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
412
            double u = (_nums.size() - 1) * quantile;
100
412
            auto index = static_cast<uint32_t>(u);
101
412
            return _nums[index] +
102
412
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
586
        } else {
104
8
            DCHECK(_nums.empty());
105
8
            size_t rows = 0;
106
24
            for (const auto& i : _sorted_nums_vec) {
107
24
                rows += i.size();
108
24
            }
109
8
            const bool reverse = quantile > 0.5 && rows > 2;
110
8
            double u = (rows - 1) * quantile;
111
8
            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
8
            size_t target = reverse ? rows - index - 2 : index;
120
8
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
8
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
8
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
8
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
8
        }
129
594
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
960
    double terminate(double quantile) {
80
960
        if (_sorted_nums_vec.size() <= 1) {
81
766
            if (_sorted_nums_vec.size() == 1) {
82
536
                _nums = std::move(_sorted_nums_vec[0]);
83
536
            }
84
85
766
            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
766
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
22
                pdqsort(_nums.begin(), _nums.end());
93
22
            }
94
95
766
            if (quantile == 1 || _nums.size() == 1) {
96
484
                return _nums.back();
97
484
            }
98
99
282
            double u = (_nums.size() - 1) * quantile;
100
282
            auto index = static_cast<uint32_t>(u);
101
282
            return _nums[index] +
102
282
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
766
        } else {
104
194
            DCHECK(_nums.empty());
105
194
            size_t rows = 0;
106
534
            for (const auto& i : _sorted_nums_vec) {
107
534
                rows += i.size();
108
534
            }
109
194
            const bool reverse = quantile > 0.5 && rows > 2;
110
194
            double u = (rows - 1) * quantile;
111
194
            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
194
            size_t target = reverse ? rows - index - 2 : index;
120
194
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
194
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
194
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
194
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
194
        }
129
960
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
233
    double terminate(double quantile) {
80
233
        if (_sorted_nums_vec.size() <= 1) {
81
188
            if (_sorted_nums_vec.size() == 1) {
82
52
                _nums = std::move(_sorted_nums_vec[0]);
83
52
            }
84
85
188
            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
188
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
188
            if (quantile == 1 || _nums.size() == 1) {
96
120
                return _nums.back();
97
120
            }
98
99
68
            double u = (_nums.size() - 1) * quantile;
100
68
            auto index = static_cast<uint32_t>(u);
101
68
            return _nums[index] +
102
68
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
188
        } else {
104
45
            DCHECK(_nums.empty());
105
45
            size_t rows = 0;
106
170
            for (const auto& i : _sorted_nums_vec) {
107
170
                rows += i.size();
108
170
            }
109
45
            const bool reverse = quantile > 0.5 && rows > 2;
110
45
            double u = (rows - 1) * quantile;
111
45
            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
45
            size_t target = reverse ? rows - index - 2 : index;
120
45
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
45
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
45
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
45
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
45
        }
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
218
            if (_sorted_nums_vec.size() == 1) {
82
52
                _nums = std::move(_sorted_nums_vec[0]);
83
52
            }
84
85
218
            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
218
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
218
            if (quantile == 1 || _nums.size() == 1) {
96
60
                return _nums.back();
97
60
            }
98
99
158
            double u = (_nums.size() - 1) * quantile;
100
158
            auto index = static_cast<uint32_t>(u);
101
158
            return _nums[index] +
102
158
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
218
        } else {
104
48
            DCHECK(_nums.empty());
105
48
            size_t rows = 0;
106
144
            for (const auto& i : _sorted_nums_vec) {
107
144
                rows += i.size();
108
144
            }
109
48
            const bool reverse = quantile > 0.5 && rows > 2;
110
48
            double u = (rows - 1) * quantile;
111
48
            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
48
            size_t target = reverse ? rows - index - 2 : index;
120
48
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
48
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
48
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
48
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
48
        }
129
266
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
3.05k
        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
1.87k
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
861
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
_ZNK5doris6CountsIdE4NodessERKS2_
Line
Count
Source
137
182
        auto operator<=>(const Node& other) const { return value <=> other.value; }
138
    };
139
140
724
    void _convert_sorted_num_vec_to_nums() {
141
724
        size_t rows = 0;
142
1.27k
        for (const auto& i : _sorted_nums_vec) {
143
1.27k
            rows += i.size();
144
1.27k
        }
145
724
        _nums.resize(rows);
146
724
        size_t count = 0;
147
148
724
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
2.00k
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
1.27k
            if (!_sorted_nums_vec[i].empty()) {
151
1.27k
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
1.27k
            }
153
1.27k
        }
154
155
2.19k
        while (!min_heap.empty()) {
156
1.46k
            Node node = min_heap.top();
157
1.46k
            min_heap.pop();
158
1.46k
            _nums[count++] = node.value;
159
1.46k
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
192
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
192
                min_heap.push(node);
162
192
            }
163
1.46k
        }
164
724
        _sorted_nums_vec.clear();
165
724
    }
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
144
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
144
                min_heap.push(node);
162
144
            }
163
900
        }
164
390
        _sorted_nums_vec.clear();
165
390
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
334
    void _convert_sorted_num_vec_to_nums() {
141
334
        size_t rows = 0;
142
520
        for (const auto& i : _sorted_nums_vec) {
143
520
            rows += i.size();
144
520
        }
145
334
        _nums.resize(rows);
146
334
        size_t count = 0;
147
148
334
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
854
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
520
            if (!_sorted_nums_vec[i].empty()) {
151
520
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
520
            }
153
520
        }
154
155
902
        while (!min_heap.empty()) {
156
568
            Node node = min_heap.top();
157
568
            min_heap.pop();
158
568
            _nums[count++] = node.value;
159
568
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
48
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
48
                min_heap.push(node);
162
48
            }
163
568
        }
164
334
        _sorted_nums_vec.clear();
165
334
    }
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
293
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
293
        Ty first_number = 0, second_number = 0;
169
293
        size_t count = 0;
170
293
        if (reverse) {
171
62
            std::priority_queue<Node> max_heap;
172
290
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
228
                if (!_sorted_nums_vec[i].empty()) {
174
228
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
228
                                     _sorted_nums_vec[i].size() - 1);
176
228
                }
177
228
            }
178
179
260
            while (!max_heap.empty()) {
180
260
                Node node = max_heap.top();
181
260
                max_heap.pop();
182
260
                if (count == target) {
183
62
                    second_number = node.value;
184
198
                } else if (count == target + 1) {
185
62
                    first_number = node.value;
186
62
                    break;
187
62
                }
188
198
                ++count;
189
198
                if (--node.element_index >= 0) {
190
144
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
144
                    max_heap.push(node);
192
144
                }
193
198
            }
194
195
231
        } else {
196
231
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
875
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
644
                if (!_sorted_nums_vec[i].empty()) {
199
644
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
644
                }
201
644
            }
202
203
643
            while (!min_heap.empty()) {
204
643
                Node node = min_heap.top();
205
643
                min_heap.pop();
206
643
                if (count == target) {
207
231
                    first_number = node.value;
208
412
                } else if (count == target + 1) {
209
231
                    second_number = node.value;
210
231
                    break;
211
231
                }
212
412
                ++count;
213
412
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
246
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
246
                    min_heap.push(node);
216
246
                }
217
412
            }
218
231
        }
219
220
293
        return {first_number, second_number};
221
293
    }
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
    }
_ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
194
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
194
        Ty first_number = 0, second_number = 0;
169
194
        size_t count = 0;
170
194
        if (reverse) {
171
20
            std::priority_queue<Node> max_heap;
172
76
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
56
                if (!_sorted_nums_vec[i].empty()) {
174
56
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
56
                                     _sorted_nums_vec[i].size() - 1);
176
56
                }
177
56
            }
178
179
78
            while (!max_heap.empty()) {
180
78
                Node node = max_heap.top();
181
78
                max_heap.pop();
182
78
                if (count == target) {
183
20
                    second_number = node.value;
184
58
                } else if (count == target + 1) {
185
20
                    first_number = node.value;
186
20
                    break;
187
20
                }
188
58
                ++count;
189
58
                if (--node.element_index >= 0) {
190
44
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
44
                    max_heap.push(node);
192
44
                }
193
58
            }
194
195
174
        } else {
196
174
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
652
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
478
                if (!_sorted_nums_vec[i].empty()) {
199
478
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
478
                }
201
478
            }
202
203
488
            while (!min_heap.empty()) {
204
488
                Node node = min_heap.top();
205
488
                min_heap.pop();
206
488
                if (count == target) {
207
174
                    first_number = node.value;
208
314
                } else if (count == target + 1) {
209
174
                    second_number = node.value;
210
174
                    break;
211
174
                }
212
314
                ++count;
213
314
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
202
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
202
                    min_heap.push(node);
216
202
                }
217
314
            }
218
174
        }
219
220
194
        return {first_number, second_number};
221
194
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
45
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
45
        Ty first_number = 0, second_number = 0;
169
45
        size_t count = 0;
170
45
        if (reverse) {
171
36
            std::priority_queue<Node> max_heap;
172
184
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
148
                if (!_sorted_nums_vec[i].empty()) {
174
148
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
148
                                     _sorted_nums_vec[i].size() - 1);
176
148
                }
177
148
            }
178
179
160
            while (!max_heap.empty()) {
180
160
                Node node = max_heap.top();
181
160
                max_heap.pop();
182
160
                if (count == target) {
183
36
                    second_number = node.value;
184
124
                } else if (count == target + 1) {
185
36
                    first_number = node.value;
186
36
                    break;
187
36
                }
188
124
                ++count;
189
124
                if (--node.element_index >= 0) {
190
94
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
94
                    max_heap.push(node);
192
94
                }
193
124
            }
194
195
36
        } 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
45
        return {first_number, second_number};
221
45
    }
Unexecuted instantiation: _ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
_ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
48
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
48
        Ty first_number = 0, second_number = 0;
169
48
        size_t count = 0;
170
48
        if (reverse) {
171
4
            std::priority_queue<Node> max_heap;
172
20
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
16
                if (!_sorted_nums_vec[i].empty()) {
174
16
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
16
                                     _sorted_nums_vec[i].size() - 1);
176
16
                }
177
16
            }
178
179
10
            while (!max_heap.empty()) {
180
10
                Node node = max_heap.top();
181
10
                max_heap.pop();
182
10
                if (count == target) {
183
4
                    second_number = node.value;
184
6
                } else if (count == target + 1) {
185
4
                    first_number = node.value;
186
4
                    break;
187
4
                }
188
6
                ++count;
189
6
                if (--node.element_index >= 0) {
190
0
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
0
                    max_heap.push(node);
192
0
                }
193
6
            }
194
195
44
        } else {
196
44
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
172
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
128
                if (!_sorted_nums_vec[i].empty()) {
199
128
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
128
                }
201
128
            }
202
203
104
            while (!min_heap.empty()) {
204
104
                Node node = min_heap.top();
205
104
                min_heap.pop();
206
104
                if (count == target) {
207
44
                    first_number = node.value;
208
60
                } else if (count == target + 1) {
209
44
                    second_number = node.value;
210
44
                    break;
211
44
                }
212
60
                ++count;
213
60
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
8
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
8
                    min_heap.push(node);
216
8
                }
217
60
            }
218
44
        }
219
220
48
        return {first_number, second_number};
221
48
    }
222
223
    vectorized::PODArray<Ty> _nums;
224
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
225
};
226
227
} // namespace doris