use of org.apache.drill.exec.vector.accessor.TupleWriter 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.vector.accessor.TupleWriter in project drill by apache.
the class TestResultSetLoaderMaps method testNestedMapsRequired.
/**
* Create nested maps. Then, add columns to each map
* on the fly. Use required, variable-width columns since
* those require the most processing and are most likely to
* fail if anything is out of place.
*/
@Test
public void testNestedMapsRequired() {
final TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMap("m1").add("b", MinorType.VARCHAR).addMap("m2").add("c", MinorType.VARCHAR).resumeMap().resumeSchema().buildSchema();
final ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).build();
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
assertEquals(5, rsLoader.schemaVersion());
final RowSetLoader rootWriter = rsLoader.writer();
rsLoader.startBatch();
rootWriter.addRow(10, mapValue("b1", mapValue("c1")));
// Validate first batch
RowSet actual = fixture.wrap(rsLoader.harvest());
assertEquals(5, rsLoader.schemaVersion());
SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, mapValue("b1", mapValue("c1"))).build();
RowSetUtilities.verify(expected, actual);
// Now add columns in the second batch.
rsLoader.startBatch();
rootWriter.addRow(20, mapValue("b2", mapValue("c2")));
final TupleWriter m1Writer = rootWriter.tuple("m1");
m1Writer.addColumn(SchemaBuilder.columnSchema("d", MinorType.VARCHAR, DataMode.REQUIRED));
final TupleWriter m2Writer = m1Writer.tuple("m2");
m2Writer.addColumn(SchemaBuilder.columnSchema("e", MinorType.VARCHAR, DataMode.REQUIRED));
rootWriter.addRow(30, mapValue("b3", mapValue("c3", "e3"), "d3"));
// And another set while the write proceeds.
m1Writer.addColumn(SchemaBuilder.columnSchema("f", MinorType.VARCHAR, DataMode.REQUIRED));
m2Writer.addColumn(SchemaBuilder.columnSchema("g", MinorType.VARCHAR, DataMode.REQUIRED));
rootWriter.addRow(40, mapValue("b4", mapValue("c4", "e4", "g4"), "d4", "e4"));
// Validate second batch
actual = fixture.wrap(rsLoader.harvest());
assertEquals(9, rsLoader.schemaVersion());
final TupleMetadata expectedSchema = new SchemaBuilder().add("a", MinorType.INT).addMap("m1").add("b", MinorType.VARCHAR).addMap("m2").add("c", MinorType.VARCHAR).add("e", MinorType.VARCHAR).add("g", MinorType.VARCHAR).resumeMap().add("d", MinorType.VARCHAR).add("f", MinorType.VARCHAR).resumeSchema().buildSchema();
expected = fixture.rowSetBuilder(expectedSchema).addRow(20, mapValue("b2", mapValue("c2", "", ""), "", "")).addRow(30, mapValue("b3", mapValue("c3", "e3", ""), "d3", "")).addRow(40, mapValue("b4", mapValue("c4", "e4", "g4"), "d4", "e4")).build();
RowSetUtilities.verify(expected, actual);
rsLoader.close();
}
use of org.apache.drill.exec.vector.accessor.TupleWriter in project drill by apache.
the class TestResultSetLoaderMaps method testBasics.
@Test
public void testBasics() {
final TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").add("c", MinorType.INT).add("d", MinorType.VARCHAR).resumeSchema().add("e", MinorType.VARCHAR).buildSchema();
final ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).build();
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
assertFalse(rsLoader.isProjectionEmpty());
final RowSetLoader rootWriter = rsLoader.writer();
// Verify structure and schema
assertEquals(5, rsLoader.schemaVersion());
final TupleMetadata actualSchema = rootWriter.tupleSchema();
assertEquals(3, actualSchema.size());
assertTrue(actualSchema.metadata(1).isMap());
assertEquals(2, actualSchema.metadata("m").tupleSchema().size());
assertEquals(2, actualSchema.column("m").getChildren().size());
rsLoader.startBatch();
// Write a row the way that clients will do.
final ScalarWriter aWriter = rootWriter.scalar("a");
final TupleWriter mWriter = rootWriter.tuple("m");
final ScalarWriter cWriter = mWriter.scalar("c");
final ScalarWriter dWriter = mWriter.scalar("d");
final ScalarWriter eWriter = rootWriter.scalar("e");
rootWriter.start();
aWriter.setInt(10);
cWriter.setInt(110);
dWriter.setString("fred");
eWriter.setString("pebbles");
rootWriter.save();
// Try adding a duplicate column.
try {
mWriter.addColumn(SchemaBuilder.columnSchema("c", MinorType.INT, DataMode.OPTIONAL));
fail();
} catch (final UserException e) {
// Expected
}
// Write another using the test-time conveniences
rootWriter.addRow(20, mapValue(210, "barney"), "bam-bam");
// Harvest the batch
final RowSet actual = fixture.wrap(rsLoader.harvest());
assertEquals(5, rsLoader.schemaVersion());
assertEquals(2, actual.rowCount());
final MapVector mapVector = (MapVector) actual.container().getValueVector(1).getValueVector();
assertEquals(2, mapVector.getAccessor().getValueCount());
// Validate data
final SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, mapValue(110, "fred"), "pebbles").addRow(20, mapValue(210, "barney"), "bam-bam").build();
RowSetUtilities.verify(expected, actual);
rsLoader.close();
}
use of org.apache.drill.exec.vector.accessor.TupleWriter 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();
}
use of org.apache.drill.exec.vector.accessor.TupleWriter in project drill by apache.
the class TestResultSetLoaderMaps method testMapAddition.
/**
* Test adding a map to a loader after writing the first row.
*/
@Test
public void testMapAddition() {
final TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).buildSchema();
final ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).build();
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
assertEquals(1, rsLoader.schemaVersion());
final RowSetLoader rootWriter = rsLoader.writer();
// Start without the map. Add a map after the first row.
rsLoader.startBatch();
rootWriter.addRow(10);
final int mapIndex = rootWriter.addColumn(SchemaBuilder.columnSchema("m", MinorType.MAP, DataMode.REQUIRED));
final TupleWriter mapWriter = rootWriter.tuple(mapIndex);
// Add a column to the map with the same name as the top-level column.
// Verifies that the name spaces are independent.
final int colIndex = mapWriter.addColumn(SchemaBuilder.columnSchema("a", MinorType.VARCHAR, DataMode.REQUIRED));
assertEquals(0, colIndex);
// Ensure metadata was added
assertTrue(mapWriter.tupleSchema().size() == 1);
assertSame(mapWriter.tupleSchema(), mapWriter.schema().tupleSchema());
assertSame(mapWriter.tupleSchema().metadata(colIndex), mapWriter.scalar(colIndex).schema());
rootWriter.addRow(20, mapValue("fred")).addRow(30, mapValue("barney"));
final RowSet actual = fixture.wrap(rsLoader.harvest());
assertEquals(3, rsLoader.schemaVersion());
assertEquals(3, actual.rowCount());
final MapVector mapVector = (MapVector) actual.container().getValueVector(1).getValueVector();
final MaterializedField mapField = mapVector.getField();
assertEquals(1, mapField.getChildren().size());
assertTrue(mapWriter.scalar(colIndex).schema().schema().isEquivalent(mapField.getChildren().iterator().next()));
// Validate first batch
final TupleMetadata expectedSchema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").add("a", MinorType.VARCHAR).resumeSchema().buildSchema();
final SingleRowSet expected = fixture.rowSetBuilder(expectedSchema).addRow(10, mapValue("")).addRow(20, mapValue("fred")).addRow(30, mapValue("barney")).build();
RowSetUtilities.verify(expected, actual);
rsLoader.close();
}
Aggregations