|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql |
| 19 | + |
| 20 | +import java.io.ByteArrayOutputStream |
| 21 | + |
| 22 | +import org.apache.spark.SparkFunSuite |
| 23 | +import org.apache.spark.sql.catalyst.InternalRow |
| 24 | +import org.apache.spark.sql.catalyst.expressions.{UnsafeRow, UnsafeProjection} |
| 25 | +import org.apache.spark.sql.types.{IntegerType, StringType} |
| 26 | +import org.apache.spark.unsafe.PlatformDependent |
| 27 | +import org.apache.spark.unsafe.memory.MemoryAllocator |
| 28 | +import org.apache.spark.unsafe.types.UTF8String |
| 29 | + |
| 30 | +class UnsafeRowSuite extends SparkFunSuite { |
| 31 | + test("writeToStream") { |
| 32 | + val row = InternalRow.apply(UTF8String.fromString("hello"), UTF8String.fromString("world"), 123) |
| 33 | + val arrayBackedUnsafeRow: UnsafeRow = |
| 34 | + UnsafeProjection.create(Seq(StringType, StringType, IntegerType)).apply(row) |
| 35 | + assert(arrayBackedUnsafeRow.getBaseObject.isInstanceOf[Array[Byte]]) |
| 36 | + val bytesFromArrayBackedRow: Array[Byte] = { |
| 37 | + val baos = new ByteArrayOutputStream() |
| 38 | + arrayBackedUnsafeRow.writeToStream(baos, null) |
| 39 | + baos.toByteArray |
| 40 | + } |
| 41 | + val bytesFromOffheapRow: Array[Byte] = { |
| 42 | + val offheapRowPage = MemoryAllocator.UNSAFE.allocate(arrayBackedUnsafeRow.getSizeInBytes) |
| 43 | + try { |
| 44 | + PlatformDependent.copyMemory( |
| 45 | + arrayBackedUnsafeRow.getBaseObject, |
| 46 | + arrayBackedUnsafeRow.getBaseOffset, |
| 47 | + offheapRowPage.getBaseObject, |
| 48 | + offheapRowPage.getBaseOffset, |
| 49 | + arrayBackedUnsafeRow.getSizeInBytes |
| 50 | + ) |
| 51 | + val offheapUnsafeRow: UnsafeRow = new UnsafeRow() |
| 52 | + offheapUnsafeRow.pointTo( |
| 53 | + offheapRowPage.getBaseObject, |
| 54 | + offheapRowPage.getBaseOffset, |
| 55 | + 3, // num fields |
| 56 | + arrayBackedUnsafeRow.getSizeInBytes, |
| 57 | + null // object pool |
| 58 | + ) |
| 59 | + assert(offheapUnsafeRow.getBaseObject === null) |
| 60 | + val baos = new ByteArrayOutputStream() |
| 61 | + val writeBuffer = new Array[Byte](1024) |
| 62 | + offheapUnsafeRow.writeToStream(baos, writeBuffer) |
| 63 | + baos.toByteArray |
| 64 | + } finally { |
| 65 | + MemoryAllocator.UNSAFE.free(offheapRowPage) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + assert(bytesFromArrayBackedRow === bytesFromOffheapRow) |
| 70 | + } |
| 71 | +} |
0 commit comments