-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-9023] [SQL] Followup for #7456 (Efficiency improvements for Un…
…safeRows in Exchange) This patch addresses code review feedback from #7456. Author: Josh Rosen <[email protected]> Closes #7551 from JoshRosen/unsafe-exchange-followup and squashes the following commits: 76dbdf8 [Josh Rosen] Add comments + more methods to UnsafeRowSerializer 3d7a1f2 [Josh Rosen] Add writeToStream() method to UnsafeRow
- Loading branch information
Showing
3 changed files
with
161 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
sql/core/src/test/scala/org/apache/spark/sql/UnsafeRowSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql | ||
|
||
import java.io.ByteArrayOutputStream | ||
|
||
import org.apache.spark.SparkFunSuite | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.{UnsafeRow, UnsafeProjection} | ||
import org.apache.spark.sql.types.{IntegerType, StringType} | ||
import org.apache.spark.unsafe.PlatformDependent | ||
import org.apache.spark.unsafe.memory.MemoryAllocator | ||
import org.apache.spark.unsafe.types.UTF8String | ||
|
||
class UnsafeRowSuite extends SparkFunSuite { | ||
test("writeToStream") { | ||
val row = InternalRow.apply(UTF8String.fromString("hello"), UTF8String.fromString("world"), 123) | ||
val arrayBackedUnsafeRow: UnsafeRow = | ||
UnsafeProjection.create(Seq(StringType, StringType, IntegerType)).apply(row) | ||
assert(arrayBackedUnsafeRow.getBaseObject.isInstanceOf[Array[Byte]]) | ||
val bytesFromArrayBackedRow: Array[Byte] = { | ||
val baos = new ByteArrayOutputStream() | ||
arrayBackedUnsafeRow.writeToStream(baos, null) | ||
baos.toByteArray | ||
} | ||
val bytesFromOffheapRow: Array[Byte] = { | ||
val offheapRowPage = MemoryAllocator.UNSAFE.allocate(arrayBackedUnsafeRow.getSizeInBytes) | ||
try { | ||
PlatformDependent.copyMemory( | ||
arrayBackedUnsafeRow.getBaseObject, | ||
arrayBackedUnsafeRow.getBaseOffset, | ||
offheapRowPage.getBaseObject, | ||
offheapRowPage.getBaseOffset, | ||
arrayBackedUnsafeRow.getSizeInBytes | ||
) | ||
val offheapUnsafeRow: UnsafeRow = new UnsafeRow() | ||
offheapUnsafeRow.pointTo( | ||
offheapRowPage.getBaseObject, | ||
offheapRowPage.getBaseOffset, | ||
3, // num fields | ||
arrayBackedUnsafeRow.getSizeInBytes, | ||
null // object pool | ||
) | ||
assert(offheapUnsafeRow.getBaseObject === null) | ||
val baos = new ByteArrayOutputStream() | ||
val writeBuffer = new Array[Byte](1024) | ||
offheapUnsafeRow.writeToStream(baos, writeBuffer) | ||
baos.toByteArray | ||
} finally { | ||
MemoryAllocator.UNSAFE.free(offheapRowPage) | ||
} | ||
} | ||
|
||
assert(bytesFromArrayBackedRow === bytesFromOffheapRow) | ||
} | ||
} |