Search in sources :

Example 1 with DictVector

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

the class ColumnBuilder method buildSingleDict.

private ColumnState buildSingleDict(ContainerState parent, ColumnMetadata columnSchema) {
    final ProjectionFilter projFilter = parent.projection();
    final ProjResult projResult = projFilter.projection(columnSchema);
    // Create the dict's offset vector.
    final DictVector dictVector;
    final UInt4Vector offsetVector;
    if (projResult.isProjected) {
        // Creating the dict vector will create its contained vectors if we
        // give it a materialized field with children. So, instead pass a clone
        // without children so we can add them.
        final ColumnMetadata dictColMetadata = columnSchema.cloneEmpty();
        // vectors can be cached.
        assert columnSchema.tupleSchema().isEmpty();
        dictVector = new DictVector(dictColMetadata.schema(), parent.loader().allocator(), null);
        offsetVector = dictVector.getOffsetVector();
    } else {
        dictVector = null;
        offsetVector = null;
    }
    // Create the writer using the offset vector
    final AbstractObjectWriter writer = ObjectDictWriter.buildDict(columnSchema, dictVector, new ArrayList<>());
    // Wrap the offset vector in a vector state
    final VectorState offsetVectorState;
    if (!projResult.isProjected) {
        offsetVectorState = new NullVectorState();
    } else {
        offsetVectorState = new OffsetVectorState((((AbstractArrayWriter) writer.dict()).offsetWriter()), offsetVector, writer.dict().entry().events());
    }
    final VectorState mapVectorState = new TupleState.SingleDictVectorState(dictVector, offsetVectorState);
    // Assemble it all into the column state.
    final SingleDictState dictArrayState = new SingleDictState(parent.loader(), parent.vectorCache().childCache(columnSchema.name()), projResult.mapFilter);
    return new TupleState.DictColumnState(dictArrayState, writer, mapVectorState, parent.isVersioned());
}
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) PrimitiveColumnMetadata(org.apache.drill.exec.record.metadata.PrimitiveColumnMetadata) OffsetVectorState(org.apache.drill.exec.physical.resultSet.impl.SingleVectorState.OffsetVectorState) ProjResult(org.apache.drill.exec.physical.resultSet.impl.ProjectionFilter.ProjResult) UInt4Vector(org.apache.drill.exec.vector.UInt4Vector) SingleDictState(org.apache.drill.exec.physical.resultSet.impl.TupleState.SingleDictState) AbstractObjectWriter(org.apache.drill.exec.vector.accessor.writer.AbstractObjectWriter) MapVectorState(org.apache.drill.exec.physical.resultSet.impl.TupleState.MapVectorState) RepeatedListVectorState(org.apache.drill.exec.physical.resultSet.impl.RepeatedListState.RepeatedListVectorState) UnionVectorState(org.apache.drill.exec.physical.resultSet.impl.UnionState.UnionVectorState) OffsetVectorState(org.apache.drill.exec.physical.resultSet.impl.SingleVectorState.OffsetVectorState) ListVectorState(org.apache.drill.exec.physical.resultSet.impl.ListState.ListVectorState) SimpleVectorState(org.apache.drill.exec.physical.resultSet.impl.SingleVectorState.SimpleVectorState)

Example 2 with DictVector

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

the class SimpleReaderBuilder method buildDict.

private AbstractObjectReader buildDict(ValueVector vector, VectorAccessor va, VectorDescrip descrip) {
    boolean isArray = descrip.metadata.isArray();
    DictVector dictVector;
    VectorAccessor dictAccessor;
    if (isArray) {
        dictVector = (DictVector) ((RepeatedValueVector) vector).getDataVector();
        dictAccessor = new SingleVectorAccessor(dictVector);
    } else {
        dictVector = (DictVector) vector;
        dictAccessor = va;
    }
    List<AbstractObjectReader> readers = buildMapMembers(dictVector, descrip.childProvider());
    AbstractObjectReader reader = DictReaderImpl.build(descrip.metadata, dictAccessor, readers);
    if (!isArray) {
        return reader;
    }
    return ArrayReaderImpl.buildTuple(descrip.metadata, va, reader);
}
Also used : DictVector(org.apache.drill.exec.vector.complex.DictVector) AbstractObjectReader(org.apache.drill.exec.vector.accessor.reader.AbstractObjectReader) RepeatedValueVector(org.apache.drill.exec.vector.complex.RepeatedValueVector) SingleVectorAccessor(org.apache.drill.exec.vector.accessor.reader.VectorAccessors.SingleVectorAccessor) VectorAccessor(org.apache.drill.exec.vector.accessor.reader.VectorAccessor) SingleVectorAccessor(org.apache.drill.exec.vector.accessor.reader.VectorAccessors.SingleVectorAccessor)

