Coverage Report

Created: 2025-05-12 03:26

/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
3.64k
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIhEC2Ev
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
34
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
225
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
1.97k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
1.17k
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
30
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
204
    Counts() = default;
37
38
1.26k
    void merge(Counts* other) {
39
1.26k
        if (other != nullptr && !other->_nums.empty()) {
40
1.26k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.26k
        }
42
1.26k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIaE5mergeEPS1_
_ZN5doris6CountsIsE5mergeEPS1_
Line
Count
Source
38
3
    void merge(Counts* other) {
39
3
        if (other != nullptr && !other->_nums.empty()) {
40
3
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
3
        }
42
3
    }
_ZN5doris6CountsIiE5mergeEPS1_
Line
Count
Source
38
701
    void merge(Counts* other) {
39
701
        if (other != nullptr && !other->_nums.empty()) {
40
701
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
701
        }
42
701
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
510
    void merge(Counts* other) {
39
510
        if (other != nullptr && !other->_nums.empty()) {
40
510
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
510
        }
42
510
    }
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
50
    void merge(Counts* other) {
39
50
        if (other != nullptr && !other->_nums.empty()) {
40
50
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
50
        }
42
50
    }
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
3.16k
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIhE9incrementEh
_ZN5doris6CountsIaE9incrementEa
Line
Count
Source
52
64
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIsE9incrementEs
Line
Count
Source
52
788
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
52
1.60k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
52
412
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
52
36
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIfE9incrementEf
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
52
268
    void increment(Ty key) { _nums.push_back(key); }
53
54
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
5
        _nums.insert(keys.begin(), keys.end());
56
5
    }
Unexecuted instantiation: _ZN5doris6CountsIhE15increment_batchERKNS_10vectorized8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIaE15increment_batchERKNS_10vectorized8PODArrayIaLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIsE15increment_batchERKNS_10vectorized8PODArrayIsLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIiE15increment_batchERKNS_10vectorized8PODArrayIiLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
_ZN5doris6CountsIlE15increment_batchERKNS_10vectorized8PODArrayIlLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Line
Count
Source
54
5
    void increment_batch(const vectorized::PaddedPODArray<Ty>& keys) {
55
5
        _nums.insert(keys.begin(), keys.end());
56
5
    }
Unexecuted instantiation: _ZN5doris6CountsInE15increment_batchERKNS_10vectorized8PODArrayInLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
57
58
1.83k
    void serialize(vectorized::BufferWritable& buf) {
59
1.83k
        if (!_nums.empty()) {
60
1.37k
            pdqsort(_nums.begin(), _nums.end());
61
1.37k
            size_t size = _nums.size();
62
1.37k
            write_binary(size, buf);
63
1.37k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.37k
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
456
            _convert_sorted_num_vec_to_nums();
67
456
            serialize(buf);
68
456
        }
69
1.83k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIaE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIsE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
3
    void serialize(vectorized::BufferWritable& buf) {
59
3
        if (!_nums.empty()) {
60
3
            pdqsort(_nums.begin(), _nums.end());
61
3
            size_t size = _nums.size();
62
3
            write_binary(size, buf);
63
3
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
3
        } 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
3
    }
_ZN5doris6CountsIiE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
935
    void serialize(vectorized::BufferWritable& buf) {
59
935
        if (!_nums.empty()) {
60
779
            pdqsort(_nums.begin(), _nums.end());
61
779
            size_t size = _nums.size();
62
779
            write_binary(size, buf);
63
779
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
779
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
156
            _convert_sorted_num_vec_to_nums();
67
156
            serialize(buf);
68
156
        }
69
935
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
842
    void serialize(vectorized::BufferWritable& buf) {
59
842
        if (!_nums.empty()) {
60
542
            pdqsort(_nums.begin(), _nums.end());
61
542
            size_t size = _nums.size();
62
542
            write_binary(size, buf);
63
542
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
542
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
300
            _convert_sorted_num_vec_to_nums();
67
300
            serialize(buf);
68
300
        }
69
842
    }
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
50
    void serialize(vectorized::BufferWritable& buf) {
59
50
        if (!_nums.empty()) {
60
50
            pdqsort(_nums.begin(), _nums.end());
61
50
            size_t size = _nums.size();
62
50
            write_binary(size, buf);
63
50
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
50
        } 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
50
    }
70
71
1.26k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.26k
        size_t size;
73
1.26k
        read_binary(size, buf);
74
1.26k
        _nums.resize(size);
75
1.26k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.26k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.26k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIaE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIsE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
3
    void unserialize(vectorized::BufferReadable& buf) {
72
3
        size_t size;
73
3
        read_binary(size, buf);
74
3
        _nums.resize(size);
75
3
        auto buff = buf.read(sizeof(Ty) * size);
76
3
        memcpy(_nums.data(), buff.data, buff.size);
77
3
    }
_ZN5doris6CountsIiE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
701
    void unserialize(vectorized::BufferReadable& buf) {
72
701
        size_t size;
73
701
        read_binary(size, buf);
74
701
        _nums.resize(size);
75
701
        auto buff = buf.read(sizeof(Ty) * size);
76
701
        memcpy(_nums.data(), buff.data, buff.size);
77
701
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
510
    void unserialize(vectorized::BufferReadable& buf) {
72
510
        size_t size;
73
510
        read_binary(size, buf);
74
510
        _nums.resize(size);
75
510
        auto buff = buf.read(sizeof(Ty) * size);
76
510
        memcpy(_nums.data(), buff.data, buff.size);
77
510
    }
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
50
    void unserialize(vectorized::BufferReadable& buf) {
72
50
        size_t size;
73
50
        read_binary(size, buf);
74
50
        _nums.resize(size);
75
50
        auto buff = buf.read(sizeof(Ty) * size);
76
50
        memcpy(_nums.data(), buff.data, buff.size);
77
50
    }
78
79
1.14k
    double terminate(double quantile) {
80
1.14k
        if (_sorted_nums_vec.size() <= 1) {
81
1.13k
            if (_sorted_nums_vec.size() == 1) {
82
466
                _nums = std::move(_sorted_nums_vec[0]);
83
466
            }
84
85
1.13k
            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.13k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
201
                pdqsort(_nums.begin(), _nums.end());
93
201
            }
94
95
1.13k
            if (quantile == 1 || _nums.size() == 1) {
96
501
                return _nums.back();
97
501
            }
98
99
630
            double u = (_nums.size() - 1) * quantile;
100
630
            auto index = static_cast<uint32_t>(u);
101
630
            return _nums[index] +
102
630
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
1.13k
        } else {
104
15
            DCHECK(_nums.empty());
105
15
            size_t rows = 0;
106
64
            for (const auto& i : _sorted_nums_vec) {
107
64
                rows += i.size();
108
64
            }
109
15
            const bool reverse = quantile > 0.5 && rows > 2;
110
15
            double u = (rows - 1) * quantile;
111
15
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
15
            size_t target = reverse ? rows - index - 2 : index;
120
15
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
15
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
15
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
15
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
15
        }
129
1.14k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9terminateEd
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
79
58
    double terminate(double quantile) {
80
58
        if (_sorted_nums_vec.size() <= 1) {
81
58
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
58
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
58
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
24
                pdqsort(_nums.begin(), _nums.end());
93
24
            }
94
95
58
            if (quantile == 1 || _nums.size() == 1) {
96
34
                return _nums.back();
97
34
            }
98
99
24
            double u = (_nums.size() - 1) * quantile;
100
24
            auto index = static_cast<uint32_t>(u);
101
24
            return _nums[index] +
102
24
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
58
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
0
        }
129
58
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
79
297
    double terminate(double quantile) {
80
297
        if (_sorted_nums_vec.size() <= 1) {
81
297
            if (_sorted_nums_vec.size() == 1) {
82
3
                _nums = std::move(_sorted_nums_vec[0]);
83
3
            }
84
85
297
            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
297
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
165
                pdqsort(_nums.begin(), _nums.end());
93
165
            }
94
95
297
            if (quantile == 1 || _nums.size() == 1) {
96
87
                return _nums.back();
97
87
            }
98
99
210
            double u = (_nums.size() - 1) * quantile;
100
210
            auto index = static_cast<uint32_t>(u);
101
210
            return _nums[index] +
102
210
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
297
        } 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
297
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
495
    double terminate(double quantile) {
80
495
        if (_sorted_nums_vec.size() <= 1) {
81
495
            if (_sorted_nums_vec.size() == 1) {
82
362
                _nums = std::move(_sorted_nums_vec[0]);
83
362
            }
84
85
495
            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
495
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
11
                pdqsort(_nums.begin(), _nums.end());
93
11
            }
94
95
495
            if (quantile == 1 || _nums.size() == 1) {
96
254
                return _nums.back();
97
254
            }
98
99
241
            double u = (_nums.size() - 1) * quantile;
100
241
            auto index = static_cast<uint32_t>(u);
101
241
            return _nums[index] +
102
241
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
495
        } 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
495
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
118
    double terminate(double quantile) {
80
118
        if (_sorted_nums_vec.size() <= 1) {
81
103
            if (_sorted_nums_vec.size() == 1) {
82
51
                _nums = std::move(_sorted_nums_vec[0]);
83
51
            }
84
85
103
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
103
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
103
            if (quantile == 1 || _nums.size() == 1) {
96
60
                return _nums.back();
97
60
            }
98
99
43
            double u = (_nums.size() - 1) * quantile;
100
43
            auto index = static_cast<uint32_t>(u);
101
43
            return _nums[index] +
102
43
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
103
        } else {
104
15
            DCHECK(_nums.empty());
105
15
            size_t rows = 0;
106
64
            for (const auto& i : _sorted_nums_vec) {
107
64
                rows += i.size();
108
64
            }
109
15
            const bool reverse = quantile > 0.5 && rows > 2;
110
15
            double u = (rows - 1) * quantile;
111
15
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
15
            size_t target = reverse ? rows - index - 2 : index;
120
15
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
15
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
15
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
15
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
15
        }
129
118
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
79
30
    double terminate(double quantile) {
80
30
        if (_sorted_nums_vec.size() <= 1) {
81
30
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
30
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
30
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
30
            if (quantile == 1 || _nums.size() == 1) {
96
24
                return _nums.back();
97
24
            }
98
99
6
            double u = (_nums.size() - 1) * quantile;
100
6
            auto index = static_cast<uint32_t>(u);
101
6
            return _nums[index] +
102
6
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
30
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
0
        }
129
30
    }
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
79
148
    double terminate(double quantile) {
80
148
        if (_sorted_nums_vec.size() <= 1) {
81
148
            if (_sorted_nums_vec.size() == 1) {
82
50
                _nums = std::move(_sorted_nums_vec[0]);
83
50
            }
84
85
148
            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
148
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
148
            if (quantile == 1 || _nums.size() == 1) {
96
42
                return _nums.back();
97
42
            }
98
99
106
            double u = (_nums.size() - 1) * quantile;
100
106
            auto index = static_cast<uint32_t>(u);
101
106
            return _nums[index] +
102
106
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
148
        } 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
148
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
674
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsIhE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIaE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIsE4NodessERKS2_
_ZNK5doris6CountsIiE4NodessERKS2_
Line
Count
Source
137
291
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
383
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_
138
    };
139
140
456
    void _convert_sorted_num_vec_to_nums() {
141
456
        size_t rows = 0;
142
734
        for (const auto& i : _sorted_nums_vec) {
143
734
            rows += i.size();
144
734
        }
145
456
        _nums.resize(rows);
146
456
        size_t count = 0;
147
148
456
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
1.19k
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
734
            if (!_sorted_nums_vec[i].empty()) {
151
734
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
734
            }
153
734
        }
154
155
1.24k
        while (!min_heap.empty()) {
156
790
            Node node = min_heap.top();
157
790
            min_heap.pop();
158
790
            _nums[count++] = node.value;
159
790
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
56
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
56
                min_heap.push(node);
162
56
            }
163
790
        }
164
456
        _sorted_nums_vec.clear();
165
456
    }
Unexecuted instantiation: _ZN5doris6CountsIhE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIaE31_convert_sorted_num_vec_to_numsEv
Unexecuted instantiation: _ZN5doris6CountsIsE31_convert_sorted_num_vec_to_numsEv
_ZN5doris6CountsIiE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
156
    void _convert_sorted_num_vec_to_nums() {
141
156
        size_t rows = 0;
142
339
        for (const auto& i : _sorted_nums_vec) {
143
339
            rows += i.size();
144
339
        }
145
156
        _nums.resize(rows);
146
156
        size_t count = 0;
147
148
156
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
495
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
339
            if (!_sorted_nums_vec[i].empty()) {
151
339
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
339
            }
153
339
        }
154
155
531
        while (!min_heap.empty()) {
156
375
            Node node = min_heap.top();
157
375
            min_heap.pop();
158
375
            _nums[count++] = node.value;
159
375
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
36
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
36
                min_heap.push(node);
162
36
            }
163
375
        }
164
156
        _sorted_nums_vec.clear();
165
156
    }
_ZN5doris6CountsIlE31_convert_sorted_num_vec_to_numsEv
Line
Count
Source
140
300
    void _convert_sorted_num_vec_to_nums() {
141
300
        size_t rows = 0;
142
395
        for (const auto& i : _sorted_nums_vec) {
143
395
            rows += i.size();
144
395
        }
145
300
        _nums.resize(rows);
146
300
        size_t count = 0;
147
148
300
        std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
149
695
        for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
150
395
            if (!_sorted_nums_vec[i].empty()) {
151
395
                min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
152
395
            }
153
395
        }
154
155
715
        while (!min_heap.empty()) {
156
415
            Node node = min_heap.top();
157
415
            min_heap.pop();
158
415
            _nums[count++] = node.value;
159
415
            if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
160
20
                node.value = _sorted_nums_vec[node.array_index][node.element_index];
161
20
                min_heap.push(node);
162
20
            }
163
415
        }
