Skip to content

Commit

Permalink
[SPARK-11737] [SQL] Fix serialization of UTF8String with Kyro
Browse files Browse the repository at this point in the history
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.

(cherry picked from commit 98be816)
Signed-off-by: Davies Liu <davies.liu@gmail.com>
  • Loading branch information
Davies Liu authored and davies committed Nov 18, 2015
1 parent f33e277 commit f7a7230
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions unsafe/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
</properties>

<dependencies>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>chill_${scala.binary.version}</artifactId>
</dependency>

<!-- Core dependencies -->
<dependency>
Expand Down
24 changes: 22 additions & 2 deletions unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
import java.util.Arrays;
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.array.ByteArrayMethods;

Expand All @@ -37,9 +42,9 @@
* <p>
* 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
private Object base;
private long offset;
Expand Down Expand Up @@ -997,4 +1002,19 @@ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundExcept
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 comments on commit f7a7230

Please sign in to comment.