Search in sources :

Example 11 with ExtendableRowSet

use of org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet in project drill by apache.

the class TestVariantAccessors method testScalarList.

/**
 * Test a scalar list. Should act just like a repeated type, with the
 * addition of allowing the list for a row to be null. But, a list
 * writer does not do auto-increment, so we must do that explicitly
 * after each write.
 */
@Test
public void testScalarList() {
    final TupleMetadata schema = new SchemaBuilder().addList("list").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();
        // The list contains only a scalar. But, because lists can,
        // in general, contain multiple contents, the list requires
        // an explicit save after each entry.
        final ObjectWriter itemObj = listArray.entry();
        assertEquals(ObjectType.SCALAR, itemObj.type());
        final ScalarWriter strWriter = itemObj.scalar();
        // First row: two strings and a null
        // Unlike a repeated type, a list can mark individual elements
        // as null.
        // List will automatically detect that data was written.
        strWriter.setString("fred");
        listArray.save();
        strWriter.setNull();
        listArray.save();
        strWriter.setString("wilma");
        listArray.save();
        writer.save();
        // Second row: null
        writer.save();
        // Third row: one string
        strWriter.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: a null string and non-null
        strWriter.setNull();
        listArray.save();
        strWriter.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(0);
        assertEquals(ObjectType.ARRAY, listObj.type());
        final ArrayReader listArray = listObj.array();
        // The list is a repeated scalar
        assertEquals(ObjectType.SCALAR, listArray.entry().type());
        final ScalarReader strReader = listArray.scalar();
        // First row: two strings and a null
        assertTrue(reader.next());
        assertFalse(listArray.isNull());
        assertEquals(3, listArray.size());
        assertTrue(listArray.next());
        assertFalse(strReader.isNull());
        assertEquals("fred", strReader.getString());
        assertTrue(listArray.next());
        assertTrue(strReader.isNull());
        assertTrue(listArray.next());
        assertFalse(strReader.isNull());
        assertEquals("wilma", strReader.getString());
        assertFalse(listArray.next());
        // Second row: null
        assertTrue(reader.next());
        assertTrue(listArray.isNull());
        assertEquals(0, listArray.size());
        // Third row: one string
        assertTrue(reader.next());
        assertFalse(listArray.isNull());
        assertEquals(1, listArray.size());
        assertTrue(listArray.next());
        assertEquals("dino", strReader.getString());
        assertFalse(listArray.next());
        // Fourth row: empty array.
        assertTrue(reader.next());
        assertFalse(listArray.isNull());
        assertEquals(0, listArray.size());
        assertFalse(listArray.next());
        // Last row: a null string and non-null
        assertTrue(reader.next());
        assertFalse(listArray.isNull());
        assertEquals(2, listArray.size());
        assertTrue(listArray.next());
        assertTrue(strReader.isNull());
        assertTrue(listArray.next());
        assertFalse(strReader.isNull());
        assertEquals("pebbles", strReader.getString());
        assertFalse(listArray.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) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter) 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)

Example 12 with ExtendableRowSet

use of org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet in project drill by apache.

the class TestVariantAccessors method testBuildRowSetUnionArray.

@Test
public void testBuildRowSetUnionArray() {
    final TupleMetadata schema = new SchemaBuilder().addList("list1").addType(MinorType.BIGINT).addMap().addNullable("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).resumeUnion().addList().addType(MinorType.FLOAT8).resumeUnion().resumeSchema().buildSchema();
    final ExtendableRowSet rowSet = fixture.rowSet(schema);
    final VectorContainer vc = rowSet.container();
    assertEquals(1, vc.getNumberOfColumns());
    // List with complex internal structure
    final ValueVector vector = vc.getValueVector(0).getValueVector();
    assertTrue(vector instanceof ListVector);
    final ListVector list = (ListVector) vector;
    assertTrue(list.getDataVector() instanceof UnionVector);
    final UnionVector union = (UnionVector) list.getDataVector();
    // Union inside the list
    final MajorType unionType = union.getField().getType();
    final List<MinorType> types = unionType.getSubTypeList();
    assertEquals(3, types.size());
    assertTrue(types.contains(MinorType.BIGINT));
    assertTrue(types.contains(MinorType.MAP));
    assertTrue(types.contains(MinorType.LIST));
    final MapVector typeMap = union.getTypeMap();
    ValueVector member = typeMap.getChild(MinorType.BIGINT.name());
    assertTrue(member instanceof NullableBigIntVector);
    // Map inside the list
    member = typeMap.getChild(MinorType.MAP.name());
    assertTrue(member instanceof MapVector);
    final MapVector childMap = (MapVector) member;
    ValueVector mapMember = childMap.getChild("a");
    assertNotNull(mapMember);
    assertTrue(mapMember instanceof NullableIntVector);
    mapMember = childMap.getChild("b");
    assertNotNull(mapMember);
    assertTrue(mapMember instanceof NullableVarCharVector);
    // Single-type list inside the outer list
    member = typeMap.getChild(MinorType.LIST.name());
    assertTrue(member instanceof ListVector);
    final ListVector childList = (ListVector) member;
    assertTrue(childList.getDataVector() instanceof NullableFloat8Vector);
    rowSet.clear();
}
Also used : NullableFloat8Vector(org.apache.drill.exec.vector.NullableFloat8Vector) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) UnionVector(org.apache.drill.exec.vector.complex.UnionVector) VectorContainer(org.apache.drill.exec.record.VectorContainer) ValueVector(org.apache.drill.exec.vector.ValueVector) NullableIntVector(org.apache.drill.exec.vector.NullableIntVector) NullableVarCharVector(org.apache.drill.exec.vector.NullableVarCharVector) NullableBigIntVector(org.apache.drill.exec.vector.NullableBigIntVector) ListVector(org.apache.drill.exec.vector.complex.ListVector) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) MinorType(org.apache.drill.common.types.TypeProtos.MinorType) ExtendableRowSet(org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet) MapVector(org.apache.drill.exec.vector.complex.MapVector) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 13 with ExtendableRowSet

