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 2022/07/12 02:09:10 UTC

[iotdb] branch master updated: [IOTDB-3788] insertRecord support insert Binary as TEXT DataType (#6634)

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 a944f97b0c [IOTDB-3788] insertRecord support insert Binary as TEXT DataType (#6634)
a944f97b0c is described below

commit a944f97b0c678bd7640bd47477c6116e57d6a741
Author: Chen YZ <43...@users.noreply.github.com>
AuthorDate: Tue Jul 12 10:09:06 2022 +0800

    [IOTDB-3788] insertRecord support insert Binary as TEXT DataType (#6634)
---
 .../apache/iotdb/session/IoTDBSessionSimpleIT.java | 57 ++++++++++++++++++++++
 .../apache/iotdb/session/util/SessionUtils.java    | 13 ++++-
 .../apache/iotdb/tsfile/write/record/Tablet.java   |  8 +--
 3 files changed, 73 insertions(+), 5 deletions(-)

diff --git a/integration/src/test/java/org/apache/iotdb/session/IoTDBSessionSimpleIT.java b/integration/src/test/java/org/apache/iotdb/session/IoTDBSessionSimpleIT.java
index 5b7f41ad32..7fc0f42996 100644
--- a/integration/src/test/java/org/apache/iotdb/session/IoTDBSessionSimpleIT.java
+++ b/integration/src/test/java/org/apache/iotdb/session/IoTDBSessionSimpleIT.java
@@ -60,6 +60,7 @@ import java.io.IOException;
 import java.security.SecureRandom;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -1460,6 +1461,62 @@ public class IoTDBSessionSimpleIT {
     session.close();
   }
 
+  @Test
+  public void testInsertBinaryAsText()
+      throws IoTDBConnectionException, StatementExecutionException {
+    session = new Session("127.0.0.1", 6667, "root", "root");
+    session.open();
+    // prepare binary data
+    List<Object> bytesData = new ArrayList<>();
+    for (int i = 0; i < 3; i++) {
+      byte[] bytes = new byte[128];
+      for (int j = 0; j < 128; j++) {
+        bytes[j] = Byte.valueOf("" + (j - i), 10);
+      }
+      bytesData.add(bytes);
+    }
+    // insert data using insertRecord
+    for (int i = 0; i < bytesData.size(); i++) {
+      byte[] data = (byte[]) bytesData.get(i);
+      Binary dataBinary = new Binary(data);
+      session.insertRecord(
+          "root.sg1.d1",
+          i,
+          Collections.singletonList("s0"),
+          Collections.singletonList(TSDataType.TEXT),
+          dataBinary);
+    }
+    // insert data using insertTablet
+    List<MeasurementSchema> schemaList = new ArrayList<>();
+    schemaList.add(new MeasurementSchema("s1", TSDataType.TEXT));
+    Tablet tablet = new Tablet("root.sg1.d1", schemaList, 100);
+    for (int i = 0; i < bytesData.size(); i++) {
+      byte[] data = (byte[]) bytesData.get(i);
+      int rowIndex = tablet.rowSize++;
+      tablet.addTimestamp(rowIndex, i);
+      Binary dataBinary = new Binary(data);
+      tablet.addValue(schemaList.get(0).getMeasurementId(), rowIndex, dataBinary);
+    }
+    session.insertTablet(tablet);
+    // check result
+    SessionDataSet dataSet = session.executeQueryStatement("select ** from root.sg1.d1");
+    Assert.assertArrayEquals(
+        dataSet.getColumnNames().toArray(new String[0]),
+        new String[] {"Time", "root.sg1.d1.s0", "root.sg1.d1.s1"});
+    while (dataSet.hasNext()) {
+      RowRecord rowRecord = dataSet.next();
+      for (int i = 0; i < 2; i++) {
+        // get Binary value from SessionDataSet
+        byte[] actualBytes = rowRecord.getFields().get(i).getBinaryV().getValues();
+        // compare Binary value to origin bytesData
+        byte[] expectedBytes = (byte[]) bytesData.get((int) rowRecord.getTimestamp());
+        Assert.assertArrayEquals(expectedBytes, actualBytes);
+      }
+    }
+    dataSet.closeOperationHandle();
+    session.close();
+  }
+
   private void initTreeTemplate(String path)
       throws IoTDBConnectionException, StatementExecutionException, IOException {
     Template sessionTemplate = new Template("treeTemplate", true);
diff --git a/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java b/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java
index e022dc1e71..32ed19add9 100644
--- a/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java
+++ b/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java
@@ -107,7 +107,11 @@ public class SessionUtils {
           break;
         case TEXT:
           res += Integer.BYTES;
-          res += ((String) values.get(i)).getBytes(TSFileConfig.STRING_CHARSET).length;
+          if (values.get(i) instanceof Binary) {
+            res += ((Binary) values.get(i)).getValues().length;
+          } else {
+            res += ((String) values.get(i)).getBytes(TSFileConfig.STRING_CHARSET).length;
+          }
           break;
         default:
           throw new IoTDBConnectionException(MSG_UNSUPPORTED_DATA_TYPE + types.get(i));
@@ -149,7 +153,12 @@ public class SessionUtils {
           ReadWriteIOUtils.write((Double) values.get(i), buffer);
           break;
         case TEXT:
-          byte[] bytes = ((String) values.get(i)).getBytes(TSFileConfig.STRING_CHARSET);
+          byte[] bytes;
+          if (values.get(i) instanceof Binary) {
+            bytes = ((Binary) values.get(i)).getValues();
+          } else {
+            bytes = ((String) values.get(i)).getBytes(TSFileConfig.STRING_CHARSET);
+          }
           ReadWriteIOUtils.write(bytes.length, buffer);
           buffer.put(bytes);
           break;
diff --git a/tsfile/src/main/java/org/apache/iotdb/tsfile/write/record/Tablet.java b/tsfile/src/main/java/org/apache/iotdb/tsfile/write/record/Tablet.java
index f02acb9089..bb66da22a9 100644
--- a/tsfile/src/main/java/org/apache/iotdb/tsfile/write/record/Tablet.java
+++ b/tsfile/src/main/java/org/apache/iotdb/tsfile/write/record/Tablet.java
@@ -145,9 +145,11 @@ public class Tablet {
       case TEXT:
         {
           Binary[] sensor = (Binary[]) values[indexOfSchema];
-          if (value instanceof Binary)
-            sensor[rowIndex] = value != null ? (Binary) value : Binary.EMPTY_VALUE;
-          else sensor[rowIndex] = value != null ? new Binary((String) value) : Binary.EMPTY_VALUE;
+          if (value instanceof Binary) {
+            sensor[rowIndex] = (Binary) value;
+          } else {
+            sensor[rowIndex] = value != null ? new Binary((String) value) : Binary.EMPTY_VALUE;
+          }
           break;
         }
       case FLOAT: