use of org.apache.drill.exec.vector.accessor.ScalarReader in project drill by apache.
the class TestResultSetLoaderOverflow method testArrayOverflowWithOtherArrays.
/**
* Test the complete set of array overflow cases:
* <ul>
* <li>Array a is written before the column that has overflow,
* and must be copied, in its entirety, to the overflow row.</li>
* <li>Column b causes the overflow.</li>
* <li>Column c is written after the overflow, and should go
* to the look-ahead row.</li>
* <li>Column d is written for a while, then has empties before
* the overflow row, but is written in the overflow row.<li>
* <li>Column e is like d, but is not written in the overflow
* row.</li>
*/
@Test
public void testArrayOverflowWithOtherArrays() {
TupleMetadata schema = new SchemaBuilder().addArray("a", MinorType.INT).addArray("b", MinorType.VARCHAR).addArray("c", MinorType.INT).addArray("d", MinorType.INT).buildSchema();
ResultSetOptions options = new ResultSetOptionBuilder().rowCountLimit(ValueVector.MAX_ROW_COUNT).readerSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
// Fill batch with rows of with a single array, three values each. Tack on
// a suffix to each so we can be sure the proper data is written and moved
// to the overflow batch.
byte[] value = new byte[512];
Arrays.fill(value, (byte) 'X');
String strValue = new String(value, Charsets.UTF_8);
int aCount = 3;
int bCount = 11;
int cCount = 5;
int dCount = 7;
int cCutoff = ValueVector.MAX_BUFFER_SIZE / value.length / bCount / 2;
ScalarWriter aWriter = rootWriter.array("a").scalar();
ScalarWriter bWriter = rootWriter.array("b").scalar();
ScalarWriter cWriter = rootWriter.array("c").scalar();
ScalarWriter dWriter = rootWriter.array("d").scalar();
int count = 0;
rsLoader.startBatch();
while (rootWriter.start()) {
for (int i = 0; i < aCount; i++) {
aWriter.setInt(count * aCount + i);
}
for (int i = 0; i < bCount; i++) {
String cellValue = strValue + (count * bCount + i);
bWriter.setString(cellValue);
}
if (count < cCutoff) {
for (int i = 0; i < cCount; i++) {
cWriter.setInt(count * cCount + i);
}
}
if (count < cCutoff || rootWriter.isFull()) {
for (int i = 0; i < dCount; i++) {
dWriter.setInt(count * dCount + i);
}
}
rootWriter.save();
count++;
}
// Verify
{
VectorContainer container = rsLoader.harvest();
BatchValidator.validate(container);
RowSet result = fixture.wrap(container);
assertEquals(count - 1, result.rowCount());
RowSetReader reader = result.reader();
ArrayReader aArray = reader.array("a");
ScalarReader aReader = aArray.scalar();
ArrayReader bArray = reader.array("b");
ScalarReader bReader = bArray.scalar();
ArrayReader cArray = reader.array("c");
ScalarReader cReader = cArray.scalar();
ArrayReader dArray = reader.array("d");
ScalarReader dReader = dArray.scalar();
while (reader.next()) {
int rowId = reader.offset();
assertEquals(aCount, aArray.size());
for (int i = 0; i < aCount; i++) {
assertTrue(aArray.next());
assertEquals(rowId * aCount + i, aReader.getInt());
}
assertEquals(bCount, bArray.size());
for (int i = 0; i < bCount; i++) {
assertTrue(bArray.next());
String cellValue = strValue + (rowId * bCount + i);
assertEquals(cellValue, bReader.getString());
}
if (rowId < cCutoff) {
assertEquals(cCount, cArray.size());
for (int i = 0; i < cCount; i++) {
assertTrue(cArray.next());
assertEquals(rowId * cCount + i, cReader.getInt());
}
assertEquals(dCount, dArray.size());
for (int i = 0; i < dCount; i++) {
assertTrue(dArray.next());
assertEquals(rowId * dCount + i, dReader.getInt());
}
} else {
assertEquals(0, cArray.size());
assertEquals(0, dArray.size());
}
}
result.clear();
}
int firstCount = count - 1;
// One row is in the batch. Write more, skipping over the
// initial few values for columns c and d. Column d has a
// roll-over value, c has an empty roll-over.
rsLoader.startBatch();
for (int j = 0; j < 5; j++) {
rootWriter.start();
for (int i = 0; i < aCount; i++) {
aWriter.setInt(count * aCount + i);
}
for (int i = 0; i < bCount; i++) {
String cellValue = strValue + (count * bCount + i);
bWriter.setString(cellValue);
}
if (j > 3) {
for (int i = 0; i < cCount; i++) {
cWriter.setInt(count * cCount + i);
}
for (int i = 0; i < dCount; i++) {
dWriter.setInt(count * dCount + i);
}
}
rootWriter.save();
count++;
}
{
VectorContainer container = rsLoader.harvest();
BatchValidator.validate(container);
RowSet result = fixture.wrap(container);
assertEquals(6, result.rowCount());
RowSetReader reader = result.reader();
ArrayReader aArray = reader.array("a");
ScalarReader aReader = aArray.scalar();
ArrayReader bArray = reader.array("b");
ScalarReader bReader = bArray.scalar();
ArrayReader cArray = reader.array("c");
ScalarReader cReader = cArray.scalar();
ArrayReader dArray = reader.array("d");
ScalarReader dReader = dArray.scalar();
int j = 0;
while (reader.next()) {
int rowId = firstCount + reader.offset();
assertEquals(aCount, aArray.size());
for (int i = 0; i < aCount; i++) {
assertTrue(aArray.next());
assertEquals("Index " + i, rowId * aCount + i, aReader.getInt());
}
assertEquals(bCount, bArray.size());
for (int i = 0; i < bCount; i++) {
assertTrue(bArray.next());
String cellValue = strValue + (rowId * bCount + i);
assertEquals(cellValue, bReader.getString());
}
if (j > 4) {
assertEquals(cCount, cArray.size());
for (int i = 0; i < cCount; i++) {
assertTrue(cArray.next());
assertEquals(rowId * cCount + i, cReader.getInt());
}
} else {
assertEquals(0, cArray.size());
}
if (j == 0 || j > 4) {
assertEquals(dCount, dArray.size());
for (int i = 0; i < dCount; i++) {
assertTrue(dArray.next());
assertEquals(rowId * dCount + i, dReader.getInt());
}
} else {
assertEquals(0, dArray.size());
}
j++;
}
result.clear();
}
rsLoader.close();
}
use of org.apache.drill.exec.vector.accessor.ScalarReader 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.ScalarReader in project drill by apache.
the class TestMapAccessors method testBasics.
@Test
public void testBasics() {
final TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").add("c", MinorType.INT).add("d", MinorType.VARCHAR).resumeSchema().add("e", MinorType.VARCHAR).buildSchema();
RowSetBuilder builder = fixture.rowSetBuilder(schema);
RowSetWriter rootWriter = builder.writer();
// Verify structure and schema
final TupleMetadata actualSchema = rootWriter.tupleSchema();
assertEquals(3, actualSchema.size());
assertTrue(actualSchema.metadata(1).isMap());
assertEquals(2, actualSchema.metadata("m").tupleSchema().size());
assertEquals(2, actualSchema.column("m").getChildren().size());
// Write a row the way that clients will do.
final ScalarWriter aWriter = rootWriter.scalar("a");
final TupleWriter mWriter = rootWriter.tuple("m");
final ScalarWriter cWriter = mWriter.scalar("c");
final ScalarWriter dWriter = mWriter.scalar("d");
final ScalarWriter eWriter = rootWriter.scalar("e");
aWriter.setInt(10);
cWriter.setInt(110);
dWriter.setString("fred");
eWriter.setString("pebbles");
rootWriter.save();
// Write another using the test-time conveniences
rootWriter.addRow(20, mapValue(210, "barney"), "bam-bam");
RowSet result = builder.build();
assertEquals(2, result.rowCount());
// Validate internal structure.
VectorContainer container = result.container();
assertEquals(3, container.getNumberOfColumns());
ValueVector v = container.getValueVector(1).getValueVector();
assertTrue(v instanceof MapVector);
MapVector mv = (MapVector) v;
assertEquals(2, mv.getAccessor().getValueCount());
// Validate data. Do so using the readers to avoid verifying
// using the very mechanisms we want to test.
RowSetReader rootReader = result.reader();
final ScalarReader aReader = rootReader.scalar("a");
final TupleReader mReader = rootReader.tuple("m");
final ScalarReader cReader = mReader.scalar("c");
final ScalarReader dReader = mReader.scalar("d");
final ScalarReader eReader = rootReader.scalar("e");
rootReader.next();
assertEquals(10, aReader.getInt());
assertEquals(110, cReader.getInt());
assertEquals("fred", dReader.getString());
assertEquals("pebbles", eReader.getString());
rootReader.next();
assertEquals(20, aReader.getInt());
assertEquals(210, cReader.getInt());
assertEquals("barney", dReader.getString());
assertEquals("bam-bam", eReader.getString());
// Verify using the convenience methods.
final SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, mapValue(110, "fred"), "pebbles").addRow(20, mapValue(210, "barney"), "bam-bam").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.ScalarReader 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.ScalarReader in project drill by apache.
the class TestScalarAccessors method testNullableVarBinary.
@Test
public void testNullableVarBinary() {
TupleMetadata schema = new SchemaBuilder().addNullable("col", MinorType.VARBINARY).buildSchema();
byte[] v1 = new byte[] {};
byte[] v2 = new byte[] { (byte) 0x00, (byte) 0x7f, (byte) 0x80, (byte) 0xFF };
SingleRowSet rs = fixture.rowSetBuilder(schema).addRow(v1).addSingleCol(null).addRow(v2).build();
assertEquals(3, rs.rowCount());
RowSetReader reader = rs.reader();
ScalarReader colReader = reader.scalar(0);
assertEquals(ValueType.BYTES, colReader.valueType());
assertTrue(reader.next());
assertFalse(colReader.isNull());
assertTrue(Arrays.equals(v1, colReader.getBytes()));
assertTrue(reader.next());
assertTrue(colReader.isNull());
assertNull(colReader.getObject());
assertEquals("null", colReader.getAsString());
assertTrue(reader.next());
assertTrue(Arrays.equals(v2, colReader.getBytes()));
assertFalse(reader.next());
rs.clear();
}
Aggregations