Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-21312][SQL] correct offsetInBytes in UnsafeRow.writeToStream #18535

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public void copyFrom(UnsafeRow row) {
*/
public void writeToStream(OutputStream out, byte[] writeBuffer) throws IOException {
if (baseObject instanceof byte[]) {
int offsetInByteArray = (int) (Platform.BYTE_ARRAY_OFFSET - baseOffset);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmmm, so the writeToStream never worked before?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see, previously it only works if baseOffset = Platform.BYTE_ARRAY_OFFSET

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Was a bit surprising that the non-zero offset case is not being hit in Spark otherwise. Seen when ingesting from parquet, for example, into a data source that has custom partitioning leading to a shuffle.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I understood why @sumwale added a new test with non-zero offset.

int offsetInByteArray = (int) (baseOffset - Platform.BYTE_ARRAY_OFFSET);
out.write((byte[]) baseObject, offsetInByteArray, sizeInBytes);
} else {
int dataRemaining = sizeInBytes;
Expand Down
13 changes: 13 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/UnsafeRowSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,22 @@ class UnsafeRowSuite extends SparkFunSuite {
MemoryAllocator.UNSAFE.free(offheapRowPage)
}
}
val (bytesFromArrayBackedRowWithOffset, field0StringFromArrayBackedRowWithOffset) = {
val baos = new ByteArrayOutputStream()
val numBytes = arrayBackedUnsafeRow.getSizeInBytes
val bytesWithOffset = new Array[Byte](numBytes + 100)
System.arraycopy(arrayBackedUnsafeRow.getBaseObject.asInstanceOf[Array[Byte]], 0,
bytesWithOffset, 100, numBytes)
val arrayBackedRow = new UnsafeRow(arrayBackedUnsafeRow.numFields())
arrayBackedRow.pointTo(bytesWithOffset, Platform.BYTE_ARRAY_OFFSET + 100, numBytes)
arrayBackedRow.writeToStream(baos, null)
(baos.toByteArray, arrayBackedRow.getString(0))
}

assert(bytesFromArrayBackedRow === bytesFromOffheapRow)
assert(field0StringFromArrayBackedRow === field0StringFromOffheapRow)
assert(bytesFromArrayBackedRow === bytesFromArrayBackedRowWithOffset)
assert(field0StringFromArrayBackedRow === field0StringFromArrayBackedRowWithOffset)
}

test("calling getDouble() and getFloat() on null columns") {
Expand Down