Coverage Report

Created: 2026-09-19 06:21

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
237
    static double scale(double time_passed, double hd) { return std::exp2(-time_passed / hd); }
73
74
81
    static double sum_weights(double hd) { return 1.0 / (1.0 - std::exp2(-1.0 / hd)); }
75
76
286
    void add(double new_value, double current_time, double hd) {
77
286
        half_decay = hd;
78
286
        initialized = true;
79
286
        ExponentialMovingAverageData other;
80
286
        other.value = new_value;
81
286
        other.time = current_time;
82
286
        merge_point(other, hd);
83
286
    }
84
85
309
    void merge_point(const ExponentialMovingAverageData& other, double hd) {
86
309
        if (time > other.time) {
87
34
            value = value + other.value * scale(time - other.time, hd);
88
275
        } else if (time < other.time) {
89
203
            value = other.value + value * scale(other.time - time, hd);
90
203
            time = other.time;
91
203
        } else {
92
72
            value = value + other.value;
93
72
        }
94
309
    }
95
96
157
    void merge(const ExponentialMovingAverageData& rhs) {
97
157
        if (!rhs.initialized) {
98
31
            return;
99
31
        }
100
126
        if (!initialized) {
101
67
            *this = rhs;
102
67
            return;
103
67
        }
104
59
        if (UNLIKELY(half_decay != rhs.half_decay)) {
105
36
            throw Exception(
106
36
                    ErrorCode::INVALID_ARGUMENT,
107
36
                    "exponential_moving_average aggregate states have incompatible half decay");
108
36
        }
109
23
        merge_point(rhs, half_decay);
110
23
    }
111
112
151
    double get() const {
113
151
        check_half_decay();
114
151
        if (half_decay == 0.0) {
115
68
            return 0.0;
116
68
        }
117
83
        return value / sum_weights(half_decay);
118
151
    }
119
120
103
    void write(BufferWritable& buf) const {
121
103
        check_half_decay();
122
103
        buf.write_binary(value);
123
103
        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
103
        buf.write_binary(initialized ? half_decay : std::numeric_limits<double>::quiet_NaN());
127
103
    }
128
129
112
    void read(BufferReadable& buf) {
130
112
        buf.read_binary(value);
131
112
        buf.read_binary(time);
132
112
        buf.read_binary(half_decay);
133
112
        initialized = !std::isnan(half_decay);
134
112
        if (!initialized) {
135
16
            half_decay = 0.0;
136
16
        }
137
112
    }
138
139
254
    void check_half_decay() const {
140
254
        if (UNLIKELY(std::isnan(half_decay))) {
141
5
            throw Exception(ErrorCode::INVALID_ARGUMENT,
142
5
                            "exponential_moving_average half decay must not be NaN");
143
5
        }
144
254
    }
145
146
9
    void reset() {
147
9
        value = 0.0;
148
9
        time = 0.0;
149
9
        half_decay = 0.0;
150
9
        initialized = false;
151
9
    }
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
174
            : IAggregateFunctionDataHelper<ExponentialMovingAverageData,
162
174
                                           AggregateFunctionExponentialMovingAverage>(
163
174
                      argument_types_) {}
164
165
2
    String get_name() const override { return "exponential_moving_average"; }
166
167
302
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeFloat64>(); }
168
169
9
    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
286
             Arena&) const override {
173
286
        const double half_decay =
174
286
                assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[0])
175
286
                        .get_data()[row_num];
176
286
        const double new_value =
177
286
                assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[1])
178
286
                        .get_data()[row_num];
179
286
        const double current_time =
180
286
                assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[2])
181
286
                        .get_data()[row_num];
182
286
        this->data(place).add(new_value, current_time, half_decay);
183
286
    }
184
185
60
    void check_input_columns_type(const IColumn** columns) const override {
186
60
        this->template check_argument_column_type<ColumnFloat64>(columns[0]);
187
60
        this->template check_argument_column_type<ColumnFloat64>(columns[1]);
188
60
        this->template check_argument_column_type<ColumnFloat64>(columns[2]);
189
60
    }
190
191
    void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
192
157
               Arena&) const override {
193
157
        this->data(place).merge(this->data(rhs));
194
157
    }
195
196
103
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
197
103
        this->data(place).write(buf);
198
103
    }
199
200
    void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
201
112
                     Arena&) const override {
202
112
        this->data(place).read(buf);
203
112
    }
204
205
151
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
206
151
        assert_cast<ColumnFloat64&, TypeCheckOnRelease::DISABLE>(to).get_data().push_back(
207
151
                this->data(place).get());
208
151
    }
209
};
210
211
} // namespace doris