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/10/29 15:23:17 UTC

[GitHub] [iotdb] wshao08 commented on a change in pull request #1898: [IOTDB-968] Support time predicate in select last

wshao08 commented on a change in pull request #1898:
URL: https://github.com/apache/iotdb/pull/1898#discussion_r514345440



##########
File path: server/src/main/java/org/apache/iotdb/db/query/executor/LastQueryExecutor.java
##########
@@ -127,103 +127,64 @@ protected TimeValuePair calculateLastPairForOneSeries(
    * @param context query context
    * @return TimeValuePair
    */
-  @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning
   public static TimeValuePair calculateLastPairForOneSeriesLocally(
-      PartialPath seriesPath, TSDataType tsDataType, QueryContext context, Set<String> deviceMeasurements)
+      PartialPath seriesPath, TSDataType tsDataType, QueryContext context,
+      IExpression expression, Set<String> deviceMeasurements)
       throws IOException, QueryProcessException, StorageEngineException {
 
     // Retrieve last value from MNode
     MeasurementMNode node = null;
+    Filter filter = null;
     if (lastCacheEnabled) {
+      if (expression != null) {
+        filter = ((GlobalTimeExpression) expression).getFilter();
+      }
       try {
         node = (MeasurementMNode) IoTDB.metaManager.getNodeByPath(seriesPath);
       } catch (MetadataException e) {
         TimeValuePair timeValuePair = IoTDB.metaManager.getLastCache(seriesPath);
-        if (timeValuePair != null) {
+        if (timeValuePair != null && satisfyFilter(filter, timeValuePair)) {
           return timeValuePair;
         }
       }
 
       if (node != null && node.getCachedLast() != null) {
-        return node.getCachedLast();
+        TimeValuePair timeValuePair =  node.getCachedLast();
+        if (timeValuePair != null && satisfyFilter(filter, timeValuePair)) {
+          return timeValuePair;
+        }
       }
     }
 
-    return calculateLastPairByScanningTsFiles(seriesPath, tsDataType, context, deviceMeasurements, node);
+    return calculateLastPairByScanningTsFiles(
+        seriesPath, tsDataType, context, filter, deviceMeasurements, node);
   }
 
   private static TimeValuePair calculateLastPairByScanningTsFiles(
-          PartialPath seriesPath, TSDataType tsDataType, QueryContext context, Set<String> deviceMeasurements,
-          MeasurementMNode node) throws QueryProcessException, StorageEngineException, IOException {
+          PartialPath seriesPath, TSDataType tsDataType, QueryContext context,
+          Filter filter, Set<String> deviceMeasurements, MeasurementMNode node)
+      throws QueryProcessException, StorageEngineException, IOException {
 
     QueryDataSource dataSource =
-        QueryResourceManager.getInstance().getQueryDataSource(seriesPath, context, null);
-
-    List<TsFileResource> seqFileResources = dataSource.getSeqResources();
-    List<TsFileResource> unseqFileResources = dataSource.getUnseqResources();
-
-    TimeValuePair resultPair = new TimeValuePair(Long.MIN_VALUE, null);
-
-    if (!seqFileResources.isEmpty()) {
-      for (int i = seqFileResources.size() - 1; i >= 0; i--) {
-        TimeseriesMetadata timeseriesMetadata = FileLoaderUtils.loadTimeSeriesMetadata(
-            seqFileResources.get(i), seriesPath, context, null, deviceMeasurements);
-        if (timeseriesMetadata != null) {
-          if (!timeseriesMetadata.isModified()) {
-            Statistics timeseriesMetadataStats = timeseriesMetadata.getStatistics();
-            resultPair = constructLastPair(
-                timeseriesMetadataStats.getEndTime(),
-                timeseriesMetadataStats.getLastValue(),
-                tsDataType);
-            break;
-          } else {
-            List<ChunkMetadata> chunkMetadataList = timeseriesMetadata.loadChunkMetadataList();
-            if (!chunkMetadataList.isEmpty()) {
-              ChunkMetadata lastChunkMetaData = chunkMetadataList.get(chunkMetadataList.size() - 1);
-              Statistics chunkStatistics = lastChunkMetaData.getStatistics();
-              resultPair =
-                  constructLastPair(
-                      chunkStatistics.getEndTime(), chunkStatistics.getLastValue(), tsDataType);
-              break;
-            }
-          }
-        }
-      }
-    }
+        QueryResourceManager.getInstance().getQueryDataSource(seriesPath, context, filter);
 
-    long version = 0;
-    for (TsFileResource resource : unseqFileResources) {
-      if (resource.getEndTime(seriesPath.getDevice()) < resultPair.getTimestamp()) {
-        continue;
-      }
-      TimeseriesMetadata timeseriesMetadata =
-          FileLoaderUtils
-              .loadTimeSeriesMetadata(resource, seriesPath, context, null, deviceMeasurements);
-      if (timeseriesMetadata != null) {
-        for (ChunkMetadata chunkMetaData : timeseriesMetadata.loadChunkMetadataList()) {
-          if (chunkMetaData.getEndTime() > resultPair.getTimestamp()
-              || (chunkMetaData.getEndTime() == resultPair.getTimestamp()
-              && chunkMetaData.getVersion() > version)) {
-            Statistics chunkStatistics = chunkMetaData.getStatistics();
-            resultPair =
-                constructLastPair(
-                    chunkStatistics.getEndTime(), chunkStatistics.getLastValue(), tsDataType);
-            version = chunkMetaData.getVersion();
-          }
-        }
-      }
-    }
+    LastPointReader lastReader = new LastPointReader(
+        seriesPath, tsDataType, deviceMeasurements, context, dataSource, Long.MAX_VALUE, filter);
+    TimeValuePair resultPair = lastReader.readLastPoint();
 
-    // Update cached last value with low priority
-    if (lastCacheEnabled) {
-      IoTDB.metaManager.updateLastCache(seriesPath,
-              resultPair, false, Long.MIN_VALUE, node);
+    // Update cached last value with low priority unless "FROM" expression exists
+    if (lastCacheEnabled && filter == null) {
+      IoTDB.metaManager.updateLastCache(
+          seriesPath, resultPair, false, Long.MIN_VALUE, node);
     }
     return resultPair;
   }
 
-  private static TimeValuePair constructLastPair(long timestamp, Object value,
-                                                 TSDataType dataType) {
-    return new TimeValuePair(timestamp, TsPrimitiveType.getByType(dataType, value));
+  private static boolean satisfyFilter(Filter filter, TimeValuePair tvPair) {
+    if (filter != null &&
+        filter.satisfy(tvPair.getTimestamp(), tvPair.getValue().getValue())) {
+      return true;
+    }

Review comment:
       Fixed

##########
File path: server/src/main/java/org/apache/iotdb/db/query/executor/fill/LastPointReader.java
##########
@@ -216,6 +216,9 @@ private boolean shouldUpdate(long time, long version, long newTime, long newVers
   }
 
   private boolean endtimeContainedByTimeFilter(Statistics statistics) {
+    if (timeFilter == null) {
+      return false;
+    }

Review comment:
       Fixed




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