Skip to content
Snippets Groups Projects
Commit 98be8169 authored by Davies Liu's avatar Davies Liu Committed by Davies Liu
Browse files

[SPARK-11737] [SQL] Fix serialization of UTF8String with Kyro

The default implementation of serialization UTF8String with Kyro may be not correct (BYTE_ARRAY_OFFSET could be different across JVM)

Author: Davies Liu <davies@databricks.com>

Closes #9704 from davies/kyro_string.
parent e33053ee
No related branches found
No related tags found
No related merge requests found
...@@ -36,6 +36,10 @@ ...@@ -36,6 +36,10 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>chill_${scala.binary.version}</artifactId>
</dependency>
<!-- Core dependencies --> <!-- Core dependencies -->
<dependency> <dependency>
......
...@@ -24,6 +24,11 @@ import java.nio.ByteOrder; ...@@ -24,6 +24,11 @@ import java.nio.ByteOrder;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoSerializable;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.apache.spark.unsafe.Platform; import org.apache.spark.unsafe.Platform;
import org.apache.spark.unsafe.array.ByteArrayMethods; import org.apache.spark.unsafe.array.ByteArrayMethods;
...@@ -38,9 +43,9 @@ import static org.apache.spark.unsafe.Platform.*; ...@@ -38,9 +43,9 @@ import static org.apache.spark.unsafe.Platform.*;
* <p> * <p>
* Note: This is not designed for general use cases, should not be used outside SQL. * Note: This is not designed for general use cases, should not be used outside SQL.
*/ */
public final class UTF8String implements Comparable<UTF8String>, Externalizable { public final class UTF8String implements Comparable<UTF8String>, Externalizable, KryoSerializable {
// These are only updated by readExternal() // These are only updated by readExternal() or read()
@Nonnull @Nonnull
private Object base; private Object base;
private long offset; private long offset;
...@@ -1003,4 +1008,19 @@ public final class UTF8String implements Comparable<UTF8String>, Externalizable ...@@ -1003,4 +1008,19 @@ public final class UTF8String implements Comparable<UTF8String>, Externalizable
in.readFully((byte[]) base); in.readFully((byte[]) base);
} }
@Override
public void write(Kryo kryo, Output out) {
byte[] bytes = getBytes();
out.writeInt(bytes.length);
out.write(bytes);
}
@Override
public void read(Kryo kryo, Input in) {
this.offset = BYTE_ARRAY_OFFSET;
this.numBytes = in.readInt();
this.base = new byte[numBytes];
in.read((byte[]) base);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment