use of org.apache.drill.exec.record.metadata.SchemaBuilder in project drill by apache.
the class TestResultSetLoaderMaps method testOverwriteRow.
/**
* Version of the {#link TestResultSetLoaderProtocol#testOverwriteRow()} test
* that uses nested columns.
*/
@Test
public void testOverwriteRow() {
final TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").add("b", MinorType.INT).add("c", MinorType.VARCHAR).resumeSchema().buildSchema();
final ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).rowCountLimit(ValueVector.MAX_ROW_COUNT).build();
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
final RowSetLoader rootWriter = rsLoader.writer();
// Can't use the shortcut to populate rows when doing overwrites.
final ScalarWriter aWriter = rootWriter.scalar("a");
final TupleWriter mWriter = rootWriter.tuple("m");
final ScalarWriter bWriter = mWriter.scalar("b");
final ScalarWriter cWriter = mWriter.scalar("c");
// Write 100,000 rows, overwriting 99% of them. This will cause vector
// overflow and data corruption if overwrite does not work; but will happily
// produce the correct result if everything works as it should.
final byte[] value = new byte[512];
Arrays.fill(value, (byte) 'X');
int count = 0;
rsLoader.startBatch();
while (count < 100_000) {
rootWriter.start();
count++;
aWriter.setInt(count);
bWriter.setInt(count * 10);
cWriter.setBytes(value, value.length);
if (count % 100 == 0) {
rootWriter.save();
}
}
// Verify using a reader.
final RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(count / 100, result.rowCount());
final RowSetReader reader = result.reader();
final TupleReader mReader = reader.tuple("m");
int rowId = 1;
while (reader.next()) {
assertEquals(rowId * 100, reader.scalar("a").getInt());
assertEquals(rowId * 1000, mReader.scalar("b").getInt());
assertTrue(Arrays.equals(value, mReader.scalar("c").getBytes()));
rowId++;
}
result.clear();
rsLoader.close();
}
use of org.apache.drill.exec.record.metadata.SchemaBuilder in project drill by apache.
the class TestResultSetLoaderMaps method testMapWithOverflow.
/**
* Create a schema with a map, then trigger an overflow on one of the columns
* in the map. Proper overflow handling should occur regardless of nesting
* depth.
*/
@Test
public void testMapWithOverflow() {
final TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMap("m1").add("b", MinorType.INT).addMap("m2").add("c", // Before overflow, written
MinorType.INT).add("d", MinorType.VARCHAR).add("e", // After overflow, not yet written
MinorType.INT).resumeMap().resumeSchema().buildSchema();
final ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).rowCountLimit(ValueVector.MAX_ROW_COUNT).build();
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
final RowSetLoader rootWriter = rsLoader.writer();
final byte[] value = new byte[512];
Arrays.fill(value, (byte) 'X');
int count = 0;
rsLoader.startBatch();
while (!rootWriter.isFull()) {
rootWriter.addRow(count, mapValue(count * 10, mapValue(count * 100, value, count * 1000)));
count++;
}
// Our row count should include the overflow row
final int expectedCount = ValueVector.MAX_BUFFER_SIZE / value.length;
assertEquals(expectedCount + 1, count);
// Loader's row count should include only "visible" rows
assertEquals(expectedCount, rootWriter.rowCount());
// Total count should include invisible and look-ahead rows.
assertEquals(expectedCount + 1, rsLoader.totalRowCount());
// Result should exclude the overflow row
RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(expectedCount, result.rowCount());
// Ensure the odd map vector value count variable is set correctly.
final MapVector m1Vector = (MapVector) result.container().getValueVector(1).getValueVector();
assertEquals(expectedCount, m1Vector.getAccessor().getValueCount());
final MapVector m2Vector = (MapVector) m1Vector.getChildByOrdinal(1);
assertEquals(expectedCount, m2Vector.getAccessor().getValueCount());
result.clear();
// Next batch should start with the overflow row
rsLoader.startBatch();
assertEquals(1, rootWriter.rowCount());
assertEquals(expectedCount + 1, rsLoader.totalRowCount());
result = fixture.wrap(rsLoader.harvest());
assertEquals(1, result.rowCount());
result.clear();
rsLoader.close();
}
use of org.apache.drill.exec.record.metadata.SchemaBuilder in project drill by apache.
the class TestResultSetLoaderMaps method testMapAddition.
/**
* Test adding a map to a loader after writing the first row.
*/
@Test
public void testMapAddition() {
final TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).buildSchema();
final ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).build();
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
assertEquals(1, rsLoader.schemaVersion());
final RowSetLoader rootWriter = rsLoader.writer();
// Start without the map. Add a map after the first row.
rsLoader.startBatch();
rootWriter.addRow(10);
final int mapIndex = rootWriter.addColumn(SchemaBuilder.columnSchema("m", MinorType.MAP, DataMode.REQUIRED));
final TupleWriter mapWriter = rootWriter.tuple(mapIndex);
// Add a column to the map with the same name as the top-level column.
// Verifies that the name spaces are independent.
final int colIndex = mapWriter.addColumn(SchemaBuilder.columnSchema("a", MinorType.VARCHAR, DataMode.REQUIRED));
assertEquals(0, colIndex);
// Ensure metadata was added
assertTrue(mapWriter.tupleSchema().size() == 1);
assertSame(mapWriter.tupleSchema(), mapWriter.schema().tupleSchema());
assertSame(mapWriter.tupleSchema().metadata(colIndex), mapWriter.scalar(colIndex).schema());
rootWriter.addRow(20, mapValue("fred")).addRow(30, mapValue("barney"));
final RowSet actual = fixture.wrap(rsLoader.harvest());
assertEquals(3, rsLoader.schemaVersion());
assertEquals(3, actual.rowCount());
final MapVector mapVector = (MapVector) actual.container().getValueVector(1).getValueVector();
final MaterializedField mapField = mapVector.getField();
assertEquals(1, mapField.getChildren().size());
assertTrue(mapWriter.scalar(colIndex).schema().schema().isEquivalent(mapField.getChildren().iterator().next()));
// Validate first batch
final TupleMetadata expectedSchema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").add("a", MinorType.VARCHAR).resumeSchema().buildSchema();
final SingleRowSet expected = fixture.rowSetBuilder(expectedSchema).addRow(10, mapValue("")).addRow(20, mapValue("fred")).addRow(30, mapValue("barney")).build();
RowSetUtilities.verify(expected, actual);
rsLoader.close();
}
use of org.apache.drill.exec.record.metadata.SchemaBuilder in project drill by apache.
the class TestFillEmpties method testBitDefaultValue.
@Test
public void testBitDefaultValue() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.BIT).buildSchema();
ExtendableRowSet rs = fixture.rowSet(schema);
RowSetWriter writer = rs.writer();
ScalarWriter colWriter = writer.scalar(0);
colWriter.setDefaultValue(true);
for (int i = 0; i < ROW_COUNT; i++) {
if (i % 43 == 0) {
colWriter.setInt(0);
}
writer.save();
}
SingleRowSet result = writer.done();
RowSetReader reader = result.reader();
ScalarReader colReader = reader.scalar(0);
for (int i = 0; i < ROW_COUNT; i++) {
assertTrue(reader.next());
assertEquals(i % 43 == 0 ? 0 : 1, colReader.getInt());
}
result.clear();
}
use of org.apache.drill.exec.record.metadata.SchemaBuilder in project drill by apache.
the class TestFillEmpties method testDefaultInSchema.
/**
* Test the more typical case in which the default value is set in the
* column metadata. The reader mechanism will automatically set the default
* for the column writer from the (properly formed) default value in the
* column metadata.
*/
@Test
public void testDefaultInSchema() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).buildSchema();
schema.metadata("a").setDefaultValue("11");
ExtendableRowSet rs = fixture.rowSet(schema);
RowSetWriter writer = rs.writer();
ScalarWriter colWriter = writer.scalar(0);
ValueType valueType = colWriter.extendedType();
for (int i = 0; i < ROW_COUNT; i++) {
if (i % 5 == 0) {
colWriter.setInt(i);
}
writer.save();
}
SingleRowSet result = writer.done();
RowSetReader reader = result.reader();
ScalarReader colReader = reader.scalar(0);
Object defaultValue = schema.metadata("a").decodeDefaultValue();
assertNotNull(defaultValue);
for (int i = 0; i < ROW_COUNT; i++) {
assertTrue(reader.next());
Object actual = colReader.getValue();
Object expected = i % 5 == 0 ? i : defaultValue;
RowSetUtilities.assertEqualValues(MinorType.INT.toString() + "[" + i + "]", valueType, expected, actual);
}
result.clear();
}
Aggregations