Search in sources :

Example 6 with ObjectWriter

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

use of org.apache.drill.exec.vector.accessor.ObjectWriter 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 8 with ObjectWriter

use of org.apache.drill.exec.vector.accessor.ObjectWriter 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 9 with ObjectWriter

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

the class BaseFieldFactory method buildOuterArrays.

/**
 * Create layers of repeated list listeners around the type-specific
 * array. If the JSON has three array levels, the outer two are repeated
 * lists, the inner is type-specific: say an array of {@code BIGINT} or
 * a map array.
 */
public ElementParser buildOuterArrays(ObjectWriter writer, int dims, Function<ObjectWriter, ElementParser> innerCreator) {
    ObjectWriter[] writers = new ObjectWriter[dims];
    writers[0] = writer;
    for (int i = 1; i < dims; i++) {
        writers[i] = writers[i - 1].array().entry();
    }
    ElementParser prevElementParser = innerCreator.apply(writers[dims - 1]);
    for (int i = dims - 2; i >= 0; i--) {
        prevElementParser = parserFactory().arrayValueParser(new StructureArrayListener(writers[i].array()), prevElementParser);
    }
    return prevElementParser;
}
Also used : StructureArrayListener(org.apache.drill.exec.store.easy.json.loader.SimpleArrayListener.StructureArrayListener) ObjectWriter(org.apache.drill.exec.vector.accessor.ObjectWriter) ElementParser(org.apache.drill.exec.store.easy.json.parser.ElementParser)

Example 10 with ObjectWriter

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

the class ProvidedFieldFactory method variantParserForSchema.

private ElementParser variantParserForSchema(FieldDefn fieldDefn) {
    // A variant can contain multiple types. The schema does not
    // declare the types; rather they are discovered by the reader.
    // That is, there is no VARIANT<INT, DOUBLE>, there is just VARIANT.
    ColumnMetadata colSchema = fieldDefn.providedColumn().cloneEmpty();
    ObjectWriter fieldWriter = fieldDefn.fieldWriterFor(colSchema);
    if (colSchema.isArray()) {
        return variantArrayParserFor(fieldWriter.array());
    } else {
        return variantParserFor(fieldWriter.variant());
    }
}
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