Coverage Report

Created: 2025-05-01 14:43

/root/doris/be/src/util/counts.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <pdqsort.h>
21
22
#include <algorithm>
23
#include <cmath>
24
#include <queue>
25
26
#include "udf/udf.h"
27
#include "vec/common/pod_array.h"
28
#include "vec/common/string_buffer.hpp"
29
#include "vec/io/io_helper.h"
30
31
namespace doris {
32
33
template <typename Ty>
34
class Counts {
35
public:
36
5.09k
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIhEC2Ev
_ZN5doris6CountsIaEC2Ev
Line
Count
Source
36
68
    Counts() = default;
_ZN5doris6CountsIsEC2Ev
Line
Count
Source
36
486
    Counts() = default;
_ZN5doris6CountsIiEC2Ev
Line
Count
Source
36
2.61k
    Counts() = default;
_ZN5doris6CountsIlEC2Ev
Line
Count
Source
36
1.71k
    Counts() = default;
_ZN5doris6CountsInEC2Ev
Line
Count
Source
36
56
    Counts() = default;
Unexecuted instantiation: _ZN5doris6CountsIfEC2Ev
_ZN5doris6CountsIdEC2Ev
Line
Count
Source
36
156
    Counts() = default;
37
38
1.73k
    void merge(Counts* other) {
39
1.73k
        if (other != nullptr && !other->_nums.empty()) {
40
1.73k
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
1.73k
        }
42
1.73k
    }
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
996
    void merge(Counts* other) {
39
996
        if (other != nullptr && !other->_nums.empty()) {
40
996
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
996
        }
42
996
    }
_ZN5doris6CountsIlE5mergeEPS1_
Line
Count
Source
38
700
    void merge(Counts* other) {
39
700
        if (other != nullptr && !other->_nums.empty()) {
40
700
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
700
        }
42
700
    }
Unexecuted instantiation: _ZN5doris6CountsInE5mergeEPS1_
Unexecuted instantiation: _ZN5doris6CountsIfE5mergeEPS1_
_ZN5doris6CountsIdE5mergeEPS1_
Line
Count
Source
38
16
    void merge(Counts* other) {
39
16
        if (other != nullptr && !other->_nums.empty()) {
40
16
            _sorted_nums_vec.emplace_back(std::move(other->_nums));
41
16
        }
42
16
    }
43
44
    void increment(Ty key, uint32_t i) {
45
        auto old_size = _nums.size();
46
        _nums.resize(_nums.size() + i);
47
        for (uint32_t j = 0; j < i; ++j) {
48
            _nums[old_size + j] = key;
49
        }
50
    }
51
52
4.78k
    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.57k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIiE9incrementEi
Line
Count
Source
52
1.83k
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsIlE9incrementEl
Line
Count
Source
52
824
    void increment(Ty key) { _nums.push_back(key); }
_ZN5doris6CountsInE9incrementEn
Line
Count
Source
52
72
    void increment(Ty key) { _nums.push_back(key); }
Unexecuted instantiation: _ZN5doris6CountsIfE9incrementEf
_ZN5doris6CountsIdE9incrementEd
Line
Count
Source
52
344
    void increment(Ty key) { _nums.push_back(key); }
53
54
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_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
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_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIfE15increment_batchERKNS_10vectorized8PODArrayIfLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
Unexecuted instantiation: _ZN5doris6CountsIdE15increment_batchERKNS_10vectorized8PODArrayIdLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorEEELm16ELm15EEE
57
58
2.68k
    void serialize(vectorized::BufferWritable& buf) {
59
2.68k
        if (!_nums.empty()) {
60
1.95k
            pdqsort(_nums.begin(), _nums.end());
61
1.95k
            size_t size = _nums.size();
62
1.95k
            write_binary(size, buf);
63
1.95k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.95k
        } 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
2.68k
    }
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
            write_binary(size, buf);
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
1.54k
    void serialize(vectorized::BufferWritable& buf) {
59
1.54k
        if (!_nums.empty()) {
60
1.15k
            pdqsort(_nums.begin(), _nums.end());
61
1.15k
            size_t size = _nums.size();
62
1.15k
            write_binary(size, buf);
63
1.15k
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
1.15k
        } 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
1.54k
    }
_ZN5doris6CountsIlE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
1.09k
    void serialize(vectorized::BufferWritable& buf) {
59
1.09k
        if (!_nums.empty()) {
60
764
            pdqsort(_nums.begin(), _nums.end());
61
764
            size_t size = _nums.size();
62
764
            write_binary(size, buf);
63
764
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
764
        } 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.09k
    }
Unexecuted instantiation: _ZN5doris6CountsInE9serializeERNS_10vectorized14BufferWritableE
Unexecuted instantiation: _ZN5doris6CountsIfE9serializeERNS_10vectorized14BufferWritableE
_ZN5doris6CountsIdE9serializeERNS_10vectorized14BufferWritableE
Line
Count
Source
58
16
    void serialize(vectorized::BufferWritable& buf) {
59
16
        if (!_nums.empty()) {
60
16
            pdqsort(_nums.begin(), _nums.end());
61
16
            size_t size = _nums.size();
62
16
            write_binary(size, buf);
63
16
            buf.write(reinterpret_cast<const char*>(_nums.data()), sizeof(Ty) * size);
64
16
        } else {
65
            // convert _sorted_nums_vec to _nums and do seiralize again
66
0
            _convert_sorted_num_vec_to_nums();
67
0
            serialize(buf);
68
0
        }
69
16
    }
70
71
1.73k
    void unserialize(vectorized::BufferReadable& buf) {
72
1.73k
        size_t size;
73
1.73k
        read_binary(size, buf);
74
1.73k
        _nums.resize(size);
75
1.73k
        auto buff = buf.read(sizeof(Ty) * size);
76
1.73k
        memcpy(_nums.data(), buff.data, buff.size);
77
1.73k
    }
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
        read_binary(size, buf);
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
996
    void unserialize(vectorized::BufferReadable& buf) {
72
996
        size_t size;
73
996
        read_binary(size, buf);
74
996
        _nums.resize(size);
75
996
        auto buff = buf.read(sizeof(Ty) * size);
76
996
        memcpy(_nums.data(), buff.data, buff.size);
77
996
    }
_ZN5doris6CountsIlE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
700
    void unserialize(vectorized::BufferReadable& buf) {
72
700
        size_t size;
73
700
        read_binary(size, buf);
74
700
        _nums.resize(size);
75
700
        auto buff = buf.read(sizeof(Ty) * size);
76
700
        memcpy(_nums.data(), buff.data, buff.size);
77
700
    }
Unexecuted instantiation: _ZN5doris6CountsInE11unserializeERNS_10vectorized14BufferReadableE
Unexecuted instantiation: _ZN5doris6CountsIfE11unserializeERNS_10vectorized14BufferReadableE
_ZN5doris6CountsIdE11unserializeERNS_10vectorized14BufferReadableE
Line
Count
Source
71
16
    void unserialize(vectorized::BufferReadable& buf) {
72
16
        size_t size;
73
16
        read_binary(size, buf);
74
16
        _nums.resize(size);
75
16
        auto buff = buf.read(sizeof(Ty) * size);
76
16
        memcpy(_nums.data(), buff.data, buff.size);
77
16
    }
78
79
1.67k
    double terminate(double quantile) {
80
1.67k
        if (_sorted_nums_vec.size() <= 1) {
81
1.60k
            if (_sorted_nums_vec.size() == 1) {
82
238
                _nums = std::move(_sorted_nums_vec[0]);
83
238
            }
84
85
1.60k
            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.60k
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
401
                pdqsort(_nums.begin(), _nums.end());
93
401
            }
94
95
1.60k
            if (quantile == 1 || _nums.size() == 1) {
96
650
                return _nums.back();
97
650
            }
98
99
950
            double u = (_nums.size() - 1) * quantile;
100
950
            auto index = static_cast<uint32_t>(u);
101
950
            return _nums[index] +
102
950
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
1.60k
        } else {
104
77
            DCHECK(_nums.empty());
105
77
            size_t rows = 0;
106
222
            for (const auto& i : _sorted_nums_vec) {
107
222
                rows += i.size();
108
222
            }
109
77
            const bool reverse = quantile > 0.5 && rows > 2;
110
77
            double u = (rows - 1) * quantile;
111
77
            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
77
            size_t target = reverse ? rows - index - 2 : index;
120
77
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
77
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
77
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
77
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
77
        }
129
1.67k
    }
Unexecuted instantiation: _ZN5doris6CountsIhE9terminateEd
_ZN5doris6CountsIaE9terminateEd
Line
Count
Source
79
116
    double terminate(double quantile) {
80
116
        if (_sorted_nums_vec.size() <= 1) {
81
116
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
116
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
116
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
48
                pdqsort(_nums.begin(), _nums.end());
93
48
            }
94
95
116
            if (quantile == 1 || _nums.size() == 1) {
96
68
                return _nums.back();
97
68
            }
98
99
48
            double u = (_nums.size() - 1) * quantile;
100
48
            auto index = static_cast<uint32_t>(u);
101
48
            return _nums[index] +
102
48
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
116
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
0
        }
129
116
    }
