use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.
the class TestVariantAccessors method testListOfUnions.
/**
* Test a union list.
*/
@Test
public void testListOfUnions() {
final TupleMetadata schema = new SchemaBuilder().addList("list").addType(MinorType.INT).addType(MinorType.VARCHAR).resumeSchema().buildSchema();
final ExtendableRowSet rowSet = fixture.rowSet(schema);
final RowSetWriter writer = rowSet.writer();
{
final ObjectWriter listObj = writer.column(0);
assertEquals(ObjectType.ARRAY, listObj.type());
final ArrayWriter listArray = listObj.array();
final ObjectWriter itemObj = listArray.entry();
assertEquals(ObjectType.VARIANT, itemObj.type());
final VariantWriter variant = itemObj.variant();
final ScalarWriter intWriter = variant.scalar(MinorType.INT);
final ScalarWriter strWriter = variant.scalar(MinorType.VARCHAR);
// First row: (1, "two", 3)
variant.setType(MinorType.INT);
intWriter.setInt(1);
listArray.save();
variant.setType(MinorType.VARCHAR);
strWriter.setString("two");
listArray.save();
variant.setType(MinorType.INT);
intWriter.setInt(3);
listArray.save();
writer.save();
// Second row: null
writer.save();
// Third row: 4, null, "six", null int, null string
variant.setType(MinorType.INT);
intWriter.setInt(4);
listArray.save();
variant.setNull();
listArray.save();
variant.setType(MinorType.VARCHAR);
strWriter.setString("six");
listArray.save();
variant.setType(MinorType.INT);
intWriter.setNull();
listArray.save();
variant.setType(MinorType.VARCHAR);
intWriter.setNull();
listArray.save();
writer.save();
// Fourth row: empty array.
listArray.setNull(false);
writer.save();
// Fifth row: 9
variant.setType(MinorType.INT);
intWriter.setInt(9);
listArray.save();
writer.save();
}
final SingleRowSet result = writer.done();
assertEquals(5, result.rowCount());
{
final RowSetReader reader = result.reader();
final ObjectReader listObj = reader.column(0);
assertEquals(ObjectType.ARRAY, listObj.type());
final ArrayReader listArray = listObj.array();
assertEquals(ObjectType.VARIANT, listArray.entry().type());
final VariantReader variant = listArray.variant();
final ScalarReader intReader = variant.scalar(MinorType.INT);
final ScalarReader strReader = variant.scalar(MinorType.VARCHAR);
// First row: (1, "two", 3)
assertTrue(reader.next());
assertFalse(listArray.isNull());
assertEquals(3, listArray.size());
assertTrue(listArray.next());
assertEquals(MinorType.INT, variant.dataType());
assertFalse(intReader.isNull());
assertTrue(strReader.isNull());
assertEquals(1, intReader.getInt());
assertEquals(1, variant.scalar().getInt());
assertTrue(listArray.next());
assertEquals(MinorType.VARCHAR, variant.dataType());
assertTrue(intReader.isNull());
assertFalse(strReader.isNull());
assertEquals("two", strReader.getString());
assertEquals("two", variant.scalar().getString());
assertTrue(listArray.next());
assertEquals(MinorType.INT, variant.dataType());
assertEquals(3, intReader.getInt());
assertFalse(listArray.next());
// Second row: null
assertTrue(reader.next());
assertTrue(listArray.isNull());
assertEquals(0, listArray.size());
// Third row: 4, null, "six", null int, null string
assertTrue(reader.next());
assertEquals(5, listArray.size());
assertTrue(listArray.next());
assertEquals(4, intReader.getInt());
assertTrue(listArray.next());
assertTrue(variant.isNull());
assertTrue(listArray.next());
assertEquals("six", strReader.getString());
assertTrue(listArray.next());
assertEquals(MinorType.INT, variant.dataType());
assertTrue(intReader.isNull());
assertTrue(listArray.next());
assertEquals(MinorType.VARCHAR, variant.dataType());
assertTrue(strReader.isNull());
assertFalse(listArray.next());
// Fourth row: empty array.
assertTrue(reader.next());
assertFalse(listArray.isNull());
assertEquals(0, listArray.size());
assertFalse(listArray.next());
// Fifth row: 9
assertTrue(reader.next());
assertEquals(1, listArray.size());
assertTrue(listArray.next());
assertEquals(9, intReader.getInt());
assertFalse(listArray.next());
assertFalse(reader.next());
}
result.clear();
}
use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.
the class ColumnConverterFactory method getArrayConverter.
private ColumnConverter getArrayConverter(TupleMetadata providedSchema, ColumnMetadata readerSchema, ArrayWriter arrayWriter) {
ObjectWriter valueWriter = arrayWriter.entry();
ColumnConverter valueConverter;
if (readerSchema.isMap()) {
valueConverter = getMapConverter(providedSchema, readerSchema.tupleSchema(), valueWriter.tuple());
} else if (readerSchema.isDict()) {
valueConverter = getDictConverter(providedSchema, readerSchema.tupleSchema(), valueWriter.dict());
} else if (readerSchema.isMultiList()) {
valueConverter = getConverter(null, readerSchema.childSchema(), valueWriter);
} else {
valueConverter = getScalarConverter(readerSchema, valueWriter.scalar());
}
return new ArrayColumnConverter(arrayWriter, valueConverter);
}
use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.
the class BuildFromSchema method buildMap.
private ObjectWriter buildMap(ParentShim parent, ColumnMetadata colSchema) {
final ObjectWriter colWriter = parent.add(colSchema.cloneEmpty());
expandMap(colWriter, colSchema);
return colWriter;
}
use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.
the class BuildFromSchema method buildRepeatedList.
/**
* Expand a repeated list. The list may be multi-dimensional, meaning that
* it may have may layers of other repeated lists before we get to the element
* (inner-most) array.
*
* @param parent tuple writer for the tuple that holds the array
* @param colSchema schema definition of the array
*/
private ObjectWriter buildRepeatedList(ParentShim parent, ColumnMetadata colSchema) {
final ObjectWriter objWriter = parent.add(colSchema.cloneEmpty());
final RepeatedListWriter listWriter = (RepeatedListWriter) objWriter.array();
final ColumnMetadata elements = colSchema.childSchema();
if (elements != null) {
final RepeatedListShim listShim = new RepeatedListShim(listWriter);
buildColumn(listShim, elements);
}
return objWriter;
}
use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.
the class ProvidedFieldFactory method multiDimArrayParserForSchema.
/**
* Create a repeated list column and its multiple levels of inner structure
* from a provided schema. Repeated lists can nest to any number of levels to
* provide any number of dimensions. In general, if an array is <i>n</i>-dimensional,
* then there are <i>n</i>-1 repeated lists with some array type as the
* innermost dimension.
*/
private ElementParser multiDimArrayParserForSchema(FieldDefn fieldDefn) {
// Parse the stack of repeated lists to count the "outer" dimensions and
// to locate the innermost array (the "list" which is "repeated").
// For inner array
int dims = 1;
ColumnMetadata elementSchema = fieldDefn.providedColumn();
while (MetadataUtils.isRepeatedList(elementSchema)) {
dims++;
elementSchema = elementSchema.childSchema();
Preconditions.checkArgument(elementSchema != null);
}
ColumnMetadata colSchema = repeatedListSchemaFor(fieldDefn.key(), dims, elementSchema.cloneEmpty());
ObjectWriter fieldWriter = fieldDefn.fieldWriterFor(colSchema);
switch(elementSchema.structureType()) {
case PRIMITIVE:
return multiDimScalarArrayFor(fieldWriter, dims);
case TUPLE:
return multiDimObjectArrayFor(fieldWriter, dims, elementSchema.tupleSchema());
case VARIANT:
return multiDimVariantArrayParserFor(fieldWriter, dims);
default:
throw loader().unsupportedType(fieldDefn.providedColumn());
}
}
Aggregations