use of org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet 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.SingleRowSet 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);
}
use of org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet in project drill by apache.
the class TestRowSet method example.
/**
* The code below is not a test. Rather, it is a simple example of
* how to write a batch of data using writers, then read it using
* readers.
*/
@Test
public void example() {
// Step 1: Define a schema. In a real app, this
// will be provided by a reader, by an incoming batch,
// etc.
final TupleMetadata schema = new SchemaBuilder().add("a", MinorType.VARCHAR).addArray("b", MinorType.INT).addMap("c").add("c1", MinorType.INT).add("c2", MinorType.VARCHAR).resumeSchema().buildSchema();
// Step 2: Create a batch. Done here because this is
// a batch-oriented test. Done automatically in the
// result set loader.
final DirectRowSet drs = DirectRowSet.fromSchema(fixture.allocator(), schema);
// Step 3: Create the writer.
final RowSetWriter writer = drs.writer();
// Step 4: Populate data. Here we do it the way an app would:
// using the individual accessors. See tests above for the many
// ways this can be done depending on the need of the app.
//
// Write two rows:
// ("fred", [10, 11], {12, "wilma"})
// ("barney", [20, 21], {22, "betty"})
//
// This example uses Java strings for Varchar. Real code might
// use byte arrays.
writer.scalar("a").setString("fred");
final ArrayWriter bWriter = writer.array("b");
bWriter.scalar().setInt(10);
bWriter.scalar().setInt(11);
final TupleWriter cWriter = writer.tuple("c");
cWriter.scalar("c1").setInt(12);
cWriter.scalar("c2").setString("wilma");
writer.save();
writer.scalar("a").setString("barney");
bWriter.scalar().setInt(20);
bWriter.scalar().setInt(21);
cWriter.scalar("c1").setInt(22);
cWriter.scalar("c2").setString("betty");
writer.save();
// Step 5: "Harvest" the batch. Done differently in the
// result set loader.
final SingleRowSet rowSet = writer.done();
// Step 5: Create a reader.
final RowSetReader reader = rowSet.reader();
while (reader.next()) {
final StringBuilder sb = new StringBuilder();
sb.append(print(reader.scalar("a").getString()));
final ArrayReader bReader = reader.array("b");
while (bReader.next()) {
sb.append(print(bReader.scalar().getInt()));
}
final TupleReader cReader = reader.tuple("c");
sb.append(print(cReader.scalar("c1").getInt()));
sb.append(print(cReader.scalar("c2").getString()));
logger.debug(sb.toString());
}
// Step 7: Free memory.
rowSet.clear();
}
use of org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet in project drill by apache.
the class TestRowSet method testDictStructure.
@Test
public void testDictStructure() {
final String dictName = "d";
final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addDict(dictName, MinorType.INT).value(// required int
MinorType.VARCHAR).resumeSchema().buildSchema();
final ExtendableRowSet rowSet = fixture.rowSet(schema);
final RowSetWriter writer = rowSet.writer();
// Dict
// Pick out components and lightly test. (Assumes structure
// tested earlier is still valid, so no need to exhaustively
// test again.)
assertEquals(ObjectType.ARRAY, writer.column(dictName).type());
assertTrue(writer.column(dictName).schema().isDict());
final ScalarWriter idWriter = writer.column(0).scalar();
final DictWriter dictWriter = writer.column(1).dict();
assertEquals(ValueType.INTEGER, dictWriter.keyType());
assertEquals(ObjectType.SCALAR, dictWriter.valueType());
final ScalarWriter keyWriter = dictWriter.keyWriter();
final ScalarWriter valueWriter = dictWriter.valueWriter().scalar();
assertEquals(ValueType.INTEGER, keyWriter.valueType());
assertEquals(ValueType.STRING, valueWriter.valueType());
// Write data
idWriter.setInt(1);
keyWriter.setInt(11);
valueWriter.setString("a");
// Advance to next entry position
dictWriter.save();
keyWriter.setInt(12);
valueWriter.setString("b");
dictWriter.save();
writer.save();
idWriter.setInt(2);
keyWriter.setInt(21);
valueWriter.setString("c");
dictWriter.save();
writer.save();
idWriter.setInt(3);
keyWriter.setInt(31);
valueWriter.setString("d");
dictWriter.save();
keyWriter.setInt(32);
valueWriter.setString("e");
dictWriter.save();
writer.save();
// Finish the row set and get a reader.
final SingleRowSet actual = writer.done();
final RowSetReader reader = actual.reader();
// Verify reader structure
assertEquals(ObjectType.ARRAY, reader.column(dictName).type());
final DictReader dictReader = reader.dict(1);
assertEquals(ObjectType.ARRAY, dictReader.type());
assertEquals(ValueType.INTEGER, dictReader.keyColumnType());
assertEquals(ObjectType.SCALAR, dictReader.valueColumnType());
// Row 1: get value reader with its position set to entry corresponding to a key
assertTrue(reader.next());
// dict itself is not null
assertFalse(dictReader.isNull());
dictReader.getAsString();
final KeyAccessor keyAccessor = dictReader.keyAccessor();
final ScalarReader valueReader = dictReader.valueReader().scalar();
assertTrue(keyAccessor.find(12));
assertEquals("b", valueReader.getString());
assertTrue(keyAccessor.find(11));
assertEquals("a", valueReader.getString());
// compare entire dict
Map<Object, Object> map = map(11, "a", 12, "b");
assertEquals(map, dictReader.getObject());
// Row 2
assertTrue(reader.next());
// the dict does not contain an entry with the key
assertFalse(keyAccessor.find(22));
assertTrue(keyAccessor.find(21));
assertEquals("c", valueReader.getString());
map = map(21, "c");
assertEquals(map, dictReader.getObject());
// Row 3
assertTrue(reader.next());
assertTrue(keyAccessor.find(31));
assertEquals("d", valueReader.getString());
assertFalse(keyAccessor.find(33));
assertTrue(keyAccessor.find(32));
assertEquals("e", valueReader.getString());
map = map(31, "d", 32, "e");
assertEquals(map, dictReader.getObject());
assertFalse(reader.next());
// Verify that the dict accessor's value count was set.
final DictVector dictVector = (DictVector) actual.container().getValueVector(1).getValueVector();
assertEquals(3, dictVector.getAccessor().getValueCount());
final SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(1, map(11, "a", 12, "b")).addRow(2, map(21, "c")).addRow(3, map(31, "d", 32, "e")).build();
RowSetUtilities.verify(expected, actual);
}
use of org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet in project drill by apache.
the class TestHyperVectorReaders method testRepeated.
/**
* Test an array to test the indirection from the repeated vector
* to the array offsets vector and the array values vector. (Uses
* varchar to add another level of indirection to the data offset
* and data values vectors.)
*/
@Test
public void testRepeated() {
TupleMetadata schema = new SchemaBuilder().addArray("a", MinorType.VARCHAR).buildSchema();
SingleRowSet rowSet1 = fixture.rowSetBuilder(schema).addSingleCol(strArray("sixth", "6.1", "6.2")).addSingleCol(strArray("second", "2.1", "2.2", "2.3")).addSingleCol(strArray("fourth", "4.1")).build();
SingleRowSet rowSet2 = fixture.rowSetBuilder(schema).addSingleCol(strArray("fifth", "51", "5.2")).addSingleCol(strArray("first", "1.1", "1.2", "1.3")).addSingleCol(strArray("third", "3.1")).build();
// Build the hyper batch
HyperRowSet hyperSet = HyperRowSetImpl.fromRowSets(fixture.allocator(), rowSet1, rowSet2);
assertEquals(6, hyperSet.rowCount());
SelectionVector4 sv4 = hyperSet.getSv4();
sv4.set(0, 1, 1);
sv4.set(1, 0, 1);
sv4.set(2, 1, 2);
sv4.set(3, 0, 2);
sv4.set(4, 1, 0);
sv4.set(5, 0, 0);
SingleRowSet expected = fixture.rowSetBuilder(schema).addSingleCol(strArray("first", "1.1", "1.2", "1.3")).addSingleCol(strArray("second", "2.1", "2.2", "2.3")).addSingleCol(strArray("third", "3.1")).addSingleCol(strArray("fourth", "4.1")).addSingleCol(strArray("fifth", "51", "5.2")).addSingleCol(strArray("sixth", "6.1", "6.2")).build();
RowSetUtilities.verify(expected, hyperSet);
}
Aggregations