Search in sources :

Example 1 with UnionWriterImpl

use of org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl in project drill by apache.

the class BaseWriterBuilder method buildUnion.

private AbstractObjectWriter buildUnion(UnionVector vector, VectorDescrip descrip) {
    if (vector == null) {
        throw new UnsupportedOperationException("Dummy variant writer not yet supported");
    }
    final AbstractObjectWriter[] variants = new AbstractObjectWriter[MinorType.values().length];
    final MetadataProvider mdProvider = descrip.childProvider();
    int i = 0;
    for (final MinorType type : vector.getField().getType().getSubTypeList()) {
        // This call will create the vector if it does not yet exist.
        // Will throw an exception for unsupported types.
        // so call this only if the MajorType reports that the type
        // already exists.
        final ValueVector memberVector = vector.getMember(type);
        final VectorDescrip memberDescrip = new VectorDescrip(mdProvider, i++, memberVector.getField());
        variants[type.ordinal()] = buildVectorWriter(memberVector, memberDescrip);
    }
    return new VariantObjectWriter(new UnionWriterImpl(descrip.metadata, vector, variants));
}
Also used : ValueVector(org.apache.drill.exec.vector.ValueVector) VariantObjectWriter(org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl.VariantObjectWriter) MetadataProvider(org.apache.drill.exec.physical.resultSet.model.MetadataProvider) MinorType(org.apache.drill.common.types.TypeProtos.MinorType) VectorDescrip(org.apache.drill.exec.physical.resultSet.model.MetadataProvider.VectorDescrip) AbstractObjectWriter(org.apache.drill.exec.vector.accessor.writer.AbstractObjectWriter) UnionWriterImpl(org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl)

Example 2 with UnionWriterImpl

use of org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl in project drill by apache.

the class ColumnBuilder method buildUnionList.

/**
 * Create a list based on a (possible) union. The list starts empty here. The client
 * can then add types as they are discovered. The list itself will transition from a
 * list of nulls (no child type), to a list of a single type, to a list of unions.
 * The writer interface will consistently present the list as a list of unions, even
 * when the list itself has no subtype or a single subtype.
 * <p>
 * List vectors (lists of unions) are not supported in
 * Drill. The code here works up through the scan operator. But, other operators do
 * not support the {@code ListVector} type.
 *
 * @param parent the parent (tuple, union or list) that holds this list
 * @param columnSchema description of the list (must be empty of
 * subtypes)
 * @return the column state for the list
 */
private ColumnState buildUnionList(ContainerState parent, ColumnMetadata columnSchema) {
    // The variant must start out empty.
    assert columnSchema.variantSchema().size() == 0;
    // Create the union writer, bound to an empty list shim.
    final UnionWriterImpl unionWriter = new UnionWriterImpl(columnSchema);
    unionWriter.bindShim(new EmptyListShim());
    final VariantObjectWriter unionObjWriter = new VariantObjectWriter(unionWriter);
    // Create the list vector. Starts with the default (dummy) data
    // vector which corresponds to the empty union shim above.
    // Don't get the list vector from the vector cache. List vectors may
    // have content that varies from batch to batch. Only the leaf
    // vectors can be cached.
    final ListVector listVector = new ListVector(columnSchema.schema(), parent.loader().allocator(), null);
    // Create the list vector state that tracks the list vector lifecycle.
    final ListVectorState vectorState = new ListVectorState(unionWriter, listVector);
    // Create the list writer: an array of unions.
    final AbstractObjectWriter listWriter = new ArrayObjectWriter(new ListWriterImpl(columnSchema, listVector, unionObjWriter));
    // Create the manager for the columns within the list (which may or
    // may not be grouped into a union.)
    final ListState listState = new ListState(parent.loader(), parent.vectorCache().childCache(columnSchema.name()));
    // Bind the union state to the union writer to handle column additions.
    unionWriter.bindListener(listState);
    // Assemble it all into a union column state.
    return new UnionColumnState(parent.loader(), listWriter, vectorState, listState);
}
Also used : VariantObjectWriter(org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl.VariantObjectWriter) ListWriterImpl(org.apache.drill.exec.vector.accessor.writer.ListWriterImpl) EmptyListShim(org.apache.drill.exec.vector.accessor.writer.EmptyListShim) ArrayObjectWriter(org.apache.drill.exec.vector.accessor.writer.AbstractArrayWriter.ArrayObjectWriter) RepeatedListVector(org.apache.drill.exec.vector.complex.RepeatedListVector) ListVector(org.apache.drill.exec.vector.complex.ListVector) RepeatedListVectorState(org.apache.drill.exec.physical.resultSet.impl.RepeatedListState.RepeatedListVectorState) ListVectorState(org.apache.drill.exec.physical.resultSet.impl.ListState.ListVectorState) UnionWriterImpl(org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl) AbstractObjectWriter(org.apache.drill.exec.vector.accessor.writer.AbstractObjectWriter) UnionColumnState(org.apache.drill.exec.physical.resultSet.impl.UnionState.UnionColumnState)

