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/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 0b95436  [IOTDB-1091] add illegal parameter exception for sdt when 
creating timeseries (#2543)
0b95436 is described below

commit 0b9543677722f63ec954612df9d32b9af594ccff
Author: Haimei Guo <[email protected]>
AuthorDate: Thu Jan 28 10:56:45 2021 +0800

    [IOTDB-1091] add illegal parameter exception for sdt when creating 
timeseries (#2543)
---
 .../org/apache/iotdb/db/conf/IoTDBConstant.java    |  7 +++
 .../metadata/IllegalParameterOfPathException.java  | 30 +++++++++++
 .../java/org/apache/iotdb/db/metadata/MTree.java   | 58 +++++++++++++++++++++-
 .../iotdb/db/integration/IoTDBSimpleQueryIT.java   | 29 +++++++++++
 .../java/org/apache/iotdb/rpc/TSStatusCode.java    |  1 +
 .../iotdb/tsfile/write/chunk/ChunkWriterImpl.java  | 51 ++++++-------------
 6 files changed, 139 insertions(+), 37 deletions(-)

diff --git a/server/src/main/java/org/apache/iotdb/db/conf/IoTDBConstant.java 
b/server/src/main/java/org/apache/iotdb/db/conf/IoTDBConstant.java
index aac1541..5ee5054 100644
--- a/server/src/main/java/org/apache/iotdb/db/conf/IoTDBConstant.java
+++ b/server/src/main/java/org/apache/iotdb/db/conf/IoTDBConstant.java
@@ -102,6 +102,13 @@ public class IoTDBConstant {
   public static final String PATH_WILDCARD = "*";
   public static final String TIME = "time";
 
+  //sdt parameters
+  public static final String LOSS = "loss";
+  public static final String SDT = "sdt";
+  public static final String SDT_COMP_DEV = "compdev";
+  public static final String SDT_COMP_MIN_TIME = "compmintime";
+  public static final String SDT_COMP_MAX_TIME = "compmaxtime";
+
   // data folder name
   public static final String SEQUENCE_FLODER_NAME = "sequence";
   public static final String UNSEQUENCE_FLODER_NAME = "unsequence";
diff --git 
a/server/src/main/java/org/apache/iotdb/db/exception/metadata/IllegalParameterOfPathException.java
 
b/server/src/main/java/org/apache/iotdb/db/exception/metadata/IllegalParameterOfPathException.java
new file mode 100644
index 0000000..9eaa66d
--- /dev/null
+++ 
b/server/src/main/java/org/apache/iotdb/db/exception/metadata/IllegalParameterOfPathException.java
@@ -0,0 +1,30 @@
+/*
+ * 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.exception.metadata;
+
+import org.apache.iotdb.rpc.TSStatusCode;
+
+public class IllegalParameterOfPathException extends MetadataException {
+
+  public IllegalParameterOfPathException(String msg, String path) {
+    super(String.format("%s. Failed to create timeseries for path %s", msg, 
path));
+    errorCode = TSStatusCode.ILLEGAL_PARAMETER.getStatusCode();
+    this.isUserException = true;
+  }
+}
diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/MTree.java 
b/server/src/main/java/org/apache/iotdb/db/metadata/MTree.java
index c204459..0b549f5 100644
--- a/server/src/main/java/org/apache/iotdb/db/metadata/MTree.java
+++ b/server/src/main/java/org/apache/iotdb/db/metadata/MTree.java
@@ -21,6 +21,11 @@ package org.apache.iotdb.db.metadata;
 import static java.util.stream.Collectors.toList;
 import static org.apache.iotdb.db.conf.IoTDBConstant.PATH_SEPARATOR;
 import static org.apache.iotdb.db.conf.IoTDBConstant.PATH_WILDCARD;
+import static org.apache.iotdb.db.conf.IoTDBConstant.LOSS;
+import static org.apache.iotdb.db.conf.IoTDBConstant.SDT;
+import static org.apache.iotdb.db.conf.IoTDBConstant.SDT_COMP_DEV;
+import static org.apache.iotdb.db.conf.IoTDBConstant.SDT_COMP_MAX_TIME;
+import static org.apache.iotdb.db.conf.IoTDBConstant.SDT_COMP_MIN_TIME;
 
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
@@ -50,6 +55,7 @@ import org.apache.iotdb.db.conf.IoTDBConstant;
 import org.apache.iotdb.db.conf.IoTDBDescriptor;
 import org.apache.iotdb.db.engine.querycontext.QueryDataSource;
 import org.apache.iotdb.db.exception.metadata.AliasAlreadyExistException;
+import org.apache.iotdb.db.exception.metadata.IllegalParameterOfPathException;
 import org.apache.iotdb.db.exception.metadata.IllegalPathException;
 import org.apache.iotdb.db.exception.metadata.MetadataException;
 import org.apache.iotdb.db.exception.metadata.PathAlreadyExistException;
@@ -190,8 +196,7 @@ public class MTree implements Serializable {
    * @param alias      alias of measurement
    */
   MeasurementMNode createTimeseries(PartialPath path, TSDataType dataType, 
TSEncoding encoding,
-      CompressionType compressor, Map<String, String> props, String alias)
-      throws MetadataException {
+      CompressionType compressor, Map<String, String> props, String alias) 
throws MetadataException {
     String[] nodeNames = path.getNodes();
     if (nodeNames.length <= 2 || !nodeNames[0].equals(root.getName())) {
       throw new IllegalPathException(path.getFullPath());
@@ -212,6 +217,11 @@ public class MTree implements Serializable {
       }
       cur = cur.getChild(nodeName);
     }
+
+    if (props != null && props.containsKey(LOSS) && 
props.get(LOSS).equals(SDT)) {
+      checkSDTFormat(path.getFullPath(), props);
+    }
+
     String leafName = nodeNames[nodeNames.length - 1];
 
     // synchronize check and add, we need addChild and add Alias become atomic 
operation
@@ -237,6 +247,50 @@ public class MTree implements Serializable {
     }
   }
 
+  //check if sdt parameters are valid
+  private void checkSDTFormat(String path, Map<String, String> props) throws 
IllegalParameterOfPathException {
+    if (!props.containsKey(SDT_COMP_DEV)) {
+      throw new IllegalParameterOfPathException("SDT compression deviation is 
required", path);
+    }
+
+    try {
+      double d = Double.parseDouble(props.get(SDT_COMP_DEV));
+      if (d < 0) {
+        throw new IllegalParameterOfPathException("SDT compression deviation 
cannot be negative", path);
+      }
+    } catch (NumberFormatException e) {
+      throw new IllegalParameterOfPathException("SDT compression deviation 
formatting error", path);
+    }
+
+    long compMinTime = sdtCompressionTimeFormat(SDT_COMP_MIN_TIME, props, 
path);
+    long compMaxTime = sdtCompressionTimeFormat(SDT_COMP_MAX_TIME, props, 
path);
+
+    if (compMaxTime <= compMinTime) {
+      throw new IllegalParameterOfPathException(
+          "SDT compression maximum time needs to be greater than compression 
minimum time", path);
+    }
+  }
+
+  private long sdtCompressionTimeFormat(String compTime, Map<String, String> 
props, String path)
+      throws IllegalParameterOfPathException {
+    boolean isCompMaxTime = compTime.equals(SDT_COMP_MAX_TIME);
+    long time = isCompMaxTime ? Long.MAX_VALUE : 0;
+    String s = isCompMaxTime ? "maximum" : "minimum";
+    if (props.containsKey(compTime)) {
+      try {
+        time = Long.parseLong(props.get(compTime));
+        if (time < 0) {
+          throw new IllegalParameterOfPathException(String.format("SDT 
compression %s time cannot be negative", s), path);
+        }
+      } catch (IllegalParameterOfPathException e) {
+        throw new IllegalParameterOfPathException(String.format("SDT 
compression %s time formatting error", s), path);
+      }
+    } else {
+      logger.info("{} enabled SDT but did not set compression {} time", path, 
s);
+    }
+    return time;
+  }
+
   /**
    * Add an interval path to MTree. This is only used for automatically 
creating schema
    *
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 b9c5661..712f525 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
@@ -121,6 +121,35 @@ public class IoTDBSimpleQueryIT {
   }
 
   @Test
+  public void testFailedToCreateTimeseriesSDTProperties() 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.setFetchSize(5);
+      statement.execute("SET STORAGE GROUP TO root.sg1");
+      try {
+        statement.execute(
+            "CREATE TIMESERIES root.sg1.d0.s1 WITH 
DATATYPE=INT32,ENCODING=PLAIN,LOSS=SDT,COMPDEV=-2");
+      } catch (Exception e) {
+        assertEquals("318: SDT compression deviation cannot be negative. 
Failed to create timeseries for path root.sg1.d0.s1", e.getMessage());
+      }
+
+      int count = 0;
+      try (ResultSet resultSet = statement.executeQuery("show timeseries")) {
+        while (resultSet.next()) {
+          count++;
+        }
+      }
+      assertEquals(0, count);
+
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+  }
+
+  @Test
   public void testSDTEncodingSeq() throws ClassNotFoundException {
     Class.forName(Config.JDBC_DRIVER_NAME);
 
diff --git a/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java 
b/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java
index a6f1ee5..24913e8 100644
--- a/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java
+++ b/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java
@@ -45,6 +45,7 @@ public enum TSStatusCode {
   PATH_ILLEGAL(315),
   LOAD_FILE_ERROR(316),
   STORAGE_GROUP_NOT_READY(317),
+  ILLEGAL_PARAMETER(318),
 
   EXECUTE_STATEMENT_ERROR(400),
   SQL_PARSE_ERROR(401),
diff --git 
a/tsfile/src/main/java/org/apache/iotdb/tsfile/write/chunk/ChunkWriterImpl.java 
b/tsfile/src/main/java/org/apache/iotdb/tsfile/write/chunk/ChunkWriterImpl.java
index 9a705f3..46b1fa4 100644
--- 
a/tsfile/src/main/java/org/apache/iotdb/tsfile/write/chunk/ChunkWriterImpl.java
+++ 
b/tsfile/src/main/java/org/apache/iotdb/tsfile/write/chunk/ChunkWriterImpl.java
@@ -79,10 +79,16 @@ public class ChunkWriterImpl implements IChunkWriter {
    */
   private Statistics<?> statistics;
 
-  private boolean isSdtEncoding;
-
+  /**
+   * SDT parameters
+   */
   private SDTEncoder sdtEncoder;
-
+  private boolean isSdtEncoding;
+  private static final String LOSS = "loss";
+  private static final String SDT = "sdt";
+  private static final String SDT_COMP_DEV = "compdev";
+  private static final String SDT_COMP_MIN_TIME = "compmintime";
+  private static final String SDT_COMP_MAX_TIME = "compmaxtime";
   /**
    * do not re-execute SDT compression when merging chunks
    */
@@ -127,46 +133,21 @@ public class ChunkWriterImpl implements IChunkWriter {
 
   private void checkSdtEncoding() {
     if (measurementSchema.getProps() != null && !isMerging) {
-      if (measurementSchema.getProps().getOrDefault("loss", "").equals("sdt")) 
{
+      if (measurementSchema.getProps().getOrDefault(LOSS, "").equals(SDT)) {
         isSdtEncoding = true;
         sdtEncoder = new SDTEncoder();
       }
 
-      if (isSdtEncoding && 
measurementSchema.getProps().containsKey("compdev")) {
-        try {
-          sdtEncoder
-              
.setCompDeviation(Double.parseDouble(measurementSchema.getProps().get("compdev")));
-        } catch (NumberFormatException e) {
-          logger.error("meet error when formatting SDT compression deviation");
-        }
-        if (sdtEncoder.getCompDeviation() < 0) {
-          logger
-              .error("SDT compression deviation cannot be negative. SDT 
encoding is turned off.");
-          isSdtEncoding = false;
-        }
-      }
-
-      if (isSdtEncoding && 
measurementSchema.getProps().containsKey("compmintime")) {
-        try {
-          
sdtEncoder.setCompMinTime(Long.parseLong(measurementSchema.getProps().get("compmintime")));
-        } catch (NumberFormatException e) {
-          logger.error("meet error when formatting SDT compression minimum");
-        }
+      if (isSdtEncoding && 
measurementSchema.getProps().containsKey(SDT_COMP_DEV)) {
+        
sdtEncoder.setCompDeviation(Double.parseDouble(measurementSchema.getProps().get(SDT_COMP_DEV)));
       }
 
-      if (isSdtEncoding && 
measurementSchema.getProps().containsKey("compmaxtime")) {
-        try {
-          
sdtEncoder.setCompMaxTime(Long.parseLong(measurementSchema.getProps().get("compmaxtime")));
-        } catch (NumberFormatException e) {
-          logger.error("meet error when formatting SDT compression maximum");
-        }
+      if (isSdtEncoding && 
measurementSchema.getProps().containsKey(SDT_COMP_MIN_TIME)) {
+        
sdtEncoder.setCompMinTime(Long.parseLong(measurementSchema.getProps().get(SDT_COMP_MIN_TIME)));
       }
 
-      if (isSdtEncoding && sdtEncoder.getCompMaxTime() <= 
sdtEncoder.getCompMinTime()) {
-        logger
-            .error(
-                "SDT compression maximum needs to be greater than compression 
minimum. SDT encoding is turned off");
-        isSdtEncoding = false;
+      if (isSdtEncoding && 
measurementSchema.getProps().containsKey(SDT_COMP_MAX_TIME)) {
+        
sdtEncoder.setCompMaxTime(Long.parseLong(measurementSchema.getProps().get(SDT_COMP_MAX_TIME)));
       }
     }
   }

Reply via email to