Search in sources :

Example 1 with RepeatedDictVector

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

the class BaseWriterBuilder method buildDict.

private AbstractObjectWriter buildDict(ValueVector vector, VectorDescrip descrip) {
    if (vector.getField().getType().getMode() == DataMode.REPEATED) {
        ValueVector dataVector = ((RepeatedDictVector) vector).getDataVector();
        List<AbstractObjectWriter> writers = buildMap((AbstractMapVector) dataVector, descrip);
        return ObjectDictWriter.buildDictArray(descrip.metadata, (RepeatedDictVector) vector, writers);
    } else {
        List<AbstractObjectWriter> writers = buildMap((AbstractMapVector) vector, descrip);
        return ObjectDictWriter.buildDict(descrip.metadata, (DictVector) vector, writers);
    }
}
Also used : ValueVector(org.apache.drill.exec.vector.ValueVector) RepeatedDictVector(org.apache.drill.exec.vector.complex.RepeatedDictVector) AbstractObjectWriter(org.apache.drill.exec.vector.accessor.writer.AbstractObjectWriter)

Example 2 with RepeatedDictVector

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

the class ObjectDictWriter method buildDictArray.

public static ArrayObjectWriter buildDictArray(ColumnMetadata metadata, RepeatedDictVector vector, List<AbstractObjectWriter> keyValueWriters) {
    final DictVector dataVector;
    if (vector != null) {
        dataVector = (DictVector) vector.getDataVector();
    } else {
        dataVector = null;
    }
    ObjectDictWriter.DictObjectWriter dictWriter = buildDict(metadata, dataVector, keyValueWriters);
    AbstractArrayWriter arrayWriter;
    if (vector != null) {
        arrayWriter = new ObjectArrayWriter(metadata, vector.getOffsetVector(), dictWriter);
    } else {
        arrayWriter = new DummyArrayWriter(metadata, dictWriter);
    }
    return new ArrayObjectWriter(arrayWriter);
}
Also used : DictVector(org.apache.drill.exec.vector.complex.DictVector) RepeatedDictVector(org.apache.drill.exec.vector.complex.RepeatedDictVector) DummyArrayWriter(org.apache.drill.exec.vector.accessor.writer.dummy.DummyArrayWriter)

Example 3 with RepeatedDictVector

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

the class TestResultSetLoaderDictArray method testBasics.

@Test
public void testBasics() {
    TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addDictArray("d", MinorType.INT).value(MinorType.VARCHAR).resumeSchema().buildSchema();
    ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).build();
    ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
    RowSetLoader rootWriter = rsLoader.writer();
    // Verify structure and schema
    TupleMetadata actualSchema = rootWriter.tupleSchema();
    assertEquals(2, actualSchema.size());
    assertTrue(actualSchema.metadata(1).isArray());
    assertTrue(actualSchema.metadata(1).isDict());
    assertEquals(2, actualSchema.metadata("d").tupleSchema().size());
    assertEquals(2, actualSchema.column("d").getChildren().size());
    DictWriter dictWriter = rootWriter.array("d").dict();
    assertSame(actualSchema.metadata("d").tupleSchema(), dictWriter.schema().tupleSchema());
    // Write a couple of rows with arrays.
    rsLoader.startBatch();
    rootWriter.addRow(10, objArray(map(110, "d1.1", 111, "d1.2", 112, "d1.3"), map(120, "d2.2"))).addRow(20, objArray()).addRow(30, objArray(map(310, "d3.1", 311, "d3.2", 313, "d3.4", 317, "d3.9"), map(320, "d4.2"), map(332, "d5.1", 339, "d5.5", 337, "d5.6")));
    // Verify the batch
    RowSet actual = fixture.wrap(rsLoader.harvest());
    RepeatedDictVector repeatedDictVector = (RepeatedDictVector) actual.container().getValueVector(1).getValueVector();
    // RepeatedDictVector contains one child - DictVector
    MaterializedField dictArrayField = repeatedDictVector.getField();
    assertEquals(1, dictArrayField.getChildren().size());
    DictVector dictVector = (DictVector) repeatedDictVector.getDataVector();
    Iterator<MaterializedField> iter = dictVector.getField().getChildren().iterator();
    assertTrue(dictWriter.keyWriter().schema().schema().isEquivalent(iter.next()));
    assertTrue(dictWriter.valueWriter().scalar().schema().schema().isEquivalent(iter.next()));
    SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, objArray(map(110, "d1.1", 111, "d1.2", 112, "d1.3"), map(120, "d2.2"))).addRow(20, objArray()).addRow(30, objArray(map(310, "d3.1", 311, "d3.2", 313, "d3.4", 317, "d3.9"), map(320, "d4.2"), map(332, "d5.1", 339, "d5.5", 337, "d5.6"))).build();
    RowSetUtilities.verify(expected, actual);
    rsLoader.close();
}
Also used : DictVector(org.apache.drill.exec.vector.complex.DictVector) RepeatedDictVector(org.apache.drill.exec.vector.complex.RepeatedDictVector) DictWriter(org.apache.drill.exec.vector.accessor.DictWriter) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) MaterializedField(org.apache.drill.exec.record.MaterializedField) ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) RepeatedDictVector(org.apache.drill.exec.vector.complex.RepeatedDictVector) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 4 with RepeatedDictVector

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

