Search in sources :

Example 16 with ObjectWriter

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();
}
Also used : VariantWriter(org.apache.drill.exec.vector.accessor.VariantWriter) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter) ScalarReader(org.apache.drill.exec.vector.accessor.ScalarReader) ArrayReader(org.apache.drill.exec.vector.accessor.ArrayReader) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) ObjectReader(org.apache.drill.exec.vector.accessor.ObjectReader) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) ExtendableRowSet(org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet) VariantReader(org.apache.drill.exec.vector.accessor.VariantReader) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 17 with ObjectWriter

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);
}
Also used : ArrayColumnConverter(org.apache.drill.exec.record.ColumnConverter.ArrayColumnConverter) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter) ScalarColumnConverter(org.apache.drill.exec.record.ColumnConverter.ScalarColumnConverter) DummyColumnConverter(org.apache.drill.exec.record.ColumnConverter.DummyColumnConverter) DictColumnConverter(org.apache.drill.exec.record.ColumnConverter.DictColumnConverter) MapColumnConverter(org.apache.drill.exec.record.ColumnConverter.MapColumnConverter) ArrayColumnConverter(org.apache.drill.exec.record.ColumnConverter.ArrayColumnConverter)

Example 18 with ObjectWriter

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;
}
Also used : ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter)

Example 19 with ObjectWriter

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;
}
Also used : ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) RepeatedListWriter(org.apache.drill.exec.vector.accessor.writer.RepeatedListWriter) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter)

Example 20 with ObjectWriter

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());
    }
}
Also used : ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter)

Aggregations

ObjectWriter (org.apache.drill.exec.vector.accessor.ObjectWriter)20 SingleRowSet (org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet)10 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)10 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)9 SubOperatorTest (org.apache.drill.test.SubOperatorTest)9 Test (org.junit.Test)9 ObjectReader (org.apache.drill.exec.vector.accessor.ObjectReader)7 ScalarWriter (org.apache.drill.exec.vector.accessor.ScalarWriter)7 ExtendableRowSet (org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet)6 ArrayWriter (org.apache.drill.exec.vector.accessor.ArrayWriter)6 ScalarReader (org.apache.drill.exec.vector.accessor.ScalarReader)6 ColumnMetadata (org.apache.drill.exec.record.metadata.ColumnMetadata)5 VariantWriter (org.apache.drill.exec.vector.accessor.VariantWriter)5 ArrayReader (org.apache.drill.exec.vector.accessor.ArrayReader)4 VariantReader (org.apache.drill.exec.vector.accessor.VariantReader)4 RowSetLoader (org.apache.drill.exec.physical.resultSet.RowSetLoader)3 MinorType (org.apache.drill.common.types.TypeProtos.MinorType)2 ResultSetLoader (org.apache.drill.exec.physical.resultSet.ResultSetLoader)2 RowSet (org.apache.drill.exec.physical.rowSet.RowSet)2 TupleReader (org.apache.drill.exec.vector.accessor.TupleReader)2