Search in sources :

Example 6 with RowSetLoader

use of org.apache.drill.exec.physical.resultSet.RowSetLoader 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 RowSetLoader

use of org.apache.drill.exec.physical.resultSet.RowSetLoader 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);
}
Also used : VariantWriter(org.apache.drill.exec.vector.accessor.VariantWriter) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) TupleWriter(org.apache.drill.exec.vector.accessor.TupleWriter) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 8 with RowSetLoader

use of org.apache.drill.exec.physical.resultSet.RowSetLoader 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);
}
Also used : VariantWriter(org.apache.drill.exec.vector.accessor.VariantWriter) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) TupleWriter(org.apache.drill.exec.vector.accessor.TupleWriter) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) 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 9 with RowSetLoader

use of org.apache.drill.exec.physical.resultSet.RowSetLoader in project drill by apache.

the class TestResultSetLoaderUnions method testListofListofScalar.

/**
 * The semantics of the ListVector are such that it allows
 * multi-dimensional lists. In this way, it is like a (slightly
 * more normalized) version of the repeated list vector. This form
 * allows arrays to be null.
 * <p>
 * This test verifies that the (non-repeated) list vector can
 * be used to create multi-dimensional arrays in the result set
 * loader layer. However, the rest of Drill does not support this
 * functionality at present, so this test is more of a proof-of-
 * concept than a necessity.
 */
@Test
public void testListofListofScalar() {
    // JSON equivalent: {a: [[1, 2], [3, 4]]}
    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("a", Types.optional(MinorType.LIST)));
    final ArrayWriter outerArray = writer.array("a");
    final VariantWriter outerVariant = outerArray.variant();
    outerVariant.addMember(MinorType.LIST);
    final ArrayWriter innerArray = outerVariant.array();
    final VariantWriter innerVariant = innerArray.variant();
    innerVariant.addMember(MinorType.INT);
    writer.addSingleCol(listValue(listValue(1, 2), listValue(3, 4)));
    final RowSet results = fixture.wrap(rsLoader.harvest());
    // Verify metadata
    final ListVector outer = (ListVector) results.container().getValueVector(0).getValueVector();
    final MajorType outerType = outer.getField().getType();
    assertEquals(1, outerType.getSubTypeCount());
    assertEquals(MinorType.LIST, outerType.getSubType(0));
    assertEquals(1, outer.getField().getChildren().size());
    final ListVector inner = (ListVector) outer.getDataVector();
    assertSame(inner.getField(), outer.getField().getChildren().iterator().next());
    final MajorType innerType = inner.getField().getType();
    assertEquals(1, innerType.getSubTypeCount());
    assertEquals(MinorType.INT, innerType.getSubType(0));
    assertEquals(1, inner.getField().getChildren().size());
    final ValueVector data = inner.getDataVector();
    assertSame(data.getField(), inner.getField().getChildren().iterator().next());
    assertEquals(MinorType.INT, data.getField().getType().getMinorType());
    assertEquals(DataMode.OPTIONAL, data.getField().getType().getMode());
    assertTrue(data instanceof NullableIntVector);
    // Note use of TupleMetadata: BatchSchema can't hold the
    // structure of a list.
    final TupleMetadata expectedSchema = new SchemaBuilder().addList("a").addList().addType(MinorType.INT).resumeUnion().resumeSchema().buildSchema();
    final RowSet expected = new RowSetBuilder(fixture.allocator(), expectedSchema).addSingleCol(listValue(listValue(1, 2), listValue(3, 4))).build();
    RowSetUtilities.verify(expected, results);
}
Also used : VariantWriter(org.apache.drill.exec.vector.accessor.VariantWriter) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) ValueVector(org.apache.drill.exec.vector.ValueVector) NullableIntVector(org.apache.drill.exec.vector.NullableIntVector) RowSetBuilder(org.apache.drill.exec.physical.rowSet.RowSetBuilder) ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) ListVector(org.apache.drill.exec.vector.complex.ListVector) 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 10 with RowSetLoader

use of org.apache.drill.exec.physical.resultSet.RowSetLoader 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()));
}
Also used : VariantWriter(org.apache.drill.exec.vector.accessor.VariantWriter) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Aggregations

RowSetLoader (org.apache.drill.exec.physical.resultSet.RowSetLoader)98 ResultSetLoader (org.apache.drill.exec.physical.resultSet.ResultSetLoader)90 Test (org.junit.Test)86 SubOperatorTest (org.apache.drill.test.SubOperatorTest)85 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)82 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)82 SingleRowSet (org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet)66 RowSet (org.apache.drill.exec.physical.rowSet.RowSet)63 ScalarWriter (org.apache.drill.exec.vector.accessor.ScalarWriter)25 TupleWriter (org.apache.drill.exec.vector.accessor.TupleWriter)25 ResultSetOptions (org.apache.drill.exec.physical.resultSet.impl.ResultSetLoaderImpl.ResultSetOptions)23 RowSetReader (org.apache.drill.exec.physical.rowSet.RowSetReader)17 ArrayWriter (org.apache.drill.exec.vector.accessor.ArrayWriter)16 VectorContainer (org.apache.drill.exec.record.VectorContainer)15 SchemaPath (org.apache.drill.common.expression.SchemaPath)12 DictWriter (org.apache.drill.exec.vector.accessor.DictWriter)11 EvfTest (org.apache.drill.categories.EvfTest)10 MaterializedField (org.apache.drill.exec.record.MaterializedField)9 ColumnMetadata (org.apache.drill.exec.record.metadata.ColumnMetadata)6 ArrayReader (org.apache.drill.exec.vector.accessor.ArrayReader)5