Example 3 with UnionWriterImpl

use of org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl in project drill by apache.

the class ColumnBuilder method buildUnion.

/**
 * Builds a union column.
 * <p>
 * The union vector type is not well supported in Drill. The idea is that
 * arbitrary operators can absorb schema changes by converting vectors to
 * unions so that an operator can handle, say, a nullable int and a varchar.
 * In practice, most operators don't support this feature. (Sort does -- but
 * does not manage memory for the union case.) In principal, union can't solve
 * the problem because ODBC and JDBC don't support unions, and it is easy
 * to envision changes that unions won't solve (int and varchar types combining
 * in a join column, say.) Still, Drill supports unions, so the code here
 * does so. Unions are fully tested in the row set writer mechanism.
 *
 * @param parent container of vectors
 * @param columnSchema implied projection type for the column
 * @return column
 */
private ColumnState buildUnion(ContainerState parent, ColumnMetadata columnSchema) {
    assert columnSchema.isVariant() && !columnSchema.isArray();
    // vectors can be cached.
    assert columnSchema.variantSchema().size() == 0;
    final UnionVector vector = new UnionVector(columnSchema.schema(), parent.loader().allocator(), null);
    // Then the union writer.
    final UnionWriterImpl unionWriter = new UnionWriterImpl(columnSchema, vector, null);
    final VariantObjectWriter writer = new VariantObjectWriter(unionWriter);
    // The union vector state which manages the types vector.
    final UnionVectorState vectorState = new UnionVectorState(vector, unionWriter);
    // Create the manager for the columns within the union.
    final UnionState unionState = new UnionState(parent.loader(), parent.vectorCache().childCache(columnSchema.name()));
    // Bind the union state to the union writer to handle column additions.
    unionWriter.bindListener(unionState);
    // Assemble it all into a union column state.
    return new UnionColumnState(parent.loader(), writer, vectorState, unionState);
}
Also used : VariantObjectWriter(org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl.VariantObjectWriter) UnionVector(org.apache.drill.exec.vector.complex.UnionVector) UnionVectorState(org.apache.drill.exec.physical.resultSet.impl.UnionState.UnionVectorState) UnionWriterImpl(org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl) UnionColumnState(org.apache.drill.exec.physical.resultSet.impl.UnionState.UnionColumnState)

Example 4 with UnionWriterImpl

use of org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl in project drill by apache.

the class ListState method addSecondType.

/**
 * Perform the delicate dance of promoting a list vector from a single type to
 * a union, while leaving the writer client blissfully ignorant that the underlying
 * vector representation just did a radical change. Key tasks:
 * <ul>
 * <li>Create the new column (type member) requested by the client.</li>
 * <li>The List vector currently has a single type. Promote the list to
 * a union, adding the existing type (column) as the first union member.</li>
 * <li>Initialize the union's type vector with either the type of the existing
 * column, or null, depending on the setting of the is-set bits in the existing
 * column vector.</li>
 * <li>Since we've written values into the union's type vector, mark the
 * last-write position in the union vector's type vector writer to reflect
 * these writes. (Otherwise, the writer will helpfully zero-fill the previous
 * positions as part of it's back-fill handling.</li>
 * <li>Replace the single-type shim in the union vector with a full union
 * shim.</li>
 * <li>Move the existing column writer or the member column across from the
 * single-writer shim to the new union shim.</li>
 * <li>Augment the list vector's vector state to include a vector state for
 * the newly created union vector.</li>
 * </ul>
 * <p>
 * Here, yet again, an editorial comment might be useful. List vectors are
 * very strange and not at all well designed for high-speed writing. They are
 * too complex; too much can go wrong and there are too many states to handle.
 * Not only that, variant types don't play well with a relational model like
 * SQL. This code works, but the overall list concept really needs rethinking.
 *
 * @param colState the column state for the newly added type column; the
 * one causing the list to change from single-type to a union
 */
