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