use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.
the class BuildFromSchema method buildDict.
private ObjectWriter buildDict(ParentShim parent, ColumnMetadata colSchema) {
final ObjectWriter colWriter = parent.add(colSchema.cloneEmpty());
expandDict(colWriter, colSchema);
return colWriter;
}
use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.
the class BuildFromSchema method buildVariant.
/**
* Expand a variant column. We use the term "variant" to mean either
* a UNION, or a LIST of UNIONs. (A LIST of UNIONs is, conceptually,
* a repeated UNION, though it is far more complex.)
*
* @param parent shim object used to associate the UNION types with its
* parent column (which is a UNION or a LIST). Since UNION and LIST are
* far different types, the shim provides a facade that encapsulates
* the common behavior
* @param colSchema the schema of the variant (LIST or UNION) column
*/
private ObjectWriter buildVariant(ParentShim parent, ColumnMetadata colSchema) {
final ObjectWriter colWriter = parent.add(colSchema.cloneEmpty());
expandVariant(colWriter, colSchema);
return colWriter;
}
use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.
the class BuildFromSchema method buildSingleList.
private ObjectWriter buildSingleList(ParentShim parent, ColumnMetadata colSchema) {
final ColumnMetadata seed = colSchema.cloneEmpty();
final ColumnMetadata subtype = colSchema.variantSchema().listSubtype();
seed.variantSchema().addType(subtype.cloneEmpty());
seed.variantSchema().becomeSimple();
final ObjectWriter listWriter = parent.add(seed);
expandColumn(listWriter, subtype);
return listWriter;
}
use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.
the class TestResultSetLoaderUnions method testUnionBasics.
@Test
public void testUnionBasics() {
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 ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).build();
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
final RowSetLoader writer = rsLoader.writer();
// Sanity check of writer structure
final ObjectWriter wo = writer.column(1);
assertEquals(ObjectType.VARIANT, wo.type());
final VariantWriter vw = wo.variant();
assertTrue(vw.hasType(MinorType.VARCHAR));
assertNotNull(vw.memberWriter(MinorType.VARCHAR));
assertTrue(vw.hasType(MinorType.MAP));
assertNotNull(vw.memberWriter(MinorType.MAP));
// Write values
rsLoader.startBatch();
writer.addRow(1, "first").addRow(2, mapValue(20, "fred")).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 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();
RowSetUtilities.verify(expected, fixture.wrap(rsLoader.harvest()));
}
use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.
the class TestVariantAccessors method testUnionWithMap.
/**
* Test a variant (AKA "union vector") at the top level which
* includes a map.
*/
@Test
public void testUnionWithMap() {
final TupleMetadata schema = new SchemaBuilder().addUnion("u").addType(MinorType.VARCHAR).addMap().addNullable("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).resumeUnion().resumeSchema().buildSchema();
SingleRowSet result;
// Write values
{
final ExtendableRowSet rs = fixture.rowSet(schema);
final RowSetWriter writer = rs.writer();
// Sanity check of writer structure
final ObjectWriter wo = writer.column(0);
assertEquals(ObjectType.VARIANT, wo.type());
final VariantWriter vw = wo.variant();
assertTrue(vw.hasType(MinorType.VARCHAR));
final ObjectWriter strObj = vw.member(MinorType.VARCHAR);
final ScalarWriter strWriter = strObj.scalar();
assertSame(strWriter, vw.scalar(MinorType.VARCHAR));
assertTrue(vw.hasType(MinorType.MAP));
final ObjectWriter mapObj = vw.member(MinorType.MAP);
final TupleWriter mWriter = mapObj.tuple();
assertSame(mWriter, vw.tuple());
final ScalarWriter aWriter = mWriter.scalar("a");
final ScalarWriter bWriter = mWriter.scalar("b");
// First row: string "first"
vw.setType(MinorType.VARCHAR);
strWriter.setString("first");
writer.save();
// Second row: a map
vw.setType(MinorType.MAP);
aWriter.setInt(20);
bWriter.setString("fred");
writer.save();
// Third row: null
vw.setNull();
writer.save();
// Fourth row: map with a null string
vw.setType(MinorType.MAP);
aWriter.setInt(40);
bWriter.setNull();
writer.save();
// Fifth row: string "last"
vw.setType(MinorType.VARCHAR);
strWriter.setString("last");
writer.save();
result = writer.done();
assertEquals(5, result.rowCount());
}
// Read the values.
{
final RowSetReader reader = result.reader();
// Sanity check of structure
final ObjectReader ro = reader.column(0);
assertEquals(ObjectType.VARIANT, ro.type());
final VariantReader vr = ro.variant();
assertTrue(vr.hasType(MinorType.VARCHAR));
final ObjectReader strObj = vr.member(MinorType.VARCHAR);
final ScalarReader strReader = strObj.scalar();
assertSame(strReader, vr.scalar(MinorType.VARCHAR));
assertTrue(vr.hasType(MinorType.MAP));
final ObjectReader mapObj = vr.member(MinorType.MAP);
final TupleReader mReader = mapObj.tuple();
assertSame(mReader, vr.tuple());
final ScalarReader aReader = mReader.scalar("a");
final ScalarReader bReader = mReader.scalar("b");
// First row: string "first"
assertTrue(reader.next());
assertFalse(vr.isNull());
assertEquals(MinorType.VARCHAR, vr.dataType());
assertFalse(strReader.isNull());
assertTrue(mReader.isNull());
assertEquals("first", strReader.getString());
// Second row: a map
assertTrue(reader.next());
assertFalse(vr.isNull());
assertEquals(MinorType.MAP, vr.dataType());
assertTrue(strReader.isNull());
assertFalse(mReader.isNull());
assertFalse(aReader.isNull());
assertEquals(20, aReader.getInt());
assertFalse(bReader.isNull());
assertEquals("fred", bReader.getString());
// Third row: null
assertTrue(reader.next());
assertTrue(vr.isNull());
assertTrue(strReader.isNull());
assertTrue(mReader.isNull());
assertTrue(aReader.isNull());
assertTrue(bReader.isNull());
// Fourth row: map with a null string
assertTrue(reader.next());
assertEquals(MinorType.MAP, vr.dataType());
assertEquals(40, aReader.getInt());
assertTrue(bReader.isNull());
// Fifth row: string "last"
assertTrue(reader.next());
assertEquals(MinorType.VARCHAR, vr.dataType());
assertEquals("last", strReader.getString());
assertFalse(reader.next());
}
result.clear();
}
Aggregations