use of org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet in project drill by apache.

the class TestVariantAccessors method testBuildRowSetUnion.

@Test
public void testBuildRowSetUnion() {
    final TupleMetadata schema = new SchemaBuilder().addUnion("u").addType(MinorType.INT).addMap().addNullable("c", MinorType.BIGINT).addNullable("d", MinorType.VARCHAR).resumeUnion().addList().addType(MinorType.VARCHAR).resumeUnion().resumeSchema().buildSchema();
    final ExtendableRowSet rowSet = fixture.rowSet(schema);
    final VectorContainer vc = rowSet.container();
    assertEquals(1, vc.getNumberOfColumns());
    // Single union
    final ValueVector vector = vc.getValueVector(0).getValueVector();
    assertTrue(vector instanceof UnionVector);
    final UnionVector union = (UnionVector) vector;
    final MapVector typeMap = union.getTypeMap();
    ValueVector member = typeMap.getChild(MinorType.INT.name());
    assertTrue(member instanceof NullableIntVector);
    // Inner map
    member = typeMap.getChild(MinorType.MAP.name());
    assertTrue(member instanceof MapVector);
    member = typeMap.getChild(MinorType.MAP.name());
    assertTrue(member instanceof MapVector);
    final MapVector childMap = (MapVector) member;
    ValueVector mapMember = childMap.getChild("c");
    assertNotNull(mapMember);
    assertTrue(mapMember instanceof NullableBigIntVector);
    mapMember = childMap.getChild("d");
    assertNotNull(mapMember);
    assertTrue(mapMember instanceof NullableVarCharVector);
    // Inner list
    member = typeMap.getChild(MinorType.LIST.name());
    assertTrue(member instanceof ListVector);
    final ListVector list = (ListVector) member;
    assertTrue(list.getDataVector() instanceof NullableVarCharVector);
    rowSet.clear();
}
Also used : ValueVector(org.apache.drill.exec.vector.ValueVector) NullableIntVector(org.apache.drill.exec.vector.NullableIntVector) NullableVarCharVector(org.apache.drill.exec.vector.NullableVarCharVector) NullableBigIntVector(org.apache.drill.exec.vector.NullableBigIntVector) ListVector(org.apache.drill.exec.vector.complex.ListVector) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) UnionVector(org.apache.drill.exec.vector.complex.UnionVector) ExtendableRowSet(org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet) VectorContainer(org.apache.drill.exec.record.VectorContainer) MapVector(org.apache.drill.exec.vector.complex.MapVector) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 14 with ExtendableRowSet

use of org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet in project drill by apache.

the class TestIndirectReaders method testArray.

/**
 * More complex case with two levels of offset vector (one for the
 * array, another for the Varchar values.) Only the top level goes
 * through the indirection.
 */
@Test
public void testArray() {
    TupleMetadata schema = new SchemaBuilder().addArray("a", MinorType.VARCHAR).buildSchema();
    ExtendableRowSet rowSet = fixture.rowSet(schema);
    RowSetWriter writer = rowSet.writer();
    ArrayWriter aWriter = writer.array(0);
    ScalarWriter strWriter = aWriter.scalar();
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 5; j++) {
            strWriter.setString("value" + i + "." + j);
        }
        writer.save();
    }
    SingleRowSet result = writer.done().toIndirect();
    SelectionVector2 sv2 = result.getSv2();
    for (int i = 0; i < 10; i++) {
        sv2.setIndex(i, 9 - i);
    }
    RowSetReader reader = result.reader();
    ArrayReader aReader = reader.array(0);
    ScalarReader strReader = aReader.scalar();
    for (int i = 9; i >= 0; i--) {
        assertTrue(reader.next());
        for (int j = 0; j < 5; j++) {
            assertTrue(aReader.next());
            assertEquals("value" + i + "." + j, strReader.getString());
        }
    }
    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) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) SelectionVector2(org.apache.drill.exec.record.selection.SelectionVector2) 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)

