Search in sources :

Example 6 with UnionVector

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);
}
Also used : VariantWriter(org.apache.drill.exec.vector.accessor.VariantWriter) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) SimpleListShim(org.apache.drill.exec.vector.accessor.writer.SimpleListShim) UnionVector(org.apache.drill.exec.vector.complex.UnionVector) UnionWriterImpl(org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl) UnionVectorShim(org.apache.drill.exec.vector.accessor.writer.UnionVectorShim) ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) NullableVarCharVector(org.apache.drill.exec.vector.NullableVarCharVector) EmptyListShim(org.apache.drill.exec.vector.accessor.writer.EmptyListShim) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 7 with UnionVector

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;
}
Also used : TransferPair(org.apache.drill.exec.record.TransferPair) UnionVector(org.apache.drill.exec.vector.complex.UnionVector)

Example 8 with UnionVector

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();
}
Also used : NullableFloat8Vector(org.apache.drill.exec.vector.NullableFloat8Vector) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) UnionVector(org.apache.drill.exec.vector.complex.UnionVector) VectorContainer(org.apache.drill.exec.record.VectorContainer) ValueVector(org.apache.drill.exec.vector.ValueVector) NullableIntVector(org.apache.drill.exec.vector.NullableIntVector) NullableVarCharVector(org.apache.drill.exec.vector.NullableVarCharVector) NullableBigIntVector(org.apache.drill.exec.vector.NullableBigIntVector) ListVector(org.apache.drill.exec.vector.complex.ListVector) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) MinorType(org.apache.drill.common.types.TypeProtos.MinorType) ExtendableRowSet(org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet) MapVector(org.apache.drill.exec.vector.complex.MapVector) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 9 with UnionVector

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();
}
Also used : ValueVector(org.apache.drill.exec.vector.ValueVector) NullableIntVector(org.apache.drill.exec.vector.NullableIntVector) NullableVarCharVector(org.apache.drill.exec.vector.NullableVarCharVector) NullableBigIntVector(org.apache.drill.exec.vector.NullableBigIntVector) ListVector(org.apache.drill.exec.vector.complex.ListVector) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) UnionVector(org.apache.drill.exec.vector.complex.UnionVector) ExtendableRowSet(org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet) VectorContainer(org.apache.drill.exec.record.VectorContainer) MapVector(org.apache.drill.exec.vector.complex.MapVector) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 10 with UnionVector

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;
}
Also used : TransferPair(org.apache.drill.exec.record.TransferPair) UnionVector(org.apache.drill.exec.vector.complex.UnionVector)

Aggregations

UnionVector (org.apache.drill.exec.vector.complex.UnionVector)11 ValueVector (org.apache.drill.exec.vector.ValueVector)5 MinorType (org.apache.drill.common.types.TypeProtos.MinorType)3 TransferPair (org.apache.drill.exec.record.TransferPair)3 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)3 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)3 NullableVarCharVector (org.apache.drill.exec.vector.NullableVarCharVector)3 UnionWriterImpl (org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl)3 ListVector (org.apache.drill.exec.vector.complex.ListVector)3 SubOperatorTest (org.apache.drill.test.SubOperatorTest)3 Test (org.junit.Test)3 UnionVectorState (org.apache.drill.exec.physical.resultSet.impl.UnionState.UnionVectorState)2 ExtendableRowSet (org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet)2 VectorContainer (org.apache.drill.exec.record.VectorContainer)2 NullableBigIntVector (org.apache.drill.exec.vector.NullableBigIntVector)2 NullableIntVector (org.apache.drill.exec.vector.NullableIntVector)2 SimpleListShim (org.apache.drill.exec.vector.accessor.writer.SimpleListShim)2 UnionVectorShim (org.apache.drill.exec.vector.accessor.writer.UnionVectorShim)2 MapVector (org.apache.drill.exec.vector.complex.MapVector)2 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)1