Search in sources :

Example 11 with ObjectWriter

use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.

the class UnionWriterImpl method member.

@Override
public ObjectWriter member(MinorType type) {
    // Get the writer first, which may trigger the single-to-union
    // conversion. Then set the type because, if the conversion is
    // done, the type vector exists only after creating the member.
    final ObjectWriter writer = shim.member(type);
    setType(type);
    return writer;
}
Also used : ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter)

Example 12 with ObjectWriter

use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.

the class TestResultSetLoaderRepeatedList method test2DLateSchemaIncremental.

@Test
public void test2DLateSchemaIncremental() {
    final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addRepeatedList("list1").addArray(MinorType.VARCHAR).resumeSchema().addRepeatedList("list2").addArray(MinorType.VARCHAR).resumeSchema().buildSchema();
    final ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().build();
    final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
    final RowSetLoader writer = rsLoader.writer();
    // Add columns dynamically
    writer.addColumn(schema.metadata(0));
    // Write a row without the array.
    rsLoader.startBatch();
    writer.addRow(1);
    // Add the repeated list, but without contents.
    writer.addColumn(schema.metadata(1).cloneEmpty());
    // Sanity check of writer structure
    assertEquals(2, writer.size());
    final ObjectWriter listObj = writer.column("list1");
    assertEquals(ObjectType.ARRAY, listObj.type());
    final ArrayWriter listWriter = listObj.array();
    // No child defined yet. A dummy child is inserted instead.
    assertEquals(MinorType.NULL, listWriter.entry().schema().type());
    assertEquals(ObjectType.ARRAY, listWriter.entryType());
    assertEquals(ObjectType.SCALAR, listWriter.array().entryType());
    assertEquals(ValueType.NULL, listWriter.array().scalar().valueType());
    // Although we don't know the type of the inner, we can still
    // create null (empty) elements in the outer array.
    writer.addRow(2, null).addRow(3, objArray()).addRow(4, objArray(objArray(), null));
    // Define the inner type.
    final RepeatedListWriter listWriterImpl = (RepeatedListWriter) listWriter;
    listWriterImpl.defineElement(MaterializedField.create("list1", Types.repeated(MinorType.VARCHAR)));
    // Sanity check of completed structure
    assertEquals(ObjectType.ARRAY, listWriter.entryType());
    final ArrayWriter innerWriter = listWriter.array();
    assertEquals(ObjectType.SCALAR, innerWriter.entryType());
    final ScalarWriter strWriter = innerWriter.scalar();
    assertEquals(ValueType.STRING, strWriter.valueType());
    // Write values
    writer.addRow(5, objArray(strArray("a1", "b1"), strArray("c1", "d1")));
    // Add the second list, with a complete type
    writer.addColumn(schema.metadata(2));
    // Sanity check of writer structure
    assertEquals(3, writer.size());
    final ObjectWriter list2Obj = writer.column("list2");
    assertEquals(ObjectType.ARRAY, list2Obj.type());
    final ArrayWriter list2Writer = list2Obj.array();
    assertEquals(ObjectType.ARRAY, list2Writer.entryType());
    final ArrayWriter inner2Writer = list2Writer.array();
    assertEquals(ObjectType.SCALAR, inner2Writer.entryType());
    final ScalarWriter str2Writer = inner2Writer.scalar();
    assertEquals(ValueType.STRING, str2Writer.valueType());
    // Write values
    writer.addRow(6, objArray(strArray("a2", "b2"), strArray("c2", "d2")), objArray(strArray("w2", "x2"), strArray("y2", "z2")));
    // Add the second list, with a complete type
    // Verify the values.
    // (Relies on the row set level repeated list tests having passed.)
    final RowSet expected = fixture.rowSetBuilder(schema).addRow(1, objArray(), objArray()).addRow(2, objArray(), objArray()).addRow(3, objArray(), objArray()).addRow(4, objArray(objArray(), null), objArray()).addRow(5, objArray(strArray("a1", "b1"), strArray("c1", "d1")), objArray()).addRow(6, objArray(strArray("a2", "b2"), strArray("c2", "d2")), objArray(strArray("w2", "x2"), strArray("y2", "z2"))).build();
    RowSetUtilities.verify(expected, fixture.wrap(rsLoader.harvest()));
    rsLoader.close();
}
Also used : ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) RepeatedListWriter(org.apache.drill.exec.vector.accessor.writer.RepeatedListWriter) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 13 with ObjectWriter

