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
add initialize() method for reusing CachedBatchColumnVector
  • Loading branch information
kiszk committed Aug 7, 2017
commit 157fde1e0fa5418d816e033e8f084f641a8aee88
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
*/
public final class CachedBatchColumnVector extends ReadOnlyColumnVector {

// buffer for a column
private byte[] buffer;

// accessor for a column
private ColumnAccessor columnAccessor;

Expand All @@ -50,7 +53,9 @@ public final class CachedBatchColumnVector extends ReadOnlyColumnVector {

public CachedBatchColumnVector(byte[] buffer, int numRows, DataType type) {
super(numRows, type, MemoryMode.ON_HEAP);
initialize(buffer, type);
this.buffer = buffer;
initialize();
initializeRowAccessor(type);
}

@Override
Expand Down Expand Up @@ -79,7 +84,7 @@ private void prepareAccess(int rowId) {
}
} else {
throw new UnsupportedOperationException("Row access order must be equal or ascending." +
" Row " + rowId + "is accessed after row "+ previousRowId + " was accessed.");
" Row " + rowId + " is accessed after row "+ previousRowId + " was accessed.");
}
}

Expand Down Expand Up @@ -231,10 +236,13 @@ public final UTF8String getUTF8String(int rowId) {
return unsafeRow.getUTF8String(ORDINAL);
}

private void initialize(byte[] buffer, DataType type) {
void initialize() {
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
columnAccessor = ColumnAccessor$.MODULE$.apply(type, byteBuffer);
previousRowId = -1;
}

private void initializeRowAccessor(DataType type) {
unsafeRow = new UnsafeRow(1);
bufferHolder = new BufferHolder(unsafeRow);
rowWriter = new UnsafeRowWriter(bufferHolder, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1267,10 +1267,14 @@ class ColumnarBatchSuite extends SparkFunSuite {
val column = new CachedBatchColumnVector(
JavaUtils.bufferToArray(columnBuilder.build), 1024, dataType)

assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getBoolean(i) == (i % 2 == 0))
// reuse CachedBatchColumnVector
for (j <- 0 to 1) {
column.initialize
assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getBoolean(i) == (i % 2 == 0))
}
}
column.close
}
Expand All @@ -1290,10 +1294,14 @@ class ColumnarBatchSuite extends SparkFunSuite {
val column = new CachedBatchColumnVector(
JavaUtils.bufferToArray(columnBuilder.build), 1024, dataType)

assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getByte(i) == i)
// reuse CachedBatchColumnVector
for (j <- 0 to 1) {
column.initialize
assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getByte(i) == i)
}
}
column.close
}
Expand All @@ -1313,10 +1321,14 @@ class ColumnarBatchSuite extends SparkFunSuite {
val column = new CachedBatchColumnVector(
JavaUtils.bufferToArray(columnBuilder.build), 1024, dataType)

assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getShort(i) == i)
// reuse CachedBatchColumnVector
for (j <- 0 to 1) {
column.initialize
assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getShort(i) == i)
}
}
column.close
}
Expand All @@ -1336,10 +1348,14 @@ class ColumnarBatchSuite extends SparkFunSuite {
val column = new CachedBatchColumnVector(
JavaUtils.bufferToArray(columnBuilder.build), 1024, dataType)

assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getInt(i) == i)
// reuse CachedBatchColumnVector
for (j <- 0 to 1) {
column.initialize
assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getInt(i) == i)
}
}
column.close
}
Expand All @@ -1359,10 +1375,14 @@ class ColumnarBatchSuite extends SparkFunSuite {
val column = new CachedBatchColumnVector(
JavaUtils.bufferToArray(columnBuilder.build), 1024, dataType)

assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getLong(i) == i.toLong)
// reuse CachedBatchColumnVector
for (j <- 0 to 1) {
column.initialize
assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getLong(i) == i.toLong)
}
}
column.close
}
Expand All @@ -1382,10 +1402,14 @@ class ColumnarBatchSuite extends SparkFunSuite {
val column = new CachedBatchColumnVector(
JavaUtils.bufferToArray(columnBuilder.build), 1024, dataType)

assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getFloat(i) == i.toFloat)
// reuse CachedBatchColumnVector
for (j <- 0 to 1) {
column.initialize
assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getFloat(i) == i.toFloat)
}
}
column.close
}
Expand All @@ -1405,10 +1429,14 @@ class ColumnarBatchSuite extends SparkFunSuite {
val column = new CachedBatchColumnVector(
JavaUtils.bufferToArray(columnBuilder.build), 1024, dataType)

assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getDouble(i) == i.toDouble)
// reuse CachedBatchColumnVector
for (j <- 0 to 1) {
column.initialize
assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getDouble(i) == i.toDouble)
}
}
column.close
}
Expand All @@ -1428,11 +1456,13 @@ class ColumnarBatchSuite extends SparkFunSuite {
val column = new CachedBatchColumnVector(
JavaUtils.bufferToArray(columnBuilder.build), 1024, dataType)

assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
dataType match {
case _ : StringType => assert(column.getUTF8String(i).toString == (i % 4).toString)
// reuse CachedBatchColumnVector
for (j <- 0 to 1) {
column.initialize
assert(column.isNullAt(0) == true)
for (i <- 1 until 16) {
assert(column.isNullAt(i) == false)
assert(column.getUTF8String(i).toString == (i % 4).toString)
}
}
column.close
Expand Down