You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2020/05/06 08:09:20 UTC

[GitHub] [incubator-iotdb] qiaojialin commented on a change in pull request #1136: [IOTDB-632]Performance improve for Linear Fill

qiaojialin commented on a change in pull request #1136:
URL: https://github.com/apache/incubator-iotdb/pull/1136#discussion_r420608387



##########
File path: server/src/main/java/org/apache/iotdb/db/query/executor/fill/LinearFill.java
##########
@@ -83,57 +92,80 @@ public IFill copy() {
   }
 
   @Override
-  Filter constructFilter() {
+  void constructFilter() {
     Filter lowerBound = beforeRange == -1 ? TimeFilter.gtEq(Long.MIN_VALUE)
         : TimeFilter.gtEq(queryTime - beforeRange);
     Filter upperBound = afterRange == -1 ? TimeFilter.ltEq(Long.MAX_VALUE)
         : TimeFilter.ltEq(queryTime + afterRange);
     // [queryTIme - beforeRange, queryTime + afterRange]
-    return FilterFactory.and(lowerBound, upperBound);
+    beforeFilter = FilterFactory.and(lowerBound, TimeFilter.ltEq(queryTime));
+    afterFilter = FilterFactory.and(TimeFilter.gtEq(queryTime), upperBound);
   }
 
   @Override
-  public void configureFill(Path path, TSDataType dataType, long queryTime,
-      Set<String> sensors, QueryContext context)
-      throws StorageEngineException, QueryProcessException {
+  public void configureFill(
+      Path path, TSDataType dataType, long queryTime, Set<String> sensors, QueryContext context) {
+    this.seriesPath = path;
     this.dataType = dataType;
     this.queryTime = queryTime;
-    Filter timeFilter = constructFilter();
-    dataReader = new SeriesRawDataBatchReader(path, sensors, dataType, context,
-        QueryResourceManager.getInstance().getQueryDataSource(path, context, timeFilter),
-        timeFilter, null, null);
+    this.context = context;
+    this.allSensors = sensors;
+    constructFilter();
   }
 
   @Override
-  public TimeValuePair getFillResult() throws IOException, UnSupportedFillTypeException {
-    TimeValuePair beforePair = null;
-    TimeValuePair afterPair = null;
-    while (batchData.hasCurrent() || dataReader.hasNextBatch()) {
-      if (!batchData.hasCurrent() && dataReader.hasNextBatch()) {
-        batchData = dataReader.nextBatch();
-      }
-      afterPair = new TimeValuePair(batchData.currentTime(), batchData.currentTsPrimitiveType());
-      batchData.next();
-      if (afterPair.getTimestamp() <= queryTime) {
-        beforePair = afterPair;
-      } else {
-        break;
-      }
-    }
+  public TimeValuePair getFillResult()
+      throws IOException, QueryProcessException, StorageEngineException {
+    QueryDataSource dataSource =
+        QueryResourceManager.getInstance().getQueryDataSource(seriesPath, context, beforeFilter);
+    LastPointReader lastReader =
+        new LastPointReader(seriesPath, dataType, allSensors, context, dataSource, queryTime, beforeFilter);
+
+    TimeValuePair beforePair = lastReader.readLastPoint();
+    TimeValuePair afterPair = calculatFirstPointAfterQueryTime();
 
     // no before data or has data on the query timestamp
-    if (beforePair == null || beforePair.getTimestamp() == queryTime) {
+    if (beforePair.getValue() == null || beforePair.getTimestamp() == queryTime) {
       return beforePair;
     }
 
     // on after data or after data is out of range
-    if (afterPair.getTimestamp() < queryTime || (afterRange != -1 && afterPair.getTimestamp() > queryTime + afterRange)) {
+    if (afterPair.getValue() == null || afterPair.getTimestamp() < queryTime ||
+        (afterRange != -1 && afterPair.getTimestamp() > queryTime + afterRange)) {
       return new TimeValuePair(queryTime, null);
     }
 
     return average(beforePair, afterPair);
   }
 
+  private TimeValuePair calculatFirstPointAfterQueryTime()

Review comment:
       ```suggestion
     private TimeValuePair calculateFirstPointAfterQueryTime()
   ```

##########
File path: server/src/main/java/org/apache/iotdb/db/query/executor/fill/LinearFill.java
##########
@@ -83,57 +92,80 @@ public IFill copy() {
   }
 
   @Override
-  Filter constructFilter() {
+  void constructFilter() {
     Filter lowerBound = beforeRange == -1 ? TimeFilter.gtEq(Long.MIN_VALUE)
         : TimeFilter.gtEq(queryTime - beforeRange);
     Filter upperBound = afterRange == -1 ? TimeFilter.ltEq(Long.MAX_VALUE)
         : TimeFilter.ltEq(queryTime + afterRange);
     // [queryTIme - beforeRange, queryTime + afterRange]
-    return FilterFactory.and(lowerBound, upperBound);
+    beforeFilter = FilterFactory.and(lowerBound, TimeFilter.ltEq(queryTime));
+    afterFilter = FilterFactory.and(TimeFilter.gtEq(queryTime), upperBound);
   }
 
   @Override
-  public void configureFill(Path path, TSDataType dataType, long queryTime,
-      Set<String> sensors, QueryContext context)
-      throws StorageEngineException, QueryProcessException {
+  public void configureFill(
+      Path path, TSDataType dataType, long queryTime, Set<String> sensors, QueryContext context) {
+    this.seriesPath = path;
     this.dataType = dataType;
     this.queryTime = queryTime;
-    Filter timeFilter = constructFilter();
-    dataReader = new SeriesRawDataBatchReader(path, sensors, dataType, context,
-        QueryResourceManager.getInstance().getQueryDataSource(path, context, timeFilter),
-        timeFilter, null, null);
+    this.context = context;
+    this.allSensors = sensors;
+    constructFilter();
   }
 
   @Override
-  public TimeValuePair getFillResult() throws IOException, UnSupportedFillTypeException {
-    TimeValuePair beforePair = null;
-    TimeValuePair afterPair = null;
-    while (batchData.hasCurrent() || dataReader.hasNextBatch()) {
-      if (!batchData.hasCurrent() && dataReader.hasNextBatch()) {
-        batchData = dataReader.nextBatch();
-      }
-      afterPair = new TimeValuePair(batchData.currentTime(), batchData.currentTsPrimitiveType());
-      batchData.next();
-      if (afterPair.getTimestamp() <= queryTime) {
-        beforePair = afterPair;
-      } else {
-        break;
-      }
-    }
+  public TimeValuePair getFillResult()
+      throws IOException, QueryProcessException, StorageEngineException {
+    QueryDataSource dataSource =
+        QueryResourceManager.getInstance().getQueryDataSource(seriesPath, context, beforeFilter);
+    LastPointReader lastReader =
+        new LastPointReader(seriesPath, dataType, allSensors, context, dataSource, queryTime, beforeFilter);
+
+    TimeValuePair beforePair = lastReader.readLastPoint();
+    TimeValuePair afterPair = calculatFirstPointAfterQueryTime();
 
     // no before data or has data on the query timestamp
-    if (beforePair == null || beforePair.getTimestamp() == queryTime) {
+    if (beforePair.getValue() == null || beforePair.getTimestamp() == queryTime) {
       return beforePair;
     }
 
     // on after data or after data is out of range
-    if (afterPair.getTimestamp() < queryTime || (afterRange != -1 && afterPair.getTimestamp() > queryTime + afterRange)) {
+    if (afterPair.getValue() == null || afterPair.getTimestamp() < queryTime ||
+        (afterRange != -1 && afterPair.getTimestamp() > queryTime + afterRange)) {
       return new TimeValuePair(queryTime, null);
     }
 
     return average(beforePair, afterPair);
   }
 
+  private TimeValuePair calculatFirstPointAfterQueryTime()
+      throws IOException, StorageEngineException, QueryProcessException {
+    TimeValuePair result = new TimeValuePair(0, null);
+    List<String> aggregations = new ArrayList<>();
+    aggregations.add(AggregationType.MIN_TIME.toString());
+    aggregations.add(AggregationType.FIRST_VALUE.toString());

Review comment:
       This is not needed, you could directly construct AggregateResult using dataType.

##########
File path: server/src/main/java/org/apache/iotdb/db/query/executor/fill/LinearFill.java
##########
@@ -83,57 +92,80 @@ public IFill copy() {
   }
 
   @Override
-  Filter constructFilter() {
+  void constructFilter() {
     Filter lowerBound = beforeRange == -1 ? TimeFilter.gtEq(Long.MIN_VALUE)
         : TimeFilter.gtEq(queryTime - beforeRange);
     Filter upperBound = afterRange == -1 ? TimeFilter.ltEq(Long.MAX_VALUE)
         : TimeFilter.ltEq(queryTime + afterRange);
     // [queryTIme - beforeRange, queryTime + afterRange]
-    return FilterFactory.and(lowerBound, upperBound);
+    beforeFilter = FilterFactory.and(lowerBound, TimeFilter.ltEq(queryTime));
+    afterFilter = FilterFactory.and(TimeFilter.gtEq(queryTime), upperBound);
   }
 
   @Override
-  public void configureFill(Path path, TSDataType dataType, long queryTime,
-      Set<String> sensors, QueryContext context)
-      throws StorageEngineException, QueryProcessException {
+  public void configureFill(
+      Path path, TSDataType dataType, long queryTime, Set<String> sensors, QueryContext context) {
+    this.seriesPath = path;
     this.dataType = dataType;
     this.queryTime = queryTime;
-    Filter timeFilter = constructFilter();
-    dataReader = new SeriesRawDataBatchReader(path, sensors, dataType, context,
-        QueryResourceManager.getInstance().getQueryDataSource(path, context, timeFilter),
-        timeFilter, null, null);
+    this.context = context;
+    this.allSensors = sensors;
+    constructFilter();
   }
 
   @Override
-  public TimeValuePair getFillResult() throws IOException, UnSupportedFillTypeException {
-    TimeValuePair beforePair = null;
-    TimeValuePair afterPair = null;
-    while (batchData.hasCurrent() || dataReader.hasNextBatch()) {
-      if (!batchData.hasCurrent() && dataReader.hasNextBatch()) {
-        batchData = dataReader.nextBatch();
-      }
-      afterPair = new TimeValuePair(batchData.currentTime(), batchData.currentTsPrimitiveType());
-      batchData.next();
-      if (afterPair.getTimestamp() <= queryTime) {
-        beforePair = afterPair;
-      } else {
-        break;
-      }
-    }
+  public TimeValuePair getFillResult()
+      throws IOException, QueryProcessException, StorageEngineException {
+    QueryDataSource dataSource =
+        QueryResourceManager.getInstance().getQueryDataSource(seriesPath, context, beforeFilter);
+    LastPointReader lastReader =
+        new LastPointReader(seriesPath, dataType, allSensors, context, dataSource, queryTime, beforeFilter);
+
+    TimeValuePair beforePair = lastReader.readLastPoint();
+    TimeValuePair afterPair = calculatFirstPointAfterQueryTime();
 
     // no before data or has data on the query timestamp
-    if (beforePair == null || beforePair.getTimestamp() == queryTime) {
+    if (beforePair.getValue() == null || beforePair.getTimestamp() == queryTime) {

Review comment:
       add the LinearFill in UserGuide, explain how we fill when beforePair or afterPair is null




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org