You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by xi...@apache.org on 2021/05/07 08:10:05 UTC

[iotdb] branch groupbymonthbug updated: add tests

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

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


The following commit(s) were added to refs/heads/groupbymonthbug by this push:
     new 8153262  add tests
8153262 is described below

commit 815326253f87cb7e4e49819d0d3e7ec0f0b6a494
Author: Alima777 <wx...@gmail.com>
AuthorDate: Fri May 7 16:09:26 2021 +0800

    add tests
---
 .../apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java    | 12 +++++++++-
 .../iotdb/db/integration/IoTDBGroupByMonthIT.java  | 28 ++++++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/server/src/main/java/org/apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java b/server/src/main/java/org/apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java
index 3b2f32c..bfebaaf 100644
--- a/server/src/main/java/org/apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java
+++ b/server/src/main/java/org/apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java
@@ -1863,13 +1863,16 @@ public class IoTDBSqlVisitor extends SqlBaseBaseVisitor<Operator> {
   }
 
   /**
-   * parse time unit or sliding step in group by query.
+   * parse time unit or sliding step in group by query. If 'mo' is used, then other units can not be
+   * used together, like '1mo1d'.
    *
    * @param durationStr represent duration string like: 12d8m9ns, 1y1mo, etc.
    * @return time in milliseconds, microseconds, or nanoseconds depending on the profile
    */
   private long parseTimeUnitOrSlidingStep(
       QueryOperator queryOp, String durationStr, boolean isParsingTimeUnit) {
+    boolean hasMonthUnit = false;
+    boolean hasOtherUnits = false;
     for (int i = 0; i < durationStr.length(); i++) {
       char ch = durationStr.charAt(i);
       if (!Character.isDigit(ch)) {
@@ -1885,9 +1888,16 @@ public class IoTDBSqlVisitor extends SqlBaseBaseVisitor<Operator> {
           } else {
             queryOp.setSlidingStepByMonth(true);
           }
+          hasMonthUnit = true;
+        } else {
+          hasOtherUnits = true;
         }
       }
     }
+    if (hasMonthUnit && hasOtherUnits) {
+      throw new SQLParserException(
+          "Natural month unit can not be used with other time units together now.");
+    }
     return parseDuration(durationStr);
   }
 
diff --git a/server/src/test/java/org/apache/iotdb/db/integration/IoTDBGroupByMonthIT.java b/server/src/test/java/org/apache/iotdb/db/integration/IoTDBGroupByMonthIT.java
index 5ff2236..fdfb216 100644
--- a/server/src/test/java/org/apache/iotdb/db/integration/IoTDBGroupByMonthIT.java
+++ b/server/src/test/java/org/apache/iotdb/db/integration/IoTDBGroupByMonthIT.java
@@ -224,6 +224,34 @@ public class IoTDBGroupByMonthIT {
     }
   }
 
+  /** StartTime: now() - 1mo, EndTime: now(). */
+  @Test
+  public void groupByNaturalMonth6() {
+    try (Connection connection =
+            DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
+        Statement statement = connection.createStatement()) {
+
+      boolean hasResultSet =
+          statement.execute(
+              "select sum(temperature) from root.sg1.d1 GROUP BY ([now() - 1mo, now()), 1d)");
+
+      Assert.assertTrue(hasResultSet);
+      int cnt = 0;
+      try (ResultSet resultSet = statement.getResultSet()) {
+        while (resultSet.next()) {
+          String ans = resultSet.getString(sum("root.sg1.d1.temperature"));
+          if (ans.equals("0.0")) {
+            cnt++;
+          }
+        }
+        Assert.assertEquals(30, cnt);
+      }
+    } catch (Exception e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+  }
+
   private void prepareData() {
     try (Connection connection =
             DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");