qiaojialin commented on a change in pull request #2029:
URL: https://github.com/apache/iotdb/pull/2029#discussion_r538097995



##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByEngineDataSet.java
##########
@@ -68,38 +80,81 @@ public GroupByEngineDataSet(QueryContext context, 
GroupByTimePlan groupByTimePla
       long queryRange = endTime - startTime;
       // calculate the total interval number
       long intervalNum = (long) Math.ceil(queryRange / (double) slidingStep);
-      curStartTime = slidingStep * (intervalNum - 1) + startTime;
+      if (isSlidingStepByMonth) {
+        intervalTimes = (int) intervalNum - 1;
+        curStartTime = startTime + calcIntervalByMonth(intervalTimes * 
slidingStep / MS_TO_MONTH, startTime);
+      } else {
+        curStartTime = slidingStep * (intervalNum - 1) + startTime;
+      }
+    }
+
+    if (isSlidingStepByMonth) {
+      slidingStep = slidingStep / MS_TO_MONTH;
+    }
+
+    if (isIntervalByMonth) {
+      //calculate interval length by natural month based on curStartTime
+      //ie. startTIme = 1/31, interval = 1mo, curEndTime will be set to 2/29
+      curEndTime = Math.min(startTime + calcIntervalByMonth(interval + 
slidingStep * intervalTimes, startTime), endTime);
+    } else {
+      curEndTime = Math.min(curStartTime + interval, endTime);
     }
-    curEndTime = Math.min(curStartTime + interval, endTime);
+
     this.hasCachedTimeInterval = true;
   }
 
   @Override
   protected boolean hasNextWithoutConstraint() {
+    long curSlidingStep = slidingStep;
+    long curInterval = interval;
     // has cached
     if (hasCachedTimeInterval) {
       return true;
     }
 
+    intervalTimes += ascending ? 1 : -1;
+    //group by natural month, given startTime recalculate interval and sliding 
step
+    if (isIntervalByMonth) {
+      curInterval = calcIntervalByMonth(intervalTimes * slidingStep + 
interval, startTime);
+    }
+    if (isSlidingStepByMonth) {
+      curSlidingStep = calcIntervalByMonth(slidingStep * intervalTimes, 
curStartTime);
+    }
+
     // check if the next interval out of range
     if (ascending) {
-      curStartTime += slidingStep;
+      curStartTime += curSlidingStep;
       //This is an open interval , [0-100)
       if (curStartTime >= endTime) {
         return false;
       }
     } else {
-      curStartTime -= slidingStep;
+      if (isSlidingStepByMonth) {
+        curStartTime = startTime + calcIntervalByMonth(slidingStep * 
intervalTimes, startTime);
+      } else {
+        curStartTime -= curSlidingStep;
+      }
       if (curStartTime < startTime) {
         return false;
       }
     }
 
     hasCachedTimeInterval = true;
-    curEndTime = Math.min(curStartTime + interval, endTime);
+    if (isIntervalByMonth) {
+      curEndTime = Math.min(startTime + curInterval, endTime);
+    } else {
+      curEndTime = Math.min(curStartTime + curInterval, endTime);
+    }
     return true;
   }
 
+  public long calcIntervalByMonth(long numMonths, long curStartTime) {

Review comment:
       I just wonder how to set the curStartTime... pls add a Javadoc for this 
function and the intervalTimes




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