Skip to content

Commit

Permalink
[SPARK-10934] [SQL] handle hashCode of unsafe array correctly
Browse files Browse the repository at this point in the history
`Murmur3_x86_32.hashUnsafeWords` only accepts word-aligned bytes, but unsafe array is not.

Author: Wenchen Fan <cloud0fan@163.com>

Closes #8987 from cloud-fan/hash.
  • Loading branch information
cloud-fan authored and davies committed Oct 6, 2015
1 parent d323e5e commit c8392cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Expand Up @@ -286,7 +286,11 @@ public MapData getMap(int ordinal) {

@Override
public int hashCode() {
return Murmur3_x86_32.hashUnsafeWords(baseObject, baseOffset, sizeInBytes, 42);
int result = 37;
for (int i = 0; i < sizeInBytes; i++) {
result = 37 * result + Platform.getByte(baseObject, baseOffset + i);
}
return result;
}

@Override
Expand Down
Expand Up @@ -130,4 +130,11 @@ class UnsafeRowSuite extends SparkFunSuite {
assert(emptyRow.getInt(0) === unsafeRow.getInt(0))
assert(emptyRow.getUTF8String(1) === unsafeRow.getUTF8String(1))
}

test("calling hashCode on unsafe array returned by getArray(ordinal)") {
val row = InternalRow.apply(new GenericArrayData(Array(1L)))
val unsafeRow = UnsafeProjection.create(Array[DataType](ArrayType(LongType))).apply(row)
// Makes sure hashCode on unsafe array won't crash
unsafeRow.getArray(0).hashCode()
}
}

0 comments on commit c8392cd

Please sign in to comment.