use of org.apache.drill.exec.vector.accessor.ScalarWriter in project drill by apache.
the class ShpBatchReader method writeIntColumn.
private void writeIntColumn(TupleWriter rowWriter, String name, int value) {
int index = rowWriter.tupleSchema().index(name);
if (index == -1) {
ColumnMetadata colSchema = MetadataUtils.newScalar(name, TypeProtos.MinorType.INT, TypeProtos.DataMode.OPTIONAL);
index = rowWriter.addColumn(colSchema);
}
ScalarWriter colWriter = rowWriter.scalar(index);
colWriter.setInt(value);
}
use of org.apache.drill.exec.vector.accessor.ScalarWriter in project drill by apache.
the class ShpBatchReader method writeTimeColumn.
private void writeTimeColumn(TupleWriter rowWriter, String name, long value) {
int index = rowWriter.tupleSchema().index(name);
Instant instant = Instant.ofEpochMilli(value);
if (index == -1) {
ColumnMetadata colSchema = MetadataUtils.newScalar(name, TypeProtos.MinorType.INT, TypeProtos.DataMode.OPTIONAL);
index = rowWriter.addColumn(colSchema);
}
ScalarWriter colWriter = rowWriter.scalar(index);
colWriter.setTimestamp(instant);
}
use of org.apache.drill.exec.vector.accessor.ScalarWriter in project drill by apache.
the class CompliantTextBatchReader method buildWithSchema.
/**
* File has headers and a provided schema is provided. Convert from VARCHAR
* input type to the provided output type, but only if the column is projected.
*/
private FieldVarCharOutput buildWithSchema(ColumnsSchemaNegotiator schemaNegotiator, String[] fieldNames) {
TupleMetadata readerSchema = mergeSchemas(schemaNegotiator.providedSchema(), fieldNames);
schemaNegotiator.tableSchema(readerSchema, true);
writer = schemaNegotiator.build().writer();
StandardConversions conversions = conversions(schemaNegotiator.providedSchema());
ValueWriter[] colWriters = new ValueWriter[fieldNames.length];
for (int i = 0; i < fieldNames.length; i++) {
ScalarWriter colWriter = writer.scalar(fieldNames[i]);
if (writer.isProjected()) {
colWriters[i] = conversions.converterFor(colWriter, MinorType.VARCHAR);
} else {
colWriters[i] = colWriter;
}
}
return new FieldVarCharOutput(writer, colWriters);
}
use of org.apache.drill.exec.vector.accessor.ScalarWriter in project drill by apache.
the class TestVariantAccessors method testUnionWithMap.
/**
* Test a variant (AKA "union vector") at the top level which
* includes a map.
*/
@Test
public void testUnionWithMap() {
final TupleMetadata schema = new SchemaBuilder().addUnion("u").addType(MinorType.VARCHAR).addMap().addNullable("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).resumeUnion().resumeSchema().buildSchema();
SingleRowSet result;
// Write values
{
final ExtendableRowSet rs = fixture.rowSet(schema);
final RowSetWriter writer = rs.writer();
// Sanity check of writer structure
final ObjectWriter wo = writer.column(0);
assertEquals(ObjectType.VARIANT, wo.type());
final VariantWriter vw = wo.variant();
assertTrue(vw.hasType(MinorType.VARCHAR));
final ObjectWriter strObj = vw.member(MinorType.VARCHAR);
final ScalarWriter strWriter = strObj.scalar();
assertSame(strWriter, vw.scalar(MinorType.VARCHAR));
assertTrue(vw.hasType(MinorType.MAP));
final ObjectWriter mapObj = vw.member(MinorType.MAP);
final TupleWriter mWriter = mapObj.tuple();
assertSame(mWriter, vw.tuple());
final ScalarWriter aWriter = mWriter.scalar("a");
final ScalarWriter bWriter = mWriter.scalar("b");
// First row: string "first"
vw.setType(MinorType.VARCHAR);
strWriter.setString("first");
writer.save();
// Second row: a map
vw.setType(MinorType.MAP);
aWriter.setInt(20);
bWriter.setString("fred");
writer.save();
// Third row: null
vw.setNull();
writer.save();
// Fourth row: map with a null string
vw.setType(MinorType.MAP);
aWriter.setInt(40);
bWriter.setNull();
writer.save();
// Fifth row: string "last"
vw.setType(MinorType.VARCHAR);
strWriter.setString("last");
writer.save();
result = writer.done();
assertEquals(5, result.rowCount());
}
// Read the values.
{
final RowSetReader reader = result.reader();
// Sanity check of structure
final ObjectReader ro = reader.column(0);
assertEquals(ObjectType.VARIANT, ro.type());
final VariantReader vr = ro.variant();
assertTrue(vr.hasType(MinorType.VARCHAR));
final ObjectReader strObj = vr.member(MinorType.VARCHAR);
final ScalarReader strReader = strObj.scalar();
assertSame(strReader, vr.scalar(MinorType.VARCHAR));
assertTrue(vr.hasType(MinorType.MAP));
final ObjectReader mapObj = vr.member(MinorType.MAP);
final TupleReader mReader = mapObj.tuple();
assertSame(mReader, vr.tuple());
final ScalarReader aReader = mReader.scalar("a");
final ScalarReader bReader = mReader.scalar("b");
// First row: string "first"
assertTrue(reader.next());
assertFalse(vr.isNull());
assertEquals(MinorType.VARCHAR, vr.dataType());
assertFalse(strReader.isNull());
assertTrue(mReader.isNull());
assertEquals("first", strReader.getString());
// Second row: a map
assertTrue(reader.next());
assertFalse(vr.isNull());
assertEquals(MinorType.MAP, vr.dataType());
assertTrue(strReader.isNull());
assertFalse(mReader.isNull());
assertFalse(aReader.isNull());
assertEquals(20, aReader.getInt());
assertFalse(bReader.isNull());
assertEquals("fred", bReader.getString());
// Third row: null
assertTrue(reader.next());
assertTrue(vr.isNull());
assertTrue(strReader.isNull());
assertTrue(mReader.isNull());
assertTrue(aReader.isNull());
assertTrue(bReader.isNull());
// Fourth row: map with a null string
assertTrue(reader.next());
assertEquals(MinorType.MAP, vr.dataType());
assertEquals(40, aReader.getInt());
assertTrue(bReader.isNull());
// Fifth row: string "last"
assertTrue(reader.next());
assertEquals(MinorType.VARCHAR, vr.dataType());
assertEquals("last", strReader.getString());
assertFalse(reader.next());
}
result.clear();
}
use of org.apache.drill.exec.vector.accessor.ScalarWriter in project drill by apache.
the class TestVariantAccessors method testUnionWithList.
/**
* Test a variant (AKA "union vector") at the top level which includes
* a list.
*/
@Test
public void testUnionWithList() {
final TupleMetadata schema = new SchemaBuilder().addUnion("u").addType(MinorType.INT).addList().addType(MinorType.VARCHAR).resumeUnion().resumeSchema().buildSchema();
SingleRowSet result;
// Write values
{
final ExtendableRowSet rs = fixture.rowSet(schema);
final RowSetWriter writer = rs.writer();
final VariantWriter vw = writer.variant("u");
assertTrue(vw.hasType(MinorType.INT));
final ScalarWriter intWriter = vw.scalar(MinorType.INT);
assertTrue(vw.hasType(MinorType.LIST));
final ArrayWriter aWriter = vw.array();
final ScalarWriter strWriter = aWriter.scalar();
// Row 1: 1, ["fred", "barney"]
intWriter.setInt(1);
strWriter.setString("fred");
aWriter.save();
strWriter.setString("barney");
aWriter.save();
writer.save();
// Row 2, 2, ["wilma", "betty"]
intWriter.setInt(2);
strWriter.setString("wilma");
aWriter.save();
strWriter.setString("betty");
aWriter.save();
writer.save();
result = writer.done();
assertEquals(2, result.rowCount());
}
// Read the values.
{
final RowSetReader reader = result.reader();
final VariantReader vr = reader.variant("u");
assertTrue(vr.hasType(MinorType.INT));
final ScalarReader intReader = vr.scalar(MinorType.INT);
assertTrue(vr.hasType(MinorType.LIST));
final ArrayReader aReader = vr.array();
final ScalarReader strReader = aReader.scalar();
assertTrue(reader.next());
assertEquals(1, intReader.getInt());
assertEquals(2, aReader.size());
assertTrue(aReader.next());
assertEquals("fred", strReader.getString());
assertTrue(aReader.next());
assertEquals("barney", strReader.getString());
assertFalse(aReader.next());
assertTrue(reader.next());
assertEquals(2, intReader.getInt());
assertEquals(2, aReader.size());
assertTrue(aReader.next());
assertEquals("wilma", strReader.getString());
assertTrue(aReader.next());
assertEquals("betty", strReader.getString());
assertFalse(aReader.next());
assertFalse(reader.next());
}
result.clear();
}
Aggregations