use of org.apache.drill.exec.physical.rowSet.RowSetReader in project drill by apache.
the class TestResultSetLoaderOverflow method testVectorSizeLimitWithAppend.
@Test
public void testVectorSizeLimitWithAppend() {
TupleMetadata schema = new SchemaBuilder().add("s", MinorType.VARCHAR).buildSchema();
ResultSetOptions options = new ResultSetOptionBuilder().rowCountLimit(ValueVector.MAX_ROW_COUNT).readerSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
rsLoader.startBatch();
byte[] head = "abc".getBytes();
byte[] tail = new byte[523];
Arrays.fill(tail, (byte) 'X');
String expected = new String(head, Charsets.UTF_8);
expected += new String(tail, Charsets.UTF_8);
expected += new String(tail, Charsets.UTF_8);
int count = 0;
ScalarWriter colWriter = rootWriter.scalar(0);
while (!rootWriter.isFull()) {
rootWriter.start();
colWriter.setBytes(head, head.length);
colWriter.appendBytes(tail, tail.length);
colWriter.appendBytes(tail, tail.length);
rootWriter.save();
count++;
}
// Number of rows should be driven by vector size.
// Our row count should include the overflow row
int valueLength = head.length + 2 * tail.length;
int expectedCount = ValueVector.MAX_BUFFER_SIZE / valueLength;
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
{
VectorContainer container = rsLoader.harvest();
BatchValidator.validate(container);
RowSet result = fixture.wrap(container);
assertEquals(expectedCount, result.rowCount());
// Verify that the values were, in fact, appended.
RowSetReader reader = result.reader();
while (reader.next()) {
assertEquals(expected, reader.scalar(0).getString());
}
result.clear();
}
// Next batch should start with the overflow row
rsLoader.startBatch();
assertEquals(1, rootWriter.rowCount());
assertEquals(expectedCount + 1, rsLoader.totalRowCount());
{
VectorContainer container = rsLoader.harvest();
BatchValidator.validate(container);
RowSet result = fixture.wrap(container);
assertEquals(1, result.rowCount());
RowSetReader reader = result.reader();
while (reader.next()) {
assertEquals(expected, reader.scalar(0).getString());
}
result.clear();
}
rsLoader.close();
}
use of org.apache.drill.exec.physical.rowSet.RowSetReader in project drill by apache.
the class TestResultSetLoaderOverflow method testArrayOverflowWithOtherArrays.
/**
* Test the complete set of array overflow cases:
* <ul>
* <li>Array a is written before the column that has overflow,
* and must be copied, in its entirety, to the overflow row.</li>
* <li>Column b causes the overflow.</li>
* <li>Column c is written after the overflow, and should go
* to the look-ahead row.</li>
* <li>Column d is written for a while, then has empties before
* the overflow row, but is written in the overflow row.<li>
* <li>Column e is like d, but is not written in the overflow
* row.</li>
*/
@Test
public void testArrayOverflowWithOtherArrays() {
TupleMetadata schema = new SchemaBuilder().addArray("a", MinorType.INT).addArray("b", MinorType.VARCHAR).addArray("c", MinorType.INT).addArray("d", MinorType.INT).buildSchema();
ResultSetOptions options = new ResultSetOptionBuilder().rowCountLimit(ValueVector.MAX_ROW_COUNT).readerSchema(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.
byte[] value = new byte[512];
Arrays.fill(value, (byte) 'X');
String strValue = new String(value, Charsets.UTF_8);
int aCount = 3;
int bCount = 11;
int cCount = 5;
int dCount = 7;
int cCutoff = ValueVector.MAX_BUFFER_SIZE / value.length / bCount / 2;
ScalarWriter aWriter = rootWriter.array("a").scalar();
ScalarWriter bWriter = rootWriter.array("b").scalar();
ScalarWriter cWriter = rootWriter.array("c").scalar();
ScalarWriter dWriter = rootWriter.array("d").scalar();
int count = 0;
rsLoader.startBatch();
while (rootWriter.start()) {
for (int i = 0; i < aCount; i++) {
aWriter.setInt(count * aCount + i);
}
for (int i = 0; i < bCount; i++) {
String cellValue = strValue + (count * bCount + i);
bWriter.setString(cellValue);
}
if (count < cCutoff) {
for (int i = 0; i < cCount; i++) {
cWriter.setInt(count * cCount + i);
}
}
if (count < cCutoff || rootWriter.isFull()) {
for (int i = 0; i < dCount; i++) {
dWriter.setInt(count * dCount + i);
}
}
rootWriter.save();
count++;
}
// Verify
{
VectorContainer container = rsLoader.harvest();
BatchValidator.validate(container);
RowSet result = fixture.wrap(container);
assertEquals(count - 1, result.rowCount());
RowSetReader reader = result.reader();
ArrayReader aArray = reader.array("a");
ScalarReader aReader = aArray.scalar();
ArrayReader bArray = reader.array("b");
ScalarReader bReader = bArray.scalar();
ArrayReader cArray = reader.array("c");
ScalarReader cReader = cArray.scalar();
ArrayReader dArray = reader.array("d");
ScalarReader dReader = dArray.scalar();
while (reader.next()) {
int rowId = reader.offset();
assertEquals(aCount, aArray.size());
for (int i = 0; i < aCount; i++) {
assertTrue(aArray.next());
assertEquals(rowId * aCount + i, aReader.getInt());
}
assertEquals(bCount, bArray.size());
for (int i = 0; i < bCount; i++) {
assertTrue(bArray.next());
String cellValue = strValue + (rowId * bCount + i);
assertEquals(cellValue, bReader.getString());
}
if (rowId < cCutoff) {
assertEquals(cCount, cArray.size());
for (int i = 0; i < cCount; i++) {
assertTrue(cArray.next());
assertEquals(rowId * cCount + i, cReader.getInt());
}
assertEquals(dCount, dArray.size());
for (int i = 0; i < dCount; i++) {
assertTrue(dArray.next());
assertEquals(rowId * dCount + i, dReader.getInt());
}
} else {
assertEquals(0, cArray.size());
assertEquals(0, dArray.size());
}
}
result.clear();
}
int firstCount = count - 1;
// One row is in the batch. Write more, skipping over the
// initial few values for columns c and d. Column d has a
// roll-over value, c has an empty roll-over.
rsLoader.startBatch();
for (int j = 0; j < 5; j++) {
rootWriter.start();
for (int i = 0; i < aCount; i++) {
aWriter.setInt(count * aCount + i);
}
for (int i = 0; i < bCount; i++) {
String cellValue = strValue + (count * bCount + i);
bWriter.setString(cellValue);
}
if (j > 3) {
for (int i = 0; i < cCount; i++) {
cWriter.setInt(count * cCount + i);
}
for (int i = 0; i < dCount; i++) {
dWriter.setInt(count * dCount + i);
}
}
rootWriter.save();
count++;
}
{
VectorContainer container = rsLoader.harvest();
BatchValidator.validate(container);
RowSet result = fixture.wrap(container);
assertEquals(6, result.rowCount());
RowSetReader reader = result.reader();
ArrayReader aArray = reader.array("a");
ScalarReader aReader = aArray.scalar();
ArrayReader bArray = reader.array("b");
ScalarReader bReader = bArray.scalar();
ArrayReader cArray = reader.array("c");
ScalarReader cReader = cArray.scalar();
ArrayReader dArray = reader.array("d");
ScalarReader dReader = dArray.scalar();
int j = 0;
while (reader.next()) {
int rowId = firstCount + reader.offset();
assertEquals(aCount, aArray.size());
for (int i = 0; i < aCount; i++) {
assertTrue(aArray.next());
assertEquals("Index " + i, rowId * aCount + i, aReader.getInt());
}
assertEquals(bCount, bArray.size());
for (int i = 0; i < bCount; i++) {
assertTrue(bArray.next());
String cellValue = strValue + (rowId * bCount + i);
assertEquals(cellValue, bReader.getString());
}
if (j > 4) {
assertEquals(cCount, cArray.size());
for (int i = 0; i < cCount; i++) {
assertTrue(cArray.next());
assertEquals(rowId * cCount + i, cReader.getInt());
}
} else {
assertEquals(0, cArray.size());
}
if (j == 0 || j > 4) {
assertEquals(dCount, dArray.size());
for (int i = 0; i < dCount; i++) {
assertTrue(dArray.next());
assertEquals(rowId * dCount + i, dReader.getInt());
}
} else {
assertEquals(0, dArray.size());
}
j++;
}
result.clear();
}
rsLoader.close();
}
use of org.apache.drill.exec.physical.rowSet.RowSetReader in project drill by apache.
the class TestResultSetLoaderUnions method testUnionOverflow.
@Test
public void testUnionOverflow() {
final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addUnion("u").addType(MinorType.INT).addType(MinorType.VARCHAR).resumeSchema().buildSchema();
final ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().rowCountLimit(ValueVector.MAX_ROW_COUNT).readerSchema(schema).build();
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
final RowSetLoader writer = rsLoader.writer();
// Fill the batch with enough data to cause overflow.
// Fill even rows with a Varchar, odd rows with an int.
// Data must be large enough to cause overflow before 32K rows
// (the half that get strings.
// 16 MB / 32 K = 512 bytes
// Make a bit bigger to overflow early.
final int strLength = 600;
final byte[] value = new byte[strLength - 6];
Arrays.fill(value, (byte) 'X');
final String strValue = new String(value, Charsets.UTF_8);
int count = 0;
rsLoader.startBatch();
while (!writer.isFull()) {
if (count % 2 == 0) {
writer.addRow(count, String.format("%s%06d", strValue, count));
} else {
writer.addRow(count, count * 10);
}
count++;
}
// Number of rows should be driven by vector size.
// Our row count should include the overflow row
final int expectedCount = ValueVector.MAX_BUFFER_SIZE / strLength * 2;
assertEquals(expectedCount + 1, count);
// Loader's row count should include only "visible" rows
assertEquals(expectedCount, writer.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());
// Verify the data.
RowSetReader reader = result.reader();
int readCount = 0;
while (reader.next()) {
assertEquals(readCount, reader.scalar(0).getInt());
if (readCount % 2 == 0) {
assertEquals(String.format("%s%06d", strValue, readCount), reader.variant(1).scalar().getString());
} else {
assertEquals(readCount * 10, reader.variant(1).scalar().getInt());
}
readCount++;
}
assertEquals(readCount, result.rowCount());
result.clear();
// Write a few more rows to verify the overflow row.
rsLoader.startBatch();
for (int i = 0; i < 1000; i++) {
if (count % 2 == 0) {
writer.addRow(count, String.format("%s%06d", strValue, count));
} else {
writer.addRow(count, count * 10);
}
count++;
}
result = fixture.wrap(rsLoader.harvest());
assertEquals(1001, result.rowCount());
final int startCount = readCount;
reader = result.reader();
while (reader.next()) {
assertEquals(readCount, reader.scalar(0).getInt());
if (readCount % 2 == 0) {
assertEquals(String.format("%s%06d", strValue, readCount), reader.variant(1).scalar().getString());
} else {
assertEquals(readCount * 10, reader.variant(1).scalar().getInt());
}
readCount++;
}
assertEquals(readCount - startCount, result.rowCount());
result.clear();
rsLoader.close();
}
use of org.apache.drill.exec.physical.rowSet.RowSetReader in project drill by apache.
the class TestResultSetSchemaChange method testSchemaChangeWithOverflow.
/**
* Test a schema change on the row that overflows. If the
* new column is added after overflow, it will appear as
* a schema-change in the following batch. This is fine as
* we are essentially time-shifting: pretending that the
* overflow row was written in the next batch (which, in
* fact, it is: that's what overflow means.)
*/
@Test
public void testSchemaChangeWithOverflow() {
ResultSetOptions options = new ResultSetOptionBuilder().rowCountLimit(ValueVector.MAX_ROW_COUNT).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
rootWriter.addColumn(SchemaBuilder.columnSchema("a", MinorType.VARCHAR, DataMode.REQUIRED));
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);
if (rootWriter.isFull()) {
rootWriter.addColumn(SchemaBuilder.columnSchema("b", MinorType.INT, DataMode.OPTIONAL));
rootWriter.scalar(1).setInt(count);
// Add a Varchar to ensure its offset fiddling is done properly
rootWriter.addColumn(SchemaBuilder.columnSchema("c", MinorType.VARCHAR, DataMode.OPTIONAL));
rootWriter.scalar(2).setString("c-" + count);
// Allow adding a required column at this point.
// (Not intuitively obvious that this should work; we back-fill
// with zeros.)
rootWriter.addColumn(SchemaBuilder.columnSchema("d", MinorType.INT, DataMode.REQUIRED));
}
rootWriter.save();
count++;
}
// Result should include only the first column.
SchemaBuilder schemaBuilder = new SchemaBuilder().add("a", MinorType.VARCHAR);
BatchSchema expectedSchema = new BatchSchemaBuilder().withSchemaBuilder(schemaBuilder).build();
RowSet result = fixture.wrap(rsLoader.harvest());
assertTrue(result.batchSchema().isEquivalent(expectedSchema));
assertEquals(count - 1, result.rowCount());
result.clear();
assertEquals(1, rsLoader.schemaVersion());
// Double check: still can add a required column after
// starting the next batch. (No longer in overflow state.)
rsLoader.startBatch();
rootWriter.addColumn(SchemaBuilder.columnSchema("e", MinorType.INT, DataMode.REQUIRED));
// Next batch should start with the overflow row, including
// the column added at the end of the previous batch, after
// overflow.
result = fixture.wrap(rsLoader.harvest());
assertEquals(5, rsLoader.schemaVersion());
assertEquals(1, result.rowCount());
BatchSchemaBuilder batchSchemaBuilder = new BatchSchemaBuilder(expectedSchema);
batchSchemaBuilder.schemaBuilder().addNullable("b", MinorType.INT).addNullable("c", MinorType.VARCHAR).add("d", MinorType.INT).add("e", MinorType.INT);
expectedSchema = batchSchemaBuilder.build();
assertTrue(result.batchSchema().isEquivalent(expectedSchema));
RowSetReader reader = result.reader();
reader.next();
assertEquals(count - 1, reader.scalar(1).getInt());
assertEquals("c-" + (count - 1), reader.scalar(2).getString());
assertEquals(0, reader.scalar("d").getInt());
assertEquals(0, reader.scalar("e").getInt());
result.clear();
rsLoader.close();
}
use of org.apache.drill.exec.physical.rowSet.RowSetReader in project drill by apache.
the class TestAnalyze method verifyAnalyzeOutput.
// Helper function to verify output of ANALYZE statement
private void verifyAnalyzeOutput(String query, String message) throws Exception {
DirectRowSet rowSet = queryBuilder().sql(query).rowSet();
try {
assertEquals(1, rowSet.rowCount());
RowSetReader reader = rowSet.reader();
assertEquals(2, reader.columnCount());
while (reader.next()) {
ObjectReader column = reader.column(1);
assertEquals(message, column.isNull() ? null : column.getObject().toString());
}
} finally {
rowSet.clear();
}
}
Aggregations