Aggarwal-Raghav commented on code in PR #6973:
URL: https://github.com/apache/hive/pull/6973#discussion_r4670418338


##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/mapreduce/IcebergSplit.java:
##########
@@ -80,15 +84,79 @@ public String[] getLocations() {
 
   @Override
   public void write(DataOutput out) throws IOException {
-    byte[] data = SerializationUtil.serializeToBytes(this.taskGroup);
-    out.writeInt(data.length);
-    out.write(data);
+    String codecName = conf != null ?
+        HiveConf.getVar(conf, 
HiveConf.ConfVars.HIVE_ICEBERG_SPLIT_COMPRESSION_CODEC) :
+        
HiveConf.ConfVars.HIVE_ICEBERG_SPLIT_COMPRESSION_CODEC.getDefaultValue();
+    Codec codec = Codec.valueOf(codecName.toUpperCase(Locale.ROOT));
+    byte[] stored = 
codec.compress(SerializationUtil.serializeToBytes(this.taskGroup));
+    out.writeByte(codec.id);
+    out.writeInt(stored.length);
+    out.write(stored);
   }
 
   @Override
   public void readFields(DataInput in) throws IOException {
-    byte[] data = new byte[in.readInt()];
-    in.readFully(data);
-    this.taskGroup = SerializationUtil.deserializeFromBytes(data);
+    Codec codec = Codec.of(in.readByte());
+    byte[] stored = new byte[in.readInt()];
+    in.readFully(stored);
+    this.taskGroup = 
SerializationUtil.deserializeFromBytes(codec.decompress(stored));
+  }
+
+  // The payload is the codec id, the length of the stored bytes and the 
stored bytes. Snappy and zstd keep the length
+  // of the serialized task group in their own headers.
+  private enum Codec {
+    NONE(0) {
+      @Override
+      byte[] compress(byte[] data) {
+        return data;
+      }
+
+      @Override
+      byte[] decompress(byte[] stored) {
+        return stored;
+      }
+    },
+    SNAPPY(1) {
+      @Override
+      byte[] compress(byte[] data) throws IOException {
+        return Snappy.compress(data);
+      }
+
+      @Override
+      byte[] decompress(byte[] stored) throws IOException {
+        return Snappy.uncompress(stored);
+      }
+    },
+    ZSTD(2) {
+      @Override
+      byte[] compress(byte[] data) {
+        // the fastest level: a payload is a few KB, the ratio of a higher 
level does not pay for its time
+        return Zstd.compress(data, 1);
+      }
+
+      @Override
+      byte[] decompress(byte[] stored) {
+        return Zstd.decompress(stored, (int) Zstd.getFrameContentSize(stored));

Review Comment:
   ` Zstd.getFrameContentSize(stored));` can return negative as well as per its 
javadoc:
   ```
   /**
        * Return the original size of a compressed buffer (if known)
        *
        * @param src the compressed buffer
        * @return the number of bytes of the original buffer
        *         0 if the original size is not known,
        *         negative if there is an error decoding the frame header
        */
   ```
   
   and returnign -ve in decompress can throw exception
   ```
   public byte[] decompress(byte[] src, int srcOffset, int srcSize, int 
originalSize) throws ZstdException {
           if (originalSize < 0) {
               throw new ZstdException(Zstd.errGeneric(), "Original size should 
not be negative");
           }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to