Coverage Report

Created: 2026-07-30 07:20

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 <array>
24
#include <cmath>
25
#include <memory>
26
27
#include "core/assert_cast.h"
28
#include "core/block/column_with_type_and_name.h"
29
#include "core/column/column_const.h"
30
#include "core/column/column_nullable.h"
31
#include "core/column/column_vector.h"
32
#include "core/data_type/data_type_number.h"
33
#include "core/types.h"
34
#include "exprs/aggregate/aggregate_function.h"
35
#include "exprs/vexpr_context.h"
36
37
namespace doris {
38
class Arena;
39
class BufferReadable;
40
class BufferWritable;
41
class IColumn;
42
43
/**
44
 * Exponentially smoothed moving average over time.
45
 *
46
 * Each value corresponds to a timeunit index. The half_decay parameter is the
47
 * time lag at which exponential weights decay by one-half.
48
 *
49
 * State is a (value, time) pair representing the exponentially accumulated sum
50
 * at a reference time. To get the average, divide by sumWeights(half_decay).
51
 *
52
 * Formula:
53
 *   scale(dt, x) = 2^(-dt/x)
54
 *   sumWeights(x) = 1 / (1 - 2^(-1/x))
55
 *   add(v, t): merge current state with point (v, t)
56
 *   merge(a, b): move both to the later time, then sum values
57
 *   get():  value / sumWeights(half_decay)
58
 *
59
 * Usage: exponential_moving_average(half_decay, value, timeunit)
60
 *   - half_decay: constant double, the half-life period in timeunit units
61
 *   - value:      numeric column to average
62
 *   - timeunit:   numeric time index (not raw timestamp; use intDiv if needed)
63
 * Returns DOUBLE.
64
 */
65
struct ExponentialMovingAverageData {
66
    double value = 0.0;
67
    double time = 0.0;
68
    double half_decay = 0.0;
69
70
0
    static double scale(double time_passed, double hd) { return std::exp2(-time_passed / hd); }
71
72
0
    static double sum_weights(double hd) { return 1.0 / (1.0 - std::exp2(-1.0 / hd)); }
73
74
0
    void add(double new_value, double current_time, double hd) {
75
0
        half_decay = hd;
76
0
        ExponentialMovingAverageData other;
77
0
        other.value = new_value;
78
0
        other.time = current_time;
79
0
        merge_point(other, hd);
80
0
    }
81
82
0
    void merge_point(const ExponentialMovingAverageData& other, double hd) {
83
0
        if (time > other.time) {
84
0
            value = value + other.value * scale(time - other.time, hd);
85
0
        } else if (time < other.time) {
86
0
            value = other.value + value * scale(other.time - time, hd);
87
0
            time = other.time;
88
0
        } else {
89
0
            value = value + other.value;
90
0
        }
91
0
    }
92
93
0
    void merge(const ExponentialMovingAverageData& rhs) {
94
0
        double hd = half_decay != 0.0 ? half_decay : rhs.half_decay;
95
0
        if (hd == 0.0) {
96
0
            return;
97
0
        }
98
0
        half_decay = hd;
99
0
        merge_point(rhs, hd);
100
0
    }
101
102
0
    double get() const {
103
0
        if (half_decay == 0.0) {
104
0
            return 0.0;
105
0
        }
106
0
        return value / sum_weights(half_decay);
107
0
    }
108
109
0
    void write(BufferWritable& buf) const {
110
0
        buf.write_binary(value);
111
0
        buf.write_binary(time);
112
0
        buf.write_binary(half_decay);
113
0
    }
114
115
0
    void read(BufferReadable& buf) {
116
0
        buf.read_binary(value);
117
0
        buf.read_binary(time);
118
0
        buf.read_binary(half_decay);
119
0
    }
120
121
0
    void reset() {
122
0
        value = 0.0;
123
0
        time = 0.0;
124
0
        half_decay = 0.0;
125
0
    }
126
};
127
128
class AggregateFunctionExponentialMovingAverage final
129
        : public IAggregateFunctionDataHelper<ExponentialMovingAverageData,
130
                                              AggregateFunctionExponentialMovingAverage>,
131
          MultiExpression,
132
          NullableAggregateFunction {
133
public:
134
    AggregateFunctionExponentialMovingAverage(const DataTypes& argument_types_)
135
0
            : IAggregateFunctionDataHelper<ExponentialMovingAverageData,
136
0
                                           AggregateFunctionExponentialMovingAverage>(
137
0
                      argument_types_) {}
138
139
0
    String get_name() const override { return "exponential_moving_average"; }
140
141
0
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeFloat64>(); }
142
143
0
    const std::vector<size_t>& get_const_argument_indexes() const override {
144
0
        static const std::vector<size_t> indexes {0};
145
0
        return indexes;
146
0
    }
147
148
0
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
149
150
    void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num,
151
0
             Arena&) const override {
152
0
        const double new_value =
153
0
                assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[1])
154
0
                        .get_data()[row_num];
155
0
        const double current_time =
156
0
                assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[2])
157
0
                        .get_data()[row_num];
158
0
        const auto& half_decay_column =
159
0
                *check_and_get_column_with_const<ColumnFloat64>(*columns[0]);
160
0
        this->data(place).add(new_value, current_time, half_decay_column.get_data()[0]);
161
0
    }
162
163
0
    void check_input_columns_type(const IColumn** columns) const override {
164
0
        this->template check_const_argument_column_type<ColumnFloat64>(columns[0]);
165
0
        this->template check_argument_column_type<ColumnFloat64>(columns[1]);
166
0
        this->template check_argument_column_type<ColumnFloat64>(columns[2]);
167
0
    }
168
169
    void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
170
0
               Arena&) const override {
171
0
        this->data(place).merge(this->data(rhs));
172
0
    }
173
174
0
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
175
0
        this->data(place).write(buf);
176
0
    }
177
178
    void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
179
0
                     Arena&) const override {
180
0
        this->data(place).read(buf);
181
0
    }
182
183
0
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
184
0
        assert_cast<ColumnFloat64&, TypeCheckOnRelease::DISABLE>(to).get_data().push_back(
185
0
                this->data(place).get());
186
0
    }
187
};
188
189
} // namespace doris