Skip to content

Commit

Permalink
[SPARK-9421] Fix null-handling bugs in UnsafeRow.getDouble, getFloat(…
Browse files Browse the repository at this point in the history
…), and get(ordinal, dataType)

UnsafeRow.getDouble and getFloat() return NaN when called on columns that are null, which is inconsistent with the behavior of other row classes (which is to return 0.0).

In addition, the generic get(ordinal, dataType) method should always return null for a null literal, but currently it handles nulls by calling the type-specific accessors.

This patch addresses both of these issues and adds a regression test.

Author: Josh Rosen <[email protected]>

Closes #7736 from JoshRosen/unsafe-row-null-fixes and squashes the following commits:

c8eb2ee [Josh Rosen] Fix test in UnsafeRowConverterSuite
6214682 [Josh Rosen] Fixes to null handling in UnsafeRow
  • Loading branch information
JoshRosen committed Jul 29, 2015
1 parent 6662ee2 commit e78ec1a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public Object get(int ordinal) {

@Override
public Object get(int ordinal, DataType dataType) {
if (dataType instanceof NullType) {
if (isNullAt(ordinal) || dataType instanceof NullType) {
return null;
} else if (dataType instanceof BooleanType) {
return getBoolean(ordinal);
Expand Down Expand Up @@ -313,21 +313,13 @@ public long getLong(int ordinal) {
@Override
public float getFloat(int ordinal) {
assertIndexIsValid(ordinal);
if (isNullAt(ordinal)) {
return Float.NaN;
} else {
return PlatformDependent.UNSAFE.getFloat(baseObject, getFieldOffset(ordinal));
}
return PlatformDependent.UNSAFE.getFloat(baseObject, getFieldOffset(ordinal));
}

@Override
public double getDouble(int ordinal) {
assertIndexIsValid(ordinal);
if (isNullAt(ordinal)) {
return Float.NaN;
} else {
return PlatformDependent.UNSAFE.getDouble(baseObject, getFieldOffset(ordinal));
}
return PlatformDependent.UNSAFE.getDouble(baseObject, getFieldOffset(ordinal));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ class UnsafeRowConverterSuite extends SparkFunSuite with Matchers {
assert(createdFromNull.getShort(3) === 0)
assert(createdFromNull.getInt(4) === 0)
assert(createdFromNull.getLong(5) === 0)
assert(java.lang.Float.isNaN(createdFromNull.getFloat(6)))
assert(java.lang.Double.isNaN(createdFromNull.getDouble(7)))
assert(createdFromNull.getFloat(6) === 0.0f)
assert(createdFromNull.getDouble(7) === 0.0d)
assert(createdFromNull.getUTF8String(8) === null)
assert(createdFromNull.getBinary(9) === null)
// assert(createdFromNull.get(10) === null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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.{DataType, IntegerType, StringType}
import org.apache.spark.sql.types._
import org.apache.spark.unsafe.PlatformDependent
import org.apache.spark.unsafe.memory.MemoryAllocator
import org.apache.spark.unsafe.types.UTF8String
Expand Down Expand Up @@ -67,4 +67,19 @@ class UnsafeRowSuite extends SparkFunSuite {

assert(bytesFromArrayBackedRow === bytesFromOffheapRow)
}

test("calling getDouble() and getFloat() on null columns") {
val row = InternalRow.apply(null, null)
val unsafeRow = UnsafeProjection.create(Array[DataType](FloatType, DoubleType)).apply(row)
assert(unsafeRow.getFloat(0) === row.getFloat(0))
assert(unsafeRow.getDouble(1) === row.getDouble(1))
}

test("calling get(ordinal, datatype) on null columns") {
val row = InternalRow.apply(null)
val unsafeRow = UnsafeProjection.create(Array[DataType](NullType)).apply(row)
for (dataType <- DataTypeTestUtils.atomicTypes) {
assert(unsafeRow.get(0, dataType) === null)
}
}
}

0 comments on commit e78ec1a

Please sign in to comment.