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/23 01:08:41 UTC

[iotdb] branch master updated: [IOTDB-959]Add createStorageGroup Grammar (#3608)

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 3c6925c  [IOTDB-959]Add createStorageGroup Grammar (#3608)
3c6925c is described below

commit 3c6925ce4746e9dfd3b2de1afcf5d6f49223454b
Author: Hang Ji <55...@users.noreply.github.com>
AuthorDate: Fri Jul 23 09:08:11 2021 +0800

    [IOTDB-959]Add createStorageGroup Grammar (#3608)
---
 .../antlr4/org/apache/iotdb/db/qp/sql/SqlBase.g4   |   1 +
 .../DDL-Data-Definition-Language.md                |   6 +-
 .../DDL-Data-Definition-Language.md                |   6 +-
 .../apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java    |  10 ++
 .../db/integration/IoTDBCreateStorageGroupIT.java  | 128 +++++++++++++++++++++
 .../iotdb/db/qp/physical/PhysicalPlanTest.java     |   8 ++
 6 files changed, 155 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 c678c7e..2e157d8 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
@@ -37,6 +37,7 @@ statement
     | UPDATE prefixPath setClause whereClause? #updateStatement
     | DELETE FROM prefixPath (COMMA prefixPath)* (whereClause)? #deleteStatement
     | SET STORAGE GROUP TO prefixPath #setStorageGroup
+    | CREATE STORAGE GROUP prefixPath #createStorageGroup
     | DELETE STORAGE GROUP prefixPath (COMMA prefixPath)* #deleteStorageGroup
     | SHOW METADATA #showMetadata // not support yet
     | DESCRIBE prefixPath #describePath // not support yet
diff --git a/docs/UserGuide/IoTDB-SQL-Language/DDL-Data-Definition-Language.md b/docs/UserGuide/IoTDB-SQL-Language/DDL-Data-Definition-Language.md
index 5f9f712..0c0d3b9 100644
--- a/docs/UserGuide/IoTDB-SQL-Language/DDL-Data-Definition-Language.md
+++ b/docs/UserGuide/IoTDB-SQL-Language/DDL-Data-Definition-Language.md
@@ -24,11 +24,11 @@
 ## Stroage Group Management
 ### Create Storage Group
 
-According to the storage model we can set up the corresponding storage group. The SQL statements for creating storage groups are as follows:
+According to the storage model we can set up the corresponding storage group. Two SQL statements are supported for creating storage groups, as follows:
 
 ```
 IoTDB > set storage group to root.ln
-IoTDB > set storage group to root.sgcc
+IoTDB > create storage group root.sgcc
 ```
 
 We can thus create two storage groups using the above two SQL statements.
@@ -38,6 +38,8 @@ It is worth noting that when the path itself or the parent/child layer of the pa
 ```
 IoTDB> set storage group to root.ln.wf01
 Msg: 300: root.ln has already been set to storage group.
+IoTDB> create storage group root.ln.wf01
+Msg: 300: root.ln has already been set to storage group.
 ```
 The LayerName of storage group can only be characters, numbers, underscores and hyphens. 
  
diff --git a/docs/zh/UserGuide/IoTDB-SQL-Language/DDL-Data-Definition-Language.md b/docs/zh/UserGuide/IoTDB-SQL-Language/DDL-Data-Definition-Language.md
index 941567c..e20059e 100644
--- a/docs/zh/UserGuide/IoTDB-SQL-Language/DDL-Data-Definition-Language.md
+++ b/docs/zh/UserGuide/IoTDB-SQL-Language/DDL-Data-Definition-Language.md
@@ -25,11 +25,11 @@
 
 ### 创建存储组
 
-我们可以根据存储模型建立相应的存储组。创建存储组的SQL语句如下所示:
+我们可以根据存储模型建立相应的存储组。创建存储组支持两种SQL语句,如下所示:
 
 ```
 IoTDB > set storage group to root.ln
-IoTDB > set storage group to root.sgcc
+IoTDB > create storage group root.sgcc
 ```
 
 根据以上两条SQL语句,我们可以创建出两个存储组。
@@ -39,6 +39,8 @@ IoTDB > set storage group to root.sgcc
 ```
 IoTDB> set storage group to root.ln.wf01
 Msg: 300: root.ln has already been set to storage group.
+IoTDB> create storage group root.ln.wf01
+Msg: 300: root.ln has already been set to storage group.
 ```
 存储组节点名只支持中英文字符、数字、下划线和中划线的组合。
 
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 20d7d32..5d6989c 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
@@ -117,6 +117,7 @@ import org.apache.iotdb.db.qp.sql.SqlBaseParser.CreateFunctionContext;
 import org.apache.iotdb.db.qp.sql.SqlBaseParser.CreateIndexContext;
 import org.apache.iotdb.db.qp.sql.SqlBaseParser.CreateRoleContext;
 import org.apache.iotdb.db.qp.sql.SqlBaseParser.CreateSnapshotContext;
+import org.apache.iotdb.db.qp.sql.SqlBaseParser.CreateStorageGroupContext;
 import org.apache.iotdb.db.qp.sql.SqlBaseParser.CreateTimeseriesContext;
 import org.apache.iotdb.db.qp.sql.SqlBaseParser.CreateTriggerContext;
 import org.apache.iotdb.db.qp.sql.SqlBaseParser.CreateUserContext;
@@ -1114,6 +1115,15 @@ public class IoTDBSqlVisitor extends SqlBaseBaseVisitor<Operator> {
   }
 
   @Override
+  public Operator visitCreateStorageGroup(CreateStorageGroupContext ctx) {
+    SetStorageGroupOperator setStorageGroupOperator =
+        new SetStorageGroupOperator(SQLConstant.TOK_METADATA_SET_FILE_LEVEL);
+    PartialPath path = parsePrefixPath(ctx.prefixPath());
+    setStorageGroupOperator.setPath(path);
+    return setStorageGroupOperator;
+  }
+
+  @Override
   public Operator visitCreateContinuousQueryStatement(CreateContinuousQueryStatementContext ctx) {
     CreateContinuousQueryOperator createContinuousQueryOperator =
         new CreateContinuousQueryOperator(SQLConstant.TOK_CONTINUOUS_QUERY_CREATE);
diff --git a/server/src/test/java/org/apache/iotdb/db/integration/IoTDBCreateStorageGroupIT.java b/server/src/test/java/org/apache/iotdb/db/integration/IoTDBCreateStorageGroupIT.java
new file mode 100644
index 0000000..28d75d1
--- /dev/null
+++ b/server/src/test/java/org/apache/iotdb/db/integration/IoTDBCreateStorageGroupIT.java
@@ -0,0 +1,128 @@
+/*
+ * 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.After;
+import org.junit.Assert;
+import org.junit.Before;
+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.stream.Collectors;
+
+/**
+ * Notice that, all test begins with "IoTDB" is integration test. All test which will start the
+ * IoTDB server should be defined as integration test.
+ */
+public class IoTDBCreateStorageGroupIT {
+  private Statement statement;
+  private Connection connection;
+
+  @Before
+  public void setUp() throws Exception {
+    EnvironmentUtils.envSetUp();
+
+    Class.forName(Config.JDBC_DRIVER_NAME);
+    connection = DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
+    statement = connection.createStatement();
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    statement.close();
+    connection.close();
+    EnvironmentUtils.cleanEnv();
+  }
+
+  /** The test creates three storage groups */
+  @Test
+  public void testCreateStorageGroup() throws Exception {
+    String[] storageGroups = {"root.sg1", "root.sg2", "root.sg3"};
+
+    for (String storageGroup : storageGroups) {
+      statement.execute(String.format("create storage group %s", storageGroup));
+    }
+
+    // ensure that current StorageGroup in cache is right.
+    createStorageGroupTool(storageGroups);
+
+    EnvironmentUtils.stopDaemon();
+    setUp();
+
+    // ensure StorageGroup in cache is right after recovering.
+    createStorageGroupTool(storageGroups);
+  }
+
+  private void createStorageGroupTool(String[] storageGroups) throws SQLException {
+    boolean hasResult = statement.execute("show storage group");
+    Assert.assertTrue(hasResult);
+
+    List<String> resultList = new ArrayList<>();
+    try (ResultSet resultSet = statement.getResultSet()) {
+      while (resultSet.next()) {
+        String storageGroupPath = resultSet.getString("storage group");
+        resultList.add(storageGroupPath);
+      }
+    }
+    Assert.assertEquals(3, resultList.size());
+
+    resultList = resultList.stream().sorted().collect(Collectors.toList());
+
+    Assert.assertEquals(storageGroups[0], resultList.get(0));
+    Assert.assertEquals(storageGroups[1], resultList.get(1));
+    Assert.assertEquals(storageGroups[2], resultList.get(2));
+  }
+
+  /** Test creating a storage group that path is an existence storage group */
+  @Test
+  public void testCreateExistStorageGroup1() throws Exception {
+    String storageGroup = "root.sg";
+
+    statement.execute(String.format("set storage group to %s", storageGroup));
+
+    try {
+      statement.execute(String.format("create storage group %s", storageGroup));
+    } catch (SQLException e) {
+      Assert.assertEquals(e.getMessage(), "300: root.sg has already been set to storage group");
+    }
+  }
+
+  /** Test the parent node has been set as a storage group */
+  @Test
+  public void testCreateExistStorageGroup2() throws Exception {
+
+    statement.execute("create storage group root.sg");
+
+    try {
+      statement.execute("create storage group root.sg.device");
+    } catch (SQLException e) {
+      Assert.assertEquals(e.getMessage(), "300: root.sg has already been set to storage group");
+    }
+  }
+}
diff --git a/server/src/test/java/org/apache/iotdb/db/qp/physical/PhysicalPlanTest.java b/server/src/test/java/org/apache/iotdb/db/qp/physical/PhysicalPlanTest.java
index d8efa63..2712308 100644
--- a/server/src/test/java/org/apache/iotdb/db/qp/physical/PhysicalPlanTest.java
+++ b/server/src/test/java/org/apache/iotdb/db/qp/physical/PhysicalPlanTest.java
@@ -48,6 +48,7 @@ import org.apache.iotdb.db.qp.physical.sys.DropFunctionPlan;
 import org.apache.iotdb.db.qp.physical.sys.DropTriggerPlan;
 import org.apache.iotdb.db.qp.physical.sys.LoadConfigurationPlan;
 import org.apache.iotdb.db.qp.physical.sys.OperateFilePlan;
+import org.apache.iotdb.db.qp.physical.sys.SetStorageGroupPlan;
 import org.apache.iotdb.db.qp.physical.sys.ShowContinuousQueriesPlan;
 import org.apache.iotdb.db.qp.physical.sys.ShowFunctionsPlan;
 import org.apache.iotdb.db.qp.physical.sys.ShowPlan;
@@ -1459,4 +1460,11 @@ public class PhysicalPlanTest {
     Assert.assertTrue(plan.isQuery());
     Assert.assertEquals(ShowContentType.FUNCTIONS, plan.getShowContentType());
   }
+
+  @Test
+  public void testCreateStorageGroup() throws QueryProcessException {
+    String sqlStr = "CREATE STORAGE GROUP root.sg";
+    SetStorageGroupPlan plan = (SetStorageGroupPlan) processor.parseSQLToPhysicalPlan(sqlStr);
+    assertEquals("SetStorageGroup{root.sg}", plan.toString());
+  }
 }