use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.

the class TestResultSetLoaderRepeatedList method do2DTest.

private void do2DTest(TupleMetadata schema, ResultSetLoader rsLoader) {
    final RowSetLoader writer = rsLoader.writer();
    // Sanity check of writer structure
    assertEquals(2, writer.size());
    final ObjectWriter listObj = writer.column("list2");
    assertEquals(ObjectType.ARRAY, listObj.type());
    final ArrayWriter listWriter = listObj.array();
    assertEquals(ObjectType.ARRAY, listWriter.entryType());
    final ArrayWriter innerWriter = listWriter.array();
    assertEquals(ObjectType.SCALAR, innerWriter.entryType());
    final ScalarWriter strWriter = innerWriter.scalar();
    assertEquals(ValueType.STRING, strWriter.valueType());
    // Sanity test of schema
    final TupleMetadata rowSchema = writer.tupleSchema();
    assertEquals(2, rowSchema.size());
    final ColumnMetadata listSchema = rowSchema.metadata(1);
    assertEquals(MinorType.LIST, listSchema.type());
    assertEquals(DataMode.REPEATED, listSchema.mode());
    assertTrue(listSchema instanceof RepeatedListColumnMetadata);
    assertEquals(StructureType.MULTI_ARRAY, listSchema.structureType());
    assertNotNull(listSchema.childSchema());
    final ColumnMetadata elementSchema = listSchema.childSchema();
    assertEquals(listSchema.name(), elementSchema.name());
    assertEquals(MinorType.VARCHAR, elementSchema.type());
    assertEquals(DataMode.REPEATED, elementSchema.mode());
    // Write values
    rsLoader.startBatch();
    writer.addRow(1, objArray(strArray("a", "b"), strArray("c", "d"))).addRow(2, objArray(strArray("e"), strArray(), strArray("f", "g", "h"))).addRow(3, objArray()).addRow(4, objArray(strArray(), strArray("i"), strArray()));
    // Verify the values.
    // (Relies on the row set level repeated list tests having passed.)
    final RowSet expected = fixture.rowSetBuilder(schema).addRow(1, objArray(strArray("a", "b"), strArray("c", "d"))).addRow(2, objArray(strArray("e"), strArray(), strArray("f", "g", "h"))).addRow(3, objArray()).addRow(4, objArray(strArray(), strArray("i"), strArray())).build();
    RowSetUtilities.verify(expected, fixture.wrap(rsLoader.harvest()));
}
Also used : ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) RepeatedListColumnMetadata(org.apache.drill.exec.record.metadata.RepeatedListColumnMetadata) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) RepeatedListColumnMetadata(org.apache.drill.exec.record.metadata.RepeatedListColumnMetadata)

Example 14 with ObjectWriter

use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.

the class TestRepeatedListAccessors method testSchema2DWriterReader.

