You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by hx...@apache.org on 2020/03/03 00:48:14 UTC

[incubator-iotdb] branch master updated: [IOTDB-529] Relative times and NOW() operator cannot be used in Group By (#871)

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

hxd pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new dea6737  [IOTDB-529] Relative times and NOW() operator cannot be used in Group By (#871)
dea6737 is described below

commit dea6737bc183340a0f6c48ffcf644f78cb358a43
Author: Haonan <hh...@outlook.com>
AuthorDate: Tue Mar 3 08:48:03 2020 +0800

    [IOTDB-529] Relative times and NOW() operator cannot be used in Group By (#871)
    
    * use NOW() operator in Group by
---
 .../org/apache/iotdb/db/qp/strategy/SqlBase.g4     |  1 +
 .../iotdb/db/qp/strategy/LogicalGenerator.java     |  4 ++
 .../iotdb/db/integration/IOTDBGroupByIT.java       | 62 ++++++++++++++++++++++
 3 files changed, 67 insertions(+)

diff --git a/server/src/main/antlr4/org/apache/iotdb/db/qp/strategy/SqlBase.g4 b/server/src/main/antlr4/org/apache/iotdb/db/qp/strategy/SqlBase.g4
index 8dc81d2..9e547e3 100644
--- a/server/src/main/antlr4/org/apache/iotdb/db/qp/strategy/SqlBase.g4
+++ b/server/src/main/antlr4/org/apache/iotdb/db/qp/strategy/SqlBase.g4
@@ -258,6 +258,7 @@ timeInterval
 
 timeValue
     : dateFormat
+    | dateExpression
     | INT
     ;
 
diff --git a/server/src/main/java/org/apache/iotdb/db/qp/strategy/LogicalGenerator.java b/server/src/main/java/org/apache/iotdb/db/qp/strategy/LogicalGenerator.java
index 272bc14..b67f048 100644
--- a/server/src/main/java/org/apache/iotdb/db/qp/strategy/LogicalGenerator.java
+++ b/server/src/main/java/org/apache/iotdb/db/qp/strategy/LogicalGenerator.java
@@ -688,11 +688,15 @@ public class LogicalGenerator extends SqlBaseBaseListener {
     TimeIntervalContext timeInterval = ctx.timeInterval();
     if (timeInterval.timeValue(0).INT() != null) {
       startTime = Long.parseLong(timeInterval.timeValue(0).INT().getText());
+    } else if (timeInterval.timeValue(0).dateExpression() != null) {
+      startTime = parseDateExpression(timeInterval.timeValue(0).dateExpression());
     } else {
       startTime = parseTimeFormat(timeInterval.timeValue(0).dateFormat().getText());
     }
     if (timeInterval.timeValue(1).INT() != null) {
       endTime = Long.parseLong(timeInterval.timeValue(1).INT().getText());
+    } else if (timeInterval.timeValue(1).dateExpression() != null) {
+      endTime = parseDateExpression(timeInterval.timeValue(1).dateExpression());
     } else {
       endTime = parseTimeFormat(timeInterval.timeValue(1).dateFormat().getText());
     }
diff --git a/server/src/test/java/org/apache/iotdb/db/integration/IOTDBGroupByIT.java b/server/src/test/java/org/apache/iotdb/db/integration/IOTDBGroupByIT.java
index 3ccad2a..6f737ad 100644
--- a/server/src/test/java/org/apache/iotdb/db/integration/IOTDBGroupByIT.java
+++ b/server/src/test/java/org/apache/iotdb/db/integration/IOTDBGroupByIT.java
@@ -506,6 +506,68 @@ public class IOTDBGroupByIT {
     }
   }
 
+  @Test
+  public void countSumAvgNoDataTest() {
+    String[] retArray1 = new String[]{
+        ",0,0.0,null",
+        ",0,0.0,null",
+        ",0,0.0,null",
+        ",0,0.0,null",
+        ",0,0.0,null",
+        ",0,0.0,null",
+    };
+
+    try (Connection connection = DriverManager.
+        getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
+        Statement statement = connection.createStatement()) {
+      boolean hasResultSet = statement.execute(
+          "select count(temperature), sum(temperature), avg(temperature) from "
+              + "root.ln.wf01.wt01 where temperature > 3 "
+              + "GROUP BY ([NOW()-30ms, NOW()), 5ms)");
+
+      Assert.assertTrue(hasResultSet);
+      int cnt;
+      try (ResultSet resultSet = statement.getResultSet()) {
+        cnt = 0;
+        while (resultSet.next()) {
+          String ans = "," + resultSet
+              .getString(count("root.ln.wf01.wt01.temperature")) + "," +
+              resultSet.getString(sum("root.ln.wf01.wt01.temperature")) + "," + resultSet
+              .getString(avg("root.ln.wf01.wt01.temperature"));
+          Assert.assertEquals(retArray1[cnt], ans);
+          cnt++;
+        }
+        Assert.assertEquals(retArray1.length, cnt);
+      }
+    } catch (Exception e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+  }
+
+  @Test
+  public void usingNowFunction() {
+    try (Connection connection = DriverManager.
+        getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
+        Statement statement = connection.createStatement()) {
+      statement.execute("INSERT INTO root.ln.wf01.wt01(timestamp,temperature,status, hardware) "
+          + "values(now(), 35.5, false, 650)");
+      ResultSet resultSet = statement.executeQuery(
+          "select count(temperature), sum(temperature), avg(temperature) from "
+              + "root.ln.wf01.wt01 "
+              + "GROUP BY ([now() - 1h, now() + 1h), 2h)");
+      Assert.assertTrue(resultSet.next());
+      //resultSet.getLong(1) is the timestamp
+      Assert.assertEquals(1, Integer.valueOf(resultSet.getString(2)).intValue());
+      Assert.assertEquals(35.5, Float.valueOf(resultSet.getString(3)).floatValue(), 0.01);
+      Assert.assertEquals(35.5, Double.valueOf(resultSet.getString(4)).doubleValue(), 0.01);
+
+    } catch (Exception e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+  }
+
   private void prepareData() {
     try (Connection connection = DriverManager
         .getConnection(Config.IOTDB_URL_PREFIX + "127.0.0.1:6667/", "root",