Search in sources :

Example 11 with RepeatedMapVector

use of org.apache.drill.exec.vector.complex.RepeatedMapVector in project drill by apache.

the class UnnestRecordBatch method getUnnestFieldTransferPair.

/**
 * The data layout is the same for the actual data within a repeated field, as
 * it is in a scalar vector for the same sql type. For example, a repeated int
 * vector has a vector of offsets into a regular int vector to represent the
 * lists. As the data layout for the actual values in the same in the repeated
 * vector as in the scalar vector of the same type, we can avoid making
 * individual copies for the column being unnested, and just use vector copies
 * between the inner vector of the repeated field to the resulting scalar
 * vector from the unnest operation. This is completed after we determine how
 * many records will fit (as we will hit either a batch end, or the end of one
 * of the other vectors while we are copying the data of the other vectors
 * alongside each new unnested value coming out of the repeated field.)
 */
private TransferPair getUnnestFieldTransferPair(FieldReference reference) {
    int[] typeFieldIds = unnestTypedFieldId.getFieldIds();
    Class<?> vectorClass = incoming.getSchema().getColumn(typeFieldIds[0]).getValueClass();
    ValueVector unnestField = incoming.getValueAccessorById(vectorClass, typeFieldIds).getValueVector();
    TransferPair tp = null;
    if (unnestField instanceof RepeatedMapVector) {
        tp = ((RepeatedMapVector) unnestField).getTransferPairToSingleMap(reference.getAsNamePart().getName(), oContext.getAllocator());
    } else if (!(unnestField instanceof RepeatedValueVector)) {
        if (incoming.getRecordCount() != 0) {
            throw UserException.unsupportedError().message("Unnest does not support inputs of non-list values.").build(logger);
        }
        logger.error("Cannot cast {} to RepeatedValueVector", unnestField);
        // when incoming recordCount is 0, don't throw exception since the type being seen here is not solid
        ValueVector vv = new RepeatedMapVector(unnestField.getField(), oContext.getAllocator(), null);
        tp = RepeatedValueVector.class.cast(vv).getTransferPair(reference.getAsNamePart().getName(), oContext.getAllocator());
    } else {
        ValueVector vvIn = RepeatedValueVector.class.cast(unnestField).getDataVector();
        // vvIn may be null because of fast schema return for repeated list vectors
        if (vvIn != null) {
            tp = vvIn.getTransferPair(reference.getAsNamePart().getName(), oContext.getAllocator());
        }
    }
    return tp;
}
Also used : RepeatedValueVector(org.apache.drill.exec.vector.complex.RepeatedValueVector) ValueVector(org.apache.drill.exec.vector.ValueVector) TransferPair(org.apache.drill.exec.record.TransferPair) RepeatedMapVector(org.apache.drill.exec.vector.complex.RepeatedMapVector) RepeatedValueVector(org.apache.drill.exec.vector.complex.RepeatedValueVector)

Example 12 with RepeatedMapVector

use of org.apache.drill.exec.vector.complex.RepeatedMapVector in project drill by apache.

the class TestMapAccessors method testBasicRepeatedMap.

