use of org.apache.drill.exec.vector.accessor.ScalarElementReader in project drill by axbaretto.
the class TestScalarAccessors method doubleArrayTester.
private void doubleArrayTester(MinorType type) {
BatchSchema batchSchema = new SchemaBuilder().addArray("col", type).build();
SingleRowSet rs = fixture.rowSetBuilder(batchSchema).addSingleCol(new double[] {}).addSingleCol(new double[] { 0, 20.5, 30.0 }).build();
assertEquals(2, rs.rowCount());
RowSetReader reader = rs.reader();
ScalarElementReader colReader = reader.elements(0);
assertEquals(ValueType.DOUBLE, colReader.valueType());
assertTrue(reader.next());
assertEquals(0, colReader.size());
assertTrue(reader.next());
assertEquals(3, colReader.size());
assertEquals(0, colReader.getDouble(0), 0.00001);
assertEquals(20.5, colReader.getDouble(1), 0.00001);
assertEquals(30.0, colReader.getDouble(2), 0.00001);
assertEquals(0, (double) colReader.getObject(0), 0.00001);
assertEquals(20.5, (double) colReader.getObject(1), 0.00001);
assertEquals(30.0, (double) colReader.getObject(2), 0.00001);
assertEquals("0.0", colReader.getAsString(0));
assertEquals("20.5", colReader.getAsString(1));
assertEquals("30.0", colReader.getAsString(2));
assertFalse(reader.next());
rs.clear();
}
use of org.apache.drill.exec.vector.accessor.ScalarElementReader in project drill by axbaretto.
the class RowSetTest method testMapStructure.
/**
* Test a simple map structure at the top level of a row.
*
* @throws VectorOverflowException should never occur
*/
@Test
public void testMapStructure() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").addArray("b", MinorType.INT).resumeSchema().buildSchema();
ExtendableRowSet rowSet = fixture.rowSet(schema);
RowSetWriter writer = rowSet.writer();
// Map and Int
// Test Invariants
assertEquals(ObjectType.SCALAR, writer.column("a").type());
assertEquals(ObjectType.SCALAR, writer.column(0).type());
assertEquals(ObjectType.TUPLE, writer.column("m").type());
assertEquals(ObjectType.TUPLE, writer.column(1).type());
assertSame(writer.column(1).tuple(), writer.tuple(1));
TupleWriter mapWriter = writer.column(1).tuple();
assertEquals(ObjectType.SCALAR, mapWriter.column("b").array().entry().type());
assertEquals(ObjectType.SCALAR, mapWriter.column("b").array().entryType());
ScalarWriter aWriter = writer.column("a").scalar();
ScalarWriter bWriter = writer.column("m").tuple().column("b").array().entry().scalar();
assertSame(bWriter, writer.tuple(1).array(0).scalar());
assertEquals(ValueType.INTEGER, bWriter.valueType());
try {
writer.column(1).scalar();
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
try {
writer.column(1).array();
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
// Write data
aWriter.setInt(10);
bWriter.setInt(11);
bWriter.setInt(12);
writer.save();
aWriter.setInt(20);
bWriter.setInt(21);
bWriter.setInt(22);
writer.save();
aWriter.setInt(30);
bWriter.setInt(31);
bWriter.setInt(32);
writer.save();
// Finish the row set and get a reader.
SingleRowSet actual = writer.done();
RowSetReader reader = actual.reader();
assertEquals(ObjectType.SCALAR, reader.column("a").type());
assertEquals(ObjectType.SCALAR, reader.column(0).type());
assertEquals(ObjectType.TUPLE, reader.column("m").type());
assertEquals(ObjectType.TUPLE, reader.column(1).type());
assertSame(reader.column(1).tuple(), reader.tuple(1));
ScalarReader aReader = reader.column(0).scalar();
TupleReader mReader = reader.column(1).tuple();
assertEquals(ObjectType.SCALAR, mReader.column("b").array().entryType());
ScalarElementReader bReader = mReader.column(0).elements();
assertEquals(ValueType.INTEGER, bReader.valueType());
assertTrue(reader.next());
assertEquals(10, aReader.getInt());
assertEquals(11, bReader.getInt(0));
assertEquals(12, bReader.getInt(1));
assertTrue(reader.next());
assertEquals(20, aReader.getInt());
assertEquals(21, bReader.getInt(0));
assertEquals(22, bReader.getInt(1));
assertTrue(reader.next());
assertEquals(30, aReader.getInt());
assertEquals(31, bReader.getInt(0));
assertEquals(32, bReader.getInt(1));
assertFalse(reader.next());
// Verify that the map accessor's value count was set.
@SuppressWarnings("resource") MapVector mapVector = (MapVector) actual.container().getValueVector(1).getValueVector();
assertEquals(actual.rowCount(), mapVector.getAccessor().getValueCount());
SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, objArray(intArray(11, 12))).addRow(20, objArray(intArray(21, 22))).addRow(30, objArray(intArray(31, 32))).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
}
use of org.apache.drill.exec.vector.accessor.ScalarElementReader in project drill by axbaretto.
the class TestResultSetLoaderOverflow method testMissingArrayValues.
/**
* Test the case that an array has "missing values" before the overflow.
*/
@Test
public void testMissingArrayValues() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).add("b", MinorType.VARCHAR).addArray("c", MinorType.INT).buildSchema();
ResultSetOptions options = new OptionBuilder().setRowCountLimit(ValueVector.MAX_ROW_COUNT).setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
byte[] value = new byte[512];
Arrays.fill(value, (byte) 'X');
int blankAfter = ValueVector.MAX_BUFFER_SIZE / 512 * 2 / 3;
ScalarWriter cWriter = rootWriter.array("c").scalar();
rsLoader.startBatch();
int rowId = 0;
while (rootWriter.start()) {
rootWriter.scalar("a").setInt(rowId);
rootWriter.scalar("b").setBytes(value, value.length);
if (rowId < blankAfter) {
for (int i = 0; i < 3; i++) {
cWriter.setInt(rowId * 3 + i);
}
}
rootWriter.save();
rowId++;
}
RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(rowId - 1, result.rowCount());
RowSetReader reader = result.reader();
ScalarElementReader cReader = reader.array("c").elements();
while (reader.next()) {
assertEquals(reader.rowIndex(), reader.scalar("a").getInt());
assertTrue(Arrays.equals(value, reader.scalar("b").getBytes()));
if (reader.rowIndex() < blankAfter) {
assertEquals(3, cReader.size());
for (int i = 0; i < 3; i++) {
assertEquals(reader.rowIndex() * 3 + i, cReader.getInt(i));
}
} else {
assertEquals(0, cReader.size());
}
}
result.clear();
rsLoader.close();
}
use of org.apache.drill.exec.vector.accessor.ScalarElementReader in project drill by axbaretto.
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 OptionBuilder().setRowCountLimit(ValueVector.MAX_ROW_COUNT).setSchema(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()) {
if (rootWriter.rowCount() == 2952) {
count = count + 0;
}
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
RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(count - 1, result.rowCount());
RowSetReader reader = result.reader();
ScalarElementReader aReader = reader.array("a").elements();
ScalarElementReader bReader = reader.array("b").elements();
ScalarElementReader cReader = reader.array("c").elements();
ScalarElementReader dReader = reader.array("d").elements();
while (reader.next()) {
int rowId = reader.rowIndex();
assertEquals(aCount, aReader.size());
for (int i = 0; i < aCount; i++) {
assertEquals(rowId * aCount + i, aReader.getInt(i));
}
assertEquals(bCount, bReader.size());
for (int i = 0; i < bCount; i++) {
String cellValue = strValue + (rowId * bCount + i);
assertEquals(cellValue, bReader.getString(i));
}
if (rowId < cCutoff) {
assertEquals(cCount, cReader.size());
for (int i = 0; i < cCount; i++) {
assertEquals(rowId * cCount + i, cReader.getInt(i));
}
assertEquals(dCount, dReader.size());
for (int i = 0; i < dCount; i++) {
assertEquals(rowId * dCount + i, dReader.getInt(i));
}
} else {
assertEquals(0, cReader.size());
assertEquals(0, dReader.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++;
}
result = fixture.wrap(rsLoader.harvest());
assertEquals(6, result.rowCount());
reader = result.reader();
aReader = reader.array("a").elements();
bReader = reader.array("b").elements();
cReader = reader.array("c").elements();
dReader = reader.array("d").elements();
int j = 0;
while (reader.next()) {
int rowId = firstCount + reader.rowIndex();
assertEquals(aCount, aReader.size());
for (int i = 0; i < aCount; i++) {
assertEquals("Index " + i, rowId * aCount + i, aReader.getInt(i));
}
assertEquals(bCount, bReader.size());
for (int i = 0; i < bCount; i++) {
String cellValue = strValue + (rowId * bCount + i);
assertEquals(cellValue, bReader.getString(i));
}
if (j > 4) {
assertEquals(cCount, cReader.size());
for (int i = 0; i < cCount; i++) {
assertEquals(rowId * cCount + i, cReader.getInt(i));
}
} else {
assertEquals(0, cReader.size());
}
if (j == 0 || j > 4) {
assertEquals(dCount, dReader.size());
for (int i = 0; i < dCount; i++) {
assertEquals(rowId * dCount + i, dReader.getInt(i));
}
} else {
assertEquals(0, dReader.size());
}
j++;
}
result.clear();
rsLoader.close();
}
use of org.apache.drill.exec.vector.accessor.ScalarElementReader in project drill by axbaretto.
the class TestResultSetLoaderMapArray method testDoubleNestedArray.
/**
* Test a doubly-nested arrays 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();
ResultSetLoaderImpl.ResultSetOptions options = new OptionBuilder().setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
RowSetLoader rootWriter = rsLoader.writer();
rsLoader.startBatch();
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++) {
rootWriter.start();
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 = fixture.wrap(rsLoader.harvest());
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");
ScalarElementReader dReader = m2Reader.elements("d");
for (int i = 0; i < 5; i++) {
reader.next();
assertEquals(i, aReader.getInt());
for (int j = 0; j < 4; j++) {
a1Reader.setPosn(j);
int a1Key = i + 10 + j;
assertEquals(a1Key, bReader.getInt());
for (int k = 0; k < 3; k++) {
a2Reader.setPosn(k);
int a2Key = a1Key * 10 + k;
assertEquals(a2Key, cReader.getInt());
for (int l = 0; l < 2; l++) {
assertEquals("d-" + (a2Key * 10 + l), dReader.getString(l));
}
}
}
}
rsLoader.close();
}
Aggregations