This is an automated email from the ASF dual-hosted git repository.

qiaojialin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 3eb01b2  [IOTDB-514] Fix avg calculation (#834)
3eb01b2 is described below

commit 3eb01b2b021c2c8afa0c42a280ef34056bb91e7a
Author: Jialin Qiao <[email protected]>
AuthorDate: Tue Feb 25 12:16:17 2020 +0800

    [IOTDB-514] Fix avg calculation (#834)
    
    * update avg calculation
---
 .../db/query/aggregation/impl/AvgAggrResult.java    |  5 +++--
 .../query/aggregation/impl/LastValueAggrResult.java |  8 ++++----
 .../query/aggregation/impl/MinTimeAggrResult.java   |  4 ++++
 .../db/query/aggregation/AggregateResultTest.java   | 21 +++++++++++++--------
 4 files changed, 24 insertions(+), 14 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..86ad7f3 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,7 @@ 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/main/java/org/apache/iotdb/db/query/aggregation/impl/LastValueAggrResult.java
 
b/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/LastValueAggrResult.java
index 9154545..206f36d 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/LastValueAggrResult.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/LastValueAggrResult.java
@@ -95,10 +95,10 @@ public class LastValueAggrResult extends AggregateResult {
 
   @Override
   public void merge(AggregateResult another) {
-    LastValueAggrResult anotherFirst = (LastValueAggrResult) another;
-    if(this.getValue() == null || this.timestamp < anotherFirst.timestamp){
-      this.setValue( anotherFirst.getValue() );
-      this.timestamp = anotherFirst.timestamp;
+    LastValueAggrResult anotherLast = (LastValueAggrResult) another;
+    if(this.getValue() == null || this.timestamp < anotherLast.timestamp){
+      this.setValue( anotherLast.getValue() );
+      this.timestamp = anotherLast.timestamp;
     }
   }
 
diff --git 
a/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/MinTimeAggrResult.java
 
b/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/MinTimeAggrResult.java
index 5c3ac9d..8396fa4 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/MinTimeAggrResult.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/MinTimeAggrResult.java
@@ -85,6 +85,10 @@ public class MinTimeAggrResult extends AggregateResult {
   @Override
   public void merge(AggregateResult another) {
     MinTimeAggrResult anotherMinTime = (MinTimeAggrResult) another;
+    if (!hasResult() && anotherMinTime.hasResult()) {
+      setLongValue(anotherMinTime.getResult());
+      return;
+    }
     if (hasResult() && anotherMinTime.hasResult() && getResult() > 
anotherMinTime.getResult()) {
       setLongValue(anotherMinTime.getResult());
     }
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..8f99b21 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
@@ -96,19 +99,21 @@ public class AggregateResultTest {
 
   @Test
   public void minTimeAggrResultTest() throws QueryProcessException {
+    AggregateResult finalResult = 
AggreResultFactory.getAggrResultByName(SQLConstant.MIN_TIME, TSDataType.DOUBLE);
     AggregateResult minTimeAggrResult1 = 
AggreResultFactory.getAggrResultByName(SQLConstant.MIN_TIME, TSDataType.DOUBLE);
     AggregateResult minTimeAggrResult2 = 
AggreResultFactory.getAggrResultByName(SQLConstant.MIN_TIME, TSDataType.DOUBLE);
 
     Statistics statistics1 = Statistics.getStatsByType(TSDataType.DOUBLE);
     Statistics statistics2 = Statistics.getStatsByType(TSDataType.DOUBLE);
-    statistics1.update(1l, 1d);
+    statistics1.update(10l, 1d);
     statistics2.update(2l,1d);
 
     minTimeAggrResult1.updateResultFromStatistics(statistics1);
     minTimeAggrResult2.updateResultFromStatistics(statistics2);
-    minTimeAggrResult1.merge(minTimeAggrResult2);
+    finalResult.merge(minTimeAggrResult1);
+    finalResult.merge(minTimeAggrResult2);
 
-    Assert.assertEquals(1l, (long)minTimeAggrResult1.getResult(), 0.01);
+    Assert.assertEquals(2l, (long)finalResult.getResult(), 0.01);
   }
 
   @Test

Reply via email to