use of org.apache.drill.test.rowSet.RowSet in project drill by axbaretto.
the class TestResultSetLoaderMaps method testOverwriteRow.
/**
* Version of the {#link TestResultSetLoaderProtocol#testOverwriteRow()} test
* that uses nested columns.
*/
@Test
public void testOverwriteRow() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").add("b", MinorType.INT).add("c", MinorType.VARCHAR).resumeSchema().buildSchema();
ResultSetLoaderImpl.ResultSetOptions options = new OptionBuilder().setSchema(schema).setRowCountLimit(ValueVector.MAX_ROW_COUNT).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
// Can't use the shortcut to populate rows when doing overwrites.
ScalarWriter aWriter = rootWriter.scalar("a");
TupleWriter mWriter = rootWriter.tuple("m");
ScalarWriter bWriter = mWriter.scalar("b");
ScalarWriter cWriter = mWriter.scalar("c");
// Write 100,000 rows, overwriting 99% of them. This will cause vector
// overflow and data corruption if overwrite does not work; but will happily
// produce the correct result if everything works as it should.
byte[] value = new byte[512];
Arrays.fill(value, (byte) 'X');
int count = 0;
rsLoader.startBatch();
while (count < 100_000) {
rootWriter.start();
count++;
aWriter.setInt(count);
bWriter.setInt(count * 10);
cWriter.setBytes(value, value.length);
if (count % 100 == 0) {
rootWriter.save();
}
}
// Verify using a reader.
RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(count / 100, result.rowCount());
RowSetReader reader = result.reader();
TupleReader mReader = reader.tuple("m");
int rowId = 1;
while (reader.next()) {
assertEquals(rowId * 100, reader.scalar("a").getInt());
assertEquals(rowId * 1000, mReader.scalar("b").getInt());
assertTrue(Arrays.equals(value, mReader.scalar("c").getBytes()));
rowId++;
}
result.clear();
rsLoader.close();
}
use of org.apache.drill.test.rowSet.RowSet in project drill by axbaretto.
the class TestResultSetLoaderOverflow method testVectorSizeLimit.
/**
* Test that the writer detects a vector overflow. The offending column
* value should be moved to the next batch.
*/
@Test
public void testVectorSizeLimit() {
TupleMetadata schema = new SchemaBuilder().add("s", MinorType.VARCHAR).buildSchema();
ResultSetOptions options = new OptionBuilder().setRowCountLimit(ValueVector.MAX_ROW_COUNT).setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
rsLoader.startBatch();
byte[] value = new byte[512];
Arrays.fill(value, (byte) 'X');
int count = 0;
while (!rootWriter.isFull()) {
rootWriter.start();
rootWriter.scalar(0).setBytes(value, value.length);
rootWriter.save();
count++;
}
// Number of rows should be driven by vector size.
// Our row count should include the overflow row
int expectedCount = ValueVector.MAX_BUFFER_SIZE / value.length;
assertEquals(expectedCount + 1, count);
// Loader's row count should include only "visible" rows
assertEquals(expectedCount, rootWriter.rowCount());
// Total count should include invisible and look-ahead rows.
assertEquals(expectedCount + 1, rsLoader.totalRowCount());
// Result should exclude the overflow row
RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(expectedCount, result.rowCount());
result.clear();
// Next batch should start with the overflow row
rsLoader.startBatch();
assertEquals(1, rootWriter.rowCount());
assertEquals(expectedCount + 1, rsLoader.totalRowCount());
result = fixture.wrap(rsLoader.harvest());
assertEquals(1, result.rowCount());
result.clear();
rsLoader.close();
}
use of org.apache.drill.test.rowSet.RowSet in project drill by axbaretto.
the class TestResultSetLoaderOverflow method testSizeLimitOnArray.
/**
* Test a row with a single array column which overflows. Verifies
* that all the fiddly bits about offset vectors and so on works
* correctly. Run this test (the simplest case) if you change anything
* about the array handling code.
*/
@Test
public void testSizeLimitOnArray() {
TupleMetadata schema = new SchemaBuilder().addArray("s", MinorType.VARCHAR).buildSchema();
ResultSetOptions options = new OptionBuilder().setRowCountLimit(ValueVector.MAX_ROW_COUNT).setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
// Fill batch with rows of with a single array, three values each. Tack on
// a suffix to each so we can be sure the proper data is written and moved
// to the overflow batch.
rsLoader.startBatch();
byte[] value = new byte[473];
Arrays.fill(value, (byte) 'X');
String strValue = new String(value, Charsets.UTF_8);
int count = 0;
int rowSize = 0;
int totalSize = 0;
int valuesPerArray = 13;
while (rootWriter.start()) {
totalSize += rowSize;
rowSize = 0;
ScalarWriter array = rootWriter.array(0).scalar();
for (int i = 0; i < valuesPerArray; i++) {
String cellValue = strValue + (count + 1) + "." + i;
array.setString(cellValue);
rowSize += cellValue.length();
}
rootWriter.save();
count++;
}
// Row count should include the overflow row.
int expectedCount = count - 1;
// Size without overflow row should fit in the vector, size
// with overflow should not.
assertTrue(totalSize <= ValueVector.MAX_BUFFER_SIZE);
assertTrue(totalSize + rowSize > ValueVector.MAX_BUFFER_SIZE);
// Result should exclude the overflow row. Last row
// should hold the last full array.
RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(expectedCount, result.rowCount());
RowSetReader reader = result.reader();
reader.set(expectedCount - 1);
ScalarElementReader arrayReader = reader.column(0).elements();
assertEquals(valuesPerArray, arrayReader.size());
for (int i = 0; i < valuesPerArray; i++) {
String cellValue = strValue + (count - 1) + "." + i;
assertEquals(cellValue, arrayReader.getString(i));
}
result.clear();
// Next batch should start with the overflow row.
// The only row in this next batch should be the whole
// array being written at the time of overflow.
rsLoader.startBatch();
// VectorPrinter.printStrings((VarCharVector) ((VarCharColumnWriter) rootWriter.array(0).scalar()).vector(), 0, 5);
// ((ResultSetLoaderImpl) rsLoader).dump(new HierarchicalPrinter());
assertEquals(1, rootWriter.rowCount());
assertEquals(expectedCount + 1, rsLoader.totalRowCount());
result = fixture.wrap(rsLoader.harvest());
// VectorPrinter.printStrings((VarCharVector) ((VarCharColumnWriter) rootWriter.array(0).scalar()).vector(), 0, 5);
assertEquals(1, result.rowCount());
reader = result.reader();
reader.next();
arrayReader = reader.column(0).elements();
assertEquals(valuesPerArray, arrayReader.size());
for (int i = 0; i < valuesPerArray; i++) {
String cellValue = strValue + (count) + "." + i;
assertEquals(cellValue, arrayReader.getString(i));
}
result.clear();
rsLoader.close();
}
use of org.apache.drill.test.rowSet.RowSet in project drill by axbaretto.
the class TestResultSetLoaderOverflow method testCloseWithOverflow.
/**
* Load a batch to overflow. Then, close the loader with the overflow
* batch unharvested. The Loader should release the memory allocated
* to the unused overflow vectors.
*/
@Test
public void testCloseWithOverflow() {
TupleMetadata schema = new SchemaBuilder().add("s", MinorType.VARCHAR).buildSchema();
ResultSetOptions options = new OptionBuilder().setRowCountLimit(ValueVector.MAX_ROW_COUNT).setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
rsLoader.startBatch();
byte[] value = new byte[512];
Arrays.fill(value, (byte) 'X');
int count = 0;
while (!rootWriter.isFull()) {
rootWriter.start();
rootWriter.scalar(0).setBytes(value, value.length);
rootWriter.save();
count++;
}
assertTrue(count < ValueVector.MAX_ROW_COUNT);
// Harvest the full batch
RowSet result = fixture.wrap(rsLoader.harvest());
result.clear();
// Close without harvesting the overflow batch.
rsLoader.close();
}
use of org.apache.drill.test.rowSet.RowSet in project drill by axbaretto.
the class TestResultSetLoaderProtocol method testOverwriteRow.
/**
* The writer protocol allows a client to write to a row any number of times
* before invoking <tt>save()</tt>. In this case, each new value simply
* overwrites the previous value. Here, we test the most basic case: a simple,
* flat tuple with no arrays. We use a very large Varchar that would, if
* overwrite were not working, cause vector overflow.
* <p>
* The ability to overwrite rows is seldom needed except in one future use
* case: writing a row, then applying a filter "in-place" to discard unwanted
* rows, without having to send the row downstream.
* <p>
* Because of this use case, specific rules apply when discarding row or
* overwriting values.
* <ul>
* <li>Values can be written once per row. Fixed-width columns actually allow
* multiple writes. But, because of the way variable-width columns work,
* multiple writes will cause undefined results.</li>
* <li>To overwrite a row, call <tt>start()</tt> without calling
* <tt>save()</tt> on the previous row. Doing so ignores data for the
* previous row and starts a new row in place of the old one.</li>
* </ul>
* Note that there is no explicit method to discard a row. Instead,
* the rule is that a row is not saved until <tt>save()</tt> is called.
*/
@Test
public void testOverwriteRow() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).add("b", MinorType.VARCHAR).buildSchema();
ResultSetLoaderImpl.ResultSetOptions options = new OptionBuilder().setSchema(schema).setRowCountLimit(ValueVector.MAX_ROW_COUNT).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
// Can't use the shortcut to populate rows when doing overwrites.
ScalarWriter aWriter = rootWriter.scalar("a");
ScalarWriter bWriter = rootWriter.scalar("b");
// Write 100,000 rows, overwriting 99% of them. This will cause vector
// overflow and data corruption if overwrite does not work; but will happily
// produce the correct result if everything works as it should.
byte[] value = new byte[512];
Arrays.fill(value, (byte) 'X');
int count = 0;
rsLoader.startBatch();
while (count < 100_000) {
rootWriter.start();
count++;
aWriter.setInt(count);
bWriter.setBytes(value, value.length);
if (count % 100 == 0) {
rootWriter.save();
}
}
// Verify using a reader.
RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(count / 100, result.rowCount());
RowSetReader reader = result.reader();
int rowId = 1;
while (reader.next()) {
assertEquals(rowId * 100, reader.scalar("a").getInt());
assertTrue(Arrays.equals(value, reader.scalar("b").getBytes()));
rowId++;
}
result.clear();
rsLoader.close();
}
Aggregations