private void addSecondType(ColumnState colState) {
    final UnionWriterImpl unionWriter = unionWriter();
    final ListVector listVector = listVector();
    // Going from one type to a union
    // Convert the list from single type to a union,
    // moving across the previous type vector.
    final int typeFillCount = unionWriter.elementPosition().writeIndex();
    final UnionVector unionVector = listVector.convertToUnion(innerCardinality(), typeFillCount);
    unionVector.addType(colState.vector());
    // Replace the single-type shim with a union shim, copying
    // across the existing writer.
    final SimpleListShim oldShim = (SimpleListShim) unionWriter.shim();
    final UnionVectorShim newShim = new UnionVectorShim(unionVector);
    unionWriter.bindShim(newShim);
    newShim.addMemberWriter(oldShim.memberWriter());
    newShim.initTypeIndex(typeFillCount);
    // The union vector will be managed within the list vector state.
    // (Do this last because the union vector state expects the union
    // writer to be operating in "union mode".
    listVectorState().replaceMember(new UnionVectorState(unionVector, unionWriter));
}
Also used : UnionVectorShim(org.apache.drill.exec.vector.accessor.writer.UnionVectorShim) ListVector(org.apache.drill.exec.vector.complex.ListVector) SimpleListShim(org.apache.drill.exec.vector.accessor.writer.SimpleListShim) UnionVector(org.apache.drill.exec.vector.complex.UnionVector) UnionVectorState(org.apache.drill.exec.physical.resultSet.impl.UnionState.UnionVectorState) UnionWriterImpl(org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl)

Example 5 with UnionWriterImpl

use of org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl 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)

Aggregations

UnionWriterImpl (org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl)5 VariantObjectWriter (org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl.VariantObjectWriter)3 UnionVector (org.apache.drill.exec.vector.complex.UnionVector)3 UnionColumnState (org.apache.drill.exec.physical.resultSet.impl.UnionState.UnionColumnState)2 UnionVectorState (org.apache.drill.exec.physical.resultSet.impl.UnionState.UnionVectorState)2 AbstractObjectWriter (org.apache.drill.exec.vector.accessor.writer.AbstractObjectWriter)2 EmptyListShim (org.apache.drill.exec.vector.accessor.writer.EmptyListShim)2 SimpleListShim (org.apache.drill.exec.vector.accessor.writer.SimpleListShim)2 UnionVectorShim (org.apache.drill.exec.vector.accessor.writer.UnionVectorShim)2 ListVector (org.apache.drill.exec.vector.complex.ListVector)2 MinorType (org.apache.drill.common.types.TypeProtos.MinorType)1 ResultSetLoader (org.apache.drill.exec.physical.resultSet.ResultSetLoader)1 RowSetLoader (org.apache.drill.exec.physical.resultSet.RowSetLoader)1 ListVectorState (org.apache.drill.exec.physical.resultSet.impl.ListState.ListVectorState)1 RepeatedListVectorState (org.apache.drill.exec.physical.resultSet.impl.RepeatedListState.RepeatedListVectorState)1 MetadataProvider (org.apache.drill.exec.physical.resultSet.model.MetadataProvider)1 VectorDescrip (org.apache.drill.exec.physical.resultSet.model.MetadataProvider.VectorDescrip)1 RowSet (org.apache.drill.exec.physical.rowSet.RowSet)1 SingleRowSet (org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet)1 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)1