use of org.apache.drill.exec.physical.rowSet.RowSetReader in project drill by apache.
the class TestResultSetLoaderRepeatedList method test2DOverflow.
@Test
public void test2DOverflow() {
final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addRepeatedList("list2").addArray(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.
// Data must be large enough to cause overflow before 64K rows
// Make a bit bigger to overflow early.
final int outerSize = 7;
final int innerSize = 5;
final int strLength = ValueVector.MAX_BUFFER_SIZE / ValueVector.MAX_ROW_COUNT / outerSize / innerSize + 20;
final byte[] value = new byte[strLength - 6];
Arrays.fill(value, (byte) 'X');
final String strValue = new String(value, Charsets.UTF_8);
int rowCount = 0;
int elementCount = 0;
final ArrayWriter outerWriter = writer.array(1);
final ArrayWriter innerWriter = outerWriter.array();
final ScalarWriter elementWriter = innerWriter.scalar();
rsLoader.startBatch();
while (!writer.isFull()) {
writer.start();
writer.scalar(0).setInt(rowCount);
for (int j = 0; j < outerSize; j++) {
for (int k = 0; k < innerSize; k++) {
elementWriter.setString(String.format("%s%06d", strValue, elementCount));
elementCount++;
}
outerWriter.save();
}
writer.save();
rowCount++;
}
// 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 * innerSize * outerSize);
assertEquals(expectedCount + 1, rowCount);
// 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();
ArrayReader outerReader = reader.array(1);
ArrayReader innerReader = outerReader.array();
ScalarReader strReader = innerReader.scalar();
int readRowCount = 0;
int readElementCount = 0;
while (reader.next()) {
assertEquals(readRowCount, reader.scalar(0).getInt());
for (int i = 0; i < outerSize; i++) {
assertTrue(outerReader.next());
for (int j = 0; j < innerSize; j++) {
assertTrue(innerReader.next());
assertEquals(String.format("%s%06d", strValue, readElementCount), strReader.getString());
readElementCount++;
}
assertFalse(innerReader.next());
}
assertFalse(outerReader.next());
readRowCount++;
}
assertEquals(readRowCount, result.rowCount());
result.clear();
// Write a few more rows to verify the overflow row.
rsLoader.startBatch();
for (int i = 0; i < 1000; i++) {
writer.start();
writer.scalar(0).setInt(rowCount);
for (int j = 0; j < outerSize; j++) {
for (int k = 0; k < innerSize; k++) {
elementWriter.setString(String.format("%s%06d", strValue, elementCount));
elementCount++;
}
outerWriter.save();
}
writer.save();
rowCount++;
}
result = fixture.wrap(rsLoader.harvest());
assertEquals(1001, result.rowCount());
final int startCount = readRowCount;
reader = result.reader();
outerReader = reader.array(1);
innerReader = outerReader.array();
strReader = innerReader.scalar();
while (reader.next()) {
assertEquals(readRowCount, reader.scalar(0).getInt());
for (int i = 0; i < outerSize; i++) {
assertTrue(outerReader.next());
for (int j = 0; j < innerSize; j++) {
assertTrue(innerReader.next());
elementWriter.setString(String.format("%s%06d", strValue, readElementCount));
assertEquals(String.format("%s%06d", strValue, readElementCount), strReader.getString());
readElementCount++;
}
assertFalse(innerReader.next());
}
assertFalse(outerReader.next());
readRowCount++;
}
assertEquals(readRowCount - startCount, result.rowCount());
result.clear();
rsLoader.close();
}
use of org.apache.drill.exec.physical.rowSet.RowSetReader in project drill by apache.
the class TestResultSetLoaderMapArray method testOmittedValues.
/**
* Check that the "fill-empties" logic descends down into
* a repeated map.
*/
@Test
public void testOmittedValues() {
TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addMapArray("m").addNullable("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).resumeSchema().buildSchema();
ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).rowCountLimit(ValueVector.MAX_ROW_COUNT).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
int mapSkip = 5;
int entrySkip = 3;
int rowCount = 1000;
int entryCount = 10;
rsLoader.startBatch();
ArrayWriter maWriter = rootWriter.array("m");
TupleWriter mWriter = maWriter.tuple();
for (int i = 0; i < rowCount; i++) {
rootWriter.start();
rootWriter.scalar(0).setInt(i);
if (i % mapSkip != 0) {
for (int j = 0; j < entryCount; j++) {
if (j % entrySkip != 0) {
mWriter.scalar(0).setInt(i * entryCount + j);
mWriter.scalar(1).setString("b-" + i + "." + j);
}
maWriter.save();
}
}
rootWriter.save();
}
RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(rowCount, result.rowCount());
RowSetReader reader = result.reader();
ArrayReader maReader = reader.array("m");
TupleReader mReader = maReader.tuple();
for (int i = 0; i < rowCount; i++) {
assertTrue(reader.next());
assertEquals(i, reader.scalar(0).getInt());
if (i % mapSkip == 0) {
assertEquals(0, maReader.size());
continue;
}
assertEquals(entryCount, maReader.size());
for (int j = 0; j < entryCount; j++) {
assertTrue(maReader.next());
if (j % entrySkip == 0) {
assertTrue(mReader.scalar(0).isNull());
assertTrue(mReader.scalar(1).isNull());
} else {
assertFalse(mReader.scalar(0).isNull());
assertFalse(mReader.scalar(1).isNull());
assertEquals(i * entryCount + j, mReader.scalar(0).getInt());
assertEquals("b-" + i + "." + j, mReader.scalar(1).getString());
}
}
}
result.clear();
rsLoader.close();
}
use of org.apache.drill.exec.physical.rowSet.RowSetReader in project drill by apache.
the class TestResultSetLoaderMapArray method testDoubleNestedArray.
/**
* Test a doubly-nested array of maps.
*/
@Test
public void testDoubleNestedArray() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMapArray("m1").add("b", MinorType.INT).addMapArray("m2").add("c", MinorType.INT).addArray("d", MinorType.VARCHAR).resumeMap().resumeSchema().buildSchema();
ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
rsLoader.startBatch();
ScalarWriter aWriter = rootWriter.scalar("a");
ArrayWriter a1Writer = rootWriter.array("m1");
TupleWriter m1Writer = a1Writer.tuple();
ScalarWriter bWriter = m1Writer.scalar("b");
ArrayWriter a2Writer = m1Writer.array("m2");
TupleWriter m2Writer = a2Writer.tuple();
ScalarWriter cWriter = m2Writer.scalar("c");
ScalarWriter dWriter = m2Writer.array("d").scalar();
for (int i = 0; i < 5; i++) {
rootWriter.start();
aWriter.setInt(i);
for (int j = 0; j < 4; j++) {
int a1Key = i + 10 + j;
bWriter.setInt(a1Key);
for (int k = 0; k < 3; k++) {
int a2Key = a1Key * 10 + k;
cWriter.setInt(a2Key);
for (int l = 0; l < 2; l++) {
dWriter.setString("d-" + (a2Key * 10 + l));
}
a2Writer.save();
}
a1Writer.save();
}
rootWriter.save();
}
RowSet results = fixture.wrap(rsLoader.harvest());
RowSetReader reader = results.reader();
ScalarReader aReader = reader.scalar("a");
ArrayReader a1Reader = reader.array("m1");
TupleReader m1Reader = a1Reader.tuple();
ScalarReader bReader = m1Reader.scalar("b");
ArrayReader a2Reader = m1Reader.array("m2");
TupleReader m2Reader = a2Reader.tuple();
ScalarReader cReader = m2Reader.scalar("c");
ArrayReader dArray = m2Reader.array("d");
ScalarReader dReader = dArray.scalar();
for (int i = 0; i < 5; i++) {
assertTrue(reader.next());
assertEquals(i, aReader.getInt());
for (int j = 0; j < 4; j++) {
assertTrue(a1Reader.next());
int a1Key = i + 10 + j;
assertEquals(a1Key, bReader.getInt());
for (int k = 0; k < 3; k++) {
assertTrue(a2Reader.next());
int a2Key = a1Key * 10 + k;
assertEquals(a2Key, cReader.getInt());
for (int l = 0; l < 2; l++) {
assertTrue(dArray.next());
assertEquals("d-" + (a2Key * 10 + l), dReader.getString());
}
}
}
}
rsLoader.close();
}
use of org.apache.drill.exec.physical.rowSet.RowSetReader in project drill by apache.
the class TestResultSetLoaderOmittedValues method testOmittedValuesAtEndWithOverflow.
/**
* Test "holes" at the end of a batch when batch overflows. Completed
* batch must be finalized correctly, new batch initialized correct,
* for the missing values.
*/
@Test
public void testOmittedValuesAtEndWithOverflow() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).add("b", MinorType.VARCHAR).addNullable("c", MinorType.VARCHAR).addNullable("d", MinorType.VARCHAR).buildSchema();
ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().rowCountLimit(ValueVector.MAX_ROW_COUNT).readerSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
// Fill the batch. Column d has some values. Column c is worst case: no values.
rsLoader.startBatch();
byte[] value = new byte[533];
Arrays.fill(value, (byte) 'X');
int rowNumber = 0;
while (!rootWriter.isFull()) {
rootWriter.start();
rowNumber++;
rootWriter.scalar(0).setInt(rowNumber);
rootWriter.scalar(1).setBytes(value, value.length);
if (rowNumber < 10_000) {
rootWriter.scalar(3).setString("d-" + rowNumber);
}
rootWriter.save();
assertEquals(rowNumber, rsLoader.totalRowCount());
}
// Harvest and verify
RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(rowNumber - 1, result.rowCount());
RowSetReader reader = result.reader();
int rowIndex = 0;
while (reader.next()) {
int expectedRowNumber = 1 + rowIndex;
assertEquals(expectedRowNumber, reader.scalar(0).getInt());
assertTrue(reader.scalar(2).isNull());
if (expectedRowNumber < 10_000) {
assertEquals("d-" + expectedRowNumber, reader.scalar(3).getString());
} else {
assertTrue(reader.scalar(3).isNull());
}
rowIndex++;
}
// Start count for this batch is one less than current
// count, because of the overflow row.
int startRowNumber = rowNumber;
// Write a few more rows to the next batch
rsLoader.startBatch();
for (int i = 0; i < 10; i++) {
rootWriter.start();
rowNumber++;
rootWriter.scalar(0).setInt(rowNumber);
rootWriter.scalar(1).setBytes(value, value.length);
if (i > 5) {
rootWriter.scalar(3).setString("d-" + rowNumber);
}
rootWriter.save();
assertEquals(rowNumber, rsLoader.totalRowCount());
}
// Verify that holes were preserved.
result = fixture.wrap(rsLoader.harvest());
assertEquals(rowNumber, rsLoader.totalRowCount());
assertEquals(rowNumber - startRowNumber + 1, result.rowCount());
// result.print();
reader = result.reader();
rowIndex = 0;
while (reader.next()) {
int expectedRowNumber = startRowNumber + rowIndex;
assertEquals(expectedRowNumber, reader.scalar(0).getInt());
assertTrue(reader.scalar(2).isNull());
if (rowIndex > 6) {
assertEquals("d-" + expectedRowNumber, reader.scalar(3).getString());
} else {
assertTrue("Row " + rowIndex + " col d should be null", reader.scalar(3).isNull());
}
rowIndex++;
}
assertEquals(rowIndex, 11);
rsLoader.close();
}
use of org.apache.drill.exec.physical.rowSet.RowSetReader in project drill by apache.
the class TestResultSetLoaderMaps method testOverwriteRow.
/**
* Version of the {#link TestResultSetLoaderProtocol#testOverwriteRow()} test
* that uses nested columns.
*/
@Test
public void testOverwriteRow() {
final TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").add("b", MinorType.INT).add("c", MinorType.VARCHAR).resumeSchema().buildSchema();
final ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).rowCountLimit(ValueVector.MAX_ROW_COUNT).build();
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
final RowSetLoader rootWriter = rsLoader.writer();
// Can't use the shortcut to populate rows when doing overwrites.
final ScalarWriter aWriter = rootWriter.scalar("a");
final TupleWriter mWriter = rootWriter.tuple("m");
final ScalarWriter bWriter = mWriter.scalar("b");
final 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.
final 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.
final RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(count / 100, result.rowCount());
final RowSetReader reader = result.reader();
final 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();
}
Aggregations