Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rename
  • Loading branch information
cloud-fan committed Apr 28, 2016
commit c6c3584c6cc7c500ca31cb21a00391c06caf3aa6
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,18 @@ private[spark] class MatrixUDT extends UserDefinedType[Matrix] {
val numRows = row.getInt(1)
val numCols = row.getInt(2)
val values = row.getArray(5) match {
case u: UnsafeArrayData => u.toPrimitiveDoubleArray
case u: UnsafeArrayData => u.toDoubleArrayUnchecked
case a => a.toDoubleArray()
}
val isTransposed = row.getBoolean(6)
tpe match {
case 0 =>
val colPtrs = row.getArray(3) match {
case u: UnsafeArrayData => u.toPrimitiveIntArray
case u: UnsafeArrayData => u.toIntArrayUnchecked
case a => a.toIntArray()
}
val rowIndices = row.getArray(4) match {
case u: UnsafeArrayData => u.toPrimitiveIntArray
case u: UnsafeArrayData => u.toIntArrayUnchecked
case a => a.toIntArray()
}
new SparseMatrix(numRows, numCols, colPtrs, rowIndices, values, isTransposed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,17 @@ class VectorUDT extends UserDefinedType[Vector] {
case 0 =>
val size = row.getInt(1)
val indices = row.getArray(2) match {
case u: UnsafeArrayData => u.toPrimitiveIntArray
case u: UnsafeArrayData => u.toIntArrayUnchecked
case a => a.toIntArray()
}
val values = row.getArray(3) match {
case u: UnsafeArrayData => u.toPrimitiveDoubleArray
case u: UnsafeArrayData => u.toDoubleArrayUnchecked
case a => a.toDoubleArray()
}
new SparseVector(size, indices, values)
case 1 =>
val values = row.getArray(3) match {
case u: UnsafeArrayData => u.toPrimitiveDoubleArray
case u: UnsafeArrayData => u.toDoubleArrayUnchecked
case a => a.toDoubleArray()
}
new DenseVector(values)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,24 @@ public UnsafeArrayData copy() {
return arrayCopy;
}

public int[] toPrimitiveIntArray() {
/**
* A faster version of `toIntArray`, which use memory copy instead of iterating all elements.
* Note that, this method is dangerous if this array contains null elements. We don't write
* null elements into the data region and memory copy will crash as the data size doesn't match.
*/
public int[] toIntArrayUnchecked() {
int[] result = new int[numElements];
Platform.copyMemory(baseObject, baseOffset + 4 + 4L * numElements,
result, Platform.INT_ARRAY_OFFSET, 4L * numElements);
return result;
}

public double[] toPrimitiveDoubleArray() {
/**
* A faster version of `toDoubleArray`, which use memory copy instead of iterating all elements.
* Note that, this method is dangerous if this array contains null elements. We don't write
* null elements into the data region and memory copy will crash as the data size doesn't match.
*/
public double[] toDoubleArrayUnchecked() {
double[] result = new double[numElements];
Platform.copyMemory(baseObject, baseOffset + 4 + 4L * numElements,
result, Platform.DOUBLE_ARRAY_OFFSET, 8L * numElements);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ class UnsafeArraySuite extends SparkFunSuite {
assert(unsafe.getDouble(2) == 3.3)
}

test("to primitive int array") {
test("to int array unchecked") {
val array = Array(1, 10, 100)
val unsafe = UnsafeArrayData.fromPrimitiveArray(array)
val array2 = unsafe.toPrimitiveIntArray
val array2 = unsafe.toIntArrayUnchecked
assert(array.toSeq == array2.toSeq)
}

test("to primitive double array") {
test("to double array unchecked") {
val array = Array(1.1, 2.2, 3.3)
val unsafe = UnsafeArrayData.fromPrimitiveArray(array)
val array2 = unsafe.toPrimitiveDoubleArray
val array2 = unsafe.toDoubleArrayUnchecked
assert(array.toSeq == array2.toSeq)
}
}