_ZN5doris6CountsIsE9terminateEd
Line
Count
Source
79
594
    double terminate(double quantile) {
80
594
        if (_sorted_nums_vec.size() <= 1) {
81
588
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
588
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
588
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
330
                pdqsort(_nums.begin(), _nums.end());
93
330
            }
94
95
588
            if (quantile == 1 || _nums.size() == 1) {
96
174
                return _nums.back();
97
174
            }
98
99
414
            double u = (_nums.size() - 1) * quantile;
100
414
            auto index = static_cast<uint32_t>(u);
101
414
            return _nums[index] +
102
414
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
588
        } else {
104
6
            DCHECK(_nums.empty());
105
6
            size_t rows = 0;
106
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
594
    }
_ZN5doris6CountsIiE9terminateEd
Line
Count
Source
79
462
    double terminate(double quantile) {
80
462
        if (_sorted_nums_vec.size() <= 1) {
81
420
            if (_sorted_nums_vec.size() == 1) {
82
156
                _nums = std::move(_sorted_nums_vec[0]);
83
156
            }
84
85
420
            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
420
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
22
                pdqsort(_nums.begin(), _nums.end());
93
22
            }
94
95
420
            if (quantile == 1 || _nums.size() == 1) {
96
192
                return _nums.back();
97
192
            }
98
99
228
            double u = (_nums.size() - 1) * quantile;
100
228
            auto index = static_cast<uint32_t>(u);
101
228
            return _nums[index] +
102
228
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
420
        } else {
104
42
            DCHECK(_nums.empty());
105
42
            size_t rows = 0;
106
84
            for (const auto& i : _sorted_nums_vec) {
107
84
                rows += i.size();
108
84
            }
109
42
            const bool reverse = quantile > 0.5 && rows > 2;
110
42
            double u = (rows - 1) * quantile;
111
42
            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
42
            size_t target = reverse ? rows - index - 2 : index;
120
42
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
42
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
42
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
42
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
42
        }
