You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ca...@apache.org on 2024/10/21 08:00:06 UTC

(iotdb) branch hasFinalResult updated: fix final result

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

caogaofei pushed a commit to branch hasFinalResult
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/hasFinalResult by this push:
     new 30944d12103 fix final result
30944d12103 is described below

commit 30944d12103ca2baf9919cd302e52c21c8d0f904
Author: Beyyes <cg...@foxmail.com>
AuthorDate: Mon Oct 21 15:59:50 2024 +0800

    fix final result
---
 .../TableAggregationTableScanOperator.java         | 47 ++++++++++++++++++----
 .../plan/planner/TableOperatorGenerator.java       |  5 ++-
 2 files changed, 42 insertions(+), 10 deletions(-)

diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/TableAggregationTableScanOperator.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/TableAggregationTableScanOperator.java
index 8e85d86560f..8b02b393324 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/TableAggregationTableScanOperator.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/TableAggregationTableScanOperator.java
@@ -110,6 +110,8 @@ public class TableAggregationTableScanOperator extends AbstractSeriesAggregation
 
   ITableTimeRangeIterator timeIterator;
 
+  private boolean allAggregatorsHasFinalResult = false;
+
   public TableAggregationTableScanOperator(
       PlanNodeId sourceId,
       OperatorContext context,
@@ -278,17 +280,20 @@ public class TableAggregationTableScanOperator extends AbstractSeriesAggregation
     try {
       if (calcFromCachedData()) {
         updateResultTsBlock();
+        checkIfAllAggregatorHasFinalResult();
         return true;
       }
 
       if (readAndCalcFromPage()) {
         updateResultTsBlock();
+        checkIfAllAggregatorHasFinalResult();
         return true;
       }
 
       // only when all the page data has been consumed, we need to read the chunk data
       if (!seriesScanUtil.hasNextPage() && readAndCalcFromChunk()) {
         updateResultTsBlock();
+        checkIfAllAggregatorHasFinalResult();
         return true;
       }
 
@@ -297,6 +302,7 @@ public class TableAggregationTableScanOperator extends AbstractSeriesAggregation
           && !seriesScanUtil.hasNextChunk()
           && readAndCalcFromFile()) {
         updateResultTsBlock();
+        checkIfAllAggregatorHasFinalResult();
         return true;
       }
 
@@ -338,13 +344,37 @@ public class TableAggregationTableScanOperator extends AbstractSeriesAggregation
     resetTableAggregators();
   }
 
+  private void checkIfAllAggregatorHasFinalResult() {
+    if (allAggregatorsHasFinalResult) {
+      // for SINGLE_TIME_ITERATOR, if allAggregatorsHasFinalResult, just consume next device
+      if (timeIterator.getType() == ITableTimeRangeIterator.TimeIteratorType.SINGLE_TIME_ITERATOR) {
+        currentDeviceIndex++;
+        inputTsBlock = null;
+
+        if (currentDeviceIndex < deviceCount) {
+          // construct AlignedSeriesScanUtil for next device
+          this.seriesScanUtil =
+              constructAlignedSeriesScanUtil(deviceEntries.get(currentDeviceIndex));
+          queryDataSource.reset();
+          this.seriesScanUtil.initQueryDataSource(queryDataSource);
+        }
+
+        if (currentDeviceIndex >= deviceCount) {
+          // all devices have been consumed
+          timeIterator.setFinished();
+        }
+
+        allAggregatorsHasFinalResult = false;
+      }
+    }
+  }
+
   protected boolean calcFromCachedData() {
     return calcFromRawData(inputTsBlock);
   }
 
   private boolean calcFromRawData(TsBlock tsBlock) {
-    Pair<Boolean, TsBlock> calcResult =
-        calculateAggregationFromRawData(tsBlock, tableAggregators, ascending);
+    Pair<Boolean, TsBlock> calcResult = calculateAggregationFromRawData(tsBlock, ascending);
     inputTsBlock = calcResult.getRight();
     return calcResult.getLeft();
   }