@Test
public void testSchema2DWriterReader() {
    final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addRepeatedList("list2").addArray(MinorType.VARCHAR).resumeSchema().buildSchema();
    final DirectRowSet rowSet = DirectRowSet.fromSchema(fixture.allocator(), schema);
    SingleRowSet result;
    {
        final RowSetWriter writer = rowSet.writer();
        assertEquals(2, writer.size());
        final ObjectWriter listObj = writer.column("list2");
        assertEquals(ObjectType.ARRAY, listObj.type());
        final ArrayWriter listWriter = listObj.array();
        assertEquals(ObjectType.ARRAY, listWriter.entryType());
        final ArrayWriter innerWriter = listWriter.array();
        assertEquals(ObjectType.SCALAR, innerWriter.entryType());
        final ScalarWriter strWriter = innerWriter.scalar();
        // Write one row using writers explicitly.
        // 
        // (1, [["a, "b"], ["c", "d"]])
        // 
        // Note auto increment of inner list on write.
        writer.scalar("id").setInt(1);
        strWriter.setString("a");
        strWriter.setString("b");
        listWriter.save();
        strWriter.setString("c");
        strWriter.setString("d");
        listWriter.save();
        writer.save();
        // Write more rows using the convenience methods.
        // 
        // (2, [["e"], [], ["f", "g", "h"]])
        // (3, [])
        // (4, [[], ["i"], []])
        writer.addRow(2, objArray(strArray("e"), strArray(), strArray("f", "g", "h"))).addRow(3, objArray()).addRow(4, objArray(strArray(), strArray("i"), strArray()));
        result = writer.done();
    }
    // Verify one row using the individual readers.
    {
        final RowSetReader reader = result.reader();
        assertEquals(2, reader.columnCount());
        final ObjectReader listObj = reader.column("list2");
        assertEquals(ObjectType.ARRAY, listObj.type());
        final ArrayReader listReader = listObj.array();
        assertEquals(ObjectType.ARRAY, listReader.entryType());
        final ArrayReader innerReader = listReader.array();
        assertEquals(ObjectType.SCALAR, innerReader.entryType());
        final ScalarReader strReader = innerReader.scalar();
        // Write one row using writers explicitly.
        // 
        // (1, [["a, "b"], ["c", "d"]])
        assertTrue(reader.next());
        assertEquals(2, listReader.size());
        assertTrue(listReader.next());
        assertEquals(2, innerReader.size());
        assertTrue(innerReader.next());
        assertEquals("a", strReader.getString());
        assertTrue(innerReader.next());
        assertEquals("b", strReader.getString());
        assertFalse(innerReader.next());
        assertTrue(listReader.next());
        assertEquals(2, innerReader.size());
        assertTrue(innerReader.next());
        assertEquals("c", strReader.getString());
        assertTrue(innerReader.next());
        assertEquals("d", strReader.getString());
        assertFalse(innerReader.next());
        assertFalse(listReader.next());
    }
    // Verify both rows by building another row set and comparing.
    final RowSet expected = fixture.rowSetBuilder(schema).addRow(1, objArray(strArray("a", "b"), strArray("c", "d"))).addRow(2, objArray(strArray("e"), strArray(), strArray("f", "g", "h"))).addRow(3, objArray()).addRow(4, objArray(strArray(), strArray("i"), strArray())).build();
    new RowSetComparison(expected).verify(result);
    // Test that the row set rebuilds its internal structure from
    // a vector container.
    RowSet wrapped = fixture.wrap(result.container());
    RowSetUtilities.verify(expected, wrapped);
}
Also used : SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) 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) RowSetComparison(org.apache.drill.test.rowSet.RowSetComparison) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) BatchSchemaBuilder(org.apache.drill.exec.record.BatchSchemaBuilder) ObjectReader(org.apache.drill.exec.vector.accessor.ObjectReader) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 15 with ObjectWriter

use of org.apache.drill.exec.vector.accessor.ObjectWriter in project drill by apache.

the class TestVariantAccessors method testListOfMaps.

/**
 * List of maps. Like a repeated map, but each list entry can be
 * null.
 */