the class TestRowSet method testRepeatedDictStructure.

@Test
public void testRepeatedDictStructure() {
    final String dictName = "d";
    final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addDictArray(dictName, MinorType.FLOAT8).value(MinorType.VARCHAR).resumeSchema().buildSchema();
    final ExtendableRowSet rowSet = fixture.rowSet(schema);
    final RowSetWriter writer = rowSet.writer();
    // Repeated dict
    assertEquals(ObjectType.ARRAY, writer.column(dictName).type());
    final ScalarWriter idWriter = writer.scalar(0);
    final ArrayWriter dictArrayWriter = writer.column(1).array();
    assertEquals(ObjectType.ARRAY, dictArrayWriter.entryType());
    DictWriter dictWriter = dictArrayWriter.dict();
    assertEquals(ValueType.DOUBLE, dictWriter.keyType());
    assertEquals(ObjectType.SCALAR, dictWriter.valueType());
    final ScalarWriter keyWriter = dictWriter.keyWriter();
    final ScalarWriter valueWriter = dictWriter.valueWriter().scalar();
    assertEquals(ValueType.DOUBLE, keyWriter.valueType());
    assertEquals(ValueType.STRING, valueWriter.valueType());
    // Write data
    idWriter.setInt(1);
    keyWriter.setDouble(1);
    valueWriter.setString("a");
    // advance to next entry position
    dictWriter.save();
    keyWriter.setDouble(2);
    valueWriter.setString("b");
    dictWriter.save();
    // advance to next array position
    dictArrayWriter.save();
    keyWriter.setDouble(3);
    valueWriter.setString("c");
    dictWriter.save();
    dictArrayWriter.save();
    // advance to next row
    writer.save();
    idWriter.setInt(2);
    keyWriter.setDouble(11);
    valueWriter.setString("d");
    dictWriter.save();
    keyWriter.setDouble(12);
    valueWriter.setString("e");
    dictWriter.save();
    dictArrayWriter.save();
    writer.save();
    idWriter.setInt(3);
    keyWriter.setDouble(21);
    valueWriter.setString("f");
    dictWriter.save();
    keyWriter.setDouble(22);
    valueWriter.setString("g");
    dictWriter.save();
    keyWriter.setDouble(23);
    valueWriter.setString("h");
    dictWriter.save();
    dictArrayWriter.save();
    keyWriter.setDouble(24);
    valueWriter.setString("i");
    dictWriter.save();
    keyWriter.setDouble(25);
    valueWriter.setString("j");
    dictWriter.save();
    keyWriter.setDouble(26.5);
    valueWriter.setString("k");
    dictWriter.save();
    keyWriter.setDouble(27);
    valueWriter.setString("l");
    dictWriter.save();
    keyWriter.setDouble(28);
    valueWriter.setString("m");
    dictWriter.save();
    dictArrayWriter.save();
    writer.save();
    // Finish the row set and get a reader.
    final SingleRowSet actual = writer.done();
    final RowSetReader reader = actual.reader();
    // Verify reader structure
    assertEquals(ObjectType.ARRAY, reader.column(dictName).type());
    final ArrayReader dictArrayReader = reader.array(1);
    assertEquals(ObjectType.ARRAY, dictArrayReader.entryType());
    final DictReader dictReader = dictArrayReader.entry().dict();
    assertEquals(ValueType.DOUBLE, dictReader.keyColumnType());
    assertEquals(ObjectType.SCALAR, dictReader.valueColumnType());
    final KeyAccessor keyAccessor = dictReader.keyAccessor();
    final ScalarReader valueReader = dictReader.valueReader().scalar();
    // Row 1
    assertTrue(reader.next());
    // array is not null
    assertFalse(dictArrayReader.isNull());
    assertTrue(dictArrayReader.next());
    // first dict is not null
    assertFalse(dictArrayReader.isNull());
    assertTrue(keyAccessor.find(2.0f));
    assertEquals("b", valueReader.getObject());
    assertTrue(keyAccessor.find(1.0f));
    assertEquals("a", valueReader.getObject());
    // no entry for given key
    assertFalse(keyAccessor.find(1.1f));
    assertTrue(dictArrayReader.next());
    assertTrue(keyAccessor.find(3.0f));
    assertEquals("c", valueReader.getObject());
    assertFalse(keyAccessor.find(1.0f));
    assertEquals(Arrays.asList(map(1.0, "a", 2.0, "b"), map(3.0, "c")), dictArrayReader.getObject());
    // Row 2
    assertTrue(reader.next());
    assertTrue(dictArrayReader.next());
    assertTrue(keyAccessor.find(11.0f));
    assertEquals("d", valueReader.getString());
    assertFalse(keyAccessor.find(1.0f));
    assertTrue(keyAccessor.find(12.0f));
    assertEquals("e", valueReader.getString());
    // Row 3: use explicit positioning
    assertTrue(reader.next());
    dictArrayReader.setPosn(1);
    assertTrue(keyAccessor.find(24.0f));
    assertEquals("i", valueReader.getString());
    assertTrue(keyAccessor.find(26.5f));
    assertEquals("k", valueReader.getString());
    assertTrue(keyAccessor.find(28.0f));
    assertEquals("m", valueReader.getString());
    assertFalse(keyAccessor.find(35.0f));
    assertTrue(keyAccessor.find(27.0f));
    assertEquals("l", valueReader.getString());
    Map<Object, Object> element1 = map(24.0, "i", 25.0, "j", 26.5, "k", 27.0, "l", 28.0, "m");
    assertEquals(element1, dictReader.getObject());
    dictArrayReader.setPosn(0);
    assertTrue(keyAccessor.find(23.0f));
    assertEquals("h", valueReader.getObject());
    assertTrue(keyAccessor.find(21.0f));
    assertEquals("f", valueReader.getObject());
    assertFalse(keyAccessor.find(23.05f));
    Map<Object, Object> element0 = map(21.0, "f", 22.0, "g", 23.0, "h");
    assertEquals(element0, dictReader.getObject());
    assertEquals(Arrays.asList(element0, element1), dictArrayReader.getObject());
    assertFalse(reader.next());
    // Verify that the dict accessor's value count was set.
    final RepeatedDictVector vector = (RepeatedDictVector) actual.container().getValueVector(1).getValueVector();
    assertEquals(3, vector.getAccessor().getValueCount());
    final SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(1, objArray(map(1.0f, "a", 2.0f, "b"), map(3.0f, "c"))).addRow(2, objArray(singleObjArray(map(11.0f, "d", 12.0f, "e")))).addRow(3, objArray(map(21.0f, "f", 22.0f, "g", 23.0f, "h"), map(24.0f, "i", 25.0f, "j", 26.5f, "k", 27.0f, "l", 28.0f, "m"))).build();
    RowSetUtilities.verify(expected, actual);
}
Also used : DictWriter(org.apache.drill.exec.vector.accessor.DictWriter) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) ScalarReader(org.apache.drill.exec.vector.accessor.ScalarReader) ArrayReader(org.apache.drill.exec.vector.accessor.ArrayReader) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) KeyAccessor(org.apache.drill.exec.vector.accessor.KeyAccessor) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter) DictReader(org.apache.drill.exec.vector.accessor.DictReader) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) RepeatedDictVector(org.apache.drill.exec.vector.complex.RepeatedDictVector) ExtendableRowSet(org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 5 with RepeatedDictVector

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

the class SingleSchemaInference method inferDictSchema.

private TupleSchema inferDictSchema(ValueVector vector) {
    final List<ColumnMetadata> columns = new ArrayList<>();
    DictVector dictVector;
    if (vector.getField().getType().getMode() == DataMode.REPEATED) {
        dictVector = (DictVector) ((RepeatedDictVector) vector).getDataVector();
    } else {
        dictVector = (DictVector) vector;
    }
    for (int i = 0; i < dictVector.size(); i++) {
        columns.add(inferVector(dictVector.getChildByOrdinal(i)));
    }
    return MetadataUtils.fromColumns(columns);
}
Also used : DictVector(org.apache.drill.exec.vector.complex.DictVector) RepeatedDictVector(org.apache.drill.exec.vector.complex.RepeatedDictVector) ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) ArrayList(java.util.ArrayList) RepeatedDictVector(org.apache.drill.exec.vector.complex.RepeatedDictVector)