@@ -356,7 +386,7 @@ public class TableAggregationTableScanOperator extends AbstractSeriesAggregation
    *     remaining tsBlock
    */
   public Pair<Boolean, TsBlock> calculateAggregationFromRawData(
-      TsBlock inputTsBlock, List<TableAggregator> aggregators, boolean ascending) {
+      TsBlock inputTsBlock, boolean ascending) {
     if (inputTsBlock == null || inputTsBlock.isEmpty()) {
       return new Pair<>(false, inputTsBlock);
     }
@@ -372,7 +402,7 @@ public class TableAggregationTableScanOperator extends AbstractSeriesAggregation
         inputTsBlock = skipPointsOutOfTimeRange(inputTsBlock, curTimeRange, ascending);
       }
 
-      inputTsBlock = process(inputTsBlock, curTimeRange, aggregators);
+      inputTsBlock = process(inputTsBlock, curTimeRange);
     }
 
     // judge whether the calculation finished
@@ -382,11 +412,10 @@ public class TableAggregationTableScanOperator extends AbstractSeriesAggregation
                 ? inputTsBlock.getEndTime() > curTimeRange.getMax()
                 : inputTsBlock.getEndTime() < curTimeRange.getMin());
     return new Pair<>(
-        isAllAggregatorsHasFinalResult(aggregators) || isTsBlockOutOfBound, inputTsBlock);
+        isAllAggregatorsHasFinalResult(tableAggregators) || isTsBlockOutOfBound, inputTsBlock);
   }
 
-  private TsBlock process(
-      TsBlock inputTsBlock, TimeRange curTimeRange, List<TableAggregator> aggregators) {
+  private TsBlock process(TsBlock inputTsBlock, TimeRange curTimeRange) {
     // Get the row which need to be processed by aggregator
     IWindow curWindow = new TimeWindow(curTimeRange);
     Column timeColumn = inputTsBlock.getTimeColumn();
@@ -758,7 +787,7 @@ public class TableAggregationTableScanOperator extends AbstractSeriesAggregation
     tsBlockBuilder.declarePosition();
   }
 
-  public static boolean isAllAggregatorsHasFinalResult(List<TableAggregator> aggregators) {
+  public boolean isAllAggregatorsHasFinalResult(List<TableAggregator> aggregators) {
     // no aggregation function, just output ids or attributes
     if (aggregators.isEmpty()) {
       return false;
@@ -769,6 +798,8 @@ public class TableAggregationTableScanOperator extends AbstractSeriesAggregation
         return false;
       }
     }
+
+    this.allAggregatorsHasFinalResult = true;
     return true;
   }
 
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/TableOperatorGenerator.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/TableOperatorGenerator.java
index af0c414f54c..784e95fd6d8 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/TableOperatorGenerator.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/TableOperatorGenerator.java
@@ -129,6 +129,7 @@ import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Expression;
 import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.FunctionCall;
 import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Literal;
 import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.LongLiteral;
+import org.apache.iotdb.db.queryengine.plan.statement.component.Ordering;
 import org.apache.iotdb.db.queryengine.transformation.dag.column.ColumnTransformer;
 import org.apache.iotdb.db.queryengine.transformation.dag.column.leaf.LeafColumnTransformer;
 import org.apache.iotdb.db.queryengine.transformation.dag.column.unary.scalar.DateBinFunctionColumnTransformer;
@@ -1640,7 +1641,7 @@ public class TableOperatorGenerator extends PlanVisitor<Operator, LocalExecution
             columnSchemas,
             columnsIndexArray,
             node.getDeviceEntries(),
-            node.getScanOrder(),
+            scanAscending ? Ordering.ASC : Ordering.DESC,
             scanOptionsBuilder.build(),
             measurementColumnNames,
             measurementSchemas,
@@ -1650,7 +1651,7 @@ public class TableOperatorGenerator extends PlanVisitor<Operator, LocalExecution
             groupingKeySchemas,
             groupingKeyIndex,
             timeRangeIterator,
-            node.getScanOrder().isAscending(),
+            scanAscending,
             null,
             calculateMaxAggregationResultSize(),
             canUseStatistic,