164
300
        _sorted_nums_vec.clear();
165
300
    }
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
15
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
15
        Ty first_number = 0, second_number = 0;
169
15
        size_t count = 0;
170
15
        if (reverse) {
171
14
            std::priority_queue<Node> max_heap;
172
76
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
62
                if (!_sorted_nums_vec[i].empty()) {
174
62
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
62
                                     _sorted_nums_vec[i].size() - 1);
176
62
                }
177
62
            }
178
179
64
            while (!max_heap.empty()) {
180
64
                Node node = max_heap.top();
181
64
                max_heap.pop();
182
64
                if (count == target) {
183
14
                    second_number = node.value;
184
50
                } else if (count == target + 1) {
185
14
                    first_number = node.value;
186
14
                    break;
187
14
                }
188
50
                ++count;
189
50
                if (--node.element_index >= 0) {
190
40
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
40
                    max_heap.push(node);
192
40
                }
193
50
            }
194
195
14
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
7
            while (!min_heap.empty()) {
204
7
                Node node = min_heap.top();
205
7
                min_heap.pop();
206
7
                if (count == target) {
207
1
                    first_number = node.value;
208
6
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
6
                ++count;
213
6
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
6
                    min_heap.push(node);
216
6
                }
217
6
            }
218
1
        }
219
220
15
        return {first_number, second_number};