Example 3 with DictVector

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

the class TestDictVector method testVectorCreation.

@Test
public void testVectorCreation() {
    MaterializedField field = MaterializedField.create("map", DictVector.TYPE);
    try (DictVector mapVector = new DictVector(field, allocator, null)) {
        mapVector.allocateNew();
        List<Map<Object, Object>> maps = Arrays.asList(TestBuilder.mapOfObject(4f, 1L, 5.3f, 2L, 0.3f, 3L, -0.2f, 4L, 102.07f, 5L), TestBuilder.mapOfObject(45f, 6L, 9.2f, 7L), TestBuilder.mapOfObject(4.01f, 8L, 9.2f, 9L, -2.3f, 10L), TestBuilder.mapOfObject(), TestBuilder.mapOfObject(11f, 11L, 9.73f, 12L, 0.03f, 13L));
        BaseWriter.DictWriter mapWriter = new SingleDictWriter(mapVector, null);
        int index = 0;
        for (Map<Object, Object> map : maps) {
            mapWriter.setPosition(index++);
            mapWriter.start();
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                mapWriter.startKeyValuePair();
                mapWriter.float4(DictVector.FIELD_KEY_NAME).writeFloat4((float) entry.getKey());
                mapWriter.bigInt(DictVector.FIELD_VALUE_NAME).writeBigInt((long) entry.getValue());
                mapWriter.endKeyValuePair();
            }
            mapWriter.end();
        }
        BaseReader.DictReader mapReader = mapVector.getReader();
        index = 0;
        for (Map<Object, Object> map : maps) {
            mapReader.setPosition(index++);
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                mapReader.next();
                Float actualKey = mapReader.reader(DictVector.FIELD_KEY_NAME).readFloat();
                Long actualValue = mapReader.reader(DictVector.FIELD_VALUE_NAME).readLong();
                assertEquals(entry.getKey(), actualKey);
                assertEquals(entry.getValue(), actualValue);
            }
        }
    }
}
Also used : DictVector(org.apache.drill.exec.vector.complex.DictVector) BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) MaterializedField(org.apache.drill.exec.record.MaterializedField) BaseReader(org.apache.drill.exec.vector.complex.reader.BaseReader) SingleDictWriter(org.apache.drill.exec.vector.complex.impl.SingleDictWriter) Map(java.util.Map) ExecTest(org.apache.drill.exec.ExecTest) Test(org.junit.Test) VectorTest(org.apache.drill.categories.VectorTest)

Example 4 with DictVector

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

the class TestDictVector method testVectorCreationWithNullValues.

@Test
public void testVectorCreationWithNullValues() {
    MaterializedField field = MaterializedField.create("map", DictVector.TYPE);
    try (DictVector mapVector = new DictVector(field, allocator, null)) {
        mapVector.allocateNew();
        List<Map<Object, Object>> maps = Arrays.asList(TestBuilder.mapOfObject(4f, 1L, 5.3f, 2L, 0.3f, null, -0.2f, 4L, 102.07f, 5L), TestBuilder.mapOfObject(45f, 6L, 9.2f, 7L), TestBuilder.mapOfObject(4.01f, null, 9.2f, 9L, -2.3f, 10L), TestBuilder.mapOfObject(), TestBuilder.mapOfObject(11f, 11L, 9.73f, null, 0.03f, 13L));
        BaseWriter.DictWriter mapWriter = new SingleDictWriter(mapVector, null);
        int index = 0;
        for (Map<Object, Object> map : maps) {
            mapWriter.setPosition(index++);
            mapWriter.start();
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                mapWriter.startKeyValuePair();
                mapWriter.float4(DictVector.FIELD_KEY_NAME).writeFloat4((float) entry.getKey());
                Long value = (Long) entry.getValue();
                if (value != null) {
                    mapWriter.bigInt(DictVector.FIELD_VALUE_NAME).writeBigInt(value);
                }
                // else skip writing a value. Notice that index was incremented
                mapWriter.endKeyValuePair();
            }
            mapWriter.end();
        }
        BaseReader.DictReader mapReader = mapVector.getReader();
        index = 0;
        for (Map<Object, Object> map : maps) {
            mapReader.setPosition(index++);
            assertEquals(map.size(), mapReader.size());
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                mapReader.next();
                Float actualKey = mapReader.reader(DictVector.FIELD_KEY_NAME).readFloat();
                Long actualValue = mapReader.reader(DictVector.FIELD_VALUE_NAME).readLong();
                assertEquals(entry.getKey(), actualKey);
                assertEquals(entry.getValue(), actualValue);
            }
        }
    }
}
Also used : DictVector(org.apache.drill.exec.vector.complex.DictVector) BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) MaterializedField(org.apache.drill.exec.record.MaterializedField) BaseReader(org.apache.drill.exec.vector.complex.reader.BaseReader) SingleDictWriter(org.apache.drill.exec.vector.complex.impl.SingleDictWriter) Map(java.util.Map) ExecTest(org.apache.drill.exec.ExecTest) Test(org.junit.Test) VectorTest(org.apache.drill.categories.VectorTest)

