use of org.apache.drill.exec.vector.complex.UnionVector in project drill by apache.
the class TestResultSetLoaderUnions method testVariantListDynamic.
/**
* Test a variant list created dynamically at load time.
* The list starts with no type, at which time it can hold
* only null values. Then we add a Varchar, and finally an
* Int.
* <p>
* This test is superficial. There are many odd cases to consider.
* <ul>
* <li>Write nulls to a list with no type. (This test ensures that
* adding a (nullable) scalar "does the right thing."</li>
* <li>Add a map to the list. Maps carry no "bits" vector, so null
* list entries to that point are lost. (For maps, we could go straight
* to a union, with just a map, to preserve the null states. This whole
* area is a huge mess...)</li>
* <li>Do the type transitions when writing to a row. (The tests here
* do the transition between rows.)</li>
* </ul>
*
* The reason for the sparse coverage is that Drill barely supports lists
* and unions; most code is just plain broken. Our goal here is not to fix
* all those problems, just to leave things no more broken than before.
*/
@Test
public void testVariantListDynamic() {
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator());
final RowSetLoader writer = rsLoader.writer();
// Can write a batch as if this was a repeated Varchar, except
// that any value can also be null.
rsLoader.startBatch();
writer.addColumn(MaterializedField.create("id", Types.required(MinorType.INT)));
writer.addColumn(MaterializedField.create("list", Types.optional(MinorType.LIST)));
// Sanity check: should be an array of variants because we said the
// types within the list are expandable (which is the default.)
final ArrayWriter arrWriter = writer.array("list");
assertEquals(ObjectType.VARIANT, arrWriter.entryType());
final VariantWriter variant = arrWriter.variant();
// We need to verify that the internal state is what we expect, so
// the next assertion peeks inside the private bits of the union
// writer. No client code should ever need to do this, of course.
assertTrue(((UnionWriterImpl) variant).shim() instanceof EmptyListShim);
// No types, so all we can do is 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);
assertTrue(variant.hasType(MinorType.VARCHAR));
// Sanity check: sniff inside to ensure that the list contains a single
// type.
assertTrue(((UnionWriterImpl) variant).shim() instanceof SimpleListShim);
assertTrue(((ListWriterImpl) arrWriter).vector().getDataVector() instanceof NullableVarCharVector);
writer.addRow(4, variantArray("fred", null, "barney"));
// Add an integer. The list vector should be promoted to union.
// Now we can add both types.
variant.addMember(MinorType.INT);
// Sanity check: sniff inside to ensure promotion to union occurred
assertTrue(((UnionWriterImpl) variant).shim() instanceof UnionVectorShim);
assertTrue(((ListWriterImpl) arrWriter).vector().getDataVector() instanceof UnionVector);
writer.addRow(5, variantArray("wilma", null, 30));
// Verify
final RowSet result = fixture.wrap(rsLoader.harvest());
final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addList("list").addType(MinorType.VARCHAR).addType(MinorType.INT).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("wilma", null, 30)).build();
RowSetUtilities.verify(expected, result);
}
use of org.apache.drill.exec.vector.complex.UnionVector in project drill by apache.
the class PromotableWriter method promoteToUnion.
private FieldWriter promoteToUnion(MinorType newType) {
String name = vector.getField().getName();
TransferPair tp = vector.getTransferPair(vector.getField().getType().getMinorType().name().toLowerCase(), vector.getAllocator());
tp.transfer();
if (parentContainer != null) {
unionVector = parentContainer.addOrGet(name, Types.optional(MinorType.UNION), UnionVector.class);
} else if (listVector != null) {
unionVector = listVector.promoteToUnion();
}
// fix early init issue with different type lists in one union vector
unionVector.addSubType(newType);
unionVector.addVector(tp.getTo());
writer = new UnionWriter(unionVector);
writer.setPosition(idx());
for (int i = 0; i < idx(); i++) {
unionVector.getMutator().setType(i, vector.getField().getType().getMinorType());
}
vector = null;
state = State.UNION;
return writer;
}
use of org.apache.drill.exec.vector.complex.UnionVector in project drill by apache.
the class TestVariantAccessors method testBuildRowSetUnionArray.
@Test
public void testBuildRowSetUnionArray() {
final TupleMetadata schema = new SchemaBuilder().addList("list1").addType(MinorType.BIGINT).addMap().addNullable("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).resumeUnion().addList().addType(MinorType.FLOAT8).resumeUnion().resumeSchema().buildSchema();
final ExtendableRowSet rowSet = fixture.rowSet(schema);
final VectorContainer vc = rowSet.container();
assertEquals(1, vc.getNumberOfColumns());
// List with complex internal structure
final ValueVector vector = vc.getValueVector(0).getValueVector();
assertTrue(vector instanceof ListVector);
final ListVector list = (ListVector) vector;
assertTrue(list.getDataVector() instanceof UnionVector);
final UnionVector union = (UnionVector) list.getDataVector();
// Union inside the list
final MajorType unionType = union.getField().getType();
final List<MinorType> types = unionType.getSubTypeList();
assertEquals(3, types.size());
assertTrue(types.contains(MinorType.BIGINT));
assertTrue(types.contains(MinorType.MAP));
assertTrue(types.contains(MinorType.LIST));
final MapVector typeMap = union.getTypeMap();
ValueVector member = typeMap.getChild(MinorType.BIGINT.name());
assertTrue(member instanceof NullableBigIntVector);
// Map inside the list
member = typeMap.getChild(MinorType.MAP.name());
assertTrue(member instanceof MapVector);
final MapVector childMap = (MapVector) member;
ValueVector mapMember = childMap.getChild("a");
assertNotNull(mapMember);
assertTrue(mapMember instanceof NullableIntVector);
mapMember = childMap.getChild("b");
assertNotNull(mapMember);
assertTrue(mapMember instanceof NullableVarCharVector);
// Single-type list inside the outer list
member = typeMap.getChild(MinorType.LIST.name());
assertTrue(member instanceof ListVector);
final ListVector childList = (ListVector) member;
assertTrue(childList.getDataVector() instanceof NullableFloat8Vector);
rowSet.clear();
}
use of org.apache.drill.exec.vector.complex.UnionVector in project drill by apache.
the class TestVariantAccessors method testBuildRowSetUnion.
@Test
public void testBuildRowSetUnion() {
final TupleMetadata schema = new SchemaBuilder().addUnion("u").addType(MinorType.INT).addMap().addNullable("c", MinorType.BIGINT).addNullable("d", MinorType.VARCHAR).resumeUnion().addList().addType(MinorType.VARCHAR).resumeUnion().resumeSchema().buildSchema();
final ExtendableRowSet rowSet = fixture.rowSet(schema);
final VectorContainer vc = rowSet.container();
assertEquals(1, vc.getNumberOfColumns());
// Single union
final ValueVector vector = vc.getValueVector(0).getValueVector();
assertTrue(vector instanceof UnionVector);
final UnionVector union = (UnionVector) vector;
final MapVector typeMap = union.getTypeMap();
ValueVector member = typeMap.getChild(MinorType.INT.name());
assertTrue(member instanceof NullableIntVector);
// Inner map
member = typeMap.getChild(MinorType.MAP.name());
assertTrue(member instanceof MapVector);
member = typeMap.getChild(MinorType.MAP.name());
assertTrue(member instanceof MapVector);
final MapVector childMap = (MapVector) member;
ValueVector mapMember = childMap.getChild("c");
assertNotNull(mapMember);
assertTrue(mapMember instanceof NullableBigIntVector);
mapMember = childMap.getChild("d");
assertNotNull(mapMember);
assertTrue(mapMember instanceof NullableVarCharVector);
// Inner list
member = typeMap.getChild(MinorType.LIST.name());
assertTrue(member instanceof ListVector);
final ListVector list = (ListVector) member;
assertTrue(list.getDataVector() instanceof NullableVarCharVector);
rowSet.clear();
}
use of org.apache.drill.exec.vector.complex.UnionVector in project drill by axbaretto.
the class PromotableWriter method promoteToUnion.
private FieldWriter promoteToUnion() {
String name = vector.getField().getName();
TransferPair tp = vector.getTransferPair(vector.getField().getType().getMinorType().name().toLowerCase(), vector.getAllocator());
tp.transfer();
if (parentContainer != null) {
unionVector = parentContainer.addOrGet(name, Types.optional(MinorType.UNION), UnionVector.class);
} else if (listVector != null) {
unionVector = listVector.promoteToUnion();
}
unionVector.addVector(tp.getTo());
writer = new UnionWriter(unionVector);
writer.setPosition(idx());
for (int i = 0; i < idx(); i++) {
unionVector.getMutator().setType(i, vector.getField().getType().getMinorType());
}
vector = null;
state = State.UNION;
return writer;
}
Aggregations