liutaohua commented on a change in pull request #1650:
URL: https://github.com/apache/incubator-iotdb/pull/1650#discussion_r486789846



##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;
         }
       } else {
+        cachedTimestamps.addFirst(timestamp);

Review comment:
       i think we need put it back , for example , the interval is [0,10) , 
[10,20), [20,30), if the first cache is 30 ,and the end time is 10, so that 
need put back 

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,

Review comment:
       fixed

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,
+是**伪倒序读取**(因为文件内依然是升序存储的),数据示例:
+
+```
+    按照开始时间升序排列 (1,6)
+   +---TsFile1---+   +---TsFile2---+   
+   | 1,2,3,4,5   |   | 6,7,8,9,10  |
+   +-------------+   +-------------+
+   
+    按照开始时间降序排列 (10,5)

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;
         }
       } else {
+        cachedTimestamps.addFirst(timestamp);

Review comment:
       i think we need put it back , for example , the interval is [0,10) , 
[10,20), [20,30), if the first cache is 30 ,and the end time is 10, so that 
need put back 

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,

Review comment:
       fixed

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,
+是**伪倒序读取**(因为文件内依然是升序存储的),数据示例:
+
+```
+    按照开始时间升序排列 (1,6)
+   +---TsFile1---+   +---TsFile2---+   
+   | 1,2,3,4,5   |   | 6,7,8,9,10  |
+   +-------------+   +-------------+
+   
+    按照开始时间降序排列 (10,5)

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;
         }
       } else {
+        cachedTimestamps.addFirst(timestamp);

Review comment:
       i think we need put it back , for example , the interval is [0,10) , 
[10,20), [20,30), if the first cache is 30 ,and the end time is 10, so that 
need put back 

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,

Review comment:
       fixed

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,
+是**伪倒序读取**(因为文件内依然是升序存储的),数据示例:
+
+```
+    按照开始时间升序排列 (1,6)
+   +---TsFile1---+   +---TsFile2---+   
+   | 1,2,3,4,5   |   | 6,7,8,9,10  |
+   +-------------+   +-------------+
+   
+    按照开始时间降序排列 (10,5)

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;
         }
       } else {
+        cachedTimestamps.addFirst(timestamp);

Review comment:
       i think we need put it back , for example , the interval is [0,10) , 
[10,20), [20,30), if the first cache is 30 ,and the end time is 10, so that 
need put back 

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,

Review comment:
       fixed

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,
+是**伪倒序读取**(因为文件内依然是升序存储的),数据示例:
+
+```
+    按照开始时间升序排列 (1,6)
+   +---TsFile1---+   +---TsFile2---+   
+   | 1,2,3,4,5   |   | 6,7,8,9,10  |
+   +-------------+   +-------------+
+   
+    按照开始时间降序排列 (10,5)

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;
         }
       } else {
+        cachedTimestamps.addFirst(timestamp);

Review comment:
       i think we need put it back , for example , the interval is [0,10) , 
[10,20), [20,30), if the first cache is 30 ,and the end time is 10, so that 
need put back 

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,

Review comment:
       fixed

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,
+是**伪倒序读取**(因为文件内依然是升序存储的),数据示例:
+
+```
+    按照开始时间升序排列 (1,6)
+   +---TsFile1---+   +---TsFile2---+   
+   | 1,2,3,4,5   |   | 6,7,8,9,10  |
+   +-------------+   +-------------+
+   
+    按照开始时间降序排列 (10,5)

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;
         }
       } else {
+        cachedTimestamps.addFirst(timestamp);

Review comment:
       i think we need put it back , for example , the interval is [0,10) , 
[10,20), [20,30), if the first cache is 30 ,and the end time is 10, so that 
need put back 

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,

Review comment:
       fixed

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,
+是**伪倒序读取**(因为文件内依然是升序存储的),数据示例:
+
+```
+    按照开始时间升序排列 (1,6)
+   +---TsFile1---+   +---TsFile2---+   
+   | 1,2,3,4,5   |   | 6,7,8,9,10  |
+   +-------------+   +-------------+
+   
+    按照开始时间降序排列 (10,5)

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;
         }
       } else {
+        cachedTimestamps.addFirst(timestamp);

Review comment:
       i think we need put it back , for example , the interval is [0,10) , 
[10,20), [20,30), if the first cache is 30 ,and the end time is 10, so that 
need put back 

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,

Review comment:
       fixed

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,
+是**伪倒序读取**(因为文件内依然是升序存储的),数据示例:
+
+```
+    按照开始时间升序排列 (1,6)
+   +---TsFile1---+   +---TsFile2---+   
+   | 1,2,3,4,5   |   | 6,7,8,9,10  |
+   +-------------+   +-------------+
+   
+    按照开始时间降序排列 (10,5)

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;
         }
       } else {
+        cachedTimestamps.addFirst(timestamp);

Review comment:
       i think we need put it back , for example , the interval is [0,10) , 
[10,20), [20,30), if the first cache is 30 ,and the end time is 10, so that 
need put back 

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,

Review comment:
       fixed

##########
File path: docs/zh/SystemDesign/DataQuery/OrderByTimeQuery.md
##########
@@ -0,0 +1,190 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+        http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+
+# 按时间倒序查询
+
+## 实现原理
+
+倒序分为 3 个维度来实现,由内核层到用户层分别为: `TsFile`、`BatchData`、`数据集`.
+1. `TsFile` 是用来存储所有原始数据的文件格式,在存储时分为 `顺序文件` 和 `乱序文件`.
+`ChunkData` 是 `TsFile` 中的基本数据块,保存了具体某一个测点的数据,且**数据是按照时间升序存储的**.
+2. `BatchData` 是基础数据交换结构,无论是从文件系统还是缓存读出的数据都会被转换成 `BatchData` 结构.
+3. `数据集` 是封装根据用户输入的 `SQL` 语句的结果集格式,转换和封装 `BatchData` 中的数据。
+
+实际系统需要考虑的方面会更多,但主要为以上 3 点。
+下面分别介绍各个层面如何实现基于时间的倒序查询:
+
+## TsFile
+1.`TsFile` 在文件末尾存储了各个测点在文件中的最小时间(开始时间)和最大时间(结束时间),
+所以当我们按照开始时间升序排列文件并读取数据时,是升序读取;当使用开始时间倒序排列文件并读取数据时,
+是**伪倒序读取**(因为文件内依然是升序存储的),数据示例:
+
+```
+    按照开始时间升序排列 (1,6)
+   +---TsFile1---+   +---TsFile2---+   
+   | 1,2,3,4,5   |   | 6,7,8,9,10  |
+   +-------------+   +-------------+
+   
+    按照开始时间降序排列 (10,5)

Review comment:
       fixed

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByWithValueFilterDataSet.java
##########
@@ -122,13 +127,18 @@ protected RowRecord nextWithoutConstraint() throws 
IOException {
 
     long[] timestampArray = new long[timeStampFetchSize];
     int timeArrayLength = 0;
-    if (hasCachedTimestamp) {
+    while (!cachedTimestamps.isEmpty()) {
+      long timestamp = cachedTimestamps.remove();
       if (timestamp < curEndTime) {
+        if (!groupByTimePlan.isAscending() && timestamp < curStartTime) {
+          cachedTimestamps.addFirst(timestamp);
+          return constructRowRecord(aggregateResultList);
+        }
         if (timestamp >= curStartTime) {
-          hasCachedTimestamp = false;
           timestampArray[timeArrayLength++] = timestamp;

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:
us...@infra.apache.org


Reply via email to