You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ka...@apache.org on 2021/01/08 03:42:09 UTC

[iotdb] 01/01: Fix zeppelin bug on Show Storage Group

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

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

commit 69b3ebe5c7a3460ccf4c1149269c118ffd60b14f
Author: kr11 <3095717866.com>
AuthorDate: Fri Jan 8 11:41:39 2021 +0800

    Fix zeppelin bug on Show Storage Group
    
    fix bugs on query statements ignoring time stamps.
    add tests on show and list.
---
 .../apache/zeppelin/iotdb/IoTDBInterpreter.java    | 30 +++++--
 .../zeppelin/iotdb/IoTDBInterpreterTest.java       | 98 ++++++++++++++++++++++
 2 files changed, 120 insertions(+), 8 deletions(-)

diff --git a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/iotdb/IoTDBInterpreter.java b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/iotdb/IoTDBInterpreter.java
index dc1c86c..e5733a7 100644
--- a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/iotdb/IoTDBInterpreter.java
+++ b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/iotdb/IoTDBInterpreter.java
@@ -28,11 +28,13 @@ import java.sql.Statement;
 import java.time.ZoneId;
 import java.util.Arrays;
 import java.util.HashSet;
+import java.util.Optional;
 import java.util.Properties;
 import java.util.Set;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.iotdb.jdbc.Config;
 import org.apache.iotdb.jdbc.IoTDBConnection;
+import org.apache.iotdb.jdbc.IoTDBJDBCResultSet;
 import org.apache.iotdb.rpc.IoTDBConnectionException;
 import org.apache.iotdb.rpc.RpcUtils;
 import org.apache.iotdb.rpc.StatementExecutionException;
