Coverage Report

Created: 2026-04-14 05:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/aggregate/moments.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 <stddef.h>
21
22
#include "common/exception.h"
23
#include "common/status.h"
24
#include "util/io_helper.h"
25
26
namespace doris {
27
28
class BufferReadable;
29
class BufferWritable;
30
31
template <typename T, size_t _level>
32
struct VarMoments {
33
    // m[1] = sum(x)
34
    // m[2] = sum(x^2)
35
    // m[3] = sum(x^3)
36
    // m[4] = sum(x^4)
37
    T m[_level + 1] {};
38
39
134
    void add(T x) {
40
134
        ++m[0];
41
134
        m[1] += x;
42
134
        m[2] += x * x;
43
134
        if constexpr (_level >= 3) m[3] += x * x * x;
44
134
        if constexpr (_level >= 4) m[4] += x * x * x * x;
45
134
    }
_ZN5doris10VarMomentsIdLm3EE3addEd
Line
Count
Source
39
67
    void add(T x) {
40
67
        ++m[0];
41
67
        m[1] += x;
42
67
        m[2] += x * x;
43
67
        if constexpr (_level >= 3) m[3] += x * x * x;
44
        if constexpr (_level >= 4) m[4] += x * x * x * x;
45
67
    }
_ZN5doris10VarMomentsIdLm4EE3addEd
Line
Count
Source
39
67
    void add(T x) {
40
67
        ++m[0];
41
67
        m[1] += x;
42
67
        m[2] += x * x;
43
67
        if constexpr (_level >= 3) m[3] += x * x * x;
44
67
        if constexpr (_level >= 4) m[4] += x * x * x * x;
45
67
    }
46
47
47
    void merge(const VarMoments& rhs) {
48
47
        m[0] += rhs.m[0];
49
47
        m[1] += rhs.m[1];
50
47
        m[2] += rhs.m[2];
51
47
        if constexpr (_level >= 3) m[3] += rhs.m[3];
52
47
        if constexpr (_level >= 4) m[4] += rhs.m[4];
53
47
    }
_ZN5doris10VarMomentsIdLm3EE5mergeERKS1_
Line
Count
Source
47
28
    void merge(const VarMoments& rhs) {
48
28
        m[0] += rhs.m[0];
49
28
        m[1] += rhs.m[1];
50
28
        m[2] += rhs.m[2];
51
28
        if constexpr (_level >= 3) m[3] += rhs.m[3];
52
        if constexpr (_level >= 4) m[4] += rhs.m[4];
53
28
    }
_ZN5doris10VarMomentsIdLm4EE5mergeERKS1_
Line
Count
Source
47
19
    void merge(const VarMoments& rhs) {
48
19
        m[0] += rhs.m[0];
49
19
        m[1] += rhs.m[1];
50
19
        m[2] += rhs.m[2];
51
19
        if constexpr (_level >= 3) m[3] += rhs.m[3];
52
19
        if constexpr (_level >= 4) m[4] += rhs.m[4];
53
19
    }
54
55
47
    void write(BufferWritable& buf) const { buf.write_binary(*this); }
_ZNK5doris10VarMomentsIdLm3EE5writeERNS_14BufferWritableE
Line
Count
Source
55
28
    void write(BufferWritable& buf) const { buf.write_binary(*this); }
_ZNK5doris10VarMomentsIdLm4EE5writeERNS_14BufferWritableE
Line
Count
Source
55
19
    void write(BufferWritable& buf) const { buf.write_binary(*this); }
56
57
47
    void read(BufferReadable& buf) { buf.read_binary(*this); }
_ZN5doris10VarMomentsIdLm3EE4readERNS_14BufferReadableE
Line
Count
Source
57
28
    void read(BufferReadable& buf) { buf.read_binary(*this); }
_ZN5doris10VarMomentsIdLm4EE4readERNS_14BufferReadableE
Line
Count
Source
57
19
    void read(BufferReadable& buf) { buf.read_binary(*this); }
58
59
    T get() const {
60
        throw doris::Exception(ErrorCode::INTERNAL_ERROR,
61
                               "Variation moments should be obtained by 'get_population' method");
62
    }
63
64
84
    T get_population() const {
65
84
        if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN();
66
67
        /// Due to numerical errors, the result can be slightly less than zero,
68
        /// but it should be impossible. Trim to zero.
69
70
68
        return std::max(T {}, (m[2] - m[1] * m[1] / m[0]) / m[0]);
71
84
    }
_ZNK5doris10VarMomentsIdLm3EE14get_populationEv
Line
Count
Source
64
42
    T get_population() const {
65
42
        if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN();
66
67
        /// Due to numerical errors, the result can be slightly less than zero,
68
        /// but it should be impossible. Trim to zero.
69
70
34
        return std::max(T {}, (m[2] - m[1] * m[1] / m[0]) / m[0]);
71
42
    }
_ZNK5doris10VarMomentsIdLm4EE14get_populationEv
Line
Count
Source
64
42
    T get_population() const {
65
42
        if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN();
66
67
        /// Due to numerical errors, the result can be slightly less than zero,
68
        /// but it should be impossible. Trim to zero.
69
70
34
        return std::max(T {}, (m[2] - m[1] * m[1] / m[0]) / m[0]);
71
42
    }
72
73
    T get_sample() const {
74
        if (m[0] <= 1) return std::numeric_limits<T>::quiet_NaN();
75
        return std::max(T {}, (m[2] - m[1] * m[1] / m[0]) / (m[0] - 1));
76
    }
77
78
42
    T get_moment_3() const {
79
        if constexpr (_level < 3) {
80
            throw doris::Exception(
81
                    ErrorCode::INTERNAL_ERROR,
82
                    "Variation moments should be obtained by 'get_population' method");
83
42
        } else {
84
42
            if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN();
85
            // to avoid accuracy problem
86
34
            if (m[0] == 1) return 0;
87
            /// \[ \frac{1}{m_0} (m_3 - (3 * m_2 - \frac{2 * {m_1}^2}{m_0}) * \frac{m_1}{m_0});\]
88
14
            return (m[3] - (3 * m[2] - 2 * m[1] * m[1] / m[0]) * m[1] / m[0]) / m[0];
89
34
        }
90
42
    }
_ZNK5doris10VarMomentsIdLm3EE12get_moment_3Ev
Line
Count
Source
78
42
    T get_moment_3() const {
79
        if constexpr (_level < 3) {
80
            throw doris::Exception(
81
                    ErrorCode::INTERNAL_ERROR,
82
                    "Variation moments should be obtained by 'get_population' method");
83
42
        } else {
84
42
            if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN();
85
            // to avoid accuracy problem
86
34
            if (m[0] == 1) return 0;
87
            /// \[ \frac{1}{m_0} (m_3 - (3 * m_2 - \frac{2 * {m_1}^2}{m_0}) * \frac{m_1}{m_0});\]
88
14
            return (m[3] - (3 * m[2] - 2 * m[1] * m[1] / m[0]) * m[1] / m[0]) / m[0];
89
34
        }
90
42
    }
Unexecuted instantiation: _ZNK5doris10VarMomentsIdLm4EE12get_moment_3Ev
91
92
42
    T get_moment_4() const {
93
42
        if constexpr (_level < 4) {
94
0
            throw doris::Exception(
95
0
                    ErrorCode::INTERNAL_ERROR,
96
0
                    "Variation moments should be obtained by 'get_population' method");
97
42
        } else {
98
42
            if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN();
99
            // to avoid accuracy problem
100
34
            if (m[0] == 1) return 0;
101
            /// \[ \frac{1}{m_0}(m_4 - (4 * m_3 - (6 * m_2 - \frac{3 * m_1^2}{m_0} ) \frac{m_1}{m_0})\frac{m_1}{m_0})\]
102
14
            return (m[4] -
103
14
                    (4 * m[3] - (6 * m[2] - 3 * m[1] * m[1] / m[0]) * m[1] / m[0]) * m[1] / m[0]) /
104
14
                   m[0];
105
34
        }
106
42
    }
Unexecuted instantiation: _ZNK5doris10VarMomentsIdLm3EE12get_moment_4Ev
_ZNK5doris10VarMomentsIdLm4EE12get_moment_4Ev
Line
Count
Source
92
42
    T get_moment_4() const {
93
        if constexpr (_level < 4) {
94
            throw doris::Exception(
95
                    ErrorCode::INTERNAL_ERROR,
96
                    "Variation moments should be obtained by 'get_population' method");
97
42
        } else {
98
42
            if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN();
99
            // to avoid accuracy problem
100
34
            if (m[0] == 1) return 0;
101
            /// \[ \frac{1}{m_0}(m_4 - (4 * m_3 - (6 * m_2 - \frac{3 * m_1^2}{m_0} ) \frac{m_1}{m_0})\frac{m_1}{m_0})\]
102
14
            return (m[4] -
103
14
                    (4 * m[3] - (6 * m[2] - 3 * m[1] * m[1] / m[0]) * m[1] / m[0]) * m[1] / m[0]) /
104
14
                   m[0];
105
34
        }
106
42
    }
107
108
    void reset() {
109
        m = {};
110
        return;
111
    }
112
};
113
114
} // namespace doris