You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ja...@apache.org on 2021/11/15 01:10:31 UTC

[iotdb] branch rel/0.12 updated: [To rel/0.12][IOTDB-1997]Fix bug of descending aggregation query (#4381)

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

jackietien pushed a commit to branch rel/0.12
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/rel/0.12 by this push:
     new 282cf6d  [To rel/0.12][IOTDB-1997]Fix bug of descending aggregation query (#4381)
282cf6d is described below

commit 282cf6da7da9c90ee911aea340ff2e9f7294c2fa
Author: BaiJian <er...@hotmail.com>
AuthorDate: Mon Nov 15 09:09:55 2021 +0800

    [To rel/0.12][IOTDB-1997]Fix bug of descending aggregation query (#4381)
---
 .../iotdb/cluster/query/LocalQueryExecutor.java    | 42 ++++++++--------
 .../db/query/aggregation/AggregateResult.java      | 10 +++-
 .../aggregation/impl/FirstValueAggrResult.java     |  8 +--
 .../aggregation/impl/FirstValueDescAggrResult.java |  5 ++
 .../query/aggregation/DescAggregateResultTest.java | 58 ++++++++++++++++++++++
 5 files changed, 92 insertions(+), 31 deletions(-)

diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/query/LocalQueryExecutor.java b/cluster/src/main/java/org/apache/iotdb/cluster/query/LocalQueryExecutor.java
index 74a2721..10f04b4 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/query/LocalQueryExecutor.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/query/LocalQueryExecutor.java
@@ -627,37 +627,35 @@ public class LocalQueryExecutor {
 
     ClusterQueryUtils.checkPathExistence(path);
     List<AggregateResult> results = new ArrayList<>();
+    List<AggregateResult> ascResults = new ArrayList<>();
+    List<AggregateResult> descResults = new ArrayList<>();
     for (String aggregation : aggregations) {
-      results.add(AggregateResultFactory.getAggrResultByName(aggregation, dataType, ascending));
+      AggregateResult ar =
+          AggregateResultFactory.getAggrResultByName(aggregation, dataType, ascending);
+      if (ar.isAscending()) {
+        ascResults.add(ar);
+      } else {
+        descResults.add(ar);
+      }
+      results.add(ar);
     }
     List<Integer> nodeSlots =
         ((SlotPartitionTable) dataGroupMember.getMetaGroupMember().getPartitionTable())
             .getNodeSlots(dataGroupMember.getHeader());
     try {
-      if (ascending) {
-        AggregationExecutor.aggregateOneSeries(
-            new PartialPath(path),
-            allSensors,
-            context,
-            timeFilter,
-            dataType,
-            results,
-            null,
-            new SlotTsFileFilter(nodeSlots));
-      } else {
-        AggregationExecutor.aggregateOneSeries(
-            new PartialPath(path),
-            allSensors,
-            context,
-            timeFilter,
-            dataType,
-            null,
-            results,
-            new SlotTsFileFilter(nodeSlots));
-      }
+      AggregationExecutor.aggregateOneSeries(
+          new PartialPath(path),
+          allSensors,
+          context,
+          timeFilter,
+          dataType,
+          ascResults,
+          descResults,
+          new SlotTsFileFilter(nodeSlots));
     } catch (IllegalPathException e) {
       // ignore
     }
+
     return results;
   }
 
diff --git a/server/src/main/java/org/apache/iotdb/db/query/aggregation/AggregateResult.java b/server/src/main/java/org/apache/iotdb/db/query/aggregation/AggregateResult.java
index 8016be7..e53abe9 100644
--- a/server/src/main/java/org/apache/iotdb/db/query/aggregation/AggregateResult.java
+++ b/server/src/main/java/org/apache/iotdb/db/query/aggregation/AggregateResult.java
@@ -70,7 +70,8 @@ public abstract class AggregateResult {
       throws QueryProcessException;
 
   /**
-   * Aggregate results cannot be calculated using Statistics directly, using the data in each page
+   * Aggregate results cannot be calculated using Statistics directly, using the data in each page.
+   * This method is used in global aggregation query.
    *
    * @param dataInThisPage the data in Page
    */
@@ -78,7 +79,8 @@ public abstract class AggregateResult {
       throws IOException, QueryProcessException;
 
   /**
-   * Aggregate results cannot be calculated using Statistics directly, using the data in each page
+   * Aggregate results cannot be calculated using Statistics directly, using the data in each page.
+   * This method is used in GROUP BY aggregation query.
    *
    * @param dataInThisPage the data in Page
    * @param minBound calculate points whose time >= bound
@@ -311,6 +313,10 @@ public abstract class AggregateResult {
     return aggregationType;
   }
 
+  /**
+   * Whether the AggregationResult accepts data in time ascending order, if it returns false, the
+   * data should be passed in time descending order.
+   */
   public boolean isAscending() {
     return true;
   }
diff --git a/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/FirstValueAggrResult.java b/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/FirstValueAggrResult.java
index 56ce8c5..7fd636e 100644
--- a/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/FirstValueAggrResult.java
+++ b/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/FirstValueAggrResult.java
@@ -65,13 +65,7 @@ public class FirstValueAggrResult extends AggregateResult {
 
   @Override
   public void updateResultFromPageData(BatchData dataInThisPage) {
-    if (hasFinalResult()) {
-      return;
-    }
-    if (dataInThisPage.hasCurrent()) {
-      setValue(dataInThisPage.currentValue());
-      timestamp = dataInThisPage.currentTime();
-    }
+    updateResultFromPageData(dataInThisPage, Long.MIN_VALUE, Long.MAX_VALUE);
   }
 
   @Override
diff --git a/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/FirstValueDescAggrResult.java b/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/FirstValueDescAggrResult.java
index 8eae923..a3965d8 100644
--- a/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/FirstValueDescAggrResult.java
+++ b/server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/FirstValueDescAggrResult.java
@@ -75,6 +75,11 @@ public class FirstValueDescAggrResult extends FirstValueAggrResult {
   }
 
   @Override
+  public boolean isAscending() {
+    return false;
+  }
+
+  @Override
   public boolean hasFinalResult() {
     return false;
   }
diff --git a/server/src/test/java/org/apache/iotdb/db/query/aggregation/DescAggregateResultTest.java b/server/src/test/java/org/apache/iotdb/db/query/aggregation/DescAggregateResultTest.java
index debc350..dc07701 100644
--- a/server/src/test/java/org/apache/iotdb/db/query/aggregation/DescAggregateResultTest.java
+++ b/server/src/test/java/org/apache/iotdb/db/query/aggregation/DescAggregateResultTest.java
@@ -24,6 +24,8 @@ import org.apache.iotdb.db.qp.constant.SQLConstant;
 import org.apache.iotdb.db.query.factory.AggregateResultFactory;
 import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
 import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics;
+import org.apache.iotdb.tsfile.read.common.BatchData;
+import org.apache.iotdb.tsfile.read.common.BatchDataFactory;
 import org.apache.iotdb.tsfile.utils.Binary;
 
 import org.junit.Assert;
@@ -56,6 +58,20 @@ public class DescAggregateResultTest {
     ByteBuffer byteBuffer = ByteBuffer.wrap(outputStream.toByteArray());
     AggregateResult result = AggregateResult.deserializeFrom(byteBuffer);
     Assert.assertEquals(10L, (long) result.getResult());
+
+    maxTimeDescAggrResult.reset();
+    BatchData batchData = BatchDataFactory.createBatchData(TSDataType.FLOAT, false, false);
+    batchData.putFloat(1, 1.0F);
+    batchData.putFloat(2, 2.0F);
+    batchData.putFloat(3, 3.0F);
+    batchData.putFloat(4, 4.0F);
+    batchData.putFloat(5, 5.0F);
+    batchData.resetBatchData();
+    maxTimeDescAggrResult.updateResultFromPageData(batchData);
+    Assert.assertEquals(5L, maxTimeDescAggrResult.getResult());
+    batchData.resetBatchData();
+    maxTimeDescAggrResult.updateResultFromPageData(batchData, 2, 5);
+    Assert.assertEquals(5L, maxTimeDescAggrResult.getResult());
   }
 
   @Test
@@ -78,6 +94,20 @@ public class DescAggregateResultTest {
     ByteBuffer byteBuffer = ByteBuffer.wrap(outputStream.toByteArray());
     AggregateResult result = AggregateResult.deserializeFrom(byteBuffer);
     Assert.assertEquals(1L, (long) result.getResult());
+
+    minTimeDescAggrResult.reset();
+    BatchData batchData = BatchDataFactory.createBatchData(TSDataType.FLOAT, false, false);
+    batchData.putFloat(1, 1.0F);
+    batchData.putFloat(2, 2.0F);
+    batchData.putFloat(3, 3.0F);
+    batchData.putFloat(4, 4.0F);
+    batchData.putFloat(5, 5.0F);
+    batchData.resetBatchData();
+    minTimeDescAggrResult.updateResultFromPageData(batchData);
+    Assert.assertEquals(1L, minTimeDescAggrResult.getResult());
+    batchData.resetBatchData();
+    minTimeDescAggrResult.updateResultFromPageData(batchData, 1, 3);
+    Assert.assertEquals(1L, minTimeDescAggrResult.getResult());
   }
 
   @Test
@@ -101,6 +131,20 @@ public class DescAggregateResultTest {
     ByteBuffer byteBuffer = ByteBuffer.wrap(outputStream.toByteArray());
     AggregateResult result = AggregateResult.deserializeFrom(byteBuffer);
     Assert.assertEquals(false, result.getResult());
+
+    firstValueDescAggrResult.reset();
+    BatchData batchData = BatchDataFactory.createBatchData(TSDataType.BOOLEAN, false, false);
+    batchData.putBoolean(1, true);
+    batchData.putBoolean(2, false);
+    batchData.putBoolean(3, false);
+    batchData.putBoolean(4, true);
+    batchData.putBoolean(5, false);
+    batchData.resetBatchData();
+    firstValueDescAggrResult.updateResultFromPageData(batchData);
+    Assert.assertTrue((boolean) firstValueDescAggrResult.getResult());
+    batchData.resetBatchData();
+    firstValueDescAggrResult.updateResultFromPageData(batchData, 1, 3);
+    Assert.assertTrue((boolean) firstValueDescAggrResult.getResult());
   }
 
   @Test
@@ -123,5 +167,19 @@ public class DescAggregateResultTest {
     ByteBuffer byteBuffer = ByteBuffer.wrap(outputStream.toByteArray());
     AggregateResult result = AggregateResult.deserializeFrom(byteBuffer);
     Assert.assertEquals("last", String.valueOf(result.getResult()));
+
+    lastValueDescAggrResult.reset();
+    BatchData batchData = BatchDataFactory.createBatchData(TSDataType.TEXT, false, false);
+    batchData.putBinary(1L, new Binary("a"));
+    batchData.putBinary(2L, new Binary("b"));
+    batchData.putBinary(3L, new Binary("c"));
+    batchData.putBinary(4L, new Binary("d"));
+    batchData.putBinary(5L, new Binary("e"));
+    batchData.resetBatchData();
+    lastValueDescAggrResult.updateResultFromPageData(batchData);
+    Assert.assertEquals("e", ((Binary) lastValueDescAggrResult.getResult()).getStringValue());
+    batchData.resetBatchData();
+    lastValueDescAggrResult.updateResultFromPageData(batchData, 3, 5);
+    Assert.assertEquals("e", ((Binary) lastValueDescAggrResult.getResult()).getStringValue());
   }
 }