@Test
public void testListOfMaps() {
    final TupleMetadata schema = new SchemaBuilder().addList("list").addMap().addNullable("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).resumeUnion().resumeSchema().buildSchema();
    final ExtendableRowSet rowSet = fixture.rowSet(schema);
    final RowSetWriter writer = rowSet.writer();
    {
        final ObjectWriter listObj = writer.column("list");
        assertEquals(ObjectType.ARRAY, listObj.type());
        final ArrayWriter listArray = listObj.array();
        final ObjectWriter itemObj = listArray.entry();
        assertEquals(ObjectType.TUPLE, itemObj.type());
        final TupleWriter mapWriter = itemObj.tuple();
        final ScalarWriter aWriter = mapWriter.scalar("a");
        final ScalarWriter bWriter = mapWriter.scalar("b");
        // First row:
        // {1, "fred"}, null, {3, null}
        aWriter.setInt(1);
        bWriter.setString("fred");
        listArray.save();
        // Can't mark the map as null. Instead, we simply skip
        // the map and the contained nullable members will automatically
        // back-fill each entry with a null value.
        listArray.save();
        aWriter.setInt(3);
        bWriter.setNull();
        listArray.save();
        writer.save();
        // Second row: null
        writer.save();
        // Third row: {null, "dino"}
        aWriter.setNull();
        bWriter.setString("dino");
        listArray.save();
        writer.save();
        // Fourth row: empty array. Note that there is no trigger
        // to say that the column is not null, so we have to do it
        // explicitly.
        listArray.setNull(false);
        writer.save();
        // Last row: {4, "pebbles"}
        aWriter.setInt(4);
        bWriter.setString("pebbles");
        listArray.save();
        writer.save();
    }
    final SingleRowSet result = writer.done();
    assertEquals(5, result.rowCount());
    {
        final RowSetReader reader = result.reader();
        final ObjectReader listObj = reader.column("list");
        assertEquals(ObjectType.ARRAY, listObj.type());
        final ArrayReader listArray = listObj.array();
        assertEquals(ObjectType.TUPLE, listArray.entry().type());
        final TupleReader mapReader = listArray.tuple();
        final ScalarReader aReader = mapReader.scalar("a");
        final ScalarReader bReader = mapReader.scalar("b");
        // First row:
        // {1, "fred"}, null, {3, null}
        assertTrue(reader.next());
        assertFalse(listArray.isNull());
        assertFalse(mapReader.isNull());
        assertEquals(3, listArray.size());
        assertTrue(listArray.next());
        assertFalse(aReader.isNull());
        assertEquals(1, aReader.getInt());
        assertFalse(bReader.isNull());
        assertEquals("fred", bReader.getString());
        assertTrue(listArray.next());
        // Awkward: the map has no null state, but its
        // members do.
        assertTrue(aReader.isNull());
        assertTrue(bReader.isNull());
        assertTrue(listArray.next());
        assertFalse(aReader.isNull());
        assertEquals(3, aReader.getInt());
        assertTrue(bReader.isNull());
        assertFalse(listArray.next());
        // Second row: null
        assertTrue(reader.next());
        assertTrue(listArray.isNull());
        assertEquals(0, listArray.size());
        // Third row: {null, "dino"}
        assertTrue(reader.next());
        assertFalse(listArray.isNull());
        assertEquals(1, listArray.size());
        assertTrue(listArray.next());
        assertTrue(aReader.isNull());
        assertFalse(bReader.isNull());
        assertEquals("dino", bReader.getString());
        assertFalse(listArray.next());
        // Fourth row: empty array.
        assertTrue(reader.next());
        assertFalse(listArray.isNull());
        assertEquals(0, listArray.size());
        assertFalse(listArray.next());
        // Last row: {4, "pebbles"}
        assertTrue(reader.next());
        assertFalse(listArray.isNull());
        assertEquals(1, listArray.size());
        assertTrue(listArray.next());
        assertEquals(4, aReader.getInt());
        assertEquals("pebbles", bReader.getString());
        assertFalse(listArray.next());
        assertFalse(reader.next());
    }
    result.clear();
}
Also used : TupleReader(org.apache.drill.exec.vector.accessor.TupleReader) 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) TupleWriter(org.apache.drill.exec.vector.accessor.TupleWriter) 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) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

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