@@ -65,6 +67,7 @@ public class IoTDBInterpreter extends AbstractInterpreter {
   static final String DEFAULT_ENABLE_RPC_COMPRESSION = "false";
   static final String DEFAULT_TIME_DISPLAY_TYPE = "long";
   static final String DEFAULT_ZONE_ID = "UTC";
+  static final String NULL_ITEM = "null";
 
   private static final char TAB = '\t';
   private static final char NEWLINE = '\n';
@@ -211,26 +214,33 @@ public class IoTDBInterpreter extends AbstractInterpreter {
       InterpreterResult interpreterResult;
       if (hasResultSet) {
         try (ResultSet resultSet = statement.getResultSet()) {
+          boolean printTimestamp = resultSet instanceof IoTDBJDBCResultSet &&
+              !((IoTDBJDBCResultSet) resultSet).isIgnoreTimeStamp();
           final ResultSetMetaData metaData = resultSet.getMetaData();
           final int columnCount = metaData.getColumnCount();
 
           for (int i = 1; i <= columnCount; i++) {
-            stringBuilder.append(metaData.getColumnLabel(i));
+            stringBuilder.append(metaData.getColumnLabel(i).trim());
             stringBuilder.append(TAB);
           }
-          stringBuilder.deleteCharAt(stringBuilder.length() - 1);
+          deleteLast(stringBuilder);
           stringBuilder.append(NEWLINE);
           while (resultSet.next()) {
-            stringBuilder
-                .append(RpcUtils.formatDatetime(timeFormat, RpcUtils.DEFAULT_TIMESTAMP_PRECISION,
-                    resultSet.getLong(TIMESTAMP_STR), zoneId));
-            for (int i = 2; i <= columnCount; i++) {
+            for (int i = 1; i <= columnCount; i++) {
+              if (printTimestamp && i == 1) {
+                stringBuilder.append(RpcUtils.formatDatetime(timeFormat,
+                    RpcUtils.DEFAULT_TIMESTAMP_PRECISION, resultSet.getLong(TIMESTAMP_STR),
+                    zoneId));
+              } else {
+                stringBuilder
+                    .append(Optional.ofNullable(resultSet.getString(i)).orElse(NULL_ITEM).trim());
+              }
               stringBuilder.append(TAB);
-              stringBuilder.append(resultSet.getString(i));
             }
+            deleteLast(stringBuilder);
             stringBuilder.append(NEWLINE);
           }
-          stringBuilder.deleteCharAt(stringBuilder.length() - 1);
+          deleteLast(stringBuilder);
           interpreterResult = new InterpreterResult(Code.SUCCESS);
           interpreterResult.add(Type.TABLE, stringBuilder.toString());
           return interpreterResult;
@@ -243,6 +253,10 @@ public class IoTDBInterpreter extends AbstractInterpreter {
     }
   }
 
+  private void deleteLast(StringBuilder stringBuilder) {
+    stringBuilder.deleteCharAt(stringBuilder.length() - 1);
+  }
+
   @Override
   public int getProgress(InterpreterContext context) {
     return 0;
diff --git a/zeppelin-interpreter/src/test/java/org/apache/zeppelin/iotdb/IoTDBInterpreterTest.java b/zeppelin-interpreter/src/test/java/org/apache/zeppelin/iotdb/IoTDBInterpreterTest.java
index 94b836f..5826079 100644
--- a/zeppelin-interpreter/src/test/java/org/apache/zeppelin/iotdb/IoTDBInterpreterTest.java
+++ b/zeppelin-interpreter/src/test/java/org/apache/zeppelin/iotdb/IoTDBInterpreterTest.java
@@ -37,6 +37,7 @@ import static org.apache.zeppelin.iotdb.IoTDBInterpreter.SET_TIMESTAMP_DISPLAY;
 
 import java.io.IOException;
 import java.util.Properties;
+import org.apache.iotdb.db.conf.IoTDBConstant;
 import org.apache.iotdb.db.exception.StorageEngineException;
 import org.apache.iotdb.db.utils.EnvironmentUtils;
 import org.apache.zeppelin.interpreter.InterpreterResult;
@@ -69,6 +70,7 @@ public class IoTDBInterpreterTest {
   }
 
   private void initInsert() {
+    interpreter.internalInterpret("set storage group to root.test.wf01", null);
     interpreter.internalInterpret(
         "INSERT INTO root.test.wf01.wt01 (timestamp, temperature, status, hardware) VALUES (1, 1.1, false, 11)",
         null);
@@ -84,6 +86,14 @@ public class IoTDBInterpreterTest {
     interpreter.internalInterpret(
         "INSERT INTO root.test.wf01.wt01 (timestamp, temperature, status, hardware) VALUES (5, 5.5, false, 55)",
         null);
+
+    interpreter.internalInterpret("set storage group to root.test.wf02", null);
+    interpreter.internalInterpret(
+        "INSERT INTO root.test.wf02.wt02 (timestamp, temperature, status, hardware) VALUES (44, 4.4, false, 44)",
+        null);
+    interpreter.internalInterpret(
+        "INSERT INTO root.test.wf02.wt02 (timestamp, temperature, status, hardware) VALUES (54, 5.5, false, 55)",
+        null);
   }
 
   @After
@@ -271,4 +281,92 @@ public class IoTDBInterpreterTest {
     };
     Assert.assertArrayEquals(gt, IoTDBInterpreter.parseMultiLinesSQL(query));
   }
+
+  @Test
+  public void testShowVersion() {
+    InterpreterResult actual = interpreter
+        .internalInterpret("SHOW VERSION", null);
+    String gt = "version\n" + IoTDBConstant.VERSION;
+    Assert.assertNotNull(actual);
+    Assert.assertEquals(Code.SUCCESS, actual.code());
+    Assert.assertEquals(gt, actual.message().get(0).getData());
+  }
+
+  @Test
+  public void testShowTimeseries() {
+    InterpreterResult actual = interpreter
+        .internalInterpret("show timeseries", null);
+    String gt =
+        "timeseries\talias\tstorage group\tdataType\tencoding\tcompression\ttags\tattributes\n"
+            + "root.test.wf02.wt02.temperature\tnull\troot.test.wf02\tFLOAT\tGORILLA\tSNAPPY\tnull\tnull\n"
+            + "root.test.wf02.wt02.status\tnull\troot.test.wf02\tBOOLEAN\tRLE\tSNAPPY\tnull\tnull\n"
+            + "root.test.wf02.wt02.hardware\tnull\troot.test.wf02\tFLOAT\tGORILLA\tSNAPPY\tnull\tnull\n"
+            + "root.test.wf01.wt01.temperature\tnull\troot.test.wf01\tFLOAT\tGORILLA\tSNAPPY\tnull\tnull\n"
+            + "root.test.wf01.wt01.status\tnull\troot.test.wf01\tBOOLEAN\tRLE\tSNAPPY\tnull\tnull\n"
+            + "root.test.wf01.wt01.hardware\tnull\troot.test.wf01\tFLOAT\tGORILLA\tSNAPPY\tnull\tnull";
+    Assert.assertNotNull(actual);
+    Assert.assertEquals(Code.SUCCESS, actual.code());
+    Assert.assertEquals(gt, actual.message().get(0).getData());
+  }
+
+  @Test
+  public void testShowDevices() {
+    InterpreterResult actual = interpreter
+        .internalInterpret("show devices", null);
+    String gt = "devices\n"
+        + "root.test.wf01.wt01\n"
+        + "root.test.wf02.wt02";
+    Assert.assertNotNull(actual);
+    Assert.assertEquals(Code.SUCCESS, actual.code());
+    Assert.assertEquals(gt, actual.message().get(0).getData());
+  }
+
+  @Test
+  public void testShowAllTTL() {
+    interpreter.internalInterpret("SET TTL TO root.test.wf01 12345", null);
+    InterpreterResult actual = interpreter
+        .internalInterpret("SHOW ALL TTL", null);
+    String gt = "storage group\tttl\n"
+        + "root.test.wf02\tnull\n"
+        + "root.test.wf01\t12345";
+    Assert.assertNotNull(actual);
+    Assert.assertEquals(Code.SUCCESS, actual.code());
+    Assert.assertEquals(gt, actual.message().get(0).getData());
+  }
+
+  @Test
+  public void testShowTTL() {
+    interpreter.internalInterpret("SET TTL TO root.test.wf01 12345", null);
+    InterpreterResult actual = interpreter
+        .internalInterpret("SHOW TTL ON root.test.wf01", null);
+    String gt = "storage group\tttl\n"
+        + "root.test.wf01\t12345";
+    Assert.assertNotNull(actual);
+    Assert.assertEquals(Code.SUCCESS, actual.code());
+    Assert.assertEquals(gt, actual.message().get(0).getData());
+  }
+
+  @Test
+  public void testShowStorageGroup() {
+    InterpreterResult actual = interpreter
+        .internalInterpret("SHOW STORAGE GROUP", null);
+    String gt = "storage group\n"
+        + "root.test.wf02\n"
+        + "root.test.wf01";
+    Assert.assertNotNull(actual);
+    Assert.assertEquals(Code.SUCCESS, actual.code());
+    Assert.assertEquals(gt, actual.message().get(0).getData());
+  }
+
+  @Test
+  public void testListUser() {
+    interpreter.internalInterpret("CREATE USER user1 'password1'", null);
+    InterpreterResult actual = interpreter.internalInterpret("LIST USER", null);
+    String gt = "user\n"
+        + "root\n"
+        + "user1";
+    Assert.assertNotNull(actual);
+    Assert.assertEquals(Code.SUCCESS, actual.code());
+    Assert.assertEquals(gt, actual.message().get(0).getData());
+  }
 }
\ No newline at end of file