use of org.apache.drill.test.rowSet.RowSet.SingleRowSet in project drill by axbaretto.
the class TestResultSetLoaderMaps method testMapAddition.
/**
* Test adding a map to a loader after writing the first row.
*/
@Test
public void testMapAddition() {
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);
// Add a column to the map with the same name as the top-level column.
// Verifies that the name spaces are independent.
mapWriter.addColumn(SchemaBuilder.columnSchema("a", MinorType.VARCHAR, DataMode.REQUIRED));
rootWriter.addRow(20, objArray("fred")).addRow(30, objArray("barney"));
RowSet actual = fixture.wrap(rsLoader.harvest());
assertEquals(3, rsLoader.schemaVersion());
assertEquals(3, actual.rowCount());
// Validate first batch
TupleMetadata expectedSchema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").add("a", MinorType.VARCHAR).resumeSchema().buildSchema();
SingleRowSet expected = fixture.rowSetBuilder(expectedSchema).addRow(10, objArray("")).addRow(20, objArray("fred")).addRow(30, objArray("barney")).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
rsLoader.close();
}
use of org.apache.drill.test.rowSet.RowSet.SingleRowSet in project drill by axbaretto.
the class TestResultSetLoaderMaps method testMapWithArray.
/**
* Test a map that contains a scalar array. No reason to suspect that this
* will have problem as the array writer is fully tested in the accessor
* subsystem. Still, need to test the cardinality methods of the loader
* layer.
*/
@Test
public void testMapWithArray() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").addArray("c", MinorType.INT).addArray("d", MinorType.VARCHAR).resumeSchema().buildSchema();
ResultSetLoaderImpl.ResultSetOptions options = new OptionBuilder().setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
// Write some rows
rsLoader.startBatch();
rootWriter.addRow(10, objArray(intArray(110, 120, 130), strArray("d1.1", "d1.2", "d1.3", "d1.4"))).addRow(20, objArray(intArray(210), strArray())).addRow(30, objArray(intArray(), strArray("d3.1")));
// Validate first batch
RowSet actual = fixture.wrap(rsLoader.harvest());
SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, objArray(intArray(110, 120, 130), strArray("d1.1", "d1.2", "d1.3", "d1.4"))).addRow(20, objArray(intArray(210), strArray())).addRow(30, objArray(intArray(), strArray("d3.1"))).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
// Add another array after the first row in the second batch.
rsLoader.startBatch();
rootWriter.addRow(40, objArray(intArray(410, 420), strArray("d4.1", "d4.2"))).addRow(50, objArray(intArray(510), strArray("d5.1")));
TupleWriter mapWriter = rootWriter.tuple("m");
mapWriter.addColumn(SchemaBuilder.columnSchema("e", MinorType.VARCHAR, DataMode.REPEATED));
rootWriter.addRow(60, objArray(intArray(610, 620), strArray("d6.1", "d6.2"), strArray("e6.1", "e6.2"))).addRow(70, objArray(intArray(710), strArray(), strArray("e7.1", "e7.2")));
// Validate first batch. The new array should have been back-filled with
// empty offsets for the missing rows.
actual = fixture.wrap(rsLoader.harvest());
// System.out.println(actual.schema().toString());
expected = fixture.rowSetBuilder(actual.schema()).addRow(40, objArray(intArray(410, 420), strArray("d4.1", "d4.2"), strArray())).addRow(50, objArray(intArray(510), strArray("d5.1"), strArray())).addRow(60, objArray(intArray(610, 620), strArray("d6.1", "d6.2"), strArray("e6.1", "e6.2"))).addRow(70, objArray(intArray(710), strArray(), strArray("e7.1", "e7.2"))).build();
// expected.print();
new RowSetComparison(expected).verifyAndClearAll(actual);
rsLoader.close();
}
use of org.apache.drill.test.rowSet.RowSet.SingleRowSet 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.test.rowSet.RowSet.SingleRowSet in project drill by axbaretto.
the class TestResultSetLoaderOmittedValues method testSkipRows.
/**
* Test that omitting the call to saveRow() effectively discards
* the row. Note that the vectors still contain values in the
* discarded position; just the various pointers are unset. If
* the batch ends before the discarded values are overwritten, the
* discarded values just exist at the end of the vector. Since vectors
* start with garbage contents, the discarded values are simply a different
* kind of garbage. But, if the client writes a new row, then the new
* row overwrites the discarded row. This works because we only change
* the tail part of a vector; never the internals.
*/
@Test
public void testSkipRows() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).buildSchema();
ResultSetLoaderImpl.ResultSetOptions options = new OptionBuilder().setRowCountLimit(ValueVector.MAX_ROW_COUNT).setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
rsLoader.startBatch();
int rowNumber = 0;
for (int i = 0; i < 14; i++) {
rootWriter.start();
rowNumber++;
rootWriter.scalar(0).setInt(rowNumber);
if (i % 3 == 0) {
rootWriter.scalar(1).setNull();
} else {
rootWriter.scalar(1).setString("b-" + rowNumber);
}
if (i % 2 == 0) {
rootWriter.save();
}
}
RowSet result = fixture.wrap(rsLoader.harvest());
// result.print();
SingleRowSet expected = fixture.rowSetBuilder(result.batchSchema()).addRow(1, null).addRow(3, "b-3").addRow(5, "b-5").addRow(7, null).addRow(9, "b-9").addRow(11, "b-11").addRow(13, null).build();
// expected.print();
new RowSetComparison(expected).verifyAndClearAll(result);
rsLoader.close();
}
use of org.apache.drill.test.rowSet.RowSet.SingleRowSet in project drill by axbaretto.
the class TestResultSetLoaderProjection method testMapProjection.
@Test
public void testMapProjection() {
List<SchemaPath> selection = Lists.newArrayList(SchemaPath.getSimplePath("m1"), SchemaPath.getCompoundPath("m2", "d"));
TupleMetadata schema = new SchemaBuilder().addMap("m1").add("a", MinorType.INT).add("b", MinorType.INT).resumeSchema().addMap("m2").add("c", MinorType.INT).add("d", MinorType.INT).resumeSchema().addMap("m3").add("e", MinorType.INT).add("f", MinorType.INT).resumeSchema().buildSchema();
ResultSetOptions options = new OptionBuilder().setProjection(selection).setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
// Verify the projected columns
TupleMetadata actualSchema = rootWriter.schema();
ColumnMetadata m1Md = actualSchema.metadata("m1");
assertTrue(m1Md.isMap());
assertTrue(m1Md.isProjected());
assertEquals(2, m1Md.mapSchema().size());
assertTrue(m1Md.mapSchema().metadata("a").isProjected());
assertTrue(m1Md.mapSchema().metadata("b").isProjected());
ColumnMetadata m2Md = actualSchema.metadata("m2");
assertTrue(m2Md.isMap());
assertTrue(m2Md.isProjected());
assertEquals(2, m2Md.mapSchema().size());
assertFalse(m2Md.mapSchema().metadata("c").isProjected());
assertTrue(m2Md.mapSchema().metadata("d").isProjected());
ColumnMetadata m3Md = actualSchema.metadata("m3");
assertTrue(m3Md.isMap());
assertFalse(m3Md.isProjected());
assertEquals(2, m3Md.mapSchema().size());
assertFalse(m3Md.mapSchema().metadata("e").isProjected());
assertFalse(m3Md.mapSchema().metadata("f").isProjected());
// Write a couple of rows.
rsLoader.startBatch();
rootWriter.start();
rootWriter.tuple("m1").scalar("a").setInt(1);
rootWriter.tuple("m1").scalar("b").setInt(2);
rootWriter.tuple("m2").scalar("c").setInt(3);
rootWriter.tuple("m2").scalar("d").setInt(4);
rootWriter.tuple("m3").scalar("e").setInt(5);
rootWriter.tuple("m3").scalar("f").setInt(6);
rootWriter.save();
rootWriter.start();
rootWriter.tuple("m1").scalar("a").setInt(11);
rootWriter.tuple("m1").scalar("b").setInt(12);
rootWriter.tuple("m2").scalar("c").setInt(13);
rootWriter.tuple("m2").scalar("d").setInt(14);
rootWriter.tuple("m3").scalar("e").setInt(15);
rootWriter.tuple("m3").scalar("f").setInt(16);
rootWriter.save();
// Verify. Only the projected columns appear in the result set.
BatchSchema expectedSchema = new SchemaBuilder().addMap("m1").add("a", MinorType.INT).add("b", MinorType.INT).resumeSchema().addMap("m2").add("d", MinorType.INT).resumeSchema().build();
SingleRowSet expected = fixture.rowSetBuilder(expectedSchema).addRow(objArray(1, 2), objArray(4)).addRow(objArray(11, 12), objArray(14)).build();
new RowSetComparison(expected).verifyAndClearAll(fixture.wrap(rsLoader.harvest()));
rsLoader.close();
}
Aggregations