use of org.apache.drill.exec.vector.accessor.TupleWriter in project drill by axbaretto.
the class RowSetTest method testRepeatedMapStructure.
@Test
public void testRepeatedMapStructure() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMapArray("m").add("b", MinorType.INT).add("c", MinorType.INT).resumeSchema().buildSchema();
ExtendableRowSet rowSet = fixture.rowSet(schema);
RowSetWriter writer = rowSet.writer();
// Map and Int
// Pick out components and lightly test. (Assumes structure
// tested earlier is still valid, so no need to exhaustively
// test again.)
assertEquals(ObjectType.SCALAR, writer.column("a").type());
assertEquals(ObjectType.ARRAY, writer.column("m").type());
ArrayWriter maWriter = writer.column(1).array();
assertEquals(ObjectType.TUPLE, maWriter.entryType());
TupleWriter mapWriter = maWriter.tuple();
assertEquals(ObjectType.SCALAR, mapWriter.column("b").type());
assertEquals(ObjectType.SCALAR, mapWriter.column("c").type());
ScalarWriter aWriter = writer.column("a").scalar();
ScalarWriter bWriter = mapWriter.scalar("b");
ScalarWriter cWriter = mapWriter.scalar("c");
assertEquals(ValueType.INTEGER, aWriter.valueType());
assertEquals(ValueType.INTEGER, bWriter.valueType());
assertEquals(ValueType.INTEGER, cWriter.valueType());
// Write data
aWriter.setInt(10);
bWriter.setInt(101);
cWriter.setInt(102);
// Advance to next array position
maWriter.save();
bWriter.setInt(111);
cWriter.setInt(112);
maWriter.save();
writer.save();
aWriter.setInt(20);
bWriter.setInt(201);
cWriter.setInt(202);
maWriter.save();
bWriter.setInt(211);
cWriter.setInt(212);
maWriter.save();
writer.save();
aWriter.setInt(30);
bWriter.setInt(301);
cWriter.setInt(302);
maWriter.save();
bWriter.setInt(311);
cWriter.setInt(312);
maWriter.save();
writer.save();
// Finish the row set and get a reader.
SingleRowSet actual = writer.done();
RowSetReader reader = actual.reader();
// Verify reader structure
assertEquals(ObjectType.SCALAR, reader.column("a").type());
assertEquals(ObjectType.ARRAY, reader.column("m").type());
ArrayReader maReader = reader.column(1).array();
assertEquals(ObjectType.TUPLE, maReader.entryType());
TupleReader mapReader = maReader.tuple();
assertEquals(ObjectType.SCALAR, mapReader.column("b").type());
assertEquals(ObjectType.SCALAR, mapReader.column("c").type());
ScalarReader aReader = reader.column("a").scalar();
ScalarReader bReader = mapReader.scalar("b");
ScalarReader cReader = mapReader.scalar("c");
assertEquals(ValueType.INTEGER, aReader.valueType());
assertEquals(ValueType.INTEGER, bReader.valueType());
assertEquals(ValueType.INTEGER, cReader.valueType());
// Row 1: use index accessors
assertTrue(reader.next());
assertEquals(10, aReader.getInt());
TupleReader ixReader = maReader.tuple(0);
assertEquals(101, ixReader.scalar(0).getInt());
assertEquals(102, ixReader.scalar(1).getInt());
ixReader = maReader.tuple(1);
assertEquals(111, ixReader.scalar(0).getInt());
assertEquals(112, ixReader.scalar(1).getInt());
// Row 2: use common accessor with explicit positioning,
// but access scalars through the map reader.
assertTrue(reader.next());
assertEquals(20, aReader.getInt());
maReader.setPosn(0);
assertEquals(201, mapReader.scalar(0).getInt());
assertEquals(202, mapReader.scalar(1).getInt());
maReader.setPosn(1);
assertEquals(211, mapReader.scalar(0).getInt());
assertEquals(212, mapReader.scalar(1).getInt());
// Row 3: use common accessor for scalars
assertTrue(reader.next());
assertEquals(30, aReader.getInt());
maReader.setPosn(0);
assertEquals(301, bReader.getInt());
assertEquals(302, cReader.getInt());
maReader.setPosn(1);
assertEquals(311, bReader.getInt());
assertEquals(312, cReader.getInt());
assertFalse(reader.next());
// Verify that the map accessor's value count was set.
@SuppressWarnings("resource") RepeatedMapVector mapVector = (RepeatedMapVector) actual.container().getValueVector(1).getValueVector();
assertEquals(3, mapVector.getAccessor().getValueCount());
// Verify the readers and writers again using the testing tools.
SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, objArray(objArray(101, 102), objArray(111, 112))).addRow(20, objArray(objArray(201, 202), objArray(211, 212))).addRow(30, objArray(objArray(301, 302), objArray(311, 312))).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
}
use of org.apache.drill.exec.vector.accessor.TupleWriter in project drill by apache.
the class TestResultSetLoaderUnions method testUnionAddTypes.
@Test
public void testUnionAddTypes() {
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator());
final RowSetLoader writer = rsLoader.writer();
rsLoader.startBatch();
// First row, (1, "first"), create types as we go.
writer.start();
writer.addColumn(SchemaBuilder.columnSchema("id", MinorType.INT, DataMode.REQUIRED));
writer.scalar("id").setInt(1);
writer.addColumn(SchemaBuilder.columnSchema("u", MinorType.UNION, DataMode.OPTIONAL));
final VariantWriter variant = writer.column("u").variant();
variant.member(MinorType.VARCHAR).scalar().setString("first");
writer.save();
// Second row, (2, {20, "fred"}), create types as we go.
writer.start();
writer.scalar("id").setInt(2);
final TupleWriter innerMap = variant.member(MinorType.MAP).tuple();
innerMap.addColumn(SchemaBuilder.columnSchema("a", MinorType.INT, DataMode.OPTIONAL));
innerMap.scalar("a").setInt(20);
innerMap.addColumn(SchemaBuilder.columnSchema("b", MinorType.VARCHAR, DataMode.OPTIONAL));
innerMap.scalar("b").setString("fred");
writer.save();
// Write remaining rows using convenient methods, using
// schema defined above.
writer.addRow(3, null).addRow(4, mapValue(40, null)).addRow(5, "last");
// Verify the values.
// (Relies on the row set level union tests having passed.)
final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addUnion("u").addType(MinorType.VARCHAR).addMap().addNullable("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).resumeUnion().resumeSchema().buildSchema();
final SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(1, "first").addRow(2, mapValue(20, "fred")).addRow(3, null).addRow(4, mapValue(40, null)).addRow(5, "last").build();
final RowSet result = fixture.wrap(rsLoader.harvest());
RowSetUtilities.verify(expected, result);
}
use of org.apache.drill.exec.vector.accessor.TupleWriter in project drill by apache.
the class TestResultSetLoaderUnions method testVariantListWithMap.
/**
* Dynamically add a map to a list that also contains scalars.
* Assumes that {@link #testVariantListDynamic()} passed.
*/
@Test
public void testVariantListWithMap() {
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator());
final RowSetLoader writer = rsLoader.writer();
rsLoader.startBatch();
writer.addColumn(MaterializedField.create("id", Types.required(MinorType.INT)));
writer.addColumn(MaterializedField.create("list", Types.optional(MinorType.LIST)));
final ArrayWriter arrWriter = writer.array("list");
final VariantWriter variant = arrWriter.variant();
// Add a null list, or a list of nulls.
writer.addRow(1, null).addRow(2, variantArray()).addRow(3, variantArray(null, null));
// Add a String. Now we can create a list of strings and/or nulls.
variant.addMember(MinorType.VARCHAR);
writer.addRow(4, variantArray("fred", null, "barney"));
// Add a map
final TupleWriter mapWriter = variant.addMember(MinorType.MAP).tuple();
mapWriter.addColumn(MetadataUtils.newScalar("first", Types.optional(MinorType.VARCHAR)));
mapWriter.addColumn(MetadataUtils.newScalar("last", Types.optional(MinorType.VARCHAR)));
// Add a map-based record
writer.addRow(5, variantArray(mapValue("wilma", "flintstone"), mapValue("betty", "rubble")));
// Verify
final RowSet result = fixture.wrap(rsLoader.harvest());
final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addList("list").addType(MinorType.VARCHAR).addMap().addNullable("first", MinorType.VARCHAR).addNullable("last", MinorType.VARCHAR).resumeUnion().resumeSchema().buildSchema();
final SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(1, null).addRow(2, variantArray()).addRow(3, variantArray(null, null)).addRow(4, variantArray("fred", null, "barney")).addRow(5, variantArray(mapValue("wilma", "flintstone"), mapValue("betty", "rubble"))).build();
RowSetUtilities.verify(expected, result);
}
use of org.apache.drill.exec.vector.accessor.TupleWriter in project drill by apache.
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 ResultSetOptionBuilder().readerSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
// Verify structure and schema
TupleMetadata actualSchema = rootWriter.tupleSchema();
assertEquals(2, actualSchema.size());
assertTrue(actualSchema.metadata(1).isArray());
assertTrue(actualSchema.metadata(1).isMap());
assertEquals(2, actualSchema.metadata("m").tupleSchema().size());
assertEquals(2, actualSchema.column("m").getChildren().size());
TupleWriter mapWriter = rootWriter.array("m").tuple();
assertSame(actualSchema.metadata("m").tupleSchema(), mapWriter.schema().tupleSchema());
assertSame(mapWriter.tupleSchema(), mapWriter.schema().tupleSchema());
assertSame(mapWriter.tupleSchema().metadata(0), mapWriter.scalar(0).schema());
assertSame(mapWriter.tupleSchema().metadata(1), mapWriter.scalar(1).schema());
// Write a couple of rows with arrays.
rsLoader.startBatch();
rootWriter.addRow(10, mapArray(mapValue(110, "d1.1"), mapValue(120, "d2.2"))).addRow(20, mapArray()).addRow(30, mapArray(mapValue(310, "d3.1"), mapValue(320, "d3.2"), mapValue(330, "d3.3")));
// Verify the first batch
RowSet actual = fixture.wrap(rsLoader.harvest());
RepeatedMapVector mapVector = (RepeatedMapVector) actual.container().getValueVector(1).getValueVector();
MaterializedField mapField = mapVector.getField();
assertEquals(2, mapField.getChildren().size());
Iterator<MaterializedField> iter = mapField.getChildren().iterator();
assertTrue(mapWriter.scalar(0).schema().schema().isEquivalent(iter.next()));
assertTrue(mapWriter.scalar(1).schema().schema().isEquivalent(iter.next()));
SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, mapArray(mapValue(110, "d1.1"), mapValue(120, "d2.2"))).addRow(20, mapArray()).addRow(30, mapArray(mapValue(310, "d3.1"), mapValue(320, "d3.2"), mapValue(330, "d3.3"))).build();
RowSetUtilities.verify(expected, 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, mapArray(mapValue(410, "d4.1"), mapValue(420, "d4.2")));
mapWriter.addColumn(SchemaBuilder.columnSchema("e", MinorType.VARCHAR, DataMode.OPTIONAL));
rootWriter.addRow(50, mapArray(mapValue(510, "d5.1", "e5.1"), mapValue(520, "d5.2", null))).addRow(60, mapArray(mapValue(610, "d6.1", "e6.1"), mapValue(620, "d6.2", null), mapValue(630, "d6.3", "e6.3")));
// Verify the second batch
actual = fixture.wrap(rsLoader.harvest());
mapVector = (RepeatedMapVector) actual.container().getValueVector(1).getValueVector();
mapField = mapVector.getField();
assertEquals(3, mapField.getChildren().size());
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, mapArray(mapValue(410, "d4.1", null), mapValue(420, "d4.2", null))).addRow(50, mapArray(mapValue(510, "d5.1", "e5.1"), mapValue(520, "d5.2", null))).addRow(60, mapArray(mapValue(610, "d6.1", "e6.1"), mapValue(620, "d6.2", null), mapValue(630, "d6.3", "e6.3"))).build();
RowSetUtilities.verify(expected, actual);
rsLoader.close();
}
use of org.apache.drill.exec.vector.accessor.TupleWriter 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();
}
Aggregations