use of org.apache.drill.exec.vector.accessor.ScalarWriter 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.ScalarWriter 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.ScalarWriter 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();
}
use of org.apache.drill.exec.vector.accessor.ScalarWriter in project drill by apache.
the class XMLReader method writeAttributeData.
/**
* Writes a attribute. If the field does not have a corresponding ScalarWriter, this method will
* create one.
* @param fieldName The field name
* @param fieldValue The field value to be written
* @param writer The TupleWriter which represents
*/
private void writeAttributeData(String fieldName, String fieldValue, TupleWriter writer) {
if (fieldName == null) {
return;
}
// Find the TupleWriter object
int index = writer.tupleSchema().index(fieldName);
if (index == -1) {
ColumnMetadata colSchema = MetadataUtils.newScalar(fieldName, TypeProtos.MinorType.VARCHAR, TypeProtos.DataMode.OPTIONAL);
index = writer.addColumn(colSchema);
}
ScalarWriter colWriter = writer.scalar(index);
if (fieldValue != null) {
colWriter.setString(fieldValue);
}
}
use of org.apache.drill.exec.vector.accessor.ScalarWriter in project drill by apache.
the class HttpdLogRecord method setDate.
/**
* This method is referenced and called via reflection. This is added as a parsing target for the parser. It will get
* called when the value of a log field is a Date data type.
*
* @param field name of field
* @param value value of field
*/
@SuppressWarnings("unused")
public void setDate(String field, String value) {
if (value != null) {
final ScalarWriter w = dates.get(field);
if (w != null) {
logger.debug("Parsed field: {}, as long: {}", field, value);
w.setDate(LocalDate.parse(value));
} else {
logger.warn("No 'date' writer found for field: {}", field);
}
}
}
Aggregations