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

[iotdb] branch master updated: [IOTDB-1059] Support sql statement insert without timestamp (#3067)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 6e9f340  [IOTDB-1059] Support sql statement insert without timestamp (#3067)
6e9f340 is described below

commit 6e9f3404442108c08493351d1447e6d2c1ec1347
Author: Superainbower <35...@users.noreply.github.com>
AuthorDate: Wed Jul 21 13:06:11 2021 +0800

    [IOTDB-1059] Support sql statement insert without timestamp (#3067)
---
 .../antlr4/org/apache/iotdb/db/qp/sql/SqlBase.g4   |   5 +-
 .../apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java    |   6 +-
 .../db/integration/IoTDBInsertWithoutTimeIT.java   | 129 +++++++++++++++++++++
 3 files changed, 136 insertions(+), 4 deletions(-)

diff --git a/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/SqlBase.g4 b/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/SqlBase.g4
index 41235f4..c678c7e 100644
--- a/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/SqlBase.g4
+++ b/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/SqlBase.g4
@@ -363,7 +363,7 @@ comparisonOperator
     ;
 
 insertColumnsSpec
-    : LR_BRACKET (TIMESTAMP|TIME) (COMMA measurementName)+ RR_BRACKET
+    : LR_BRACKET (TIMESTAMP|TIME)? (COMMA? measurementName)+ RR_BRACKET
     ;
 measurementName
     : nodeNameWithoutStar
@@ -377,6 +377,7 @@ insertValuesSpec
 insertMultiValue
     : LR_BRACKET dateFormat (COMMA measurementValue)+ RR_BRACKET
     | LR_BRACKET INT (COMMA measurementValue)+ RR_BRACKET
+    | LR_BRACKET (measurementValue COMMA?)+ RR_BRACKET
     ;
 
 measurementValue
@@ -567,7 +568,6 @@ nodeNameWithoutStar
     | PREVIOUSUNTILLAST
     | METADATA
     | TIMESERIES
-    | TIMESTAMP
     | PROPERTY
     | WITH
     | DATATYPE
@@ -614,7 +614,6 @@ nodeNameWithoutStar
     | DISABLE
     | ALIGN
     | COMPRESSION
-    | TIME
     | ATTRIBUTES
     | TAGS
     | RENAME
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 737423b..20d7d32 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
@@ -2130,8 +2130,12 @@ public class IoTDBSqlVisitor extends SqlBaseBaseVisitor<Operator> {
       long timestamp;
       if (insertMultiValues.get(i).dateFormat() != null) {
         timestamp = parseTimeFormat(insertMultiValues.get(i).dateFormat().getText());
-      } else {
+      } else if (insertMultiValues.get(i).INT() != null) {
         timestamp = Long.parseLong(insertMultiValues.get(i).INT().getText());
+      } else if (insertMultiValues.size() != 1) {
+        throw new SQLParserException("need timestamps when insert multi rows");
+      } else {
+        timestamp = parseTimeFormat(SQLConstant.NOW_FUNC);
       }
       timeArray[i] = timestamp;
       List<MeasurementValueContext> values = insertMultiValues.get(i).measurementValue();
diff --git a/server/src/test/java/org/apache/iotdb/db/integration/IoTDBInsertWithoutTimeIT.java b/server/src/test/java/org/apache/iotdb/db/integration/IoTDBInsertWithoutTimeIT.java
new file mode 100644
index 0000000..0defce0
--- /dev/null
+++ b/server/src/test/java/org/apache/iotdb/db/integration/IoTDBInsertWithoutTimeIT.java
@@ -0,0 +1,129 @@
+/*
+ * 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.
+ */
+
+package org.apache.iotdb.db.integration;
+
+import org.apache.iotdb.db.utils.EnvironmentUtils;
+import org.apache.iotdb.jdbc.Config;
+
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+/** @Author: Architect @Date: 2021-07-13 16:32 */
+public class IoTDBInsertWithoutTimeIT {
+  private static List<String> sqls = new ArrayList<>();
+  private static Connection connection;
+
+  @BeforeClass
+  public static void setUp() throws Exception {
+    EnvironmentUtils.closeStatMonitor();
+    initCreateSQLStatement();
+    EnvironmentUtils.envSetUp();
+    insertData();
+  }
+
+  @AfterClass
+  public static void tearDown() throws Exception {
+    close();
+    EnvironmentUtils.cleanEnv();
+  }
+
+  private static void close() {
+    if (Objects.nonNull(connection)) {
+      try {
+        connection.close();
+      } catch (Exception e) {
+        e.printStackTrace();
+      }
+    }
+  }
+
+  private static void initCreateSQLStatement() {
+    sqls.add("SET STORAGE GROUP TO root.t1");
+    sqls.add("CREATE TIMESERIES root.t1.wf01.wt01.status WITH DATATYPE=BOOLEAN, ENCODING=PLAIN");
+    sqls.add("CREATE TIMESERIES root.t1.wf01.wt01.temperature WITH DATATYPE=FLOAT, ENCODING=RLE");
+  }
+
+  private static void insertData() throws ClassNotFoundException, SQLException {
+    Class.forName(Config.JDBC_DRIVER_NAME);
+    connection =
+        DriverManager.getConnection(Config.IOTDB_URL_PREFIX + "127.0.0.1:6667/", "root", "root");
+    Statement statement = connection.createStatement();
+
+    for (String sql : sqls) {
+      statement.execute(sql);
+    }
+
+    statement.close();
+  }
+
+  @Test
+  public void testInsertWithoutTime() throws SQLException, InterruptedException {
+    Statement st0 = connection.createStatement();
+    st0.execute("insert into root.t1.wf01.wt01(time, status, temperature) values (1, true, 11)");
+    st0.execute(
+        "insert into root.t1.wf01.wt01(time, status, temperature) values (2, false, 22),(3, true, 33)");
+    st0.execute("insert into root.t1.wf01.wt01(status, temperature) values (true, 10)");
+    Thread.sleep(3);
+    st0.execute("insert into root.t1.wf01.wt01(status) values (false)");
+
+    Statement st1 = connection.createStatement();
+    ResultSet rs1 = st1.executeQuery("select count(status) from root.t1.wf01.wt01");
+    rs1.next();
+    long countStatus = rs1.getLong(1);
+    Assert.assertEquals(countStatus, 5L);
+
+    st1.close();
+  }
+
+  @Test(expected = Exception.class)
+  public void testInsertWithTimesColumns() throws SQLException {
+    Statement st1 = connection.createStatement();
+    st1.execute("insert into root.t1.wf01.wt01(time) values (1)");
+  }
+
+  @Test(expected = Exception.class)
+  public void testInsertMultiRow() throws SQLException {
+    Statement st1 = connection.createStatement();
+    st1.execute("insert into root.t1.wf01.wt01(status) values (false),(true)");
+  }
+
+  @Test(expected = Exception.class)
+  public void testInsertWithMultiTimesColumns1() throws SQLException {
+    Statement st1 = connection.createStatement();
+    st1.execute("insert into root.t1.wf01.wt01(time,time) values(1,1)");
+  }
+
+  @Test(expected = Exception.class)
+  public void testInsertWithMultiTimesColumns2() throws SQLException {
+    Statement st1 = connection.createStatement();
+    st1.execute("insert into root.t1.wf01.wt01(time,status,time) values(1,false,1)");
+  }
+}