Coverage Report

Created: 2026-03-13 03:47

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