use of org.apache.drill.exec.vector.accessor.ObjectReader in project drill by apache.
the class TestVariantAccessors method testAddTypes.
/**
* Test a variant (AKA "union vector") at the top level, using
* just scalar values.
*/
@Test
public void testAddTypes() {
final TupleMetadata batchSchema = new SchemaBuilder().addNullable("v", MinorType.UNION).buildSchema();
final ExtendableRowSet rs = fixture.rowSet(batchSchema);
final RowSetWriter writer = rs.writer();
// Sanity check of writer structure
final ObjectWriter wo = writer.column(0);
assertEquals(ObjectType.VARIANT, wo.type());
final VariantWriter vw = wo.variant();
assertSame(vw, writer.variant(0));
assertSame(vw, writer.variant("v"));
for (final MinorType type : MinorType.values()) {
assertFalse(vw.hasType(type));
}
// Write values of different types
vw.scalar(MinorType.INT).setInt(10);
assertTrue(vw.hasType(MinorType.INT));
assertFalse(vw.hasType(MinorType.VARCHAR));
writer.save();
vw.scalar(MinorType.VARCHAR).setString("fred");
assertTrue(vw.hasType(MinorType.VARCHAR));
writer.save();
vw.setNull();
writer.save();
vw.scalar(MinorType.FLOAT8).setDouble(123.45);
assertTrue(vw.hasType(MinorType.INT));
assertTrue(vw.hasType(MinorType.FLOAT8));
writer.save();
final SingleRowSet result = writer.done();
assertEquals(4, result.rowCount());
// Read the values.
final RowSetReader reader = result.reader();
// Sanity check of structure
final ObjectReader ro = reader.column(0);
assertEquals(ObjectType.VARIANT, ro.type());
final VariantReader vr = ro.variant();
assertSame(vr, reader.variant(0));
assertSame(vr, reader.variant("v"));
for (final MinorType type : MinorType.values()) {
if (type == MinorType.INT || type == MinorType.VARCHAR || type == MinorType.FLOAT8) {
assertTrue(vr.hasType(type));
} else {
assertFalse(vr.hasType(type));
}
}
// Verify the data
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.INT);
assertSame(vr.scalar(MinorType.INT), vr.scalar());
assertNotNull(vr.member());
assertSame(vr.scalar(), vr.member().scalar());
assertEquals(10, vr.scalar().getInt());
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.VARCHAR);
assertSame(vr.scalar(MinorType.VARCHAR), vr.scalar());
assertEquals("fred", vr.scalar().getString());
assertTrue(reader.next());
assertTrue(vr.isNull());
assertNull(vr.dataType());
assertNull(vr.scalar());
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.FLOAT8);
assertSame(vr.scalar(MinorType.FLOAT8), vr.scalar());
assertEquals(123.45, vr.scalar().getDouble(), 0.001);
assertFalse(reader.next());
result.clear();
}
use of org.apache.drill.exec.vector.accessor.ObjectReader in project drill by apache.
the class TestVariantAccessors method testScalarVariant.
/**
* Test a variant (AKA "union vector") at the top level, using
* just scalar values.
*/
@Test
public void testScalarVariant() {
final TupleMetadata schema = new SchemaBuilder().addUnion("u").addType(MinorType.INT).addType(MinorType.VARCHAR).addType(MinorType.FLOAT8).resumeSchema().buildSchema();
final ExtendableRowSet rs = fixture.rowSet(schema);
final RowSetWriter writer = rs.writer();
// Sanity check of writer structure
final ObjectWriter wo = writer.column(0);
assertEquals(ObjectType.VARIANT, wo.type());
final VariantWriter vw = wo.variant();
assertSame(vw, writer.variant(0));
assertSame(vw, writer.variant("u"));
assertTrue(vw.hasType(MinorType.INT));
assertTrue(vw.hasType(MinorType.VARCHAR));
assertTrue(vw.hasType(MinorType.FLOAT8));
// Write values of different types
vw.scalar(MinorType.INT).setInt(10);
writer.save();
vw.scalar(MinorType.VARCHAR).setString("fred");
writer.save();
// The entire variant is null
vw.setNull();
writer.save();
vw.scalar(MinorType.FLOAT8).setDouble(123.45);
writer.save();
// Strange case: just the value is null, but the variant
// is not null.
vw.scalar(MinorType.INT).setNull();
writer.save();
// Marker to avoid fill-empty issues (fill-empties tested elsewhere.)
vw.scalar(MinorType.INT).setInt(20);
writer.save();
final SingleRowSet result = writer.done();
assertEquals(6, result.rowCount());
// Read the values.
final RowSetReader reader = result.reader();
// Sanity check of structure
final ObjectReader ro = reader.column(0);
assertEquals(ObjectType.VARIANT, ro.type());
final VariantReader vr = ro.variant();
assertSame(vr, reader.variant(0));
assertSame(vr, reader.variant("u"));
for (final MinorType type : MinorType.values()) {
if (type == MinorType.INT || type == MinorType.VARCHAR || type == MinorType.FLOAT8) {
assertTrue(vr.hasType(type));
} else {
assertFalse(vr.hasType(type));
}
}
// Can get readers up front
final ScalarReader intReader = vr.scalar(MinorType.INT);
final ScalarReader strReader = vr.scalar(MinorType.VARCHAR);
final ScalarReader floatReader = vr.scalar(MinorType.FLOAT8);
// Verify the data
// Int 10
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.INT);
assertSame(intReader, vr.scalar());
assertNotNull(vr.member());
assertSame(vr.scalar(), vr.member().scalar());
assertFalse(intReader.isNull());
assertEquals(10, intReader.getInt());
assertTrue(strReader.isNull());
assertTrue(floatReader.isNull());
// String "fred"
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.VARCHAR);
assertSame(strReader, vr.scalar());
assertFalse(strReader.isNull());
assertEquals("fred", strReader.getString());
assertTrue(intReader.isNull());
assertTrue(floatReader.isNull());
// Null value
assertTrue(reader.next());
assertTrue(vr.isNull());
assertNull(vr.dataType());
assertNull(vr.scalar());
assertTrue(intReader.isNull());
assertTrue(strReader.isNull());
assertTrue(floatReader.isNull());
// Double 123.45
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.FLOAT8);
assertSame(floatReader, vr.scalar());
assertFalse(floatReader.isNull());
assertEquals(123.45, vr.scalar().getDouble(), 0.001);
assertTrue(intReader.isNull());
assertTrue(strReader.isNull());
// Strange case: null int (but union is not null)
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.INT);
assertTrue(intReader.isNull());
// Int 20
assertTrue(reader.next());
assertFalse(vr.isNull());
assertFalse(intReader.isNull());
assertEquals(20, intReader.getInt());
assertFalse(reader.next());
result.clear();
}
use of org.apache.drill.exec.vector.accessor.ObjectReader 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.vector.accessor.ObjectReader in project drill by apache.
the class TestRepeatedListAccessors method testSchema2DWriterReader.
@Test
public void testSchema2DWriterReader() {
final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addRepeatedList("list2").addArray(MinorType.VARCHAR).resumeSchema().buildSchema();
final DirectRowSet rowSet = DirectRowSet.fromSchema(fixture.allocator(), schema);
SingleRowSet result;
{
final RowSetWriter writer = rowSet.writer();
assertEquals(2, writer.size());
final ObjectWriter listObj = writer.column("list2");
assertEquals(ObjectType.ARRAY, listObj.type());
final ArrayWriter listWriter = listObj.array();
assertEquals(ObjectType.ARRAY, listWriter.entryType());
final ArrayWriter innerWriter = listWriter.array();
assertEquals(ObjectType.SCALAR, innerWriter.entryType());
final ScalarWriter strWriter = innerWriter.scalar();
// Write one row using writers explicitly.
//
// (1, [["a, "b"], ["c", "d"]])
//
// Note auto increment of inner list on write.
writer.scalar("id").setInt(1);
strWriter.setString("a");
strWriter.setString("b");
listWriter.save();
strWriter.setString("c");
strWriter.setString("d");
listWriter.save();
writer.save();
// Write more rows using the convenience methods.
//
// (2, [["e"], [], ["f", "g", "h"]])
// (3, [])
// (4, [[], ["i"], []])
writer.addRow(2, objArray(strArray("e"), strArray(), strArray("f", "g", "h"))).addRow(3, objArray()).addRow(4, objArray(strArray(), strArray("i"), strArray()));
result = writer.done();
}
// Verify one row using the individual readers.
{
final RowSetReader reader = result.reader();
assertEquals(2, reader.columnCount());
final ObjectReader listObj = reader.column("list2");
assertEquals(ObjectType.ARRAY, listObj.type());
final ArrayReader listReader = listObj.array();
assertEquals(ObjectType.ARRAY, listReader.entryType());
final ArrayReader innerReader = listReader.array();
assertEquals(ObjectType.SCALAR, innerReader.entryType());
final ScalarReader strReader = innerReader.scalar();
// Write one row using writers explicitly.
//
// (1, [["a, "b"], ["c", "d"]])
assertTrue(reader.next());
assertEquals(2, listReader.size());
assertTrue(listReader.next());
assertEquals(2, innerReader.size());
assertTrue(innerReader.next());
assertEquals("a", strReader.getString());
assertTrue(innerReader.next());
assertEquals("b", strReader.getString());
assertFalse(innerReader.next());
assertTrue(listReader.next());
assertEquals(2, innerReader.size());
assertTrue(innerReader.next());
assertEquals("c", strReader.getString());
assertTrue(innerReader.next());
assertEquals("d", strReader.getString());
assertFalse(innerReader.next());
assertFalse(listReader.next());
}
// Verify both rows by building another row set and comparing.
final RowSet expected = fixture.rowSetBuilder(schema).addRow(1, objArray(strArray("a", "b"), strArray("c", "d"))).addRow(2, objArray(strArray("e"), strArray(), strArray("f", "g", "h"))).addRow(3, objArray()).addRow(4, objArray(strArray(), strArray("i"), strArray())).build();
new RowSetComparison(expected).verify(result);
// Test that the row set rebuilds its internal structure from
// a vector container.
RowSet wrapped = fixture.wrap(result.container());
RowSetUtilities.verify(expected, wrapped);
}
use of org.apache.drill.exec.vector.accessor.ObjectReader in project drill by apache.
the class TestVariantAccessors method testListOfMaps.
/**
* List of maps. Like a repeated map, but each list entry can be
* null.
*/
@Test
public void testListOfMaps() {
final TupleMetadata schema = new SchemaBuilder().addList("list").addMap().addNullable("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).resumeUnion().resumeSchema().buildSchema();
final ExtendableRowSet rowSet = fixture.rowSet(schema);
final RowSetWriter writer = rowSet.writer();
{
final ObjectWriter listObj = writer.column("list");
assertEquals(ObjectType.ARRAY, listObj.type());
final ArrayWriter listArray = listObj.array();
final ObjectWriter itemObj = listArray.entry();
assertEquals(ObjectType.TUPLE, itemObj.type());
final TupleWriter mapWriter = itemObj.tuple();
final ScalarWriter aWriter = mapWriter.scalar("a");
final ScalarWriter bWriter = mapWriter.scalar("b");
// First row:
// {1, "fred"}, null, {3, null}
aWriter.setInt(1);
bWriter.setString("fred");
listArray.save();
// Can't mark the map as null. Instead, we simply skip
// the map and the contained nullable members will automatically
// back-fill each entry with a null value.
listArray.save();
aWriter.setInt(3);
bWriter.setNull();
listArray.save();
writer.save();
// Second row: null
writer.save();
// Third row: {null, "dino"}
aWriter.setNull();
bWriter.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: {4, "pebbles"}
aWriter.setInt(4);
bWriter.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("list");
assertEquals(ObjectType.ARRAY, listObj.type());
final ArrayReader listArray = listObj.array();
assertEquals(ObjectType.TUPLE, listArray.entry().type());
final TupleReader mapReader = listArray.tuple();
final ScalarReader aReader = mapReader.scalar("a");
final ScalarReader bReader = mapReader.scalar("b");
// First row:
// {1, "fred"}, null, {3, null}
assertTrue(reader.next());
assertFalse(listArray.isNull());
assertFalse(mapReader.isNull());
assertEquals(3, listArray.size());
assertTrue(listArray.next());
assertFalse(aReader.isNull());
assertEquals(1, aReader.getInt());
assertFalse(bReader.isNull());
assertEquals("fred", bReader.getString());
assertTrue(listArray.next());
// Awkward: the map has no null state, but its
// members do.
assertTrue(aReader.isNull());
assertTrue(bReader.isNull());
assertTrue(listArray.next());
assertFalse(aReader.isNull());
assertEquals(3, aReader.getInt());
assertTrue(bReader.isNull());
assertFalse(listArray.next());
// Second row: null
assertTrue(reader.next());
assertTrue(listArray.isNull());
assertEquals(0, listArray.size());
// Third row: {null, "dino"}
assertTrue(reader.next());
assertFalse(listArray.isNull());
assertEquals(1, listArray.size());
assertTrue(listArray.next());
assertTrue(aReader.isNull());
assertFalse(bReader.isNull());
assertEquals("dino", bReader.getString());
assertFalse(listArray.next());
// Fourth row: empty array.
assertTrue(reader.next());
assertFalse(listArray.isNull());
assertEquals(0, listArray.size());
assertFalse(listArray.next());
// Last row: {4, "pebbles"}
assertTrue(reader.next());
assertFalse(listArray.isNull());
assertEquals(1, listArray.size());
assertTrue(listArray.next());
assertEquals(4, aReader.getInt());
assertEquals("pebbles", bReader.getString());
assertFalse(listArray.next());
assertFalse(reader.next());
}
result.clear();
}
Aggregations