@Test
public void testBasicRepeatedMap() {
    TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMapArray("m").add("c", MinorType.INT).add("d", MinorType.VARCHAR).resumeSchema().buildSchema();
    RowSetBuilder builder = fixture.rowSetBuilder(schema);
    RowSetWriter rootWriter = builder.writer();
    // Verify structure and schema
    TupleMetadata actualSchema = rootWriter.tupleSchema();
    assertEquals(2, actualSchema.size());
    assertTrue(actualSchema.metadata(1).isArray());
    assertTrue(actualSchema.metadata(1).isMap());
    assertEquals(2, actualSchema.metadata("m").tupleSchema().size());
    assertEquals(2, actualSchema.column("m").getChildren().size());
    TupleWriter mapWriter = rootWriter.array("m").tuple();
    assertSame(actualSchema.metadata("m").tupleSchema(), mapWriter.schema().tupleSchema());
    assertSame(mapWriter.tupleSchema(), mapWriter.schema().tupleSchema());
    assertSame(mapWriter.tupleSchema().metadata(0), mapWriter.scalar(0).schema());
    assertSame(mapWriter.tupleSchema().metadata(1), mapWriter.scalar(1).schema());
    // Write a couple of rows with arrays.
    rootWriter.addRow(10, mapArray(mapValue(110, "d1.1"), mapValue(120, "d2.2"))).addRow(20, mapArray()).addRow(30, mapArray(mapValue(310, "d3.1"), mapValue(320, "d3.2"), mapValue(330, "d3.3")));
    // Verify the first batch
    RowSet actual = builder.build();
    RepeatedMapVector mapVector = (RepeatedMapVector) actual.container().getValueVector(1).getValueVector();
    MaterializedField mapField = mapVector.getField();
    assertEquals(2, mapField.getChildren().size());
    Iterator<MaterializedField> iter = mapField.getChildren().iterator();
    assertTrue(mapWriter.scalar(0).schema().schema().isEquivalent(iter.next()));
    assertTrue(mapWriter.scalar(1).schema().schema().isEquivalent(iter.next()));
    SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, mapArray(mapValue(110, "d1.1"), mapValue(120, "d2.2"))).addRow(20, mapArray()).addRow(30, mapArray(mapValue(310, "d3.1"), mapValue(320, "d3.2"), mapValue(330, "d3.3"))).build();
    new RowSetComparison(expected).verify(actual);
    // Test that the row set rebuilds its internal structure from
    // a vector container.
    RowSet wrapped = fixture.wrap(actual.container());
    RowSetUtilities.verify(expected, wrapped);
}
Also used : SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSetComparison(org.apache.drill.test.rowSet.RowSetComparison) TupleWriter(org.apache.drill.exec.vector.accessor.TupleWriter) RepeatedMapVector(org.apache.drill.exec.vector.complex.RepeatedMapVector) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) MaterializedField(org.apache.drill.exec.record.MaterializedField) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 13 with RepeatedMapVector

use of org.apache.drill.exec.vector.complex.RepeatedMapVector in project drill by apache.

the class VectorTest method buildFromMap.

private RepeatedMapVector buildFromMap() {
    RepeatedMapVector v = buildRepeatedMap();
    // Can't figure out how to get the indexes to step for
    // inner and outer values.
    // If we get the int writer from the repeated map writer,
    // the column will be converted to nullable int.
    IntVector iv = getInner(v);
    IntVector.Mutator im = iv.getMutator();
    UInt4Vector ov = v.getOffsetVector();
    UInt4Vector.Mutator om = ov.getMutator();
    om.set(0, 0);
    // Record 1: [10, 20]
    im.set(0, 10);
    im.set(1, 20);
    om.set(1, 2);
    // Record 2: [30, 40, 50]
    im.set(2, 30);
    im.set(3, 40);
    im.set(4, 50);
    om.set(2, 5);
    // Record 3: []
    om.set(3, 5);
    om.setValueCount(4);
    v.getMutator().setValueCount(3);
    // Sanity check
    validateFrom(v);
    return v;
}
Also used : RepeatedMapVector(org.apache.drill.exec.vector.complex.RepeatedMapVector)

Example 14 with RepeatedMapVector

use of org.apache.drill.exec.vector.complex.RepeatedMapVector in project drill by axbaretto.

the class TestRecordBatchSizer method testSizerRepeatedMap.

