Search in sources :

Example 1 with VariantReader

use of org.apache.drill.exec.vector.accessor.VariantReader 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)

Example 2 with VariantReader

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

the class TestVariantAccessors method testAddTypes.

/**
 * Test a variant (AKA "union vector") at the top level, using
 * just scalar values.
 */
@Test
public void testAddTypes() {
    final TupleMetadata batchSchema = new SchemaBuilder().addNullable("v", MinorType.UNION).buildSchema();
    final ExtendableRowSet rs = fixture.rowSet(batchSchema);
    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();
    assertSame(vw, writer.variant(0));
    assertSame(vw, writer.variant("v"));
    for (final MinorType type : MinorType.values()) {
        assertFalse(vw.hasType(type));
    }
    // Write values of different types
    vw.scalar(MinorType.INT).setInt(10);
    assertTrue(vw.hasType(MinorType.INT));
    assertFalse(vw.hasType(MinorType.VARCHAR));
    writer.save();
    vw.scalar(MinorType.VARCHAR).setString("fred");
    assertTrue(vw.hasType(MinorType.VARCHAR));
    writer.save();
    vw.setNull();
    writer.save();
    vw.scalar(MinorType.FLOAT8).setDouble(123.45);
    assertTrue(vw.hasType(MinorType.INT));
    assertTrue(vw.hasType(MinorType.FLOAT8));
    writer.save();
    final SingleRowSet result = writer.done();
    assertEquals(4, 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();
    assertSame(vr, reader.variant(0));
    assertSame(vr, reader.variant("v"));
    for (final MinorType type : MinorType.values()) {
        if (type == MinorType.INT || type == MinorType.VARCHAR || type == MinorType.FLOAT8) {
            assertTrue(vr.hasType(type));
        } else {
            assertFalse(vr.hasType(type));
        }
    }
    // Verify the data
    assertTrue(reader.next());
    assertFalse(vr.isNull());
    assertSame(vr.dataType(), MinorType.INT);
    assertSame(vr.scalar(MinorType.INT), vr.scalar());
    assertNotNull(vr.member());
    assertSame(vr.scalar(), vr.member().scalar());
    assertEquals(10, vr.scalar().getInt());
    assertTrue(reader.next());
    assertFalse(vr.isNull());
    assertSame(vr.dataType(), MinorType.VARCHAR);
    assertSame(vr.scalar(MinorType.VARCHAR), vr.scalar());
    assertEquals("fred", vr.scalar().getString());
    assertTrue(reader.next());
    assertTrue(vr.isNull());
    assertNull(vr.dataType());
    assertNull(vr.scalar());
    assertTrue(reader.next());
    assertFalse(vr.isNull());
    assertSame(vr.dataType(), MinorType.FLOAT8);
    assertSame(vr.scalar(MinorType.FLOAT8), vr.scalar());
    assertEquals(123.45, vr.scalar().getDouble(), 0.001);
    assertFalse(reader.next());
    result.clear();
}
Also used : VariantWriter(org.apache.drill.exec.vector.accessor.VariantWriter) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter) MinorType(org.apache.drill.common.types.TypeProtos.MinorType) ObjectReader(org.apache.drill.exec.vector.accessor.ObjectReader) 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 3 with VariantReader

use of org.apache.drill.exec.vector.accessor.VariantReader 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();
}
Also used : ScalarReader(org.apache.drill.exec.vector.accessor.ScalarReader) ArrayReader(org.apache.drill.exec.vector.accessor.ArrayReader) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) VariantWriter(org.apache.drill.exec.vector.accessor.VariantWriter) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) 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 4 with VariantReader

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

the class TestVariantAccessors method testScalarVariant.

/**
 * Test a variant (AKA "union vector") at the top level, using
 * just scalar values.
 */