129
462
    }
_ZN5doris6CountsIlE9terminateEd
Line
Count
Source
79
233
    double terminate(double quantile) {
80
233
        if (_sorted_nums_vec.size() <= 1) {
81
204
            if (_sorted_nums_vec.size() == 1) {
82
66
                _nums = std::move(_sorted_nums_vec[0]);
83
66
            }
84
85
204
            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
204
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
1
                pdqsort(_nums.begin(), _nums.end());
93
1
            }
94
95
204
            if (quantile == 1 || _nums.size() == 1) {
96
120
                return _nums.back();
97
120
            }
98
99
84
            double u = (_nums.size() - 1) * quantile;
100
84
            auto index = static_cast<uint32_t>(u);
101
84
            return _nums[index] +
102
84
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
204
        } else {
104
29
            DCHECK(_nums.empty());
105
29
            size_t rows = 0;
106
114
            for (const auto& i : _sorted_nums_vec) {
107
114
                rows += i.size();
108
114
            }
109
29
            const bool reverse = quantile > 0.5 && rows > 2;
110
29
            double u = (rows - 1) * quantile;
111
29
            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
29
            size_t target = reverse ? rows - index - 2 : index;
120
29
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
29
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
29
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
29
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
29
        }
129
233
    }
_ZN5doris6CountsInE9terminateEd
Line
Count
Source
79
60
    double terminate(double quantile) {
80
60
        if (_sorted_nums_vec.size() <= 1) {
81
60
            if (_sorted_nums_vec.size() == 1) {
82
0
                _nums = std::move(_sorted_nums_vec[0]);
83
0
            }
84
85
60
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
60
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
60
            if (quantile == 1 || _nums.size() == 1) {
96
48
                return _nums.back();
97
48
            }
98
99
12
            double u = (_nums.size() - 1) * quantile;
100
12
            auto index = static_cast<uint32_t>(u);
101
12
            return _nums[index] +
102
12
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
60
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
0
        }
129
60
    }