Aggregations

RepeatedDictVector (org.apache.drill.exec.vector.complex.RepeatedDictVector)8 DictVector (org.apache.drill.exec.vector.complex.DictVector)5 ColumnMetadata (org.apache.drill.exec.record.metadata.ColumnMetadata)3 SingleRowSet (org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet)2 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)2 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)2 DictWriter (org.apache.drill.exec.vector.accessor.DictWriter)2 AbstractObjectWriter (org.apache.drill.exec.vector.accessor.writer.AbstractObjectWriter)2 SubOperatorTest (org.apache.drill.test.SubOperatorTest)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 ResultSetLoader (org.apache.drill.exec.physical.resultSet.ResultSetLoader)1 RowSetLoader (org.apache.drill.exec.physical.resultSet.RowSetLoader)1 ListVectorState (org.apache.drill.exec.physical.resultSet.impl.ListState.ListVectorState)1 ProjResult (org.apache.drill.exec.physical.resultSet.impl.ProjectionFilter.ProjResult)1 RepeatedListVectorState (org.apache.drill.exec.physical.resultSet.impl.RepeatedListState.RepeatedListVectorState)1 OffsetVectorState (org.apache.drill.exec.physical.resultSet.impl.SingleVectorState.OffsetVectorState)1 SimpleVectorState (org.apache.drill.exec.physical.resultSet.impl.SingleVectorState.SimpleVectorState)1 MapVectorState (org.apache.drill.exec.physical.resultSet.impl.TupleState.MapVectorState)1 UnionVectorState (org.apache.drill.exec.physical.resultSet.impl.UnionState.UnionVectorState)1