@Test
public void testSizerRepeatedMap() {
    BatchSchema schema = new SchemaBuilder().addMapArray("map").add("key", MinorType.INT).add("value", MinorType.VARCHAR).resumeSchema().build();
    RowSetBuilder builder = fixture.rowSetBuilder(schema);
    for (int i = 0; i < 10; i++) {
        builder.addRow((Object) new Object[] { new Object[] { 110, "a" }, new Object[] { 120, "b" } });
    }
    RowSet rows = builder.build();
    // Run the record batch sizer on the resulting batch.
    RecordBatchSizer sizer = new RecordBatchSizer(rows.container());
    assertEquals(1, sizer.columns().size());
    /**
     * stdDataSize:50+4, stdNetSize:50+4+4+4, dataSizePerEntry:(4+1)*2,
     * netSizePerEntry: 4*2+1*2+4*2+4,
     * totalDataSize:5*2*10, totalNetSize:netSizePerEntry*2,
     * valueCount:10,
     * elementCount:20, estElementCountPerArray:2, isVariableWidth:true
     */
    verifyColumnValues(sizer.columns().get("map"), 54, 62, 10, 22, 100, 220, 10, 20, 2, false);
    SingleRowSet empty = fixture.rowSet(schema);
    VectorAccessible accessible = empty.vectorAccessible();
    UInt4Vector offsetVector;
    for (VectorWrapper<?> vw : accessible) {
        ValueVector v = vw.getValueVector();
        RecordBatchSizer.ColumnSize colSize = sizer.getColumn(v.getField().getName());
        // Allocates to nearest power of two
        colSize.allocateVector(v, testRowCount);
        RepeatedMapVector mapVector = (RepeatedMapVector) v;
        offsetVector = ((RepeatedValueVector) mapVector).getOffsetVector();
        assertEquals((Integer.highestOneBit(testRowCount) << 1), offsetVector.getValueCapacity());
        ValueVector keyVector = mapVector.getChild("key");
        ValueVector valueVector1 = mapVector.getChild("value");
        assertEquals(((Integer.highestOneBit(testRowCount) << 1) * 2), keyVector.getValueCapacity());
        offsetVector = ((VariableWidthVector) valueVector1).getOffsetVector();
        assertEquals((Integer.highestOneBit(testRowCount) << 1) * 2, offsetVector.getValueCapacity());
        assertEquals(Integer.highestOneBit(testRowCount << 1) * 2 - 1, valueVector1.getValueCapacity());
        // Allocates the same as value passed since it is already power of two.
        colSize.allocateVector(v, testRowCountPowerTwo - 1);
        mapVector = (RepeatedMapVector) v;
        offsetVector = ((RepeatedValueVector) mapVector).getOffsetVector();
        assertEquals(testRowCountPowerTwo, offsetVector.getValueCapacity());
        keyVector = mapVector.getChild("key");
        valueVector1 = mapVector.getChild("value");
        assertEquals(testRowCountPowerTwo * 2, keyVector.getValueCapacity());
        offsetVector = ((VariableWidthVector) valueVector1).getOffsetVector();
        assertEquals(testRowCountPowerTwo * 2, offsetVector.getValueCapacity());
        assertEquals(testRowCountPowerTwo * 2 - 1, valueVector1.getValueCapacity());
        // Allocate for max rows.
        colSize.allocateVector(v, ValueVector.MAX_ROW_COUNT - 1);
        mapVector = (RepeatedMapVector) v;
        offsetVector = ((RepeatedValueVector) mapVector).getOffsetVector();
        assertEquals(ValueVector.MAX_ROW_COUNT, offsetVector.getValueCapacity());
        keyVector = mapVector.getChild("key");
        valueVector1 = mapVector.getChild("value");
        assertEquals(ValueVector.MAX_ROW_COUNT * 2, keyVector.getValueCapacity());
        offsetVector = ((VariableWidthVector) valueVector1).getOffsetVector();
        assertEquals(ValueVector.MAX_ROW_COUNT * 2, offsetVector.getValueCapacity());
        assertEquals(ValueVector.MAX_ROW_COUNT * 2 - 1, valueVector1.getValueCapacity());
        // Allocate for 0 rows. should atleast do allocation for 1 row.
        colSize.allocateVector(v, 0);
        mapVector = (RepeatedMapVector) v;
        offsetVector = ((RepeatedValueVector) mapVector).getOffsetVector();
        assertEquals(ValueVector.MIN_ROW_COUNT, offsetVector.getValueCapacity());
        keyVector = mapVector.getChild("key");
        valueVector1 = mapVector.getChild("value");
        assertEquals(ValueVector.MIN_ROW_COUNT, keyVector.getValueCapacity());
        offsetVector = ((VariableWidthVector) valueVector1).getOffsetVector();
        assertEquals(ValueVector.MIN_ROW_COUNT + 1, offsetVector.getValueCapacity());
        assertEquals(ValueVector.MIN_ROW_COUNT, valueVector1.getValueCapacity());
        v.clear();
    }
    empty.clear();
    rows.clear();
}
Also used : SingleRowSet(org.apache.drill.test.rowSet.RowSet.SingleRowSet) ColumnSize(org.apache.drill.exec.record.RecordBatchSizer.ColumnSize) RepeatedMapVector(org.apache.drill.exec.vector.complex.RepeatedMapVector) SingleRowSet(org.apache.drill.test.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.test.rowSet.RowSet) UInt4Vector(org.apache.drill.exec.vector.UInt4Vector) ValueVector(org.apache.drill.exec.vector.ValueVector) RepeatedValueVector(org.apache.drill.exec.vector.complex.RepeatedValueVector) RowSetBuilder(org.apache.drill.test.rowSet.RowSetBuilder) SchemaBuilder(org.apache.drill.test.rowSet.schema.SchemaBuilder) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 15 with RepeatedMapVector

use of org.apache.drill.exec.vector.complex.RepeatedMapVector in project drill by apache.