Unexecuted instantiation: _ZN5doris6CountsIfE9terminateEd
_ZN5doris6CountsIdE9terminateEd
Line
Count
Source
79
212
    double terminate(double quantile) {
80
212
        if (_sorted_nums_vec.size() <= 1) {
81
212
            if (_sorted_nums_vec.size() == 1) {
82
16
                _nums = std::move(_sorted_nums_vec[0]);
83
16
            }
84
85
212
            if (_nums.empty()) {
86
                // Although set null here, but the value is 0.0 and the call method just
87
                // get val in aggregate_function_percentile_approx.h
88
0
                return 0.0;
89
0
            }
90
91
212
            if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
92
0
                pdqsort(_nums.begin(), _nums.end());
93
0
            }
94
95
212
            if (quantile == 1 || _nums.size() == 1) {
96
48
                return _nums.back();
97
48
            }
98
99
164
            double u = (_nums.size() - 1) * quantile;
100
164
            auto index = static_cast<uint32_t>(u);
101
164
            return _nums[index] +
102
164
                   (u - static_cast<double>(index)) * (_nums[index + 1] - _nums[index]);
103
212
        } else {
104
0
            DCHECK(_nums.empty());
105
0
            size_t rows = 0;
106
0
            for (const auto& i : _sorted_nums_vec) {
107
0
                rows += i.size();
108
0
            }
109
0
            const bool reverse = quantile > 0.5 && rows > 2;
110
0
            double u = (rows - 1) * quantile;
111
0
            auto index = static_cast<uint32_t>(u);
112
            // if reverse, the step of target should start 0 like not reverse
113
            // so here rows need to minus index + 2
114
            // eg: rows = 10, index = 5
115
            // if not reverse, so the first number loc is 5, the second number loc is 6
116
            // if reverse, so the second number is 3, the first number is 4
117
            // 5 + 4 = 3 + 6 = 9 = rows - 1.
118
            // the rows must GE 2 beacuse `_sorted_nums_vec` size GE 2
119
0
            size_t target = reverse ? rows - index - 2 : index;
120
0
            if (quantile == 1) {
121
0
                target = 0;
122
0
            }
123
0
            auto [first_number, second_number] = _merge_sort_and_get_numbers(target, reverse);
124
0
            if (quantile == 1) {
125
0
                return second_number;
126
0
            }
127
0
            return first_number + (u - static_cast<double>(index)) * (second_number - first_number);
128
0
        }
129
212
    }
130
131
private:
132
    struct Node {
133
        Ty value;
134
        int array_index;
135
        int64_t element_index;
136
137
1.59k
        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
768
        auto operator<=>(const Node& other) const { return value <=> other.value; }
_ZNK5doris6CountsIlE4NodessERKS2_
Line
Count
Source
137
687
        auto operator<=>(const Node& other) const { return value <=> other.value; }
Unexecuted instantiation: _ZNK5doris6CountsInE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIfE4NodessERKS2_
Unexecuted instantiation: _ZNK5doris6CountsIdE4NodessERKS2_
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
77
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
77
        Ty first_number = 0, second_number = 0;
169
77
        size_t count = 0;
170
77
        if (reverse) {
171
32
            std::priority_queue<Node> max_heap;
172
156
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
124
                if (!_sorted_nums_vec[i].empty()) {
174
124
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
124
                                     _sorted_nums_vec[i].size() - 1);
176
124
                }
177
124
            }
178
179
182
            while (!max_heap.empty()) {
180
182
                Node node = max_heap.top();
181
182
                max_heap.pop();
182
182
                if (count == target) {
183
32
                    second_number = node.value;
184
150
                } else if (count == target + 1) {
185
32
                    first_number = node.value;
186
32
                    break;
187
32
                }
188
150
                ++count;
189
150
                if (--node.element_index >= 0) {
190
124
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
124
                    max_heap.push(node);
192
124
                }
193
150
            }
