Coverage Report

Created: 2026-09-17 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/aggregate/aggregate_function_ema.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
// This file is adapted from
19
// https://github.com/ClickHouse/ClickHouse/blob/master/src/AggregateFunctions/AggregateFunctionExponentialMovingAverage.cpp
20
21
#pragma once
22
23
#include <cmath>
24
#include <limits>
25
#include <memory>
26
27
#include "common/exception.h"
28
#include "core/assert_cast.h"
29
#include "core/column/column_vector.h"
30
#include "core/data_type/data_type_number.h"
31
#include "core/types.h"
32
#include "exprs/aggregate/aggregate_function.h"
33
34
namespace doris {
35
class Arena;
36
class BufferReadable;
37
class BufferWritable;
38
class IColumn;
39
40
/**
41
 * Exponentially smoothed moving average over time.
42
 *
43
 * Each value corresponds to a timeunit index. The half_decay parameter is the
44
 * time lag at which exponential weights decay by one-half.
45
 *
46
 * State is a (value, time) pair representing the exponentially accumulated sum
47
 * at a reference time. To get the average, divide by sumWeights(half_decay).
48
 *
49
 * Formula:
50
 *   scale(dt, x) = 2^(-dt/x)
51
 *   sumWeights(x) = 1 / (1 - 2^(-1/x))
52
 *   add(v, t): merge current state with point (v, t)
53
 *   merge(a, b): move both to the later time, then sum values
54
 *   get():  value / sumWeights(half_decay)
55
 *
56
 * Usage: exponential_moving_average(half_decay, value, timeunit)
57
 *   - half_decay: constant double, the half-life period in timeunit units
58
 *   - value:      numeric column to average
59
 *   - timeunit:   numeric time index (not raw timestamp; use intDiv if needed)
60
 * Returns DOUBLE.
61
 *
62
 * A zero half_decay returns 0 but remains an initialized configuration. Only
63
 * fresh/reset states are identities; all initialized states must have matching
64
 * half decays when merged.
65
 */
66
struct ExponentialMovingAverageData {
67
    double value = 0.0;
68
    double time = 0.0;
69
    double half_decay = 0.0;
70
    bool initialized = false;
71
72
88
    static double scale(double time_passed, double hd) { return std::exp2(-time_passed / hd); }
73
74
22
    static double sum_weights(double hd) { return 1.0 / (1.0 - std::exp2(-1.0 / hd)); }
75
76
79
    void add(double new_value, double current_time, double hd) {
77
79
        half_decay = hd;
78
79
        initialized = true;
79
79
        ExponentialMovingAverageData other;
80
79
        other.value = new_value;
81
79
        other.time = current_time;
82
79
        merge_point(other, hd);
83
79
    }
84
85
90
    void merge_point(const ExponentialMovingAverageData& other, double hd) {
86
90
        if (time > other.time) {
87
26
            value = value + other.value * scale(time - other.time, hd);
88
64
        } else if (time < other.time) {
89
62
            value = other.value + value * scale(other.time - time, hd);
90
62
            time = other.time;
91
62
        } else {
92
2
            value = value + other.value;
93
2
        }
94
90
    }
95
96
0
    void merge(const ExponentialMovingAverageData& rhs) {
97
0
        if (!rhs.initialized) {
98
0
            return;
99
0
        }
100
0
        if (!initialized) {
101
0
            *this = rhs;
102
0
            return;
103
0
        }
104
0
        if (UNLIKELY(half_decay != rhs.half_decay)) {
105
0
            throw Exception(
106
0
                    ErrorCode::INVALID_ARGUMENT,
107
0
                    "exponential_moving_average aggregate states have incompatible half decay");
108
0
        }
109
0
        merge_point(rhs, half_decay);
110
0
    }
111
112
23
    double get() const {
113
23
        check_half_decay();
114
23
        if (half_decay == 0.0) {
115
1
            return 0.0;
116
1
        }
117
22
        return value / sum_weights(half_decay);
118
23
    }
119
120
0
    void write(BufferWritable& buf) const {
121
0
        check_half_decay();
122
0
        buf.write_binary(value);
123
0
        buf.write_binary(time);
124
        // NaN is rejected for configured states, so it can encode initialization without
125
        // adding a field or colliding with the valid zero half-decay configuration.
126
0
        buf.write_binary(initialized ? half_decay : std::numeric_limits<double>::quiet_NaN());
127
0
    }
128
129
0
    void read(BufferReadable& buf) {
130
0
        buf.read_binary(value);
131
0
        buf.read_binary(time);
132
0
        buf.read_binary(half_decay);
133
0
        initialized = !std::isnan(half_decay);
134
0
        if (!initialized) {
135
0
            half_decay = 0.0;
136
0
        }
137
0
    }
138
139
0
    void check_half_decay() const {
140
0
        if (UNLIKELY(std::isnan(half_decay))) {
141
0
            throw Exception(ErrorCode::INVALID_ARGUMENT,
142
0
                            "exponential_moving_average half decay must not be NaN");
143
0
        }
144
0
    }
145
146
0
    void reset() {
147
0
        value = 0.0;
148
0
        time = 0.0;
149
0
        half_decay = 0.0;
150
0
        initialized = false;
151
0
    }
152
};
153
154
class AggregateFunctionExponentialMovingAverage final
155
        : public IAggregateFunctionDataHelper<ExponentialMovingAverageData,
156
                                              AggregateFunctionExponentialMovingAverage>,
157
          MultiExpression,
158
          NullableAggregateFunction {
159
public:
160
    AggregateFunctionExponentialMovingAverage(const DataTypes& argument_types_)
161
24
            : IAggregateFunctionDataHelper<ExponentialMovingAverageData,
162
24
                                           AggregateFunctionExponentialMovingAverage>(
163
24
                      argument_types_) {}
164
165
1
    String get_name() const override { return "exponential_moving_average"; }
166
167
101
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeFloat64>(); }
168
169
0
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
170
171
    void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num,
172
79
             Arena&) const override {
173
79
        const double half_decay =
174
79
                assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[0])
175
79
                        .get_data()[row_num];
176
79
        const double new_value =
177
79
                assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[1])
178
79
                        .get_data()[row_num];
179
79
        const double current_time =
180
79
                assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[2])
181
79
                        .get_data()[row_num];
182
79
        this->data(place).add(new_value, current_time, half_decay);
183
79
    }
184
185
20
    void check_input_columns_type(const IColumn** columns) const override {
186
20
        this->template check_argument_column_type<ColumnFloat64>(columns[0]);
187
20
        this->template check_argument_column_type<ColumnFloat64>(columns[1]);
188
20
        this->template check_argument_column_type<ColumnFloat64>(columns[2]);
189
20
    }
190
191
    void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
192
12
               Arena&) const override {
193
12
        this->data(place).merge(this->data(rhs));
194
12
    }
195
196
12
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
197
12
        this->data(place).write(buf);
198
12
    }
199
200
    void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
201
12
                     Arena&) const override {
202
12
        this->data(place).read(buf);
203
12
    }
204
205
23
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
206
23
        assert_cast<ColumnFloat64&, TypeCheckOnRelease::DISABLE>(to).get_data().push_back(
207
23
                this->data(place).get());
208
23
    }
209
};
210
211
} // namespace doris