use of org.apache.drill.exec.vector.accessor.ScalarWriter in project drill by axbaretto.
the class TestResultSetLoaderOmittedValues method testOmittedValuesAtEnd.
/**
* Test "holes" in the middle of a batch, and unset columns at
* the end. Ending the batch should fill in missing values.
*/
@Test
public void testOmittedValuesAtEnd() {
// Create columns up front
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).add("b", MinorType.VARCHAR).addNullable("c", MinorType.VARCHAR).add("d", MinorType.INT).addNullable("e", MinorType.INT).addArray("f", MinorType.VARCHAR).buildSchema();
ResultSetLoaderImpl.ResultSetOptions options = new OptionBuilder().setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
rsLoader.startBatch();
int rowCount = 0;
ScalarWriter arrayWriter;
for (int i = 0; i < 2; i++) {
// Row 0, 1
rootWriter.start();
rowCount++;
rootWriter.scalar(0).setInt(rowCount);
rootWriter.scalar(1).setString("b_" + rowCount);
rootWriter.scalar(2).setString("c_" + rowCount);
rootWriter.scalar(3).setInt(rowCount * 10);
rootWriter.scalar(4).setInt(rowCount * 100);
arrayWriter = rootWriter.column(5).array().scalar();
arrayWriter.setString("f_" + rowCount + "-1");
arrayWriter.setString("f_" + rowCount + "-2");
rootWriter.save();
}
for (int i = 0; i < 2; i++) {
// Rows 2, 3
rootWriter.start();
rowCount++;
rootWriter.scalar(0).setInt(rowCount);
rootWriter.scalar(1).setString("b_" + rowCount);
rootWriter.scalar(3).setInt(rowCount * 10);
arrayWriter = rootWriter.column(5).array().scalar();
arrayWriter.setString("f_" + rowCount + "-1");
arrayWriter.setString("f_" + rowCount + "-2");
rootWriter.save();
}
for (int i = 0; i < 2; i++) {
// Rows 4, 5
rootWriter.start();
rowCount++;
rootWriter.scalar(0).setInt(rowCount);
rootWriter.scalar(2).setString("c_" + rowCount);
rootWriter.scalar(4).setInt(rowCount * 100);
rootWriter.save();
}
for (int i = 0; i < 2; i++) {
// Rows 6, 7
rootWriter.start();
rowCount++;
rootWriter.scalar(0).setInt(rowCount);
rootWriter.scalar(1).setString("b_" + rowCount);
rootWriter.scalar(2).setString("c_" + rowCount);
rootWriter.scalar(3).setInt(rowCount * 10);
rootWriter.scalar(4).setInt(rowCount * 100);
arrayWriter = rootWriter.column(5).array().scalar();
arrayWriter.setString("f_" + rowCount + "-1");
arrayWriter.setString("f_" + rowCount + "-2");
rootWriter.save();
}
for (int i = 0; i < 2; i++) {
// Rows 8, 9
rootWriter.start();
rowCount++;
rootWriter.scalar(0).setInt(rowCount);
rootWriter.save();
}
// Harvest the row and verify.
RowSet actual = fixture.wrap(rsLoader.harvest());
// actual.print();
BatchSchema expectedSchema = new SchemaBuilder().add("a", MinorType.INT).add("b", MinorType.VARCHAR).addNullable("c", MinorType.VARCHAR).add("3", MinorType.INT).addNullable("e", MinorType.INT).addArray("f", MinorType.VARCHAR).build();
SingleRowSet expected = fixture.rowSetBuilder(expectedSchema).addRow(1, "b_1", "c_1", 10, 100, strArray("f_1-1", "f_1-2")).addRow(2, "b_2", "c_2", 20, 200, strArray("f_2-1", "f_2-2")).addRow(3, "b_3", null, 30, null, strArray("f_3-1", "f_3-2")).addRow(4, "b_4", null, 40, null, strArray("f_4-1", "f_4-2")).addRow(5, "", "c_5", 0, 500, strArray()).addRow(6, "", "c_6", 0, 600, strArray()).addRow(7, "b_7", "c_7", 70, 700, strArray("f_7-1", "f_7-2")).addRow(8, "b_8", "c_8", 80, 800, strArray("f_8-1", "f_8-2")).addRow(9, "", null, 0, null, strArray()).addRow(10, "", null, 0, null, strArray()).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
rsLoader.close();
}
use of org.apache.drill.exec.vector.accessor.ScalarWriter in project drill by axbaretto.
the class TestResultSetLoaderOverflow method testLargeArray.
/**
* Create an array that contains more than 64K values. Drill has no numeric
* limit on array lengths. (Well, it does, but the limit is about 2 billion
* which, even for bytes, is too large to fit into a vector...)
*/
@Test
public void testLargeArray() {
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator());
RowSetLoader rootWriter = rsLoader.writer();
MaterializedField field = SchemaBuilder.columnSchema("a", MinorType.INT, DataMode.REPEATED);
rootWriter.addColumn(field);
// Create a single array as the column value in the first row. When
// this overflows, an exception is thrown since overflow is not possible.
rsLoader.startBatch();
rootWriter.start();
ScalarWriter array = rootWriter.array(0).scalar();
try {
for (int i = 0; i < Integer.MAX_VALUE; i++) {
array.setInt(i + 1);
}
fail();
} catch (UserException e) {
// Expected
}
rsLoader.close();
}
use of org.apache.drill.exec.vector.accessor.ScalarWriter in project drill by axbaretto.
the class TestResultSetLoaderOverflow method testMissingArrayValues.
/**
* Test the case that an array has "missing values" before the overflow.
*/
@Test
public void testMissingArrayValues() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).add("b", MinorType.VARCHAR).addArray("c", MinorType.INT).buildSchema();
ResultSetOptions options = new OptionBuilder().setRowCountLimit(ValueVector.MAX_ROW_COUNT).setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
byte[] value = new byte[512];
Arrays.fill(value, (byte) 'X');
int blankAfter = ValueVector.MAX_BUFFER_SIZE / 512 * 2 / 3;
ScalarWriter cWriter = rootWriter.array("c").scalar();
rsLoader.startBatch();
int rowId = 0;
while (rootWriter.start()) {
rootWriter.scalar("a").setInt(rowId);
rootWriter.scalar("b").setBytes(value, value.length);
if (rowId < blankAfter) {
for (int i = 0; i < 3; i++) {
cWriter.setInt(rowId * 3 + i);
}
}
rootWriter.save();
rowId++;
}
RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(rowId - 1, result.rowCount());
RowSetReader reader = result.reader();
ScalarElementReader cReader = reader.array("c").elements();
while (reader.next()) {
assertEquals(reader.rowIndex(), reader.scalar("a").getInt());
assertTrue(Arrays.equals(value, reader.scalar("b").getBytes()));
if (reader.rowIndex() < blankAfter) {
assertEquals(3, cReader.size());
for (int i = 0; i < 3; i++) {
assertEquals(reader.rowIndex() * 3 + i, cReader.getInt(i));
}
} else {
assertEquals(0, cReader.size());
}
}
result.clear();
rsLoader.close();
}
use of org.apache.drill.exec.vector.accessor.ScalarWriter in project drill by axbaretto.
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 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.
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()) {
if (rootWriter.rowCount() == 2952) {
count = count + 0;
}
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
RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(count - 1, result.rowCount());
RowSetReader reader = result.reader();
ScalarElementReader aReader = reader.array("a").elements();
ScalarElementReader bReader = reader.array("b").elements();
ScalarElementReader cReader = reader.array("c").elements();
ScalarElementReader dReader = reader.array("d").elements();
while (reader.next()) {
int rowId = reader.rowIndex();
assertEquals(aCount, aReader.size());
for (int i = 0; i < aCount; i++) {
assertEquals(rowId * aCount + i, aReader.getInt(i));
}
assertEquals(bCount, bReader.size());
for (int i = 0; i < bCount; i++) {
String cellValue = strValue + (rowId * bCount + i);
assertEquals(cellValue, bReader.getString(i));
}
if (rowId < cCutoff) {
assertEquals(cCount, cReader.size());
for (int i = 0; i < cCount; i++) {
assertEquals(rowId * cCount + i, cReader.getInt(i));
}
assertEquals(dCount, dReader.size());
for (int i = 0; i < dCount; i++) {
assertEquals(rowId * dCount + i, dReader.getInt(i));
}
} else {
assertEquals(0, cReader.size());
assertEquals(0, dReader.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++;
}
result = fixture.wrap(rsLoader.harvest());
assertEquals(6, result.rowCount());
reader = result.reader();
aReader = reader.array("a").elements();
bReader = reader.array("b").elements();
cReader = reader.array("c").elements();
dReader = reader.array("d").elements();
int j = 0;
while (reader.next()) {
int rowId = firstCount + reader.rowIndex();
assertEquals(aCount, aReader.size());
for (int i = 0; i < aCount; i++) {
assertEquals("Index " + i, rowId * aCount + i, aReader.getInt(i));
}
assertEquals(bCount, bReader.size());
for (int i = 0; i < bCount; i++) {
String cellValue = strValue + (rowId * bCount + i);
assertEquals(cellValue, bReader.getString(i));
}
if (j > 4) {
assertEquals(cCount, cReader.size());
for (int i = 0; i < cCount; i++) {
assertEquals(rowId * cCount + i, cReader.getInt(i));
}
} else {
assertEquals(0, cReader.size());
}
if (j == 0 || j > 4) {
assertEquals(dCount, dReader.size());
for (int i = 0; i < dCount; i++) {
assertEquals(rowId * dCount + i, dReader.getInt(i));
}
} else {
assertEquals(0, dReader.size());
}
j++;
}
result.clear();
rsLoader.close();
}
use of org.apache.drill.exec.vector.accessor.ScalarWriter in project drill by axbaretto.
the class TestResultSetSchemaChange method testSchemaChangeFirstBatch.
/**
* Test the case where the schema changes in the first batch.
* Schema changes before the first record are trivial and tested
* elsewhere. Here we write some records, then add new columns, as a
* JSON reader might do.
*/
@Test
public void testSchemaChangeFirstBatch() {
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator());
RowSetLoader rootWriter = rsLoader.writer();
rootWriter.addColumn(SchemaBuilder.columnSchema("a", MinorType.VARCHAR, DataMode.REQUIRED));
// Create initial rows
rsLoader.startBatch();
int rowCount = 0;
for (int i = 0; i < 2; i++) {
rootWriter.start();
rowCount++;
rootWriter.scalar(0).setString("a_" + rowCount);
rootWriter.save();
}
// Add a second column: nullable.
rootWriter.addColumn(SchemaBuilder.columnSchema("b", MinorType.INT, DataMode.OPTIONAL));
for (int i = 0; i < 2; i++) {
rootWriter.start();
rowCount++;
rootWriter.scalar(0).setString("a_" + rowCount);
rootWriter.scalar(1).setInt(rowCount);
rootWriter.save();
}
// Add a third column. Use variable-width so that offset
// vectors must be back-filled.
rootWriter.addColumn(SchemaBuilder.columnSchema("c", MinorType.VARCHAR, DataMode.OPTIONAL));
for (int i = 0; i < 2; i++) {
rootWriter.start();
rowCount++;
rootWriter.scalar(0).setString("a_" + rowCount);
rootWriter.scalar(1).setInt(rowCount);
rootWriter.scalar(2).setString("c_" + rowCount);
rootWriter.save();
}
// Fourth: Required Varchar. Previous rows are back-filled with empty strings.
// And a required int. Back-filled with zeros.
// May occasionally be useful. But, does have to work to prevent
// vector corruption if some reader decides to go this route.
rootWriter.addColumn(SchemaBuilder.columnSchema("d", MinorType.VARCHAR, DataMode.REQUIRED));
rootWriter.addColumn(SchemaBuilder.columnSchema("e", MinorType.INT, DataMode.REQUIRED));
for (int i = 0; i < 2; i++) {
rootWriter.start();
rowCount++;
rootWriter.scalar(0).setString("a_" + rowCount);
rootWriter.scalar(1).setInt(rowCount);
rootWriter.scalar(2).setString("c_" + rowCount);
rootWriter.scalar(3).setString("d_" + rowCount);
rootWriter.scalar(4).setInt(rowCount * 10);
rootWriter.save();
}
// Add an array. Now two offset vectors must be back-filled.
rootWriter.addColumn(SchemaBuilder.columnSchema("f", MinorType.VARCHAR, DataMode.REPEATED));
for (int i = 0; i < 2; i++) {
rootWriter.start();
rowCount++;
rootWriter.scalar(0).setString("a_" + rowCount);
rootWriter.scalar(1).setInt(rowCount);
rootWriter.scalar(2).setString("c_" + rowCount);
rootWriter.scalar(3).setString("d_" + rowCount);
rootWriter.scalar(4).setInt(rowCount * 10);
ScalarWriter arrayWriter = rootWriter.column(5).array().scalar();
arrayWriter.setString("f_" + rowCount + "-1");
arrayWriter.setString("f_" + rowCount + "-2");
rootWriter.save();
}
// Harvest the batch and verify.
RowSet actual = fixture.wrap(rsLoader.harvest());
BatchSchema expectedSchema = new SchemaBuilder().add("a", MinorType.VARCHAR).addNullable("b", MinorType.INT).addNullable("c", MinorType.VARCHAR).add("d", MinorType.VARCHAR).add("e", MinorType.INT).addArray("f", MinorType.VARCHAR).build();
SingleRowSet expected = fixture.rowSetBuilder(expectedSchema).addRow("a_1", null, null, "", 0, strArray()).addRow("a_2", null, null, "", 0, strArray()).addRow("a_3", 3, null, "", 0, strArray()).addRow("a_4", 4, null, "", 0, strArray()).addRow("a_5", 5, "c_5", "", 0, strArray()).addRow("a_6", 6, "c_6", "", 0, strArray()).addRow("a_7", 7, "c_7", "d_7", 70, strArray()).addRow("a_8", 8, "c_8", "d_8", 80, strArray()).addRow("a_9", 9, "c_9", "d_9", 90, strArray("f_9-1", "f_9-2")).addRow("a_10", 10, "c_10", "d_10", 100, strArray("f_10-1", "f_10-2")).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
rsLoader.close();
}
Aggregations