194
195
45
        } else {
196
45
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
143
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
98
                if (!_sorted_nums_vec[i].empty()) {
199
98
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
98
                }
201
98
            }
202
203
181
            while (!min_heap.empty()) {
204
181
                Node node = min_heap.top();
205
181
                min_heap.pop();
206
181
                if (count == target) {
207
45
                    first_number = node.value;
208
136
                } else if (count == target + 1) {
209
45
                    second_number = node.value;
210
45
                    break;
211
45
                }
212
136
                ++count;
213
136
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
136
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
136
                    min_heap.push(node);
216
136
                }
217
136
            }
218
45
        }
219
220
77
        return {first_number, second_number};
221
77
    }
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
42
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
42
        Ty first_number = 0, second_number = 0;
169
42
        size_t count = 0;
170
42
        if (reverse) {
171
6
            std::priority_queue<Node> max_heap;
172
18
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
173
12
                if (!_sorted_nums_vec[i].empty()) {
174
12
                    max_heap.emplace(_sorted_nums_vec[i][_sorted_nums_vec[i].size() - 1], i,
175
12
                                     _sorted_nums_vec[i].size() - 1);
176
12
                }
177
12
            }
178
179
48
            while (!max_heap.empty()) {
180
48
                Node node = max_heap.top();
181
48
                max_heap.pop();
182
48
                if (count == target) {
183
6
                    second_number = node.value;
184
42
                } else if (count == target + 1) {
185
6
                    first_number = node.value;
186
6
                    break;
187
6
                }
188
42
                ++count;
189
42
                if (--node.element_index >= 0) {
190
42
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
42
                    max_heap.push(node);
192
42
                }
193
42
            }
194
195
36
        } else {
196
36
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
108
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
72
                if (!_sorted_nums_vec[i].empty()) {
199
72
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
72
                }
201
72
            }
202
203
138
            while (!min_heap.empty()) {
204
138
                Node node = min_heap.top();
205
138
                min_heap.pop();
206
138
                if (count == target) {
207
36
                    first_number = node.value;
208
102
                } else if (count == target + 1) {
209
36
                    second_number = node.value;
210
36
                    break;
211
36
                }
212
102
                ++count;
213
102
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
102
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
102
                    min_heap.push(node);
216
102
                }
217
102
            }
218
36
        }
219
220
42
        return {first_number, second_number};
221
42
    }
_ZN5doris6CountsIlE27_merge_sort_and_get_numbersElb
Line
Count
Source
167
29
    std::pair<Ty, Ty> _merge_sort_and_get_numbers(int64_t target, bool reverse) {
168
29
        Ty first_number = 0, second_number = 0;
169
29
        size_t count = 0;
170
29
        if (reverse) {
171
24
            std::priority_queue<Node> max_heap;
172
128
            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
122
            while (!max_heap.empty()) {
180
122
                Node node = max_heap.top();
181
122
                max_heap.pop();
182
122
                if (count == target) {
183
24
                    second_number = node.value;
184
98
                } else if (count == target + 1) {
185
24
                    first_number = node.value;
186
24
                    break;
187
24
                }
188
98
                ++count;
189
98
                if (--node.element_index >= 0) {
190
76
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
191
76
                    max_heap.push(node);
192
76
                }
193
98
            }
194
195
24
        } else {
196
5
            std::priority_queue<Node, std::vector<Node>, std::greater<Node>> min_heap;
197
15
            for (int i = 0; i < _sorted_nums_vec.size(); ++i) {
198
10
                if (!_sorted_nums_vec[i].empty()) {
199
10
                    min_heap.emplace(_sorted_nums_vec[i][0], i, 0);
200
10
                }
201
10
            }
202
203
17
            while (!min_heap.empty()) {
204
17
                Node node = min_heap.top();
205
17
                min_heap.pop();
206
17
                if (count == target) {
207
5
                    first_number = node.value;
208
12
                } else if (count == target + 1) {
209
5
                    second_number = node.value;
210
5
                    break;
211
5
                }
212
12
                ++count;
213
12
                if (++node.element_index < _sorted_nums_vec[node.array_index].size()) {
214
12
                    node.value = _sorted_nums_vec[node.array_index][node.element_index];
215
12
                    min_heap.push(node);
216
12
                }
217
12
            }
218
5
        }
219
220
29
        return {first_number, second_number};
221
29
    }
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