use of org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet in project drill by apache.
the class TestVariantAccessors method testScalarList.
/**
* Test a scalar list. Should act just like a repeated type, with the
* addition of allowing the list for a row to be null. But, a list
* writer does not do auto-increment, so we must do that explicitly
* after each write.
*/
@Test
public void testScalarList() {
final TupleMetadata schema = new SchemaBuilder().addList("list").addType(MinorType.VARCHAR).resumeSchema().buildSchema();
final ExtendableRowSet rowSet = fixture.rowSet(schema);
final RowSetWriter writer = rowSet.writer();
{
final ObjectWriter listObj = writer.column(0);
assertEquals(ObjectType.ARRAY, listObj.type());
final ArrayWriter listArray = listObj.array();
// The list contains only a scalar. But, because lists can,
// in general, contain multiple contents, the list requires
// an explicit save after each entry.
final ObjectWriter itemObj = listArray.entry();
assertEquals(ObjectType.SCALAR, itemObj.type());
final ScalarWriter strWriter = itemObj.scalar();
// First row: two strings and a null
// Unlike a repeated type, a list can mark individual elements
// as null.
// List will automatically detect that data was written.
strWriter.setString("fred");
listArray.save();
strWriter.setNull();
listArray.save();
strWriter.setString("wilma");
listArray.save();
writer.save();
// Second row: null
writer.save();
// Third row: one string
strWriter.setString("dino");
listArray.save();
writer.save();
// Fourth row: empty array. Note that there is no trigger
// to say that the column is not null, so we have to do it
// explicitly.
listArray.setNull(false);
writer.save();
// Last row: a null string and non-null
strWriter.setNull();
listArray.save();
strWriter.setString("pebbles");
listArray.save();
writer.save();
}
final SingleRowSet result = writer.done();
assertEquals(5, result.rowCount());
{
final RowSetReader reader = result.reader();
final ObjectReader listObj = reader.column(0);
assertEquals(ObjectType.ARRAY, listObj.type());
final ArrayReader listArray = listObj.array();
// The list is a repeated scalar
assertEquals(ObjectType.SCALAR, listArray.entry().type());
final ScalarReader strReader = listArray.scalar();
// First row: two strings and a null
assertTrue(reader.next());
assertFalse(listArray.isNull());
assertEquals(3, listArray.size());
assertTrue(listArray.next());
assertFalse(strReader.isNull());
assertEquals("fred", strReader.getString());
assertTrue(listArray.next());
assertTrue(strReader.isNull());
assertTrue(listArray.next());
assertFalse(strReader.isNull());
assertEquals("wilma", strReader.getString());
assertFalse(listArray.next());
// Second row: null
assertTrue(reader.next());
assertTrue(listArray.isNull());
assertEquals(0, listArray.size());
// Third row: one string
assertTrue(reader.next());
assertFalse(listArray.isNull());
assertEquals(1, listArray.size());
assertTrue(listArray.next());
assertEquals("dino", strReader.getString());
assertFalse(listArray.next());
// Fourth row: empty array.
assertTrue(reader.next());
assertFalse(listArray.isNull());
assertEquals(0, listArray.size());
assertFalse(listArray.next());
// Last row: a null string and non-null
assertTrue(reader.next());
assertFalse(listArray.isNull());
assertEquals(2, listArray.size());
assertTrue(listArray.next());
assertTrue(strReader.isNull());
assertTrue(listArray.next());
assertFalse(strReader.isNull());
assertEquals("pebbles", strReader.getString());
assertFalse(listArray.next());
assertFalse(reader.next());
}
result.clear();
}
use of org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet 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.physical.rowSet.RowSet.ExtendableRowSet 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.physical.rowSet.RowSet.ExtendableRowSet in project drill by apache.
the class TestIndirectReaders method testArray.
/**
* More complex case with two levels of offset vector (one for the
* array, another for the Varchar values.) Only the top level goes
* through the indirection.
*/
@Test
public void testArray() {
TupleMetadata schema = new SchemaBuilder().addArray("a", MinorType.VARCHAR).buildSchema();
ExtendableRowSet rowSet = fixture.rowSet(schema);
RowSetWriter writer = rowSet.writer();
ArrayWriter aWriter = writer.array(0);
ScalarWriter strWriter = aWriter.scalar();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 5; j++) {
strWriter.setString("value" + i + "." + j);
}
writer.save();
}
SingleRowSet result = writer.done().toIndirect();
SelectionVector2 sv2 = result.getSv2();
for (int i = 0; i < 10; i++) {
sv2.setIndex(i, 9 - i);
}
RowSetReader reader = result.reader();
ArrayReader aReader = reader.array(0);
ScalarReader strReader = aReader.scalar();
for (int i = 9; i >= 0; i--) {
assertTrue(reader.next());
for (int j = 0; j < 5; j++) {
assertTrue(aReader.next());
assertEquals("value" + i + "." + j, strReader.getString());
}
}
result.clear();
}
use of org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet in project drill by apache.
the class TestRowSet method testScalarStructure.
/**
* Test the simplest constructs: a row with top-level scalar
* columns.
* <p>
* The focus here is the structure of the readers and writers, along
* with the row set loader and verifier that use those constructs.
* That is, while this test uses the int vector, this test is not
* focused on that vector.
*/
@Test
public void testScalarStructure() {
final TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).buildSchema();
final ExtendableRowSet rowSet = fixture.rowSet(schema);
final RowSetWriter writer = rowSet.writer();
// Required Int
// Verify the invariants of the "full" and "simple" access paths
assertEquals(ObjectType.SCALAR, writer.column("a").type());
assertSame(writer.column("a"), writer.column(0));
assertSame(writer.scalar("a"), writer.scalar(0));
assertSame(writer.column("a").scalar(), writer.scalar("a"));
assertSame(writer.column(0).scalar(), writer.scalar(0));
assertEquals(ValueType.INTEGER, writer.scalar(0).valueType());
try {
writer.column(0).array();
fail();
} catch (final UnsupportedOperationException e) {
// Expected
}
try {
writer.column(0).tuple();
fail();
} catch (final UnsupportedOperationException e) {
// Expected
}
// Test the various ways to get at the scalar writer.
writer.column("a").scalar().setInt(10);
writer.save();
writer.scalar("a").setInt(20);
writer.save();
writer.column(0).scalar().setInt(30);
writer.save();
writer.scalar(0).setInt(40);
writer.save();
// Finish the row set and get a reader.
final SingleRowSet actual = writer.done();
final RowSetReader reader = actual.reader();
// Verify invariants
assertEquals(ObjectType.SCALAR, reader.column(0).type());
assertSame(reader.column("a"), reader.column(0));
assertSame(reader.scalar("a"), reader.scalar(0));
assertSame(reader.column("a").scalar(), reader.scalar("a"));
assertSame(reader.column(0).scalar(), reader.scalar(0));
assertEquals(ValueType.INTEGER, reader.scalar(0).valueType());
assertTrue(schema.metadata("a").isEquivalent(reader.column("a").schema()));
// Test various accessors: full and simple
assertTrue(reader.next());
assertFalse(reader.column("a").scalar().isNull());
assertEquals(10, reader.column("a").scalar().getInt());
assertTrue(reader.next());
assertFalse(reader.scalar("a").isNull());
assertEquals(20, reader.scalar("a").getInt());
assertTrue(reader.next());
assertFalse(reader.column(0).scalar().isNull());
assertEquals(30, reader.column(0).scalar().getInt());
assertTrue(reader.next());
assertFalse(reader.column(0).scalar().isNull());
assertEquals(40, reader.scalar(0).getInt());
assertFalse(reader.next());
// Test the above again via the writer and reader
// utility classes.
final SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10).addRow(20).addRow(30).addRow(40).build();
RowSetUtilities.verify(expected, actual);
}
Aggregations