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);
}
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);
}
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);
}
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);
}
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()));
}
Aggregations