use of org.apache.drill.exec.vector.accessor.TupleWriter in project drill by axbaretto.
the class TestResultSetLoaderMapArray method testBasics.
@Test
public void testBasics() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMapArray("m").add("c", MinorType.INT).add("d", MinorType.VARCHAR).resumeSchema().buildSchema();
ResultSetLoaderImpl.ResultSetOptions options = new OptionBuilder().setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
// Verify structure and schema
TupleMetadata actualSchema = rootWriter.schema();
assertEquals(2, actualSchema.size());
assertTrue(actualSchema.metadata(1).isArray());
assertTrue(actualSchema.metadata(1).isMap());
assertEquals(2, actualSchema.metadata("m").mapSchema().size());
assertEquals(2, actualSchema.column("m").getChildren().size());
// Write a couple of rows with arrays.
rsLoader.startBatch();
rootWriter.addRow(10, objArray(objArray(110, "d1.1"), objArray(120, "d2.2"))).addRow(20, objArray()).addRow(30, objArray(objArray(310, "d3.1"), objArray(320, "d3.2"), objArray(330, "d3.3")));
// Verify the first batch
RowSet actual = fixture.wrap(rsLoader.harvest());
SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, objArray(objArray(110, "d1.1"), objArray(120, "d2.2"))).addRow(20, objArray()).addRow(30, objArray(objArray(310, "d3.1"), objArray(320, "d3.2"), objArray(330, "d3.3"))).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
// In the second, create a row, then add a map member.
// Should be back-filled to empty for the first row.
rsLoader.startBatch();
rootWriter.addRow(40, objArray(objArray(410, "d4.1"), objArray(420, "d4.2")));
TupleWriter mapWriter = rootWriter.array("m").tuple();
mapWriter.addColumn(SchemaBuilder.columnSchema("e", MinorType.VARCHAR, DataMode.OPTIONAL));
rootWriter.addRow(50, objArray(objArray(510, "d5.1", "e5.1"), objArray(520, "d5.2", null))).addRow(60, objArray(objArray(610, "d6.1", "e6.1"), objArray(620, "d6.2", null), objArray(630, "d6.3", "e6.3")));
// Verify the second batch
actual = fixture.wrap(rsLoader.harvest());
TupleMetadata expectedSchema = new SchemaBuilder().add("a", MinorType.INT).addMapArray("m").add("c", MinorType.INT).add("d", MinorType.VARCHAR).addNullable("e", MinorType.VARCHAR).resumeSchema().buildSchema();
expected = fixture.rowSetBuilder(expectedSchema).addRow(40, objArray(objArray(410, "d4.1", null), objArray(420, "d4.2", null))).addRow(50, objArray(objArray(510, "d5.1", "e5.1"), objArray(520, "d5.2", null))).addRow(60, objArray(objArray(610, "d6.1", "e6.1"), objArray(620, "d6.2", null), objArray(630, "d6.3", "e6.3"))).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
rsLoader.close();
}
use of org.apache.drill.exec.vector.accessor.TupleWriter in project drill by axbaretto.
the class TestResultSetLoaderMapArray method testOverwriteRow.
/**
* Version of the {#link TestResultSetLoaderProtocol#testOverwriteRow()} test
* that uses nested columns inside an array of maps. Here we must call
* <tt>start()</tt> to reset the array back to the initial start position after
* each "discard."
*/
@Test
public void testOverwriteRow() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMapArray("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");
ArrayWriter maWriter = rootWriter.array("m");
TupleWriter mWriter = maWriter.tuple();
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 < 10_000) {
rootWriter.start();
count++;
aWriter.setInt(count);
for (int i = 0; i < 10; i++) {
bWriter.setInt(count * 10 + i);
cWriter.setBytes(value, value.length);
maWriter.save();
}
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();
ArrayReader maReader = reader.array("m");
TupleReader mReader = maReader.tuple();
int rowId = 1;
while (reader.next()) {
assertEquals(rowId * 100, reader.scalar("a").getInt());
assertEquals(10, maReader.size());
for (int i = 0; i < 10; i++) {
maReader.setPosn(i);
assertEquals(rowId * 1000 + i, 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 axbaretto.
the class TestResultSetLoaderMaps method testEmptyMapAddition.
/**
* Test adding an empty map to a loader after writing the first row.
* Then add columns in another batch. Yes, this is a bizarre condition,
* but we must check it anyway for robustness.
*/
@Test
public void testEmptyMapAddition() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).buildSchema();
ResultSetLoaderImpl.ResultSetOptions options = new OptionBuilder().setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
assertEquals(1, rsLoader.schemaVersion());
RowSetLoader rootWriter = rsLoader.writer();
// Start without the map. Add a map after the first row.
rsLoader.startBatch();
rootWriter.addRow(10);
int mapIndex = rootWriter.addColumn(SchemaBuilder.columnSchema("m", MinorType.MAP, DataMode.REQUIRED));
TupleWriter mapWriter = rootWriter.tuple(mapIndex);
rootWriter.addRow(20, objArray()).addRow(30, objArray());
RowSet actual = fixture.wrap(rsLoader.harvest());
assertEquals(2, rsLoader.schemaVersion());
assertEquals(3, actual.rowCount());
// Validate first batch
TupleMetadata expectedSchema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").resumeSchema().buildSchema();
SingleRowSet expected = fixture.rowSetBuilder(expectedSchema).addRow(10, objArray()).addRow(20, objArray()).addRow(30, objArray()).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
// Now add another column to the map
rsLoader.startBatch();
mapWriter.addColumn(SchemaBuilder.columnSchema("a", MinorType.VARCHAR, DataMode.REQUIRED));
rootWriter.addRow(40, objArray("fred")).addRow(50, objArray("barney"));
actual = fixture.wrap(rsLoader.harvest());
assertEquals(3, rsLoader.schemaVersion());
assertEquals(2, actual.rowCount());
// Validate first batch
expectedSchema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").add("a", MinorType.VARCHAR).resumeSchema().buildSchema();
expected = fixture.rowSetBuilder(expectedSchema).addRow(40, objArray("fred")).addRow(50, objArray("barney")).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
rsLoader.close();
}
use of org.apache.drill.exec.vector.accessor.TupleWriter 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.exec.vector.accessor.TupleWriter in project drill by axbaretto.
the class RowSetTest method testMapStructure.
/**
* Test a simple map structure at the top level of a row.
*
* @throws VectorOverflowException should never occur
*/
@Test
public void testMapStructure() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").addArray("b", MinorType.INT).resumeSchema().buildSchema();
ExtendableRowSet rowSet = fixture.rowSet(schema);
RowSetWriter writer = rowSet.writer();
// Map and Int
// Test Invariants
assertEquals(ObjectType.SCALAR, writer.column("a").type());
assertEquals(ObjectType.SCALAR, writer.column(0).type());
assertEquals(ObjectType.TUPLE, writer.column("m").type());
assertEquals(ObjectType.TUPLE, writer.column(1).type());
assertSame(writer.column(1).tuple(), writer.tuple(1));
TupleWriter mapWriter = writer.column(1).tuple();
assertEquals(ObjectType.SCALAR, mapWriter.column("b").array().entry().type());
assertEquals(ObjectType.SCALAR, mapWriter.column("b").array().entryType());
ScalarWriter aWriter = writer.column("a").scalar();
ScalarWriter bWriter = writer.column("m").tuple().column("b").array().entry().scalar();
assertSame(bWriter, writer.tuple(1).array(0).scalar());
assertEquals(ValueType.INTEGER, bWriter.valueType());
try {
writer.column(1).scalar();
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
try {
writer.column(1).array();
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
// Write data
aWriter.setInt(10);
bWriter.setInt(11);
bWriter.setInt(12);
writer.save();
aWriter.setInt(20);
bWriter.setInt(21);
bWriter.setInt(22);
writer.save();
aWriter.setInt(30);
bWriter.setInt(31);
bWriter.setInt(32);
writer.save();
// Finish the row set and get a reader.
SingleRowSet actual = writer.done();
RowSetReader reader = actual.reader();
assertEquals(ObjectType.SCALAR, reader.column("a").type());
assertEquals(ObjectType.SCALAR, reader.column(0).type());
assertEquals(ObjectType.TUPLE, reader.column("m").type());
assertEquals(ObjectType.TUPLE, reader.column(1).type());
assertSame(reader.column(1).tuple(), reader.tuple(1));
ScalarReader aReader = reader.column(0).scalar();
TupleReader mReader = reader.column(1).tuple();
assertEquals(ObjectType.SCALAR, mReader.column("b").array().entryType());
ScalarElementReader bReader = mReader.column(0).elements();
assertEquals(ValueType.INTEGER, bReader.valueType());
assertTrue(reader.next());
assertEquals(10, aReader.getInt());
assertEquals(11, bReader.getInt(0));
assertEquals(12, bReader.getInt(1));
assertTrue(reader.next());
assertEquals(20, aReader.getInt());
assertEquals(21, bReader.getInt(0));
assertEquals(22, bReader.getInt(1));
assertTrue(reader.next());
assertEquals(30, aReader.getInt());
assertEquals(31, bReader.getInt(0));
assertEquals(32, bReader.getInt(1));
assertFalse(reader.next());
// Verify that the map accessor's value count was set.
@SuppressWarnings("resource") MapVector mapVector = (MapVector) actual.container().getValueVector(1).getValueVector();
assertEquals(actual.rowCount(), mapVector.getAccessor().getValueCount());
SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, objArray(intArray(11, 12))).addRow(20, objArray(intArray(21, 22))).addRow(30, objArray(intArray(31, 32))).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
}
Aggregations