use of org.apache.drill.exec.vector.accessor.ArrayReader in project drill by apache.
the class TestMapAccessors method testDoubleNestedArray.
/**
* Test a doubly-nested array of maps.
*/
@Test
public void testDoubleNestedArray() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMapArray("m1").add("b", MinorType.INT).addMapArray("m2").add("c", MinorType.INT).addArray("d", MinorType.VARCHAR).resumeMap().resumeSchema().buildSchema();
RowSetBuilder builder = fixture.rowSetBuilder(schema);
RowSetWriter rootWriter = builder.writer();
ScalarWriter aWriter = rootWriter.scalar("a");
ArrayWriter a1Writer = rootWriter.array("m1");
TupleWriter m1Writer = a1Writer.tuple();
ScalarWriter bWriter = m1Writer.scalar("b");
ArrayWriter a2Writer = m1Writer.array("m2");
TupleWriter m2Writer = a2Writer.tuple();
ScalarWriter cWriter = m2Writer.scalar("c");
ScalarWriter dWriter = m2Writer.array("d").scalar();
for (int i = 0; i < 5; i++) {
aWriter.setInt(i);
for (int j = 0; j < 4; j++) {
int a1Key = i + 10 + j;
bWriter.setInt(a1Key);
for (int k = 0; k < 3; k++) {
int a2Key = a1Key * 10 + k;
cWriter.setInt(a2Key);
for (int l = 0; l < 2; l++) {
dWriter.setString("d-" + (a2Key * 10 + l));
}
a2Writer.save();
}
a1Writer.save();
}
rootWriter.save();
}
RowSet results = builder.build();
RowSetReader reader = results.reader();
ScalarReader aReader = reader.scalar("a");
ArrayReader a1Reader = reader.array("m1");
TupleReader m1Reader = a1Reader.tuple();
ScalarReader bReader = m1Reader.scalar("b");
ArrayReader a2Reader = m1Reader.array("m2");
TupleReader m2Reader = a2Reader.tuple();
ScalarReader cReader = m2Reader.scalar("c");
ArrayReader dArray = m2Reader.array("d");
ScalarReader dReader = dArray.scalar();
for (int i = 0; i < 5; i++) {
assertTrue(reader.next());
assertEquals(i, aReader.getInt());
for (int j = 0; j < 4; j++) {
assertTrue(a1Reader.next());
int a1Key = i + 10 + j;
assertEquals(a1Key, bReader.getInt());
for (int k = 0; k < 3; k++) {
assertTrue(a2Reader.next());
int a2Key = a1Key * 10 + k;
assertEquals(a2Key, cReader.getInt());
for (int l = 0; l < 2; l++) {
assertTrue(dArray.next());
assertEquals("d-" + (a2Key * 10 + l), dReader.getString());
}
}
}
}
results.clear();
}
use of org.apache.drill.exec.vector.accessor.ArrayReader 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.ArrayReader in project drill by apache.
the class TestScalarAccessors method testFloatArray.
@Test
public void testFloatArray() {
TupleMetadata schema = new SchemaBuilder().addArray("col", MinorType.FLOAT4).buildSchema();
SingleRowSet rs = fixture.rowSetBuilder(schema).addSingleCol(new double[] {}).addSingleCol(new double[] { 0, 20.5, 30.0 }).build();
assertEquals(2, rs.rowCount());
RowSetReader reader = rs.reader();
ArrayReader arrayReader = reader.array(0);
ScalarReader colReader = arrayReader.scalar();
assertEquals(ValueType.FLOAT, colReader.valueType());
assertTrue(reader.next());
assertEquals(0, arrayReader.size());
assertTrue(reader.next());
assertEquals(3, arrayReader.size());
assertTrue(arrayReader.next());
assertEquals(0, colReader.getDouble(), 0.00001);
assertEquals(0, (float) colReader.getObject(), 0.00001);
assertEquals("0.0", colReader.getAsString());
assertTrue(arrayReader.next());
assertEquals(20.5, colReader.getDouble(), 0.00001);
assertEquals(20.5, (float) colReader.getObject(), 0.00001);
assertEquals("20.5", colReader.getAsString());
assertTrue(arrayReader.next());
assertEquals(30.0, colReader.getDouble(), 0.00001);
assertEquals(30.0, (float) colReader.getObject(), 0.00001);
assertEquals("30.0", colReader.getAsString());
assertFalse(arrayReader.next());
assertEquals("[0.0, 20.5, 30.0]", arrayReader.getAsString());
assertEquals(Arrays.asList(0.0F, 20.5F, 30F), arrayReader.getObject());
assertFalse(reader.next());
rs.clear();
}
use of org.apache.drill.exec.vector.accessor.ArrayReader in project drill by apache.
the class TestScalarAccessors method testIntervalYearArray.
@Test
public void testIntervalYearArray() {
TupleMetadata schema = new SchemaBuilder().addArray("col", MinorType.INTERVALYEAR).buildSchema();
Period p1 = Period.years(0);
Period p2 = Period.years(2).plusMonths(3);
Period p3 = Period.years(1234).plusMonths(11);
SingleRowSet rs = fixture.rowSetBuilder(schema).addSingleCol(new Period[] {}).addSingleCol(new Period[] { p1, p2, p3 }).build();
assertEquals(2, rs.rowCount());
RowSetReader reader = rs.reader();
ArrayReader arrayReader = reader.array(0);
ScalarReader colReader = arrayReader.scalar();
assertEquals(ValueType.PERIOD, colReader.valueType());
assertTrue(reader.next());
assertEquals(0, arrayReader.size());
assertTrue(reader.next());
assertEquals(3, arrayReader.size());
assertTrue(arrayReader.next());
assertEquals(p1, colReader.getPeriod());
assertTrue(arrayReader.next());
assertEquals(p2, colReader.getPeriod());
assertEquals(p2, colReader.getObject());
assertEquals(p2.toString(), colReader.getAsString());
assertTrue(arrayReader.next());
assertEquals(p3, colReader.getPeriod());
assertFalse(arrayReader.next());
assertFalse(reader.next());
rs.clear();
}
use of org.apache.drill.exec.vector.accessor.ArrayReader in project drill by apache.
the class TestScalarAccessors method testVarBinaryArray.
@Test
public void testVarBinaryArray() {
TupleMetadata schema = new SchemaBuilder().addArray("col", MinorType.VARBINARY).buildSchema();
byte[] v1 = new byte[] {};
byte[] v2 = new byte[] { (byte) 0x00, (byte) 0x7f, (byte) 0x80, (byte) 0xFF };
byte[] v3 = new byte[] { (byte) 0xDE, (byte) 0xAD, (byte) 0xBE, (byte) 0xAF };
SingleRowSet rs = fixture.rowSetBuilder(schema).addSingleCol(new byte[][] {}).addSingleCol(new byte[][] { v1, v2, v3 }).build();
assertEquals(2, rs.rowCount());
RowSetReader reader = rs.reader();
ArrayReader arrayReader = reader.array(0);
ScalarReader colReader = arrayReader.scalar();
assertEquals(ValueType.BYTES, colReader.valueType());
assertTrue(reader.next());
assertEquals(0, arrayReader.size());
assertTrue(reader.next());
assertEquals(3, arrayReader.size());
assertTrue(arrayReader.next());
assertTrue(Arrays.equals(v1, colReader.getBytes()));
assertTrue(arrayReader.next());
assertTrue(Arrays.equals(v2, colReader.getBytes()));
assertTrue(Arrays.equals(v2, (byte[]) colReader.getObject()));
assertEquals("[00, 7f, 80, ff]", colReader.getAsString());
assertTrue(arrayReader.next());
assertTrue(Arrays.equals(v3, colReader.getBytes()));
assertFalse(arrayReader.next());
assertFalse(reader.next());
rs.clear();
}
Aggregations