use of org.apache.drill.exec.vector.accessor.ValueType in project drill by axbaretto.
the class TestFillEmpties method doFillEmptiesScalar.
private void doFillEmptiesScalar(MajorType majorType) {
TupleMetadata schema = new SchemaBuilder().add("a", majorType).buildSchema();
ExtendableRowSet rs = fixture.rowSet(schema);
RowSetWriter writer = rs.writer();
ScalarWriter colWriter = writer.scalar(0);
ValueType valueType = colWriter.valueType();
boolean nullable = majorType.getMode() == DataMode.OPTIONAL;
for (int i = 0; i < ROW_COUNT; i++) {
if (i % 5 == 0) {
colWriter.setObject(RowSetUtilities.testDataFromInt(valueType, majorType, i));
}
writer.save();
}
SingleRowSet result = writer.done();
RowSetReader reader = result.reader();
ScalarReader colReader = reader.scalar(0);
MinorType type = majorType.getMinorType();
boolean isVariable = (type == MinorType.VARCHAR || type == MinorType.VAR16CHAR || type == MinorType.VARBINARY);
for (int i = 0; i < ROW_COUNT; i++) {
assertTrue(reader.next());
if (i % 5 != 0) {
if (nullable) {
// Nullable types fill with nulls.
assertTrue(colReader.isNull());
continue;
}
if (isVariable) {
// Variable width types fill with a zero-length value.
assertEquals(0, colReader.getBytes().length);
continue;
}
}
// All other types fill with zero-bytes, interpreted as some form
// of zero for each type.
Object actual = colReader.getObject();
Object expected = RowSetUtilities.testDataFromInt(valueType, majorType, i % 5 == 0 ? i : 0);
RowSetUtilities.assertEqualValues(majorType.toString().replace('\n', ' ') + "[" + i + "]", valueType, expected, actual);
}
result.clear();
}
use of org.apache.drill.exec.vector.accessor.ValueType in project drill by axbaretto.
the class JsonFileBuilder method build.
public void build(File tableFile) throws IOException {
tableFile.getParentFile().mkdirs();
try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(tableFile))) {
final RowSetReader reader = rowSet.reader();
final int numCols = rowSet.batchSchema().getFieldCount();
final Iterator<MaterializedField> fieldIterator = rowSet.batchSchema().iterator();
final List<String> columnNames = Lists.newArrayList();
final List<String> columnFormatters = Lists.newArrayList();
// Build formatters from first row.
while (fieldIterator.hasNext()) {
final String columnName = fieldIterator.next().getName();
final ScalarReader columnReader = reader.scalar(columnName);
final ValueType valueType = columnReader.valueType();
final String columnFormatter;
if (customFormatters.containsKey(columnName)) {
columnFormatter = customFormatters.get(columnName);
} else if (DEFAULT_FORMATTERS.containsKey(valueType)) {
columnFormatter = DEFAULT_FORMATTERS.get(valueType);
} else {
final String message = String.format("Unsupported column type %s", valueType);
throw new UnsupportedOperationException(message);
}
columnNames.add(columnName);
columnFormatters.add(columnFormatter);
}
final StringBuilder sb = new StringBuilder();
String lineSeparator = "";
for (int index = 0; index < rowSet.rowCount(); index++) {
reader.next();
sb.append(lineSeparator);
sb.append('{');
String separator = "";
for (int columnIndex = 0; columnIndex < numCols; columnIndex++) {
sb.append(separator);
final String columnName = columnNames.get(columnIndex);
final ScalarReader columnReader = reader.scalar(columnIndex);
final String columnFormatter = columnFormatters.get(columnIndex);
final Object columnObject = columnReader.getObject();
final String columnString = String.format(columnFormatter, columnObject);
sb.append('"').append(columnName).append('"').append(':').append(columnString);
separator = ",";
}
sb.append('}');
lineSeparator = "\n";
os.write(sb.toString().getBytes());
sb.delete(0, sb.length());
}
}
}
use of org.apache.drill.exec.vector.accessor.ValueType 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();
}
use of org.apache.drill.exec.vector.accessor.ValueType in project drill by apache.
the class TestFillEmpties method dofillEmptiesRepeated.
private void dofillEmptiesRepeated(MajorType majorType) {
TupleMetadata schema = new SchemaBuilder().add("a", majorType).buildSchema();
ExtendableRowSet rs = fixture.rowSet(schema);
RowSetWriter writer = rs.writer();
ScalarWriter colWriter = writer.array(0).scalar();
ValueType valueType = colWriter.valueType();
for (int i = 0; i < ROW_COUNT; i++) {
if (i % 5 == 0) {
// Write two values so we can exercise a bit of the array logic.
colWriter.setObject(RowSetUtilities.testDataFromInt(valueType, majorType, i));
colWriter.setObject(RowSetUtilities.testDataFromInt(valueType, majorType, i + 1));
}
writer.save();
}
SingleRowSet result = writer.done();
RowSetReader reader = result.reader();
ArrayReader aReader = reader.array(0);
ScalarReader colReader = aReader.scalar();
for (int i = 0; i < ROW_COUNT; i++) {
assertTrue(reader.next());
if (i % 5 != 0) {
// Empty arrays are defined to be the same as a zero-length array.
assertEquals(0, aReader.size());
} else {
for (int j = 0; j < 2; j++) {
assertTrue(aReader.next());
Object actual = colReader.getObject();
Object expected = RowSetUtilities.testDataFromInt(valueType, majorType, i + j);
RowSetUtilities.assertEqualValues(majorType.toString().replace('\n', ' ') + "[" + i + "][" + j + "]", valueType, expected, actual);
}
}
}
result.clear();
}
use of org.apache.drill.exec.vector.accessor.ValueType in project drill by apache.
the class TestFillEmpties method doTestDefaultValue.
private void doTestDefaultValue(MajorType majorType) {
TupleMetadata schema = new SchemaBuilder().add("a", majorType).buildSchema();
ExtendableRowSet rs = fixture.rowSet(schema);
RowSetWriter writer = rs.writer();
ScalarWriter colWriter = writer.scalar(0);
ValueType valueType = colWriter.extendedType();
Object defaultValue = RowSetUtilities.testDataFromInt(valueType, majorType, 100);
colWriter.setDefaultValue(defaultValue);
for (int i = 0; i < ROW_COUNT; i++) {
if (i % 5 == 0) {
colWriter.setObject(RowSetUtilities.testDataFromInt(valueType, majorType, i));
}
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());
Object actual = colReader.getValue();
Object expected = i % 5 == 0 ? RowSetUtilities.testDataFromInt(valueType, majorType, i) : defaultValue;
RowSetUtilities.assertEqualValues(majorType.toString().replace('\n', ' ') + "[" + i + "]", valueType, expected, actual);
}
result.clear();
}
Aggregations