You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by qi...@apache.org on 2020/02/25 01:23:06 UTC
[incubator-iotdb] 01/01: update avg calculation
This is an automated email from the ASF dual-hosted git repository.
qiaojialin pushed a commit to branch fix_avg_calculation
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git
commit 35f481c2260b5502f6fd7722ee7b13183af47483
Author: qiaojialin <64...@qq.com>
AuthorDate: Tue Feb 25 09:22:51 2020 +0800
update avg calculation
---
.../iotdb/db/query/aggregation/impl/AvgAggrResult.java | 4 ++--
.../iotdb/db/query/aggregation/AggregateResultTest.java | 13 ++++++++-----
2 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/AvgAggrResult.java b/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/AvgAggrResult.java
index dbc677e..a9b9ffa 100644
--- a/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/AvgAggrResult.java
+++ b/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/AvgAggrResult.java
@@ -104,7 +104,7 @@ public class AvgAggrResult extends AggregateResult {
throw new IOException(
String.format("Unsupported data type in aggregation AVG : %s", type));
}
- avg = avg * ((double) cnt / (cnt + 1)) + (1.0 / (cnt + 1)) * val;
+ avg = avg * ((double) cnt / (cnt + 1)) + val * (1.0 / (cnt + 1));
cnt++;
}
@@ -116,6 +116,6 @@ public class AvgAggrResult extends AggregateResult {
@Override
public void merge(AggregateResult another) {
AvgAggrResult anotherAvg = (AvgAggrResult) another;
- avg = (avg * cnt + anotherAvg.avg * anotherAvg.cnt) / (cnt + anotherAvg.cnt);
+ avg = avg * ((double) cnt / (cnt + anotherAvg.cnt)) + anotherAvg.avg * ((double) anotherAvg.cnt / (cnt + anotherAvg.cnt));
}
}
diff --git a/server/src/test/java/org/apache/iotdb/db/query/aggregation/AggregateResultTest.java b/server/src/test/java/org/apache/iotdb/db/query/aggregation/AggregateResultTest.java
index 698b943..0268b93 100644
--- a/server/src/test/java/org/apache/iotdb/db/query/aggregation/AggregateResultTest.java
+++ b/server/src/test/java/org/apache/iotdb/db/query/aggregation/AggregateResultTest.java
@@ -34,13 +34,16 @@ public class AggregateResultTest {
AggregateResult avgAggrResult1 = AggreResultFactory.getAggrResultByName(SQLConstant.AVG, TSDataType.DOUBLE);
AggregateResult avgAggrResult2 = AggreResultFactory.getAggrResultByName(SQLConstant.AVG, TSDataType.DOUBLE);
- Statistics statistics = Statistics.getStatsByType(TSDataType.DOUBLE);
- statistics.update(1l,1d);
+ Statistics statistics1 = Statistics.getStatsByType(TSDataType.DOUBLE);
+ Statistics statistics2 = Statistics.getStatsByType(TSDataType.DOUBLE);
+ statistics1.update(1l,1d);
+ statistics1.update(2l,1d);
+ statistics2.update(1l,2d);
- avgAggrResult1.updateResultFromStatistics(statistics);
- avgAggrResult2.updateResultFromStatistics(statistics);
+ avgAggrResult1.updateResultFromStatistics(statistics1);
+ avgAggrResult2.updateResultFromStatistics(statistics2);
avgAggrResult1.merge(avgAggrResult2);
- Assert.assertEquals(1d, (double)avgAggrResult1.getResult(), 0.01);
+ Assert.assertEquals(1.333d, (double)avgAggrResult1.getResult(), 0.01);
}
@Test