Coverage Report

Created: 2026-04-15 12:36

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
0
    void add(T x) {
40
0
        ++m[0];
41
0
        m[1] += x;
42
0
        m[2] += x * x;
43
0
        if constexpr (_level >= 3) m[3] += x * x * x;
44
0
        if constexpr (_level >= 4) m[4] += x * x * x * x;
45
0
    }
Unexecuted instantiation: _ZN5doris10VarMomentsIdLm4EE3addEd
Unexecuted instantiation: _ZN5doris10VarMomentsIdLm3EE3addEd
46
47
0
    void merge(const VarMoments& rhs) {
48
0
        m[0] += rhs.m[0];
49
0
        m[1] += rhs.m[1];
50
0
        m[2] += rhs.m[2];
51
0
        if constexpr (_level >= 3) m[3] += rhs.m[3];
52
0
        if constexpr (_level >= 4) m[4] += rhs.m[4];
53
0
    }
Unexecuted instantiation: _ZN5doris10VarMomentsIdLm4EE5mergeERKS1_
Unexecuted instantiation: _ZN5doris10VarMomentsIdLm3EE5mergeERKS1_
54
55
0
    void write(BufferWritable& buf) const { buf.write_binary(*this); }
Unexecuted instantiation: _ZNK5doris10VarMomentsIdLm4EE5writeERNS_14BufferWritableE
Unexecuted instantiation: _ZNK5doris10VarMomentsIdLm3EE5writeERNS_14BufferWritableE
56
57
0
    void read(BufferReadable& buf) { buf.read_binary(*this); }
Unexecuted instantiation: _ZN5doris10VarMomentsIdLm4EE4readERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris10VarMomentsIdLm3EE4readERNS_14BufferReadableE
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
0
    T get_population() const {
65
0
        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
0
        return std::max(T {}, (m[2] - m[1] * m[1] / m[0]) / m[0]);
71
0
    }
Unexecuted instantiation: _ZNK5doris10VarMomentsIdLm4EE14get_populationEv
Unexecuted instantiation: _ZNK5doris10VarMomentsIdLm3EE14get_populationEv
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
0
    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
0
        } else {
84
0
            if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN();
85
            // to avoid accuracy problem
86
0
            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
0
            return (m[3] - (3 * m[2] - 2 * m[1] * m[1] / m[0]) * m[1] / m[0]) / m[0];
89
0
        }
90
0
    }
Unexecuted instantiation: _ZNK5doris10VarMomentsIdLm4EE12get_moment_3Ev
Unexecuted instantiation: _ZNK5doris10VarMomentsIdLm3EE12get_moment_3Ev
91
92
0
    T get_moment_4() const {
93
0
        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
0
        } else {
98
0
            if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN();
99
            // to avoid accuracy problem
100
0
            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
0
            return (m[4] -
103
0
                    (4 * m[3] - (6 * m[2] - 3 * m[1] * m[1] / m[0]) * m[1] / m[0]) * m[1] / m[0]) /
104
0
                   m[0];
105
0
        }
106
0
    }
Unexecuted instantiation: _ZNK5doris10VarMomentsIdLm4EE12get_moment_4Ev
Unexecuted instantiation: _ZNK5doris10VarMomentsIdLm3EE12get_moment_4Ev
107
108
    void reset() {
109
        m = {};
110
        return;
111
    }
112
};
113
114
} // namespace doris