Coverage Report

Created: 2026-04-22 18:58

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