github-actions[bot] commented on code in PR #63499:
URL: https://github.com/apache/doris/pull/63499#discussion_r3285364204


##########
be/src/exprs/aggregate/aggregate_function_ema.h:
##########
@@ -0,0 +1,173 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// This file is adapted from
+// 
https://github.com/ClickHouse/ClickHouse/blob/master/src/AggregateFunctions/AggregateFunctionExponentialMovingAverage.cpp
+
+#pragma once
+
+#include <cmath>
+#include <memory>
+
+#include "core/assert_cast.h"
+#include "core/column/column_vector.h"
+#include "core/data_type/data_type_number.h"
+#include "core/types.h"
+#include "exprs/aggregate/aggregate_function.h"
+
+namespace doris {
+class Arena;
+class BufferReadable;
+class BufferWritable;
+class IColumn;
+
+/**
+ * Exponentially smoothed moving average over time.
+ *
+ * Each value corresponds to a timeunit index. The half_decay parameter is the
+ * time lag at which exponential weights decay by one-half.
+ *
+ * State is a (value, time) pair representing the exponentially accumulated sum
+ * at a reference time. To get the average, divide by sumWeights(half_decay).
+ *
+ * Formula:
+ *   scale(dt, x) = 2^(-dt/x)
+ *   sumWeights(x) = 1 / (1 - 2^(-1/x))
+ *   add(v, t): merge current state with point (v, t)
+ *   merge(a, b): move both to the later time, then sum values
+ *   get():  value / sumWeights(half_decay)
+ *
+ * Usage: exponential_moving_average(half_decay, value, timeunit)
+ *   - half_decay: constant double, the half-life period in timeunit units
+ *   - value:      numeric column to average
+ *   - timeunit:   numeric time index (not raw timestamp; use intDiv if needed)
+ * Returns DOUBLE.
+ */
+struct ExponentialMovingAverageData {
+    double value = 0.0;
+    double time = 0.0;
+    double half_decay = 0.0;
+
+    static double scale(double time_passed, double hd) { return 
std::exp2(-time_passed / hd); }
+
+    static double sum_weights(double hd) { return 1.0 / (1.0 - std::exp2(-1.0 
/ hd)); }
+
+    void add(double new_value, double current_time, double hd) {
+        half_decay = hd;
+        ExponentialMovingAverageData other;
+        other.value = new_value;
+        other.time = current_time;
+        merge_point(other, hd);
+    }
+
+    void merge_point(const ExponentialMovingAverageData& other, double hd) {

Review Comment:
   This state needs an explicit initialized/has-value flag before merging the 
first point. It currently starts with `time = 0`, so the first row is merged 
against that synthetic timestamp. FE allows any numeric `timeunit`, including 
negative values, and a single-row example such as 
`exponential_moving_average(1.0, 10.0, -1.0)` is incorrectly decayed from time 
-1 to 0: `value` becomes `10 * scale(1, 1)` and the result is `2.5`, while a 
single input should initialize the state and return `10 / sum_weights(1) = 5` 
just like the positive-time single-row case. Please track whether the state has 
seen a row and initialize `value`, `time`, and `half_decay` directly for the 
first non-null row; add a regression case with a negative timeunit so this does 
not regress.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to