221
15
    }
Unexecuted instantiation: _ZN5doris6CountsIhE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIaE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIsE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIiE27_merge_sort_and_get_numbersElb
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
15
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
15
        Ty first_number = 0, second_number = 0;
169
15
        size_t count = 0;
170
15
        if (reverse) {
171
14
            std::priority_queue<Node> max_heap;
172
76
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
62
                if (!_sorted_nums_vec[i].empty()) {
174
62
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
62
                                     _sorted_nums_vec[i].size() - 1);
176
62
                }
177
62
            }
178
179
64
            while (!max_heap.empty()) {
180
64
                Node node = max_heap.top();
181
64
                max_heap.pop();
182
64
                if (count == target) {
183
14
                    second_number = node.value;
184
50
                } else if (count == target + 1) {
185
14
                    first_number = node.value;
186
14
                    break;
187
14
                }
188
50
                ++count;
189
50
                if (--node.element_index >= 0) {
190
40
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
40
                    max_heap.push(node);
192
40
                }
193
50
            }
194
195
14
        } else {
196
1
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
3
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
2
                if (!_sorted_nums_vec[i].empty()) {
199
2
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
2
                }
201
2
            }
202
203
7
            while (!min_heap.empty()) {
204
7
                Node node = min_heap.top();
205
7
                min_heap.pop();
206
7
                if (count == target) {
207
1
                    first_number = node.value;
208
6
                } else if (count == target + 1) {
209
1
                    second_number = node.value;
210
1
                    break;
211
1
                }
212
6
                ++count;
213
6
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
6
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
6
                    min_heap.push(node);
216
6
                }
217
6
            }
218
1
        }
219
220
15
        return {first_number, second_number};
221
15
    }
Unexecuted instantiation: _ZN5doris6CountsInE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIfE27_merge_sort_and_get_numbersElb
Unexecuted instantiation: _ZN5doris6CountsIdE27_merge_sort_and_get_numbersElb
222
223
    vectorized::PODArray<Ty> _nums;
224
    std::vector<vectorized::PODArray<Ty>> _sorted_nums_vec;
225
};
226
227
} // namespace doris