Example 15 with ExtendableRowSet

use of org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet in project drill by apache.

the class TestRowSet method testScalarStructure.

/**
 * Test the simplest constructs: a row with top-level scalar
 * columns.
 * <p>
 * The focus here is the structure of the readers and writers, along
 * with the row set loader and verifier that use those constructs.
 * That is, while this test uses the int vector, this test is not
 * focused on that vector.
 */
@Test
public void testScalarStructure() {
    final TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).buildSchema();
    final ExtendableRowSet rowSet = fixture.rowSet(schema);
    final RowSetWriter writer = rowSet.writer();
    // Required Int
    // Verify the invariants of the "full" and "simple" access paths
    assertEquals(ObjectType.SCALAR, writer.column("a").type());
    assertSame(writer.column("a"), writer.column(0));
    assertSame(writer.scalar("a"), writer.scalar(0));
    assertSame(writer.column("a").scalar(), writer.scalar("a"));
    assertSame(writer.column(0).scalar(), writer.scalar(0));
    assertEquals(ValueType.INTEGER, writer.scalar(0).valueType());
    try {
        writer.column(0).array();
        fail();
    } catch (final UnsupportedOperationException e) {
    // Expected
    }
    try {
        writer.column(0).tuple();
        fail();
    } catch (final UnsupportedOperationException e) {
    // Expected
    }
    // Test the various ways to get at the scalar writer.
    writer.column("a").scalar().setInt(10);
    writer.save();
    writer.scalar("a").setInt(20);
    writer.save();
    writer.column(0).scalar().setInt(30);
    writer.save();
    writer.scalar(0).setInt(40);
    writer.save();
    // Finish the row set and get a reader.
    final SingleRowSet actual = writer.done();
    final RowSetReader reader = actual.reader();
    // Verify invariants
    assertEquals(ObjectType.SCALAR, reader.column(0).type());
    assertSame(reader.column("a"), reader.column(0));
    assertSame(reader.scalar("a"), reader.scalar(0));
    assertSame(reader.column("a").scalar(), reader.scalar("a"));
    assertSame(reader.column(0).scalar(), reader.scalar(0));
    assertEquals(ValueType.INTEGER, reader.scalar(0).valueType());
    assertTrue(schema.metadata("a").isEquivalent(reader.column("a").schema()));
    // Test various accessors: full and simple
    assertTrue(reader.next());
    assertFalse(reader.column("a").scalar().isNull());
    assertEquals(10, reader.column("a").scalar().getInt());
    assertTrue(reader.next());
    assertFalse(reader.scalar("a").isNull());
    assertEquals(20, reader.scalar("a").getInt());
    assertTrue(reader.next());
    assertFalse(reader.column(0).scalar().isNull());
    assertEquals(30, reader.column(0).scalar().getInt());
    assertTrue(reader.next());
    assertFalse(reader.column(0).scalar().isNull());
    assertEquals(40, reader.scalar(0).getInt());
    assertFalse(reader.next());
    // Test the above again via the writer and reader
    // utility classes.
    final SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10).addRow(20).addRow(30).addRow(40).build();
    RowSetUtilities.verify(expected, actual);
}
Also used : 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) ExtendableRowSet(org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Aggregations

ExtendableRowSet (org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet)34 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)31 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)31 SubOperatorTest (org.apache.drill.test.SubOperatorTest)27 Test (org.junit.Test)27 SingleRowSet (org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet)24 ScalarReader (org.apache.drill.exec.vector.accessor.ScalarReader)19 ScalarWriter (org.apache.drill.exec.vector.accessor.ScalarWriter)19 ArrayReader (org.apache.drill.exec.vector.accessor.ArrayReader)11 ArrayWriter (org.apache.drill.exec.vector.accessor.ArrayWriter)7 ObjectReader (org.apache.drill.exec.vector.accessor.ObjectReader)6 ObjectWriter (org.apache.drill.exec.vector.accessor.ObjectWriter)6 TupleReader (org.apache.drill.exec.vector.accessor.TupleReader)5 TupleWriter (org.apache.drill.exec.vector.accessor.TupleWriter)5 VariantReader (org.apache.drill.exec.vector.accessor.VariantReader)5 VariantWriter (org.apache.drill.exec.vector.accessor.VariantWriter)5 MinorType (org.apache.drill.common.types.TypeProtos.MinorType)4 RowSetWriter (org.apache.drill.exec.physical.rowSet.RowSetWriter)4 ValueType (org.apache.drill.exec.vector.accessor.ValueType)4 VectorContainer (org.apache.drill.exec.record.VectorContainer)3