Example 5 with DictVector

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

the class TestDictVector method testVectorCreationWithNullKeys.

@Test
public void testVectorCreationWithNullKeys() {
    thrown.expect(DrillRuntimeException.class);
    thrown.expectMessage(containsString("Key in DICT cannot be null. Index: 1"));
    MaterializedField field = MaterializedField.create("map", DictVector.TYPE);
    try (DictVector mapVector = new DictVector(field, allocator, null)) {
        mapVector.allocateNew();
        List<Map<Object, Object>> maps = Arrays.asList(TestBuilder.mapOfObject(4f, 1L, 5.3f, 2L, 1.23f, 3L, -0.2f, 4L, 102.07f, 5L), // this map contains null key which is not supported
        TestBuilder.mapOfObject(45f, 6L, null, 7L), TestBuilder.mapOfObject(4.01f, 8L, 9.2f, 9L, -2.3f, 10L), TestBuilder.mapOfObject(), TestBuilder.mapOfObject(11f, 11L, 9.73f, 12L, 0.03f, 13L));
        BaseWriter.DictWriter mapWriter = new SingleDictWriter(mapVector, null);
        int index = 0;
        for (Map<Object, Object> map : maps) {
            mapWriter.setPosition(index++);
            mapWriter.start();
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                mapWriter.startKeyValuePair();
                if (entry.getKey() != null) {
                    mapWriter.float4(DictVector.FIELD_KEY_NAME).writeFloat4((float) entry.getKey());
                }
                // else skip writing a key. Notice that index was incremented.
                // Whether the key is written will be checked in endKeyValuePair()
                // and an Exception will be thrown in case it was not set (i.e. it is null).
                mapWriter.bigInt(DictVector.FIELD_VALUE_NAME).writeBigInt((Long) entry.getValue());
                mapWriter.endKeyValuePair();
            }
            mapWriter.end();
        }
    }
}
Also used : DictVector(org.apache.drill.exec.vector.complex.DictVector) BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) SingleDictWriter(org.apache.drill.exec.vector.complex.impl.SingleDictWriter) MaterializedField(org.apache.drill.exec.record.MaterializedField) Map(java.util.Map) ExecTest(org.apache.drill.exec.ExecTest) Test(org.junit.Test) VectorTest(org.apache.drill.categories.VectorTest)

Aggregations

DictVector (org.apache.drill.exec.vector.complex.DictVector)19 Test (org.junit.Test)13 MaterializedField (org.apache.drill.exec.record.MaterializedField)10 Map (java.util.Map)8 VectorTest (org.apache.drill.categories.VectorTest)8 ExecTest (org.apache.drill.exec.ExecTest)8 RepeatedDictVector (org.apache.drill.exec.vector.complex.RepeatedDictVector)8 SingleDictWriter (org.apache.drill.exec.vector.complex.impl.SingleDictWriter)8 BaseWriter (org.apache.drill.exec.vector.complex.writer.BaseWriter)8 BaseReader (org.apache.drill.exec.vector.complex.reader.BaseReader)6 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 DictWriter (org.apache.drill.exec.vector.accessor.DictWriter)5 SubOperatorTest (org.apache.drill.test.SubOperatorTest)5 ColumnMetadata (org.apache.drill.exec.record.metadata.ColumnMetadata)4 ResultSetLoader (org.apache.drill.exec.physical.resultSet.ResultSetLoader)3 RowSetLoader (org.apache.drill.exec.physical.resultSet.RowSetLoader)3 RowSet (org.apache.drill.exec.physical.rowSet.RowSet)3 ScalarWriter (org.apache.drill.exec.vector.accessor.ScalarWriter)3