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 | 134 | void add(T x) { |
39 | 134 | ++m[0]; |
40 | 134 | m[1] += x; |
41 | 134 | m[2] += x * x; |
42 | 134 | if constexpr (_level >= 3) m[3] += x * x * x; |
43 | 134 | if constexpr (_level >= 4) m[4] += x * x * x * x; |
44 | 134 | } _ZN5doris10VarMomentsIdLm3EE3addEd Line | Count | Source | 38 | 67 | void add(T x) { | 39 | 67 | ++m[0]; | 40 | 67 | m[1] += x; | 41 | 67 | m[2] += x * x; | 42 | 67 | if constexpr (_level >= 3) m[3] += x * x * x; | 43 | | if constexpr (_level >= 4) m[4] += x * x * x * x; | 44 | 67 | } |
_ZN5doris10VarMomentsIdLm4EE3addEd Line | Count | Source | 38 | 67 | void add(T x) { | 39 | 67 | ++m[0]; | 40 | 67 | m[1] += x; | 41 | 67 | m[2] += x * x; | 42 | 67 | if constexpr (_level >= 3) m[3] += x * x * x; | 43 | 67 | if constexpr (_level >= 4) m[4] += x * x * x * x; | 44 | 67 | } |
|
45 | | |
46 | 47 | void merge(const VarMoments& rhs) { |
47 | 47 | m[0] += rhs.m[0]; |
48 | 47 | m[1] += rhs.m[1]; |
49 | 47 | m[2] += rhs.m[2]; |
50 | 47 | if constexpr (_level >= 3) m[3] += rhs.m[3]; |
51 | 47 | if constexpr (_level >= 4) m[4] += rhs.m[4]; |
52 | 47 | } _ZN5doris10VarMomentsIdLm3EE5mergeERKS1_ Line | Count | Source | 46 | 19 | void merge(const VarMoments& rhs) { | 47 | 19 | m[0] += rhs.m[0]; | 48 | 19 | m[1] += rhs.m[1]; | 49 | 19 | m[2] += rhs.m[2]; | 50 | 19 | if constexpr (_level >= 3) m[3] += rhs.m[3]; | 51 | | if constexpr (_level >= 4) m[4] += rhs.m[4]; | 52 | 19 | } |
_ZN5doris10VarMomentsIdLm4EE5mergeERKS1_ Line | Count | Source | 46 | 28 | void merge(const VarMoments& rhs) { | 47 | 28 | m[0] += rhs.m[0]; | 48 | 28 | m[1] += rhs.m[1]; | 49 | 28 | m[2] += rhs.m[2]; | 50 | 28 | if constexpr (_level >= 3) m[3] += rhs.m[3]; | 51 | 28 | if constexpr (_level >= 4) m[4] += rhs.m[4]; | 52 | 28 | } |
|
53 | | |
54 | 47 | void write(BufferWritable& buf) const { buf.write_binary(*this); }_ZNK5doris10VarMomentsIdLm3EE5writeERNS_14BufferWritableE Line | Count | Source | 54 | 19 | void write(BufferWritable& buf) const { buf.write_binary(*this); } |
_ZNK5doris10VarMomentsIdLm4EE5writeERNS_14BufferWritableE Line | Count | Source | 54 | 28 | void write(BufferWritable& buf) const { buf.write_binary(*this); } |
|
55 | | |
56 | 47 | void read(BufferReadable& buf) { buf.read_binary(*this); }_ZN5doris10VarMomentsIdLm3EE4readERNS_14BufferReadableE Line | Count | Source | 56 | 19 | void read(BufferReadable& buf) { buf.read_binary(*this); } |
_ZN5doris10VarMomentsIdLm4EE4readERNS_14BufferReadableE Line | Count | Source | 56 | 28 | void read(BufferReadable& buf) { buf.read_binary(*this); } |
|
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 | 84 | T get_population() const { |
64 | 84 | 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 | 68 | return std::max(T {}, (m[2] - m[1] * m[1] / m[0]) / m[0]); |
70 | 84 | } _ZNK5doris10VarMomentsIdLm3EE14get_populationEv Line | Count | Source | 63 | 42 | T get_population() const { | 64 | 42 | 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 | 34 | return std::max(T {}, (m[2] - m[1] * m[1] / m[0]) / m[0]); | 70 | 42 | } |
_ZNK5doris10VarMomentsIdLm4EE14get_populationEv Line | Count | Source | 63 | 42 | T get_population() const { | 64 | 42 | 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 | 34 | return std::max(T {}, (m[2] - m[1] * m[1] / m[0]) / m[0]); | 70 | 42 | } |
|
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 | 42 | 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 | 42 | } else { |
83 | 42 | if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN(); |
84 | | // to avoid accuracy problem |
85 | 34 | 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 | 14 | return (m[3] - (3 * m[2] - 2 * m[1] * m[1] / m[0]) * m[1] / m[0]) / m[0]; |
88 | 34 | } |
89 | 42 | } _ZNK5doris10VarMomentsIdLm3EE12get_moment_3Ev Line | Count | Source | 77 | 42 | 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 | 42 | } else { | 83 | 42 | if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN(); | 84 | | // to avoid accuracy problem | 85 | 34 | 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 | 14 | return (m[3] - (3 * m[2] - 2 * m[1] * m[1] / m[0]) * m[1] / m[0]) / m[0]; | 88 | 34 | } | 89 | 42 | } |
Unexecuted instantiation: _ZNK5doris10VarMomentsIdLm4EE12get_moment_3Ev |
90 | | |
91 | 42 | T get_moment_4() const { |
92 | 42 | 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 | 42 | } else { |
97 | 42 | if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN(); |
98 | | // to avoid accuracy problem |
99 | 34 | 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 | 14 | return (m[4] - |
102 | 14 | (4 * m[3] - (6 * m[2] - 3 * m[1] * m[1] / m[0]) * m[1] / m[0]) * m[1] / m[0]) / |
103 | 14 | m[0]; |
104 | 34 | } |
105 | 42 | } Unexecuted instantiation: _ZNK5doris10VarMomentsIdLm3EE12get_moment_4Ev _ZNK5doris10VarMomentsIdLm4EE12get_moment_4Ev Line | Count | Source | 91 | 42 | T get_moment_4() const { | 92 | | if constexpr (_level < 4) { | 93 | | throw doris::Exception( | 94 | | ErrorCode::INTERNAL_ERROR, | 95 | | "Variation moments should be obtained by 'get_population' method"); | 96 | 42 | } else { | 97 | 42 | if (m[0] == 0) return std::numeric_limits<T>::quiet_NaN(); | 98 | | // to avoid accuracy problem | 99 | 34 | 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 | 14 | return (m[4] - | 102 | 14 | (4 * m[3] - (6 * m[2] - 3 * m[1] * m[1] / m[0]) * m[1] / m[0]) * m[1] / m[0]) / | 103 | 14 | m[0]; | 104 | 34 | } | 105 | 42 | } |
|
106 | | |
107 | | void reset() { |
108 | | m = {}; |
109 | | return; |
110 | | } |
111 | | }; |
112 | | |
113 | | } // namespace doris |