@Test
public void testScalarVariant() {
    final TupleMetadata schema = new SchemaBuilder().addUnion("u").addType(MinorType.INT).addType(MinorType.VARCHAR).addType(MinorType.FLOAT8).resumeSchema().buildSchema();
    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();
    assertSame(vw, writer.variant(0));
    assertSame(vw, writer.variant("u"));
    assertTrue(vw.hasType(MinorType.INT));
    assertTrue(vw.hasType(MinorType.VARCHAR));
    assertTrue(vw.hasType(MinorType.FLOAT8));
    // Write values of different types
    vw.scalar(MinorType.INT).setInt(10);
    writer.save();
    vw.scalar(MinorType.VARCHAR).setString("fred");
    writer.save();
    // The entire variant is null
    vw.setNull();
    writer.save();
    vw.scalar(MinorType.FLOAT8).setDouble(123.45);
    writer.save();
    // Strange case: just the value is null, but the variant
    // is not null.
    vw.scalar(MinorType.INT).setNull();
    writer.save();
    // Marker to avoid fill-empty issues (fill-empties tested elsewhere.)
    vw.scalar(MinorType.INT).setInt(20);
    writer.save();
    final SingleRowSet result = writer.done();
    assertEquals(6, 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();
    assertSame(vr, reader.variant(0));
    assertSame(vr, reader.variant("u"));
    for (final MinorType type : MinorType.values()) {
        if (type == MinorType.INT || type == MinorType.VARCHAR || type == MinorType.FLOAT8) {
            assertTrue(vr.hasType(type));
        } else {
            assertFalse(vr.hasType(type));
        }
    }
    // Can get readers up front
    final ScalarReader intReader = vr.scalar(MinorType.INT);
    final ScalarReader strReader = vr.scalar(MinorType.VARCHAR);
    final ScalarReader floatReader = vr.scalar(MinorType.FLOAT8);
    // Verify the data
    // Int 10
    assertTrue(reader.next());
    assertFalse(vr.isNull());
    assertSame(vr.dataType(), MinorType.INT);
    assertSame(intReader, vr.scalar());
    assertNotNull(vr.member());
    assertSame(vr.scalar(), vr.member().scalar());
    assertFalse(intReader.isNull());
    assertEquals(10, intReader.getInt());
    assertTrue(strReader.isNull());
    assertTrue(floatReader.isNull());
    // String "fred"
    assertTrue(reader.next());
    assertFalse(vr.isNull());
    assertSame(vr.dataType(), MinorType.VARCHAR);
    assertSame(strReader, vr.scalar());
    assertFalse(strReader.isNull());
    assertEquals("fred", strReader.getString());
    assertTrue(intReader.isNull());
    assertTrue(floatReader.isNull());
    // Null value
    assertTrue(reader.next());
    assertTrue(vr.isNull());
    assertNull(vr.dataType());
    assertNull(vr.scalar());
    assertTrue(intReader.isNull());
    assertTrue(strReader.isNull());
    assertTrue(floatReader.isNull());
    // Double 123.45
    assertTrue(reader.next());
    assertFalse(vr.isNull());
    assertSame(vr.dataType(), MinorType.FLOAT8);
    assertSame(floatReader, vr.scalar());
    assertFalse(floatReader.isNull());
    assertEquals(123.45, vr.scalar().getDouble(), 0.001);
    assertTrue(intReader.isNull());
    assertTrue(strReader.isNull());
    // Strange case: null int (but union is not null)
    assertTrue(reader.next());
    assertFalse(vr.isNull());
    assertSame(vr.dataType(), MinorType.INT);
    assertTrue(intReader.isNull());
    // Int 20
    assertTrue(reader.next());
    assertFalse(vr.isNull());
    assertFalse(intReader.isNull());
    assertEquals(20, intReader.getInt());
    assertFalse(reader.next());
    result.clear();
}
Also used : ScalarReader(org.apache.drill.exec.vector.accessor.ScalarReader) VariantWriter(org.apache.drill.exec.vector.accessor.VariantWriter) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter) MinorType(org.apache.drill.common.types.TypeProtos.MinorType) ObjectReader(org.apache.drill.exec.vector.accessor.ObjectReader) 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 5 with VariantReader

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

the class UnionWriterImpl method copy.

@Override
public void copy(ColumnReader from) {
    if (!from.isNull()) {
        VariantReader source = (VariantReader) from;
        member(source.dataType()).copy(source.member());
    }
}
Also used : VariantReader(org.apache.drill.exec.vector.accessor.VariantReader)

Aggregations

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