the class VectorTest method testRepeatedMapWriter.

@Test
public void testRepeatedMapWriter() throws Exception {
    // The writers will create a nullable int inner vector.
    MaterializedField field = MaterializedField.create("repeated_map", Types.repeated(TypeProtos.MinorType.MAP));
    try (RepeatedMapVector v = new RepeatedMapVector(field, allocator, null)) {
        SpecialMapVector mapVector = new SpecialMapVector(v);
        @SuppressWarnings("resource") SingleMapWriter mapRoot = new SingleMapWriter(mapVector, null, false);
        ListWriter lw = mapRoot.list("repeated_map");
        MapWriter mw = lw.map();
        // Record 1: [10, 20]
        lw.setPosition(0);
        lw.startList();
        mw.start();
        IntWriter iw = mw.integer("inner");
        iw.writeInt(10);
        mw.end();
        mw.start();
        iw.writeInt(20);
        mw.end();
        lw.endList();
        // Record 2: [30, 40, 50]
        lw.setPosition(1);
        lw.startList();
        mw.start();
        iw.writeInt(30);
        mw.end();
        mw.start();
        iw.writeInt(40);
        mw.end();
        mw.start();
        iw.writeInt(50);
        mw.end();
        lw.endList();
        // Record 3: []
        lw.setPosition(2);
        lw.startList();
        lw.endList();
        v.getMutator().setValueCount(3);
        assertEquals(3, v.getAccessor().getValueCount());
        UInt4Vector.Accessor oa = v.getOffsetVector().getAccessor();
        assertEquals(4, oa.getValueCount());
        assertEquals(0, oa.get(0));
        assertEquals(2, oa.get(1));
        assertEquals(5, oa.get(2));
        assertEquals(5, oa.get(3));
        // Past end
        assertEquals(0, oa.get(4));
        NullableIntVector inner = v.addOrGet("inner", Types.optional(TypeProtos.MinorType.INT), NullableIntVector.class);
        UInt1Vector.Accessor ba = inner.getBitsVector().getAccessor();
        assertEquals(1, ba.get(0));
        assertEquals(1, ba.get(1));
        assertEquals(1, ba.get(2));
        assertEquals(1, ba.get(3));
        assertEquals(1, ba.get(4));
        // Past end
        assertEquals(0, ba.get(5));
        NullableIntVector.Accessor ia = inner.getAccessor();
        assertEquals(10, ia.get(0));
        assertEquals(20, ia.get(1));
        assertEquals(30, ia.get(2));
        assertEquals(40, ia.get(3));
        assertEquals(50, ia.get(4));
    }
}
Also used : SingleMapWriter(org.apache.drill.exec.vector.complex.impl.SingleMapWriter) MapWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.MapWriter) SingleMapWriter(org.apache.drill.exec.vector.complex.impl.SingleMapWriter) RepeatedMapVector(org.apache.drill.exec.vector.complex.RepeatedMapVector) IntWriter(org.apache.drill.exec.vector.complex.writer.IntWriter) MaterializedField(org.apache.drill.exec.record.MaterializedField) ListWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter) Test(org.junit.Test) BaseTest(org.apache.drill.test.BaseTest)

Aggregations

RepeatedMapVector (org.apache.drill.exec.vector.complex.RepeatedMapVector)22 ValueVector (org.apache.drill.exec.vector.ValueVector)11 Test (org.junit.Test)10 MaterializedField (org.apache.drill.exec.record.MaterializedField)9 RepeatedValueVector (org.apache.drill.exec.vector.complex.RepeatedValueVector)9 UInt4Vector (org.apache.drill.exec.vector.UInt4Vector)7 SubOperatorTest (org.apache.drill.test.SubOperatorTest)7 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)6 SingleRowSet (org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet)5 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)5 TransferPair (org.apache.drill.exec.record.TransferPair)4 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)4 TupleWriter (org.apache.drill.exec.vector.accessor.TupleWriter)4 RowSet (org.apache.drill.exec.physical.rowSet.RowSet)3 ColumnSize (org.apache.drill.exec.record.RecordBatchSizer.ColumnSize)3 MapVector (org.apache.drill.exec.vector.complex.MapVector)3 BaseTest (org.apache.drill.test.BaseTest)3 RowSetBuilder (org.apache.drill.exec.physical.rowSet.RowSetBuilder)2 ColumnMetadata (org.apache.drill.exec.record.metadata.ColumnMetadata)2 BaseValueVector (org.apache.drill.exec.vector.BaseValueVector)2