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

hui 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 5721496be8 [IOTDB-3624] Meet error when inserting data in SQL without 
timestamp (#6642)
5721496be8 is described below

commit 5721496be88f7c44a59dd31564fd8860c8da9485
Author: ly <[email protected]>
AuthorDate: Thu Jul 21 10:45:17 2022 +0800

    [IOTDB-3624] Meet error when inserting data in SQL without timestamp (#6642)
    
    Co-authored-by: Minghui Liu <[email protected]>
---
 .../iotdb/db/it/IoTDBInsertWithoutTimeIT.java      | 122 +++++++++++++++++++
 .../org/apache/iotdb/db/it/utils/TestUtils.java    |  12 +-
 .../db/integration/IoTDBInsertWithoutTimeIT.java   | 131 ---------------------
 .../iotdb/db/mpp/plan/parser/ASTVisitor.java       |  15 ++-
 .../apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java    |  15 ++-
 5 files changed, 153 insertions(+), 142 deletions(-)

diff --git 
a/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBInsertWithoutTimeIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBInsertWithoutTimeIT.java
new file mode 100644
index 0000000000..70518b235a
--- /dev/null
+++ 
b/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBInsertWithoutTimeIT.java
@@ -0,0 +1,122 @@
+/*
+ * 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.it;
+
+import org.apache.iotdb.it.env.EnvFactory;
+import org.apache.iotdb.it.framework.IoTDBTestRunner;
+import org.apache.iotdb.itbase.category.ClusterIT;
+import org.apache.iotdb.itbase.category.LocalStandaloneIT;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.apache.iotdb.db.it.utils.TestUtils.assertNonQueryTestFail;
+import static org.apache.iotdb.db.it.utils.TestUtils.resultSetEqualTest;
+import static org.junit.Assert.fail;
+
+@RunWith(IoTDBTestRunner.class)
+@Category({LocalStandaloneIT.class, ClusterIT.class})
+public class IoTDBInsertWithoutTimeIT {
+
+  private static final List<String> sqls =
+      Arrays.asList(
+          "SET STORAGE GROUP TO root.sg1",
+          "CREATE TIMESERIES root.sg1.d1.s1 INT64",
+          "CREATE TIMESERIES root.sg1.d1.s2 FLOAT",
+          "CREATE TIMESERIES root.sg1.d1.s3 TEXT");
+
+  @Before
+  public void setUp() throws Exception {
+    EnvFactory.getEnv().initBeforeTest();
+    createTimeseries();
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    EnvFactory.getEnv().cleanAfterTest();
+  }
+
+  private void createTimeseries() {
+    try (Connection connection = EnvFactory.getEnv().getConnection();
+        Statement statement = connection.createStatement()) {
+      for (String sql : sqls) {
+        statement.execute(sql);
+      }
+    } catch (SQLException e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+  }
+
+  @Test
+  public void testInsertWithoutTime() {
+    try (Connection connection = EnvFactory.getEnv().getConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("insert into root.sg1.d1(s1, s2, s3) values (1, 1, 
'1')");
+      statement.execute("insert into root.sg1.d1(s2, s1, s3) values (2, 2, 
'2')");
+      statement.execute("insert into root.sg1.d1(s3, s2, s1) values ('3', 3, 
3)");
+      statement.execute("insert into root.sg1.d1(s1) values (1)");
+      statement.execute("insert into root.sg1.d1(s2) values (2)");
+      statement.execute("insert into root.sg1.d1(s3) values ('3')");
+    } catch (SQLException e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+
+    String expectedHeader = 
"count(root.sg1.d1.s1),count(root.sg1.d1.s2),count(root.sg1.d1.s3),";
+    String[] retArray = new String[] {"4,4,4,"};
+    resultSetEqualTest(
+        "select count(s1), count(s2), count(s3) from root.sg1.d1", 
expectedHeader, retArray);
+  }
+
+  @Test
+  public void testInsertWithoutValueColumns() {
+    assertNonQueryTestFail(
+        "insert into root.sg1.d1(time) values (1)", "Error occurred while 
parsing SQL");
+  }
+
+  @Test
+  public void testInsertMultiRow() {
+    assertNonQueryTestFail(
+        "insert into root.sg1.d1(s3) values ('1'), ('2')",
+        "need timestamps when insert multi rows");
+    assertNonQueryTestFail(
+        "insert into root.sg1.d1(s1, s2) values (1, 1), (2, 2)",
+        "need timestamps when insert multi rows");
+  }
+
+  @Test
+  public void testInsertWithMultiTimesColumns() {
+    assertNonQueryTestFail(
+        "insert into root.sg1.d1(time, time) values (1, 1)", "Error occurred 
while parsing SQL");
+    assertNonQueryTestFail(
+        "insert into root.sg1.d1(time, s1, time) values (1, 1, 1)",
+        "Error occurred while parsing SQL");
+  }
+}
diff --git 
a/integration-test/src/test/java/org/apache/iotdb/db/it/utils/TestUtils.java 
b/integration-test/src/test/java/org/apache/iotdb/db/it/utils/TestUtils.java
index cd9d6cc9a4..7980106e0e 100644
--- a/integration-test/src/test/java/org/apache/iotdb/db/it/utils/TestUtils.java
+++ b/integration-test/src/test/java/org/apache/iotdb/db/it/utils/TestUtils.java
@@ -189,7 +189,17 @@ public class TestUtils {
     try (Connection connection = EnvFactory.getEnv().getConnection();
         Statement statement = connection.createStatement()) {
       statement.executeQuery(sql);
-      fail();
+      fail("No exception!");
+    } catch (SQLException e) {
+      Assert.assertTrue(e.getMessage(), e.getMessage().contains(errMsg));
+    }
+  }
+
+  public static void assertNonQueryTestFail(String sql, String errMsg) {
+    try (Connection connection = EnvFactory.getEnv().getConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute(sql);
+      fail("No exception!");
     } catch (SQLException e) {
       Assert.assertTrue(e.getMessage(), e.getMessage().contains(errMsg));
     }
diff --git 
a/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBInsertWithoutTimeIT.java
 
b/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBInsertWithoutTimeIT.java
deleted file mode 100644
index 3e43055e2d..0000000000
--- 
a/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBInsertWithoutTimeIT.java
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * 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.itbase.category.LocalStandaloneTest;
-import org.apache.iotdb.jdbc.Config;
-
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-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 */
-@Category({LocalStandaloneTest.class})
-public class IoTDBInsertWithoutTimeIT {
-  private static List<String> sqls = new ArrayList<>();
-  private static Connection connection;
-
-  @BeforeClass
-  public static void setUp() throws Exception {
-    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)");
-  }
-}
diff --git 
a/server/src/main/java/org/apache/iotdb/db/mpp/plan/parser/ASTVisitor.java 
b/server/src/main/java/org/apache/iotdb/db/mpp/plan/parser/ASTVisitor.java
index 5ca1454d64..c81604671f 100644
--- a/server/src/main/java/org/apache/iotdb/db/mpp/plan/parser/ASTVisitor.java
+++ b/server/src/main/java/org/apache/iotdb/db/mpp/plan/parser/ASTVisitor.java
@@ -1272,13 +1272,19 @@ public class ASTVisitor extends 
IoTDBSqlParserBaseVisitor<Statement> {
     for (int i = 0; i < insertMultiValues.size(); i++) {
       // parse timestamp
       long timestamp;
+      List<String> valueList = new ArrayList<>();
+
       if (insertMultiValues.get(i).timeValue() != null) {
         if (isTimeDefault) {
-          throw new SemanticException(
-              "the measurementList's size is not consistent with the 
valueList's size");
+          if (insertMultiValues.size() != 1) {
+            throw new SemanticException("need timestamps when insert multi 
rows");
+          }
+          valueList.add(insertMultiValues.get(i).timeValue().getText());
+          timestamp = DatetimeUtils.currentTime();
+        } else {
+          timestamp =
+              parseTimeValue(insertMultiValues.get(i).timeValue(), 
DatetimeUtils.currentTime());
         }
-        timestamp =
-            parseTimeValue(insertMultiValues.get(i).timeValue(), 
DatetimeUtils.currentTime());
       } else {
         if (!isTimeDefault) {
           throw new SemanticException(
@@ -1292,7 +1298,6 @@ public class ASTVisitor extends 
IoTDBSqlParserBaseVisitor<Statement> {
       timeArray[i] = timestamp;
 
       // parse values
-      List<String> valueList = new ArrayList<>();
       List<IoTDBSqlParser.MeasurementValueContext> values =
           insertMultiValues.get(i).measurementValue();
       for (IoTDBSqlParser.MeasurementValueContext value : values) {
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 a6418326b1..f389236c65 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
@@ -1835,13 +1835,19 @@ public class IoTDBSqlVisitor extends 
IoTDBSqlParserBaseVisitor<Operator> {
     for (int i = 0; i < insertMultiValues.size(); i++) {
       // parse timestamp
       long timestamp;
+      List<String> valueList = new ArrayList<>();
+
       if (insertMultiValues.get(i).timeValue() != null) {
         if (isTimeDefault) {
-          throw new SQLParserException(
-              "the measurementList's size is not consistent with the 
valueList's size");
+          if (insertMultiValues.size() != 1) {
+            throw new SQLParserException("need timestamps when insert multi 
rows");
+          }
+          valueList.add(insertMultiValues.get(i).timeValue().getText());
+          timestamp = DatetimeUtils.currentTime();
+        } else {
+          timestamp =
+              parseTimeValue(insertMultiValues.get(i).timeValue(), 
DatetimeUtils.currentTime());
         }
-        timestamp =
-            parseTimeValue(insertMultiValues.get(i).timeValue(), 
DatetimeUtils.currentTime());
       } else {
         if (!isTimeDefault) {
           throw new SQLParserException(
@@ -1855,7 +1861,6 @@ public class IoTDBSqlVisitor extends 
IoTDBSqlParserBaseVisitor<Operator> {
       timeArray[i] = timestamp;
 
       // parse values
-      List<String> valueList = new ArrayList<>();
       List<IoTDBSqlParser.MeasurementValueContext> values =
           insertMultiValues.get(i).measurementValue();
       for (IoTDBSqlParser.MeasurementValueContext value : values) {

Reply via email to