Search in sources :

Example 1 with ObjectWriter

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

the class BuildFromSchema method buildDict.

private ObjectWriter buildDict(ParentShim parent, ColumnMetadata colSchema) {
    final ObjectWriter colWriter = parent.add(colSchema.cloneEmpty());
    expandDict(colWriter, colSchema);
    return colWriter;
}
Also used : ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter)

Example 2 with ObjectWriter

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

the class BuildFromSchema method buildVariant.

/**
 * Expand a variant column. We use the term "variant" to mean either
 * a UNION, or a LIST of UNIONs. (A LIST of UNIONs is, conceptually,
 * a repeated UNION, though it is far more complex.)
 *
 * @param parent shim object used to associate the UNION types with its
 * parent column (which is a UNION or a LIST). Since UNION and LIST are
 * far different types, the shim provides a facade that encapsulates
 * the common behavior
 * @param colSchema the schema of the variant (LIST or UNION) column
 */
private ObjectWriter buildVariant(ParentShim parent, ColumnMetadata colSchema) {
    final ObjectWriter colWriter = parent.add(colSchema.cloneEmpty());
    expandVariant(colWriter, colSchema);
    return colWriter;
}
Also used : ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter)

Example 3 with ObjectWriter

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

the class BuildFromSchema method buildSingleList.

private ObjectWriter buildSingleList(ParentShim parent, ColumnMetadata colSchema) {
    final ColumnMetadata seed = colSchema.cloneEmpty();
    final ColumnMetadata subtype = colSchema.variantSchema().listSubtype();
    seed.variantSchema().addType(subtype.cloneEmpty());
    seed.variantSchema().becomeSimple();
    final ObjectWriter listWriter = parent.add(seed);
    expandColumn(listWriter, subtype);
    return listWriter;
}
Also used : ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter)

Example 4 with ObjectWriter

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

the class TestResultSetLoaderUnions method testUnionBasics.

@Test
public void testUnionBasics() {
    final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addUnion("u").addType(MinorType.VARCHAR).addMap().addNullable("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).resumeUnion().resumeSchema().buildSchema();
    final ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).build();
    final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
    final RowSetLoader writer = rsLoader.writer();
    // Sanity check of writer structure
    final ObjectWriter wo = writer.column(1);
    assertEquals(ObjectType.VARIANT, wo.type());
    final VariantWriter vw = wo.variant();
    assertTrue(vw.hasType(MinorType.VARCHAR));
    assertNotNull(vw.memberWriter(MinorType.VARCHAR));
    assertTrue(vw.hasType(MinorType.MAP));
    assertNotNull(vw.memberWriter(MinorType.MAP));
    // Write values
    rsLoader.startBatch();
    writer.addRow(1, "first").addRow(2, mapValue(20, "fred")).addRow(3, null).addRow(4, mapValue(40, null)).addRow(5, "last");
    // Verify the values.
    // (Relies on the row set level union tests having passed.)
    final SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(1, "first").addRow(2, mapValue(20, "fred")).addRow(3, null).addRow(4, mapValue(40, null)).addRow(5, "last").build();
    RowSetUtilities.verify(expected, fixture.wrap(rsLoader.harvest()));
}
Also used : VariantWriter(org.apache.drill.exec.vector.accessor.VariantWriter) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 5 with ObjectWriter

use of org.apache.drill.exec.vector.accessor.ObjectWriter 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();
}
Also used : TupleReader(org.apache.drill.exec.vector.accessor.TupleReader) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) VariantWriter(org.apache.drill.exec.vector.accessor.VariantWriter) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter) ScalarReader(org.apache.drill.exec.vector.accessor.ScalarReader) 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) 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)

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