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

qiaojialin 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 794ad5f  [ISSUE#1599]check datatype with encoding. (#1600)
794ad5f is described below

commit 794ad5fde7da36b5ae38da9b4e9b0741dbf07003
Author: Boris <[email protected]>
AuthorDate: Fri Aug 7 13:04:20 2020 +0800

    [ISSUE#1599]check datatype with encoding. (#1600)
---
 .../org/apache/iotdb/db/metadata/MManager.java     |  5 ++-
 .../org/apache/iotdb/db/utils/SchemaUtils.java     | 47 +++++++++++++++++++---
 .../iotdb/db/integration/IoTDBSimpleQueryIT.java   | 39 ++++++++++++++++++
 .../iotdb/db/metadata/MManagerImproveTest.java     |  2 +-
 4 files changed, 84 insertions(+), 9 deletions(-)

diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/MManager.java 
b/server/src/main/java/org/apache/iotdb/db/metadata/MManager.java
index def92ff..dd04ac1 100644
--- a/server/src/main/java/org/apache/iotdb/db/metadata/MManager.java
+++ b/server/src/main/java/org/apache/iotdb/db/metadata/MManager.java
@@ -69,6 +69,7 @@ import org.apache.iotdb.db.qp.physical.sys.ShowTimeSeriesPlan;
 import org.apache.iotdb.db.query.context.QueryContext;
 import org.apache.iotdb.db.query.dataset.ShowTimeSeriesResult;
 import org.apache.iotdb.db.utils.RandomDeleteCache;
+import org.apache.iotdb.db.utils.SchemaUtils;
 import org.apache.iotdb.db.utils.TestOnly;
 import org.apache.iotdb.db.utils.TypeInferenceUtils;
 import org.apache.iotdb.tsfile.common.conf.TSFileDescriptor;
@@ -369,8 +370,9 @@ public class MManager {
 
   public void createTimeseries(CreateTimeSeriesPlan plan, long offset) throws 
MetadataException {
     lock.writeLock().lock();
-    String path = plan.getPath().getFullPath();
     try {
+      String path = plan.getPath().getFullPath();
+      SchemaUtils.checkDataTypeWithEncoding(plan.getDataType(), 
plan.getEncoding());
       /*
        * get the storage group with auto create schema
        */
@@ -422,7 +424,6 @@ public class MManager {
         logWriter.createTimeseries(plan, offset);
       }
       leafMNode.setOffset(offset);
-
     } catch (IOException | ConfigAdjusterException e) {
       throw new MetadataException(e.getMessage());
     } finally {
diff --git a/server/src/main/java/org/apache/iotdb/db/utils/SchemaUtils.java 
b/server/src/main/java/org/apache/iotdb/db/utils/SchemaUtils.java
index abf158a..8a30c6d 100644
--- a/server/src/main/java/org/apache/iotdb/db/utils/SchemaUtils.java
+++ b/server/src/main/java/org/apache/iotdb/db/utils/SchemaUtils.java
@@ -18,6 +18,14 @@
  */
 package org.apache.iotdb.db.utils;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.EnumMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 import org.apache.iotdb.db.exception.metadata.MetadataException;
 import org.apache.iotdb.db.exception.metadata.PathAlreadyExistException;
 import org.apache.iotdb.db.qp.constant.SQLConstant;
@@ -26,22 +34,42 @@ import 
org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
 import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
 import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
 import org.apache.iotdb.tsfile.read.common.Path;
-import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
 import org.apache.iotdb.tsfile.write.schema.TimeseriesSchema;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-
 public class SchemaUtils {
 
   private SchemaUtils() {
 
   }
 
+  private static Map<TSDataType, Set<TSEncoding>> schemaChecker = new 
EnumMap<>(TSDataType.class);
+
+  static {
+    Set<TSEncoding> booleanSet = new HashSet<>();
+    booleanSet.add(TSEncoding.PLAIN);
+    booleanSet.add(TSEncoding.RLE);
+    schemaChecker.put(TSDataType.BOOLEAN, booleanSet);
+    Set<TSEncoding> int32Set = new HashSet<>();
+    int32Set.add(TSEncoding.PLAIN);
+    int32Set.add(TSEncoding.RLE);
+    int32Set.add(TSEncoding.TS_2DIFF);
+    int32Set.add(TSEncoding.REGULAR);
+    schemaChecker.put(TSDataType.INT32, int32Set);
+    schemaChecker.put(TSDataType.INT64, int32Set);
+    Set<TSEncoding> floatSet = new HashSet<>();
+    floatSet.add(TSEncoding.PLAIN);
+    floatSet.add(TSEncoding.RLE);
+    floatSet.add(TSEncoding.TS_2DIFF);
+    floatSet.add(TSEncoding.GORILLA);
+    schemaChecker.put(TSDataType.FLOAT, floatSet);
+    schemaChecker.put(TSDataType.DOUBLE, floatSet);
+    Set<TSEncoding> textSet = new HashSet<>();
+    textSet.add(TSEncoding.PLAIN);
+    schemaChecker.put(TSDataType.TEXT, textSet);
+  }
+
   private static final Logger logger = 
LoggerFactory.getLogger(SchemaUtils.class);
 
   public static void registerTimeseries(TimeseriesSchema schema) {
@@ -142,4 +170,11 @@ public class SchemaUtils {
             "aggregate does not support " + aggregation + " function.");
     }
   }
+
+  public static void checkDataTypeWithEncoding(TSDataType dataType, TSEncoding 
encoding)
+      throws MetadataException {
+    if(!schemaChecker.get(dataType).contains(encoding)) {
+      throw new MetadataException(String.format("encoding %s does not support 
%s", dataType.toString(), encoding.toString()));
+    }
+  }
 }
diff --git 
a/server/src/test/java/org/apache/iotdb/db/integration/IoTDBSimpleQueryIT.java 
b/server/src/test/java/org/apache/iotdb/db/integration/IoTDBSimpleQueryIT.java
index f345f16..022c37a 100644
--- 
a/server/src/test/java/org/apache/iotdb/db/integration/IoTDBSimpleQueryIT.java
+++ 
b/server/src/test/java/org/apache/iotdb/db/integration/IoTDBSimpleQueryIT.java
@@ -504,4 +504,43 @@ public class IoTDBSimpleQueryIT {
       fail();
     }
   }
+
+
+  @Test
+  public void testInvalidSchema() throws ClassNotFoundException {
+    Class.forName(Config.JDBC_DRIVER_NAME);
+    try (Connection connection = DriverManager
+        .getConnection(Config.IOTDB_URL_PREFIX + "127.0.0.1:6667/",
+            "root", "root");
+        Statement statement = connection.createStatement()) {
+      statement.execute("SET STORAGE GROUP TO root.sg1");
+      try {
+        statement.execute("CREATE TIMESERIES root.sg1.d1.s1 with 
datatype=BOOLEAN, encoding=TS_2DIFF");
+      } catch (Exception e) {
+        Assert.assertEquals("303: 
org.apache.iotdb.db.exception.metadata.MetadataException: encoding BOOLEAN does 
not support TS_2DIFF", e.getMessage());
+      }
+
+      try {
+        statement.execute("CREATE TIMESERIES root.sg1.d1.s2 with 
datatype=INT32, encoding=GORILLA");
+      } catch (Exception e) {
+        Assert.assertEquals("303: 
org.apache.iotdb.db.exception.metadata.MetadataException: encoding INT32 does 
not support GORILLA", e.getMessage());
+      }
+
+      try {
+        statement.execute("CREATE TIMESERIES root.sg1.d1.s3 with 
datatype=DOUBLE, encoding=REGULAR");
+      } catch (Exception e) {
+        Assert.assertEquals("303: 
org.apache.iotdb.db.exception.metadata.MetadataException: encoding DOUBLE does 
not support REGULAR", e.getMessage());
+      }
+
+      try {
+        statement.execute("CREATE TIMESERIES root.sg1.d1.s4 with 
datatype=TEXT, encoding=TS_2DIFF");
+      } catch (Exception e) {
+        Assert.assertEquals("303: 
org.apache.iotdb.db.exception.metadata.MetadataException: encoding TEXT does 
not support TS_2DIFF", e.getMessage());
+      }
+
+
+    } catch (SQLException e) {
+      fail();
+    }
+  }
 }
diff --git 
a/server/src/test/java/org/apache/iotdb/db/metadata/MManagerImproveTest.java 
b/server/src/test/java/org/apache/iotdb/db/metadata/MManagerImproveTest.java
index dc6172f..6fc0867 100644
--- a/server/src/test/java/org/apache/iotdb/db/metadata/MManagerImproveTest.java
+++ b/server/src/test/java/org/apache/iotdb/db/metadata/MManagerImproveTest.java
@@ -55,7 +55,7 @@ public class MManagerImproveTest {
     for (int j = 0; j < DEVICE_NUM; j++) {
       for (int i = 0; i < TIMESERIES_NUM; i++) {
         String p = "root.t1.v2.d" + j + ".s" + i;
-        mManager.createTimeseries(p, TSDataType.TEXT, TSEncoding.RLE,
+        mManager.createTimeseries(p, TSDataType.TEXT, TSEncoding.PLAIN,
             TSFileDescriptor.getInstance().getConfig().getCompressor(), 
Collections.emptyMap());
       }
     }

Reply via email to